diff --git a/src/MultiDiskAdaptor.cc b/src/MultiDiskAdaptor.cc index 069ff2a3..1d43cb85 100644 --- a/src/MultiDiskAdaptor.cc +++ b/src/MultiDiskAdaptor.cc @@ -124,7 +124,10 @@ MultiDiskAdaptor::MultiDiskAdaptor() readOnly_{false} {} -MultiDiskAdaptor::~MultiDiskAdaptor() {} +MultiDiskAdaptor::~MultiDiskAdaptor() +{ + closeFile(); +} namespace { std::unique_ptr createDiskWriterEntry @@ -267,10 +270,15 @@ void MultiDiskAdaptor::openExistingFile() void MultiDiskAdaptor::closeFile() { + size_t n = 0; openedDiskWriterEntries_.clear(); for(auto& dwent : diskWriterEntries_) { - dwent->closeFile(); + if(dwent->isOpen()) { + ++n; + dwent->closeFile(); + } } + getRequestGroupMan()->reduceNumOfOpenedFile(n); } namespace { diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index 4c04f21a..2e182fca 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -985,4 +985,10 @@ void RequestGroupMan::ensureMaxOpenFileLimit(size_t numNewFile) numOpenFile_ += numNewFile - numClose; } +void RequestGroupMan::reduceNumOfOpenedFile(size_t numCloseFile) +{ + assert(numOpenFile_ >= numCloseFile); + numOpenFile_ -= numCloseFile; +} + } // namespace aria2 diff --git a/src/RequestGroupMan.h b/src/RequestGroupMan.h index 0cfc527c..87f594a0 100644 --- a/src/RequestGroupMan.h +++ b/src/RequestGroupMan.h @@ -357,6 +357,8 @@ public: // Currently the only download using MultiDiskAdaptor is affected by // the global limit. void ensureMaxOpenFileLimit(size_t numNewFile); + // Reduces the number of open files managed by this object. + void reduceNumOfOpenedFile(size_t numCloseFile); }; } // namespace aria2