mingw: Use MoveFileExW for better atomic move

pull/291/head
Tatsuhiro Tsujikawa 2014-10-10 23:49:53 +09:00
parent 24e3822a10
commit 09d7956537
2 changed files with 12 additions and 10 deletions

View File

@ -233,19 +233,24 @@ bool File::isDir(const std::string& filename)
bool File::renameTo(const std::string& dest)
{
#ifdef __MINGW32__
/* MinGW's rename() doesn't delete an existing destination */
if (_waccess(utf8ToWChar(dest).c_str(), 0) == 0) {
if (a2unlink(utf8ToWChar(dest).c_str()) != 0) {
return false;
}
// MinGW's rename() doesn't delete an existing destination. Better
// to use MoveFileEx, which usually provides atomic move in aria2
// usecase.
if(MoveFileExW(utf8ToWChar(name_).c_str(), utf8ToWChar(dest).c_str(),
MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING)) {
name_ = dest;
return true;
}
#endif // __MINGW32__
if(a2rename(utf8ToWChar(name_).c_str(), utf8ToWChar(dest).c_str()) == 0) {
return false;
#else // !__MINGW32__
if(rename(name_.c_str(), dest.c_str()) == 0) {
name_ = dest;
return true;
} else {
return false;
}
#endif // !__MINGW32__
}
bool File::utime(const Time& actime, const Time& modtime) const

View File

@ -137,7 +137,6 @@
# define a2utime(path, times) _wutime(path, times)
# define a2unlink(path) _wunlink(path)
# define a2rmdir(path) _wrmdir(path)
# define a2rename(src, dest) _wrename(src, dest)
// For Windows, we share files for reading and writing.
# define a2open(path, flags, mode) _wsopen(path, flags, _SH_DENYNO, mode)
# define a2fopen(path, mode) _wfsopen(path, mode, _SH_DENYNO)
@ -155,7 +154,6 @@
# define a2utime(path, times) ::utime(path, times)
# define a2unlink(path) unlink(path)
# define a2rmdir(path) rmdir(path)
# define a2rename(src, dest) rename(src, dest)
# define a2open(path, flags, mode) open(path, flags, mode)
# define a2fopen(path, mode) fopen(path, mode)
// Android NDK R8e does not provide ftruncate64 prototype, so let's
@ -183,7 +181,6 @@ extern int ftruncate64(int fd, off64_t length);
# define a2utime(path, times) ::utime(path, times)
# define a2unlink(path) unlink(path)
# define a2rmdir(path) rmdir(path)
# define a2rename(src, dest) rename(src, dest)
# define a2open(path, flags, mode) open(path, flags, mode)
# define a2fopen(path, mode) fopen(path, mode)
# define a2ftruncate(fd, length) ftruncate(fd, length)