You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2013/11/08 02:21:37 UTC

[4/4] git commit: Revised os::realpath to return Result for when path does not exist.

Revised os::realpath to return Result for when path does not exist.

Review: https://reviews.apache.org/r/15302


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/f69eab21
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/f69eab21
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/f69eab21

Branch: refs/heads/master
Commit: f69eab21808ce9cb2c9cc8290d9d944c5daaaa5d
Parents: a7dcb99
Author: Benjamin Hindman <be...@gmail.com>
Authored: Wed Nov 6 21:30:41 2013 -1000
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Thu Nov 7 15:19:20 2013 -1000

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/os.hpp         |  5 +-
 src/cli/mesos.cpp                               |  2 +-
 src/files/files.cpp                             | 13 +++--
 src/linux/cgroups.cpp                           | 59 ++++++++++++--------
 src/slave/process_isolator.cpp                  |  9 ++-
 src/slave/slave.cpp                             |  8 ++-
 src/slave/state.cpp                             | 20 ++++---
 src/tests/flags.hpp                             |  2 +-
 src/tests/script.cpp                            |  7 ++-
 9 files changed, 80 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/f69eab21/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
index 18d8b7f..4ce9344 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
@@ -361,10 +361,13 @@ inline Try<std::string> dirname(const std::string& path)
 }
 
 
