OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
fastmove.cpp
Go to the documentation of this file.
1#include "fastmove.h"
2#include <system_error>
3
4using namespace std;
5using namespace std::filesystem;
6
7namespace Utils
8{
9 void move_file(const path& source, const path& dest, bool overwriteExisting, std::error_code& ec)
10 {
11 auto options = copy_options::none;
12 if (overwriteExisting)
13 {
14 options |= copy_options::overwrite_existing;
15 }
16
17 if (copy_file(source, dest, options, ec) &&
18 ec.value() == 0)
19 {
20 remove(source, ec);
21 }
22 }
23
24 void fastmove_file(const path& source, const path& dest, bool overwriteExisting, std::error_code& ec)
25 {
26 // Security-audit F002: POSIX rename() silently replaces an existing
27 // destination, which made the overwriteExisting=false contract a no-op
28 // - callers asking for no-overwrite (design upload) were overwriting
29 // stored files anyway. Honor the flag: refuse when the destination
30 // exists unless overwrite was explicitly requested. Note the check is
31 // not atomic with rename(): two concurrent no-overwrite moves of the
32 // same name race to last-writer-wins (still contained to the caller's
33 // destination directory, so this is a correctness nicety, not a
34 // containment hole).
35 ec.clear();
36 if (!overwriteExisting)
37 {
38 std::error_code existsEc;
39 const bool destExists = exists(dest, existsEc);
40 if (existsEc)
41 {
42 ec = existsEc;
43 return;
44 }
45 if (destExists)
46 {
47 ec = std::make_error_code(std::errc::file_exists);
48 return;
49 }
50 }
51
52 rename(source, dest, ec);
53 if (ec.value() == static_cast<int>(std::errc::cross_device_link))
54 {
55 move_file(source, dest, overwriteExisting, ec);
56 }
57 }
58}
void move_file(const path &source, const path &dest, bool overwriteExisting, std::error_code &ec)
Definition fastmove.cpp:9
void fastmove_file(const path &source, const path &dest, bool overwriteExisting, std::error_code &ec)
Definition fastmove.cpp:24