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 ensure_attribute_hash (void)
146 if (attribute_hash != NULL)
149 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
150 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
154 lookup_namespace (const char *namespace)
159 G_LOCK (attribute_hash);
161 ensure_attribute_hash ();
163 ns_info = _lookup_namespace (namespace);
168 G_UNLOCK (attribute_hash);
174 get_attribute_for_id (int attribute)
177 G_LOCK (attribute_hash);
178 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
179 G_UNLOCK (attribute_hash);
184 lookup_attribute (const char *attribute)
191 G_LOCK (attribute_hash);
192 ensure_attribute_hash ();
194 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
198 G_UNLOCK (attribute_hash);
202 colon = strstr (attribute, "::");
204 ns = g_strndup (attribute, colon - attribute);
208 ns_info = _lookup_namespace (ns);
211 id = ++ns_info->attribute_id_counter;
212 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
213 attributes[ns_info->id][id] = g_strdup (attribute);
215 attr_id = MAKE_ATTR_ID (ns_info->id, id);
217 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
219 G_UNLOCK (attribute_hash);
225 g_file_info_finalize (GObject *object)
229 GFileAttribute *attrs;
231 info = G_FILE_INFO (object);
233 attrs = (GFileAttribute *)info->attributes->data;
234 for (i = 0; i < info->attributes->len; i++)
235 _g_file_attribute_value_clear (&attrs[i].value);
236 g_array_free (info->attributes, TRUE);
238 if (info->mask != NO_ATTRIBUTE_MASK)
239 g_file_attribute_matcher_unref (info->mask);
241 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
245 g_file_info_class_init (GFileInfoClass *klass)
247 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
249 gobject_class->finalize = g_file_info_finalize;
253 g_file_info_init (GFileInfo *info)
255 info->mask = NO_ATTRIBUTE_MASK;
256 info->attributes = g_array_new (FALSE, FALSE,
257 sizeof (GFileAttribute));
263 * Creates a new file info structure.
265 * Returns: a #GFileInfo.
268 g_file_info_new (void)
270 return g_object_new (G_TYPE_FILE_INFO, NULL);
274 * g_file_info_copy_into:
275 * @src_info: source to copy attributes from.
276 * @dest_info: destination to copy attributes to.
278 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
281 g_file_info_copy_into (GFileInfo *src_info,
282 GFileInfo *dest_info)
284 GFileAttribute *source, *dest;
287 g_return_if_fail (G_IS_FILE_INFO (src_info));
288 g_return_if_fail (G_IS_FILE_INFO (dest_info));
290 dest = (GFileAttribute *)dest_info->attributes->data;
291 for (i = 0; i < dest_info->attributes->len; i++)
292 _g_file_attribute_value_clear (&dest[i].value);
294 g_array_set_size (dest_info->attributes,
295 src_info->attributes->len);
297 source = (GFileAttribute *)src_info->attributes->data;
298 dest = (GFileAttribute *)dest_info->attributes->data;
300 for (i = 0; i < src_info->attributes->len; i++)
302 dest[i].attribute = source[i].attribute;
303 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
304 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
307 if (dest_info->mask != NO_ATTRIBUTE_MASK)
308 g_file_attribute_matcher_unref (dest_info->mask);
310 if (src_info->mask == NO_ATTRIBUTE_MASK)
311 dest_info->mask = NO_ATTRIBUTE_MASK;
313 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
318 * @other: a #GFileInfo.
320 * Duplicates a file info structure.
322 * Returns: a duplicate #GFileInfo of @other.
325 g_file_info_dup (GFileInfo *other)
329 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
331 new = g_file_info_new ();
332 g_file_info_copy_into (other, new);
337 * g_file_info_set_attribute_mask:
338 * @info: a #GFileInfo.
339 * @mask: a #GFileAttributeMatcher.
341 * Sets @mask on @info to match specific attribute types.
344 g_file_info_set_attribute_mask (GFileInfo *info,
345 GFileAttributeMatcher *mask)
347 GFileAttribute *attr;
350 g_return_if_fail (G_IS_FILE_INFO (info));
352 if (mask != info->mask)
354 if (info->mask != NO_ATTRIBUTE_MASK)
355 g_file_attribute_matcher_unref (info->mask);
356 info->mask = g_file_attribute_matcher_ref (mask);
358 /* Remove non-matching attributes */
359 for (i = 0; i < info->attributes->len; i++)
361 attr = &g_array_index (info->attributes, GFileAttribute, i);
362 if (!g_file_attribute_matcher_matches_id (mask,
365 _g_file_attribute_value_clear (&attr->value);
366 g_array_remove_index (info->attributes, i);
374 * g_file_info_unset_attribute_mask:
377 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
381 g_file_info_unset_attribute_mask (GFileInfo *info)
383 g_return_if_fail (G_IS_FILE_INFO (info));
385 if (info->mask != NO_ATTRIBUTE_MASK)
386 g_file_attribute_matcher_unref (info->mask);
387 info->mask = NO_ATTRIBUTE_MASK;
391 * g_file_info_clear_status:
392 * @info: a #GFileInfo.
394 * Clears the status information from @info.
397 g_file_info_clear_status (GFileInfo *info)
399 GFileAttribute *attrs;
402 g_return_if_fail (G_IS_FILE_INFO (info));
404 attrs = (GFileAttribute *)info->attributes->data;
405 for (i = 0; i < info->attributes->len; i++)
406 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
410 g_file_info_find_place (GFileInfo *info,
414 GFileAttribute *attrs;
415 /* Binary search for the place where attribute would be, if it's
419 max = info->attributes->len;
421 attrs = (GFileAttribute *)info->attributes->data;
425 med = min + (max - min) / 2;
426 if (attrs[med].attribute == attribute)
431 else if (attrs[med].attribute < attribute)
433 else /* attrs[med].attribute > attribute */
440 static GFileAttributeValue *
441 g_file_info_find_value (GFileInfo *info,
444 GFileAttribute *attrs;
447 i = g_file_info_find_place (info, attr_id);
448 attrs = (GFileAttribute *)info->attributes->data;
449 if (i < info->attributes->len &&
450 attrs[i].attribute == attr_id)
451 return &attrs[i].value;
456 static GFileAttributeValue *
457 g_file_info_find_value_by_name (GFileInfo *info,
458 const char *attribute)
462 attr_id = lookup_attribute (attribute);
463 return g_file_info_find_value (info, attr_id);
467 * g_file_info_has_attribute:
468 * @info: a #GFileInfo.
469 * @attribute: a file attribute key.
471 * Checks if a file info structure has an attribute named @attribute.
473 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
477 g_file_info_has_attribute (GFileInfo *info,
478 const char *attribute)
480 GFileAttributeValue *value;
482 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
483 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
485 value = g_file_info_find_value_by_name (info, attribute);
486 return value != NULL;
490 * g_file_info_has_namespace:
491 * @info: a #GFileInfo.
492 * @name_space: a file attribute namespace.
494 * Checks if a file info structure has an attribute in the
495 * specified @name_space.
497 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
503 g_file_info_has_namespace (GFileInfo *info,
504 const char *name_space)
506 GFileAttribute *attrs;
510 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
511 g_return_val_if_fail (name_space != NULL, FALSE);
513 ns_id = lookup_namespace (name_space);
515 attrs = (GFileAttribute *)info->attributes->data;
516 for (i = 0; i < info->attributes->len; i++)
518 if (GET_NS (attrs[i].attribute) == ns_id)
526 * g_file_info_list_attributes:
527 * @info: a #GFileInfo.
528 * @name_space: a file attribute key's namespace.
530 * Lists the file info structure's attributes.
532 * Returns: a null-terminated array of strings of all of the
533 * possible attribute types for the given @name_space, or
537 g_file_info_list_attributes (GFileInfo *info,
538 const char *name_space)
541 GFileAttribute *attrs;
543 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
546 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
548 names = g_ptr_array_new ();
549 attrs = (GFileAttribute *)info->attributes->data;
550 for (i = 0; i < info->attributes->len; i++)
552 attribute = attrs[i].attribute;
553 if (ns_id == 0 || GET_NS (attribute) == ns_id)
554 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
558 g_ptr_array_add (names, NULL);
560 return (char **)g_ptr_array_free (names, FALSE);
564 * g_file_info_get_attribute_type:
565 * @info: a #GFileInfo.
566 * @attribute: a file attribute key.
568 * Gets the attribute type for an attribute key.
570 * Returns: a #GFileAttributeType for the given @attribute, or
571 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
574 g_file_info_get_attribute_type (GFileInfo *info,
575 const char *attribute)
577 GFileAttributeValue *value;
579 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
580 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
582 value = g_file_info_find_value_by_name (info, attribute);
586 return G_FILE_ATTRIBUTE_TYPE_INVALID;
590 * g_file_info_remove_attribute:
591 * @info: a #GFileInfo.
592 * @attribute: a file attribute key.
594 * Removes all cases of @attribute from @info if it exists.
597 g_file_info_remove_attribute (GFileInfo *info,
598 const char *attribute)
601 GFileAttribute *attrs;
604 g_return_if_fail (G_IS_FILE_INFO (info));
605 g_return_if_fail (attribute != NULL && *attribute != '\0');
607 attr_id = lookup_attribute (attribute);
609 i = g_file_info_find_place (info, attr_id);
610 attrs = (GFileAttribute *)info->attributes->data;
611 if (i < info->attributes->len &&
612 attrs[i].attribute == attr_id)
614 _g_file_attribute_value_clear (&attrs[i].value);
615 g_array_remove_index (info->attributes, i);
620 * g_file_info_get_attribute_data:
621 * @info: a #GFileInfo
622 * @attribute: a file attribute key
623 * @type: return location for the attribute type, or %NULL
624 * @value_pp: return location for the attribute value, or %NULL
625 * @status: return location for the attribute status, or %NULL
627 * Gets the attribute type, value and status for an attribute key.
629 * Returns: %TRUE if @info has an attribute named @attribute,
633 g_file_info_get_attribute_data (GFileInfo *info,
634 const char *attribute,
635 GFileAttributeType *type,
637 GFileAttributeStatus *status)
639 GFileAttributeValue *value;
641 value = g_file_info_find_value_by_name (info, attribute);
646 *status = value->status;
652 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
658 * g_file_info_get_attribute_status:
659 * @info: a #GFileInfo
660 * @attribute: a file attribute key
662 * Gets the attribute status for an attribute key.
664 * Returns: a #GFileAttributeStatus for the given @attribute, or
665 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
669 g_file_info_get_attribute_status (GFileInfo *info,
670 const char *attribute)
672 GFileAttributeValue *val;
674 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
675 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
677 val = g_file_info_find_value_by_name (info, attribute);
681 return G_FILE_ATTRIBUTE_STATUS_UNSET;
685 * g_file_info_set_attribute_status:
686 * @info: a #GFileInfo
687 * @attribute: a file attribute key
688 * @status: a #GFileAttributeStatus
690 * Sets the attribute status for an attribute key. This is only
691 * needed by external code that implement g_file_set_attributes_from_info()
692 * or similar functions.
694 * The attribute must exist in @info for this to work. Otherwise %FALSE
695 * is returned and @info is unchanged.
697 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
702 g_file_info_set_attribute_status (GFileInfo *info,
703 const char *attribute,
704 GFileAttributeStatus status)
706 GFileAttributeValue *val;
708 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
709 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
711 val = g_file_info_find_value_by_name (info, attribute);
714 val->status = status;
721 GFileAttributeValue *
722 _g_file_info_get_attribute_value (GFileInfo *info,
723 const char *attribute)
726 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
727 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
729 return g_file_info_find_value_by_name (info, attribute);
733 * g_file_info_get_attribute_as_string:
734 * @info: a #GFileInfo.
735 * @attribute: a file attribute key.
737 * Gets the value of a attribute, formated as a string.
738 * This escapes things as needed to make the string valid
741 * Returns: a UTF-8 string associated with the given @attribute.
742 * When you're done with the string it must be freed with g_free().
745 g_file_info_get_attribute_as_string (GFileInfo *info,
746 const char *attribute)
748 GFileAttributeValue *val;
749 val = _g_file_info_get_attribute_value (info, attribute);
751 return _g_file_attribute_value_as_string (val);
757 * g_file_info_get_attribute_object:
758 * @info: a #GFileInfo.
759 * @attribute: a file attribute key.
761 * Gets the value of a #GObject attribute. If the attribute does
762 * not contain a #GObject, %NULL will be returned.
764 * Returns: a #GObject associated with the given @attribute, or
768 g_file_info_get_attribute_object (GFileInfo *info,
769 const char *attribute)
771 GFileAttributeValue *value;
773 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
774 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
776 value = g_file_info_find_value_by_name (info, attribute);
777 return _g_file_attribute_value_get_object (value);
781 * g_file_info_get_attribute_string:
782 * @info: a #GFileInfo.
783 * @attribute: a file attribute key.
785 * Gets the value of a string attribute. If the attribute does
786 * not contain a string, %NULL will be returned.
788 * Returns: the contents of the @attribute value as a string, or
792 g_file_info_get_attribute_string (GFileInfo *info,
793 const char *attribute)
795 GFileAttributeValue *value;
797 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
798 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
800 value = g_file_info_find_value_by_name (info, attribute);
801 return _g_file_attribute_value_get_string (value);
805 * g_file_info_get_attribute_byte_string:
806 * @info: a #GFileInfo.
807 * @attribute: a file attribute key.
809 * Gets the value of a byte string attribute. If the attribute does
810 * not contain a byte string, %NULL will be returned.
812 * Returns: the contents of the @attribute value as a byte string, or
816 g_file_info_get_attribute_byte_string (GFileInfo *info,
817 const char *attribute)
819 GFileAttributeValue *value;
821 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
822 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
824 value = g_file_info_find_value_by_name (info, attribute);
825 return _g_file_attribute_value_get_byte_string (value);
829 * g_file_info_get_attribute_stringv:
830 * @info: a #GFileInfo.
831 * @attribute: a file attribute key.
833 * Gets the value of a stringv attribute. If the attribute does
834 * not contain a stringv, %NULL will be returned.
836 * Returns: the contents of the @attribute value as a stringv, or
837 * %NULL otherwise. Do not free.
840 g_file_info_get_attribute_stringv (GFileInfo *info,
841 const char *attribute)
843 GFileAttributeValue *value;
845 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
846 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
848 value = g_file_info_find_value_by_name (info, attribute);
849 return _g_file_attribute_value_get_stringv (value);
853 * g_file_info_get_attribute_boolean:
854 * @info: a #GFileInfo.
855 * @attribute: a file attribute key.
857 * Gets the value of a boolean attribute. If the attribute does not
858 * contain a boolean value, %FALSE will be returned.
860 * Returns: the boolean value contained within the attribute.
863 g_file_info_get_attribute_boolean (GFileInfo *info,
864 const char *attribute)
866 GFileAttributeValue *value;
868 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
869 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
871 value = g_file_info_find_value_by_name (info, attribute);
872 return _g_file_attribute_value_get_boolean (value);
876 * g_file_info_get_attribute_uint32:
877 * @info: a #GFileInfo.
878 * @attribute: a file attribute key.
880 * Gets an unsigned 32-bit integer contained within the attribute. If the
881 * attribute does not contain an unsigned 32-bit integer, or is invalid,
882 * 0 will be returned.
884 * Returns: an unsigned 32-bit integer from the attribute.
887 g_file_info_get_attribute_uint32 (GFileInfo *info,
888 const char *attribute)
890 GFileAttributeValue *value;
892 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
893 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
895 value = g_file_info_find_value_by_name (info, attribute);
896 return _g_file_attribute_value_get_uint32 (value);
900 * g_file_info_get_attribute_int32:
901 * @info: a #GFileInfo.
902 * @attribute: a file attribute key.
904 * Gets a signed 32-bit integer contained within the attribute. If the
905 * attribute does not contain a signed 32-bit integer, or is invalid,
906 * 0 will be returned.
908 * Returns: a signed 32-bit integer from the attribute.
911 g_file_info_get_attribute_int32 (GFileInfo *info,
912 const char *attribute)
914 GFileAttributeValue *value;
916 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
917 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
919 value = g_file_info_find_value_by_name (info, attribute);
920 return _g_file_attribute_value_get_int32 (value);
924 * g_file_info_get_attribute_uint64:
925 * @info: a #GFileInfo.
926 * @attribute: a file attribute key.
928 * Gets a unsigned 64-bit integer contained within the attribute. If the
929 * attribute does not contain an unsigned 64-bit integer, or is invalid,
930 * 0 will be returned.
932 * Returns: a unsigned 64-bit integer from the attribute.
935 g_file_info_get_attribute_uint64 (GFileInfo *info,
936 const char *attribute)
938 GFileAttributeValue *value;
940 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
941 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
943 value = g_file_info_find_value_by_name (info, attribute);
944 return _g_file_attribute_value_get_uint64 (value);
948 * g_file_info_get_attribute_int64:
949 * @info: a #GFileInfo.
950 * @attribute: a file attribute key.
952 * Gets a signed 64-bit integer contained within the attribute. If the
953 * attribute does not contain an signed 64-bit integer, or is invalid,
954 * 0 will be returned.
956 * Returns: a signed 64-bit integer from the attribute.
959 g_file_info_get_attribute_int64 (GFileInfo *info,
960 const char *attribute)
962 GFileAttributeValue *value;
964 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
965 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
967 value = g_file_info_find_value_by_name (info, attribute);
968 return _g_file_attribute_value_get_int64 (value);
971 static GFileAttributeValue *
972 g_file_info_create_value (GFileInfo *info,
975 GFileAttribute *attrs;
978 if (info->mask != NO_ATTRIBUTE_MASK &&
979 !g_file_attribute_matcher_matches_id (info->mask, attr_id))
982 i = g_file_info_find_place (info, attr_id);
984 attrs = (GFileAttribute *)info->attributes->data;
985 if (i < info->attributes->len &&
986 attrs[i].attribute == attr_id)
987 return &attrs[i].value;
990 GFileAttribute attr = { 0 };
991 attr.attribute = attr_id;
992 g_array_insert_val (info->attributes, i, attr);
994 attrs = (GFileAttribute *)info->attributes->data;
995 return &attrs[i].value;
999 static GFileAttributeValue *
1000 g_file_info_create_value_by_name (GFileInfo *info,
1001 const char *attribute)
1005 attr_id = lookup_attribute (attribute);
1007 return g_file_info_create_value (info, attr_id);
1011 * g_file_info_set_attribute:
1012 * @info: a #GFileInfo.
1013 * @attribute: a file attribute key.
1014 * @type: a #GFileAttributeType
1015 * @value_p: pointer to the value
1017 * Sets the @attribute to contain the given value, if possible.
1020 g_file_info_set_attribute (GFileInfo *info,
1021 const char *attribute,
1022 GFileAttributeType type,
1025 GFileAttributeValue *value;
1027 g_return_if_fail (G_IS_FILE_INFO (info));
1028 g_return_if_fail (attribute != NULL && *attribute != '\0');
1030 value = g_file_info_create_value_by_name (info, attribute);
1033 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1037 * g_file_info_set_attribute_object:
1038 * @info: a #GFileInfo.
1039 * @attribute: a file attribute key.
1040 * @attr_value: a #GObject.
1042 * Sets the @attribute to contain the given @attr_value,
1046 g_file_info_set_attribute_object (GFileInfo *info,
1047 const char *attribute,
1048 GObject *attr_value)
1050 GFileAttributeValue *value;
1052 g_return_if_fail (G_IS_FILE_INFO (info));
1053 g_return_if_fail (attribute != NULL && *attribute != '\0');
1054 g_return_if_fail (G_IS_OBJECT (attr_value));
1056 value = g_file_info_create_value_by_name (info, attribute);
1058 _g_file_attribute_value_set_object (value, attr_value);
1062 * g_file_info_set_attribute_stringv:
1063 * @info: a #GFileInfo.
1064 * @attribute: a file attribute key.
1065 * @attr_value: a %NULL terminated string array
1067 * Sets the @attribute to contain the given @attr_value,
1073 g_file_info_set_attribute_stringv (GFileInfo *info,
1074 const char *attribute,
1077 GFileAttributeValue *value;
1079 g_return_if_fail (G_IS_FILE_INFO (info));
1080 g_return_if_fail (attribute != NULL && *attribute != '\0');
1081 g_return_if_fail (attr_value != NULL);
1083 value = g_file_info_create_value_by_name (info, attribute);
1085 _g_file_attribute_value_set_stringv (value, attr_value);
1089 * g_file_info_set_attribute_string:
1090 * @info: a #GFileInfo.
1091 * @attribute: a file attribute key.
1092 * @attr_value: a string.
1094 * Sets the @attribute to contain the given @attr_value,
1098 g_file_info_set_attribute_string (GFileInfo *info,
1099 const char *attribute,
1100 const char *attr_value)
1102 GFileAttributeValue *value;
1104 g_return_if_fail (G_IS_FILE_INFO (info));
1105 g_return_if_fail (attribute != NULL && *attribute != '\0');
1106 g_return_if_fail (attr_value != NULL);
1108 value = g_file_info_create_value_by_name (info, attribute);
1110 _g_file_attribute_value_set_string (value, attr_value);
1114 * g_file_info_set_attribute_byte_string:
1115 * @info: a #GFileInfo.
1116 * @attribute: a file attribute key.
1117 * @attr_value: a byte string.
1119 * Sets the @attribute to contain the given @attr_value,
1123 g_file_info_set_attribute_byte_string (GFileInfo *info,
1124 const char *attribute,
1125 const char *attr_value)
1127 GFileAttributeValue *value;
1129 g_return_if_fail (G_IS_FILE_INFO (info));
1130 g_return_if_fail (attribute != NULL && *attribute != '\0');
1131 g_return_if_fail (attr_value != NULL);
1133 value = g_file_info_create_value_by_name (info, attribute);
1135 _g_file_attribute_value_set_byte_string (value, attr_value);
1139 * g_file_info_set_attribute_boolean:
1140 * @info: a #GFileInfo.
1141 * @attribute: a file attribute key.
1142 * @attr_value: a boolean value.
1144 * Sets the @attribute to contain the given @attr_value,
1148 g_file_info_set_attribute_boolean (GFileInfo *info,
1149 const char *attribute,
1150 gboolean attr_value)
1152 GFileAttributeValue *value;
1154 g_return_if_fail (G_IS_FILE_INFO (info));
1155 g_return_if_fail (attribute != NULL && *attribute != '\0');
1157 value = g_file_info_create_value_by_name (info, attribute);
1159 _g_file_attribute_value_set_boolean (value, attr_value);
1163 * g_file_info_set_attribute_uint32:
1164 * @info: a #GFileInfo.
1165 * @attribute: a file attribute key.
1166 * @attr_value: an unsigned 32-bit integer.
1168 * Sets the @attribute to contain the given @attr_value,
1172 g_file_info_set_attribute_uint32 (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_uint32 (value, attr_value);
1188 * g_file_info_set_attribute_int32:
1189 * @info: a #GFileInfo.
1190 * @attribute: a file attribute key.
1191 * @attr_value: a signed 32-bit integer
1193 * Sets the @attribute to contain the given @attr_value,
1197 g_file_info_set_attribute_int32 (GFileInfo *info,
1198 const char *attribute,
1201 GFileAttributeValue *value;
1203 g_return_if_fail (G_IS_FILE_INFO (info));
1204 g_return_if_fail (attribute != NULL && *attribute != '\0');
1206 value = g_file_info_create_value_by_name (info, attribute);
1208 _g_file_attribute_value_set_int32 (value, attr_value);
1212 * g_file_info_set_attribute_uint64:
1213 * @info: a #GFileInfo.
1214 * @attribute: a file attribute key.
1215 * @attr_value: an unsigned 64-bit integer.
1217 * Sets the @attribute to contain the given @attr_value,
1221 g_file_info_set_attribute_uint64 (GFileInfo *info,
1222 const char *attribute,
1225 GFileAttributeValue *value;
1227 g_return_if_fail (G_IS_FILE_INFO (info));
1228 g_return_if_fail (attribute != NULL && *attribute != '\0');
1230 value = g_file_info_create_value_by_name (info, attribute);
1232 _g_file_attribute_value_set_uint64 (value, attr_value);
1236 * g_file_info_set_attribute_int64:
1237 * @info: a #GFileInfo.
1238 * @attribute: attribute name to set.
1239 * @attr_value: int64 value to set attribute to.
1241 * Sets the @attribute to contain the given @attr_value,
1246 g_file_info_set_attribute_int64 (GFileInfo *info,
1247 const char *attribute,
1250 GFileAttributeValue *value;
1252 g_return_if_fail (G_IS_FILE_INFO (info));
1253 g_return_if_fail (attribute != NULL && *attribute != '\0');
1255 value = g_file_info_create_value_by_name (info, attribute);
1257 _g_file_attribute_value_set_int64 (value, attr_value);
1260 /* Helper getters */
1262 * g_file_info_get_file_type:
1263 * @info: a #GFileInfo.
1265 * Gets a file's type (whether it is a regular file, symlink, etc).
1266 * This is different from the file's content type, see g_file_info_get_content_type().
1268 * Returns: a #GFileType for the given file.
1271 g_file_info_get_file_type (GFileInfo *info)
1273 static guint32 attr = 0;
1274 GFileAttributeValue *value;
1276 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1279 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1281 value = g_file_info_find_value (info, attr);
1282 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1286 * g_file_info_get_is_hidden:
1287 * @info: a #GFileInfo.
1289 * Checks if a file is hidden.
1291 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1294 g_file_info_get_is_hidden (GFileInfo *info)
1296 static guint32 attr = 0;
1297 GFileAttributeValue *value;
1299 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1302 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1304 value = g_file_info_find_value (info, attr);
1305 return (GFileType)_g_file_attribute_value_get_boolean (value);
1309 * g_file_info_get_is_backup:
1310 * @info: a #GFileInfo.
1312 * Checks if a file is a backup file.
1314 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1317 g_file_info_get_is_backup (GFileInfo *info)
1319 static guint32 attr = 0;
1320 GFileAttributeValue *value;
1322 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1325 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1327 value = g_file_info_find_value (info, attr);
1328 return (GFileType)_g_file_attribute_value_get_boolean (value);
1332 * g_file_info_get_is_symlink:
1333 * @info: a #GFileInfo.
1335 * Checks if a file is a symlink.
1337 * Returns: %TRUE if the given @info is a symlink.
1340 g_file_info_get_is_symlink (GFileInfo *info)
1342 static guint32 attr = 0;
1343 GFileAttributeValue *value;
1345 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1348 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1350 value = g_file_info_find_value (info, attr);
1351 return (GFileType)_g_file_attribute_value_get_boolean (value);
1355 * g_file_info_get_name:
1356 * @info: a #GFileInfo.
1358 * Gets the name for a file.
1360 * Returns: a string containing the file name.
1363 g_file_info_get_name (GFileInfo *info)
1365 static guint32 attr = 0;
1366 GFileAttributeValue *value;
1368 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1371 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1373 value = g_file_info_find_value (info, attr);
1374 return _g_file_attribute_value_get_byte_string (value);
1378 * g_file_info_get_display_name:
1379 * @info: a #GFileInfo.
1381 * Gets a display name for a file.
1383 * Returns: a string containing the display name.
1386 g_file_info_get_display_name (GFileInfo *info)
1388 static guint32 attr = 0;
1389 GFileAttributeValue *value;
1391 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1394 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1396 value = g_file_info_find_value (info, attr);
1397 return _g_file_attribute_value_get_string (value);
1401 * g_file_info_get_edit_name:
1402 * @info: a #GFileInfo.
1404 * Gets the edit name for a file.
1406 * Returns: a string containing the edit name.
1409 g_file_info_get_edit_name (GFileInfo *info)
1411 static guint32 attr = 0;
1412 GFileAttributeValue *value;
1414 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1417 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1419 value = g_file_info_find_value (info, attr);
1420 return _g_file_attribute_value_get_string (value);
1424 * g_file_info_get_icon:
1425 * @info: a #GFileInfo.
1427 * Gets the icon for a file.
1429 * Returns: #GIcon for the given @info.
1432 g_file_info_get_icon (GFileInfo *info)
1434 static guint32 attr = 0;
1435 GFileAttributeValue *value;
1438 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1441 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1443 value = g_file_info_find_value (info, attr);
1444 obj = _g_file_attribute_value_get_object (value);
1445 if (G_IS_ICON (obj))
1446 return G_ICON (obj);
1451 * g_file_info_get_content_type:
1452 * @info: a #GFileInfo.
1454 * Gets the file's content type.
1456 * Returns: a string containing the file's content type.
1459 g_file_info_get_content_type (GFileInfo *info)
1461 static guint32 attr = 0;
1462 GFileAttributeValue *value;
1464 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1467 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1469 value = g_file_info_find_value (info, attr);
1470 return _g_file_attribute_value_get_string (value);
1474 * g_file_info_get_size:
1475 * @info: a #GFileInfo.
1477 * Gets the file's size.
1479 * Returns: a #goffset containing the file's size.
1482 g_file_info_get_size (GFileInfo *info)
1484 static guint32 attr = 0;
1485 GFileAttributeValue *value;
1487 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1490 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1492 value = g_file_info_find_value (info, attr);
1493 return (goffset) _g_file_attribute_value_get_uint64 (value);
1497 * g_file_info_get_modification_time:
1498 * @info: a #GFileInfo.
1499 * @result: a #GTimeVal.
1501 * Gets the modification time of the current @info and sets it
1505 g_file_info_get_modification_time (GFileInfo *info,
1508 static guint32 attr_mtime = 0, attr_mtime_usec;
1509 GFileAttributeValue *value;
1511 g_return_if_fail (G_IS_FILE_INFO (info));
1512 g_return_if_fail (result != NULL);
1514 if (attr_mtime == 0)
1516 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1517 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1520 value = g_file_info_find_value (info, attr_mtime);
1521 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1522 value = g_file_info_find_value (info, attr_mtime_usec);
1523 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1527 * g_file_info_get_symlink_target:
1528 * @info: a #GFileInfo.
1530 * Gets the symlink target for a given #GFileInfo.
1532 * Returns: a string containing the symlink target.
1535 g_file_info_get_symlink_target (GFileInfo *info)
1537 static guint32 attr = 0;
1538 GFileAttributeValue *value;
1540 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1543 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1545 value = g_file_info_find_value (info, attr);
1546 return _g_file_attribute_value_get_byte_string (value);
1550 * g_file_info_get_etag:
1551 * @info: a #GFileInfo.
1553 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1554 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1556 * Returns: a string containing the value of the "etag:value" attribute.
1559 g_file_info_get_etag (GFileInfo *info)
1561 static guint32 attr = 0;
1562 GFileAttributeValue *value;
1564 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1567 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1569 value = g_file_info_find_value (info, attr);
1570 return _g_file_attribute_value_get_string (value);
1574 * g_file_info_get_sort_order:
1575 * @info: a #GFileInfo.
1577 * Gets the value of the sort_order attribute from the #GFileInfo.
1578 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1580 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1583 g_file_info_get_sort_order (GFileInfo *info)
1585 static guint32 attr = 0;
1586 GFileAttributeValue *value;
1588 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1591 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1593 value = g_file_info_find_value (info, attr);
1594 return _g_file_attribute_value_get_int32 (value);
1597 /* Helper setters: */
1599 * g_file_info_set_file_type:
1600 * @info: a #GFileInfo.
1601 * @type: a #GFileType.
1603 * Sets the file type in a #GFileInfo to @type.
1604 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1607 g_file_info_set_file_type (GFileInfo *info,
1610 static guint32 attr = 0;
1611 GFileAttributeValue *value;
1613 g_return_if_fail (G_IS_FILE_INFO (info));
1616 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1618 value = g_file_info_create_value (info, attr);
1620 _g_file_attribute_value_set_uint32 (value, type);
1624 * g_file_info_set_is_hidden:
1625 * @info: a #GFileInfo.
1626 * @is_hidden: a #gboolean.
1628 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1629 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1632 g_file_info_set_is_hidden (GFileInfo *info,
1635 static guint32 attr = 0;
1636 GFileAttributeValue *value;
1638 g_return_if_fail (G_IS_FILE_INFO (info));
1641 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1643 value = g_file_info_create_value (info, attr);
1645 _g_file_attribute_value_set_boolean (value, is_hidden);
1649 * g_file_info_set_is_symlink:
1650 * @info: a #GFileInfo.
1651 * @is_symlink: a #gboolean.
1653 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1654 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1657 g_file_info_set_is_symlink (GFileInfo *info,
1658 gboolean is_symlink)
1660 static guint32 attr = 0;
1661 GFileAttributeValue *value;
1663 g_return_if_fail (G_IS_FILE_INFO (info));
1666 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1668 value = g_file_info_create_value (info, attr);
1670 _g_file_attribute_value_set_boolean (value, is_symlink);
1674 * g_file_info_set_name:
1675 * @info: a #GFileInfo.
1676 * @name: a string containing a name.
1678 * Sets the name attribute for the current #GFileInfo.
1679 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1682 g_file_info_set_name (GFileInfo *info,
1685 static guint32 attr = 0;
1686 GFileAttributeValue *value;
1688 g_return_if_fail (G_IS_FILE_INFO (info));
1689 g_return_if_fail (name != NULL);
1692 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1694 value = g_file_info_create_value (info, attr);
1696 _g_file_attribute_value_set_byte_string (value, name);
1700 * g_file_info_set_display_name:
1701 * @info: a #GFileInfo.
1702 * @display_name: a string containing a display name.
1704 * Sets the display name for the current #GFileInfo.
1705 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1708 g_file_info_set_display_name (GFileInfo *info,
1709 const char *display_name)
1711 static guint32 attr = 0;
1712 GFileAttributeValue *value;
1714 g_return_if_fail (G_IS_FILE_INFO (info));
1715 g_return_if_fail (display_name != NULL);
1718 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1720 value = g_file_info_create_value (info, attr);
1722 _g_file_attribute_value_set_string (value, display_name);
1726 * g_file_info_set_edit_name:
1727 * @info: a #GFileInfo.
1728 * @edit_name: a string containing an edit name.
1730 * Sets the edit name for the current file.
1731 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1734 g_file_info_set_edit_name (GFileInfo *info,
1735 const char *edit_name)
1737 static guint32 attr = 0;
1738 GFileAttributeValue *value;
1740 g_return_if_fail (G_IS_FILE_INFO (info));
1741 g_return_if_fail (edit_name != NULL);
1744 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1746 value = g_file_info_create_value (info, attr);
1748 _g_file_attribute_value_set_string (value, edit_name);
1752 * g_file_info_set_icon:
1753 * @info: a #GFileInfo.
1756 * Sets the icon for a given #GFileInfo.
1757 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1760 g_file_info_set_icon (GFileInfo *info,
1763 static guint32 attr = 0;
1764 GFileAttributeValue *value;
1766 g_return_if_fail (G_IS_FILE_INFO (info));
1767 g_return_if_fail (G_IS_ICON (icon));
1770 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1772 value = g_file_info_create_value (info, attr);
1774 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1778 * g_file_info_set_content_type:
1779 * @info: a #GFileInfo.
1780 * @content_type: a content type. See #GContentType.
1782 * Sets the content type attribute for a given #GFileInfo.
1783 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1786 g_file_info_set_content_type (GFileInfo *info,
1787 const char *content_type)
1789 static guint32 attr = 0;
1790 GFileAttributeValue *value;
1792 g_return_if_fail (G_IS_FILE_INFO (info));
1793 g_return_if_fail (content_type != NULL);
1796 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1798 value = g_file_info_create_value (info, attr);
1800 _g_file_attribute_value_set_string (value, content_type);
1804 * g_file_info_set_size:
1805 * @info: a #GFileInfo.
1806 * @size: a #goffset containing the file's size.
1808 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1809 * to the given size.
1812 g_file_info_set_size (GFileInfo *info,
1815 static guint32 attr = 0;
1816 GFileAttributeValue *value;
1818 g_return_if_fail (G_IS_FILE_INFO (info));
1821 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1823 value = g_file_info_create_value (info, attr);
1825 _g_file_attribute_value_set_uint64 (value, size);
1829 * g_file_info_set_modification_time
1830 * @info: a #GFileInfo.
1831 * @mtime: a #GTimeVal.
1833 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
1834 * info to the given time value.
1837 g_file_info_set_modification_time (GFileInfo *info,
1840 static guint32 attr_mtime = 0, attr_mtime_usec;
1841 GFileAttributeValue *value;
1843 g_return_if_fail (G_IS_FILE_INFO (info));
1844 g_return_if_fail (mtime != NULL);
1846 if (attr_mtime == 0)
1848 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1849 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1852 value = g_file_info_create_value (info, attr_mtime);
1854 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
1855 value = g_file_info_create_value (info, attr_mtime_usec);
1857 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
1861 * g_file_info_set_symlink_target:
1862 * @info: a #GFileInfo.
1863 * @symlink_target: a static string containing a path to a symlink target.
1865 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
1866 * to the given symlink target.
1869 g_file_info_set_symlink_target (GFileInfo *info,
1870 const char *symlink_target)
1872 static guint32 attr = 0;
1873 GFileAttributeValue *value;
1875 g_return_if_fail (G_IS_FILE_INFO (info));
1876 g_return_if_fail (symlink_target != NULL);
1879 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1881 value = g_file_info_create_value (info, attr);
1883 _g_file_attribute_value_set_byte_string (value, symlink_target);
1887 * g_file_info_set_sort_order:
1888 * @info: a #GFileInfo.
1889 * @sort_order: a sort order integer.
1891 * Sets the sort order attribute in the file info structure. See
1892 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1895 g_file_info_set_sort_order (GFileInfo *info,
1898 static guint32 attr = 0;
1899 GFileAttributeValue *value;
1901 g_return_if_fail (G_IS_FILE_INFO (info));
1904 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1906 value = g_file_info_create_value (info, attr);
1908 _g_file_attribute_value_set_int32 (value, sort_order);
1912 #define ON_STACK_MATCHERS 5
1919 struct _GFileAttributeMatcher {
1921 SubMatcher sub_matchers[ON_STACK_MATCHERS];
1922 GArray *more_sub_matchers;
1925 guint32 iterator_ns;
1931 matcher_add (GFileAttributeMatcher *matcher,
1935 SubMatcher *sub_matchers;
1939 for (i = 0; i < ON_STACK_MATCHERS; i++)
1941 /* First empty spot, not found, use this */
1942 if (matcher->sub_matchers[i].id == 0)
1944 matcher->sub_matchers[i].id = id;
1945 matcher->sub_matchers[i].mask = mask;
1950 if (matcher->sub_matchers[i].id == id &&
1951 matcher->sub_matchers[i].mask == mask)
1955 if (matcher->more_sub_matchers == NULL)
1956 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
1958 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
1959 for (i = 0; i < matcher->more_sub_matchers->len; i++)
1962 if (sub_matchers[i].id == id &&
1963 sub_matchers[i].mask == mask)
1970 g_array_append_val (matcher->more_sub_matchers, s);
1974 * g_file_attribute_matcher_new:
1975 * @attributes: an attribute string to match.
1977 * Creates a new file attribute matcher, which matches attributes
1978 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
1979 * counted structures, and are created with a reference count of 1. If
1980 * the number of references falls to 0, the #GFileAttributeMatcher is
1981 * automatically destroyed.
1983 * The @attribute string should be formatted with specific keys separated
1984 * from namespaces with a double colon. Several "namespace::key" strings may be
1985 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
1986 * The wildcard "*" may be used to match all keys and namespaces, or
1987 * "namespace::*" will match all keys in a given namespace.
1989 * Examples of strings to use:
1991 * <title>File Attribute Matcher strings and results</title>
1992 * <tgroup cols='2' align='left'><thead>
1993 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
1995 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
1996 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
1997 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
1998 * all keys in the unix namespace.</entry></row>
2002 * Returns: a #GFileAttributeMatcher.
2004 GFileAttributeMatcher *
2005 g_file_attribute_matcher_new (const char *attributes)
2010 GFileAttributeMatcher *matcher;
2012 if (attributes == NULL || *attributes == '\0')
2015 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2018 split = g_strsplit (attributes, ",", -1);
2020 for (i = 0; split[i] != NULL; i++)
2022 if (strcmp (split[i], "*") == 0)
2023 matcher->all = TRUE;
2028 colon = strstr (split[i], "::");
2029 if (colon != NULL &&
2034 id = lookup_attribute (split[i]);
2042 id = lookup_namespace (split[i]) << NS_POS;
2043 mask = NS_MASK << NS_POS;
2046 matcher_add (matcher, id, mask);
2056 * g_file_attribute_matcher_ref:
2057 * @matcher: a #GFileAttributeMatcher.
2059 * References a file attribute matcher.
2061 * Returns: a #GFileAttributeMatcher.
2063 GFileAttributeMatcher *
2064 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2068 g_return_val_if_fail (matcher->ref > 0, NULL);
2069 g_atomic_int_inc (&matcher->ref);
2075 * g_file_attribute_matcher_unref:
2076 * @matcher: a #GFileAttributeMatcher.
2078 * Unreferences @matcher. If the reference count falls below 1,
2079 * the @matcher is automatically freed.
2083 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2087 g_return_if_fail (matcher->ref > 0);
2089 if (g_atomic_int_dec_and_test (&matcher->ref))
2091 if (matcher->more_sub_matchers)
2092 g_array_free (matcher->more_sub_matchers, TRUE);
2100 * g_file_attribute_matcher_matches_only:
2101 * @matcher: a #GFileAttributeMatcher.
2102 * @attribute: a file attribute key.
2104 * Checks if a attribute matcher only matches a given attribute. Always
2105 * returns %FALSE if "*" was used when creating the matcher.
2107 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2110 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2111 const char *attribute)
2115 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2117 if (matcher == NULL ||
2121 id = lookup_attribute (attribute);
2123 if (matcher->sub_matchers[0].id != 0 &&
2124 matcher->sub_matchers[1].id == 0 &&
2125 matcher->sub_matchers[0].mask == 0xffffffff &&
2126 matcher->sub_matchers[0].id == id)
2133 matcher_matches_id (GFileAttributeMatcher *matcher,
2136 SubMatcher *sub_matchers;
2139 for (i = 0; i < ON_STACK_MATCHERS; i++)
2141 if (matcher->sub_matchers[i].id == 0)
2144 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2148 if (matcher->more_sub_matchers)
2150 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2151 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2153 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2162 g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2165 /* We return a NULL matcher for an empty match string, so handle this */
2166 if (matcher == NULL)
2172 return matcher_matches_id (matcher, id);
2176 * g_file_attribute_matcher_matches:
2177 * @matcher: a #GFileAttributeMatcher.
2178 * @attribute: a file attribute key.
2180 * Checks if an attribute will be matched by an attribute matcher. If
2181 * the matcher was created with the "*" matching string, this function
2182 * will always return %TRUE.
2184 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2187 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2188 const char *attribute)
2190 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2192 /* We return a NULL matcher for an empty match string, so handle this */
2193 if (matcher == NULL)
2199 return matcher_matches_id (matcher, lookup_attribute (attribute));
2202 /* return TRUE -> all */
2204 * g_file_attribute_matcher_enumerate_namespace:
2205 * @matcher: a #GFileAttributeMatcher.
2206 * @ns: a string containing a file attribute namespace.
2208 * Checks if the matcher will match all of the keys in a given namespace.
2209 * This will always return %TRUE if a wildcard character is in use (e.g. if
2210 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2211 * using "*" and namespace is anything.)
2213 * TODO: this is awkwardly worded.
2215 * Returns: %TRUE if the matcher matches all of the entries
2216 * in the given @ns, %FALSE otherwise.
2219 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2222 SubMatcher *sub_matchers;
2226 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2228 /* We return a NULL matcher for an empty match string, so handle this */
2229 if (matcher == NULL)
2235 ns_id = lookup_namespace (ns) << NS_POS;
2237 for (i = 0; i < ON_STACK_MATCHERS; i++)
2239 if (matcher->sub_matchers[i].id == ns_id)
2243 if (matcher->more_sub_matchers)
2245 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2246 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2248 if (sub_matchers[i].id == ns_id)
2253 matcher->iterator_ns = ns_id;
2254 matcher->iterator_pos = 0;
2260 * g_file_attribute_matcher_enumerate_next:
2261 * @matcher: a #GFileAttributeMatcher.
2263 * Gets the next matched attribute from a #GFileAttributeMatcher.
2265 * Returns: a string containing the next attribute or %NULL if
2266 * no more attribute exist.
2269 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2272 SubMatcher *sub_matcher;
2274 /* We return a NULL matcher for an empty match string, so handle this */
2275 if (matcher == NULL)
2280 i = matcher->iterator_pos++;
2282 if (i < ON_STACK_MATCHERS)
2284 if (matcher->sub_matchers[i].id == 0)
2287 sub_matcher = &matcher->sub_matchers[i];
2291 if (matcher->more_sub_matchers == NULL)
2294 i -= ON_STACK_MATCHERS;
2295 if (i < matcher->more_sub_matchers->len)
2296 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2301 if (sub_matcher->mask == 0xffffffff &&
2302 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2303 return get_attribute_for_id (sub_matcher->id);
2307 #define __G_FILE_INFO_C__
2308 #include "gioaliasdef.c"