Pass the number of requested files as 2nd argument to the command specified in

--on-download-* hook.

As a consequence, first filename is passed as 3rd argument.
pull/1/head
Tatsuhiro Tsujikawa 2010-12-04 19:00:48 +09:00
parent 0cdfc3d104
commit c5ef9dadbd
3 changed files with 52 additions and 8 deletions

View File

@ -211,6 +211,30 @@ const std::string& DownloadContext::getBasePath() const
} }
} }
SharedHandle<FileEntry>
DownloadContext::getFirstRequestedFileEntry() const
{
for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
if((*i)->isRequested()) {
return *i;
}
}
return SharedHandle<FileEntry>();
}
size_t DownloadContext::countRequestedFileEntry() const
{
size_t numFiles = 0;
for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
if((*i)->isRequested()) {
++numFiles;
}
}
return numFiles;
}
bool DownloadContext::isChecksumVerificationNeeded() const bool DownloadContext::isChecksumVerificationNeeded() const
{ {
return pieceHashAlgo_.empty() && return pieceHashAlgo_.empty() &&

View File

@ -127,6 +127,13 @@ public:
return fileEntries_[0]; return fileEntries_[0];
} }
// This function returns first FileEntry whose isRequested() returns
// true. If there is no such FileEntry, returns
// SharedHandle<FileEntry>().
SharedHandle<FileEntry> getFirstRequestedFileEntry() const;
size_t countRequestedFileEntry() const;
template<typename InputIterator> template<typename InputIterator>
void setFileEntries(InputIterator first, InputIterator last) void setFileEntries(InputIterator first, InputIterator last)
{ {

View File

@ -1457,19 +1457,27 @@ namespace {
void executeHook void executeHook
(const std::string& command, (const std::string& command,
const std::string& gid, gid_t gid,
size_t numFiles,
const std::string& firstFilename) const std::string& firstFilename)
{ {
A2_LOG_INFO(fmt("Executing user command: %s %s %s", const std::string gidStr = util::itos(gid);
const std::string numFilesStr = util::uitos(numFiles);
A2_LOG_INFO(fmt("Executing user command: %s %s %s %s",
command.c_str(), command.c_str(),
gid.c_str(), gidStr.c_str(),
numFilesStr.c_str(),
firstFilename.c_str())); firstFilename.c_str()));
#ifndef __MINGW32__ #ifndef __MINGW32__
pid_t cpid = fork(); pid_t cpid = fork();
if(cpid == -1) { if(cpid == -1) {
A2_LOG_ERROR("fork() failed. Cannot execute user command."); A2_LOG_ERROR("fork() failed. Cannot execute user command.");
} else if(cpid == 0) { } else if(cpid == 0) {
execl(command.c_str(), command.c_str(), gid.c_str(), firstFilename.c_str(), execl(command.c_str(),
command.c_str(),
gidStr.c_str(),
numFilesStr.c_str(),
firstFilename.c_str(),
reinterpret_cast<char*>(0)); reinterpret_cast<char*>(0));
perror(("Could not execute user command: "+command).c_str()); perror(("Could not execute user command: "+command).c_str());
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -1484,7 +1492,7 @@ void executeHook
memset(&pi, 0, sizeof (pi)); memset(&pi, 0, sizeof (pi));
std::string cmdline = command; std::string cmdline = command;
strappend(cmdline, " ", gid, " \"", firstFilename, "\""); strappend(cmdline, " ", gidStr, " ", numFilesStr, " \"", firstFilename, "\"");
DWORD rc = CreateProcess( DWORD rc = CreateProcess(
NULL, NULL,
@ -1519,10 +1527,15 @@ void executeHookByOptName
if(!option->blank(opt)) { if(!option->blank(opt)) {
const SharedHandle<DownloadContext> dctx = group->getDownloadContext(); const SharedHandle<DownloadContext> dctx = group->getDownloadContext();
std::string firstFilename; std::string firstFilename;
if(!group->inMemoryDownload() && !dctx->getFileEntries().empty()) { size_t numFiles = 0;
firstFilename = group->getFirstFilePath(); if(!group->inMemoryDownload()) {
SharedHandle<FileEntry> file = dctx->getFirstRequestedFileEntry();
if(file) {
firstFilename = file->getPath();
}
numFiles = dctx->countRequestedFileEntry();
} }
executeHook(option->get(opt), util::itos(group->getGID()), firstFilename); executeHook(option->get(opt), group->getGID(), numFiles, firstFilename);
} }
} }