MCPcopy Create free account
hub / github.com/bcndev/bytecoin / atomic_replace_file

Function atomic_replace_file

src/platform/PathTools.cpp:209–225  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

207}
208
209bool atomic_replace_file(const std::string &from_path, const std::string &to_path) {
210#if defined(_WIN32)
211 auto wfrom_path = FileStream::utf8_to_utf16(expand_path(from_path));
212 auto wto_path = FileStream::utf8_to_utf16(expand_path(to_path));
213 // Maximizing chances for success
214 DWORD attributes = GetFileAttributesW(wto_path.c_str());
215 if (INVALID_FILE_ATTRIBUTES != attributes)
216 SetFileAttributesW(wto_path.c_str(), attributes & (~FILE_ATTRIBUTE_READONLY));
217 bool ok = MoveFileExW(wfrom_path.c_str(), wto_path.c_str(), MOVEFILE_REPLACE_EXISTING) != 0;
218// int code = ok ? 0 : static_cast<int>(::GetLastError());
219#else
220 bool ok = std::rename(expand_path(from_path).c_str(), expand_path(to_path).c_str()) == 0;
221// int code = ok ? 0 : errno;
222#endif
223 // if(err) *err = std::error_code(code, std::system_category());
224 return ok;
225}
226bool copy_file(const std::string &from_path, const std::string &to_path) {
227 platform::FileStream from(from_path, platform::O_READ_EXISTING);
228 platform::FileStream to(to_path, platform::O_CREATE_ALWAYS);

Callers 3

do_log_stringMethod · 0.85
atomic_save_fileFunction · 0.85
save_and_checkMethod · 0.85

Calls 1

expand_pathFunction · 0.85

Tested by

no test coverage detected