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_attribute (const char *attribute)
151 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
156 colon = strstr (attribute, "::");
158 ns = g_strndup (attribute, colon - attribute);
162 ns_info = _lookup_namespace (ns);
165 id = ++ns_info->attribute_id_counter;
166 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
167 attributes[ns_info->id][id] = g_strdup (attribute);
169 attr_id = MAKE_ATTR_ID (ns_info->id, id);
171 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
177 ensure_attribute_hash (void)
179 if (attribute_hash != NULL)
182 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
183 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
189 lookup_namespace (const char *namespace)
194 G_LOCK (attribute_hash);
196 ensure_attribute_hash ();
198 ns_info = _lookup_namespace (namespace);
203 G_UNLOCK (attribute_hash);
209 get_attribute_for_id (int attribute)
212 G_LOCK (attribute_hash);
213 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
214 G_UNLOCK (attribute_hash);
219 lookup_attribute (const char *attribute)
223 G_LOCK (attribute_hash);
224 ensure_attribute_hash ();
226 attr_id = _lookup_attribute (attribute);
228 G_UNLOCK (attribute_hash);
234 g_file_info_finalize (GObject *object)
238 GFileAttribute *attrs;
240 info = G_FILE_INFO (object);
242 attrs = (GFileAttribute *)info->attributes->data;
243 for (i = 0; i < info->attributes->len; i++)
244 _g_file_attribute_value_clear (&attrs[i].value);
245 g_array_free (info->attributes, TRUE);
247 if (info->mask != NO_ATTRIBUTE_MASK)
248 g_file_attribute_matcher_unref (info->mask);
250 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
254 g_file_info_class_init (GFileInfoClass *klass)
256 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
258 gobject_class->finalize = g_file_info_finalize;
262 g_file_info_init (GFileInfo *info)
264 info->mask = NO_ATTRIBUTE_MASK;
265 info->attributes = g_array_new (FALSE, FALSE,
266 sizeof (GFileAttribute));
272 * Creates a new file info structure.
274 * Returns: a #GFileInfo.
277 g_file_info_new (void)
279 return g_object_new (G_TYPE_FILE_INFO, NULL);
283 * g_file_info_copy_into:
284 * @src_info: source to copy attributes from.
285 * @dest_info: destination to copy attributes to.
287 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
290 g_file_info_copy_into (GFileInfo *src_info,
291 GFileInfo *dest_info)
293 GFileAttribute *source, *dest;
296 g_return_if_fail (G_IS_FILE_INFO (src_info));
297 g_return_if_fail (G_IS_FILE_INFO (dest_info));
299 dest = (GFileAttribute *)dest_info->attributes->data;
300 for (i = 0; i < dest_info->attributes->len; i++)
301 _g_file_attribute_value_clear (&dest[i].value);
303 g_array_set_size (dest_info->attributes,
304 src_info->attributes->len);
306 source = (GFileAttribute *)src_info->attributes->data;
307 dest = (GFileAttribute *)dest_info->attributes->data;
309 for (i = 0; i < src_info->attributes->len; i++)
311 dest[i].attribute = source[i].attribute;
312 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
313 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
316 if (dest_info->mask != NO_ATTRIBUTE_MASK)
317 g_file_attribute_matcher_unref (dest_info->mask);
319 if (src_info->mask == NO_ATTRIBUTE_MASK)
320 dest_info->mask = NO_ATTRIBUTE_MASK;
322 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
327 * @other: a #GFileInfo.
329 * Duplicates a file info structure.
331 * Returns: a duplicate #GFileInfo of @other.
334 g_file_info_dup (GFileInfo *other)
338 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
340 new = g_file_info_new ();
341 g_file_info_copy_into (other, new);
346 * g_file_info_set_attribute_mask:
347 * @info: a #GFileInfo.
348 * @mask: a #GFileAttributeMatcher.
350 * Sets @mask on @info to match specific attribute types.
353 g_file_info_set_attribute_mask (GFileInfo *info,
354 GFileAttributeMatcher *mask)
356 GFileAttribute *attr;
359 g_return_if_fail (G_IS_FILE_INFO (info));
361 if (mask != info->mask)
363 if (info->mask != NO_ATTRIBUTE_MASK)
364 g_file_attribute_matcher_unref (info->mask);
365 info->mask = g_file_attribute_matcher_ref (mask);
367 /* Remove non-matching attributes */
368 for (i = 0; i < info->attributes->len; i++)
370 attr = &g_array_index (info->attributes, GFileAttribute, i);
371 if (!g_file_attribute_matcher_matches_id (mask,
374 _g_file_attribute_value_clear (&attr->value);
375 g_array_remove_index (info->attributes, i);
383 * g_file_info_unset_attribute_mask:
386 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
390 g_file_info_unset_attribute_mask (GFileInfo *info)
392 g_return_if_fail (G_IS_FILE_INFO (info));
394 if (info->mask != NO_ATTRIBUTE_MASK)
395 g_file_attribute_matcher_unref (info->mask);
396 info->mask = NO_ATTRIBUTE_MASK;
400 * g_file_info_clear_status:
401 * @info: a #GFileInfo.
403 * Clears the status information from @info.
406 g_file_info_clear_status (GFileInfo *info)
408 GFileAttribute *attrs;
411 g_return_if_fail (G_IS_FILE_INFO (info));
413 attrs = (GFileAttribute *)info->attributes->data;
414 for (i = 0; i < info->attributes->len; i++)
415 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
419 g_file_info_find_place (GFileInfo *info,
423 GFileAttribute *attrs;
424 /* Binary search for the place where attribute would be, if it's
428 max = info->attributes->len;
430 attrs = (GFileAttribute *)info->attributes->data;
434 med = min + (max - min) / 2;
435 if (attrs[med].attribute == attribute)
440 else if (attrs[med].attribute < attribute)
442 else /* attrs[med].attribute > attribute */
449 static GFileAttributeValue *
450 g_file_info_find_value (GFileInfo *info,
453 GFileAttribute *attrs;
456 i = g_file_info_find_place (info, attr_id);
457 attrs = (GFileAttribute *)info->attributes->data;
458 if (i < info->attributes->len &&
459 attrs[i].attribute == attr_id)
460 return &attrs[i].value;
465 static GFileAttributeValue *
466 g_file_info_find_value_by_name (GFileInfo *info,
467 const char *attribute)
471 attr_id = lookup_attribute (attribute);
472 return g_file_info_find_value (info, attr_id);
476 * g_file_info_has_attribute:
477 * @info: a #GFileInfo.
478 * @attribute: a file attribute key.
480 * Checks if a file info structure has an attribute named @attribute.
482 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
486 g_file_info_has_attribute (GFileInfo *info,
487 const char *attribute)
489 GFileAttributeValue *value;
491 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
492 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
494 value = g_file_info_find_value_by_name (info, attribute);
495 return value != NULL;
499 * g_file_info_has_namespace:
500 * @info: a #GFileInfo.
501 * @name_space: a file attribute namespace.
503 * Checks if a file info structure has an attribute in the
504 * specified @name_space.
506 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
512 g_file_info_has_namespace (GFileInfo *info,
513 const char *name_space)
515 GFileAttribute *attrs;
519 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
520 g_return_val_if_fail (name_space != NULL, FALSE);
522 ns_id = lookup_namespace (name_space);
524 attrs = (GFileAttribute *)info->attributes->data;
525 for (i = 0; i < info->attributes->len; i++)
527 if (GET_NS (attrs[i].attribute) == ns_id)
535 * g_file_info_list_attributes:
536 * @info: a #GFileInfo.
537 * @name_space: a file attribute key's namespace.
539 * Lists the file info structure's attributes.
541 * Returns: a null-terminated array of strings of all of the
542 * possible attribute types for the given @name_space, or
546 g_file_info_list_attributes (GFileInfo *info,
547 const char *name_space)
550 GFileAttribute *attrs;
552 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
555 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
557 names = g_ptr_array_new ();
558 attrs = (GFileAttribute *)info->attributes->data;
559 for (i = 0; i < info->attributes->len; i++)
561 attribute = attrs[i].attribute;
562 if (ns_id == 0 || GET_NS (attribute) == ns_id)
563 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
567 g_ptr_array_add (names, NULL);
569 return (char **)g_ptr_array_free (names, FALSE);
573 * g_file_info_get_attribute_type:
574 * @info: a #GFileInfo.
575 * @attribute: a file attribute key.
577 * Gets the attribute type for an attribute key.
579 * Returns: a #GFileAttributeType for the given @attribute, or
580 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
583 g_file_info_get_attribute_type (GFileInfo *info,
584 const char *attribute)
586 GFileAttributeValue *value;
588 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
589 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
591 value = g_file_info_find_value_by_name (info, attribute);
595 return G_FILE_ATTRIBUTE_TYPE_INVALID;
599 * g_file_info_remove_attribute:
600 * @info: a #GFileInfo.
601 * @attribute: a file attribute key.
603 * Removes all cases of @attribute from @info if it exists.
606 g_file_info_remove_attribute (GFileInfo *info,
607 const char *attribute)
610 GFileAttribute *attrs;
613 g_return_if_fail (G_IS_FILE_INFO (info));
614 g_return_if_fail (attribute != NULL && *attribute != '\0');
616 attr_id = lookup_attribute (attribute);
618 i = g_file_info_find_place (info, attr_id);
619 attrs = (GFileAttribute *)info->attributes->data;
620 if (i < info->attributes->len &&
621 attrs[i].attribute == attr_id)
623 _g_file_attribute_value_clear (&attrs[i].value);
624 g_array_remove_index (info->attributes, i);
629 * g_file_info_get_attribute_data:
630 * @info: a #GFileInfo
631 * @attribute: a file attribute key
632 * @type: return location for the attribute type, or %NULL
633 * @value_pp: return location for the attribute value, or %NULL
634 * @status: return location for the attribute status, or %NULL
636 * Gets the attribute type, value and status for an attribute key.
638 * Returns: %TRUE if @info has an attribute named @attribute,
642 g_file_info_get_attribute_data (GFileInfo *info,
643 const char *attribute,
644 GFileAttributeType *type,
646 GFileAttributeStatus *status)
648 GFileAttributeValue *value;
650 value = g_file_info_find_value_by_name (info, attribute);
655 *status = value->status;
661 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
667 * g_file_info_get_attribute_status:
668 * @info: a #GFileInfo
669 * @attribute: a file attribute key
671 * Gets the attribute status for an attribute key.
673 * Returns: a #GFileAttributeStatus for the given @attribute, or
674 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
678 g_file_info_get_attribute_status (GFileInfo *info,
679 const char *attribute)
681 GFileAttributeValue *val;
683 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
684 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
686 val = g_file_info_find_value_by_name (info, attribute);
690 return G_FILE_ATTRIBUTE_STATUS_UNSET;
694 * g_file_info_set_attribute_status:
695 * @info: a #GFileInfo
696 * @attribute: a file attribute key
697 * @status: a #GFileAttributeStatus
699 * Sets the attribute status for an attribute key. This is only
700 * needed by external code that implement g_file_set_attributes_from_info()
701 * or similar functions.
703 * The attribute must exist in @info for this to work. Otherwise %FALSE
704 * is returned and @info is unchanged.
706 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
711 g_file_info_set_attribute_status (GFileInfo *info,
712 const char *attribute,
713 GFileAttributeStatus status)
715 GFileAttributeValue *val;
717 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
718 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
720 val = g_file_info_find_value_by_name (info, attribute);
723 val->status = status;
730 GFileAttributeValue *
731 _g_file_info_get_attribute_value (GFileInfo *info,
732 const char *attribute)
735 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
736 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
738 return g_file_info_find_value_by_name (info, attribute);
742 * g_file_info_get_attribute_as_string:
743 * @info: a #GFileInfo.
744 * @attribute: a file attribute key.
746 * Gets the value of a attribute, formated as a string.
747 * This escapes things as needed to make the string valid
750 * Returns: a UTF-8 string associated with the given @attribute.
751 * When you're done with the string it must be freed with g_free().
754 g_file_info_get_attribute_as_string (GFileInfo *info,
755 const char *attribute)
757 GFileAttributeValue *val;
758 val = _g_file_info_get_attribute_value (info, attribute);
760 return _g_file_attribute_value_as_string (val);
766 * g_file_info_get_attribute_object:
767 * @info: a #GFileInfo.
768 * @attribute: a file attribute key.
770 * Gets the value of a #GObject attribute. If the attribute does
771 * not contain a #GObject, %NULL will be returned.
773 * Returns: a #GObject associated with the given @attribute, or
777 g_file_info_get_attribute_object (GFileInfo *info,
778 const char *attribute)
780 GFileAttributeValue *value;
782 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
783 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
785 value = g_file_info_find_value_by_name (info, attribute);
786 return _g_file_attribute_value_get_object (value);
790 * g_file_info_get_attribute_string:
791 * @info: a #GFileInfo.
792 * @attribute: a file attribute key.
794 * Gets the value of a string attribute. If the attribute does
795 * not contain a string, %NULL will be returned.
797 * Returns: the contents of the @attribute value as a string, or
801 g_file_info_get_attribute_string (GFileInfo *info,
802 const char *attribute)
804 GFileAttributeValue *value;
806 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
807 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
809 value = g_file_info_find_value_by_name (info, attribute);
810 return _g_file_attribute_value_get_string (value);
814 * g_file_info_get_attribute_byte_string:
815 * @info: a #GFileInfo.
816 * @attribute: a file attribute key.
818 * Gets the value of a byte string attribute. If the attribute does
819 * not contain a byte string, %NULL will be returned.
821 * Returns: the contents of the @attribute value as a byte string, or
825 g_file_info_get_attribute_byte_string (GFileInfo *info,
826 const char *attribute)
828 GFileAttributeValue *value;
830 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
831 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
833 value = g_file_info_find_value_by_name (info, attribute);
834 return _g_file_attribute_value_get_byte_string (value);
838 * g_file_info_get_attribute_stringv:
839 * @info: a #GFileInfo.
840 * @attribute: a file attribute key.
842 * Gets the value of a stringv attribute. If the attribute does
843 * not contain a stringv, %NULL will be returned.
845 * Returns: the contents of the @attribute value as a stringv, or
846 * %NULL otherwise. Do not free.
849 g_file_info_get_attribute_stringv (GFileInfo *info,
850 const char *attribute)
852 GFileAttributeValue *value;
854 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
855 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
857 value = g_file_info_find_value_by_name (info, attribute);
858 return _g_file_attribute_value_get_stringv (value);
862 * g_file_info_get_attribute_boolean:
863 * @info: a #GFileInfo.
864 * @attribute: a file attribute key.
866 * Gets the value of a boolean attribute. If the attribute does not
867 * contain a boolean value, %FALSE will be returned.
869 * Returns: the boolean value contained within the attribute.
872 g_file_info_get_attribute_boolean (GFileInfo *info,
873 const char *attribute)
875 GFileAttributeValue *value;
877 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
878 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
880 value = g_file_info_find_value_by_name (info, attribute);
881 return _g_file_attribute_value_get_boolean (value);
885 * g_file_info_get_attribute_uint32:
886 * @info: a #GFileInfo.
887 * @attribute: a file attribute key.
889 * Gets an unsigned 32-bit integer contained within the attribute. If the
890 * attribute does not contain an unsigned 32-bit integer, or is invalid,
891 * 0 will be returned.
893 * Returns: an unsigned 32-bit integer from the attribute.
896 g_file_info_get_attribute_uint32 (GFileInfo *info,
897 const char *attribute)
899 GFileAttributeValue *value;
901 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
902 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
904 value = g_file_info_find_value_by_name (info, attribute);
905 return _g_file_attribute_value_get_uint32 (value);
909 * g_file_info_get_attribute_int32:
910 * @info: a #GFileInfo.
911 * @attribute: a file attribute key.
913 * Gets a signed 32-bit integer contained within the attribute. If the
914 * attribute does not contain a signed 32-bit integer, or is invalid,
915 * 0 will be returned.
917 * Returns: a signed 32-bit integer from the attribute.
920 g_file_info_get_attribute_int32 (GFileInfo *info,
921 const char *attribute)
923 GFileAttributeValue *value;
925 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
926 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
928 value = g_file_info_find_value_by_name (info, attribute);
929 return _g_file_attribute_value_get_int32 (value);
933 * g_file_info_get_attribute_uint64:
934 * @info: a #GFileInfo.
935 * @attribute: a file attribute key.
937 * Gets a unsigned 64-bit integer contained within the attribute. If the
938 * attribute does not contain an unsigned 64-bit integer, or is invalid,
939 * 0 will be returned.
941 * Returns: a unsigned 64-bit integer from the attribute.
944 g_file_info_get_attribute_uint64 (GFileInfo *info,
945 const char *attribute)
947 GFileAttributeValue *value;
949 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
950 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
952 value = g_file_info_find_value_by_name (info, attribute);
953 return _g_file_attribute_value_get_uint64 (value);
957 * g_file_info_get_attribute_int64:
958 * @info: a #GFileInfo.
959 * @attribute: a file attribute key.
961 * Gets a signed 64-bit integer contained within the attribute. If the
962 * attribute does not contain an signed 64-bit integer, or is invalid,
963 * 0 will be returned.
965 * Returns: a signed 64-bit integer from the attribute.
968 g_file_info_get_attribute_int64 (GFileInfo *info,
969 const char *attribute)
971 GFileAttributeValue *value;
973 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
974 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
976 value = g_file_info_find_value_by_name (info, attribute);
977 return _g_file_attribute_value_get_int64 (value);
980 static GFileAttributeValue *
981 g_file_info_create_value (GFileInfo *info,
984 GFileAttribute *attrs;
987 if (info->mask != NO_ATTRIBUTE_MASK &&
988 !g_file_attribute_matcher_matches_id (info->mask, attr_id))
991 i = g_file_info_find_place (info, attr_id);
993 attrs = (GFileAttribute *)info->attributes->data;
994 if (i < info->attributes->len &&
995 attrs[i].attribute == attr_id)
996 return &attrs[i].value;
999 GFileAttribute attr = { 0 };
1000 attr.attribute = attr_id;
1001 g_array_insert_val (info->attributes, i, attr);
1003 attrs = (GFileAttribute *)info->attributes->data;
1004 return &attrs[i].value;
1008 static GFileAttributeValue *
1009 g_file_info_create_value_by_name (GFileInfo *info,
1010 const char *attribute)
1014 attr_id = lookup_attribute (attribute);
1016 return g_file_info_create_value (info, attr_id);
1020 * g_file_info_set_attribute:
1021 * @info: a #GFileInfo.
1022 * @attribute: a file attribute key.
1023 * @type: a #GFileAttributeType
1024 * @value_p: pointer to the value
1026 * Sets the @attribute to contain the given value, if possible.
1029 g_file_info_set_attribute (GFileInfo *info,
1030 const char *attribute,
1031 GFileAttributeType type,
1034 GFileAttributeValue *value;
1036 g_return_if_fail (G_IS_FILE_INFO (info));
1037 g_return_if_fail (attribute != NULL && *attribute != '\0');
1039 value = g_file_info_create_value_by_name (info, attribute);
1042 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1046 * g_file_info_set_attribute_object:
1047 * @info: a #GFileInfo.
1048 * @attribute: a file attribute key.
1049 * @attr_value: a #GObject.
1051 * Sets the @attribute to contain the given @attr_value,
1055 g_file_info_set_attribute_object (GFileInfo *info,
1056 const char *attribute,
1057 GObject *attr_value)
1059 GFileAttributeValue *value;
1061 g_return_if_fail (G_IS_FILE_INFO (info));
1062 g_return_if_fail (attribute != NULL && *attribute != '\0');
1063 g_return_if_fail (G_IS_OBJECT (attr_value));
1065 value = g_file_info_create_value_by_name (info, attribute);
1067 _g_file_attribute_value_set_object (value, attr_value);
1071 * g_file_info_set_attribute_stringv:
1072 * @info: a #GFileInfo.
1073 * @attribute: a file attribute key.
1074 * @attr_value: a %NULL terminated string array
1076 * Sets the @attribute to contain the given @attr_value,
1082 g_file_info_set_attribute_stringv (GFileInfo *info,
1083 const char *attribute,
1086 GFileAttributeValue *value;
1088 g_return_if_fail (G_IS_FILE_INFO (info));
1089 g_return_if_fail (attribute != NULL && *attribute != '\0');
1090 g_return_if_fail (attr_value != NULL);
1092 value = g_file_info_create_value_by_name (info, attribute);
1094 _g_file_attribute_value_set_stringv (value, attr_value);
1098 * g_file_info_set_attribute_string:
1099 * @info: a #GFileInfo.
1100 * @attribute: a file attribute key.
1101 * @attr_value: a string.
1103 * Sets the @attribute to contain the given @attr_value,
1107 g_file_info_set_attribute_string (GFileInfo *info,
1108 const char *attribute,
1109 const char *attr_value)
1111 GFileAttributeValue *value;
1113 g_return_if_fail (G_IS_FILE_INFO (info));
1114 g_return_if_fail (attribute != NULL && *attribute != '\0');
1115 g_return_if_fail (attr_value != NULL);
1117 value = g_file_info_create_value_by_name (info, attribute);
1119 _g_file_attribute_value_set_string (value, attr_value);
1123 * g_file_info_set_attribute_byte_string:
1124 * @info: a #GFileInfo.
1125 * @attribute: a file attribute key.
1126 * @attr_value: a byte string.
1128 * Sets the @attribute to contain the given @attr_value,
1132 g_file_info_set_attribute_byte_string (GFileInfo *info,
1133 const char *attribute,
1134 const char *attr_value)
1136 GFileAttributeValue *value;
1138 g_return_if_fail (G_IS_FILE_INFO (info));
1139 g_return_if_fail (attribute != NULL && *attribute != '\0');
1140 g_return_if_fail (attr_value != NULL);
1142 value = g_file_info_create_value_by_name (info, attribute);
1144 _g_file_attribute_value_set_byte_string (value, attr_value);
1148 * g_file_info_set_attribute_boolean:
1149 * @info: a #GFileInfo.
1150 * @attribute: a file attribute key.
1151 * @attr_value: a boolean value.
1153 * Sets the @attribute to contain the given @attr_value,
1157 g_file_info_set_attribute_boolean (GFileInfo *info,
1158 const char *attribute,
1159 gboolean attr_value)
1161 GFileAttributeValue *value;
1163 g_return_if_fail (G_IS_FILE_INFO (info));
1164 g_return_if_fail (attribute != NULL && *attribute != '\0');
1166 value = g_file_info_create_value_by_name (info, attribute);
1168 _g_file_attribute_value_set_boolean (value, attr_value);
1172 * g_file_info_set_attribute_uint32:
1173 * @info: a #GFileInfo.
1174 * @attribute: a file attribute key.
1175 * @attr_value: an unsigned 32-bit integer.
1177 * Sets the @attribute to contain the given @attr_value,
1181 g_file_info_set_attribute_uint32 (GFileInfo *info,
1182 const char *attribute,
1185 GFileAttributeValue *value;
1187 g_return_if_fail (G_IS_FILE_INFO (info));
1188 g_return_if_fail (attribute != NULL && *attribute != '\0');
1190 value = g_file_info_create_value_by_name (info, attribute);
1192 _g_file_attribute_value_set_uint32 (value, attr_value);
1197 * g_file_info_set_attribute_int32:
1198 * @info: a #GFileInfo.
1199 * @attribute: a file attribute key.
1200 * @attr_value: a signed 32-bit integer
1202 * Sets the @attribute to contain the given @attr_value,
1206 g_file_info_set_attribute_int32 (GFileInfo *info,
1207 const char *attribute,
1210 GFileAttributeValue *value;
1212 g_return_if_fail (G_IS_FILE_INFO (info));
1213 g_return_if_fail (attribute != NULL && *attribute != '\0');
1215 value = g_file_info_create_value_by_name (info, attribute);
1217 _g_file_attribute_value_set_int32 (value, attr_value);
1221 * g_file_info_set_attribute_uint64:
1222 * @info: a #GFileInfo.
1223 * @attribute: a file attribute key.
1224 * @attr_value: an unsigned 64-bit integer.
1226 * Sets the @attribute to contain the given @attr_value,
1230 g_file_info_set_attribute_uint64 (GFileInfo *info,
1231 const char *attribute,
1234 GFileAttributeValue *value;
1236 g_return_if_fail (G_IS_FILE_INFO (info));
1237 g_return_if_fail (attribute != NULL && *attribute != '\0');
1239 value = g_file_info_create_value_by_name (info, attribute);
1241 _g_file_attribute_value_set_uint64 (value, attr_value);
1245 * g_file_info_set_attribute_int64:
1246 * @info: a #GFileInfo.
1247 * @attribute: attribute name to set.
1248 * @attr_value: int64 value to set attribute to.
1250 * Sets the @attribute to contain the given @attr_value,
1255 g_file_info_set_attribute_int64 (GFileInfo *info,
1256 const char *attribute,
1259 GFileAttributeValue *value;
1261 g_return_if_fail (G_IS_FILE_INFO (info));
1262 g_return_if_fail (attribute != NULL && *attribute != '\0');
1264 value = g_file_info_create_value_by_name (info, attribute);
1266 _g_file_attribute_value_set_int64 (value, attr_value);
1269 /* Helper getters */
1271 * g_file_info_get_file_type:
1272 * @info: a #GFileInfo.
1274 * Gets a file's type (whether it is a regular file, symlink, etc).
1275 * This is different from the file's content type, see g_file_info_get_content_type().
1277 * Returns: a #GFileType for the given file.
1280 g_file_info_get_file_type (GFileInfo *info)
1282 static guint32 attr = 0;
1283 GFileAttributeValue *value;
1285 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1288 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1290 value = g_file_info_find_value (info, attr);
1291 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1295 * g_file_info_get_is_hidden:
1296 * @info: a #GFileInfo.
1298 * Checks if a file is hidden.
1300 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1303 g_file_info_get_is_hidden (GFileInfo *info)
1305 static guint32 attr = 0;
1306 GFileAttributeValue *value;
1308 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1311 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1313 value = g_file_info_find_value (info, attr);
1314 return (GFileType)_g_file_attribute_value_get_boolean (value);
1318 * g_file_info_get_is_backup:
1319 * @info: a #GFileInfo.
1321 * Checks if a file is a backup file.
1323 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1326 g_file_info_get_is_backup (GFileInfo *info)
1328 static guint32 attr = 0;
1329 GFileAttributeValue *value;
1331 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1334 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1336 value = g_file_info_find_value (info, attr);
1337 return (GFileType)_g_file_attribute_value_get_boolean (value);
1341 * g_file_info_get_is_symlink:
1342 * @info: a #GFileInfo.
1344 * Checks if a file is a symlink.
1346 * Returns: %TRUE if the given @info is a symlink.
1349 g_file_info_get_is_symlink (GFileInfo *info)
1351 static guint32 attr = 0;
1352 GFileAttributeValue *value;
1354 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1357 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1359 value = g_file_info_find_value (info, attr);
1360 return (GFileType)_g_file_attribute_value_get_boolean (value);
1364 * g_file_info_get_name:
1365 * @info: a #GFileInfo.
1367 * Gets the name for a file.
1369 * Returns: a string containing the file name.
1372 g_file_info_get_name (GFileInfo *info)
1374 static guint32 attr = 0;
1375 GFileAttributeValue *value;
1377 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1380 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1382 value = g_file_info_find_value (info, attr);
1383 return _g_file_attribute_value_get_byte_string (value);
1387 * g_file_info_get_display_name:
1388 * @info: a #GFileInfo.
1390 * Gets a display name for a file.
1392 * Returns: a string containing the display name.
1395 g_file_info_get_display_name (GFileInfo *info)
1397 static guint32 attr = 0;
1398 GFileAttributeValue *value;
1400 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1403 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1405 value = g_file_info_find_value (info, attr);
1406 return _g_file_attribute_value_get_string (value);
1410 * g_file_info_get_edit_name:
1411 * @info: a #GFileInfo.
1413 * Gets the edit name for a file.
1415 * Returns: a string containing the edit name.
1418 g_file_info_get_edit_name (GFileInfo *info)
1420 static guint32 attr = 0;
1421 GFileAttributeValue *value;
1423 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1426 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1428 value = g_file_info_find_value (info, attr);
1429 return _g_file_attribute_value_get_string (value);
1433 * g_file_info_get_icon:
1434 * @info: a #GFileInfo.
1436 * Gets the icon for a file.
1438 * Returns: #GIcon for the given @info.
1441 g_file_info_get_icon (GFileInfo *info)
1443 static guint32 attr = 0;
1444 GFileAttributeValue *value;
1447 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1450 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1452 value = g_file_info_find_value (info, attr);
1453 obj = _g_file_attribute_value_get_object (value);
1454 if (G_IS_ICON (obj))
1455 return G_ICON (obj);
1460 * g_file_info_get_content_type:
1461 * @info: a #GFileInfo.
1463 * Gets the file's content type.
1465 * Returns: a string containing the file's content type.
1468 g_file_info_get_content_type (GFileInfo *info)
1470 static guint32 attr = 0;
1471 GFileAttributeValue *value;
1473 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1476 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1478 value = g_file_info_find_value (info, attr);
1479 return _g_file_attribute_value_get_string (value);
1483 * g_file_info_get_size:
1484 * @info: a #GFileInfo.
1486 * Gets the file's size.
1488 * Returns: a #goffset containing the file's size.
1491 g_file_info_get_size (GFileInfo *info)
1493 static guint32 attr = 0;
1494 GFileAttributeValue *value;
1496 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1499 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1501 value = g_file_info_find_value (info, attr);
1502 return (goffset) _g_file_attribute_value_get_uint64 (value);
1506 * g_file_info_get_modification_time:
1507 * @info: a #GFileInfo.
1508 * @result: a #GTimeVal.
1510 * Gets the modification time of the current @info and sets it
1514 g_file_info_get_modification_time (GFileInfo *info,
1517 static guint32 attr_mtime = 0, attr_mtime_usec;
1518 GFileAttributeValue *value;
1520 g_return_if_fail (G_IS_FILE_INFO (info));
1521 g_return_if_fail (result != NULL);
1523 if (attr_mtime == 0)
1525 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1526 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1529 value = g_file_info_find_value (info, attr_mtime);
1530 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1531 value = g_file_info_find_value (info, attr_mtime_usec);
1532 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1536 * g_file_info_get_symlink_target:
1537 * @info: a #GFileInfo.
1539 * Gets the symlink target for a given #GFileInfo.
1541 * Returns: a string containing the symlink target.
1544 g_file_info_get_symlink_target (GFileInfo *info)
1546 static guint32 attr = 0;
1547 GFileAttributeValue *value;
1549 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1552 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1554 value = g_file_info_find_value (info, attr);
1555 return _g_file_attribute_value_get_byte_string (value);
1559 * g_file_info_get_etag:
1560 * @info: a #GFileInfo.
1562 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1563 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1565 * Returns: a string containing the value of the "etag:value" attribute.
1568 g_file_info_get_etag (GFileInfo *info)
1570 static guint32 attr = 0;
1571 GFileAttributeValue *value;
1573 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1576 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1578 value = g_file_info_find_value (info, attr);
1579 return _g_file_attribute_value_get_string (value);
1583 * g_file_info_get_sort_order:
1584 * @info: a #GFileInfo.
1586 * Gets the value of the sort_order attribute from the #GFileInfo.
1587 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1589 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1592 g_file_info_get_sort_order (GFileInfo *info)
1594 static guint32 attr = 0;
1595 GFileAttributeValue *value;
1597 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1600 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1602 value = g_file_info_find_value (info, attr);
1603 return _g_file_attribute_value_get_int32 (value);
1606 /* Helper setters: */
1608 * g_file_info_set_file_type:
1609 * @info: a #GFileInfo.
1610 * @type: a #GFileType.
1612 * Sets the file type in a #GFileInfo to @type.
1613 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1616 g_file_info_set_file_type (GFileInfo *info,
1619 static guint32 attr = 0;
1620 GFileAttributeValue *value;
1622 g_return_if_fail (G_IS_FILE_INFO (info));
1625 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1627 value = g_file_info_create_value (info, attr);
1629 _g_file_attribute_value_set_uint32 (value, type);
1633 * g_file_info_set_is_hidden:
1634 * @info: a #GFileInfo.
1635 * @is_hidden: a #gboolean.
1637 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1638 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1641 g_file_info_set_is_hidden (GFileInfo *info,
1644 static guint32 attr = 0;
1645 GFileAttributeValue *value;
1647 g_return_if_fail (G_IS_FILE_INFO (info));
1650 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1652 value = g_file_info_create_value (info, attr);
1654 _g_file_attribute_value_set_boolean (value, is_hidden);
1658 * g_file_info_set_is_symlink:
1659 * @info: a #GFileInfo.
1660 * @is_symlink: a #gboolean.
1662 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1663 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1666 g_file_info_set_is_symlink (GFileInfo *info,
1667 gboolean is_symlink)
1669 static guint32 attr = 0;
1670 GFileAttributeValue *value;
1672 g_return_if_fail (G_IS_FILE_INFO (info));
1675 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1677 value = g_file_info_create_value (info, attr);
1679 _g_file_attribute_value_set_boolean (value, is_symlink);
1683 * g_file_info_set_name:
1684 * @info: a #GFileInfo.
1685 * @name: a string containing a name.
1687 * Sets the name attribute for the current #GFileInfo.
1688 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1691 g_file_info_set_name (GFileInfo *info,
1694 static guint32 attr = 0;
1695 GFileAttributeValue *value;
1697 g_return_if_fail (G_IS_FILE_INFO (info));
1698 g_return_if_fail (name != NULL);
1701 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1703 value = g_file_info_create_value (info, attr);
1705 _g_file_attribute_value_set_byte_string (value, name);
1709 * g_file_info_set_display_name:
1710 * @info: a #GFileInfo.
1711 * @display_name: a string containing a display name.
1713 * Sets the display name for the current #GFileInfo.
1714 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1717 g_file_info_set_display_name (GFileInfo *info,
1718 const char *display_name)
1720 static guint32 attr = 0;
1721 GFileAttributeValue *value;
1723 g_return_if_fail (G_IS_FILE_INFO (info));
1724 g_return_if_fail (display_name != NULL);
1727 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1729 value = g_file_info_create_value (info, attr);
1731 _g_file_attribute_value_set_string (value, display_name);
1735 * g_file_info_set_edit_name:
1736 * @info: a #GFileInfo.
1737 * @edit_name: a string containing an edit name.
1739 * Sets the edit name for the current file.
1740 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1743 g_file_info_set_edit_name (GFileInfo *info,
1744 const char *edit_name)
1746 static guint32 attr = 0;
1747 GFileAttributeValue *value;
1749 g_return_if_fail (G_IS_FILE_INFO (info));
1750 g_return_if_fail (edit_name != NULL);
1753 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1755 value = g_file_info_create_value (info, attr);
1757 _g_file_attribute_value_set_string (value, edit_name);
1761 * g_file_info_set_icon:
1762 * @info: a #GFileInfo.
1765 * Sets the icon for a given #GFileInfo.
1766 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1769 g_file_info_set_icon (GFileInfo *info,
1772 static guint32 attr = 0;
1773 GFileAttributeValue *value;
1775 g_return_if_fail (G_IS_FILE_INFO (info));
1776 g_return_if_fail (G_IS_ICON (icon));
1779 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1781 value = g_file_info_create_value (info, attr);
1783 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1787 * g_file_info_set_content_type:
1788 * @info: a #GFileInfo.
1789 * @content_type: a content type. See #GContentType.
1791 * Sets the content type attribute for a given #GFileInfo.
1792 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1795 g_file_info_set_content_type (GFileInfo *info,
1796 const char *content_type)
1798 static guint32 attr = 0;
1799 GFileAttributeValue *value;
1801 g_return_if_fail (G_IS_FILE_INFO (info));
1802 g_return_if_fail (content_type != NULL);
1805 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1807 value = g_file_info_create_value (info, attr);
1809 _g_file_attribute_value_set_string (value, content_type);
1813 * g_file_info_set_size:
1814 * @info: a #GFileInfo.
1815 * @size: a #goffset containing the file's size.
1817 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1818 * to the given size.
1821 g_file_info_set_size (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_SIZE);
1832 value = g_file_info_create_value (info, attr);
1834 _g_file_attribute_value_set_uint64 (value, size);
1838 * g_file_info_set_modification_time
1839 * @info: a #GFileInfo.
1840 * @mtime: a #GTimeVal.
1842 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
1843 * info to the given time value.
1846 g_file_info_set_modification_time (GFileInfo *info,
1849 static guint32 attr_mtime = 0, attr_mtime_usec;
1850 GFileAttributeValue *value;
1852 g_return_if_fail (G_IS_FILE_INFO (info));
1853 g_return_if_fail (mtime != NULL);
1855 if (attr_mtime == 0)
1857 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1858 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1861 value = g_file_info_create_value (info, attr_mtime);
1863 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
1864 value = g_file_info_create_value (info, attr_mtime_usec);
1866 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
1870 * g_file_info_set_symlink_target:
1871 * @info: a #GFileInfo.
1872 * @symlink_target: a static string containing a path to a symlink target.
1874 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
1875 * to the given symlink target.
1878 g_file_info_set_symlink_target (GFileInfo *info,
1879 const char *symlink_target)
1881 static guint32 attr = 0;
1882 GFileAttributeValue *value;
1884 g_return_if_fail (G_IS_FILE_INFO (info));
1885 g_return_if_fail (symlink_target != NULL);
1888 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1890 value = g_file_info_create_value (info, attr);
1892 _g_file_attribute_value_set_byte_string (value, symlink_target);
1896 * g_file_info_set_sort_order:
1897 * @info: a #GFileInfo.
1898 * @sort_order: a sort order integer.
1900 * Sets the sort order attribute in the file info structure. See
1901 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1904 g_file_info_set_sort_order (GFileInfo *info,
1907 static guint32 attr = 0;
1908 GFileAttributeValue *value;
1910 g_return_if_fail (G_IS_FILE_INFO (info));
1913 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1915 value = g_file_info_create_value (info, attr);
1917 _g_file_attribute_value_set_int32 (value, sort_order);
1921 #define ON_STACK_MATCHERS 5
1928 struct _GFileAttributeMatcher {
1930 SubMatcher sub_matchers[ON_STACK_MATCHERS];
1931 GArray *more_sub_matchers;
1934 guint32 iterator_ns;
1940 matcher_add (GFileAttributeMatcher *matcher,
1944 SubMatcher *sub_matchers;
1948 for (i = 0; i < ON_STACK_MATCHERS; i++)
1950 /* First empty spot, not found, use this */
1951 if (matcher->sub_matchers[i].id == 0)
1953 matcher->sub_matchers[i].id = id;
1954 matcher->sub_matchers[i].mask = mask;
1959 if (matcher->sub_matchers[i].id == id &&
1960 matcher->sub_matchers[i].mask == mask)
1964 if (matcher->more_sub_matchers == NULL)
1965 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
1967 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
1968 for (i = 0; i < matcher->more_sub_matchers->len; i++)
1971 if (sub_matchers[i].id == id &&
1972 sub_matchers[i].mask == mask)
1979 g_array_append_val (matcher->more_sub_matchers, s);
1983 * g_file_attribute_matcher_new:
1984 * @attributes: an attribute string to match.
1986 * Creates a new file attribute matcher, which matches attributes
1987 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
1988 * counted structures, and are created with a reference count of 1. If
1989 * the number of references falls to 0, the #GFileAttributeMatcher is
1990 * automatically destroyed.
1992 * The @attribute string should be formatted with specific keys separated
1993 * from namespaces with a double colon. Several "namespace::key" strings may be
1994 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
1995 * The wildcard "*" may be used to match all keys and namespaces, or
1996 * "namespace::*" will match all keys in a given namespace.
1998 * Examples of strings to use:
2000 * <title>File Attribute Matcher strings and results</title>
2001 * <tgroup cols='2' align='left'><thead>
2002 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
2004 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
2005 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
2006 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
2007 * all keys in the unix namespace.</entry></row>
2011 * Returns: a #GFileAttributeMatcher.
2013 GFileAttributeMatcher *
2014 g_file_attribute_matcher_new (const char *attributes)
2019 GFileAttributeMatcher *matcher;
2021 if (attributes == NULL || *attributes == '\0')
2024 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2027 split = g_strsplit (attributes, ",", -1);
2029 for (i = 0; split[i] != NULL; i++)
2031 if (strcmp (split[i], "*") == 0)
2032 matcher->all = TRUE;
2037 colon = strstr (split[i], "::");
2038 if (colon != NULL &&
2043 id = lookup_attribute (split[i]);
2051 id = lookup_namespace (split[i]) << NS_POS;
2052 mask = NS_MASK << NS_POS;
2055 matcher_add (matcher, id, mask);
2065 * g_file_attribute_matcher_ref:
2066 * @matcher: a #GFileAttributeMatcher.
2068 * References a file attribute matcher.
2070 * Returns: a #GFileAttributeMatcher.
2072 GFileAttributeMatcher *
2073 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2077 g_return_val_if_fail (matcher->ref > 0, NULL);
2078 g_atomic_int_inc (&matcher->ref);
2084 * g_file_attribute_matcher_unref:
2085 * @matcher: a #GFileAttributeMatcher.
2087 * Unreferences @matcher. If the reference count falls below 1,
2088 * the @matcher is automatically freed.
2092 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2096 g_return_if_fail (matcher->ref > 0);
2098 if (g_atomic_int_dec_and_test (&matcher->ref))
2100 if (matcher->more_sub_matchers)
2101 g_array_free (matcher->more_sub_matchers, TRUE);
2109 * g_file_attribute_matcher_matches_only:
2110 * @matcher: a #GFileAttributeMatcher.
2111 * @attribute: a file attribute key.
2113 * Checks if a attribute matcher only matches a given attribute. Always
2114 * returns %FALSE if "*" was used when creating the matcher.
2116 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2119 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2120 const char *attribute)
2124 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2126 if (matcher == NULL ||
2130 id = lookup_attribute (attribute);
2132 if (matcher->sub_matchers[0].id != 0 &&
2133 matcher->sub_matchers[1].id == 0 &&
2134 matcher->sub_matchers[0].mask == 0xffffffff &&
2135 matcher->sub_matchers[0].id == id)
2142 matcher_matches_id (GFileAttributeMatcher *matcher,
2145 SubMatcher *sub_matchers;
2148 for (i = 0; i < ON_STACK_MATCHERS; i++)
2150 if (matcher->sub_matchers[i].id == 0)
2153 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2157 if (matcher->more_sub_matchers)
2159 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2160 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2162 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2171 g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2174 /* We return a NULL matcher for an empty match string, so handle this */
2175 if (matcher == NULL)
2181 return matcher_matches_id (matcher, id);
2185 * g_file_attribute_matcher_matches:
2186 * @matcher: a #GFileAttributeMatcher.
2187 * @attribute: a file attribute key.
2189 * Checks if an attribute will be matched by an attribute matcher. If
2190 * the matcher was created with the "*" matching string, this function
2191 * will always return %TRUE.
2193 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2196 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2197 const char *attribute)
2199 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2201 /* We return a NULL matcher for an empty match string, so handle this */
2202 if (matcher == NULL)
2208 return matcher_matches_id (matcher, lookup_attribute (attribute));
2211 /* return TRUE -> all */
2213 * g_file_attribute_matcher_enumerate_namespace:
2214 * @matcher: a #GFileAttributeMatcher.
2215 * @ns: a string containing a file attribute namespace.
2217 * Checks if the matcher will match all of the keys in a given namespace.
2218 * This will always return %TRUE if a wildcard character is in use (e.g. if
2219 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2220 * using "*" and namespace is anything.)
2222 * TODO: this is awkwardly worded.
2224 * Returns: %TRUE if the matcher matches all of the entries
2225 * in the given @ns, %FALSE otherwise.
2228 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2231 SubMatcher *sub_matchers;
2235 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2237 /* We return a NULL matcher for an empty match string, so handle this */
2238 if (matcher == NULL)
2244 ns_id = lookup_namespace (ns) << NS_POS;
2246 for (i = 0; i < ON_STACK_MATCHERS; i++)
2248 if (matcher->sub_matchers[i].id == ns_id)
2252 if (matcher->more_sub_matchers)
2254 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2255 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2257 if (sub_matchers[i].id == ns_id)
2262 matcher->iterator_ns = ns_id;
2263 matcher->iterator_pos = 0;
2269 * g_file_attribute_matcher_enumerate_next:
2270 * @matcher: a #GFileAttributeMatcher.
2272 * Gets the next matched attribute from a #GFileAttributeMatcher.
2274 * Returns: a string containing the next attribute or %NULL if
2275 * no more attribute exist.
2278 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2281 SubMatcher *sub_matcher;
2283 /* We return a NULL matcher for an empty match string, so handle this */
2284 if (matcher == NULL)
2289 i = matcher->iterator_pos++;
2291 if (i < ON_STACK_MATCHERS)
2293 if (matcher->sub_matchers[i].id == 0)
2296 sub_matcher = &matcher->sub_matchers[i];
2300 if (matcher->more_sub_matchers == NULL)
2303 i -= ON_STACK_MATCHERS;
2304 if (i < matcher->more_sub_matchers->len)
2305 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2310 if (sub_matcher->mask == 0xffffffff &&
2311 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2312 return get_attribute_for_id (sub_matcher->id);
2316 #define __G_FILE_INFO_C__
2317 #include "gioaliasdef.c"