diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2013-11-08 00:59:57 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2013-11-08 00:59:57 +0900 |
| commit | e1e6bb1ec5a813c7e86343d4fdb451d18ac048f0 (patch) | |
| tree | c6e60d6fb2af96f065d8f909257d7e2febd5b998 /src/RequestGroup.cc | |
| parent | 2aa8e01836da65fca6b19843610ec438eeca4b4a (diff) | |
Don't fail multiple concurrent dl same file if auto-file-renaming is enabled
Diffstat (limited to 'src/RequestGroup.cc')
| -rw-r--r-- | src/RequestGroup.cc | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc index 53d9ab62..b7332e03 100644 --- a/src/RequestGroup.cc +++ b/src/RequestGroup.cc @@ -465,11 +465,6 @@ void RequestGroup::createInitialCommand( createNextCommand(commands, e, 1); return; } - if (e->getRequestGroupMan()->isSameFileBeingDownloaded(this)) { - throw DOWNLOAD_FAILURE_EXCEPTION2(fmt(EX_DUPLICATE_FILE_DOWNLOAD, - downloadContext_->getBasePath().c_str()), - error_code::DUPLICATE_DOWNLOAD); - } auto progressInfoFile = std::make_shared<DefaultBtProgressInfoFile>( downloadContext_, nullptr, @@ -652,6 +647,15 @@ void RequestGroup::adjustFilename( // OK, no need to care about filename. return; } + // TODO need this? + if(requestGroupMan_) { + if(requestGroupMan_->isSameFileBeingDownloaded(this)) { + // The file name must be renamed + tryAutoFileRenaming(); + A2_LOG_NOTICE(fmt(MSG_FILE_RENAMED, getFirstFilePath().c_str())); + return; + } + } if (!option_->getAsBool(PREF_DRY_RUN) && option_->getAsBool(PREF_REMOVE_CONTROL_FILE) && infoFile->exists()) { @@ -743,37 +747,36 @@ void RequestGroup::shouldCancelDownloadForSafety() return; } + tryAutoFileRenaming(); + A2_LOG_NOTICE(fmt(MSG_FILE_RENAMED, getFirstFilePath().c_str())); +} + +void RequestGroup::tryAutoFileRenaming() +{ if (!option_->getAsBool(PREF_AUTO_FILE_RENAMING)) { throw DOWNLOAD_FAILURE_EXCEPTION2(fmt(MSG_FILE_ALREADY_EXISTS, getFirstFilePath().c_str()), error_code::FILE_ALREADY_EXISTS); } - if (!tryAutoFileRenaming()) { + std::string filepath = getFirstFilePath(); + if(filepath.empty()) { throw DOWNLOAD_FAILURE_EXCEPTION2(fmt("File renaming failed: %s", getFirstFilePath().c_str()), error_code::FILE_RENAMING_FAILED); } - - A2_LOG_NOTICE(fmt(MSG_FILE_RENAMED, getFirstFilePath().c_str())); -} - -bool RequestGroup::tryAutoFileRenaming() -{ - std::string filepath = getFirstFilePath(); - if(filepath.empty()) { - return false; - } for (int i = 1; i < 10000; ++i) { auto newfilename = fmt("%s.%d", filepath.c_str(), i); File newfile(newfilename); File ctrlfile(newfile.getPath() + DefaultBtProgressInfoFile::getSuffix()); if (!newfile.exists() || (newfile.exists() && ctrlfile.exists())) { downloadContext_->getFirstFileEntry()->setPath(newfile.getPath()); - return true; + return; } } - return false; + throw DOWNLOAD_FAILURE_EXCEPTION2(fmt("File renaming failed: %s", + getFirstFilePath().c_str()), + error_code::FILE_RENAMING_FAILED); } void RequestGroup::createNextCommandWithAdj( |
