From 6254fb56225ff4501f70d7844cbfee9feb5e0ff6 Mon Sep 17 00:00:00 2001 From: Adam Sutton Date: Fri, 2 Nov 2012 15:37:11 +0000 Subject: [PATCH] util: Added a generic makedirs replacement for the 2 diff implementations. --- src/dvr/dvr_rec.c | 51 +---------------------------------------------- src/settings.c | 19 +++++++----------- src/tvheadend.h | 2 ++ src/utils.c | 36 +++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 62 deletions(-) diff --git a/src/dvr/dvr_rec.c b/src/dvr/dvr_rec.c index 925d721d..6dc560dd 100755 --- a/src/dvr/dvr_rec.c +++ b/src/dvr/dvr_rec.c @@ -117,55 +117,6 @@ dvr_rec_unsubscribe(dvr_entry_t *de, int stopcode) } -/** - * - */ -static int -makedirs(const char *path) -{ - struct stat st; - char *p; - int l, r; - - if(stat(path, &st) == 0 && S_ISDIR(st.st_mode)) - return 0; /* Dir already there */ - - if(mkdir(path, 0777) == 0) - return 0; /* Dir created ok */ - - if(errno == ENOENT) { - - /* Parent does not exist, try to create it */ - /* Allocate new path buffer and strip off last directory component */ - - l = strlen(path); - p = alloca(l + 1); - memcpy(p, path, l); - p[l--] = 0; - - for(; l >= 0; l--) - if(p[l] == '/') - break; - if(l == 0) { - return ENOENT; - } - p[l] = 0; - - if((r = makedirs(p)) != 0) - return r; - - /* Try again */ - if(mkdir(path, 0777) == 0) - return 0; /* Dir created ok */ - } - r = errno; - - tvhlog(LOG_ERR, "dvr", "Unable to create directory \"%s\" -- %s", - path, strerror(r)); - return r; -} - - /** * Replace various chars with a dash */ @@ -247,7 +198,7 @@ pvr_generate_filename(dvr_entry_t *de, const streaming_start_t *ss) /* */ - if(makedirs(path) != 0) { + if(makedirs(path, 0777) != 0) { return -1; } diff --git a/src/settings.c b/src/settings.c index 8f0de39b..3857a383 100644 --- a/src/settings.c +++ b/src/settings.c @@ -83,23 +83,18 @@ hts_settings_init(const char *confpath) int hts_settings_makedirs ( const char *inpath ) { - size_t x; - struct stat st; + size_t x = strlen(inpath) - 1; char path[512]; - size_t l = strlen(inpath); strcpy(path, inpath); - for(x = 0; x < l; x++) { - if(path[x] == '/' && x != 0) { + + while (x) { + if (path[x] == '/') { path[x] = 0; - if(stat(path, &st) && mkdir(path, 0700)) { - tvhlog(LOG_ALERT, "settings", "Unable to create dir \"%s\": %s", - path, strerror(errno)); - return -1; - } - path[x] = '/'; + break; } + x--; } - return 0; + return makedirs(path, 0700); } /** diff --git a/src/tvheadend.h b/src/tvheadend.h index e117f6ab..64a3318d 100644 --- a/src/tvheadend.h +++ b/src/tvheadend.h @@ -480,6 +480,8 @@ void sbuf_put_byte(sbuf_t *sb, uint8_t u8); char *md5sum ( const char *str ); +int makedirs ( const char *path, int mode ); + /* printing */ #if __SIZEOF_LONG__ == 8 #define PRItime_t PRId64 diff --git a/src/utils.c b/src/utils.c index 89ae56d4..7aaca13a 100644 --- a/src/utils.c +++ b/src/utils.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "tvheadend.h" /** @@ -337,3 +338,38 @@ md5sum ( const char *str ) ret[MD5_DIGEST_LENGTH*2] = '\0'; return ret; } + +int +makedirs ( const char *inpath, int mode ) +{ + int err, ok; + size_t x; + struct stat st; + char path[512]; + + if (!inpath || !*inpath) return -1; + + x = 1; + ok = 1; + strcpy(path, inpath); + while(ok) { + ok = path[x]; + if (path[x] == '/' || !path[x]) { + path[x] = 0; + if (stat(path, &st)) { + err = mkdir(path, mode); + } else { + err = S_ISDIR(st.st_mode) ? 0 : 1; + errno = ENOTDIR; + } + if (err) { + tvhlog(LOG_ALERT, "settings", "Unable to create dir \"%s\": %s", + path, strerror(errno)); + return -1; + } + path[x] = '/'; + } + x++; + } + return 0; +}