1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2014,2015 Colin Walters <walters@verbum.org>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <string.h>
#include <stdio.h>
#include <glnx-xattrs.h>
#include <glnx-errors.h>
#include <glnx-local-alloc.h>
static GVariant *
variant_new_ay_bytes (GBytes *bytes)
{
gsize size;
gconstpointer data;
data = g_bytes_get_data (bytes, &size);
g_bytes_ref (bytes);
return g_variant_new_from_data (G_VARIANT_TYPE ("ay"), data, size,
TRUE, (GDestroyNotify)g_bytes_unref, bytes);
}
static char *
canonicalize_xattrs (char *xattr_string,
size_t len)
{
char *p;
GSList *xattrs = NULL;
GSList *iter;
GString *result;
result = g_string_new (0);
p = xattr_string;
while (p < xattr_string+len)
{
xattrs = g_slist_prepend (xattrs, p);
p += strlen (p) + 1;
}
xattrs = g_slist_sort (xattrs, (GCompareFunc) strcmp);
for (iter = xattrs; iter; iter = iter->next) {
g_string_append (result, iter->data);
g_string_append_c (result, '\0');
}
g_slist_free (xattrs);
return g_string_free (result, FALSE);
}
static gboolean
read_xattr_name_array (const char *path,
int fd,
const char *xattrs,
size_t len,
GVariantBuilder *builder,
GError **error)
{
gboolean ret = FALSE;
const char *p;
int r;
const char *funcstr;
g_assert (path != NULL || fd != -1);
funcstr = fd != -1 ? "fgetxattr" : "lgetxattr";
p = xattrs;
while (p < xattrs+len)
{
ssize_t bytes_read;
char *buf;
GBytes *bytes = NULL;
if (fd != -1)
bytes_read = fgetxattr (fd, p, NULL, 0);
else
bytes_read = lgetxattr (path, p, NULL, 0);
if (bytes_read < 0)
{
glnx_set_prefix_error_from_errno (error, "%s", funcstr);
goto out;
}
if (bytes_read == 0)
continue;
buf = g_malloc (bytes_read);
bytes = g_bytes_new_take (buf, bytes_read);
if (fd != -1)
r = fgetxattr (fd, p, buf, bytes_read);
else
r = lgetxattr (path, p, buf, bytes_read);
if (r < 0)
{
g_bytes_unref (bytes);
glnx_set_prefix_error_from_errno (error, "%s", funcstr);
goto out;
}
g_variant_builder_add (builder, "(@ay@ay)",
g_variant_new_bytestring (p),
variant_new_ay_bytes (bytes));
p = p + strlen (p) + 1;
g_bytes_unref (bytes);
}
ret = TRUE;
out:
return ret;
}
static gboolean
get_xattrs_impl (const char *path,
GVariant **out_xattrs,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
ssize_t bytes_read;
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 = llistxattr (path, NULL, 0);
if (bytes_read < 0)
{
if (errno != ENOTSUP)
{
glnx_set_prefix_error_from_errno (error, "%s", "llistxattr");
goto out;
}
}
else if (bytes_read > 0)
{
xattr_names = g_malloc (bytes_read);
if (llistxattr (path, xattr_names, bytes_read) < 0)
{
glnx_set_prefix_error_from_errno (error, "%s", "llistxattr");
goto out;
}
xattr_names_canonical = canonicalize_xattrs (xattr_names, bytes_read);
if (!read_xattr_name_array (path, -1, xattr_names_canonical, bytes_read, &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;
}
/**
* glnx_fd_get_all_xattrs:
* @fd: a file descriptor
* @out_xattrs: (out): A new #GVariant containing the extended attributes
* @cancellable: Cancellable
* @error: Error
*
* Read all extended attributes from @fd in a canonical sorted order, and
* set @out_xattrs with the result.
*
* If the filesystem does not support extended attributes, @out_xattrs
* will have 0 elements, and this function will return successfully.
*/
gboolean
glnx_fd_get_all_xattrs (int fd,
GVariant **out_xattrs,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
ssize_t bytes_read;
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);
if (flistxattr (fd, xattr_names, bytes_read) < 0)
{
glnx_set_prefix_error_from_errno (error, "%s", "flistxattr");
goto out;
}
xattr_names_canonical = canonicalize_xattrs (xattr_names, bytes_read);
if (!read_xattr_name_array (NULL, fd, xattr_names_canonical, bytes_read, &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;
}
/**
* glnx_dfd_name_get_all_xattrs:
* @dfd: Parent directory file descriptor
* @name: File name
* @out_xattrs: (out): Extended attribute set
* @cancellable: Cancellable
* @error: Error
*
* Load all extended attributes for the file named @name residing in
* directory @dfd.
*/
gboolean
glnx_dfd_name_get_all_xattrs (int dfd,
const char *name,
GVariant **out_xattrs,
GCancellable *cancellable,
GError **error)
{
if (dfd == AT_FDCWD || dfd == -1)
{
return get_xattrs_impl (name, out_xattrs, cancellable, error);
}
else
{
char buf[PATH_MAX];
/* A workaround for the lack of lgetxattrat(), thanks to Florian Weimer:
* https://mail.gnome.org/archives/ostree-list/2014-February/msg00017.html
*/
snprintf (buf, sizeof (buf), "/proc/self/fd/%d/%s", dfd, name);
return get_xattrs_impl (buf, out_xattrs, cancellable, error);
}
}
static gboolean
set_all_xattrs_for_path (const char *path,
GVariant *xattrs,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
int i, n;
n = g_variant_n_children (xattrs);
for (i = 0; i < n; i++)
{
const guint8* name;
g_autoptr(GVariant) value = NULL;
const guint8* value_data;
gsize value_len;
g_variant_get_child (xattrs, i, "(^&ay@ay)",
&name, &value);
value_data = g_variant_get_fixed_array (value, &value_len, 1);
if (lsetxattr (path, (char*)name, (char*)value_data, value_len, 0) < 0)
{
glnx_set_prefix_error_from_errno (error, "%s", "lsetxattr");
goto out;
}
}
ret = TRUE;
out:
return ret;
}
/**
* glnx_dfd_name_set_all_xattrs:
* @dfd: Parent directory file descriptor
* @name: File name
* @xattrs: Extended attribute set
* @cancellable: Cancellable
* @error: Error
*
* Set all extended attributes for the file named @name residing in
* directory @dfd.
*/
gboolean
glnx_dfd_name_set_all_xattrs (int dfd,
const char *name,
GVariant *xattrs,
GCancellable *cancellable,
GError **error)
{
if (dfd == AT_FDCWD || dfd == -1)
{
return set_all_xattrs_for_path (name, xattrs, cancellable, error);
}
else
{
char buf[PATH_MAX];
/* A workaround for the lack of lsetxattrat(), thanks to Florian Weimer:
* https://mail.gnome.org/archives/ostree-list/2014-February/msg00017.html
*/
snprintf (buf, sizeof (buf), "/proc/self/fd/%d/%s", dfd, name);
return set_all_xattrs_for_path (buf, xattrs, cancellable, error);
}
}
/**
* glnx_fd_set_all_xattrs:
* @fd: 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 @fd. This function does not remove any
* attributes not in @xattrs.
*/
gboolean
glnx_fd_set_all_xattrs (int fd,
GVariant *xattrs,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
int i, n;
n = g_variant_n_children (xattrs);
for (i = 0; i < n; i++)
{
const guint8* name;
const guint8* value_data;
g_autoptr(GVariant) value = NULL;
gsize value_len;
int res;
g_variant_get_child (xattrs, i, "(^&ay@ay)",
&name, &value);
value_data = g_variant_get_fixed_array (value, &value_len, 1);
do
res = fsetxattr (fd, (char*)name, (char*)value_data, value_len, 0);
while (G_UNLIKELY (res == -1 && errno == EINTR));
if (G_UNLIKELY (res == -1))
{
glnx_set_prefix_error_from_errno (error, "%s", "fsetxattr");
goto out;
}
}
ret = TRUE;
out:
return ret;
}
/**
* glnx_lgetxattrat:
* @dfd: Directory file descriptor
* @subpath: Subpath
* @attribute: Extended attribute to retrieve
* @error: Error
*
* Retrieve an extended attribute value, relative to a directory file
* descriptor.
*/
GBytes *
glnx_lgetxattrat (int dfd,
const char *subpath,
const char *attribute,
GError **error)
{
char pathbuf[PATH_MAX];
GBytes *bytes = NULL;
ssize_t bytes_read, real_size;
guint8 *buf;
snprintf (pathbuf, sizeof (pathbuf), "/proc/self/fd/%d/%s", dfd, subpath);
do
bytes_read = lgetxattr (pathbuf, attribute, NULL, 0);
while (G_UNLIKELY (bytes_read < 0 && errno == EINTR));
if (G_UNLIKELY (bytes_read < 0))
{
glnx_set_error_from_errno (error);
goto out;
}
buf = g_malloc (bytes_read);
do
real_size = lgetxattr (pathbuf, attribute, buf, bytes_read);
while (G_UNLIKELY (real_size < 0 && errno == EINTR));
if (G_UNLIKELY (real_size < 0))
{
glnx_set_error_from_errno (error);
g_free (buf);
goto out;
}
bytes = g_bytes_new_take (buf, real_size);
out:
return bytes;
}
/**
* glnx_fgetxattr_bytes:
* @fd: Directory file descriptor
* @attribute: Extended attribute to retrieve
* @error: Error
*
* Returns: (transfer full): An extended attribute value, or %NULL on error
*/
GBytes *
glnx_fgetxattr_bytes (int fd,
const char *attribute,
GError **error)
{
GBytes *bytes = NULL;
ssize_t bytes_read, real_size;
guint8 *buf;
do
bytes_read = fgetxattr (fd, attribute, NULL, 0);
while (G_UNLIKELY (bytes_read < 0 && errno == EINTR));
if (G_UNLIKELY (bytes_read < 0))
{
glnx_set_error_from_errno (error);
goto out;
}
buf = g_malloc (bytes_read);
do
real_size = fgetxattr (fd, attribute, buf, bytes_read);
while (G_UNLIKELY (real_size < 0 && errno == EINTR));
if (G_UNLIKELY (real_size < 0))
{
glnx_set_error_from_errno (error);
g_free (buf);
goto out;
}
bytes = g_bytes_new_take (buf, real_size);
out:
return bytes;
}
/**
* glnx_lsetxattrat:
* @dfd: Directory file descriptor
* @subpath: Path
* @attribute: An attribute name
* @value: (array length=len) (element-type guint8): Attribute value
* @len: Length of @value
* @flags: Flags, containing either XATTR_CREATE or XATTR_REPLACE
* @error: Error
*
* Set an extended attribute, relative to a directory file descriptor.
*/
gboolean
glnx_lsetxattrat (int dfd,
const char *subpath,
const char *attribute,
const guint8 *value,
gsize len,
int flags,
GError **error)
{
char pathbuf[PATH_MAX];
int res;
snprintf (pathbuf, sizeof (pathbuf), "/proc/self/fd/%d/%s", dfd, subpath);
do
res = lsetxattr (subpath, attribute, value, len, flags);
while (G_UNLIKELY (res == -1 && errno == EINTR));
if (G_UNLIKELY (res == -1))
{
glnx_set_error_from_errno (error);
return FALSE;
}
return TRUE;
}
|