[PATCH] Reduce code duplication in virFileMakePath(Helper)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.
---
 src/util/util.c |   44 ++++++++++++++------------------------------
 1 files changed, 14 insertions(+), 30 deletions(-)

diff --git a/src/util/util.c b/src/util/util.c
index 4710fc5..d944123 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -1013,11 +1013,17 @@ int virDirCreate(const char *path ATTRIBUTE_UNUSED,
 static int virFileMakePathHelper(char *path)
 {
     struct stat st;
-    char *p = NULL;
+    char *p;
 
-    if (stat(path, &st) >= 0)
-        return 0;
-    else if (errno != ENOENT)
+    if (stat(path, &st) >= 0) {
+        if (S_ISDIR(st.st_mode))
+            return 0;
+
+        errno = ENOTDIR;
+        return -1;
+    }
+
+    if (errno != ENOENT)
         return -1;
 
     if ((p = strrchr(path, '/')) == NULL) {
@@ -1049,39 +1055,17 @@ static int virFileMakePathHelper(char *path)
 int virFileMakePath(const char *path)
 {
     int ret = -1;
-    struct stat st;
-    char *parent = NULL;
-    char *p;
-
-    if (stat(path, &st) >= 0)
-        return 0;
-    else if (errno != ENOENT)
-        goto cleanup;
+    char *tmp;
 
-    if ((parent = strdup(path)) == NULL) {
+    if ((tmp = 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)
-        goto cleanup;
-
-    ret = 0;
+    ret = virFileMakePathHelper(tmp);
 
 cleanup:
-    VIR_FREE(parent);
+    VIR_FREE(tmp);
     return ret;
 }
 
-- 
1.7.4.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]