]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add create_parent_dirs()
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 7 Oct 2010 19:49:07 +0000 (21:49 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Thu, 7 Oct 2010 19:49:07 +0000 (21:49 +0200)
ccache.h
util.c

index 03f63763e550cf65a2bafa97afa94347fa99e75e..8611429e5586e0a2c7e5d86691a76550d47a721d 100644 (file)
--- a/ccache.h
+++ b/ccache.h
@@ -109,6 +109,7 @@ int move_uncompressed_file(const char *src, const char *dest,
 bool file_is_compressed(const char *filename);
 
 int create_dir(const char *dir);
+int create_parent_dirs(const char *path);
 const char *get_hostname(void);
 const char *tmp_string(void);
 char *format_hash_as_string(const unsigned char *hash, unsigned size);
diff --git a/util.c b/util.c
index e09b2cf06407edf7b449c2883300d8d4451ea3c8..5a5b7dc86d34377721033580e066b34d1a53592d 100644 (file)
--- a/util.c
+++ b/util.c
@@ -406,6 +406,33 @@ create_dir(const char *dir)
        return 0;
 }
 
+/* Create directories leading to path. Returns 0 on success, otherwise -1. */
+int
+create_parent_dirs(const char *path)
+{
+       struct stat st;
+       int res;
+       char *parent = dirname(path);
+
+       if (stat(parent, &st) == 0) {
+               if (S_ISDIR(st.st_mode)) {
+                       res = 0;
+               } else {
+                       res = -1;
+                       errno = ENOTDIR;
+               }
+       } else {
+               res = create_parent_dirs(parent);
+               if (res == 0) {
+                       res = mkdir(parent, 0777);
+               } else {
+                       res = -1;
+               }
+       }
+       free(parent);
+       return res;
+}
+
 /*
  * Return a static string with the current hostname.
  */