1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Alexander Larsson <alexl@redhat.com>
19 */
20
21 /**
22 * SECTION:gfileinfo
23 * @short_description: File Information and Attributes
24 * @include: gio/gio.h
25 * @see_also: #GFile, [GFileAttribute][gio-GFileAttribute]
26 *
27 * Functionality for manipulating basic metadata for files. #GFileInfo
28 * implements methods for getting information that all files should
29 * contain, and allows for manipulation of extended attributes.
30 *
31 * See [GFileAttribute][gio-GFileAttribute] for more information on how
32 * GIO handles file attributes.
33 *
34 * To obtain a #GFileInfo for a #GFile, use g_file_query_info() (or its
35 * async variant). To obtain a #GFileInfo for a file input or output
36 * stream, use g_file_input_stream_query_info() or
37 * g_file_output_stream_query_info() (or their async variants).
38 *
39 * To change the actual attributes of a file, you should then set the
40 * attribute in the #GFileInfo and call g_file_set_attributes_from_info()
41 * or g_file_set_attributes_async() on a GFile.
42 *
43 * However, not all attributes can be changed in the file. For instance,
44 * the actual size of a file cannot be changed via g_file_info_set_size().
45 * You may call g_file_query_settable_attributes() and
46 * g_file_query_writable_namespaces() to discover the settable attributes
47 * of a particular file at runtime.
48 *
49 * #GFileAttributeMatcher allows for searching through a #GFileInfo for
50 * attributes.
51 **/
52
53 #include "config.h"
54
55 #include <string.h>
56
57 #include "gfileinfo.h"
58 #include "gfileinfo-priv.h"
59 #include "gfileattribute-priv.h"
60 #include "gicon.h"
61 #include "glibintl.h"
62
63
64 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
65 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
66
67 typedef struct {
68 guint32 attribute;
69 GFileAttributeValue value;
70 } GFileAttribute;
71
72 struct _GFileInfo
73 {
74 GObject parent_instance;
75
76 GArray *attributes;
77 GFileAttributeMatcher *mask;
78 };
79
80 struct _GFileInfoClass
81 {
82 GObjectClass parent_class;
83 };
84
85
86 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT)
87
88 typedef struct {
89 guint32 id;
90 guint32 attribute_id_counter;
91 } NSInfo;
92
93 G_LOCK_DEFINE_STATIC (attribute_hash);
94 static int namespace_id_counter = 0;
95 static GHashTable *ns_hash = NULL;
96 static GHashTable *attribute_hash = NULL;
97 static char ***attributes = NULL;
98
99 /* Attribute ids are 32bit, we split it up like this:
100 * |------------|--------------------|
101 * 12 bit 20 bit
102 * namespace attribute id
103 *
104 * This way the attributes gets sorted in namespace order
105 */
106
107 #define NS_POS 20
108 #define NS_MASK ((guint32)((1<<12) - 1))
109 #define ID_POS 0
110 #define ID_MASK ((guint32)((1<<20) - 1))
111
112 #define GET_NS(_attr_id) \
113 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
114 #define GET_ID(_attr_id) \
115 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
116
117 #define MAKE_ATTR_ID(_ns, _id) \
118 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
119 ((((guint32) _id) & ID_MASK) << ID_POS) )
120
121 static NSInfo *
_lookup_namespace(const char * namespace)122 _lookup_namespace (const char *namespace)
123 {
124 NSInfo *ns_info;
125
126 ns_info = g_hash_table_lookup (ns_hash, namespace);
127 if (ns_info == NULL)
128 {
129 ns_info = g_new0 (NSInfo, 1);
130 ns_info->id = ++namespace_id_counter;
131 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
132 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
133 attributes[ns_info->id] = g_new (char *, 1);
134 attributes[ns_info->id][0] = g_strconcat (namespace, "::*", NULL);
135 }
136 return ns_info;
137 }
138
139 static guint32
_lookup_attribute(const char * attribute)140 _lookup_attribute (const char *attribute)
141 {
142 guint32 attr_id, id;
143 char *ns;
144 const char *colon;
145 NSInfo *ns_info;
146
147 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
148
149 if (attr_id != 0)
150 return attr_id;
151
152 colon = strstr (attribute, "::");
153 if (colon)
154 ns = g_strndup (attribute, colon - attribute);
155 else
156 ns = g_strdup ("");
157
158 ns_info = _lookup_namespace (ns);
159 g_free (ns);
160
161 id = ++ns_info->attribute_id_counter;
162 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
163 attributes[ns_info->id][id] = g_strdup (attribute);
164
165 attr_id = MAKE_ATTR_ID (ns_info->id, id);
166
167 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
168
169 return attr_id;
170 }
171
172 static void
ensure_attribute_hash(void)173 ensure_attribute_hash (void)
174 {
175 if (attribute_hash != NULL)
176 return;
177
178 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
179 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
180
181 #define REGISTER_ATTRIBUTE(name) G_STMT_START{\
182 guint _u G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */; \
183 _u = _lookup_attribute (G_FILE_ATTRIBUTE_ ## name); \
184 /* use for generating the ID: g_print ("#define G_FILE_ATTRIBUTE_ID_%s (%u + %u)\n", #name + 17, _u & ~ID_MASK, _u & ID_MASK); */ \
185 g_assert (_u == G_FILE_ATTRIBUTE_ID_ ## name); \
186 }G_STMT_END
187
188 REGISTER_ATTRIBUTE (STANDARD_TYPE);
189 REGISTER_ATTRIBUTE (STANDARD_IS_HIDDEN);
190 REGISTER_ATTRIBUTE (STANDARD_IS_BACKUP);
191 REGISTER_ATTRIBUTE (STANDARD_IS_SYMLINK);
192 REGISTER_ATTRIBUTE (STANDARD_IS_VIRTUAL);
193 REGISTER_ATTRIBUTE (STANDARD_NAME);
194 REGISTER_ATTRIBUTE (STANDARD_DISPLAY_NAME);
195 REGISTER_ATTRIBUTE (STANDARD_EDIT_NAME);
196 REGISTER_ATTRIBUTE (STANDARD_COPY_NAME);
197 REGISTER_ATTRIBUTE (STANDARD_DESCRIPTION);
198 REGISTER_ATTRIBUTE (STANDARD_ICON);
199 REGISTER_ATTRIBUTE (STANDARD_CONTENT_TYPE);
200 REGISTER_ATTRIBUTE (STANDARD_FAST_CONTENT_TYPE);
201 REGISTER_ATTRIBUTE (STANDARD_SIZE);
202 REGISTER_ATTRIBUTE (STANDARD_ALLOCATED_SIZE);
203 REGISTER_ATTRIBUTE (STANDARD_SYMLINK_TARGET);
204 REGISTER_ATTRIBUTE (STANDARD_TARGET_URI);
205 REGISTER_ATTRIBUTE (STANDARD_SORT_ORDER);
206 REGISTER_ATTRIBUTE (STANDARD_SYMBOLIC_ICON);
207 REGISTER_ATTRIBUTE (STANDARD_IS_VOLATILE);
208 REGISTER_ATTRIBUTE (ETAG_VALUE);
209 REGISTER_ATTRIBUTE (ID_FILE);
210 REGISTER_ATTRIBUTE (ID_FILESYSTEM);
211 REGISTER_ATTRIBUTE (ACCESS_CAN_READ);
212 REGISTER_ATTRIBUTE (ACCESS_CAN_WRITE);
213 REGISTER_ATTRIBUTE (ACCESS_CAN_EXECUTE);
214 REGISTER_ATTRIBUTE (ACCESS_CAN_DELETE);
215 REGISTER_ATTRIBUTE (ACCESS_CAN_TRASH);
216 REGISTER_ATTRIBUTE (ACCESS_CAN_RENAME);
217 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_MOUNT);
218 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_UNMOUNT);
219 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_EJECT);
220 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE);
221 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE_FILE);
222 REGISTER_ATTRIBUTE (MOUNTABLE_HAL_UDI);
223 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START);
224 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START_DEGRADED);
225 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_STOP);
226 REGISTER_ATTRIBUTE (MOUNTABLE_START_STOP_TYPE);
227 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_POLL);
228 REGISTER_ATTRIBUTE (MOUNTABLE_IS_MEDIA_CHECK_AUTOMATIC);
229 REGISTER_ATTRIBUTE (TIME_MODIFIED);
230 REGISTER_ATTRIBUTE (TIME_MODIFIED_USEC);
231 REGISTER_ATTRIBUTE (TIME_ACCESS);
232 REGISTER_ATTRIBUTE (TIME_ACCESS_USEC);
233 REGISTER_ATTRIBUTE (TIME_CHANGED);
234 REGISTER_ATTRIBUTE (TIME_CHANGED_USEC);
235 REGISTER_ATTRIBUTE (TIME_CREATED);
236 REGISTER_ATTRIBUTE (TIME_CREATED_USEC);
237 REGISTER_ATTRIBUTE (UNIX_DEVICE);
238 REGISTER_ATTRIBUTE (UNIX_INODE);
239 REGISTER_ATTRIBUTE (UNIX_MODE);
240 REGISTER_ATTRIBUTE (UNIX_NLINK);
241 REGISTER_ATTRIBUTE (UNIX_UID);
242 REGISTER_ATTRIBUTE (UNIX_GID);
243 REGISTER_ATTRIBUTE (UNIX_RDEV);
244 REGISTER_ATTRIBUTE (UNIX_BLOCK_SIZE);
245 REGISTER_ATTRIBUTE (UNIX_BLOCKS);
246 REGISTER_ATTRIBUTE (UNIX_IS_MOUNTPOINT);
247 REGISTER_ATTRIBUTE (DOS_IS_ARCHIVE);
248 REGISTER_ATTRIBUTE (DOS_IS_SYSTEM);
249 REGISTER_ATTRIBUTE (DOS_IS_MOUNTPOINT);
250 REGISTER_ATTRIBUTE (DOS_REPARSE_POINT_TAG);
251 REGISTER_ATTRIBUTE (OWNER_USER);
252 REGISTER_ATTRIBUTE (OWNER_USER_REAL);
253 REGISTER_ATTRIBUTE (OWNER_GROUP);
254 REGISTER_ATTRIBUTE (THUMBNAIL_PATH);
255 REGISTER_ATTRIBUTE (THUMBNAILING_FAILED);
256 REGISTER_ATTRIBUTE (THUMBNAIL_IS_VALID);
257 REGISTER_ATTRIBUTE (PREVIEW_ICON);
258 REGISTER_ATTRIBUTE (FILESYSTEM_SIZE);
259 REGISTER_ATTRIBUTE (FILESYSTEM_FREE);
260 REGISTER_ATTRIBUTE (FILESYSTEM_TYPE);
261 REGISTER_ATTRIBUTE (FILESYSTEM_READONLY);
262 REGISTER_ATTRIBUTE (FILESYSTEM_USE_PREVIEW);
263 REGISTER_ATTRIBUTE (GVFS_BACKEND);
264 REGISTER_ATTRIBUTE (SELINUX_CONTEXT);
265 REGISTER_ATTRIBUTE (TRASH_ITEM_COUNT);
266 REGISTER_ATTRIBUTE (TRASH_ORIG_PATH);
267 REGISTER_ATTRIBUTE (TRASH_DELETION_DATE);
268
269 #undef REGISTER_ATTRIBUTE
270 }
271
272 static guint32
lookup_namespace(const char * namespace)273 lookup_namespace (const char *namespace)
274 {
275 NSInfo *ns_info;
276 guint32 id;
277
278 G_LOCK (attribute_hash);
279
280 ensure_attribute_hash ();
281
282 ns_info = _lookup_namespace (namespace);
283 id = 0;
284 if (ns_info)
285 id = ns_info->id;
286
287 G_UNLOCK (attribute_hash);
288
289 return id;
290 }
291
292 static char *
get_attribute_for_id(int attribute)293 get_attribute_for_id (int attribute)
294 {
295 char *s;
296 G_LOCK (attribute_hash);
297 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
298 G_UNLOCK (attribute_hash);
299 return s;
300 }
301
302 static guint32
lookup_attribute(const char * attribute)303 lookup_attribute (const char *attribute)
304 {
305 guint32 attr_id;
306
307 G_LOCK (attribute_hash);
308 ensure_attribute_hash ();
309
310 attr_id = _lookup_attribute (attribute);
311
312 G_UNLOCK (attribute_hash);
313
314 return attr_id;
315 }
316
317 static void
g_file_info_finalize(GObject * object)318 g_file_info_finalize (GObject *object)
319 {
320 GFileInfo *info;
321 guint i;
322 GFileAttribute *attrs;
323
324 info = G_FILE_INFO (object);
325
326 attrs = (GFileAttribute *)info->attributes->data;
327 for (i = 0; i < info->attributes->len; i++)
328 _g_file_attribute_value_clear (&attrs[i].value);
329 g_array_free (info->attributes, TRUE);
330
331 if (info->mask != NO_ATTRIBUTE_MASK)
332 g_file_attribute_matcher_unref (info->mask);
333
334 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
335 }
336
337 static void
g_file_info_class_init(GFileInfoClass * klass)338 g_file_info_class_init (GFileInfoClass *klass)
339 {
340 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
341
342 gobject_class->finalize = g_file_info_finalize;
343 }
344
345 static void
g_file_info_init(GFileInfo * info)346 g_file_info_init (GFileInfo *info)
347 {
348 info->mask = NO_ATTRIBUTE_MASK;
349 info->attributes = g_array_new (FALSE, FALSE,
350 sizeof (GFileAttribute));
351 }
352
353 /**
354 * g_file_info_new:
355 *
356 * Creates a new file info structure.
357 *
358 * Returns: a #GFileInfo.
359 **/
360 GFileInfo *
g_file_info_new(void)361 g_file_info_new (void)
362 {
363 return g_object_new (G_TYPE_FILE_INFO, NULL);
364 }
365
366 /**
367 * g_file_info_copy_into:
368 * @src_info: source to copy attributes from.
369 * @dest_info: destination to copy attributes to.
370 *
371 * First clears all of the [GFileAttribute][gio-GFileAttribute] of @dest_info,
372 * and then copies all of the file attributes from @src_info to @dest_info.
373 **/
374 void
g_file_info_copy_into(GFileInfo * src_info,GFileInfo * dest_info)375 g_file_info_copy_into (GFileInfo *src_info,
376 GFileInfo *dest_info)
377 {
378 GFileAttribute *source, *dest;
379 guint i;
380
381 g_return_if_fail (G_IS_FILE_INFO (src_info));
382 g_return_if_fail (G_IS_FILE_INFO (dest_info));
383
384 dest = (GFileAttribute *)dest_info->attributes->data;
385 for (i = 0; i < dest_info->attributes->len; i++)
386 _g_file_attribute_value_clear (&dest[i].value);
387
388 g_array_set_size (dest_info->attributes,
389 src_info->attributes->len);
390
391 source = (GFileAttribute *)src_info->attributes->data;
392 dest = (GFileAttribute *)dest_info->attributes->data;
393
394 for (i = 0; i < src_info->attributes->len; i++)
395 {
396 dest[i].attribute = source[i].attribute;
397 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
398 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
399 }
400
401 if (dest_info->mask != NO_ATTRIBUTE_MASK)
402 g_file_attribute_matcher_unref (dest_info->mask);
403
404 if (src_info->mask == NO_ATTRIBUTE_MASK)
405 dest_info->mask = NO_ATTRIBUTE_MASK;
406 else
407 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
408 }
409
410 /**
411 * g_file_info_dup:
412 * @other: a #GFileInfo.
413 *
414 * Duplicates a file info structure.
415 *
416 * Returns: (transfer full): a duplicate #GFileInfo of @other.
417 **/
418 GFileInfo *
g_file_info_dup(GFileInfo * other)419 g_file_info_dup (GFileInfo *other)
420 {
421 GFileInfo *new;
422
423 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
424
425 new = g_file_info_new ();
426 g_file_info_copy_into (other, new);
427 return new;
428 }
429
430 /**
431 * g_file_info_set_attribute_mask:
432 * @info: a #GFileInfo.
433 * @mask: a #GFileAttributeMatcher.
434 *
435 * Sets @mask on @info to match specific attribute types.
436 **/
437 void
g_file_info_set_attribute_mask(GFileInfo * info,GFileAttributeMatcher * mask)438 g_file_info_set_attribute_mask (GFileInfo *info,
439 GFileAttributeMatcher *mask)
440 {
441 GFileAttribute *attr;
442 guint i;
443
444 g_return_if_fail (G_IS_FILE_INFO (info));
445
446 if (mask != info->mask)
447 {
448 if (info->mask != NO_ATTRIBUTE_MASK)
449 g_file_attribute_matcher_unref (info->mask);
450 info->mask = g_file_attribute_matcher_ref (mask);
451
452 /* Remove non-matching attributes */
453 for (i = 0; i < info->attributes->len; i++)
454 {
455 attr = &g_array_index (info->attributes, GFileAttribute, i);
456 if (!_g_file_attribute_matcher_matches_id (mask,
457 attr->attribute))
458 {
459 _g_file_attribute_value_clear (&attr->value);
460 g_array_remove_index (info->attributes, i);
461 i--;
462 }
463 }
464 }
465 }
466
467 /**
468 * g_file_info_unset_attribute_mask:
469 * @info: #GFileInfo.
470 *
471 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
472 * is set.
473 **/
474 void
g_file_info_unset_attribute_mask(GFileInfo * info)475 g_file_info_unset_attribute_mask (GFileInfo *info)
476 {
477 g_return_if_fail (G_IS_FILE_INFO (info));
478
479 if (info->mask != NO_ATTRIBUTE_MASK)
480 g_file_attribute_matcher_unref (info->mask);
481 info->mask = NO_ATTRIBUTE_MASK;
482 }
483
484 /**
485 * g_file_info_clear_status:
486 * @info: a #GFileInfo.
487 *
488 * Clears the status information from @info.
489 **/
490 void
g_file_info_clear_status(GFileInfo * info)491 g_file_info_clear_status (GFileInfo *info)
492 {
493 GFileAttribute *attrs;
494 guint i;
495
496 g_return_if_fail (G_IS_FILE_INFO (info));
497
498 attrs = (GFileAttribute *)info->attributes->data;
499 for (i = 0; i < info->attributes->len; i++)
500 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
501 }
502
503 static int
g_file_info_find_place(GFileInfo * info,guint32 attribute)504 g_file_info_find_place (GFileInfo *info,
505 guint32 attribute)
506 {
507 int min, max, med;
508 GFileAttribute *attrs;
509 /* Binary search for the place where attribute would be, if it's
510 in the array */
511
512 min = 0;
513 max = info->attributes->len;
514
515 attrs = (GFileAttribute *)info->attributes->data;
516
517 while (min < max)
518 {
519 med = min + (max - min) / 2;
520 if (attrs[med].attribute == attribute)
521 {
522 min = med;
523 break;
524 }
525 else if (attrs[med].attribute < attribute)
526 min = med + 1;
527 else /* attrs[med].attribute > attribute */
528 max = med;
529 }
530
531 return min;
532 }
533
534 static GFileAttributeValue *
g_file_info_find_value(GFileInfo * info,guint32 attr_id)535 g_file_info_find_value (GFileInfo *info,
536 guint32 attr_id)
537 {
538 GFileAttribute *attrs;
539 guint i;
540
541 i = g_file_info_find_place (info, attr_id);
542 attrs = (GFileAttribute *)info->attributes->data;
543 if (i < info->attributes->len &&
544 attrs[i].attribute == attr_id)
545 return &attrs[i].value;
546
547 return NULL;
548 }
549
550 static GFileAttributeValue *
g_file_info_find_value_by_name(GFileInfo * info,const char * attribute)551 g_file_info_find_value_by_name (GFileInfo *info,
552 const char *attribute)
553 {
554 guint32 attr_id;
555
556 attr_id = lookup_attribute (attribute);
557 return g_file_info_find_value (info, attr_id);
558 }
559
560 /**
561 * g_file_info_has_attribute:
562 * @info: a #GFileInfo.
563 * @attribute: a file attribute key.
564 *
565 * Checks if a file info structure has an attribute named @attribute.
566 *
567 * Returns: %TRUE if @info has an attribute named @attribute,
568 * %FALSE otherwise.
569 **/
570 gboolean
g_file_info_has_attribute(GFileInfo * info,const char * attribute)571 g_file_info_has_attribute (GFileInfo *info,
572 const char *attribute)
573 {
574 GFileAttributeValue *value;
575
576 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
577 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
578
579 value = g_file_info_find_value_by_name (info, attribute);
580 return value != NULL;
581 }
582
583 /**
584 * g_file_info_has_namespace:
585 * @info: a #GFileInfo.
586 * @name_space: a file attribute namespace.
587 *
588 * Checks if a file info structure has an attribute in the
589 * specified @name_space.
590 *
591 * Returns: %TRUE if @info has an attribute in @name_space,
592 * %FALSE otherwise.
593 *
594 * Since: 2.22
595 **/
596 gboolean
g_file_info_has_namespace(GFileInfo * info,const char * name_space)597 g_file_info_has_namespace (GFileInfo *info,
598 const char *name_space)
599 {
600 GFileAttribute *attrs;
601 guint32 ns_id;
602 guint i;
603
604 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
605 g_return_val_if_fail (name_space != NULL, FALSE);
606
607 ns_id = lookup_namespace (name_space);
608
609 attrs = (GFileAttribute *)info->attributes->data;
610 for (i = 0; i < info->attributes->len; i++)
611 {
612 if (GET_NS (attrs[i].attribute) == ns_id)
613 return TRUE;
614 }
615
616 return FALSE;
617 }
618
619 /**
620 * g_file_info_list_attributes:
621 * @info: a #GFileInfo.
622 * @name_space: (nullable): a file attribute key's namespace, or %NULL to list
623 * all attributes.
624 *
625 * Lists the file info structure's attributes.
626 *
627 * Returns: (nullable) (array zero-terminated=1) (transfer full): a
628 * null-terminated array of strings of all of the possible attribute
629 * types for the given @name_space, or %NULL on error.
630 **/
631 char **
g_file_info_list_attributes(GFileInfo * info,const char * name_space)632 g_file_info_list_attributes (GFileInfo *info,
633 const char *name_space)
634 {
635 GPtrArray *names;
636 GFileAttribute *attrs;
637 guint32 attribute;
638 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
639 guint i;
640
641 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
642
643 names = g_ptr_array_new ();
644 attrs = (GFileAttribute *)info->attributes->data;
645 for (i = 0; i < info->attributes->len; i++)
646 {
647 attribute = attrs[i].attribute;
648 if (ns_id == 0 || GET_NS (attribute) == ns_id)
649 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
650 }
651
652 /* NULL terminate */
653 g_ptr_array_add (names, NULL);
654
655 return (char **)g_ptr_array_free (names, FALSE);
656 }
657
658 /**
659 * g_file_info_get_attribute_type:
660 * @info: a #GFileInfo.
661 * @attribute: a file attribute key.
662 *
663 * Gets the attribute type for an attribute key.
664 *
665 * Returns: a #GFileAttributeType for the given @attribute, or
666 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
667 **/
668 GFileAttributeType
g_file_info_get_attribute_type(GFileInfo * info,const char * attribute)669 g_file_info_get_attribute_type (GFileInfo *info,
670 const char *attribute)
671 {
672 GFileAttributeValue *value;
673
674 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
675 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
676
677 value = g_file_info_find_value_by_name (info, attribute);
678 if (value)
679 return value->type;
680 else
681 return G_FILE_ATTRIBUTE_TYPE_INVALID;
682 }
683
684 /**
685 * g_file_info_remove_attribute:
686 * @info: a #GFileInfo.
687 * @attribute: a file attribute key.
688 *
689 * Removes all cases of @attribute from @info if it exists.
690 **/
691 void
g_file_info_remove_attribute(GFileInfo * info,const char * attribute)692 g_file_info_remove_attribute (GFileInfo *info,
693 const char *attribute)
694 {
695 guint32 attr_id;
696 GFileAttribute *attrs;
697 guint i;
698
699 g_return_if_fail (G_IS_FILE_INFO (info));
700 g_return_if_fail (attribute != NULL && *attribute != '\0');
701
702 attr_id = lookup_attribute (attribute);
703
704 i = g_file_info_find_place (info, attr_id);
705 attrs = (GFileAttribute *)info->attributes->data;
706 if (i < info->attributes->len &&
707 attrs[i].attribute == attr_id)
708 {
709 _g_file_attribute_value_clear (&attrs[i].value);
710 g_array_remove_index (info->attributes, i);
711 }
712 }
713
714 /**
715 * g_file_info_get_attribute_data:
716 * @info: a #GFileInfo
717 * @attribute: a file attribute key
718 * @type: (out) (optional): return location for the attribute type, or %NULL
719 * @value_pp: (out) (optional) (not nullable): return location for the
720 * attribute value, or %NULL; the attribute value will not be %NULL
721 * @status: (out) (optional): return location for the attribute status, or %NULL
722 *
723 * Gets the attribute type, value and status for an attribute key.
724 *
725 * Returns: (transfer none): %TRUE if @info has an attribute named @attribute,
726 * %FALSE otherwise.
727 */
728 gboolean
g_file_info_get_attribute_data(GFileInfo * info,const char * attribute,GFileAttributeType * type,gpointer * value_pp,GFileAttributeStatus * status)729 g_file_info_get_attribute_data (GFileInfo *info,
730 const char *attribute,
731 GFileAttributeType *type,
732 gpointer *value_pp,
733 GFileAttributeStatus *status)
734 {
735 GFileAttributeValue *value;
736
737 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
738 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
739
740 value = g_file_info_find_value_by_name (info, attribute);
741 if (value == NULL)
742 return FALSE;
743
744 if (status)
745 *status = value->status;
746
747 if (type)
748 *type = value->type;
749
750 if (value_pp)
751 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
752
753 return TRUE;
754 }
755
756 /**
757 * g_file_info_get_attribute_status:
758 * @info: a #GFileInfo
759 * @attribute: a file attribute key
760 *
761 * Gets the attribute status for an attribute key.
762 *
763 * Returns: a #GFileAttributeStatus for the given @attribute, or
764 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
765 *
766 */
767 GFileAttributeStatus
g_file_info_get_attribute_status(GFileInfo * info,const char * attribute)768 g_file_info_get_attribute_status (GFileInfo *info,
769 const char *attribute)
770 {
771 GFileAttributeValue *val;
772
773 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
774 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
775
776 val = g_file_info_find_value_by_name (info, attribute);
777 if (val)
778 return val->status;
779
780 return G_FILE_ATTRIBUTE_STATUS_UNSET;
781 }
782
783 /**
784 * g_file_info_set_attribute_status:
785 * @info: a #GFileInfo
786 * @attribute: a file attribute key
787 * @status: a #GFileAttributeStatus
788 *
789 * Sets the attribute status for an attribute key. This is only
790 * needed by external code that implement g_file_set_attributes_from_info()
791 * or similar functions.
792 *
793 * The attribute must exist in @info for this to work. Otherwise %FALSE
794 * is returned and @info is unchanged.
795 *
796 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
797 *
798 * Since: 2.22
799 */
800 gboolean
g_file_info_set_attribute_status(GFileInfo * info,const char * attribute,GFileAttributeStatus status)801 g_file_info_set_attribute_status (GFileInfo *info,
802 const char *attribute,
803 GFileAttributeStatus status)
804 {
805 GFileAttributeValue *val;
806
807 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
808 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
809
810 val = g_file_info_find_value_by_name (info, attribute);
811 if (val)
812 {
813 val->status = status;
814 return TRUE;
815 }
816
817 return FALSE;
818 }
819
820 GFileAttributeValue *
_g_file_info_get_attribute_value(GFileInfo * info,const char * attribute)821 _g_file_info_get_attribute_value (GFileInfo *info,
822 const char *attribute)
823
824 {
825 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
826 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
827
828 return g_file_info_find_value_by_name (info, attribute);
829 }
830
831 /**
832 * g_file_info_get_attribute_as_string:
833 * @info: a #GFileInfo.
834 * @attribute: a file attribute key.
835 *
836 * Gets the value of a attribute, formatted as a string.
837 * This escapes things as needed to make the string valid
838 * UTF-8.
839 *
840 * Returns: (nullable): a UTF-8 string associated with the given @attribute, or
841 * %NULL if the attribute wasn’t set.
842 * When you're done with the string it must be freed with g_free().
843 **/
844 char *
g_file_info_get_attribute_as_string(GFileInfo * info,const char * attribute)845 g_file_info_get_attribute_as_string (GFileInfo *info,
846 const char *attribute)
847 {
848 GFileAttributeValue *val;
849 val = _g_file_info_get_attribute_value (info, attribute);
850 if (val)
851 return _g_file_attribute_value_as_string (val);
852 return NULL;
853 }
854
855
856 /**
857 * g_file_info_get_attribute_object:
858 * @info: a #GFileInfo.
859 * @attribute: a file attribute key.
860 *
861 * Gets the value of a #GObject attribute. If the attribute does
862 * not contain a #GObject, %NULL will be returned.
863 *
864 * Returns: (transfer none) (nullable): a #GObject associated with the given @attribute,
865 * or %NULL otherwise.
866 **/
867 GObject *
g_file_info_get_attribute_object(GFileInfo * info,const char * attribute)868 g_file_info_get_attribute_object (GFileInfo *info,
869 const char *attribute)
870 {
871 GFileAttributeValue *value;
872
873 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
874 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
875
876 value = g_file_info_find_value_by_name (info, attribute);
877 return _g_file_attribute_value_get_object (value);
878 }
879
880 /**
881 * g_file_info_get_attribute_string:
882 * @info: a #GFileInfo.
883 * @attribute: a file attribute key.
884 *
885 * Gets the value of a string attribute. If the attribute does
886 * not contain a string, %NULL will be returned.
887 *
888 * Returns: (nullable): the contents of the @attribute value as a UTF-8 string,
889 * or %NULL otherwise.
890 **/
891 const char *
g_file_info_get_attribute_string(GFileInfo * info,const char * attribute)892 g_file_info_get_attribute_string (GFileInfo *info,
893 const char *attribute)
894 {
895 GFileAttributeValue *value;
896
897 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
898 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
899
900 value = g_file_info_find_value_by_name (info, attribute);
901 return _g_file_attribute_value_get_string (value);
902 }
903
904 /**
905 * g_file_info_get_attribute_byte_string:
906 * @info: a #GFileInfo.
907 * @attribute: a file attribute key.
908 *
909 * Gets the value of a byte string attribute. If the attribute does
910 * not contain a byte string, %NULL will be returned.
911 *
912 * Returns: (nullable): the contents of the @attribute value as a byte string, or
913 * %NULL otherwise.
914 **/
915 const char *
g_file_info_get_attribute_byte_string(GFileInfo * info,const char * attribute)916 g_file_info_get_attribute_byte_string (GFileInfo *info,
917 const char *attribute)
918 {
919 GFileAttributeValue *value;
920
921 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
922 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
923
924 value = g_file_info_find_value_by_name (info, attribute);
925 return _g_file_attribute_value_get_byte_string (value);
926 }
927
928 /**
929 * g_file_info_get_attribute_stringv:
930 * @info: a #GFileInfo.
931 * @attribute: a file attribute key.
932 *
933 * Gets the value of a stringv attribute. If the attribute does
934 * not contain a stringv, %NULL will be returned.
935 *
936 * Returns: (transfer none) (nullable): the contents of the @attribute value as a stringv,
937 * or %NULL otherwise. Do not free. These returned strings are UTF-8.
938 *
939 * Since: 2.22
940 **/
941 char **
g_file_info_get_attribute_stringv(GFileInfo * info,const char * attribute)942 g_file_info_get_attribute_stringv (GFileInfo *info,
943 const char *attribute)
944 {
945 GFileAttributeValue *value;
946
947 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
948 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
949
950 value = g_file_info_find_value_by_name (info, attribute);
951 return _g_file_attribute_value_get_stringv (value);
952 }
953
954 /**
955 * g_file_info_get_attribute_boolean:
956 * @info: a #GFileInfo.
957 * @attribute: a file attribute key.
958 *
959 * Gets the value of a boolean attribute. If the attribute does not
960 * contain a boolean value, %FALSE will be returned.
961 *
962 * Returns: the boolean value contained within the attribute.
963 **/
964 gboolean
g_file_info_get_attribute_boolean(GFileInfo * info,const char * attribute)965 g_file_info_get_attribute_boolean (GFileInfo *info,
966 const char *attribute)
967 {
968 GFileAttributeValue *value;
969
970 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
971 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
972
973 value = g_file_info_find_value_by_name (info, attribute);
974 return _g_file_attribute_value_get_boolean (value);
975 }
976
977 /**
978 * g_file_info_get_attribute_uint32:
979 * @info: a #GFileInfo.
980 * @attribute: a file attribute key.
981 *
982 * Gets an unsigned 32-bit integer contained within the attribute. If the
983 * attribute does not contain an unsigned 32-bit integer, or is invalid,
984 * 0 will be returned.
985 *
986 * Returns: an unsigned 32-bit integer from the attribute.
987 **/
988 guint32
g_file_info_get_attribute_uint32(GFileInfo * info,const char * attribute)989 g_file_info_get_attribute_uint32 (GFileInfo *info,
990 const char *attribute)
991 {
992 GFileAttributeValue *value;
993
994 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
995 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
996
997 value = g_file_info_find_value_by_name (info, attribute);
998 return _g_file_attribute_value_get_uint32 (value);
999 }
1000
1001 /**
1002 * g_file_info_get_attribute_int32:
1003 * @info: a #GFileInfo.
1004 * @attribute: a file attribute key.
1005 *
1006 * Gets a signed 32-bit integer contained within the attribute. If the
1007 * attribute does not contain a signed 32-bit integer, or is invalid,
1008 * 0 will be returned.
1009 *
1010 * Returns: a signed 32-bit integer from the attribute.
1011 **/
1012 gint32
g_file_info_get_attribute_int32(GFileInfo * info,const char * attribute)1013 g_file_info_get_attribute_int32 (GFileInfo *info,
1014 const char *attribute)
1015 {
1016 GFileAttributeValue *value;
1017
1018 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1019 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1020
1021 value = g_file_info_find_value_by_name (info, attribute);
1022 return _g_file_attribute_value_get_int32 (value);
1023 }
1024
1025 /**
1026 * g_file_info_get_attribute_uint64:
1027 * @info: a #GFileInfo.
1028 * @attribute: a file attribute key.
1029 *
1030 * Gets a unsigned 64-bit integer contained within the attribute. If the
1031 * attribute does not contain an unsigned 64-bit integer, or is invalid,
1032 * 0 will be returned.
1033 *
1034 * Returns: a unsigned 64-bit integer from the attribute.
1035 **/
1036 guint64
g_file_info_get_attribute_uint64(GFileInfo * info,const char * attribute)1037 g_file_info_get_attribute_uint64 (GFileInfo *info,
1038 const char *attribute)
1039 {
1040 GFileAttributeValue *value;
1041
1042 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1043 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1044
1045 value = g_file_info_find_value_by_name (info, attribute);
1046 return _g_file_attribute_value_get_uint64 (value);
1047 }
1048
1049 /**
1050 * g_file_info_get_attribute_int64:
1051 * @info: a #GFileInfo.
1052 * @attribute: a file attribute key.
1053 *
1054 * Gets a signed 64-bit integer contained within the attribute. If the
1055 * attribute does not contain a signed 64-bit integer, or is invalid,
1056 * 0 will be returned.
1057 *
1058 * Returns: a signed 64-bit integer from the attribute.
1059 **/
1060 gint64
g_file_info_get_attribute_int64(GFileInfo * info,const char * attribute)1061 g_file_info_get_attribute_int64 (GFileInfo *info,
1062 const char *attribute)
1063 {
1064 GFileAttributeValue *value;
1065
1066 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1067 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1068
1069 value = g_file_info_find_value_by_name (info, attribute);
1070 return _g_file_attribute_value_get_int64 (value);
1071 }
1072
1073 static GFileAttributeValue *
g_file_info_create_value(GFileInfo * info,guint32 attr_id)1074 g_file_info_create_value (GFileInfo *info,
1075 guint32 attr_id)
1076 {
1077 GFileAttribute *attrs;
1078 guint i;
1079
1080 if (info->mask != NO_ATTRIBUTE_MASK &&
1081 !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
1082 return NULL;
1083
1084 i = g_file_info_find_place (info, attr_id);
1085
1086 attrs = (GFileAttribute *)info->attributes->data;
1087 if (i < info->attributes->len &&
1088 attrs[i].attribute == attr_id)
1089 return &attrs[i].value;
1090 else
1091 {
1092 GFileAttribute attr = { 0 };
1093 attr.attribute = attr_id;
1094 g_array_insert_val (info->attributes, i, attr);
1095
1096 attrs = (GFileAttribute *)info->attributes->data;
1097 return &attrs[i].value;
1098 }
1099 }
1100
1101 void
_g_file_info_set_attribute_by_id(GFileInfo * info,guint32 attribute,GFileAttributeType type,gpointer value_p)1102 _g_file_info_set_attribute_by_id (GFileInfo *info,
1103 guint32 attribute,
1104 GFileAttributeType type,
1105 gpointer value_p)
1106 {
1107 GFileAttributeValue *value;
1108
1109 value = g_file_info_create_value (info, attribute);
1110
1111 if (value)
1112 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1113 }
1114
1115 /**
1116 * g_file_info_set_attribute:
1117 * @info: a #GFileInfo.
1118 * @attribute: a file attribute key.
1119 * @type: a #GFileAttributeType
1120 * @value_p: (not nullable): pointer to the value
1121 *
1122 * Sets the @attribute to contain the given value, if possible. To unset the
1123 * attribute, use %G_FILE_ATTRIBUTE_TYPE_INVALID for @type.
1124 **/
1125 void
g_file_info_set_attribute(GFileInfo * info,const char * attribute,GFileAttributeType type,gpointer value_p)1126 g_file_info_set_attribute (GFileInfo *info,
1127 const char *attribute,
1128 GFileAttributeType type,
1129 gpointer value_p)
1130 {
1131 g_return_if_fail (G_IS_FILE_INFO (info));
1132 g_return_if_fail (attribute != NULL && *attribute != '\0');
1133
1134 _g_file_info_set_attribute_by_id (info, lookup_attribute (attribute), type, value_p);
1135 }
1136
1137 void
_g_file_info_set_attribute_object_by_id(GFileInfo * info,guint32 attribute,GObject * attr_value)1138 _g_file_info_set_attribute_object_by_id (GFileInfo *info,
1139 guint32 attribute,
1140 GObject *attr_value)
1141 {
1142 GFileAttributeValue *value;
1143
1144 value = g_file_info_create_value (info, attribute);
1145 if (value)
1146 _g_file_attribute_value_set_object (value, attr_value);
1147 }
1148
1149 /**
1150 * g_file_info_set_attribute_object:
1151 * @info: a #GFileInfo.
1152 * @attribute: a file attribute key.
1153 * @attr_value: a #GObject.
1154 *
1155 * Sets the @attribute to contain the given @attr_value,
1156 * if possible.
1157 **/
1158 void
g_file_info_set_attribute_object(GFileInfo * info,const char * attribute,GObject * attr_value)1159 g_file_info_set_attribute_object (GFileInfo *info,
1160 const char *attribute,
1161 GObject *attr_value)
1162 {
1163 g_return_if_fail (G_IS_FILE_INFO (info));
1164 g_return_if_fail (attribute != NULL && *attribute != '\0');
1165 g_return_if_fail (G_IS_OBJECT (attr_value));
1166
1167 _g_file_info_set_attribute_object_by_id (info,
1168 lookup_attribute (attribute),
1169 attr_value);
1170 }
1171
1172 void
_g_file_info_set_attribute_stringv_by_id(GFileInfo * info,guint32 attribute,char ** attr_value)1173 _g_file_info_set_attribute_stringv_by_id (GFileInfo *info,
1174 guint32 attribute,
1175 char **attr_value)
1176 {
1177 GFileAttributeValue *value;
1178
1179 value = g_file_info_create_value (info, attribute);
1180 if (value)
1181 _g_file_attribute_value_set_stringv (value, attr_value);
1182 }
1183
1184 /**
1185 * g_file_info_set_attribute_stringv:
1186 * @info: a #GFileInfo.
1187 * @attribute: a file attribute key
1188 * @attr_value: (array zero-terminated=1) (element-type utf8): a %NULL
1189 * terminated array of UTF-8 strings.
1190 *
1191 * Sets the @attribute to contain the given @attr_value,
1192 * if possible.
1193 *
1194 * Sinze: 2.22
1195 **/
1196 void
g_file_info_set_attribute_stringv(GFileInfo * info,const char * attribute,char ** attr_value)1197 g_file_info_set_attribute_stringv (GFileInfo *info,
1198 const char *attribute,
1199 char **attr_value)
1200 {
1201 g_return_if_fail (G_IS_FILE_INFO (info));
1202 g_return_if_fail (attribute != NULL && *attribute != '\0');
1203 g_return_if_fail (attr_value != NULL);
1204
1205 _g_file_info_set_attribute_stringv_by_id (info,
1206 lookup_attribute (attribute),
1207 attr_value);
1208 }
1209
1210 void
_g_file_info_set_attribute_string_by_id(GFileInfo * info,guint32 attribute,const char * attr_value)1211 _g_file_info_set_attribute_string_by_id (GFileInfo *info,
1212 guint32 attribute,
1213 const char *attr_value)
1214 {
1215 GFileAttributeValue *value;
1216
1217 value = g_file_info_create_value (info, attribute);
1218 if (value)
1219 _g_file_attribute_value_set_string (value, attr_value);
1220 }
1221
1222 /**
1223 * g_file_info_set_attribute_string:
1224 * @info: a #GFileInfo.
1225 * @attribute: a file attribute key.
1226 * @attr_value: a UTF-8 string.
1227 *
1228 * Sets the @attribute to contain the given @attr_value,
1229 * if possible.
1230 **/
1231 void
g_file_info_set_attribute_string(GFileInfo * info,const char * attribute,const char * attr_value)1232 g_file_info_set_attribute_string (GFileInfo *info,
1233 const char *attribute,
1234 const char *attr_value)
1235 {
1236 g_return_if_fail (G_IS_FILE_INFO (info));
1237 g_return_if_fail (attribute != NULL && *attribute != '\0');
1238 g_return_if_fail (attr_value != NULL);
1239
1240 _g_file_info_set_attribute_string_by_id (info,
1241 lookup_attribute (attribute),
1242 attr_value);
1243 }
1244
1245 void
_g_file_info_set_attribute_byte_string_by_id(GFileInfo * info,guint32 attribute,const char * attr_value)1246 _g_file_info_set_attribute_byte_string_by_id (GFileInfo *info,
1247 guint32 attribute,
1248 const char *attr_value)
1249 {
1250 GFileAttributeValue *value;
1251
1252 value = g_file_info_create_value (info, attribute);
1253 if (value)
1254 _g_file_attribute_value_set_byte_string (value, attr_value);
1255 }
1256
1257 /**
1258 * g_file_info_set_attribute_byte_string:
1259 * @info: a #GFileInfo.
1260 * @attribute: a file attribute key.
1261 * @attr_value: a byte string.
1262 *
1263 * Sets the @attribute to contain the given @attr_value,
1264 * if possible.
1265 **/
1266 void
g_file_info_set_attribute_byte_string(GFileInfo * info,const char * attribute,const char * attr_value)1267 g_file_info_set_attribute_byte_string (GFileInfo *info,
1268 const char *attribute,
1269 const char *attr_value)
1270 {
1271 g_return_if_fail (G_IS_FILE_INFO (info));
1272 g_return_if_fail (attribute != NULL && *attribute != '\0');
1273 g_return_if_fail (attr_value != NULL);
1274
1275 _g_file_info_set_attribute_byte_string_by_id (info,
1276 lookup_attribute (attribute),
1277 attr_value);
1278 }
1279
1280 void
_g_file_info_set_attribute_boolean_by_id(GFileInfo * info,guint32 attribute,gboolean attr_value)1281 _g_file_info_set_attribute_boolean_by_id (GFileInfo *info,
1282 guint32 attribute,
1283 gboolean attr_value)
1284 {
1285 GFileAttributeValue *value;
1286
1287 value = g_file_info_create_value (info, attribute);
1288 if (value)
1289 _g_file_attribute_value_set_boolean (value, attr_value);
1290 }
1291
1292 /**
1293 * g_file_info_set_attribute_boolean:
1294 * @info: a #GFileInfo.
1295 * @attribute: a file attribute key.
1296 * @attr_value: a boolean value.
1297 *
1298 * Sets the @attribute to contain the given @attr_value,
1299 * if possible.
1300 **/
1301 void
g_file_info_set_attribute_boolean(GFileInfo * info,const char * attribute,gboolean attr_value)1302 g_file_info_set_attribute_boolean (GFileInfo *info,
1303 const char *attribute,
1304 gboolean attr_value)
1305 {
1306 g_return_if_fail (G_IS_FILE_INFO (info));
1307 g_return_if_fail (attribute != NULL && *attribute != '\0');
1308
1309 _g_file_info_set_attribute_boolean_by_id (info,
1310 lookup_attribute (attribute),
1311 attr_value);
1312 }
1313
1314 void
_g_file_info_set_attribute_uint32_by_id(GFileInfo * info,guint32 attribute,guint32 attr_value)1315 _g_file_info_set_attribute_uint32_by_id (GFileInfo *info,
1316 guint32 attribute,
1317 guint32 attr_value)
1318 {
1319 GFileAttributeValue *value;
1320
1321 value = g_file_info_create_value (info, attribute);
1322 if (value)
1323 _g_file_attribute_value_set_uint32 (value, attr_value);
1324 }
1325
1326 /**
1327 * g_file_info_set_attribute_uint32:
1328 * @info: a #GFileInfo.
1329 * @attribute: a file attribute key.
1330 * @attr_value: an unsigned 32-bit integer.
1331 *
1332 * Sets the @attribute to contain the given @attr_value,
1333 * if possible.
1334 **/
1335 void
g_file_info_set_attribute_uint32(GFileInfo * info,const char * attribute,guint32 attr_value)1336 g_file_info_set_attribute_uint32 (GFileInfo *info,
1337 const char *attribute,
1338 guint32 attr_value)
1339 {
1340 g_return_if_fail (G_IS_FILE_INFO (info));
1341 g_return_if_fail (attribute != NULL && *attribute != '\0');
1342
1343 _g_file_info_set_attribute_uint32_by_id (info,
1344 lookup_attribute (attribute),
1345 attr_value);
1346 }
1347
1348 void
_g_file_info_set_attribute_int32_by_id(GFileInfo * info,guint32 attribute,gint32 attr_value)1349 _g_file_info_set_attribute_int32_by_id (GFileInfo *info,
1350 guint32 attribute,
1351 gint32 attr_value)
1352 {
1353 GFileAttributeValue *value;
1354
1355 value = g_file_info_create_value (info, attribute);
1356 if (value)
1357 _g_file_attribute_value_set_int32 (value, attr_value);
1358 }
1359
1360 /**
1361 * g_file_info_set_attribute_int32:
1362 * @info: a #GFileInfo.
1363 * @attribute: a file attribute key.
1364 * @attr_value: a signed 32-bit integer
1365 *
1366 * Sets the @attribute to contain the given @attr_value,
1367 * if possible.
1368 **/
1369 void
g_file_info_set_attribute_int32(GFileInfo * info,const char * attribute,gint32 attr_value)1370 g_file_info_set_attribute_int32 (GFileInfo *info,
1371 const char *attribute,
1372 gint32 attr_value)
1373 {
1374 g_return_if_fail (G_IS_FILE_INFO (info));
1375 g_return_if_fail (attribute != NULL && *attribute != '\0');
1376
1377 _g_file_info_set_attribute_int32_by_id (info,
1378 lookup_attribute (attribute),
1379 attr_value);
1380 }
1381
1382 void
_g_file_info_set_attribute_uint64_by_id(GFileInfo * info,guint32 attribute,guint64 attr_value)1383 _g_file_info_set_attribute_uint64_by_id (GFileInfo *info,
1384 guint32 attribute,
1385 guint64 attr_value)
1386 {
1387 GFileAttributeValue *value;
1388
1389 value = g_file_info_create_value (info, attribute);
1390 if (value)
1391 _g_file_attribute_value_set_uint64 (value, attr_value);
1392 }
1393
1394 /**
1395 * g_file_info_set_attribute_uint64:
1396 * @info: a #GFileInfo.
1397 * @attribute: a file attribute key.
1398 * @attr_value: an unsigned 64-bit integer.
1399 *
1400 * Sets the @attribute to contain the given @attr_value,
1401 * if possible.
1402 **/
1403 void
g_file_info_set_attribute_uint64(GFileInfo * info,const char * attribute,guint64 attr_value)1404 g_file_info_set_attribute_uint64 (GFileInfo *info,
1405 const char *attribute,
1406 guint64 attr_value)
1407 {
1408 g_return_if_fail (G_IS_FILE_INFO (info));
1409 g_return_if_fail (attribute != NULL && *attribute != '\0');
1410
1411 _g_file_info_set_attribute_uint64_by_id (info,
1412 lookup_attribute (attribute),
1413 attr_value);
1414 }
1415
1416 void
_g_file_info_set_attribute_int64_by_id(GFileInfo * info,guint32 attribute,gint64 attr_value)1417 _g_file_info_set_attribute_int64_by_id (GFileInfo *info,
1418 guint32 attribute,
1419 gint64 attr_value)
1420 {
1421 GFileAttributeValue *value;
1422
1423 value = g_file_info_create_value (info, attribute);
1424 if (value)
1425 _g_file_attribute_value_set_int64 (value, attr_value);
1426 }
1427
1428 /**
1429 * g_file_info_set_attribute_int64:
1430 * @info: a #GFileInfo.
1431 * @attribute: attribute name to set.
1432 * @attr_value: int64 value to set attribute to.
1433 *
1434 * Sets the @attribute to contain the given @attr_value,
1435 * if possible.
1436 *
1437 **/
1438 void
g_file_info_set_attribute_int64(GFileInfo * info,const char * attribute,gint64 attr_value)1439 g_file_info_set_attribute_int64 (GFileInfo *info,
1440 const char *attribute,
1441 gint64 attr_value)
1442 {
1443 g_return_if_fail (G_IS_FILE_INFO (info));
1444 g_return_if_fail (attribute != NULL && *attribute != '\0');
1445
1446 _g_file_info_set_attribute_int64_by_id (info,
1447 lookup_attribute (attribute),
1448 attr_value);
1449 }
1450
1451 /* Helper getters */
1452 /**
1453 * g_file_info_get_deletion_date:
1454 * @info: a #GFileInfo.
1455 *
1456 * Returns the #GDateTime representing the deletion date of the file, as
1457 * available in G_FILE_ATTRIBUTE_TRASH_DELETION_DATE. If the
1458 * G_FILE_ATTRIBUTE_TRASH_DELETION_DATE attribute is unset, %NULL is returned.
1459 *
1460 * Returns: (nullable): a #GDateTime, or %NULL.
1461 *
1462 * Since: 2.36
1463 **/
1464 GDateTime *
g_file_info_get_deletion_date(GFileInfo * info)1465 g_file_info_get_deletion_date (GFileInfo *info)
1466 {
1467 static guint32 attr = 0;
1468 GFileAttributeValue *value;
1469 const char *date_str;
1470 GTimeZone *local_tz = NULL;
1471 GDateTime *dt = NULL;
1472
1473 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1474
1475 if (attr == 0)
1476 attr = lookup_attribute (G_FILE_ATTRIBUTE_TRASH_DELETION_DATE);
1477
1478 value = g_file_info_find_value (info, attr);
1479 date_str = _g_file_attribute_value_get_string (value);
1480 if (!date_str)
1481 return NULL;
1482
1483 local_tz = g_time_zone_new_local ();
1484 dt = g_date_time_new_from_iso8601 (date_str, local_tz);
1485 g_time_zone_unref (local_tz);
1486
1487 return g_steal_pointer (&dt);
1488 }
1489
1490 /**
1491 * g_file_info_get_file_type:
1492 * @info: a #GFileInfo.
1493 *
1494 * Gets a file's type (whether it is a regular file, symlink, etc).
1495 * This is different from the file's content type, see g_file_info_get_content_type().
1496 *
1497 * Returns: a #GFileType for the given file.
1498 **/
1499 GFileType
g_file_info_get_file_type(GFileInfo * info)1500 g_file_info_get_file_type (GFileInfo *info)
1501 {
1502 static guint32 attr = 0;
1503 GFileAttributeValue *value;
1504
1505 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1506
1507 if (attr == 0)
1508 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1509
1510 value = g_file_info_find_value (info, attr);
1511 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1512 }
1513
1514 /**
1515 * g_file_info_get_is_hidden:
1516 * @info: a #GFileInfo.
1517 *
1518 * Checks if a file is hidden.
1519 *
1520 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1521 **/
1522 gboolean
g_file_info_get_is_hidden(GFileInfo * info)1523 g_file_info_get_is_hidden (GFileInfo *info)
1524 {
1525 static guint32 attr = 0;
1526 GFileAttributeValue *value;
1527
1528 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1529
1530 if (attr == 0)
1531 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1532
1533 value = g_file_info_find_value (info, attr);
1534 return (GFileType)_g_file_attribute_value_get_boolean (value);
1535 }
1536
1537 /**
1538 * g_file_info_get_is_backup:
1539 * @info: a #GFileInfo.
1540 *
1541 * Checks if a file is a backup file.
1542 *
1543 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1544 **/
1545 gboolean
g_file_info_get_is_backup(GFileInfo * info)1546 g_file_info_get_is_backup (GFileInfo *info)
1547 {
1548 static guint32 attr = 0;
1549 GFileAttributeValue *value;
1550
1551 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1552
1553 if (attr == 0)
1554 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1555
1556 value = g_file_info_find_value (info, attr);
1557 return (GFileType)_g_file_attribute_value_get_boolean (value);
1558 }
1559
1560 /**
1561 * g_file_info_get_is_symlink:
1562 * @info: a #GFileInfo.
1563 *
1564 * Checks if a file is a symlink.
1565 *
1566 * Returns: %TRUE if the given @info is a symlink.
1567 **/
1568 gboolean
g_file_info_get_is_symlink(GFileInfo * info)1569 g_file_info_get_is_symlink (GFileInfo *info)
1570 {
1571 static guint32 attr = 0;
1572 GFileAttributeValue *value;
1573
1574 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1575
1576 if (attr == 0)
1577 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1578
1579 value = g_file_info_find_value (info, attr);
1580 return (GFileType)_g_file_attribute_value_get_boolean (value);
1581 }
1582
1583 /**
1584 * g_file_info_get_name:
1585 * @info: a #GFileInfo.
1586 *
1587 * Gets the name for a file. This is guaranteed to always be set.
1588 *
1589 * Returns: (type filename) (not nullable): a string containing the file name.
1590 **/
1591 const char *
g_file_info_get_name(GFileInfo * info)1592 g_file_info_get_name (GFileInfo *info)
1593 {
1594 static guint32 attr = 0;
1595 GFileAttributeValue *value;
1596
1597 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1598
1599 if (attr == 0)
1600 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1601
1602 value = g_file_info_find_value (info, attr);
1603 return _g_file_attribute_value_get_byte_string (value);
1604 }
1605
1606 /**
1607 * g_file_info_get_display_name:
1608 * @info: a #GFileInfo.
1609 *
1610 * Gets a display name for a file. This is guaranteed to always be set.
1611 *
1612 * Returns: (not nullable): a string containing the display name.
1613 **/
1614 const char *
g_file_info_get_display_name(GFileInfo * info)1615 g_file_info_get_display_name (GFileInfo *info)
1616 {
1617 static guint32 attr = 0;
1618 GFileAttributeValue *value;
1619
1620 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1621
1622 if (attr == 0)
1623 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1624
1625 value = g_file_info_find_value (info, attr);
1626 return _g_file_attribute_value_get_string (value);
1627 }
1628
1629 /**
1630 * g_file_info_get_edit_name:
1631 * @info: a #GFileInfo.
1632 *
1633 * Gets the edit name for a file.
1634 *
1635 * Returns: a string containing the edit name.
1636 **/
1637 const char *
g_file_info_get_edit_name(GFileInfo * info)1638 g_file_info_get_edit_name (GFileInfo *info)
1639 {
1640 static guint32 attr = 0;
1641 GFileAttributeValue *value;
1642
1643 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1644
1645 if (attr == 0)
1646 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1647
1648 value = g_file_info_find_value (info, attr);
1649 return _g_file_attribute_value_get_string (value);
1650 }
1651
1652 /**
1653 * g_file_info_get_icon:
1654 * @info: a #GFileInfo.
1655 *
1656 * Gets the icon for a file.
1657 *
1658 * Returns: (nullable) (transfer none): #GIcon for the given @info.
1659 **/
1660 GIcon *
g_file_info_get_icon(GFileInfo * info)1661 g_file_info_get_icon (GFileInfo *info)
1662 {
1663 static guint32 attr = 0;
1664 GFileAttributeValue *value;
1665 GObject *obj;
1666
1667 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1668
1669 if (attr == 0)
1670 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1671
1672 value = g_file_info_find_value (info, attr);
1673 obj = _g_file_attribute_value_get_object (value);
1674 if (G_IS_ICON (obj))
1675 return G_ICON (obj);
1676 return NULL;
1677 }
1678
1679 /**
1680 * g_file_info_get_symbolic_icon:
1681 * @info: a #GFileInfo.
1682 *
1683 * Gets the symbolic icon for a file.
1684 *
1685 * Returns: (nullable) (transfer none): #GIcon for the given @info.
1686 *
1687 * Since: 2.34
1688 **/
1689 GIcon *
g_file_info_get_symbolic_icon(GFileInfo * info)1690 g_file_info_get_symbolic_icon (GFileInfo *info)
1691 {
1692 static guint32 attr = 0;
1693 GFileAttributeValue *value;
1694 GObject *obj;
1695
1696 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1697
1698 if (attr == 0)
1699 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON);
1700
1701 value = g_file_info_find_value (info, attr);
1702 obj = _g_file_attribute_value_get_object (value);
1703 if (G_IS_ICON (obj))
1704 return G_ICON (obj);
1705 return NULL;
1706 }
1707
1708 /**
1709 * g_file_info_get_content_type:
1710 * @info: a #GFileInfo.
1711 *
1712 * Gets the file's content type.
1713 *
1714 * Returns: (nullable): a string containing the file's content type,
1715 * or %NULL if unknown.
1716 **/
1717 const char *
g_file_info_get_content_type(GFileInfo * info)1718 g_file_info_get_content_type (GFileInfo *info)
1719 {
1720 static guint32 attr = 0;
1721 GFileAttributeValue *value;
1722
1723 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1724
1725 if (attr == 0)
1726 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1727
1728 value = g_file_info_find_value (info, attr);
1729 return _g_file_attribute_value_get_string (value);
1730 }
1731
1732 /**
1733 * g_file_info_get_size:
1734 * @info: a #GFileInfo.
1735 *
1736 * Gets the file's size (in bytes). The size is retrieved through the value of
1737 * the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute and is converted
1738 * from #guint64 to #goffset before returning the result.
1739 *
1740 * Returns: a #goffset containing the file's size (in bytes).
1741 **/
1742 goffset
g_file_info_get_size(GFileInfo * info)1743 g_file_info_get_size (GFileInfo *info)
1744 {
1745 static guint32 attr = 0;
1746 GFileAttributeValue *value;
1747
1748 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1749
1750 if (attr == 0)
1751 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1752
1753 value = g_file_info_find_value (info, attr);
1754 return (goffset) _g_file_attribute_value_get_uint64 (value);
1755 }
1756
1757 /**
1758 * g_file_info_get_modification_time:
1759 * @info: a #GFileInfo.
1760 * @result: (out caller-allocates): a #GTimeVal.
1761 *
1762 * Gets the modification time of the current @info and sets it
1763 * in @result.
1764 *
1765 * Deprecated: 2.62: Use g_file_info_get_modification_date_time() instead, as
1766 * #GTimeVal is deprecated due to the year 2038 problem.
1767 **/
1768 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1769 void
g_file_info_get_modification_time(GFileInfo * info,GTimeVal * result)1770 g_file_info_get_modification_time (GFileInfo *info,
1771 GTimeVal *result)
1772 {
1773 static guint32 attr_mtime = 0, attr_mtime_usec;
1774 GFileAttributeValue *value;
1775
1776 g_return_if_fail (G_IS_FILE_INFO (info));
1777 g_return_if_fail (result != NULL);
1778
1779 if (attr_mtime == 0)
1780 {
1781 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1782 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1783 }
1784
1785 value = g_file_info_find_value (info, attr_mtime);
1786 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1787 value = g_file_info_find_value (info, attr_mtime_usec);
1788 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1789 }
1790 G_GNUC_END_IGNORE_DEPRECATIONS
1791
1792 /**
1793 * g_file_info_get_modification_date_time:
1794 * @info: a #GFileInfo.
1795 *
1796 * Gets the modification time of the current @info and returns it as a
1797 * #GDateTime.
1798 *
1799 * This requires the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute. If
1800 * %G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC is provided, the resulting #GDateTime
1801 * will have microsecond precision.
1802 *
1803 * Returns: (transfer full) (nullable): modification time, or %NULL if unknown
1804 * Since: 2.62
1805 */
1806 GDateTime *
g_file_info_get_modification_date_time(GFileInfo * info)1807 g_file_info_get_modification_date_time (GFileInfo *info)
1808 {
1809 static guint32 attr_mtime = 0, attr_mtime_usec;
1810 GFileAttributeValue *value, *value_usec;
1811 GDateTime *dt = NULL, *dt2 = NULL;
1812
1813 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1814
1815 if (attr_mtime == 0)
1816 {
1817 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1818 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1819 }
1820
1821 value = g_file_info_find_value (info, attr_mtime);
1822 if (value == NULL)
1823 return NULL;
1824
1825 dt = g_date_time_new_from_unix_utc (_g_file_attribute_value_get_uint64 (value));
1826
1827 value_usec = g_file_info_find_value (info, attr_mtime_usec);
1828 if (value_usec == NULL)
1829 return g_steal_pointer (&dt);
1830
1831 dt2 = g_date_time_add (dt, _g_file_attribute_value_get_uint32 (value_usec));
1832 g_date_time_unref (dt);
1833
1834 return g_steal_pointer (&dt2);
1835 }
1836
1837 /**
1838 * g_file_info_get_symlink_target:
1839 * @info: a #GFileInfo.
1840 *
1841 * Gets the symlink target for a given #GFileInfo.
1842 *
1843 * Returns: (nullable): a string containing the symlink target.
1844 **/
1845 const char *
g_file_info_get_symlink_target(GFileInfo * info)1846 g_file_info_get_symlink_target (GFileInfo *info)
1847 {
1848 static guint32 attr = 0;
1849 GFileAttributeValue *value;
1850
1851 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1852
1853 if (attr == 0)
1854 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1855
1856 value = g_file_info_find_value (info, attr);
1857 return _g_file_attribute_value_get_byte_string (value);
1858 }
1859
1860 /**
1861 * g_file_info_get_etag:
1862 * @info: a #GFileInfo.
1863 *
1864 * Gets the [entity tag][gfile-etag] for a given
1865 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1866 *
1867 * Returns: (nullable): a string containing the value of the "etag:value" attribute.
1868 **/
1869 const char *
g_file_info_get_etag(GFileInfo * info)1870 g_file_info_get_etag (GFileInfo *info)
1871 {
1872 static guint32 attr = 0;
1873 GFileAttributeValue *value;
1874
1875 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1876
1877 if (attr == 0)
1878 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1879
1880 value = g_file_info_find_value (info, attr);
1881 return _g_file_attribute_value_get_string (value);
1882 }
1883
1884 /**
1885 * g_file_info_get_sort_order:
1886 * @info: a #GFileInfo.
1887 *
1888 * Gets the value of the sort_order attribute from the #GFileInfo.
1889 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1890 *
1891 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1892 **/
1893 gint32
g_file_info_get_sort_order(GFileInfo * info)1894 g_file_info_get_sort_order (GFileInfo *info)
1895 {
1896 static guint32 attr = 0;
1897 GFileAttributeValue *value;
1898
1899 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1900
1901 if (attr == 0)
1902 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1903
1904 value = g_file_info_find_value (info, attr);
1905 return _g_file_attribute_value_get_int32 (value);
1906 }
1907
1908 /* Helper setters: */
1909 /**
1910 * g_file_info_set_file_type:
1911 * @info: a #GFileInfo.
1912 * @type: a #GFileType.
1913 *
1914 * Sets the file type in a #GFileInfo to @type.
1915 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1916 **/
1917 void
g_file_info_set_file_type(GFileInfo * info,GFileType type)1918 g_file_info_set_file_type (GFileInfo *info,
1919 GFileType type)
1920 {
1921 static guint32 attr = 0;
1922 GFileAttributeValue *value;
1923
1924 g_return_if_fail (G_IS_FILE_INFO (info));
1925
1926 if (attr == 0)
1927 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1928
1929 value = g_file_info_create_value (info, attr);
1930 if (value)
1931 _g_file_attribute_value_set_uint32 (value, type);
1932 }
1933
1934 /**
1935 * g_file_info_set_is_hidden:
1936 * @info: a #GFileInfo.
1937 * @is_hidden: a #gboolean.
1938 *
1939 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_hidden.
1940 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1941 **/
1942 void
g_file_info_set_is_hidden(GFileInfo * info,gboolean is_hidden)1943 g_file_info_set_is_hidden (GFileInfo *info,
1944 gboolean is_hidden)
1945 {
1946 static guint32 attr = 0;
1947 GFileAttributeValue *value;
1948
1949 g_return_if_fail (G_IS_FILE_INFO (info));
1950
1951 if (attr == 0)
1952 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1953
1954 value = g_file_info_create_value (info, attr);
1955 if (value)
1956 _g_file_attribute_value_set_boolean (value, is_hidden);
1957 }
1958
1959 /**
1960 * g_file_info_set_is_symlink:
1961 * @info: a #GFileInfo.
1962 * @is_symlink: a #gboolean.
1963 *
1964 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1965 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1966 **/
1967 void
g_file_info_set_is_symlink(GFileInfo * info,gboolean is_symlink)1968 g_file_info_set_is_symlink (GFileInfo *info,
1969 gboolean is_symlink)
1970 {
1971 static guint32 attr = 0;
1972 GFileAttributeValue *value;
1973
1974 g_return_if_fail (G_IS_FILE_INFO (info));
1975
1976 if (attr == 0)
1977 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1978
1979 value = g_file_info_create_value (info, attr);
1980 if (value)
1981 _g_file_attribute_value_set_boolean (value, is_symlink);
1982 }
1983
1984 /**
1985 * g_file_info_set_name:
1986 * @info: a #GFileInfo.
1987 * @name: (type filename): a string containing a name.
1988 *
1989 * Sets the name attribute for the current #GFileInfo.
1990 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1991 **/
1992 void
g_file_info_set_name(GFileInfo * info,const char * name)1993 g_file_info_set_name (GFileInfo *info,
1994 const char *name)
1995 {
1996 static guint32 attr = 0;
1997 GFileAttributeValue *value;
1998
1999 g_return_if_fail (G_IS_FILE_INFO (info));
2000 g_return_if_fail (name != NULL);
2001
2002 if (attr == 0)
2003 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
2004
2005 value = g_file_info_create_value (info, attr);
2006 if (value)
2007 _g_file_attribute_value_set_byte_string (value, name);
2008 }
2009
2010 /**
2011 * g_file_info_set_display_name:
2012 * @info: a #GFileInfo.
2013 * @display_name: a string containing a display name.
2014 *
2015 * Sets the display name for the current #GFileInfo.
2016 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
2017 **/
2018 void
g_file_info_set_display_name(GFileInfo * info,const char * display_name)2019 g_file_info_set_display_name (GFileInfo *info,
2020 const char *display_name)
2021 {
2022 static guint32 attr = 0;
2023 GFileAttributeValue *value;
2024
2025 g_return_if_fail (G_IS_FILE_INFO (info));
2026 g_return_if_fail (display_name != NULL);
2027
2028 if (attr == 0)
2029 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
2030
2031 value = g_file_info_create_value (info, attr);
2032 if (value)
2033 _g_file_attribute_value_set_string (value, display_name);
2034 }
2035
2036 /**
2037 * g_file_info_set_edit_name:
2038 * @info: a #GFileInfo.
2039 * @edit_name: a string containing an edit name.
2040 *
2041 * Sets the edit name for the current file.
2042 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
2043 **/
2044 void
g_file_info_set_edit_name(GFileInfo * info,const char * edit_name)2045 g_file_info_set_edit_name (GFileInfo *info,
2046 const char *edit_name)
2047 {
2048 static guint32 attr = 0;
2049 GFileAttributeValue *value;
2050
2051 g_return_if_fail (G_IS_FILE_INFO (info));
2052 g_return_if_fail (edit_name != NULL);
2053
2054 if (attr == 0)
2055 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
2056
2057 value = g_file_info_create_value (info, attr);
2058 if (value)
2059 _g_file_attribute_value_set_string (value, edit_name);
2060 }
2061
2062 /**
2063 * g_file_info_set_icon:
2064 * @info: a #GFileInfo.
2065 * @icon: a #GIcon.
2066 *
2067 * Sets the icon for a given #GFileInfo.
2068 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
2069 **/
2070 void
g_file_info_set_icon(GFileInfo * info,GIcon * icon)2071 g_file_info_set_icon (GFileInfo *info,
2072 GIcon *icon)
2073 {
2074 static guint32 attr = 0;
2075 GFileAttributeValue *value;
2076
2077 g_return_if_fail (G_IS_FILE_INFO (info));
2078 g_return_if_fail (G_IS_ICON (icon));
2079
2080 if (attr == 0)
2081 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
2082
2083 value = g_file_info_create_value (info, attr);
2084 if (value)
2085 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
2086 }
2087
2088 /**
2089 * g_file_info_set_symbolic_icon:
2090 * @info: a #GFileInfo.
2091 * @icon: a #GIcon.
2092 *
2093 * Sets the symbolic icon for a given #GFileInfo.
2094 * See %G_FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON.
2095 *
2096 * Since: 2.34
2097 **/
2098 void
g_file_info_set_symbolic_icon(GFileInfo * info,GIcon * icon)2099 g_file_info_set_symbolic_icon (GFileInfo *info,
2100 GIcon *icon)
2101 {
2102 static guint32 attr = 0;
2103 GFileAttributeValue *value;
2104
2105 g_return_if_fail (G_IS_FILE_INFO (info));
2106 g_return_if_fail (G_IS_ICON (icon));
2107
2108 if (attr == 0)
2109 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON);
2110
2111 value = g_file_info_create_value (info, attr);
2112 if (value)
2113 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
2114 }
2115
2116 /**
2117 * g_file_info_set_content_type:
2118 * @info: a #GFileInfo.
2119 * @content_type: a content type. See [GContentType][gio-GContentType]
2120 *
2121 * Sets the content type attribute for a given #GFileInfo.
2122 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
2123 **/
2124 void
g_file_info_set_content_type(GFileInfo * info,const char * content_type)2125 g_file_info_set_content_type (GFileInfo *info,
2126 const char *content_type)
2127 {
2128 static guint32 attr = 0;
2129 GFileAttributeValue *value;
2130
2131 g_return_if_fail (G_IS_FILE_INFO (info));
2132 g_return_if_fail (content_type != NULL);
2133
2134 if (attr == 0)
2135 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
2136
2137 value = g_file_info_create_value (info, attr);
2138 if (value)
2139 _g_file_attribute_value_set_string (value, content_type);
2140 }
2141
2142 /**
2143 * g_file_info_set_size:
2144 * @info: a #GFileInfo.
2145 * @size: a #goffset containing the file's size.
2146 *
2147 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
2148 * to the given size.
2149 **/
2150 void
g_file_info_set_size(GFileInfo * info,goffset size)2151 g_file_info_set_size (GFileInfo *info,
2152 goffset size)
2153 {
2154 static guint32 attr = 0;
2155 GFileAttributeValue *value;
2156
2157 g_return_if_fail (G_IS_FILE_INFO (info));
2158
2159 if (attr == 0)
2160 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
2161
2162 value = g_file_info_create_value (info, attr);
2163 if (value)
2164 _g_file_attribute_value_set_uint64 (value, size);
2165 }
2166
2167 /**
2168 * g_file_info_set_modification_time:
2169 * @info: a #GFileInfo.
2170 * @mtime: a #GTimeVal.
2171 *
2172 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED and
2173 * %G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC attributes in the file info to the
2174 * given time value.
2175 *
2176 * Deprecated: 2.62: Use g_file_info_set_modification_date_time() instead, as
2177 * #GTimeVal is deprecated due to the year 2038 problem.
2178 **/
2179 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2180 void
g_file_info_set_modification_time(GFileInfo * info,GTimeVal * mtime)2181 g_file_info_set_modification_time (GFileInfo *info,
2182 GTimeVal *mtime)
2183 {
2184 static guint32 attr_mtime = 0, attr_mtime_usec;
2185 GFileAttributeValue *value;
2186
2187 g_return_if_fail (G_IS_FILE_INFO (info));
2188 g_return_if_fail (mtime != NULL);
2189
2190 if (attr_mtime == 0)
2191 {
2192 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
2193 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2194 }
2195
2196 value = g_file_info_create_value (info, attr_mtime);
2197 if (value)
2198 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
2199 value = g_file_info_create_value (info, attr_mtime_usec);
2200 if (value)
2201 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
2202 }
2203 G_GNUC_END_IGNORE_DEPRECATIONS
2204
2205 /**
2206 * g_file_info_set_modification_date_time:
2207 * @info: a #GFileInfo.
2208 * @mtime: (not nullable): a #GDateTime.
2209 *
2210 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED and
2211 * %G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC attributes in the file info to the
2212 * given date/time value.
2213 *
2214 * Since: 2.62
2215 */
2216 void
g_file_info_set_modification_date_time(GFileInfo * info,GDateTime * mtime)2217 g_file_info_set_modification_date_time (GFileInfo *info,
2218 GDateTime *mtime)
2219 {
2220 static guint32 attr_mtime = 0, attr_mtime_usec;
2221 GFileAttributeValue *value;
2222
2223 g_return_if_fail (G_IS_FILE_INFO (info));
2224 g_return_if_fail (mtime != NULL);
2225
2226 if (attr_mtime == 0)
2227 {
2228 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
2229 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2230 }
2231
2232 value = g_file_info_create_value (info, attr_mtime);
2233 if (value)
2234 _g_file_attribute_value_set_uint64 (value, g_date_time_to_unix (mtime));
2235 value = g_file_info_create_value (info, attr_mtime_usec);
2236 if (value)
2237 _g_file_attribute_value_set_uint32 (value, g_date_time_get_microsecond (mtime));
2238 }
2239
2240 /**
2241 * g_file_info_set_symlink_target:
2242 * @info: a #GFileInfo.
2243 * @symlink_target: a static string containing a path to a symlink target.
2244 *
2245 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
2246 * to the given symlink target.
2247 **/
2248 void
g_file_info_set_symlink_target(GFileInfo * info,const char * symlink_target)2249 g_file_info_set_symlink_target (GFileInfo *info,
2250 const char *symlink_target)
2251 {
2252 static guint32 attr = 0;
2253 GFileAttributeValue *value;
2254
2255 g_return_if_fail (G_IS_FILE_INFO (info));
2256 g_return_if_fail (symlink_target != NULL);
2257
2258 if (attr == 0)
2259 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2260
2261 value = g_file_info_create_value (info, attr);
2262 if (value)
2263 _g_file_attribute_value_set_byte_string (value, symlink_target);
2264 }
2265
2266 /**
2267 * g_file_info_set_sort_order:
2268 * @info: a #GFileInfo.
2269 * @sort_order: a sort order integer.
2270 *
2271 * Sets the sort order attribute in the file info structure. See
2272 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
2273 **/
2274 void
g_file_info_set_sort_order(GFileInfo * info,gint32 sort_order)2275 g_file_info_set_sort_order (GFileInfo *info,
2276 gint32 sort_order)
2277 {
2278 static guint32 attr = 0;
2279 GFileAttributeValue *value;
2280
2281 g_return_if_fail (G_IS_FILE_INFO (info));
2282
2283 if (attr == 0)
2284 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
2285
2286 value = g_file_info_create_value (info, attr);
2287 if (value)
2288 _g_file_attribute_value_set_int32 (value, sort_order);
2289 }
2290
2291
2292 typedef struct {
2293 guint32 id;
2294 guint32 mask;
2295 } SubMatcher;
2296
2297 struct _GFileAttributeMatcher {
2298 gboolean all;
2299 gint ref;
2300
2301 GArray *sub_matchers;
2302
2303 /* Iterator */
2304 guint32 iterator_ns;
2305 gint iterator_pos;
2306 };
2307
G_DEFINE_BOXED_TYPE(GFileAttributeMatcher,g_file_attribute_matcher,g_file_attribute_matcher_ref,g_file_attribute_matcher_unref)2308 G_DEFINE_BOXED_TYPE (GFileAttributeMatcher, g_file_attribute_matcher,
2309 g_file_attribute_matcher_ref,
2310 g_file_attribute_matcher_unref)
2311
2312 static gint
2313 compare_sub_matchers (gconstpointer a,
2314 gconstpointer b)
2315 {
2316 const SubMatcher *suba = a;
2317 const SubMatcher *subb = b;
2318 int diff;
2319
2320 diff = suba->id - subb->id;
2321
2322 if (diff)
2323 return diff;
2324
2325 return suba->mask - subb->mask;
2326 }
2327
2328 static gboolean
sub_matcher_matches(SubMatcher * matcher,SubMatcher * submatcher)2329 sub_matcher_matches (SubMatcher *matcher,
2330 SubMatcher *submatcher)
2331 {
2332 if ((matcher->mask & submatcher->mask) != matcher->mask)
2333 return FALSE;
2334
2335 return matcher->id == (submatcher->id & matcher->mask);
2336 }
2337
2338 /* Call this function after modifying a matcher.
2339 * It will ensure all the invariants other functions rely on.
2340 */
2341 static GFileAttributeMatcher *
matcher_optimize(GFileAttributeMatcher * matcher)2342 matcher_optimize (GFileAttributeMatcher *matcher)
2343 {
2344 SubMatcher *submatcher, *compare;
2345 guint i, j;
2346
2347 /* remove sub_matchers if we match everything anyway */
2348 if (matcher->all)
2349 {
2350 if (matcher->sub_matchers)
2351 {
2352 g_array_free (matcher->sub_matchers, TRUE);
2353 matcher->sub_matchers = NULL;
2354 }
2355 return matcher;
2356 }
2357
2358 if (matcher->sub_matchers->len == 0)
2359 {
2360 g_file_attribute_matcher_unref (matcher);
2361 return NULL;
2362 }
2363
2364 /* sort sub_matchers by id (and then mask), so we can bsearch
2365 * and compare matchers in O(N) instead of O(N²) */
2366 g_array_sort (matcher->sub_matchers, compare_sub_matchers);
2367
2368 /* remove duplicates and specific matches when we match the whole namespace */
2369 j = 0;
2370 compare = &g_array_index (matcher->sub_matchers, SubMatcher, j);
2371
2372 for (i = 1; i < matcher->sub_matchers->len; i++)
2373 {
2374 submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2375 if (sub_matcher_matches (compare, submatcher))
2376 continue;
2377
2378 j++;
2379 compare++;
2380
2381 if (j < i)
2382 *compare = *submatcher;
2383 }
2384
2385 g_array_set_size (matcher->sub_matchers, j + 1);
2386
2387 return matcher;
2388 }
2389
2390 /**
2391 * g_file_attribute_matcher_new:
2392 * @attributes: an attribute string to match.
2393 *
2394 * Creates a new file attribute matcher, which matches attributes
2395 * against a given string. #GFileAttributeMatchers are reference
2396 * counted structures, and are created with a reference count of 1. If
2397 * the number of references falls to 0, the #GFileAttributeMatcher is
2398 * automatically destroyed.
2399 *
2400 * The @attributes string should be formatted with specific keys separated
2401 * from namespaces with a double colon. Several "namespace::key" strings may be
2402 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
2403 * The wildcard "*" may be used to match all keys and namespaces, or
2404 * "namespace::*" will match all keys in a given namespace.
2405 *
2406 * ## Examples of file attribute matcher strings and results
2407 *
2408 * - `"*"`: matches all attributes.
2409 * - `"standard::is-hidden"`: matches only the key is-hidden in the
2410 * standard namespace.
2411 * - `"standard::type,unix::*"`: matches the type key in the standard
2412 * namespace and all keys in the unix namespace.
2413 *
2414 * Returns: a #GFileAttributeMatcher
2415 */
2416 GFileAttributeMatcher *
g_file_attribute_matcher_new(const char * attributes)2417 g_file_attribute_matcher_new (const char *attributes)
2418 {
2419 char **split;
2420 char *colon;
2421 int i;
2422 GFileAttributeMatcher *matcher;
2423
2424 if (attributes == NULL || *attributes == '\0')
2425 return NULL;
2426
2427 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2428 matcher->ref = 1;
2429 matcher->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2430
2431 split = g_strsplit (attributes, ",", -1);
2432
2433 for (i = 0; split[i] != NULL; i++)
2434 {
2435 if (strcmp (split[i], "*") == 0)
2436 matcher->all = TRUE;
2437 else
2438 {
2439 SubMatcher s;
2440
2441 colon = strstr (split[i], "::");
2442 if (colon != NULL &&
2443 !(colon[2] == 0 ||
2444 (colon[2] == '*' &&
2445 colon[3] == 0)))
2446 {
2447 s.id = lookup_attribute (split[i]);
2448 s.mask = 0xffffffff;
2449 }
2450 else
2451 {
2452 if (colon)
2453 *colon = 0;
2454
2455 s.id = lookup_namespace (split[i]) << NS_POS;
2456 s.mask = NS_MASK << NS_POS;
2457 }
2458
2459 g_array_append_val (matcher->sub_matchers, s);
2460 }
2461 }
2462
2463 g_strfreev (split);
2464
2465 matcher = matcher_optimize (matcher);
2466
2467 return matcher;
2468 }
2469
2470 /**
2471 * g_file_attribute_matcher_subtract:
2472 * @matcher: (nullable): Matcher to subtract from
2473 * @subtract: (nullable): The matcher to subtract
2474 *
2475 * Subtracts all attributes of @subtract from @matcher and returns
2476 * a matcher that supports those attributes.
2477 *
2478 * Note that currently it is not possible to remove a single
2479 * attribute when the @matcher matches the whole namespace - or remove
2480 * a namespace or attribute when the matcher matches everything. This
2481 * is a limitation of the current implementation, but may be fixed
2482 * in the future.
2483 *
2484 * Returns: (nullable): A file attribute matcher matching all attributes of
2485 * @matcher that are not matched by @subtract
2486 **/
2487 GFileAttributeMatcher *
g_file_attribute_matcher_subtract(GFileAttributeMatcher * matcher,GFileAttributeMatcher * subtract)2488 g_file_attribute_matcher_subtract (GFileAttributeMatcher *matcher,
2489 GFileAttributeMatcher *subtract)
2490 {
2491 GFileAttributeMatcher *result;
2492 guint mi, si;
2493 SubMatcher *msub, *ssub;
2494
2495 if (matcher == NULL)
2496 return NULL;
2497 if (subtract == NULL)
2498 return g_file_attribute_matcher_ref (matcher);
2499 if (subtract->all)
2500 return NULL;
2501 if (matcher->all)
2502 return g_file_attribute_matcher_ref (matcher);
2503
2504 result = g_malloc0 (sizeof (GFileAttributeMatcher));
2505 result->ref = 1;
2506 result->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2507
2508 si = 0;
2509 g_assert (subtract->sub_matchers->len > 0);
2510 ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);
2511
2512 for (mi = 0; mi < matcher->sub_matchers->len; mi++)
2513 {
2514 msub = &g_array_index (matcher->sub_matchers, SubMatcher, mi);
2515
2516 retry:
2517 if (sub_matcher_matches (ssub, msub))
2518 continue;
2519
2520 si++;
2521 if (si >= subtract->sub_matchers->len)
2522 break;
2523
2524 ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);
2525 if (ssub->id <= msub->id)
2526 goto retry;
2527
2528 g_array_append_val (result->sub_matchers, *msub);
2529 }
2530
2531 if (mi < matcher->sub_matchers->len)
2532 g_array_append_vals (result->sub_matchers,
2533 &g_array_index (matcher->sub_matchers, SubMatcher, mi),
2534 matcher->sub_matchers->len - mi);
2535
2536 result = matcher_optimize (result);
2537
2538 return result;
2539 }
2540
2541 /**
2542 * g_file_attribute_matcher_ref:
2543 * @matcher: a #GFileAttributeMatcher.
2544 *
2545 * References a file attribute matcher.
2546 *
2547 * Returns: a #GFileAttributeMatcher.
2548 **/
2549 GFileAttributeMatcher *
g_file_attribute_matcher_ref(GFileAttributeMatcher * matcher)2550 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2551 {
2552 if (matcher)
2553 {
2554 g_return_val_if_fail (matcher->ref > 0, NULL);
2555 g_atomic_int_inc (&matcher->ref);
2556 }
2557 return matcher;
2558 }
2559
2560 /**
2561 * g_file_attribute_matcher_unref:
2562 * @matcher: a #GFileAttributeMatcher.
2563 *
2564 * Unreferences @matcher. If the reference count falls below 1,
2565 * the @matcher is automatically freed.
2566 *
2567 **/
2568 void
g_file_attribute_matcher_unref(GFileAttributeMatcher * matcher)2569 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2570 {
2571 if (matcher)
2572 {
2573 g_return_if_fail (matcher->ref > 0);
2574
2575 if (g_atomic_int_dec_and_test (&matcher->ref))
2576 {
2577 if (matcher->sub_matchers)
2578 g_array_free (matcher->sub_matchers, TRUE);
2579
2580 g_free (matcher);
2581 }
2582 }
2583 }
2584
2585 /**
2586 * g_file_attribute_matcher_matches_only:
2587 * @matcher: a #GFileAttributeMatcher.
2588 * @attribute: a file attribute key.
2589 *
2590 * Checks if a attribute matcher only matches a given attribute. Always
2591 * returns %FALSE if "*" was used when creating the matcher.
2592 *
2593 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2594 **/
2595 gboolean
g_file_attribute_matcher_matches_only(GFileAttributeMatcher * matcher,const char * attribute)2596 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2597 const char *attribute)
2598 {
2599 SubMatcher *sub_matcher;
2600 guint32 id;
2601
2602 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2603
2604 if (matcher == NULL ||
2605 matcher->all)
2606 return FALSE;
2607
2608 if (matcher->sub_matchers->len != 1)
2609 return FALSE;
2610
2611 id = lookup_attribute (attribute);
2612
2613 sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, 0);
2614
2615 return sub_matcher->id == id &&
2616 sub_matcher->mask == 0xffffffff;
2617 }
2618
2619 static gboolean
matcher_matches_id(GFileAttributeMatcher * matcher,guint32 id)2620 matcher_matches_id (GFileAttributeMatcher *matcher,
2621 guint32 id)
2622 {
2623 SubMatcher *sub_matchers;
2624 guint i;
2625
2626 if (matcher->sub_matchers)
2627 {
2628 sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
2629 for (i = 0; i < matcher->sub_matchers->len; i++)
2630 {
2631 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2632 return TRUE;
2633 }
2634 }
2635
2636 return FALSE;
2637 }
2638
2639 gboolean
_g_file_attribute_matcher_matches_id(GFileAttributeMatcher * matcher,guint32 id)2640 _g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2641 guint32 id)
2642 {
2643 /* We return a NULL matcher for an empty match string, so handle this */
2644 if (matcher == NULL)
2645 return FALSE;
2646
2647 if (matcher->all)
2648 return TRUE;
2649
2650 return matcher_matches_id (matcher, id);
2651 }
2652
2653 /**
2654 * g_file_attribute_matcher_matches:
2655 * @matcher: a #GFileAttributeMatcher.
2656 * @attribute: a file attribute key.
2657 *
2658 * Checks if an attribute will be matched by an attribute matcher. If
2659 * the matcher was created with the "*" matching string, this function
2660 * will always return %TRUE.
2661 *
2662 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2663 **/
2664 gboolean
g_file_attribute_matcher_matches(GFileAttributeMatcher * matcher,const char * attribute)2665 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2666 const char *attribute)
2667 {
2668 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2669
2670 /* We return a NULL matcher for an empty match string, so handle this */
2671 if (matcher == NULL)
2672 return FALSE;
2673
2674 if (matcher->all)
2675 return TRUE;
2676
2677 return matcher_matches_id (matcher, lookup_attribute (attribute));
2678 }
2679
2680 /* return TRUE -> all */
2681 /**
2682 * g_file_attribute_matcher_enumerate_namespace:
2683 * @matcher: a #GFileAttributeMatcher.
2684 * @ns: a string containing a file attribute namespace.
2685 *
2686 * Checks if the matcher will match all of the keys in a given namespace.
2687 * This will always return %TRUE if a wildcard character is in use (e.g. if
2688 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2689 * using "*" and namespace is anything.)
2690 *
2691 * TODO: this is awkwardly worded.
2692 *
2693 * Returns: %TRUE if the matcher matches all of the entries
2694 * in the given @ns, %FALSE otherwise.
2695 **/
2696 gboolean
g_file_attribute_matcher_enumerate_namespace(GFileAttributeMatcher * matcher,const char * ns)2697 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2698 const char *ns)
2699 {
2700 SubMatcher *sub_matchers;
2701 guint ns_id;
2702 guint i;
2703
2704 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2705
2706 /* We return a NULL matcher for an empty match string, so handle this */
2707 if (matcher == NULL)
2708 return FALSE;
2709
2710 if (matcher->all)
2711 return TRUE;
2712
2713 ns_id = lookup_namespace (ns) << NS_POS;
2714
2715 if (matcher->sub_matchers)
2716 {
2717 sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
2718 for (i = 0; i < matcher->sub_matchers->len; i++)
2719 {
2720 if (sub_matchers[i].id == ns_id)
2721 return TRUE;
2722 }
2723 }
2724
2725 matcher->iterator_ns = ns_id;
2726 matcher->iterator_pos = 0;
2727
2728 return FALSE;
2729 }
2730
2731 /**
2732 * g_file_attribute_matcher_enumerate_next:
2733 * @matcher: a #GFileAttributeMatcher.
2734 *
2735 * Gets the next matched attribute from a #GFileAttributeMatcher.
2736 *
2737 * Returns: (nullable): a string containing the next attribute or, %NULL if
2738 * no more attribute exist.
2739 **/
2740 const char *
g_file_attribute_matcher_enumerate_next(GFileAttributeMatcher * matcher)2741 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2742 {
2743 guint i;
2744 SubMatcher *sub_matcher;
2745
2746 /* We return a NULL matcher for an empty match string, so handle this */
2747 if (matcher == NULL)
2748 return NULL;
2749
2750 while (1)
2751 {
2752 i = matcher->iterator_pos++;
2753
2754 if (matcher->sub_matchers == NULL)
2755 return NULL;
2756
2757 if (i < matcher->sub_matchers->len)
2758 sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2759 else
2760 return NULL;
2761
2762 if (sub_matcher->mask == 0xffffffff &&
2763 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2764 return get_attribute_for_id (sub_matcher->id);
2765 }
2766 }
2767
2768 /**
2769 * g_file_attribute_matcher_to_string:
2770 * @matcher: (nullable): a #GFileAttributeMatcher.
2771 *
2772 * Prints what the matcher is matching against. The format will be
2773 * equal to the format passed to g_file_attribute_matcher_new().
2774 * The output however, might not be identical, as the matcher may
2775 * decide to use a different order or omit needless parts.
2776 *
2777 * Returns: a string describing the attributes the matcher matches
2778 * against or %NULL if @matcher was %NULL.
2779 *
2780 * Since: 2.32
2781 **/
2782 char *
g_file_attribute_matcher_to_string(GFileAttributeMatcher * matcher)2783 g_file_attribute_matcher_to_string (GFileAttributeMatcher *matcher)
2784 {
2785 GString *string;
2786 guint i;
2787
2788 if (matcher == NULL)
2789 return NULL;
2790
2791 if (matcher->all)
2792 return g_strdup ("*");
2793
2794 string = g_string_new ("");
2795 for (i = 0; i < matcher->sub_matchers->len; i++)
2796 {
2797 SubMatcher *submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2798
2799 if (i > 0)
2800 g_string_append_c (string, ',');
2801
2802 g_string_append (string, get_attribute_for_id (submatcher->id));
2803 }
2804
2805 return g_string_free (string, FALSE);
2806 }
2807