瀏覽代碼

LibCore: Add File::ensure_directories()

Tim Schumacher 3 年之前
父節點
當前提交
62f0fa818d
共有 2 個文件被更改,包括 14 次插入8 次删除
  1. 13 8
      Userland/Libraries/LibCore/File.cpp
  2. 1 0
      Userland/Libraries/LibCore/File.h

+ 13 - 8
Userland/Libraries/LibCore/File.cpp

@@ -183,17 +183,22 @@ String File::real_path_for(String const& filename)
 
 bool File::ensure_parent_directories(String const& path)
 {
-    VERIFY(path.starts_with("/"));
-
-    int saved_errno = 0;
-    ScopeGuard restore_errno = [&saved_errno] { errno = saved_errno; };
-
     char* parent_buffer = strdup(path.characters());
     ScopeGuard free_buffer = [parent_buffer] { free(parent_buffer); };
 
     char const* parent = dirname(parent_buffer);
 
-    int rc = mkdir(parent, 0755);
+    return ensure_directories(parent);
+}
+
+bool File::ensure_directories(String const& path)
+{
+    VERIFY(path.starts_with("/"));
+
+    int saved_errno = 0;
+    ScopeGuard restore_errno = [&saved_errno] { errno = saved_errno; };
+
+    int rc = mkdir(path.characters(), 0755);
     saved_errno = errno;
 
     if (rc == 0 || errno == EEXIST)
@@ -202,12 +207,12 @@ bool File::ensure_parent_directories(String const& path)
     if (errno != ENOENT)
         return false;
 
-    bool ok = ensure_parent_directories(parent);
+    bool ok = ensure_parent_directories(path);
     saved_errno = errno;
     if (!ok)
         return false;
 
-    rc = mkdir(parent, 0755);
+    rc = mkdir(path.characters(), 0755);
     saved_errno = errno;
     return rc == 0;
 }

+ 1 - 0
Userland/Libraries/LibCore/File.h

@@ -38,6 +38,7 @@ public:
     static bool exists(String const& filename);
     static ErrorOr<size_t> size(String const& filename);
     static bool ensure_parent_directories(String const& path);
+    static bool ensure_directories(String const& path);
     static String current_working_directory();
     static String absolute_path(String const& path);