Use std::unique_ptr for CheckIntegrityEntry and FileAllocationEntry

pull/106/head
Tatsuhiro Tsujikawa 2013-07-06 00:17:44 +09:00
parent c9e58779e1
commit 6b397c8125
24 changed files with 118 additions and 127 deletions

View File

@ -770,10 +770,11 @@ std::string AbstractCommand::resolveHostname
} }
void AbstractCommand::prepareForNextAction void AbstractCommand::prepareForNextAction
(const std::shared_ptr<CheckIntegrityEntry>& checkEntry) (std::unique_ptr<CheckIntegrityEntry> checkEntry)
{ {
std::vector<std::unique_ptr<Command>> commands; std::vector<std::unique_ptr<Command>> commands;
requestGroup_->processCheckIntegrityEntry(commands, checkEntry, e_); requestGroup_->processCheckIntegrityEntry(commands, std::move(checkEntry),
e_);
e_->addCommand(std::move(commands)); e_->addCommand(std::move(commands));
e_->setNoWait(true); e_->setNoWait(true);
} }

View File

@ -179,8 +179,7 @@ public:
void setTimeout(time_t timeout) { timeout_ = timeout; } void setTimeout(time_t timeout) { timeout_ = timeout; }
void prepareForNextAction void prepareForNextAction(std::unique_ptr<CheckIntegrityEntry> checkEntry);
(const std::shared_ptr<CheckIntegrityEntry>& checkEntry);
// Check if socket is connected. If socket is not connected and // Check if socket is connected. If socket is not connected and
// there are other addresses to try, command is created using // there are other addresses to try, command is created using

View File

@ -51,21 +51,22 @@ BtCheckIntegrityEntry::~BtCheckIntegrityEntry() {}
void BtCheckIntegrityEntry::onDownloadIncomplete void BtCheckIntegrityEntry::onDownloadIncomplete
(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e) (std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e)
{ {
const std::shared_ptr<PieceStorage>& ps = getRequestGroup()->getPieceStorage(); auto& ps = getRequestGroup()->getPieceStorage();
ps->onDownloadIncomplete(); ps->onDownloadIncomplete();
if(getRequestGroup()->getOption()->getAsBool(PREF_HASH_CHECK_ONLY)) { if(getRequestGroup()->getOption()->getAsBool(PREF_HASH_CHECK_ONLY)) {
return; return;
} }
const std::shared_ptr<DiskAdaptor>& diskAdaptor = ps->getDiskAdaptor(); const auto& diskAdaptor = ps->getDiskAdaptor();
if(diskAdaptor->isReadOnlyEnabled()) { if(diskAdaptor->isReadOnlyEnabled()) {
// Now reopen DiskAdaptor with read only disabled. // Now reopen DiskAdaptor with read only disabled.
diskAdaptor->closeFile(); diskAdaptor->closeFile();
diskAdaptor->disableReadOnly(); diskAdaptor->disableReadOnly();
diskAdaptor->openFile(); diskAdaptor->openFile();
} }
std::shared_ptr<BtFileAllocationEntry> entry proceedFileAllocation(commands,
(new BtFileAllocationEntry(getRequestGroup())); make_unique<BtFileAllocationEntry>
proceedFileAllocation(commands, entry, e); (getRequestGroup()),
e);
} }
void BtCheckIntegrityEntry::onDownloadFinished void BtCheckIntegrityEntry::onDownloadFinished
@ -77,9 +78,10 @@ void BtCheckIntegrityEntry::onDownloadFinished
// behavior. // behavior.
if(!getRequestGroup()->getOption()->getAsBool(PREF_HASH_CHECK_ONLY) && if(!getRequestGroup()->getOption()->getAsBool(PREF_HASH_CHECK_ONLY) &&
getRequestGroup()->getOption()->getAsBool(PREF_BT_HASH_CHECK_SEED)) { getRequestGroup()->getOption()->getAsBool(PREF_BT_HASH_CHECK_SEED)) {
std::shared_ptr<BtFileAllocationEntry> entry proceedFileAllocation(commands,
(new BtFileAllocationEntry(getRequestGroup())); make_unique<BtFileAllocationEntry>
proceedFileAllocation(commands, entry, e); (getRequestGroup()),
e);
} }
} }

View File

