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 static gboolean g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
92 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
96 guint32 attribute_id_counter;
99 G_LOCK_DEFINE_STATIC (attribute_hash);
100 static int namespace_id_counter = 0;
101 static GHashTable *ns_hash = NULL;
102 static GHashTable *attribute_hash = NULL;
103 static char ***attributes = NULL;
105 /* Attribute ids are 32bit, we split it up like this:
106 * |------------|--------------------|
108 * namespace attribute id
110 * This way the attributes gets sorted in namespace order
114 #define NS_MASK ((guint32)((1<<12) - 1))
116 #define ID_MASK ((guint32)((1<<20) - 1))
118 #define GET_NS(_attr_id) \
119 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
120 #define GET_ID(_attr_id) \
121 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
123 #define MAKE_ATTR_ID(_ns, _id) \
124 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
125 ((((guint32) _id) & ID_MASK) << ID_POS) )
128 _lookup_namespace (const char *namespace)
132 ns_info = g_hash_table_lookup (ns_hash, namespace);
135 ns_info = g_new0 (NSInfo, 1);
136 ns_info->id = ++namespace_id_counter;
137 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
138 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
139 attributes[ns_info->id] = NULL;
145 _lookup_attribute (const char *attribute)
152 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
157 colon = strstr (attribute, "::");
159 ns = g_strndup (attribute, colon - attribute);
163 ns_info = _lookup_namespace (ns);
166 id = ++ns_info->attribute_id_counter;
167 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
168 attributes[ns_info->id][id] = g_strdup (attribute);
170 attr_id = MAKE_ATTR_ID (ns_info->id, id);
172 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
178 ensure_attribute_hash (void)
180 if (attribute_hash != NULL)
183 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
184 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
186 #define REGISTER_ATTRIBUTE(name) G_STMT_START{\
187 guint _u = _lookup_attribute (G_FILE_ATTRIBUTE_ ## name); \
188 /* use for generating the ID: g_print ("#define G_FILE_ATTRIBUTE_ID_%s (%u + %u)\n", #name + 17, _u & ~ID_MASK, _u & ID_MASK); */ \
189 g_assert (_u == G_FILE_ATTRIBUTE_ID_ ## name); \
192 REGISTER_ATTRIBUTE (STANDARD_TYPE);
193 REGISTER_ATTRIBUTE (STANDARD_IS_HIDDEN);
194 REGISTER_ATTRIBUTE (STANDARD_IS_BACKUP);
195 REGISTER_ATTRIBUTE (STANDARD_IS_SYMLINK);
196 REGISTER_ATTRIBUTE (STANDARD_IS_VIRTUAL);
197 REGISTER_ATTRIBUTE (STANDARD_NAME);
198 REGISTER_ATTRIBUTE (STANDARD_DISPLAY_NAME);
199 REGISTER_ATTRIBUTE (STANDARD_EDIT_NAME);
200 REGISTER_ATTRIBUTE (STANDARD_COPY_NAME);
201 REGISTER_ATTRIBUTE (STANDARD_DESCRIPTION);
202 REGISTER_ATTRIBUTE (STANDARD_ICON);
203 REGISTER_ATTRIBUTE (STANDARD_CONTENT_TYPE);
204 REGISTER_ATTRIBUTE (STANDARD_FAST_CONTENT_TYPE);
205 REGISTER_ATTRIBUTE (STANDARD_SIZE);
206 REGISTER_ATTRIBUTE (STANDARD_ALLOCATED_SIZE);
207 REGISTER_ATTRIBUTE (STANDARD_SYMLINK_TARGET);
208 REGISTER_ATTRIBUTE (STANDARD_TARGET_URI);
209 REGISTER_ATTRIBUTE (STANDARD_SORT_ORDER);
210 REGISTER_ATTRIBUTE (ETAG_VALUE);
211 REGISTER_ATTRIBUTE (ID_FILE);
212 REGISTER_ATTRIBUTE (ID_FILESYSTEM);
213 REGISTER_ATTRIBUTE (ACCESS_CAN_READ);
214 REGISTER_ATTRIBUTE (ACCESS_CAN_WRITE);
215 REGISTER_ATTRIBUTE (ACCESS_CAN_EXECUTE);
216 REGISTER_ATTRIBUTE (ACCESS_CAN_DELETE);
217 REGISTER_ATTRIBUTE (ACCESS_CAN_TRASH);
218 REGISTER_ATTRIBUTE (ACCESS_CAN_RENAME);
219 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_MOUNT);
220 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_UNMOUNT);
221 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_EJECT);
222 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE);
223 REGISTER_ATTRIBUTE (MOUNTABLE_HAL_UDI);
224 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START);
225 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_STOP);
226 REGISTER_ATTRIBUTE (MOUNTABLE_START_STOP_TYPE);
227 REGISTER_ATTRIBUTE (TIME_MODIFIED);
228 REGISTER_ATTRIBUTE (TIME_MODIFIED_USEC);
229 REGISTER_ATTRIBUTE (TIME_ACCESS);
230 REGISTER_ATTRIBUTE (TIME_ACCESS_USEC);
231 REGISTER_ATTRIBUTE (TIME_CHANGED);
232 REGISTER_ATTRIBUTE (TIME_CHANGED_USEC);
233 REGISTER_ATTRIBUTE (TIME_CREATED);
234 REGISTER_ATTRIBUTE (TIME_CREATED_USEC);
235 REGISTER_ATTRIBUTE (UNIX_DEVICE);
236 REGISTER_ATTRIBUTE (UNIX_INODE);
237 REGISTER_ATTRIBUTE (UNIX_MODE);
238 REGISTER_ATTRIBUTE (UNIX_NLINK);
239 REGISTER_ATTRIBUTE (UNIX_UID);
240 REGISTER_ATTRIBUTE (UNIX_GID);
241 REGISTER_ATTRIBUTE (UNIX_RDEV);
242 REGISTER_ATTRIBUTE (UNIX_BLOCK_SIZE);
243 REGISTER_ATTRIBUTE (UNIX_BLOCKS);
244 REGISTER_ATTRIBUTE (UNIX_IS_MOUNTPOINT);
245 REGISTER_ATTRIBUTE (DOS_IS_ARCHIVE);
246 REGISTER_ATTRIBUTE (DOS_IS_SYSTEM);
247 REGISTER_ATTRIBUTE (OWNER_USER);
248 REGISTER_ATTRIBUTE (OWNER_USER_REAL);
249 REGISTER_ATTRIBUTE (OWNER_GROUP);
250 REGISTER_ATTRIBUTE (THUMBNAIL_PATH);
251 REGISTER_ATTRIBUTE (THUMBNAILING_FAILED);
252 REGISTER_ATTRIBUTE (PREVIEW_ICON);
253 REGISTER_ATTRIBUTE (FILESYSTEM_SIZE);
254 REGISTER_ATTRIBUTE (FILESYSTEM_FREE);
255 REGISTER_ATTRIBUTE (FILESYSTEM_TYPE);
256 REGISTER_ATTRIBUTE (FILESYSTEM_READONLY);
257 REGISTER_ATTRIBUTE (FILESYSTEM_USE_PREVIEW);
258 REGISTER_ATTRIBUTE (GVFS_BACKEND);
259 REGISTER_ATTRIBUTE (SELINUX_CONTEXT);
260 REGISTER_ATTRIBUTE (TRASH_ITEM_COUNT);
262 #undef REGISTER_ATTRIBUTE
266 lookup_namespace (const char *namespace)
271 G_LOCK (attribute_hash);
273 ensure_attribute_hash ();
275 ns_info = _lookup_namespace (namespace);
280 G_UNLOCK (attribute_hash);
286 get_attribute_for_id (int attribute)
289 G_LOCK (attribute_hash);
290 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
291 G_UNLOCK (attribute_hash);
296 lookup_attribute (const char *attribute)
300 G_LOCK (attribute_hash);
301 ensure_attribute_hash ();
303 attr_id = _lookup_attribute (attribute);
305 G_UNLOCK (attribute_hash);
311 g_file_info_finalize (GObject *object)
315 GFileAttribute *attrs;
317 info = G_FILE_INFO (object);
319 attrs = (GFileAttribute *)info->attributes->data;
320 for (i = 0; i < info->attributes->len; i++)
321 _g_file_attribute_value_clear (&attrs[i].value);
322 g_array_free (info->attributes, TRUE);
324 if (info->mask != NO_ATTRIBUTE_MASK)
325 g_file_attribute_matcher_unref (info->mask);
327 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
331 g_file_info_class_init (GFileInfoClass *klass)
333 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
335 gobject_class->finalize = g_file_info_finalize;
339 g_file_info_init (GFileInfo *info)
341 info->mask = NO_ATTRIBUTE_MASK;
342 info->attributes = g_array_new (FALSE, FALSE,
343 sizeof (GFileAttribute));
349 * Creates a new file info structure.
351 * Returns: a #GFileInfo.
354 g_file_info_new (void)
356 return g_object_new (G_TYPE_FILE_INFO, NULL);
360 * g_file_info_copy_into:
361 * @src_info: source to copy attributes from.
362 * @dest_info: destination to copy attributes to.
364 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
367 g_file_info_copy_into (GFileInfo *src_info,
368 GFileInfo *dest_info)
370 GFileAttribute *source, *dest;
373 g_return_if_fail (G_IS_FILE_INFO (src_info));
374 g_return_if_fail (G_IS_FILE_INFO (dest_info));
376 dest = (GFileAttribute *)dest_info->attributes->data;
377 for (i = 0; i < dest_info->attributes->len; i++)
378 _g_file_attribute_value_clear (&dest[i].value);
380 g_array_set_size (dest_info->attributes,
381 src_info->attributes->len);
383 source = (GFileAttribute *)src_info->attributes->data;
384 dest = (GFileAttribute *)dest_info->attributes->data;
386 for (i = 0; i < src_info->attributes->len; i++)
388 dest[i].attribute = source[i].attribute;
389 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
390 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
393 if (dest_info->mask != NO_ATTRIBUTE_MASK)
394 g_file_attribute_matcher_unref (dest_info->mask);
396 if (src_info->mask == NO_ATTRIBUTE_MASK)
397 dest_info->mask = NO_ATTRIBUTE_MASK;
399 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
404 * @other: a #GFileInfo.
406 * Duplicates a file info structure.
408 * Returns: a duplicate #GFileInfo of @other.
411 g_file_info_dup (GFileInfo *other)
415 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
417 new = g_file_info_new ();
418 g_file_info_copy_into (other, new);
423 * g_file_info_set_attribute_mask:
424 * @info: a #GFileInfo.
425 * @mask: a #GFileAttributeMatcher.
427 * Sets @mask on @info to match specific attribute types.
430 g_file_info_set_attribute_mask (GFileInfo *info,
431 GFileAttributeMatcher *mask)
433 GFileAttribute *attr;
436 g_return_if_fail (G_IS_FILE_INFO (info));
438 if (mask != info->mask)
440 if (info->mask != NO_ATTRIBUTE_MASK)
441 g_file_attribute_matcher_unref (info->mask);
442 info->mask = g_file_attribute_matcher_ref (mask);
444 /* Remove non-matching attributes */
445 for (i = 0; i < info->attributes->len; i++)
447 attr = &g_array_index (info->attributes, GFileAttribute, i);
448 if (!g_file_attribute_matcher_matches_id (mask,
451 _g_file_attribute_value_clear (&attr->value);
452 g_array_remove_index (info->attributes, i);
460 * g_file_info_unset_attribute_mask:
463 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
467 g_file_info_unset_attribute_mask (GFileInfo *info)
469 g_return_if_fail (G_IS_FILE_INFO (info));
471 if (info->mask != NO_ATTRIBUTE_MASK)
472 g_file_attribute_matcher_unref (info->mask);
473 info->mask = NO_ATTRIBUTE_MASK;
477 * g_file_info_clear_status:
478 * @info: a #GFileInfo.
480 * Clears the status information from @info.
483 g_file_info_clear_status (GFileInfo *info)
485 GFileAttribute *attrs;
488 g_return_if_fail (G_IS_FILE_INFO (info));
490 attrs = (GFileAttribute *)info->attributes->data;
491 for (i = 0; i < info->attributes->len; i++)
492 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
496 g_file_info_find_place (GFileInfo *info,
500 GFileAttribute *attrs;
501 /* Binary search for the place where attribute would be, if it's
505 max = info->attributes->len;
507 attrs = (GFileAttribute *)info->attributes->data;
511 med = min + (max - min) / 2;
512 if (attrs[med].attribute == attribute)
517 else if (attrs[med].attribute < attribute)
519 else /* attrs[med].attribute > attribute */
526 static GFileAttributeValue *
527 g_file_info_find_value (GFileInfo *info,
530 GFileAttribute *attrs;
533 i = g_file_info_find_place (info, attr_id);
534 attrs = (GFileAttribute *)info->attributes->data;
535 if (i < info->attributes->len &&
536 attrs[i].attribute == attr_id)
537 return &attrs[i].value;
542 static GFileAttributeValue *
543 g_file_info_find_value_by_name (GFileInfo *info,
544 const char *attribute)
548 attr_id = lookup_attribute (attribute);
549 return g_file_info_find_value (info, attr_id);
553 * g_file_info_has_attribute:
554 * @info: a #GFileInfo.
555 * @attribute: a file attribute key.
557 * Checks if a file info structure has an attribute named @attribute.
559 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
563 g_file_info_has_attribute (GFileInfo *info,
564 const char *attribute)
566 GFileAttributeValue *value;
568 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
569 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
571 value = g_file_info_find_value_by_name (info, attribute);
572 return value != NULL;
576 * g_file_info_has_namespace:
577 * @info: a #GFileInfo.
578 * @name_space: a file attribute namespace.
580 * Checks if a file info structure has an attribute in the
581 * specified @name_space.
583 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
589 g_file_info_has_namespace (GFileInfo *info,
590 const char *name_space)
592 GFileAttribute *attrs;
596 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
597 g_return_val_if_fail (name_space != NULL, FALSE);
599 ns_id = lookup_namespace (name_space);
601 attrs = (GFileAttribute *)info->attributes->data;
602 for (i = 0; i < info->attributes->len; i++)
604 if (GET_NS (attrs[i].attribute) == ns_id)
612 * g_file_info_list_attributes:
613 * @info: a #GFileInfo.
614 * @name_space: a file attribute key's namespace.
616 * Lists the file info structure's attributes.
618 * Returns: a null-terminated array of strings of all of the
619 * possible attribute types for the given @name_space, or
623 g_file_info_list_attributes (GFileInfo *info,
624 const char *name_space)
627 GFileAttribute *attrs;
629 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
632 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
634 names = g_ptr_array_new ();
635 attrs = (GFileAttribute *)info->attributes->data;
636 for (i = 0; i < info->attributes->len; i++)
638 attribute = attrs[i].attribute;
639 if (ns_id == 0 || GET_NS (attribute) == ns_id)
640 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
644 g_ptr_array_add (names, NULL);
646 return (char **)g_ptr_array_free (names, FALSE);
650 * g_file_info_get_attribute_type:
651 * @info: a #GFileInfo.
652 * @attribute: a file attribute key.
654 * Gets the attribute type for an attribute key.
656 * Returns: a #GFileAttributeType for the given @attribute, or
657 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
660 g_file_info_get_attribute_type (GFileInfo *info,
661 const char *attribute)
663 GFileAttributeValue *value;
665 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
666 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
668 value = g_file_info_find_value_by_name (info, attribute);
672 return G_FILE_ATTRIBUTE_TYPE_INVALID;
676 * g_file_info_remove_attribute:
677 * @info: a #GFileInfo.
678 * @attribute: a file attribute key.
680 * Removes all cases of @attribute from @info if it exists.
683 g_file_info_remove_attribute (GFileInfo *info,
684 const char *attribute)
687 GFileAttribute *attrs;
690 g_return_if_fail (G_IS_FILE_INFO (info));
691 g_return_if_fail (attribute != NULL && *attribute != '\0');
693 attr_id = lookup_attribute (attribute);
695 i = g_file_info_find_place (info, attr_id);
696 attrs = (GFileAttribute *)info->attributes->data;
697 if (i < info->attributes->len &&
698 attrs[i].attribute == attr_id)
700 _g_file_attribute_value_clear (&attrs[i].value);
701 g_array_remove_index (info->attributes, i);
706 * g_file_info_get_attribute_data:
707 * @info: a #GFileInfo
708 * @attribute: a file attribute key
709 * @type: return location for the attribute type, or %NULL
710 * @value_pp: return location for the attribute value, or %NULL
711 * @status: return location for the attribute status, or %NULL
713 * Gets the attribute type, value and status for an attribute key.
715 * Returns: %TRUE if @info has an attribute named @attribute,
719 g_file_info_get_attribute_data (GFileInfo *info,
720 const char *attribute,
721 GFileAttributeType *type,
723 GFileAttributeStatus *status)
725 GFileAttributeValue *value;
727 value = g_file_info_find_value_by_name (info, attribute);
732 *status = value->status;
738 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
744 * g_file_info_get_attribute_status:
745 * @info: a #GFileInfo
746 * @attribute: a file attribute key
748 * Gets the attribute status for an attribute key.
750 * Returns: a #GFileAttributeStatus for the given @attribute, or
751 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
755 g_file_info_get_attribute_status (GFileInfo *info,
756 const char *attribute)
758 GFileAttributeValue *val;
760 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
761 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
763 val = g_file_info_find_value_by_name (info, attribute);
767 return G_FILE_ATTRIBUTE_STATUS_UNSET;
771 * g_file_info_set_attribute_status:
772 * @info: a #GFileInfo
773 * @attribute: a file attribute key
774 * @status: a #GFileAttributeStatus
776 * Sets the attribute status for an attribute key. This is only
777 * needed by external code that implement g_file_set_attributes_from_info()
778 * or similar functions.
780 * The attribute must exist in @info for this to work. Otherwise %FALSE
781 * is returned and @info is unchanged.
783 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
788 g_file_info_set_attribute_status (GFileInfo *info,
789 const char *attribute,
790 GFileAttributeStatus status)
792 GFileAttributeValue *val;
794 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
795 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
797 val = g_file_info_find_value_by_name (info, attribute);
800 val->status = status;
807 GFileAttributeValue *
808 _g_file_info_get_attribute_value (GFileInfo *info,
809 const char *attribute)
812 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
813 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
815 return g_file_info_find_value_by_name (info, attribute);
819 * g_file_info_get_attribute_as_string:
820 * @info: a #GFileInfo.
821 * @attribute: a file attribute key.
823 * Gets the value of a attribute, formated as a string.
824 * This escapes things as needed to make the string valid
827 * Returns: a UTF-8 string associated with the given @attribute.
828 * When you're done with the string it must be freed with g_free().
831 g_file_info_get_attribute_as_string (GFileInfo *info,
832 const char *attribute)
834 GFileAttributeValue *val;
835 val = _g_file_info_get_attribute_value (info, attribute);
837 return _g_file_attribute_value_as_string (val);
843 * g_file_info_get_attribute_object:
844 * @info: a #GFileInfo.
845 * @attribute: a file attribute key.
847 * Gets the value of a #GObject attribute. If the attribute does
848 * not contain a #GObject, %NULL will be returned.
850 * Returns: a #GObject associated with the given @attribute, or
854 g_file_info_get_attribute_object (GFileInfo *info,
855 const char *attribute)
857 GFileAttributeValue *value;
859 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
860 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
862 value = g_file_info_find_value_by_name (info, attribute);
863 return _g_file_attribute_value_get_object (value);
867 * g_file_info_get_attribute_string:
868 * @info: a #GFileInfo.
869 * @attribute: a file attribute key.
871 * Gets the value of a string attribute. If the attribute does
872 * not contain a string, %NULL will be returned.
874 * Returns: the contents of the @attribute value as a string, or
878 g_file_info_get_attribute_string (GFileInfo *info,
879 const char *attribute)
881 GFileAttributeValue *value;
883 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
884 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
886 value = g_file_info_find_value_by_name (info, attribute);
887 return _g_file_attribute_value_get_string (value);
891 * g_file_info_get_attribute_byte_string:
892 * @info: a #GFileInfo.
893 * @attribute: a file attribute key.
895 * Gets the value of a byte string attribute. If the attribute does
896 * not contain a byte string, %NULL will be returned.
898 * Returns: the contents of the @attribute value as a byte string, or
902 g_file_info_get_attribute_byte_string (GFileInfo *info,
903 const char *attribute)
905 GFileAttributeValue *value;
907 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
908 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
910 value = g_file_info_find_value_by_name (info, attribute);
911 return _g_file_attribute_value_get_byte_string (value);
915 * g_file_info_get_attribute_stringv:
916 * @info: a #GFileInfo.
917 * @attribute: a file attribute key.
919 * Gets the value of a stringv attribute. If the attribute does
920 * not contain a stringv, %NULL will be returned.
922 * Returns: the contents of the @attribute value as a stringv, or
923 * %NULL otherwise. Do not free.
926 g_file_info_get_attribute_stringv (GFileInfo *info,
927 const char *attribute)
929 GFileAttributeValue *value;
931 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
932 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
934 value = g_file_info_find_value_by_name (info, attribute);
935 return _g_file_attribute_value_get_stringv (value);
939 * g_file_info_get_attribute_boolean:
940 * @info: a #GFileInfo.
941 * @attribute: a file attribute key.
943 * Gets the value of a boolean attribute. If the attribute does not
944 * contain a boolean value, %FALSE will be returned.
946 * Returns: the boolean value contained within the attribute.
949 g_file_info_get_attribute_boolean (GFileInfo *info,
950 const char *attribute)
952 GFileAttributeValue *value;
954 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
955 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
957 value = g_file_info_find_value_by_name (info, attribute);
958 return _g_file_attribute_value_get_boolean (value);
962 * g_file_info_get_attribute_uint32:
963 * @info: a #GFileInfo.
964 * @attribute: a file attribute key.
966 * Gets an unsigned 32-bit integer contained within the attribute. If the
967 * attribute does not contain an unsigned 32-bit integer, or is invalid,
968 * 0 will be returned.
970 * Returns: an unsigned 32-bit integer from the attribute.
973 g_file_info_get_attribute_uint32 (GFileInfo *info,
974 const char *attribute)
976 GFileAttributeValue *value;
978 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
979 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
981 value = g_file_info_find_value_by_name (info, attribute);
982 return _g_file_attribute_value_get_uint32 (value);
986 * g_file_info_get_attribute_int32:
987 * @info: a #GFileInfo.
988 * @attribute: a file attribute key.
990 * Gets a signed 32-bit integer contained within the attribute. If the
991 * attribute does not contain a signed 32-bit integer, or is invalid,
992 * 0 will be returned.
994 * Returns: a signed 32-bit integer from the attribute.
997 g_file_info_get_attribute_int32 (GFileInfo *info,
998 const char *attribute)
1000 GFileAttributeValue *value;
1002 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1003 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1005 value = g_file_info_find_value_by_name (info, attribute);
1006 return _g_file_attribute_value_get_int32 (value);
1010 * g_file_info_get_attribute_uint64:
1011 * @info: a #GFileInfo.
1012 * @attribute: a file attribute key.
1014 * Gets a unsigned 64-bit integer contained within the attribute. If the
1015 * attribute does not contain an unsigned 64-bit integer, or is invalid,
1016 * 0 will be returned.
1018 * Returns: a unsigned 64-bit integer from the attribute.
1021 g_file_info_get_attribute_uint64 (GFileInfo *info,
1022 const char *attribute)
1024 GFileAttributeValue *value;
1026 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1027 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1029 value = g_file_info_find_value_by_name (info, attribute);
1030 return _g_file_attribute_value_get_uint64 (value);
1034 * g_file_info_get_attribute_int64:
1035 * @info: a #GFileInfo.
1036 * @attribute: a file attribute key.
1038 * Gets a signed 64-bit integer contained within the attribute. If the
1039 * attribute does not contain an signed 64-bit integer, or is invalid,
1040 * 0 will be returned.
1042 * Returns: a signed 64-bit integer from the attribute.
1045 g_file_info_get_attribute_int64 (GFileInfo *info,
1046 const char *attribute)
1048 GFileAttributeValue *value;
1050 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1051 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1053 value = g_file_info_find_value_by_name (info, attribute);
1054 return _g_file_attribute_value_get_int64 (value);
1057 static GFileAttributeValue *
1058 g_file_info_create_value (GFileInfo *info,
1061 GFileAttribute *attrs;
1064 if (info->mask != NO_ATTRIBUTE_MASK &&
1065 !g_file_attribute_matcher_matches_id (info->mask, attr_id))
1068 i = g_file_info_find_place (info, attr_id);
1070 attrs = (GFileAttribute *)info->attributes->data;
1071 if (i < info->attributes->len &&
1072 attrs[i].attribute == attr_id)
1073 return &attrs[i].value;
1076 GFileAttribute attr = { 0 };
1077 attr.attribute = attr_id;
1078 g_array_insert_val (info->attributes, i, attr);
1080 attrs = (GFileAttribute *)info->attributes->data;
1081 return &attrs[i].value;
1085 static GFileAttributeValue *
1086 g_file_info_create_value_by_name (GFileInfo *info,
1087 const char *attribute)
1091 attr_id = lookup_attribute (attribute);
1093 return g_file_info_create_value (info, attr_id);
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 GFileAttributeValue *value;
1113 g_return_if_fail (G_IS_FILE_INFO (info));
1114 g_return_if_fail (attribute != NULL && *attribute != '\0');
1116 value = g_file_info_create_value_by_name (info, attribute);
1119 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1123 * g_file_info_set_attribute_object:
1124 * @info: a #GFileInfo.
1125 * @attribute: a file attribute key.
1126 * @attr_value: a #GObject.
1128 * Sets the @attribute to contain the given @attr_value,
1132 g_file_info_set_attribute_object (GFileInfo *info,
1133 const char *attribute,
1134 GObject *attr_value)
1136 GFileAttributeValue *value;
1138 g_return_if_fail (G_IS_FILE_INFO (info));
1139 g_return_if_fail (attribute != NULL && *attribute != '\0');
1140 g_return_if_fail (G_IS_OBJECT (attr_value));
1142 value = g_file_info_create_value_by_name (info, attribute);
1144 _g_file_attribute_value_set_object (value, attr_value);
1148 * g_file_info_set_attribute_stringv:
1149 * @info: a #GFileInfo.
1150 * @attribute: a file attribute key.
1151 * @attr_value: a %NULL terminated string array
1153 * Sets the @attribute to contain the given @attr_value,
1159 g_file_info_set_attribute_stringv (GFileInfo *info,
1160 const char *attribute,
1163 GFileAttributeValue *value;
1165 g_return_if_fail (G_IS_FILE_INFO (info));
1166 g_return_if_fail (attribute != NULL && *attribute != '\0');
1167 g_return_if_fail (attr_value != NULL);
1169 value = g_file_info_create_value_by_name (info, attribute);
1171 _g_file_attribute_value_set_stringv (value, attr_value);
1175 * g_file_info_set_attribute_string:
1176 * @info: a #GFileInfo.
1177 * @attribute: a file attribute key.
1178 * @attr_value: a string.
1180 * Sets the @attribute to contain the given @attr_value,
1184 g_file_info_set_attribute_string (GFileInfo *info,
1185 const char *attribute,
1186 const char *attr_value)
1188 GFileAttributeValue *value;
1190 g_return_if_fail (G_IS_FILE_INFO (info));
1191 g_return_if_fail (attribute != NULL && *attribute != '\0');
1192 g_return_if_fail (attr_value != NULL);
1194 value = g_file_info_create_value_by_name (info, attribute);
1196 _g_file_attribute_value_set_string (value, attr_value);
1200 * g_file_info_set_attribute_byte_string:
1201 * @info: a #GFileInfo.
1202 * @attribute: a file attribute key.
1203 * @attr_value: a byte string.
1205 * Sets the @attribute to contain the given @attr_value,
1209 g_file_info_set_attribute_byte_string (GFileInfo *info,
1210 const char *attribute,
1211 const char *attr_value)
1213 GFileAttributeValue *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 value = g_file_info_create_value_by_name (info, attribute);
1221 _g_file_attribute_value_set_byte_string (value, attr_value);
1225 * g_file_info_set_attribute_boolean:
1226 * @info: a #GFileInfo.
1227 * @attribute: a file attribute key.
1228 * @attr_value: a boolean value.
1230 * Sets the @attribute to contain the given @attr_value,
1234 g_file_info_set_attribute_boolean (GFileInfo *info,
1235 const char *attribute,
1236 gboolean attr_value)
1238 GFileAttributeValue *value;
1240 g_return_if_fail (G_IS_FILE_INFO (info));
1241 g_return_if_fail (attribute != NULL && *attribute != '\0');
1243 value = g_file_info_create_value_by_name (info, attribute);
1245 _g_file_attribute_value_set_boolean (value, attr_value);
1249 * g_file_info_set_attribute_uint32:
1250 * @info: a #GFileInfo.
1251 * @attribute: a file attribute key.
1252 * @attr_value: an unsigned 32-bit integer.
1254 * Sets the @attribute to contain the given @attr_value,
1258 g_file_info_set_attribute_uint32 (GFileInfo *info,
1259 const char *attribute,
1262 GFileAttributeValue *value;
1264 g_return_if_fail (G_IS_FILE_INFO (info));
1265 g_return_if_fail (attribute != NULL && *attribute != '\0');
1267 value = g_file_info_create_value_by_name (info, attribute);
1269 _g_file_attribute_value_set_uint32 (value, attr_value);
1274 * g_file_info_set_attribute_int32:
1275 * @info: a #GFileInfo.
1276 * @attribute: a file attribute key.
1277 * @attr_value: a signed 32-bit integer
1279 * Sets the @attribute to contain the given @attr_value,
1283 g_file_info_set_attribute_int32 (GFileInfo *info,
1284 const char *attribute,
1287 GFileAttributeValue *value;
1289 g_return_if_fail (G_IS_FILE_INFO (info));
1290 g_return_if_fail (attribute != NULL && *attribute != '\0');
1292 value = g_file_info_create_value_by_name (info, attribute);
1294 _g_file_attribute_value_set_int32 (value, attr_value);
1298 * g_file_info_set_attribute_uint64:
1299 * @info: a #GFileInfo.
1300 * @attribute: a file attribute key.
1301 * @attr_value: an unsigned 64-bit integer.
1303 * Sets the @attribute to contain the given @attr_value,
1307 g_file_info_set_attribute_uint64 (GFileInfo *info,
1308 const char *attribute,
1311 GFileAttributeValue *value;
1313 g_return_if_fail (G_IS_FILE_INFO (info));
1314 g_return_if_fail (attribute != NULL && *attribute != '\0');
1316 value = g_file_info_create_value_by_name (info, attribute);
1318 _g_file_attribute_value_set_uint64 (value, attr_value);
1322 * g_file_info_set_attribute_int64:
1323 * @info: a #GFileInfo.
1324 * @attribute: attribute name to set.
1325 * @attr_value: int64 value to set attribute to.
1327 * Sets the @attribute to contain the given @attr_value,
1332 g_file_info_set_attribute_int64 (GFileInfo *info,
1333 const char *attribute,
1336 GFileAttributeValue *value;
1338 g_return_if_fail (G_IS_FILE_INFO (info));
1339 g_return_if_fail (attribute != NULL && *attribute != '\0');
1341 value = g_file_info_create_value_by_name (info, attribute);
1343 _g_file_attribute_value_set_int64 (value, attr_value);
1346 /* Helper getters */
1348 * g_file_info_get_file_type:
1349 * @info: a #GFileInfo.
1351 * Gets a file's type (whether it is a regular file, symlink, etc).
1352 * This is different from the file's content type, see g_file_info_get_content_type().
1354 * Returns: a #GFileType for the given file.
1357 g_file_info_get_file_type (GFileInfo *info)
1359 static guint32 attr = 0;
1360 GFileAttributeValue *value;
1362 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1365 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1367 value = g_file_info_find_value (info, attr);
1368 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1372 * g_file_info_get_is_hidden:
1373 * @info: a #GFileInfo.
1375 * Checks if a file is hidden.
1377 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1380 g_file_info_get_is_hidden (GFileInfo *info)
1382 static guint32 attr = 0;
1383 GFileAttributeValue *value;
1385 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1388 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1390 value = g_file_info_find_value (info, attr);
1391 return (GFileType)_g_file_attribute_value_get_boolean (value);
1395 * g_file_info_get_is_backup:
1396 * @info: a #GFileInfo.
1398 * Checks if a file is a backup file.
1400 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1403 g_file_info_get_is_backup (GFileInfo *info)
1405 static guint32 attr = 0;
1406 GFileAttributeValue *value;
1408 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1411 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1413 value = g_file_info_find_value (info, attr);
1414 return (GFileType)_g_file_attribute_value_get_boolean (value);
1418 * g_file_info_get_is_symlink:
1419 * @info: a #GFileInfo.
1421 * Checks if a file is a symlink.
1423 * Returns: %TRUE if the given @info is a symlink.
1426 g_file_info_get_is_symlink (GFileInfo *info)
1428 static guint32 attr = 0;
1429 GFileAttributeValue *value;
1431 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1434 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1436 value = g_file_info_find_value (info, attr);
1437 return (GFileType)_g_file_attribute_value_get_boolean (value);
1441 * g_file_info_get_name:
1442 * @info: a #GFileInfo.
1444 * Gets the name for a file.
1446 * Returns: a string containing the file name.
1449 g_file_info_get_name (GFileInfo *info)
1451 static guint32 attr = 0;
1452 GFileAttributeValue *value;
1454 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1457 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1459 value = g_file_info_find_value (info, attr);
1460 return _g_file_attribute_value_get_byte_string (value);
1464 * g_file_info_get_display_name:
1465 * @info: a #GFileInfo.
1467 * Gets a display name for a file.
1469 * Returns: a string containing the display name.
1472 g_file_info_get_display_name (GFileInfo *info)
1474 static guint32 attr = 0;
1475 GFileAttributeValue *value;
1477 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1480 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1482 value = g_file_info_find_value (info, attr);
1483 return _g_file_attribute_value_get_string (value);
1487 * g_file_info_get_edit_name:
1488 * @info: a #GFileInfo.
1490 * Gets the edit name for a file.
1492 * Returns: a string containing the edit name.
1495 g_file_info_get_edit_name (GFileInfo *info)
1497 static guint32 attr = 0;
1498 GFileAttributeValue *value;
1500 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1503 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1505 value = g_file_info_find_value (info, attr);
1506 return _g_file_attribute_value_get_string (value);
1510 * g_file_info_get_icon:
1511 * @info: a #GFileInfo.
1513 * Gets the icon for a file.
1515 * Returns: #GIcon for the given @info.
1518 g_file_info_get_icon (GFileInfo *info)
1520 static guint32 attr = 0;
1521 GFileAttributeValue *value;
1524 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1527 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1529 value = g_file_info_find_value (info, attr);
1530 obj = _g_file_attribute_value_get_object (value);
1531 if (G_IS_ICON (obj))
1532 return G_ICON (obj);
1537 * g_file_info_get_content_type:
1538 * @info: a #GFileInfo.
1540 * Gets the file's content type.
1542 * Returns: a string containing the file's content type.
1545 g_file_info_get_content_type (GFileInfo *info)
1547 static guint32 attr = 0;
1548 GFileAttributeValue *value;
1550 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1553 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1555 value = g_file_info_find_value (info, attr);
1556 return _g_file_attribute_value_get_string (value);
1560 * g_file_info_get_size:
1561 * @info: a #GFileInfo.
1563 * Gets the file's size.
1565 * Returns: a #goffset containing the file's size.
1568 g_file_info_get_size (GFileInfo *info)
1570 static guint32 attr = 0;
1571 GFileAttributeValue *value;
1573 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1576 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1578 value = g_file_info_find_value (info, attr);
1579 return (goffset) _g_file_attribute_value_get_uint64 (value);
1583 * g_file_info_get_modification_time:
1584 * @info: a #GFileInfo.
1585 * @result: a #GTimeVal.
1587 * Gets the modification time of the current @info and sets it
1591 g_file_info_get_modification_time (GFileInfo *info,
1594 static guint32 attr_mtime = 0, attr_mtime_usec;
1595 GFileAttributeValue *value;
1597 g_return_if_fail (G_IS_FILE_INFO (info));
1598 g_return_if_fail (result != NULL);
1600 if (attr_mtime == 0)
1602 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1603 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1606 value = g_file_info_find_value (info, attr_mtime);
1607 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1608 value = g_file_info_find_value (info, attr_mtime_usec);
1609 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1613 * g_file_info_get_symlink_target:
1614 * @info: a #GFileInfo.
1616 * Gets the symlink target for a given #GFileInfo.
1618 * Returns: a string containing the symlink target.
1621 g_file_info_get_symlink_target (GFileInfo *info)
1623 static guint32 attr = 0;
1624 GFileAttributeValue *value;
1626 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1629 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1631 value = g_file_info_find_value (info, attr);
1632 return _g_file_attribute_value_get_byte_string (value);
1636 * g_file_info_get_etag:
1637 * @info: a #GFileInfo.
1639 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1640 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1642 * Returns: a string containing the value of the "etag:value" attribute.
1645 g_file_info_get_etag (GFileInfo *info)
1647 static guint32 attr = 0;
1648 GFileAttributeValue *value;
1650 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1653 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1655 value = g_file_info_find_value (info, attr);
1656 return _g_file_attribute_value_get_string (value);
1660 * g_file_info_get_sort_order:
1661 * @info: a #GFileInfo.
1663 * Gets the value of the sort_order attribute from the #GFileInfo.
1664 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1666 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1669 g_file_info_get_sort_order (GFileInfo *info)
1671 static guint32 attr = 0;
1672 GFileAttributeValue *value;
1674 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1677 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1679 value = g_file_info_find_value (info, attr);
1680 return _g_file_attribute_value_get_int32 (value);
1683 /* Helper setters: */
1685 * g_file_info_set_file_type:
1686 * @info: a #GFileInfo.
1687 * @type: a #GFileType.
1689 * Sets the file type in a #GFileInfo to @type.
1690 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1693 g_file_info_set_file_type (GFileInfo *info,
1696 static guint32 attr = 0;
1697 GFileAttributeValue *value;
1699 g_return_if_fail (G_IS_FILE_INFO (info));
1702 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1704 value = g_file_info_create_value (info, attr);
1706 _g_file_attribute_value_set_uint32 (value, type);
1710 * g_file_info_set_is_hidden:
1711 * @info: a #GFileInfo.
1712 * @is_hidden: a #gboolean.
1714 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1715 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1718 g_file_info_set_is_hidden (GFileInfo *info,
1721 static guint32 attr = 0;
1722 GFileAttributeValue *value;
1724 g_return_if_fail (G_IS_FILE_INFO (info));
1727 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1729 value = g_file_info_create_value (info, attr);
1731 _g_file_attribute_value_set_boolean (value, is_hidden);
1735 * g_file_info_set_is_symlink:
1736 * @info: a #GFileInfo.
1737 * @is_symlink: a #gboolean.
1739 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1740 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1743 g_file_info_set_is_symlink (GFileInfo *info,
1744 gboolean is_symlink)
1746 static guint32 attr = 0;
1747 GFileAttributeValue *value;
1749 g_return_if_fail (G_IS_FILE_INFO (info));
1752 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1754 value = g_file_info_create_value (info, attr);
1756 _g_file_attribute_value_set_boolean (value, is_symlink);
1760 * g_file_info_set_name:
1761 * @info: a #GFileInfo.
1762 * @name: a string containing a name.
1764 * Sets the name attribute for the current #GFileInfo.
1765 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1768 g_file_info_set_name (GFileInfo *info,
1771 static guint32 attr = 0;
1772 GFileAttributeValue *value;
1774 g_return_if_fail (G_IS_FILE_INFO (info));
1775 g_return_if_fail (name != NULL);
1778 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1780 value = g_file_info_create_value (info, attr);
1782 _g_file_attribute_value_set_byte_string (value, name);
1786 * g_file_info_set_display_name:
1787 * @info: a #GFileInfo.
1788 * @display_name: a string containing a display name.
1790 * Sets the display name for the current #GFileInfo.
1791 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1794 g_file_info_set_display_name (GFileInfo *info,
1795 const char *display_name)
1797 static guint32 attr = 0;
1798 GFileAttributeValue *value;
1800 g_return_if_fail (G_IS_FILE_INFO (info));
1801 g_return_if_fail (display_name != NULL);
1804 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1806 value = g_file_info_create_value (info, attr);
1808 _g_file_attribute_value_set_string (value, display_name);
1812 * g_file_info_set_edit_name:
1813 * @info: a #GFileInfo.
1814 * @edit_name: a string containing an edit name.
1816 * Sets the edit name for the current file.
1817 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1820 g_file_info_set_edit_name (GFileInfo *info,
1821 const char *edit_name)
1823 static guint32 attr = 0;
1824 GFileAttributeValue *value;
1826 g_return_if_fail (G_IS_FILE_INFO (info));
1827 g_return_if_fail (edit_name != NULL);
1830 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1832 value = g_file_info_create_value (info, attr);
1834 _g_file_attribute_value_set_string (value, edit_name);
1838 * g_file_info_set_icon:
1839 * @info: a #GFileInfo.
1842 * Sets the icon for a given #GFileInfo.
1843 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1846 g_file_info_set_icon (GFileInfo *info,
1849 static guint32 attr = 0;
1850 GFileAttributeValue *value;
1852 g_return_if_fail (G_IS_FILE_INFO (info));
1853 g_return_if_fail (G_IS_ICON (icon));
1856 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1858 value = g_file_info_create_value (info, attr);
1860 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1864 * g_file_info_set_content_type:
1865 * @info: a #GFileInfo.
1866 * @content_type: a content type. See #GContentType.
1868 * Sets the content type attribute for a given #GFileInfo.
1869 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1872 g_file_info_set_content_type (GFileInfo *info,
1873 const char *content_type)
1875 static guint32 attr = 0;
1876 GFileAttributeValue *value;
1878 g_return_if_fail (G_IS_FILE_INFO (info));
1879 g_return_if_fail (content_type != NULL);
1882 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1884 value = g_file_info_create_value (info, attr);
1886 _g_file_attribute_value_set_string (value, content_type);
1890 * g_file_info_set_size:
1891 * @info: a #GFileInfo.
1892 * @size: a #goffset containing the file's size.
1894 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1895 * to the given size.
1898 g_file_info_set_size (GFileInfo *info,
1901 static guint32 attr = 0;
1902 GFileAttributeValue *value;
1904 g_return_if_fail (G_IS_FILE_INFO (info));
1907 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1909 value = g_file_info_create_value (info, attr);
1911 _g_file_attribute_value_set_uint64 (value, size);
1915 * g_file_info_set_modification_time
1916 * @info: a #GFileInfo.
1917 * @mtime: a #GTimeVal.
1919 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
1920 * info to the given time value.
1923 g_file_info_set_modification_time (GFileInfo *info,
1926 static guint32 attr_mtime = 0, attr_mtime_usec;
1927 GFileAttributeValue *value;
1929 g_return_if_fail (G_IS_FILE_INFO (info));
1930 g_return_if_fail (mtime != NULL);
1932 if (attr_mtime == 0)
1934 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1935 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1938 value = g_file_info_create_value (info, attr_mtime);
1940 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
1941 value = g_file_info_create_value (info, attr_mtime_usec);
1943 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
1947 * g_file_info_set_symlink_target:
1948 * @info: a #GFileInfo.
1949 * @symlink_target: a static string containing a path to a symlink target.
1951 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
1952 * to the given symlink target.
1955 g_file_info_set_symlink_target (GFileInfo *info,
1956 const char *symlink_target)
1958 static guint32 attr = 0;
1959 GFileAttributeValue *value;
1961 g_return_if_fail (G_IS_FILE_INFO (info));
1962 g_return_if_fail (symlink_target != NULL);
1965 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1967 value = g_file_info_create_value (info, attr);
1969 _g_file_attribute_value_set_byte_string (value, symlink_target);
1973 * g_file_info_set_sort_order:
1974 * @info: a #GFileInfo.
1975 * @sort_order: a sort order integer.
1977 * Sets the sort order attribute in the file info structure. See
1978 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1981 g_file_info_set_sort_order (GFileInfo *info,
1984 static guint32 attr = 0;
1985 GFileAttributeValue *value;
1987 g_return_if_fail (G_IS_FILE_INFO (info));
1990 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1992 value = g_file_info_create_value (info, attr);
1994 _g_file_attribute_value_set_int32 (value, sort_order);
1998 #define ON_STACK_MATCHERS 5
2005 struct _GFileAttributeMatcher {
2007 SubMatcher sub_matchers[ON_STACK_MATCHERS];
2008 GArray *more_sub_matchers;
2011 guint32 iterator_ns;
2017 matcher_add (GFileAttributeMatcher *matcher,
2021 SubMatcher *sub_matchers;
2025 for (i = 0; i < ON_STACK_MATCHERS; i++)
2027 /* First empty spot, not found, use this */
2028 if (matcher->sub_matchers[i].id == 0)
2030 matcher->sub_matchers[i].id = id;
2031 matcher->sub_matchers[i].mask = mask;
2036 if (matcher->sub_matchers[i].id == id &&
2037 matcher->sub_matchers[i].mask == mask)
2041 if (matcher->more_sub_matchers == NULL)
2042 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2044 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2045 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2048 if (sub_matchers[i].id == id &&
2049 sub_matchers[i].mask == mask)
2056 g_array_append_val (matcher->more_sub_matchers, s);
2060 * g_file_attribute_matcher_new:
2061 * @attributes: an attribute string to match.
2063 * Creates a new file attribute matcher, which matches attributes
2064 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
2065 * counted structures, and are created with a reference count of 1. If
2066 * the number of references falls to 0, the #GFileAttributeMatcher is
2067 * automatically destroyed.
2069 * The @attribute string should be formatted with specific keys separated
2070 * from namespaces with a double colon. Several "namespace::key" strings may be
2071 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
2072 * The wildcard "*" may be used to match all keys and namespaces, or
2073 * "namespace::*" will match all keys in a given namespace.
2075 * Examples of strings to use:
2077 * <title>File Attribute Matcher strings and results</title>
2078 * <tgroup cols='2' align='left'><thead>
2079 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
2081 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
2082 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
2083 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
2084 * all keys in the unix namespace.</entry></row>
2088 * Returns: a #GFileAttributeMatcher.
2090 GFileAttributeMatcher *
2091 g_file_attribute_matcher_new (const char *attributes)
2096 GFileAttributeMatcher *matcher;
2098 if (attributes == NULL || *attributes == '\0')
2101 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2104 split = g_strsplit (attributes, ",", -1);
2106 for (i = 0; split[i] != NULL; i++)
2108 if (strcmp (split[i], "*") == 0)
2109 matcher->all = TRUE;
2114 colon = strstr (split[i], "::");
2115 if (colon != NULL &&
2120 id = lookup_attribute (split[i]);
2128 id = lookup_namespace (split[i]) << NS_POS;
2129 mask = NS_MASK << NS_POS;
2132 matcher_add (matcher, id, mask);
2142 * g_file_attribute_matcher_ref:
2143 * @matcher: a #GFileAttributeMatcher.
2145 * References a file attribute matcher.
2147 * Returns: a #GFileAttributeMatcher.
2149 GFileAttributeMatcher *
2150 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2154 g_return_val_if_fail (matcher->ref > 0, NULL);
2155 g_atomic_int_inc (&matcher->ref);
2161 * g_file_attribute_matcher_unref:
2162 * @matcher: a #GFileAttributeMatcher.
2164 * Unreferences @matcher. If the reference count falls below 1,
2165 * the @matcher is automatically freed.
2169 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2173 g_return_if_fail (matcher->ref > 0);
2175 if (g_atomic_int_dec_and_test (&matcher->ref))
2177 if (matcher->more_sub_matchers)
2178 g_array_free (matcher->more_sub_matchers, TRUE);
2186 * g_file_attribute_matcher_matches_only:
2187 * @matcher: a #GFileAttributeMatcher.
2188 * @attribute: a file attribute key.
2190 * Checks if a attribute matcher only matches a given attribute. Always
2191 * returns %FALSE if "*" was used when creating the matcher.
2193 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2196 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2197 const char *attribute)
2201 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2203 if (matcher == NULL ||
2207 id = lookup_attribute (attribute);
2209 if (matcher->sub_matchers[0].id != 0 &&
2210 matcher->sub_matchers[1].id == 0 &&
2211 matcher->sub_matchers[0].mask == 0xffffffff &&
2212 matcher->sub_matchers[0].id == id)
2219 matcher_matches_id (GFileAttributeMatcher *matcher,
2222 SubMatcher *sub_matchers;
2225 for (i = 0; i < ON_STACK_MATCHERS; i++)
2227 if (matcher->sub_matchers[i].id == 0)
2230 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2234 if (matcher->more_sub_matchers)
2236 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2237 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2239 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2248 g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2251 /* We return a NULL matcher for an empty match string, so handle this */
2252 if (matcher == NULL)
2258 return matcher_matches_id (matcher, id);
2262 * g_file_attribute_matcher_matches:
2263 * @matcher: a #GFileAttributeMatcher.
2264 * @attribute: a file attribute key.
2266 * Checks if an attribute will be matched by an attribute matcher. If
2267 * the matcher was created with the "*" matching string, this function
2268 * will always return %TRUE.
2270 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2273 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2274 const char *attribute)
2276 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2278 /* We return a NULL matcher for an empty match string, so handle this */
2279 if (matcher == NULL)
2285 return matcher_matches_id (matcher, lookup_attribute (attribute));
2288 /* return TRUE -> all */
2290 * g_file_attribute_matcher_enumerate_namespace:
2291 * @matcher: a #GFileAttributeMatcher.
2292 * @ns: a string containing a file attribute namespace.
2294 * Checks if the matcher will match all of the keys in a given namespace.
2295 * This will always return %TRUE if a wildcard character is in use (e.g. if
2296 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2297 * using "*" and namespace is anything.)
2299 * TODO: this is awkwardly worded.
2301 * Returns: %TRUE if the matcher matches all of the entries
2302 * in the given @ns, %FALSE otherwise.
2305 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2308 SubMatcher *sub_matchers;
2312 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2314 /* We return a NULL matcher for an empty match string, so handle this */
2315 if (matcher == NULL)
2321 ns_id = lookup_namespace (ns) << NS_POS;
2323 for (i = 0; i < ON_STACK_MATCHERS; i++)
2325 if (matcher->sub_matchers[i].id == ns_id)
2329 if (matcher->more_sub_matchers)
2331 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2332 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2334 if (sub_matchers[i].id == ns_id)
2339 matcher->iterator_ns = ns_id;
2340 matcher->iterator_pos = 0;
2346 * g_file_attribute_matcher_enumerate_next:
2347 * @matcher: a #GFileAttributeMatcher.
2349 * Gets the next matched attribute from a #GFileAttributeMatcher.
2351 * Returns: a string containing the next attribute or %NULL if
2352 * no more attribute exist.
2355 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2358 SubMatcher *sub_matcher;
2360 /* We return a NULL matcher for an empty match string, so handle this */
2361 if (matcher == NULL)
2366 i = matcher->iterator_pos++;
2368 if (i < ON_STACK_MATCHERS)
2370 if (matcher->sub_matchers[i].id == 0)
2373 sub_matcher = &matcher->sub_matchers[i];
2377 if (matcher->more_sub_matchers == NULL)
2380 i -= ON_STACK_MATCHERS;
2381 if (i < matcher->more_sub_matchers->len)
2382 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2387 if (sub_matcher->mask == 0xffffffff &&
2388 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2389 return get_attribute_for_id (sub_matcher->id);
2393 #define __G_FILE_INFO_C__
2394 #include "gioaliasdef.c"