]> sourceware.org Git - systemtap.git/commitdiff
create_dir: create path components if they do not exist.
authorDave Brolley <brolley@redhat.com>
Thu, 12 Aug 2010 21:05:51 +0000 (17:05 -0400)
committerDave Brolley <brolley@redhat.com>
Fri, 20 Aug 2010 15:18:20 +0000 (11:18 -0400)
util.cxx
util.h

index 26f312ab9dbbfb778513d36dbe075aa8f00eda37..b241ddb4a765cff8f0c9ea85244d09a976c99463 100644 (file)
--- a/util.cxx
+++ b/util.cxx
@@ -22,6 +22,7 @@
 #include <map>
 #include <string>
 #include <fstream>
+#include <cassert>
 
 extern "C" {
 #include <fcntl.h>
@@ -168,7 +169,7 @@ error:
 
 // Make sure a directory exists.
 int
-create_dir(const char *dir)
+create_dir(const char *dir, int mode)
 {
   struct stat st;
   if (stat(dir, &st) == 0)
@@ -179,8 +180,24 @@ create_dir(const char *dir)
       return 1;
     }
 
-  if (mkdir(dir, 0777) != 0 && errno != EEXIST)
-    return 1;
+  // Create the directory. We must create each component
+  // of the path ourselves.
+  vector<string> components;
+  tokenize (dir, components, "/");
+  string path;
+  if (*dir == '/')
+    {
+      // Absolute path
+      path = "/";
+    }
+  unsigned limit = components.size ();
+  assert (limit != 0);
+  for (unsigned ix = 0; ix < limit; ++ix)
+    {
+      path += components[ix] + '/';
+      if (mkdir(path.c_str (), mode) != 0 && errno != EEXIST)
+       return 1;
+    }
 
   return 0;
 }
diff --git a/util.h b/util.h
index c8bafbc1a9dd2cf8de15a7213ee2ea83958cef23..fd2e0e15122717a07c99d8134e8c846984b89341 100644 (file)
--- a/util.h
+++ b/util.h
@@ -12,7 +12,7 @@ size_t get_file_size(int fd);
 bool file_exists (const std::string &path);
 bool copy_file(const std::string& src, const std::string& dest,
                bool verbose=false);
-int create_dir(const char *dir);
+int create_dir(const char *dir, int mode = 0777);
 int remove_file_or_dir(const char *dir);
 bool in_group_id (gid_t target_gid);
 std::string getmemusage ();
This page took 0.028445 seconds and 5 git commands to generate.