Procházet zdrojové kódy

mingw: Use MoveFileExW for better atomic move

Tatsuhiro Tsujikawa před 11 roky
rodič
revize
09d7956537
2 změnil soubory, kde provedl 12 přidání a 10 odebrání
  1. 12 7
      src/File.cc
  2. 0 3
      src/a2io.h

+ 12 - 7
src/File.cc

@@ -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

+ 0 - 3
src/a2io.h

@@ -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)