@ -50,22 +50,23 @@ namespace aria2 {
CheckIntegrityCommand::CheckIntegrityCommand CheckIntegrityCommand::CheckIntegrityCommand
(cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e, (cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e,
const std::shared_ptr<CheckIntegrityEntry>& entry): CheckIntegrityEntry* entry)
RealtimeCommand(cuid, requestGroup, e), : RealtimeCommand{cuid, requestGroup, e},
entry_(entry) entry_{entry}
{} {}
CheckIntegrityCommand::~CheckIntegrityCommand() {} CheckIntegrityCommand::~CheckIntegrityCommand()
{
getDownloadEngine()->getCheckIntegrityMan()->dropPickedEntry();
}
bool CheckIntegrityCommand::executeInternal() bool CheckIntegrityCommand::executeInternal()
{ {
if(getRequestGroup()->isHaltRequested()) { if(getRequestGroup()->isHaltRequested()) {
getDownloadEngine()->getCheckIntegrityMan()->dropPickedEntry();
return true; return true;
} }
entry_->validateChunk(); entry_->validateChunk();
if(entry_->finished()) { if(entry_->finished()) {
getDownloadEngine()->getCheckIntegrityMan()->dropPickedEntry();
// Enable control file saving here. See also // Enable control file saving here. See also
// RequestGroup::processCheckIntegrityEntry() to know why this is // RequestGroup::processCheckIntegrityEntry() to know why this is
// needed. // needed.
@ -95,7 +96,6 @@ bool CheckIntegrityCommand::executeInternal()
bool CheckIntegrityCommand::handleException(Exception& e) bool CheckIntegrityCommand::handleException(Exception& e)
{ {
getDownloadEngine()->getCheckIntegrityMan()->dropPickedEntry();
A2_LOG_ERROR_EX(fmt(MSG_FILE_VALIDATION_FAILURE, A2_LOG_ERROR_EX(fmt(MSG_FILE_VALIDATION_FAILURE,
getCuid()), getCuid()),
e); e);

View File

@ -45,12 +45,12 @@ class CheckIntegrityEntry;
class CheckIntegrityCommand : public RealtimeCommand { class CheckIntegrityCommand : public RealtimeCommand {
private: private:
std::shared_ptr<CheckIntegrityEntry> entry_; CheckIntegrityEntry* entry_;
public: public:
CheckIntegrityCommand(cuid_t cuid, CheckIntegrityCommand(cuid_t cuid,
RequestGroup* requestGroup, RequestGroup* requestGroup,
DownloadEngine* e, DownloadEngine* e,
const std::shared_ptr<CheckIntegrityEntry>& entry); CheckIntegrityEntry* entry);
virtual ~CheckIntegrityCommand(); virtual ~CheckIntegrityCommand();

View File

@ -47,17 +47,17 @@ CheckIntegrityDispatcherCommand::CheckIntegrityDispatcherCommand
(cuid_t cuid, (cuid_t cuid,
const std::shared_ptr<CheckIntegrityMan>& fileAllocMan, const std::shared_ptr<CheckIntegrityMan>& fileAllocMan,
DownloadEngine* e) DownloadEngine* e)
: SequentialDispatcherCommand<CheckIntegrityEntry>(cuid, fileAllocMan, e) : SequentialDispatcherCommand<CheckIntegrityEntry>{cuid, fileAllocMan, e}
{ {
setStatusRealtime(); setStatusRealtime();
} }
std::unique_ptr<Command> CheckIntegrityDispatcherCommand::createCommand std::unique_ptr<Command> CheckIntegrityDispatcherCommand::createCommand
(const std::shared_ptr<CheckIntegrityEntry>& entry) (CheckIntegrityEntry* entry)
{ {
cuid_t newCUID = getDownloadEngine()->newCUID(); cuid_t newCUID = getDownloadEngine()->newCUID();
A2_LOG_INFO(fmt("CUID#%" PRId64 " - Dispatching CheckIntegrityCommand CUID#%" PRId64 ".", A2_LOG_INFO(fmt("CUID#%" PRId64 " - Dispatching CheckIntegrityCommand "
getCuid(), newCUID)); "CUID#%" PRId64 ".", getCuid(), newCUID));
return make_unique<CheckIntegrityCommand> return make_unique<CheckIntegrityCommand>
(newCUID, entry->getRequestGroup(), getDownloadEngine(), entry); (newCUID, entry->getRequestGroup(), getDownloadEngine(), entry);
} }

View File

@ -50,8 +50,7 @@ public:
const std::shared_ptr<CheckIntegrityMan>& checkMan, const std::shared_ptr<CheckIntegrityMan>& checkMan,
DownloadEngine* e); DownloadEngine* e);
protected: protected:
virtual std::unique_ptr<Command> createCommand virtual std::unique_ptr<Command> createCommand(CheckIntegrityEntry* entry);
(const std::shared_ptr<CheckIntegrityEntry>& entry);
}; };
} // namespace aria2 } // namespace aria2

View File

@ -86,11 +86,11 @@ void CheckIntegrityEntry::cutTrailingGarbage()
void CheckIntegrityEntry::proceedFileAllocation void CheckIntegrityEntry::proceedFileAllocation
(std::vector<std::unique_ptr<Command>>& commands, (std::vector<std::unique_ptr<Command>>& commands,
const std::shared_ptr<FileAllocationEntry>& entry, std::unique_ptr<FileAllocationEntry> entry,
DownloadEngine* e) DownloadEngine* e)
{ {
if(getRequestGroup()->needsFileAllocation()) { if(getRequestGroup()->needsFileAllocation()) {
e->getFileAllocationMan()->pushEntry(entry); e->getFileAllocationMan()->pushEntry(std::move(entry));
} else { } else {
entry->prepareForNextAction(commands, e); entry->prepareForNextAction(commands, e);
} }

View File

@ -56,7 +56,7 @@ protected:
void setValidator(std::unique_ptr<IteratableValidator> validator); void setValidator(std::unique_ptr<IteratableValidator> validator);
void proceedFileAllocation(std::vector<std::unique_ptr<Command>>& commands, void proceedFileAllocation(std::vector<std::unique_ptr<Command>>& commands,
const std::shared_ptr<FileAllocationEntry>& entry, std::unique_ptr<FileAllocationEntry> entry,
DownloadEngine* e); DownloadEngine* e);
public: public:
CheckIntegrityEntry(RequestGroup* requestGroup, CheckIntegrityEntry(RequestGroup* requestGroup,

View File

@ -78,9 +78,10 @@ ChecksumCheckIntegrityEntry::onDownloadIncomplete
(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e) (std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e)
{ {
if(redownload_) { if(redownload_) {
std::shared_ptr<FileAllocationEntry> entry proceedFileAllocation(commands,
(new StreamFileAllocationEntry(getRequestGroup(), popNextCommand())); make_unique<StreamFileAllocationEntry>
proceedFileAllocation(commands, entry, e); (getRequestGroup(), popNextCommand()),
e);
} }
} }

View File

@ -330,8 +330,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
} }
{ {
const std::shared_ptr<FileAllocationEntry>& entry = auto& entry = e->getFileAllocationMan()->getPickedEntry();
e->getFileAllocationMan()->getPickedEntry();
if(entry) { if(entry) {
o << " [FileAlloc:#" o << " [FileAlloc:#"
<< GroupId::toAbbrevHex(entry->getRequestGroup()->getGID()) << " " << GroupId::toAbbrevHex(entry->getRequestGroup()->getGID()) << " "
@ -350,8 +349,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
} }
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
{ {
const std::shared_ptr<CheckIntegrityEntry>& entry = auto& entry = e->getCheckIntegrityMan()->getPickedEntry();
e->getCheckIntegrityMan()->getPickedEntry();
if(entry) { if(entry) {
o << " [Checksum:#" o << " [Checksum:#"
<< GroupId::toAbbrevHex(entry->getRequestGroup()->getGID()) << " " << GroupId::toAbbrevHex(entry->getRequestGroup()->getGID()) << " "

View File

@ -327,12 +327,12 @@ bool DownloadCommand::prepareForNextSegment() {
} }
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
if(getDownloadContext()->getPieceHashType().empty()) { if(getDownloadContext()->getPieceHashType().empty()) {
std::shared_ptr<CheckIntegrityEntry> entry auto entry = make_unique<ChecksumCheckIntegrityEntry>(getRequestGroup());
(new ChecksumCheckIntegrityEntry(getRequestGroup()));
if(entry->isValidationReady()) { if(entry->isValidationReady()) {
entry->initValidator(); entry->initValidator();
entry->cutTrailingGarbage(); entry->cutTrailingGarbage();
getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry); getDownloadEngine()->getCheckIntegrityMan()->pushEntry
(std::move(entry));
} }
} }
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST

View File

@ -54,16 +54,19 @@ namespace aria2 {
FileAllocationCommand::FileAllocationCommand FileAllocationCommand::FileAllocationCommand
(cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e, (cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e,
const std::shared_ptr<FileAllocationEntry>& fileAllocationEntry): FileAllocationEntry* fileAllocationEntry)
RealtimeCommand(cuid, requestGroup, e), : RealtimeCommand{cuid, requestGroup, e},
fileAllocationEntry_(fileAllocationEntry) {} fileAllocationEntry_{fileAllocationEntry}
{}
FileAllocationCommand::~FileAllocationCommand() {} FileAllocationCommand::~FileAllocationCommand()
{
getDownloadEngine()->getFileAllocationMan()->dropPickedEntry();
}
bool FileAllocationCommand::executeInternal() bool FileAllocationCommand::executeInternal()
{ {
if(getRequestGroup()->isHaltRequested()) { if(getRequestGroup()->isHaltRequested()) {
getDownloadEngine()->getFileAllocationMan()->dropPickedEntry();
return true; return true;
} }
fileAllocationEntry_->allocateChunk(); fileAllocationEntry_->allocateChunk();
@ -72,8 +75,6 @@ bool FileAllocationCommand::executeInternal()
(fmt(MSG_ALLOCATION_COMPLETED, (fmt(MSG_ALLOCATION_COMPLETED,
static_cast<long int>(timer_.difference(global::wallclock())), static_cast<long int>(timer_.difference(global::wallclock())),
getRequestGroup()->getTotalLength())); getRequestGroup()->getTotalLength()));
getDownloadEngine()->getFileAllocationMan()->dropPickedEntry();
std::vector<std::unique_ptr<Command>> commands; std::vector<std::unique_ptr<Command>> commands;
fileAllocationEntry_->prepareForNextAction(commands, getDownloadEngine()); fileAllocationEntry_->prepareForNextAction(commands, getDownloadEngine());
getDownloadEngine()->addCommand(std::move(commands)); getDownloadEngine()->addCommand(std::move(commands));
@ -87,7 +88,6 @@ bool FileAllocationCommand::executeInternal()
bool FileAllocationCommand::handleException(Exception& e) bool FileAllocationCommand::handleException(Exception& e)
{ {
getDownloadEngine()->getFileAllocationMan()->dropPickedEntry();
A2_LOG_ERROR_EX(fmt(MSG_FILE_ALLOCATION_FAILURE, A2_LOG_ERROR_EX(fmt(MSG_FILE_ALLOCATION_FAILURE,
getCuid()), getCuid()),
e); e);

View File

@ -47,12 +47,12 @@ class FileAllocationEntry;
class FileAllocationCommand : public RealtimeCommand { class FileAllocationCommand : public RealtimeCommand {
private: private:
std::shared_ptr<FileAllocationEntry> fileAllocationEntry_; FileAllocationEntry* fileAllocationEntry_;
Timer timer_; Timer timer_;
public: public:
FileAllocationCommand(cuid_t cuid, RequestGroup* requestGroup, FileAllocationCommand(cuid_t cuid, RequestGroup* requestGroup,
DownloadEngine* e, DownloadEngine* e,
const std::shared_ptr<FileAllocationEntry>& fileAllocationEntry); FileAllocationEntry* fileAllocationEntry);
virtual ~FileAllocationCommand(); virtual ~FileAllocationCommand();

View File

@ -47,11 +47,11 @@ FileAllocationDispatcherCommand::FileAllocationDispatcherCommand
(cuid_t cuid, (cuid_t cuid,
const std::shared_ptr<FileAllocationMan>& fileAllocMan, const std::shared_ptr<FileAllocationMan>& fileAllocMan,
DownloadEngine* e) DownloadEngine* e)
: SequentialDispatcherCommand<FileAllocationEntry>(cuid, fileAllocMan, e) : SequentialDispatcherCommand<FileAllocationEntry>{cuid, fileAllocMan, e}
{} {}
std::unique_ptr<Command> FileAllocationDispatcherCommand::createCommand std::unique_ptr<Command> FileAllocationDispatcherCommand::createCommand
(const std::shared_ptr<FileAllocationEntry>& entry) (FileAllocationEntry* entry)
{ {
cuid_t newCUID = getDownloadEngine()->newCUID(); cuid_t newCUID = getDownloadEngine()->newCUID();
A2_LOG_INFO(fmt(MSG_FILE_ALLOCATION_DISPATCH, newCUID)); A2_LOG_INFO(fmt(MSG_FILE_ALLOCATION_DISPATCH, newCUID));

View File

@ -50,8 +50,7 @@ public:
const std::shared_ptr<FileAllocationMan>& fileAllocMan, const std::shared_ptr<FileAllocationMan>& fileAllocMan,
DownloadEngine* e); DownloadEngine* e);
protected: protected:
virtual std::unique_ptr<Command> createCommand virtual std::unique_ptr<Command> createCommand(FileAllocationEntry* entry);
(const std::shared_ptr<FileAllocationEntry>& entry);
}; };
} // namespace aria2 } // namespace aria2

View File

@ -400,11 +400,12 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength)
getRequestGroup()->initPieceStorage(); getRequestGroup()->initPieceStorage();
if(getDownloadContext()->isChecksumVerificationNeeded()) { if(getDownloadContext()->isChecksumVerificationNeeded()) {
A2_LOG_DEBUG("Zero length file exists. Verify checksum."); A2_LOG_DEBUG("Zero length file exists. Verify checksum.");
std::shared_ptr<ChecksumCheckIntegrityEntry> entry auto entry = make_unique<ChecksumCheckIntegrityEntry>
(new ChecksumCheckIntegrityEntry(getRequestGroup())); (getRequestGroup());
entry->initValidator(); entry->initValidator();
getPieceStorage()->getDiskAdaptor()->openExistingFile(); getPieceStorage()->getDiskAdaptor()->openExistingFile();
getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry); getDownloadEngine()->getCheckIntegrityMan()->pushEntry
(std::move(entry));
sequence_ = SEQ_EXIT; sequence_ = SEQ_EXIT;
} else } else
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
@ -434,10 +435,11 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength)
// HttpResponseCommand::handleOtherEncoding() // HttpResponseCommand::handleOtherEncoding()
if(getDownloadContext()->isChecksumVerificationNeeded()) { if(getDownloadContext()->isChecksumVerificationNeeded()) {
A2_LOG_DEBUG("Verify checksum for zero-length file"); A2_LOG_DEBUG("Verify checksum for zero-length file");
std::shared_ptr<ChecksumCheckIntegrityEntry> entry auto entry = make_unique<ChecksumCheckIntegrityEntry>
(new ChecksumCheckIntegrityEntry(getRequestGroup())); (getRequestGroup());
entry->initValidator(); entry->initValidator();
getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry); getDownloadEngine()->getCheckIntegrityMan()->pushEntry
(std::move(entry));
sequence_ = SEQ_EXIT; sequence_ = SEQ_EXIT;
} else } else
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
@ -465,8 +467,7 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength)
return false; return false;
} }
std::shared_ptr<CheckIntegrityEntry> checkIntegrityEntry = auto checkIntegrityEntry = getRequestGroup()->createCheckIntegrityEntry();
getRequestGroup()->createCheckIntegrityEntry();
if(!checkIntegrityEntry) { if(!checkIntegrityEntry) {
sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED; sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED;
poolConnection(); poolConnection();
@ -478,7 +479,7 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength)
// AbstractCommand::execute() // AbstractCommand::execute()
getSegmentMan()->getSegmentWithIndex(getCuid(), 0); getSegmentMan()->getSegmentWithIndex(getCuid(), 0);
prepareForNextAction(checkIntegrityEntry); prepareForNextAction(std::move(checkIntegrityEntry));
disableReadCheckSocket(); disableReadCheckSocket();
} }

View File

@ -377,7 +377,7 @@ bool HttpResponseCommand::handleDefaultEncoding
getFileEntry()->poolRequest(getRequest()); getFileEntry()->poolRequest(getRequest());
} }
prepareForNextAction(checkEntry); prepareForNextAction(std::move(checkEntry));
if(getRequest()->getMethod() == Request::METHOD_HEAD) { if(getRequest()->getMethod() == Request::METHOD_HEAD) {
poolConnection(); poolConnection();
@ -421,11 +421,11 @@ bool HttpResponseCommand::handleOtherEncoding
// See also FtpNegotiationCommand::onFileSizeDetermined() // See also FtpNegotiationCommand::onFileSizeDetermined()
if(getDownloadContext()->isChecksumVerificationNeeded()) { if(getDownloadContext()->isChecksumVerificationNeeded()) {
A2_LOG_DEBUG("Zero length file exists. Verify checksum."); A2_LOG_DEBUG("Zero length file exists. Verify checksum.");
std::shared_ptr<ChecksumCheckIntegrityEntry> entry auto entry = make_unique<ChecksumCheckIntegrityEntry>
(new ChecksumCheckIntegrityEntry(getRequestGroup())); (getRequestGroup());
entry->initValidator(); entry->initValidator();
getPieceStorage()->getDiskAdaptor()->openExistingFile(); getPieceStorage()->getDiskAdaptor()->openExistingFile();
getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry); getDownloadEngine()->getCheckIntegrityMan()->pushEntry(std::move(entry));
} else } else
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
{ {
@ -455,10 +455,10 @@ bool HttpResponseCommand::handleOtherEncoding
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
if(getDownloadContext()->isChecksumVerificationNeeded()) { if(getDownloadContext()->isChecksumVerificationNeeded()) {
A2_LOG_DEBUG("Verify checksum for zero-length file"); A2_LOG_DEBUG("Verify checksum for zero-length file");
auto entry = std::make_shared<ChecksumCheckIntegrityEntry> auto entry = make_unique<ChecksumCheckIntegrityEntry>
(getRequestGroup()); (getRequestGroup());
entry->initValidator(); entry->initValidator();
getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry); getDownloadEngine()->getCheckIntegrityMan()->pushEntry(std::move(entry));
} else } else
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
{ {

View File

@ -216,18 +216,16 @@ void RequestGroup::closeFile()
// TODO The function name is not intuitive at all.. it does not convey // TODO The function name is not intuitive at all.. it does not convey
// that this function open file. // that this function open file.
std::shared_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry() std::unique_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
{ {
std::shared_ptr<BtProgressInfoFile> infoFile auto infoFile = std::make_shared<DefaultBtProgressInfoFile>
(new DefaultBtProgressInfoFile(downloadContext_, pieceStorage_, (downloadContext_, pieceStorage_, option_.get());
option_.get()));
std::shared_ptr<CheckIntegrityEntry> checkEntry;
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);
checkEntry.reset(new StreamCheckIntegrityEntry(this)); return make_unique<StreamCheckIntegrityEntry>(this);
} else if(isPreLocalFileCheckEnabled() && } else if(isPreLocalFileCheckEnabled() &&
(infoFile->exists() || (infoFile->exists() ||
(File(getFirstFilePath()).exists() && (File(getFirstFilePath()).exists() &&
@ -242,10 +240,9 @@ std::shared_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
if(downloadContext_->isChecksumVerificationNeeded()) { if(downloadContext_->isChecksumVerificationNeeded()) {
A2_LOG_INFO(MSG_HASH_CHECK_NOT_DONE); A2_LOG_INFO(MSG_HASH_CHECK_NOT_DONE);
ChecksumCheckIntegrityEntry* tempEntry = auto tempEntry = make_unique<ChecksumCheckIntegrityEntry>(this);
new ChecksumCheckIntegrityEntry(this);
tempEntry->setRedownload(true); tempEntry->setRedownload(true);
checkEntry.reset(tempEntry); return std::move(tempEntry);
} else } else
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
{ {
@ -253,9 +250,10 @@ std::shared_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
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;
} }
} else { } else {
checkEntry.reset(new StreamCheckIntegrityEntry(this)); return make_unique<StreamCheckIntegrityEntry>(this);
} }
} else } else
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
@ -263,17 +261,15 @@ std::shared_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
downloadContext_->isChecksumVerificationAvailable()) { downloadContext_->isChecksumVerificationAvailable()) {
pieceStorage_->markAllPiecesDone(); pieceStorage_->markAllPiecesDone();
loadAndOpenFile(infoFile); loadAndOpenFile(infoFile);
ChecksumCheckIntegrityEntry* tempEntry = auto tempEntry = make_unique<ChecksumCheckIntegrityEntry>(this);
new ChecksumCheckIntegrityEntry(this);
tempEntry->setRedownload(true); tempEntry->setRedownload(true);
checkEntry.reset(tempEntry); return std::move(tempEntry);
} else } else
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
{ {
loadAndOpenFile(infoFile); loadAndOpenFile(infoFile);
checkEntry.reset(new StreamCheckIntegrityEntry(this)); return make_unique<StreamCheckIntegrityEntry>(this);
} }
return checkEntry;
} }
void RequestGroup::createInitialCommand void RequestGroup::createInitialCommand
@ -379,11 +375,7 @@ void RequestGroup::createInitialCommand
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);
std::shared_ptr<CheckIntegrityEntry> entry
(new BtCheckIntegrityEntry(this));
entry->onDownloadIncomplete(commands, e);
return; return;
} }
removeDefunctControlFile(progressInfoFile); removeDefunctControlFile(progressInfoFile);
@ -454,14 +446,14 @@ void RequestGroup::createInitialCommand
e->addCommand(std::move(command)); e->addCommand(std::move(command));
} }
} }
std::shared_ptr<CheckIntegrityEntry> entry(new BtCheckIntegrityEntry(this)); auto entry = make_unique<BtCheckIntegrityEntry>(this);
// --bt-seed-unverified=true is given and download has completed, skip // --bt-seed-unverified=true is given and download has completed, skip
// validation for piece hashes. // validation for piece hashes.
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, entry, e); processCheckIntegrityEntry(commands, std::move(entry), e);
} }
return; return;
} }
@ -485,10 +477,9 @@ void RequestGroup::createInitialCommand
(downloadContext_, std::shared_ptr<PieceStorage>(), option_.get())); (downloadContext_, std::shared_ptr<PieceStorage>(), option_.get()));
adjustFilename(progressInfoFile); adjustFilename(progressInfoFile);
initPieceStorage(); initPieceStorage();
std::shared_ptr<CheckIntegrityEntry> checkEntry = auto checkEntry = createCheckIntegrityEntry();
createCheckIntegrityEntry();
if(checkEntry) { if(checkEntry) {
processCheckIntegrityEntry(commands, checkEntry, e); processCheckIntegrityEntry(commands, std::move(checkEntry), e);
} }
} }
} else { } else {
@ -538,15 +529,15 @@ void RequestGroup::createInitialCommand
} }
} }
progressInfoFile_ = progressInfoFile; progressInfoFile_ = progressInfoFile;
std::shared_ptr<CheckIntegrityEntry> checkIntegrityEntry processCheckIntegrityEntry(commands,
(new StreamCheckIntegrityEntry(this)); make_unique<StreamCheckIntegrityEntry>(this),
processCheckIntegrityEntry(commands, checkIntegrityEntry, e); e);
} }
} }
void RequestGroup::processCheckIntegrityEntry void RequestGroup::processCheckIntegrityEntry
(std::vector<std::unique_ptr<Command>>& commands, (std::vector<std::unique_ptr<Command>>& commands,
const std::shared_ptr<CheckIntegrityEntry>& entry, std::unique_ptr<CheckIntegrityEntry> entry,
DownloadEngine* e) DownloadEngine* e)
{ {
int64_t actualFileSize = pieceStorage_->getDiskAdaptor()->size(); int64_t actualFileSize = pieceStorage_->getDiskAdaptor()->size();
@ -565,7 +556,7 @@ void RequestGroup::processCheckIntegrityEntry
// enableSaveControlFile() will be called after hash checking is // enableSaveControlFile() will be called after hash checking is
// done. See CheckIntegrityCommand. // done. See CheckIntegrityCommand.
disableSaveControlFile(); disableSaveControlFile();
e->getCheckIntegrityMan()->pushEntry(entry); e->getCheckIntegrityMan()->pushEntry(std::move(entry));
} else } else
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
{ {

View File

@ -210,7 +210,7 @@ public:
return segmentMan_; return segmentMan_;
} }
std::shared_ptr<CheckIntegrityEntry> createCheckIntegrityEntry(); std::unique_ptr<CheckIntegrityEntry> createCheckIntegrityEntry();
// Returns first bootstrap commands to initiate a download. // Returns first bootstrap commands to initiate a download.
// If this is HTTP/FTP download and file size is unknown, only 1 command // If this is HTTP/FTP download and file size is unknown, only 1 command
@ -387,7 +387,7 @@ public:
void processCheckIntegrityEntry void processCheckIntegrityEntry
(std::vector<std::unique_ptr<Command>>& commands, (std::vector<std::unique_ptr<Command>>& commands,
const std::shared_ptr<CheckIntegrityEntry>& entry, std::unique_ptr<CheckIntegrityEntry> entry,
DownloadEngine* e); DownloadEngine* e);
// Initializes pieceStorage_ and segmentMan_. We guarantee that // Initializes pieceStorage_ and segmentMan_. We guarantee that

