[libvirt] [PATCH 2/6] util: new virFileRelLinkPointsTo function

John Ferlan jferlan at redhat.com
Fri Apr 11 19:11:24 UTC 2014



On 04/11/2014 12:21 AM, Eric Blake wrote:
> When checking if two filenames point to the same inode (whether
> by hardlink or symlink), sometimes one of the names might be
> relative.  This convenience function makes it easier to check.
> 
> * src/util/virfile.h (virFileRelLinkPointsTo): New prototype.
> * src/util/virfile.c (virFileRelLinkPointsTo): New function.
> * src/libvirt_private.syms (virfile.h): Export it.
> * src/xen/xm_internal.c (xenXMDomainGetAutostart): Use it.
> 
> Signed-off-by: Eric Blake <eblake at redhat.com>
> ---
>  src/libvirt_private.syms |  1 +
>  src/util/virfile.c       | 26 +++++++++++++++++++++++++-
>  src/util/virfile.h       |  3 +++
>  src/xen/xm_internal.c    | 11 +++++------
>  4 files changed, 34 insertions(+), 7 deletions(-)
> 
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index cd43335..c2bce13 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -1255,6 +1255,7 @@ virFilePrintf;
>  virFileReadAll;
>  virFileReadHeaderFD;
>  virFileReadLimFD;
> +virFileRelLinkPointsTo;
>  virFileResolveAllLinks;
>  virFileResolveLink;
>  virFileRewrite;
> diff --git a/src/util/virfile.c b/src/util/virfile.c
> index a28cbf1..10c4337 100644
> --- a/src/util/virfile.c
> +++ b/src/util/virfile.c
> @@ -1368,7 +1368,8 @@ virFileHasSuffix(const char *str,
>     && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
> 
>  /* Return nonzero if checkLink and checkDest
> -   refer to the same file.  Otherwise, return 0.  */
> + * refer to the same file.  Otherwise, return 0.
> + */
>  int
>  virFileLinkPointsTo(const char *checkLink,
>                      const char *checkDest)
> @@ -1382,6 +1383,29 @@ virFileLinkPointsTo(const char *checkLink,
>  }
> 
> 
> +/* Return positive if checkLink (residing within directory if not
> + * absolute) and checkDest refer to the same file.  Otherwise, return
> + * -1 on allocation failure (error reported), or 0 if not the same
> + * (silent).
> + */
> +int
> +virFileRelLinkPointsTo(const char *directory,
> +                       const char *checkLink,
> +                       const char *checkDest)
> +{
> +    char *candidate;
> +    int ret;
> +
> +    if (*checkLink == '/')
> +        return virFileLinkPointsTo(checkLink, checkDest);
> +    if (virAsprintf(&candidate, "%s/%s", directory, checkLink) < 0)
> +        return -1;
> +    ret = virFileLinkPointsTo(candidate, checkDest);
> +    VIR_FREE(candidate);
> +    return ret;
> +}
> +
> +
>  static int
>  virFileResolveLinkHelper(const char *linkpath,
>                           bool intermediatePaths,
> diff --git a/src/util/virfile.h b/src/util/virfile.h
> index 638378a..168eb0d 100644
> --- a/src/util/virfile.h
> +++ b/src/util/virfile.h
> @@ -144,6 +144,9 @@ int virFileStripSuffix(char *str,
> 
>  int virFileLinkPointsTo(const char *checkLink,
>                          const char *checkDest);
> +int virFileRelLinkPointsTo(const char *directory,
> +                           const char *checkLink,
> +                           const char *checkDest);

Should there be a ATTRIBUTE_NONNULL(1,2,3) here?? Probably same for
PointsTo argument 1...

> 
>  int virFileResolveLink(const char *linkpath,
>                         char **resultpath) ATTRIBUTE_RETURN_CHECK;
> diff --git a/src/xen/xm_internal.c b/src/xen/xm_internal.c
> index 52d2a1e..f25a7df 100644
> --- a/src/xen/xm_internal.c
> +++ b/src/xen/xm_internal.c
> @@ -1427,25 +1427,24 @@ int
>  xenXMDomainGetAutostart(virDomainDefPtr def,
>                          int *autostart)
>  {
> -    char *linkname = xenXMAutostartLinkName(def);
>      char *config = xenXMDomainConfigName(def);
>      int ret = -1;
> 
> -    if (!linkname || !config)
> +    if (!config)
>          goto cleanup;
> 
> -    *autostart = virFileLinkPointsTo(linkname, config);
> +    *autostart = virFileRelLinkPointsTo("/etc/xen/auto/", def->name, config);
>      if (*autostart < 0) {
>          virReportSystemError(errno,
> -                             _("cannot check link %s points to config %s"),
> -                             linkname, config);
> +                             _("cannot check link /etc/xen/auto/%s points "
> +                               "to config %s"),
> +                             def->name, config);
>          goto cleanup;
>      }
> 
>      ret = 0;
> 
>   cleanup:
> -    VIR_FREE(linkname);
>      VIR_FREE(config);
>      return ret;
>  }
> 

ACK in general though

John




More information about the libvir-list mailing list