create file volume's parent directory when necessary

Signed-off-by: Peng Tao <bergwolf@gmail.com>
This commit is contained in:
Peng Tao
2016-08-10 18:02:05 +08:00
parent 2f4626dde2
commit da986b4a3a
+20
View File
@@ -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;