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"
66 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
67 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
71 GFileAttributeValue value;
76 GObject parent_instance;
79 GFileAttributeMatcher *mask;
82 struct _GFileInfoClass
84 GObjectClass parent_class;
88 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
92 guint32 attribute_id_counter;
95 G_LOCK_DEFINE_STATIC (attribute_hash);
96 static int namespace_id_counter = 0;
97 static GHashTable *ns_hash = NULL;
98 static GHashTable *attribute_hash = NULL;
99 static char ***attributes = NULL;
101 /* Attribute ids are 32bit, we split it up like this:
102 * |------------|--------------------|
104 * namespace attribute id
106 * This way the attributes gets sorted in namespace order
110 #define NS_MASK ((guint32)((1<<12) - 1))
112 #define ID_MASK ((guint32)((1<<20) - 1))
114 #define GET_NS(_attr_id) \
115 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
116 #define GET_ID(_attr_id) \
117 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
119 #define MAKE_ATTR_ID(_ns, _id) \
120 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
121 ((((guint32) _id) & ID_MASK) << ID_POS) )
124 _lookup_namespace (const char *namespace)
128 ns_info = g_hash_table_lookup (ns_hash, namespace);
131 ns_info = g_new0 (NSInfo, 1);
132 ns_info->id = ++namespace_id_counter;
133 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
134 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
135 attributes[ns_info->id] = NULL;
141 _lookup_attribute (const char *attribute)
148 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
153 colon = strstr (attribute, "::");
155 ns = g_strndup (attribute, colon - attribute);
159 ns_info = _lookup_namespace (ns);
162 id = ++ns_info->attribute_id_counter;
163 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
164 attributes[ns_info->id][id] = g_strdup (attribute);
166 attr_id = MAKE_ATTR_ID (ns_info->id, id);
168 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
174 ensure_attribute_hash (void)
176 if (attribute_hash != NULL)
179 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
180 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
182 #define REGISTER_ATTRIBUTE(name) G_STMT_START{\
183 guint _u = _lookup_attribute (G_FILE_ATTRIBUTE_ ## name); \
184 /* use for generating the ID: g_print ("#define G_FILE_ATTRIBUTE_ID_%s (%u + %u)\n", #name + 17, _u & ~ID_MASK, _u & ID_MASK); */ \
185 g_assert (_u == G_FILE_ATTRIBUTE_ID_ ## name); \
188 REGISTER_ATTRIBUTE (STANDARD_TYPE);
189 REGISTER_ATTRIBUTE (STANDARD_IS_HIDDEN);
190 REGISTER_ATTRIBUTE (STANDARD_IS_BACKUP);
191 REGISTER_ATTRIBUTE (STANDARD_IS_SYMLINK);
192 REGISTER_ATTRIBUTE (STANDARD_IS_VIRTUAL);
193 REGISTER_ATTRIBUTE (STANDARD_NAME);
194 REGISTER_ATTRIBUTE (STANDARD_DISPLAY_NAME);
195 REGISTER_ATTRIBUTE (STANDARD_EDIT_NAME);
196 REGISTER_ATTRIBUTE (STANDARD_COPY_NAME);
197 REGISTER_ATTRIBUTE (STANDARD_DESCRIPTION);
198 REGISTER_ATTRIBUTE (STANDARD_ICON);
199 REGISTER_ATTRIBUTE (STANDARD_CONTENT_TYPE);
200 REGISTER_ATTRIBUTE (STANDARD_FAST_CONTENT_TYPE);
201 REGISTER_ATTRIBUTE (STANDARD_SIZE);
202 REGISTER_ATTRIBUTE (STANDARD_ALLOCATED_SIZE);
203 REGISTER_ATTRIBUTE (STANDARD_SYMLINK_TARGET);
204 REGISTER_ATTRIBUTE (STANDARD_TARGET_URI);
205 REGISTER_ATTRIBUTE (STANDARD_SORT_ORDER);
206 REGISTER_ATTRIBUTE (ETAG_VALUE);
207 REGISTER_ATTRIBUTE (ID_FILE);
208 REGISTER_ATTRIBUTE (ID_FILESYSTEM);
209 REGISTER_ATTRIBUTE (ACCESS_CAN_READ);
210 REGISTER_ATTRIBUTE (ACCESS_CAN_WRITE);
211 REGISTER_ATTRIBUTE (ACCESS_CAN_EXECUTE);
212 REGISTER_ATTRIBUTE (ACCESS_CAN_DELETE);
213 REGISTER_ATTRIBUTE (ACCESS_CAN_TRASH);
214 REGISTER_ATTRIBUTE (ACCESS_CAN_RENAME);
215 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_MOUNT);
216 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_UNMOUNT);
217 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_EJECT);
218 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE);
219 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE_FILE);
220 REGISTER_ATTRIBUTE (MOUNTABLE_HAL_UDI);
221 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START);
222 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START_DEGRADED);
223 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_STOP);
224 REGISTER_ATTRIBUTE (MOUNTABLE_START_STOP_TYPE);
225 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_POLL);
226 REGISTER_ATTRIBUTE (MOUNTABLE_IS_MEDIA_CHECK_AUTOMATIC);
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);
261 REGISTER_ATTRIBUTE (TRASH_ORIG_PATH);
262 REGISTER_ATTRIBUTE (TRASH_DELETION_DATE);
264 #undef REGISTER_ATTRIBUTE
268 lookup_namespace (const char *namespace)
273 G_LOCK (attribute_hash);
275 ensure_attribute_hash ();
277 ns_info = _lookup_namespace (namespace);
282 G_UNLOCK (attribute_hash);
288 get_attribute_for_id (int attribute)
291 G_LOCK (attribute_hash);
292 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
293 G_UNLOCK (attribute_hash);
298 lookup_attribute (const char *attribute)
302 G_LOCK (attribute_hash);
303 ensure_attribute_hash ();
305 attr_id = _lookup_attribute (attribute);
307 G_UNLOCK (attribute_hash);
313 g_file_info_finalize (GObject *object)
317 GFileAttribute *attrs;
319 info = G_FILE_INFO (object);
321 attrs = (GFileAttribute *)info->attributes->data;
322 for (i = 0; i < info->attributes->len; i++)
323 _g_file_attribute_value_clear (&attrs[i].value);
324 g_array_free (info->attributes, TRUE);
326 if (info->mask != NO_ATTRIBUTE_MASK)
327 g_file_attribute_matcher_unref (info->mask);
329 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
333 g_file_info_class_init (GFileInfoClass *klass)
335 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
337 gobject_class->finalize = g_file_info_finalize;
341 g_file_info_init (GFileInfo *info)
343 info->mask = NO_ATTRIBUTE_MASK;
344 info->attributes = g_array_new (FALSE, FALSE,
345 sizeof (GFileAttribute));
351 * Creates a new file info structure.
353 * Returns: a #GFileInfo.
356 g_file_info_new (void)
358 return g_object_new (G_TYPE_FILE_INFO, NULL);
362 * g_file_info_copy_into:
363 * @src_info: source to copy attributes from.
364 * @dest_info: destination to copy attributes to.
366 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
369 g_file_info_copy_into (GFileInfo *src_info,
370 GFileInfo *dest_info)
372 GFileAttribute *source, *dest;
375 g_return_if_fail (G_IS_FILE_INFO (src_info));
376 g_return_if_fail (G_IS_FILE_INFO (dest_info));
378 dest = (GFileAttribute *)dest_info->attributes->data;
379 for (i = 0; i < dest_info->attributes->len; i++)
380 _g_file_attribute_value_clear (&dest[i].value);
382 g_array_set_size (dest_info->attributes,
383 src_info->attributes->len);
385 source = (GFileAttribute *)src_info->attributes->data;
386 dest = (GFileAttribute *)dest_info->attributes->data;
388 for (i = 0; i < src_info->attributes->len; i++)
390 dest[i].attribute = source[i].attribute;
391 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
392 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
395 if (dest_info->mask != NO_ATTRIBUTE_MASK)
396 g_file_attribute_matcher_unref (dest_info->mask);
398 if (src_info->mask == NO_ATTRIBUTE_MASK)
399 dest_info->mask = NO_ATTRIBUTE_MASK;
401 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
406 * @other: a #GFileInfo.
408 * Duplicates a file info structure.
410 * Returns: (transfer full): a duplicate #GFileInfo of @other.
413 g_file_info_dup (GFileInfo *other)
417 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
419 new = g_file_info_new ();
420 g_file_info_copy_into (other, new);
425 * g_file_info_set_attribute_mask:
426 * @info: a #GFileInfo.
427 * @mask: a #GFileAttributeMatcher.
429 * Sets @mask on @info to match specific attribute types.
432 g_file_info_set_attribute_mask (GFileInfo *info,
433 GFileAttributeMatcher *mask)
435 GFileAttribute *attr;
438 g_return_if_fail (G_IS_FILE_INFO (info));
440 if (mask != info->mask)
442 if (info->mask != NO_ATTRIBUTE_MASK)
443 g_file_attribute_matcher_unref (info->mask);
444 info->mask = g_file_attribute_matcher_ref (mask);
446 /* Remove non-matching attributes */
447 for (i = 0; i < info->attributes->len; i++)
449 attr = &g_array_index (info->attributes, GFileAttribute, i);
450 if (!_g_file_attribute_matcher_matches_id (mask,
453 _g_file_attribute_value_clear (&attr->value);
454 g_array_remove_index (info->attributes, i);
462 * g_file_info_unset_attribute_mask:
465 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
469 g_file_info_unset_attribute_mask (GFileInfo *info)
471 g_return_if_fail (G_IS_FILE_INFO (info));
473 if (info->mask != NO_ATTRIBUTE_MASK)
474 g_file_attribute_matcher_unref (info->mask);
475 info->mask = NO_ATTRIBUTE_MASK;
479 * g_file_info_clear_status:
480 * @info: a #GFileInfo.
482 * Clears the status information from @info.
485 g_file_info_clear_status (GFileInfo *info)
487 GFileAttribute *attrs;
490 g_return_if_fail (G_IS_FILE_INFO (info));
492 attrs = (GFileAttribute *)info->attributes->data;
493 for (i = 0; i < info->attributes->len; i++)
494 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
498 g_file_info_find_place (GFileInfo *info,
502 GFileAttribute *attrs;
503 /* Binary search for the place where attribute would be, if it's
507 max = info->attributes->len;
509 attrs = (GFileAttribute *)info->attributes->data;
513 med = min + (max - min) / 2;
514 if (attrs[med].attribute == attribute)
519 else if (attrs[med].attribute < attribute)
521 else /* attrs[med].attribute > attribute */
528 static GFileAttributeValue *
529 g_file_info_find_value (GFileInfo *info,
532 GFileAttribute *attrs;
535 i = g_file_info_find_place (info, attr_id);
536 attrs = (GFileAttribute *)info->attributes->data;
537 if (i < info->attributes->len &&
538 attrs[i].attribute == attr_id)
539 return &attrs[i].value;
544 static GFileAttributeValue *
545 g_file_info_find_value_by_name (GFileInfo *info,
546 const char *attribute)
550 attr_id = lookup_attribute (attribute);
551 return g_file_info_find_value (info, attr_id);
555 * g_file_info_has_attribute:
556 * @info: a #GFileInfo.
557 * @attribute: a file attribute key.
559 * Checks if a file info structure has an attribute named @attribute.
561 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
565 g_file_info_has_attribute (GFileInfo *info,
566 const char *attribute)
568 GFileAttributeValue *value;
570 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
571 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
573 value = g_file_info_find_value_by_name (info, attribute);
574 return value != NULL;
578 * g_file_info_has_namespace:
579 * @info: a #GFileInfo.
580 * @name_space: a file attribute namespace.
582 * Checks if a file info structure has an attribute in the
583 * specified @name_space.
585 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
591 g_file_info_has_namespace (GFileInfo *info,
592 const char *name_space)
594 GFileAttribute *attrs;
598 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
599 g_return_val_if_fail (name_space != NULL, FALSE);
601 ns_id = lookup_namespace (name_space);
603 attrs = (GFileAttribute *)info->attributes->data;
604 for (i = 0; i < info->attributes->len; i++)
606 if (GET_NS (attrs[i].attribute) == ns_id)
614 * g_file_info_list_attributes:
615 * @info: a #GFileInfo.
616 * @name_space: a file attribute key's namespace.
618 * Lists the file info structure's attributes.
620 * Returns: (array zero-terminated=1) (transfer full): a null-terminated array of strings of all of the
621 * possible attribute types for the given @name_space, or
625 g_file_info_list_attributes (GFileInfo *info,
626 const char *name_space)
629 GFileAttribute *attrs;
631 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
634 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
636 names = g_ptr_array_new ();
637 attrs = (GFileAttribute *)info->attributes->data;
638 for (i = 0; i < info->attributes->len; i++)
640 attribute = attrs[i].attribute;
641 if (ns_id == 0 || GET_NS (attribute) == ns_id)
642 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
646 g_ptr_array_add (names, NULL);
648 return (char **)g_ptr_array_free (names, FALSE);
652 * g_file_info_get_attribute_type:
653 * @info: a #GFileInfo.
654 * @attribute: a file attribute key.
656 * Gets the attribute type for an attribute key.
658 * Returns: a #GFileAttributeType for the given @attribute, or
659 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
662 g_file_info_get_attribute_type (GFileInfo *info,
663 const char *attribute)
665 GFileAttributeValue *value;
667 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
668 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
670 value = g_file_info_find_value_by_name (info, attribute);
674 return G_FILE_ATTRIBUTE_TYPE_INVALID;
678 * g_file_info_remove_attribute:
679 * @info: a #GFileInfo.
680 * @attribute: a file attribute key.
682 * Removes all cases of @attribute from @info if it exists.
685 g_file_info_remove_attribute (GFileInfo *info,
686 const char *attribute)
689 GFileAttribute *attrs;
692 g_return_if_fail (G_IS_FILE_INFO (info));
693 g_return_if_fail (attribute != NULL && *attribute != '\0');
695 attr_id = lookup_attribute (attribute);
697 i = g_file_info_find_place (info, attr_id);
698 attrs = (GFileAttribute *)info->attributes->data;
699 if (i < info->attributes->len &&
700 attrs[i].attribute == attr_id)
702 _g_file_attribute_value_clear (&attrs[i].value);
703 g_array_remove_index (info->attributes, i);
708 * g_file_info_get_attribute_data:
709 * @info: a #GFileInfo
710 * @attribute: a file attribute key
711 * @type: (out) (allow-none): return location for the attribute type, or %NULL
712 * @value_pp: (out) (allow-none): return location for the attribute value, or %NULL
713 * @status: (out) (allow-none): return location for the attribute status, or %NULL
715 * Gets the attribute type, value and status for an attribute key.
717 * Returns: (transfer none): %TRUE if @info has an attribute named @attribute,
721 g_file_info_get_attribute_data (GFileInfo *info,
722 const char *attribute,
723 GFileAttributeType *type,
725 GFileAttributeStatus *status)
727 GFileAttributeValue *value;
729 value = g_file_info_find_value_by_name (info, attribute);
734 *status = value->status;
740 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
746 * g_file_info_get_attribute_status:
747 * @info: a #GFileInfo
748 * @attribute: a file attribute key
750 * Gets the attribute status for an attribute key.
752 * Returns: a #GFileAttributeStatus for the given @attribute, or
753 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
757 g_file_info_get_attribute_status (GFileInfo *info,
758 const char *attribute)
760 GFileAttributeValue *val;
762 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
763 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
765 val = g_file_info_find_value_by_name (info, attribute);
769 return G_FILE_ATTRIBUTE_STATUS_UNSET;
773 * g_file_info_set_attribute_status:
774 * @info: a #GFileInfo
775 * @attribute: a file attribute key
776 * @status: a #GFileAttributeStatus
778 * Sets the attribute status for an attribute key. This is only
779 * needed by external code that implement g_file_set_attributes_from_info()
780 * or similar functions.
782 * The attribute must exist in @info for this to work. Otherwise %FALSE
783 * is returned and @info is unchanged.
785 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
790 g_file_info_set_attribute_status (GFileInfo *info,
791 const char *attribute,
792 GFileAttributeStatus status)
794 GFileAttributeValue *val;
796 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
797 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
799 val = g_file_info_find_value_by_name (info, attribute);
802 val->status = status;
809 GFileAttributeValue *
810 _g_file_info_get_attribute_value (GFileInfo *info,
811 const char *attribute)
814 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
815 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
817 return g_file_info_find_value_by_name (info, attribute);
821 * g_file_info_get_attribute_as_string:
822 * @info: a #GFileInfo.
823 * @attribute: a file attribute key.
825 * Gets the value of a attribute, formated as a string.
826 * This escapes things as needed to make the string valid
829 * Returns: a UTF-8 string associated with the given @attribute.
830 * When you're done with the string it must be freed with g_free().
833 g_file_info_get_attribute_as_string (GFileInfo *info,
834 const char *attribute)
836 GFileAttributeValue *val;
837 val = _g_file_info_get_attribute_value (info, attribute);
839 return _g_file_attribute_value_as_string (val);
845 * g_file_info_get_attribute_object:
846 * @info: a #GFileInfo.
847 * @attribute: a file attribute key.
849 * Gets the value of a #GObject attribute. If the attribute does
850 * not contain a #GObject, %NULL will be returned.
852 * Returns: (transfer none): a #GObject associated with the given @attribute, or
856 g_file_info_get_attribute_object (GFileInfo *info,
857 const char *attribute)
859 GFileAttributeValue *value;
861 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
862 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
864 value = g_file_info_find_value_by_name (info, attribute);
865 return _g_file_attribute_value_get_object (value);
869 * g_file_info_get_attribute_string:
870 * @info: a #GFileInfo.
871 * @attribute: a file attribute key.
873 * Gets the value of a string attribute. If the attribute does
874 * not contain a string, %NULL will be returned.
876 * Returns: the contents of the @attribute value as a UTF-8 string, or
880 g_file_info_get_attribute_string (GFileInfo *info,
881 const char *attribute)
883 GFileAttributeValue *value;
885 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
886 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
888 value = g_file_info_find_value_by_name (info, attribute);
889 return _g_file_attribute_value_get_string (value);
893 * g_file_info_get_attribute_byte_string:
894 * @info: a #GFileInfo.
895 * @attribute: a file attribute key.
897 * Gets the value of a byte string attribute. If the attribute does
898 * not contain a byte string, %NULL will be returned.
900 * Returns: the contents of the @attribute value as a byte string, or
904 g_file_info_get_attribute_byte_string (GFileInfo *info,
905 const char *attribute)
907 GFileAttributeValue *value;
909 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
910 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
912 value = g_file_info_find_value_by_name (info, attribute);
913 return _g_file_attribute_value_get_byte_string (value);
917 * g_file_info_get_attribute_stringv:
918 * @info: a #GFileInfo.
919 * @attribute: a file attribute key.
921 * Gets the value of a stringv attribute. If the attribute does
922 * not contain a stringv, %NULL will be returned.
924 * Returns: (transfer none): the contents of the @attribute value as a stringv, or
925 * %NULL otherwise. Do not free. These returned strings are UTF-8.
930 g_file_info_get_attribute_stringv (GFileInfo *info,
931 const char *attribute)
933 GFileAttributeValue *value;
935 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
936 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
938 value = g_file_info_find_value_by_name (info, attribute);
939 return _g_file_attribute_value_get_stringv (value);
943 * g_file_info_get_attribute_boolean:
944 * @info: a #GFileInfo.
945 * @attribute: a file attribute key.
947 * Gets the value of a boolean attribute. If the attribute does not
948 * contain a boolean value, %FALSE will be returned.
950 * Returns: the boolean value contained within the attribute.
953 g_file_info_get_attribute_boolean (GFileInfo *info,
954 const char *attribute)
956 GFileAttributeValue *value;
958 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
959 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
961 value = g_file_info_find_value_by_name (info, attribute);
962 return _g_file_attribute_value_get_boolean (value);
966 * g_file_info_get_attribute_uint32:
967 * @info: a #GFileInfo.
968 * @attribute: a file attribute key.
970 * Gets an unsigned 32-bit integer contained within the attribute. If the
971 * attribute does not contain an unsigned 32-bit integer, or is invalid,
972 * 0 will be returned.
974 * Returns: an unsigned 32-bit integer from the attribute.
977 g_file_info_get_attribute_uint32 (GFileInfo *info,
978 const char *attribute)
980 GFileAttributeValue *value;
982 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
983 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
985 value = g_file_info_find_value_by_name (info, attribute);
986 return _g_file_attribute_value_get_uint32 (value);
990 * g_file_info_get_attribute_int32:
991 * @info: a #GFileInfo.
992 * @attribute: a file attribute key.
994 * Gets a signed 32-bit integer contained within the attribute. If the
995 * attribute does not contain a signed 32-bit integer, or is invalid,
996 * 0 will be returned.
998 * Returns: a signed 32-bit integer from the attribute.
1001 g_file_info_get_attribute_int32 (GFileInfo *info,
1002 const char *attribute)
1004 GFileAttributeValue *value;
1006 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1007 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1009 value = g_file_info_find_value_by_name (info, attribute);
1010 return _g_file_attribute_value_get_int32 (value);
1014 * g_file_info_get_attribute_uint64:
1015 * @info: a #GFileInfo.
1016 * @attribute: a file attribute key.
1018 * Gets a unsigned 64-bit integer contained within the attribute. If the
1019 * attribute does not contain an unsigned 64-bit integer, or is invalid,
1020 * 0 will be returned.
1022 * Returns: a unsigned 64-bit integer from the attribute.
1025 g_file_info_get_attribute_uint64 (GFileInfo *info,
1026 const char *attribute)
1028 GFileAttributeValue *value;
1030 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1031 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1033 value = g_file_info_find_value_by_name (info, attribute);
1034 return _g_file_attribute_value_get_uint64 (value);
1038 * g_file_info_get_attribute_int64:
1039 * @info: a #GFileInfo.
1040 * @attribute: a file attribute key.
1042 * Gets a signed 64-bit integer contained within the attribute. If the
1043 * attribute does not contain an signed 64-bit integer, or is invalid,
1044 * 0 will be returned.
1046 * Returns: a signed 64-bit integer from the attribute.
1049 g_file_info_get_attribute_int64 (GFileInfo *info,
1050 const char *attribute)
1052 GFileAttributeValue *value;
1054 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1055 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1057 value = g_file_info_find_value_by_name (info, attribute);
1058 return _g_file_attribute_value_get_int64 (value);
1061 static GFileAttributeValue *
1062 g_file_info_create_value (GFileInfo *info,
1065 GFileAttribute *attrs;
1068 if (info->mask != NO_ATTRIBUTE_MASK &&
1069 !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
1072 i = g_file_info_find_place (info, attr_id);
1074 attrs = (GFileAttribute *)info->attributes->data;
1075 if (i < info->attributes->len &&
1076 attrs[i].attribute == attr_id)
1077 return &attrs[i].value;
1080 GFileAttribute attr = { 0 };
1081 attr.attribute = attr_id;
1082 g_array_insert_val (info->attributes, i, attr);
1084 attrs = (GFileAttribute *)info->attributes->data;
1085 return &attrs[i].value;
1090 _g_file_info_set_attribute_by_id (GFileInfo *info,
1092 GFileAttributeType type,
1095 GFileAttributeValue *value;
1097 value = g_file_info_create_value (info, attribute);
1100 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1104 * g_file_info_set_attribute:
1105 * @info: a #GFileInfo.
1106 * @attribute: a file attribute key.
1107 * @type: a #GFileAttributeType
1108 * @value_p: pointer to the value
1110 * Sets the @attribute to contain the given value, if possible.
1113 g_file_info_set_attribute (GFileInfo *info,
1114 const char *attribute,
1115 GFileAttributeType type,
1118 g_return_if_fail (G_IS_FILE_INFO (info));
1119 g_return_if_fail (attribute != NULL && *attribute != '\0');
1121 _g_file_info_set_attribute_by_id (info, lookup_attribute (attribute), type, value_p);
1125 _g_file_info_set_attribute_object_by_id (GFileInfo *info,
1127 GObject *attr_value)
1129 GFileAttributeValue *value;
1131 value = g_file_info_create_value (info, attribute);
1133 _g_file_attribute_value_set_object (value, attr_value);
1137 * g_file_info_set_attribute_object:
1138 * @info: a #GFileInfo.
1139 * @attribute: a file attribute key.
1140 * @attr_value: a #GObject.
1142 * Sets the @attribute to contain the given @attr_value,
1146 g_file_info_set_attribute_object (GFileInfo *info,
1147 const char *attribute,
1148 GObject *attr_value)
1150 g_return_if_fail (G_IS_FILE_INFO (info));
1151 g_return_if_fail (attribute != NULL && *attribute != '\0');
1152 g_return_if_fail (G_IS_OBJECT (attr_value));
1154 _g_file_info_set_attribute_object_by_id (info,
1155 lookup_attribute (attribute),
1160 _g_file_info_set_attribute_stringv_by_id (GFileInfo *info,
1164 GFileAttributeValue *value;
1166 value = g_file_info_create_value (info, attribute);
1168 _g_file_attribute_value_set_stringv (value, attr_value);
1172 * g_file_info_set_attribute_stringv:
1173 * @info: a #GFileInfo.
1174 * @attribute: a file attribute key
1175 * @attr_value: a %NULL terminated array of UTF-8 strings.
1177 * Sets the @attribute to contain the given @attr_value,
1183 g_file_info_set_attribute_stringv (GFileInfo *info,
1184 const char *attribute,
1187 g_return_if_fail (G_IS_FILE_INFO (info));
1188 g_return_if_fail (attribute != NULL && *attribute != '\0');
1189 g_return_if_fail (attr_value != NULL);
1191 _g_file_info_set_attribute_stringv_by_id (info,
1192 lookup_attribute (attribute),
1197 _g_file_info_set_attribute_string_by_id (GFileInfo *info,
1199 const char *attr_value)
1201 GFileAttributeValue *value;
1203 value = g_file_info_create_value (info, attribute);
1205 _g_file_attribute_value_set_string (value, attr_value);
1209 * g_file_info_set_attribute_string:
1210 * @info: a #GFileInfo.
1211 * @attribute: a file attribute key.
1212 * @attr_value: a UTF-8 string.
1214 * Sets the @attribute to contain the given @attr_value,
1218 g_file_info_set_attribute_string (GFileInfo *info,
1219 const char *attribute,
1220 const char *attr_value)
1222 g_return_if_fail (G_IS_FILE_INFO (info));
1223 g_return_if_fail (attribute != NULL && *attribute != '\0');
1224 g_return_if_fail (attr_value != NULL);
1226 _g_file_info_set_attribute_string_by_id (info,
1227 lookup_attribute (attribute),
1232 _g_file_info_set_attribute_byte_string_by_id (GFileInfo *info,
1234 const char *attr_value)
1236 GFileAttributeValue *value;
1238 value = g_file_info_create_value (info, attribute);
1240 _g_file_attribute_value_set_byte_string (value, attr_value);
1244 * g_file_info_set_attribute_byte_string:
1245 * @info: a #GFileInfo.
1246 * @attribute: a file attribute key.
1247 * @attr_value: a byte string.
1249 * Sets the @attribute to contain the given @attr_value,
1253 g_file_info_set_attribute_byte_string (GFileInfo *info,
1254 const char *attribute,
1255 const char *attr_value)
1257 g_return_if_fail (G_IS_FILE_INFO (info));
1258 g_return_if_fail (attribute != NULL && *attribute != '\0');
1259 g_return_if_fail (attr_value != NULL);
1261 _g_file_info_set_attribute_byte_string_by_id (info,
1262 lookup_attribute (attribute),
1267 _g_file_info_set_attribute_boolean_by_id (GFileInfo *info,
1269 gboolean attr_value)
1271 GFileAttributeValue *value;
1273 value = g_file_info_create_value (info, attribute);
1275 _g_file_attribute_value_set_boolean (value, attr_value);
1279 * g_file_info_set_attribute_boolean:
1280 * @info: a #GFileInfo.
1281 * @attribute: a file attribute key.
1282 * @attr_value: a boolean value.
1284 * Sets the @attribute to contain the given @attr_value,
1288 g_file_info_set_attribute_boolean (GFileInfo *info,
1289 const char *attribute,
1290 gboolean attr_value)
1292 g_return_if_fail (G_IS_FILE_INFO (info));
1293 g_return_if_fail (attribute != NULL && *attribute != '\0');
1295 _g_file_info_set_attribute_boolean_by_id (info,
1296 lookup_attribute (attribute),
1301 _g_file_info_set_attribute_uint32_by_id (GFileInfo *info,
1305 GFileAttributeValue *value;
1307 value = g_file_info_create_value (info, attribute);
1309 _g_file_attribute_value_set_uint32 (value, attr_value);
1313 * g_file_info_set_attribute_uint32:
1314 * @info: a #GFileInfo.
1315 * @attribute: a file attribute key.
1316 * @attr_value: an unsigned 32-bit integer.
1318 * Sets the @attribute to contain the given @attr_value,
1322 g_file_info_set_attribute_uint32 (GFileInfo *info,
1323 const char *attribute,
1326 g_return_if_fail (G_IS_FILE_INFO (info));
1327 g_return_if_fail (attribute != NULL && *attribute != '\0');
1329 _g_file_info_set_attribute_uint32_by_id (info,
1330 lookup_attribute (attribute),
1335 _g_file_info_set_attribute_int32_by_id (GFileInfo *info,
1339 GFileAttributeValue *value;
1341 value = g_file_info_create_value (info, attribute);
1343 _g_file_attribute_value_set_int32 (value, attr_value);
1347 * g_file_info_set_attribute_int32:
1348 * @info: a #GFileInfo.
1349 * @attribute: a file attribute key.
1350 * @attr_value: a signed 32-bit integer
1352 * Sets the @attribute to contain the given @attr_value,
1356 g_file_info_set_attribute_int32 (GFileInfo *info,
1357 const char *attribute,
1360 g_return_if_fail (G_IS_FILE_INFO (info));
1361 g_return_if_fail (attribute != NULL && *attribute != '\0');
1363 _g_file_info_set_attribute_int32_by_id (info,
1364 lookup_attribute (attribute),
1369 _g_file_info_set_attribute_uint64_by_id (GFileInfo *info,
1373 GFileAttributeValue *value;
1375 value = g_file_info_create_value (info, attribute);
1377 _g_file_attribute_value_set_uint64 (value, attr_value);
1381 * g_file_info_set_attribute_uint64:
1382 * @info: a #GFileInfo.
1383 * @attribute: a file attribute key.
1384 * @attr_value: an unsigned 64-bit integer.
1386 * Sets the @attribute to contain the given @attr_value,
1390 g_file_info_set_attribute_uint64 (GFileInfo *info,
1391 const char *attribute,
1394 g_return_if_fail (G_IS_FILE_INFO (info));
1395 g_return_if_fail (attribute != NULL && *attribute != '\0');
1397 _g_file_info_set_attribute_uint64_by_id (info,
1398 lookup_attribute (attribute),
1403 _g_file_info_set_attribute_int64_by_id (GFileInfo *info,
1407 GFileAttributeValue *value;
1409 value = g_file_info_create_value (info, attribute);
1411 _g_file_attribute_value_set_int64 (value, attr_value);
1415 * g_file_info_set_attribute_int64:
1416 * @info: a #GFileInfo.
1417 * @attribute: attribute name to set.
1418 * @attr_value: int64 value to set attribute to.
1420 * Sets the @attribute to contain the given @attr_value,
1425 g_file_info_set_attribute_int64 (GFileInfo *info,
1426 const char *attribute,
1429 g_return_if_fail (G_IS_FILE_INFO (info));
1430 g_return_if_fail (attribute != NULL && *attribute != '\0');
1432 _g_file_info_set_attribute_int64_by_id (info,
1433 lookup_attribute (attribute),
1437 /* Helper getters */
1439 * g_file_info_get_file_type:
1440 * @info: a #GFileInfo.
1442 * Gets a file's type (whether it is a regular file, symlink, etc).
1443 * This is different from the file's content type, see g_file_info_get_content_type().
1445 * Returns: a #GFileType for the given file.
1448 g_file_info_get_file_type (GFileInfo *info)
1450 static guint32 attr = 0;
1451 GFileAttributeValue *value;
1453 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1456 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1458 value = g_file_info_find_value (info, attr);
1459 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1463 * g_file_info_get_is_hidden:
1464 * @info: a #GFileInfo.
1466 * Checks if a file is hidden.
1468 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1471 g_file_info_get_is_hidden (GFileInfo *info)
1473 static guint32 attr = 0;
1474 GFileAttributeValue *value;
1476 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1479 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1481 value = g_file_info_find_value (info, attr);
1482 return (GFileType)_g_file_attribute_value_get_boolean (value);
1486 * g_file_info_get_is_backup:
1487 * @info: a #GFileInfo.
1489 * Checks if a file is a backup file.
1491 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1494 g_file_info_get_is_backup (GFileInfo *info)
1496 static guint32 attr = 0;
1497 GFileAttributeValue *value;
1499 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1502 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1504 value = g_file_info_find_value (info, attr);
1505 return (GFileType)_g_file_attribute_value_get_boolean (value);
1509 * g_file_info_get_is_symlink:
1510 * @info: a #GFileInfo.
1512 * Checks if a file is a symlink.
1514 * Returns: %TRUE if the given @info is a symlink.
1517 g_file_info_get_is_symlink (GFileInfo *info)
1519 static guint32 attr = 0;
1520 GFileAttributeValue *value;
1522 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1525 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1527 value = g_file_info_find_value (info, attr);
1528 return (GFileType)_g_file_attribute_value_get_boolean (value);
1532 * g_file_info_get_name:
1533 * @info: a #GFileInfo.
1535 * Gets the name for a file.
1537 * Returns: a string containing the file name.
1540 g_file_info_get_name (GFileInfo *info)
1542 static guint32 attr = 0;
1543 GFileAttributeValue *value;
1545 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1548 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1550 value = g_file_info_find_value (info, attr);
1551 return _g_file_attribute_value_get_byte_string (value);
1555 * g_file_info_get_display_name:
1556 * @info: a #GFileInfo.
1558 * Gets a display name for a file.
1560 * Returns: a string containing the display name.
1563 g_file_info_get_display_name (GFileInfo *info)
1565 static guint32 attr = 0;
1566 GFileAttributeValue *value;
1568 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1571 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1573 value = g_file_info_find_value (info, attr);
1574 return _g_file_attribute_value_get_string (value);
1578 * g_file_info_get_edit_name:
1579 * @info: a #GFileInfo.
1581 * Gets the edit name for a file.
1583 * Returns: a string containing the edit name.
1586 g_file_info_get_edit_name (GFileInfo *info)
1588 static guint32 attr = 0;
1589 GFileAttributeValue *value;
1591 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1594 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1596 value = g_file_info_find_value (info, attr);
1597 return _g_file_attribute_value_get_string (value);
1601 * g_file_info_get_icon:
1602 * @info: a #GFileInfo.
1604 * Gets the icon for a file.
1606 * Returns: (transfer none): #GIcon for the given @info.
1609 g_file_info_get_icon (GFileInfo *info)
1611 static guint32 attr = 0;
1612 GFileAttributeValue *value;
1615 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1618 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1620 value = g_file_info_find_value (info, attr);
1621 obj = _g_file_attribute_value_get_object (value);
1622 if (G_IS_ICON (obj))
1623 return G_ICON (obj);
1628 * g_file_info_get_content_type:
1629 * @info: a #GFileInfo.
1631 * Gets the file's content type.
1633 * Returns: a string containing the file's content type.
1636 g_file_info_get_content_type (GFileInfo *info)
1638 static guint32 attr = 0;
1639 GFileAttributeValue *value;
1641 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1644 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1646 value = g_file_info_find_value (info, attr);
1647 return _g_file_attribute_value_get_string (value);
1651 * g_file_info_get_size:
1652 * @info: a #GFileInfo.
1654 * Gets the file's size.
1656 * Returns: a #goffset containing the file's size.
1659 g_file_info_get_size (GFileInfo *info)
1661 static guint32 attr = 0;
1662 GFileAttributeValue *value;
1664 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1667 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1669 value = g_file_info_find_value (info, attr);
1670 return (goffset) _g_file_attribute_value_get_uint64 (value);
1674 * g_file_info_get_modification_time:
1675 * @info: a #GFileInfo.
1676 * @result: a #GTimeVal.
1678 * Gets the modification time of the current @info and sets it
1682 g_file_info_get_modification_time (GFileInfo *info,
1685 static guint32 attr_mtime = 0, attr_mtime_usec;
1686 GFileAttributeValue *value;
1688 g_return_if_fail (G_IS_FILE_INFO (info));
1689 g_return_if_fail (result != NULL);
1691 if (attr_mtime == 0)
1693 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1694 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1697 value = g_file_info_find_value (info, attr_mtime);
1698 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1699 value = g_file_info_find_value (info, attr_mtime_usec);
1700 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1704 * g_file_info_get_symlink_target:
1705 * @info: a #GFileInfo.
1707 * Gets the symlink target for a given #GFileInfo.
1709 * Returns: a string containing the symlink target.
1712 g_file_info_get_symlink_target (GFileInfo *info)
1714 static guint32 attr = 0;
1715 GFileAttributeValue *value;
1717 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1720 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1722 value = g_file_info_find_value (info, attr);
1723 return _g_file_attribute_value_get_byte_string (value);
1727 * g_file_info_get_etag:
1728 * @info: a #GFileInfo.
1730 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1731 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1733 * Returns: a string containing the value of the "etag:value" attribute.
1736 g_file_info_get_etag (GFileInfo *info)
1738 static guint32 attr = 0;
1739 GFileAttributeValue *value;
1741 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1744 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1746 value = g_file_info_find_value (info, attr);
1747 return _g_file_attribute_value_get_string (value);
1751 * g_file_info_get_sort_order:
1752 * @info: a #GFileInfo.
1754 * Gets the value of the sort_order attribute from the #GFileInfo.
1755 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1757 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1760 g_file_info_get_sort_order (GFileInfo *info)
1762 static guint32 attr = 0;
1763 GFileAttributeValue *value;
1765 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1768 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1770 value = g_file_info_find_value (info, attr);
1771 return _g_file_attribute_value_get_int32 (value);
1774 /* Helper setters: */
1776 * g_file_info_set_file_type:
1777 * @info: a #GFileInfo.
1778 * @type: a #GFileType.
1780 * Sets the file type in a #GFileInfo to @type.
1781 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1784 g_file_info_set_file_type (GFileInfo *info,
1787 static guint32 attr = 0;
1788 GFileAttributeValue *value;
1790 g_return_if_fail (G_IS_FILE_INFO (info));
1793 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1795 value = g_file_info_create_value (info, attr);
1797 _g_file_attribute_value_set_uint32 (value, type);
1801 * g_file_info_set_is_hidden:
1802 * @info: a #GFileInfo.
1803 * @is_hidden: a #gboolean.
1805 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1806 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1809 g_file_info_set_is_hidden (GFileInfo *info,
1812 static guint32 attr = 0;
1813 GFileAttributeValue *value;
1815 g_return_if_fail (G_IS_FILE_INFO (info));
1818 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1820 value = g_file_info_create_value (info, attr);
1822 _g_file_attribute_value_set_boolean (value, is_hidden);
1826 * g_file_info_set_is_symlink:
1827 * @info: a #GFileInfo.
1828 * @is_symlink: a #gboolean.
1830 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1831 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1834 g_file_info_set_is_symlink (GFileInfo *info,
1835 gboolean is_symlink)
1837 static guint32 attr = 0;
1838 GFileAttributeValue *value;
1840 g_return_if_fail (G_IS_FILE_INFO (info));
1843 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1845 value = g_file_info_create_value (info, attr);
1847 _g_file_attribute_value_set_boolean (value, is_symlink);
1851 * g_file_info_set_name:
1852 * @info: a #GFileInfo.
1853 * @name: a string containing a name.
1855 * Sets the name attribute for the current #GFileInfo.
1856 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1859 g_file_info_set_name (GFileInfo *info,
1862 static guint32 attr = 0;
1863 GFileAttributeValue *value;
1865 g_return_if_fail (G_IS_FILE_INFO (info));
1866 g_return_if_fail (name != NULL);
1869 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1871 value = g_file_info_create_value (info, attr);
1873 _g_file_attribute_value_set_byte_string (value, name);
1877 * g_file_info_set_display_name:
1878 * @info: a #GFileInfo.
1879 * @display_name: a string containing a display name.
1881 * Sets the display name for the current #GFileInfo.
1882 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1885 g_file_info_set_display_name (GFileInfo *info,
1886 const char *display_name)
1888 static guint32 attr = 0;
1889 GFileAttributeValue *value;
1891 g_return_if_fail (G_IS_FILE_INFO (info));
1892 g_return_if_fail (display_name != NULL);
1895 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1897 value = g_file_info_create_value (info, attr);
1899 _g_file_attribute_value_set_string (value, display_name);
1903 * g_file_info_set_edit_name:
1904 * @info: a #GFileInfo.
1905 * @edit_name: a string containing an edit name.
1907 * Sets the edit name for the current file.
1908 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1911 g_file_info_set_edit_name (GFileInfo *info,
1912 const char *edit_name)
1914 static guint32 attr = 0;
1915 GFileAttributeValue *value;
1917 g_return_if_fail (G_IS_FILE_INFO (info));
1918 g_return_if_fail (edit_name != NULL);
1921 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1923 value = g_file_info_create_value (info, attr);
1925 _g_file_attribute_value_set_string (value, edit_name);
1929 * g_file_info_set_icon:
1930 * @info: a #GFileInfo.
1933 * Sets the icon for a given #GFileInfo.
1934 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1937 g_file_info_set_icon (GFileInfo *info,
1940 static guint32 attr = 0;
1941 GFileAttributeValue *value;
1943 g_return_if_fail (G_IS_FILE_INFO (info));
1944 g_return_if_fail (G_IS_ICON (icon));
1947 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1949 value = g_file_info_create_value (info, attr);
1951 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1955 * g_file_info_set_content_type:
1956 * @info: a #GFileInfo.
1957 * @content_type: a content type. See <link linkend="gio-GContentType">GContentType</link>.
1959 * Sets the content type attribute for a given #GFileInfo.
1960 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1963 g_file_info_set_content_type (GFileInfo *info,
1964 const char *content_type)
1966 static guint32 attr = 0;
1967 GFileAttributeValue *value;
1969 g_return_if_fail (G_IS_FILE_INFO (info));
1970 g_return_if_fail (content_type != NULL);
1973 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1975 value = g_file_info_create_value (info, attr);
1977 _g_file_attribute_value_set_string (value, content_type);
1981 * g_file_info_set_size:
1982 * @info: a #GFileInfo.
1983 * @size: a #goffset containing the file's size.
1985 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1986 * to the given size.
1989 g_file_info_set_size (GFileInfo *info,
1992 static guint32 attr = 0;
1993 GFileAttributeValue *value;
1995 g_return_if_fail (G_IS_FILE_INFO (info));
1998 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
2000 value = g_file_info_create_value (info, attr);
2002 _g_file_attribute_value_set_uint64 (value, size);
2006 * g_file_info_set_modification_time
2007 * @info: a #GFileInfo.
2008 * @mtime: a #GTimeVal.
2010 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
2011 * info to the given time value.
2014 g_file_info_set_modification_time (GFileInfo *info,
2017 static guint32 attr_mtime = 0, attr_mtime_usec;
2018 GFileAttributeValue *value;
2020 g_return_if_fail (G_IS_FILE_INFO (info));
2021 g_return_if_fail (mtime != NULL);
2023 if (attr_mtime == 0)
2025 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
2026 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2029 value = g_file_info_create_value (info, attr_mtime);
2031 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
2032 value = g_file_info_create_value (info, attr_mtime_usec);
2034 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
2038 * g_file_info_set_symlink_target:
2039 * @info: a #GFileInfo.
2040 * @symlink_target: a static string containing a path to a symlink target.
2042 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
2043 * to the given symlink target.
2046 g_file_info_set_symlink_target (GFileInfo *info,
2047 const char *symlink_target)
2049 static guint32 attr = 0;
2050 GFileAttributeValue *value;
2052 g_return_if_fail (G_IS_FILE_INFO (info));
2053 g_return_if_fail (symlink_target != NULL);
2056 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2058 value = g_file_info_create_value (info, attr);
2060 _g_file_attribute_value_set_byte_string (value, symlink_target);
2064 * g_file_info_set_sort_order:
2065 * @info: a #GFileInfo.
2066 * @sort_order: a sort order integer.
2068 * Sets the sort order attribute in the file info structure. See
2069 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
2072 g_file_info_set_sort_order (GFileInfo *info,
2075 static guint32 attr = 0;
2076 GFileAttributeValue *value;
2078 g_return_if_fail (G_IS_FILE_INFO (info));
2081 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
2083 value = g_file_info_create_value (info, attr);
2085 _g_file_attribute_value_set_int32 (value, sort_order);
2089 #define ON_STACK_MATCHERS 5
2096 struct _GFileAttributeMatcher {
2098 SubMatcher sub_matchers[ON_STACK_MATCHERS];
2101 GArray *more_sub_matchers;
2104 guint32 iterator_ns;
2109 matcher_add (GFileAttributeMatcher *matcher,
2113 SubMatcher *sub_matchers;
2117 for (i = 0; i < ON_STACK_MATCHERS; i++)
2119 /* First empty spot, not found, use this */
2120 if (matcher->sub_matchers[i].id == 0)
2122 matcher->sub_matchers[i].id = id;
2123 matcher->sub_matchers[i].mask = mask;
2128 if (matcher->sub_matchers[i].id == id &&
2129 matcher->sub_matchers[i].mask == mask)
2133 if (matcher->more_sub_matchers == NULL)
2134 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2136 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2137 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2140 if (sub_matchers[i].id == id &&
2141 sub_matchers[i].mask == mask)
2148 g_array_append_val (matcher->more_sub_matchers, s);
2151 G_DEFINE_BOXED_TYPE (GFileAttributeMatcher, g_file_attribute_matcher,
2152 g_file_attribute_matcher_ref,
2153 g_file_attribute_matcher_unref)
2156 * g_file_attribute_matcher_new:
2157 * @attributes: an attribute string to match.
2159 * Creates a new file attribute matcher, which matches attributes
2160 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
2161 * counted structures, and are created with a reference count of 1. If
2162 * the number of references falls to 0, the #GFileAttributeMatcher is
2163 * automatically destroyed.
2165 * The @attribute string should be formatted with specific keys separated
2166 * from namespaces with a double colon. Several "namespace::key" strings may be
2167 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
2168 * The wildcard "*" may be used to match all keys and namespaces, or
2169 * "namespace::*" will match all keys in a given namespace.
2171 * Examples of strings to use:
2173 * <title>File Attribute Matcher strings and results</title>
2174 * <tgroup cols='2' align='left'><thead>
2175 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
2177 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
2178 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
2179 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
2180 * all keys in the unix namespace.</entry></row>
2184 * Returns: a #GFileAttributeMatcher.
2186 GFileAttributeMatcher *
2187 g_file_attribute_matcher_new (const char *attributes)
2192 GFileAttributeMatcher *matcher;
2194 if (attributes == NULL || *attributes == '\0')
2197 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2200 split = g_strsplit (attributes, ",", -1);
2202 for (i = 0; split[i] != NULL; i++)
2204 if (strcmp (split[i], "*") == 0)
2205 matcher->all = TRUE;
2210 colon = strstr (split[i], "::");
2211 if (colon != NULL &&
2216 id = lookup_attribute (split[i]);
2224 id = lookup_namespace (split[i]) << NS_POS;
2225 mask = NS_MASK << NS_POS;
2228 matcher_add (matcher, id, mask);
2238 * g_file_attribute_matcher_ref:
2239 * @matcher: a #GFileAttributeMatcher.
2241 * References a file attribute matcher.
2243 * Returns: a #GFileAttributeMatcher.
2245 GFileAttributeMatcher *
2246 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2250 g_return_val_if_fail (matcher->ref > 0, NULL);
2251 g_atomic_int_inc (&matcher->ref);
2257 * g_file_attribute_matcher_unref:
2258 * @matcher: a #GFileAttributeMatcher.
2260 * Unreferences @matcher. If the reference count falls below 1,
2261 * the @matcher is automatically freed.
2265 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2269 g_return_if_fail (matcher->ref > 0);
2271 if (g_atomic_int_dec_and_test (&matcher->ref))
2273 if (matcher->more_sub_matchers)
2274 g_array_free (matcher->more_sub_matchers, TRUE);
2282 * g_file_attribute_matcher_matches_only:
2283 * @matcher: a #GFileAttributeMatcher.
2284 * @attribute: a file attribute key.
2286 * Checks if a attribute matcher only matches a given attribute. Always
2287 * returns %FALSE if "*" was used when creating the matcher.
2289 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2292 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2293 const char *attribute)
2297 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2299 if (matcher == NULL ||
2303 id = lookup_attribute (attribute);
2305 if (matcher->sub_matchers[0].id != 0 &&
2306 matcher->sub_matchers[1].id == 0 &&
2307 matcher->sub_matchers[0].mask == 0xffffffff &&
2308 matcher->sub_matchers[0].id == id)
2315 matcher_matches_id (GFileAttributeMatcher *matcher,
2318 SubMatcher *sub_matchers;
2321 for (i = 0; i < ON_STACK_MATCHERS; i++)
2323 if (matcher->sub_matchers[i].id == 0)
2326 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2330 if (matcher->more_sub_matchers)
2332 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2333 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2335 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2344 _g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2347 /* We return a NULL matcher for an empty match string, so handle this */
2348 if (matcher == NULL)
2354 return matcher_matches_id (matcher, id);
2358 * g_file_attribute_matcher_matches:
2359 * @matcher: a #GFileAttributeMatcher.
2360 * @attribute: a file attribute key.
2362 * Checks if an attribute will be matched by an attribute matcher. If
2363 * the matcher was created with the "*" matching string, this function
2364 * will always return %TRUE.
2366 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2369 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2370 const char *attribute)
2372 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2374 /* We return a NULL matcher for an empty match string, so handle this */
2375 if (matcher == NULL)
2381 return matcher_matches_id (matcher, lookup_attribute (attribute));
2384 /* return TRUE -> all */
2386 * g_file_attribute_matcher_enumerate_namespace:
2387 * @matcher: a #GFileAttributeMatcher.
2388 * @ns: a string containing a file attribute namespace.
2390 * Checks if the matcher will match all of the keys in a given namespace.
2391 * This will always return %TRUE if a wildcard character is in use (e.g. if
2392 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2393 * using "*" and namespace is anything.)
2395 * TODO: this is awkwardly worded.
2397 * Returns: %TRUE if the matcher matches all of the entries
2398 * in the given @ns, %FALSE otherwise.
2401 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2404 SubMatcher *sub_matchers;
2408 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2410 /* We return a NULL matcher for an empty match string, so handle this */
2411 if (matcher == NULL)
2417 ns_id = lookup_namespace (ns) << NS_POS;
2419 for (i = 0; i < ON_STACK_MATCHERS; i++)
2421 if (matcher->sub_matchers[i].id == ns_id)
2425 if (matcher->more_sub_matchers)
2427 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2428 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2430 if (sub_matchers[i].id == ns_id)
2435 matcher->iterator_ns = ns_id;
2436 matcher->iterator_pos = 0;
2442 * g_file_attribute_matcher_enumerate_next:
2443 * @matcher: a #GFileAttributeMatcher.
2445 * Gets the next matched attribute from a #GFileAttributeMatcher.
2447 * Returns: a string containing the next attribute or %NULL if
2448 * no more attribute exist.
2451 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2454 SubMatcher *sub_matcher;
2456 /* We return a NULL matcher for an empty match string, so handle this */
2457 if (matcher == NULL)
2462 i = matcher->iterator_pos++;
2464 if (i < ON_STACK_MATCHERS)
2466 if (matcher->sub_matchers[i].id == 0)
2469 sub_matcher = &matcher->sub_matchers[i];
2473 if (matcher->more_sub_matchers == NULL)
2476 i -= ON_STACK_MATCHERS;
2477 if (i < matcher->more_sub_matchers->len)
2478 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2483 if (sub_matcher->mask == 0xffffffff &&
2484 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2485 return get_attribute_for_id (sub_matcher->id);