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
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
59 #include "gfileinfo.h"
60 #include "gfileattribute-priv.h"
66 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
67 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
71 GFileAttributeValue value;
76 GObject parent_instance;
79 GFileAttributeMatcher *mask;
82 struct _GFileInfoClass
84 GObjectClass parent_class;
88 static gboolean g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
91 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
95 guint32 attribute_id_counter;
98 G_LOCK_DEFINE_STATIC (attribute_hash);
99 static int namespace_id_counter = 0;
100 static GHashTable *ns_hash = NULL;
101 static GHashTable *attribute_hash = NULL;
102 static char ***attributes = NULL;
104 /* Attribute ids are 32bit, we split it up like this:
105 * |------------|--------------------|
107 * namespace attribute id
109 * This way the attributes gets sorted in namespace order
113 #define NS_MASK ((guint32)((1<<12) - 1))
115 #define ID_MASK ((guint32)((1<<20) - 1))
117 #define GET_NS(_attr_id) \
118 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
119 #define GET_ID(_attr_id) \
120 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
122 #define MAKE_ATTR_ID(_ns, _id) \
123 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
124 ((((guint32) _id) & ID_MASK) << ID_POS) )
127 _lookup_namespace (const char *namespace)
131 ns_info = g_hash_table_lookup (ns_hash, namespace);
134 ns_info = g_new0 (NSInfo, 1);
135 ns_info->id = ++namespace_id_counter;
136 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
137 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
138 attributes[ns_info->id] = NULL;
144 lookup_namespace (const char *namespace)
149 G_LOCK (attribute_hash);
151 if (attribute_hash == NULL)
153 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
154 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
157 ns_info = _lookup_namespace (namespace);
162 G_UNLOCK (attribute_hash);
168 get_attribute_for_id (int attribute)
171 G_LOCK (attribute_hash);
172 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
173 G_UNLOCK (attribute_hash);
178 lookup_attribute (const char *attribute)
185 G_LOCK (attribute_hash);
186 if (attribute_hash == NULL)
188 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
189 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
192 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
196 G_UNLOCK (attribute_hash);
200 colon = strstr (attribute, "::");
202 ns = g_strndup (attribute, colon - attribute);
206 ns_info = _lookup_namespace (ns);
209 id = ++ns_info->attribute_id_counter;
210 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
211 attributes[ns_info->id][id] = g_strdup (attribute);
213 attr_id = MAKE_ATTR_ID (ns_info->id, id);
215 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
217 G_UNLOCK (attribute_hash);
223 g_file_info_finalize (GObject *object)
227 GFileAttribute *attrs;
229 info = G_FILE_INFO (object);
231 attrs = (GFileAttribute *)info->attributes->data;
232 for (i = 0; i < info->attributes->len; i++)
233 _g_file_attribute_value_clear (&attrs[i].value);
234 g_array_free (info->attributes, TRUE);
236 if (info->mask != NO_ATTRIBUTE_MASK)
237 g_file_attribute_matcher_unref (info->mask);
239 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
243 g_file_info_class_init (GFileInfoClass *klass)
245 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
247 gobject_class->finalize = g_file_info_finalize;
251 g_file_info_init (GFileInfo *info)
253 info->mask = NO_ATTRIBUTE_MASK;
254 info->attributes = g_array_new (FALSE, FALSE,
255 sizeof (GFileAttribute));
261 * Creates a new file info structure.
263 * Returns: a #GFileInfo.
266 g_file_info_new (void)
268 return g_object_new (G_TYPE_FILE_INFO, NULL);
272 * g_file_info_copy_into:
273 * @src_info: source to copy attributes from.
274 * @dest_info: destination to copy attributes to.
276 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
279 g_file_info_copy_into (GFileInfo *src_info,
280 GFileInfo *dest_info)
282 GFileAttribute *source, *dest;
285 g_return_if_fail (G_IS_FILE_INFO (src_info));
286 g_return_if_fail (G_IS_FILE_INFO (dest_info));
288 dest = (GFileAttribute *)dest_info->attributes->data;
289 for (i = 0; i < dest_info->attributes->len; i++)
290 _g_file_attribute_value_clear (&dest[i].value);
292 g_array_set_size (dest_info->attributes,
293 src_info->attributes->len);
295 source = (GFileAttribute *)src_info->attributes->data;
296 dest = (GFileAttribute *)dest_info->attributes->data;
298 for (i = 0; i < src_info->attributes->len; i++)
300 dest[i].attribute = source[i].attribute;
301 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
302 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
305 if (dest_info->mask != NO_ATTRIBUTE_MASK)
306 g_file_attribute_matcher_unref (dest_info->mask);
308 if (src_info->mask == NO_ATTRIBUTE_MASK)
309 dest_info->mask = NO_ATTRIBUTE_MASK;
311 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
316 * @other: a #GFileInfo.
318 * Duplicates a file info structure.
320 * Returns: a duplicate #GFileInfo of @other.
323 g_file_info_dup (GFileInfo *other)
327 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
329 new = g_file_info_new ();
330 g_file_info_copy_into (other, new);
335 * g_file_info_set_attribute_mask:
336 * @info: a #GFileInfo.
337 * @mask: a #GFileAttributeMatcher.
339 * Sets @mask on @info to match specific attribute types.
342 g_file_info_set_attribute_mask (GFileInfo *info,
343 GFileAttributeMatcher *mask)
345 GFileAttribute *attr;
348 g_return_if_fail (G_IS_FILE_INFO (info));
350 if (mask != info->mask)
352 if (info->mask != NO_ATTRIBUTE_MASK)
353 g_file_attribute_matcher_unref (info->mask);
354 info->mask = g_file_attribute_matcher_ref (mask);
356 /* Remove non-matching attributes */
357 for (i = 0; i < info->attributes->len; i++)
359 attr = &g_array_index (info->attributes, GFileAttribute, i);
360 if (!g_file_attribute_matcher_matches_id (mask,
363 _g_file_attribute_value_clear (&attr->value);
364 g_array_remove_index (info->attributes, i);
372 * g_file_info_unset_attribute_mask:
375 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
379 g_file_info_unset_attribute_mask (GFileInfo *info)
381 g_return_if_fail (G_IS_FILE_INFO (info));
383 if (info->mask != NO_ATTRIBUTE_MASK)
384 g_file_attribute_matcher_unref (info->mask);
385 info->mask = NO_ATTRIBUTE_MASK;
389 * g_file_info_clear_status:
390 * @info: a #GFileInfo.
392 * Clears the status information from @info.
395 g_file_info_clear_status (GFileInfo *info)
397 GFileAttribute *attrs;
400 g_return_if_fail (G_IS_FILE_INFO (info));
402 attrs = (GFileAttribute *)info->attributes->data;
403 for (i = 0; i < info->attributes->len; i++)
404 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
408 g_file_info_find_place (GFileInfo *info,
412 GFileAttribute *attrs;
413 /* Binary search for the place where attribute would be, if it's
417 max = info->attributes->len;
419 attrs = (GFileAttribute *)info->attributes->data;
423 med = min + (max - min) / 2;
424 if (attrs[med].attribute == attribute)
429 else if (attrs[med].attribute < attribute)
431 else /* attrs[med].attribute > attribute */
438 static GFileAttributeValue *
439 g_file_info_find_value (GFileInfo *info,
442 GFileAttribute *attrs;
445 i = g_file_info_find_place (info, attr_id);
446 attrs = (GFileAttribute *)info->attributes->data;
447 if (i < info->attributes->len &&
448 attrs[i].attribute == attr_id)
449 return &attrs[i].value;
454 static GFileAttributeValue *
455 g_file_info_find_value_by_name (GFileInfo *info,
456 const char *attribute)
460 attr_id = lookup_attribute (attribute);
461 return g_file_info_find_value (info, attr_id);
465 * g_file_info_has_attribute:
466 * @info: a #GFileInfo.
467 * @attribute: a file attribute key.
469 * Checks if a file info structure has an attribute named @attribute.
471 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
475 g_file_info_has_attribute (GFileInfo *info,
476 const char *attribute)
478 GFileAttributeValue *value;
480 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
481 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
483 value = g_file_info_find_value_by_name (info, attribute);
484 return value != NULL;
488 * g_file_info_list_attributes:
489 * @info: a #GFileInfo.
490 * @name_space: a file attribute key's namespace.
492 * Lists the file info structure's attributes.
494 * Returns: a null-terminated array of strings of all of the
495 * possible attribute types for the given @name_space, or
499 g_file_info_list_attributes (GFileInfo *info,
500 const char *name_space)
503 GFileAttribute *attrs;
505 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
508 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
510 names = g_ptr_array_new ();
511 attrs = (GFileAttribute *)info->attributes->data;
512 for (i = 0; i < info->attributes->len; i++)
514 attribute = attrs[i].attribute;
515 if (ns_id == 0 || GET_NS (attribute) == ns_id)
516 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
520 g_ptr_array_add (names, NULL);
522 return (char **)g_ptr_array_free (names, FALSE);
526 * g_file_info_get_attribute_type:
527 * @info: a #GFileInfo.
528 * @attribute: a file attribute key.
530 * Gets the attribute type for an attribute key.
532 * Returns: a #GFileAttributeType for the given @attribute, or
533 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is invalid.
536 g_file_info_get_attribute_type (GFileInfo *info,
537 const char *attribute)
539 GFileAttributeValue *value;
541 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
542 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
544 value = g_file_info_find_value_by_name (info, attribute);
548 return G_FILE_ATTRIBUTE_TYPE_INVALID;
552 * g_file_info_remove_attribute:
553 * @info: a #GFileInfo.
554 * @attribute: a file attribute key.
556 * Removes all cases of @attribute from @info if it exists.
559 g_file_info_remove_attribute (GFileInfo *info,
560 const char *attribute)
563 GFileAttribute *attrs;
566 g_return_if_fail (G_IS_FILE_INFO (info));
567 g_return_if_fail (attribute != NULL && *attribute != '\0');
569 attr_id = lookup_attribute (attribute);
571 i = g_file_info_find_place (info, attr_id);
572 attrs = (GFileAttribute *)info->attributes->data;
573 if (i < info->attributes->len &&
574 attrs[i].attribute == attr_id)
576 _g_file_attribute_value_clear (&attrs[i].value);
577 g_array_remove_index (info->attributes, i);
582 * g_file_info_get_attribute_data:
583 * @info: a #GFileInfo
584 * @attribute: a file attribute key
585 * @type: return location for the attribute type, or %NULL
586 * @value_pp: return location for the attribute value, or %NULL
587 * @status: return location for the attribute status, or %NULL
589 * Gets the attribute type, value and status for an attribute key.
591 * Returns: %TRUE if @info has an attribute named @attribute,
595 g_file_info_get_attribute_data (GFileInfo *info,
596 const char *attribute,
597 GFileAttributeType *type,
599 GFileAttributeStatus *status)
601 GFileAttributeValue *value;
603 value = g_file_info_find_value_by_name (info, attribute);
608 *status = value->status;
614 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
620 * g_file_info_get_attribute_status:
621 * @info: a #GFileInfo
622 * @attribute: a file attribute key
624 * Gets the attribute status for an attribute key.
626 * Returns: a #GFileAttributeStatus for the given @attribute, or
627 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
631 g_file_info_get_attribute_status (GFileInfo *info,
632 const char *attribute)
634 GFileAttributeValue *val;
636 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
637 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
639 val = g_file_info_find_value_by_name (info, attribute);
643 return G_FILE_ATTRIBUTE_STATUS_UNSET;
647 GFileAttributeValue *
648 _g_file_info_get_attribute_value (GFileInfo *info,
649 const char *attribute)
652 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
653 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
655 return g_file_info_find_value_by_name (info, attribute);
659 * g_file_info_get_attribute_as_string:
660 * @info: a #GFileInfo.
661 * @attribute: a file attribute key.
663 * Gets the value of a attribute, formated as a string.
664 * This escapes things as needed to make the string valid
667 * Returns: a UTF-8 string associated with the given @attribute.
668 * When you're done with the string it must be freed with g_free().
671 g_file_info_get_attribute_as_string (GFileInfo *info,
672 const char *attribute)
674 GFileAttributeValue *val;
675 val = _g_file_info_get_attribute_value (info, attribute);
677 return _g_file_attribute_value_as_string (val);
683 * g_file_info_get_attribute_object:
684 * @info: a #GFileInfo.
685 * @attribute: a file attribute key.
687 * Gets the value of a #GObject attribute. If the attribute does
688 * not contain a #GObject, %NULL will be returned.
690 * Returns: a #GObject associated with the given @attribute, or
694 g_file_info_get_attribute_object (GFileInfo *info,
695 const char *attribute)
697 GFileAttributeValue *value;
699 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
700 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
702 value = g_file_info_find_value_by_name (info, attribute);
703 return _g_file_attribute_value_get_object (value);
707 * g_file_info_get_attribute_string:
708 * @info: a #GFileInfo.
709 * @attribute: a file attribute key.
711 * Gets the value of a string attribute. If the attribute does
712 * not contain a string, %NULL will be returned.
714 * Returns: the contents of the @attribute value as a string, or
718 g_file_info_get_attribute_string (GFileInfo *info,
719 const char *attribute)
721 GFileAttributeValue *value;
723 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
724 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
726 value = g_file_info_find_value_by_name (info, attribute);
727 return _g_file_attribute_value_get_string (value);
731 * g_file_info_get_attribute_byte_string:
732 * @info: a #GFileInfo.
733 * @attribute: a file attribute key.
735 * Gets the value of a byte string attribute. If the attribute does
736 * not contain a byte string, %NULL will be returned.
738 * Returns: the contents of the @attribute value as a byte string, or
742 g_file_info_get_attribute_byte_string (GFileInfo *info,
743 const char *attribute)
745 GFileAttributeValue *value;
747 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
748 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
750 value = g_file_info_find_value_by_name (info, attribute);
751 return _g_file_attribute_value_get_byte_string (value);
755 * g_file_info_get_attribute_boolean:
756 * @info: a #GFileInfo.
757 * @attribute: a file attribute key.
759 * Gets the value of a boolean attribute. If the attribute does not
760 * contain a boolean value, %FALSE will be returned.
762 * Returns: the boolean value contained within the attribute.
765 g_file_info_get_attribute_boolean (GFileInfo *info,
766 const char *attribute)
768 GFileAttributeValue *value;
770 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
771 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
773 value = g_file_info_find_value_by_name (info, attribute);
774 return _g_file_attribute_value_get_boolean (value);
778 * g_file_info_get_attribute_uint32:
779 * @info: a #GFileInfo.
780 * @attribute: a file attribute key.
782 * Gets an unsigned 32-bit integer contained within the attribute. If the
783 * attribute does not contain an unsigned 32-bit integer, or is invalid,
784 * 0 will be returned.
786 * Returns: an unsigned 32-bit integer from the attribute.
789 g_file_info_get_attribute_uint32 (GFileInfo *info,
790 const char *attribute)
792 GFileAttributeValue *value;
794 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
795 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
797 value = g_file_info_find_value_by_name (info, attribute);
798 return _g_file_attribute_value_get_uint32 (value);
802 * g_file_info_get_attribute_int32:
803 * @info: a #GFileInfo.
804 * @attribute: a file attribute key.
806 * Gets a signed 32-bit integer contained within the attribute. If the
807 * attribute does not contain a signed 32-bit integer, or is invalid,
808 * 0 will be returned.
810 * Returns: a signed 32-bit integer from the attribute.
813 g_file_info_get_attribute_int32 (GFileInfo *info,
814 const char *attribute)
816 GFileAttributeValue *value;
818 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
819 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
821 value = g_file_info_find_value_by_name (info, attribute);
822 return _g_file_attribute_value_get_int32 (value);
826 * g_file_info_get_attribute_uint64:
827 * @info: a #GFileInfo.
828 * @attribute: a file attribute key.
830 * Gets a unsigned 64-bit integer contained within the attribute. If the
831 * attribute does not contain an unsigned 64-bit integer, or is invalid,
832 * 0 will be returned.
834 * Returns: a unsigned 64-bit integer from the attribute.
837 g_file_info_get_attribute_uint64 (GFileInfo *info,
838 const char *attribute)
840 GFileAttributeValue *value;
842 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
843 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
845 value = g_file_info_find_value_by_name (info, attribute);
846 return _g_file_attribute_value_get_uint64 (value);
850 * g_file_info_get_attribute_int64:
851 * @info: a #GFileInfo.
852 * @attribute: a file attribute key.
854 * Gets a signed 64-bit integer contained within the attribute. If the
855 * attribute does not contain an signed 64-bit integer, or is invalid,
856 * 0 will be returned.
858 * Returns: a signed 64-bit integer from the attribute.
861 g_file_info_get_attribute_int64 (GFileInfo *info,
862 const char *attribute)
864 GFileAttributeValue *value;
866 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
867 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
869 value = g_file_info_find_value_by_name (info, attribute);
870 return _g_file_attribute_value_get_int64 (value);
873 static GFileAttributeValue *
874 g_file_info_create_value (GFileInfo *info,
877 GFileAttribute *attrs;
880 if (info->mask != NO_ATTRIBUTE_MASK &&
881 !g_file_attribute_matcher_matches_id (info->mask, attr_id))
884 i = g_file_info_find_place (info, attr_id);
886 attrs = (GFileAttribute *)info->attributes->data;
887 if (i < info->attributes->len &&
888 attrs[i].attribute == attr_id)
889 return &attrs[i].value;
892 GFileAttribute attr = { 0 };
893 attr.attribute = attr_id;
894 g_array_insert_val (info->attributes, i, attr);
896 attrs = (GFileAttribute *)info->attributes->data;
897 return &attrs[i].value;
901 static GFileAttributeValue *
902 g_file_info_create_value_by_name (GFileInfo *info,
903 const char *attribute)
907 attr_id = lookup_attribute (attribute);
909 return g_file_info_create_value (info, attr_id);
913 * g_file_info_set_attribute:
914 * @info: a #GFileInfo.
915 * @attribute: a file attribute key.
916 * @type: a #GFileAttributeType
917 * @value_p: pointer to the value
919 * Sets the @attribute to contain the given value, if possible.
922 g_file_info_set_attribute (GFileInfo *info,
923 const char *attribute,
924 GFileAttributeType type,
927 GFileAttributeValue *value;
929 g_return_if_fail (G_IS_FILE_INFO (info));
930 g_return_if_fail (attribute != NULL && *attribute != '\0');
932 value = g_file_info_create_value_by_name (info, attribute);
935 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
939 * g_file_info_set_attribute_object:
940 * @info: a #GFileInfo.
941 * @attribute: a file attribute key.
942 * @attr_value: a #GObject.
944 * Sets the @attribute to contain the given @attr_value,
948 g_file_info_set_attribute_object (GFileInfo *info,
949 const char *attribute,
952 GFileAttributeValue *value;
954 g_return_if_fail (G_IS_FILE_INFO (info));
955 g_return_if_fail (attribute != NULL && *attribute != '\0');
956 g_return_if_fail (G_IS_OBJECT (attr_value));
958 value = g_file_info_create_value_by_name (info, attribute);
960 _g_file_attribute_value_set_object (value, attr_value);
964 * g_file_info_set_attribute_string:
965 * @info: a #GFileInfo.
966 * @attribute: a file attribute key.
967 * @attr_value: a string.
969 * Sets the @attribute to contain the given @attr_value,
973 g_file_info_set_attribute_string (GFileInfo *info,
974 const char *attribute,
975 const char *attr_value)
977 GFileAttributeValue *value;
979 g_return_if_fail (G_IS_FILE_INFO (info));
980 g_return_if_fail (attribute != NULL && *attribute != '\0');
981 g_return_if_fail (attr_value != NULL);
983 value = g_file_info_create_value_by_name (info, attribute);
985 _g_file_attribute_value_set_string (value, attr_value);
989 * g_file_info_set_attribute_byte_string:
990 * @info: a #GFileInfo.
991 * @attribute: a file attribute key.
992 * @attr_value: a byte string.
994 * Sets the @attribute to contain the given @attr_value,
998 g_file_info_set_attribute_byte_string (GFileInfo *info,
999 const char *attribute,
1000 const char *attr_value)
1002 GFileAttributeValue *value;
1004 g_return_if_fail (G_IS_FILE_INFO (info));
1005 g_return_if_fail (attribute != NULL && *attribute != '\0');
1006 g_return_if_fail (attr_value != NULL);
1008 value = g_file_info_create_value_by_name (info, attribute);
1010 _g_file_attribute_value_set_byte_string (value, attr_value);
1014 * g_file_info_set_attribute_boolean:
1015 * @info: a #GFileInfo.
1016 * @attribute: a file attribute key.
1017 * @attr_value: a boolean value.
1019 * Sets the @attribute to contain the given @attr_value,
1023 g_file_info_set_attribute_boolean (GFileInfo *info,
1024 const char *attribute,
1025 gboolean attr_value)
1027 GFileAttributeValue *value;
1029 g_return_if_fail (G_IS_FILE_INFO (info));
1030 g_return_if_fail (attribute != NULL && *attribute != '\0');
1032 value = g_file_info_create_value_by_name (info, attribute);
1034 _g_file_attribute_value_set_boolean (value, attr_value);
1038 * g_file_info_set_attribute_uint32:
1039 * @info: a #GFileInfo.
1040 * @attribute: a file attribute key.
1041 * @attr_value: an unsigned 32-bit integer.
1043 * Sets the @attribute to contain the given @attr_value,
1047 g_file_info_set_attribute_uint32 (GFileInfo *info,
1048 const char *attribute,
1051 GFileAttributeValue *value;
1053 g_return_if_fail (G_IS_FILE_INFO (info));
1054 g_return_if_fail (attribute != NULL && *attribute != '\0');
1056 value = g_file_info_create_value_by_name (info, attribute);
1058 _g_file_attribute_value_set_uint32 (value, attr_value);
1063 * g_file_info_set_attribute_int32:
1064 * @info: a #GFileInfo.
1065 * @attribute: a file attribute key.
1066 * @attr_value: a signed 32-bit integer
1068 * Sets the @attribute to contain the given @attr_value,
1072 g_file_info_set_attribute_int32 (GFileInfo *info,
1073 const char *attribute,
1076 GFileAttributeValue *value;
1078 g_return_if_fail (G_IS_FILE_INFO (info));
1079 g_return_if_fail (attribute != NULL && *attribute != '\0');
1081 value = g_file_info_create_value_by_name (info, attribute);
1083 _g_file_attribute_value_set_int32 (value, attr_value);
1087 * g_file_info_set_attribute_uint64:
1088 * @info: a #GFileInfo.
1089 * @attribute: a file attribute key.
1090 * @attr_value: an unsigned 64-bit integer.
1092 * Sets the @attribute to contain the given @attr_value,
1096 g_file_info_set_attribute_uint64 (GFileInfo *info,
1097 const char *attribute,
1100 GFileAttributeValue *value;
1102 g_return_if_fail (G_IS_FILE_INFO (info));
1103 g_return_if_fail (attribute != NULL && *attribute != '\0');
1105 value = g_file_info_create_value_by_name (info, attribute);
1107 _g_file_attribute_value_set_uint64 (value, attr_value);
1111 * g_file_info_set_attribute_int64:
1112 * @info: a #GFileInfo.
1113 * @attribute: attribute name to set.
1114 * @attr_value: int64 value to set attribute to.
1116 * Sets the @attribute to contain the given @attr_value,
1121 g_file_info_set_attribute_int64 (GFileInfo *info,
1122 const char *attribute,
1125 GFileAttributeValue *value;
1127 g_return_if_fail (G_IS_FILE_INFO (info));
1128 g_return_if_fail (attribute != NULL && *attribute != '\0');
1130 value = g_file_info_create_value_by_name (info, attribute);
1132 _g_file_attribute_value_set_int64 (value, attr_value);
1135 /* Helper getters */
1137 * g_file_info_get_file_type:
1138 * @info: a #GFileInfo.
1140 * Gets a file's type (whether it is a regular file, symlink, etc).
1141 * This is different from the file's content type, see g_file_info_get_content_type().
1143 * Returns: a #GFileType for the given file.
1146 g_file_info_get_file_type (GFileInfo *info)
1148 static guint32 attr = 0;
1149 GFileAttributeValue *value;
1151 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1154 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1156 value = g_file_info_find_value (info, attr);
1157 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1161 * g_file_info_get_is_hidden:
1162 * @info: a #GFileInfo.
1164 * Checks if a file is hidden.
1166 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1169 g_file_info_get_is_hidden (GFileInfo *info)
1171 static guint32 attr = 0;
1172 GFileAttributeValue *value;
1174 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1177 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1179 value = g_file_info_find_value (info, attr);
1180 return (GFileType)_g_file_attribute_value_get_boolean (value);
1184 * g_file_info_get_is_backup:
1185 * @info: a #GFileInfo.
1187 * Checks if a file is a backup file.
1189 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1192 g_file_info_get_is_backup (GFileInfo *info)
1194 static guint32 attr = 0;
1195 GFileAttributeValue *value;
1197 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1200 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1202 value = g_file_info_find_value (info, attr);
1203 return (GFileType)_g_file_attribute_value_get_boolean (value);
1207 * g_file_info_get_is_symlink:
1208 * @info: a #GFileInfo.
1210 * Checks if a file is a symlink.
1212 * Returns: %TRUE if the given @info is a symlink.
1215 g_file_info_get_is_symlink (GFileInfo *info)
1217 static guint32 attr = 0;
1218 GFileAttributeValue *value;
1220 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1223 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1225 value = g_file_info_find_value (info, attr);
1226 return (GFileType)_g_file_attribute_value_get_boolean (value);
1230 * g_file_info_get_name:
1231 * @info: a #GFileInfo.
1233 * Gets the name for a file.
1235 * Returns: a string containing the file name.
1238 g_file_info_get_name (GFileInfo *info)
1240 static guint32 attr = 0;
1241 GFileAttributeValue *value;
1243 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1246 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1248 value = g_file_info_find_value (info, attr);
1249 return _g_file_attribute_value_get_byte_string (value);
1253 * g_file_info_get_display_name:
1254 * @info: a #GFileInfo.
1256 * Gets a display name for a file.
1258 * Returns: a string containing the display name.
1261 g_file_info_get_display_name (GFileInfo *info)
1263 static guint32 attr = 0;
1264 GFileAttributeValue *value;
1266 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1269 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1271 value = g_file_info_find_value (info, attr);
1272 return _g_file_attribute_value_get_string (value);
1276 * g_file_info_get_edit_name:
1277 * @info: a #GFileInfo.
1279 * Gets the edit name for a file.
1281 * Returns: a string containing the edit name.
1284 g_file_info_get_edit_name (GFileInfo *info)
1286 static guint32 attr = 0;
1287 GFileAttributeValue *value;
1289 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1292 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1294 value = g_file_info_find_value (info, attr);
1295 return _g_file_attribute_value_get_string (value);
1299 * g_file_info_get_icon:
1300 * @info: a #GFileInfo.
1302 * Gets the icon for a file.
1304 * Returns: #GIcon for the given @info.
1307 g_file_info_get_icon (GFileInfo *info)
1309 static guint32 attr = 0;
1310 GFileAttributeValue *value;
1313 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1316 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1318 value = g_file_info_find_value (info, attr);
1319 obj = _g_file_attribute_value_get_object (value);
1320 if (G_IS_ICON (obj))
1321 return G_ICON (obj);
1326 * g_file_info_get_content_type:
1327 * @info: a #GFileInfo.
1329 * Gets the file's content type.
1331 * Returns: a string containing the file's content type.
1334 g_file_info_get_content_type (GFileInfo *info)
1336 static guint32 attr = 0;
1337 GFileAttributeValue *value;
1339 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1342 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1344 value = g_file_info_find_value (info, attr);
1345 return _g_file_attribute_value_get_string (value);
1349 * g_file_info_get_size:
1350 * @info: a #GFileInfo.
1352 * Gets the file's size.
1354 * Returns: a #goffset containing the file's size.
1357 g_file_info_get_size (GFileInfo *info)
1359 static guint32 attr = 0;
1360 GFileAttributeValue *value;
1362 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1365 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1367 value = g_file_info_find_value (info, attr);
1368 return (goffset) _g_file_attribute_value_get_uint64 (value);
1372 * g_file_info_get_modification_time:
1373 * @info: a #GFileInfo.
1374 * @result: a #GTimeVal.
1376 * Gets the modification time of the current @info and sets it
1380 g_file_info_get_modification_time (GFileInfo *info,
1383 static guint32 attr_mtime = 0, attr_mtime_usec;
1384 GFileAttributeValue *value;
1386 g_return_if_fail (G_IS_FILE_INFO (info));
1387 g_return_if_fail (result != NULL);
1389 if (attr_mtime == 0)
1391 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1392 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1395 value = g_file_info_find_value (info, attr_mtime);
1396 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1397 value = g_file_info_find_value (info, attr_mtime_usec);
1398 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1402 * g_file_info_get_symlink_target:
1403 * @info: a #GFileInfo.
1405 * Gets the symlink target for a given #GFileInfo.
1407 * Returns: a string containing the symlink target.
1410 g_file_info_get_symlink_target (GFileInfo *info)
1412 static guint32 attr = 0;
1413 GFileAttributeValue *value;
1415 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1418 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1420 value = g_file_info_find_value (info, attr);
1421 return _g_file_attribute_value_get_byte_string (value);
1425 * g_file_info_get_etag:
1426 * @info: a #GFileInfo.
1428 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1429 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1431 * Returns: a string containing the value of the "etag:value" attribute.
1434 g_file_info_get_etag (GFileInfo *info)
1436 static guint32 attr = 0;
1437 GFileAttributeValue *value;
1439 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1442 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1444 value = g_file_info_find_value (info, attr);
1445 return _g_file_attribute_value_get_string (value);
1449 * g_file_info_get_sort_order:
1450 * @info: a #GFileInfo.
1452 * Gets the value of the sort_order attribute from the #GFileInfo.
1453 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1455 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1458 g_file_info_get_sort_order (GFileInfo *info)
1460 static guint32 attr = 0;
1461 GFileAttributeValue *value;
1463 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1466 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1468 value = g_file_info_find_value (info, attr);
1469 return _g_file_attribute_value_get_int32 (value);
1472 /* Helper setters: */
1474 * g_file_info_set_file_type:
1475 * @info: a #GFileInfo.
1476 * @type: a #GFileType.
1478 * Sets the file type in a #GFileInfo to @type.
1479 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1482 g_file_info_set_file_type (GFileInfo *info,
1485 static guint32 attr = 0;
1486 GFileAttributeValue *value;
1488 g_return_if_fail (G_IS_FILE_INFO (info));
1491 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1493 value = g_file_info_create_value (info, attr);
1495 _g_file_attribute_value_set_uint32 (value, type);
1499 * g_file_info_set_is_hidden:
1500 * @info: a #GFileInfo.
1501 * @is_hidden: a #gboolean.
1503 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1504 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1507 g_file_info_set_is_hidden (GFileInfo *info,
1510 static guint32 attr = 0;
1511 GFileAttributeValue *value;
1513 g_return_if_fail (G_IS_FILE_INFO (info));
1516 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1518 value = g_file_info_create_value (info, attr);
1520 _g_file_attribute_value_set_boolean (value, is_hidden);
1524 * g_file_info_set_is_symlink:
1525 * @info: a #GFileInfo.
1526 * @is_symlink: a #gboolean.
1528 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1529 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1532 g_file_info_set_is_symlink (GFileInfo *info,
1533 gboolean is_symlink)
1535 static guint32 attr = 0;
1536 GFileAttributeValue *value;
1538 g_return_if_fail (G_IS_FILE_INFO (info));
1541 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1543 value = g_file_info_create_value (info, attr);
1545 _g_file_attribute_value_set_boolean (value, is_symlink);
1549 * g_file_info_set_name:
1550 * @info: a #GFileInfo.
1551 * @name: a string containing a name.
1553 * Sets the name attribute for the current #GFileInfo.
1554 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1557 g_file_info_set_name (GFileInfo *info,
1560 static guint32 attr = 0;
1561 GFileAttributeValue *value;
1563 g_return_if_fail (G_IS_FILE_INFO (info));
1564 g_return_if_fail (name != NULL);
1567 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1569 value = g_file_info_create_value (info, attr);
1571 _g_file_attribute_value_set_byte_string (value, name);
1575 * g_file_info_set_display_name:
1576 * @info: a #GFileInfo.
1577 * @display_name: a string containing a display name.
1579 * Sets the display name for the current #GFileInfo.
1580 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1583 g_file_info_set_display_name (GFileInfo *info,
1584 const char *display_name)
1586 static guint32 attr = 0;
1587 GFileAttributeValue *value;
1589 g_return_if_fail (G_IS_FILE_INFO (info));
1590 g_return_if_fail (display_name != NULL);
1593 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1595 value = g_file_info_create_value (info, attr);
1597 _g_file_attribute_value_set_string (value, display_name);
1601 * g_file_info_set_edit_name:
1602 * @info: a #GFileInfo.
1603 * @edit_name: a string containing an edit name.
1605 * Sets the edit name for the current file.
1606 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1609 g_file_info_set_edit_name (GFileInfo *info,
1610 const char *edit_name)
1612 static guint32 attr = 0;
1613 GFileAttributeValue *value;
1615 g_return_if_fail (G_IS_FILE_INFO (info));
1616 g_return_if_fail (edit_name != NULL);
1619 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1621 value = g_file_info_create_value (info, attr);
1623 _g_file_attribute_value_set_string (value, edit_name);
1627 * g_file_info_set_icon:
1628 * @info: a #GFileInfo.
1631 * Sets the icon for a given #GFileInfo.
1632 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1635 g_file_info_set_icon (GFileInfo *info,
1638 static guint32 attr = 0;
1639 GFileAttributeValue *value;
1641 g_return_if_fail (G_IS_FILE_INFO (info));
1642 g_return_if_fail (G_IS_ICON (icon));
1645 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1647 value = g_file_info_create_value (info, attr);
1649 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1653 * g_file_info_set_content_type:
1654 * @info: a #GFileInfo.
1655 * @content_type: a content type. See #GContentType.
1657 * Sets the content type attribute for a given #GFileInfo.
1658 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1661 g_file_info_set_content_type (GFileInfo *info,
1662 const char *content_type)
1664 static guint32 attr = 0;
1665 GFileAttributeValue *value;
1667 g_return_if_fail (G_IS_FILE_INFO (info));
1668 g_return_if_fail (content_type != NULL);
1671 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1673 value = g_file_info_create_value (info, attr);
1675 _g_file_attribute_value_set_string (value, content_type);
1679 * g_file_info_set_size:
1680 * @info: a #GFileInfo.
1681 * @size: a #goffset containing the file's size.
1683 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1684 * to the given size.
1687 g_file_info_set_size (GFileInfo *info,
1690 static guint32 attr = 0;
1691 GFileAttributeValue *value;
1693 g_return_if_fail (G_IS_FILE_INFO (info));
1696 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1698 value = g_file_info_create_value (info, attr);
1700 _g_file_attribute_value_set_uint64 (value, size);
1704 * g_file_info_set_modification_time
1705 * @info: a #GFileInfo.
1706 * @mtime: a #GTimeVal.
1708 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
1709 * info to the given time value.
1712 g_file_info_set_modification_time (GFileInfo *info,
1715 static guint32 attr_mtime = 0, attr_mtime_usec;
1716 GFileAttributeValue *value;
1718 g_return_if_fail (G_IS_FILE_INFO (info));
1719 g_return_if_fail (mtime != NULL);
1721 if (attr_mtime == 0)
1723 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1724 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1727 value = g_file_info_create_value (info, attr_mtime);
1729 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
1730 value = g_file_info_create_value (info, attr_mtime_usec);
1732 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
1736 * g_file_info_set_symlink_target:
1737 * @info: a #GFileInfo.
1738 * @symlink_target: a static string containing a path to a symlink target.
1740 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
1741 * to the given symlink target.
1744 g_file_info_set_symlink_target (GFileInfo *info,
1745 const char *symlink_target)
1747 static guint32 attr = 0;
1748 GFileAttributeValue *value;
1750 g_return_if_fail (G_IS_FILE_INFO (info));
1751 g_return_if_fail (symlink_target != NULL);
1754 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1756 value = g_file_info_create_value (info, attr);
1758 _g_file_attribute_value_set_byte_string (value, symlink_target);
1762 * g_file_info_set_sort_order:
1763 * @info: a #GFileInfo.
1764 * @sort_order: a sort order integer.
1766 * Sets the sort order attribute in the file info structure. See
1767 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1770 g_file_info_set_sort_order (GFileInfo *info,
1773 static guint32 attr = 0;
1774 GFileAttributeValue *value;
1776 g_return_if_fail (G_IS_FILE_INFO (info));
1779 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1781 value = g_file_info_create_value (info, attr);
1783 _g_file_attribute_value_set_int32 (value, sort_order);
1787 #define ON_STACK_MATCHERS 5
1794 struct _GFileAttributeMatcher {
1796 SubMatcher sub_matchers[ON_STACK_MATCHERS];
1797 GArray *more_sub_matchers;
1800 guint32 iterator_ns;
1806 matcher_add (GFileAttributeMatcher *matcher,
1810 SubMatcher *sub_matchers;
1814 for (i = 0; i < ON_STACK_MATCHERS; i++)
1816 /* First empty spot, not found, use this */
1817 if (matcher->sub_matchers[i].id == 0)
1819 matcher->sub_matchers[i].id = id;
1820 matcher->sub_matchers[i].mask = mask;
1825 if (matcher->sub_matchers[i].id == id &&
1826 matcher->sub_matchers[i].mask == mask)
1830 if (matcher->more_sub_matchers == NULL)
1831 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
1833 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
1834 for (i = 0; i < matcher->more_sub_matchers->len; i++)
1837 if (sub_matchers[i].id == id &&
1838 sub_matchers[i].mask == mask)
1845 g_array_append_val (matcher->more_sub_matchers, s);
1849 * g_file_attribute_matcher_new:
1850 * @attributes: an attribute string to match.
1852 * Creates a new file attribute matcher, which matches attributes
1853 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
1854 * counted structures, and are created with a reference count of 1. If
1855 * the number of references falls to 0, the #GFileAttributeMatcher is
1856 * automatically destroyed.
1858 * The @attribute string should be formatted with specific keys separated
1859 * from namespaces with a double colon. Several "namespace::key" strings may be
1860 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
1861 * The wildcard "*" may be used to match all keys and namespaces, or
1862 * "namespace::*" will match all keys in a given namespace.
1864 * Examples of strings to use:
1866 * <title>File Attribute Matcher strings and results</title>
1867 * <tgroup cols='2' align='left'><thead>
1868 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
1870 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
1871 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
1872 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
1873 * all keys in the unix namespace.</entry></row>
1877 * Returns: a #GFileAttributeMatcher.
1879 GFileAttributeMatcher *
1880 g_file_attribute_matcher_new (const char *attributes)
1885 GFileAttributeMatcher *matcher;
1887 if (attributes == NULL || *attributes == '\0')
1890 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
1893 split = g_strsplit (attributes, ",", -1);
1895 for (i = 0; split[i] != NULL; i++)
1897 if (strcmp (split[i], "*") == 0)
1898 matcher->all = TRUE;
1903 colon = strstr (split[i], "::");
1904 if (colon != NULL &&
1909 id = lookup_attribute (split[i]);
1917 id = lookup_namespace (split[i]) << NS_POS;
1918 mask = NS_MASK << NS_POS;
1921 matcher_add (matcher, id, mask);
1931 * g_file_attribute_matcher_ref:
1932 * @matcher: a #GFileAttributeMatcher.
1934 * References a file attribute matcher.
1936 * Returns: a #GFileAttributeMatcher.
1938 GFileAttributeMatcher *
1939 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
1943 g_return_val_if_fail (matcher->ref > 0, NULL);
1944 g_atomic_int_inc (&matcher->ref);
1950 * g_file_attribute_matcher_unref:
1951 * @matcher: a #GFileAttributeMatcher.
1953 * Unreferences @matcher. If the reference count falls below 1,
1954 * the @matcher is automatically freed.
1958 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
1962 g_return_if_fail (matcher->ref > 0);
1964 if (g_atomic_int_dec_and_test (&matcher->ref))
1966 if (matcher->more_sub_matchers)
1967 g_array_free (matcher->more_sub_matchers, TRUE);
1975 * g_file_attribute_matcher_matches_only:
1976 * @matcher: a #GFileAttributeMatcher.
1977 * @attribute: a file attribute key.
1979 * Checks if a attribute matcher only matches a given attribute. Always
1980 * returns %FALSE if "*" was used when creating the matcher.
1982 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
1985 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
1986 const char *attribute)
1990 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
1992 if (matcher == NULL ||
1996 id = lookup_attribute (attribute);
1998 if (matcher->sub_matchers[0].id != 0 &&
1999 matcher->sub_matchers[1].id == 0 &&
2000 matcher->sub_matchers[0].mask == 0xffffffff &&
2001 matcher->sub_matchers[0].id == id)
2008 matcher_matches_id (GFileAttributeMatcher *matcher,
2011 SubMatcher *sub_matchers;
2014 for (i = 0; i < ON_STACK_MATCHERS; i++)
2016 if (matcher->sub_matchers[i].id == 0)
2019 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2023 if (matcher->more_sub_matchers)
2025 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2026 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2028 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2037 g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2040 /* We return a NULL matcher for an empty match string, so handle this */
2041 if (matcher == NULL)
2047 return matcher_matches_id (matcher, id);
2051 * g_file_attribute_matcher_matches:
2052 * @matcher: a #GFileAttributeMatcher.
2053 * @attribute: a file attribute key.
2055 * Checks if an attribute will be matched by an attribute matcher. If
2056 * the matcher was created with the "*" matching string, this function
2057 * will always return %TRUE.
2059 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2062 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2063 const char *attribute)
2065 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2067 /* We return a NULL matcher for an empty match string, so handle this */
2068 if (matcher == NULL)
2074 return matcher_matches_id (matcher, lookup_attribute (attribute));
2077 /* return TRUE -> all */
2079 * g_file_attribute_matcher_enumerate_namespace:
2080 * @matcher: a #GFileAttributeMatcher.
2081 * @ns: a string containing a file attribute namespace.
2083 * Checks if the matcher will match all of the keys in a given namespace.
2084 * This will always return %TRUE if a wildcard character is in use (e.g. if
2085 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2086 * using "*" and namespace is anything.)
2088 * TODO: this is awkwardly worded.
2090 * Returns: %TRUE if the matcher matches all of the entries
2091 * in the given @ns, %FALSE otherwise.
2094 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2097 SubMatcher *sub_matchers;
2101 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2103 /* We return a NULL matcher for an empty match string, so handle this */
2104 if (matcher == NULL)
2110 ns_id = lookup_namespace (ns) << NS_POS;
2112 for (i = 0; i < ON_STACK_MATCHERS; i++)
2114 if (matcher->sub_matchers[i].id == ns_id)
2118 if (matcher->more_sub_matchers)
2120 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2121 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2123 if (sub_matchers[i].id == ns_id)
2128 matcher->iterator_ns = ns_id;
2129 matcher->iterator_pos = 0;
2135 * g_file_attribute_matcher_enumerate_next:
2136 * @matcher: a #GFileAttributeMatcher.
2138 * Gets the next matched attribute from a #GFileAttributeMatcher.
2140 * Returns: a string containing the next attribute or %NULL if
2141 * no more attribute exist.
2144 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2147 SubMatcher *sub_matcher;
2149 /* We return a NULL matcher for an empty match string, so handle this */
2150 if (matcher == NULL)
2155 i = matcher->iterator_pos++;
2157 if (i < ON_STACK_MATCHERS)
2159 if (matcher->sub_matchers[i].id == 0)
2162 sub_matcher = &matcher->sub_matchers[i];
2166 if (matcher->more_sub_matchers == NULL)
2169 i -= ON_STACK_MATCHERS;
2170 if (i < matcher->more_sub_matchers->len)
2171 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2176 if (sub_matcher->mask == 0xffffffff &&
2177 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2178 return get_attribute_for_id (sub_matcher->id);
2182 #define __G_FILE_INFO_C__
2183 #include "gioaliasdef.c"