Przeglądaj źródła

mingw32: Re-open files with read-only mode enabled on seeding

On Mingw32 build, if aria2 opens file with GENERIC_WRITE access right,
some programs cannot open the file aria2 is seeding. To avoid this
situation, re-open files with read-only enabled when seeding is about
to begin.
Tatsuhiro Tsujikawa 12 lat temu
rodzic
commit
c09a5a95f4
2 zmienionych plików z 28 dodań i 0 usunięć
  1. 17 0
      src/BtFileAllocationEntry.cc
  2. 11 0
      src/DefaultPieceStorage.cc

+ 17 - 0
src/BtFileAllocationEntry.cc

@@ -46,6 +46,7 @@
 #include "DiskAdaptor.h"
 #include "Option.h"
 #include "prefs.h"
+#include "LogFactory.h"
 
 namespace aria2 {
 
@@ -72,6 +73,22 @@ void BtFileAllocationEntry::prepareForNextAction
        (fileEntries.begin(), fileEntries.end())) {
       getRequestGroup()->createNextCommandWithAdj(commands, e, 0);
     }
+  } else {
+#ifdef __MINGW32__
+    const SharedHandle<DiskAdaptor>& diskAdaptor =
+      getRequestGroup()->getPieceStorage()->getDiskAdaptor();
+    if(!diskAdaptor->isReadOnlyEnabled()) {
+      // On Windows, if aria2 opens files with GENERIC_WRITE access
+      // right, some programs cannot open them aria2 is seeding. To
+      // avoid this situation, re-open the files with read-only
+      // enabled.
+      A2_LOG_INFO("Closing files and re-open them with read-only mode"
+                  " enabled.");
+      diskAdaptor->closeFile();
+      diskAdaptor->enableReadOnly();
+      diskAdaptor->openFile();
+    }
+#endif // __MINGW32__
   }
 }
 

+ 11 - 0
src/DefaultPieceStorage.cc

@@ -480,6 +480,17 @@ void DefaultPieceStorage::completePiece(const SharedHandle<Piece>& piece)
       SharedHandle<TorrentAttribute> torrentAttrs =
         bittorrent::getTorrentAttrs(downloadContext_);
       if(!torrentAttrs->metadata.empty()) {
+#ifdef __MINGW32__
+        // On Windows, if aria2 opens files with GENERIC_WRITE access
+        // right, some programs cannot open them aria2 is seeding. To
+        // avoid this situation, re-open the files with read-only
+        // enabled.
+        A2_LOG_INFO("Closing files and re-open them with read-only mode"
+                    " enabled.");
+        diskAdaptor_->closeFile();
+        diskAdaptor_->enableReadOnly();
+        diskAdaptor_->openFile();
+#endif // __MINGW32__
         util::executeHookByOptName(downloadContext_->getOwnerRequestGroup(),
                                    option_, PREF_ON_BT_DOWNLOAD_COMPLETE);
         SingletonHolder<Notifier>::instance()->