Skip to content
Snippets Groups Projects
Commit 1ac35488 authored by Colin Walters's avatar Colin Walters
Browse files

xattrs: Dedup fd reading code

By taking both fd and path into one copy of the reader func, exactly like we do
in `read_xattr_name_array`, we can abstract over the difference.

Preparatory cleanup for more work here.
parent afd178fb
No related branches found
No related tags found
No related merge requests found
...@@ -137,9 +137,10 @@ read_xattr_name_array (const char *path, ...@@ -137,9 +137,10 @@ read_xattr_name_array (const char *path,
static gboolean static gboolean
get_xattrs_impl (const char *path, get_xattrs_impl (const char *path,
int fd,
GVariant **out_xattrs, GVariant **out_xattrs,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
gboolean ret = FALSE; gboolean ret = FALSE;
ssize_t bytes_read, real_size; ssize_t bytes_read, real_size;
...@@ -149,10 +150,15 @@ get_xattrs_impl (const char *path, ...@@ -149,10 +150,15 @@ get_xattrs_impl (const char *path,
gboolean builder_initialized = FALSE; gboolean builder_initialized = FALSE;
g_autoptr(GVariant) ret_xattrs = NULL; g_autoptr(GVariant) ret_xattrs = NULL;
g_assert (path != NULL || fd != -1);
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ayay)")); g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ayay)"));
builder_initialized = TRUE; builder_initialized = TRUE;
bytes_read = llistxattr (path, NULL, 0); if (path)
bytes_read = llistxattr (path, NULL, 0);
else
bytes_read = flistxattr (fd, NULL, 0);
if (bytes_read < 0) if (bytes_read < 0)
{ {
...@@ -165,7 +171,10 @@ get_xattrs_impl (const char *path, ...@@ -165,7 +171,10 @@ get_xattrs_impl (const char *path,
else if (bytes_read > 0) else if (bytes_read > 0)
{ {
xattr_names = g_malloc (bytes_read); xattr_names = g_malloc (bytes_read);
real_size = llistxattr (path, xattr_names, bytes_read); if (path)
real_size = llistxattr (path, xattr_names, bytes_read);
else
real_size = flistxattr (fd, xattr_names, bytes_read);
if (real_size < 0) if (real_size < 0)
{ {
glnx_set_prefix_error_from_errno (error, "%s", "llistxattr"); glnx_set_prefix_error_from_errno (error, "%s", "llistxattr");
...@@ -175,7 +184,7 @@ get_xattrs_impl (const char *path, ...@@ -175,7 +184,7 @@ get_xattrs_impl (const char *path,
{ {
xattr_names_canonical = canonicalize_xattrs (xattr_names, real_size); xattr_names_canonical = canonicalize_xattrs (xattr_names, real_size);
if (!read_xattr_name_array (path, -1, xattr_names_canonical, real_size, &builder, error)) if (!read_xattr_name_array (path, fd, xattr_names_canonical, real_size, &builder, error))
goto out; goto out;
} }
} }
...@@ -212,56 +221,8 @@ glnx_fd_get_all_xattrs (int fd, ...@@ -212,56 +221,8 @@ glnx_fd_get_all_xattrs (int fd,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
gboolean ret = FALSE; return get_xattrs_impl (NULL, fd, out_xattrs,
ssize_t bytes_read, real_size; cancellable, error);
glnx_free char *xattr_names = NULL;
glnx_free char *xattr_names_canonical = NULL;
GVariantBuilder builder;
gboolean builder_initialized = FALSE;
g_autoptr(GVariant) ret_xattrs = NULL;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ayay)"));
builder_initialized = TRUE;
bytes_read = flistxattr (fd, NULL, 0);
if (bytes_read < 0)
{
if (errno != ENOTSUP)
{
glnx_set_prefix_error_from_errno (error, "%s", "flistxattr");
goto out;
}
}
else if (bytes_read > 0)
{
xattr_names = g_malloc (bytes_read);
real_size = flistxattr (fd, xattr_names, bytes_read);
if (real_size < 0)
{
glnx_set_prefix_error_from_errno (error, "%s", "flistxattr");
goto out;
}
else if (real_size > 0)
{
xattr_names_canonical = canonicalize_xattrs (xattr_names, real_size);
if (!read_xattr_name_array (NULL, fd, xattr_names_canonical, real_size, &builder, error))
goto out;
}
}
ret_xattrs = g_variant_builder_end (&builder);
builder_initialized = FALSE;
g_variant_ref_sink (ret_xattrs);
ret = TRUE;
if (out_xattrs)
*out_xattrs = g_steal_pointer (&ret_xattrs);
out:
if (!builder_initialized)
g_variant_builder_clear (&builder);
return ret;
} }
/** /**
...@@ -284,7 +245,7 @@ glnx_dfd_name_get_all_xattrs (int dfd, ...@@ -284,7 +245,7 @@ glnx_dfd_name_get_all_xattrs (int dfd,
{ {
if (dfd == AT_FDCWD || dfd == -1) if (dfd == AT_FDCWD || dfd == -1)
{ {
return get_xattrs_impl (name, out_xattrs, cancellable, error); return get_xattrs_impl (name, -1, out_xattrs, cancellable, error);
} }
else else
{ {
...@@ -293,7 +254,7 @@ glnx_dfd_name_get_all_xattrs (int dfd, ...@@ -293,7 +254,7 @@ glnx_dfd_name_get_all_xattrs (int dfd,
* https://mail.gnome.org/archives/ostree-list/2014-February/msg00017.html * https://mail.gnome.org/archives/ostree-list/2014-February/msg00017.html
*/ */
snprintf (buf, sizeof (buf), "/proc/self/fd/%d/%s", dfd, name); snprintf (buf, sizeof (buf), "/proc/self/fd/%d/%s", dfd, name);
return get_xattrs_impl (buf, out_xattrs, cancellable, error); return get_xattrs_impl (buf, -1, out_xattrs, cancellable, error);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment