Skip to content

Commit

Permalink
Win32/File.cpp: Use ReplaceFile for hardlink overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
elad335 committed Oct 31, 2024
1 parent a4a1d62 commit b52aed0
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions Utilities/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2490,7 +2490,8 @@ bool fs::pending_file::commit(bool overwrite)

const auto ws1 = to_wchar(m_path);

const HANDLE file_handle = CreateFileW(ws1.get(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
const HANDLE file_handle = !overwrite ? INVALID_HANDLE_VALUE
: CreateFileW(ws1.get(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);

while (file_handle != INVALID_HANDLE_VALUE)
{
Expand Down Expand Up @@ -2551,21 +2552,32 @@ bool fs::pending_file::commit(bool overwrite)
CloseHandle(file_handle);
break;
}

if (!hardlink_paths.empty())
{
// REPLACEFILE_WRITE_THROUGH is not supported
file.sync();
}
#endif

file.close();

#ifdef _WIN32
const auto ws2 = to_wchar(m_dest);

if (MoveFileExW(ws1.get(), ws2.get(), overwrite ? MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH : MOVEFILE_WRITE_THROUGH))
bool ok = false;

if (hardlink_paths.empty())
{
for (auto&& path : hardlink_paths)
{
// Recreate hard links
CreateHardLinkW(path.c_str(), ws2.get(), NULL);
}
ok = MoveFileExW(ws1.get(), ws2.get(), overwrite ? MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH : MOVEFILE_WRITE_THROUGH);
}
else
{
ok = ReplaceFileW(ws1.get(), ws2.get(), nullptr, 0, nullptr, nullptr);
}

if (ok)
{
// Disable the destructor
m_path.clear();
return true;
Expand Down

0 comments on commit b52aed0

Please sign in to comment.