提交 cd9a4232 编写于 作者: M Matthias Bolte

Reduce code duplication in virFileMakePath(Helper)

Move stat and mkdir to virFileMakePathHelper.

Also use the stat result to detect whether the existing path
is a directory and set errno accordingly if it's not.
上级 2d2d6a01
...@@ -1013,11 +1013,17 @@ int virDirCreate(const char *path ATTRIBUTE_UNUSED, ...@@ -1013,11 +1013,17 @@ int virDirCreate(const char *path ATTRIBUTE_UNUSED,
static int virFileMakePathHelper(char *path) static int virFileMakePathHelper(char *path)
{ {
struct stat st; struct stat st;
char *p = NULL; char *p;
if (stat(path, &st) >= 0) if (stat(path, &st) >= 0) {
if (S_ISDIR(st.st_mode))
return 0; return 0;
else if (errno != ENOENT)
errno = ENOTDIR;
return -1;
}
if (errno != ENOENT)
return -1; return -1;
if ((p = strrchr(path, '/')) == NULL) { if ((p = strrchr(path, '/')) == NULL) {
...@@ -1049,39 +1055,15 @@ static int virFileMakePathHelper(char *path) ...@@ -1049,39 +1055,15 @@ static int virFileMakePathHelper(char *path)
int virFileMakePath(const char *path) int virFileMakePath(const char *path)
{ {
int ret = -1; int ret = -1;
struct stat st; char *tmp;
char *parent = NULL;
char *p;
if (stat(path, &st) >= 0)
return 0;
else if (errno != ENOENT)
goto cleanup;
if ((parent = strdup(path)) == NULL) {
errno = ENOMEM;
goto cleanup;
}
if ((p = strrchr(parent, '/')) == NULL) {
errno = EINVAL;
goto cleanup;
}
if (p != parent) {
*p = '\0';
if (virFileMakePathHelper(parent) < 0)
goto cleanup;
}
if (mkdir(path, 0777) < 0 && errno != EEXIST) if ((tmp = strdup(path)) == NULL)
goto cleanup; goto cleanup;
ret = 0; ret = virFileMakePathHelper(tmp);
cleanup: cleanup:
VIR_FREE(parent); VIR_FREE(tmp);
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册