]> xenbits.xensource.com Git - libvirt.git/commitdiff
virfile: Introduce virFileRemoveLastComponent
authorMichal Privoznik <mprivozn@redhat.com>
Thu, 12 May 2016 11:05:37 +0000 (13:05 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 13 May 2016 12:17:15 +0000 (14:17 +0200)
Move some parts of virStorageFileRemoveLastPathComponent
into a separate function so they can be reused.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
src/libvirt_private.syms
src/util/virfile.c
src/util/virfile.h
src/util/virstoragefile.c

index a980a32673b7c2707934c9c6a693676f3c8e433b..fb248085904fed499818efdf8b53b07c3b35719c 100644 (file)
@@ -1517,6 +1517,7 @@ virFileReadHeaderFD;
 virFileReadLimFD;
 virFileRelLinkPointsTo;
 virFileRemove;
+virFileRemoveLastComponent;
 virFileResolveAllLinks;
 virFileResolveLink;
 virFileRewrite;
index 4d7b510ce0e8bea199391ebaae07b6e28d815f00..9d460b9ebae679c5606af099fcd156a75ea4d1fe 100644 (file)
@@ -3132,6 +3132,23 @@ virFileSanitizePath(const char *path)
     return cleanpath;
 }
 
+/**
+ * virFileRemoveLastComponent:
+ *
+ * For given path cut off the last component. If there's no dir
+ * separator (whole path is one file name), @path is turned into
+ * an empty string.
+ */
+void
+virFileRemoveLastComponent(char *path)
+{
+    char *tmp;
+
+    if ((tmp = strrchr(path, VIR_FILE_DIR_SEPARATOR)))
+        tmp[1] = '\0';
+    else
+        path[0] = '\0';
+}
 
 /**
  * virFilePrintf:
index dc62eab3816bbceb702b0292e88f8add47e137f8..dae234e2e486b4627339a3a4a786980493ae64aa 100644 (file)
@@ -268,6 +268,7 @@ bool virFileIsAbsPath(const char *path);
 int virFileAbsPath(const char *path,
                    char **abspath) ATTRIBUTE_RETURN_CHECK;
 const char *virFileSkipRoot(const char *path);
+void virFileRemoveLastComponent(char *path);
 
 int virFileOpenTty(int *ttymaster,
                    char **ttyName,
index d4e61ca6aad3274476a45febd0369af6b78b1833..d2da9e712fe7e7aab134d1b25e5d36c5a4c97b27 100644 (file)
@@ -2868,16 +2868,12 @@ virStorageFileCanonicalizePath(const char *path,
 static char *
 virStorageFileRemoveLastPathComponent(const char *path)
 {
-    char *tmp;
     char *ret;
 
     if (VIR_STRDUP(ret, path ? path : "") < 0)
         return NULL;
 
-    if ((tmp = strrchr(ret, '/')))
-        tmp[1] = '\0';
-    else
-        ret[0] = '\0';
+    virFileRemoveLastComponent(ret);
 
     return ret;
 }