From 8d4f29d3024c328c892b37f7b7db27fbde022e51 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 20 Jun 2009 16:47:54 +0000 Subject: [PATCH] 2009-06-21 Tatsuhiro Tsujikawa The default value of --dir option is the absolute path to the current directory. If getcwd() is failed, then it is ".", which is the same value with old implementation. This change is necessary because after daemon() call, the current working directory is changed to /. * src/File.cc * src/File.h * src/OptionHandlerFactory.cc * src/option_processing.cc --- ChangeLog | 12 ++++++++++++ src/File.cc | 18 ++++++++++++++++++ src/File.h | 5 +++++ src/OptionHandlerFactory.cc | 2 +- src/option_processing.cc | 5 +++++ 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 0cc22bc8..05148c00 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2009-06-21 Tatsuhiro Tsujikawa + + The default value of --dir option is the absolute path to the + current directory. If getcwd() is failed, then it is ".", which is + the same value with old implementation. This change is necessary + because after daemon() call, the current working directory is + changed to /. + * src/File.cc + * src/File.h + * src/OptionHandlerFactory.cc + * src/option_processing.cc + 2009-06-21 Tatsuhiro Tsujikawa Call daemon() with arguments(0,0), which means daemon() changes diff --git a/src/File.cc b/src/File.cc index 705059da..5550213e 100644 --- a/src/File.cc +++ b/src/File.cc @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -44,6 +45,7 @@ #include "Util.h" #include "A2STR.h" +#include "array_fun.h" namespace aria2 { @@ -201,4 +203,20 @@ Time File::getModifiedTime() return Time(fstat.st_mtime); } +std::string File::getCurrentDir() +{ + size_t buflen = 256; + while(buflen <= 2048) { + array_ptr buf(new char[buflen]); + if(getcwd(buf, buflen)) { + return std::string(buf); + } else if(errno == ERANGE) { + buflen *= 2; + } else { + break; + } + } + return A2STR::DOT_C; +} + } // namespace aria2 diff --git a/src/File.h b/src/File.h index 31bd90a0..ecb2700b 100644 --- a/src/File.h +++ b/src/File.h @@ -109,6 +109,11 @@ public: bool utime(const Time& actime, const Time& modtime) const; Time getModifiedTime(); + + // Returns the current working directory. If the current working + // directory cannot be retrieved or its length is larger than 2048, + // returns ".". + static std::string getCurrentDir(); }; } // namespace aria2 diff --git a/src/OptionHandlerFactory.cc b/src/OptionHandlerFactory.cc index c63905ad..b55c8941 100644 --- a/src/OptionHandlerFactory.cc +++ b/src/OptionHandlerFactory.cc @@ -141,7 +141,7 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers() SharedHandle op(new DefaultOptionHandler (PREF_DIR, TEXT_DIR, - ".", + File::getCurrentDir(), A2STR::NIL, OptionHandler::REQ_ARG, 'd')); diff --git a/src/option_processing.cc b/src/option_processing.cc index 14613c80..8b27f197 100644 --- a/src/option_processing.cc +++ b/src/option_processing.cc @@ -191,6 +191,11 @@ void option_processing(Option& op, std::deque& uris, } #ifdef HAVE_DAEMON if(op.getAsBool(PREF_DAEMON)) { + if(File::getCurrentDir() == ".") { + std::cerr << "Failed to get the current working directory." + << " With -D option engaged," + << " the default value of --dir option is /." << std::endl; + } if(daemon(0, 0) < 0) { perror(MSG_DAEMON_FAILED); exit(DownloadResult::UNKNOWN_ERROR);