View File

@ -59,10 +59,11 @@ protected:
return e_; return e_;
} }
public: public:
SequentialDispatcherCommand(cuid_t cuid, SequentialDispatcherCommand
const std::shared_ptr<SequentialPicker<T> >& picker, (cuid_t cuid,
DownloadEngine* e): const std::shared_ptr<SequentialPicker<T>>& picker,
Command(cuid), picker_(picker), e_(e) DownloadEngine* e)
: Command{cuid}, picker_{picker}, e_{e}
{ {
setStatusRealtime(); setStatusRealtime();
} }
@ -83,8 +84,7 @@ public:
} }
protected: protected:
virtual std::unique_ptr<Command> createCommand virtual std::unique_ptr<Command> createCommand(T* entry) = 0;
(const std::shared_ptr<T>& entry) = 0;
}; };
} // namespace aria2 } // namespace aria2

View File

@ -45,15 +45,15 @@ namespace aria2 {
template<typename T> template<typename T>
class SequentialPicker { class SequentialPicker {
private: private:
std::deque<std::shared_ptr<T> > entries_; std::deque<std::unique_ptr<T>> entries_;
std::shared_ptr<T> pickedEntry_; std::unique_ptr<T> pickedEntry_;
public: public:
bool isPicked() const bool isPicked() const
{ {
return pickedEntry_.get(); return pickedEntry_.get();
} }
const std::shared_ptr<T>& getPickedEntry() const const std::unique_ptr<T>& getPickedEntry() const
{ {
return pickedEntry_; return pickedEntry_;
} }
@ -68,20 +68,19 @@ public:
return !entries_.empty(); return !entries_.empty();
} }
std::shared_ptr<T> pickNext() T* pickNext()
{ {
std::shared_ptr<T> r;
if(hasNext()) { if(hasNext()) {
r = entries_.front(); pickedEntry_ = std::move(entries_.front());
entries_.pop_front(); entries_.pop_front();
pickedEntry_ = r; return pickedEntry_.get();
} }
return r; return nullptr;
} }
void pushEntry(const std::shared_ptr<T>& entry) void pushEntry(std::unique_ptr<T> entry)
{ {
entries_.push_back(entry); entries_.push_back(std::move(entry));
} }
size_t countEntryInQueue() const size_t countEntryInQueue() const

View File

@ -52,14 +52,15 @@ StreamCheckIntegrityEntry::~StreamCheckIntegrityEntry() {}
void StreamCheckIntegrityEntry::onDownloadIncomplete void StreamCheckIntegrityEntry::onDownloadIncomplete
(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e) (std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e)
{ {
const std::shared_ptr<PieceStorage>& ps = getRequestGroup()->getPieceStorage(); auto& ps = getRequestGroup()->getPieceStorage();
ps->onDownloadIncomplete(); ps->onDownloadIncomplete();
if(getRequestGroup()->getOption()->getAsBool(PREF_HASH_CHECK_ONLY)) { if(getRequestGroup()->getOption()->getAsBool(PREF_HASH_CHECK_ONLY)) {
return; return;
} }
std::shared_ptr<FileAllocationEntry> entry proceedFileAllocation(commands,
(new StreamFileAllocationEntry(getRequestGroup(), popNextCommand())); make_unique<StreamFileAllocationEntry>
proceedFileAllocation(commands, entry, e); (getRequestGroup(), popNextCommand()),
e);
} }
void StreamCheckIntegrityEntry::onDownloadFinished void StreamCheckIntegrityEntry::onDownloadFinished

View File

@ -2,9 +2,9 @@
#include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/HelperMacros.h>
namespace aria2 { #include "a2functional.h"
typedef std::shared_ptr<int> Integer; namespace aria2 {
class SequentialPickerTest:public CppUnit::TestFixture { class SequentialPickerTest:public CppUnit::TestFixture {
@ -26,8 +26,8 @@ void SequentialPickerTest::testPick()
CPPUNIT_ASSERT(!picker.hasNext()); CPPUNIT_ASSERT(!picker.hasNext());
CPPUNIT_ASSERT_EQUAL((size_t)0, picker.countEntryInQueue()); CPPUNIT_ASSERT_EQUAL((size_t)0, picker.countEntryInQueue());
picker.pushEntry(Integer(new int(1))); picker.pushEntry(make_unique<int>(1));
picker.pushEntry(Integer(new int(2))); picker.pushEntry(make_unique<int>(2));
CPPUNIT_ASSERT(picker.hasNext()); CPPUNIT_ASSERT(picker.hasNext());
CPPUNIT_ASSERT_EQUAL((size_t)2, picker.countEntryInQueue()); CPPUNIT_ASSERT_EQUAL((size_t)2, picker.countEntryInQueue());
@ -35,7 +35,7 @@ void SequentialPickerTest::testPick()
picker.pickNext(); picker.pickNext();
CPPUNIT_ASSERT(picker.isPicked()); CPPUNIT_ASSERT(picker.isPicked());
CPPUNIT_ASSERT_EQUAL(*Integer(new int(1)), *picker.getPickedEntry()); CPPUNIT_ASSERT_EQUAL(1, *picker.getPickedEntry());
picker.dropPickedEntry(); picker.dropPickedEntry();
@ -44,7 +44,7 @@ void SequentialPickerTest::testPick()
picker.pickNext(); picker.pickNext();
CPPUNIT_ASSERT_EQUAL(*Integer(new int(2)), *picker.getPickedEntry()); CPPUNIT_ASSERT_EQUAL(2, *picker.getPickedEntry());
CPPUNIT_ASSERT(!picker.hasNext()); CPPUNIT_ASSERT(!picker.hasNext());
} }