[libvirt] [PATCH 07/14] Add virFileMakeParentPath helper function

Daniel P. Berrange berrange at redhat.com
Fri Feb 7 15:33:05 UTC 2014


Add a helper function which takes a file path and ensures
that all directory components leading upto the file exist.
IOW, it strips the filename part of the path and passes
the result to virFileMakePath.

Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virfile.c       | 27 +++++++++++++++++++++++++++
 src/util/virfile.h       |  1 +
 3 files changed, 29 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 2c9536a..5554a30 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1225,6 +1225,7 @@ virFileIsMountPoint;
 virFileLinkPointsTo;
 virFileLock;
 virFileLoopDeviceAssociate;
+virFileMakeParentPath;
 virFileMakePath;
 virFileMakePathWithMode;
 virFileMatchesNameSuffix;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index 631cd06..92a8621 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -2340,6 +2340,33 @@ cleanup:
     return ret;
 }
 
+
+int
+virFileMakeParentPath(const char *path)
+{
+    char *p;
+    char *tmp;
+    int ret;
+
+    VIR_DEBUG("path=%s", path);
+
+    if (VIR_STRDUP(tmp, path) < 0) {
+        errno = ENOMEM;
+        return -1;
+    }
+
+    if ((p = strrchr(tmp, '/')) == NULL) {
+        errno = EINVAL;
+        return -1;
+    }
+    *p = '\0';
+
+    ret = virFileMakePathHelper(tmp, 0777);
+    VIR_FREE(tmp);
+    return ret;
+}
+
+
 /* Build up a fully qualified path for a config file to be
  * associated with a persistent guest or network */
 char *
diff --git a/src/util/virfile.h b/src/util/virfile.h
index 0d20cdb..20baf6f 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -198,6 +198,7 @@ int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid,
 int virFileMakePath(const char *path) ATTRIBUTE_RETURN_CHECK;
 int virFileMakePathWithMode(const char *path,
                             mode_t mode) ATTRIBUTE_RETURN_CHECK;
+int virFileMakeParentPath(const char *path) ATTRIBUTE_RETURN_CHECK;
 
 char *virFileBuildPath(const char *dir,
                        const char *name,
-- 
1.8.5.3




More information about the libvir-list mailing list