fs::rename fixed

Added overwriting policy
sys_fs_rename improved
This commit is contained in:
Nekotekina 2017-08-30 17:14:30 +03:00
parent da3f3fd1fb
commit 010b75251f
5 changed files with 44 additions and 8 deletions

View file

@ -510,7 +510,7 @@ bool fs::remove_dir(const std::string& path)
#endif
}
bool fs::rename(const std::string& from, const std::string& to)
bool fs::rename(const std::string& from, const std::string& to, bool overwrite)
{
const auto device = get_virtual_device(from);
@ -525,14 +525,43 @@ bool fs::rename(const std::string& from, const std::string& to)
}
#ifdef _WIN32
if (!MoveFileW(to_wchar(from).get(), to_wchar(to).get()))
const auto ws1 = to_wchar(from);
const auto ws2 = to_wchar(to);
if (!MoveFileExW(ws1.get(), ws2.get(), overwrite ? MOVEFILE_REPLACE_EXISTING : 0))
{
g_tls_error = to_error(GetLastError());
DWORD error1 = GetLastError();
if (overwrite && error1 == ERROR_ACCESS_DENIED && is_dir(from) && is_dir(to))
{
if (RemoveDirectoryW(ws2.get()))
{
if (MoveFileW(ws1.get(), ws2.get()))
{
return true;
}
error1 = GetLastError();
CreateDirectoryW(ws2.get(), NULL); // TODO
}
else
{
error1 = GetLastError();
}
}
g_tls_error = to_error(error1);
return false;
}
return true;
#else
if (!overwrite && exists(to))
{
g_tls_error = fs::error::exist;
return false;
}
if (::rename(from.c_str(), to.c_str()) != 0)
{
g_tls_error = to_error(errno);