1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 * @short_description: File Information and Attributes
27 * @see_also: #GFile, <link linkend="gio-GFileAttribute">GFileAttribute</link>
29 * Functionality for manipulating basic metadata for files. #GFileInfo
30 * implements methods for getting information that all files should
31 * contain, and allows for manipulation of extended attributes.
33 * See <link linkend="gio-GFileAttribute">GFileAttribute</link> for more
34 * information on how GIO handles file attributes.
36 * To obtain a #GFileInfo for a #GFile, use g_file_query_info() (or its
37 * async variant). To obtain a #GFileInfo for a file input or output
38 * stream, use g_file_input_stream_query_info() or
39 * g_file_output_stream_query_info() (or their async variants).
41 * To change the actual attributes of a file, you should then set the
42 * attribute in the #GFileInfo and call g_file_set_attributes_from_info()
43 * or g_file_set_attributes_async() on a GFile.
45 * However, not all attributes can be changed in the file. For instance,
46 * the actual size of a file cannot be changed via g_file_info_set_size().
47 * You may call g_file_query_settable_attributes() and
48 * g_file_query_writable_namespaces() to discover the settable attributes
49 * of a particular file at runtime.
51 * #GFileAttributeMatcher allows for searching through a #GFileInfo for
59 #include "gfileinfo.h"
60 #include "gfileattribute-priv.h"
66 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
67 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
71 GFileAttributeValue value;
76 GObject parent_instance;
79 GFileAttributeMatcher *mask;
82 struct _GFileInfoClass
84 GObjectClass parent_class;
88 static gboolean g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
91 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
95 guint32 attribute_id_counter;
98 G_LOCK_DEFINE_STATIC (attribute_hash);
99 static int namespace_id_counter = 0;
100 static GHashTable *ns_hash = NULL;
101 static GHashTable *attribute_hash = NULL;
102 static char ***attributes = NULL;
104 /* Attribute ids are 32bit, we split it up like this:
105 * |------------|--------------------|
107 * namespace attribute id
109 * This way the attributes gets sorted in namespace order
113 #define NS_MASK ((guint32)((1<<12) - 1))
115 #define ID_MASK ((guint32)((1<<20) - 1))
117 #define GET_NS(_attr_id) \
118 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
119 #define GET_ID(_attr_id) \
120 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
122 #define MAKE_ATTR_ID(_ns, _id) \
123 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
124 ((((guint32) _id) & ID_MASK) << ID_POS) )
127 _lookup_namespace (const char *namespace)
131 ns_info = g_hash_table_lookup (ns_hash, namespace);
134 ns_info = g_new0 (NSInfo, 1);
135 ns_info->id = ++namespace_id_counter;
136 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
137 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
138 attributes[ns_info->id] = NULL;
144 lookup_namespace (const char *namespace)
149 G_LOCK (attribute_hash);
151 if (attribute_hash == NULL)
153 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
154 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
157 ns_info = _lookup_namespace (namespace);
162 G_UNLOCK (attribute_hash);
168 get_attribute_for_id (int attribute)
171 G_LOCK (attribute_hash);
172 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
173 G_UNLOCK (attribute_hash);
178 lookup_attribute (const char *attribute)
185 G_LOCK (attribute_hash);
186 if (attribute_hash == NULL)
188 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
189 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
192 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
196 G_UNLOCK (attribute_hash);
200 colon = strstr (attribute, "::");
202 ns = g_strndup (attribute, colon - attribute);
206 ns_info = _lookup_namespace (ns);
209 id = ++ns_info->attribute_id_counter;
210 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
211 attributes[ns_info->id][id] = g_strdup (attribute);
213 attr_id = MAKE_ATTR_ID (ns_info->id, id);
215 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
217 G_UNLOCK (attribute_hash);
223 g_file_info_finalize (GObject *object)
227 GFileAttribute *attrs;
229 info = G_FILE_INFO (object);
231 attrs = (GFileAttribute *)info->attributes->data;
232 for (i = 0; i < info->attributes->len; i++)
233 _g_file_attribute_value_clear (&attrs[i].value);
234 g_array_free (info->attributes, TRUE);
236 if (info->mask != NO_ATTRIBUTE_MASK)
237 g_file_attribute_matcher_unref (info->mask);
239 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
243 g_file_info_class_init (GFileInfoClass *klass)
245 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
247 gobject_class->finalize = g_file_info_finalize;
251 g_file_info_init (GFileInfo *info)
253 info->mask = NO_ATTRIBUTE_MASK;
254 info->attributes = g_array_new (FALSE, FALSE,
255 sizeof (GFileAttribute));
261 * Creates a new file info structure.
263 * Returns: a #GFileInfo.
266 g_file_info_new (void)
268 return g_object_new (G_TYPE_FILE_INFO, NULL);
272 * g_file_info_copy_into:
273 * @src_info: source to copy attributes from.
274 * @dest_info: destination to copy attributes to.
276 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
279 g_file_info_copy_into (GFileInfo *src_info,
280 GFileInfo *dest_info)
282 GFileAttribute *source, *dest;
285 g_return_if_fail (G_IS_FILE_INFO (src_info));
286 g_return_if_fail (G_IS_FILE_INFO (dest_info));
288 dest = (GFileAttribute *)dest_info->attributes->data;
289 for (i = 0; i < dest_info->attributes->len; i++)
290 _g_file_attribute_value_clear (&dest[i].value);
292 g_array_set_size (dest_info->attributes,
293 src_info->attributes->len);
295 source = (GFileAttribute *)src_info->attributes->data;
296 dest = (GFileAttribute *)dest_info->attributes->data;
298 for (i = 0; i < src_info->attributes->len; i++)
300 dest[i].attribute = source[i].attribute;
301 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
302 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
305 if (dest_info->mask != NO_ATTRIBUTE_MASK)
306 g_file_attribute_matcher_unref (dest_info->mask);
308 if (src_info->mask == NO_ATTRIBUTE_MASK)
309 dest_info->mask = NO_ATTRIBUTE_MASK;
311 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
316 * @other: a #GFileInfo.
318 * Duplicates a file info structure.
320 * Returns: a duplicate #GFileInfo of @other.
323 g_file_info_dup (GFileInfo *other)
327 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
329 new = g_file_info_new ();
330 g_file_info_copy_into (other, new);
335 * g_file_info_set_attribute_mask:
336 * @info: a #GFileInfo.
337 * @mask: a #GFileAttributeMatcher.
339 * Sets @mask on @info to match specific attribute types.
342 g_file_info_set_attribute_mask (GFileInfo *info,
343 GFileAttributeMatcher *mask)
345 GFileAttribute *attr;
348 g_return_if_fail (G_IS_FILE_INFO (info));
350 if (mask != info->mask)
352 if (info->mask != NO_ATTRIBUTE_MASK)
353 g_file_attribute_matcher_unref (info->mask);
354 info->mask = g_file_attribute_matcher_ref (mask);
356 /* Remove non-matching attributes */
357 for (i = 0; i < info->attributes->len; i++)
359 attr = &g_array_index (info->attributes, GFileAttribute, i);
360 if (!g_file_attribute_matcher_matches_id (mask,
363 _g_file_attribute_value_clear (&attr->value);
364 g_array_remove_index (info->attributes, i);
372 * g_file_info_unset_attribute_mask:
375 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
379 g_file_info_unset_attribute_mask (GFileInfo *info)
381 g_return_if_fail (G_IS_FILE_INFO (info));
383 if (info->mask != NO_ATTRIBUTE_MASK)
384 g_file_attribute_matcher_unref (info->mask);
385 info->mask = NO_ATTRIBUTE_MASK;
389 * g_file_info_clear_status:
390 * @info: a #GFileInfo.
392 * Clears the status information from @info.
395 g_file_info_clear_status (GFileInfo *info)
397 GFileAttribute *attrs;
400 g_return_if_fail (G_IS_FILE_INFO (info));
402 attrs = (GFileAttribute *)info->attributes->data;
403 for (i = 0; i < info->attributes->len; i++)
404 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
408 g_file_info_find_place (GFileInfo *info,
412 GFileAttribute *attrs;
413 /* Binary search for the place where attribute would be, if it's
417 max = info->attributes->len;
419 attrs = (GFileAttribute *)info->attributes->data;
423 med = min + (max - min) / 2;
424 if (attrs[med].attribute == attribute)
429 else if (attrs[med].attribute < attribute)
431 else /* attrs[med].attribute > attribute */
438 static GFileAttributeValue *
439 g_file_info_find_value (GFileInfo *info,
442 GFileAttribute *attrs;
445 i = g_file_info_find_place (info, attr_id);
446 attrs = (GFileAttribute *)info->attributes->data;
447 if (i < info->attributes->len &&
448 attrs[i].attribute == attr_id)
449 return &attrs[i].value;
454 static GFileAttributeValue *
455 g_file_info_find_value_by_name (GFileInfo *info,
456 const char *attribute)
460 attr_id = lookup_attribute (attribute);
461 return g_file_info_find_value (info, attr_id);
465 * g_file_info_has_attribute:
466 * @info: a #GFileInfo.
467 * @attribute: a file attribute key.
469 * Checks if a file info structure has an attribute named @attribute.
471 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
475 g_file_info_has_attribute (GFileInfo *info,
476 const char *attribute)
478 GFileAttributeValue *value;
480 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
481 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
483 value = g_file_info_find_value_by_name (info, attribute);
484 return value != NULL;
488 * g_file_info_list_attributes:
489 * @info: a #GFileInfo.
490 * @name_space: a file attribute key's namespace.
492 * Lists the file info structure's attributes.
494 * Returns: a null-terminated array of strings of all of the
495 * possible attribute types for the given @name_space, or
499 g_file_info_list_attributes (GFileInfo *info,
500 const char *name_space)
503 GFileAttribute *attrs;
505 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
508 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
510 names = g_ptr_array_new ();
511 attrs = (GFileAttribute *)info->attributes->data;
512 for (i = 0; i < info->attributes->len; i++)
514 attribute = attrs[i].attribute;
515 if (ns_id == 0 || GET_NS (attribute) == ns_id)
516 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
520 g_ptr_array_add (names, NULL);
522 return (char **)g_ptr_array_free (names, FALSE);
526 * g_file_info_get_attribute_type:
527 * @info: a #GFileInfo.
528 * @attribute: a file attribute key.
530 * Gets the attribute type for an attribute key.
532 * Returns: a #GFileAttributeType for the given @attribute, or
533 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is invalid.
536 g_file_info_get_attribute_type (GFileInfo *info,
537 const char *attribute)
539 GFileAttributeValue *value;
541 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
542 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
544 value = g_file_info_find_value_by_name (info, attribute);
548 return G_FILE_ATTRIBUTE_TYPE_INVALID;
552 * g_file_info_remove_attribute:
553 * @info: a #GFileInfo.
554 * @attribute: a file attribute key.
556 * Removes all cases of @attribute from @info if it exists.
559 g_file_info_remove_attribute (GFileInfo *info,
560 const char *attribute)
563 GFileAttribute *attrs;
566 g_return_if_fail (G_IS_FILE_INFO (info));
567 g_return_if_fail (attribute != NULL && *attribute != '\0');
569 attr_id = lookup_attribute (attribute);
571 i = g_file_info_find_place (info, attr_id);
572 attrs = (GFileAttribute *)info->attributes->data;
573 if (i < info->attributes->len &&
574 attrs[i].attribute == attr_id)
576 _g_file_attribute_value_clear (&attrs[i].value);
577 g_array_remove_index (info->attributes, i);
582 * g_file_info_get_attribute_data:
583 * @info: a #GFileInfo
584 * @attribute: a file attribute key
585 * @type: return location for the attribute type, or %NULL
586 * @value_pp: return location for the attribute value, or %NULL
587 * @status: return location for the attribute status, or %NULL
589 * Gets the attribute type, value and status for an attribute key.
591 * Returns: %TRUE if @info has an attribute named @attribute,
595 g_file_info_get_attribute_data (GFileInfo *info,
596 const char *attribute,
597 GFileAttributeType *type,
599 GFileAttributeStatus *status)
601 GFileAttributeValue *value;
603 value = g_file_info_find_value_by_name (info, attribute);
608 *status = value->status;
614 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
620 * g_file_info_get_attribute_status:
621 * @info: a #GFileInfo
622 * @attribute: a file attribute key
624 * Gets the attribute status for an attribute key.
626 * Returns: a #GFileAttributeStatus for the given @attribute, or
627 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
631 g_file_info_get_attribute_status (GFileInfo *info,
632 const char *attribute)
634 GFileAttributeValue *val;
636 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
637 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
639 val = g_file_info_find_value_by_name (info, attribute);
643 return G_FILE_ATTRIBUTE_STATUS_UNSET;
647 GFileAttributeValue *
648 _g_file_info_get_attribute_value (GFileInfo *info,
649 const char *attribute)
652 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
653 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
655 return g_file_info_find_value_by_name (info, attribute);
659 * g_file_info_get_attribute_as_string:
660 * @info: a #GFileInfo.
661 * @attribute: a file attribute key.
663 * Gets the value of a attribute, formated as a string.
664 * This escapes things as needed to make the string valid
667 * Returns: a UTF-8 string associated with the given @attribute.
668 * When you're done with the string it must be freed with g_free().
671 g_file_info_get_attribute_as_string (GFileInfo *info,
672 const char *attribute)
674 GFileAttributeValue *val;
675 val = _g_file_info_get_attribute_value (info, attribute);
677 return _g_file_attribute_value_as_string (val);
683 * g_file_info_get_attribute_object:
684 * @info: a #GFileInfo.
685 * @attribute: a file attribute key.
687 * Gets the value of a #GObject attribute. If the attribute does
688 * not contain a #GObject, %NULL will be returned.
690 * Returns: a #GObject associated with the given @attribute, or
694 g_file_info_get_attribute_object (GFileInfo *info,
695 const char *attribute)
697 GFileAttributeValue *value;
699 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
700 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
702 value = g_file_info_find_value_by_name (info, attribute);
703 return _g_file_attribute_value_get_object (value);
707 * g_file_info_get_attribute_string:
708 * @info: a #GFileInfo.
709 * @attribute: a file attribute key.
711 * Gets the value of a string attribute. If the attribute does
712 * not contain a string, %NULL will be returned.
714 * Returns: the contents of the @attribute value as a string, or
718 g_file_info_get_attribute_string (GFileInfo *info,
719 const char *attribute)
721 GFileAttributeValue *value;
723 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
724 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
726 value = g_file_info_find_value_by_name (info, attribute);
727 return _g_file_attribute_value_get_string (value);
731 * g_file_info_get_attribute_byte_string:
732 * @info: a #GFileInfo.
733 * @attribute: a file attribute key.
735 * Gets the value of a byte string attribute. If the attribute does
736 * not contain a byte string, %NULL will be returned.
738 * Returns: the contents of the @attribute value as a byte string, or
742 g_file_info_get_attribute_byte_string (GFileInfo *info,
743 const char *attribute)
745 GFileAttributeValue *value;
747 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
748 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
750 value = g_file_info_find_value_by_name (info, attribute);
751 return _g_file_attribute_value_get_byte_string (value);
755 * g_file_info_get_attribute_stringv:
756 * @info: a #GFileInfo.
757 * @attribute: a file attribute key.
759 * Gets the value of a stringv attribute. If the attribute does
760 * not contain a stringv, %NULL will be returned.
762 * Returns: the contents of the @attribute value as a stringv, or
763 * %NULL otherwise. Do not free.
766 g_file_info_get_attribute_stringv (GFileInfo *info,
767 const char *attribute)
769 GFileAttributeValue *value;
771 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
772 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
774 value = g_file_info_find_value_by_name (info, attribute);
775 return _g_file_attribute_value_get_stringv (value);
779 * g_file_info_get_attribute_boolean:
780 * @info: a #GFileInfo.
781 * @attribute: a file attribute key.
783 * Gets the value of a boolean attribute. If the attribute does not
784 * contain a boolean value, %FALSE will be returned.
786 * Returns: the boolean value contained within the attribute.
789 g_file_info_get_attribute_boolean (GFileInfo *info,
790 const char *attribute)
792 GFileAttributeValue *value;
794 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
795 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
797 value = g_file_info_find_value_by_name (info, attribute);
798 return _g_file_attribute_value_get_boolean (value);
802 * g_file_info_get_attribute_uint32:
803 * @info: a #GFileInfo.
804 * @attribute: a file attribute key.
806 * Gets an unsigned 32-bit integer contained within the attribute. If the
807 * attribute does not contain an unsigned 32-bit integer, or is invalid,
808 * 0 will be returned.
810 * Returns: an unsigned 32-bit integer from the attribute.
813 g_file_info_get_attribute_uint32 (GFileInfo *info,
814 const char *attribute)
816 GFileAttributeValue *value;
818 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
819 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
821 value = g_file_info_find_value_by_name (info, attribute);
822 return _g_file_attribute_value_get_uint32 (value);
826 * g_file_info_get_attribute_int32:
827 * @info: a #GFileInfo.
828 * @attribute: a file attribute key.
830 * Gets a signed 32-bit integer contained within the attribute. If the
831 * attribute does not contain a signed 32-bit integer, or is invalid,
832 * 0 will be returned.
834 * Returns: a signed 32-bit integer from the attribute.
837 g_file_info_get_attribute_int32 (GFileInfo *info,
838 const char *attribute)
840 GFileAttributeValue *value;
842 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
843 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
845 value = g_file_info_find_value_by_name (info, attribute);
846 return _g_file_attribute_value_get_int32 (value);
850 * g_file_info_get_attribute_uint64:
851 * @info: a #GFileInfo.
852 * @attribute: a file attribute key.
854 * Gets a unsigned 64-bit integer contained within the attribute. If the
855 * attribute does not contain an unsigned 64-bit integer, or is invalid,
856 * 0 will be returned.
858 * Returns: a unsigned 64-bit integer from the attribute.
861 g_file_info_get_attribute_uint64 (GFileInfo *info,
862 const char *attribute)
864 GFileAttributeValue *value;
866 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
867 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
869 value = g_file_info_find_value_by_name (info, attribute);
870 return _g_file_attribute_value_get_uint64 (value);
874 * g_file_info_get_attribute_int64:
875 * @info: a #GFileInfo.
876 * @attribute: a file attribute key.
878 * Gets a signed 64-bit integer contained within the attribute. If the
879 * attribute does not contain an signed 64-bit integer, or is invalid,
880 * 0 will be returned.
882 * Returns: a signed 64-bit integer from the attribute.
885 g_file_info_get_attribute_int64 (GFileInfo *info,
886 const char *attribute)
888 GFileAttributeValue *value;
890 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
891 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
893 value = g_file_info_find_value_by_name (info, attribute);
894 return _g_file_attribute_value_get_int64 (value);
897 static GFileAttributeValue *
898 g_file_info_create_value (GFileInfo *info,
901 GFileAttribute *attrs;
904 if (info->mask != NO_ATTRIBUTE_MASK &&
905 !g_file_attribute_matcher_matches_id (info->mask, attr_id))
908 i = g_file_info_find_place (info, attr_id);
910 attrs = (GFileAttribute *)info->attributes->data;
911 if (i < info->attributes->len &&
912 attrs[i].attribute == attr_id)
913 return &attrs[i].value;
916 GFileAttribute attr = { 0 };
917 attr.attribute = attr_id;
918 g_array_insert_val (info->attributes, i, attr);
920 attrs = (GFileAttribute *)info->attributes->data;
921 return &attrs[i].value;
925 static GFileAttributeValue *
926 g_file_info_create_value_by_name (GFileInfo *info,
927 const char *attribute)
931 attr_id = lookup_attribute (attribute);
933 return g_file_info_create_value (info, attr_id);
937 * g_file_info_set_attribute:
938 * @info: a #GFileInfo.
939 * @attribute: a file attribute key.
940 * @type: a #GFileAttributeType
941 * @value_p: pointer to the value
943 * Sets the @attribute to contain the given value, if possible.
946 g_file_info_set_attribute (GFileInfo *info,
947 const char *attribute,
948 GFileAttributeType type,
951 GFileAttributeValue *value;
953 g_return_if_fail (G_IS_FILE_INFO (info));
954 g_return_if_fail (attribute != NULL && *attribute != '\0');
956 value = g_file_info_create_value_by_name (info, attribute);
959 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
963 * g_file_info_set_attribute_object:
964 * @info: a #GFileInfo.
965 * @attribute: a file attribute key.
966 * @attr_value: a #GObject.
968 * Sets the @attribute to contain the given @attr_value,
972 g_file_info_set_attribute_object (GFileInfo *info,
973 const char *attribute,
976 GFileAttributeValue *value;
978 g_return_if_fail (G_IS_FILE_INFO (info));
979 g_return_if_fail (attribute != NULL && *attribute != '\0');
980 g_return_if_fail (G_IS_OBJECT (attr_value));
982 value = g_file_info_create_value_by_name (info, attribute);
984 _g_file_attribute_value_set_object (value, attr_value);
988 * g_file_info_set_attribute_stringv:
989 * @info: a #GFileInfo.
990 * @attribute: a file attribute key.
991 * @attr_value: a %NULL terminated string array
993 * Sets the @attribute to contain the given @attr_value,
999 g_file_info_set_attribute_stringv (GFileInfo *info,
1000 const char *attribute,
1003 GFileAttributeValue *value;
1005 g_return_if_fail (G_IS_FILE_INFO (info));
1006 g_return_if_fail (attribute != NULL && *attribute != '\0');
1007 g_return_if_fail (attr_value != NULL);
1009 value = g_file_info_create_value_by_name (info, attribute);
1011 _g_file_attribute_value_set_stringv (value, attr_value);
1015 * g_file_info_set_attribute_string:
1016 * @info: a #GFileInfo.
1017 * @attribute: a file attribute key.
1018 * @attr_value: a string.
1020 * Sets the @attribute to contain the given @attr_value,
1024 g_file_info_set_attribute_string (GFileInfo *info,
1025 const char *attribute,
1026 const char *attr_value)
1028 GFileAttributeValue *value;
1030 g_return_if_fail (G_IS_FILE_INFO (info));
1031 g_return_if_fail (attribute != NULL && *attribute != '\0');
1032 g_return_if_fail (attr_value != NULL);
1034 value = g_file_info_create_value_by_name (info, attribute);
1036 _g_file_attribute_value_set_string (value, attr_value);
1040 * g_file_info_set_attribute_byte_string:
1041 * @info: a #GFileInfo.
1042 * @attribute: a file attribute key.
1043 * @attr_value: a byte string.
1045 * Sets the @attribute to contain the given @attr_value,
1049 g_file_info_set_attribute_byte_string (GFileInfo *info,
1050 const char *attribute,
1051 const char *attr_value)
1053 GFileAttributeValue *value;
1055 g_return_if_fail (G_IS_FILE_INFO (info));
1056 g_return_if_fail (attribute != NULL && *attribute != '\0');
1057 g_return_if_fail (attr_value != NULL);
1059 value = g_file_info_create_value_by_name (info, attribute);
1061 _g_file_attribute_value_set_byte_string (value, attr_value);
1065 * g_file_info_set_attribute_boolean:
1066 * @info: a #GFileInfo.
1067 * @attribute: a file attribute key.
1068 * @attr_value: a boolean value.
1070 * Sets the @attribute to contain the given @attr_value,
1074 g_file_info_set_attribute_boolean (GFileInfo *info,
1075 const char *attribute,
1076 gboolean attr_value)
1078 GFileAttributeValue *value;
1080 g_return_if_fail (G_IS_FILE_INFO (info));
1081 g_return_if_fail (attribute != NULL && *attribute != '\0');
1083 value = g_file_info_create_value_by_name (info, attribute);
1085 _g_file_attribute_value_set_boolean (value, attr_value);
1089 * g_file_info_set_attribute_uint32:
1090 * @info: a #GFileInfo.
1091 * @attribute: a file attribute key.
1092 * @attr_value: an unsigned 32-bit integer.
1094 * Sets the @attribute to contain the given @attr_value,
1098 g_file_info_set_attribute_uint32 (GFileInfo *info,
1099 const char *attribute,
1102 GFileAttributeValue *value;
1104 g_return_if_fail (G_IS_FILE_INFO (info));
1105 g_return_if_fail (attribute != NULL && *attribute != '\0');
1107 value = g_file_info_create_value_by_name (info, attribute);
1109 _g_file_attribute_value_set_uint32 (value, attr_value);
1114 * g_file_info_set_attribute_int32:
1115 * @info: a #GFileInfo.
1116 * @attribute: a file attribute key.
1117 * @attr_value: a signed 32-bit integer
1119 * Sets the @attribute to contain the given @attr_value,
1123 g_file_info_set_attribute_int32 (GFileInfo *info,
1124 const char *attribute,
1127 GFileAttributeValue *value;
1129 g_return_if_fail (G_IS_FILE_INFO (info));
1130 g_return_if_fail (attribute != NULL && *attribute != '\0');
1132 value = g_file_info_create_value_by_name (info, attribute);
1134 _g_file_attribute_value_set_int32 (value, attr_value);
1138 * g_file_info_set_attribute_uint64:
1139 * @info: a #GFileInfo.
1140 * @attribute: a file attribute key.
1141 * @attr_value: an unsigned 64-bit integer.
1143 * Sets the @attribute to contain the given @attr_value,
1147 g_file_info_set_attribute_uint64 (GFileInfo *info,
1148 const char *attribute,
1151 GFileAttributeValue *value;
1153 g_return_if_fail (G_IS_FILE_INFO (info));
1154 g_return_if_fail (attribute != NULL && *attribute != '\0');
1156 value = g_file_info_create_value_by_name (info, attribute);
1158 _g_file_attribute_value_set_uint64 (value, attr_value);
1162 * g_file_info_set_attribute_int64:
1163 * @info: a #GFileInfo.
1164 * @attribute: attribute name to set.
1165 * @attr_value: int64 value to set attribute to.
1167 * Sets the @attribute to contain the given @attr_value,
1172 g_file_info_set_attribute_int64 (GFileInfo *info,
1173 const char *attribute,
1176 GFileAttributeValue *value;
1178 g_return_if_fail (G_IS_FILE_INFO (info));
1179 g_return_if_fail (attribute != NULL && *attribute != '\0');
1181 value = g_file_info_create_value_by_name (info, attribute);
1183 _g_file_attribute_value_set_int64 (value, attr_value);
1186 /* Helper getters */
1188 * g_file_info_get_file_type:
1189 * @info: a #GFileInfo.
1191 * Gets a file's type (whether it is a regular file, symlink, etc).
1192 * This is different from the file's content type, see g_file_info_get_content_type().
1194 * Returns: a #GFileType for the given file.
1197 g_file_info_get_file_type (GFileInfo *info)
1199 static guint32 attr = 0;
1200 GFileAttributeValue *value;
1202 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1205 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1207 value = g_file_info_find_value (info, attr);
1208 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1212 * g_file_info_get_is_hidden:
1213 * @info: a #GFileInfo.
1215 * Checks if a file is hidden.
1217 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1220 g_file_info_get_is_hidden (GFileInfo *info)
1222 static guint32 attr = 0;
1223 GFileAttributeValue *value;
1225 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1228 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1230 value = g_file_info_find_value (info, attr);
1231 return (GFileType)_g_file_attribute_value_get_boolean (value);
1235 * g_file_info_get_is_backup:
1236 * @info: a #GFileInfo.
1238 * Checks if a file is a backup file.
1240 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1243 g_file_info_get_is_backup (GFileInfo *info)
1245 static guint32 attr = 0;
1246 GFileAttributeValue *value;
1248 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1251 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1253 value = g_file_info_find_value (info, attr);
1254 return (GFileType)_g_file_attribute_value_get_boolean (value);
1258 * g_file_info_get_is_symlink:
1259 * @info: a #GFileInfo.
1261 * Checks if a file is a symlink.
1263 * Returns: %TRUE if the given @info is a symlink.
1266 g_file_info_get_is_symlink (GFileInfo *info)
1268 static guint32 attr = 0;
1269 GFileAttributeValue *value;
1271 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1274 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1276 value = g_file_info_find_value (info, attr);
1277 return (GFileType)_g_file_attribute_value_get_boolean (value);
1281 * g_file_info_get_name:
1282 * @info: a #GFileInfo.
1284 * Gets the name for a file.
1286 * Returns: a string containing the file name.
1289 g_file_info_get_name (GFileInfo *info)
1291 static guint32 attr = 0;
1292 GFileAttributeValue *value;
1294 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1297 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1299 value = g_file_info_find_value (info, attr);
1300 return _g_file_attribute_value_get_byte_string (value);
1304 * g_file_info_get_display_name:
1305 * @info: a #GFileInfo.
1307 * Gets a display name for a file.
1309 * Returns: a string containing the display name.
1312 g_file_info_get_display_name (GFileInfo *info)
1314 static guint32 attr = 0;
1315 GFileAttributeValue *value;
1317 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1320 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1322 value = g_file_info_find_value (info, attr);
1323 return _g_file_attribute_value_get_string (value);
1327 * g_file_info_get_edit_name:
1328 * @info: a #GFileInfo.
1330 * Gets the edit name for a file.
1332 * Returns: a string containing the edit name.
1335 g_file_info_get_edit_name (GFileInfo *info)
1337 static guint32 attr = 0;
1338 GFileAttributeValue *value;
1340 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1343 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1345 value = g_file_info_find_value (info, attr);
1346 return _g_file_attribute_value_get_string (value);
1350 * g_file_info_get_icon:
1351 * @info: a #GFileInfo.
1353 * Gets the icon for a file.
1355 * Returns: #GIcon for the given @info.
1358 g_file_info_get_icon (GFileInfo *info)
1360 static guint32 attr = 0;
1361 GFileAttributeValue *value;
1364 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1367 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1369 value = g_file_info_find_value (info, attr);
1370 obj = _g_file_attribute_value_get_object (value);
1371 if (G_IS_ICON (obj))
1372 return G_ICON (obj);
1377 * g_file_info_get_content_type:
1378 * @info: a #GFileInfo.
1380 * Gets the file's content type.
1382 * Returns: a string containing the file's content type.
1385 g_file_info_get_content_type (GFileInfo *info)
1387 static guint32 attr = 0;
1388 GFileAttributeValue *value;
1390 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1393 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1395 value = g_file_info_find_value (info, attr);
1396 return _g_file_attribute_value_get_string (value);
1400 * g_file_info_get_size:
1401 * @info: a #GFileInfo.
1403 * Gets the file's size.
1405 * Returns: a #goffset containing the file's size.
1408 g_file_info_get_size (GFileInfo *info)
1410 static guint32 attr = 0;
1411 GFileAttributeValue *value;
1413 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1416 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1418 value = g_file_info_find_value (info, attr);
1419 return (goffset) _g_file_attribute_value_get_uint64 (value);
1423 * g_file_info_get_modification_time:
1424 * @info: a #GFileInfo.
1425 * @result: a #GTimeVal.
1427 * Gets the modification time of the current @info and sets it
1431 g_file_info_get_modification_time (GFileInfo *info,
1434 static guint32 attr_mtime = 0, attr_mtime_usec;
1435 GFileAttributeValue *value;
1437 g_return_if_fail (G_IS_FILE_INFO (info));
1438 g_return_if_fail (result != NULL);
1440 if (attr_mtime == 0)
1442 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1443 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1446 value = g_file_info_find_value (info, attr_mtime);
1447 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1448 value = g_file_info_find_value (info, attr_mtime_usec);
1449 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1453 * g_file_info_get_symlink_target:
1454 * @info: a #GFileInfo.
1456 * Gets the symlink target for a given #GFileInfo.
1458 * Returns: a string containing the symlink target.
1461 g_file_info_get_symlink_target (GFileInfo *info)
1463 static guint32 attr = 0;
1464 GFileAttributeValue *value;
1466 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1469 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1471 value = g_file_info_find_value (info, attr);
1472 return _g_file_attribute_value_get_byte_string (value);
1476 * g_file_info_get_etag:
1477 * @info: a #GFileInfo.
1479 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1480 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1482 * Returns: a string containing the value of the "etag:value" attribute.
1485 g_file_info_get_etag (GFileInfo *info)
1487 static guint32 attr = 0;
1488 GFileAttributeValue *value;
1490 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1493 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1495 value = g_file_info_find_value (info, attr);
1496 return _g_file_attribute_value_get_string (value);
1500 * g_file_info_get_sort_order:
1501 * @info: a #GFileInfo.
1503 * Gets the value of the sort_order attribute from the #GFileInfo.
1504 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1506 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1509 g_file_info_get_sort_order (GFileInfo *info)
1511 static guint32 attr = 0;
1512 GFileAttributeValue *value;
1514 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1517 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1519 value = g_file_info_find_value (info, attr);
1520 return _g_file_attribute_value_get_int32 (value);
1523 /* Helper setters: */
1525 * g_file_info_set_file_type:
1526 * @info: a #GFileInfo.
1527 * @type: a #GFileType.
1529 * Sets the file type in a #GFileInfo to @type.
1530 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1533 g_file_info_set_file_type (GFileInfo *info,
1536 static guint32 attr = 0;
1537 GFileAttributeValue *value;
1539 g_return_if_fail (G_IS_FILE_INFO (info));
1542 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1544 value = g_file_info_create_value (info, attr);
1546 _g_file_attribute_value_set_uint32 (value, type);
1550 * g_file_info_set_is_hidden:
1551 * @info: a #GFileInfo.
1552 * @is_hidden: a #gboolean.
1554 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1555 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1558 g_file_info_set_is_hidden (GFileInfo *info,
1561 static guint32 attr = 0;
1562 GFileAttributeValue *value;
1564 g_return_if_fail (G_IS_FILE_INFO (info));
1567 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1569 value = g_file_info_create_value (info, attr);
1571 _g_file_attribute_value_set_boolean (value, is_hidden);
1575 * g_file_info_set_is_symlink:
1576 * @info: a #GFileInfo.
1577 * @is_symlink: a #gboolean.
1579 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1580 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1583 g_file_info_set_is_symlink (GFileInfo *info,
1584 gboolean is_symlink)
1586 static guint32 attr = 0;
1587 GFileAttributeValue *value;
1589 g_return_if_fail (G_IS_FILE_INFO (info));
1592 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1594 value = g_file_info_create_value (info, attr);
1596 _g_file_attribute_value_set_boolean (value, is_symlink);
1600 * g_file_info_set_name:
1601 * @info: a #GFileInfo.
1602 * @name: a string containing a name.
1604 * Sets the name attribute for the current #GFileInfo.
1605 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1608 g_file_info_set_name (GFileInfo *info,
1611 static guint32 attr = 0;
1612 GFileAttributeValue *value;
1614 g_return_if_fail (G_IS_FILE_INFO (info));
1615 g_return_if_fail (name != NULL);
1618 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1620 value = g_file_info_create_value (info, attr);
1622 _g_file_attribute_value_set_byte_string (value, name);
1626 * g_file_info_set_display_name:
1627 * @info: a #GFileInfo.
1628 * @display_name: a string containing a display name.
1630 * Sets the display name for the current #GFileInfo.
1631 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1634 g_file_info_set_display_name (GFileInfo *info,
1635 const char *display_name)
1637 static guint32 attr = 0;
1638 GFileAttributeValue *value;
1640 g_return_if_fail (G_IS_FILE_INFO (info));
1641 g_return_if_fail (display_name != NULL);
1644 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1646 value = g_file_info_create_value (info, attr);
1648 _g_file_attribute_value_set_string (value, display_name);
1652 * g_file_info_set_edit_name:
1653 * @info: a #GFileInfo.
1654 * @edit_name: a string containing an edit name.
1656 * Sets the edit name for the current file.
1657 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1660 g_file_info_set_edit_name (GFileInfo *info,
1661 const char *edit_name)
1663 static guint32 attr = 0;
1664 GFileAttributeValue *value;
1666 g_return_if_fail (G_IS_FILE_INFO (info));
1667 g_return_if_fail (edit_name != NULL);
1670 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1672 value = g_file_info_create_value (info, attr);
1674 _g_file_attribute_value_set_string (value, edit_name);
1678 * g_file_info_set_icon:
1679 * @info: a #GFileInfo.
1682 * Sets the icon for a given #GFileInfo.
1683 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1686 g_file_info_set_icon (GFileInfo *info,
1689 static guint32 attr = 0;
1690 GFileAttributeValue *value;
1692 g_return_if_fail (G_IS_FILE_INFO (info));
1693 g_return_if_fail (G_IS_ICON (icon));
1696 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1698 value = g_file_info_create_value (info, attr);
1700 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1704 * g_file_info_set_content_type:
1705 * @info: a #GFileInfo.
1706 * @content_type: a content type. See #GContentType.
1708 * Sets the content type attribute for a given #GFileInfo.
1709 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1712 g_file_info_set_content_type (GFileInfo *info,
1713 const char *content_type)
1715 static guint32 attr = 0;
1716 GFileAttributeValue *value;
1718 g_return_if_fail (G_IS_FILE_INFO (info));
1719 g_return_if_fail (content_type != NULL);
1722 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1724 value = g_file_info_create_value (info, attr);
1726 _g_file_attribute_value_set_string (value, content_type);
1730 * g_file_info_set_size:
1731 * @info: a #GFileInfo.
1732 * @size: a #goffset containing the file's size.
1734 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1735 * to the given size.
1738 g_file_info_set_size (GFileInfo *info,
1741 static guint32 attr = 0;
1742 GFileAttributeValue *value;
1744 g_return_if_fail (G_IS_FILE_INFO (info));
1747 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1749 value = g_file_info_create_value (info, attr);
1751 _g_file_attribute_value_set_uint64 (value, size);
1755 * g_file_info_set_modification_time
1756 * @info: a #GFileInfo.
1757 * @mtime: a #GTimeVal.
1759 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
1760 * info to the given time value.
1763 g_file_info_set_modification_time (GFileInfo *info,
1766 static guint32 attr_mtime = 0, attr_mtime_usec;
1767 GFileAttributeValue *value;
1769 g_return_if_fail (G_IS_FILE_INFO (info));
1770 g_return_if_fail (mtime != NULL);
1772 if (attr_mtime == 0)
1774 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1775 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1778 value = g_file_info_create_value (info, attr_mtime);
1780 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
1781 value = g_file_info_create_value (info, attr_mtime_usec);
1783 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
1787 * g_file_info_set_symlink_target:
1788 * @info: a #GFileInfo.
1789 * @symlink_target: a static string containing a path to a symlink target.
1791 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
1792 * to the given symlink target.
1795 g_file_info_set_symlink_target (GFileInfo *info,
1796 const char *symlink_target)
1798 static guint32 attr = 0;
1799 GFileAttributeValue *value;
1801 g_return_if_fail (G_IS_FILE_INFO (info));
1802 g_return_if_fail (symlink_target != NULL);
1805 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1807 value = g_file_info_create_value (info, attr);
1809 _g_file_attribute_value_set_byte_string (value, symlink_target);
1813 * g_file_info_set_sort_order:
1814 * @info: a #GFileInfo.
1815 * @sort_order: a sort order integer.
1817 * Sets the sort order attribute in the file info structure. See
1818 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1821 g_file_info_set_sort_order (GFileInfo *info,
1824 static guint32 attr = 0;
1825 GFileAttributeValue *value;
1827 g_return_if_fail (G_IS_FILE_INFO (info));
1830 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1832 value = g_file_info_create_value (info, attr);
1834 _g_file_attribute_value_set_int32 (value, sort_order);
1838 #define ON_STACK_MATCHERS 5
1845 struct _GFileAttributeMatcher {
1847 SubMatcher sub_matchers[ON_STACK_MATCHERS];
1848 GArray *more_sub_matchers;
1851 guint32 iterator_ns;
1857 matcher_add (GFileAttributeMatcher *matcher,
1861 SubMatcher *sub_matchers;
1865 for (i = 0; i < ON_STACK_MATCHERS; i++)
1867 /* First empty spot, not found, use this */
1868 if (matcher->sub_matchers[i].id == 0)
1870 matcher->sub_matchers[i].id = id;
1871 matcher->sub_matchers[i].mask = mask;
1876 if (matcher->sub_matchers[i].id == id &&
1877 matcher->sub_matchers[i].mask == mask)
1881 if (matcher->more_sub_matchers == NULL)
1882 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
1884 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
1885 for (i = 0; i < matcher->more_sub_matchers->len; i++)
1888 if (sub_matchers[i].id == id &&
1889 sub_matchers[i].mask == mask)
1896 g_array_append_val (matcher->more_sub_matchers, s);
1900 * g_file_attribute_matcher_new:
1901 * @attributes: an attribute string to match.
1903 * Creates a new file attribute matcher, which matches attributes
1904 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
1905 * counted structures, and are created with a reference count of 1. If
1906 * the number of references falls to 0, the #GFileAttributeMatcher is
1907 * automatically destroyed.
1909 * The @attribute string should be formatted with specific keys separated
1910 * from namespaces with a double colon. Several "namespace::key" strings may be
1911 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
1912 * The wildcard "*" may be used to match all keys and namespaces, or
1913 * "namespace::*" will match all keys in a given namespace.
1915 * Examples of strings to use:
1917 * <title>File Attribute Matcher strings and results</title>
1918 * <tgroup cols='2' align='left'><thead>
1919 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
1921 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
1922 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
1923 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
1924 * all keys in the unix namespace.</entry></row>
1928 * Returns: a #GFileAttributeMatcher.
1930 GFileAttributeMatcher *
1931 g_file_attribute_matcher_new (const char *attributes)
1936 GFileAttributeMatcher *matcher;
1938 if (attributes == NULL || *attributes == '\0')
1941 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
1944 split = g_strsplit (attributes, ",", -1);
1946 for (i = 0; split[i] != NULL; i++)
1948 if (strcmp (split[i], "*") == 0)
1949 matcher->all = TRUE;
1954 colon = strstr (split[i], "::");
1955 if (colon != NULL &&
1960 id = lookup_attribute (split[i]);
1968 id = lookup_namespace (split[i]) << NS_POS;
1969 mask = NS_MASK << NS_POS;
1972 matcher_add (matcher, id, mask);
1982 * g_file_attribute_matcher_ref:
1983 * @matcher: a #GFileAttributeMatcher.
1985 * References a file attribute matcher.
1987 * Returns: a #GFileAttributeMatcher.
1989 GFileAttributeMatcher *
1990 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
1994 g_return_val_if_fail (matcher->ref > 0, NULL);
1995 g_atomic_int_inc (&matcher->ref);
2001 * g_file_attribute_matcher_unref:
2002 * @matcher: a #GFileAttributeMatcher.
2004 * Unreferences @matcher. If the reference count falls below 1,
2005 * the @matcher is automatically freed.
2009 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2013 g_return_if_fail (matcher->ref > 0);
2015 if (g_atomic_int_dec_and_test (&matcher->ref))
2017 if (matcher->more_sub_matchers)
2018 g_array_free (matcher->more_sub_matchers, TRUE);
2026 * g_file_attribute_matcher_matches_only:
2027 * @matcher: a #GFileAttributeMatcher.
2028 * @attribute: a file attribute key.
2030 * Checks if a attribute matcher only matches a given attribute. Always
2031 * returns %FALSE if "*" was used when creating the matcher.
2033 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2036 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2037 const char *attribute)
2041 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2043 if (matcher == NULL ||
2047 id = lookup_attribute (attribute);
2049 if (matcher->sub_matchers[0].id != 0 &&
2050 matcher->sub_matchers[1].id == 0 &&
2051 matcher->sub_matchers[0].mask == 0xffffffff &&
2052 matcher->sub_matchers[0].id == id)
2059 matcher_matches_id (GFileAttributeMatcher *matcher,
2062 SubMatcher *sub_matchers;
2065 for (i = 0; i < ON_STACK_MATCHERS; i++)
2067 if (matcher->sub_matchers[i].id == 0)
2070 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2074 if (matcher->more_sub_matchers)
2076 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2077 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2079 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2088 g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2091 /* We return a NULL matcher for an empty match string, so handle this */
2092 if (matcher == NULL)
2098 return matcher_matches_id (matcher, id);
2102 * g_file_attribute_matcher_matches:
2103 * @matcher: a #GFileAttributeMatcher.
2104 * @attribute: a file attribute key.
2106 * Checks if an attribute will be matched by an attribute matcher. If
2107 * the matcher was created with the "*" matching string, this function
2108 * will always return %TRUE.
2110 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2113 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2114 const char *attribute)
2116 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2118 /* We return a NULL matcher for an empty match string, so handle this */
2119 if (matcher == NULL)
2125 return matcher_matches_id (matcher, lookup_attribute (attribute));
2128 /* return TRUE -> all */
2130 * g_file_attribute_matcher_enumerate_namespace:
2131 * @matcher: a #GFileAttributeMatcher.
2132 * @ns: a string containing a file attribute namespace.
2134 * Checks if the matcher will match all of the keys in a given namespace.
2135 * This will always return %TRUE if a wildcard character is in use (e.g. if
2136 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2137 * using "*" and namespace is anything.)
2139 * TODO: this is awkwardly worded.
2141 * Returns: %TRUE if the matcher matches all of the entries
2142 * in the given @ns, %FALSE otherwise.
2145 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2148 SubMatcher *sub_matchers;
2152 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2154 /* We return a NULL matcher for an empty match string, so handle this */
2155 if (matcher == NULL)
2161 ns_id = lookup_namespace (ns) << NS_POS;
2163 for (i = 0; i < ON_STACK_MATCHERS; i++)
2165 if (matcher->sub_matchers[i].id == ns_id)
2169 if (matcher->more_sub_matchers)
2171 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2172 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2174 if (sub_matchers[i].id == ns_id)
2179 matcher->iterator_ns = ns_id;
2180 matcher->iterator_pos = 0;
2186 * g_file_attribute_matcher_enumerate_next:
2187 * @matcher: a #GFileAttributeMatcher.
2189 * Gets the next matched attribute from a #GFileAttributeMatcher.
2191 * Returns: a string containing the next attribute or %NULL if
2192 * no more attribute exist.
2195 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2198 SubMatcher *sub_matcher;
2200 /* We return a NULL matcher for an empty match string, so handle this */
2201 if (matcher == NULL)
2206 i = matcher->iterator_pos++;
2208 if (i < ON_STACK_MATCHERS)
2210 if (matcher->sub_matchers[i].id == 0)
2213 sub_matcher = &matcher->sub_matchers[i];
2217 if (matcher->more_sub_matchers == NULL)
2220 i -= ON_STACK_MATCHERS;
2221 if (i < matcher->more_sub_matchers->len)
2222 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2227 if (sub_matcher->mask == 0xffffffff &&
2228 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2229 return get_attribute_for_id (sub_matcher->id);
2233 #define __G_FILE_INFO_C__
2234 #include "gioaliasdef.c"