-inline Try<std::string> realpath(const std::string& path)
+inline Result<std::string> realpath(const std::string& path)
 {
   char temp[PATH_MAX];
   if (::realpath(path.c_str(), temp) == NULL) {
+    if (errno == ENOENT || errno == ENOTDIR) {
+      return None();
+    }
     return ErrnoError();
   }
   return std::string(temp);

http://git-wip-us.apache.org/repos/asf/mesos/blob/f69eab21/src/cli/mesos.cpp
----------------------------------------------------------------------
diff --git a/src/cli/mesos.cpp b/src/cli/mesos.cpp
index fbdc3b9..171a707 100644
--- a/src/cli/mesos.cpp
+++ b/src/cli/mesos.cpp
@@ -57,7 +57,7 @@ int main(int argc, char** argv)
   // the path).
   Try<string> dirname = os::dirname(argv[0]);
   if (dirname.isSome()) {
-    Try<string> realpath = os::realpath(dirname.get());
+    Result<string> realpath = os::realpath(dirname.get());
     if (realpath.isSome()) {
       os::setenv("PATH", realpath.get() + ":" + os::getenv("PATH"));
     }

http://git-wip-us.apache.org/repos/asf/mesos/blob/f69eab21/src/files/files.cpp
----------------------------------------------------------------------
diff --git a/src/files/files.cpp b/src/files/files.cpp
index 69d6852..6b85842 100644
--- a/src/files/files.cpp
+++ b/src/files/files.cpp
@@ -113,11 +113,14 @@ void FilesProcess::initialize()
 
 Future<Nothing> FilesProcess::attach(const string& path, const string& name)
 {
-  Try<string> result = os::realpath(path);
+  Result<string> result = os::realpath(path);
 
-  if (result.isError()) {
+  if (!result.isSome()) {
     return Future<Nothing>::failed(
-        "Failed to get realpath of '" + path + "': " + result.error());
+        "Failed to get realpath of '" + path + "': " +
+        (result.isError()
+         ? result.error()
+         : "No such file or directory"));
   }
 
   // Make sure we have permissions to read the file/dir.
@@ -409,11 +412,13 @@ Result<string> FilesProcess::resolve(const string& path)
       path = path::join(path, suffix);
 
       // Canonicalize the absolute path.
-      Try<string> realpath = os::realpath(path);
+      Result<string> realpath = os::realpath(path);
       if (realpath.isError()) {
         return Error(
             "Failed to determine canonical path of '" + path +
             "': " + realpath.error());
+      } else if (realpath.isNone()) {
+        return None();
       }
 
       // Make sure the canonicalized absolute path is accessible

http://git-wip-us.apache.org/repos/asf/mesos/blob/f69eab21/src/linux/cgroups.cpp
----------------------------------------------------------------------
diff --git a/src/linux/cgroups.cpp b/src/linux/cgroups.cpp
index f1aec3f..8379aa3 100644
--- a/src/linux/cgroups.cpp
+++ b/src/linux/cgroups.cpp
@@ -458,11 +458,13 @@ Try<set<string> > hierarchies()
   set<string> results;
   foreach (const fs::MountTable::Entry& entry, table.get().entries) {
     if (entry.type == "cgroup") {
-      Try<string> realpath = os::realpath(entry.dir);
-      if (realpath.isError()) {
+      Result<string> realpath = os::realpath(entry.dir);
+      if (!realpath.isSome()) {
         return Error(
-            "Failed to determine canonical path of " + entry.dir +
-            ": " + realpath.error());
+            "Failed to determine canonical path of " + entry.dir + ": " +
+            (realpath.isError()
+             ? realpath.error()
+             : "No such file or directory"));
       }
       results.insert(realpath.get());
     }
@@ -573,11 +575,13 @@ Try<set<string> > subsystems()
 Try<set<string> > subsystems(const string& hierarchy)
 {
   // We compare the canonicalized absolute paths.
-  Try<string> hierarchyAbsPath = os::realpath(hierarchy);
-  if (hierarchyAbsPath.isError()) {
+  Result<string> hierarchyAbsPath = os::realpath(hierarchy);
+  if (!hierarchyAbsPath.isSome()) {
     return Error(
-        "Failed to determine canonical path of '" + hierarchy +
-        "': " + hierarchyAbsPath.error());
+        "Failed to determine canonical path of '" + hierarchy + "': " +
+        (hierarchyAbsPath.isError()
+         ? hierarchyAbsPath.error()
+         : "No such file or directory"));
   }
 
   // Read currently mounted file systems from /proc/mounts.
@@ -590,11 +594,13 @@ Try<set<string> > subsystems(const string& hierarchy)
   Option<fs::MountTable::Entry> hierarchyEntry;
   foreach (const fs::MountTable::Entry& entry, table.get().entries) {
     if (entry.type == "cgroup") {
-      Try<string> dirAbsPath = os::realpath(entry.dir);
-      if (dirAbsPath.isError()) {
+      Result<string> dirAbsPath = os::realpath(entry.dir);
+      if (!dirAbsPath.isSome()) {
         return Error(
-            "Failed to determine canonical path of '" + entry.dir +
-            "': " + dirAbsPath.error());
+            "Failed to determine canonical path of '" + entry.dir + "': " +
+            (dirAbsPath.isError()
+             ? dirAbsPath.error()
+             : "No such file or directory"));
       }
 
       // Seems that a directory can be mounted more than once. Previous mounts
@@ -676,11 +682,13 @@ Try<bool> mounted(const string& hierarchy, const string& subsystems)
   }
 
   // We compare canonicalized absolute paths.
-  Try<string> realpath = os::realpath(hierarchy);
-  if (realpath.isError()) {
+  Result<string> realpath = os::realpath(hierarchy);
+  if (!realpath.isSome()) {
     return Error(
-        "Failed to determine canonical path of '" + hierarchy +
-        "': " + realpath.error());
+        "Failed to determine canonical path of '" + hierarchy + "': " +
+        (realpath.isError()
+         ? realpath.error()
+         : "No such file or directory"));
   }
 
   Try<set<string> > hierarchies = cgroups::hierarchies();
@@ -760,18 +768,23 @@ Try<vector<string> > get(const string& hierarchy, const string& cgroup)
     return Error(error.get());
   }
 
-  Try<string> hierarchyAbsPath = os::realpath(hierarchy);
-  if (hierarchyAbsPath.isError()) {
+  Result<string> hierarchyAbsPath = os::realpath(hierarchy);
+  if (!hierarchyAbsPath.isSome()) {
     return Error(
-        "Failed to determine canonical path of '" + hierarchy +
-        "': " + hierarchyAbsPath.error());
+        "Failed to determine canonical path of '" + hierarchy + "': " +
+        (hierarchyAbsPath.isError()
+         ? hierarchyAbsPath.error()
+         : "No such file or directory"));
   }
 
-  Try<string> destAbsPath = os::realpath(path::join(hierarchy, cgroup));
-  if (destAbsPath.isError()) {
+  Result<string> destAbsPath = os::realpath(path::join(hierarchy, cgroup));
+  if (!destAbsPath.isSome()) {
     return Error(
         "Failed to determine canonical path of '" +
-        path::join(hierarchy, cgroup) + "': " + destAbsPath.error());
+        path::join(hierarchy, cgroup) + "': " +
+        (destAbsPath.isError()
+         ? destAbsPath.error()
+         : "No such file or directory"));
   }
 
   char* paths[] = { const_cast<char*>(destAbsPath.get().c_str()), NULL };

http://git-wip-us.apache.org/repos/asf/mesos/blob/f69eab21/src/slave/process_isolator.cpp
----------------------------------------------------------------------
diff --git a/src/slave/process_isolator.cpp b/src/slave/process_isolator.cpp
index fa80293..a6e9ed6 100644
--- a/src/slave/process_isolator.cpp
+++ b/src/slave/process_isolator.cpp
@@ -211,14 +211,17 @@ void ProcessIsolator::launchExecutor(
     const char** args = (const char**) new char*[2];
 
     // Determine path for mesos-launcher.
-    Try<string> realpath = os::realpath(
+    Result<string> realpath = os::realpath(
         path::join(flags.launcher_dir, "mesos-launcher"));
 
-    if (realpath.isError()) {
+    if (!realpath.isSome()) {
       EXIT(1) << "Failed to determine the canonical path "
               << "for the mesos-launcher '"
               << path::join(flags.launcher_dir, "mesos-launcher")
-              << "': " << realpath.error();
+              << "': "
+              << (realpath.isError()
+                  ? realpath.error()
+                  : "No such file or directory");
     }
 
     // Grab a copy of the path so that we can reliably use 'c_str()'.

http://git-wip-us.apache.org/repos/asf/mesos/blob/f69eab21/src/slave/slave.cpp
----------------------------------------------------------------------
diff --git a/src/slave/slave.cpp b/src/slave/slave.cpp
index 90575db..13cb418 100644
--- a/src/slave/slave.cpp
+++ b/src/slave/slave.cpp
@@ -1998,14 +1998,18 @@ ExecutorInfo Slave::getExecutorInfo(
     // echo the error and exit).
     executor.mutable_command()->MergeFrom(task.command());
 
-    Try<string> path = os::realpath(
+    Result<string> path = os::realpath(
         path::join(flags.launcher_dir, "mesos-executor"));
 
     if (path.isSome()) {
       executor.mutable_command()->set_value(path.get());
     } else {
       executor.mutable_command()->set_value(
-          "echo '" + path.error() + "'; exit 1");
+          "echo '" +
+          (path.isError()
+           ? path.error()
+           : "No such file or directory") +
+          "'; exit 1");
     }
 
     // TODO(benh): Set some resources for the executor so that a task

http://git-wip-us.apache.org/repos/asf/mesos/blob/f69eab21/src/slave/state.cpp
----------------------------------------------------------------------
diff --git a/src/slave/state.cpp b/src/slave/state.cpp
index c675b4c..bf267b5 100644
--- a/src/slave/state.cpp
+++ b/src/slave/state.cpp
@@ -50,9 +50,12 @@ Result<SlaveState> recover(const string& rootDir, bool strict)
   }
 
   // Get the latest slave id.
-  Try<string> directory = os::realpath(latest);
-  if (directory.isError()) {
-    return Error("Failed to find latest slave: " + directory.error());
+  Result<string> directory = os::realpath(latest);
+  if (!directory.isSome()) {
+    return Error("Failed to find latest slave: " +
+                 (directory.isError()
+                  ? directory.error()
+                  : "No such file or directory"));
   }
 
   SlaveID slaveId;
@@ -309,11 +312,14 @@ Try<ExecutorState> ExecutorState::recover(
   // Recover the runs.
   foreach (const string& path, runs.get()) {
     if (os::basename(path).get() == paths::LATEST_SYMLINK) {
-      const Try<string>& latest = os::realpath(path);
-      if (latest.isError()) {
+      const Result<string>& latest = os::realpath(path);
+      if (!latest.isSome()) {
         return Error(
-            "Failed to find latest run of executor '" + executorId.value() +
-            "': " + latest.error());
+            "Failed to find latest run of executor '" +
+            executorId.value() + "': " +
+            (latest.isError()
+             ? latest.error()
+             : "No such file or directory"));
       }
 
       // Store the UUID of the latest executor run.

http://git-wip-us.apache.org/repos/asf/mesos/blob/f69eab21/src/tests/flags.hpp
----------------------------------------------------------------------
diff --git a/src/tests/flags.hpp b/src/tests/flags.hpp
index 071f138..9760a7a 100644
--- a/src/tests/flags.hpp
+++ b/src/tests/flags.hpp
@@ -47,7 +47,7 @@ public:
     // preprocessor definitions (at the time this comment was written
     // these were set via '-DSOURCE_DIR=...' and '-DBUILD_DIR=...' in
     // src/Makefile.am).
-    Try<std::string> path = os::realpath(SOURCE_DIR);
+    Result<std::string> path = os::realpath(SOURCE_DIR);
     CHECK_SOME(path);
     add(&Flags::source_dir,
         "source_dir",

http://git-wip-us.apache.org/repos/asf/mesos/blob/f69eab21/src/tests/script.cpp
----------------------------------------------------------------------
diff --git a/src/tests/script.cpp b/src/tests/script.cpp
index b3bbf87..09c7f3b 100644
--- a/src/tests/script.cpp
+++ b/src/tests/script.cpp
@@ -53,11 +53,12 @@ void execute(const string& script)
   }
 
   // Determine the path for the script.
-  Try<string> path =
+  Result<string> path =
     os::realpath(path::join(flags.source_dir, "src", "tests", script));
 
-  if (path.isError()) {
-    FAIL() << "Failed to locate script: " << path.error();
+  if (!path.isSome()) {
+    FAIL() << "Failed to locate script: "
+           << (path.isError() ? path.error() : "No such file or directory");
   }
 
   // Fork a process to change directory and run the test.