From 6094169b83562e23b4b262dc5df28e54d498663d Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Wed, 18 Jan 2017 18:35:31 +0100 Subject: [PATCH] util: Introduce virFileReadLink We will need to traverse the symlinks one step at the time. Therefore we need to see where a symlink is pointing to. Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/util/virfile.c | 12 ++++++++++++ src/util/virfile.h | 3 +++ 3 files changed, 16 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 8e994c7f06..d556c7d522 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1616,6 +1616,7 @@ virFileReadAllQuiet; virFileReadBufQuiet; virFileReadHeaderFD; virFileReadLimFD; +virFileReadLink; virFileRelLinkPointsTo; virFileRemove; virFileRemoveLastComponent; diff --git a/src/util/virfile.c b/src/util/virfile.c index bf8099e342..49ea1d1f00 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -76,6 +76,7 @@ #include "virutil.h" #include "c-ctype.h" +#include "areadlink.h" #define VIR_FROM_THIS VIR_FROM_NONE @@ -1614,6 +1615,17 @@ virFileIsLink(const char *linkpath) return S_ISLNK(st.st_mode) != 0; } +/* + * Read where symlink is pointing to. + * + * Returns 0 on success (@linkpath is a successfully read link), + * -1 with errno set upon error. + */ +int +virFileReadLink(const char *linkpath, char **resultpath) +{ + return (*resultpath = areadlink(linkpath)) ? 0 : -1; +} /* * Finds a requested executable file in the PATH env. e.g.: diff --git a/src/util/virfile.h b/src/util/virfile.h index 0343acd5b8..232c1d66e1 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -166,6 +166,9 @@ int virFileResolveAllLinks(const char *linkpath, int virFileIsLink(const char *linkpath) ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; +int virFileReadLink(const char *linkpath, char **resultpath) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; + char *virFindFileInPath(const char *file); char *virFileFindResource(const char *filename,