diff options
Diffstat (limited to 'src/storage.c')
-rw-r--r-- | src/storage.c | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/src/storage.c b/src/storage.c index 300c6eb4..618c111f 100644 --- a/src/storage.c +++ b/src/storage.c @@ -42,22 +42,31 @@ int create_dirs(const char *filename, const mode_t mode) const char *prev, *next; int err; + if (filename[0] != '/') + return -1; + err = stat(filename, &st); if (!err && S_ISREG(st.st_mode)) return 0; - dir = g_malloc(strlen(filename) + 1); + dir = g_try_malloc(strlen(filename) + 1); + if (dir == NULL) + return -1; + strcpy(dir, "/"); - for (prev = filename; (next = strchr(prev + 1, '/')); prev = next) - if (next > prev + 1) { - strncat(dir, prev + 1, next - prev); + for (prev = filename; (next = strchr(prev + 1, '/')); prev = next) { + /* Skip consecutive '/' characters */ + if (next - prev == 1) + continue; + + strncat(dir, prev + 1, next - prev); - if (mkdir(dir, mode) && errno != EEXIST) { - g_free(dir); - return -1; - } + if (mkdir(dir, mode) == -1 && errno != EEXIST) { + g_free(dir); + return -1; } + } g_free(dir); return 0; |