[llvm] r201347 - Use mkdir instead of stat+mkdir.

Rafael Espindola rafael.espindola at gmail.com
Thu Feb 13 08:58:19 PST 2014


Author: rafael
Date: Thu Feb 13 10:58:19 2014
New Revision: 201347

URL: http://llvm.org/viewvc/llvm-project?rev=201347&view=rev
Log:
Use mkdir instead of stat+mkdir.

This is an optimistic version of create_diretories: it tries to create the
directory first and looks at the parent only if that fails.

Running strace on "mkdir -p" shows that it is pessimistic, calling mkdir on
every element of the path. We could implement that if needed.

In any case, with both strategies there is no reason to call stat, just check
the return of mkdir.

Modified:
    llvm/trunk/lib/Support/Path.cpp

Modified: llvm/trunk/lib/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Path.cpp?rev=201347&r1=201346&r2=201347&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Path.cpp (original)
+++ llvm/trunk/lib/Support/Path.cpp Thu Feb 13 10:58:19 2014
@@ -751,20 +751,27 @@ error_code make_absolute(SmallVectorImpl
                    "occurred above!");
 }
 
-error_code create_directories(const Twine &path, bool &existed) {
-  SmallString<128> path_storage;
-  StringRef p = path.toStringRef(path_storage);
-
-  StringRef parent = path::parent_path(p);
-  if (!parent.empty()) {
-    bool parent_exists;
-    if (error_code ec = fs::exists(parent, parent_exists)) return ec;
-
-    if (!parent_exists)
-      if (error_code ec = create_directories(parent, existed)) return ec;
-  }
+error_code create_directories(const Twine &Path, bool &Existed) {
+  SmallString<128> PathStorage;
+  StringRef P = Path.toStringRef(PathStorage);
 
-  return create_directory(p, existed);
+  // Be optimistic and try to create the directory
+  error_code EC = create_directory(P, Existed);
+  // If we succeeded, or had any error other than the parent not existing, just
+  // return it.
+  if (EC != errc::no_such_file_or_directory)
+    return EC;
+
+  // We failed because of a no_such_file_or_directory, try to create the
+  // parent.
+  StringRef Parent = path::parent_path(P);
+  if (Parent.empty())
+    return EC;
+
+  if ((EC = create_directories(Parent)))
+      return EC;
+
+  return create_directory(P, Existed);
 }
 
 bool exists(file_status status) {





More information about the llvm-commits mailing list