[libvirt] [PATCH v2 1/4] virFile: Add APIs for extended attributes handling

Daniel P. Berrange berrange at redhat.com
Thu Mar 7 15:35:56 UTC 2013


On Wed, Mar 06, 2013 at 03:05:53PM +0100, Michal Privoznik wrote:
> Currently, only three wrappers are being implemented:
> virFileSetAttr for setting attributes
> virFileGetAttr for querying attributes (note we need to call it twice,
> first time to get length of attribute value, second to get actual value)
> virFileRemoveAttr for removing attributes
> ---
>  configure.ac             |   1 +
>  libvirt.spec.in          |   3 ++
>  src/libvirt_private.syms |   3 ++
>  src/util/virfile.c       | 106 +++++++++++++++++++++++++++++++++++++++++++++++
>  src/util/virfile.h       |  12 ++++++
>  5 files changed, 125 insertions(+)
> 
> diff --git a/configure.ac b/configure.ac
> index 9d366e9..cbb20c4 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -275,6 +275,7 @@ dnl header could be found.
>  AM_CONDITIONAL([HAVE_LIBTASN1], [test "x$ac_cv_header_libtasn1_h" = "xyes"])
>  
>  AC_CHECK_LIB([intl],[gettext],[])
> +AC_CHECK_LIB([attr],[getxattr])

We already have a check for libattr - see m4/virt-attr.m4

>  
>  dnl Do we have rpcgen?
>  AC_PATH_PROG([RPCGEN], [rpcgen], [no])
> diff --git a/libvirt.spec.in b/libvirt.spec.in
> index 9fb753a..90629c8 100644
> --- a/libvirt.spec.in
> +++ b/libvirt.spec.in
> @@ -577,6 +577,9 @@ BuildRequires: gawk
>  # For storage wiping with different algorithms
>  BuildRequires: scrub
>  
> +# For xattr
> +BuildRequires: libattr-devel

Again we already have a BuildRequires on libattr-devel

> diff --git a/src/util/virfile.c b/src/util/virfile.c
> index eec9bcc..aa4579e 100644
> --- a/src/util/virfile.c
> +++ b/src/util/virfile.c
> @@ -37,6 +37,11 @@
>  # include <sys/ioctl.h>
>  #endif
>  
> +#ifdef HAVE_LIBATTR
> +# include <attr/xattr.h>
> +# include <sys/types.h>
> +#endif
> +
>  #include "vircommand.h"
>  #include "configmake.h"
>  #include "viralloc.h"
> @@ -644,3 +649,104 @@ int virFileLoopDeviceAssociate(const char *file,
>  }
>  
>  #endif /* __linux__ */
> +
> +#ifdef HAVE_LIBATTR /* HAVE_LIBATTR */

Pointless comment at end of line

> +int
> +virFileSetAttr(const char *file,
> +               const char *name,
> +               const char *value)
> +{
> +    size_t valueSize = strlen(value);
> +    if (setxattr(file, name, value, valueSize, 0) < 0) {
> +        virReportSystemError(errno,
> +                             _("Unable to set extended attribute '%s' on '%s'"),
> +                             name, file);
> +        return -1;
> +    }
> +    return 0;
> +}
> +
> +int
> +virFileGetAttr(const char *file,
> +               const char *name,
> +               char **value)
> +{
> +    int ret = -1;
> +    char *buf = NULL;
> +    ssize_t valueSize;
> +
> +    /* get attribute length */
> +    if ((valueSize = getxattr(file, name, NULL, 0)) < 0) {
> +        /* The Linux kernel does not define ENOATTR, but maps it to ENODATA. */
> +        if (errno == ENOATTR || errno == ENODATA) {
> +            *value = NULL;
> +            return 0;
> +        } else {
> +            virReportSystemError(errno,
> +                                 _("Unable to get extended attribute '%s' on '%s'"),
> +                                 name, file);
> +            return ret;
> +        }
> +    }
> +
> +    if (VIR_ALLOC_N(buf, valueSize) < 0) {
> +        virReportOOMError();
> +        return ret;
> +    }
> +
> +    if ((ret = getxattr(file, name, buf, valueSize)) < 0) {
> +        VIR_FREE(buf);
> +        virReportSystemError(errno,
> +                             _("Unable to get extended attribute '%s' on '%s'"),
> +                             name, file);
> +    } else {
> +        *value = buf;
> +    }
> +
> +    return ret;
> +}
> +
> +int
> +virFileRemoveAttr(const char *file,
> +                  const char *name)
> +{
> +    if (removexattr(file, name) < 0) {
> +        virReportSystemError(errno,
> +                             _("Unable to remove extended attribute '%s' on '%s'"),
> +                             name, file);
> +        return -1;
> +    }
> +    return 0;
> +}


Looks good besides the issues mentioned


Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the libvir-list mailing list