diff options
author | Marcel Holtmann <marcel@holtmann.org> | 2005-08-30 00:35:39 +0000 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2005-08-30 00:35:39 +0000 |
commit | 44385c0baa98b0e27dad39cb8bd0e33ce5a027f6 (patch) | |
tree | e216d9746db798291f00dade69b46f33fdf2ff45 /common/textfile.c | |
parent | 3882b4e761ed1411406e375ec1b4b0790509939e (diff) |
Move create_dirs() and create_file() into the textfile library
Diffstat (limited to 'common/textfile.c')
-rw-r--r-- | common/textfile.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/common/textfile.c b/common/textfile.c index a5d0a58b..26cbbea7 100644 --- a/common/textfile.c +++ b/common/textfile.c @@ -41,6 +41,56 @@ #include <sys/mman.h> #include <sys/param.h> +int create_dirs(char *filename, mode_t mode) +{ + struct stat st; + char dir[PATH_MAX + 1], *prev, *next; + int err; + + err = stat(filename, &st); + if (!err && S_ISREG(st.st_mode)) + return 0; + + memset(dir, 0, PATH_MAX + 1); + strcat(dir, "/"); + + prev = strchr(filename, '/'); + + while (prev) { + next = strchr(prev + 1, '/'); + if (!next) + break; + + if (next - prev == 1) { + prev = next; + continue; + } + + strncat(dir, prev + 1, next - prev); + mkdir(dir, mode); + + prev = next; + } + + return 0; +} + +int create_file(char *filename, mode_t mode) +{ + int fd; + + umask(S_IWGRP | S_IWOTH); + create_dirs(filename, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); + + fd = open(filename, O_RDWR | O_CREAT, mode); + if (fd < 0) + return fd; + + close(fd); + + return 0; +} + static inline int write_key_value(int fd, char *key, char *value) { char *str; |