From 92709b2b2f579300b1c007e5a3ba869e78fcc922 Mon Sep 17 00:00:00 2001 From: Bert Date: Thu, 7 Apr 2011 17:29:59 +0200 Subject: Use directory structure in cache dir --- util.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'util.c') diff --git a/util.c b/util.c index 517b4ba..42d1384 100644 --- a/util.c +++ b/util.c @@ -18,6 +18,8 @@ #include #include +#include +#include #include #include @@ -156,6 +158,47 @@ end: return path; } +int create_dir_rec(const char *path) { + char *dir, *d; + struct stat stats; + int err = 0; + + if (!path || !*path) + return -1; + + if (!stat(path, &stats)) { + if (S_ISDIR(stats.st_mode)) { + return 0; + } else { + warn("not a directory: %s", path); + return -1; + } + } + + d = dir = (char*) s_malloc(strlen(path) + 1); + strcpy(dir, path); + + while (d != NULL && !err) { + d = strchr(d + 1, '/'); + if (d != NULL) + *d = '\0'; + if (access(dir, F_OK) && errno == ENOENT) { + if (mkdir(dir, 0755)) { + warn("could not create directory: %s", dir); + err = -1; + } + } else if (stat(dir, &stats) || !S_ISDIR(stats.st_mode)) { + warn("not a directory: %s", dir); + err = -1; + } + if (d != NULL) + *d = '/'; + } + free(dir); + + return err; +} + char* readline(FILE *stream) { size_t len; char *buf, *s, *end; -- cgit v1.2.3-54-g00ecf