From: Joel Rosdahl Date: Thu, 7 Oct 2010 19:49:07 +0000 (+0200) Subject: Add create_parent_dirs() X-Git-Tag: v3.2~304 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f375665ef8786071411bdc326af83b01291b2036;p=thirdparty%2Fccache.git Add create_parent_dirs() --- diff --git a/ccache.h b/ccache.h index 03f63763e..8611429e5 100644 --- 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 e09b2cf06..5a5b7dc86 100644 --- 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. */