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"
45 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
46 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
50 GFileAttributeValue value;
55 GObject parent_instance;
58 GFileAttributeMatcher *mask;
61 struct _GFileInfoClass
63 GObjectClass parent_class;
66 static gboolean g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
69 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
73 guint32 attribute_id_counter;
76 G_LOCK_DEFINE_STATIC (attribute_hash);
77 static int namespace_id_counter = 0;
78 static GHashTable *ns_hash = NULL;
79 static GHashTable *attribute_hash = NULL;
80 static char ***attributes = NULL;
82 /* Attribute ids are 32bit, we split it up like this:
83 * |------------|--------------------|
85 * namespace attribute id
87 * This way the attributes gets sorted in namespace order
91 #define NS_MASK ((guint32)((1<<12) - 1))
93 #define ID_MASK ((guint32)((1<<20) - 1))
95 #define GET_NS(_attr_id) \
96 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
97 #define GET_ID(_attr_id) \
98 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
100 #define MAKE_ATTR_ID(_ns, _id) \
101 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
102 ((((guint32) _id) & ID_MASK) << ID_POS) )
105 _lookup_namespace (const char *namespace)
109 ns_info = g_hash_table_lookup (ns_hash, namespace);
112 ns_info = g_new0 (NSInfo, 1);
113 ns_info->id = ++namespace_id_counter;
114 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
115 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
116 attributes[ns_info->id] = NULL;
122 lookup_namespace (const char *namespace)
127 G_LOCK (attribute_hash);
129 if (attribute_hash == NULL)
131 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
132 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
135 ns_info = _lookup_namespace (namespace);
140 G_UNLOCK (attribute_hash);
146 get_attribute_for_id (int attribute)
149 G_LOCK (attribute_hash);
150 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
151 G_UNLOCK (attribute_hash);
156 lookup_attribute (const char *attribute)
163 G_LOCK (attribute_hash);
164 if (attribute_hash == NULL)
166 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
167 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
170 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
174 G_UNLOCK (attribute_hash);
178 colon = strchr (attribute, ':');
180 ns = g_strndup (attribute, colon - attribute);
184 ns_info = _lookup_namespace (ns);
187 id = ++ns_info->attribute_id_counter;
188 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
189 attributes[ns_info->id][id] = g_strdup (attribute);
191 attr_id = MAKE_ATTR_ID (ns_info->id, id);
193 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
195 G_UNLOCK (attribute_hash);
201 g_file_info_finalize (GObject *object)
205 GFileAttribute *attrs;
207 info = G_FILE_INFO (object);
209 attrs = (GFileAttribute *)info->attributes->data;
210 for (i = 0; i < info->attributes->len; i++)
211 g_file_attribute_value_clear (&attrs[i].value);
212 g_array_free (info->attributes, TRUE);
214 if (info->mask != NO_ATTRIBUTE_MASK)
215 g_file_attribute_matcher_unref (info->mask);
217 if (G_OBJECT_CLASS (g_file_info_parent_class)->finalize)
218 (*G_OBJECT_CLASS (g_file_info_parent_class)->finalize) (object);
222 g_file_info_class_init (GFileInfoClass *klass)
224 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
226 gobject_class->finalize = g_file_info_finalize;
230 g_file_info_init (GFileInfo *info)
232 info->mask = NO_ATTRIBUTE_MASK;
233 info->attributes = g_array_new (FALSE, FALSE,
234 sizeof (GFileAttribute));
240 * Creates a new file info structure.
242 * Returns: a #GFileInfo.
245 g_file_info_new (void)
247 return g_object_new (G_TYPE_FILE_INFO, NULL);
251 * g_file_info_copy_into:
252 * @src_info: source to copy attributes from.
253 * @dest_info: destination to copy attributes to.
255 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
258 g_file_info_copy_into (GFileInfo *src_info, GFileInfo *dest_info)
260 GFileAttribute *source, *dest;
263 g_return_if_fail (G_IS_FILE_INFO (src_info));
264 g_return_if_fail (G_IS_FILE_INFO (dest_info));
266 dest = (GFileAttribute *)dest_info->attributes->data;
267 for (i = 0; i < dest_info->attributes->len; i++)
268 g_file_attribute_value_clear (&dest[i].value);
270 g_array_set_size (dest_info->attributes,
271 src_info->attributes->len);
273 source = (GFileAttribute *)src_info->attributes->data;
274 dest = (GFileAttribute *)dest_info->attributes->data;
276 for (i = 0; i < src_info->attributes->len; i++)
278 dest[i].attribute = source[i].attribute;
279 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
280 g_file_attribute_value_set (&dest[i].value, &source[i].value);
283 if (src_info->mask == NO_ATTRIBUTE_MASK)
284 dest_info->mask = NO_ATTRIBUTE_MASK;
286 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
291 * @other: a #GFileInfo.
293 * Duplicates a file info structure.
295 * Returns: a duplicate #GFileInfo of @other.
298 g_file_info_dup (GFileInfo *other)
302 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
304 new = g_file_info_new ();
305 g_file_info_copy_into (other, new);
310 * g_file_info_set_attribute_mask:
311 * @info: a #GFileInfo.
312 * @mask: a #GFileAttributeMatcher.
314 * Sets @mask on @info to match specific attribute types.
317 g_file_info_set_attribute_mask (GFileInfo *info,
318 GFileAttributeMatcher *mask)
320 GFileAttribute *attr;
323 g_return_if_fail (G_IS_FILE_INFO (info));
324 g_return_if_fail (mask != NULL);
326 if (mask != info->mask)
328 if (info->mask != NO_ATTRIBUTE_MASK)
329 g_file_attribute_matcher_unref (info->mask);
330 info->mask = g_file_attribute_matcher_ref (mask);
332 /* Remove non-matching attributes */
333 for (i = 0; i < info->attributes->len; i++)
335 attr = &g_array_index (info->attributes, GFileAttribute, i);
336 if (!g_file_attribute_matcher_matches_id (mask,
339 g_file_attribute_value_clear (&attr->value);
340 g_array_remove_index (info->attributes, i);
348 * g_file_info_unset_attribute_mask:
351 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
355 g_file_info_unset_attribute_mask (GFileInfo *info)
357 g_return_if_fail (G_IS_FILE_INFO (info));
359 if (info->mask != NO_ATTRIBUTE_MASK)
360 g_file_attribute_matcher_unref (info->mask);
361 info->mask = NO_ATTRIBUTE_MASK;
365 * g_file_info_clear_status:
366 * @info: a #GFileInfo.
368 * Clears the status information from @info.
371 g_file_info_clear_status (GFileInfo *info)
373 GFileAttribute *attrs;
376 g_return_if_fail (G_IS_FILE_INFO (info));
378 attrs = (GFileAttribute *)info->attributes->data;
379 for (i = 0; i < info->attributes->len; i++)
380 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
384 g_file_info_find_place (GFileInfo *info,
388 GFileAttribute *attrs;
389 /* Binary search for the place where attribute would be, if its
393 max = info->attributes->len;
395 attrs = (GFileAttribute *)info->attributes->data;
399 med = min + (max - min) / 2;
400 if (attrs[med].attribute == attribute)
405 else if (attrs[med].attribute < attribute)
407 else /* attrs[med].attribute > attribute */
414 static GFileAttributeValue *
415 g_file_info_find_value (GFileInfo *info,
418 GFileAttribute *attrs;
421 i = g_file_info_find_place (info, attr_id);
422 attrs = (GFileAttribute *)info->attributes->data;
423 if (i < info->attributes->len &&
424 attrs[i].attribute == attr_id)
425 return &attrs[i].value;
430 static GFileAttributeValue *
431 g_file_info_find_value_by_name (GFileInfo *info,
432 const char *attribute)
436 attr_id = lookup_attribute (attribute);
437 return g_file_info_find_value (info, attr_id);
441 * g_file_info_has_attribute:
442 * @info: a #GFileInfo.
443 * @attribute: a file attribute key.
445 * Checks if a file info structure has an attribute named @attribute.
447 * Returns: %TRUE if @GFileInfo has an attribute named @attribute,
451 g_file_info_has_attribute (GFileInfo *info,
452 const char *attribute)
454 GFileAttributeValue *value;
456 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
457 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
459 value = g_file_info_find_value_by_name (info, attribute);
460 return value != NULL;
464 * g_file_info_list_attributes:
465 * @info: a #GFileInfo.
466 * @name_space: a file attribute key's namespace.
468 * Lists the file info structure's attributes.
470 * Returns: a null-terminated array of strings of all of the
471 * possible attribute types for the given @name_space, or
475 g_file_info_list_attributes (GFileInfo *info,
476 const char *name_space)
479 GFileAttribute *attrs;
481 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
484 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
486 names = g_ptr_array_new ();
487 attrs = (GFileAttribute *)info->attributes->data;
488 for (i = 0; i < info->attributes->len; i++)
490 attribute = attrs[i].attribute;
491 if (ns_id == 0 || GET_NS (attribute) == ns_id)
492 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
496 g_ptr_array_add (names, NULL);
498 return (char **)g_ptr_array_free (names, FALSE);
502 * g_file_info_get_attribute_type:
503 * @info: a #GFileInfo.
504 * @attribute: a file attribute key.
506 * Gets the attribute type for an attribute key.
508 * Returns: a #GFileAttributeType for the given @attribute, or
509 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is invalid.
512 g_file_info_get_attribute_type (GFileInfo *info,
513 const char *attribute)
515 GFileAttributeValue *value;
517 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
518 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
520 value = g_file_info_find_value_by_name (info, attribute);
524 return G_FILE_ATTRIBUTE_TYPE_INVALID;
528 * g_file_info_remove_attribute:
529 * @info: a #GFileInfo.
530 * @attribute: a file attribute key.
532 * Removes all cases of @attribute from @info if it exists.
536 g_file_info_remove_attribute (GFileInfo *info,
537 const char *attribute)
540 GFileAttribute *attrs;
543 g_return_if_fail (G_IS_FILE_INFO (info));
544 g_return_if_fail (attribute != NULL && *attribute != '\0');
546 attr_id = lookup_attribute (attribute);
548 i = g_file_info_find_place (info, attr_id);
549 attrs = (GFileAttribute *)info->attributes->data;
550 if (i < info->attributes->len &&
551 attrs[i].attribute == attr_id)
553 g_file_attribute_value_clear (&attrs[i].value);
554 g_array_remove_index (info->attributes, i);
559 * g_file_info_get_attribute:
560 * @info: a #GFileInfo.
561 * @attribute: a file attribute key.
563 * Gets an attribute value from a file info structure.
565 * Returns: a #GFileAttributeValue for the given @attribute, or
568 GFileAttributeValue *
569 g_file_info_get_attribute (GFileInfo *info,
570 const char *attribute)
573 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
574 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
576 return g_file_info_find_value_by_name (info, attribute);
580 * g_file_info_get_attribute_object:
581 * @info: a #GFileInfo.
582 * @attribute: a file attribute key.
584 * Gets the value of a #GObject attribute. If the attribute does
585 * not contain a #GObject, %NULL will be returned.
587 * Returns: a #GObject associated with the given @attribute, or
591 g_file_info_get_attribute_object (GFileInfo *info,
592 const char *attribute)
594 GFileAttributeValue *value;
596 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
597 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
599 value = g_file_info_find_value_by_name (info, attribute);
600 return g_file_attribute_value_get_object (value);
604 * g_file_info_get_attribute_string:
605 * @info: a #GFileInfo.
606 * @attribute: a file attribute key.
608 * Gets the value of a string attribute. If the attribute does
609 * not contain a string, %NULL will be returned.
611 * Returns: the contents of the @attribute value as a string, or
615 g_file_info_get_attribute_string (GFileInfo *info,
616 const char *attribute)
618 GFileAttributeValue *value;
620 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
621 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
623 value = g_file_info_find_value_by_name (info, attribute);
624 return g_file_attribute_value_get_string (value);
628 * g_file_info_get_attribute_byte_string:
629 * @info: a #GFileInfo.
630 * @attribute: a file attribute key.
632 * Gets the value of a byte string attribute. If the attribute does
633 * not contain a byte string, %NULL will be returned.
635 * Returns: the contents of the @attribute value as a byte string, or
639 g_file_info_get_attribute_byte_string (GFileInfo *info,
640 const char *attribute)
642 GFileAttributeValue *value;
644 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
645 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
647 value = g_file_info_find_value_by_name (info, attribute);
648 return g_file_attribute_value_get_byte_string (value);
652 * g_file_info_get_attribute_boolean:
653 * @info: a #GFileInfo.
654 * @attribute: a file attribute key.
656 * Gets the value of a boolean attribute. If the attribute does not
657 * contain a boolean value, %FALSE will be returned.
659 * Returns: the boolean value contained within the attribute.
662 g_file_info_get_attribute_boolean (GFileInfo *info,
663 const char *attribute)
665 GFileAttributeValue *value;
667 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
668 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
670 value = g_file_info_find_value_by_name (info, attribute);
671 return g_file_attribute_value_get_boolean (value);
675 * g_file_info_get_attribute_uint32:
676 * @info: a #GFileInfo.
677 * @attribute: a file attribute key.
679 * Gets an unsigned 32-bit integer contained within the attribute. If the
680 * attribute does not contain an unsigned 32-bit integer, or is invalid,
681 * %0 will be returned.
683 * Returns: an unsigned 32-bit integer from the attribute.
686 g_file_info_get_attribute_uint32 (GFileInfo *info,
687 const char *attribute)
689 GFileAttributeValue *value;
691 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
692 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
694 value = g_file_info_find_value_by_name (info, attribute);
695 return g_file_attribute_value_get_uint32 (value);
699 * g_file_info_get_attribute_int32:
700 * @info: a #GFileInfo.
701 * @attribute: a file attribute key.
703 * Gets a signed 32-bit integer contained within the attribute. If the
704 * attribute does not contain a signed 32-bit integer, or is invalid,
705 * %0 will be returned.
707 * Returns: a signed 32-bit integer from the attribute.
710 g_file_info_get_attribute_int32 (GFileInfo *info,
711 const char *attribute)
713 GFileAttributeValue *value;
715 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
716 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
718 value = g_file_info_find_value_by_name (info, attribute);
719 return g_file_attribute_value_get_int32 (value);
723 * g_file_info_get_attribute_uint64:
724 * @info: a #GFileInfo.
725 * @attribute: a file attribute key.
727 * Gets a unsigned 64-bit integer contained within the attribute. If the
728 * attribute does not contain an unsigned 64-bit integer, or is invalid,
729 * %0 will be returned.
731 * Returns: a unsigned 64-bit integer from the attribute.
734 g_file_info_get_attribute_uint64 (GFileInfo *info,
735 const char *attribute)
737 GFileAttributeValue *value;
739 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
740 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
742 value = g_file_info_find_value_by_name (info, attribute);
743 return g_file_attribute_value_get_uint64 (value);
747 * g_file_info_get_attribute_int64:
748 * @info: a #GFileInfo.
749 * @attribute: a file attribute key.
751 * Gets a signed 64-bit integer contained within the attribute. If the
752 * attribute does not contain an signed 64-bit integer, or is invalid,
753 * %0 will be returned.
755 * Returns: a signed 64-bit integer from the attribute.
758 g_file_info_get_attribute_int64 (GFileInfo *info,
759 const char *attribute)
761 GFileAttributeValue *value;
763 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
764 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
766 value = g_file_info_find_value_by_name (info, attribute);
767 return g_file_attribute_value_get_int64 (value);
770 static GFileAttributeValue *
771 g_file_info_create_value (GFileInfo *info,
774 GFileAttribute *attrs;
778 if (info->mask != NO_ATTRIBUTE_MASK &&
779 !g_file_attribute_matcher_matches_id (info->mask, attr_id))
782 i = g_file_info_find_place (info, attr_id);
784 attrs = (GFileAttribute *)info->attributes->data;
785 if (i < info->attributes->len &&
786 attrs[i].attribute == attr_id)
787 return &attrs[i].value;
790 attr.attribute = attr_id;
791 attr.value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
792 g_array_insert_val (info->attributes, i, attr);
794 attrs = (GFileAttribute *)info->attributes->data;
795 return &attrs[i].value;
799 static GFileAttributeValue *
800 g_file_info_create_value_by_name (GFileInfo *info,
801 const char *attribute)
805 attr_id = lookup_attribute (attribute);
807 return g_file_info_create_value (info, attr_id);
811 * g_file_info_set_attribute:
812 * @info: a #GFileInfo.
813 * @attribute: a file attribute key.
814 * @attr_value: a #GFileAttributeValue.
816 * Sets the @attribute to contain the given @attr_value,
821 g_file_info_set_attribute (GFileInfo *info,
822 const char *attribute,
823 const GFileAttributeValue *attr_value)
825 GFileAttributeValue *value;
827 g_return_if_fail (G_IS_FILE_INFO (info));
828 g_return_if_fail (attribute != NULL && *attribute != '\0');
829 g_return_if_fail (attr_value != NULL);
831 value = g_file_info_create_value_by_name (info, attribute);
833 g_file_attribute_value_set (value, attr_value);
837 * g_file_info_set_attribute_object:
838 * @info: a #GFileInfo.
839 * @attribute: a file attribute key.
840 * @attr_value: a #GObject.
842 * Sets the @attribute to contain the given @attr_value,
847 g_file_info_set_attribute_object (GFileInfo *info,
848 const char *attribute,
851 GFileAttributeValue *value;
853 g_return_if_fail (G_IS_FILE_INFO (info));
854 g_return_if_fail (attribute != NULL && *attribute != '\0');
855 g_return_if_fail (G_IS_OBJECT (attr_value));
857 value = g_file_info_create_value_by_name (info, attribute);
859 g_file_attribute_value_set_object (value, attr_value);
863 * g_file_info_set_attribute_string:
864 * @info: a #GFileInfo.
865 * @attribute: a file attribute key.
866 * @attr_value: a string.
868 * Sets the @attribute to contain the given @attr_value,
873 g_file_info_set_attribute_string (GFileInfo *info,
874 const char *attribute,
875 const char *attr_value)
877 GFileAttributeValue *value;
879 g_return_if_fail (G_IS_FILE_INFO (info));
880 g_return_if_fail (attribute != NULL && *attribute != '\0');
881 g_return_if_fail (attr_value != NULL);
883 value = g_file_info_create_value_by_name (info, attribute);
885 g_file_attribute_value_set_string (value, attr_value);
889 * g_file_info_set_attribute_byte_string:
890 * @info: a #GFileInfo.
891 * @attribute: a file attribute key.
892 * @attr_value: a byte string.
894 * Sets the @attribute to contain the given @attr_value,
899 g_file_info_set_attribute_byte_string (GFileInfo *info,
900 const char *attribute,
901 const char *attr_value)
903 GFileAttributeValue *value;
905 g_return_if_fail (G_IS_FILE_INFO (info));
906 g_return_if_fail (attribute != NULL && *attribute != '\0');
907 g_return_if_fail (attr_value != NULL);
909 value = g_file_info_create_value_by_name (info, attribute);
911 g_file_attribute_value_set_byte_string (value, attr_value);
915 * g_file_info_set_attribute_boolean:
916 * @info: a #GFileInfo.
917 * @attribute: a file attribute key.
918 * @attr_value: a boolean value.
920 * Sets the @attribute to contain the given @attr_value,
925 g_file_info_set_attribute_boolean (GFileInfo *info,
926 const char *attribute,
929 GFileAttributeValue *value;
931 g_return_if_fail (G_IS_FILE_INFO (info));
932 g_return_if_fail (attribute != NULL && *attribute != '\0');
934 value = g_file_info_create_value_by_name (info, attribute);
936 g_file_attribute_value_set_boolean (value, attr_value);
940 * g_file_info_set_attribute_uint32:
941 * @info: a #GFileInfo.
942 * @attribute: a file attribute key.
943 * @attr_value: an unsigned 32-bit integer.
945 * Sets the @attribute to contain the given @attr_value,
951 g_file_info_set_attribute_uint32 (GFileInfo *info,
952 const char *attribute,
955 GFileAttributeValue *value;
957 g_return_if_fail (G_IS_FILE_INFO (info));
958 g_return_if_fail (attribute != NULL && *attribute != '\0');
960 value = g_file_info_create_value_by_name (info, attribute);
962 g_file_attribute_value_set_uint32 (value, attr_value);
967 * g_file_info_set_attribute_int32:
968 * @info: a #GFileInfo.
969 * @attribute: a file attribute key.
970 * @attr_value: a signed 32-bit integer
972 * Sets the @attribute to contain the given @attr_value,
977 g_file_info_set_attribute_int32 (GFileInfo *info,
978 const char *attribute,
981 GFileAttributeValue *value;
983 g_return_if_fail (G_IS_FILE_INFO (info));
984 g_return_if_fail (attribute != NULL && *attribute != '\0');
986 value = g_file_info_create_value_by_name (info, attribute);
988 g_file_attribute_value_set_int32 (value, attr_value);
992 * g_file_info_set_attribute_uint64:
993 * @info: a #GFileInfo.
994 * @attribute: a file attribute key.
995 * @attr_value: an unsigned 64-bit integer.
997 * Sets the @attribute to contain the given @attr_value,
1002 g_file_info_set_attribute_uint64 (GFileInfo *info,
1003 const char *attribute,
1006 GFileAttributeValue *value;
1008 g_return_if_fail (G_IS_FILE_INFO (info));
1009 g_return_if_fail (attribute != NULL && *attribute != '\0');
1011 value = g_file_info_create_value_by_name (info, attribute);
1013 g_file_attribute_value_set_uint64 (value, attr_value);
1017 * g_file_info_set_attribute_int64:
1018 * @info: a #GFileInfo.
1019 * @attribute: attribute name to set.
1020 * @attr_value: int64 value to set attribute to.
1022 * Sets the @attribute to contain the given @attr_value,
1027 g_file_info_set_attribute_int64 (GFileInfo *info,
1028 const char *attribute,
1031 GFileAttributeValue *value;
1033 g_return_if_fail (G_IS_FILE_INFO (info));
1034 g_return_if_fail (attribute != NULL && *attribute != '\0');
1036 value = g_file_info_create_value_by_name (info, attribute);
1038 g_file_attribute_value_set_int64 (value, attr_value);
1041 /* Helper getters */
1043 * g_file_info_get_file_type:
1044 * @info: a #GFileInfo.
1046 * Gets a file's type (whether it is a regular file, symlink, etc).
1047 * This is different from the file's content type, see g_file_info_get_content_type().
1049 * Returns: a #GFileType for the given file.
1052 g_file_info_get_file_type (GFileInfo *info)
1054 static guint32 attr = 0;
1055 GFileAttributeValue *value;
1057 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1060 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_TYPE);
1062 value = g_file_info_find_value (info, attr);
1063 return (GFileType)g_file_attribute_value_get_uint32 (value);
1067 * g_file_info_get_is_hidden:
1068 * @info: a #GFileInfo.
1070 * Checks if a file is hidden.
1072 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1075 g_file_info_get_is_hidden (GFileInfo *info)
1077 static guint32 attr = 0;
1078 GFileAttributeValue *value;
1080 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1083 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_IS_HIDDEN);
1085 value = g_file_info_find_value (info, attr);
1086 return (GFileType)g_file_attribute_value_get_boolean (value);
1090 * g_file_info_get_is_backup:
1091 * @info: a #GFileInfo.
1093 * Checks if a file is a backup file.
1095 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1098 g_file_info_get_is_backup (GFileInfo *info)
1100 static guint32 attr = 0;
1101 GFileAttributeValue *value;
1103 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1106 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_IS_BACKUP);
1108 value = g_file_info_find_value (info, attr);
1109 return (GFileType)g_file_attribute_value_get_boolean (value);
1113 * g_file_info_get_is_symlink:
1114 * @info: a #GFileInfo.
1116 * Checks if a file is a symlink.
1118 * Returns: %TRUE if the given @info is a symlink.
1121 g_file_info_get_is_symlink (GFileInfo *info)
1123 static guint32 attr = 0;
1124 GFileAttributeValue *value;
1126 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1129 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_IS_SYMLINK);
1131 value = g_file_info_find_value (info, attr);
1132 return (GFileType)g_file_attribute_value_get_boolean (value);
1136 * g_file_info_get_name:
1137 * @info: a #GFileInfo.
1139 * Gets the name for a file.
1141 * Returns: a string containing the file name.
1144 g_file_info_get_name (GFileInfo *info)
1146 static guint32 attr = 0;
1147 GFileAttributeValue *value;
1149 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1152 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_NAME);
1154 value = g_file_info_find_value (info, attr);
1155 return g_file_attribute_value_get_byte_string (value);
1159 * g_file_info_get_display_name:
1160 * @info: a #GFileInfo.
1162 * Gets a display name for a file.
1164 * Returns: a string containing the display name.
1167 g_file_info_get_display_name (GFileInfo *info)
1169 static guint32 attr = 0;
1170 GFileAttributeValue *value;
1172 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1175 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_DISPLAY_NAME);
1177 value = g_file_info_find_value (info, attr);
1178 return g_file_attribute_value_get_string (value);
1182 * g_file_info_get_edit_name:
1183 * @info: a #GFileInfo.
1185 * Gets the edit name for a file.
1187 * Returns: a string containing the edit name.
1190 g_file_info_get_edit_name (GFileInfo *info)
1192 static guint32 attr = 0;
1193 GFileAttributeValue *value;
1195 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1198 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_EDIT_NAME);
1200 value = g_file_info_find_value (info, attr);
1201 return g_file_attribute_value_get_string (value);
1205 * g_file_info_get_icon:
1206 * @info: a #GFileInfo.
1208 * Gets the icon for a file.
1210 * Returns: #GIcon for the given @info.
1213 g_file_info_get_icon (GFileInfo *info)
1215 static guint32 attr = 0;
1216 GFileAttributeValue *value;
1219 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1222 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_ICON);
1224 value = g_file_info_find_value (info, attr);
1225 obj = g_file_attribute_value_get_object (value);
1226 if (obj != NULL && G_IS_ICON (obj))
1227 return G_ICON (obj);
1232 * g_file_info_get_content_type:
1233 * @info: a #GFileInfo.
1235 * Gets the file's content type.
1237 * Returns: a string containing the file's content type.s
1240 g_file_info_get_content_type (GFileInfo *info)
1242 static guint32 attr = 0;
1243 GFileAttributeValue *value;
1245 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1248 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_CONTENT_TYPE);
1250 value = g_file_info_find_value (info, attr);
1251 return g_file_attribute_value_get_string (value);
1255 * g_file_info_get_size:
1256 * @info: a #GFileInfo.
1258 * Gets the file's size.
1260 * Returns: a #goffset containing the file's size.
1263 g_file_info_get_size (GFileInfo *info)
1265 static guint32 attr = 0;
1266 GFileAttributeValue *value;
1268 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1271 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_SIZE);
1273 value = g_file_info_find_value (info, attr);
1274 return (goffset) g_file_attribute_value_get_uint64 (value);
1278 * g_file_info_get_modification_time:
1279 * @info: a #GFileInfo.
1280 * @result: a #GTimeVal.
1282 * Gets the modification time of the current @info and sets it
1288 g_file_info_get_modification_time (GFileInfo *info,
1291 static guint32 attr_mtime = 0, attr_mtime_usec;
1292 GFileAttributeValue *value;
1294 g_return_if_fail (G_IS_FILE_INFO (info));
1295 g_return_if_fail (result != NULL);
1297 if (attr_mtime == 0)
1299 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1300 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1303 value = g_file_info_find_value (info, attr_mtime);
1304 result->tv_sec = g_file_attribute_value_get_uint64 (value);
1305 value = g_file_info_find_value (info, attr_mtime_usec);
1306 result->tv_usec = g_file_attribute_value_get_uint32 (value);
1310 * g_file_info_get_symlink_target:
1311 * @info: a #GFileInfo.
1313 * Gets the symlink target for a given #GFileInfo.
1315 * Returns: a string containing the symlink target.
1318 g_file_info_get_symlink_target (GFileInfo *info)
1320 static guint32 attr = 0;
1321 GFileAttributeValue *value;
1323 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1326 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET);
1328 value = g_file_info_find_value (info, attr);
1329 return g_file_attribute_value_get_byte_string (value);
1333 * g_file_info_get_etag:
1334 * @info: a #GFileInfo.
1336 * Gets the entity tag for a given #GFileInfo.
1337 * See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1339 * Returns: a string containing the value of the "etag:value" attribute.
1342 g_file_info_get_etag (GFileInfo *info)
1344 static guint32 attr = 0;
1345 GFileAttributeValue *value;
1347 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1350 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1352 value = g_file_info_find_value (info, attr);
1353 return g_file_attribute_value_get_string (value);
1357 * g_file_info_get_sort_order:
1358 * @info: a #GFileInfo.
1360 * Gets the value of the sort_order attribute from the #GFileInfo.
1361 * See %G_FILE_ATTRIBUTE_STD_SORT_ORDER.
1363 * Returns: a #gint32 containing the value of the "std:sort_order" attribute.
1366 g_file_info_get_sort_order (GFileInfo *info)
1368 static guint32 attr = 0;
1369 GFileAttributeValue *value;
1371 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1374 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_SORT_ORDER);
1376 value = g_file_info_find_value (info, attr);
1377 return g_file_attribute_value_get_int32 (value);
1380 /* Helper setters: */
1382 * g_file_info_set_file_type:
1383 * @info: a #GFileInfo.
1384 * @type: a #GFileType.
1386 * Sets the file type in a #GFileInfo to @type.
1387 * See %G_FILE_ATTRIBUTE_STD_TYPE.
1390 g_file_info_set_file_type (GFileInfo *info,
1393 static guint32 attr = 0;
1394 GFileAttributeValue *value;
1396 g_return_if_fail (G_IS_FILE_INFO (info));
1399 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_TYPE);
1401 value = g_file_info_create_value (info, attr);
1403 g_file_attribute_value_set_uint32 (value, type);
1407 * g_file_info_set_is_hidden:
1408 * @info: a #GFileInfo.
1409 * @is_hidden: a #gboolean.
1411 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1412 * See %G_FILE_ATTRIBUTE_STD_IS_HIDDEN.
1415 g_file_info_set_is_hidden (GFileInfo *info,
1418 static guint32 attr = 0;
1419 GFileAttributeValue *value;
1421 g_return_if_fail (G_IS_FILE_INFO (info));
1424 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_IS_HIDDEN);
1426 value = g_file_info_create_value (info, attr);
1428 g_file_attribute_value_set_boolean (value, is_hidden);
1432 * g_file_info_set_is_symlink:
1433 * @info: a #GFileInfo.
1434 * @is_symlink: a #gboolean.
1436 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1437 * See %G_FILE_ATTRIBUTE_STD_IS_SYMLINK.
1441 g_file_info_set_is_symlink (GFileInfo *info,
1442 gboolean is_symlink)
1444 static guint32 attr = 0;
1445 GFileAttributeValue *value;
1447 g_return_if_fail (G_IS_FILE_INFO (info));
1450 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_IS_SYMLINK);
1452 value = g_file_info_create_value (info, attr);
1454 g_file_attribute_value_set_boolean (value, is_symlink);
1458 * g_file_info_set_name:
1459 * @info: a #GFileInfo.
1460 * @name: a string containing a name.
1462 * Sets the name attribute for the current #GFileInfo.
1463 * See %G_FILE_ATTRIBUTE_STD_NAME.
1467 g_file_info_set_name (GFileInfo *info,
1470 static guint32 attr = 0;
1471 GFileAttributeValue *value;
1473 g_return_if_fail (G_IS_FILE_INFO (info));
1474 g_return_if_fail (name != NULL);
1477 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_NAME);
1479 value = g_file_info_create_value (info, attr);
1481 g_file_attribute_value_set_byte_string (value, name);
1485 * g_file_info_set_display_name:
1486 * @info: a #GFileInfo.
1487 * @display_name: a string containing a display name.
1489 * Sets the display name for the current #GFileInfo.
1490 * See %G_FILE_ATTRIBUTE_STD_DISPLAY_NAME.
1494 g_file_info_set_display_name (GFileInfo *info,
1495 const char *display_name)
1497 static guint32 attr = 0;
1498 GFileAttributeValue *value;
1500 g_return_if_fail (G_IS_FILE_INFO (info));
1501 g_return_if_fail (display_name != NULL);
1504 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_DISPLAY_NAME);
1506 value = g_file_info_create_value (info, attr);
1508 g_file_attribute_value_set_string (value, display_name);
1512 * g_file_info_set_edit_name:
1513 * @info: a #GFileInfo.
1514 * @edit_name: a string containing an edit name.
1516 * Sets the edit name for the current file.
1517 * See %G_FILE_ATTRIBUTE_STD_EDIT_NAME.
1522 g_file_info_set_edit_name (GFileInfo *info,
1523 const char *edit_name)
1525 static guint32 attr = 0;
1526 GFileAttributeValue *value;
1528 g_return_if_fail (G_IS_FILE_INFO (info));
1529 g_return_if_fail (edit_name != NULL);
1532 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_EDIT_NAME);
1534 value = g_file_info_create_value (info, attr);
1536 g_file_attribute_value_set_string (value, edit_name);
1540 * g_file_info_set_icon:
1541 * @info: a #GFileInfo.
1544 * Sets the icon for a given #GFileInfo.
1545 * See %G_FILE_ATTRIBUTE_STD_ICON.
1549 g_file_info_set_icon (GFileInfo *info,
1552 static guint32 attr = 0;
1553 GFileAttributeValue *value;
1555 g_return_if_fail (G_IS_FILE_INFO (info));
1556 g_return_if_fail (G_IS_ICON (icon));
1559 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_ICON);
1561 value = g_file_info_create_value (info, attr);
1563 g_file_attribute_value_set_object (value, G_OBJECT (icon));
1567 * g_file_info_set_content_type:
1568 * @info: a #GFileInfo.
1569 * @content_type: a content type. See #GContentType.
1571 * Sets the content type attribute for a given #GFileInfo.
1572 * See %G_FILE_ATTRIBUTE_STD_CONTENT_TYPE.
1576 g_file_info_set_content_type (GFileInfo *info,
1577 const char *content_type)
1579 static guint32 attr = 0;
1580 GFileAttributeValue *value;
1582 g_return_if_fail (G_IS_FILE_INFO (info));
1583 g_return_if_fail (content_type != NULL);
1586 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_CONTENT_TYPE);
1588 value = g_file_info_create_value (info, attr);
1590 g_file_attribute_value_set_string (value, content_type);
1594 * g_file_info_set_size:
1595 * @info: a #GFileInfo.
1596 * @size: a #goffset containing the file's size.
1598 * Sets the %G_FILE_ATTRIBUTE_STD_SIZE attribute in the file info
1599 * to the given size.
1603 g_file_info_set_size (GFileInfo *info,
1606 static guint32 attr = 0;
1607 GFileAttributeValue *value;
1609 g_return_if_fail (G_IS_FILE_INFO (info));
1612 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_SIZE);
1614 value = g_file_info_create_value (info, attr);
1616 g_file_attribute_value_set_uint64 (value, size);
1620 * g_file_info_set_modification_time
1621 * @info: a #GFileInfo.
1622 * @mtime: a #GTimeVal.
1624 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
1625 * info to the given time value.
1629 g_file_info_set_modification_time (GFileInfo *info,
1632 static guint32 attr_mtime = 0, attr_mtime_usec;
1633 GFileAttributeValue *value;
1635 g_return_if_fail (G_IS_FILE_INFO (info));
1636 g_return_if_fail (mtime != NULL);
1638 if (attr_mtime == 0)
1640 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1641 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1644 value = g_file_info_create_value (info, attr_mtime);
1646 g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
1647 value = g_file_info_create_value (info, attr_mtime_usec);
1649 g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
1653 * g_file_info_set_symlink_target:
1654 * @info: a #GFileInfo.
1655 * @symlink_target: a static string containing a path to a symlink target.
1657 * Sets the %G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET attribute in the file info
1658 * to the given symlink target.
1662 g_file_info_set_symlink_target (GFileInfo *info,
1663 const char *symlink_target)
1665 static guint32 attr = 0;
1666 GFileAttributeValue *value;
1668 g_return_if_fail (G_IS_FILE_INFO (info));
1669 g_return_if_fail (symlink_target != NULL);
1672 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET);
1674 value = g_file_info_create_value (info, attr);
1676 g_file_attribute_value_set_byte_string (value, symlink_target);
1680 * g_file_info_set_sort_order:
1681 * @info: a #GFileInfo.
1682 * @sort_order: a sort order integer.
1684 * Sets the sort order attribute in the file info structure. See
1685 * %G_FILE_ATTRIBUTE_STD_SORT_ORDER.
1689 g_file_info_set_sort_order (GFileInfo *info,
1692 static guint32 attr = 0;
1693 GFileAttributeValue *value;
1695 g_return_if_fail (G_IS_FILE_INFO (info));
1698 attr = lookup_attribute (G_FILE_ATTRIBUTE_STD_SORT_ORDER);
1700 value = g_file_info_create_value (info, attr);
1702 g_file_attribute_value_set_int32 (value, sort_order);
1706 #define KILOBYTE_FACTOR 1024.0
1707 #define MEGABYTE_FACTOR (1024.0 * 1024.0)
1708 #define GIGABYTE_FACTOR (1024.0 * 1024.0 * 1024.0)
1711 * g_format_file_size_for_display:
1712 * @size: a file size.
1714 * Formats a file size into a human readable string. Sizes are rounded
1715 * to the nearest metric prefix and are displayed rounded to the nearest
1716 * tenth. E.g. the file size 3292528 bytes will be converted into the string
1719 * Returns: a formatted string containing a human readable file size.
1722 g_format_file_size_for_display (goffset size)
1724 if (size < (goffset) KILOBYTE_FACTOR)
1725 return g_strdup_printf (dngettext(GETTEXT_PACKAGE, "%u byte", "%u bytes",(guint) size), (guint) size);
1728 gdouble displayed_size;
1730 if (size < (goffset) MEGABYTE_FACTOR)
1732 displayed_size = (gdouble) size / KILOBYTE_FACTOR;
1733 return g_strdup_printf (_("%.1f KB"), displayed_size);
1735 else if (size < (goffset) GIGABYTE_FACTOR)
1737 displayed_size = (gdouble) size / MEGABYTE_FACTOR;
1738 return g_strdup_printf (_("%.1f MB"), displayed_size);
1742 displayed_size = (gdouble) size / GIGABYTE_FACTOR;
1743 return g_strdup_printf (_("%.1f GB"), displayed_size);
1748 #define ON_STACK_MATCHERS 5
1755 struct _GFileAttributeMatcher {
1757 SubMatcher sub_matchers[ON_STACK_MATCHERS];
1758 GArray *more_sub_matchers;
1761 guint32 iterator_ns;
1767 matcher_add (GFileAttributeMatcher *matcher,
1768 guint id, guint mask)
1770 SubMatcher *sub_matchers;
1774 for (i = 0; i < ON_STACK_MATCHERS; i++)
1776 /* First empty spot, not found, use this */
1777 if (matcher->sub_matchers[i].id == 0)
1779 matcher->sub_matchers[i].id = id;
1780 matcher->sub_matchers[i].mask = mask;
1785 if (matcher->sub_matchers[i].id == id &&
1786 matcher->sub_matchers[i].mask == mask)
1790 if (matcher->more_sub_matchers == NULL)
1791 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
1793 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
1794 for (i = 0; i < matcher->more_sub_matchers->len; i++)
1797 if (sub_matchers[i].id == id &&
1798 sub_matchers[i].mask == mask)
1805 g_array_append_val (matcher->more_sub_matchers, s);
1809 * g_file_attribute_matcher_new:
1810 * @attributes: an attribute string to match.
1812 * Creates a new file attribute matcher, which matches attributes against
1813 * a given string. #GFileAttributeMatcher<!-- -->s are reference counted structures,
1814 * and are created with a reference count of 1. If the number of references
1815 * falls to 0, the #GFileAttributeMatcher is automatically destroyed.
1817 * The @attribute string should be formatted with specific keys separated
1818 * from namespaces with a colon. Several "namespace:key" strings may be
1819 * concatenated with a single comma (e.g. "std:type,std:is_hidden").
1820 * The wildcard "*" may be used to match all keys and namespaces, or "namespace:*" will
1821 * match all keys in a given namespace.
1823 * Examples of strings to use:
1825 * <title>File Attribute Matcher strings and results</title>
1826 * <tgroup cols='2' align='left'><thead>
1827 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
1829 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
1830 * <row><entry>"std:is_hidden"</entry><entry>matches only the key is_hidden in the std namespace.</entry></row>
1831 * <row><entry>"std:type,unix:*"</entry><entry>matches the type key in the std namespace and all keys in the unix
1832 * namespace.</entry></row>
1836 * Returns: a #GFileAttributeMatcher.
1838 GFileAttributeMatcher *
1839 g_file_attribute_matcher_new (const char *attributes)
1844 GFileAttributeMatcher *matcher;
1846 if (attributes == NULL || *attributes == '\0')
1849 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
1852 split = g_strsplit (attributes, ",", -1);
1854 for (i = 0; split[i] != NULL; i++)
1856 if (strcmp (split[i], "*") == 0)
1857 matcher->all = TRUE;
1862 colon = strchr (split[i], ':');
1863 if (colon != NULL &&
1868 id = lookup_attribute (split[i]);
1876 id = lookup_namespace (split[i]) << NS_POS;
1877 mask = NS_MASK << NS_POS;
1880 matcher_add (matcher, id, mask);
1890 * g_file_attribute_matcher_ref:
1891 * @matcher: a #GFileAttributeMatcher.
1893 * References a file attribute matcher.
1895 * Returns: a #GFileAttributeMatcher.
1897 GFileAttributeMatcher *
1898 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
1900 g_return_val_if_fail (matcher != NULL, NULL);
1901 g_return_val_if_fail (matcher->ref > 0, NULL);
1903 g_atomic_int_inc (&matcher->ref);
1909 * g_file_attribute_matcher_unref:
1910 * @matcher: a #GFileAttributeMatcher.
1912 * Unreferences @matcher. If the reference count falls below 1,
1913 * the @matcher is automatically freed.
1917 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
1919 g_return_if_fail (matcher != NULL);
1920 g_return_if_fail (matcher->ref > 0);
1922 if (g_atomic_int_dec_and_test (&matcher->ref))
1924 if (matcher->more_sub_matchers)
1925 g_array_free (matcher->more_sub_matchers, TRUE);
1932 * g_file_attribute_matcher_matches_only:
1933 * @matcher: a #GFileAttributeMatcher.
1934 * @attribute: a file attribute key.
1936 * Checks if a attribute matcher only matches a given attribute. Always
1937 * returns %FALSE if "*" was used when creating the matcher.
1939 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
1942 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
1943 const char *attribute)
1947 g_return_val_if_fail (matcher != NULL, FALSE);
1948 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
1953 id = lookup_attribute (attribute);
1955 if (matcher->sub_matchers[0].id != 0 &&
1956 matcher->sub_matchers[1].id == 0 &&
1957 matcher->sub_matchers[0].mask == 0xffffffff &&
1958 matcher->sub_matchers[0].id == id)
1965 matcher_matches_id (GFileAttributeMatcher *matcher,
1968 SubMatcher *sub_matchers;
1971 for (i = 0; i < ON_STACK_MATCHERS; i++)
1973 if (matcher->sub_matchers[i].id == 0)
1976 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
1980 if (matcher->more_sub_matchers)
1982 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
1983 for (i = 0; i < matcher->more_sub_matchers->len; i++)
1985 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
1994 g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
1997 g_return_val_if_fail (matcher != NULL, FALSE);
2002 return matcher_matches_id (matcher, id);
2006 * g_file_attribute_matcher_matches:
2007 * @matcher: a #GFileAttributeMatcher.
2008 * @attribute: a file attribute key.
2010 * Checks if an attribute will be matched by an attribute matcher. If
2011 * the matcher was created with the "*" matching string, this function
2012 * will always return %TRUE.
2014 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2017 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2018 const char *attribute)
2020 g_return_val_if_fail (matcher != NULL, FALSE);
2021 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2026 return matcher_matches_id (matcher, lookup_attribute (attribute));
2029 /* return TRUE -> all */
2031 * g_file_attribute_matcher_enumerate_namespace:
2032 * @matcher: a #GFileAttributeMatcher.
2033 * @ns: a string containing a file attribute namespace.
2035 * Checks if the matcher will match all of the keys in a given namespace.
2036 * This will always return %TRUE if a wildcard character is in use (e.g. if
2037 * matcher was created with "std:*" and @ns is "std", or if matcher was created
2038 * using "*" and namespace is anything.)
2040 * TODO: this is awkwardly worded.
2042 * Returns: %TRUE if the matcher matches all of the entries
2043 * in the given @ns, %FALSE otherwise.
2046 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2049 SubMatcher *sub_matchers;
2053 g_return_val_if_fail (matcher != NULL, FALSE);
2054 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2059 ns_id = lookup_namespace (ns) << NS_POS;
2061 for (i = 0; i < ON_STACK_MATCHERS; i++)
2063 if (matcher->sub_matchers[i].id == ns_id)
2067 if (matcher->more_sub_matchers)
2069 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2070 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2072 if (sub_matchers[i].id == ns_id)
2077 matcher->iterator_ns = ns_id;
2078 matcher->iterator_pos = 0;
2084 * g_file_attribute_matcher_enumerate_next:
2085 * @matcher: a #GFileAttributeMatcher.
2087 * Gets the next matched attribute from a #GFileAttributeMatcher.
2089 * Returns: a string containing the next attribute or %NULL if
2090 * no more attribute exist.
2093 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2096 SubMatcher *sub_matcher;
2098 g_return_val_if_fail (matcher != NULL, NULL);
2102 i = matcher->iterator_pos++;
2104 if (i < ON_STACK_MATCHERS)
2106 if (matcher->sub_matchers[i].id == 0)
2109 sub_matcher = &matcher->sub_matchers[i];
2113 if (matcher->more_sub_matchers == NULL)
2116 i -= ON_STACK_MATCHERS;
2117 if (i < matcher->more_sub_matchers->len)
2118 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2123 if (sub_matcher->mask == 0xffffffff &&
2124 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2125 return get_attribute_for_id (sub_matcher->id);