From da986b4a3a678c0c7344dfb6e51479640f521ec4 Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Wed, 27 Jul 2016 10:45:07 +0800 Subject: [PATCH] create file volume's parent directory when necessary Signed-off-by: Peng Tao --- src/util.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/util.c b/src/util.c index c1be6b9..2308c68 100644 --- a/src/util.c +++ b/src/util.c @@ -208,6 +208,23 @@ int hyper_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroup return ret; } +static int hyper_create_parent_dir(const char *hyper_path) +{ + char *p, *path = strdup(hyper_path); + int ret = 0; + + if (path == NULL) + return -1; + p = strrchr(path, '/'); + if (p != NULL && p != path) { + *p = '\0'; + ret = hyper_mkdir(path, 0777); + } + free(path); + + return ret; +} + int hyper_create_file(const char *hyper_path) { int fd; @@ -220,6 +237,9 @@ int hyper_create_file(const char *hyper_path) return -1; } + if (hyper_create_parent_dir(hyper_path) < 0) + return -1; + fd = open(hyper_path, O_CREAT|O_WRONLY, 0666); if (fd < 0) return -1;