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
28 * Functionality for manipulating basic metadata for files. #GFileInfo
29 * implements methods for getting information that all files should
30 * contain, and allows for manipulation of extended attributes.
31 * #GFileAttributeMatcher allows for searching through a #GFileInfo for
42 #include "gfileinfo.h"
43 #include "gfileattribute-priv.h"
48 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
49 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
53 GFileAttributeValue value;
58 GObject parent_instance;
61 GFileAttributeMatcher *mask;
64 struct _GFileInfoClass
66 GObjectClass parent_class;
70 static gboolean g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
73 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
77 guint32 attribute_id_counter;
80 G_LOCK_DEFINE_STATIC (attribute_hash);
81 static int namespace_id_counter = 0;
82 static GHashTable *ns_hash = NULL;
83 static GHashTable *attribute_hash = NULL;
84 static char ***attributes = NULL;
86 /* Attribute ids are 32bit, we split it up like this:
87 * |------------|--------------------|
89 * namespace attribute id
91 * This way the attributes gets sorted in namespace order
95 #define NS_MASK ((guint32)((1<<12) - 1))
97 #define ID_MASK ((guint32)((1<<20) - 1))
99 #define GET_NS(_attr_id) \
100 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
101 #define GET_ID(_attr_id) \
102 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
104 #define MAKE_ATTR_ID(_ns, _id) \
105 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
106 ((((guint32) _id) & ID_MASK) << ID_POS) )
109 _lookup_namespace (const char *namespace)
113 ns_info = g_hash_table_lookup (ns_hash, namespace);
116 ns_info = g_new0 (NSInfo, 1);
117 ns_info->id = ++namespace_id_counter;
118 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
119 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
120 attributes[ns_info->id] = NULL;
126 lookup_namespace (const char *namespace)
131 G_LOCK (attribute_hash);
133 if (attribute_hash == NULL)
135 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
136 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
139 ns_info = _lookup_namespace (namespace);
144 G_UNLOCK (attribute_hash);
150 get_attribute_for_id (int attribute)
153 G_LOCK (attribute_hash);
154 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
155 G_UNLOCK (attribute_hash);
160 lookup_attribute (const char *attribute)
167 G_LOCK (attribute_hash);
168 if (attribute_hash == NULL)
170 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
171 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
174 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
178 G_UNLOCK (attribute_hash);
182 colon = strstr (attribute, "::");
184 ns = g_strndup (attribute, colon - attribute);
188 ns_info = _lookup_namespace (ns);
191 id = ++ns_info->attribute_id_counter;
192 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
193 attributes[ns_info->id][id] = g_strdup (attribute);
195 attr_id = MAKE_ATTR_ID (ns_info->id, id);
197 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
199 G_UNLOCK (attribute_hash);
205 g_file_info_finalize (GObject *object)
209 GFileAttribute *attrs;
211 info = G_FILE_INFO (object);
213 attrs = (GFileAttribute *)info->attributes->data;
214 for (i = 0; i < info->attributes->len; i++)
215 _g_file_attribute_value_clear (&attrs[i].value);
216 g_array_free (info->attributes, TRUE);
218 if (info->mask != NO_ATTRIBUTE_MASK)
219 g_file_attribute_matcher_unref (info->mask);
221 if (G_OBJECT_CLASS (g_file_info_parent_class)->finalize)
222 (*G_OBJECT_CLASS (g_file_info_parent_class)->finalize) (object);
226 g_file_info_class_init (GFileInfoClass *klass)
228 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
230 gobject_class->finalize = g_file_info_finalize;
234 g_file_info_init (GFileInfo *info)
236 info->mask = NO_ATTRIBUTE_MASK;
237 info->attributes = g_array_new (FALSE, FALSE,
238 sizeof (GFileAttribute));
244 * Creates a new file info structure.
246 * Returns: a #GFileInfo.
249 g_file_info_new (void)
251 return g_object_new (G_TYPE_FILE_INFO, NULL);
255 * g_file_info_copy_into:
256 * @src_info: source to copy attributes from.
257 * @dest_info: destination to copy attributes to.
259 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
262 g_file_info_copy_into (GFileInfo *src_info,
263 GFileInfo *dest_info)
265 GFileAttribute *source, *dest;
268 g_return_if_fail (G_IS_FILE_INFO (src_info));
269 g_return_if_fail (G_IS_FILE_INFO (dest_info));
271 dest = (GFileAttribute *)dest_info->attributes->data;
272 for (i = 0; i < dest_info->attributes->len; i++)
273 _g_file_attribute_value_clear (&dest[i].value);
275 g_array_set_size (dest_info->attributes,
276 src_info->attributes->len);
278 source = (GFileAttribute *)src_info->attributes->data;
279 dest = (GFileAttribute *)dest_info->attributes->data;
281 for (i = 0; i < src_info->attributes->len; i++)
283 dest[i].attribute = source[i].attribute;
284 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
285 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
288 if (src_info->mask == NO_ATTRIBUTE_MASK)
289 dest_info->mask = NO_ATTRIBUTE_MASK;
291 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
296 * @other: a #GFileInfo.
298 * Duplicates a file info structure.
300 * Returns: a duplicate #GFileInfo of @other.
303 g_file_info_dup (GFileInfo *other)
307 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
309 new = g_file_info_new ();
310 g_file_info_copy_into (other, new);
315 * g_file_info_set_attribute_mask:
316 * @info: a #GFileInfo.
317 * @mask: a #GFileAttributeMatcher.
319 * Sets @mask on @info to match specific attribute types.
322 g_file_info_set_attribute_mask (GFileInfo *info,
323 GFileAttributeMatcher *mask)
325 GFileAttribute *attr;
328 g_return_if_fail (G_IS_FILE_INFO (info));
329 g_return_if_fail (mask != NULL);
331 if (mask != info->mask)
333 if (info->mask != NO_ATTRIBUTE_MASK)
334 g_file_attribute_matcher_unref (info->mask);
335 info->mask = g_file_attribute_matcher_ref (mask);
337 /* Remove non-matching attributes */
338 for (i = 0; i < info->attributes->len; i++)
340 attr = &g_array_index (info->attributes, GFileAttribute, i);
341 if (!g_file_attribute_matcher_matches_id (mask,
344 _g_file_attribute_value_clear (&attr->value);
345 g_array_remove_index (info->attributes, i);
353 * g_file_info_unset_attribute_mask:
356 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
360 g_file_info_unset_attribute_mask (GFileInfo *info)
362 g_return_if_fail (G_IS_FILE_INFO (info));
364 if (info->mask != NO_ATTRIBUTE_MASK)
365 g_file_attribute_matcher_unref (info->mask);
366 info->mask = NO_ATTRIBUTE_MASK;
370 * g_file_info_clear_status:
371 * @info: a #GFileInfo.
373 * Clears the status information from @info.
376 g_file_info_clear_status (GFileInfo *info)
378 GFileAttribute *attrs;
381 g_return_if_fail (G_IS_FILE_INFO (info));
383 attrs = (GFileAttribute *)info->attributes->data;
384 for (i = 0; i < info->attributes->len; i++)
385 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
389 g_file_info_find_place (GFileInfo *info,
393 GFileAttribute *attrs;
394 /* Binary search for the place where attribute would be, if its
398 max = info->attributes->len;
400 attrs = (GFileAttribute *)info->attributes->data;
404 med = min + (max - min) / 2;
405 if (attrs[med].attribute == attribute)
410 else if (attrs[med].attribute < attribute)
412 else /* attrs[med].attribute > attribute */
419 static GFileAttributeValue *
420 g_file_info_find_value (GFileInfo *info,
423 GFileAttribute *attrs;
426 i = g_file_info_find_place (info, attr_id);
427 attrs = (GFileAttribute *)info->attributes->data;
428 if (i < info->attributes->len &&
429 attrs[i].attribute == attr_id)
430 return &attrs[i].value;
435 static GFileAttributeValue *
436 g_file_info_find_value_by_name (GFileInfo *info,
437 const char *attribute)
441 attr_id = lookup_attribute (attribute);
442 return g_file_info_find_value (info, attr_id);
446 * g_file_info_has_attribute:
447 * @info: a #GFileInfo.
448 * @attribute: a file attribute key.
450 * Checks if a file info structure has an attribute named @attribute.
452 * Returns: %TRUE if @GFileInfo has an attribute named @attribute,
456 g_file_info_has_attribute (GFileInfo *info,
457 const char *attribute)
459 GFileAttributeValue *value;
461 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
462 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
464 value = g_file_info_find_value_by_name (info, attribute);
465 return value != NULL;
469 * g_file_info_list_attributes:
470 * @info: a #GFileInfo.
471 * @name_space: a file attribute key's namespace.
473 * Lists the file info structure's attributes.
475 * Returns: a null-terminated array of strings of all of the
476 * possible attribute types for the given @name_space, or
480 g_file_info_list_attributes (GFileInfo *info,
481 const char *name_space)
484 GFileAttribute *attrs;
486 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
489 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
491 names = g_ptr_array_new ();
492 attrs = (GFileAttribute *)info->attributes->data;
493 for (i = 0; i < info->attributes->len; i++)
495 attribute = attrs[i].attribute;
496 if (ns_id == 0 || GET_NS (attribute) == ns_id)
497 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
501 g_ptr_array_add (names, NULL);
503 return (char **)g_ptr_array_free (names, FALSE);
507 * g_file_info_get_attribute_type:
508 * @info: a #GFileInfo.
509 * @attribute: a file attribute key.
511 * Gets the attribute type for an attribute key.
513 * Returns: a #GFileAttributeType for the given @attribute, or
514 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is invalid.
517 g_file_info_get_attribute_type (GFileInfo *info,
518 const char *attribute)
520 GFileAttributeValue *value;
522 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
523 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
525 value = g_file_info_find_value_by_name (info, attribute);
529 return G_FILE_ATTRIBUTE_TYPE_INVALID;
533 * g_file_info_remove_attribute:
534 * @info: a #GFileInfo.
535 * @attribute: a file attribute key.
537 * Removes all cases of @attribute from @info if it exists.
541 g_file_info_remove_attribute (GFileInfo *info,
542 const char *attribute)
545 GFileAttribute *attrs;
548 g_return_if_fail (G_IS_FILE_INFO (info));
549 g_return_if_fail (attribute != NULL && *attribute != '\0');
551 attr_id = lookup_attribute (attribute);
553 i = g_file_info_find_place (info, attr_id);
554 attrs = (GFileAttribute *)info->attributes->data;
555 if (i < info->attributes->len &&
556 attrs[i].attribute == attr_id)
558 _g_file_attribute_value_clear (&attrs[i].value);
559 g_array_remove_index (info->attributes, i);
564 g_file_info_get_attribute_data (GFileInfo *info,
565 const char *attribute,
566 GFileAttributeType *type,
568 GFileAttributeStatus *status)
570 GFileAttributeValue *value;
572 value = g_file_info_find_value_by_name (info, attribute);
577 *status = value->status;
583 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
589 g_file_info_get_attribute_status (GFileInfo *info,
590 const char *attribute)
592 GFileAttributeValue *val;
594 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
595 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
597 val = g_file_info_find_value_by_name (info, attribute);
605 GFileAttributeValue *
606 _g_file_info_get_attribute_value (GFileInfo *info,
607 const char *attribute)
610 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
611 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
613 return g_file_info_find_value_by_name (info, attribute);
617 * g_file_info_get_attribute_as_string:
618 * @info: a #GFileInfo.
619 * @attribute: a file attribute key.
621 * Gets the value of a attribute, formated as a string.
622 * This escapes things as needed to make the string valid
625 * Returns: a utf8 string associated with the given @attribute.
626 * When you're done with the string it must be freed.
629 g_file_info_get_attribute_as_string (GFileInfo *info,
630 const char *attribute)
632 GFileAttributeValue *val;
633 val = _g_file_info_get_attribute_value (info, attribute);
635 return _g_file_attribute_value_as_string (val);
641 * g_file_info_get_attribute_object:
642 * @info: a #GFileInfo.
643 * @attribute: a file attribute key.
645 * Gets the value of a #GObject attribute. If the attribute does
646 * not contain a #GObject, %NULL will be returned.
648 * Returns: a #GObject associated with the given @attribute, or
652 g_file_info_get_attribute_object (GFileInfo *info,
653 const char *attribute)
655 GFileAttributeValue *value;
657 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
658 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
660 value = g_file_info_find_value_by_name (info, attribute);
661 return _g_file_attribute_value_get_object (value);
665 * g_file_info_get_attribute_string:
666 * @info: a #GFileInfo.
667 * @attribute: a file attribute key.
669 * Gets the value of a string attribute. If the attribute does
670 * not contain a string, %NULL will be returned.
672 * Returns: the contents of the @attribute value as a string, or
676 g_file_info_get_attribute_string (GFileInfo *info,
677 const char *attribute)
679 GFileAttributeValue *value;
681 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
682 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
684 value = g_file_info_find_value_by_name (info, attribute);
685 return _g_file_attribute_value_get_string (value);
689 * g_file_info_get_attribute_byte_string:
690 * @info: a #GFileInfo.
691 * @attribute: a file attribute key.
693 * Gets the value of a byte string attribute. If the attribute does
694 * not contain a byte string, %NULL will be returned.
696 * Returns: the contents of the @attribute value as a byte string, or
700 g_file_info_get_attribute_byte_string (GFileInfo *info,
701 const char *attribute)
703 GFileAttributeValue *value;
705 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
706 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
708 value = g_file_info_find_value_by_name (info, attribute);
709 return _g_file_attribute_value_get_byte_string (value);
713 * g_file_info_get_attribute_boolean:
714 * @info: a #GFileInfo.
715 * @attribute: a file attribute key.
717 * Gets the value of a boolean attribute. If the attribute does not
718 * contain a boolean value, %FALSE will be returned.
720 * Returns: the boolean value contained within the attribute.
723 g_file_info_get_attribute_boolean (GFileInfo *info,
724 const char *attribute)
726 GFileAttributeValue *value;
728 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
729 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
731 value = g_file_info_find_value_by_name (info, attribute);
732 return _g_file_attribute_value_get_boolean (value);
736 * g_file_info_get_attribute_uint32:
737 * @info: a #GFileInfo.
738 * @attribute: a file attribute key.
740 * Gets an unsigned 32-bit integer contained within the attribute. If the
741 * attribute does not contain an unsigned 32-bit integer, or is invalid,
742 * 0 will be returned.
744 * Returns: an unsigned 32-bit integer from the attribute.
747 g_file_info_get_attribute_uint32 (GFileInfo *info,
748 const char *attribute)
750 GFileAttributeValue *value;
752 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
753 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
755 value = g_file_info_find_value_by_name (info, attribute);
756 return _g_file_attribute_value_get_uint32 (value);
760 * g_file_info_get_attribute_int32:
761 * @info: a #GFileInfo.
762 * @attribute: a file attribute key.
764 * Gets a signed 32-bit integer contained within the attribute. If the
765 * attribute does not contain a signed 32-bit integer, or is invalid,
766 * 0 will be returned.
768 * Returns: a signed 32-bit integer from the attribute.
771 g_file_info_get_attribute_int32 (GFileInfo *info,
772 const char *attribute)
774 GFileAttributeValue *value;
776 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
777 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
779 value = g_file_info_find_value_by_name (info, attribute);
780 return _g_file_attribute_value_get_int32 (value);
784 * g_file_info_get_attribute_uint64:
785 * @info: a #GFileInfo.
786 * @attribute: a file attribute key.
788 * Gets a unsigned 64-bit integer contained within the attribute. If the
789 * attribute does not contain an unsigned 64-bit integer, or is invalid,
790 * 0 will be returned.
792 * Returns: a unsigned 64-bit integer from the attribute.
795 g_file_info_get_attribute_uint64 (GFileInfo *info,
796 const char *attribute)
798 GFileAttributeValue *value;
800 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
801 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
803 value = g_file_info_find_value_by_name (info, attribute);
804 return _g_file_attribute_value_get_uint64 (value);
808 * g_file_info_get_attribute_int64:
809 * @info: a #GFileInfo.
810 * @attribute: a file attribute key.
812 * Gets a signed 64-bit integer contained within the attribute. If the
813 * attribute does not contain an signed 64-bit integer, or is invalid,
814 * 0 will be returned.
816 * Returns: a signed 64-bit integer from the attribute.
819 g_file_info_get_attribute_int64 (GFileInfo *info,
820 const char *attribute)
822 GFileAttributeValue *value;
824 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
825 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
827 value = g_file_info_find_value_by_name (info, attribute);
828 return _g_file_attribute_value_get_int64 (value);
831 static GFileAttributeValue *
832 g_file_info_create_value (GFileInfo *info,
835 GFileAttribute *attrs;
838 if (info->mask != NO_ATTRIBUTE_MASK &&
839 !g_file_attribute_matcher_matches_id (info->mask, attr_id))
842 i = g_file_info_find_place (info, attr_id);
844 attrs = (GFileAttribute *)info->attributes->data;
845 if (i < info->attributes->len &&
846 attrs[i].attribute == attr_id)
847 return &attrs[i].value;
850 GFileAttribute attr = { 0 };
851 attr.attribute = attr_id;
852 g_array_insert_val (info->attributes, i, attr);
854 attrs = (GFileAttribute *)info->attributes->data;
855 return &attrs[i].value;
859 static GFileAttributeValue *
860 g_file_info_create_value_by_name (GFileInfo *info,
861 const char *attribute)
865 attr_id = lookup_attribute (attribute);
867 return g_file_info_create_value (info, attr_id);
871 * g_file_info_set_attribute:
872 * @info: a #GFileInfo.
873 * @attribute: a file attribute key.
874 * @attr_value: a #GFileAttributeValue.
876 * Sets the @attribute to contain the given @attr_value,
880 g_file_info_set_attribute (GFileInfo *info,
881 const char *attribute,
882 GFileAttributeType type,
885 GFileAttributeValue *value;
887 g_return_if_fail (G_IS_FILE_INFO (info));
888 g_return_if_fail (attribute != NULL && *attribute != '\0');
890 value = g_file_info_create_value_by_name (info, attribute);
893 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
897 * g_file_info_set_attribute_object:
898 * @info: a #GFileInfo.
899 * @attribute: a file attribute key.
900 * @attr_value: a #GObject.
902 * Sets the @attribute to contain the given @attr_value,
906 g_file_info_set_attribute_object (GFileInfo *info,
907 const char *attribute,
910 GFileAttributeValue *value;
912 g_return_if_fail (G_IS_FILE_INFO (info));
913 g_return_if_fail (attribute != NULL && *attribute != '\0');
914 g_return_if_fail (G_IS_OBJECT (attr_value));
916 value = g_file_info_create_value_by_name (info, attribute);
918 _g_file_attribute_value_set_object (value, attr_value);
922 * g_file_info_set_attribute_string:
923 * @info: a #GFileInfo.
924 * @attribute: a file attribute key.
925 * @attr_value: a string.
927 * Sets the @attribute to contain the given @attr_value,
931 g_file_info_set_attribute_string (GFileInfo *info,
932 const char *attribute,
933 const char *attr_value)
935 GFileAttributeValue *value;
937 g_return_if_fail (G_IS_FILE_INFO (info));
938 g_return_if_fail (attribute != NULL && *attribute != '\0');
939 g_return_if_fail (attr_value != NULL);
941 value = g_file_info_create_value_by_name (info, attribute);
943 _g_file_attribute_value_set_string (value, attr_value);
947 * g_file_info_set_attribute_byte_string:
948 * @info: a #GFileInfo.
949 * @attribute: a file attribute key.
950 * @attr_value: a byte string.
952 * Sets the @attribute to contain the given @attr_value,
956 g_file_info_set_attribute_byte_string (GFileInfo *info,
957 const char *attribute,
958 const char *attr_value)
960 GFileAttributeValue *value;
962 g_return_if_fail (G_IS_FILE_INFO (info));
963 g_return_if_fail (attribute != NULL && *attribute != '\0');
964 g_return_if_fail (attr_value != NULL);
966 value = g_file_info_create_value_by_name (info, attribute);
968 _g_file_attribute_value_set_byte_string (value, attr_value);
972 * g_file_info_set_attribute_boolean:
973 * @info: a #GFileInfo.
974 * @attribute: a file attribute key.
975 * @attr_value: a boolean value.
977 * Sets the @attribute to contain the given @attr_value,
981 g_file_info_set_attribute_boolean (GFileInfo *info,
982 const char *attribute,
985 GFileAttributeValue *value;
987 g_return_if_fail (G_IS_FILE_INFO (info));
988 g_return_if_fail (attribute != NULL && *attribute != '\0');
990 value = g_file_info_create_value_by_name (info, attribute);
992 _g_file_attribute_value_set_boolean (value, attr_value);
996 * g_file_info_set_attribute_uint32:
997 * @info: a #GFileInfo.
998 * @attribute: a file attribute key.
999 * @attr_value: an unsigned 32-bit integer.
1001 * Sets the @attribute to contain the given @attr_value,
1005 g_file_info_set_attribute_uint32 (GFileInfo *info,
1006 const char *attribute,
1009 GFileAttributeValue *value;
1011 g_return_if_fail (G_IS_FILE_INFO (info));
1012 g_return_if_fail (attribute != NULL && *attribute != '\0');
1014 value = g_file_info_create_value_by_name (info, attribute);
1016 _g_file_attribute_value_set_uint32 (value, attr_value);
1021 * g_file_info_set_attribute_int32:
1022 * @info: a #GFileInfo.
1023 * @attribute: a file attribute key.
1024 * @attr_value: a signed 32-bit integer
1026 * Sets the @attribute to contain the given @attr_value,
1030 g_file_info_set_attribute_int32 (GFileInfo *info,
1031 const char *attribute,
1034 GFileAttributeValue *value;
1036 g_return_if_fail (G_IS_FILE_INFO (info));
1037 g_return_if_fail (attribute != NULL && *attribute != '\0');
1039 value = g_file_info_create_value_by_name (info, attribute);
1041 _g_file_attribute_value_set_int32 (value, attr_value);
1045 * g_file_info_set_attribute_uint64:
1046 * @info: a #GFileInfo.
1047 * @attribute: a file attribute key.
1048 * @attr_value: an unsigned 64-bit integer.
1050 * Sets the @attribute to contain the given @attr_value,
1054 g_file_info_set_attribute_uint64 (GFileInfo *info,
1055 const char *attribute,
1058 GFileAttributeValue *value;
1060 g_return_if_fail (G_IS_FILE_INFO (info));
1061 g_return_if_fail (attribute != NULL && *attribute != '\0');
1063 value = g_file_info_create_value_by_name (info, attribute);
1065 _g_file_attribute_value_set_uint64 (value, attr_value);
1069 * g_file_info_set_attribute_int64:
1070 * @info: a #GFileInfo.
1071 * @attribute: attribute name to set.
1072 * @attr_value: int64 value to set attribute to.
1074 * Sets the @attribute to contain the given @attr_value,
1079 g_file_info_set_attribute_int64 (GFileInfo *info,
1080 const char *attribute,
1083 GFileAttributeValue *value;
1085 g_return_if_fail (G_IS_FILE_INFO (info));
1086 g_return_if_fail (attribute != NULL && *attribute != '\0');
1088 value = g_file_info_create_value_by_name (info, attribute);
1090 _g_file_attribute_value_set_int64 (value, attr_value);
1093 /* Helper getters */
1095 * g_file_info_get_file_type:
1096 * @info: a #GFileInfo.
1098 * Gets a file's type (whether it is a regular file, symlink, etc).
1099 * This is different from the file's content type, see g_file_info_get_content_type().
1101 * Returns: a #GFileType for the given file.
1104 g_file_info_get_file_type (GFileInfo *info)
1106 static guint32 attr = 0;
1107 GFileAttributeValue *value;
1109 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1112 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_TYPE);
1114 value = g_file_info_find_value (info, attr);
1115 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1119 * g_file_info_get_is_hidden:
1120 * @info: a #GFileInfo.
1122 * Checks if a file is hidden.
1124 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1127 g_file_info_get_is_hidden (GFileInfo *info)
1129 static guint32 attr = 0;
1130 GFileAttributeValue *value;
1132 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1135 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_IS_HIDDEN);
1137 value = g_file_info_find_value (info, attr);
1138 return (GFileType)_g_file_attribute_value_get_boolean (value);
1142 * g_file_info_get_is_backup:
1143 * @info: a #GFileInfo.
1145 * Checks if a file is a backup file.
1147 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1150 g_file_info_get_is_backup (GFileInfo *info)
1152 static guint32 attr = 0;
1153 GFileAttributeValue *value;
1155 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1158 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_IS_BACKUP);
1160 value = g_file_info_find_value (info, attr);
1161 return (GFileType)_g_file_attribute_value_get_boolean (value);
1165 * g_file_info_get_is_symlink:
1166 * @info: a #GFileInfo.
1168 * Checks if a file is a symlink.
1170 * Returns: %TRUE if the given @info is a symlink.
1173 g_file_info_get_is_symlink (GFileInfo *info)
1175 static guint32 attr = 0;
1176 GFileAttributeValue *value;
1178 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1181 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_IS_SYMLINK);
1183 value = g_file_info_find_value (info, attr);
1184 return (GFileType)_g_file_attribute_value_get_boolean (value);
1188 * g_file_info_get_name:
1189 * @info: a #GFileInfo.
1191 * Gets the name for a file.
1193 * Returns: a string containing the file name.
1196 g_file_info_get_name (GFileInfo *info)
1198 static guint32 attr = 0;
1199 GFileAttributeValue *value;
1201 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1204 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_NAME);
1206 value = g_file_info_find_value (info, attr);
1207 return _g_file_attribute_value_get_byte_string (value);
1211 * g_file_info_get_display_name:
1212 * @info: a #GFileInfo.
1214 * Gets a display name for a file.
1216 * Returns: a string containing the display name.
1219 g_file_info_get_display_name (GFileInfo *info)
1221 static guint32 attr = 0;
1222 GFileAttributeValue *value;
1224 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1227 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_DISPLAY_NAME);
1229 value = g_file_info_find_value (info, attr);
1230 return _g_file_attribute_value_get_string (value);
1234 * g_file_info_get_edit_name:
1235 * @info: a #GFileInfo.
1237 * Gets the edit name for a file.
1239 * Returns: a string containing the edit name.
1242 g_file_info_get_edit_name (GFileInfo *info)
1244 static guint32 attr = 0;
1245 GFileAttributeValue *value;
1247 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1250 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_EDIT_NAME);
1252 value = g_file_info_find_value (info, attr);
1253 return _g_file_attribute_value_get_string (value);
1257 * g_file_info_get_icon:
1258 * @info: a #GFileInfo.
1260 * Gets the icon for a file.
1262 * Returns: #GIcon for the given @info.
1265 g_file_info_get_icon (GFileInfo *info)
1267 static guint32 attr = 0;
1268 GFileAttributeValue *value;
1271 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1274 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_ICON);
1276 value = g_file_info_find_value (info, attr);
1277 obj = _g_file_attribute_value_get_object (value);
1278 if (obj != NULL && G_IS_ICON (obj))
1279 return G_ICON (obj);
1284 * g_file_info_get_content_type:
1285 * @info: a #GFileInfo.
1287 * Gets the file's content type.
1289 * Returns: a string containing the file's content type.s
1292 g_file_info_get_content_type (GFileInfo *info)
1294 static guint32 attr = 0;
1295 GFileAttributeValue *value;
1297 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1300 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_CONTENT_TYPE);
1302 value = g_file_info_find_value (info, attr);
1303 return _g_file_attribute_value_get_string (value);
1307 * g_file_info_get_size:
1308 * @info: a #GFileInfo.
1310 * Gets the file's size.
1312 * Returns: a #goffset containing the file's size.
1315 g_file_info_get_size (GFileInfo *info)
1317 static guint32 attr = 0;
1318 GFileAttributeValue *value;
1320 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1323 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_SIZE);
1325 value = g_file_info_find_value (info, attr);
1326 return (goffset) _g_file_attribute_value_get_uint64 (value);
1330 * g_file_info_get_modification_time:
1331 * @info: a #GFileInfo.
1332 * @result: a #GTimeVal.
1334 * Gets the modification time of the current @info and sets it
1338 g_file_info_get_modification_time (GFileInfo *info,
1341 static guint32 attr_mtime = 0, attr_mtime_usec;
1342 GFileAttributeValue *value;
1344 g_return_if_fail (G_IS_FILE_INFO (info));
1345 g_return_if_fail (result != NULL);
1347 if (attr_mtime == 0)
1349 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1350 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1353 value = g_file_info_find_value (info, attr_mtime);
1354 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1355 value = g_file_info_find_value (info, attr_mtime_usec);
1356 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1360 * g_file_info_get_symlink_target:
1361 * @info: a #GFileInfo.
1363 * Gets the symlink target for a given #GFileInfo.
1365 * Returns: a string containing the symlink target.
1368 g_file_info_get_symlink_target (GFileInfo *info)
1370 static guint32 attr = 0;
1371 GFileAttributeValue *value;
1373 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1376 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET);
1378 value = g_file_info_find_value (info, attr);
1379 return _g_file_attribute_value_get_byte_string (value);
1383 * g_file_info_get_etag:
1384 * @info: a #GFileInfo.
1386 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1387 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1389 * Returns: a string containing the value of the "etag:value" attribute.
1392 g_file_info_get_etag (GFileInfo *info)
1394 static guint32 attr = 0;
1395 GFileAttributeValue *value;
1397 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1400 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1402 value = g_file_info_find_value (info, attr);
1403 return _g_file_attribute_value_get_string (value);
1407 * g_file_info_get_sort_order:
1408 * @info: a #GFileInfo.
1410 * Gets the value of the sort_order attribute from the #GFileInfo.
1411 * See %G_FILE_ATTRIBUTE_STD_SORT_ORDER.
1413 * Returns: a #gint32 containing the value of the "std:sort_order" attribute.
1416 g_file_info_get_sort_order (GFileInfo *info)
1418 static guint32 attr = 0;
1419 GFileAttributeValue *value;
1421 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1424 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_SORT_ORDER);
1426 value = g_file_info_find_value (info, attr);
1427 return _g_file_attribute_value_get_int32 (value);
1430 /* Helper setters: */
1432 * g_file_info_set_file_type:
1433 * @info: a #GFileInfo.
1434 * @type: a #GFileType.
1436 * Sets the file type in a #GFileInfo to @type.
1437 * See %G_FILE_ATTRIBUTE_STD_TYPE.
1440 g_file_info_set_file_type (GFileInfo *info,
1443 static guint32 attr = 0;
1444 GFileAttributeValue *value;
1446 g_return_if_fail (G_IS_FILE_INFO (info));
1449 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_TYPE);
1451 value = g_file_info_create_value (info, attr);
1453 _g_file_attribute_value_set_uint32 (value, type);
1457 * g_file_info_set_is_hidden:
1458 * @info: a #GFileInfo.
1459 * @is_hidden: a #gboolean.
1461 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1462 * See %G_FILE_ATTRIBUTE_STD_IS_HIDDEN.
1465 g_file_info_set_is_hidden (GFileInfo *info,
1468 static guint32 attr = 0;
1469 GFileAttributeValue *value;
1471 g_return_if_fail (G_IS_FILE_INFO (info));
1474 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_IS_HIDDEN);
1476 value = g_file_info_create_value (info, attr);
1478 _g_file_attribute_value_set_boolean (value, is_hidden);
1482 * g_file_info_set_is_symlink:
1483 * @info: a #GFileInfo.
1484 * @is_symlink: a #gboolean.
1486 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1487 * See %G_FILE_ATTRIBUTE_STD_IS_SYMLINK.
1490 g_file_info_set_is_symlink (GFileInfo *info,
1491 gboolean is_symlink)
1493 static guint32 attr = 0;
1494 GFileAttributeValue *value;
1496 g_return_if_fail (G_IS_FILE_INFO (info));
1499 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_IS_SYMLINK);
1501 value = g_file_info_create_value (info, attr);
1503 _g_file_attribute_value_set_boolean (value, is_symlink);
1507 * g_file_info_set_name:
1508 * @info: a #GFileInfo.
1509 * @name: a string containing a name.
1511 * Sets the name attribute for the current #GFileInfo.
1512 * See %G_FILE_ATTRIBUTE_STD_NAME.
1515 g_file_info_set_name (GFileInfo *info,
1518 static guint32 attr = 0;
1519 GFileAttributeValue *value;
1521 g_return_if_fail (G_IS_FILE_INFO (info));
1522 g_return_if_fail (name != NULL);
1525 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_NAME);
1527 value = g_file_info_create_value (info, attr);
1529 _g_file_attribute_value_set_byte_string (value, name);
1533 * g_file_info_set_display_name:
1534 * @info: a #GFileInfo.
1535 * @display_name: a string containing a display name.
1537 * Sets the display name for the current #GFileInfo.
1538 * See %G_FILE_ATTRIBUTE_STD_DISPLAY_NAME.
1541 g_file_info_set_display_name (GFileInfo *info,
1542 const char *display_name)
1544 static guint32 attr = 0;
1545 GFileAttributeValue *value;
1547 g_return_if_fail (G_IS_FILE_INFO (info));
1548 g_return_if_fail (display_name != NULL);
1551 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_DISPLAY_NAME);
1553 value = g_file_info_create_value (info, attr);
1555 _g_file_attribute_value_set_string (value, display_name);
1559 * g_file_info_set_edit_name:
1560 * @info: a #GFileInfo.
1561 * @edit_name: a string containing an edit name.
1563 * Sets the edit name for the current file.
1564 * See %G_FILE_ATTRIBUTE_STD_EDIT_NAME.
1567 g_file_info_set_edit_name (GFileInfo *info,
1568 const char *edit_name)
1570 static guint32 attr = 0;
1571 GFileAttributeValue *value;
1573 g_return_if_fail (G_IS_FILE_INFO (info));
1574 g_return_if_fail (edit_name != NULL);
1577 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_EDIT_NAME);
1579 value = g_file_info_create_value (info, attr);
1581 _g_file_attribute_value_set_string (value, edit_name);
1585 * g_file_info_set_icon:
1586 * @info: a #GFileInfo.
1589 * Sets the icon for a given #GFileInfo.
1590 * See %G_FILE_ATTRIBUTE_STD_ICON.
1593 g_file_info_set_icon (GFileInfo *info,
1596 static guint32 attr = 0;
1597 GFileAttributeValue *value;
1599 g_return_if_fail (G_IS_FILE_INFO (info));
1600 g_return_if_fail (G_IS_ICON (icon));
1603 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_ICON);
1605 value = g_file_info_create_value (info, attr);
1607 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1611 * g_file_info_set_content_type:
1612 * @info: a #GFileInfo.
1613 * @content_type: a content type. See #GContentType.
1615 * Sets the content type attribute for a given #GFileInfo.
1616 * See %G_FILE_ATTRIBUTE_STD_CONTENT_TYPE.
1619 g_file_info_set_content_type (GFileInfo *info,
1620 const char *content_type)
1622 static guint32 attr = 0;
1623 GFileAttributeValue *value;
1625 g_return_if_fail (G_IS_FILE_INFO (info));
1626 g_return_if_fail (content_type != NULL);
1629 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_CONTENT_TYPE);
1631 value = g_file_info_create_value (info, attr);
1633 _g_file_attribute_value_set_string (value, content_type);
1637 * g_file_info_set_size:
1638 * @info: a #GFileInfo.
1639 * @size: a #goffset containing the file's size.
1641 * Sets the %G_FILE_ATTRIBUTE_STD_SIZE attribute in the file info
1642 * to the given size.
1645 g_file_info_set_size (GFileInfo *info,
1648 static guint32 attr = 0;
1649 GFileAttributeValue *value;
1651 g_return_if_fail (G_IS_FILE_INFO (info));
1654 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_SIZE);
1656 value = g_file_info_create_value (info, attr);
1658 _g_file_attribute_value_set_uint64 (value, size);
1662 * g_file_info_set_modification_time
1663 * @info: a #GFileInfo.
1664 * @mtime: a #GTimeVal.
1666 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
1667 * info to the given time value.
1670 g_file_info_set_modification_time (GFileInfo *info,
1673 static guint32 attr_mtime = 0, attr_mtime_usec;
1674 GFileAttributeValue *value;
1676 g_return_if_fail (G_IS_FILE_INFO (info));
1677 g_return_if_fail (mtime != NULL);
1679 if (attr_mtime == 0)
1681 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1682 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1685 value = g_file_info_create_value (info, attr_mtime);
1687 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
1688 value = g_file_info_create_value (info, attr_mtime_usec);
1690 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
1694 * g_file_info_set_symlink_target:
1695 * @info: a #GFileInfo.
1696 * @symlink_target: a static string containing a path to a symlink target.
1698 * Sets the %G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET attribute in the file info
1699 * to the given symlink target.
1702 g_file_info_set_symlink_target (GFileInfo *info,
1703 const char *symlink_target)
1705 static guint32 attr = 0;
1706 GFileAttributeValue *value;
1708 g_return_if_fail (G_IS_FILE_INFO (info));
1709 g_return_if_fail (symlink_target != NULL);
1712 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET);
1714 value = g_file_info_create_value (info, attr);
1716 _g_file_attribute_value_set_byte_string (value, symlink_target);
1720 * g_file_info_set_sort_order:
1721 * @info: a #GFileInfo.
1722 * @sort_order: a sort order integer.
1724 * Sets the sort order attribute in the file info structure. See
1725 * %G_FILE_ATTRIBUTE_STD_SORT_ORDER.
1728 g_file_info_set_sort_order (GFileInfo *info,
1731 static guint32 attr = 0;
1732 GFileAttributeValue *value;
1734 g_return_if_fail (G_IS_FILE_INFO (info));
1737 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_SORT_ORDER);
1739 value = g_file_info_create_value (info, attr);
1741 _g_file_attribute_value_set_int32 (value, sort_order);
1745 #define ON_STACK_MATCHERS 5
1752 struct _GFileAttributeMatcher {
1754 SubMatcher sub_matchers[ON_STACK_MATCHERS];
1755 GArray *more_sub_matchers;
1758 guint32 iterator_ns;
1764 matcher_add (GFileAttributeMatcher *matcher,
1768 SubMatcher *sub_matchers;
1772 for (i = 0; i < ON_STACK_MATCHERS; i++)
1774 /* First empty spot, not found, use this */
1775 if (matcher->sub_matchers[i].id == 0)
1777 matcher->sub_matchers[i].id = id;
1778 matcher->sub_matchers[i].mask = mask;
1783 if (matcher->sub_matchers[i].id == id &&
1784 matcher->sub_matchers[i].mask == mask)
1788 if (matcher->more_sub_matchers == NULL)
1789 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
1791 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
1792 for (i = 0; i < matcher->more_sub_matchers->len; i++)
1795 if (sub_matchers[i].id == id &&
1796 sub_matchers[i].mask == mask)
1803 g_array_append_val (matcher->more_sub_matchers, s);
1807 * g_file_attribute_matcher_new:
1808 * @attributes: an attribute string to match.
1810 * Creates a new file attribute matcher, which matches attributes
1811 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
1812 * counted structures, and are created with a reference count of 1. If
1813 * the number of references falls to 0, the #GFileAttributeMatcher is
1814 * automatically destroyed.
1816 * The @attribute string should be formatted with specific keys separated
1817 * from namespaces with a double colon. Several "namespace::key" strings may be
1818 * concatenated with a single comma (e.g. "std::type,std::is-hidden").
1819 * The wildcard "*" may be used to match all keys and namespaces, or
1820 * "namespace::*" will match all keys in a given namespace.
1822 * Examples of strings to use:
1824 * <title>File Attribute Matcher strings and results</title>
1825 * <tgroup cols='2' align='left'><thead>
1826 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
1828 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
1829 * <row><entry>"std::is-hidden"</entry><entry>matches only the key is-hidden in the std namespace.</entry></row>
1830 * <row><entry>"std::type,unix::*"</entry><entry>matches the type key in the std namespace and all keys in the unix
1831 * namespace.</entry></row>
1835 * Returns: a #GFileAttributeMatcher.
1837 GFileAttributeMatcher *
1838 g_file_attribute_matcher_new (const char *attributes)
1843 GFileAttributeMatcher *matcher;
1845 if (attributes == NULL || *attributes == '\0')
1848 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
1851 split = g_strsplit (attributes, ",", -1);
1853 for (i = 0; split[i] != NULL; i++)
1855 if (strcmp (split[i], "*") == 0)
1856 matcher->all = TRUE;
1861 colon = strstr (split[i], "::");
1862 if (colon != NULL &&
1867 id = lookup_attribute (split[i]);
1875 id = lookup_namespace (split[i]) << NS_POS;
1876 mask = NS_MASK << NS_POS;
1879 matcher_add (matcher, id, mask);
1889 * g_file_attribute_matcher_ref:
1890 * @matcher: a #GFileAttributeMatcher.
1892 * References a file attribute matcher.
1894 * Returns: a #GFileAttributeMatcher.
1896 GFileAttributeMatcher *
1897 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
1899 g_return_val_if_fail (matcher != NULL, NULL);
1900 g_return_val_if_fail (matcher->ref > 0, NULL);
1902 g_atomic_int_inc (&matcher->ref);
1908 * g_file_attribute_matcher_unref:
1909 * @matcher: a #GFileAttributeMatcher.
1911 * Unreferences @matcher. If the reference count falls below 1,
1912 * the @matcher is automatically freed.
1916 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
1918 g_return_if_fail (matcher != NULL);
1919 g_return_if_fail (matcher->ref > 0);
1921 if (g_atomic_int_dec_and_test (&matcher->ref))
1923 if (matcher->more_sub_matchers)
1924 g_array_free (matcher->more_sub_matchers, TRUE);
1931 * g_file_attribute_matcher_matches_only:
1932 * @matcher: a #GFileAttributeMatcher.
1933 * @attribute: a file attribute key.
1935 * Checks if a attribute matcher only matches a given attribute. Always
1936 * returns %FALSE if "*" was used when creating the matcher.
1938 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
1941 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
1942 const char *attribute)
1946 g_return_val_if_fail (matcher != NULL, FALSE);
1947 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
1952 id = lookup_attribute (attribute);
1954 if (matcher->sub_matchers[0].id != 0 &&
1955 matcher->sub_matchers[1].id == 0 &&
1956 matcher->sub_matchers[0].mask == 0xffffffff &&
1957 matcher->sub_matchers[0].id == id)
1964 matcher_matches_id (GFileAttributeMatcher *matcher,
1967 SubMatcher *sub_matchers;
1970 for (i = 0; i < ON_STACK_MATCHERS; i++)
1972 if (matcher->sub_matchers[i].id == 0)
1975 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
1979 if (matcher->more_sub_matchers)
1981 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
1982 for (i = 0; i < matcher->more_sub_matchers->len; i++)
1984 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
1993 g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
1996 g_return_val_if_fail (matcher != NULL, FALSE);
2001 return matcher_matches_id (matcher, id);
2005 * g_file_attribute_matcher_matches:
2006 * @matcher: a #GFileAttributeMatcher.
2007 * @attribute: a file attribute key.
2009 * Checks if an attribute will be matched by an attribute matcher. If
2010 * the matcher was created with the "*" matching string, this function
2011 * will always return %TRUE.
2013 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2016 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2017 const char *attribute)
2019 g_return_val_if_fail (matcher != NULL, FALSE);
2020 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2025 return matcher_matches_id (matcher, lookup_attribute (attribute));
2028 /* return TRUE -> all */
2030 * g_file_attribute_matcher_enumerate_namespace:
2031 * @matcher: a #GFileAttributeMatcher.
2032 * @ns: a string containing a file attribute namespace.
2034 * Checks if the matcher will match all of the keys in a given namespace.
2035 * This will always return %TRUE if a wildcard character is in use (e.g. if
2036 * matcher was created with "std::*" and @ns is "std", or if matcher was created
2037 * using "*" and namespace is anything.)
2039 * TODO: this is awkwardly worded.
2041 * Returns: %TRUE if the matcher matches all of the entries
2042 * in the given @ns, %FALSE otherwise.
2045 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2048 SubMatcher *sub_matchers;
2052 g_return_val_if_fail (matcher != NULL, FALSE);
2053 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2058 ns_id = lookup_namespace (ns) << NS_POS;
2060 for (i = 0; i < ON_STACK_MATCHERS; i++)
2062 if (matcher->sub_matchers[i].id == ns_id)
2066 if (matcher->more_sub_matchers)
2068 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2069 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2071 if (sub_matchers[i].id == ns_id)
2076 matcher->iterator_ns = ns_id;
2077 matcher->iterator_pos = 0;
2083 * g_file_attribute_matcher_enumerate_next:
2084 * @matcher: a #GFileAttributeMatcher.
2086 * Gets the next matched attribute from a #GFileAttributeMatcher.
2088 * Returns: a string containing the next attribute or %NULL if
2089 * no more attribute exist.
2092 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2095 SubMatcher *sub_matcher;
2097 g_return_val_if_fail (matcher != NULL, NULL);
2101 i = matcher->iterator_pos++;
2103 if (i < ON_STACK_MATCHERS)
2105 if (matcher->sub_matchers[i].id == 0)
2108 sub_matcher = &matcher->sub_matchers[i];
2112 if (matcher->more_sub_matchers == NULL)
2115 i -= ON_STACK_MATCHERS;
2116 if (i < matcher->more_sub_matchers->len)
2117 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2122 if (sub_matcher->mask == 0xffffffff &&
2123 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2124 return get_attribute_for_id (sub_matcher->id);
2128 #define __G_FILE_INFO_C__
2129 #include "gioaliasdef.c"