flush internal buffers before auto-saving control file

Otherwise, some pieces may be marked as finished in the control file
though they have not yet been written to the storage file.

This should prevent data loss and corruption when resuming downloads
after an app crash.

Signed-off-by: Ali MJ Al-Nasrawy <alimjalnasrawy@gmail.com>
This commit is contained in:
Ali MJ Al-Nasrawy 2020-06-17 00:08:55 +03:00
parent 9d0a48ac81
commit 870e2a6014
6 changed files with 14 additions and 8 deletions

View file

@ -685,7 +685,7 @@ std::shared_ptr<DiskAdaptor> DefaultPieceStorage::getDiskAdaptor()
WrDiskCache* DefaultPieceStorage::getWrDiskCache() { return wrDiskCache_; } WrDiskCache* DefaultPieceStorage::getWrDiskCache() { return wrDiskCache_; }
void DefaultPieceStorage::flushWrDiskCacheEntry() void DefaultPieceStorage::flushWrDiskCacheEntry(bool releaseEntries)
{ {
if (!wrDiskCache_) { if (!wrDiskCache_) {
return; return;
@ -697,10 +697,12 @@ void DefaultPieceStorage::flushWrDiskCacheEntry()
auto ce = piece->getWrDiskCacheEntry(); auto ce = piece->getWrDiskCacheEntry();
if (ce) { if (ce) {
piece->flushWrCache(wrDiskCache_); piece->flushWrCache(wrDiskCache_);
if (releaseEntries) {
piece->releaseWrCache(wrDiskCache_); piece->releaseWrCache(wrDiskCache_);
} }
} }
} }
}
int32_t DefaultPieceStorage::getPieceLength(size_t index) int32_t DefaultPieceStorage::getPieceLength(size_t index)
{ {

View file

@ -234,7 +234,7 @@ public:
virtual WrDiskCache* getWrDiskCache() CXX11_OVERRIDE; virtual WrDiskCache* getWrDiskCache() CXX11_OVERRIDE;
virtual void flushWrDiskCacheEntry() CXX11_OVERRIDE; virtual void flushWrDiskCacheEntry(bool releaseEntries) CXX11_OVERRIDE;
virtual int32_t getPieceLength(size_t index) CXX11_OVERRIDE; virtual int32_t getPieceLength(size_t index) CXX11_OVERRIDE;

View file

@ -226,8 +226,9 @@ public:
virtual WrDiskCache* getWrDiskCache() = 0; virtual WrDiskCache* getWrDiskCache() = 0;
// Flushes write disk cache for in-flight piece and evicts them. // Flushes write disk cache for in-flight piece
virtual void flushWrDiskCacheEntry() = 0; // and optionally releases the associated cache entries.
virtual void flushWrDiskCacheEntry(bool releaseEntries) = 0;
virtual int32_t getPieceLength(size_t index) = 0; virtual int32_t getPieceLength(size_t index) = 0;

View file

@ -210,7 +210,7 @@ std::pair<error_code::Value, std::string> RequestGroup::downloadResult() const
void RequestGroup::closeFile() void RequestGroup::closeFile()
{ {
if (pieceStorage_) { if (pieceStorage_) {
pieceStorage_->flushWrDiskCacheEntry(); pieceStorage_->flushWrDiskCacheEntry(true);
pieceStorage_->getDiskAdaptor()->closeFile(); pieceStorage_->getDiskAdaptor()->closeFile();
} }
} }
@ -1290,6 +1290,9 @@ bool RequestGroup::doesUploadSpeedExceed()
void RequestGroup::saveControlFile() const void RequestGroup::saveControlFile() const
{ {
if (saveControlFile_) { if (saveControlFile_) {
if (pieceStorage_) {
pieceStorage_->flushWrDiskCacheEntry(false);
}
progressInfoFile_->save(); progressInfoFile_->save();
} }
} }

View file

@ -216,7 +216,7 @@ public:
virtual WrDiskCache* getWrDiskCache() CXX11_OVERRIDE { return nullptr; } virtual WrDiskCache* getWrDiskCache() CXX11_OVERRIDE { return nullptr; }
virtual void flushWrDiskCacheEntry() CXX11_OVERRIDE {} virtual void flushWrDiskCacheEntry(bool releaseEntries) CXX11_OVERRIDE {}
virtual int32_t getPieceLength(size_t index) CXX11_OVERRIDE; virtual int32_t getPieceLength(size_t index) CXX11_OVERRIDE;

View file

@ -228,7 +228,7 @@ public:
virtual WrDiskCache* getWrDiskCache() CXX11_OVERRIDE { return 0; } virtual WrDiskCache* getWrDiskCache() CXX11_OVERRIDE { return 0; }
virtual void flushWrDiskCacheEntry() CXX11_OVERRIDE {} virtual void flushWrDiskCacheEntry(bool releaseEntries) CXX11_OVERRIDE {}
void setDiskAdaptor(const std::shared_ptr<DiskAdaptor>& adaptor) void setDiskAdaptor(const std::shared_ptr<DiskAdaptor>& adaptor)
{ {