浏览代码

2009-06-21 Tatsuhiro Tsujikawa <[email protected]>

	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
Tatsuhiro Tsujikawa 16 年之前
父节点
当前提交
8d4f29d302
共有 5 个文件被更改,包括 41 次插入1 次删除
  1. 12 0
      ChangeLog
  2. 18 0
      src/File.cc
  3. 5 0
      src/File.h
  4. 1 1
      src/OptionHandlerFactory.cc
  5. 5 0
      src/option_processing.cc

+ 12 - 0
ChangeLog

@@ -1,3 +1,15 @@
+2009-06-21  Tatsuhiro Tsujikawa  <[email protected]>
+
+	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  <[email protected]>
 2009-06-21  Tatsuhiro Tsujikawa  <[email protected]>
 
 
 	Call daemon() with arguments(0,0), which means daemon() changes
 	Call daemon() with arguments(0,0), which means daemon() changes

+ 18 - 0
src/File.cc

@@ -37,6 +37,7 @@
 #include <stdlib.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/types.h>
 #include <utime.h>
 #include <utime.h>
+#include <unistd.h>
 
 
 #include <deque>
 #include <deque>
 #include <cstring>
 #include <cstring>
@@ -44,6 +45,7 @@
 
 
 #include "Util.h"
 #include "Util.h"
 #include "A2STR.h"
 #include "A2STR.h"
+#include "array_fun.h"
 
 
 namespace aria2 {
 namespace aria2 {
 
 
@@ -201,4 +203,20 @@ Time File::getModifiedTime()
   return Time(fstat.st_mtime);
   return Time(fstat.st_mtime);
 }
 }
 
 
+std::string File::getCurrentDir()
+{
+  size_t buflen = 256;
+  while(buflen <= 2048) {
+    array_ptr<char> 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
 } // namespace aria2

+ 5 - 0
src/File.h

@@ -109,6 +109,11 @@ public:
   bool utime(const Time& actime, const Time& modtime) const;
   bool utime(const Time& actime, const Time& modtime) const;
 
 
   Time getModifiedTime();
   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
 } // namespace aria2

+ 1 - 1
src/OptionHandlerFactory.cc

@@ -141,7 +141,7 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers()
     SharedHandle<OptionHandler> op(new DefaultOptionHandler
     SharedHandle<OptionHandler> op(new DefaultOptionHandler
 				   (PREF_DIR,
 				   (PREF_DIR,
 				    TEXT_DIR,
 				    TEXT_DIR,
-				    ".",
+				    File::getCurrentDir(),
 				    A2STR::NIL,
 				    A2STR::NIL,
 				    OptionHandler::REQ_ARG,
 				    OptionHandler::REQ_ARG,
 				    'd'));
 				    'd'));

+ 5 - 0
src/option_processing.cc

@@ -191,6 +191,11 @@ void option_processing(Option& op, std::deque<std::string>& uris,
   }
   }
 #ifdef HAVE_DAEMON
 #ifdef HAVE_DAEMON
   if(op.getAsBool(PREF_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) {
     if(daemon(0, 0) < 0) {
       perror(MSG_DAEMON_FAILED);
       perror(MSG_DAEMON_FAILED);
       exit(DownloadResult::UNKNOWN_ERROR);
       exit(DownloadResult::UNKNOWN_ERROR);