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 "gfileinfo-priv.h"
61 #include "gfileattribute-priv.h"
67 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
68 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
72 GFileAttributeValue value;
77 GObject parent_instance;
80 GFileAttributeMatcher *mask;
83 struct _GFileInfoClass
85 GObjectClass parent_class;
89 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
93 guint32 attribute_id_counter;
96 G_LOCK_DEFINE_STATIC (attribute_hash);
97 static int namespace_id_counter = 0;
98 static GHashTable *ns_hash = NULL;
99 static GHashTable *attribute_hash = NULL;
100 static char ***attributes = NULL;
102 /* Attribute ids are 32bit, we split it up like this:
103 * |------------|--------------------|
105 * namespace attribute id
107 * This way the attributes gets sorted in namespace order
111 #define NS_MASK ((guint32)((1<<12) - 1))
113 #define ID_MASK ((guint32)((1<<20) - 1))
115 #define GET_NS(_attr_id) \
116 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
117 #define GET_ID(_attr_id) \
118 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
120 #define MAKE_ATTR_ID(_ns, _id) \
121 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
122 ((((guint32) _id) & ID_MASK) << ID_POS) )
125 _lookup_namespace (const char *namespace)
129 ns_info = g_hash_table_lookup (ns_hash, namespace);
132 ns_info = g_new0 (NSInfo, 1);
133 ns_info->id = ++namespace_id_counter;
134 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
135 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
136 attributes[ns_info->id] = NULL;
142 _lookup_attribute (const char *attribute)
149 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
154 colon = strstr (attribute, "::");
156 ns = g_strndup (attribute, colon - attribute);
160 ns_info = _lookup_namespace (ns);
163 id = ++ns_info->attribute_id_counter;
164 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
165 attributes[ns_info->id][id] = g_strdup (attribute);
167 attr_id = MAKE_ATTR_ID (ns_info->id, id);
169 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
175 ensure_attribute_hash (void)
177 if (attribute_hash != NULL)
180 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
181 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
183 #define REGISTER_ATTRIBUTE(name) G_STMT_START{\
184 guint _u = _lookup_attribute (G_FILE_ATTRIBUTE_ ## name); \
185 /* use for generating the ID: g_print ("#define G_FILE_ATTRIBUTE_ID_%s (%u + %u)\n", #name + 17, _u & ~ID_MASK, _u & ID_MASK); */ \
186 g_assert (_u == G_FILE_ATTRIBUTE_ID_ ## name); \
189 REGISTER_ATTRIBUTE (STANDARD_TYPE);
190 REGISTER_ATTRIBUTE (STANDARD_IS_HIDDEN);
191 REGISTER_ATTRIBUTE (STANDARD_IS_BACKUP);
192 REGISTER_ATTRIBUTE (STANDARD_IS_SYMLINK);
193 REGISTER_ATTRIBUTE (STANDARD_IS_VIRTUAL);
194 REGISTER_ATTRIBUTE (STANDARD_NAME);
195 REGISTER_ATTRIBUTE (STANDARD_DISPLAY_NAME);
196 REGISTER_ATTRIBUTE (STANDARD_EDIT_NAME);
197 REGISTER_ATTRIBUTE (STANDARD_COPY_NAME);
198 REGISTER_ATTRIBUTE (STANDARD_DESCRIPTION);
199 REGISTER_ATTRIBUTE (STANDARD_ICON);
200 REGISTER_ATTRIBUTE (STANDARD_CONTENT_TYPE);
201 REGISTER_ATTRIBUTE (STANDARD_FAST_CONTENT_TYPE);
202 REGISTER_ATTRIBUTE (STANDARD_SIZE);
203 REGISTER_ATTRIBUTE (STANDARD_ALLOCATED_SIZE);
204 REGISTER_ATTRIBUTE (STANDARD_SYMLINK_TARGET);
205 REGISTER_ATTRIBUTE (STANDARD_TARGET_URI);
206 REGISTER_ATTRIBUTE (STANDARD_SORT_ORDER);
207 REGISTER_ATTRIBUTE (ETAG_VALUE);
208 REGISTER_ATTRIBUTE (ID_FILE);
209 REGISTER_ATTRIBUTE (ID_FILESYSTEM);
210 REGISTER_ATTRIBUTE (ACCESS_CAN_READ);
211 REGISTER_ATTRIBUTE (ACCESS_CAN_WRITE);
212 REGISTER_ATTRIBUTE (ACCESS_CAN_EXECUTE);
213 REGISTER_ATTRIBUTE (ACCESS_CAN_DELETE);
214 REGISTER_ATTRIBUTE (ACCESS_CAN_TRASH);
215 REGISTER_ATTRIBUTE (ACCESS_CAN_RENAME);
216 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_MOUNT);
217 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_UNMOUNT);
218 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_EJECT);
219 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE);
220 REGISTER_ATTRIBUTE (MOUNTABLE_HAL_UDI);
221 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START);
222 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_STOP);
223 REGISTER_ATTRIBUTE (MOUNTABLE_START_STOP_TYPE);
224 REGISTER_ATTRIBUTE (TIME_MODIFIED);
225 REGISTER_ATTRIBUTE (TIME_MODIFIED_USEC);
226 REGISTER_ATTRIBUTE (TIME_ACCESS);
227 REGISTER_ATTRIBUTE (TIME_ACCESS_USEC);
228 REGISTER_ATTRIBUTE (TIME_CHANGED);
229 REGISTER_ATTRIBUTE (TIME_CHANGED_USEC);
230 REGISTER_ATTRIBUTE (TIME_CREATED);
231 REGISTER_ATTRIBUTE (TIME_CREATED_USEC);
232 REGISTER_ATTRIBUTE (UNIX_DEVICE);
233 REGISTER_ATTRIBUTE (UNIX_INODE);
234 REGISTER_ATTRIBUTE (UNIX_MODE);
235 REGISTER_ATTRIBUTE (UNIX_NLINK);
236 REGISTER_ATTRIBUTE (UNIX_UID);
237 REGISTER_ATTRIBUTE (UNIX_GID);
238 REGISTER_ATTRIBUTE (UNIX_RDEV);
239 REGISTER_ATTRIBUTE (UNIX_BLOCK_SIZE);
240 REGISTER_ATTRIBUTE (UNIX_BLOCKS);
241 REGISTER_ATTRIBUTE (UNIX_IS_MOUNTPOINT);
242 REGISTER_ATTRIBUTE (DOS_IS_ARCHIVE);
243 REGISTER_ATTRIBUTE (DOS_IS_SYSTEM);
244 REGISTER_ATTRIBUTE (OWNER_USER);
245 REGISTER_ATTRIBUTE (OWNER_USER_REAL);
246 REGISTER_ATTRIBUTE (OWNER_GROUP);
247 REGISTER_ATTRIBUTE (THUMBNAIL_PATH);
248 REGISTER_ATTRIBUTE (THUMBNAILING_FAILED);
249 REGISTER_ATTRIBUTE (PREVIEW_ICON);
250 REGISTER_ATTRIBUTE (FILESYSTEM_SIZE);
251 REGISTER_ATTRIBUTE (FILESYSTEM_FREE);
252 REGISTER_ATTRIBUTE (FILESYSTEM_TYPE);
253 REGISTER_ATTRIBUTE (FILESYSTEM_READONLY);
254 REGISTER_ATTRIBUTE (FILESYSTEM_USE_PREVIEW);
255 REGISTER_ATTRIBUTE (GVFS_BACKEND);
256 REGISTER_ATTRIBUTE (SELINUX_CONTEXT);
257 REGISTER_ATTRIBUTE (TRASH_ITEM_COUNT);
259 #undef REGISTER_ATTRIBUTE
263 lookup_namespace (const char *namespace)
268 G_LOCK (attribute_hash);
270 ensure_attribute_hash ();
272 ns_info = _lookup_namespace (namespace);
277 G_UNLOCK (attribute_hash);
283 get_attribute_for_id (int attribute)
286 G_LOCK (attribute_hash);
287 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
288 G_UNLOCK (attribute_hash);
293 lookup_attribute (const char *attribute)
297 G_LOCK (attribute_hash);
298 ensure_attribute_hash ();
300 attr_id = _lookup_attribute (attribute);
302 G_UNLOCK (attribute_hash);
308 g_file_info_finalize (GObject *object)
312 GFileAttribute *attrs;
314 info = G_FILE_INFO (object);
316 attrs = (GFileAttribute *)info->attributes->data;
317 for (i = 0; i < info->attributes->len; i++)
318 _g_file_attribute_value_clear (&attrs[i].value);
319 g_array_free (info->attributes, TRUE);
321 if (info->mask != NO_ATTRIBUTE_MASK)
322 g_file_attribute_matcher_unref (info->mask);
324 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
328 g_file_info_class_init (GFileInfoClass *klass)
330 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
332 gobject_class->finalize = g_file_info_finalize;
336 g_file_info_init (GFileInfo *info)
338 info->mask = NO_ATTRIBUTE_MASK;
339 info->attributes = g_array_new (FALSE, FALSE,
340 sizeof (GFileAttribute));
346 * Creates a new file info structure.
348 * Returns: a #GFileInfo.
351 g_file_info_new (void)
353 return g_object_new (G_TYPE_FILE_INFO, NULL);
357 * g_file_info_copy_into:
358 * @src_info: source to copy attributes from.
359 * @dest_info: destination to copy attributes to.
361 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
364 g_file_info_copy_into (GFileInfo *src_info,
365 GFileInfo *dest_info)
367 GFileAttribute *source, *dest;
370 g_return_if_fail (G_IS_FILE_INFO (src_info));
371 g_return_if_fail (G_IS_FILE_INFO (dest_info));
373 dest = (GFileAttribute *)dest_info->attributes->data;
374 for (i = 0; i < dest_info->attributes->len; i++)
375 _g_file_attribute_value_clear (&dest[i].value);
377 g_array_set_size (dest_info->attributes,
378 src_info->attributes->len);
380 source = (GFileAttribute *)src_info->attributes->data;
381 dest = (GFileAttribute *)dest_info->attributes->data;
383 for (i = 0; i < src_info->attributes->len; i++)
385 dest[i].attribute = source[i].attribute;
386 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
387 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
390 if (dest_info->mask != NO_ATTRIBUTE_MASK)
391 g_file_attribute_matcher_unref (dest_info->mask);
393 if (src_info->mask == NO_ATTRIBUTE_MASK)
394 dest_info->mask = NO_ATTRIBUTE_MASK;
396 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
401 * @other: a #GFileInfo.
403 * Duplicates a file info structure.
405 * Returns: a duplicate #GFileInfo of @other.
408 g_file_info_dup (GFileInfo *other)
412 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
414 new = g_file_info_new ();
415 g_file_info_copy_into (other, new);
420 * g_file_info_set_attribute_mask:
421 * @info: a #GFileInfo.
422 * @mask: a #GFileAttributeMatcher.
424 * Sets @mask on @info to match specific attribute types.
427 g_file_info_set_attribute_mask (GFileInfo *info,
428 GFileAttributeMatcher *mask)
430 GFileAttribute *attr;
433 g_return_if_fail (G_IS_FILE_INFO (info));
435 if (mask != info->mask)
437 if (info->mask != NO_ATTRIBUTE_MASK)
438 g_file_attribute_matcher_unref (info->mask);
439 info->mask = g_file_attribute_matcher_ref (mask);
441 /* Remove non-matching attributes */
442 for (i = 0; i < info->attributes->len; i++)
444 attr = &g_array_index (info->attributes, GFileAttribute, i);
445 if (!_g_file_attribute_matcher_matches_id (mask,
448 _g_file_attribute_value_clear (&attr->value);
449 g_array_remove_index (info->attributes, i);
457 * g_file_info_unset_attribute_mask:
460 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
464 g_file_info_unset_attribute_mask (GFileInfo *info)
466 g_return_if_fail (G_IS_FILE_INFO (info));
468 if (info->mask != NO_ATTRIBUTE_MASK)
469 g_file_attribute_matcher_unref (info->mask);
470 info->mask = NO_ATTRIBUTE_MASK;
474 * g_file_info_clear_status:
475 * @info: a #GFileInfo.
477 * Clears the status information from @info.
480 g_file_info_clear_status (GFileInfo *info)
482 GFileAttribute *attrs;
485 g_return_if_fail (G_IS_FILE_INFO (info));
487 attrs = (GFileAttribute *)info->attributes->data;
488 for (i = 0; i < info->attributes->len; i++)
489 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
493 g_file_info_find_place (GFileInfo *info,
497 GFileAttribute *attrs;
498 /* Binary search for the place where attribute would be, if it's
502 max = info->attributes->len;
504 attrs = (GFileAttribute *)info->attributes->data;
508 med = min + (max - min) / 2;
509 if (attrs[med].attribute == attribute)
514 else if (attrs[med].attribute < attribute)
516 else /* attrs[med].attribute > attribute */
523 static GFileAttributeValue *
524 g_file_info_find_value (GFileInfo *info,
527 GFileAttribute *attrs;
530 i = g_file_info_find_place (info, attr_id);
531 attrs = (GFileAttribute *)info->attributes->data;
532 if (i < info->attributes->len &&
533 attrs[i].attribute == attr_id)
534 return &attrs[i].value;
539 static GFileAttributeValue *
540 g_file_info_find_value_by_name (GFileInfo *info,
541 const char *attribute)
545 attr_id = lookup_attribute (attribute);
546 return g_file_info_find_value (info, attr_id);
550 * g_file_info_has_attribute:
551 * @info: a #GFileInfo.
552 * @attribute: a file attribute key.
554 * Checks if a file info structure has an attribute named @attribute.
556 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
560 g_file_info_has_attribute (GFileInfo *info,
561 const char *attribute)
563 GFileAttributeValue *value;
565 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
566 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
568 value = g_file_info_find_value_by_name (info, attribute);
569 return value != NULL;
573 * g_file_info_has_namespace:
574 * @info: a #GFileInfo.
575 * @name_space: a file attribute namespace.
577 * Checks if a file info structure has an attribute in the
578 * specified @name_space.
580 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
586 g_file_info_has_namespace (GFileInfo *info,
587 const char *name_space)
589 GFileAttribute *attrs;
593 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
594 g_return_val_if_fail (name_space != NULL, FALSE);
596 ns_id = lookup_namespace (name_space);
598 attrs = (GFileAttribute *)info->attributes->data;
599 for (i = 0; i < info->attributes->len; i++)
601 if (GET_NS (attrs[i].attribute) == ns_id)
609 * g_file_info_list_attributes:
610 * @info: a #GFileInfo.
611 * @name_space: a file attribute key's namespace.
613 * Lists the file info structure's attributes.
615 * Returns: a null-terminated array of strings of all of the
616 * possible attribute types for the given @name_space, or
620 g_file_info_list_attributes (GFileInfo *info,
621 const char *name_space)
624 GFileAttribute *attrs;
626 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
629 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
631 names = g_ptr_array_new ();
632 attrs = (GFileAttribute *)info->attributes->data;
633 for (i = 0; i < info->attributes->len; i++)
635 attribute = attrs[i].attribute;
636 if (ns_id == 0 || GET_NS (attribute) == ns_id)
637 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
641 g_ptr_array_add (names, NULL);
643 return (char **)g_ptr_array_free (names, FALSE);
647 * g_file_info_get_attribute_type:
648 * @info: a #GFileInfo.
649 * @attribute: a file attribute key.
651 * Gets the attribute type for an attribute key.
653 * Returns: a #GFileAttributeType for the given @attribute, or
654 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
657 g_file_info_get_attribute_type (GFileInfo *info,
658 const char *attribute)
660 GFileAttributeValue *value;
662 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
663 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
665 value = g_file_info_find_value_by_name (info, attribute);
669 return G_FILE_ATTRIBUTE_TYPE_INVALID;
673 * g_file_info_remove_attribute:
674 * @info: a #GFileInfo.
675 * @attribute: a file attribute key.
677 * Removes all cases of @attribute from @info if it exists.
680 g_file_info_remove_attribute (GFileInfo *info,
681 const char *attribute)
684 GFileAttribute *attrs;
687 g_return_if_fail (G_IS_FILE_INFO (info));
688 g_return_if_fail (attribute != NULL && *attribute != '\0');
690 attr_id = lookup_attribute (attribute);
692 i = g_file_info_find_place (info, attr_id);
693 attrs = (GFileAttribute *)info->attributes->data;
694 if (i < info->attributes->len &&
695 attrs[i].attribute == attr_id)
697 _g_file_attribute_value_clear (&attrs[i].value);
698 g_array_remove_index (info->attributes, i);
703 * g_file_info_get_attribute_data:
704 * @info: a #GFileInfo
705 * @attribute: a file attribute key
706 * @type: return location for the attribute type, or %NULL
707 * @value_pp: return location for the attribute value, or %NULL
708 * @status: return location for the attribute status, or %NULL
710 * Gets the attribute type, value and status for an attribute key.
712 * Returns: %TRUE if @info has an attribute named @attribute,
716 g_file_info_get_attribute_data (GFileInfo *info,
717 const char *attribute,
718 GFileAttributeType *type,
720 GFileAttributeStatus *status)
722 GFileAttributeValue *value;
724 value = g_file_info_find_value_by_name (info, attribute);
729 *status = value->status;
735 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
741 * g_file_info_get_attribute_status:
742 * @info: a #GFileInfo
743 * @attribute: a file attribute key
745 * Gets the attribute status for an attribute key.
747 * Returns: a #GFileAttributeStatus for the given @attribute, or
748 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
752 g_file_info_get_attribute_status (GFileInfo *info,
753 const char *attribute)
755 GFileAttributeValue *val;
757 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
758 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
760 val = g_file_info_find_value_by_name (info, attribute);
764 return G_FILE_ATTRIBUTE_STATUS_UNSET;
768 * g_file_info_set_attribute_status:
769 * @info: a #GFileInfo
770 * @attribute: a file attribute key
771 * @status: a #GFileAttributeStatus
773 * Sets the attribute status for an attribute key. This is only
774 * needed by external code that implement g_file_set_attributes_from_info()
775 * or similar functions.
777 * The attribute must exist in @info for this to work. Otherwise %FALSE
778 * is returned and @info is unchanged.
780 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
785 g_file_info_set_attribute_status (GFileInfo *info,
786 const char *attribute,
787 GFileAttributeStatus status)
789 GFileAttributeValue *val;
791 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
792 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
794 val = g_file_info_find_value_by_name (info, attribute);
797 val->status = status;
804 GFileAttributeValue *
805 _g_file_info_get_attribute_value (GFileInfo *info,
806 const char *attribute)
809 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
810 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
812 return g_file_info_find_value_by_name (info, attribute);
816 * g_file_info_get_attribute_as_string:
817 * @info: a #GFileInfo.
818 * @attribute: a file attribute key.
820 * Gets the value of a attribute, formated as a string.
821 * This escapes things as needed to make the string valid
824 * Returns: a UTF-8 string associated with the given @attribute.
825 * When you're done with the string it must be freed with g_free().
828 g_file_info_get_attribute_as_string (GFileInfo *info,
829 const char *attribute)
831 GFileAttributeValue *val;
832 val = _g_file_info_get_attribute_value (info, attribute);
834 return _g_file_attribute_value_as_string (val);
840 * g_file_info_get_attribute_object:
841 * @info: a #GFileInfo.
842 * @attribute: a file attribute key.
844 * Gets the value of a #GObject attribute. If the attribute does
845 * not contain a #GObject, %NULL will be returned.
847 * Returns: a #GObject associated with the given @attribute, or
851 g_file_info_get_attribute_object (GFileInfo *info,
852 const char *attribute)
854 GFileAttributeValue *value;
856 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
857 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
859 value = g_file_info_find_value_by_name (info, attribute);
860 return _g_file_attribute_value_get_object (value);
864 * g_file_info_get_attribute_string:
865 * @info: a #GFileInfo.
866 * @attribute: a file attribute key.
868 * Gets the value of a string attribute. If the attribute does
869 * not contain a string, %NULL will be returned.
871 * Returns: the contents of the @attribute value as a string, or
875 g_file_info_get_attribute_string (GFileInfo *info,
876 const char *attribute)
878 GFileAttributeValue *value;
880 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
881 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
883 value = g_file_info_find_value_by_name (info, attribute);
884 return _g_file_attribute_value_get_string (value);
888 * g_file_info_get_attribute_byte_string:
889 * @info: a #GFileInfo.
890 * @attribute: a file attribute key.
892 * Gets the value of a byte string attribute. If the attribute does
893 * not contain a byte string, %NULL will be returned.
895 * Returns: the contents of the @attribute value as a byte string, or
899 g_file_info_get_attribute_byte_string (GFileInfo *info,
900 const char *attribute)
902 GFileAttributeValue *value;
904 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
905 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
907 value = g_file_info_find_value_by_name (info, attribute);
908 return _g_file_attribute_value_get_byte_string (value);
912 * g_file_info_get_attribute_stringv:
913 * @info: a #GFileInfo.
914 * @attribute: a file attribute key.
916 * Gets the value of a stringv attribute. If the attribute does
917 * not contain a stringv, %NULL will be returned.
919 * Returns: the contents of the @attribute value as a stringv, or
920 * %NULL otherwise. Do not free.
923 g_file_info_get_attribute_stringv (GFileInfo *info,
924 const char *attribute)
926 GFileAttributeValue *value;
928 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
929 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
931 value = g_file_info_find_value_by_name (info, attribute);
932 return _g_file_attribute_value_get_stringv (value);
936 * g_file_info_get_attribute_boolean:
937 * @info: a #GFileInfo.
938 * @attribute: a file attribute key.
940 * Gets the value of a boolean attribute. If the attribute does not
941 * contain a boolean value, %FALSE will be returned.
943 * Returns: the boolean value contained within the attribute.
946 g_file_info_get_attribute_boolean (GFileInfo *info,
947 const char *attribute)
949 GFileAttributeValue *value;
951 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
952 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
954 value = g_file_info_find_value_by_name (info, attribute);
955 return _g_file_attribute_value_get_boolean (value);
959 * g_file_info_get_attribute_uint32:
960 * @info: a #GFileInfo.
961 * @attribute: a file attribute key.
963 * Gets an unsigned 32-bit integer contained within the attribute. If the
964 * attribute does not contain an unsigned 32-bit integer, or is invalid,
965 * 0 will be returned.
967 * Returns: an unsigned 32-bit integer from the attribute.
970 g_file_info_get_attribute_uint32 (GFileInfo *info,
971 const char *attribute)
973 GFileAttributeValue *value;
975 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
976 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
978 value = g_file_info_find_value_by_name (info, attribute);
979 return _g_file_attribute_value_get_uint32 (value);
983 * g_file_info_get_attribute_int32:
984 * @info: a #GFileInfo.
985 * @attribute: a file attribute key.
987 * Gets a signed 32-bit integer contained within the attribute. If the
988 * attribute does not contain a signed 32-bit integer, or is invalid,
989 * 0 will be returned.
991 * Returns: a signed 32-bit integer from the attribute.
994 g_file_info_get_attribute_int32 (GFileInfo *info,
995 const char *attribute)
997 GFileAttributeValue *value;
999 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1000 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1002 value = g_file_info_find_value_by_name (info, attribute);
1003 return _g_file_attribute_value_get_int32 (value);
1007 * g_file_info_get_attribute_uint64:
1008 * @info: a #GFileInfo.
1009 * @attribute: a file attribute key.
1011 * Gets a unsigned 64-bit integer contained within the attribute. If the
1012 * attribute does not contain an unsigned 64-bit integer, or is invalid,
1013 * 0 will be returned.
1015 * Returns: a unsigned 64-bit integer from the attribute.
1018 g_file_info_get_attribute_uint64 (GFileInfo *info,
1019 const char *attribute)
1021 GFileAttributeValue *value;
1023 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1024 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1026 value = g_file_info_find_value_by_name (info, attribute);
1027 return _g_file_attribute_value_get_uint64 (value);
1031 * g_file_info_get_attribute_int64:
1032 * @info: a #GFileInfo.
1033 * @attribute: a file attribute key.
1035 * Gets a signed 64-bit integer contained within the attribute. If the
1036 * attribute does not contain an signed 64-bit integer, or is invalid,
1037 * 0 will be returned.
1039 * Returns: a signed 64-bit integer from the attribute.
1042 g_file_info_get_attribute_int64 (GFileInfo *info,
1043 const char *attribute)
1045 GFileAttributeValue *value;
1047 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1048 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1050 value = g_file_info_find_value_by_name (info, attribute);
1051 return _g_file_attribute_value_get_int64 (value);
1054 static GFileAttributeValue *
1055 g_file_info_create_value (GFileInfo *info,
1058 GFileAttribute *attrs;
1061 if (info->mask != NO_ATTRIBUTE_MASK &&
1062 !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
1065 i = g_file_info_find_place (info, attr_id);
1067 attrs = (GFileAttribute *)info->attributes->data;
1068 if (i < info->attributes->len &&
1069 attrs[i].attribute == attr_id)
1070 return &attrs[i].value;
1073 GFileAttribute attr = { 0 };
1074 attr.attribute = attr_id;
1075 g_array_insert_val (info->attributes, i, attr);
1077 attrs = (GFileAttribute *)info->attributes->data;
1078 return &attrs[i].value;
1083 _g_file_info_set_attribute_by_id (GFileInfo *info,
1085 GFileAttributeType type,
1088 GFileAttributeValue *value;
1090 value = g_file_info_create_value (info, attribute);
1093 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1097 * g_file_info_set_attribute:
1098 * @info: a #GFileInfo.
1099 * @attribute: a file attribute key.
1100 * @type: a #GFileAttributeType
1101 * @value_p: pointer to the value
1103 * Sets the @attribute to contain the given value, if possible.
1106 g_file_info_set_attribute (GFileInfo *info,
1107 const char *attribute,
1108 GFileAttributeType type,
1111 g_return_if_fail (G_IS_FILE_INFO (info));
1112 g_return_if_fail (attribute != NULL && *attribute != '\0');
1114 _g_file_info_set_attribute_by_id (info, lookup_attribute (attribute), type, value_p);
1118 _g_file_info_set_attribute_object_by_id (GFileInfo *info,
1120 GObject *attr_value)
1122 GFileAttributeValue *value;
1124 value = g_file_info_create_value (info, attribute);
1126 _g_file_attribute_value_set_object (value, attr_value);
1130 * g_file_info_set_attribute_object:
1131 * @info: a #GFileInfo.
1132 * @attribute: a file attribute key.
1133 * @attr_value: a #GObject.
1135 * Sets the @attribute to contain the given @attr_value,
1139 g_file_info_set_attribute_object (GFileInfo *info,
1140 const char *attribute,
1141 GObject *attr_value)
1143 g_return_if_fail (G_IS_FILE_INFO (info));
1144 g_return_if_fail (attribute != NULL && *attribute != '\0');
1145 g_return_if_fail (G_IS_OBJECT (attr_value));
1147 _g_file_info_set_attribute_object_by_id (info,
1148 lookup_attribute (attribute),
1153 _g_file_info_set_attribute_stringv_by_id (GFileInfo *info,
1157 GFileAttributeValue *value;
1159 value = g_file_info_create_value (info, attribute);
1161 _g_file_attribute_value_set_stringv (value, attr_value);
1165 * g_file_info_set_attribute_stringv:
1166 * @info: a #GFileInfo.
1167 * @attribute: a file attribute key.
1168 * @attr_value: a %NULL terminated string array
1170 * Sets the @attribute to contain the given @attr_value,
1176 g_file_info_set_attribute_stringv (GFileInfo *info,
1177 const char *attribute,
1180 g_return_if_fail (G_IS_FILE_INFO (info));
1181 g_return_if_fail (attribute != NULL && *attribute != '\0');
1182 g_return_if_fail (attr_value != NULL);
1184 _g_file_info_set_attribute_stringv_by_id (info,
1185 lookup_attribute (attribute),
1190 _g_file_info_set_attribute_string_by_id (GFileInfo *info,
1192 const char *attr_value)
1194 GFileAttributeValue *value;
1196 value = g_file_info_create_value (info, attribute);
1198 _g_file_attribute_value_set_string (value, attr_value);
1202 * g_file_info_set_attribute_string:
1203 * @info: a #GFileInfo.
1204 * @attribute: a file attribute key.
1205 * @attr_value: a string.
1207 * Sets the @attribute to contain the given @attr_value,
1211 g_file_info_set_attribute_string (GFileInfo *info,
1212 const char *attribute,
1213 const char *attr_value)
1215 g_return_if_fail (G_IS_FILE_INFO (info));
1216 g_return_if_fail (attribute != NULL && *attribute != '\0');
1217 g_return_if_fail (attr_value != NULL);
1219 _g_file_info_set_attribute_string_by_id (info,
1220 lookup_attribute (attribute),
1225 _g_file_info_set_attribute_byte_string_by_id (GFileInfo *info,
1227 const char *attr_value)
1229 GFileAttributeValue *value;
1231 value = g_file_info_create_value (info, attribute);
1233 _g_file_attribute_value_set_byte_string (value, attr_value);
1237 * g_file_info_set_attribute_byte_string:
1238 * @info: a #GFileInfo.
1239 * @attribute: a file attribute key.
1240 * @attr_value: a byte string.
1242 * Sets the @attribute to contain the given @attr_value,
1246 g_file_info_set_attribute_byte_string (GFileInfo *info,
1247 const char *attribute,
1248 const char *attr_value)
1250 g_return_if_fail (G_IS_FILE_INFO (info));
1251 g_return_if_fail (attribute != NULL && *attribute != '\0');
1252 g_return_if_fail (attr_value != NULL);
1254 _g_file_info_set_attribute_byte_string_by_id (info,
1255 lookup_attribute (attribute),
1260 _g_file_info_set_attribute_boolean_by_id (GFileInfo *info,
1262 gboolean attr_value)
1264 GFileAttributeValue *value;
1266 value = g_file_info_create_value (info, attribute);
1268 _g_file_attribute_value_set_boolean (value, attr_value);
1272 * g_file_info_set_attribute_boolean:
1273 * @info: a #GFileInfo.
1274 * @attribute: a file attribute key.
1275 * @attr_value: a boolean value.
1277 * Sets the @attribute to contain the given @attr_value,
1281 g_file_info_set_attribute_boolean (GFileInfo *info,
1282 const char *attribute,
1283 gboolean attr_value)
1285 g_return_if_fail (G_IS_FILE_INFO (info));
1286 g_return_if_fail (attribute != NULL && *attribute != '\0');
1288 _g_file_info_set_attribute_boolean_by_id (info,
1289 lookup_attribute (attribute),
1294 _g_file_info_set_attribute_uint32_by_id (GFileInfo *info,
1298 GFileAttributeValue *value;
1300 value = g_file_info_create_value (info, attribute);
1302 _g_file_attribute_value_set_uint32 (value, attr_value);
1306 * g_file_info_set_attribute_uint32:
1307 * @info: a #GFileInfo.
1308 * @attribute: a file attribute key.
1309 * @attr_value: an unsigned 32-bit integer.
1311 * Sets the @attribute to contain the given @attr_value,
1315 g_file_info_set_attribute_uint32 (GFileInfo *info,
1316 const char *attribute,
1319 g_return_if_fail (G_IS_FILE_INFO (info));
1320 g_return_if_fail (attribute != NULL && *attribute != '\0');
1322 _g_file_info_set_attribute_uint32_by_id (info,
1323 lookup_attribute (attribute),
1328 _g_file_info_set_attribute_int32_by_id (GFileInfo *info,
1332 GFileAttributeValue *value;
1334 value = g_file_info_create_value (info, attribute);
1336 _g_file_attribute_value_set_int32 (value, attr_value);
1340 * g_file_info_set_attribute_int32:
1341 * @info: a #GFileInfo.
1342 * @attribute: a file attribute key.
1343 * @attr_value: a signed 32-bit integer
1345 * Sets the @attribute to contain the given @attr_value,
1349 g_file_info_set_attribute_int32 (GFileInfo *info,
1350 const char *attribute,
1353 g_return_if_fail (G_IS_FILE_INFO (info));
1354 g_return_if_fail (attribute != NULL && *attribute != '\0');
1356 _g_file_info_set_attribute_int32_by_id (info,
1357 lookup_attribute (attribute),
1362 _g_file_info_set_attribute_uint64_by_id (GFileInfo *info,
1366 GFileAttributeValue *value;
1368 value = g_file_info_create_value (info, attribute);
1370 _g_file_attribute_value_set_uint64 (value, attr_value);
1374 * g_file_info_set_attribute_uint64:
1375 * @info: a #GFileInfo.
1376 * @attribute: a file attribute key.
1377 * @attr_value: an unsigned 64-bit integer.
1379 * Sets the @attribute to contain the given @attr_value,
1383 g_file_info_set_attribute_uint64 (GFileInfo *info,
1384 const char *attribute,
1387 g_return_if_fail (G_IS_FILE_INFO (info));
1388 g_return_if_fail (attribute != NULL && *attribute != '\0');
1390 _g_file_info_set_attribute_uint64_by_id (info,
1391 lookup_attribute (attribute),
1396 _g_file_info_set_attribute_int64_by_id (GFileInfo *info,
1400 GFileAttributeValue *value;
1402 value = g_file_info_create_value (info, attribute);
1404 _g_file_attribute_value_set_int64 (value, attr_value);
1408 * g_file_info_set_attribute_int64:
1409 * @info: a #GFileInfo.
1410 * @attribute: attribute name to set.
1411 * @attr_value: int64 value to set attribute to.
1413 * Sets the @attribute to contain the given @attr_value,
1418 g_file_info_set_attribute_int64 (GFileInfo *info,
1419 const char *attribute,
1422 g_return_if_fail (G_IS_FILE_INFO (info));
1423 g_return_if_fail (attribute != NULL && *attribute != '\0');
1425 _g_file_info_set_attribute_int64_by_id (info,
1426 lookup_attribute (attribute),
1430 /* Helper getters */
1432 * g_file_info_get_file_type:
1433 * @info: a #GFileInfo.
1435 * Gets a file's type (whether it is a regular file, symlink, etc).
1436 * This is different from the file's content type, see g_file_info_get_content_type().
1438 * Returns: a #GFileType for the given file.
1441 g_file_info_get_file_type (GFileInfo *info)
1443 static guint32 attr = 0;
1444 GFileAttributeValue *value;
1446 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1449 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1451 value = g_file_info_find_value (info, attr);
1452 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1456 * g_file_info_get_is_hidden:
1457 * @info: a #GFileInfo.
1459 * Checks if a file is hidden.
1461 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1464 g_file_info_get_is_hidden (GFileInfo *info)
1466 static guint32 attr = 0;
1467 GFileAttributeValue *value;
1469 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1472 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1474 value = g_file_info_find_value (info, attr);
1475 return (GFileType)_g_file_attribute_value_get_boolean (value);
1479 * g_file_info_get_is_backup:
1480 * @info: a #GFileInfo.
1482 * Checks if a file is a backup file.
1484 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1487 g_file_info_get_is_backup (GFileInfo *info)
1489 static guint32 attr = 0;
1490 GFileAttributeValue *value;
1492 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1495 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1497 value = g_file_info_find_value (info, attr);
1498 return (GFileType)_g_file_attribute_value_get_boolean (value);
1502 * g_file_info_get_is_symlink:
1503 * @info: a #GFileInfo.
1505 * Checks if a file is a symlink.
1507 * Returns: %TRUE if the given @info is a symlink.
1510 g_file_info_get_is_symlink (GFileInfo *info)
1512 static guint32 attr = 0;
1513 GFileAttributeValue *value;
1515 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1518 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1520 value = g_file_info_find_value (info, attr);
1521 return (GFileType)_g_file_attribute_value_get_boolean (value);
1525 * g_file_info_get_name:
1526 * @info: a #GFileInfo.
1528 * Gets the name for a file.
1530 * Returns: a string containing the file name.
1533 g_file_info_get_name (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_NAME);
1543 value = g_file_info_find_value (info, attr);
1544 return _g_file_attribute_value_get_byte_string (value);
1548 * g_file_info_get_display_name:
1549 * @info: a #GFileInfo.
1551 * Gets a display name for a file.
1553 * Returns: a string containing the display name.
1556 g_file_info_get_display_name (GFileInfo *info)
1558 static guint32 attr = 0;
1559 GFileAttributeValue *value;
1561 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1564 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1566 value = g_file_info_find_value (info, attr);
1567 return _g_file_attribute_value_get_string (value);
1571 * g_file_info_get_edit_name:
1572 * @info: a #GFileInfo.
1574 * Gets the edit name for a file.
1576 * Returns: a string containing the edit name.
1579 g_file_info_get_edit_name (GFileInfo *info)
1581 static guint32 attr = 0;
1582 GFileAttributeValue *value;
1584 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1587 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1589 value = g_file_info_find_value (info, attr);
1590 return _g_file_attribute_value_get_string (value);
1594 * g_file_info_get_icon:
1595 * @info: a #GFileInfo.
1597 * Gets the icon for a file.
1599 * Returns: #GIcon for the given @info.
1602 g_file_info_get_icon (GFileInfo *info)
1604 static guint32 attr = 0;
1605 GFileAttributeValue *value;
1608 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1611 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1613 value = g_file_info_find_value (info, attr);
1614 obj = _g_file_attribute_value_get_object (value);
1615 if (G_IS_ICON (obj))
1616 return G_ICON (obj);
1621 * g_file_info_get_content_type:
1622 * @info: a #GFileInfo.
1624 * Gets the file's content type.
1626 * Returns: a string containing the file's content type.
1629 g_file_info_get_content_type (GFileInfo *info)
1631 static guint32 attr = 0;
1632 GFileAttributeValue *value;
1634 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1637 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1639 value = g_file_info_find_value (info, attr);
1640 return _g_file_attribute_value_get_string (value);
1644 * g_file_info_get_size:
1645 * @info: a #GFileInfo.
1647 * Gets the file's size.
1649 * Returns: a #goffset containing the file's size.
1652 g_file_info_get_size (GFileInfo *info)
1654 static guint32 attr = 0;
1655 GFileAttributeValue *value;
1657 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1660 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1662 value = g_file_info_find_value (info, attr);
1663 return (goffset) _g_file_attribute_value_get_uint64 (value);
1667 * g_file_info_get_modification_time:
1668 * @info: a #GFileInfo.
1669 * @result: a #GTimeVal.
1671 * Gets the modification time of the current @info and sets it
1675 g_file_info_get_modification_time (GFileInfo *info,
1678 static guint32 attr_mtime = 0, attr_mtime_usec;
1679 GFileAttributeValue *value;
1681 g_return_if_fail (G_IS_FILE_INFO (info));
1682 g_return_if_fail (result != NULL);
1684 if (attr_mtime == 0)
1686 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1687 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1690 value = g_file_info_find_value (info, attr_mtime);
1691 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1692 value = g_file_info_find_value (info, attr_mtime_usec);
1693 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1697 * g_file_info_get_symlink_target:
1698 * @info: a #GFileInfo.
1700 * Gets the symlink target for a given #GFileInfo.
1702 * Returns: a string containing the symlink target.
1705 g_file_info_get_symlink_target (GFileInfo *info)
1707 static guint32 attr = 0;
1708 GFileAttributeValue *value;
1710 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1713 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1715 value = g_file_info_find_value (info, attr);
1716 return _g_file_attribute_value_get_byte_string (value);
1720 * g_file_info_get_etag:
1721 * @info: a #GFileInfo.
1723 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1724 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1726 * Returns: a string containing the value of the "etag:value" attribute.
1729 g_file_info_get_etag (GFileInfo *info)
1731 static guint32 attr = 0;
1732 GFileAttributeValue *value;
1734 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1737 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1739 value = g_file_info_find_value (info, attr);
1740 return _g_file_attribute_value_get_string (value);
1744 * g_file_info_get_sort_order:
1745 * @info: a #GFileInfo.
1747 * Gets the value of the sort_order attribute from the #GFileInfo.
1748 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1750 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1753 g_file_info_get_sort_order (GFileInfo *info)
1755 static guint32 attr = 0;
1756 GFileAttributeValue *value;
1758 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1761 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1763 value = g_file_info_find_value (info, attr);
1764 return _g_file_attribute_value_get_int32 (value);
1767 /* Helper setters: */
1769 * g_file_info_set_file_type:
1770 * @info: a #GFileInfo.
1771 * @type: a #GFileType.
1773 * Sets the file type in a #GFileInfo to @type.
1774 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1777 g_file_info_set_file_type (GFileInfo *info,
1780 static guint32 attr = 0;
1781 GFileAttributeValue *value;
1783 g_return_if_fail (G_IS_FILE_INFO (info));
1786 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1788 value = g_file_info_create_value (info, attr);
1790 _g_file_attribute_value_set_uint32 (value, type);
1794 * g_file_info_set_is_hidden:
1795 * @info: a #GFileInfo.
1796 * @is_hidden: a #gboolean.
1798 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1799 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1802 g_file_info_set_is_hidden (GFileInfo *info,
1805 static guint32 attr = 0;
1806 GFileAttributeValue *value;
1808 g_return_if_fail (G_IS_FILE_INFO (info));
1811 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1813 value = g_file_info_create_value (info, attr);
1815 _g_file_attribute_value_set_boolean (value, is_hidden);
1819 * g_file_info_set_is_symlink:
1820 * @info: a #GFileInfo.
1821 * @is_symlink: a #gboolean.
1823 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1824 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1827 g_file_info_set_is_symlink (GFileInfo *info,
1828 gboolean is_symlink)
1830 static guint32 attr = 0;
1831 GFileAttributeValue *value;
1833 g_return_if_fail (G_IS_FILE_INFO (info));
1836 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1838 value = g_file_info_create_value (info, attr);
1840 _g_file_attribute_value_set_boolean (value, is_symlink);
1844 * g_file_info_set_name:
1845 * @info: a #GFileInfo.
1846 * @name: a string containing a name.
1848 * Sets the name attribute for the current #GFileInfo.
1849 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1852 g_file_info_set_name (GFileInfo *info,
1855 static guint32 attr = 0;
1856 GFileAttributeValue *value;
1858 g_return_if_fail (G_IS_FILE_INFO (info));
1859 g_return_if_fail (name != NULL);
1862 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1864 value = g_file_info_create_value (info, attr);
1866 _g_file_attribute_value_set_byte_string (value, name);
1870 * g_file_info_set_display_name:
1871 * @info: a #GFileInfo.
1872 * @display_name: a string containing a display name.
1874 * Sets the display name for the current #GFileInfo.
1875 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1878 g_file_info_set_display_name (GFileInfo *info,
1879 const char *display_name)
1881 static guint32 attr = 0;
1882 GFileAttributeValue *value;
1884 g_return_if_fail (G_IS_FILE_INFO (info));
1885 g_return_if_fail (display_name != NULL);
1888 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1890 value = g_file_info_create_value (info, attr);
1892 _g_file_attribute_value_set_string (value, display_name);
1896 * g_file_info_set_edit_name:
1897 * @info: a #GFileInfo.
1898 * @edit_name: a string containing an edit name.
1900 * Sets the edit name for the current file.
1901 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1904 g_file_info_set_edit_name (GFileInfo *info,
1905 const char *edit_name)
1907 static guint32 attr = 0;
1908 GFileAttributeValue *value;
1910 g_return_if_fail (G_IS_FILE_INFO (info));
1911 g_return_if_fail (edit_name != NULL);
1914 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1916 value = g_file_info_create_value (info, attr);
1918 _g_file_attribute_value_set_string (value, edit_name);
1922 * g_file_info_set_icon:
1923 * @info: a #GFileInfo.
1926 * Sets the icon for a given #GFileInfo.
1927 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1930 g_file_info_set_icon (GFileInfo *info,
1933 static guint32 attr = 0;
1934 GFileAttributeValue *value;
1936 g_return_if_fail (G_IS_FILE_INFO (info));
1937 g_return_if_fail (G_IS_ICON (icon));
1940 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1942 value = g_file_info_create_value (info, attr);
1944 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1948 * g_file_info_set_content_type:
1949 * @info: a #GFileInfo.
1950 * @content_type: a content type. See #GContentType.
1952 * Sets the content type attribute for a given #GFileInfo.
1953 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1956 g_file_info_set_content_type (GFileInfo *info,
1957 const char *content_type)
1959 static guint32 attr = 0;
1960 GFileAttributeValue *value;
1962 g_return_if_fail (G_IS_FILE_INFO (info));
1963 g_return_if_fail (content_type != NULL);
1966 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1968 value = g_file_info_create_value (info, attr);
1970 _g_file_attribute_value_set_string (value, content_type);
1974 * g_file_info_set_size:
1975 * @info: a #GFileInfo.
1976 * @size: a #goffset containing the file's size.
1978 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1979 * to the given size.
1982 g_file_info_set_size (GFileInfo *info,
1985 static guint32 attr = 0;
1986 GFileAttributeValue *value;
1988 g_return_if_fail (G_IS_FILE_INFO (info));
1991 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1993 value = g_file_info_create_value (info, attr);
1995 _g_file_attribute_value_set_uint64 (value, size);
1999 * g_file_info_set_modification_time
2000 * @info: a #GFileInfo.
2001 * @mtime: a #GTimeVal.
2003 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
2004 * info to the given time value.
2007 g_file_info_set_modification_time (GFileInfo *info,
2010 static guint32 attr_mtime = 0, attr_mtime_usec;
2011 GFileAttributeValue *value;
2013 g_return_if_fail (G_IS_FILE_INFO (info));
2014 g_return_if_fail (mtime != NULL);
2016 if (attr_mtime == 0)
2018 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
2019 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2022 value = g_file_info_create_value (info, attr_mtime);
2024 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
2025 value = g_file_info_create_value (info, attr_mtime_usec);
2027 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
2031 * g_file_info_set_symlink_target:
2032 * @info: a #GFileInfo.
2033 * @symlink_target: a static string containing a path to a symlink target.
2035 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
2036 * to the given symlink target.
2039 g_file_info_set_symlink_target (GFileInfo *info,
2040 const char *symlink_target)
2042 static guint32 attr = 0;
2043 GFileAttributeValue *value;
2045 g_return_if_fail (G_IS_FILE_INFO (info));
2046 g_return_if_fail (symlink_target != NULL);
2049 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2051 value = g_file_info_create_value (info, attr);
2053 _g_file_attribute_value_set_byte_string (value, symlink_target);
2057 * g_file_info_set_sort_order:
2058 * @info: a #GFileInfo.
2059 * @sort_order: a sort order integer.
2061 * Sets the sort order attribute in the file info structure. See
2062 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
2065 g_file_info_set_sort_order (GFileInfo *info,
2068 static guint32 attr = 0;
2069 GFileAttributeValue *value;
2071 g_return_if_fail (G_IS_FILE_INFO (info));
2074 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
2076 value = g_file_info_create_value (info, attr);
2078 _g_file_attribute_value_set_int32 (value, sort_order);
2082 #define ON_STACK_MATCHERS 5
2089 struct _GFileAttributeMatcher {
2091 SubMatcher sub_matchers[ON_STACK_MATCHERS];
2092 GArray *more_sub_matchers;
2095 guint32 iterator_ns;
2101 matcher_add (GFileAttributeMatcher *matcher,
2105 SubMatcher *sub_matchers;
2109 for (i = 0; i < ON_STACK_MATCHERS; i++)
2111 /* First empty spot, not found, use this */
2112 if (matcher->sub_matchers[i].id == 0)
2114 matcher->sub_matchers[i].id = id;
2115 matcher->sub_matchers[i].mask = mask;
2120 if (matcher->sub_matchers[i].id == id &&
2121 matcher->sub_matchers[i].mask == mask)
2125 if (matcher->more_sub_matchers == NULL)
2126 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2128 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2129 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2132 if (sub_matchers[i].id == id &&
2133 sub_matchers[i].mask == mask)
2140 g_array_append_val (matcher->more_sub_matchers, s);
2144 * g_file_attribute_matcher_new:
2145 * @attributes: an attribute string to match.
2147 * Creates a new file attribute matcher, which matches attributes
2148 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
2149 * counted structures, and are created with a reference count of 1. If
2150 * the number of references falls to 0, the #GFileAttributeMatcher is
2151 * automatically destroyed.
2153 * The @attribute string should be formatted with specific keys separated
2154 * from namespaces with a double colon. Several "namespace::key" strings may be
2155 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
2156 * The wildcard "*" may be used to match all keys and namespaces, or
2157 * "namespace::*" will match all keys in a given namespace.
2159 * Examples of strings to use:
2161 * <title>File Attribute Matcher strings and results</title>
2162 * <tgroup cols='2' align='left'><thead>
2163 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
2165 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
2166 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
2167 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
2168 * all keys in the unix namespace.</entry></row>
2172 * Returns: a #GFileAttributeMatcher.
2174 GFileAttributeMatcher *
2175 g_file_attribute_matcher_new (const char *attributes)
2180 GFileAttributeMatcher *matcher;
2182 if (attributes == NULL || *attributes == '\0')
2185 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2188 split = g_strsplit (attributes, ",", -1);
2190 for (i = 0; split[i] != NULL; i++)
2192 if (strcmp (split[i], "*") == 0)
2193 matcher->all = TRUE;
2198 colon = strstr (split[i], "::");
2199 if (colon != NULL &&
2204 id = lookup_attribute (split[i]);
2212 id = lookup_namespace (split[i]) << NS_POS;
2213 mask = NS_MASK << NS_POS;
2216 matcher_add (matcher, id, mask);
2226 * g_file_attribute_matcher_ref:
2227 * @matcher: a #GFileAttributeMatcher.
2229 * References a file attribute matcher.
2231 * Returns: a #GFileAttributeMatcher.
2233 GFileAttributeMatcher *
2234 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2238 g_return_val_if_fail (matcher->ref > 0, NULL);
2239 g_atomic_int_inc (&matcher->ref);
2245 * g_file_attribute_matcher_unref:
2246 * @matcher: a #GFileAttributeMatcher.
2248 * Unreferences @matcher. If the reference count falls below 1,
2249 * the @matcher is automatically freed.
2253 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2257 g_return_if_fail (matcher->ref > 0);
2259 if (g_atomic_int_dec_and_test (&matcher->ref))
2261 if (matcher->more_sub_matchers)
2262 g_array_free (matcher->more_sub_matchers, TRUE);
2270 * g_file_attribute_matcher_matches_only:
2271 * @matcher: a #GFileAttributeMatcher.
2272 * @attribute: a file attribute key.
2274 * Checks if a attribute matcher only matches a given attribute. Always
2275 * returns %FALSE if "*" was used when creating the matcher.
2277 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2280 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2281 const char *attribute)
2285 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2287 if (matcher == NULL ||
2291 id = lookup_attribute (attribute);
2293 if (matcher->sub_matchers[0].id != 0 &&
2294 matcher->sub_matchers[1].id == 0 &&
2295 matcher->sub_matchers[0].mask == 0xffffffff &&
2296 matcher->sub_matchers[0].id == id)
2303 matcher_matches_id (GFileAttributeMatcher *matcher,
2306 SubMatcher *sub_matchers;
2309 for (i = 0; i < ON_STACK_MATCHERS; i++)
2311 if (matcher->sub_matchers[i].id == 0)
2314 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2318 if (matcher->more_sub_matchers)
2320 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2321 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2323 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2332 _g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2335 /* We return a NULL matcher for an empty match string, so handle this */
2336 if (matcher == NULL)
2342 return matcher_matches_id (matcher, id);
2346 * g_file_attribute_matcher_matches:
2347 * @matcher: a #GFileAttributeMatcher.
2348 * @attribute: a file attribute key.
2350 * Checks if an attribute will be matched by an attribute matcher. If
2351 * the matcher was created with the "*" matching string, this function
2352 * will always return %TRUE.
2354 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2357 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2358 const char *attribute)
2360 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2362 /* We return a NULL matcher for an empty match string, so handle this */
2363 if (matcher == NULL)
2369 return matcher_matches_id (matcher, lookup_attribute (attribute));
2372 /* return TRUE -> all */
2374 * g_file_attribute_matcher_enumerate_namespace:
2375 * @matcher: a #GFileAttributeMatcher.
2376 * @ns: a string containing a file attribute namespace.
2378 * Checks if the matcher will match all of the keys in a given namespace.
2379 * This will always return %TRUE if a wildcard character is in use (e.g. if
2380 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2381 * using "*" and namespace is anything.)
2383 * TODO: this is awkwardly worded.
2385 * Returns: %TRUE if the matcher matches all of the entries
2386 * in the given @ns, %FALSE otherwise.
2389 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2392 SubMatcher *sub_matchers;
2396 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2398 /* We return a NULL matcher for an empty match string, so handle this */
2399 if (matcher == NULL)
2405 ns_id = lookup_namespace (ns) << NS_POS;
2407 for (i = 0; i < ON_STACK_MATCHERS; i++)
2409 if (matcher->sub_matchers[i].id == ns_id)
2413 if (matcher->more_sub_matchers)
2415 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2416 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2418 if (sub_matchers[i].id == ns_id)
2423 matcher->iterator_ns = ns_id;
2424 matcher->iterator_pos = 0;
2430 * g_file_attribute_matcher_enumerate_next:
2431 * @matcher: a #GFileAttributeMatcher.
2433 * Gets the next matched attribute from a #GFileAttributeMatcher.
2435 * Returns: a string containing the next attribute or %NULL if
2436 * no more attribute exist.
2439 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2442 SubMatcher *sub_matcher;
2444 /* We return a NULL matcher for an empty match string, so handle this */
2445 if (matcher == NULL)
2450 i = matcher->iterator_pos++;
2452 if (i < ON_STACK_MATCHERS)
2454 if (matcher->sub_matchers[i].id == 0)
2457 sub_matcher = &matcher->sub_matchers[i];
2461 if (matcher->more_sub_matchers == NULL)
2464 i -= ON_STACK_MATCHERS;
2465 if (i < matcher->more_sub_matchers->len)
2466 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2471 if (sub_matcher->mask == 0xffffffff &&
2472 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2473 return get_attribute_for_id (sub_matcher->id);
2477 #define __G_FILE_INFO_C__
2478 #include "gioaliasdef.c"