}
-/**
- *
- */
-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
*/
/* */
- if(makedirs(path) != 0) {
+ if(makedirs(path, 0777) != 0) {
return -1;
}
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);
}
/**
char *md5sum ( const char *str );
+int makedirs ( const char *path, int mode );
+
/* printing */
#if __SIZEOF_LONG__ == 8
#define PRItime_t PRId64
#include <string.h>
#include <assert.h>
#include <openssl/md5.h>
+#include <sys/stat.h>
#include "tvheadend.h"
/**
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;
+}