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>
25 * @short_description: File Information and Attributes
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.
32 * #GFileAttributeMatcher allows for searching through a #GFileInfo for
40 #include "gfileinfo.h"
41 #include "gfileattribute-priv.h"
46 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
47 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
51 GFileAttributeValue value;
56 GObject parent_instance;
59 GFileAttributeMatcher *mask;
62 struct _GFileInfoClass
64 GObjectClass parent_class;
68 static gboolean g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
71 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
75 guint32 attribute_id_counter;
78 G_LOCK_DEFINE_STATIC (attribute_hash);
79 static int namespace_id_counter = 0;
80 static GHashTable *ns_hash = NULL;
81 static GHashTable *attribute_hash = NULL;
82 static char ***attributes = NULL;
84 /* Attribute ids are 32bit, we split it up like this:
85 * |------------|--------------------|
87 * namespace attribute id
89 * This way the attributes gets sorted in namespace order
93 #define NS_MASK ((guint32)((1<<12) - 1))
95 #define ID_MASK ((guint32)((1<<20) - 1))
97 #define GET_NS(_attr_id) \
98 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
99 #define GET_ID(_attr_id) \
100 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
102 #define MAKE_ATTR_ID(_ns, _id) \
103 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
104 ((((guint32) _id) & ID_MASK) << ID_POS) )
107 _lookup_namespace (const char *namespace)
111 ns_info = g_hash_table_lookup (ns_hash, namespace);
114 ns_info = g_new0 (NSInfo, 1);
115 ns_info->id = ++namespace_id_counter;
116 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
117 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
118 attributes[ns_info->id] = NULL;
124 lookup_namespace (const char *namespace)
129 G_LOCK (attribute_hash);
131 if (attribute_hash == NULL)
133 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
134 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
137 ns_info = _lookup_namespace (namespace);
142 G_UNLOCK (attribute_hash);
148 get_attribute_for_id (int attribute)
151 G_LOCK (attribute_hash);
152 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
153 G_UNLOCK (attribute_hash);
158 lookup_attribute (const char *attribute)
165 G_LOCK (attribute_hash);
166 if (attribute_hash == NULL)
168 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
169 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
172 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
176 G_UNLOCK (attribute_hash);
180 colon = strstr (attribute, "::");
182 ns = g_strndup (attribute, colon - attribute);
186 ns_info = _lookup_namespace (ns);
189 id = ++ns_info->attribute_id_counter;
190 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
191 attributes[ns_info->id][id] = g_strdup (attribute);
193 attr_id = MAKE_ATTR_ID (ns_info->id, id);
195 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
197 G_UNLOCK (attribute_hash);
203 g_file_info_finalize (GObject *object)
207 GFileAttribute *attrs;
209 info = G_FILE_INFO (object);
211 attrs = (GFileAttribute *)info->attributes->data;
212 for (i = 0; i < info->attributes->len; i++)
213 _g_file_attribute_value_clear (&attrs[i].value);
214 g_array_free (info->attributes, TRUE);
216 if (info->mask != NO_ATTRIBUTE_MASK)
217 g_file_attribute_matcher_unref (info->mask);
219 if (G_OBJECT_CLASS (g_file_info_parent_class)->finalize)
220 (*G_OBJECT_CLASS (g_file_info_parent_class)->finalize) (object);
224 g_file_info_class_init (GFileInfoClass *klass)
226 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
228 gobject_class->finalize = g_file_info_finalize;
232 g_file_info_init (GFileInfo *info)
234 info->mask = NO_ATTRIBUTE_MASK;
235 info->attributes = g_array_new (FALSE, FALSE,
236 sizeof (GFileAttribute));
242 * Creates a new file info structure.
244 * Returns: a #GFileInfo.
247 g_file_info_new (void)
249 return g_object_new (G_TYPE_FILE_INFO, NULL);
253 * g_file_info_copy_into:
254 * @src_info: source to copy attributes from.
255 * @dest_info: destination to copy attributes to.
257 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
260 g_file_info_copy_into (GFileInfo *src_info,
261 GFileInfo *dest_info)
263 GFileAttribute *source, *dest;
266 g_return_if_fail (G_IS_FILE_INFO (src_info));
267 g_return_if_fail (G_IS_FILE_INFO (dest_info));
269 dest = (GFileAttribute *)dest_info->attributes->data;
270 for (i = 0; i < dest_info->attributes->len; i++)
271 _g_file_attribute_value_clear (&dest[i].value);
273 g_array_set_size (dest_info->attributes,
274 src_info->attributes->len);
276 source = (GFileAttribute *)src_info->attributes->data;
277 dest = (GFileAttribute *)dest_info->attributes->data;
279 for (i = 0; i < src_info->attributes->len; i++)
281 dest[i].attribute = source[i].attribute;
282 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
283 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
286 if (src_info->mask == NO_ATTRIBUTE_MASK)
287 dest_info->mask = NO_ATTRIBUTE_MASK;
289 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
294 * @other: a #GFileInfo.
296 * Duplicates a file info structure.
298 * Returns: a duplicate #GFileInfo of @other.
301 g_file_info_dup (GFileInfo *other)
305 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
307 new = g_file_info_new ();
308 g_file_info_copy_into (other, new);
313 * g_file_info_set_attribute_mask:
314 * @info: a #GFileInfo.
315 * @mask: a #GFileAttributeMatcher.
317 * Sets @mask on @info to match specific attribute types.
320 g_file_info_set_attribute_mask (GFileInfo *info,
321 GFileAttributeMatcher *mask)
323 GFileAttribute *attr;
326 g_return_if_fail (G_IS_FILE_INFO (info));
327 g_return_if_fail (mask != NULL);
329 if (mask != info->mask)
331 if (info->mask != NO_ATTRIBUTE_MASK)
332 g_file_attribute_matcher_unref (info->mask);
333 info->mask = g_file_attribute_matcher_ref (mask);
335 /* Remove non-matching attributes */
336 for (i = 0; i < info->attributes->len; i++)
338 attr = &g_array_index (info->attributes, GFileAttribute, i);
339 if (!g_file_attribute_matcher_matches_id (mask,
342 _g_file_attribute_value_clear (&attr->value);
343 g_array_remove_index (info->attributes, i);
351 * g_file_info_unset_attribute_mask:
354 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
358 g_file_info_unset_attribute_mask (GFileInfo *info)
360 g_return_if_fail (G_IS_FILE_INFO (info));
362 if (info->mask != NO_ATTRIBUTE_MASK)
363 g_file_attribute_matcher_unref (info->mask);
364 info->mask = NO_ATTRIBUTE_MASK;
368 * g_file_info_clear_status:
369 * @info: a #GFileInfo.
371 * Clears the status information from @info.
374 g_file_info_clear_status (GFileInfo *info)
376 GFileAttribute *attrs;
379 g_return_if_fail (G_IS_FILE_INFO (info));
381 attrs = (GFileAttribute *)info->attributes->data;
382 for (i = 0; i < info->attributes->len; i++)
383 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
387 g_file_info_find_place (GFileInfo *info,
391 GFileAttribute *attrs;
392 /* Binary search for the place where attribute would be, if its
396 max = info->attributes->len;
398 attrs = (GFileAttribute *)info->attributes->data;
402 med = min + (max - min) / 2;
403 if (attrs[med].attribute == attribute)
408 else if (attrs[med].attribute < attribute)
410 else /* attrs[med].attribute > attribute */
417 static GFileAttributeValue *
418 g_file_info_find_value (GFileInfo *info,
421 GFileAttribute *attrs;
424 i = g_file_info_find_place (info, attr_id);
425 attrs = (GFileAttribute *)info->attributes->data;
426 if (i < info->attributes->len &&
427 attrs[i].attribute == attr_id)
428 return &attrs[i].value;
433 static GFileAttributeValue *
434 g_file_info_find_value_by_name (GFileInfo *info,
435 const char *attribute)
439 attr_id = lookup_attribute (attribute);
440 return g_file_info_find_value (info, attr_id);
444 * g_file_info_has_attribute:
445 * @info: a #GFileInfo.
446 * @attribute: a file attribute key.
448 * Checks if a file info structure has an attribute named @attribute.
450 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
454 g_file_info_has_attribute (GFileInfo *info,
455 const char *attribute)
457 GFileAttributeValue *value;
459 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
460 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
462 value = g_file_info_find_value_by_name (info, attribute);
463 return value != NULL;
467 * g_file_info_list_attributes:
468 * @info: a #GFileInfo.
469 * @name_space: a file attribute key's namespace.
471 * Lists the file info structure's attributes.
473 * Returns: a null-terminated array of strings of all of the
474 * possible attribute types for the given @name_space, or
478 g_file_info_list_attributes (GFileInfo *info,
479 const char *name_space)
482 GFileAttribute *attrs;
484 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
487 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
489 names = g_ptr_array_new ();
490 attrs = (GFileAttribute *)info->attributes->data;
491 for (i = 0; i < info->attributes->len; i++)
493 attribute = attrs[i].attribute;
494 if (ns_id == 0 || GET_NS (attribute) == ns_id)
495 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
499 g_ptr_array_add (names, NULL);
501 return (char **)g_ptr_array_free (names, FALSE);
505 * g_file_info_get_attribute_type:
506 * @info: a #GFileInfo.
507 * @attribute: a file attribute key.
509 * Gets the attribute type for an attribute key.
511 * Returns: a #GFileAttributeType for the given @attribute, or
512 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is invalid.
515 g_file_info_get_attribute_type (GFileInfo *info,
516 const char *attribute)
518 GFileAttributeValue *value;
520 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
521 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
523 value = g_file_info_find_value_by_name (info, attribute);
527 return G_FILE_ATTRIBUTE_TYPE_INVALID;
531 * g_file_info_remove_attribute:
532 * @info: a #GFileInfo.
533 * @attribute: a file attribute key.
535 * Removes all cases of @attribute from @info if it exists.
538 g_file_info_remove_attribute (GFileInfo *info,
539 const char *attribute)
542 GFileAttribute *attrs;
545 g_return_if_fail (G_IS_FILE_INFO (info));
546 g_return_if_fail (attribute != NULL && *attribute != '\0');
548 attr_id = lookup_attribute (attribute);
550 i = g_file_info_find_place (info, attr_id);
551 attrs = (GFileAttribute *)info->attributes->data;
552 if (i < info->attributes->len &&
553 attrs[i].attribute == attr_id)
555 _g_file_attribute_value_clear (&attrs[i].value);
556 g_array_remove_index (info->attributes, i);
561 * g_file_info_get_attribute_data:
562 * @info: a #GFileInfo
563 * @attribute: a file attribute key
564 * @type: return location for the attribute type, or %NULL
565 * @value_pp: return location for the attribute value, or %NULL
566 * @status: return location for the attribute status, or %NULL
568 * Gets the attribute type, value and status for an attribute key.
570 * Returns: %TRUE if @info has an attribute named @attribute,
574 g_file_info_get_attribute_data (GFileInfo *info,
575 const char *attribute,
576 GFileAttributeType *type,
578 GFileAttributeStatus *status)
580 GFileAttributeValue *value;
582 value = g_file_info_find_value_by_name (info, attribute);
587 *status = value->status;
593 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
599 * g_file_info_get_attribute_status:
600 * @info: a #GFileInfo
601 * @attribute: a file attribute key
603 * Gets the attribute status for an attribute key.
605 * Returns: a #GFileAttributeStatus for the given @attribute, or
606 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
610 g_file_info_get_attribute_status (GFileInfo *info,
611 const char *attribute)
613 GFileAttributeValue *val;
615 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
616 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
618 val = g_file_info_find_value_by_name (info, attribute);
622 return G_FILE_ATTRIBUTE_STATUS_UNSET;
626 GFileAttributeValue *
627 _g_file_info_get_attribute_value (GFileInfo *info,
628 const char *attribute)
631 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
632 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
634 return g_file_info_find_value_by_name (info, attribute);
638 * g_file_info_get_attribute_as_string:
639 * @info: a #GFileInfo.
640 * @attribute: a file attribute key.
642 * Gets the value of a attribute, formated as a string.
643 * This escapes things as needed to make the string valid
646 * Returns: a UTF-8 string associated with the given @attribute.
647 * When you're done with the string it must be freed with g_free().
650 g_file_info_get_attribute_as_string (GFileInfo *info,
651 const char *attribute)
653 GFileAttributeValue *val;
654 val = _g_file_info_get_attribute_value (info, attribute);
656 return _g_file_attribute_value_as_string (val);
662 * g_file_info_get_attribute_object:
663 * @info: a #GFileInfo.
664 * @attribute: a file attribute key.
666 * Gets the value of a #GObject attribute. If the attribute does
667 * not contain a #GObject, %NULL will be returned.
669 * Returns: a #GObject associated with the given @attribute, or
673 g_file_info_get_attribute_object (GFileInfo *info,
674 const char *attribute)
676 GFileAttributeValue *value;
678 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
679 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
681 value = g_file_info_find_value_by_name (info, attribute);
682 return _g_file_attribute_value_get_object (value);
686 * g_file_info_get_attribute_string:
687 * @info: a #GFileInfo.
688 * @attribute: a file attribute key.
690 * Gets the value of a string attribute. If the attribute does
691 * not contain a string, %NULL will be returned.
693 * Returns: the contents of the @attribute value as a string, or
697 g_file_info_get_attribute_string (GFileInfo *info,
698 const char *attribute)
700 GFileAttributeValue *value;
702 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
703 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
705 value = g_file_info_find_value_by_name (info, attribute);
706 return _g_file_attribute_value_get_string (value);
710 * g_file_info_get_attribute_byte_string:
711 * @info: a #GFileInfo.
712 * @attribute: a file attribute key.
714 * Gets the value of a byte string attribute. If the attribute does
715 * not contain a byte string, %NULL will be returned.
717 * Returns: the contents of the @attribute value as a byte string, or
721 g_file_info_get_attribute_byte_string (GFileInfo *info,
722 const char *attribute)
724 GFileAttributeValue *value;
726 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
727 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
729 value = g_file_info_find_value_by_name (info, attribute);
730 return _g_file_attribute_value_get_byte_string (value);
734 * g_file_info_get_attribute_boolean:
735 * @info: a #GFileInfo.
736 * @attribute: a file attribute key.
738 * Gets the value of a boolean attribute. If the attribute does not
739 * contain a boolean value, %FALSE will be returned.
741 * Returns: the boolean value contained within the attribute.
744 g_file_info_get_attribute_boolean (GFileInfo *info,
745 const char *attribute)
747 GFileAttributeValue *value;
749 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
750 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
752 value = g_file_info_find_value_by_name (info, attribute);
753 return _g_file_attribute_value_get_boolean (value);
757 * g_file_info_get_attribute_uint32:
758 * @info: a #GFileInfo.
759 * @attribute: a file attribute key.
761 * Gets an unsigned 32-bit integer contained within the attribute. If the
762 * attribute does not contain an unsigned 32-bit integer, or is invalid,
763 * 0 will be returned.
765 * Returns: an unsigned 32-bit integer from the attribute.
768 g_file_info_get_attribute_uint32 (GFileInfo *info,
769 const char *attribute)
771 GFileAttributeValue *value;
773 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
774 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
776 value = g_file_info_find_value_by_name (info, attribute);
777 return _g_file_attribute_value_get_uint32 (value);
781 * g_file_info_get_attribute_int32:
782 * @info: a #GFileInfo.
783 * @attribute: a file attribute key.
785 * Gets a signed 32-bit integer contained within the attribute. If the
786 * attribute does not contain a signed 32-bit integer, or is invalid,
787 * 0 will be returned.
789 * Returns: a signed 32-bit integer from the attribute.
792 g_file_info_get_attribute_int32 (GFileInfo *info,
793 const char *attribute)
795 GFileAttributeValue *value;
797 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
798 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
800 value = g_file_info_find_value_by_name (info, attribute);
801 return _g_file_attribute_value_get_int32 (value);
805 * g_file_info_get_attribute_uint64:
806 * @info: a #GFileInfo.
807 * @attribute: a file attribute key.
809 * Gets a unsigned 64-bit integer contained within the attribute. If the
810 * attribute does not contain an unsigned 64-bit integer, or is invalid,
811 * 0 will be returned.
813 * Returns: a unsigned 64-bit integer from the attribute.
816 g_file_info_get_attribute_uint64 (GFileInfo *info,
817 const char *attribute)
819 GFileAttributeValue *value;
821 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
822 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
824 value = g_file_info_find_value_by_name (info, attribute);
825 return _g_file_attribute_value_get_uint64 (value);
829 * g_file_info_get_attribute_int64:
830 * @info: a #GFileInfo.
831 * @attribute: a file attribute key.
833 * Gets a signed 64-bit integer contained within the attribute. If the
834 * attribute does not contain an signed 64-bit integer, or is invalid,
835 * 0 will be returned.
837 * Returns: a signed 64-bit integer from the attribute.
840 g_file_info_get_attribute_int64 (GFileInfo *info,
841 const char *attribute)
843 GFileAttributeValue *value;
845 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
846 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
848 value = g_file_info_find_value_by_name (info, attribute);
849 return _g_file_attribute_value_get_int64 (value);
852 static GFileAttributeValue *
853 g_file_info_create_value (GFileInfo *info,
856 GFileAttribute *attrs;
859 if (info->mask != NO_ATTRIBUTE_MASK &&
860 !g_file_attribute_matcher_matches_id (info->mask, attr_id))
863 i = g_file_info_find_place (info, attr_id);
865 attrs = (GFileAttribute *)info->attributes->data;
866 if (i < info->attributes->len &&
867 attrs[i].attribute == attr_id)
868 return &attrs[i].value;
871 GFileAttribute attr = { 0 };
872 attr.attribute = attr_id;
873 g_array_insert_val (info->attributes, i, attr);
875 attrs = (GFileAttribute *)info->attributes->data;
876 return &attrs[i].value;
880 static GFileAttributeValue *
881 g_file_info_create_value_by_name (GFileInfo *info,
882 const char *attribute)
886 attr_id = lookup_attribute (attribute);
888 return g_file_info_create_value (info, attr_id);
892 * g_file_info_set_attribute:
893 * @info: a #GFileInfo.
894 * @attribute: a file attribute key.
895 * @type: a #GFileAttributeType
896 * @value_p: pointer to the value
898 * Sets the @attribute to contain the given value, if possible.
901 g_file_info_set_attribute (GFileInfo *info,
902 const char *attribute,
903 GFileAttributeType type,
906 GFileAttributeValue *value;
908 g_return_if_fail (G_IS_FILE_INFO (info));
909 g_return_if_fail (attribute != NULL && *attribute != '\0');
911 value = g_file_info_create_value_by_name (info, attribute);
914 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
918 * g_file_info_set_attribute_object:
919 * @info: a #GFileInfo.
920 * @attribute: a file attribute key.
921 * @attr_value: a #GObject.
923 * Sets the @attribute to contain the given @attr_value,
927 g_file_info_set_attribute_object (GFileInfo *info,
928 const char *attribute,
931 GFileAttributeValue *value;
933 g_return_if_fail (G_IS_FILE_INFO (info));
934 g_return_if_fail (attribute != NULL && *attribute != '\0');
935 g_return_if_fail (G_IS_OBJECT (attr_value));
937 value = g_file_info_create_value_by_name (info, attribute);
939 _g_file_attribute_value_set_object (value, attr_value);
943 * g_file_info_set_attribute_string:
944 * @info: a #GFileInfo.
945 * @attribute: a file attribute key.
946 * @attr_value: a string.
948 * Sets the @attribute to contain the given @attr_value,
952 g_file_info_set_attribute_string (GFileInfo *info,
953 const char *attribute,
954 const char *attr_value)
956 GFileAttributeValue *value;
958 g_return_if_fail (G_IS_FILE_INFO (info));
959 g_return_if_fail (attribute != NULL && *attribute != '\0');
960 g_return_if_fail (attr_value != NULL);
962 value = g_file_info_create_value_by_name (info, attribute);
964 _g_file_attribute_value_set_string (value, attr_value);
968 * g_file_info_set_attribute_byte_string:
969 * @info: a #GFileInfo.
970 * @attribute: a file attribute key.
971 * @attr_value: a byte string.
973 * Sets the @attribute to contain the given @attr_value,
977 g_file_info_set_attribute_byte_string (GFileInfo *info,
978 const char *attribute,
979 const char *attr_value)
981 GFileAttributeValue *value;
983 g_return_if_fail (G_IS_FILE_INFO (info));
984 g_return_if_fail (attribute != NULL && *attribute != '\0');
985 g_return_if_fail (attr_value != NULL);
987 value = g_file_info_create_value_by_name (info, attribute);
989 _g_file_attribute_value_set_byte_string (value, attr_value);
993 * g_file_info_set_attribute_boolean:
994 * @info: a #GFileInfo.
995 * @attribute: a file attribute key.
996 * @attr_value: a boolean value.
998 * Sets the @attribute to contain the given @attr_value,
1002 g_file_info_set_attribute_boolean (GFileInfo *info,
1003 const char *attribute,
1004 gboolean attr_value)
1006 GFileAttributeValue *value;
1008 g_return_if_fail (G_IS_FILE_INFO (info));
1009 g_return_if_fail (attribute != NULL && *attribute != '\0');
1011 value = g_file_info_create_value_by_name (info, attribute);
1013 _g_file_attribute_value_set_boolean (value, attr_value);
1017 * g_file_info_set_attribute_uint32:
1018 * @info: a #GFileInfo.
1019 * @attribute: a file attribute key.
1020 * @attr_value: an unsigned 32-bit integer.
1022 * Sets the @attribute to contain the given @attr_value,
1026 g_file_info_set_attribute_uint32 (GFileInfo *info,
1027 const char *attribute,
1030 GFileAttributeValue *value;
1032 g_return_if_fail (G_IS_FILE_INFO (info));
1033 g_return_if_fail (attribute != NULL && *attribute != '\0');
1035 value = g_file_info_create_value_by_name (info, attribute);
1037 _g_file_attribute_value_set_uint32 (value, attr_value);
1042 * g_file_info_set_attribute_int32:
1043 * @info: a #GFileInfo.
1044 * @attribute: a file attribute key.
1045 * @attr_value: a signed 32-bit integer
1047 * Sets the @attribute to contain the given @attr_value,
1051 g_file_info_set_attribute_int32 (GFileInfo *info,
1052 const char *attribute,
1055 GFileAttributeValue *value;
1057 g_return_if_fail (G_IS_FILE_INFO (info));
1058 g_return_if_fail (attribute != NULL && *attribute != '\0');
1060 value = g_file_info_create_value_by_name (info, attribute);
1062 _g_file_attribute_value_set_int32 (value, attr_value);
1066 * g_file_info_set_attribute_uint64:
1067 * @info: a #GFileInfo.
1068 * @attribute: a file attribute key.
1069 * @attr_value: an unsigned 64-bit integer.
1071 * Sets the @attribute to contain the given @attr_value,
1075 g_file_info_set_attribute_uint64 (GFileInfo *info,
1076 const char *attribute,
1079 GFileAttributeValue *value;
1081 g_return_if_fail (G_IS_FILE_INFO (info));
1082 g_return_if_fail (attribute != NULL && *attribute != '\0');
1084 value = g_file_info_create_value_by_name (info, attribute);
1086 _g_file_attribute_value_set_uint64 (value, attr_value);
1090 * g_file_info_set_attribute_int64:
1091 * @info: a #GFileInfo.
1092 * @attribute: attribute name to set.
1093 * @attr_value: int64 value to set attribute to.
1095 * Sets the @attribute to contain the given @attr_value,
1100 g_file_info_set_attribute_int64 (GFileInfo *info,
1101 const char *attribute,
1104 GFileAttributeValue *value;
1106 g_return_if_fail (G_IS_FILE_INFO (info));
1107 g_return_if_fail (attribute != NULL && *attribute != '\0');
1109 value = g_file_info_create_value_by_name (info, attribute);
1111 _g_file_attribute_value_set_int64 (value, attr_value);
1114 /* Helper getters */
1116 * g_file_info_get_file_type:
1117 * @info: a #GFileInfo.
1119 * Gets a file's type (whether it is a regular file, symlink, etc).
1120 * This is different from the file's content type, see g_file_info_get_content_type().
1122 * Returns: a #GFileType for the given file.
1125 g_file_info_get_file_type (GFileInfo *info)
1127 static guint32 attr = 0;
1128 GFileAttributeValue *value;
1130 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1133 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1135 value = g_file_info_find_value (info, attr);
1136 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1140 * g_file_info_get_is_hidden:
1141 * @info: a #GFileInfo.
1143 * Checks if a file is hidden.
1145 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1148 g_file_info_get_is_hidden (GFileInfo *info)
1150 static guint32 attr = 0;
1151 GFileAttributeValue *value;
1153 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1156 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1158 value = g_file_info_find_value (info, attr);
1159 return (GFileType)_g_file_attribute_value_get_boolean (value);
1163 * g_file_info_get_is_backup:
1164 * @info: a #GFileInfo.
1166 * Checks if a file is a backup file.
1168 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1171 g_file_info_get_is_backup (GFileInfo *info)
1173 static guint32 attr = 0;
1174 GFileAttributeValue *value;
1176 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1179 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1181 value = g_file_info_find_value (info, attr);
1182 return (GFileType)_g_file_attribute_value_get_boolean (value);
1186 * g_file_info_get_is_symlink:
1187 * @info: a #GFileInfo.
1189 * Checks if a file is a symlink.
1191 * Returns: %TRUE if the given @info is a symlink.
1194 g_file_info_get_is_symlink (GFileInfo *info)
1196 static guint32 attr = 0;
1197 GFileAttributeValue *value;
1199 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1202 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1204 value = g_file_info_find_value (info, attr);
1205 return (GFileType)_g_file_attribute_value_get_boolean (value);
1209 * g_file_info_get_name:
1210 * @info: a #GFileInfo.
1212 * Gets the name for a file.
1214 * Returns: a string containing the file name.
1217 g_file_info_get_name (GFileInfo *info)
1219 static guint32 attr = 0;
1220 GFileAttributeValue *value;
1222 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1225 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1227 value = g_file_info_find_value (info, attr);
1228 return _g_file_attribute_value_get_byte_string (value);
1232 * g_file_info_get_display_name:
1233 * @info: a #GFileInfo.
1235 * Gets a display name for a file.
1237 * Returns: a string containing the display name.
1240 g_file_info_get_display_name (GFileInfo *info)
1242 static guint32 attr = 0;
1243 GFileAttributeValue *value;
1245 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1248 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1250 value = g_file_info_find_value (info, attr);
1251 return _g_file_attribute_value_get_string (value);
1255 * g_file_info_get_edit_name:
1256 * @info: a #GFileInfo.
1258 * Gets the edit name for a file.
1260 * Returns: a string containing the edit name.
1263 g_file_info_get_edit_name (GFileInfo *info)
1265 static guint32 attr = 0;
1266 GFileAttributeValue *value;
1268 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1271 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1273 value = g_file_info_find_value (info, attr);
1274 return _g_file_attribute_value_get_string (value);
1278 * g_file_info_get_icon:
1279 * @info: a #GFileInfo.
1281 * Gets the icon for a file.
1283 * Returns: #GIcon for the given @info.
1286 g_file_info_get_icon (GFileInfo *info)
1288 static guint32 attr = 0;
1289 GFileAttributeValue *value;
1292 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1295 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1297 value = g_file_info_find_value (info, attr);
1298 obj = _g_file_attribute_value_get_object (value);
1299 if (obj != NULL && G_IS_ICON (obj))
1300 return G_ICON (obj);
1305 * g_file_info_get_content_type:
1306 * @info: a #GFileInfo.
1308 * Gets the file's content type.
1310 * Returns: a string containing the file's content type.s
1313 g_file_info_get_content_type (GFileInfo *info)
1315 static guint32 attr = 0;
1316 GFileAttributeValue *value;
1318 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1321 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1323 value = g_file_info_find_value (info, attr);
1324 return _g_file_attribute_value_get_string (value);
1328 * g_file_info_get_size:
1329 * @info: a #GFileInfo.
1331 * Gets the file's size.
1333 * Returns: a #goffset containing the file's size.
1336 g_file_info_get_size (GFileInfo *info)
1338 static guint32 attr = 0;
1339 GFileAttributeValue *value;
1341 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1344 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1346 value = g_file_info_find_value (info, attr);
1347 return (goffset) _g_file_attribute_value_get_uint64 (value);
1351 * g_file_info_get_modification_time:
1352 * @info: a #GFileInfo.
1353 * @result: a #GTimeVal.
1355 * Gets the modification time of the current @info and sets it
1359 g_file_info_get_modification_time (GFileInfo *info,
1362 static guint32 attr_mtime = 0, attr_mtime_usec;
1363 GFileAttributeValue *value;
1365 g_return_if_fail (G_IS_FILE_INFO (info));
1366 g_return_if_fail (result != NULL);
1368 if (attr_mtime == 0)
1370 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1371 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1374 value = g_file_info_find_value (info, attr_mtime);
1375 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1376 value = g_file_info_find_value (info, attr_mtime_usec);
1377 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1381 * g_file_info_get_symlink_target:
1382 * @info: a #GFileInfo.
1384 * Gets the symlink target for a given #GFileInfo.
1386 * Returns: a string containing the symlink target.
1389 g_file_info_get_symlink_target (GFileInfo *info)
1391 static guint32 attr = 0;
1392 GFileAttributeValue *value;
1394 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1397 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1399 value = g_file_info_find_value (info, attr);
1400 return _g_file_attribute_value_get_byte_string (value);
1404 * g_file_info_get_etag:
1405 * @info: a #GFileInfo.
1407 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1408 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1410 * Returns: a string containing the value of the "etag:value" attribute.
1413 g_file_info_get_etag (GFileInfo *info)
1415 static guint32 attr = 0;
1416 GFileAttributeValue *value;
1418 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1421 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1423 value = g_file_info_find_value (info, attr);
1424 return _g_file_attribute_value_get_string (value);
1428 * g_file_info_get_sort_order:
1429 * @info: a #GFileInfo.
1431 * Gets the value of the sort_order attribute from the #GFileInfo.
1432 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1434 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1437 g_file_info_get_sort_order (GFileInfo *info)
1439 static guint32 attr = 0;
1440 GFileAttributeValue *value;
1442 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1445 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1447 value = g_file_info_find_value (info, attr);
1448 return _g_file_attribute_value_get_int32 (value);
1451 /* Helper setters: */
1453 * g_file_info_set_file_type:
1454 * @info: a #GFileInfo.
1455 * @type: a #GFileType.
1457 * Sets the file type in a #GFileInfo to @type.
1458 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1461 g_file_info_set_file_type (GFileInfo *info,
1464 static guint32 attr = 0;
1465 GFileAttributeValue *value;
1467 g_return_if_fail (G_IS_FILE_INFO (info));
1470 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1472 value = g_file_info_create_value (info, attr);
1474 _g_file_attribute_value_set_uint32 (value, type);
1478 * g_file_info_set_is_hidden:
1479 * @info: a #GFileInfo.
1480 * @is_hidden: a #gboolean.
1482 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1483 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1486 g_file_info_set_is_hidden (GFileInfo *info,
1489 static guint32 attr = 0;
1490 GFileAttributeValue *value;
1492 g_return_if_fail (G_IS_FILE_INFO (info));
1495 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1497 value = g_file_info_create_value (info, attr);
1499 _g_file_attribute_value_set_boolean (value, is_hidden);
1503 * g_file_info_set_is_symlink:
1504 * @info: a #GFileInfo.
1505 * @is_symlink: a #gboolean.
1507 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1508 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1511 g_file_info_set_is_symlink (GFileInfo *info,
1512 gboolean is_symlink)
1514 static guint32 attr = 0;
1515 GFileAttributeValue *value;
1517 g_return_if_fail (G_IS_FILE_INFO (info));
1520 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1522 value = g_file_info_create_value (info, attr);
1524 _g_file_attribute_value_set_boolean (value, is_symlink);
1528 * g_file_info_set_name:
1529 * @info: a #GFileInfo.
1530 * @name: a string containing a name.
1532 * Sets the name attribute for the current #GFileInfo.
1533 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1536 g_file_info_set_name (GFileInfo *info,
1539 static guint32 attr = 0;
1540 GFileAttributeValue *value;
1542 g_return_if_fail (G_IS_FILE_INFO (info));
1543 g_return_if_fail (name != NULL);
1546 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1548 value = g_file_info_create_value (info, attr);
1550 _g_file_attribute_value_set_byte_string (value, name);
1554 * g_file_info_set_display_name:
1555 * @info: a #GFileInfo.
1556 * @display_name: a string containing a display name.
1558 * Sets the display name for the current #GFileInfo.
1559 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1562 g_file_info_set_display_name (GFileInfo *info,
1563 const char *display_name)
1565 static guint32 attr = 0;
1566 GFileAttributeValue *value;
1568 g_return_if_fail (G_IS_FILE_INFO (info));
1569 g_return_if_fail (display_name != NULL);
1572 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1574 value = g_file_info_create_value (info, attr);
1576 _g_file_attribute_value_set_string (value, display_name);
1580 * g_file_info_set_edit_name:
1581 * @info: a #GFileInfo.
1582 * @edit_name: a string containing an edit name.
1584 * Sets the edit name for the current file.
1585 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1588 g_file_info_set_edit_name (GFileInfo *info,
1589 const char *edit_name)
1591 static guint32 attr = 0;
1592 GFileAttributeValue *value;
1594 g_return_if_fail (G_IS_FILE_INFO (info));
1595 g_return_if_fail (edit_name != NULL);
1598 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1600 value = g_file_info_create_value (info, attr);
1602 _g_file_attribute_value_set_string (value, edit_name);
1606 * g_file_info_set_icon:
1607 * @info: a #GFileInfo.
1610 * Sets the icon for a given #GFileInfo.
1611 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1614 g_file_info_set_icon (GFileInfo *info,
1617 static guint32 attr = 0;
1618 GFileAttributeValue *value;
1620 g_return_if_fail (G_IS_FILE_INFO (info));
1621 g_return_if_fail (G_IS_ICON (icon));
1624 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1626 value = g_file_info_create_value (info, attr);
1628 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1632 * g_file_info_set_content_type:
1633 * @info: a #GFileInfo.
1634 * @content_type: a content type. See #GContentType.
1636 * Sets the content type attribute for a given #GFileInfo.
1637 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1640 g_file_info_set_content_type (GFileInfo *info,
1641 const char *content_type)
1643 static guint32 attr = 0;
1644 GFileAttributeValue *value;
1646 g_return_if_fail (G_IS_FILE_INFO (info));
1647 g_return_if_fail (content_type != NULL);
1650 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1652 value = g_file_info_create_value (info, attr);
1654 _g_file_attribute_value_set_string (value, content_type);
1658 * g_file_info_set_size:
1659 * @info: a #GFileInfo.
1660 * @size: a #goffset containing the file's size.
1662 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1663 * to the given size.
1666 g_file_info_set_size (GFileInfo *info,
1669 static guint32 attr = 0;
1670 GFileAttributeValue *value;
1672 g_return_if_fail (G_IS_FILE_INFO (info));
1675 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1677 value = g_file_info_create_value (info, attr);
1679 _g_file_attribute_value_set_uint64 (value, size);
1683 * g_file_info_set_modification_time
1684 * @info: a #GFileInfo.
1685 * @mtime: a #GTimeVal.
1687 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
1688 * info to the given time value.
1691 g_file_info_set_modification_time (GFileInfo *info,
1694 static guint32 attr_mtime = 0, attr_mtime_usec;
1695 GFileAttributeValue *value;
1697 g_return_if_fail (G_IS_FILE_INFO (info));
1698 g_return_if_fail (mtime != NULL);
1700 if (attr_mtime == 0)
1702 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1703 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1706 value = g_file_info_create_value (info, attr_mtime);
1708 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
1709 value = g_file_info_create_value (info, attr_mtime_usec);
1711 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
1715 * g_file_info_set_symlink_target:
1716 * @info: a #GFileInfo.
1717 * @symlink_target: a static string containing a path to a symlink target.
1719 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
1720 * to the given symlink target.
1723 g_file_info_set_symlink_target (GFileInfo *info,
1724 const char *symlink_target)
1726 static guint32 attr = 0;
1727 GFileAttributeValue *value;
1729 g_return_if_fail (G_IS_FILE_INFO (info));
1730 g_return_if_fail (symlink_target != NULL);
1733 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1735 value = g_file_info_create_value (info, attr);
1737 _g_file_attribute_value_set_byte_string (value, symlink_target);
1741 * g_file_info_set_sort_order:
1742 * @info: a #GFileInfo.
1743 * @sort_order: a sort order integer.
1745 * Sets the sort order attribute in the file info structure. See
1746 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1749 g_file_info_set_sort_order (GFileInfo *info,
1752 static guint32 attr = 0;
1753 GFileAttributeValue *value;
1755 g_return_if_fail (G_IS_FILE_INFO (info));
1758 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1760 value = g_file_info_create_value (info, attr);
1762 _g_file_attribute_value_set_int32 (value, sort_order);
1766 #define ON_STACK_MATCHERS 5
1773 struct _GFileAttributeMatcher {
1775 SubMatcher sub_matchers[ON_STACK_MATCHERS];
1776 GArray *more_sub_matchers;
1779 guint32 iterator_ns;
1785 matcher_add (GFileAttributeMatcher *matcher,
1789 SubMatcher *sub_matchers;
1793 for (i = 0; i < ON_STACK_MATCHERS; i++)
1795 /* First empty spot, not found, use this */
1796 if (matcher->sub_matchers[i].id == 0)
1798 matcher->sub_matchers[i].id = id;
1799 matcher->sub_matchers[i].mask = mask;
1804 if (matcher->sub_matchers[i].id == id &&
1805 matcher->sub_matchers[i].mask == mask)
1809 if (matcher->more_sub_matchers == NULL)
1810 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
1812 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
1813 for (i = 0; i < matcher->more_sub_matchers->len; i++)
1816 if (sub_matchers[i].id == id &&
1817 sub_matchers[i].mask == mask)
1824 g_array_append_val (matcher->more_sub_matchers, s);
1828 * g_file_attribute_matcher_new:
1829 * @attributes: an attribute string to match.
1831 * Creates a new file attribute matcher, which matches attributes
1832 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
1833 * counted structures, and are created with a reference count of 1. If
1834 * the number of references falls to 0, the #GFileAttributeMatcher is
1835 * automatically destroyed.
1837 * The @attribute string should be formatted with specific keys separated
1838 * from namespaces with a double colon. Several "namespace::key" strings may be
1839 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
1840 * The wildcard "*" may be used to match all keys and namespaces, or
1841 * "namespace::*" will match all keys in a given namespace.
1843 * Examples of strings to use:
1845 * <title>File Attribute Matcher strings and results</title>
1846 * <tgroup cols='2' align='left'><thead>
1847 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
1849 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
1850 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
1851 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
1852 * all keys in the unix namespace.</entry></row>
1856 * Returns: a #GFileAttributeMatcher.
1858 GFileAttributeMatcher *
1859 g_file_attribute_matcher_new (const char *attributes)
1864 GFileAttributeMatcher *matcher;
1866 if (attributes == NULL || *attributes == '\0')
1869 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
1872 split = g_strsplit (attributes, ",", -1);
1874 for (i = 0; split[i] != NULL; i++)
1876 if (strcmp (split[i], "*") == 0)
1877 matcher->all = TRUE;
1882 colon = strstr (split[i], "::");
1883 if (colon != NULL &&
1888 id = lookup_attribute (split[i]);
1896 id = lookup_namespace (split[i]) << NS_POS;
1897 mask = NS_MASK << NS_POS;
1900 matcher_add (matcher, id, mask);
1910 * g_file_attribute_matcher_ref:
1911 * @matcher: a #GFileAttributeMatcher.
1913 * References a file attribute matcher.
1915 * Returns: a #GFileAttributeMatcher.
1917 GFileAttributeMatcher *
1918 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
1920 g_return_val_if_fail (matcher != NULL, NULL);
1921 g_return_val_if_fail (matcher->ref > 0, NULL);
1923 g_atomic_int_inc (&matcher->ref);
1929 * g_file_attribute_matcher_unref:
1930 * @matcher: a #GFileAttributeMatcher.
1932 * Unreferences @matcher. If the reference count falls below 1,
1933 * the @matcher is automatically freed.
1937 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
1939 g_return_if_fail (matcher != NULL);
1940 g_return_if_fail (matcher->ref > 0);
1942 if (g_atomic_int_dec_and_test (&matcher->ref))
1944 if (matcher->more_sub_matchers)
1945 g_array_free (matcher->more_sub_matchers, TRUE);
1952 * g_file_attribute_matcher_matches_only:
1953 * @matcher: a #GFileAttributeMatcher.
1954 * @attribute: a file attribute key.
1956 * Checks if a attribute matcher only matches a given attribute. Always
1957 * returns %FALSE if "*" was used when creating the matcher.
1959 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
1962 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
1963 const char *attribute)
1967 g_return_val_if_fail (matcher != NULL, FALSE);
1968 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
1973 id = lookup_attribute (attribute);
1975 if (matcher->sub_matchers[0].id != 0 &&
1976 matcher->sub_matchers[1].id == 0 &&
1977 matcher->sub_matchers[0].mask == 0xffffffff &&
1978 matcher->sub_matchers[0].id == id)
1985 matcher_matches_id (GFileAttributeMatcher *matcher,
1988 SubMatcher *sub_matchers;
1991 for (i = 0; i < ON_STACK_MATCHERS; i++)
1993 if (matcher->sub_matchers[i].id == 0)
1996 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2000 if (matcher->more_sub_matchers)
2002 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2003 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2005 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2014 g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2017 g_return_val_if_fail (matcher != NULL, FALSE);
2022 return matcher_matches_id (matcher, id);
2026 * g_file_attribute_matcher_matches:
2027 * @matcher: a #GFileAttributeMatcher.
2028 * @attribute: a file attribute key.
2030 * Checks if an attribute will be matched by an attribute matcher. If
2031 * the matcher was created with the "*" matching string, this function
2032 * will always return %TRUE.
2034 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2037 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2038 const char *attribute)
2040 g_return_val_if_fail (matcher != NULL, FALSE);
2041 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2046 return matcher_matches_id (matcher, lookup_attribute (attribute));
2049 /* return TRUE -> all */
2051 * g_file_attribute_matcher_enumerate_namespace:
2052 * @matcher: a #GFileAttributeMatcher.
2053 * @ns: a string containing a file attribute namespace.
2055 * Checks if the matcher will match all of the keys in a given namespace.
2056 * This will always return %TRUE if a wildcard character is in use (e.g. if
2057 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2058 * using "*" and namespace is anything.)
2060 * TODO: this is awkwardly worded.
2062 * Returns: %TRUE if the matcher matches all of the entries
2063 * in the given @ns, %FALSE otherwise.
2066 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2069 SubMatcher *sub_matchers;
2073 g_return_val_if_fail (matcher != NULL, FALSE);
2074 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2079 ns_id = lookup_namespace (ns) << NS_POS;
2081 for (i = 0; i < ON_STACK_MATCHERS; i++)
2083 if (matcher->sub_matchers[i].id == ns_id)
2087 if (matcher->more_sub_matchers)
2089 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2090 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2092 if (sub_matchers[i].id == ns_id)
2097 matcher->iterator_ns = ns_id;
2098 matcher->iterator_pos = 0;
2104 * g_file_attribute_matcher_enumerate_next:
2105 * @matcher: a #GFileAttributeMatcher.
2107 * Gets the next matched attribute from a #GFileAttributeMatcher.
2109 * Returns: a string containing the next attribute or %NULL if
2110 * no more attribute exist.
2113 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2116 SubMatcher *sub_matcher;
2118 g_return_val_if_fail (matcher != NULL, NULL);
2122 i = matcher->iterator_pos++;
2124 if (i < ON_STACK_MATCHERS)
2126 if (matcher->sub_matchers[i].id == 0)
2129 sub_matcher = &matcher->sub_matchers[i];
2133 if (matcher->more_sub_matchers == NULL)
2136 i -= ON_STACK_MATCHERS;
2137 if (i < matcher->more_sub_matchers->len)
2138 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2143 if (sub_matcher->mask == 0xffffffff &&
2144 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2145 return get_attribute_for_id (sub_matcher->id);
2149 #define __G_FILE_INFO_C__
2150 #include "gioaliasdef.c"