From 285f5d51b18ec4fa2d67005928442bd788fb38f9 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Fri, 23 Dec 2016 14:59:21 +0800 Subject: [PATCH] re-implement hyper_mkdir() Signed-off-by: Lai Jiangshan --- src/util.c | 43 ++++++++++++++----------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/src/util.c b/src/util.c index 36fcd0f7..4fe80e31 100644 --- a/src/util.c +++ b/src/util.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "util.h" #include "hyper.h" @@ -287,53 +288,37 @@ int hyper_create_file(const char *hyper_path) return 0; } -int hyper_mkdir(char *hyper_path, mode_t mode) +int hyper_mkdir(char *path, mode_t mode) { struct stat st; - char *p, *path = strdup(hyper_path); - - if (path == NULL) { - errno = ENOMEM; - goto fail; - } if (stat(path, &st) >= 0) { if (S_ISDIR(st.st_mode)) - goto out; + return 0; errno = ENOTDIR; - goto fail; + return -1; } if (errno != ENOENT) - goto fail; + return -1; - p = strrchr(path, '/'); - if (p == NULL) { - errno = EINVAL; - goto fail; + char *parent_path = strdup(path); + if (parent_path == NULL) { + errno = ENOMEM; + return -1; } - - if (p != path) { - *p = '\0'; - - if (hyper_mkdir(path, mode) < 0) - goto fail; - - *p = '/'; + if (hyper_mkdir(dirname(parent_path), mode) < 0) { + free(parent_path); + return -1; } + free(parent_path); fprintf(stdout, "create directory %s\n", path); if (mkdir(path, mode) < 0 && errno != EEXIST) { perror("failed to create directory"); - goto fail; + return -1; } -out: - free(path); return 0; - -fail: - free(path); - return -1; } void online_cpu(void)