]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
util: Added a generic makedirs replacement for the 2 diff implementations.
authorAdam Sutton <dev@adamsutton.me.uk>
Fri, 2 Nov 2012 15:37:11 +0000 (15:37 +0000)
committerAdam Sutton <dev@adamsutton.me.uk>
Wed, 28 Nov 2012 09:43:13 +0000 (09:43 +0000)
src/dvr/dvr_rec.c
src/settings.c
src/tvheadend.h
src/utils.c

index 925d721d0136007042a477c79b392d438a832683..6dc560dd3d14ebc7534777ee8100b60d16fd4d58 100755 (executable)
@@ -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;
   }
   
index 8f0de39bd99f862a0f92c04d15db0f71d6a059f0..3857a38305441319851b339033b09b2fa9f78eb0 100644 (file)
@@ -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);
 }
 
 /**
index e117f6ab5b19b0d1152c99c311b06d8680442fff..64a3318d358e0d06999db8bccfc80f6e1f2d5b46 100644 (file)
@@ -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
index 89ae56d44317d02d181e83088877741d27dfa1f8..7aaca13abcae01a45c4d1c3605fc9d931c8c8c96 100644 (file)
@@ -21,6 +21,7 @@
 #include <string.h>
 #include <assert.h>
 #include <openssl/md5.h>
+#include <sys/stat.h>
 #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;
+}