Ver código fonte

Better error message when local file status cannot be retrieved

Tatsuhiro Tsujikawa 8 anos atrás
pai
commit
744a5a550d
3 arquivos alterados com 22 adições e 1 exclusões
  1. 10 0
      src/File.cc
  2. 7 0
      src/File.h
  3. 5 1
      src/OptionHandlerImpl.cc

+ 10 - 0
src/File.cc

@@ -79,6 +79,16 @@ bool File::exists()
   return fillStat(fstat) == 0;
 }
 
+bool File::exists(std::string &err)
+{
+  a2_struct_stat fstat;
+  if (fillStat(fstat) != 0) {
+    err = fmt("Could not get file status: %s", strerror(errno));
+    return false;
+  }
+  return true;
+}
+
 bool File::isFile()
 {
   a2_struct_stat fstat;

+ 7 - 0
src/File.h

@@ -70,6 +70,13 @@ public:
    */
   bool exists();
 
+  /**
+   * Tests whether the file or directory denoted by name exists.  If
+   * file does not exist, or file status could not be retrieved, this
+   * function stores error message to |err|.
+   */
+  bool exists(std::string& err);
+
   /**
    * Tests whether the file denoted by name is a regular file.
    */

+ 5 - 1
src/OptionHandlerImpl.cc

@@ -551,7 +551,11 @@ void LocalFilePathOptionHandler::parseArg(Option& option,
     auto path = util::replace(optarg, "${HOME}", util::getHomeDir());
     if (mustExist_) {
       File f(path);
-      if (!f.exists() || f.isDir()) {
+      std::string err;
+      if (!f.exists(err)) {
+        throw DL_ABORT_EX(err);
+      }
+      if (f.isDir()) {
         throw DL_ABORT_EX(fmt(MSG_NOT_FILE, optarg.c_str()));
       }
     }