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 2012/10/10 01:59:43 UTC

svn commit: r1396423 - in /incubator/mesos/trunk: src/linux/cgroups.cpp third_party/libprocess/include/stout/os.hpp

Author: benh
Date: Tue Oct  9 23:59:43 2012
New Revision: 1396423

URL: http://svn.apache.org/viewvc?rev=1396423&view=rev
Log:
Refactored os::mkdir and os::rmdir to take a 'bool recursive' flag to
differentiate 'mkdir -p' and 'rm -r' semantics (contributed by Jie Yu,
https://reviews.apache.org/r/7338).

Modified:
    incubator/mesos/trunk/src/linux/cgroups.cpp
    incubator/mesos/trunk/third_party/libprocess/include/stout/os.hpp

Modified: incubator/mesos/trunk/src/linux/cgroups.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/linux/cgroups.cpp?rev=1396423&r1=1396422&r2=1396423&view=diff
==============================================================================
--- incubator/mesos/trunk/src/linux/cgroups.cpp (original)
+++ incubator/mesos/trunk/src/linux/cgroups.cpp Tue Oct  9 23:59:43 2012
@@ -188,7 +188,9 @@ static Try<Nothing> createCgroup(const s
                                  const std::string& cgroup)
 {
   std::string path = hierarchy + "/" + cgroup;
-  Try<Nothing> mkdir = os::mkdir(path);
+
+  // Do NOT recursively create cgroups.
+  Try<Nothing> mkdir = os::mkdir(path, false);
 
   if (mkdir.isError()) {
     return Try<Nothing>::error(
@@ -213,7 +215,9 @@ static Try<Nothing> removeCgroup(const s
                                  const std::string& cgroup)
 {
   std::string path = hierarchy + "/" + cgroup;
-  Try<Nothing> rmdir = os::rmdir(path);
+
+  // Do NOT recursively remove cgroups.
+  Try<Nothing> rmdir = os::rmdir(path, false);
 
   if (rmdir.isError()) {
     return Try<Nothing>::error(

Modified: incubator/mesos/trunk/third_party/libprocess/include/stout/os.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/include/stout/os.hpp?rev=1396423&r1=1396422&r2=1396423&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/include/stout/os.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/include/stout/os.hpp Tue Oct  9 23:59:43 2012
@@ -406,9 +406,13 @@ inline Try<long> mtime(const std::string
 }
 
 
-inline Try<Nothing> mkdir(const std::string& directory)
+inline Try<Nothing> mkdir(const std::string& directory, bool recursive = true)
 {
-  try {
+  if (!recursive) {
+    if (::mkdir(directory.c_str(), 0755) < 0) {
+      return Try<Nothing>::error(strerror(errno));
+    }
+  } else {
     std::vector<std::string> tokens = strings::tokenize(directory, "/");
     std::string path = "";
 
@@ -420,13 +424,10 @@ inline Try<Nothing> mkdir(const std::str
     foreach (const std::string& token, tokens) {
       path += token;
       if (::mkdir(path.c_str(), 0755) < 0 && errno != EEXIST) {
-        return Try<Nothing>::error(
-            "Failed to mkdir: '" + path + "': " + strerror(errno));
+        return Try<Nothing>::error(strerror(errno));
       }
       path += "/";
     }
-  } catch (...) {
-    return Try<Nothing>::error("");
   }
 
   return Nothing();
@@ -449,42 +450,49 @@ inline Try<std::string> mkdtemp(const st
   }
 }
 
-// Recursively deletes a directory akin to: 'rm -r'. Note that this
-// function expects an absolute path.
-inline Try<Nothing> rmdir(const std::string& directory)
-{
-  char* paths[] = {const_cast<char*>(directory.c_str()), NULL};
-
-  FTS* tree = fts_open(paths, FTS_NOCHDIR, NULL);
-  if (tree == NULL) {
-    return Try<Nothing>::error(strerror(errno));
-  }
-
-  FTSENT* node;
-  while ((node = fts_read(tree)) != NULL) {
-    switch (node->fts_info) {
-      case FTS_DP:
-        if (::rmdir(node->fts_path) < 0 && errno != ENOENT) {
-          return Try<Nothing>::error(strerror(errno));
-        }
-        break;
-      case FTS_F:
-      case FTS_SL:
-        if (::unlink(node->fts_path) < 0 && errno != ENOENT) {
-          return Try<Nothing>::error(strerror(errno));
-        }
-        break;
-      default:
-        break;
+// By default, recursively deletes a directory akin to: 'rm -r'. If the
+// programmer sets recursive to false, it deletes a directory akin to: 'rmdir'.
+// Note that this function expects an absolute path.
+inline Try<Nothing> rmdir(const std::string& directory, bool recursive = true)
+{
+  if (!recursive) {
+    if (::rmdir(directory.c_str()) < 0) {
+      return Try<Nothing>::error(strerror(errno));
     }
-  }
+  } else {
+    char* paths[] = {const_cast<char*>(directory.c_str()), NULL};
 
-  if (errno != 0) {
-    return Try<Nothing>::error(strerror(errno));
-  }
+    FTS* tree = fts_open(paths, FTS_NOCHDIR, NULL);
+    if (tree == NULL) {
+      return Try<Nothing>::error(strerror(errno));
+    }
 
-  if (fts_close(tree) < 0) {
-    return Try<Nothing>::error(strerror(errno));
+    FTSENT* node;
+    while ((node = fts_read(tree)) != NULL) {
+      switch (node->fts_info) {
+        case FTS_DP:
+          if (::rmdir(node->fts_path) < 0 && errno != ENOENT) {
+            return Try<Nothing>::error(strerror(errno));
+          }
+          break;
+        case FTS_F:
+        case FTS_SL:
+          if (::unlink(node->fts_path) < 0 && errno != ENOENT) {
+            return Try<Nothing>::error(strerror(errno));
+          }
+          break;
+        default:
+          break;
+      }
+    }
+
+    if (errno != 0) {
+      return Try<Nothing>::error(strerror(errno));
+    }
+
+    if (fts_close(tree) < 0) {
+      return Try<Nothing>::error(strerror(errno));
+    }
   }
 
   return Nothing();