Use directory structure in cache dir

This commit is contained in:
Bert
2011-04-07 17:29:59 +02:00
parent f52a99db6c
commit 92709b2b2f
3 changed files with 58 additions and 13 deletions

43
util.c
View File

@ -18,6 +18,8 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
@ -156,6 +158,47 @@ end:
return path;
}
int create_dir_rec(const char *path) {
char *dir, *d;
struct stat stats;
int err = 0;
if (!path || !*path)
return -1;
if (!stat(path, &stats)) {
if (S_ISDIR(stats.st_mode)) {
return 0;
} else {
warn("not a directory: %s", path);
return -1;
}
}
d = dir = (char*) s_malloc(strlen(path) + 1);
strcpy(dir, path);
while (d != NULL && !err) {
d = strchr(d + 1, '/');
if (d != NULL)
*d = '\0';
if (access(dir, F_OK) && errno == ENOENT) {
if (mkdir(dir, 0755)) {
warn("could not create directory: %s", dir);
err = -1;
}
} else if (stat(dir, &stats) || !S_ISDIR(stats.st_mode)) {
warn("not a directory: %s", dir);
err = -1;
}
if (d != NULL)
*d = '/';
}
free(dir);
return err;
}
char* readline(FILE *stream) {
size_t len;
char *buf, *s, *end;