diff --git a/ChangeLog b/ChangeLog index b45c3aca..3c9fc87a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2010-07-16 Tatsuhiro Tsujikawa + + Moved executeHook and executeHookByOptName to util. + * src/RequestGroupMan.cc + * src/util.cc + * src/util.h + 2010-07-16 Tatsuhiro Tsujikawa Added --conditional-get option. Download file only when the local diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index 8f8d2573..a1a8f6a4 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -250,71 +250,17 @@ bool RequestGroupMan::removeReservedGroup(gid_t gid) } } -static void executeHook(const std::string& command, gid_t gid) -{ - LogFactory::getInstance()->info("Executing user command: %s %s", - command.c_str(), util::itos(gid).c_str()); -#ifndef __MINGW32__ - pid_t cpid = fork(); - if(cpid == -1) { - LogFactory::getInstance()->error("fork() failed." - " Cannot execute user command."); - } else if(cpid == 0) { - execl(command.c_str(), command.c_str(), util::itos(gid).c_str(), - reinterpret_cast(0)); - perror(("Could not execute user command: "+command).c_str()); - exit(EXIT_FAILURE); - } -#else - PROCESS_INFORMATION pi; - STARTUPINFO si; - - memset(&si, 0, sizeof (si)); - si.cb = sizeof(STARTUPINFO); - - memset(&pi, 0, sizeof (pi)); - - std::string cmdline = command; - strappend(cmdline, " ", util::itos(gid)); - - DWORD rc = CreateProcess( - NULL, - (LPSTR)cmdline.c_str(), - NULL, - NULL, - true, - NULL, - NULL, - 0, - &si, - &pi); - - if(!rc) - LogFactory::getInstance()->error("CreateProcess() failed." - " Cannot execute user command."); -#endif -} - -static void executeHookByOptName -(const SharedHandle& group, const Option* option, - const std::string& opt) -{ - if(!option->blank(opt)) { - executeHook(option->get(opt), group->getGID()); - } -} - static void executeStopHook (const SharedHandle& result, const Option* option) { if(result->result == downloadresultcode::FINISHED && !option->blank(PREF_ON_DOWNLOAD_COMPLETE)) { - executeHook(option->get(PREF_ON_DOWNLOAD_COMPLETE), result->gid); + util::executeHook(option->get(PREF_ON_DOWNLOAD_COMPLETE), result->gid); } else if(result->result != downloadresultcode::IN_PROGRESS && !option->blank(PREF_ON_DOWNLOAD_ERROR)) { - executeHook(option->get(PREF_ON_DOWNLOAD_ERROR), result->gid); + util::executeHook(option->get(PREF_ON_DOWNLOAD_ERROR), result->gid); } else if(!option->blank(PREF_ON_DOWNLOAD_STOP)) { - executeHook(option->get(PREF_ON_DOWNLOAD_STOP), result->gid); + util::executeHook(option->get(PREF_ON_DOWNLOAD_STOP), result->gid); } } @@ -400,7 +346,8 @@ public: group->releaseRuntimeResource(e_); if(group->isPauseRequested()) { group->setForceHaltRequested(false); - executeHookByOptName(group, e_->getOption(), PREF_ON_DOWNLOAD_PAUSE); + util::executeHookByOptName + (group, e_->getOption(), PREF_ON_DOWNLOAD_PAUSE); // TODO Should we have to prepend spend uris to remaining uris // in case PREF_REUSE_URI is disabed? } else { @@ -546,7 +493,8 @@ void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e) ++count; e->addCommand(commands); commands.clear(); - executeHookByOptName(groupToAdd, e->getOption(), PREF_ON_DOWNLOAD_START); + util::executeHookByOptName + (groupToAdd, e->getOption(), PREF_ON_DOWNLOAD_START); } catch(RecoverableException& ex) { logger_->error(EX_EXCEPTION_CAUGHT, ex); if(logger_->debug()) { diff --git a/src/util.cc b/src/util.cc index 2300234a..ea708f31 100644 --- a/src/util.cc +++ b/src/util.cc @@ -78,6 +78,8 @@ #include "bitfield.h" #include "DownloadHandlerConstants.h" #include "RequestGroup.h" +#include "LogFactory.h" +#include "Option.h" #ifdef ENABLE_MESSAGE_DIGEST # include "MessageDigestHelper.h" #endif // ENABLE_MESSAGE_DIGEST @@ -1344,6 +1346,60 @@ void removeMetalinkContentTypes(const SharedHandle& group) } } +void executeHook(const std::string& command, gid_t gid) +{ + LogFactory::getInstance()->info("Executing user command: %s %s", + command.c_str(), util::itos(gid).c_str()); +#ifndef __MINGW32__ + pid_t cpid = fork(); + if(cpid == -1) { + LogFactory::getInstance()->error("fork() failed." + " Cannot execute user command."); + } else if(cpid == 0) { + execl(command.c_str(), command.c_str(), util::itos(gid).c_str(), + reinterpret_cast(0)); + perror(("Could not execute user command: "+command).c_str()); + exit(EXIT_FAILURE); + } +#else + PROCESS_INFORMATION pi; + STARTUPINFO si; + + memset(&si, 0, sizeof (si)); + si.cb = sizeof(STARTUPINFO); + + memset(&pi, 0, sizeof (pi)); + + std::string cmdline = command; + strappend(cmdline, " ", util::itos(gid)); + + DWORD rc = CreateProcess( + NULL, + (LPSTR)cmdline.c_str(), + NULL, + NULL, + true, + NULL, + NULL, + 0, + &si, + &pi); + + if(!rc) + LogFactory::getInstance()->error("CreateProcess() failed." + " Cannot execute user command."); +#endif +} + +void executeHookByOptName +(const SharedHandle& group, const Option* option, + const std::string& opt) +{ + if(!option->blank(opt)) { + executeHook(option->get(opt), group->getGID()); + } +} + } // namespace util } // namespace aria2 diff --git a/src/util.h b/src/util.h index 9e1c2a5f..4767d0c7 100644 --- a/src/util.h +++ b/src/util.h @@ -63,6 +63,7 @@ class BitfieldMan; class BinaryStream; class FileEntry; class RequestGroup; +class Option; #define STRTOLL(X) strtoll(X, reinterpret_cast(0), 10) #define STRTOULL(X) strtoull(X, reinterpret_cast(0), 10) @@ -395,6 +396,12 @@ bool inSameCidrBlock(const std::string& ip1, const std::string& ip2, int bits); void removeMetalinkContentTypes(const SharedHandle& group); +void executeHook(const std::string& command, gid_t gid); + +void executeHookByOptName +(const SharedHandle& group, const Option* option, + const std::string& opt); + } // namespace util } // namespace aria2