1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 * @short_description: File Information and Attributes
27 * @see_also: #GFile, <link linkend="gio-GFileAttribute">GFileAttribute</link>
29 * Functionality for manipulating basic metadata for files. #GFileInfo
30 * implements methods for getting information that all files should
31 * contain, and allows for manipulation of extended attributes.
33 * See <link linkend="gio-GFileAttribute">GFileAttribute</link> for more
34 * information on how GIO handles file attributes.
36 * To obtain a #GFileInfo for a #GFile, use g_file_query_info() (or its
37 * async variant). To obtain a #GFileInfo for a file input or output
38 * stream, use g_file_input_stream_query_info() or
39 * g_file_output_stream_query_info() (or their async variants).
41 * To change the actual attributes of a file, you should then set the
42 * attribute in the #GFileInfo and call g_file_set_attributes_from_info()
43 * or g_file_set_attributes_async() on a GFile.
45 * However, not all attributes can be changed in the file. For instance,
46 * the actual size of a file cannot be changed via g_file_info_set_size().
47 * You may call g_file_query_settable_attributes() and
48 * g_file_query_writable_namespaces() to discover the settable attributes
49 * of a particular file at runtime.
51 * #GFileAttributeMatcher allows for searching through a #GFileInfo for
59 #include "gfileinfo.h"
60 #include "gfileattribute-priv.h"
66 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
67 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
71 GFileAttributeValue value;
76 GObject parent_instance;
79 GFileAttributeMatcher *mask;
82 struct _GFileInfoClass
84 GObjectClass parent_class;
88 static gboolean g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
91 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
95 guint32 attribute_id_counter;
98 G_LOCK_DEFINE_STATIC (attribute_hash);
99 static int namespace_id_counter = 0;
100 static GHashTable *ns_hash = NULL;
101 static GHashTable *attribute_hash = NULL;
102 static char ***attributes = NULL;
104 /* Attribute ids are 32bit, we split it up like this:
105 * |------------|--------------------|
107 * namespace attribute id
109 * This way the attributes gets sorted in namespace order
113 #define NS_MASK ((guint32)((1<<12) - 1))
115 #define ID_MASK ((guint32)((1<<20) - 1))
117 #define GET_NS(_attr_id) \
118 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
119 #define GET_ID(_attr_id) \
120 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
122 #define MAKE_ATTR_ID(_ns, _id) \
123 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
124 ((((guint32) _id) & ID_MASK) << ID_POS) )
127 _lookup_namespace (const char *namespace)
131 ns_info = g_hash_table_lookup (ns_hash, namespace);
134 ns_info = g_new0 (NSInfo, 1);
135 ns_info->id = ++namespace_id_counter;
136 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
137 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
138 attributes[ns_info->id] = NULL;
144 lookup_namespace (const char *namespace)
149 G_LOCK (attribute_hash);
151 if (attribute_hash == NULL)
153 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
154 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
157 ns_info = _lookup_namespace (namespace);
162 G_UNLOCK (attribute_hash);
168 get_attribute_for_id (int attribute)
171 G_LOCK (attribute_hash);
172 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
173 G_UNLOCK (attribute_hash);
178 lookup_attribute (const char *attribute)
185 G_LOCK (attribute_hash);
186 if (attribute_hash == NULL)
188 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
189 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
192 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
196 G_UNLOCK (attribute_hash);
200 colon = strstr (attribute, "::");
202 ns = g_strndup (attribute, colon - attribute);
206 ns_info = _lookup_namespace (ns);
209 id = ++ns_info->attribute_id_counter;
210 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
211 attributes[ns_info->id][id] = g_strdup (attribute);
213 attr_id = MAKE_ATTR_ID (ns_info->id, id);
215 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
217 G_UNLOCK (attribute_hash);
223 g_file_info_finalize (GObject *object)
227 GFileAttribute *attrs;
229 info = G_FILE_INFO (object);
231 attrs = (GFileAttribute *)info->attributes->data;
232 for (i = 0; i < info->attributes->len; i++)
233 _g_file_attribute_value_clear (&attrs[i].value);
234 g_array_free (info->attributes, TRUE);
236 if (info->mask != NO_ATTRIBUTE_MASK)
237 g_file_attribute_matcher_unref (info->mask);
239 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
243 g_file_info_class_init (GFileInfoClass *klass)
245 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
247 gobject_class->finalize = g_file_info_finalize;
251 g_file_info_init (GFileInfo *info)
253 info->mask = NO_ATTRIBUTE_MASK;
254 info->attributes = g_array_new (FALSE, FALSE,
255 sizeof (GFileAttribute));
261 * Creates a new file info structure.
263 * Returns: a #GFileInfo.
266 g_file_info_new (void)
268 return g_object_new (G_TYPE_FILE_INFO, NULL);
272 * g_file_info_copy_into:
273 * @src_info: source to copy attributes from.
274 * @dest_info: destination to copy attributes to.
276 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
279 g_file_info_copy_into (GFileInfo *src_info,
280 GFileInfo *dest_info)
282 GFileAttribute *source, *dest;
285 g_return_if_fail (G_IS_FILE_INFO (src_info));
286 g_return_if_fail (G_IS_FILE_INFO (dest_info));
288 dest = (GFileAttribute *)dest_info->attributes->data;
289 for (i = 0; i < dest_info->attributes->len; i++)
290 _g_file_attribute_value_clear (&dest[i].value);
292 g_array_set_size (dest_info->attributes,
293 src_info->attributes->len);
295 source = (GFileAttribute *)src_info->attributes->data;
296 dest = (GFileAttribute *)dest_info->attributes->data;
298 for (i = 0; i < src_info->attributes->len; i++)
300 dest[i].attribute = source[i].attribute;
301 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
302 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
305 if (dest_info->mask != NO_ATTRIBUTE_MASK)
306 g_file_attribute_matcher_unref (dest_info->mask);
308 if (src_info->mask == NO_ATTRIBUTE_MASK)
309 dest_info->mask = NO_ATTRIBUTE_MASK;
311 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
316 * @other: a #GFileInfo.
318 * Duplicates a file info structure.
320 * Returns: a duplicate #GFileInfo of @other.
323 g_file_info_dup (GFileInfo *other)
327 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
329 new = g_file_info_new ();
330 g_file_info_copy_into (other, new);
335 * g_file_info_set_attribute_mask:
336 * @info: a #GFileInfo.
337 * @mask: a #GFileAttributeMatcher.
339 * Sets @mask on @info to match specific attribute types.
342 g_file_info_set_attribute_mask (GFileInfo *info,
343 GFileAttributeMatcher *mask)
345 GFileAttribute *attr;
348 g_return_if_fail (G_IS_FILE_INFO (info));
350 if (mask != info->mask)
352 if (info->mask != NO_ATTRIBUTE_MASK)
353 g_file_attribute_matcher_unref (info->mask);
354 info->mask = g_file_attribute_matcher_ref (mask);
356 /* Remove non-matching attributes */
357 for (i = 0; i < info->attributes->len; i++)
359 attr = &g_array_index (info->attributes, GFileAttribute, i);
360 if (!g_file_attribute_matcher_matches_id (mask,
363 _g_file_attribute_value_clear (&attr->value);
364 g_array_remove_index (info->attributes, i);
372 * g_file_info_unset_attribute_mask:
375 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
379 g_file_info_unset_attribute_mask (GFileInfo *info)
381 g_return_if_fail (G_IS_FILE_INFO (info));
383 if (info->mask != NO_ATTRIBUTE_MASK)
384 g_file_attribute_matcher_unref (info->mask);
385 info->mask = NO_ATTRIBUTE_MASK;
389 * g_file_info_clear_status:
390 * @info: a #GFileInfo.
392 * Clears the status information from @info.
395 g_file_info_clear_status (GFileInfo *info)
397 GFileAttribute *attrs;
400 g_return_if_fail (G_IS_FILE_INFO (info));
402 attrs = (GFileAttribute *)info->attributes->data;
403 for (i = 0; i < info->attributes->len; i++)
404 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
408 g_file_info_find_place (GFileInfo *info,
412 GFileAttribute *attrs;
413 /* Binary search for the place where attribute would be, if it's
417 max = info->attributes->len;
419 attrs = (GFileAttribute *)info->attributes->data;
423 med = min + (max - min) / 2;
424 if (attrs[med].attribute == attribute)
429 else if (attrs[med].attribute < attribute)
431 else /* attrs[med].attribute > attribute */
438 static GFileAttributeValue *
439 g_file_info_find_value (GFileInfo *info,
442 GFileAttribute *attrs;
445 i = g_file_info_find_place (info, attr_id);
446 attrs = (GFileAttribute *)info->attributes->data;
447 if (i < info->attributes->len &&
448 attrs[i].attribute == attr_id)
449 return &attrs[i].value;
454 static GFileAttributeValue *
455 g_file_info_find_value_by_name (GFileInfo *info,
456 const char *attribute)
460 attr_id = lookup_attribute (attribute);
461 return g_file_info_find_value (info, attr_id);
465 * g_file_info_has_attribute:
466 * @info: a #GFileInfo.
467 * @attribute: a file attribute key.
469 * Checks if a file info structure has an attribute named @attribute.
471 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
475 g_file_info_has_attribute (GFileInfo *info,
476 const char *attribute)
478 GFileAttributeValue *value;
480 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
481 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
483 value = g_file_info_find_value_by_name (info, attribute);
484 return value != NULL;
488 * g_file_info_has_namespace:
489 * @info: a #GFileInfo.
490 * @name_space: a file attribute namespace.
492 * Checks if a file info structure has an attribute in the
493 * specified @name_space.
495 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
501 g_file_info_has_namespace (GFileInfo *info,
502 const char *name_space)
504 GFileAttribute *attrs;
508 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
509 g_return_val_if_fail (name_space != NULL, FALSE);
511 ns_id = lookup_namespace (name_space);
513 attrs = (GFileAttribute *)info->attributes->data;
514 for (i = 0; i < info->attributes->len; i++)
516 if (GET_NS (attrs[i].attribute) == ns_id)
524 * g_file_info_list_attributes:
525 * @info: a #GFileInfo.
526 * @name_space: a file attribute key's namespace.
528 * Lists the file info structure's attributes.
530 * Returns: a null-terminated array of strings of all of the
531 * possible attribute types for the given @name_space, or
535 g_file_info_list_attributes (GFileInfo *info,
536 const char *name_space)
539 GFileAttribute *attrs;
541 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
544 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
546 names = g_ptr_array_new ();
547 attrs = (GFileAttribute *)info->attributes->data;
548 for (i = 0; i < info->attributes->len; i++)
550 attribute = attrs[i].attribute;
551 if (ns_id == 0 || GET_NS (attribute) == ns_id)
552 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
556 g_ptr_array_add (names, NULL);
558 return (char **)g_ptr_array_free (names, FALSE);
562 * g_file_info_get_attribute_type:
563 * @info: a #GFileInfo.
564 * @attribute: a file attribute key.
566 * Gets the attribute type for an attribute key.
568 * Returns: a #GFileAttributeType for the given @attribute, or
569 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is invalid.
572 g_file_info_get_attribute_type (GFileInfo *info,
573 const char *attribute)
575 GFileAttributeValue *value;
577 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
578 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
580 value = g_file_info_find_value_by_name (info, attribute);
584 return G_FILE_ATTRIBUTE_TYPE_INVALID;
588 * g_file_info_remove_attribute:
589 * @info: a #GFileInfo.
590 * @attribute: a file attribute key.
592 * Removes all cases of @attribute from @info if it exists.
595 g_file_info_remove_attribute (GFileInfo *info,
596 const char *attribute)
599 GFileAttribute *attrs;
602 g_return_if_fail (G_IS_FILE_INFO (info));
603 g_return_if_fail (attribute != NULL && *attribute != '\0');
605 attr_id = lookup_attribute (attribute);
607 i = g_file_info_find_place (info, attr_id);
608 attrs = (GFileAttribute *)info->attributes->data;
609 if (i < info->attributes->len &&
610 attrs[i].attribute == attr_id)
612 _g_file_attribute_value_clear (&attrs[i].value);
613 g_array_remove_index (info->attributes, i);
618 * g_file_info_get_attribute_data:
619 * @info: a #GFileInfo
620 * @attribute: a file attribute key
621 * @type: return location for the attribute type, or %NULL
622 * @value_pp: return location for the attribute value, or %NULL
623 * @status: return location for the attribute status, or %NULL
625 * Gets the attribute type, value and status for an attribute key.
627 * Returns: %TRUE if @info has an attribute named @attribute,
631 g_file_info_get_attribute_data (GFileInfo *info,
632 const char *attribute,
633 GFileAttributeType *type,
635 GFileAttributeStatus *status)
637 GFileAttributeValue *value;
639 value = g_file_info_find_value_by_name (info, attribute);
644 *status = value->status;
650 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
656 * g_file_info_get_attribute_status:
657 * @info: a #GFileInfo
658 * @attribute: a file attribute key
660 * Gets the attribute status for an attribute key.
662 * Returns: a #GFileAttributeStatus for the given @attribute, or
663 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
667 g_file_info_get_attribute_status (GFileInfo *info,
668 const char *attribute)
670 GFileAttributeValue *val;
672 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
673 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
675 val = g_file_info_find_value_by_name (info, attribute);
679 return G_FILE_ATTRIBUTE_STATUS_UNSET;
683 GFileAttributeValue *
684 _g_file_info_get_attribute_value (GFileInfo *info,
685 const char *attribute)
688 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
689 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
691 return g_file_info_find_value_by_name (info, attribute);
695 * g_file_info_get_attribute_as_string:
696 * @info: a #GFileInfo.
697 * @attribute: a file attribute key.
699 * Gets the value of a attribute, formated as a string.
700 * This escapes things as needed to make the string valid
703 * Returns: a UTF-8 string associated with the given @attribute.
704 * When you're done with the string it must be freed with g_free().
707 g_file_info_get_attribute_as_string (GFileInfo *info,
708 const char *attribute)
710 GFileAttributeValue *val;
711 val = _g_file_info_get_attribute_value (info, attribute);
713 return _g_file_attribute_value_as_string (val);
719 * g_file_info_get_attribute_object:
720 * @info: a #GFileInfo.
721 * @attribute: a file attribute key.
723 * Gets the value of a #GObject attribute. If the attribute does
724 * not contain a #GObject, %NULL will be returned.
726 * Returns: a #GObject associated with the given @attribute, or
730 g_file_info_get_attribute_object (GFileInfo *info,
731 const char *attribute)
733 GFileAttributeValue *value;
735 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
736 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
738 value = g_file_info_find_value_by_name (info, attribute);
739 return _g_file_attribute_value_get_object (value);
743 * g_file_info_get_attribute_string:
744 * @info: a #GFileInfo.
745 * @attribute: a file attribute key.
747 * Gets the value of a string attribute. If the attribute does
748 * not contain a string, %NULL will be returned.
750 * Returns: the contents of the @attribute value as a string, or
754 g_file_info_get_attribute_string (GFileInfo *info,
755 const char *attribute)
757 GFileAttributeValue *value;
759 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
760 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
762 value = g_file_info_find_value_by_name (info, attribute);
763 return _g_file_attribute_value_get_string (value);
767 * g_file_info_get_attribute_byte_string:
768 * @info: a #GFileInfo.
769 * @attribute: a file attribute key.
771 * Gets the value of a byte string attribute. If the attribute does
772 * not contain a byte string, %NULL will be returned.
774 * Returns: the contents of the @attribute value as a byte string, or
778 g_file_info_get_attribute_byte_string (GFileInfo *info,
779 const char *attribute)
781 GFileAttributeValue *value;
783 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
784 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
786 value = g_file_info_find_value_by_name (info, attribute);
787 return _g_file_attribute_value_get_byte_string (value);
791 * g_file_info_get_attribute_stringv:
792 * @info: a #GFileInfo.
793 * @attribute: a file attribute key.
795 * Gets the value of a stringv attribute. If the attribute does
796 * not contain a stringv, %NULL will be returned.
798 * Returns: the contents of the @attribute value as a stringv, or
799 * %NULL otherwise. Do not free.
802 g_file_info_get_attribute_stringv (GFileInfo *info,
803 const char *attribute)
805 GFileAttributeValue *value;
807 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
808 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
810 value = g_file_info_find_value_by_name (info, attribute);
811 return _g_file_attribute_value_get_stringv (value);
815 * g_file_info_get_attribute_boolean:
816 * @info: a #GFileInfo.
817 * @attribute: a file attribute key.
819 * Gets the value of a boolean attribute. If the attribute does not
820 * contain a boolean value, %FALSE will be returned.
822 * Returns: the boolean value contained within the attribute.
825 g_file_info_get_attribute_boolean (GFileInfo *info,
826 const char *attribute)
828 GFileAttributeValue *value;
830 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
831 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
833 value = g_file_info_find_value_by_name (info, attribute);
834 return _g_file_attribute_value_get_boolean (value);
838 * g_file_info_get_attribute_uint32:
839 * @info: a #GFileInfo.
840 * @attribute: a file attribute key.
842 * Gets an unsigned 32-bit integer contained within the attribute. If the
843 * attribute does not contain an unsigned 32-bit integer, or is invalid,
844 * 0 will be returned.
846 * Returns: an unsigned 32-bit integer from the attribute.
849 g_file_info_get_attribute_uint32 (GFileInfo *info,
850 const char *attribute)
852 GFileAttributeValue *value;
854 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
855 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
857 value = g_file_info_find_value_by_name (info, attribute);
858 return _g_file_attribute_value_get_uint32 (value);
862 * g_file_info_get_attribute_int32:
863 * @info: a #GFileInfo.
864 * @attribute: a file attribute key.
866 * Gets a signed 32-bit integer contained within the attribute. If the
867 * attribute does not contain a signed 32-bit integer, or is invalid,
868 * 0 will be returned.
870 * Returns: a signed 32-bit integer from the attribute.
873 g_file_info_get_attribute_int32 (GFileInfo *info,
874 const char *attribute)
876 GFileAttributeValue *value;
878 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
879 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
881 value = g_file_info_find_value_by_name (info, attribute);
882 return _g_file_attribute_value_get_int32 (value);
886 * g_file_info_get_attribute_uint64:
887 * @info: a #GFileInfo.
888 * @attribute: a file attribute key.
890 * Gets a unsigned 64-bit integer contained within the attribute. If the
891 * attribute does not contain an unsigned 64-bit integer, or is invalid,
892 * 0 will be returned.
894 * Returns: a unsigned 64-bit integer from the attribute.
897 g_file_info_get_attribute_uint64 (GFileInfo *info,
898 const char *attribute)
900 GFileAttributeValue *value;
902 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
903 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
905 value = g_file_info_find_value_by_name (info, attribute);
906 return _g_file_attribute_value_get_uint64 (value);
910 * g_file_info_get_attribute_int64:
911 * @info: a #GFileInfo.
912 * @attribute: a file attribute key.
914 * Gets a signed 64-bit integer contained within the attribute. If the
915 * attribute does not contain an signed 64-bit integer, or is invalid,
916 * 0 will be returned.
918 * Returns: a signed 64-bit integer from the attribute.
921 g_file_info_get_attribute_int64 (GFileInfo *info,
922 const char *attribute)
924 GFileAttributeValue *value;
926 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
927 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
929 value = g_file_info_find_value_by_name (info, attribute);
930 return _g_file_attribute_value_get_int64 (value);
933 static GFileAttributeValue *
934 g_file_info_create_value (GFileInfo *info,
937 GFileAttribute *attrs;
940 if (info->mask != NO_ATTRIBUTE_MASK &&
941 !g_file_attribute_matcher_matches_id (info->mask, attr_id))
944 i = g_file_info_find_place (info, attr_id);
946 attrs = (GFileAttribute *)info->attributes->data;
947 if (i < info->attributes->len &&
948 attrs[i].attribute == attr_id)
949 return &attrs[i].value;
952 GFileAttribute attr = { 0 };
953 attr.attribute = attr_id;
954 g_array_insert_val (info->attributes, i, attr);
956 attrs = (GFileAttribute *)info->attributes->data;
957 return &attrs[i].value;
961 static GFileAttributeValue *
962 g_file_info_create_value_by_name (GFileInfo *info,
963 const char *attribute)
967 attr_id = lookup_attribute (attribute);
969 return g_file_info_create_value (info, attr_id);
973 * g_file_info_set_attribute:
974 * @info: a #GFileInfo.
975 * @attribute: a file attribute key.
976 * @type: a #GFileAttributeType
977 * @value_p: pointer to the value
979 * Sets the @attribute to contain the given value, if possible.
982 g_file_info_set_attribute (GFileInfo *info,
983 const char *attribute,
984 GFileAttributeType type,
987 GFileAttributeValue *value;
989 g_return_if_fail (G_IS_FILE_INFO (info));
990 g_return_if_fail (attribute != NULL && *attribute != '\0');
992 value = g_file_info_create_value_by_name (info, attribute);
995 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
999 * g_file_info_set_attribute_object:
1000 * @info: a #GFileInfo.
1001 * @attribute: a file attribute key.
1002 * @attr_value: a #GObject.
1004 * Sets the @attribute to contain the given @attr_value,
1008 g_file_info_set_attribute_object (GFileInfo *info,
1009 const char *attribute,
1010 GObject *attr_value)
1012 GFileAttributeValue *value;
1014 g_return_if_fail (G_IS_FILE_INFO (info));
1015 g_return_if_fail (attribute != NULL && *attribute != '\0');
1016 g_return_if_fail (G_IS_OBJECT (attr_value));
1018 value = g_file_info_create_value_by_name (info, attribute);
1020 _g_file_attribute_value_set_object (value, attr_value);
1024 * g_file_info_set_attribute_stringv:
1025 * @info: a #GFileInfo.
1026 * @attribute: a file attribute key.
1027 * @attr_value: a %NULL terminated string array
1029 * Sets the @attribute to contain the given @attr_value,
1035 g_file_info_set_attribute_stringv (GFileInfo *info,
1036 const char *attribute,
1039 GFileAttributeValue *value;
1041 g_return_if_fail (G_IS_FILE_INFO (info));
1042 g_return_if_fail (attribute != NULL && *attribute != '\0');
1043 g_return_if_fail (attr_value != NULL);
1045 value = g_file_info_create_value_by_name (info, attribute);
1047 _g_file_attribute_value_set_stringv (value, attr_value);
1051 * g_file_info_set_attribute_string:
1052 * @info: a #GFileInfo.
1053 * @attribute: a file attribute key.
1054 * @attr_value: a string.
1056 * Sets the @attribute to contain the given @attr_value,
1060 g_file_info_set_attribute_string (GFileInfo *info,
1061 const char *attribute,
1062 const char *attr_value)
1064 GFileAttributeValue *value;
1066 g_return_if_fail (G_IS_FILE_INFO (info));
1067 g_return_if_fail (attribute != NULL && *attribute != '\0');
1068 g_return_if_fail (attr_value != NULL);
1070 value = g_file_info_create_value_by_name (info, attribute);
1072 _g_file_attribute_value_set_string (value, attr_value);
1076 * g_file_info_set_attribute_byte_string:
1077 * @info: a #GFileInfo.
1078 * @attribute: a file attribute key.
1079 * @attr_value: a byte string.
1081 * Sets the @attribute to contain the given @attr_value,
1085 g_file_info_set_attribute_byte_string (GFileInfo *info,
1086 const char *attribute,
1087 const char *attr_value)
1089 GFileAttributeValue *value;
1091 g_return_if_fail (G_IS_FILE_INFO (info));
1092 g_return_if_fail (attribute != NULL && *attribute != '\0');
1093 g_return_if_fail (attr_value != NULL);
1095 value = g_file_info_create_value_by_name (info, attribute);
1097 _g_file_attribute_value_set_byte_string (value, attr_value);
1101 * g_file_info_set_attribute_boolean:
1102 * @info: a #GFileInfo.
1103 * @attribute: a file attribute key.
1104 * @attr_value: a boolean value.
1106 * Sets the @attribute to contain the given @attr_value,
1110 g_file_info_set_attribute_boolean (GFileInfo *info,
1111 const char *attribute,
1112 gboolean attr_value)
1114 GFileAttributeValue *value;
1116 g_return_if_fail (G_IS_FILE_INFO (info));
1117 g_return_if_fail (attribute != NULL && *attribute != '\0');
1119 value = g_file_info_create_value_by_name (info, attribute);
1121 _g_file_attribute_value_set_boolean (value, attr_value);
1125 * g_file_info_set_attribute_uint32:
1126 * @info: a #GFileInfo.
1127 * @attribute: a file attribute key.
1128 * @attr_value: an unsigned 32-bit integer.
1130 * Sets the @attribute to contain the given @attr_value,
1134 g_file_info_set_attribute_uint32 (GFileInfo *info,
1135 const char *attribute,
1138 GFileAttributeValue *value;
1140 g_return_if_fail (G_IS_FILE_INFO (info));
1141 g_return_if_fail (attribute != NULL && *attribute != '\0');
1143 value = g_file_info_create_value_by_name (info, attribute);
1145 _g_file_attribute_value_set_uint32 (value, attr_value);
1150 * g_file_info_set_attribute_int32:
1151 * @info: a #GFileInfo.
1152 * @attribute: a file attribute key.
1153 * @attr_value: a signed 32-bit integer
1155 * Sets the @attribute to contain the given @attr_value,
1159 g_file_info_set_attribute_int32 (GFileInfo *info,
1160 const char *attribute,
1163 GFileAttributeValue *value;
1165 g_return_if_fail (G_IS_FILE_INFO (info));
1166 g_return_if_fail (attribute != NULL && *attribute != '\0');
1168 value = g_file_info_create_value_by_name (info, attribute);
1170 _g_file_attribute_value_set_int32 (value, attr_value);
1174 * g_file_info_set_attribute_uint64:
1175 * @info: a #GFileInfo.
1176 * @attribute: a file attribute key.
1177 * @attr_value: an unsigned 64-bit integer.
1179 * Sets the @attribute to contain the given @attr_value,
1183 g_file_info_set_attribute_uint64 (GFileInfo *info,
1184 const char *attribute,
1187 GFileAttributeValue *value;
1189 g_return_if_fail (G_IS_FILE_INFO (info));
1190 g_return_if_fail (attribute != NULL && *attribute != '\0');
1192 value = g_file_info_create_value_by_name (info, attribute);
1194 _g_file_attribute_value_set_uint64 (value, attr_value);
1198 * g_file_info_set_attribute_int64:
1199 * @info: a #GFileInfo.
1200 * @attribute: attribute name to set.
1201 * @attr_value: int64 value to set attribute to.
1203 * Sets the @attribute to contain the given @attr_value,
1208 g_file_info_set_attribute_int64 (GFileInfo *info,
1209 const char *attribute,
1212 GFileAttributeValue *value;
1214 g_return_if_fail (G_IS_FILE_INFO (info));
1215 g_return_if_fail (attribute != NULL && *attribute != '\0');
1217 value = g_file_info_create_value_by_name (info, attribute);
1219 _g_file_attribute_value_set_int64 (value, attr_value);
1222 /* Helper getters */
1224 * g_file_info_get_file_type:
1225 * @info: a #GFileInfo.
1227 * Gets a file's type (whether it is a regular file, symlink, etc).
1228 * This is different from the file's content type, see g_file_info_get_content_type().
1230 * Returns: a #GFileType for the given file.
1233 g_file_info_get_file_type (GFileInfo *info)
1235 static guint32 attr = 0;
1236 GFileAttributeValue *value;
1238 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1241 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1243 value = g_file_info_find_value (info, attr);
1244 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1248 * g_file_info_get_is_hidden:
1249 * @info: a #GFileInfo.
1251 * Checks if a file is hidden.
1253 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1256 g_file_info_get_is_hidden (GFileInfo *info)
1258 static guint32 attr = 0;
1259 GFileAttributeValue *value;
1261 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1264 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1266 value = g_file_info_find_value (info, attr);
1267 return (GFileType)_g_file_attribute_value_get_boolean (value);
1271 * g_file_info_get_is_backup:
1272 * @info: a #GFileInfo.
1274 * Checks if a file is a backup file.
1276 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1279 g_file_info_get_is_backup (GFileInfo *info)
1281 static guint32 attr = 0;
1282 GFileAttributeValue *value;
1284 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1287 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1289 value = g_file_info_find_value (info, attr);
1290 return (GFileType)_g_file_attribute_value_get_boolean (value);
1294 * g_file_info_get_is_symlink:
1295 * @info: a #GFileInfo.
1297 * Checks if a file is a symlink.
1299 * Returns: %TRUE if the given @info is a symlink.
1302 g_file_info_get_is_symlink (GFileInfo *info)
1304 static guint32 attr = 0;
1305 GFileAttributeValue *value;
1307 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1310 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1312 value = g_file_info_find_value (info, attr);
1313 return (GFileType)_g_file_attribute_value_get_boolean (value);
1317 * g_file_info_get_name:
1318 * @info: a #GFileInfo.
1320 * Gets the name for a file.
1322 * Returns: a string containing the file name.
1325 g_file_info_get_name (GFileInfo *info)
1327 static guint32 attr = 0;
1328 GFileAttributeValue *value;
1330 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1333 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1335 value = g_file_info_find_value (info, attr);
1336 return _g_file_attribute_value_get_byte_string (value);
1340 * g_file_info_get_display_name:
1341 * @info: a #GFileInfo.
1343 * Gets a display name for a file.
1345 * Returns: a string containing the display name.
1348 g_file_info_get_display_name (GFileInfo *info)
1350 static guint32 attr = 0;
1351 GFileAttributeValue *value;
1353 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1356 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1358 value = g_file_info_find_value (info, attr);
1359 return _g_file_attribute_value_get_string (value);
1363 * g_file_info_get_edit_name:
1364 * @info: a #GFileInfo.
1366 * Gets the edit name for a file.
1368 * Returns: a string containing the edit name.
1371 g_file_info_get_edit_name (GFileInfo *info)
1373 static guint32 attr = 0;
1374 GFileAttributeValue *value;
1376 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1379 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1381 value = g_file_info_find_value (info, attr);
1382 return _g_file_attribute_value_get_string (value);
1386 * g_file_info_get_icon:
1387 * @info: a #GFileInfo.
1389 * Gets the icon for a file.
1391 * Returns: #GIcon for the given @info.
1394 g_file_info_get_icon (GFileInfo *info)
1396 static guint32 attr = 0;
1397 GFileAttributeValue *value;
1400 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1403 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1405 value = g_file_info_find_value (info, attr);
1406 obj = _g_file_attribute_value_get_object (value);
1407 if (G_IS_ICON (obj))
1408 return G_ICON (obj);
1413 * g_file_info_get_content_type:
1414 * @info: a #GFileInfo.
1416 * Gets the file's content type.
1418 * Returns: a string containing the file's content type.
1421 g_file_info_get_content_type (GFileInfo *info)
1423 static guint32 attr = 0;
1424 GFileAttributeValue *value;
1426 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1429 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1431 value = g_file_info_find_value (info, attr);
1432 return _g_file_attribute_value_get_string (value);
1436 * g_file_info_get_size:
1437 * @info: a #GFileInfo.
1439 * Gets the file's size.
1441 * Returns: a #goffset containing the file's size.
1444 g_file_info_get_size (GFileInfo *info)
1446 static guint32 attr = 0;
1447 GFileAttributeValue *value;
1449 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1452 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1454 value = g_file_info_find_value (info, attr);
1455 return (goffset) _g_file_attribute_value_get_uint64 (value);
1459 * g_file_info_get_modification_time:
1460 * @info: a #GFileInfo.
1461 * @result: a #GTimeVal.
1463 * Gets the modification time of the current @info and sets it
1467 g_file_info_get_modification_time (GFileInfo *info,
1470 static guint32 attr_mtime = 0, attr_mtime_usec;
1471 GFileAttributeValue *value;
1473 g_return_if_fail (G_IS_FILE_INFO (info));
1474 g_return_if_fail (result != NULL);
1476 if (attr_mtime == 0)
1478 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1479 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1482 value = g_file_info_find_value (info, attr_mtime);
1483 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1484 value = g_file_info_find_value (info, attr_mtime_usec);
1485 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1489 * g_file_info_get_symlink_target:
1490 * @info: a #GFileInfo.
1492 * Gets the symlink target for a given #GFileInfo.
1494 * Returns: a string containing the symlink target.
1497 g_file_info_get_symlink_target (GFileInfo *info)
1499 static guint32 attr = 0;
1500 GFileAttributeValue *value;
1502 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1505 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1507 value = g_file_info_find_value (info, attr);
1508 return _g_file_attribute_value_get_byte_string (value);
1512 * g_file_info_get_etag:
1513 * @info: a #GFileInfo.
1515 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1516 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1518 * Returns: a string containing the value of the "etag:value" attribute.
1521 g_file_info_get_etag (GFileInfo *info)
1523 static guint32 attr = 0;
1524 GFileAttributeValue *value;
1526 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1529 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1531 value = g_file_info_find_value (info, attr);
1532 return _g_file_attribute_value_get_string (value);
1536 * g_file_info_get_sort_order:
1537 * @info: a #GFileInfo.
1539 * Gets the value of the sort_order attribute from the #GFileInfo.
1540 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1542 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1545 g_file_info_get_sort_order (GFileInfo *info)
1547 static guint32 attr = 0;
1548 GFileAttributeValue *value;
1550 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1553 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1555 value = g_file_info_find_value (info, attr);
1556 return _g_file_attribute_value_get_int32 (value);
1559 /* Helper setters: */
1561 * g_file_info_set_file_type:
1562 * @info: a #GFileInfo.
1563 * @type: a #GFileType.
1565 * Sets the file type in a #GFileInfo to @type.
1566 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1569 g_file_info_set_file_type (GFileInfo *info,
1572 static guint32 attr = 0;
1573 GFileAttributeValue *value;
1575 g_return_if_fail (G_IS_FILE_INFO (info));
1578 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1580 value = g_file_info_create_value (info, attr);
1582 _g_file_attribute_value_set_uint32 (value, type);
1586 * g_file_info_set_is_hidden:
1587 * @info: a #GFileInfo.
1588 * @is_hidden: a #gboolean.
1590 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1591 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1594 g_file_info_set_is_hidden (GFileInfo *info,
1597 static guint32 attr = 0;
1598 GFileAttributeValue *value;
1600 g_return_if_fail (G_IS_FILE_INFO (info));
1603 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1605 value = g_file_info_create_value (info, attr);
1607 _g_file_attribute_value_set_boolean (value, is_hidden);
1611 * g_file_info_set_is_symlink:
1612 * @info: a #GFileInfo.
1613 * @is_symlink: a #gboolean.
1615 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1616 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1619 g_file_info_set_is_symlink (GFileInfo *info,
1620 gboolean is_symlink)
1622 static guint32 attr = 0;
1623 GFileAttributeValue *value;
1625 g_return_if_fail (G_IS_FILE_INFO (info));
1628 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1630 value = g_file_info_create_value (info, attr);
1632 _g_file_attribute_value_set_boolean (value, is_symlink);
1636 * g_file_info_set_name:
1637 * @info: a #GFileInfo.
1638 * @name: a string containing a name.
1640 * Sets the name attribute for the current #GFileInfo.
1641 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1644 g_file_info_set_name (GFileInfo *info,
1647 static guint32 attr = 0;
1648 GFileAttributeValue *value;
1650 g_return_if_fail (G_IS_FILE_INFO (info));
1651 g_return_if_fail (name != NULL);
1654 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1656 value = g_file_info_create_value (info, attr);
1658 _g_file_attribute_value_set_byte_string (value, name);
1662 * g_file_info_set_display_name:
1663 * @info: a #GFileInfo.
1664 * @display_name: a string containing a display name.
1666 * Sets the display name for the current #GFileInfo.
1667 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1670 g_file_info_set_display_name (GFileInfo *info,
1671 const char *display_name)
1673 static guint32 attr = 0;
1674 GFileAttributeValue *value;
1676 g_return_if_fail (G_IS_FILE_INFO (info));
1677 g_return_if_fail (display_name != NULL);
1680 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1682 value = g_file_info_create_value (info, attr);
1684 _g_file_attribute_value_set_string (value, display_name);
1688 * g_file_info_set_edit_name:
1689 * @info: a #GFileInfo.
1690 * @edit_name: a string containing an edit name.
1692 * Sets the edit name for the current file.
1693 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1696 g_file_info_set_edit_name (GFileInfo *info,
1697 const char *edit_name)
1699 static guint32 attr = 0;
1700 GFileAttributeValue *value;
1702 g_return_if_fail (G_IS_FILE_INFO (info));
1703 g_return_if_fail (edit_name != NULL);
1706 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1708 value = g_file_info_create_value (info, attr);
1710 _g_file_attribute_value_set_string (value, edit_name);
1714 * g_file_info_set_icon:
1715 * @info: a #GFileInfo.
1718 * Sets the icon for a given #GFileInfo.
1719 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1722 g_file_info_set_icon (GFileInfo *info,
1725 static guint32 attr = 0;
1726 GFileAttributeValue *value;
1728 g_return_if_fail (G_IS_FILE_INFO (info));
1729 g_return_if_fail (G_IS_ICON (icon));
1732 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1734 value = g_file_info_create_value (info, attr);
1736 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1740 * g_file_info_set_content_type:
1741 * @info: a #GFileInfo.
1742 * @content_type: a content type. See #GContentType.
1744 * Sets the content type attribute for a given #GFileInfo.
1745 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1748 g_file_info_set_content_type (GFileInfo *info,
1749 const char *content_type)
1751 static guint32 attr = 0;
1752 GFileAttributeValue *value;
1754 g_return_if_fail (G_IS_FILE_INFO (info));
1755 g_return_if_fail (content_type != NULL);
1758 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1760 value = g_file_info_create_value (info, attr);
1762 _g_file_attribute_value_set_string (value, content_type);
1766 * g_file_info_set_size:
1767 * @info: a #GFileInfo.
1768 * @size: a #goffset containing the file's size.
1770 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1771 * to the given size.
1774 g_file_info_set_size (GFileInfo *info,
1777 static guint32 attr = 0;
1778 GFileAttributeValue *value;
1780 g_return_if_fail (G_IS_FILE_INFO (info));
1783 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1785 value = g_file_info_create_value (info, attr);
1787 _g_file_attribute_value_set_uint64 (value, size);
1791 * g_file_info_set_modification_time
1792 * @info: a #GFileInfo.
1793 * @mtime: a #GTimeVal.
1795 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
1796 * info to the given time value.
1799 g_file_info_set_modification_time (GFileInfo *info,
1802 static guint32 attr_mtime = 0, attr_mtime_usec;
1803 GFileAttributeValue *value;
1805 g_return_if_fail (G_IS_FILE_INFO (info));
1806 g_return_if_fail (mtime != NULL);
1808 if (attr_mtime == 0)
1810 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1811 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1814 value = g_file_info_create_value (info, attr_mtime);
1816 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
1817 value = g_file_info_create_value (info, attr_mtime_usec);
1819 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
1823 * g_file_info_set_symlink_target:
1824 * @info: a #GFileInfo.
1825 * @symlink_target: a static string containing a path to a symlink target.
1827 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
1828 * to the given symlink target.
1831 g_file_info_set_symlink_target (GFileInfo *info,
1832 const char *symlink_target)
1834 static guint32 attr = 0;
1835 GFileAttributeValue *value;
1837 g_return_if_fail (G_IS_FILE_INFO (info));
1838 g_return_if_fail (symlink_target != NULL);
1841 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1843 value = g_file_info_create_value (info, attr);
1845 _g_file_attribute_value_set_byte_string (value, symlink_target);
1849 * g_file_info_set_sort_order:
1850 * @info: a #GFileInfo.
1851 * @sort_order: a sort order integer.
1853 * Sets the sort order attribute in the file info structure. See
1854 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1857 g_file_info_set_sort_order (GFileInfo *info,
1860 static guint32 attr = 0;
1861 GFileAttributeValue *value;
1863 g_return_if_fail (G_IS_FILE_INFO (info));
1866 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1868 value = g_file_info_create_value (info, attr);
1870 _g_file_attribute_value_set_int32 (value, sort_order);
1874 #define ON_STACK_MATCHERS 5
1881 struct _GFileAttributeMatcher {
1883 SubMatcher sub_matchers[ON_STACK_MATCHERS];
1884 GArray *more_sub_matchers;
1887 guint32 iterator_ns;
1893 matcher_add (GFileAttributeMatcher *matcher,
1897 SubMatcher *sub_matchers;
1901 for (i = 0; i < ON_STACK_MATCHERS; i++)
1903 /* First empty spot, not found, use this */
1904 if (matcher->sub_matchers[i].id == 0)
1906 matcher->sub_matchers[i].id = id;
1907 matcher->sub_matchers[i].mask = mask;
1912 if (matcher->sub_matchers[i].id == id &&
1913 matcher->sub_matchers[i].mask == mask)
1917 if (matcher->more_sub_matchers == NULL)
1918 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
1920 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
1921 for (i = 0; i < matcher->more_sub_matchers->len; i++)
1924 if (sub_matchers[i].id == id &&
1925 sub_matchers[i].mask == mask)
1932 g_array_append_val (matcher->more_sub_matchers, s);
1936 * g_file_attribute_matcher_new:
1937 * @attributes: an attribute string to match.
1939 * Creates a new file attribute matcher, which matches attributes
1940 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
1941 * counted structures, and are created with a reference count of 1. If
1942 * the number of references falls to 0, the #GFileAttributeMatcher is
1943 * automatically destroyed.
1945 * The @attribute string should be formatted with specific keys separated
1946 * from namespaces with a double colon. Several "namespace::key" strings may be
1947 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
1948 * The wildcard "*" may be used to match all keys and namespaces, or
1949 * "namespace::*" will match all keys in a given namespace.
1951 * Examples of strings to use:
1953 * <title>File Attribute Matcher strings and results</title>
1954 * <tgroup cols='2' align='left'><thead>
1955 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
1957 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
1958 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
1959 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
1960 * all keys in the unix namespace.</entry></row>
1964 * Returns: a #GFileAttributeMatcher.
1966 GFileAttributeMatcher *
1967 g_file_attribute_matcher_new (const char *attributes)
1972 GFileAttributeMatcher *matcher;
1974 if (attributes == NULL || *attributes == '\0')
1977 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
1980 split = g_strsplit (attributes, ",", -1);
1982 for (i = 0; split[i] != NULL; i++)
1984 if (strcmp (split[i], "*") == 0)
1985 matcher->all = TRUE;
1990 colon = strstr (split[i], "::");
1991 if (colon != NULL &&
1996 id = lookup_attribute (split[i]);
2004 id = lookup_namespace (split[i]) << NS_POS;
2005 mask = NS_MASK << NS_POS;
2008 matcher_add (matcher, id, mask);
2018 * g_file_attribute_matcher_ref:
2019 * @matcher: a #GFileAttributeMatcher.
2021 * References a file attribute matcher.
2023 * Returns: a #GFileAttributeMatcher.
2025 GFileAttributeMatcher *
2026 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2030 g_return_val_if_fail (matcher->ref > 0, NULL);
2031 g_atomic_int_inc (&matcher->ref);
2037 * g_file_attribute_matcher_unref:
2038 * @matcher: a #GFileAttributeMatcher.
2040 * Unreferences @matcher. If the reference count falls below 1,
2041 * the @matcher is automatically freed.
2045 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2049 g_return_if_fail (matcher->ref > 0);
2051 if (g_atomic_int_dec_and_test (&matcher->ref))
2053 if (matcher->more_sub_matchers)
2054 g_array_free (matcher->more_sub_matchers, TRUE);
2062 * g_file_attribute_matcher_matches_only:
2063 * @matcher: a #GFileAttributeMatcher.
2064 * @attribute: a file attribute key.
2066 * Checks if a attribute matcher only matches a given attribute. Always
2067 * returns %FALSE if "*" was used when creating the matcher.
2069 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2072 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2073 const char *attribute)
2077 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2079 if (matcher == NULL ||
2083 id = lookup_attribute (attribute);
2085 if (matcher->sub_matchers[0].id != 0 &&
2086 matcher->sub_matchers[1].id == 0 &&
2087 matcher->sub_matchers[0].mask == 0xffffffff &&
2088 matcher->sub_matchers[0].id == id)
2095 matcher_matches_id (GFileAttributeMatcher *matcher,
2098 SubMatcher *sub_matchers;
2101 for (i = 0; i < ON_STACK_MATCHERS; i++)
2103 if (matcher->sub_matchers[i].id == 0)
2106 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2110 if (matcher->more_sub_matchers)
2112 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2113 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2115 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2124 g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2127 /* We return a NULL matcher for an empty match string, so handle this */
2128 if (matcher == NULL)
2134 return matcher_matches_id (matcher, id);
2138 * g_file_attribute_matcher_matches:
2139 * @matcher: a #GFileAttributeMatcher.
2140 * @attribute: a file attribute key.
2142 * Checks if an attribute will be matched by an attribute matcher. If
2143 * the matcher was created with the "*" matching string, this function
2144 * will always return %TRUE.
2146 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2149 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2150 const char *attribute)
2152 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2154 /* We return a NULL matcher for an empty match string, so handle this */
2155 if (matcher == NULL)
2161 return matcher_matches_id (matcher, lookup_attribute (attribute));
2164 /* return TRUE -> all */
2166 * g_file_attribute_matcher_enumerate_namespace:
2167 * @matcher: a #GFileAttributeMatcher.
2168 * @ns: a string containing a file attribute namespace.
2170 * Checks if the matcher will match all of the keys in a given namespace.
2171 * This will always return %TRUE if a wildcard character is in use (e.g. if
2172 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2173 * using "*" and namespace is anything.)
2175 * TODO: this is awkwardly worded.
2177 * Returns: %TRUE if the matcher matches all of the entries
2178 * in the given @ns, %FALSE otherwise.
2181 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2184 SubMatcher *sub_matchers;
2188 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2190 /* We return a NULL matcher for an empty match string, so handle this */
2191 if (matcher == NULL)
2197 ns_id = lookup_namespace (ns) << NS_POS;
2199 for (i = 0; i < ON_STACK_MATCHERS; i++)
2201 if (matcher->sub_matchers[i].id == ns_id)
2205 if (matcher->more_sub_matchers)
2207 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2208 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2210 if (sub_matchers[i].id == ns_id)
2215 matcher->iterator_ns = ns_id;
2216 matcher->iterator_pos = 0;
2222 * g_file_attribute_matcher_enumerate_next:
2223 * @matcher: a #GFileAttributeMatcher.
2225 * Gets the next matched attribute from a #GFileAttributeMatcher.
2227 * Returns: a string containing the next attribute or %NULL if
2228 * no more attribute exist.
2231 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2234 SubMatcher *sub_matcher;
2236 /* We return a NULL matcher for an empty match string, so handle this */
2237 if (matcher == NULL)
2242 i = matcher->iterator_pos++;
2244 if (i < ON_STACK_MATCHERS)
2246 if (matcher->sub_matchers[i].id == 0)
2249 sub_matcher = &matcher->sub_matchers[i];
2253 if (matcher->more_sub_matchers == NULL)
2256 i -= ON_STACK_MATCHERS;
2257 if (i < matcher->more_sub_matchers->len)
2258 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2263 if (sub_matcher->mask == 0xffffffff &&
2264 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2265 return get_attribute_for_id (sub_matcher->id);
2269 #define __G_FILE_INFO_C__
2270 #include "gioaliasdef.c"