1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 * @short_description: File Information and Attributes
27 * @see_also: #GFile, <link linkend="gio-GFileAttribute">GFileAttribute</link>
29 * Functionality for manipulating basic metadata for files. #GFileInfo
30 * implements methods for getting information that all files should
31 * contain, and allows for manipulation of extended attributes.
33 * See <link linkend="gio-GFileAttribute">GFileAttribute</link> for more
34 * information on how GIO handles file attributes.
36 * To obtain a #GFileInfo for a #GFile, use g_file_query_info() (or its
37 * async variant). To obtain a #GFileInfo for a file input or output
38 * stream, use g_file_input_stream_query_info() or
39 * g_file_output_stream_query_info() (or their async variants).
41 * To change the actual attributes of a file, you should then set the
42 * attribute in the #GFileInfo and call g_file_set_attributes_from_info()
43 * or g_file_set_attributes_async() on a GFile.
45 * However, not all attributes can be changed in the file. For instance,
46 * the actual size of a file cannot be changed via g_file_info_set_size().
47 * You may call g_file_query_settable_attributes() and
48 * g_file_query_writable_namespaces() to discover the settable attributes
49 * of a particular file at runtime.
51 * #GFileAttributeMatcher allows for searching through a #GFileInfo for
59 #include "gfileinfo.h"
60 #include "gfileattribute-priv.h"
66 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
67 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
71 GFileAttributeValue value;
76 GObject parent_instance;
79 GFileAttributeMatcher *mask;
82 struct _GFileInfoClass
84 GObjectClass parent_class;
88 static gboolean g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
91 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
95 guint32 attribute_id_counter;
98 G_LOCK_DEFINE_STATIC (attribute_hash);
99 static int namespace_id_counter = 0;
100 static GHashTable *ns_hash = NULL;
101 static GHashTable *attribute_hash = NULL;
102 static char ***attributes = NULL;
104 /* Attribute ids are 32bit, we split it up like this:
105 * |------------|--------------------|
107 * namespace attribute id
109 * This way the attributes gets sorted in namespace order
113 #define NS_MASK ((guint32)((1<<12) - 1))
115 #define ID_MASK ((guint32)((1<<20) - 1))
117 #define GET_NS(_attr_id) \
118 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
119 #define GET_ID(_attr_id) \
120 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
122 #define MAKE_ATTR_ID(_ns, _id) \
123 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
124 ((((guint32) _id) & ID_MASK) << ID_POS) )
127 _lookup_namespace (const char *namespace)
131 ns_info = g_hash_table_lookup (ns_hash, namespace);
134 ns_info = g_new0 (NSInfo, 1);
135 ns_info->id = ++namespace_id_counter;
136 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
137 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
138 attributes[ns_info->id] = NULL;
144 lookup_namespace (const char *namespace)
149 G_LOCK (attribute_hash);
151 if (attribute_hash == NULL)
153 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
154 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
157 ns_info = _lookup_namespace (namespace);
162 G_UNLOCK (attribute_hash);
168 get_attribute_for_id (int attribute)
171 G_LOCK (attribute_hash);
172 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
173 G_UNLOCK (attribute_hash);
178 lookup_attribute (const char *attribute)
185 G_LOCK (attribute_hash);
186 if (attribute_hash == NULL)
188 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
189 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
192 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
196 G_UNLOCK (attribute_hash);
200 colon = strstr (attribute, "::");
202 ns = g_strndup (attribute, colon - attribute);
206 ns_info = _lookup_namespace (ns);
209 id = ++ns_info->attribute_id_counter;
210 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
211 attributes[ns_info->id][id] = g_strdup (attribute);
213 attr_id = MAKE_ATTR_ID (ns_info->id, id);
215 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
217 G_UNLOCK (attribute_hash);
223 g_file_info_finalize (GObject *object)
227 GFileAttribute *attrs;
229 info = G_FILE_INFO (object);
231 attrs = (GFileAttribute *)info->attributes->data;
232 for (i = 0; i < info->attributes->len; i++)
233 _g_file_attribute_value_clear (&attrs[i].value);
234 g_array_free (info->attributes, TRUE);
236 if (info->mask != NO_ATTRIBUTE_MASK)
237 g_file_attribute_matcher_unref (info->mask);
239 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
243 g_file_info_class_init (GFileInfoClass *klass)
245 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
247 gobject_class->finalize = g_file_info_finalize;
251 g_file_info_init (GFileInfo *info)
253 info->mask = NO_ATTRIBUTE_MASK;
254 info->attributes = g_array_new (FALSE, FALSE,
255 sizeof (GFileAttribute));
261 * Creates a new file info structure.
263 * Returns: a #GFileInfo.
266 g_file_info_new (void)
268 return g_object_new (G_TYPE_FILE_INFO, NULL);
272 * g_file_info_copy_into:
273 * @src_info: source to copy attributes from.
274 * @dest_info: destination to copy attributes to.
276 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
279 g_file_info_copy_into (GFileInfo *src_info,
280 GFileInfo *dest_info)
282 GFileAttribute *source, *dest;
285 g_return_if_fail (G_IS_FILE_INFO (src_info));
286 g_return_if_fail (G_IS_FILE_INFO (dest_info));
288 dest = (GFileAttribute *)dest_info->attributes->data;
289 for (i = 0; i < dest_info->attributes->len; i++)
290 _g_file_attribute_value_clear (&dest[i].value);
292 g_array_set_size (dest_info->attributes,
293 src_info->attributes->len);
295 source = (GFileAttribute *)src_info->attributes->data;
296 dest = (GFileAttribute *)dest_info->attributes->data;
298 for (i = 0; i < src_info->attributes->len; i++)
300 dest[i].attribute = source[i].attribute;
301 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
302 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
305 if (dest_info->mask != NO_ATTRIBUTE_MASK)
306 g_file_attribute_matcher_unref (dest_info->mask);
308 if (src_info->mask == NO_ATTRIBUTE_MASK)
309 dest_info->mask = NO_ATTRIBUTE_MASK;
311 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
316 * @other: a #GFileInfo.
318 * Duplicates a file info structure.
320 * Returns: a duplicate #GFileInfo of @other.
323 g_file_info_dup (GFileInfo *other)
327 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
329 new = g_file_info_new ();
330 g_file_info_copy_into (other, new);
335 * g_file_info_set_attribute_mask:
336 * @info: a #GFileInfo.
337 * @mask: a #GFileAttributeMatcher.
339 * Sets @mask on @info to match specific attribute types.
342 g_file_info_set_attribute_mask (GFileInfo *info,
343 GFileAttributeMatcher *mask)
345 GFileAttribute *attr;
348 g_return_if_fail (G_IS_FILE_INFO (info));
350 if (mask != info->mask)
352 if (info->mask != NO_ATTRIBUTE_MASK)
353 g_file_attribute_matcher_unref (info->mask);
354 info->mask = g_file_attribute_matcher_ref (mask);
356 /* Remove non-matching attributes */
357 for (i = 0; i < info->attributes->len; i++)
359 attr = &g_array_index (info->attributes, GFileAttribute, i);
360 if (!g_file_attribute_matcher_matches_id (mask,
363 _g_file_attribute_value_clear (&attr->value);
364 g_array_remove_index (info->attributes, i);
372 * g_file_info_unset_attribute_mask:
375 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
379 g_file_info_unset_attribute_mask (GFileInfo *info)
381 g_return_if_fail (G_IS_FILE_INFO (info));
383 if (info->mask != NO_ATTRIBUTE_MASK)
384 g_file_attribute_matcher_unref (info->mask);
385 info->mask = NO_ATTRIBUTE_MASK;
389 * g_file_info_clear_status:
390 * @info: a #GFileInfo.
392 * Clears the status information from @info.
395 g_file_info_clear_status (GFileInfo *info)
397 GFileAttribute *attrs;
400 g_return_if_fail (G_IS_FILE_INFO (info));
402 attrs = (GFileAttribute *)info->attributes->data;
403 for (i = 0; i < info->attributes->len; i++)
404 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
408 g_file_info_find_place (GFileInfo *info,
412 GFileAttribute *attrs;
413 /* Binary search for the place where attribute would be, if it's
417 max = info->attributes->len;
419 attrs = (GFileAttribute *)info->attributes->data;
423 med = min + (max - min) / 2;
424 if (attrs[med].attribute == attribute)
429 else if (attrs[med].attribute < attribute)
431 else /* attrs[med].attribute > attribute */
438 static GFileAttributeValue *
439 g_file_info_find_value (GFileInfo *info,
442 GFileAttribute *attrs;
445 i = g_file_info_find_place (info, attr_id);
446 attrs = (GFileAttribute *)info->attributes->data;
447 if (i < info->attributes->len &&
448 attrs[i].attribute == attr_id)
449 return &attrs[i].value;
454 static GFileAttributeValue *
455 g_file_info_find_value_by_name (GFileInfo *info,
456 const char *attribute)
460 attr_id = lookup_attribute (attribute);
461 return g_file_info_find_value (info, attr_id);
465 * g_file_info_has_attribute:
466 * @info: a #GFileInfo.
467 * @attribute: a file attribute key.
469 * Checks if a file info structure has an attribute named @attribute.
471 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
475 g_file_info_has_attribute (GFileInfo *info,
476 const char *attribute)
478 GFileAttributeValue *value;
480 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
481 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
483 value = g_file_info_find_value_by_name (info, attribute);
484 return value != NULL;
488 * g_file_info_has_namespace:
489 * @info: a #GFileInfo.
490 * @name_space: a file attribute namespace.
492 * Checks if a file info structure has an attribute in the
493 * specified @name_space.
495 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
501 g_file_info_has_namespace (GFileInfo *info,
502 const char *name_space)
504 GFileAttribute *attrs;
508 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
509 g_return_val_if_fail (name_space != NULL, FALSE);
511 ns_id = lookup_namespace (name_space);
513 attrs = (GFileAttribute *)info->attributes->data;
514 for (i = 0; i < info->attributes->len; i++)
516 if (GET_NS (attrs[i].attribute) == ns_id)
524 * g_file_info_list_attributes:
525 * @info: a #GFileInfo.
526 * @name_space: a file attribute key's namespace.
528 * Lists the file info structure's attributes.
530 * Returns: a null-terminated array of strings of all of the
531 * possible attribute types for the given @name_space, or
535 g_file_info_list_attributes (GFileInfo *info,
536 const char *name_space)
539 GFileAttribute *attrs;
541 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
544 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
546 names = g_ptr_array_new ();
547 attrs = (GFileAttribute *)info->attributes->data;
548 for (i = 0; i < info->attributes->len; i++)
550 attribute = attrs[i].attribute;
551 if (ns_id == 0 || GET_NS (attribute) == ns_id)
552 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
556 g_ptr_array_add (names, NULL);
558 return (char **)g_ptr_array_free (names, FALSE);
562 * g_file_info_get_attribute_type:
563 * @info: a #GFileInfo.
564 * @attribute: a file attribute key.
566 * Gets the attribute type for an attribute key.
568 * Returns: a #GFileAttributeType for the given @attribute, or
569 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
572 g_file_info_get_attribute_type (GFileInfo *info,
573 const char *attribute)
575 GFileAttributeValue *value;
577 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
578 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
580 value = g_file_info_find_value_by_name (info, attribute);
584 return G_FILE_ATTRIBUTE_TYPE_INVALID;
588 * g_file_info_remove_attribute:
589 * @info: a #GFileInfo.
590 * @attribute: a file attribute key.
592 * Removes all cases of @attribute from @info if it exists.
595 g_file_info_remove_attribute (GFileInfo *info,
596 const char *attribute)
599 GFileAttribute *attrs;
602 g_return_if_fail (G_IS_FILE_INFO (info));
603 g_return_if_fail (attribute != NULL && *attribute != '\0');
605 attr_id = lookup_attribute (attribute);
607 i = g_file_info_find_place (info, attr_id);
608 attrs = (GFileAttribute *)info->attributes->data;
609 if (i < info->attributes->len &&
610 attrs[i].attribute == attr_id)
612 _g_file_attribute_value_clear (&attrs[i].value);
613 g_array_remove_index (info->attributes, i);
618 * g_file_info_get_attribute_data:
619 * @info: a #GFileInfo
620 * @attribute: a file attribute key
621 * @type: return location for the attribute type, or %NULL
622 * @value_pp: return location for the attribute value, or %NULL
623 * @status: return location for the attribute status, or %NULL
625 * Gets the attribute type, value and status for an attribute key.
627 * Returns: %TRUE if @info has an attribute named @attribute,
631 g_file_info_get_attribute_data (GFileInfo *info,
632 const char *attribute,
633 GFileAttributeType *type,
635 GFileAttributeStatus *status)
637 GFileAttributeValue *value;
639 value = g_file_info_find_value_by_name (info, attribute);
644 *status = value->status;
650 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
656 * g_file_info_get_attribute_status:
657 * @info: a #GFileInfo
658 * @attribute: a file attribute key
660 * Gets the attribute status for an attribute key.
662 * Returns: a #GFileAttributeStatus for the given @attribute, or
663 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
667 g_file_info_get_attribute_status (GFileInfo *info,
668 const char *attribute)
670 GFileAttributeValue *val;
672 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
673 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
675 val = g_file_info_find_value_by_name (info, attribute);
679 return G_FILE_ATTRIBUTE_STATUS_UNSET;
683 * g_file_info_set_attribute_status:
684 * @info: a #GFileInfo
685 * @attribute: a file attribute key
686 * @status: a #GFileAttributeStatus
688 * Sets the attribute status for an attribute key. This is only
689 * needed by external code that implement g_file_set_attributes_from_info()
690 * or similar functions.
692 * The attribute must exist in @info for this to work. Otherwise %FALSE
693 * is returned and @info is unchanged.
695 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
700 g_file_info_set_attribute_status (GFileInfo *info,
701 const char *attribute,
702 GFileAttributeStatus status)
704 GFileAttributeValue *val;
706 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
707 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
709 val = g_file_info_find_value_by_name (info, attribute);
712 val->status = status;
719 GFileAttributeValue *
720 _g_file_info_get_attribute_value (GFileInfo *info,
721 const char *attribute)
724 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
725 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
727 return g_file_info_find_value_by_name (info, attribute);
731 * g_file_info_get_attribute_as_string:
732 * @info: a #GFileInfo.
733 * @attribute: a file attribute key.
735 * Gets the value of a attribute, formated as a string.
736 * This escapes things as needed to make the string valid
739 * Returns: a UTF-8 string associated with the given @attribute.
740 * When you're done with the string it must be freed with g_free().
743 g_file_info_get_attribute_as_string (GFileInfo *info,
744 const char *attribute)
746 GFileAttributeValue *val;
747 val = _g_file_info_get_attribute_value (info, attribute);
749 return _g_file_attribute_value_as_string (val);
755 * g_file_info_get_attribute_object:
756 * @info: a #GFileInfo.
757 * @attribute: a file attribute key.
759 * Gets the value of a #GObject attribute. If the attribute does
760 * not contain a #GObject, %NULL will be returned.
762 * Returns: a #GObject associated with the given @attribute, or
766 g_file_info_get_attribute_object (GFileInfo *info,
767 const char *attribute)
769 GFileAttributeValue *value;
771 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
772 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
774 value = g_file_info_find_value_by_name (info, attribute);
775 return _g_file_attribute_value_get_object (value);
779 * g_file_info_get_attribute_string:
780 * @info: a #GFileInfo.
781 * @attribute: a file attribute key.
783 * Gets the value of a string attribute. If the attribute does
784 * not contain a string, %NULL will be returned.
786 * Returns: the contents of the @attribute value as a string, or
790 g_file_info_get_attribute_string (GFileInfo *info,
791 const char *attribute)
793 GFileAttributeValue *value;
795 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
796 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
798 value = g_file_info_find_value_by_name (info, attribute);
799 return _g_file_attribute_value_get_string (value);
803 * g_file_info_get_attribute_byte_string:
804 * @info: a #GFileInfo.
805 * @attribute: a file attribute key.
807 * Gets the value of a byte string attribute. If the attribute does
808 * not contain a byte string, %NULL will be returned.
810 * Returns: the contents of the @attribute value as a byte string, or
814 g_file_info_get_attribute_byte_string (GFileInfo *info,
815 const char *attribute)
817 GFileAttributeValue *value;
819 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
820 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
822 value = g_file_info_find_value_by_name (info, attribute);
823 return _g_file_attribute_value_get_byte_string (value);
827 * g_file_info_get_attribute_stringv:
828 * @info: a #GFileInfo.
829 * @attribute: a file attribute key.
831 * Gets the value of a stringv attribute. If the attribute does
832 * not contain a stringv, %NULL will be returned.
834 * Returns: the contents of the @attribute value as a stringv, or
835 * %NULL otherwise. Do not free.
838 g_file_info_get_attribute_stringv (GFileInfo *info,
839 const char *attribute)
841 GFileAttributeValue *value;
843 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
844 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
846 value = g_file_info_find_value_by_name (info, attribute);
847 return _g_file_attribute_value_get_stringv (value);
851 * g_file_info_get_attribute_boolean:
852 * @info: a #GFileInfo.
853 * @attribute: a file attribute key.
855 * Gets the value of a boolean attribute. If the attribute does not
856 * contain a boolean value, %FALSE will be returned.
858 * Returns: the boolean value contained within the attribute.
861 g_file_info_get_attribute_boolean (GFileInfo *info,
862 const char *attribute)
864 GFileAttributeValue *value;
866 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
867 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
869 value = g_file_info_find_value_by_name (info, attribute);
870 return _g_file_attribute_value_get_boolean (value);
874 * g_file_info_get_attribute_uint32:
875 * @info: a #GFileInfo.
876 * @attribute: a file attribute key.
878 * Gets an unsigned 32-bit integer contained within the attribute. If the
879 * attribute does not contain an unsigned 32-bit integer, or is invalid,
880 * 0 will be returned.
882 * Returns: an unsigned 32-bit integer from the attribute.
885 g_file_info_get_attribute_uint32 (GFileInfo *info,
886 const char *attribute)
888 GFileAttributeValue *value;
890 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
891 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
893 value = g_file_info_find_value_by_name (info, attribute);
894 return _g_file_attribute_value_get_uint32 (value);
898 * g_file_info_get_attribute_int32:
899 * @info: a #GFileInfo.
900 * @attribute: a file attribute key.
902 * Gets a signed 32-bit integer contained within the attribute. If the
903 * attribute does not contain a signed 32-bit integer, or is invalid,
904 * 0 will be returned.
906 * Returns: a signed 32-bit integer from the attribute.
909 g_file_info_get_attribute_int32 (GFileInfo *info,
910 const char *attribute)
912 GFileAttributeValue *value;
914 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
915 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
917 value = g_file_info_find_value_by_name (info, attribute);
918 return _g_file_attribute_value_get_int32 (value);
922 * g_file_info_get_attribute_uint64:
923 * @info: a #GFileInfo.
924 * @attribute: a file attribute key.
926 * Gets a unsigned 64-bit integer contained within the attribute. If the
927 * attribute does not contain an unsigned 64-bit integer, or is invalid,
928 * 0 will be returned.
930 * Returns: a unsigned 64-bit integer from the attribute.
933 g_file_info_get_attribute_uint64 (GFileInfo *info,
934 const char *attribute)
936 GFileAttributeValue *value;
938 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
939 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
941 value = g_file_info_find_value_by_name (info, attribute);
942 return _g_file_attribute_value_get_uint64 (value);
946 * g_file_info_get_attribute_int64:
947 * @info: a #GFileInfo.
948 * @attribute: a file attribute key.
950 * Gets a signed 64-bit integer contained within the attribute. If the
951 * attribute does not contain an signed 64-bit integer, or is invalid,
952 * 0 will be returned.
954 * Returns: a signed 64-bit integer from the attribute.
957 g_file_info_get_attribute_int64 (GFileInfo *info,
958 const char *attribute)
960 GFileAttributeValue *value;
962 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
963 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
965 value = g_file_info_find_value_by_name (info, attribute);
966 return _g_file_attribute_value_get_int64 (value);
969 static GFileAttributeValue *
970 g_file_info_create_value (GFileInfo *info,
973 GFileAttribute *attrs;
976 if (info->mask != NO_ATTRIBUTE_MASK &&
977 !g_file_attribute_matcher_matches_id (info->mask, attr_id))
980 i = g_file_info_find_place (info, attr_id);
982 attrs = (GFileAttribute *)info->attributes->data;
983 if (i < info->attributes->len &&
984 attrs[i].attribute == attr_id)
985 return &attrs[i].value;
988 GFileAttribute attr = { 0 };
989 attr.attribute = attr_id;
990 g_array_insert_val (info->attributes, i, attr);
992 attrs = (GFileAttribute *)info->attributes->data;
993 return &attrs[i].value;
997 static GFileAttributeValue *
998 g_file_info_create_value_by_name (GFileInfo *info,
999 const char *attribute)
1003 attr_id = lookup_attribute (attribute);
1005 return g_file_info_create_value (info, attr_id);
1009 * g_file_info_set_attribute:
1010 * @info: a #GFileInfo.
1011 * @attribute: a file attribute key.
1012 * @type: a #GFileAttributeType
1013 * @value_p: pointer to the value
1015 * Sets the @attribute to contain the given value, if possible.
1018 g_file_info_set_attribute (GFileInfo *info,
1019 const char *attribute,
1020 GFileAttributeType type,
1023 GFileAttributeValue *value;
1025 g_return_if_fail (G_IS_FILE_INFO (info));
1026 g_return_if_fail (attribute != NULL && *attribute != '\0');
1028 value = g_file_info_create_value_by_name (info, attribute);
1031 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1035 * g_file_info_set_attribute_object:
1036 * @info: a #GFileInfo.
1037 * @attribute: a file attribute key.
1038 * @attr_value: a #GObject.
1040 * Sets the @attribute to contain the given @attr_value,
1044 g_file_info_set_attribute_object (GFileInfo *info,
1045 const char *attribute,
1046 GObject *attr_value)
1048 GFileAttributeValue *value;
1050 g_return_if_fail (G_IS_FILE_INFO (info));
1051 g_return_if_fail (attribute != NULL && *attribute != '\0');
1052 g_return_if_fail (G_IS_OBJECT (attr_value));
1054 value = g_file_info_create_value_by_name (info, attribute);
1056 _g_file_attribute_value_set_object (value, attr_value);
1060 * g_file_info_set_attribute_stringv:
1061 * @info: a #GFileInfo.
1062 * @attribute: a file attribute key.
1063 * @attr_value: a %NULL terminated string array
1065 * Sets the @attribute to contain the given @attr_value,
1071 g_file_info_set_attribute_stringv (GFileInfo *info,
1072 const char *attribute,
1075 GFileAttributeValue *value;
1077 g_return_if_fail (G_IS_FILE_INFO (info));
1078 g_return_if_fail (attribute != NULL && *attribute != '\0');
1079 g_return_if_fail (attr_value != NULL);
1081 value = g_file_info_create_value_by_name (info, attribute);
1083 _g_file_attribute_value_set_stringv (value, attr_value);
1087 * g_file_info_set_attribute_string:
1088 * @info: a #GFileInfo.
1089 * @attribute: a file attribute key.
1090 * @attr_value: a string.
1092 * Sets the @attribute to contain the given @attr_value,
1096 g_file_info_set_attribute_string (GFileInfo *info,
1097 const char *attribute,
1098 const char *attr_value)
1100 GFileAttributeValue *value;
1102 g_return_if_fail (G_IS_FILE_INFO (info));
1103 g_return_if_fail (attribute != NULL && *attribute != '\0');
1104 g_return_if_fail (attr_value != NULL);
1106 value = g_file_info_create_value_by_name (info, attribute);
1108 _g_file_attribute_value_set_string (value, attr_value);
1112 * g_file_info_set_attribute_byte_string:
1113 * @info: a #GFileInfo.
1114 * @attribute: a file attribute key.
1115 * @attr_value: a byte string.
1117 * Sets the @attribute to contain the given @attr_value,
1121 g_file_info_set_attribute_byte_string (GFileInfo *info,
1122 const char *attribute,
1123 const char *attr_value)
1125 GFileAttributeValue *value;
1127 g_return_if_fail (G_IS_FILE_INFO (info));
1128 g_return_if_fail (attribute != NULL && *attribute != '\0');
1129 g_return_if_fail (attr_value != NULL);
1131 value = g_file_info_create_value_by_name (info, attribute);
1133 _g_file_attribute_value_set_byte_string (value, attr_value);
1137 * g_file_info_set_attribute_boolean:
1138 * @info: a #GFileInfo.
1139 * @attribute: a file attribute key.
1140 * @attr_value: a boolean value.
1142 * Sets the @attribute to contain the given @attr_value,
1146 g_file_info_set_attribute_boolean (GFileInfo *info,
1147 const char *attribute,
1148 gboolean attr_value)
1150 GFileAttributeValue *value;
1152 g_return_if_fail (G_IS_FILE_INFO (info));
1153 g_return_if_fail (attribute != NULL && *attribute != '\0');
1155 value = g_file_info_create_value_by_name (info, attribute);
1157 _g_file_attribute_value_set_boolean (value, attr_value);
1161 * g_file_info_set_attribute_uint32:
1162 * @info: a #GFileInfo.
1163 * @attribute: a file attribute key.
1164 * @attr_value: an unsigned 32-bit integer.
1166 * Sets the @attribute to contain the given @attr_value,
1170 g_file_info_set_attribute_uint32 (GFileInfo *info,
1171 const char *attribute,
1174 GFileAttributeValue *value;
1176 g_return_if_fail (G_IS_FILE_INFO (info));
1177 g_return_if_fail (attribute != NULL && *attribute != '\0');
1179 value = g_file_info_create_value_by_name (info, attribute);
1181 _g_file_attribute_value_set_uint32 (value, attr_value);
1186 * g_file_info_set_attribute_int32:
1187 * @info: a #GFileInfo.
1188 * @attribute: a file attribute key.
1189 * @attr_value: a signed 32-bit integer
1191 * Sets the @attribute to contain the given @attr_value,
1195 g_file_info_set_attribute_int32 (GFileInfo *info,
1196 const char *attribute,
1199 GFileAttributeValue *value;
1201 g_return_if_fail (G_IS_FILE_INFO (info));
1202 g_return_if_fail (attribute != NULL && *attribute != '\0');
1204 value = g_file_info_create_value_by_name (info, attribute);
1206 _g_file_attribute_value_set_int32 (value, attr_value);
1210 * g_file_info_set_attribute_uint64:
1211 * @info: a #GFileInfo.
1212 * @attribute: a file attribute key.
1213 * @attr_value: an unsigned 64-bit integer.
1215 * Sets the @attribute to contain the given @attr_value,
1219 g_file_info_set_attribute_uint64 (GFileInfo *info,
1220 const char *attribute,
1223 GFileAttributeValue *value;
1225 g_return_if_fail (G_IS_FILE_INFO (info));
1226 g_return_if_fail (attribute != NULL && *attribute != '\0');
1228 value = g_file_info_create_value_by_name (info, attribute);
1230 _g_file_attribute_value_set_uint64 (value, attr_value);
1234 * g_file_info_set_attribute_int64:
1235 * @info: a #GFileInfo.
1236 * @attribute: attribute name to set.
1237 * @attr_value: int64 value to set attribute to.
1239 * Sets the @attribute to contain the given @attr_value,
1244 g_file_info_set_attribute_int64 (GFileInfo *info,
1245 const char *attribute,
1248 GFileAttributeValue *value;
1250 g_return_if_fail (G_IS_FILE_INFO (info));
1251 g_return_if_fail (attribute != NULL && *attribute != '\0');
1253 value = g_file_info_create_value_by_name (info, attribute);
1255 _g_file_attribute_value_set_int64 (value, attr_value);
1258 /* Helper getters */
1260 * g_file_info_get_file_type:
1261 * @info: a #GFileInfo.
1263 * Gets a file's type (whether it is a regular file, symlink, etc).
1264 * This is different from the file's content type, see g_file_info_get_content_type().
1266 * Returns: a #GFileType for the given file.
1269 g_file_info_get_file_type (GFileInfo *info)
1271 static guint32 attr = 0;
1272 GFileAttributeValue *value;
1274 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1277 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1279 value = g_file_info_find_value (info, attr);
1280 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1284 * g_file_info_get_is_hidden:
1285 * @info: a #GFileInfo.
1287 * Checks if a file is hidden.
1289 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1292 g_file_info_get_is_hidden (GFileInfo *info)
1294 static guint32 attr = 0;
1295 GFileAttributeValue *value;
1297 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1300 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1302 value = g_file_info_find_value (info, attr);
1303 return (GFileType)_g_file_attribute_value_get_boolean (value);
1307 * g_file_info_get_is_backup:
1308 * @info: a #GFileInfo.
1310 * Checks if a file is a backup file.
1312 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1315 g_file_info_get_is_backup (GFileInfo *info)
1317 static guint32 attr = 0;
1318 GFileAttributeValue *value;
1320 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1323 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1325 value = g_file_info_find_value (info, attr);
1326 return (GFileType)_g_file_attribute_value_get_boolean (value);
1330 * g_file_info_get_is_symlink:
1331 * @info: a #GFileInfo.
1333 * Checks if a file is a symlink.
1335 * Returns: %TRUE if the given @info is a symlink.
1338 g_file_info_get_is_symlink (GFileInfo *info)
1340 static guint32 attr = 0;
1341 GFileAttributeValue *value;
1343 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1346 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1348 value = g_file_info_find_value (info, attr);
1349 return (GFileType)_g_file_attribute_value_get_boolean (value);
1353 * g_file_info_get_name:
1354 * @info: a #GFileInfo.
1356 * Gets the name for a file.
1358 * Returns: a string containing the file name.
1361 g_file_info_get_name (GFileInfo *info)
1363 static guint32 attr = 0;
1364 GFileAttributeValue *value;
1366 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1369 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1371 value = g_file_info_find_value (info, attr);
1372 return _g_file_attribute_value_get_byte_string (value);
1376 * g_file_info_get_display_name:
1377 * @info: a #GFileInfo.
1379 * Gets a display name for a file.
1381 * Returns: a string containing the display name.
1384 g_file_info_get_display_name (GFileInfo *info)
1386 static guint32 attr = 0;
1387 GFileAttributeValue *value;
1389 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1392 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1394 value = g_file_info_find_value (info, attr);
1395 return _g_file_attribute_value_get_string (value);
1399 * g_file_info_get_edit_name:
1400 * @info: a #GFileInfo.
1402 * Gets the edit name for a file.
1404 * Returns: a string containing the edit name.
1407 g_file_info_get_edit_name (GFileInfo *info)
1409 static guint32 attr = 0;
1410 GFileAttributeValue *value;
1412 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1415 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1417 value = g_file_info_find_value (info, attr);
1418 return _g_file_attribute_value_get_string (value);
1422 * g_file_info_get_icon:
1423 * @info: a #GFileInfo.
1425 * Gets the icon for a file.
1427 * Returns: #GIcon for the given @info.
1430 g_file_info_get_icon (GFileInfo *info)
1432 static guint32 attr = 0;
1433 GFileAttributeValue *value;
1436 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1439 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1441 value = g_file_info_find_value (info, attr);
1442 obj = _g_file_attribute_value_get_object (value);
1443 if (G_IS_ICON (obj))
1444 return G_ICON (obj);
1449 * g_file_info_get_content_type:
1450 * @info: a #GFileInfo.
1452 * Gets the file's content type.
1454 * Returns: a string containing the file's content type.
1457 g_file_info_get_content_type (GFileInfo *info)
1459 static guint32 attr = 0;
1460 GFileAttributeValue *value;
1462 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1465 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1467 value = g_file_info_find_value (info, attr);
1468 return _g_file_attribute_value_get_string (value);
1472 * g_file_info_get_size:
1473 * @info: a #GFileInfo.
1475 * Gets the file's size.
1477 * Returns: a #goffset containing the file's size.
1480 g_file_info_get_size (GFileInfo *info)
1482 static guint32 attr = 0;
1483 GFileAttributeValue *value;
1485 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1488 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1490 value = g_file_info_find_value (info, attr);
1491 return (goffset) _g_file_attribute_value_get_uint64 (value);
1495 * g_file_info_get_modification_time:
1496 * @info: a #GFileInfo.
1497 * @result: a #GTimeVal.
1499 * Gets the modification time of the current @info and sets it
1503 g_file_info_get_modification_time (GFileInfo *info,
1506 static guint32 attr_mtime = 0, attr_mtime_usec;
1507 GFileAttributeValue *value;
1509 g_return_if_fail (G_IS_FILE_INFO (info));
1510 g_return_if_fail (result != NULL);
1512 if (attr_mtime == 0)
1514 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1515 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1518 value = g_file_info_find_value (info, attr_mtime);
1519 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1520 value = g_file_info_find_value (info, attr_mtime_usec);
1521 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1525 * g_file_info_get_symlink_target:
1526 * @info: a #GFileInfo.
1528 * Gets the symlink target for a given #GFileInfo.
1530 * Returns: a string containing the symlink target.
1533 g_file_info_get_symlink_target (GFileInfo *info)
1535 static guint32 attr = 0;
1536 GFileAttributeValue *value;
1538 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1541 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1543 value = g_file_info_find_value (info, attr);
1544 return _g_file_attribute_value_get_byte_string (value);
1548 * g_file_info_get_etag:
1549 * @info: a #GFileInfo.
1551 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1552 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1554 * Returns: a string containing the value of the "etag:value" attribute.
1557 g_file_info_get_etag (GFileInfo *info)
1559 static guint32 attr = 0;
1560 GFileAttributeValue *value;
1562 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1565 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1567 value = g_file_info_find_value (info, attr);
1568 return _g_file_attribute_value_get_string (value);
1572 * g_file_info_get_sort_order:
1573 * @info: a #GFileInfo.
1575 * Gets the value of the sort_order attribute from the #GFileInfo.
1576 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1578 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1581 g_file_info_get_sort_order (GFileInfo *info)
1583 static guint32 attr = 0;
1584 GFileAttributeValue *value;
1586 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1589 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1591 value = g_file_info_find_value (info, attr);
1592 return _g_file_attribute_value_get_int32 (value);
1595 /* Helper setters: */
1597 * g_file_info_set_file_type:
1598 * @info: a #GFileInfo.
1599 * @type: a #GFileType.
1601 * Sets the file type in a #GFileInfo to @type.
1602 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1605 g_file_info_set_file_type (GFileInfo *info,
1608 static guint32 attr = 0;
1609 GFileAttributeValue *value;
1611 g_return_if_fail (G_IS_FILE_INFO (info));
1614 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1616 value = g_file_info_create_value (info, attr);
1618 _g_file_attribute_value_set_uint32 (value, type);
1622 * g_file_info_set_is_hidden:
1623 * @info: a #GFileInfo.
1624 * @is_hidden: a #gboolean.
1626 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1627 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1630 g_file_info_set_is_hidden (GFileInfo *info,
1633 static guint32 attr = 0;
1634 GFileAttributeValue *value;
1636 g_return_if_fail (G_IS_FILE_INFO (info));
1639 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1641 value = g_file_info_create_value (info, attr);
1643 _g_file_attribute_value_set_boolean (value, is_hidden);
1647 * g_file_info_set_is_symlink:
1648 * @info: a #GFileInfo.
1649 * @is_symlink: a #gboolean.
1651 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1652 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1655 g_file_info_set_is_symlink (GFileInfo *info,
1656 gboolean is_symlink)
1658 static guint32 attr = 0;
1659 GFileAttributeValue *value;
1661 g_return_if_fail (G_IS_FILE_INFO (info));
1664 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1666 value = g_file_info_create_value (info, attr);
1668 _g_file_attribute_value_set_boolean (value, is_symlink);
1672 * g_file_info_set_name:
1673 * @info: a #GFileInfo.
1674 * @name: a string containing a name.
1676 * Sets the name attribute for the current #GFileInfo.
1677 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1680 g_file_info_set_name (GFileInfo *info,
1683 static guint32 attr = 0;
1684 GFileAttributeValue *value;
1686 g_return_if_fail (G_IS_FILE_INFO (info));
1687 g_return_if_fail (name != NULL);
1690 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1692 value = g_file_info_create_value (info, attr);
1694 _g_file_attribute_value_set_byte_string (value, name);
1698 * g_file_info_set_display_name:
1699 * @info: a #GFileInfo.
1700 * @display_name: a string containing a display name.
1702 * Sets the display name for the current #GFileInfo.
1703 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1706 g_file_info_set_display_name (GFileInfo *info,
1707 const char *display_name)
1709 static guint32 attr = 0;
1710 GFileAttributeValue *value;
1712 g_return_if_fail (G_IS_FILE_INFO (info));
1713 g_return_if_fail (display_name != NULL);
1716 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1718 value = g_file_info_create_value (info, attr);
1720 _g_file_attribute_value_set_string (value, display_name);
1724 * g_file_info_set_edit_name:
1725 * @info: a #GFileInfo.
1726 * @edit_name: a string containing an edit name.
1728 * Sets the edit name for the current file.
1729 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1732 g_file_info_set_edit_name (GFileInfo *info,
1733 const char *edit_name)
1735 static guint32 attr = 0;
1736 GFileAttributeValue *value;
1738 g_return_if_fail (G_IS_FILE_INFO (info));
1739 g_return_if_fail (edit_name != NULL);
1742 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1744 value = g_file_info_create_value (info, attr);
1746 _g_file_attribute_value_set_string (value, edit_name);
1750 * g_file_info_set_icon:
1751 * @info: a #GFileInfo.
1754 * Sets the icon for a given #GFileInfo.
1755 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1758 g_file_info_set_icon (GFileInfo *info,
1761 static guint32 attr = 0;
1762 GFileAttributeValue *value;
1764 g_return_if_fail (G_IS_FILE_INFO (info));
1765 g_return_if_fail (G_IS_ICON (icon));
1768 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1770 value = g_file_info_create_value (info, attr);
1772 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1776 * g_file_info_set_content_type:
1777 * @info: a #GFileInfo.
1778 * @content_type: a content type. See #GContentType.
1780 * Sets the content type attribute for a given #GFileInfo.
1781 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1784 g_file_info_set_content_type (GFileInfo *info,
1785 const char *content_type)
1787 static guint32 attr = 0;
1788 GFileAttributeValue *value;
1790 g_return_if_fail (G_IS_FILE_INFO (info));
1791 g_return_if_fail (content_type != NULL);
1794 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1796 value = g_file_info_create_value (info, attr);
1798 _g_file_attribute_value_set_string (value, content_type);
1802 * g_file_info_set_size:
1803 * @info: a #GFileInfo.
1804 * @size: a #goffset containing the file's size.
1806 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1807 * to the given size.
1810 g_file_info_set_size (GFileInfo *info,
1813 static guint32 attr = 0;
1814 GFileAttributeValue *value;
1816 g_return_if_fail (G_IS_FILE_INFO (info));
1819 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1821 value = g_file_info_create_value (info, attr);
1823 _g_file_attribute_value_set_uint64 (value, size);
1827 * g_file_info_set_modification_time
1828 * @info: a #GFileInfo.
1829 * @mtime: a #GTimeVal.
1831 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
1832 * info to the given time value.
1835 g_file_info_set_modification_time (GFileInfo *info,
1838 static guint32 attr_mtime = 0, attr_mtime_usec;
1839 GFileAttributeValue *value;
1841 g_return_if_fail (G_IS_FILE_INFO (info));
1842 g_return_if_fail (mtime != NULL);
1844 if (attr_mtime == 0)
1846 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1847 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1850 value = g_file_info_create_value (info, attr_mtime);
1852 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
1853 value = g_file_info_create_value (info, attr_mtime_usec);
1855 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
1859 * g_file_info_set_symlink_target:
1860 * @info: a #GFileInfo.
1861 * @symlink_target: a static string containing a path to a symlink target.
1863 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
1864 * to the given symlink target.
1867 g_file_info_set_symlink_target (GFileInfo *info,
1868 const char *symlink_target)
1870 static guint32 attr = 0;
1871 GFileAttributeValue *value;
1873 g_return_if_fail (G_IS_FILE_INFO (info));
1874 g_return_if_fail (symlink_target != NULL);
1877 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1879 value = g_file_info_create_value (info, attr);
1881 _g_file_attribute_value_set_byte_string (value, symlink_target);
1885 * g_file_info_set_sort_order:
1886 * @info: a #GFileInfo.
1887 * @sort_order: a sort order integer.
1889 * Sets the sort order attribute in the file info structure. See
1890 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1893 g_file_info_set_sort_order (GFileInfo *info,
1896 static guint32 attr = 0;
1897 GFileAttributeValue *value;
1899 g_return_if_fail (G_IS_FILE_INFO (info));
1902 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1904 value = g_file_info_create_value (info, attr);
1906 _g_file_attribute_value_set_int32 (value, sort_order);
1910 #define ON_STACK_MATCHERS 5
1917 struct _GFileAttributeMatcher {
1919 SubMatcher sub_matchers[ON_STACK_MATCHERS];
1920 GArray *more_sub_matchers;
1923 guint32 iterator_ns;
1929 matcher_add (GFileAttributeMatcher *matcher,
1933 SubMatcher *sub_matchers;
1937 for (i = 0; i < ON_STACK_MATCHERS; i++)
1939 /* First empty spot, not found, use this */
1940 if (matcher->sub_matchers[i].id == 0)
1942 matcher->sub_matchers[i].id = id;
1943 matcher->sub_matchers[i].mask = mask;
1948 if (matcher->sub_matchers[i].id == id &&
1949 matcher->sub_matchers[i].mask == mask)
1953 if (matcher->more_sub_matchers == NULL)
1954 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
1956 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
1957 for (i = 0; i < matcher->more_sub_matchers->len; i++)
1960 if (sub_matchers[i].id == id &&
1961 sub_matchers[i].mask == mask)
1968 g_array_append_val (matcher->more_sub_matchers, s);
1972 * g_file_attribute_matcher_new:
1973 * @attributes: an attribute string to match.
1975 * Creates a new file attribute matcher, which matches attributes
1976 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
1977 * counted structures, and are created with a reference count of 1. If
1978 * the number of references falls to 0, the #GFileAttributeMatcher is
1979 * automatically destroyed.
1981 * The @attribute string should be formatted with specific keys separated
1982 * from namespaces with a double colon. Several "namespace::key" strings may be
1983 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
1984 * The wildcard "*" may be used to match all keys and namespaces, or
1985 * "namespace::*" will match all keys in a given namespace.
1987 * Examples of strings to use:
1989 * <title>File Attribute Matcher strings and results</title>
1990 * <tgroup cols='2' align='left'><thead>
1991 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
1993 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
1994 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
1995 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
1996 * all keys in the unix namespace.</entry></row>
2000 * Returns: a #GFileAttributeMatcher.
2002 GFileAttributeMatcher *
2003 g_file_attribute_matcher_new (const char *attributes)
2008 GFileAttributeMatcher *matcher;
2010 if (attributes == NULL || *attributes == '\0')
2013 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2016 split = g_strsplit (attributes, ",", -1);
2018 for (i = 0; split[i] != NULL; i++)
2020 if (strcmp (split[i], "*") == 0)
2021 matcher->all = TRUE;
2026 colon = strstr (split[i], "::");
2027 if (colon != NULL &&
2032 id = lookup_attribute (split[i]);
2040 id = lookup_namespace (split[i]) << NS_POS;
2041 mask = NS_MASK << NS_POS;
2044 matcher_add (matcher, id, mask);
2054 * g_file_attribute_matcher_ref:
2055 * @matcher: a #GFileAttributeMatcher.
2057 * References a file attribute matcher.
2059 * Returns: a #GFileAttributeMatcher.
2061 GFileAttributeMatcher *
2062 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2066 g_return_val_if_fail (matcher->ref > 0, NULL);
2067 g_atomic_int_inc (&matcher->ref);
2073 * g_file_attribute_matcher_unref:
2074 * @matcher: a #GFileAttributeMatcher.
2076 * Unreferences @matcher. If the reference count falls below 1,
2077 * the @matcher is automatically freed.
2081 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2085 g_return_if_fail (matcher->ref > 0);
2087 if (g_atomic_int_dec_and_test (&matcher->ref))
2089 if (matcher->more_sub_matchers)
2090 g_array_free (matcher->more_sub_matchers, TRUE);
2098 * g_file_attribute_matcher_matches_only:
2099 * @matcher: a #GFileAttributeMatcher.
2100 * @attribute: a file attribute key.
2102 * Checks if a attribute matcher only matches a given attribute. Always
2103 * returns %FALSE if "*" was used when creating the matcher.
2105 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2108 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2109 const char *attribute)
2113 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2115 if (matcher == NULL ||
2119 id = lookup_attribute (attribute);
2121 if (matcher->sub_matchers[0].id != 0 &&
2122 matcher->sub_matchers[1].id == 0 &&
2123 matcher->sub_matchers[0].mask == 0xffffffff &&
2124 matcher->sub_matchers[0].id == id)
2131 matcher_matches_id (GFileAttributeMatcher *matcher,
2134 SubMatcher *sub_matchers;
2137 for (i = 0; i < ON_STACK_MATCHERS; i++)
2139 if (matcher->sub_matchers[i].id == 0)
2142 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2146 if (matcher->more_sub_matchers)
2148 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2149 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2151 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2160 g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2163 /* We return a NULL matcher for an empty match string, so handle this */
2164 if (matcher == NULL)
2170 return matcher_matches_id (matcher, id);
2174 * g_file_attribute_matcher_matches:
2175 * @matcher: a #GFileAttributeMatcher.
2176 * @attribute: a file attribute key.
2178 * Checks if an attribute will be matched by an attribute matcher. If
2179 * the matcher was created with the "*" matching string, this function
2180 * will always return %TRUE.
2182 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2185 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2186 const char *attribute)
2188 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2190 /* We return a NULL matcher for an empty match string, so handle this */
2191 if (matcher == NULL)
2197 return matcher_matches_id (matcher, lookup_attribute (attribute));
2200 /* return TRUE -> all */
2202 * g_file_attribute_matcher_enumerate_namespace:
2203 * @matcher: a #GFileAttributeMatcher.
2204 * @ns: a string containing a file attribute namespace.
2206 * Checks if the matcher will match all of the keys in a given namespace.
2207 * This will always return %TRUE if a wildcard character is in use (e.g. if
2208 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2209 * using "*" and namespace is anything.)
2211 * TODO: this is awkwardly worded.
2213 * Returns: %TRUE if the matcher matches all of the entries
2214 * in the given @ns, %FALSE otherwise.
2217 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2220 SubMatcher *sub_matchers;
2224 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2226 /* We return a NULL matcher for an empty match string, so handle this */
2227 if (matcher == NULL)
2233 ns_id = lookup_namespace (ns) << NS_POS;
2235 for (i = 0; i < ON_STACK_MATCHERS; i++)
2237 if (matcher->sub_matchers[i].id == ns_id)
2241 if (matcher->more_sub_matchers)
2243 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2244 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2246 if (sub_matchers[i].id == ns_id)
2251 matcher->iterator_ns = ns_id;
2252 matcher->iterator_pos = 0;
2258 * g_file_attribute_matcher_enumerate_next:
2259 * @matcher: a #GFileAttributeMatcher.
2261 * Gets the next matched attribute from a #GFileAttributeMatcher.
2263 * Returns: a string containing the next attribute or %NULL if
2264 * no more attribute exist.
2267 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2270 SubMatcher *sub_matcher;
2272 /* We return a NULL matcher for an empty match string, so handle this */
2273 if (matcher == NULL)
2278 i = matcher->iterator_pos++;
2280 if (i < ON_STACK_MATCHERS)
2282 if (matcher->sub_matchers[i].id == 0)
2285 sub_matcher = &matcher->sub_matchers[i];
2289 if (matcher->more_sub_matchers == NULL)
2292 i -= ON_STACK_MATCHERS;
2293 if (i < matcher->more_sub_matchers->len)
2294 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2299 if (sub_matcher->mask == 0xffffffff &&
2300 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2301 return get_attribute_for_id (sub_matcher->id);
2305 #define __G_FILE_INFO_C__
2306 #include "gioaliasdef.c"