util: Added a generic makedirs replacement for the 2 diff implementations.

This commit is contained in:
Adam Sutton 2012-11-02 15:37:11 +00:00
parent 8ff12894aa
commit 6254fb5622
4 changed files with 46 additions and 62 deletions

View file

@ -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;
}

View 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);
}
/**

View 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

View 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;
}