You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ch...@apache.org on 2018/10/31 18:28:49 UTC

[mesos] 02/04: Stout: Added a sync option for `mkdir`.

This is an automated email from the ASF dual-hosted git repository.

chhsiao pushed a commit to branch 1.7.x
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 182dd84c6fcf6390c158bfb669b2f4c0f45e2b37
Author: Chun-Hung Hsiao <ch...@mesosphere.io>
AuthorDate: Fri Oct 19 09:28:38 2018 -0700

    Stout: Added a sync option for `mkdir`.
    
    To ensure the directories created by `mkdir` are commited to their
    filesystems, an `fsync` will be called on the parent of each created
    directory if the `sync` option is set to true. This option has no
    effect on Windows.
    
    Review: https://reviews.apache.org/r/69085
---
 3rdparty/stout/include/stout/os/posix/mkdir.hpp   | 40 ++++++++++++++++++++---
 3rdparty/stout/include/stout/os/windows/mkdir.hpp |  7 ++--
 2 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/3rdparty/stout/include/stout/os/posix/mkdir.hpp b/3rdparty/stout/include/stout/os/posix/mkdir.hpp
index 418db9a..806ec39 100644
--- a/3rdparty/stout/include/stout/os/posix/mkdir.hpp
+++ b/3rdparty/stout/include/stout/os/posix/mkdir.hpp
@@ -19,21 +19,44 @@
 #include <vector>
 
 #include <stout/error.hpp>
+#include <stout/foreach.hpp>
 #include <stout/nothing.hpp>
 #include <stout/strings.hpp>
+#include <stout/path.hpp>
 #include <stout/try.hpp>
 
 #include <stout/os/constants.hpp>
-
+#include <stout/os/fsync.hpp>
 
 namespace os {
 
-inline Try<Nothing> mkdir(const std::string& directory, bool recursive = true)
+// Make a directory.
+//
+// If `recursive` is set to true, all intermediate directories will be created
+// as required. If `sync` is set to true, `fsync()` will be called on the parent
+// of each created directory to ensure that the result is committed to its
+// filesystem.
+//
+// NOTE: This function doesn't ensure that any existing directory is committed
+// to its filesystem, and it does not perform any cleanup in case of a failure.
+inline Try<Nothing> mkdir(
+    const std::string& directory,
+    bool recursive = true,
+    bool sync = false)
 {
   if (!recursive) {
     if (::mkdir(directory.c_str(), 0755) < 0) {
       return ErrnoError();
     }
+
+    if (sync) {
+      const std::string parent = Path(directory).dirname();
+      Try<Nothing> fsync = os::fsync(parent);
+      if (fsync.isError()) {
+        return Error(
+            "Failed to fsync directory '" + parent + "': " + fsync.error());
+      }
+    }
   } else {
     std::vector<std::string> tokens =
       strings::tokenize(directory, stringify(os::PATH_SEPARATOR));
@@ -47,8 +70,17 @@ inline Try<Nothing> mkdir(const std::string& directory, bool recursive = true)
 
     foreach (const std::string& token, tokens) {
       path += token;
-      if (::mkdir(path.c_str(), 0755) < 0 && errno != EEXIST) {
-        return ErrnoError();
+      if (::mkdir(path.c_str(), 0755) < 0) {
+        if (errno != EEXIST) {
+          return ErrnoError();
+        }
+      } else if (sync) {
+        const std::string parent = Path(path).dirname();
+        Try<Nothing> fsync = os::fsync(parent);
+        if (fsync.isError()) {
+          return Error(
+              "Failed to fsync directory '" + parent + "': " + fsync.error());
+        }
       }
 
       path += os::PATH_SEPARATOR;
diff --git a/3rdparty/stout/include/stout/os/windows/mkdir.hpp b/3rdparty/stout/include/stout/os/windows/mkdir.hpp
index 2aef22a..77d292c 100644
--- a/3rdparty/stout/include/stout/os/windows/mkdir.hpp
+++ b/3rdparty/stout/include/stout/os/windows/mkdir.hpp
@@ -27,10 +27,13 @@
 
 #include <stout/internal/windows/longpath.hpp>
 
-
 namespace os {
 
-inline Try<Nothing> mkdir(const std::string& directory, bool recursive = true)
+// NOTE: `sync` has no effect on Windows.
+inline Try<Nothing> mkdir(
+    const std::string& directory,
+    bool recursive = true,
+    bool sync = false)
 {
   if (!recursive) {
     // NOTE: We check for existence because parts of certain directories