Cleanup RequestGroup code a bit

pull/128/merge
Nils Maier 2013-08-25 21:00:37 +02:00
parent 3590077d5c
commit 29d569eef9
2 changed files with 469 additions and 472 deletions

View File

@ -173,38 +173,37 @@ bool RequestGroup::downloadFinished() const
{ {
if(!pieceStorage_) { if(!pieceStorage_) {
return false; return false;
} else {
return pieceStorage_->downloadFinished();
} }
return pieceStorage_->downloadFinished();
} }
bool RequestGroup::allDownloadFinished() const bool RequestGroup::allDownloadFinished() const
{ {
if(!pieceStorage_) { if(!pieceStorage_) {
return false; return false;
} else {
return pieceStorage_->allDownloadFinished();
} }
return pieceStorage_->allDownloadFinished();
} }
error_code::Value RequestGroup::downloadResult() const error_code::Value RequestGroup::downloadResult() const
{ {
if(downloadFinished() && !downloadContext_->isChecksumVerificationNeeded()) if(downloadFinished() && !downloadContext_->isChecksumVerificationNeeded()) {
return error_code::FINISHED; return error_code::FINISHED;
else { }
if(haltReason_ == RequestGroup::USER_REQUEST) { if(haltReason_ == RequestGroup::USER_REQUEST) {
return error_code::REMOVED; return error_code::REMOVED;
} else if(lastErrorCode_ == error_code::UNDEFINED) { }
if(lastErrorCode_ == error_code::UNDEFINED) {
if(haltReason_ == RequestGroup::SHUTDOWN_SIGNAL) { if(haltReason_ == RequestGroup::SHUTDOWN_SIGNAL) {
return error_code::IN_PROGRESS; return error_code::IN_PROGRESS;
} else { }
return error_code::UNKNOWN_ERROR; return error_code::UNKNOWN_ERROR;
} }
} else {
return lastErrorCode_; return lastErrorCode_;
} }
}
}
void RequestGroup::closeFile() void RequestGroup::closeFile()
{ {
@ -220,16 +219,19 @@ std::unique_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
{ {
auto infoFile = std::make_shared<DefaultBtProgressInfoFile> auto infoFile = std::make_shared<DefaultBtProgressInfoFile>
(downloadContext_, pieceStorage_, option_.get()); (downloadContext_, pieceStorage_, option_.get());
if(option_->getAsBool(PREF_CHECK_INTEGRITY) && if(option_->getAsBool(PREF_CHECK_INTEGRITY) &&
downloadContext_->isPieceHashVerificationAvailable()) { downloadContext_->isPieceHashVerificationAvailable()) {
// When checking piece hash, we don't care file is downloaded and // When checking piece hash, we don't care file is downloaded and
// infoFile exists. // infoFile exists.
loadAndOpenFile(infoFile); loadAndOpenFile(infoFile);
return make_unique<StreamCheckIntegrityEntry>(this); return make_unique<StreamCheckIntegrityEntry>(this);
} else if(isPreLocalFileCheckEnabled() && }
if (isPreLocalFileCheckEnabled() &&
(infoFile->exists() || (infoFile->exists() ||
(File(getFirstFilePath()).exists() && (File(getFirstFilePath()).exists() && option_->getAsBool(PREF_CONTINUE))
option_->getAsBool(PREF_CONTINUE)))) { )) {
// If infoFile exists or -c option is given, we need to check // If infoFile exists or -c option is given, we need to check
// download has been completed (which is determined after // download has been completed (which is determined after
// loadAndOpenFile()). If so, use ChecksumCheckIntegrityEntry when // loadAndOpenFile()). If so, use ChecksumCheckIntegrityEntry when
@ -243,19 +245,17 @@ std::unique_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
auto tempEntry = make_unique<ChecksumCheckIntegrityEntry>(this); auto tempEntry = make_unique<ChecksumCheckIntegrityEntry>(this);
tempEntry->setRedownload(true); tempEntry->setRedownload(true);
return std::move(tempEntry); return std::move(tempEntry);
} else }
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
{
downloadContext_->setChecksumVerified(true); downloadContext_->setChecksumVerified(true);
A2_LOG_NOTICE(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED, A2_LOG_NOTICE(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
gid_->toHex().c_str(), gid_->toHex().c_str(),
downloadContext_->getBasePath().c_str())); downloadContext_->getBasePath().c_str()));
return nullptr; return nullptr;
} }
} else {
return make_unique<StreamCheckIntegrityEntry>(this); return make_unique<StreamCheckIntegrityEntry>(this);
} }
} else
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
if (downloadFinishedByFileLength() && if (downloadFinishedByFileLength() &&
downloadContext_->isChecksumVerificationAvailable()) { downloadContext_->isChecksumVerificationAvailable()) {
@ -264,16 +264,15 @@ std::unique_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
auto tempEntry = make_unique<ChecksumCheckIntegrityEntry>(this); auto tempEntry = make_unique<ChecksumCheckIntegrityEntry>(this);
tempEntry->setRedownload(true); tempEntry->setRedownload(true);
return std::move(tempEntry); return std::move(tempEntry);
} else }
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
{
loadAndOpenFile(infoFile); loadAndOpenFile(infoFile);
return make_unique<StreamCheckIntegrityEntry>(this); return make_unique<StreamCheckIntegrityEntry>(this);
} }
}
void RequestGroup::createInitialCommand void RequestGroup::createInitialCommand(
(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e) std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e)
{ {
// Start session timer here. When file size becomes known, it will // Start session timer here. When file size becomes known, it will
// be reset again in *FileAllocationEntry, because hash check and // be reset again in *FileAllocationEntry, because hash check and
@ -281,73 +280,69 @@ void RequestGroup::createInitialCommand
// is unknown, session timer will not be reset. // is unknown, session timer will not be reset.
downloadContext_->resetDownloadStartTime(); downloadContext_->resetDownloadStartTime();
#ifdef ENABLE_BITTORRENT #ifdef ENABLE_BITTORRENT
{
if (downloadContext_->hasAttribute(CTX_ATTR_BT)) { if (downloadContext_->hasAttribute(CTX_ATTR_BT)) {
auto torrentAttrs = bittorrent::getTorrentAttrs(downloadContext_); auto torrentAttrs = bittorrent::getTorrentAttrs(downloadContext_);
bool metadataGetMode = torrentAttrs->metadata.empty(); bool metadataGetMode = torrentAttrs->metadata.empty();
if (option_->getAsBool(PREF_DRY_RUN)) { if (option_->getAsBool(PREF_DRY_RUN)) {
throw DOWNLOAD_FAILURE_EXCEPTION throw DOWNLOAD_FAILURE_EXCEPTION(
("Cancel BitTorrent download in dry-run context."); "Cancel BitTorrent download in dry-run context.");
} }
auto& btRegistry = e->getBtRegistry(); auto& btRegistry = e->getBtRegistry();
if (btRegistry->getDownloadContext(torrentAttrs->infoHash)) { if (btRegistry->getDownloadContext(torrentAttrs->infoHash)) {
// TODO If metadataGetMode == false and each FileEntry has // TODO If metadataGetMode == false and each FileEntry has
// URI, then go without BT. // URI, then go without BT.
throw DOWNLOAD_FAILURE_EXCEPTION2 throw DOWNLOAD_FAILURE_EXCEPTION2(fmt("InfoHash %s is already registered.",
(fmt("InfoHash %s is already registered.",
bittorrent::getInfoHashString(downloadContext_).c_str()), bittorrent::getInfoHashString(downloadContext_).c_str()),
error_code::DUPLICATE_INFO_HASH); error_code::DUPLICATE_INFO_HASH);
} }
if (metadataGetMode) { if (metadataGetMode) {
// Use UnknownLengthPieceStorage. // Use UnknownLengthPieceStorage.
initPieceStorage(); initPieceStorage();
} else { }
if(e->getRequestGroupMan()->isSameFileBeingDownloaded(this)) { else if(e->getRequestGroupMan()->isSameFileBeingDownloaded(this)) {
throw DOWNLOAD_FAILURE_EXCEPTION2 throw DOWNLOAD_FAILURE_EXCEPTION2(fmt(EX_DUPLICATE_FILE_DOWNLOAD,
(fmt(EX_DUPLICATE_FILE_DOWNLOAD,
downloadContext_->getBasePath().c_str()), downloadContext_->getBasePath().c_str()),
error_code::DUPLICATE_DOWNLOAD); error_code::DUPLICATE_DOWNLOAD);
} }
else {
initPieceStorage(); initPieceStorage();
if(downloadContext_->getFileEntries().size() > 1) { if(downloadContext_->getFileEntries().size() > 1) {
pieceStorage_->setupFileFilter(); pieceStorage_->setupFileFilter();
} }
} }
DefaultBtProgressInfoFile* progressInfoFilePtr = nullptr; std::shared_ptr<DefaultBtProgressInfoFile> progressInfoFile;
std::shared_ptr<BtProgressInfoFile> progressInfoFile;
if(!metadataGetMode) { if(!metadataGetMode) {
progressInfoFilePtr = new DefaultBtProgressInfoFile(downloadContext_, progressInfoFile = std::make_shared<DefaultBtProgressInfoFile>(
downloadContext_,
pieceStorage_, pieceStorage_,
option_.get()); option_.get());
progressInfoFile.reset(progressInfoFilePtr);
} }
std::shared_ptr<BtRuntime> btRuntime(new BtRuntime()); auto btRuntime = std::make_shared<BtRuntime>();
btRuntime->setMaxPeers(option_->getAsInt(PREF_BT_MAX_PEERS)); btRuntime->setMaxPeers(option_->getAsInt(PREF_BT_MAX_PEERS));
btRuntime_ = btRuntime.get(); btRuntime_ = btRuntime.get();
if(progressInfoFilePtr) { if(progressInfoFile) {
progressInfoFilePtr->setBtRuntime(btRuntime); progressInfoFile->setBtRuntime(btRuntime);
} }
auto peerStoragePtr(new DefaultPeerStorage()); auto peerStorage = std::make_shared<DefaultPeerStorage>();
peerStoragePtr->setBtRuntime(btRuntime); peerStorage->setBtRuntime(btRuntime);
peerStoragePtr->setPieceStorage(pieceStorage_); peerStorage->setPieceStorage(pieceStorage_);
peerStorage_ = peerStoragePtr; peerStorage_ = peerStorage.get();
std::shared_ptr<PeerStorage> peerStorage(peerStoragePtr); if(progressInfoFile) {
if(progressInfoFilePtr) { progressInfoFile->setPeerStorage(peerStorage);
progressInfoFilePtr->setPeerStorage(peerStorage);
} }
auto btAnnouncePtr = new DefaultBtAnnounce(downloadContext_.get(), auto btAnnounce = std::make_shared<DefaultBtAnnounce>(
downloadContext_.get(),
option_.get()); option_.get());
std::shared_ptr<BtAnnounce> btAnnounce(btAnnouncePtr); btAnnounce->setBtRuntime(btRuntime);
btAnnouncePtr->setBtRuntime(btRuntime); btAnnounce->setPieceStorage(pieceStorage_);
btAnnouncePtr->setPieceStorage(pieceStorage_); btAnnounce->setPeerStorage(peerStorage);
btAnnouncePtr->setPeerStorage(peerStorage); btAnnounce->setUserDefinedInterval(
btAnnouncePtr->setUserDefinedInterval option_->getAsInt(PREF_BT_TRACKER_INTERVAL));
(option_->getAsInt(PREF_BT_TRACKER_INTERVAL)); btAnnounce->shuffleAnnounce();
btAnnouncePtr->shuffleAnnounce();
assert(!btRegistry->get(gid_->getNumericId())); assert(!btRegistry->get(gid_->getNumericId()));
btRegistry->put(gid_->getNumericId(), make_unique<BtObject> btRegistry->put(gid_->getNumericId(), make_unique<BtObject>
@ -358,24 +353,29 @@ void RequestGroup::createInitialCommand
btRuntime, btRuntime,
(progressInfoFile ? (progressInfoFile ?
progressInfoFile : progressInfoFile_))); progressInfoFile : progressInfoFile_)));
if (metadataGetMode) { if (metadataGetMode) {
if (option_->getAsBool(PREF_ENABLE_DHT) || if (option_->getAsBool(PREF_ENABLE_DHT) ||
(!e->getOption()->getAsBool(PREF_DISABLE_IPV6) && (!e->getOption()->getAsBool(PREF_DISABLE_IPV6) &&
option_->getAsBool(PREF_ENABLE_DHT6))) { option_->getAsBool(PREF_ENABLE_DHT6))) {
if (option_->getAsBool(PREF_ENABLE_DHT)) { if (option_->getAsBool(PREF_ENABLE_DHT)) {
e->addCommand(DHTSetup().setup(e, AF_INET)); e->addCommand(DHTSetup().setup(e, AF_INET));
} }
if (!e->getOption()->getAsBool(PREF_DISABLE_IPV6) && if (!e->getOption()->getAsBool(PREF_DISABLE_IPV6) &&
option_->getAsBool(PREF_ENABLE_DHT6)) { option_->getAsBool(PREF_ENABLE_DHT6)) {
e->addCommand(DHTSetup().setup(e, AF_INET6)); e->addCommand(DHTSetup().setup(e, AF_INET6));
} }
} else { }
else {
A2_LOG_NOTICE(_("For BitTorrent Magnet URI, enabling DHT is strongly" A2_LOG_NOTICE(_("For BitTorrent Magnet URI, enabling DHT is strongly"
" recommended. See --enable-dht option.")); " recommended. See --enable-dht option."));
} }
BtCheckIntegrityEntry{this}.onDownloadIncomplete(commands, e); BtCheckIntegrityEntry{this}.onDownloadIncomplete(commands, e);
return; return;
} }
removeDefunctControlFile(progressInfoFile); removeDefunctControlFile(progressInfoFile);
{ {
int64_t actualFileSize = pieceStorage_->getDiskAdaptor()->size(); int64_t actualFileSize = pieceStorage_->getDiskAdaptor()->size();
@ -383,11 +383,11 @@ void RequestGroup::createInitialCommand
// First, make DiskAdaptor read-only mode to allow the // First, make DiskAdaptor read-only mode to allow the
// program to seed file in read-only media. // program to seed file in read-only media.
pieceStorage_->getDiskAdaptor()->enableReadOnly(); pieceStorage_->getDiskAdaptor()->enableReadOnly();
} else { }
else {
// Open file in writable mode to allow the program // Open file in writable mode to allow the program
// truncate the file to downloadContext_->getTotalLength() // truncate the file to downloadContext_->getTotalLength()
A2_LOG_DEBUG A2_LOG_DEBUG(fmt("File size not match. File is opened in writable"
(fmt("File size not match. File is opened in writable"
" mode. Expected:%" PRId64 " Actual:%" PRId64 "", " mode. Expected:%" PRId64 " Actual:%" PRId64 "",
downloadContext_->getTotalLength(), downloadContext_->getTotalLength(),
actualFileSize)); actualFileSize));
@ -398,25 +398,23 @@ void RequestGroup::createInitialCommand
// load .aria2 file if it exists. // load .aria2 file if it exists.
progressInfoFile->load(); progressInfoFile->load();
pieceStorage_->getDiskAdaptor()->openFile(); pieceStorage_->getDiskAdaptor()->openFile();
} else { }
if(pieceStorage_->getDiskAdaptor()->fileExists()) { else if(pieceStorage_->getDiskAdaptor()->fileExists()) {
if(!option_->getAsBool(PREF_CHECK_INTEGRITY) && if(!option_->getAsBool(PREF_CHECK_INTEGRITY) &&
!option_->getAsBool(PREF_ALLOW_OVERWRITE) && !option_->getAsBool(PREF_ALLOW_OVERWRITE) &&
!option_->getAsBool(PREF_BT_SEED_UNVERIFIED)) { !option_->getAsBool(PREF_BT_SEED_UNVERIFIED)) {
// TODO we need this->haltRequested = true? // TODO we need this->haltRequested = true?
throw DOWNLOAD_FAILURE_EXCEPTION2 throw DOWNLOAD_FAILURE_EXCEPTION2(fmt(MSG_FILE_ALREADY_EXISTS,
(fmt(MSG_FILE_ALREADY_EXISTS,
downloadContext_->getBasePath().c_str()), downloadContext_->getBasePath().c_str()),
error_code::FILE_ALREADY_EXISTS); error_code::FILE_ALREADY_EXISTS);
} else {
pieceStorage_->getDiskAdaptor()->openFile();
} }
pieceStorage_->getDiskAdaptor()->openFile();
if(option_->getAsBool(PREF_BT_SEED_UNVERIFIED)) { if(option_->getAsBool(PREF_BT_SEED_UNVERIFIED)) {
pieceStorage_->markAllPiecesDone(); pieceStorage_->markAllPiecesDone();
} }
} else {
pieceStorage_->getDiskAdaptor()->openFile();
} }
else {
pieceStorage_->getDiskAdaptor()->openFile();
} }
progressInfoFile_ = progressInfoFile; progressInfoFile_ = progressInfoFile;
@ -424,19 +422,20 @@ void RequestGroup::createInitialCommand
(option_->getAsBool(PREF_ENABLE_DHT) || (option_->getAsBool(PREF_ENABLE_DHT) ||
(!e->getOption()->getAsBool(PREF_DISABLE_IPV6) && (!e->getOption()->getAsBool(PREF_DISABLE_IPV6) &&
option_->getAsBool(PREF_ENABLE_DHT6)))) { option_->getAsBool(PREF_ENABLE_DHT6)))) {
if (option_->getAsBool(PREF_ENABLE_DHT)) { if (option_->getAsBool(PREF_ENABLE_DHT)) {
e->addCommand(DHTSetup().setup(e, AF_INET)); e->addCommand(DHTSetup().setup(e, AF_INET));
} }
if (!e->getOption()->getAsBool(PREF_DISABLE_IPV6) && if (!e->getOption()->getAsBool(PREF_DISABLE_IPV6) &&
option_->getAsBool(PREF_ENABLE_DHT6)) { option_->getAsBool(PREF_ENABLE_DHT6)) {
e->addCommand(DHTSetup().setup(e, AF_INET6)); e->addCommand(DHTSetup().setup(e, AF_INET6));
} }
const std::vector<std::pair<std::string, uint16_t> >& nodes = const auto& nodes = torrentAttrs->nodes;
torrentAttrs->nodes;
// TODO Are nodes in torrent IPv4 only? // TODO Are nodes in torrent IPv4 only?
if(!nodes.empty() && DHTRegistry::isInitialized()) { if(!nodes.empty() && DHTRegistry::isInitialized()) {
auto command = make_unique<DHTEntryPointNameResolveCommand> auto command = make_unique<DHTEntryPointNameResolveCommand>(
(e->newCUID(), e, nodes); e->newCUID(), e, nodes);
command->setTaskQueue(DHTRegistry::getData().taskQueue.get()); command->setTaskQueue(DHTRegistry::getData().taskQueue.get());
command->setTaskFactory(DHTRegistry::getData().taskFactory.get()); command->setTaskFactory(DHTRegistry::getData().taskFactory.get());
command->setRoutingTable(DHTRegistry::getData().routingTable.get()); command->setRoutingTable(DHTRegistry::getData().routingTable.get());
@ -450,49 +449,51 @@ void RequestGroup::createInitialCommand
if (option_->getAsBool(PREF_BT_SEED_UNVERIFIED) && if (option_->getAsBool(PREF_BT_SEED_UNVERIFIED) &&
pieceStorage_->downloadFinished()) { pieceStorage_->downloadFinished()) {
entry->onDownloadFinished(commands, e); entry->onDownloadFinished(commands, e);
} else { }
else {
processCheckIntegrityEntry(commands, std::move(entry), e); processCheckIntegrityEntry(commands, std::move(entry), e);
} }
return; return;
} }
}
#endif // ENABLE_BITTORRENT #endif // ENABLE_BITTORRENT
if (downloadContext_->getFileEntries().size() == 1) { if (downloadContext_->getFileEntries().size() == 1) {
// TODO I assume here when totallength is set to DownloadContext and it is // TODO I assume here when totallength is set to DownloadContext and it is
// not 0, then filepath is also set DownloadContext correctly.... // not 0, then filepath is also set DownloadContext correctly....
if (option_->getAsBool(PREF_DRY_RUN) || if (option_->getAsBool(PREF_DRY_RUN) ||
downloadContext_->getTotalLength() == 0) { downloadContext_->getTotalLength() == 0) {
createNextCommand(commands, e, 1); createNextCommand(commands, e, 1);
} else { return;
}
if (e->getRequestGroupMan()->isSameFileBeingDownloaded(this)) { if (e->getRequestGroupMan()->isSameFileBeingDownloaded(this)) {
throw DOWNLOAD_FAILURE_EXCEPTION2 throw DOWNLOAD_FAILURE_EXCEPTION2(fmt(EX_DUPLICATE_FILE_DOWNLOAD,
(fmt(EX_DUPLICATE_FILE_DOWNLOAD,
downloadContext_->getBasePath().c_str()), downloadContext_->getBasePath().c_str()),
error_code::DUPLICATE_DOWNLOAD); error_code::DUPLICATE_DOWNLOAD);
} }
std::shared_ptr<BtProgressInfoFile> progressInfoFile auto progressInfoFile = std::make_shared<DefaultBtProgressInfoFile>(
(new DefaultBtProgressInfoFile downloadContext_,
(downloadContext_, std::shared_ptr<PieceStorage>(), option_.get())); nullptr,
option_.get());
adjustFilename(progressInfoFile); adjustFilename(progressInfoFile);
initPieceStorage(); initPieceStorage();
auto checkEntry = createCheckIntegrityEntry(); auto checkEntry = createCheckIntegrityEntry();
if(checkEntry) { if(checkEntry) {
processCheckIntegrityEntry(commands, std::move(checkEntry), e); processCheckIntegrityEntry(commands, std::move(checkEntry), e);
} }
return;
} }
} else {
// TODO --dry-run is not supported for multifile download for now. // TODO --dry-run is not supported for multifile download for now.
if (option_->getAsBool(PREF_DRY_RUN)) { if (option_->getAsBool(PREF_DRY_RUN)) {
throw DOWNLOAD_FAILURE_EXCEPTION throw DOWNLOAD_FAILURE_EXCEPTION(
("--dry-run in multi-file download is not supported yet."); "--dry-run in multi-file download is not supported yet.");
} }
// TODO file size is known in this context? // TODO file size is known in this context?
// In this context, multiple FileEntry objects are in // In this context, multiple FileEntry objects are in
// DownloadContext. // DownloadContext.
if (e->getRequestGroupMan()->isSameFileBeingDownloaded(this)) { if (e->getRequestGroupMan()->isSameFileBeingDownloaded(this)) {
throw DOWNLOAD_FAILURE_EXCEPTION2 throw DOWNLOAD_FAILURE_EXCEPTION2(fmt(EX_DUPLICATE_FILE_DOWNLOAD,
(fmt(EX_DUPLICATE_FILE_DOWNLOAD,
downloadContext_->getBasePath().c_str()), downloadContext_->getBasePath().c_str()),
error_code::DUPLICATE_DOWNLOAD); error_code::DUPLICATE_DOWNLOAD);
} }
@ -500,43 +501,37 @@ void RequestGroup::createInitialCommand
if (downloadContext_->getFileEntries().size() > 1) { if (downloadContext_->getFileEntries().size() > 1) {
pieceStorage_->setupFileFilter(); pieceStorage_->setupFileFilter();
} }
std::shared_ptr<BtProgressInfoFile> progressInfoFile auto progressInfoFile = std::make_shared<DefaultBtProgressInfoFile>(
(new DefaultBtProgressInfoFile(downloadContext_, downloadContext_,
pieceStorage_, pieceStorage_,
option_.get())); option_.get());
removeDefunctControlFile(progressInfoFile); removeDefunctControlFile(progressInfoFile);
// Call Load, Save and file allocation command here // Call Load, Save and file allocation command here
if (progressInfoFile->exists()) { if (progressInfoFile->exists()) {
// load .aria2 file if it exists. // load .aria2 file if it exists.
progressInfoFile->load(); progressInfoFile->load();
pieceStorage_->getDiskAdaptor()->openFile(); pieceStorage_->getDiskAdaptor()->openFile();
} else { }
if(pieceStorage_->getDiskAdaptor()->fileExists()) { else if (pieceStorage_->getDiskAdaptor()->fileExists()) {
if(!isCheckIntegrityReady() && if(!isCheckIntegrityReady() && !option_->getAsBool(PREF_ALLOW_OVERWRITE)) {
!option_->getAsBool(PREF_ALLOW_OVERWRITE)) {
// TODO we need this->haltRequested = true? // TODO we need this->haltRequested = true?
throw DOWNLOAD_FAILURE_EXCEPTION2 throw DOWNLOAD_FAILURE_EXCEPTION2(fmt(MSG_FILE_ALREADY_EXISTS,
(fmt(MSG_FILE_ALREADY_EXISTS,
downloadContext_->getBasePath().c_str()), downloadContext_->getBasePath().c_str()),
error_code::FILE_ALREADY_EXISTS); error_code::FILE_ALREADY_EXISTS);
}
pieceStorage_->getDiskAdaptor()->openFile();
} else { } else {
pieceStorage_->getDiskAdaptor()->openFile(); pieceStorage_->getDiskAdaptor()->openFile();
} }
} else {
pieceStorage_->getDiskAdaptor()->openFile();
}
}
progressInfoFile_ = progressInfoFile; progressInfoFile_ = progressInfoFile;
processCheckIntegrityEntry(commands, processCheckIntegrityEntry(commands,
make_unique<StreamCheckIntegrityEntry>(this), make_unique<StreamCheckIntegrityEntry>(this),
e); e);
} }
}
void RequestGroup::processCheckIntegrityEntry void RequestGroup::processCheckIntegrityEntry(
(std::vector<std::unique_ptr<Command>>& commands, std::vector<std::unique_ptr<Command>>& commands,
std::unique_ptr<CheckIntegrityEntry> entry, std::unique_ptr<CheckIntegrityEntry> entry, DownloadEngine* e)
DownloadEngine* e)
{ {
int64_t actualFileSize = pieceStorage_->getDiskAdaptor()->size(); int64_t actualFileSize = pieceStorage_->getDiskAdaptor()->size();
if(actualFileSize > downloadContext_->getTotalLength()) { if(actualFileSize > downloadContext_->getTotalLength()) {
@ -555,11 +550,11 @@ void RequestGroup::processCheckIntegrityEntry
// done. See CheckIntegrityCommand. // done. See CheckIntegrityCommand.
disableSaveControlFile(); disableSaveControlFile();
e->getCheckIntegrityMan()->pushEntry(std::move(entry)); e->getCheckIntegrityMan()->pushEntry(std::move(entry));
} else return;
#endif // ENABLE_MESSAGE_DIGEST
{
entry->onDownloadIncomplete(commands, e);
} }
#endif // ENABLE_MESSAGE_DIGEST
entry->onDownloadIncomplete(commands, e);
} }
void RequestGroup::initPieceStorage() void RequestGroup::initPieceStorage()
@ -574,38 +569,38 @@ void RequestGroup::initPieceStorage()
#endif // ENABLE_BITTORRENT #endif // ENABLE_BITTORRENT
)) { )) {
#ifdef ENABLE_BITTORRENT #ifdef ENABLE_BITTORRENT
auto ps = new DefaultPieceStorage(downloadContext_, option_.get()); auto ps = std::make_shared<DefaultPieceStorage>(
std::shared_ptr<PieceStorage> psHolder(ps); downloadContext_, option_.get());
if (downloadContext_->hasAttribute(CTX_ATTR_BT)) { if (downloadContext_->hasAttribute(CTX_ATTR_BT)) {
if(isUriSuppliedForRequsetFileEntry if (isUriSuppliedForRequsetFileEntry(
(downloadContext_->getFileEntries().begin(), downloadContext_->getFileEntries().begin(),
downloadContext_->getFileEntries().end())) { downloadContext_->getFileEntries().end())) {
// Use LongestSequencePieceSelector when HTTP/FTP/BitTorrent // Use LongestSequencePieceSelector when HTTP/FTP/BitTorrent
// integrated downloads. Currently multi-file integrated // integrated downloads. Currently multi-file integrated
// download is not supported. // download is not supported.
A2_LOG_DEBUG("Using LongestSequencePieceSelector"); A2_LOG_DEBUG("Using LongestSequencePieceSelector");
ps->setPieceSelector(make_unique<LongestSequencePieceSelector>()); ps->setPieceSelector(make_unique<LongestSequencePieceSelector>());
} }
if (option_->defined(PREF_BT_PRIORITIZE_PIECE)) { if (option_->defined(PREF_BT_PRIORITIZE_PIECE)) {
std::vector<size_t> result; std::vector<size_t> result;
util::parsePrioritizePieceRange util::parsePrioritizePieceRange(result,
(result, option_->get(PREF_BT_PRIORITIZE_PIECE), option_->get(PREF_BT_PRIORITIZE_PIECE),
downloadContext_->getFileEntries(), downloadContext_->getFileEntries(),
downloadContext_->getPieceLength()); downloadContext_->getPieceLength());
if (!result.empty()) { if (!result.empty()) {
std::random_shuffle(std::begin(result), std::end(result), std::random_shuffle(std::begin(result), std::end(result),
*SimpleRandomizer::getInstance()); *SimpleRandomizer::getInstance());
auto priSelector = make_unique<PriorityPieceSelector> auto priSelector = make_unique<PriorityPieceSelector>(
(ps->popPieceSelector()); ps->popPieceSelector());
priSelector->setPriorityPiece(std::begin(result), std::end(result)); priSelector->setPriorityPiece(std::begin(result), std::end(result));
ps->setPieceSelector(std::move(priSelector)); ps->setPieceSelector(std::move(priSelector));
} }
} }
} }
#else // !ENABLE_BITTORRENT #else // !ENABLE_BITTORRENT
DefaultPieceStorage* ps = auto ps = std::make_shared<DefaultPieceStorage>(
new DefaultPieceStorage(downloadContext_, option_.get()); downloadContext_, option_.get());
std::shared_ptr<PieceStorage> psHolder(ps);
#endif // !ENABLE_BITTORRENT #endif // !ENABLE_BITTORRENT
if (requestGroupMan_) { if (requestGroupMan_) {
ps->setWrDiskCache(requestGroupMan_->getWrDiskCache()); ps->setWrDiskCache(requestGroupMan_->getWrDiskCache());
@ -613,21 +608,18 @@ void RequestGroup::initPieceStorage()
if (diskWriterFactory_) { if (diskWriterFactory_) {
ps->setDiskWriterFactory(diskWriterFactory_); ps->setDiskWriterFactory(diskWriterFactory_);
} }
tempPieceStorage.swap(psHolder); tempPieceStorage = ps;
} else { }
auto ps = new UnknownLengthPieceStorage(downloadContext_); else {
std::shared_ptr<PieceStorage> psHolder(ps); auto ps = std::make_shared<UnknownLengthPieceStorage>(downloadContext_);
if (diskWriterFactory_) { if (diskWriterFactory_) {
ps->setDiskWriterFactory(diskWriterFactory_); ps->setDiskWriterFactory(diskWriterFactory_);
} }
tempPieceStorage.swap(psHolder); tempPieceStorage = ps;
} }
tempPieceStorage->initStorage(); tempPieceStorage->initStorage();
std::shared_ptr<SegmentMan> tempSegmentMan(new SegmentMan(downloadContext_, segmentMan_ = std::make_shared<SegmentMan>(downloadContext_, tempPieceStorage);
tempPieceStorage)); pieceStorage_ = tempPieceStorage;
pieceStorage_.swap(tempPieceStorage);
segmentMan_.swap(tempSegmentMan);
} }
void RequestGroup::dropPieceStorage() void RequestGroup::dropPieceStorage()
@ -649,13 +641,12 @@ bool RequestGroup::downloadFinishedByFileLength()
File outfile(getFirstFilePath()); File outfile(getFirstFilePath());
if(outfile.exists() && downloadContext_->getTotalLength() == outfile.size()) { if(outfile.exists() && downloadContext_->getTotalLength() == outfile.size()) {
return true; return true;
} else { }
return false; return false;
} }
}
void RequestGroup::adjustFilename void RequestGroup::adjustFilename(
(const std::shared_ptr<BtProgressInfoFile>& infoFile) const std::shared_ptr<BtProgressInfoFile>& infoFile)
{ {
if(!isPreLocalFileCheckEnabled()) { if(!isPreLocalFileCheckEnabled()) {
// OK, no need to care about filename. // OK, no need to care about filename.
@ -669,29 +660,31 @@ void RequestGroup::adjustFilename
" user."), " user."),
infoFile->getFilename().c_str())); infoFile->getFilename().c_str()));
} }
if (infoFile->exists()) { if (infoFile->exists()) {
// Use current filename // Use current filename
} else { return;
}
File outfile(getFirstFilePath()); File outfile(getFirstFilePath());
if(outfile.exists() && option_->getAsBool(PREF_CONTINUE) && if(outfile.exists() && option_->getAsBool(PREF_CONTINUE) &&
outfile.size() <= downloadContext_->getTotalLength()) { outfile.size() <= downloadContext_->getTotalLength()) {
// File exists but user decided to resume it. // File exists but user decided to resume it.
} else { }
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
if(outfile.exists() && isCheckIntegrityReady()) { else if(outfile.exists() && isCheckIntegrityReady()) {
// check-integrity existing file // check-integrity existing file
} else { }
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
else {
shouldCancelDownloadForSafety(); shouldCancelDownloadForSafety();
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
} }
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
} }
}
}
void RequestGroup::removeDefunctControlFile void RequestGroup::removeDefunctControlFile(
(const std::shared_ptr<BtProgressInfoFile>& progressInfoFile) const std::shared_ptr<BtProgressInfoFile>& progressInfoFile)
{ {
// Remove the control file if download file doesn't exist // Remove the control file if download file doesn't exist
if(progressInfoFile->exists() && if(progressInfoFile->exists() &&
@ -703,8 +696,8 @@ void RequestGroup::removeDefunctControlFile
} }
} }
void RequestGroup::loadAndOpenFile void RequestGroup::loadAndOpenFile(
(const std::shared_ptr<BtProgressInfoFile>& progressInfoFile) const std::shared_ptr<BtProgressInfoFile>& progressInfoFile)
{ {
try { try {
if(!isPreLocalFileCheckEnabled()) { if(!isPreLocalFileCheckEnabled()) {
@ -715,26 +708,26 @@ void RequestGroup::loadAndOpenFile
if(progressInfoFile->exists()) { if(progressInfoFile->exists()) {
progressInfoFile->load(); progressInfoFile->load();
pieceStorage_->getDiskAdaptor()->openExistingFile(); pieceStorage_->getDiskAdaptor()->openExistingFile();
} else { }
else {
File outfile(getFirstFilePath()); File outfile(getFirstFilePath());
if (outfile.exists() && option_->getAsBool(PREF_CONTINUE) && if (outfile.exists() && option_->getAsBool(PREF_CONTINUE) &&
outfile.size() <= getTotalLength()) { outfile.size() <= getTotalLength()) {
pieceStorage_->getDiskAdaptor()->openExistingFile(); pieceStorage_->getDiskAdaptor()->openExistingFile();
pieceStorage_->markPiecesDone(outfile.size()); pieceStorage_->markPiecesDone(outfile.size());
} else { }
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
if(outfile.exists() && isCheckIntegrityReady()) { else if (outfile.exists() && isCheckIntegrityReady()) {
pieceStorage_->getDiskAdaptor()->openExistingFile(); pieceStorage_->getDiskAdaptor()->openExistingFile();
} else {
#endif // ENABLE_MESSAGE_DIGEST
pieceStorage_->getDiskAdaptor()->initAndOpenFile();
#ifdef ENABLE_MESSAGE_DIGEST
} }
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
else {
pieceStorage_->getDiskAdaptor()->initAndOpenFile();
} }
} }
setProgressInfoFile(progressInfoFile); setProgressInfoFile(progressInfoFile);
} catch(RecoverableException& e) { }
catch(RecoverableException& e) {
throw DOWNLOAD_FAILURE_EXCEPTION2(fmt(EX_DOWNLOAD_ABORTED), e); throw DOWNLOAD_FAILURE_EXCEPTION2(fmt(EX_DOWNLOAD_ABORTED), e);
} }
} }
@ -746,21 +739,23 @@ void RequestGroup::shouldCancelDownloadForSafety()
return; return;
} }
File outfile(getFirstFilePath()); File outfile(getFirstFilePath());
if(outfile.exists()) { if (!outfile.exists()) {
if(option_->getAsBool(PREF_AUTO_FILE_RENAMING)) { return;
if(tryAutoFileRenaming()) {
A2_LOG_NOTICE(fmt(MSG_FILE_RENAMED, getFirstFilePath().c_str()));
} else {
throw DOWNLOAD_FAILURE_EXCEPTION2
(fmt("File renaming failed: %s", getFirstFilePath().c_str()),
error_code::FILE_RENAMING_FAILED);
} }
} else {
throw DOWNLOAD_FAILURE_EXCEPTION2 if (!option_->getAsBool(PREF_AUTO_FILE_RENAMING)) {
(fmt(MSG_FILE_ALREADY_EXISTS, getFirstFilePath().c_str()), throw DOWNLOAD_FAILURE_EXCEPTION2(fmt(MSG_FILE_ALREADY_EXISTS,
getFirstFilePath().c_str()),
error_code::FILE_ALREADY_EXISTS); error_code::FILE_ALREADY_EXISTS);
} }
if (!tryAutoFileRenaming()) {
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() bool RequestGroup::tryAutoFileRenaming()
@ -770,7 +765,7 @@ bool RequestGroup::tryAutoFileRenaming()
return false; return false;
} }
for (int i = 1; i < 10000; ++i) { for (int i = 1; i < 10000; ++i) {
std::string newfilename = fmt("%s.%d", filepath.c_str(), i); auto newfilename = fmt("%s.%d", filepath.c_str(), i);
File newfile(newfilename); File newfile(newfilename);
File ctrlfile(newfile.getPath() + DefaultBtProgressInfoFile::getSuffix()); File ctrlfile(newfile.getPath() + DefaultBtProgressInfoFile::getSuffix());
if (!newfile.exists() || (newfile.exists() && ctrlfile.exists())) { if (!newfile.exists() || (newfile.exists() && ctrlfile.exists())) {
@ -781,26 +776,27 @@ bool RequestGroup::tryAutoFileRenaming()
return false; return false;
} }
void RequestGroup::createNextCommandWithAdj void RequestGroup::createNextCommandWithAdj(
(std::vector<std::unique_ptr<Command>>& commands, std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e,
DownloadEngine* e, int numAdj) int numAdj)
{ {
int numCommand; int numCommand;
if (getTotalLength() == 0) { if (getTotalLength() == 0) {
numCommand = 1+numAdj; numCommand = 1+numAdj;
} else { }
else {
numCommand = std::min(downloadContext_->getNumPieces(), numCommand = std::min(downloadContext_->getNumPieces(),
static_cast<size_t>(numConcurrentCommand_)); static_cast<size_t>(numConcurrentCommand_));
numCommand += numAdj; numCommand += numAdj;
} }
if (numCommand > 0) { if (numCommand > 0) {
createNextCommand(commands, e, numCommand); createNextCommand(commands, e, numCommand);
} }
} }
void RequestGroup::createNextCommand void RequestGroup::createNextCommand(
(std::vector<std::unique_ptr<Command>>& commands, std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e)
DownloadEngine* e)
{ {
int numCommand; int numCommand;
if (getTotalLength() == 0) { if (getTotalLength() == 0) {
@ -809,28 +805,27 @@ void RequestGroup::createNextCommand
} else { } else {
numCommand = 1; numCommand = 1;
} }
} else { }
if(numStreamCommand_ >= numConcurrentCommand_) { else if (numStreamCommand_ >= numConcurrentCommand_) {
numCommand = 0; numCommand = 0;
} else { }
numCommand = else {
std::min(downloadContext_->getNumPieces(), numCommand = std::min(downloadContext_->getNumPieces(),
static_cast<size_t>(numConcurrentCommand_-numStreamCommand_)); static_cast<size_t>(numConcurrentCommand_-numStreamCommand_));
} }
}
if (numCommand > 0) { if (numCommand > 0) {
createNextCommand(commands, e, numCommand); createNextCommand(commands, e, numCommand);
} }
} }
void RequestGroup::createNextCommand void RequestGroup::createNextCommand(
(std::vector<std::unique_ptr<Command>>& commands, std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e,
DownloadEngine* e,
int numCommand) int numCommand)
{ {
for (; numCommand > 0; --numCommand) { for (; numCommand > 0; --numCommand) {
commands.push_back(make_unique<CreateRequestCommand> commands.push_back(make_unique<CreateRequestCommand>(
(e->newCUID(), this, e)); e->newCUID(), this, e));
} }
if (!commands.empty()) { if (!commands.empty()) {
e->setNoWait(true); e->setNoWait(true);
@ -841,38 +836,37 @@ std::string RequestGroup::getFirstFilePath() const
{ {
assert(downloadContext_); assert(downloadContext_);
if (inMemoryDownload()) { if (inMemoryDownload()) {
return "[MEMORY]"+ return "[MEMORY]" + File(downloadContext_->getFirstFileEntry()->getPath())
File(downloadContext_->getFirstFileEntry()->getPath()).getBasename(); .getBasename();
} else {
return downloadContext_->getFirstFileEntry()->getPath();
} }
return downloadContext_->getFirstFileEntry()->getPath();
} }
int64_t RequestGroup::getTotalLength() const int64_t RequestGroup::getTotalLength() const
{ {
if (!pieceStorage_) { if (!pieceStorage_) {
return 0; return 0;
} else { }
if (pieceStorage_->isSelectiveDownloadingMode()) { if (pieceStorage_->isSelectiveDownloadingMode()) {
return pieceStorage_->getFilteredTotalLength(); return pieceStorage_->getFilteredTotalLength();
} else { }
return pieceStorage_->getTotalLength(); return pieceStorage_->getTotalLength();
} }
}
}
int64_t RequestGroup::getCompletedLength() const int64_t RequestGroup::getCompletedLength() const
{ {
if (!pieceStorage_) { if (!pieceStorage_) {
return 0; return 0;
} else { }
if (pieceStorage_->isSelectiveDownloadingMode()) { if (pieceStorage_->isSelectiveDownloadingMode()) {
return pieceStorage_->getFilteredCompletedLength(); return pieceStorage_->getFilteredCompletedLength();
} else { }
return pieceStorage_->getCompletedLength(); return pieceStorage_->getCompletedLength();
} }
}
}
void RequestGroup::validateFilename(const std::string& expectedFilename, void RequestGroup::validateFilename(const std::string& expectedFilename,
const std::string& actualFilename) const const std::string& actualFilename) const
@ -880,6 +874,7 @@ void RequestGroup::validateFilename(const std::string& expectedFilename,
if(expectedFilename.empty()) { if(expectedFilename.empty()) {
return; return;
} }
if(expectedFilename != actualFilename) { if(expectedFilename != actualFilename) {
throw DL_ABORT_EX(fmt(EX_FILENAME_MISMATCH, throw DL_ABORT_EX(fmt(EX_FILENAME_MISMATCH,
expectedFilename.c_str(), expectedFilename.c_str(),
@ -893,15 +888,17 @@ void RequestGroup::validateTotalLength(int64_t expectedTotalLength,
if(expectedTotalLength <= 0) { if(expectedTotalLength <= 0) {
return; return;
} }
if(expectedTotalLength != actualTotalLength) { if(expectedTotalLength != actualTotalLength) {
throw DL_ABORT_EX throw DL_ABORT_EX(fmt(EX_SIZE_MISMATCH, expectedTotalLength,
(fmt(EX_SIZE_MISMATCH, expectedTotalLength, actualTotalLength)); actualTotalLength));
} }
} }
void RequestGroup::validateFilename(const std::string& actualFilename) const void RequestGroup::validateFilename(const std::string& actualFilename) const
{ {
validateFilename(downloadContext_->getFileEntries().front()->getBasename(), actualFilename); validateFilename(downloadContext_->getFileEntries().front()->getBasename(),
actualFilename);
} }
void RequestGroup::validateTotalLength(int64_t actualTotalLength) const void RequestGroup::validateTotalLength(int64_t actualTotalLength) const
@ -1012,39 +1009,39 @@ void RequestGroup::preDownloadProcessing()
A2_LOG_DEBUG(fmt("Finding PreDownloadHandler for path %s.", A2_LOG_DEBUG(fmt("Finding PreDownloadHandler for path %s.",
getFirstFilePath().c_str())); getFirstFilePath().c_str()));
try { try {
for(std::vector<std::shared_ptr<PreDownloadHandler> >::const_iterator itr = for (const auto& pdh: preDownloadHandlers_) {
preDownloadHandlers_.begin(), eoi = preDownloadHandlers_.end(); if(pdh->canHandle(this)) {
itr != eoi; ++itr) { pdh->execute(this);
if((*itr)->canHandle(this)) {
(*itr)->execute(this);
return; return;
} }
} }
} catch(RecoverableException& ex) { }
catch (RecoverableException& ex) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex); A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex);
return; return;
} }
A2_LOG_DEBUG("No PreDownloadHandler found."); A2_LOG_DEBUG("No PreDownloadHandler found.");
return; return;
} }
void RequestGroup::postDownloadProcessing void RequestGroup::postDownloadProcessing(
(std::vector<std::shared_ptr<RequestGroup> >& groups) std::vector<std::shared_ptr<RequestGroup> >& groups)
{ {
A2_LOG_DEBUG(fmt("Finding PostDownloadHandler for path %s.", A2_LOG_DEBUG(fmt("Finding PostDownloadHandler for path %s.",
getFirstFilePath().c_str())); getFirstFilePath().c_str()));
try { try {
for(std::vector<std::shared_ptr<PostDownloadHandler> >::const_iterator itr = for (const auto& pdh: postDownloadHandlers_) {
postDownloadHandlers_.begin(), eoi = postDownloadHandlers_.end(); if (pdh->canHandle(this)) {
itr != eoi; ++itr) { pdh->getNextRequestGroups(groups, this);
if((*itr)->canHandle(this)) {
(*itr)->getNextRequestGroups(groups, this);
return; return;
} }
} }
} catch(RecoverableException& ex) { }
catch(RecoverableException& ex) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex); A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex);
} }
A2_LOG_DEBUG("No PostDownloadHandler found."); A2_LOG_DEBUG("No PostDownloadHandler found.");
} }
@ -1091,8 +1088,8 @@ void RequestGroup::dependsOn(const std::shared_ptr<Dependency>& dep)
dependency_ = dep; dependency_ = dep;
} }
void RequestGroup::setDiskWriterFactory void RequestGroup::setDiskWriterFactory(
(const std::shared_ptr<DiskWriterFactory>& diskWriterFactory) const std::shared_ptr<DiskWriterFactory>& diskWriterFactory)
{ {
diskWriterFactory_ = diskWriterFactory; diskWriterFactory_ = diskWriterFactory;
} }
@ -1103,8 +1100,8 @@ void RequestGroup::addPostDownloadHandler
postDownloadHandlers_.push_back(handler); postDownloadHandlers_.push_back(handler);
} }
void RequestGroup::addPreDownloadHandler void RequestGroup::addPreDownloadHandler(
(const std::shared_ptr<PreDownloadHandler>& handler) const std::shared_ptr<PreDownloadHandler>& handler)
{ {
preDownloadHandlers_.push_back(handler); preDownloadHandlers_.push_back(handler);
} }
@ -1119,13 +1116,14 @@ void RequestGroup::clearPreDownloadHandler()
preDownloadHandlers_.clear(); preDownloadHandlers_.clear();
} }
void RequestGroup::setPieceStorage(const std::shared_ptr<PieceStorage>& pieceStorage) void RequestGroup::setPieceStorage(
const std::shared_ptr<PieceStorage>& pieceStorage)
{ {
pieceStorage_ = pieceStorage; pieceStorage_ = pieceStorage;
} }
void RequestGroup::setProgressInfoFile void RequestGroup::setProgressInfoFile(
(const std::shared_ptr<BtProgressInfoFile>& progressInfoFile) const std::shared_ptr<BtProgressInfoFile>& progressInfoFile)
{ {
progressInfoFile_ = progressInfoFile; progressInfoFile_ = progressInfoFile;
} }
@ -1156,13 +1154,11 @@ std::shared_ptr<DownloadResult> RequestGroup::createDownloadResult() const
res->totalLength = getTotalLength(); res->totalLength = getTotalLength();
res->completedLength = getCompletedLength(); res->completedLength = getCompletedLength();
res->uploadLength = st.allTimeUploadLength; res->uploadLength = st.allTimeUploadLength;
if(pieceStorage_) { if(pieceStorage_ && pieceStorage_->getBitfieldLength() > 0) {
if(pieceStorage_->getBitfieldLength() > 0) {
res->bitfield.assign(pieceStorage_->getBitfield(), res->bitfield.assign(pieceStorage_->getBitfield(),
pieceStorage_->getBitfield() + pieceStorage_->getBitfield() +
pieceStorage_->getBitfieldLength()); pieceStorage_->getBitfieldLength());
} }
}
#ifdef ENABLE_BITTORRENT #ifdef ENABLE_BITTORRENT
if(downloadContext_->hasAttribute(CTX_ATTR_BT)) { if(downloadContext_->hasAttribute(CTX_ATTR_BT)) {
const unsigned char* p = bittorrent::getInfoHash(downloadContext_); const unsigned char* p = bittorrent::getInfoHash(downloadContext_);
@ -1184,7 +1180,8 @@ void RequestGroup::reportDownloadFinished()
if (downloadContext_->hasAttribute(CTX_ATTR_BT)) { if (downloadContext_->hasAttribute(CTX_ATTR_BT)) {
TransferStat stat = calculateStat(); TransferStat stat = calculateStat();
int64_t completedLength = getCompletedLength(); int64_t completedLength = getCompletedLength();
double shareRatio = completedLength == 0 ? 0.0 : double shareRatio = completedLength == 0 ?
0.0 :
1.0 * stat.allTimeUploadLength / completedLength; 1.0 * stat.allTimeUploadLength / completedLength;
auto attrs = bittorrent::getTorrentAttrs(downloadContext_); auto attrs = bittorrent::getTorrentAttrs(downloadContext_);
if(!attrs->metadata.empty()) { if(!attrs->metadata.empty()) {
@ -1204,15 +1201,15 @@ void RequestGroup::setURISelector(std::unique_ptr<URISelector> uriSelector)
void RequestGroup::applyLastModifiedTimeToLocalFiles() void RequestGroup::applyLastModifiedTimeToLocalFiles()
{ {
if(pieceStorage_ && lastModifiedTime_.good()) { if (!pieceStorage_ || !lastModifiedTime_.good()) {
return;
}
A2_LOG_INFO(fmt("Applying Last-Modified time: %s", A2_LOG_INFO(fmt("Applying Last-Modified time: %s",
lastModifiedTime_.toHTTPDate().c_str())); lastModifiedTime_.toHTTPDate().c_str()));
size_t n = size_t n = pieceStorage_->getDiskAdaptor()->utime(Time(), lastModifiedTime_);
pieceStorage_->getDiskAdaptor()->utime(Time(), lastModifiedTime_);
A2_LOG_INFO(fmt("Last-Modified attrs of %lu files were updated.", A2_LOG_INFO(fmt("Last-Modified attrs of %lu files were updated.",
static_cast<unsigned long>(n))); static_cast<unsigned long>(n)));
} }
}
void RequestGroup::updateLastModifiedTime(const Time& time) void RequestGroup::updateLastModifiedTime(const Time& time)
{ {
@ -1227,8 +1224,8 @@ void RequestGroup::increaseAndValidateFileNotFoundCount()
const int maxCount = option_->getAsInt(PREF_MAX_FILE_NOT_FOUND); const int maxCount = option_->getAsInt(PREF_MAX_FILE_NOT_FOUND);
if (maxCount > 0 && fileNotFoundCount_ >= maxCount && if (maxCount > 0 && fileNotFoundCount_ >= maxCount &&
downloadContext_->getNetStat().getSessionDownloadLength() == 0) { downloadContext_->getNetStat().getSessionDownloadLength() == 0) {
throw DOWNLOAD_FAILURE_EXCEPTION2 throw DOWNLOAD_FAILURE_EXCEPTION2(fmt("Reached max-file-not-found count=%d",
(fmt("Reached max-file-not-found count=%d", maxCount), maxCount),
error_code::MAX_FILE_NOT_FOUND); error_code::MAX_FILE_NOT_FOUND);
} }
} }
@ -1267,8 +1264,8 @@ void RequestGroup::removeControlFile() const
progressInfoFile_->removeFile(); progressInfoFile_->removeFile();
} }
void RequestGroup::setDownloadContext void RequestGroup::setDownloadContext(
(const std::shared_ptr<DownloadContext>& downloadContext) const std::shared_ptr<DownloadContext>& downloadContext)
{ {
downloadContext_ = downloadContext; downloadContext_ = downloadContext;
if(downloadContext_) { if(downloadContext_) {

View File

@ -194,8 +194,8 @@ private:
// returns error_code::UNKNOWN_ERROR. // returns error_code::UNKNOWN_ERROR.
error_code::Value downloadResult() const; error_code::Value downloadResult() const;
void removeDefunctControlFile void removeDefunctControlFile(
(const std::shared_ptr<BtProgressInfoFile>& progressInfoFile); const std::shared_ptr<BtProgressInfoFile>& progressInfoFile);
public: public:
RequestGroup(const std::shared_ptr<GroupId>& gid, RequestGroup(const std::shared_ptr<GroupId>& gid,
@ -218,8 +218,7 @@ public:
void createInitialCommand(std::vector<std::unique_ptr<Command>>& commands, void createInitialCommand(std::vector<std::unique_ptr<Command>>& commands,
DownloadEngine* e); DownloadEngine* e);
void createNextCommandWithAdj void createNextCommandWithAdj(std::vector<std::unique_ptr<Command>>& commands,
(std::vector<std::unique_ptr<Command>>& commands,
DownloadEngine* e, int numAdj); DownloadEngine* e, int numAdj);
void createNextCommand(std::vector<std::unique_ptr<Command>>& commands, void createNextCommand(std::vector<std::unique_ptr<Command>>& commands,
@ -290,7 +289,8 @@ public:
void setPieceStorage(const std::shared_ptr<PieceStorage>& pieceStorage); void setPieceStorage(const std::shared_ptr<PieceStorage>& pieceStorage);
void setProgressInfoFile(const std::shared_ptr<BtProgressInfoFile>& progressInfoFile); void setProgressInfoFile(
const std::shared_ptr<BtProgressInfoFile>& progressInfoFile);
void increaseStreamCommand(); void increaseStreamCommand();
@ -385,8 +385,8 @@ public:
void clearPreDownloadHandler(); void clearPreDownloadHandler();
void processCheckIntegrityEntry void processCheckIntegrityEntry(
(std::vector<std::unique_ptr<Command>>& commands, std::vector<std::unique_ptr<Command>>& commands,
std::unique_ptr<CheckIntegrityEntry> entry, std::unique_ptr<CheckIntegrityEntry> entry,
DownloadEngine* e); DownloadEngine* e);