mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-10 07:40:03 -06:00
cmSystemTools: Factor out RenameFile wstring conversion on Windows
This commit is contained in:
@@ -898,28 +898,29 @@ bool cmSystemTools::RenameFile(const std::string& oldname,
|
||||
# ifndef INVALID_FILE_ATTRIBUTES
|
||||
# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
|
||||
# endif
|
||||
std::wstring const oldname_wstr =
|
||||
SystemTools::ConvertToWindowsExtendedPath(oldname);
|
||||
std::wstring const newname_wstr =
|
||||
SystemTools::ConvertToWindowsExtendedPath(newname);
|
||||
|
||||
/* Windows MoveFileEx may not replace read-only or in-use files. If it
|
||||
fails then remove the read-only attribute from any existing destination.
|
||||
Try multiple times since we may be racing against another process
|
||||
creating/opening the destination file just before our MoveFileEx. */
|
||||
WindowsFileRetry retry = cmSystemTools::GetWindowsFileRetry();
|
||||
while (!cmMoveFile(SystemTools::ConvertToWindowsExtendedPath(oldname),
|
||||
SystemTools::ConvertToWindowsExtendedPath(newname)) &&
|
||||
--retry.Count) {
|
||||
while (!cmMoveFile(oldname_wstr, newname_wstr) && --retry.Count) {
|
||||
DWORD last_error = GetLastError();
|
||||
// Try again only if failure was due to access/sharing permissions.
|
||||
if (last_error != ERROR_ACCESS_DENIED &&
|
||||
last_error != ERROR_SHARING_VIOLATION) {
|
||||
return false;
|
||||
}
|
||||
DWORD attrs = GetFileAttributesW(
|
||||
SystemTools::ConvertToWindowsExtendedPath(newname).c_str());
|
||||
DWORD const attrs = GetFileAttributesW(newname_wstr.c_str());
|
||||
if ((attrs != INVALID_FILE_ATTRIBUTES) &&
|
||||
(attrs & FILE_ATTRIBUTE_READONLY)) {
|
||||
// Remove the read-only attribute from the destination file.
|
||||
SetFileAttributesW(
|
||||
SystemTools::ConvertToWindowsExtendedPath(newname).c_str(),
|
||||
attrs & ~FILE_ATTRIBUTE_READONLY);
|
||||
SetFileAttributesW(newname_wstr.c_str(),
|
||||
attrs & ~FILE_ATTRIBUTE_READONLY);
|
||||
} else {
|
||||
// The file may be temporarily in use so wait a bit.
|
||||
cmSystemTools::Delay(retry.Delay);
|
||||
|
||||
Reference in New Issue
Block a user