diff options
Diffstat (limited to 'lib/util.c')
| -rw-r--r-- | lib/util.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/util.c b/lib/util.c new file mode 100644 index 0000000..6cfcf1f --- /dev/null +++ b/lib/util.c @@ -0,0 +1,30 @@ +#include "log.h" +#include "util.h" + +#include <fcntl.h> +#include <sys/stat.h> +#include <errno.h> +#include <string.h> + +/* will clobber path */ +int vl_mkdir_parents(int fd, char *path, mode_t mode) +{ + int last = 0; + + char *ch = path; + do { + if (*ch == '/' || (*ch == '\0' && (last = 1))) { + /* last is set to 1 iff *ch == '\0' */ + *ch = '\0'; + + if (mkdirat(fd, path, mode) < 0 && errno != EEXIST) { + vl_debug("Failed to create directory %s: %s", path, strerror(errno)); + return -1; + } + + if (!last) *ch = '/'; + } + } while (*(ch++)); + + return 0; +} |
