From 9b64f3e3d0e46bfeb6b2a1a4604c82ba5ad20535 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 26 Feb 2014 16:25:59 -0500 Subject: fileutil: Add API to set xattrs of a filename relative to a dir fd This is a Linux-specific way to work around the missing lsetxattrat(). --- src/gsystem-file-utils.c | 62 +++++++++++++++++++++++++++++++++--------------- src/gsystem-file-utils.h | 6 +++++ 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/gsystem-file-utils.c b/src/gsystem-file-utils.c index 0260603..27e0e2d 100644 --- a/src/gsystem-file-utils.c +++ b/src/gsystem-file-utils.c @@ -1588,30 +1588,16 @@ gs_fd_set_all_xattrs (int fd, #endif } -/** - * gs_file_set_all_xattrs: - * @file: File descriptor - * @xattrs: Extended attributes - * @cancellable: Cancellable - * @error: Error - * - * For each attribute in @xattrs, set its value on the file or - * directory referred to by @file. This function does not remove any - * attributes not in @xattrs. - */ -gboolean -gs_file_set_all_xattrs (GFile *file, - GVariant *xattrs, - GCancellable *cancellable, - GError **error) +static gboolean +set_all_xattrs_for_path (const char *path, + GVariant *xattrs, + GCancellable *cancellable, + GError **error) { #ifdef GSYSTEM_CONFIG_XATTRS gboolean ret = FALSE; - const char *path; int i, n; - path = gs_file_get_path_cached (file); - n = g_variant_n_children (xattrs); for (i = 0; i < n; i++) { @@ -1642,3 +1628,41 @@ gs_file_set_all_xattrs (GFile *file, return TRUE; #endif } + +gboolean +gs_dfd_and_name_set_all_xattrs (int dfd, + const char *name, + GVariant *xattrs, + GCancellable *cancellable, + GError **error) +{ + /* A workaround for the lack of lsetxattrat(), thanks to Florian Weimer: + * https://mail.gnome.org/archives/ostree-list/2014-February/msg00017.html + */ + char *path = g_strdup_printf ("/proc/self/fd/%d/%s", dfd, name); + gboolean ret; + + ret = set_all_xattrs_for_path (path, xattrs, cancellable, error); + g_free (path); + return ret; +} + +/** + * gs_file_set_all_xattrs: + * @file: File descriptor + * @xattrs: Extended attributes + * @cancellable: Cancellable + * @error: Error + * + * For each attribute in @xattrs, set its value on the file or + * directory referred to by @file. This function does not remove any + * attributes not in @xattrs. + */ +gboolean +gs_file_set_all_xattrs (GFile *file, + GVariant *xattrs, + GCancellable *cancellable, + GError **error) +{ + return set_all_xattrs_for_path (gs_file_get_path_cached (file), xattrs, cancellable, error); +} diff --git a/src/gsystem-file-utils.h b/src/gsystem-file-utils.h index deb0a48..2da6646 100644 --- a/src/gsystem-file-utils.h +++ b/src/gsystem-file-utils.h @@ -162,6 +162,12 @@ gboolean gs_fd_set_all_xattrs (int fd, GCancellable *cancellable, GError **error); +gboolean gs_dfd_and_name_set_all_xattrs (int dfd, + const char *name, + GVariant *xattrs, + GCancellable *cancellable, + GError **error); + gboolean gs_file_set_all_xattrs (GFile *file, GVariant *xattrs, GCancellable *cancellable, -- cgit v1.2.1