From 7fccf12482edf5dfcc6c06700d1dc0dd0250a35c Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Thu, 12 May 2016 13:05:37 +0200 Subject: [PATCH] virfile: Introduce virFileRemoveLastComponent Move some parts of virStorageFileRemoveLastPathComponent into a separate function so they can be reused. Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/util/virfile.c | 17 +++++++++++++++++ src/util/virfile.h | 1 + src/util/virstoragefile.c | 6 +----- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index a980a32673..fb24808590 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1517,6 +1517,7 @@ virFileReadHeaderFD; virFileReadLimFD; virFileRelLinkPointsTo; virFileRemove; +virFileRemoveLastComponent; virFileResolveAllLinks; virFileResolveLink; virFileRewrite; diff --git a/src/util/virfile.c b/src/util/virfile.c index 4d7b510ce0..9d460b9eba 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -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: diff --git a/src/util/virfile.h b/src/util/virfile.h index dc62eab381..dae234e2e4 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -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, diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index d4e61ca6aa..d2da9e712f 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -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; }