diff options
author | Denis Kenzior <denkenz@gmail.com> | 2010-03-16 17:19:01 -0500 |
---|---|---|
committer | Denis Kenzior <denkenz@gmail.com> | 2010-03-16 17:19:01 -0500 |
commit | 68b01ce93edc44ba60f97b183424f5d7e23a1cc5 (patch) | |
tree | c5abf177466cd8e93fb1400a862c97f7919e20c2 /src/storage.c | |
parent | 62007cd074e33e01e4c3f5336b1735a3d56d5292 (diff) | |
download | ofono-68b01ce93edc44ba60f97b183424f5d7e23a1cc5.tar.bz2 |
Refactor: create_dirs logic
- Remove reference to g_malloc and replace with g_try_malloc
- Be extra pedantic on the inputs
- Refactor the logic to make it slightly easier to follow
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; |