gdbus tests: remove buggy use of GMainLoop
[glib.git] / gio / gfileinfo.c
blob87767bc0d1c8bf5ce1c636123f8d568b95273ef9
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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 of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 /**
24 * SECTION:gfileinfo
25 * @short_description: File Information and Attributes
26 * @include: gio/gio.h
27 * @see_also: #GFile, <link linkend="gio-GFileAttribute">GFileAttribute</link>
29 * Functionality for manipulating basic metadata for files. #GFileInfo
30 * implements methods for getting information that all files should
31 * contain, and allows for manipulation of extended attributes.
33 * See <link linkend="gio-GFileAttribute">GFileAttribute</link> for more
34 * information on how GIO handles file attributes.
36 * To obtain a #GFileInfo for a #GFile, use g_file_query_info() (or its
37 * async variant). To obtain a #GFileInfo for a file input or output
38 * stream, use g_file_input_stream_query_info() or
39 * g_file_output_stream_query_info() (or their async variants).
41 * To change the actual attributes of a file, you should then set the
42 * attribute in the #GFileInfo and call g_file_set_attributes_from_info()
43 * or g_file_set_attributes_async() on a GFile.
45 * However, not all attributes can be changed in the file. For instance,
46 * the actual size of a file cannot be changed via g_file_info_set_size().
47 * You may call g_file_query_settable_attributes() and
48 * g_file_query_writable_namespaces() to discover the settable attributes
49 * of a particular file at runtime.
51 * #GFileAttributeMatcher allows for searching through a #GFileInfo for
52 * attributes.
53 **/
55 #include "config.h"
57 #include <string.h>
59 #include "gfileinfo.h"
60 #include "gfileinfo-priv.h"
61 #include "gfileattribute-priv.h"
62 #include "gicon.h"
63 #include "glibintl.h"
66 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
67 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
69 typedef struct {
70 guint32 attribute;
71 GFileAttributeValue value;
72 } GFileAttribute;
74 struct _GFileInfo
76 GObject parent_instance;
78 GArray *attributes;
79 GFileAttributeMatcher *mask;
82 struct _GFileInfoClass
84 GObjectClass parent_class;
88 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
90 typedef struct {
91 guint32 id;
92 guint32 attribute_id_counter;
93 } NSInfo;
95 G_LOCK_DEFINE_STATIC (attribute_hash);
96 static int namespace_id_counter = 0;
97 static GHashTable *ns_hash = NULL;
98 static GHashTable *attribute_hash = NULL;
99 static char ***attributes = NULL;
101 /* Attribute ids are 32bit, we split it up like this:
102 * |------------|--------------------|
103 * 12 bit 20 bit
104 * namespace attribute id
106 * This way the attributes gets sorted in namespace order
109 #define NS_POS 20
110 #define NS_MASK ((guint32)((1<<12) - 1))
111 #define ID_POS 0
112 #define ID_MASK ((guint32)((1<<20) - 1))
114 #define GET_NS(_attr_id) \
115 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
116 #define GET_ID(_attr_id) \
117 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
119 #define MAKE_ATTR_ID(_ns, _id) \
120 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
121 ((((guint32) _id) & ID_MASK) << ID_POS) )
123 static NSInfo *
124 _lookup_namespace (const char *namespace)
126 NSInfo *ns_info;
128 ns_info = g_hash_table_lookup (ns_hash, namespace);
129 if (ns_info == NULL)
131 ns_info = g_new0 (NSInfo, 1);
132 ns_info->id = ++namespace_id_counter;
133 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
134 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
135 attributes[ns_info->id] = g_new (char *, 1);
136 attributes[ns_info->id][0] = g_strconcat (namespace, "::*", NULL);
138 return ns_info;
141 static guint32
142 _lookup_attribute (const char *attribute)
144 guint32 attr_id, id;
145 char *ns;
146 const char *colon;
147 NSInfo *ns_info;
149 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
151 if (attr_id != 0)
152 return attr_id;
154 colon = strstr (attribute, "::");
155 if (colon)
156 ns = g_strndup (attribute, colon - attribute);
157 else
158 ns = g_strdup ("");
160 ns_info = _lookup_namespace (ns);
161 g_free (ns);
163 id = ++ns_info->attribute_id_counter;
164 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
165 attributes[ns_info->id][id] = g_strdup (attribute);
167 attr_id = MAKE_ATTR_ID (ns_info->id, id);
169 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
171 return attr_id;
174 static void
175 ensure_attribute_hash (void)
177 if (attribute_hash != NULL)
178 return;
180 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
181 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
183 #define REGISTER_ATTRIBUTE(name) G_STMT_START{\
184 guint _u = _lookup_attribute (G_FILE_ATTRIBUTE_ ## name); \
185 /* use for generating the ID: g_print ("#define G_FILE_ATTRIBUTE_ID_%s (%u + %u)\n", #name + 17, _u & ~ID_MASK, _u & ID_MASK); */ \
186 g_assert (_u == G_FILE_ATTRIBUTE_ID_ ## name); \
187 }G_STMT_END
189 REGISTER_ATTRIBUTE (STANDARD_TYPE);
190 REGISTER_ATTRIBUTE (STANDARD_IS_HIDDEN);
191 REGISTER_ATTRIBUTE (STANDARD_IS_BACKUP);
192 REGISTER_ATTRIBUTE (STANDARD_IS_SYMLINK);
193 REGISTER_ATTRIBUTE (STANDARD_IS_VIRTUAL);
194 REGISTER_ATTRIBUTE (STANDARD_NAME);
195 REGISTER_ATTRIBUTE (STANDARD_DISPLAY_NAME);
196 REGISTER_ATTRIBUTE (STANDARD_EDIT_NAME);
197 REGISTER_ATTRIBUTE (STANDARD_COPY_NAME);
198 REGISTER_ATTRIBUTE (STANDARD_DESCRIPTION);
199 REGISTER_ATTRIBUTE (STANDARD_ICON);
200 REGISTER_ATTRIBUTE (STANDARD_CONTENT_TYPE);
201 REGISTER_ATTRIBUTE (STANDARD_FAST_CONTENT_TYPE);
202 REGISTER_ATTRIBUTE (STANDARD_SIZE);
203 REGISTER_ATTRIBUTE (STANDARD_ALLOCATED_SIZE);
204 REGISTER_ATTRIBUTE (STANDARD_SYMLINK_TARGET);
205 REGISTER_ATTRIBUTE (STANDARD_TARGET_URI);
206 REGISTER_ATTRIBUTE (STANDARD_SORT_ORDER);
207 REGISTER_ATTRIBUTE (ETAG_VALUE);
208 REGISTER_ATTRIBUTE (ID_FILE);
209 REGISTER_ATTRIBUTE (ID_FILESYSTEM);
210 REGISTER_ATTRIBUTE (ACCESS_CAN_READ);
211 REGISTER_ATTRIBUTE (ACCESS_CAN_WRITE);
212 REGISTER_ATTRIBUTE (ACCESS_CAN_EXECUTE);
213 REGISTER_ATTRIBUTE (ACCESS_CAN_DELETE);
214 REGISTER_ATTRIBUTE (ACCESS_CAN_TRASH);
215 REGISTER_ATTRIBUTE (ACCESS_CAN_RENAME);
216 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_MOUNT);
217 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_UNMOUNT);
218 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_EJECT);
219 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE);
220 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE_FILE);
221 REGISTER_ATTRIBUTE (MOUNTABLE_HAL_UDI);
222 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START);
223 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START_DEGRADED);
224 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_STOP);
225 REGISTER_ATTRIBUTE (MOUNTABLE_START_STOP_TYPE);
226 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_POLL);
227 REGISTER_ATTRIBUTE (MOUNTABLE_IS_MEDIA_CHECK_AUTOMATIC);
228 REGISTER_ATTRIBUTE (TIME_MODIFIED);
229 REGISTER_ATTRIBUTE (TIME_MODIFIED_USEC);
230 REGISTER_ATTRIBUTE (TIME_ACCESS);
231 REGISTER_ATTRIBUTE (TIME_ACCESS_USEC);
232 REGISTER_ATTRIBUTE (TIME_CHANGED);
233 REGISTER_ATTRIBUTE (TIME_CHANGED_USEC);
234 REGISTER_ATTRIBUTE (TIME_CREATED);
235 REGISTER_ATTRIBUTE (TIME_CREATED_USEC);
236 REGISTER_ATTRIBUTE (UNIX_DEVICE);
237 REGISTER_ATTRIBUTE (UNIX_INODE);
238 REGISTER_ATTRIBUTE (UNIX_MODE);
239 REGISTER_ATTRIBUTE (UNIX_NLINK);
240 REGISTER_ATTRIBUTE (UNIX_UID);
241 REGISTER_ATTRIBUTE (UNIX_GID);
242 REGISTER_ATTRIBUTE (UNIX_RDEV);
243 REGISTER_ATTRIBUTE (UNIX_BLOCK_SIZE);
244 REGISTER_ATTRIBUTE (UNIX_BLOCKS);
245 REGISTER_ATTRIBUTE (UNIX_IS_MOUNTPOINT);
246 REGISTER_ATTRIBUTE (DOS_IS_ARCHIVE);
247 REGISTER_ATTRIBUTE (DOS_IS_SYSTEM);
248 REGISTER_ATTRIBUTE (OWNER_USER);
249 REGISTER_ATTRIBUTE (OWNER_USER_REAL);
250 REGISTER_ATTRIBUTE (OWNER_GROUP);
251 REGISTER_ATTRIBUTE (THUMBNAIL_PATH);
252 REGISTER_ATTRIBUTE (THUMBNAILING_FAILED);
253 REGISTER_ATTRIBUTE (PREVIEW_ICON);
254 REGISTER_ATTRIBUTE (FILESYSTEM_SIZE);
255 REGISTER_ATTRIBUTE (FILESYSTEM_FREE);
256 REGISTER_ATTRIBUTE (FILESYSTEM_TYPE);
257 REGISTER_ATTRIBUTE (FILESYSTEM_READONLY);
258 REGISTER_ATTRIBUTE (FILESYSTEM_USE_PREVIEW);
259 REGISTER_ATTRIBUTE (GVFS_BACKEND);
260 REGISTER_ATTRIBUTE (SELINUX_CONTEXT);
261 REGISTER_ATTRIBUTE (TRASH_ITEM_COUNT);
262 REGISTER_ATTRIBUTE (TRASH_ORIG_PATH);
263 REGISTER_ATTRIBUTE (TRASH_DELETION_DATE);
265 #undef REGISTER_ATTRIBUTE
268 static guint32
269 lookup_namespace (const char *namespace)
271 NSInfo *ns_info;
272 guint32 id;
274 G_LOCK (attribute_hash);
276 ensure_attribute_hash ();
278 ns_info = _lookup_namespace (namespace);
279 id = 0;
280 if (ns_info)
281 id = ns_info->id;
283 G_UNLOCK (attribute_hash);
285 return id;
288 static char *
289 get_attribute_for_id (int attribute)
291 char *s;
292 G_LOCK (attribute_hash);
293 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
294 G_UNLOCK (attribute_hash);
295 return s;
298 static guint32
299 lookup_attribute (const char *attribute)
301 guint32 attr_id;
303 G_LOCK (attribute_hash);
304 ensure_attribute_hash ();
306 attr_id = _lookup_attribute (attribute);
308 G_UNLOCK (attribute_hash);
310 return attr_id;
313 static void
314 g_file_info_finalize (GObject *object)
316 GFileInfo *info;
317 int i;
318 GFileAttribute *attrs;
320 info = G_FILE_INFO (object);
322 attrs = (GFileAttribute *)info->attributes->data;
323 for (i = 0; i < info->attributes->len; i++)
324 _g_file_attribute_value_clear (&attrs[i].value);
325 g_array_free (info->attributes, TRUE);
327 if (info->mask != NO_ATTRIBUTE_MASK)
328 g_file_attribute_matcher_unref (info->mask);
330 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
333 static void
334 g_file_info_class_init (GFileInfoClass *klass)
336 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
338 gobject_class->finalize = g_file_info_finalize;
341 static void
342 g_file_info_init (GFileInfo *info)
344 info->mask = NO_ATTRIBUTE_MASK;
345 info->attributes = g_array_new (FALSE, FALSE,
346 sizeof (GFileAttribute));
350 * g_file_info_new:
352 * Creates a new file info structure.
354 * Returns: a #GFileInfo.
356 GFileInfo *
357 g_file_info_new (void)
359 return g_object_new (G_TYPE_FILE_INFO, NULL);
363 * g_file_info_copy_into:
364 * @src_info: source to copy attributes from.
365 * @dest_info: destination to copy attributes to.
367 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
369 void
370 g_file_info_copy_into (GFileInfo *src_info,
371 GFileInfo *dest_info)
373 GFileAttribute *source, *dest;
374 int i;
376 g_return_if_fail (G_IS_FILE_INFO (src_info));
377 g_return_if_fail (G_IS_FILE_INFO (dest_info));
379 dest = (GFileAttribute *)dest_info->attributes->data;
380 for (i = 0; i < dest_info->attributes->len; i++)
381 _g_file_attribute_value_clear (&dest[i].value);
383 g_array_set_size (dest_info->attributes,
384 src_info->attributes->len);
386 source = (GFileAttribute *)src_info->attributes->data;
387 dest = (GFileAttribute *)dest_info->attributes->data;
389 for (i = 0; i < src_info->attributes->len; i++)
391 dest[i].attribute = source[i].attribute;
392 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
393 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
396 if (dest_info->mask != NO_ATTRIBUTE_MASK)
397 g_file_attribute_matcher_unref (dest_info->mask);
399 if (src_info->mask == NO_ATTRIBUTE_MASK)
400 dest_info->mask = NO_ATTRIBUTE_MASK;
401 else
402 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
406 * g_file_info_dup:
407 * @other: a #GFileInfo.
409 * Duplicates a file info structure.
411 * Returns: (transfer full): a duplicate #GFileInfo of @other.
413 GFileInfo *
414 g_file_info_dup (GFileInfo *other)
416 GFileInfo *new;
418 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
420 new = g_file_info_new ();
421 g_file_info_copy_into (other, new);
422 return new;
426 * g_file_info_set_attribute_mask:
427 * @info: a #GFileInfo.
428 * @mask: a #GFileAttributeMatcher.
430 * Sets @mask on @info to match specific attribute types.
432 void
433 g_file_info_set_attribute_mask (GFileInfo *info,
434 GFileAttributeMatcher *mask)
436 GFileAttribute *attr;
437 int i;
439 g_return_if_fail (G_IS_FILE_INFO (info));
441 if (mask != info->mask)
443 if (info->mask != NO_ATTRIBUTE_MASK)
444 g_file_attribute_matcher_unref (info->mask);
445 info->mask = g_file_attribute_matcher_ref (mask);
447 /* Remove non-matching attributes */
448 for (i = 0; i < info->attributes->len; i++)
450 attr = &g_array_index (info->attributes, GFileAttribute, i);
451 if (!_g_file_attribute_matcher_matches_id (mask,
452 attr->attribute))
454 _g_file_attribute_value_clear (&attr->value);
455 g_array_remove_index (info->attributes, i);
456 i--;
463 * g_file_info_unset_attribute_mask:
464 * @info: #GFileInfo.
466 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
467 * is set.
469 void
470 g_file_info_unset_attribute_mask (GFileInfo *info)
472 g_return_if_fail (G_IS_FILE_INFO (info));
474 if (info->mask != NO_ATTRIBUTE_MASK)
475 g_file_attribute_matcher_unref (info->mask);
476 info->mask = NO_ATTRIBUTE_MASK;
480 * g_file_info_clear_status:
481 * @info: a #GFileInfo.
483 * Clears the status information from @info.
485 void
486 g_file_info_clear_status (GFileInfo *info)
488 GFileAttribute *attrs;
489 int i;
491 g_return_if_fail (G_IS_FILE_INFO (info));
493 attrs = (GFileAttribute *)info->attributes->data;
494 for (i = 0; i < info->attributes->len; i++)
495 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
498 static int
499 g_file_info_find_place (GFileInfo *info,
500 guint32 attribute)
502 int min, max, med;
503 GFileAttribute *attrs;
504 /* Binary search for the place where attribute would be, if it's
505 in the array */
507 min = 0;
508 max = info->attributes->len;
510 attrs = (GFileAttribute *)info->attributes->data;
512 while (min < max)
514 med = min + (max - min) / 2;
515 if (attrs[med].attribute == attribute)
517 min = med;
518 break;
520 else if (attrs[med].attribute < attribute)
521 min = med + 1;
522 else /* attrs[med].attribute > attribute */
523 max = med;
526 return min;
529 static GFileAttributeValue *
530 g_file_info_find_value (GFileInfo *info,
531 guint32 attr_id)
533 GFileAttribute *attrs;
534 int i;
536 i = g_file_info_find_place (info, attr_id);
537 attrs = (GFileAttribute *)info->attributes->data;
538 if (i < info->attributes->len &&
539 attrs[i].attribute == attr_id)
540 return &attrs[i].value;
542 return NULL;
545 static GFileAttributeValue *
546 g_file_info_find_value_by_name (GFileInfo *info,
547 const char *attribute)
549 guint32 attr_id;
551 attr_id = lookup_attribute (attribute);
552 return g_file_info_find_value (info, attr_id);
556 * g_file_info_has_attribute:
557 * @info: a #GFileInfo.
558 * @attribute: a file attribute key.
560 * Checks if a file info structure has an attribute named @attribute.
562 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
563 * %FALSE otherwise.
565 gboolean
566 g_file_info_has_attribute (GFileInfo *info,
567 const char *attribute)
569 GFileAttributeValue *value;
571 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
572 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
574 value = g_file_info_find_value_by_name (info, attribute);
575 return value != NULL;
579 * g_file_info_has_namespace:
580 * @info: a #GFileInfo.
581 * @name_space: a file attribute namespace.
583 * Checks if a file info structure has an attribute in the
584 * specified @name_space.
586 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
587 * %FALSE otherwise.
589 * Since: 2.22
591 gboolean
592 g_file_info_has_namespace (GFileInfo *info,
593 const char *name_space)
595 GFileAttribute *attrs;
596 guint32 ns_id;
597 int i;
599 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
600 g_return_val_if_fail (name_space != NULL, FALSE);
602 ns_id = lookup_namespace (name_space);
604 attrs = (GFileAttribute *)info->attributes->data;
605 for (i = 0; i < info->attributes->len; i++)
607 if (GET_NS (attrs[i].attribute) == ns_id)
608 return TRUE;
611 return FALSE;
615 * g_file_info_list_attributes:
616 * @info: a #GFileInfo.
617 * @name_space: a file attribute key's namespace.
619 * Lists the file info structure's attributes.
621 * Returns: (array zero-terminated=1) (transfer full): a null-terminated array of strings of all of the
622 * possible attribute types for the given @name_space, or
623 * %NULL on error.
625 char **
626 g_file_info_list_attributes (GFileInfo *info,
627 const char *name_space)
629 GPtrArray *names;
630 GFileAttribute *attrs;
631 guint32 attribute;
632 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
633 int i;
635 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
637 names = g_ptr_array_new ();
638 attrs = (GFileAttribute *)info->attributes->data;
639 for (i = 0; i < info->attributes->len; i++)
641 attribute = attrs[i].attribute;
642 if (ns_id == 0 || GET_NS (attribute) == ns_id)
643 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
646 /* NULL terminate */
647 g_ptr_array_add (names, NULL);
649 return (char **)g_ptr_array_free (names, FALSE);
653 * g_file_info_get_attribute_type:
654 * @info: a #GFileInfo.
655 * @attribute: a file attribute key.
657 * Gets the attribute type for an attribute key.
659 * Returns: a #GFileAttributeType for the given @attribute, or
660 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
662 GFileAttributeType
663 g_file_info_get_attribute_type (GFileInfo *info,
664 const char *attribute)
666 GFileAttributeValue *value;
668 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
669 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
671 value = g_file_info_find_value_by_name (info, attribute);
672 if (value)
673 return value->type;
674 else
675 return G_FILE_ATTRIBUTE_TYPE_INVALID;
679 * g_file_info_remove_attribute:
680 * @info: a #GFileInfo.
681 * @attribute: a file attribute key.
683 * Removes all cases of @attribute from @info if it exists.
685 void
686 g_file_info_remove_attribute (GFileInfo *info,
687 const char *attribute)
689 guint32 attr_id;
690 GFileAttribute *attrs;
691 int i;
693 g_return_if_fail (G_IS_FILE_INFO (info));
694 g_return_if_fail (attribute != NULL && *attribute != '\0');
696 attr_id = lookup_attribute (attribute);
698 i = g_file_info_find_place (info, attr_id);
699 attrs = (GFileAttribute *)info->attributes->data;
700 if (i < info->attributes->len &&
701 attrs[i].attribute == attr_id)
703 _g_file_attribute_value_clear (&attrs[i].value);
704 g_array_remove_index (info->attributes, i);
709 * g_file_info_get_attribute_data:
710 * @info: a #GFileInfo
711 * @attribute: a file attribute key
712 * @type: (out) (allow-none): return location for the attribute type, or %NULL
713 * @value_pp: (out) (allow-none): return location for the attribute value, or %NULL
714 * @status: (out) (allow-none): return location for the attribute status, or %NULL
716 * Gets the attribute type, value and status for an attribute key.
718 * Returns: (transfer none): %TRUE if @info has an attribute named @attribute,
719 * %FALSE otherwise.
721 gboolean
722 g_file_info_get_attribute_data (GFileInfo *info,
723 const char *attribute,
724 GFileAttributeType *type,
725 gpointer *value_pp,
726 GFileAttributeStatus *status)
728 GFileAttributeValue *value;
730 value = g_file_info_find_value_by_name (info, attribute);
731 if (value == NULL)
732 return FALSE;
734 if (status)
735 *status = value->status;
737 if (type)
738 *type = value->type;
740 if (value_pp)
741 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
743 return TRUE;
747 * g_file_info_get_attribute_status:
748 * @info: a #GFileInfo
749 * @attribute: a file attribute key
751 * Gets the attribute status for an attribute key.
753 * Returns: a #GFileAttributeStatus for the given @attribute, or
754 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
757 GFileAttributeStatus
758 g_file_info_get_attribute_status (GFileInfo *info,
759 const char *attribute)
761 GFileAttributeValue *val;
763 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
764 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
766 val = g_file_info_find_value_by_name (info, attribute);
767 if (val)
768 return val->status;
770 return G_FILE_ATTRIBUTE_STATUS_UNSET;
774 * g_file_info_set_attribute_status:
775 * @info: a #GFileInfo
776 * @attribute: a file attribute key
777 * @status: a #GFileAttributeStatus
779 * Sets the attribute status for an attribute key. This is only
780 * needed by external code that implement g_file_set_attributes_from_info()
781 * or similar functions.
783 * The attribute must exist in @info for this to work. Otherwise %FALSE
784 * is returned and @info is unchanged.
786 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
788 * Since: 2.22
790 gboolean
791 g_file_info_set_attribute_status (GFileInfo *info,
792 const char *attribute,
793 GFileAttributeStatus status)
795 GFileAttributeValue *val;
797 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
798 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
800 val = g_file_info_find_value_by_name (info, attribute);
801 if (val)
803 val->status = status;
804 return TRUE;
807 return FALSE;
810 GFileAttributeValue *
811 _g_file_info_get_attribute_value (GFileInfo *info,
812 const char *attribute)
815 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
816 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
818 return g_file_info_find_value_by_name (info, attribute);
822 * g_file_info_get_attribute_as_string:
823 * @info: a #GFileInfo.
824 * @attribute: a file attribute key.
826 * Gets the value of a attribute, formated as a string.
827 * This escapes things as needed to make the string valid
828 * utf8.
830 * Returns: a UTF-8 string associated with the given @attribute.
831 * When you're done with the string it must be freed with g_free().
833 char *
834 g_file_info_get_attribute_as_string (GFileInfo *info,
835 const char *attribute)
837 GFileAttributeValue *val;
838 val = _g_file_info_get_attribute_value (info, attribute);
839 if (val)
840 return _g_file_attribute_value_as_string (val);
841 return NULL;
846 * g_file_info_get_attribute_object:
847 * @info: a #GFileInfo.
848 * @attribute: a file attribute key.
850 * Gets the value of a #GObject attribute. If the attribute does
851 * not contain a #GObject, %NULL will be returned.
853 * Returns: (transfer none): a #GObject associated with the given @attribute, or
854 * %NULL otherwise.
856 GObject *
857 g_file_info_get_attribute_object (GFileInfo *info,
858 const char *attribute)
860 GFileAttributeValue *value;
862 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
863 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
865 value = g_file_info_find_value_by_name (info, attribute);
866 return _g_file_attribute_value_get_object (value);
870 * g_file_info_get_attribute_string:
871 * @info: a #GFileInfo.
872 * @attribute: a file attribute key.
874 * Gets the value of a string attribute. If the attribute does
875 * not contain a string, %NULL will be returned.
877 * Returns: the contents of the @attribute value as a UTF-8 string, or
878 * %NULL otherwise.
880 const char *
881 g_file_info_get_attribute_string (GFileInfo *info,
882 const char *attribute)
884 GFileAttributeValue *value;
886 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
887 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
889 value = g_file_info_find_value_by_name (info, attribute);
890 return _g_file_attribute_value_get_string (value);
894 * g_file_info_get_attribute_byte_string:
895 * @info: a #GFileInfo.
896 * @attribute: a file attribute key.
898 * Gets the value of a byte string attribute. If the attribute does
899 * not contain a byte string, %NULL will be returned.
901 * Returns: the contents of the @attribute value as a byte string, or
902 * %NULL otherwise.
904 const char *
905 g_file_info_get_attribute_byte_string (GFileInfo *info,
906 const char *attribute)
908 GFileAttributeValue *value;
910 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
911 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
913 value = g_file_info_find_value_by_name (info, attribute);
914 return _g_file_attribute_value_get_byte_string (value);
918 * g_file_info_get_attribute_stringv:
919 * @info: a #GFileInfo.
920 * @attribute: a file attribute key.
922 * Gets the value of a stringv attribute. If the attribute does
923 * not contain a stringv, %NULL will be returned.
925 * Returns: (transfer none): the contents of the @attribute value as a stringv, or
926 * %NULL otherwise. Do not free. These returned strings are UTF-8.
928 * Since: 2.22
930 char **
931 g_file_info_get_attribute_stringv (GFileInfo *info,
932 const char *attribute)
934 GFileAttributeValue *value;
936 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
937 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
939 value = g_file_info_find_value_by_name (info, attribute);
940 return _g_file_attribute_value_get_stringv (value);
944 * g_file_info_get_attribute_boolean:
945 * @info: a #GFileInfo.
946 * @attribute: a file attribute key.
948 * Gets the value of a boolean attribute. If the attribute does not
949 * contain a boolean value, %FALSE will be returned.
951 * Returns: the boolean value contained within the attribute.
953 gboolean
954 g_file_info_get_attribute_boolean (GFileInfo *info,
955 const char *attribute)
957 GFileAttributeValue *value;
959 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
960 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
962 value = g_file_info_find_value_by_name (info, attribute);
963 return _g_file_attribute_value_get_boolean (value);
967 * g_file_info_get_attribute_uint32:
968 * @info: a #GFileInfo.
969 * @attribute: a file attribute key.
971 * Gets an unsigned 32-bit integer contained within the attribute. If the
972 * attribute does not contain an unsigned 32-bit integer, or is invalid,
973 * 0 will be returned.
975 * Returns: an unsigned 32-bit integer from the attribute.
977 guint32
978 g_file_info_get_attribute_uint32 (GFileInfo *info,
979 const char *attribute)
981 GFileAttributeValue *value;
983 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
984 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
986 value = g_file_info_find_value_by_name (info, attribute);
987 return _g_file_attribute_value_get_uint32 (value);
991 * g_file_info_get_attribute_int32:
992 * @info: a #GFileInfo.
993 * @attribute: a file attribute key.
995 * Gets a signed 32-bit integer contained within the attribute. If the
996 * attribute does not contain a signed 32-bit integer, or is invalid,
997 * 0 will be returned.
999 * Returns: a signed 32-bit integer from the attribute.
1001 gint32
1002 g_file_info_get_attribute_int32 (GFileInfo *info,
1003 const char *attribute)
1005 GFileAttributeValue *value;
1007 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1008 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1010 value = g_file_info_find_value_by_name (info, attribute);
1011 return _g_file_attribute_value_get_int32 (value);
1015 * g_file_info_get_attribute_uint64:
1016 * @info: a #GFileInfo.
1017 * @attribute: a file attribute key.
1019 * Gets a unsigned 64-bit integer contained within the attribute. If the
1020 * attribute does not contain an unsigned 64-bit integer, or is invalid,
1021 * 0 will be returned.
1023 * Returns: a unsigned 64-bit integer from the attribute.
1025 guint64
1026 g_file_info_get_attribute_uint64 (GFileInfo *info,
1027 const char *attribute)
1029 GFileAttributeValue *value;
1031 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1032 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1034 value = g_file_info_find_value_by_name (info, attribute);
1035 return _g_file_attribute_value_get_uint64 (value);
1039 * g_file_info_get_attribute_int64:
1040 * @info: a #GFileInfo.
1041 * @attribute: a file attribute key.
1043 * Gets a signed 64-bit integer contained within the attribute. If the
1044 * attribute does not contain an signed 64-bit integer, or is invalid,
1045 * 0 will be returned.
1047 * Returns: a signed 64-bit integer from the attribute.
1049 gint64
1050 g_file_info_get_attribute_int64 (GFileInfo *info,
1051 const char *attribute)
1053 GFileAttributeValue *value;
1055 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1056 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1058 value = g_file_info_find_value_by_name (info, attribute);
1059 return _g_file_attribute_value_get_int64 (value);
1062 static GFileAttributeValue *
1063 g_file_info_create_value (GFileInfo *info,
1064 guint32 attr_id)
1066 GFileAttribute *attrs;
1067 int i;
1069 if (info->mask != NO_ATTRIBUTE_MASK &&
1070 !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
1071 return NULL;
1073 i = g_file_info_find_place (info, attr_id);
1075 attrs = (GFileAttribute *)info->attributes->data;
1076 if (i < info->attributes->len &&
1077 attrs[i].attribute == attr_id)
1078 return &attrs[i].value;
1079 else
1081 GFileAttribute attr = { 0 };
1082 attr.attribute = attr_id;
1083 g_array_insert_val (info->attributes, i, attr);
1085 attrs = (GFileAttribute *)info->attributes->data;
1086 return &attrs[i].value;
1090 void
1091 _g_file_info_set_attribute_by_id (GFileInfo *info,
1092 guint32 attribute,
1093 GFileAttributeType type,
1094 gpointer value_p)
1096 GFileAttributeValue *value;
1098 value = g_file_info_create_value (info, attribute);
1100 if (value)
1101 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1105 * g_file_info_set_attribute:
1106 * @info: a #GFileInfo.
1107 * @attribute: a file attribute key.
1108 * @type: a #GFileAttributeType
1109 * @value_p: pointer to the value
1111 * Sets the @attribute to contain the given value, if possible.
1113 void
1114 g_file_info_set_attribute (GFileInfo *info,
1115 const char *attribute,
1116 GFileAttributeType type,
1117 gpointer value_p)
1119 g_return_if_fail (G_IS_FILE_INFO (info));
1120 g_return_if_fail (attribute != NULL && *attribute != '\0');
1122 _g_file_info_set_attribute_by_id (info, lookup_attribute (attribute), type, value_p);
1125 void
1126 _g_file_info_set_attribute_object_by_id (GFileInfo *info,
1127 guint32 attribute,
1128 GObject *attr_value)
1130 GFileAttributeValue *value;
1132 value = g_file_info_create_value (info, attribute);
1133 if (value)
1134 _g_file_attribute_value_set_object (value, attr_value);
1138 * g_file_info_set_attribute_object:
1139 * @info: a #GFileInfo.
1140 * @attribute: a file attribute key.
1141 * @attr_value: a #GObject.
1143 * Sets the @attribute to contain the given @attr_value,
1144 * if possible.
1146 void
1147 g_file_info_set_attribute_object (GFileInfo *info,
1148 const char *attribute,
1149 GObject *attr_value)
1151 g_return_if_fail (G_IS_FILE_INFO (info));
1152 g_return_if_fail (attribute != NULL && *attribute != '\0');
1153 g_return_if_fail (G_IS_OBJECT (attr_value));
1155 _g_file_info_set_attribute_object_by_id (info,
1156 lookup_attribute (attribute),
1157 attr_value);
1160 void
1161 _g_file_info_set_attribute_stringv_by_id (GFileInfo *info,
1162 guint32 attribute,
1163 char **attr_value)
1165 GFileAttributeValue *value;
1167 value = g_file_info_create_value (info, attribute);
1168 if (value)
1169 _g_file_attribute_value_set_stringv (value, attr_value);
1173 * g_file_info_set_attribute_stringv:
1174 * @info: a #GFileInfo.
1175 * @attribute: a file attribute key
1176 * @attr_value: a %NULL terminated array of UTF-8 strings.
1178 * Sets the @attribute to contain the given @attr_value,
1179 * if possible.
1181 * Sinze: 2.22
1183 void
1184 g_file_info_set_attribute_stringv (GFileInfo *info,
1185 const char *attribute,
1186 char **attr_value)
1188 g_return_if_fail (G_IS_FILE_INFO (info));
1189 g_return_if_fail (attribute != NULL && *attribute != '\0');
1190 g_return_if_fail (attr_value != NULL);
1192 _g_file_info_set_attribute_stringv_by_id (info,
1193 lookup_attribute (attribute),
1194 attr_value);
1197 void
1198 _g_file_info_set_attribute_string_by_id (GFileInfo *info,
1199 guint32 attribute,
1200 const char *attr_value)
1202 GFileAttributeValue *value;
1204 value = g_file_info_create_value (info, attribute);
1205 if (value)
1206 _g_file_attribute_value_set_string (value, attr_value);
1210 * g_file_info_set_attribute_string:
1211 * @info: a #GFileInfo.
1212 * @attribute: a file attribute key.
1213 * @attr_value: a UTF-8 string.
1215 * Sets the @attribute to contain the given @attr_value,
1216 * if possible.
1218 void
1219 g_file_info_set_attribute_string (GFileInfo *info,
1220 const char *attribute,
1221 const char *attr_value)
1223 g_return_if_fail (G_IS_FILE_INFO (info));
1224 g_return_if_fail (attribute != NULL && *attribute != '\0');
1225 g_return_if_fail (attr_value != NULL);
1227 _g_file_info_set_attribute_string_by_id (info,
1228 lookup_attribute (attribute),
1229 attr_value);
1232 void
1233 _g_file_info_set_attribute_byte_string_by_id (GFileInfo *info,
1234 guint32 attribute,
1235 const char *attr_value)
1237 GFileAttributeValue *value;
1239 value = g_file_info_create_value (info, attribute);
1240 if (value)
1241 _g_file_attribute_value_set_byte_string (value, attr_value);
1245 * g_file_info_set_attribute_byte_string:
1246 * @info: a #GFileInfo.
1247 * @attribute: a file attribute key.
1248 * @attr_value: a byte string.
1250 * Sets the @attribute to contain the given @attr_value,
1251 * if possible.
1253 void
1254 g_file_info_set_attribute_byte_string (GFileInfo *info,
1255 const char *attribute,
1256 const char *attr_value)
1258 g_return_if_fail (G_IS_FILE_INFO (info));
1259 g_return_if_fail (attribute != NULL && *attribute != '\0');
1260 g_return_if_fail (attr_value != NULL);
1262 _g_file_info_set_attribute_byte_string_by_id (info,
1263 lookup_attribute (attribute),
1264 attr_value);
1267 void
1268 _g_file_info_set_attribute_boolean_by_id (GFileInfo *info,
1269 guint32 attribute,
1270 gboolean attr_value)
1272 GFileAttributeValue *value;
1274 value = g_file_info_create_value (info, attribute);
1275 if (value)
1276 _g_file_attribute_value_set_boolean (value, attr_value);
1280 * g_file_info_set_attribute_boolean:
1281 * @info: a #GFileInfo.
1282 * @attribute: a file attribute key.
1283 * @attr_value: a boolean value.
1285 * Sets the @attribute to contain the given @attr_value,
1286 * if possible.
1288 void
1289 g_file_info_set_attribute_boolean (GFileInfo *info,
1290 const char *attribute,
1291 gboolean attr_value)
1293 g_return_if_fail (G_IS_FILE_INFO (info));
1294 g_return_if_fail (attribute != NULL && *attribute != '\0');
1296 _g_file_info_set_attribute_boolean_by_id (info,
1297 lookup_attribute (attribute),
1298 attr_value);
1301 void
1302 _g_file_info_set_attribute_uint32_by_id (GFileInfo *info,
1303 guint32 attribute,
1304 guint32 attr_value)
1306 GFileAttributeValue *value;
1308 value = g_file_info_create_value (info, attribute);
1309 if (value)
1310 _g_file_attribute_value_set_uint32 (value, attr_value);
1314 * g_file_info_set_attribute_uint32:
1315 * @info: a #GFileInfo.
1316 * @attribute: a file attribute key.
1317 * @attr_value: an unsigned 32-bit integer.
1319 * Sets the @attribute to contain the given @attr_value,
1320 * if possible.
1322 void
1323 g_file_info_set_attribute_uint32 (GFileInfo *info,
1324 const char *attribute,
1325 guint32 attr_value)
1327 g_return_if_fail (G_IS_FILE_INFO (info));
1328 g_return_if_fail (attribute != NULL && *attribute != '\0');
1330 _g_file_info_set_attribute_uint32_by_id (info,
1331 lookup_attribute (attribute),
1332 attr_value);
1335 void
1336 _g_file_info_set_attribute_int32_by_id (GFileInfo *info,
1337 guint32 attribute,
1338 gint32 attr_value)
1340 GFileAttributeValue *value;
1342 value = g_file_info_create_value (info, attribute);
1343 if (value)
1344 _g_file_attribute_value_set_int32 (value, attr_value);
1348 * g_file_info_set_attribute_int32:
1349 * @info: a #GFileInfo.
1350 * @attribute: a file attribute key.
1351 * @attr_value: a signed 32-bit integer
1353 * Sets the @attribute to contain the given @attr_value,
1354 * if possible.
1356 void
1357 g_file_info_set_attribute_int32 (GFileInfo *info,
1358 const char *attribute,
1359 gint32 attr_value)
1361 g_return_if_fail (G_IS_FILE_INFO (info));
1362 g_return_if_fail (attribute != NULL && *attribute != '\0');
1364 _g_file_info_set_attribute_int32_by_id (info,
1365 lookup_attribute (attribute),
1366 attr_value);
1369 void
1370 _g_file_info_set_attribute_uint64_by_id (GFileInfo *info,
1371 guint32 attribute,
1372 guint64 attr_value)
1374 GFileAttributeValue *value;
1376 value = g_file_info_create_value (info, attribute);
1377 if (value)
1378 _g_file_attribute_value_set_uint64 (value, attr_value);
1382 * g_file_info_set_attribute_uint64:
1383 * @info: a #GFileInfo.
1384 * @attribute: a file attribute key.
1385 * @attr_value: an unsigned 64-bit integer.
1387 * Sets the @attribute to contain the given @attr_value,
1388 * if possible.
1390 void
1391 g_file_info_set_attribute_uint64 (GFileInfo *info,
1392 const char *attribute,
1393 guint64 attr_value)
1395 g_return_if_fail (G_IS_FILE_INFO (info));
1396 g_return_if_fail (attribute != NULL && *attribute != '\0');
1398 _g_file_info_set_attribute_uint64_by_id (info,
1399 lookup_attribute (attribute),
1400 attr_value);
1403 void
1404 _g_file_info_set_attribute_int64_by_id (GFileInfo *info,
1405 guint32 attribute,
1406 gint64 attr_value)
1408 GFileAttributeValue *value;
1410 value = g_file_info_create_value (info, attribute);
1411 if (value)
1412 _g_file_attribute_value_set_int64 (value, attr_value);
1416 * g_file_info_set_attribute_int64:
1417 * @info: a #GFileInfo.
1418 * @attribute: attribute name to set.
1419 * @attr_value: int64 value to set attribute to.
1421 * Sets the @attribute to contain the given @attr_value,
1422 * if possible.
1425 void
1426 g_file_info_set_attribute_int64 (GFileInfo *info,
1427 const char *attribute,
1428 gint64 attr_value)
1430 g_return_if_fail (G_IS_FILE_INFO (info));
1431 g_return_if_fail (attribute != NULL && *attribute != '\0');
1433 _g_file_info_set_attribute_int64_by_id (info,
1434 lookup_attribute (attribute),
1435 attr_value);
1438 /* Helper getters */
1440 * g_file_info_get_file_type:
1441 * @info: a #GFileInfo.
1443 * Gets a file's type (whether it is a regular file, symlink, etc).
1444 * This is different from the file's content type, see g_file_info_get_content_type().
1446 * Returns: a #GFileType for the given file.
1448 GFileType
1449 g_file_info_get_file_type (GFileInfo *info)
1451 static guint32 attr = 0;
1452 GFileAttributeValue *value;
1454 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1456 if (attr == 0)
1457 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1459 value = g_file_info_find_value (info, attr);
1460 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1464 * g_file_info_get_is_hidden:
1465 * @info: a #GFileInfo.
1467 * Checks if a file is hidden.
1469 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1471 gboolean
1472 g_file_info_get_is_hidden (GFileInfo *info)
1474 static guint32 attr = 0;
1475 GFileAttributeValue *value;
1477 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1479 if (attr == 0)
1480 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1482 value = g_file_info_find_value (info, attr);
1483 return (GFileType)_g_file_attribute_value_get_boolean (value);
1487 * g_file_info_get_is_backup:
1488 * @info: a #GFileInfo.
1490 * Checks if a file is a backup file.
1492 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1494 gboolean
1495 g_file_info_get_is_backup (GFileInfo *info)
1497 static guint32 attr = 0;
1498 GFileAttributeValue *value;
1500 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1502 if (attr == 0)
1503 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1505 value = g_file_info_find_value (info, attr);
1506 return (GFileType)_g_file_attribute_value_get_boolean (value);
1510 * g_file_info_get_is_symlink:
1511 * @info: a #GFileInfo.
1513 * Checks if a file is a symlink.
1515 * Returns: %TRUE if the given @info is a symlink.
1517 gboolean
1518 g_file_info_get_is_symlink (GFileInfo *info)
1520 static guint32 attr = 0;
1521 GFileAttributeValue *value;
1523 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1525 if (attr == 0)
1526 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1528 value = g_file_info_find_value (info, attr);
1529 return (GFileType)_g_file_attribute_value_get_boolean (value);
1533 * g_file_info_get_name:
1534 * @info: a #GFileInfo.
1536 * Gets the name for a file.
1538 * Returns: a string containing the file name.
1540 const char *
1541 g_file_info_get_name (GFileInfo *info)
1543 static guint32 attr = 0;
1544 GFileAttributeValue *value;
1546 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1548 if (attr == 0)
1549 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1551 value = g_file_info_find_value (info, attr);
1552 return _g_file_attribute_value_get_byte_string (value);
1556 * g_file_info_get_display_name:
1557 * @info: a #GFileInfo.
1559 * Gets a display name for a file.
1561 * Returns: a string containing the display name.
1563 const char *
1564 g_file_info_get_display_name (GFileInfo *info)
1566 static guint32 attr = 0;
1567 GFileAttributeValue *value;
1569 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1571 if (attr == 0)
1572 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1574 value = g_file_info_find_value (info, attr);
1575 return _g_file_attribute_value_get_string (value);
1579 * g_file_info_get_edit_name:
1580 * @info: a #GFileInfo.
1582 * Gets the edit name for a file.
1584 * Returns: a string containing the edit name.
1586 const char *
1587 g_file_info_get_edit_name (GFileInfo *info)
1589 static guint32 attr = 0;
1590 GFileAttributeValue *value;
1592 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1594 if (attr == 0)
1595 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1597 value = g_file_info_find_value (info, attr);
1598 return _g_file_attribute_value_get_string (value);
1602 * g_file_info_get_icon:
1603 * @info: a #GFileInfo.
1605 * Gets the icon for a file.
1607 * Returns: (transfer none): #GIcon for the given @info.
1609 GIcon *
1610 g_file_info_get_icon (GFileInfo *info)
1612 static guint32 attr = 0;
1613 GFileAttributeValue *value;
1614 GObject *obj;
1616 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1618 if (attr == 0)
1619 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1621 value = g_file_info_find_value (info, attr);
1622 obj = _g_file_attribute_value_get_object (value);
1623 if (G_IS_ICON (obj))
1624 return G_ICON (obj);
1625 return NULL;
1629 * g_file_info_get_content_type:
1630 * @info: a #GFileInfo.
1632 * Gets the file's content type.
1634 * Returns: a string containing the file's content type.
1636 const char *
1637 g_file_info_get_content_type (GFileInfo *info)
1639 static guint32 attr = 0;
1640 GFileAttributeValue *value;
1642 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1644 if (attr == 0)
1645 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1647 value = g_file_info_find_value (info, attr);
1648 return _g_file_attribute_value_get_string (value);
1652 * g_file_info_get_size:
1653 * @info: a #GFileInfo.
1655 * Gets the file's size.
1657 * Returns: a #goffset containing the file's size.
1659 goffset
1660 g_file_info_get_size (GFileInfo *info)
1662 static guint32 attr = 0;
1663 GFileAttributeValue *value;
1665 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1667 if (attr == 0)
1668 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1670 value = g_file_info_find_value (info, attr);
1671 return (goffset) _g_file_attribute_value_get_uint64 (value);
1675 * g_file_info_get_modification_time:
1676 * @info: a #GFileInfo.
1677 * @result: (out caller-allocates): a #GTimeVal.
1679 * Gets the modification time of the current @info and sets it
1680 * in @result.
1682 void
1683 g_file_info_get_modification_time (GFileInfo *info,
1684 GTimeVal *result)
1686 static guint32 attr_mtime = 0, attr_mtime_usec;
1687 GFileAttributeValue *value;
1689 g_return_if_fail (G_IS_FILE_INFO (info));
1690 g_return_if_fail (result != NULL);
1692 if (attr_mtime == 0)
1694 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1695 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1698 value = g_file_info_find_value (info, attr_mtime);
1699 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1700 value = g_file_info_find_value (info, attr_mtime_usec);
1701 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1705 * g_file_info_get_symlink_target:
1706 * @info: a #GFileInfo.
1708 * Gets the symlink target for a given #GFileInfo.
1710 * Returns: a string containing the symlink target.
1712 const char *
1713 g_file_info_get_symlink_target (GFileInfo *info)
1715 static guint32 attr = 0;
1716 GFileAttributeValue *value;
1718 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1720 if (attr == 0)
1721 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1723 value = g_file_info_find_value (info, attr);
1724 return _g_file_attribute_value_get_byte_string (value);
1728 * g_file_info_get_etag:
1729 * @info: a #GFileInfo.
1731 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1732 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1734 * Returns: a string containing the value of the "etag:value" attribute.
1736 const char *
1737 g_file_info_get_etag (GFileInfo *info)
1739 static guint32 attr = 0;
1740 GFileAttributeValue *value;
1742 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1744 if (attr == 0)
1745 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1747 value = g_file_info_find_value (info, attr);
1748 return _g_file_attribute_value_get_string (value);
1752 * g_file_info_get_sort_order:
1753 * @info: a #GFileInfo.
1755 * Gets the value of the sort_order attribute from the #GFileInfo.
1756 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1758 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1760 gint32
1761 g_file_info_get_sort_order (GFileInfo *info)
1763 static guint32 attr = 0;
1764 GFileAttributeValue *value;
1766 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1768 if (attr == 0)
1769 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1771 value = g_file_info_find_value (info, attr);
1772 return _g_file_attribute_value_get_int32 (value);
1775 /* Helper setters: */
1777 * g_file_info_set_file_type:
1778 * @info: a #GFileInfo.
1779 * @type: a #GFileType.
1781 * Sets the file type in a #GFileInfo to @type.
1782 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1784 void
1785 g_file_info_set_file_type (GFileInfo *info,
1786 GFileType type)
1788 static guint32 attr = 0;
1789 GFileAttributeValue *value;
1791 g_return_if_fail (G_IS_FILE_INFO (info));
1793 if (attr == 0)
1794 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1796 value = g_file_info_create_value (info, attr);
1797 if (value)
1798 _g_file_attribute_value_set_uint32 (value, type);
1802 * g_file_info_set_is_hidden:
1803 * @info: a #GFileInfo.
1804 * @is_hidden: a #gboolean.
1806 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1807 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1809 void
1810 g_file_info_set_is_hidden (GFileInfo *info,
1811 gboolean is_hidden)
1813 static guint32 attr = 0;
1814 GFileAttributeValue *value;
1816 g_return_if_fail (G_IS_FILE_INFO (info));
1818 if (attr == 0)
1819 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1821 value = g_file_info_create_value (info, attr);
1822 if (value)
1823 _g_file_attribute_value_set_boolean (value, is_hidden);
1827 * g_file_info_set_is_symlink:
1828 * @info: a #GFileInfo.
1829 * @is_symlink: a #gboolean.
1831 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1832 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1834 void
1835 g_file_info_set_is_symlink (GFileInfo *info,
1836 gboolean is_symlink)
1838 static guint32 attr = 0;
1839 GFileAttributeValue *value;
1841 g_return_if_fail (G_IS_FILE_INFO (info));
1843 if (attr == 0)
1844 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1846 value = g_file_info_create_value (info, attr);
1847 if (value)
1848 _g_file_attribute_value_set_boolean (value, is_symlink);
1852 * g_file_info_set_name:
1853 * @info: a #GFileInfo.
1854 * @name: a string containing a name.
1856 * Sets the name attribute for the current #GFileInfo.
1857 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1859 void
1860 g_file_info_set_name (GFileInfo *info,
1861 const char *name)
1863 static guint32 attr = 0;
1864 GFileAttributeValue *value;
1866 g_return_if_fail (G_IS_FILE_INFO (info));
1867 g_return_if_fail (name != NULL);
1869 if (attr == 0)
1870 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1872 value = g_file_info_create_value (info, attr);
1873 if (value)
1874 _g_file_attribute_value_set_byte_string (value, name);
1878 * g_file_info_set_display_name:
1879 * @info: a #GFileInfo.
1880 * @display_name: a string containing a display name.
1882 * Sets the display name for the current #GFileInfo.
1883 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1885 void
1886 g_file_info_set_display_name (GFileInfo *info,
1887 const char *display_name)
1889 static guint32 attr = 0;
1890 GFileAttributeValue *value;
1892 g_return_if_fail (G_IS_FILE_INFO (info));
1893 g_return_if_fail (display_name != NULL);
1895 if (attr == 0)
1896 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1898 value = g_file_info_create_value (info, attr);
1899 if (value)
1900 _g_file_attribute_value_set_string (value, display_name);
1904 * g_file_info_set_edit_name:
1905 * @info: a #GFileInfo.
1906 * @edit_name: a string containing an edit name.
1908 * Sets the edit name for the current file.
1909 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1911 void
1912 g_file_info_set_edit_name (GFileInfo *info,
1913 const char *edit_name)
1915 static guint32 attr = 0;
1916 GFileAttributeValue *value;
1918 g_return_if_fail (G_IS_FILE_INFO (info));
1919 g_return_if_fail (edit_name != NULL);
1921 if (attr == 0)
1922 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1924 value = g_file_info_create_value (info, attr);
1925 if (value)
1926 _g_file_attribute_value_set_string (value, edit_name);
1930 * g_file_info_set_icon:
1931 * @info: a #GFileInfo.
1932 * @icon: a #GIcon.
1934 * Sets the icon for a given #GFileInfo.
1935 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1937 void
1938 g_file_info_set_icon (GFileInfo *info,
1939 GIcon *icon)
1941 static guint32 attr = 0;
1942 GFileAttributeValue *value;
1944 g_return_if_fail (G_IS_FILE_INFO (info));
1945 g_return_if_fail (G_IS_ICON (icon));
1947 if (attr == 0)
1948 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1950 value = g_file_info_create_value (info, attr);
1951 if (value)
1952 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1956 * g_file_info_set_content_type:
1957 * @info: a #GFileInfo.
1958 * @content_type: a content type. See <link linkend="gio-GContentType">GContentType</link>.
1960 * Sets the content type attribute for a given #GFileInfo.
1961 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1963 void
1964 g_file_info_set_content_type (GFileInfo *info,
1965 const char *content_type)
1967 static guint32 attr = 0;
1968 GFileAttributeValue *value;
1970 g_return_if_fail (G_IS_FILE_INFO (info));
1971 g_return_if_fail (content_type != NULL);
1973 if (attr == 0)
1974 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1976 value = g_file_info_create_value (info, attr);
1977 if (value)
1978 _g_file_attribute_value_set_string (value, content_type);
1982 * g_file_info_set_size:
1983 * @info: a #GFileInfo.
1984 * @size: a #goffset containing the file's size.
1986 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1987 * to the given size.
1989 void
1990 g_file_info_set_size (GFileInfo *info,
1991 goffset size)
1993 static guint32 attr = 0;
1994 GFileAttributeValue *value;
1996 g_return_if_fail (G_IS_FILE_INFO (info));
1998 if (attr == 0)
1999 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
2001 value = g_file_info_create_value (info, attr);
2002 if (value)
2003 _g_file_attribute_value_set_uint64 (value, size);
2007 * g_file_info_set_modification_time
2008 * @info: a #GFileInfo.
2009 * @mtime: a #GTimeVal.
2011 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
2012 * info to the given time value.
2014 void
2015 g_file_info_set_modification_time (GFileInfo *info,
2016 GTimeVal *mtime)
2018 static guint32 attr_mtime = 0, attr_mtime_usec;
2019 GFileAttributeValue *value;
2021 g_return_if_fail (G_IS_FILE_INFO (info));
2022 g_return_if_fail (mtime != NULL);
2024 if (attr_mtime == 0)
2026 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
2027 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2030 value = g_file_info_create_value (info, attr_mtime);
2031 if (value)
2032 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
2033 value = g_file_info_create_value (info, attr_mtime_usec);
2034 if (value)
2035 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
2039 * g_file_info_set_symlink_target:
2040 * @info: a #GFileInfo.
2041 * @symlink_target: a static string containing a path to a symlink target.
2043 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
2044 * to the given symlink target.
2046 void
2047 g_file_info_set_symlink_target (GFileInfo *info,
2048 const char *symlink_target)
2050 static guint32 attr = 0;
2051 GFileAttributeValue *value;
2053 g_return_if_fail (G_IS_FILE_INFO (info));
2054 g_return_if_fail (symlink_target != NULL);
2056 if (attr == 0)
2057 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2059 value = g_file_info_create_value (info, attr);
2060 if (value)
2061 _g_file_attribute_value_set_byte_string (value, symlink_target);
2065 * g_file_info_set_sort_order:
2066 * @info: a #GFileInfo.
2067 * @sort_order: a sort order integer.
2069 * Sets the sort order attribute in the file info structure. See
2070 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
2072 void
2073 g_file_info_set_sort_order (GFileInfo *info,
2074 gint32 sort_order)
2076 static guint32 attr = 0;
2077 GFileAttributeValue *value;
2079 g_return_if_fail (G_IS_FILE_INFO (info));
2081 if (attr == 0)
2082 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
2084 value = g_file_info_create_value (info, attr);
2085 if (value)
2086 _g_file_attribute_value_set_int32 (value, sort_order);
2090 typedef struct {
2091 guint32 id;
2092 guint32 mask;
2093 } SubMatcher;
2095 struct _GFileAttributeMatcher {
2096 gboolean all;
2097 gint ref;
2099 GArray *sub_matchers;
2101 /* Interator */
2102 guint32 iterator_ns;
2103 gint iterator_pos;
2106 G_DEFINE_BOXED_TYPE (GFileAttributeMatcher, g_file_attribute_matcher,
2107 g_file_attribute_matcher_ref,
2108 g_file_attribute_matcher_unref)
2110 static gint
2111 compare_sub_matchers (gconstpointer a,
2112 gconstpointer b)
2114 const SubMatcher *suba = a;
2115 const SubMatcher *subb = b;
2116 int diff;
2118 diff = suba->id - subb->id;
2120 if (diff)
2121 return diff;
2123 return suba->mask - subb->mask;
2126 static gboolean
2127 sub_matcher_matches (SubMatcher *matcher,
2128 SubMatcher *submatcher)
2130 if ((matcher->mask & submatcher->mask) != matcher->mask)
2131 return FALSE;
2133 return matcher->id == (submatcher->id & matcher->mask);
2136 /* Call this function after modifying a matcher.
2137 * It will ensure all the invariants other functions rely on.
2139 static GFileAttributeMatcher *
2140 matcher_optimize (GFileAttributeMatcher *matcher)
2142 SubMatcher *submatcher, *compare;
2143 guint i, j;
2145 /* remove sub_matchers if we match everything anyway */
2146 if (matcher->all)
2148 if (matcher->sub_matchers)
2150 g_array_free (matcher->sub_matchers, TRUE);
2151 matcher->sub_matchers = NULL;
2153 return matcher;
2156 if (matcher->sub_matchers->len == 0)
2158 g_file_attribute_matcher_unref (matcher);
2159 return NULL;
2162 /* sort sub_matchers by id (and then mask), so we can bsearch
2163 * and compare matchers in O(N) instead of O(N²) */
2164 g_array_sort (matcher->sub_matchers, compare_sub_matchers);
2166 /* remove duplicates and specific matches when we match the whole namespace */
2167 j = 0;
2168 compare = &g_array_index (matcher->sub_matchers, SubMatcher, j);
2170 for (i = 1; i < matcher->sub_matchers->len; i++)
2172 submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2173 if (sub_matcher_matches (compare, submatcher))
2174 continue;
2176 j++;
2177 compare++;
2179 if (j < i)
2180 *compare = *submatcher;
2183 g_array_set_size (matcher->sub_matchers, j + 1);
2185 return matcher;
2189 * g_file_attribute_matcher_new:
2190 * @attributes: an attribute string to match.
2192 * Creates a new file attribute matcher, which matches attributes
2193 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
2194 * counted structures, and are created with a reference count of 1. If
2195 * the number of references falls to 0, the #GFileAttributeMatcher is
2196 * automatically destroyed.
2198 * The @attribute string should be formatted with specific keys separated
2199 * from namespaces with a double colon. Several "namespace::key" strings may be
2200 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
2201 * The wildcard "*" may be used to match all keys and namespaces, or
2202 * "namespace::*" will match all keys in a given namespace.
2204 * Examples of strings to use:
2205 * <table>
2206 * <title>File Attribute Matcher strings and results</title>
2207 * <tgroup cols='2' align='left'><thead>
2208 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
2209 * <tbody>
2210 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
2211 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
2212 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
2213 * all keys in the unix namespace.</entry></row>
2214 * </tbody></tgroup>
2215 * </table>
2217 * Returns: a #GFileAttributeMatcher.
2219 GFileAttributeMatcher *
2220 g_file_attribute_matcher_new (const char *attributes)
2222 char **split;
2223 char *colon;
2224 int i;
2225 GFileAttributeMatcher *matcher;
2227 if (attributes == NULL || *attributes == '\0')
2228 return NULL;
2230 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2231 matcher->ref = 1;
2232 matcher->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2234 split = g_strsplit (attributes, ",", -1);
2236 for (i = 0; split[i] != NULL; i++)
2238 if (strcmp (split[i], "*") == 0)
2239 matcher->all = TRUE;
2240 else
2242 SubMatcher s;
2244 colon = strstr (split[i], "::");
2245 if (colon != NULL &&
2246 !(colon[2] == 0 ||
2247 (colon[2] == '*' &&
2248 colon[3] == 0)))
2250 s.id = lookup_attribute (split[i]);
2251 s.mask = 0xffffffff;
2253 else
2255 if (colon)
2256 *colon = 0;
2258 s.id = lookup_namespace (split[i]) << NS_POS;
2259 s.mask = NS_MASK << NS_POS;
2262 g_array_append_val (matcher->sub_matchers, s);
2266 g_strfreev (split);
2268 matcher = matcher_optimize (matcher);
2270 return matcher;
2274 * g_file_attribute_matcher_subtract:
2275 * @matcher: Matcher to subtract from
2276 * @subtract: The matcher to subtract
2278 * Subtracts all attributes of @subtract from @matcher and returns
2279 * a matcher that supports those attributes.
2281 * Note that currently it is not possible to remove a single
2282 * attribute when the @matcher matches the whole namespace - or remove
2283 * a namespace or attribute when the matcher matches everything. This
2284 * is a limitation of the current implementation, but may be fixed
2285 * in the future.
2287 * Returns: A file attribute matcher matching all attributes of
2288 * @matcher that are not matched by @subtract
2290 GFileAttributeMatcher *
2291 g_file_attribute_matcher_subtract (GFileAttributeMatcher *matcher,
2292 GFileAttributeMatcher *subtract)
2294 GFileAttributeMatcher *result;
2295 guint mi, si;
2296 SubMatcher *msub, *ssub;
2298 if (matcher == NULL)
2299 return NULL;
2300 if (subtract == NULL)
2301 return g_file_attribute_matcher_ref (matcher);
2302 if (subtract->all)
2303 return NULL;
2304 if (matcher->all)
2305 return g_file_attribute_matcher_ref (matcher);
2307 result = g_malloc0 (sizeof (GFileAttributeMatcher));
2308 result->ref = 1;
2309 result->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2311 si = 0;
2312 g_assert (subtract->sub_matchers->len > 0);
2313 ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);
2315 for (mi = 0; mi < matcher->sub_matchers->len; mi++)
2317 msub = &g_array_index (matcher->sub_matchers, SubMatcher, mi);
2319 retry:
2320 if (sub_matcher_matches (ssub, msub))
2321 continue;
2323 si++;
2324 if (si >= subtract->sub_matchers->len)
2325 break;
2327 ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);
2328 if (ssub->id <= msub->id)
2329 goto retry;
2331 g_array_append_val (result->sub_matchers, *msub);
2334 if (mi < matcher->sub_matchers->len)
2335 g_array_append_vals (result->sub_matchers,
2336 &g_array_index (matcher->sub_matchers, SubMatcher, mi),
2337 matcher->sub_matchers->len - mi);
2339 result = matcher_optimize (result);
2341 return result;
2345 * g_file_attribute_matcher_ref:
2346 * @matcher: a #GFileAttributeMatcher.
2348 * References a file attribute matcher.
2350 * Returns: a #GFileAttributeMatcher.
2352 GFileAttributeMatcher *
2353 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2355 if (matcher)
2357 g_return_val_if_fail (matcher->ref > 0, NULL);
2358 g_atomic_int_inc (&matcher->ref);
2360 return matcher;
2364 * g_file_attribute_matcher_unref:
2365 * @matcher: a #GFileAttributeMatcher.
2367 * Unreferences @matcher. If the reference count falls below 1,
2368 * the @matcher is automatically freed.
2371 void
2372 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2374 if (matcher)
2376 g_return_if_fail (matcher->ref > 0);
2378 if (g_atomic_int_dec_and_test (&matcher->ref))
2380 if (matcher->sub_matchers)
2381 g_array_free (matcher->sub_matchers, TRUE);
2383 g_free (matcher);
2389 * g_file_attribute_matcher_matches_only:
2390 * @matcher: a #GFileAttributeMatcher.
2391 * @attribute: a file attribute key.
2393 * Checks if a attribute matcher only matches a given attribute. Always
2394 * returns %FALSE if "*" was used when creating the matcher.
2396 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2398 gboolean
2399 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2400 const char *attribute)
2402 SubMatcher *sub_matcher;
2403 guint32 id;
2405 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2407 if (matcher == NULL ||
2408 matcher->all)
2409 return FALSE;
2411 if (matcher->sub_matchers->len != 1)
2412 return FALSE;
2414 id = lookup_attribute (attribute);
2416 sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, 0);
2418 return sub_matcher->id == id &&
2419 sub_matcher->mask == 0xffffffff;
2422 static gboolean
2423 matcher_matches_id (GFileAttributeMatcher *matcher,
2424 guint32 id)
2426 SubMatcher *sub_matchers;
2427 int i;
2429 if (matcher->sub_matchers)
2431 sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
2432 for (i = 0; i < matcher->sub_matchers->len; i++)
2434 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2435 return TRUE;
2439 return FALSE;
2442 gboolean
2443 _g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2444 guint32 id)
2446 /* We return a NULL matcher for an empty match string, so handle this */
2447 if (matcher == NULL)
2448 return FALSE;
2450 if (matcher->all)
2451 return TRUE;
2453 return matcher_matches_id (matcher, id);
2457 * g_file_attribute_matcher_matches:
2458 * @matcher: a #GFileAttributeMatcher.
2459 * @attribute: a file attribute key.
2461 * Checks if an attribute will be matched by an attribute matcher. If
2462 * the matcher was created with the "*" matching string, this function
2463 * will always return %TRUE.
2465 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2467 gboolean
2468 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2469 const char *attribute)
2471 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2473 /* We return a NULL matcher for an empty match string, so handle this */
2474 if (matcher == NULL)
2475 return FALSE;
2477 if (matcher->all)
2478 return TRUE;
2480 return matcher_matches_id (matcher, lookup_attribute (attribute));
2483 /* return TRUE -> all */
2485 * g_file_attribute_matcher_enumerate_namespace:
2486 * @matcher: a #GFileAttributeMatcher.
2487 * @ns: a string containing a file attribute namespace.
2489 * Checks if the matcher will match all of the keys in a given namespace.
2490 * This will always return %TRUE if a wildcard character is in use (e.g. if
2491 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2492 * using "*" and namespace is anything.)
2494 * TODO: this is awkwardly worded.
2496 * Returns: %TRUE if the matcher matches all of the entries
2497 * in the given @ns, %FALSE otherwise.
2499 gboolean
2500 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2501 const char *ns)
2503 SubMatcher *sub_matchers;
2504 int ns_id;
2505 int i;
2507 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2509 /* We return a NULL matcher for an empty match string, so handle this */
2510 if (matcher == NULL)
2511 return FALSE;
2513 if (matcher->all)
2514 return TRUE;
2516 ns_id = lookup_namespace (ns) << NS_POS;
2518 if (matcher->sub_matchers)
2520 sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
2521 for (i = 0; i < matcher->sub_matchers->len; i++)
2523 if (sub_matchers[i].id == ns_id)
2524 return TRUE;
2528 matcher->iterator_ns = ns_id;
2529 matcher->iterator_pos = 0;
2531 return FALSE;
2535 * g_file_attribute_matcher_enumerate_next:
2536 * @matcher: a #GFileAttributeMatcher.
2538 * Gets the next matched attribute from a #GFileAttributeMatcher.
2540 * Returns: a string containing the next attribute or %NULL if
2541 * no more attribute exist.
2543 const char *
2544 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2546 int i;
2547 SubMatcher *sub_matcher;
2549 /* We return a NULL matcher for an empty match string, so handle this */
2550 if (matcher == NULL)
2551 return NULL;
2553 while (1)
2555 i = matcher->iterator_pos++;
2557 if (matcher->sub_matchers == NULL)
2558 return NULL;
2560 if (i < matcher->sub_matchers->len)
2561 sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2562 else
2563 return NULL;
2565 if (sub_matcher->mask == 0xffffffff &&
2566 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2567 return get_attribute_for_id (sub_matcher->id);
2572 * g_file_attribute_matcher_to_string:
2573 * @matcher: (allow-none): a #GFileAttributeMatcher.
2575 * Prints what the matcher is matching against. The format will be
2576 * equal to the format passed to g_file_attribute_matcher_new().
2577 * The output however, might not be identical, as the matcher may
2578 * decide to use a different order or omit needless parts.
2580 * Returns: a string describing the attributes the matcher matches
2581 * against or %NULL if @matcher was %NULL.
2583 * Since: 2.32
2585 char *
2586 g_file_attribute_matcher_to_string (GFileAttributeMatcher *matcher)
2588 GString *string;
2589 guint i;
2591 if (matcher == NULL)
2592 return NULL;
2594 if (matcher->all)
2595 return g_strdup ("*");
2597 string = g_string_new ("");
2598 for (i = 0; i < matcher->sub_matchers->len; i++)
2600 SubMatcher *submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2602 if (i > 0)
2603 g_string_append_c (string, ',');
2605 g_string_append (string, get_attribute_for_id (submatcher->id));
2608 return g_string_free (string, FALSE);