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] = g_new (char *, 1);
136 attributes[ns_info->id][0] = g_strconcat (namespace, "::*", NULL);
142 _lookup_attribute (const char *attribute)
149 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
154 colon = strstr (attribute, "::");
156 ns = g_strndup (attribute, colon - attribute);
160 ns_info = _lookup_namespace (ns);
163 id = ++ns_info->attribute_id_counter;
164 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
165 attributes[ns_info->id][id] = g_strdup (attribute);
167 attr_id = MAKE_ATTR_ID (ns_info->id, id);
169 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
175 ensure_attribute_hash (void)
177 if (attribute_hash != NULL)
180 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
181 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
183 #define REGISTER_ATTRIBUTE(name) G_STMT_START{\
184 guint _u = _lookup_attribute (G_FILE_ATTRIBUTE_ ## name); \
185 /* use for generating the ID: g_print ("#define G_FILE_ATTRIBUTE_ID_%s (%u + %u)\n", #name + 17, _u & ~ID_MASK, _u & ID_MASK); */ \
186 g_assert (_u == G_FILE_ATTRIBUTE_ID_ ## name); \
189 REGISTER_ATTRIBUTE (STANDARD_TYPE);
190 REGISTER_ATTRIBUTE (STANDARD_IS_HIDDEN);
191 REGISTER_ATTRIBUTE (STANDARD_IS_BACKUP);
192 REGISTER_ATTRIBUTE (STANDARD_IS_SYMLINK);
193 REGISTER_ATTRIBUTE (STANDARD_IS_VIRTUAL);
194 REGISTER_ATTRIBUTE (STANDARD_NAME);
195 REGISTER_ATTRIBUTE (STANDARD_DISPLAY_NAME);
196 REGISTER_ATTRIBUTE (STANDARD_EDIT_NAME);
197 REGISTER_ATTRIBUTE (STANDARD_COPY_NAME);
198 REGISTER_ATTRIBUTE (STANDARD_DESCRIPTION);
199 REGISTER_ATTRIBUTE (STANDARD_ICON);
200 REGISTER_ATTRIBUTE (STANDARD_CONTENT_TYPE);
201 REGISTER_ATTRIBUTE (STANDARD_FAST_CONTENT_TYPE);
202 REGISTER_ATTRIBUTE (STANDARD_SIZE);
203 REGISTER_ATTRIBUTE (STANDARD_ALLOCATED_SIZE);
204 REGISTER_ATTRIBUTE (STANDARD_SYMLINK_TARGET);
205 REGISTER_ATTRIBUTE (STANDARD_TARGET_URI);
206 REGISTER_ATTRIBUTE (STANDARD_SORT_ORDER);
207 REGISTER_ATTRIBUTE (ETAG_VALUE);
208 REGISTER_ATTRIBUTE (ID_FILE);
209 REGISTER_ATTRIBUTE (ID_FILESYSTEM);
210 REGISTER_ATTRIBUTE (ACCESS_CAN_READ);
211 REGISTER_ATTRIBUTE (ACCESS_CAN_WRITE);
212 REGISTER_ATTRIBUTE (ACCESS_CAN_EXECUTE);
213 REGISTER_ATTRIBUTE (ACCESS_CAN_DELETE);
214 REGISTER_ATTRIBUTE (ACCESS_CAN_TRASH);
215 REGISTER_ATTRIBUTE (ACCESS_CAN_RENAME);
216 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_MOUNT);
217 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_UNMOUNT);
218 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_EJECT);
219 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE);
220 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE_FILE);
221 REGISTER_ATTRIBUTE (MOUNTABLE_HAL_UDI);
222 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START);
223 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START_DEGRADED);
224 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_STOP);
225 REGISTER_ATTRIBUTE (MOUNTABLE_START_STOP_TYPE);
226 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_POLL);
227 REGISTER_ATTRIBUTE (MOUNTABLE_IS_MEDIA_CHECK_AUTOMATIC);
228 REGISTER_ATTRIBUTE (TIME_MODIFIED);
229 REGISTER_ATTRIBUTE (TIME_MODIFIED_USEC);
230 REGISTER_ATTRIBUTE (TIME_ACCESS);
231 REGISTER_ATTRIBUTE (TIME_ACCESS_USEC);
232 REGISTER_ATTRIBUTE (TIME_CHANGED);
233 REGISTER_ATTRIBUTE (TIME_CHANGED_USEC);
234 REGISTER_ATTRIBUTE (TIME_CREATED);
235 REGISTER_ATTRIBUTE (TIME_CREATED_USEC);
236 REGISTER_ATTRIBUTE (UNIX_DEVICE);
237 REGISTER_ATTRIBUTE (UNIX_INODE);
238 REGISTER_ATTRIBUTE (UNIX_MODE);
239 REGISTER_ATTRIBUTE (UNIX_NLINK);
240 REGISTER_ATTRIBUTE (UNIX_UID);
241 REGISTER_ATTRIBUTE (UNIX_GID);
242 REGISTER_ATTRIBUTE (UNIX_RDEV);
243 REGISTER_ATTRIBUTE (UNIX_BLOCK_SIZE);
244 REGISTER_ATTRIBUTE (UNIX_BLOCKS);
245 REGISTER_ATTRIBUTE (UNIX_IS_MOUNTPOINT);
246 REGISTER_ATTRIBUTE (DOS_IS_ARCHIVE);
247 REGISTER_ATTRIBUTE (DOS_IS_SYSTEM);
248 REGISTER_ATTRIBUTE (OWNER_USER);
249 REGISTER_ATTRIBUTE (OWNER_USER_REAL);
250 REGISTER_ATTRIBUTE (OWNER_GROUP);
251 REGISTER_ATTRIBUTE (THUMBNAIL_PATH);
252 REGISTER_ATTRIBUTE (THUMBNAILING_FAILED);
253 REGISTER_ATTRIBUTE (PREVIEW_ICON);
254 REGISTER_ATTRIBUTE (FILESYSTEM_SIZE);
255 REGISTER_ATTRIBUTE (FILESYSTEM_FREE);
256 REGISTER_ATTRIBUTE (FILESYSTEM_TYPE);
257 REGISTER_ATTRIBUTE (FILESYSTEM_READONLY);
258 REGISTER_ATTRIBUTE (FILESYSTEM_USE_PREVIEW);
259 REGISTER_ATTRIBUTE (GVFS_BACKEND);
260 REGISTER_ATTRIBUTE (SELINUX_CONTEXT);
261 REGISTER_ATTRIBUTE (TRASH_ITEM_COUNT);
262 REGISTER_ATTRIBUTE (TRASH_ORIG_PATH);
263 REGISTER_ATTRIBUTE (TRASH_DELETION_DATE);
265 #undef REGISTER_ATTRIBUTE
269 lookup_namespace (const char *namespace)
274 G_LOCK (attribute_hash);
276 ensure_attribute_hash ();
278 ns_info = _lookup_namespace (namespace);
283 G_UNLOCK (attribute_hash);
289 get_attribute_for_id (int attribute)
292 G_LOCK (attribute_hash);
293 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
294 G_UNLOCK (attribute_hash);
299 lookup_attribute (const char *attribute)
303 G_LOCK (attribute_hash);
304 ensure_attribute_hash ();
306 attr_id = _lookup_attribute (attribute);
308 G_UNLOCK (attribute_hash);
314 g_file_info_finalize (GObject *object)
318 GFileAttribute *attrs;
320 info = G_FILE_INFO (object);
322 attrs = (GFileAttribute *)info->attributes->data;
323 for (i = 0; i < info->attributes->len; i++)
324 _g_file_attribute_value_clear (&attrs[i].value);
325 g_array_free (info->attributes, TRUE);
327 if (info->mask != NO_ATTRIBUTE_MASK)
328 g_file_attribute_matcher_unref (info->mask);
330 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
334 g_file_info_class_init (GFileInfoClass *klass)
336 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
338 gobject_class->finalize = g_file_info_finalize;
342 g_file_info_init (GFileInfo *info)
344 info->mask = NO_ATTRIBUTE_MASK;
345 info->attributes = g_array_new (FALSE, FALSE,
346 sizeof (GFileAttribute));
352 * Creates a new file info structure.
354 * Returns: a #GFileInfo.
357 g_file_info_new (void)
359 return g_object_new (G_TYPE_FILE_INFO, NULL);
363 * g_file_info_copy_into:
364 * @src_info: source to copy attributes from.
365 * @dest_info: destination to copy attributes to.
367 * Copies all of the <link linkend="gio-GFileAttribute">GFileAttribute</link>s
368 * from @src_info to @dest_info.
371 g_file_info_copy_into (GFileInfo *src_info,
372 GFileInfo *dest_info)
374 GFileAttribute *source, *dest;
377 g_return_if_fail (G_IS_FILE_INFO (src_info));
378 g_return_if_fail (G_IS_FILE_INFO (dest_info));
380 dest = (GFileAttribute *)dest_info->attributes->data;
381 for (i = 0; i < dest_info->attributes->len; i++)
382 _g_file_attribute_value_clear (&dest[i].value);
384 g_array_set_size (dest_info->attributes,
385 src_info->attributes->len);
387 source = (GFileAttribute *)src_info->attributes->data;
388 dest = (GFileAttribute *)dest_info->attributes->data;
390 for (i = 0; i < src_info->attributes->len; i++)
392 dest[i].attribute = source[i].attribute;
393 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
394 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
397 if (dest_info->mask != NO_ATTRIBUTE_MASK)
398 g_file_attribute_matcher_unref (dest_info->mask);
400 if (src_info->mask == NO_ATTRIBUTE_MASK)
401 dest_info->mask = NO_ATTRIBUTE_MASK;
403 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
408 * @other: a #GFileInfo.
410 * Duplicates a file info structure.
412 * Returns: (transfer full): a duplicate #GFileInfo of @other.
415 g_file_info_dup (GFileInfo *other)
419 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
421 new = g_file_info_new ();
422 g_file_info_copy_into (other, new);
427 * g_file_info_set_attribute_mask:
428 * @info: a #GFileInfo.
429 * @mask: a #GFileAttributeMatcher.
431 * Sets @mask on @info to match specific attribute types.
434 g_file_info_set_attribute_mask (GFileInfo *info,
435 GFileAttributeMatcher *mask)
437 GFileAttribute *attr;
440 g_return_if_fail (G_IS_FILE_INFO (info));
442 if (mask != info->mask)
444 if (info->mask != NO_ATTRIBUTE_MASK)
445 g_file_attribute_matcher_unref (info->mask);
446 info->mask = g_file_attribute_matcher_ref (mask);
448 /* Remove non-matching attributes */
449 for (i = 0; i < info->attributes->len; i++)
451 attr = &g_array_index (info->attributes, GFileAttribute, i);
452 if (!_g_file_attribute_matcher_matches_id (mask,
455 _g_file_attribute_value_clear (&attr->value);
456 g_array_remove_index (info->attributes, i);
464 * g_file_info_unset_attribute_mask:
467 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
471 g_file_info_unset_attribute_mask (GFileInfo *info)
473 g_return_if_fail (G_IS_FILE_INFO (info));
475 if (info->mask != NO_ATTRIBUTE_MASK)
476 g_file_attribute_matcher_unref (info->mask);
477 info->mask = NO_ATTRIBUTE_MASK;
481 * g_file_info_clear_status:
482 * @info: a #GFileInfo.
484 * Clears the status information from @info.
487 g_file_info_clear_status (GFileInfo *info)
489 GFileAttribute *attrs;
492 g_return_if_fail (G_IS_FILE_INFO (info));
494 attrs = (GFileAttribute *)info->attributes->data;
495 for (i = 0; i < info->attributes->len; i++)
496 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
500 g_file_info_find_place (GFileInfo *info,
504 GFileAttribute *attrs;
505 /* Binary search for the place where attribute would be, if it's
509 max = info->attributes->len;
511 attrs = (GFileAttribute *)info->attributes->data;
515 med = min + (max - min) / 2;
516 if (attrs[med].attribute == attribute)
521 else if (attrs[med].attribute < attribute)
523 else /* attrs[med].attribute > attribute */
530 static GFileAttributeValue *
531 g_file_info_find_value (GFileInfo *info,
534 GFileAttribute *attrs;
537 i = g_file_info_find_place (info, attr_id);
538 attrs = (GFileAttribute *)info->attributes->data;
539 if (i < info->attributes->len &&
540 attrs[i].attribute == attr_id)
541 return &attrs[i].value;
546 static GFileAttributeValue *
547 g_file_info_find_value_by_name (GFileInfo *info,
548 const char *attribute)
552 attr_id = lookup_attribute (attribute);
553 return g_file_info_find_value (info, attr_id);
557 * g_file_info_has_attribute:
558 * @info: a #GFileInfo.
559 * @attribute: a file attribute key.
561 * Checks if a file info structure has an attribute named @attribute.
563 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
567 g_file_info_has_attribute (GFileInfo *info,
568 const char *attribute)
570 GFileAttributeValue *value;
572 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
573 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
575 value = g_file_info_find_value_by_name (info, attribute);
576 return value != NULL;
580 * g_file_info_has_namespace:
581 * @info: a #GFileInfo.
582 * @name_space: a file attribute namespace.
584 * Checks if a file info structure has an attribute in the
585 * specified @name_space.
587 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
593 g_file_info_has_namespace (GFileInfo *info,
594 const char *name_space)
596 GFileAttribute *attrs;
600 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
601 g_return_val_if_fail (name_space != NULL, FALSE);
603 ns_id = lookup_namespace (name_space);
605 attrs = (GFileAttribute *)info->attributes->data;
606 for (i = 0; i < info->attributes->len; i++)
608 if (GET_NS (attrs[i].attribute) == ns_id)
616 * g_file_info_list_attributes:
617 * @info: a #GFileInfo.
618 * @name_space: a file attribute key's namespace.
620 * Lists the file info structure's attributes.
622 * Returns: (array zero-terminated=1) (transfer full): a null-terminated array of strings of all of the
623 * possible attribute types for the given @name_space, or
627 g_file_info_list_attributes (GFileInfo *info,
628 const char *name_space)
631 GFileAttribute *attrs;
633 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
636 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
638 names = g_ptr_array_new ();
639 attrs = (GFileAttribute *)info->attributes->data;
640 for (i = 0; i < info->attributes->len; i++)
642 attribute = attrs[i].attribute;
643 if (ns_id == 0 || GET_NS (attribute) == ns_id)
644 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
648 g_ptr_array_add (names, NULL);
650 return (char **)g_ptr_array_free (names, FALSE);
654 * g_file_info_get_attribute_type:
655 * @info: a #GFileInfo.
656 * @attribute: a file attribute key.
658 * Gets the attribute type for an attribute key.
660 * Returns: a #GFileAttributeType for the given @attribute, or
661 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
664 g_file_info_get_attribute_type (GFileInfo *info,
665 const char *attribute)
667 GFileAttributeValue *value;
669 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
670 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
672 value = g_file_info_find_value_by_name (info, attribute);
676 return G_FILE_ATTRIBUTE_TYPE_INVALID;
680 * g_file_info_remove_attribute:
681 * @info: a #GFileInfo.
682 * @attribute: a file attribute key.
684 * Removes all cases of @attribute from @info if it exists.
687 g_file_info_remove_attribute (GFileInfo *info,
688 const char *attribute)
691 GFileAttribute *attrs;
694 g_return_if_fail (G_IS_FILE_INFO (info));
695 g_return_if_fail (attribute != NULL && *attribute != '\0');
697 attr_id = lookup_attribute (attribute);
699 i = g_file_info_find_place (info, attr_id);
700 attrs = (GFileAttribute *)info->attributes->data;
701 if (i < info->attributes->len &&
702 attrs[i].attribute == attr_id)
704 _g_file_attribute_value_clear (&attrs[i].value);
705 g_array_remove_index (info->attributes, i);
710 * g_file_info_get_attribute_data:
711 * @info: a #GFileInfo
712 * @attribute: a file attribute key
713 * @type: (out) (allow-none): return location for the attribute type, or %NULL
714 * @value_pp: (out) (allow-none): return location for the attribute value, or %NULL
715 * @status: (out) (allow-none): return location for the attribute status, or %NULL
717 * Gets the attribute type, value and status for an attribute key.
719 * Returns: (transfer none): %TRUE if @info has an attribute named @attribute,
723 g_file_info_get_attribute_data (GFileInfo *info,
724 const char *attribute,
725 GFileAttributeType *type,
727 GFileAttributeStatus *status)
729 GFileAttributeValue *value;
731 value = g_file_info_find_value_by_name (info, attribute);
736 *status = value->status;
742 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
748 * g_file_info_get_attribute_status:
749 * @info: a #GFileInfo
750 * @attribute: a file attribute key
752 * Gets the attribute status for an attribute key.
754 * Returns: a #GFileAttributeStatus for the given @attribute, or
755 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
759 g_file_info_get_attribute_status (GFileInfo *info,
760 const char *attribute)
762 GFileAttributeValue *val;
764 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
765 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
767 val = g_file_info_find_value_by_name (info, attribute);
771 return G_FILE_ATTRIBUTE_STATUS_UNSET;
775 * g_file_info_set_attribute_status:
776 * @info: a #GFileInfo
777 * @attribute: a file attribute key
778 * @status: a #GFileAttributeStatus
780 * Sets the attribute status for an attribute key. This is only
781 * needed by external code that implement g_file_set_attributes_from_info()
782 * or similar functions.
784 * The attribute must exist in @info for this to work. Otherwise %FALSE
785 * is returned and @info is unchanged.
787 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
792 g_file_info_set_attribute_status (GFileInfo *info,
793 const char *attribute,
794 GFileAttributeStatus status)
796 GFileAttributeValue *val;
798 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
799 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
801 val = g_file_info_find_value_by_name (info, attribute);
804 val->status = status;
811 GFileAttributeValue *
812 _g_file_info_get_attribute_value (GFileInfo *info,
813 const char *attribute)
816 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
817 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
819 return g_file_info_find_value_by_name (info, attribute);
823 * g_file_info_get_attribute_as_string:
824 * @info: a #GFileInfo.
825 * @attribute: a file attribute key.
827 * Gets the value of a attribute, formated as a string.
828 * This escapes things as needed to make the string valid
831 * Returns: a UTF-8 string associated with the given @attribute.
832 * When you're done with the string it must be freed with g_free().
835 g_file_info_get_attribute_as_string (GFileInfo *info,
836 const char *attribute)
838 GFileAttributeValue *val;
839 val = _g_file_info_get_attribute_value (info, attribute);
841 return _g_file_attribute_value_as_string (val);
847 * g_file_info_get_attribute_object:
848 * @info: a #GFileInfo.
849 * @attribute: a file attribute key.
851 * Gets the value of a #GObject attribute. If the attribute does
852 * not contain a #GObject, %NULL will be returned.
854 * Returns: (transfer none): a #GObject associated with the given @attribute, or
858 g_file_info_get_attribute_object (GFileInfo *info,
859 const char *attribute)
861 GFileAttributeValue *value;
863 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
864 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
866 value = g_file_info_find_value_by_name (info, attribute);
867 return _g_file_attribute_value_get_object (value);
871 * g_file_info_get_attribute_string:
872 * @info: a #GFileInfo.
873 * @attribute: a file attribute key.
875 * Gets the value of a string attribute. If the attribute does
876 * not contain a string, %NULL will be returned.
878 * Returns: the contents of the @attribute value as a UTF-8 string, or
882 g_file_info_get_attribute_string (GFileInfo *info,
883 const char *attribute)
885 GFileAttributeValue *value;
887 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
888 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
890 value = g_file_info_find_value_by_name (info, attribute);
891 return _g_file_attribute_value_get_string (value);
895 * g_file_info_get_attribute_byte_string:
896 * @info: a #GFileInfo.
897 * @attribute: a file attribute key.
899 * Gets the value of a byte string attribute. If the attribute does
900 * not contain a byte string, %NULL will be returned.
902 * Returns: the contents of the @attribute value as a byte string, or
906 g_file_info_get_attribute_byte_string (GFileInfo *info,
907 const char *attribute)
909 GFileAttributeValue *value;
911 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
912 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
914 value = g_file_info_find_value_by_name (info, attribute);
915 return _g_file_attribute_value_get_byte_string (value);
919 * g_file_info_get_attribute_stringv:
920 * @info: a #GFileInfo.
921 * @attribute: a file attribute key.
923 * Gets the value of a stringv attribute. If the attribute does
924 * not contain a stringv, %NULL will be returned.
926 * Returns: (transfer none): the contents of the @attribute value as a stringv, or
927 * %NULL otherwise. Do not free. These returned strings are UTF-8.
932 g_file_info_get_attribute_stringv (GFileInfo *info,
933 const char *attribute)
935 GFileAttributeValue *value;
937 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
938 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
940 value = g_file_info_find_value_by_name (info, attribute);
941 return _g_file_attribute_value_get_stringv (value);
945 * g_file_info_get_attribute_boolean:
946 * @info: a #GFileInfo.
947 * @attribute: a file attribute key.
949 * Gets the value of a boolean attribute. If the attribute does not
950 * contain a boolean value, %FALSE will be returned.
952 * Returns: the boolean value contained within the attribute.
955 g_file_info_get_attribute_boolean (GFileInfo *info,
956 const char *attribute)
958 GFileAttributeValue *value;
960 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
961 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
963 value = g_file_info_find_value_by_name (info, attribute);
964 return _g_file_attribute_value_get_boolean (value);
968 * g_file_info_get_attribute_uint32:
969 * @info: a #GFileInfo.
970 * @attribute: a file attribute key.
972 * Gets an unsigned 32-bit integer contained within the attribute. If the
973 * attribute does not contain an unsigned 32-bit integer, or is invalid,
974 * 0 will be returned.
976 * Returns: an unsigned 32-bit integer from the attribute.
979 g_file_info_get_attribute_uint32 (GFileInfo *info,
980 const char *attribute)
982 GFileAttributeValue *value;
984 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
985 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
987 value = g_file_info_find_value_by_name (info, attribute);
988 return _g_file_attribute_value_get_uint32 (value);
992 * g_file_info_get_attribute_int32:
993 * @info: a #GFileInfo.
994 * @attribute: a file attribute key.
996 * Gets a signed 32-bit integer contained within the attribute. If the
997 * attribute does not contain a signed 32-bit integer, or is invalid,
998 * 0 will be returned.
1000 * Returns: a signed 32-bit integer from the attribute.
1003 g_file_info_get_attribute_int32 (GFileInfo *info,
1004 const char *attribute)
1006 GFileAttributeValue *value;
1008 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1009 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1011 value = g_file_info_find_value_by_name (info, attribute);
1012 return _g_file_attribute_value_get_int32 (value);
1016 * g_file_info_get_attribute_uint64:
1017 * @info: a #GFileInfo.
1018 * @attribute: a file attribute key.
1020 * Gets a unsigned 64-bit integer contained within the attribute. If the
1021 * attribute does not contain an unsigned 64-bit integer, or is invalid,
1022 * 0 will be returned.
1024 * Returns: a unsigned 64-bit integer from the attribute.
1027 g_file_info_get_attribute_uint64 (GFileInfo *info,
1028 const char *attribute)
1030 GFileAttributeValue *value;
1032 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1033 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1035 value = g_file_info_find_value_by_name (info, attribute);
1036 return _g_file_attribute_value_get_uint64 (value);
1040 * g_file_info_get_attribute_int64:
1041 * @info: a #GFileInfo.
1042 * @attribute: a file attribute key.
1044 * Gets a signed 64-bit integer contained within the attribute. If the
1045 * attribute does not contain an signed 64-bit integer, or is invalid,
1046 * 0 will be returned.
1048 * Returns: a signed 64-bit integer from the attribute.
1051 g_file_info_get_attribute_int64 (GFileInfo *info,
1052 const char *attribute)
1054 GFileAttributeValue *value;
1056 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1057 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1059 value = g_file_info_find_value_by_name (info, attribute);
1060 return _g_file_attribute_value_get_int64 (value);
1063 static GFileAttributeValue *
1064 g_file_info_create_value (GFileInfo *info,
1067 GFileAttribute *attrs;
1070 if (info->mask != NO_ATTRIBUTE_MASK &&
1071 !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
1074 i = g_file_info_find_place (info, attr_id);
1076 attrs = (GFileAttribute *)info->attributes->data;
1077 if (i < info->attributes->len &&
1078 attrs[i].attribute == attr_id)
1079 return &attrs[i].value;
1082 GFileAttribute attr = { 0 };
1083 attr.attribute = attr_id;
1084 g_array_insert_val (info->attributes, i, attr);
1086 attrs = (GFileAttribute *)info->attributes->data;
1087 return &attrs[i].value;
1092 _g_file_info_set_attribute_by_id (GFileInfo *info,
1094 GFileAttributeType type,
1097 GFileAttributeValue *value;
1099 value = g_file_info_create_value (info, attribute);
1102 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1106 * g_file_info_set_attribute:
1107 * @info: a #GFileInfo.
1108 * @attribute: a file attribute key.
1109 * @type: a #GFileAttributeType
1110 * @value_p: pointer to the value
1112 * Sets the @attribute to contain the given value, if possible. To unset the
1113 * attribute, use %G_ATTRIBUTE_TYPE_INVALID for @type.
1116 g_file_info_set_attribute (GFileInfo *info,
1117 const char *attribute,
1118 GFileAttributeType type,
1121 g_return_if_fail (G_IS_FILE_INFO (info));
1122 g_return_if_fail (attribute != NULL && *attribute != '\0');
1124 _g_file_info_set_attribute_by_id (info, lookup_attribute (attribute), type, value_p);
1128 _g_file_info_set_attribute_object_by_id (GFileInfo *info,
1130 GObject *attr_value)
1132 GFileAttributeValue *value;
1134 value = g_file_info_create_value (info, attribute);
1136 _g_file_attribute_value_set_object (value, attr_value);
1140 * g_file_info_set_attribute_object:
1141 * @info: a #GFileInfo.
1142 * @attribute: a file attribute key.
1143 * @attr_value: a #GObject.
1145 * Sets the @attribute to contain the given @attr_value,
1149 g_file_info_set_attribute_object (GFileInfo *info,
1150 const char *attribute,
1151 GObject *attr_value)
1153 g_return_if_fail (G_IS_FILE_INFO (info));
1154 g_return_if_fail (attribute != NULL && *attribute != '\0');
1155 g_return_if_fail (G_IS_OBJECT (attr_value));
1157 _g_file_info_set_attribute_object_by_id (info,
1158 lookup_attribute (attribute),
1163 _g_file_info_set_attribute_stringv_by_id (GFileInfo *info,
1167 GFileAttributeValue *value;
1169 value = g_file_info_create_value (info, attribute);
1171 _g_file_attribute_value_set_stringv (value, attr_value);
1175 * g_file_info_set_attribute_stringv:
1176 * @info: a #GFileInfo.
1177 * @attribute: a file attribute key
1178 * @attr_value: (array) (element-type utf8): a %NULL terminated array of UTF-8 strings.
1180 * Sets the @attribute to contain the given @attr_value,
1186 g_file_info_set_attribute_stringv (GFileInfo *info,
1187 const char *attribute,
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 _g_file_info_set_attribute_stringv_by_id (info,
1195 lookup_attribute (attribute),
1200 _g_file_info_set_attribute_string_by_id (GFileInfo *info,
1202 const char *attr_value)
1204 GFileAttributeValue *value;
1206 value = g_file_info_create_value (info, attribute);
1208 _g_file_attribute_value_set_string (value, attr_value);
1212 * g_file_info_set_attribute_string:
1213 * @info: a #GFileInfo.
1214 * @attribute: a file attribute key.
1215 * @attr_value: a UTF-8 string.
1217 * Sets the @attribute to contain the given @attr_value,
1221 g_file_info_set_attribute_string (GFileInfo *info,
1222 const char *attribute,
1223 const char *attr_value)
1225 g_return_if_fail (G_IS_FILE_INFO (info));
1226 g_return_if_fail (attribute != NULL && *attribute != '\0');
1227 g_return_if_fail (attr_value != NULL);
1229 _g_file_info_set_attribute_string_by_id (info,
1230 lookup_attribute (attribute),
1235 _g_file_info_set_attribute_byte_string_by_id (GFileInfo *info,
1237 const char *attr_value)
1239 GFileAttributeValue *value;
1241 value = g_file_info_create_value (info, attribute);
1243 _g_file_attribute_value_set_byte_string (value, attr_value);
1247 * g_file_info_set_attribute_byte_string:
1248 * @info: a #GFileInfo.
1249 * @attribute: a file attribute key.
1250 * @attr_value: a byte string.
1252 * Sets the @attribute to contain the given @attr_value,
1256 g_file_info_set_attribute_byte_string (GFileInfo *info,
1257 const char *attribute,
1258 const char *attr_value)
1260 g_return_if_fail (G_IS_FILE_INFO (info));
1261 g_return_if_fail (attribute != NULL && *attribute != '\0');
1262 g_return_if_fail (attr_value != NULL);
1264 _g_file_info_set_attribute_byte_string_by_id (info,
1265 lookup_attribute (attribute),
1270 _g_file_info_set_attribute_boolean_by_id (GFileInfo *info,
1272 gboolean attr_value)
1274 GFileAttributeValue *value;
1276 value = g_file_info_create_value (info, attribute);
1278 _g_file_attribute_value_set_boolean (value, attr_value);
1282 * g_file_info_set_attribute_boolean:
1283 * @info: a #GFileInfo.
1284 * @attribute: a file attribute key.
1285 * @attr_value: a boolean value.
1287 * Sets the @attribute to contain the given @attr_value,
1291 g_file_info_set_attribute_boolean (GFileInfo *info,
1292 const char *attribute,
1293 gboolean attr_value)
1295 g_return_if_fail (G_IS_FILE_INFO (info));
1296 g_return_if_fail (attribute != NULL && *attribute != '\0');
1298 _g_file_info_set_attribute_boolean_by_id (info,
1299 lookup_attribute (attribute),
1304 _g_file_info_set_attribute_uint32_by_id (GFileInfo *info,
1308 GFileAttributeValue *value;
1310 value = g_file_info_create_value (info, attribute);
1312 _g_file_attribute_value_set_uint32 (value, attr_value);
1316 * g_file_info_set_attribute_uint32:
1317 * @info: a #GFileInfo.
1318 * @attribute: a file attribute key.
1319 * @attr_value: an unsigned 32-bit integer.
1321 * Sets the @attribute to contain the given @attr_value,
1325 g_file_info_set_attribute_uint32 (GFileInfo *info,
1326 const char *attribute,
1329 g_return_if_fail (G_IS_FILE_INFO (info));
1330 g_return_if_fail (attribute != NULL && *attribute != '\0');
1332 _g_file_info_set_attribute_uint32_by_id (info,
1333 lookup_attribute (attribute),
1338 _g_file_info_set_attribute_int32_by_id (GFileInfo *info,
1342 GFileAttributeValue *value;
1344 value = g_file_info_create_value (info, attribute);
1346 _g_file_attribute_value_set_int32 (value, attr_value);
1350 * g_file_info_set_attribute_int32:
1351 * @info: a #GFileInfo.
1352 * @attribute: a file attribute key.
1353 * @attr_value: a signed 32-bit integer
1355 * Sets the @attribute to contain the given @attr_value,
1359 g_file_info_set_attribute_int32 (GFileInfo *info,
1360 const char *attribute,
1363 g_return_if_fail (G_IS_FILE_INFO (info));
1364 g_return_if_fail (attribute != NULL && *attribute != '\0');
1366 _g_file_info_set_attribute_int32_by_id (info,
1367 lookup_attribute (attribute),
1372 _g_file_info_set_attribute_uint64_by_id (GFileInfo *info,
1376 GFileAttributeValue *value;
1378 value = g_file_info_create_value (info, attribute);
1380 _g_file_attribute_value_set_uint64 (value, attr_value);
1384 * g_file_info_set_attribute_uint64:
1385 * @info: a #GFileInfo.
1386 * @attribute: a file attribute key.
1387 * @attr_value: an unsigned 64-bit integer.
1389 * Sets the @attribute to contain the given @attr_value,
1393 g_file_info_set_attribute_uint64 (GFileInfo *info,
1394 const char *attribute,
1397 g_return_if_fail (G_IS_FILE_INFO (info));
1398 g_return_if_fail (attribute != NULL && *attribute != '\0');
1400 _g_file_info_set_attribute_uint64_by_id (info,
1401 lookup_attribute (attribute),
1406 _g_file_info_set_attribute_int64_by_id (GFileInfo *info,
1410 GFileAttributeValue *value;
1412 value = g_file_info_create_value (info, attribute);
1414 _g_file_attribute_value_set_int64 (value, attr_value);
1418 * g_file_info_set_attribute_int64:
1419 * @info: a #GFileInfo.
1420 * @attribute: attribute name to set.
1421 * @attr_value: int64 value to set attribute to.
1423 * Sets the @attribute to contain the given @attr_value,
1428 g_file_info_set_attribute_int64 (GFileInfo *info,
1429 const char *attribute,
1432 g_return_if_fail (G_IS_FILE_INFO (info));
1433 g_return_if_fail (attribute != NULL && *attribute != '\0');
1435 _g_file_info_set_attribute_int64_by_id (info,
1436 lookup_attribute (attribute),
1440 /* Helper getters */
1442 * g_file_info_get_file_type:
1443 * @info: a #GFileInfo.
1445 * Gets a file's type (whether it is a regular file, symlink, etc).
1446 * This is different from the file's content type, see g_file_info_get_content_type().
1448 * Returns: a #GFileType for the given file.
1451 g_file_info_get_file_type (GFileInfo *info)
1453 static guint32 attr = 0;
1454 GFileAttributeValue *value;
1456 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1459 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1461 value = g_file_info_find_value (info, attr);
1462 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1466 * g_file_info_get_is_hidden:
1467 * @info: a #GFileInfo.
1469 * Checks if a file is hidden.
1471 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1474 g_file_info_get_is_hidden (GFileInfo *info)
1476 static guint32 attr = 0;
1477 GFileAttributeValue *value;
1479 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1482 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1484 value = g_file_info_find_value (info, attr);
1485 return (GFileType)_g_file_attribute_value_get_boolean (value);
1489 * g_file_info_get_is_backup:
1490 * @info: a #GFileInfo.
1492 * Checks if a file is a backup file.
1494 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1497 g_file_info_get_is_backup (GFileInfo *info)
1499 static guint32 attr = 0;
1500 GFileAttributeValue *value;
1502 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1505 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1507 value = g_file_info_find_value (info, attr);
1508 return (GFileType)_g_file_attribute_value_get_boolean (value);
1512 * g_file_info_get_is_symlink:
1513 * @info: a #GFileInfo.
1515 * Checks if a file is a symlink.
1517 * Returns: %TRUE if the given @info is a symlink.
1520 g_file_info_get_is_symlink (GFileInfo *info)
1522 static guint32 attr = 0;
1523 GFileAttributeValue *value;
1525 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1528 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1530 value = g_file_info_find_value (info, attr);
1531 return (GFileType)_g_file_attribute_value_get_boolean (value);
1535 * g_file_info_get_name:
1536 * @info: a #GFileInfo.
1538 * Gets the name for a file.
1540 * Returns: a string containing the file name.
1543 g_file_info_get_name (GFileInfo *info)
1545 static guint32 attr = 0;
1546 GFileAttributeValue *value;
1548 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1551 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1553 value = g_file_info_find_value (info, attr);
1554 return _g_file_attribute_value_get_byte_string (value);
1558 * g_file_info_get_display_name:
1559 * @info: a #GFileInfo.
1561 * Gets a display name for a file.
1563 * Returns: a string containing the display name.
1566 g_file_info_get_display_name (GFileInfo *info)
1568 static guint32 attr = 0;
1569 GFileAttributeValue *value;
1571 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1574 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1576 value = g_file_info_find_value (info, attr);
1577 return _g_file_attribute_value_get_string (value);
1581 * g_file_info_get_edit_name:
1582 * @info: a #GFileInfo.
1584 * Gets the edit name for a file.
1586 * Returns: a string containing the edit name.
1589 g_file_info_get_edit_name (GFileInfo *info)
1591 static guint32 attr = 0;
1592 GFileAttributeValue *value;
1594 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1597 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1599 value = g_file_info_find_value (info, attr);
1600 return _g_file_attribute_value_get_string (value);
1604 * g_file_info_get_icon:
1605 * @info: a #GFileInfo.
1607 * Gets the icon for a file.
1609 * Returns: (transfer none): #GIcon for the given @info.
1612 g_file_info_get_icon (GFileInfo *info)
1614 static guint32 attr = 0;
1615 GFileAttributeValue *value;
1618 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1621 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1623 value = g_file_info_find_value (info, attr);
1624 obj = _g_file_attribute_value_get_object (value);
1625 if (G_IS_ICON (obj))
1626 return G_ICON (obj);
1631 * g_file_info_get_content_type:
1632 * @info: a #GFileInfo.
1634 * Gets the file's content type.
1636 * Returns: a string containing the file's content type.
1639 g_file_info_get_content_type (GFileInfo *info)
1641 static guint32 attr = 0;
1642 GFileAttributeValue *value;
1644 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1647 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1649 value = g_file_info_find_value (info, attr);
1650 return _g_file_attribute_value_get_string (value);
1654 * g_file_info_get_size:
1655 * @info: a #GFileInfo.
1657 * Gets the file's size.
1659 * Returns: a #goffset containing the file's size.
1662 g_file_info_get_size (GFileInfo *info)
1664 static guint32 attr = 0;
1665 GFileAttributeValue *value;
1667 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1670 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1672 value = g_file_info_find_value (info, attr);
1673 return (goffset) _g_file_attribute_value_get_uint64 (value);
1677 * g_file_info_get_modification_time:
1678 * @info: a #GFileInfo.
1679 * @result: (out caller-allocates): a #GTimeVal.
1681 * Gets the modification time of the current @info and sets it
1685 g_file_info_get_modification_time (GFileInfo *info,
1688 static guint32 attr_mtime = 0, attr_mtime_usec;
1689 GFileAttributeValue *value;
1691 g_return_if_fail (G_IS_FILE_INFO (info));
1692 g_return_if_fail (result != NULL);
1694 if (attr_mtime == 0)
1696 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1697 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1700 value = g_file_info_find_value (info, attr_mtime);
1701 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1702 value = g_file_info_find_value (info, attr_mtime_usec);
1703 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1707 * g_file_info_get_symlink_target:
1708 * @info: a #GFileInfo.
1710 * Gets the symlink target for a given #GFileInfo.
1712 * Returns: a string containing the symlink target.
1715 g_file_info_get_symlink_target (GFileInfo *info)
1717 static guint32 attr = 0;
1718 GFileAttributeValue *value;
1720 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1723 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1725 value = g_file_info_find_value (info, attr);
1726 return _g_file_attribute_value_get_byte_string (value);
1730 * g_file_info_get_etag:
1731 * @info: a #GFileInfo.
1733 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1734 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1736 * Returns: a string containing the value of the "etag:value" attribute.
1739 g_file_info_get_etag (GFileInfo *info)
1741 static guint32 attr = 0;
1742 GFileAttributeValue *value;
1744 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1747 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1749 value = g_file_info_find_value (info, attr);
1750 return _g_file_attribute_value_get_string (value);
1754 * g_file_info_get_sort_order:
1755 * @info: a #GFileInfo.
1757 * Gets the value of the sort_order attribute from the #GFileInfo.
1758 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1760 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1763 g_file_info_get_sort_order (GFileInfo *info)
1765 static guint32 attr = 0;
1766 GFileAttributeValue *value;
1768 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1771 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1773 value = g_file_info_find_value (info, attr);
1774 return _g_file_attribute_value_get_int32 (value);
1777 /* Helper setters: */
1779 * g_file_info_set_file_type:
1780 * @info: a #GFileInfo.
1781 * @type: a #GFileType.
1783 * Sets the file type in a #GFileInfo to @type.
1784 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1787 g_file_info_set_file_type (GFileInfo *info,
1790 static guint32 attr = 0;
1791 GFileAttributeValue *value;
1793 g_return_if_fail (G_IS_FILE_INFO (info));
1796 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1798 value = g_file_info_create_value (info, attr);
1800 _g_file_attribute_value_set_uint32 (value, type);
1804 * g_file_info_set_is_hidden:
1805 * @info: a #GFileInfo.
1806 * @is_hidden: a #gboolean.
1808 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1809 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1812 g_file_info_set_is_hidden (GFileInfo *info,
1815 static guint32 attr = 0;
1816 GFileAttributeValue *value;
1818 g_return_if_fail (G_IS_FILE_INFO (info));
1821 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1823 value = g_file_info_create_value (info, attr);
1825 _g_file_attribute_value_set_boolean (value, is_hidden);
1829 * g_file_info_set_is_symlink:
1830 * @info: a #GFileInfo.
1831 * @is_symlink: a #gboolean.
1833 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1834 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1837 g_file_info_set_is_symlink (GFileInfo *info,
1838 gboolean is_symlink)
1840 static guint32 attr = 0;
1841 GFileAttributeValue *value;
1843 g_return_if_fail (G_IS_FILE_INFO (info));
1846 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1848 value = g_file_info_create_value (info, attr);
1850 _g_file_attribute_value_set_boolean (value, is_symlink);
1854 * g_file_info_set_name:
1855 * @info: a #GFileInfo.
1856 * @name: a string containing a name.
1858 * Sets the name attribute for the current #GFileInfo.
1859 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1862 g_file_info_set_name (GFileInfo *info,
1865 static guint32 attr = 0;
1866 GFileAttributeValue *value;
1868 g_return_if_fail (G_IS_FILE_INFO (info));
1869 g_return_if_fail (name != NULL);
1872 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1874 value = g_file_info_create_value (info, attr);
1876 _g_file_attribute_value_set_byte_string (value, name);
1880 * g_file_info_set_display_name:
1881 * @info: a #GFileInfo.
1882 * @display_name: a string containing a display name.
1884 * Sets the display name for the current #GFileInfo.
1885 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1888 g_file_info_set_display_name (GFileInfo *info,
1889 const char *display_name)
1891 static guint32 attr = 0;
1892 GFileAttributeValue *value;
1894 g_return_if_fail (G_IS_FILE_INFO (info));
1895 g_return_if_fail (display_name != NULL);
1898 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1900 value = g_file_info_create_value (info, attr);
1902 _g_file_attribute_value_set_string (value, display_name);
1906 * g_file_info_set_edit_name:
1907 * @info: a #GFileInfo.
1908 * @edit_name: a string containing an edit name.
1910 * Sets the edit name for the current file.
1911 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1914 g_file_info_set_edit_name (GFileInfo *info,
1915 const char *edit_name)
1917 static guint32 attr = 0;
1918 GFileAttributeValue *value;
1920 g_return_if_fail (G_IS_FILE_INFO (info));
1921 g_return_if_fail (edit_name != NULL);
1924 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1926 value = g_file_info_create_value (info, attr);
1928 _g_file_attribute_value_set_string (value, edit_name);
1932 * g_file_info_set_icon:
1933 * @info: a #GFileInfo.
1936 * Sets the icon for a given #GFileInfo.
1937 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1940 g_file_info_set_icon (GFileInfo *info,
1943 static guint32 attr = 0;
1944 GFileAttributeValue *value;
1946 g_return_if_fail (G_IS_FILE_INFO (info));
1947 g_return_if_fail (G_IS_ICON (icon));
1950 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1952 value = g_file_info_create_value (info, attr);
1954 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1958 * g_file_info_set_content_type:
1959 * @info: a #GFileInfo.
1960 * @content_type: a content type. See <link linkend="gio-GContentType">GContentType</link>.
1962 * Sets the content type attribute for a given #GFileInfo.
1963 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1966 g_file_info_set_content_type (GFileInfo *info,
1967 const char *content_type)
1969 static guint32 attr = 0;
1970 GFileAttributeValue *value;
1972 g_return_if_fail (G_IS_FILE_INFO (info));
1973 g_return_if_fail (content_type != NULL);
1976 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1978 value = g_file_info_create_value (info, attr);
1980 _g_file_attribute_value_set_string (value, content_type);
1984 * g_file_info_set_size:
1985 * @info: a #GFileInfo.
1986 * @size: a #goffset containing the file's size.
1988 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1989 * to the given size.
1992 g_file_info_set_size (GFileInfo *info,
1995 static guint32 attr = 0;
1996 GFileAttributeValue *value;
1998 g_return_if_fail (G_IS_FILE_INFO (info));
2001 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
2003 value = g_file_info_create_value (info, attr);
2005 _g_file_attribute_value_set_uint64 (value, size);
2009 * g_file_info_set_modification_time:
2010 * @info: a #GFileInfo.
2011 * @mtime: a #GTimeVal.
2013 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
2014 * info to the given time value.
2017 g_file_info_set_modification_time (GFileInfo *info,
2020 static guint32 attr_mtime = 0, attr_mtime_usec;
2021 GFileAttributeValue *value;
2023 g_return_if_fail (G_IS_FILE_INFO (info));
2024 g_return_if_fail (mtime != NULL);
2026 if (attr_mtime == 0)
2028 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
2029 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2032 value = g_file_info_create_value (info, attr_mtime);
2034 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
2035 value = g_file_info_create_value (info, attr_mtime_usec);
2037 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
2041 * g_file_info_set_symlink_target:
2042 * @info: a #GFileInfo.
2043 * @symlink_target: a static string containing a path to a symlink target.
2045 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
2046 * to the given symlink target.
2049 g_file_info_set_symlink_target (GFileInfo *info,
2050 const char *symlink_target)
2052 static guint32 attr = 0;
2053 GFileAttributeValue *value;
2055 g_return_if_fail (G_IS_FILE_INFO (info));
2056 g_return_if_fail (symlink_target != NULL);
2059 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2061 value = g_file_info_create_value (info, attr);
2063 _g_file_attribute_value_set_byte_string (value, symlink_target);
2067 * g_file_info_set_sort_order:
2068 * @info: a #GFileInfo.
2069 * @sort_order: a sort order integer.
2071 * Sets the sort order attribute in the file info structure. See
2072 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
2075 g_file_info_set_sort_order (GFileInfo *info,
2078 static guint32 attr = 0;
2079 GFileAttributeValue *value;
2081 g_return_if_fail (G_IS_FILE_INFO (info));
2084 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
2086 value = g_file_info_create_value (info, attr);
2088 _g_file_attribute_value_set_int32 (value, sort_order);
2097 struct _GFileAttributeMatcher {
2101 GArray *sub_matchers;
2104 guint32 iterator_ns;
2108 G_DEFINE_BOXED_TYPE (GFileAttributeMatcher, g_file_attribute_matcher,
2109 g_file_attribute_matcher_ref,
2110 g_file_attribute_matcher_unref)
2113 compare_sub_matchers (gconstpointer a,
2116 const SubMatcher *suba = a;
2117 const SubMatcher *subb = b;
2120 diff = suba->id - subb->id;
2125 return suba->mask - subb->mask;
2129 sub_matcher_matches (SubMatcher *matcher,
2130 SubMatcher *submatcher)
2132 if ((matcher->mask & submatcher->mask) != matcher->mask)
2135 return matcher->id == (submatcher->id & matcher->mask);
2138 /* Call this function after modifying a matcher.
2139 * It will ensure all the invariants other functions rely on.
2141 static GFileAttributeMatcher *
2142 matcher_optimize (GFileAttributeMatcher *matcher)
2144 SubMatcher *submatcher, *compare;
2147 /* remove sub_matchers if we match everything anyway */
2150 if (matcher->sub_matchers)
2152 g_array_free (matcher->sub_matchers, TRUE);
2153 matcher->sub_matchers = NULL;
2158 if (matcher->sub_matchers->len == 0)
2160 g_file_attribute_matcher_unref (matcher);
2164 /* sort sub_matchers by id (and then mask), so we can bsearch
2165 * and compare matchers in O(N) instead of O(N²) */
2166 g_array_sort (matcher->sub_matchers, compare_sub_matchers);
2168 /* remove duplicates and specific matches when we match the whole namespace */
2170 compare = &g_array_index (matcher->sub_matchers, SubMatcher, j);
2172 for (i = 1; i < matcher->sub_matchers->len; i++)
2174 submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2175 if (sub_matcher_matches (compare, submatcher))
2182 *compare = *submatcher;
2185 g_array_set_size (matcher->sub_matchers, j + 1);
2191 * g_file_attribute_matcher_new:
2192 * @attributes: an attribute string to match.
2194 * Creates a new file attribute matcher, which matches attributes
2195 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
2196 * counted structures, and are created with a reference count of 1. If
2197 * the number of references falls to 0, the #GFileAttributeMatcher is
2198 * automatically destroyed.
2200 * The @attribute string should be formatted with specific keys separated
2201 * from namespaces with a double colon. Several "namespace::key" strings may be
2202 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
2203 * The wildcard "*" may be used to match all keys and namespaces, or
2204 * "namespace::*" will match all keys in a given namespace.
2206 * Examples of strings to use:
2208 * <title>File Attribute Matcher strings and results</title>
2209 * <tgroup cols='2' align='left'><thead>
2210 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
2212 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
2213 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
2214 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
2215 * all keys in the unix namespace.</entry></row>
2219 * Returns: a #GFileAttributeMatcher.
2221 GFileAttributeMatcher *
2222 g_file_attribute_matcher_new (const char *attributes)
2227 GFileAttributeMatcher *matcher;
2229 if (attributes == NULL || *attributes == '\0')
2232 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2234 matcher->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2236 split = g_strsplit (attributes, ",", -1);
2238 for (i = 0; split[i] != NULL; i++)
2240 if (strcmp (split[i], "*") == 0)
2241 matcher->all = TRUE;
2246 colon = strstr (split[i], "::");
2247 if (colon != NULL &&
2252 s.id = lookup_attribute (split[i]);
2253 s.mask = 0xffffffff;
2260 s.id = lookup_namespace (split[i]) << NS_POS;
2261 s.mask = NS_MASK << NS_POS;
2264 g_array_append_val (matcher->sub_matchers, s);
2270 matcher = matcher_optimize (matcher);
2276 * g_file_attribute_matcher_subtract:
2277 * @matcher: Matcher to subtract from
2278 * @subtract: The matcher to subtract
2280 * Subtracts all attributes of @subtract from @matcher and returns
2281 * a matcher that supports those attributes.
2283 * Note that currently it is not possible to remove a single
2284 * attribute when the @matcher matches the whole namespace - or remove
2285 * a namespace or attribute when the matcher matches everything. This
2286 * is a limitation of the current implementation, but may be fixed
2289 * Returns: A file attribute matcher matching all attributes of
2290 * @matcher that are not matched by @subtract
2292 GFileAttributeMatcher *
2293 g_file_attribute_matcher_subtract (GFileAttributeMatcher *matcher,
2294 GFileAttributeMatcher *subtract)
2296 GFileAttributeMatcher *result;
2298 SubMatcher *msub, *ssub;
2300 if (matcher == NULL)
2302 if (subtract == NULL)
2303 return g_file_attribute_matcher_ref (matcher);
2307 return g_file_attribute_matcher_ref (matcher);
2309 result = g_malloc0 (sizeof (GFileAttributeMatcher));
2311 result->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2314 g_assert (subtract->sub_matchers->len > 0);
2315 ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);
2317 for (mi = 0; mi < matcher->sub_matchers->len; mi++)
2319 msub = &g_array_index (matcher->sub_matchers, SubMatcher, mi);
2322 if (sub_matcher_matches (ssub, msub))
2326 if (si >= subtract->sub_matchers->len)
2329 ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);
2330 if (ssub->id <= msub->id)
2333 g_array_append_val (result->sub_matchers, *msub);
2336 if (mi < matcher->sub_matchers->len)
2337 g_array_append_vals (result->sub_matchers,
2338 &g_array_index (matcher->sub_matchers, SubMatcher, mi),
2339 matcher->sub_matchers->len - mi);
2341 result = matcher_optimize (result);
2347 * g_file_attribute_matcher_ref:
2348 * @matcher: a #GFileAttributeMatcher.
2350 * References a file attribute matcher.
2352 * Returns: a #GFileAttributeMatcher.
2354 GFileAttributeMatcher *
2355 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2359 g_return_val_if_fail (matcher->ref > 0, NULL);
2360 g_atomic_int_inc (&matcher->ref);
2366 * g_file_attribute_matcher_unref:
2367 * @matcher: a #GFileAttributeMatcher.
2369 * Unreferences @matcher. If the reference count falls below 1,
2370 * the @matcher is automatically freed.
2374 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2378 g_return_if_fail (matcher->ref > 0);
2380 if (g_atomic_int_dec_and_test (&matcher->ref))
2382 if (matcher->sub_matchers)
2383 g_array_free (matcher->sub_matchers, TRUE);
2391 * g_file_attribute_matcher_matches_only:
2392 * @matcher: a #GFileAttributeMatcher.
2393 * @attribute: a file attribute key.
2395 * Checks if a attribute matcher only matches a given attribute. Always
2396 * returns %FALSE if "*" was used when creating the matcher.
2398 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2401 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2402 const char *attribute)
2404 SubMatcher *sub_matcher;
2407 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2409 if (matcher == NULL ||
2413 if (matcher->sub_matchers->len != 1)
2416 id = lookup_attribute (attribute);
2418 sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, 0);
2420 return sub_matcher->id == id &&
2421 sub_matcher->mask == 0xffffffff;
2425 matcher_matches_id (GFileAttributeMatcher *matcher,
2428 SubMatcher *sub_matchers;
2431 if (matcher->sub_matchers)
2433 sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
2434 for (i = 0; i < matcher->sub_matchers->len; i++)
2436 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2445 _g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2448 /* We return a NULL matcher for an empty match string, so handle this */
2449 if (matcher == NULL)
2455 return matcher_matches_id (matcher, id);
2459 * g_file_attribute_matcher_matches:
2460 * @matcher: a #GFileAttributeMatcher.
2461 * @attribute: a file attribute key.
2463 * Checks if an attribute will be matched by an attribute matcher. If
2464 * the matcher was created with the "*" matching string, this function
2465 * will always return %TRUE.
2467 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2470 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2471 const char *attribute)
2473 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2475 /* We return a NULL matcher for an empty match string, so handle this */
2476 if (matcher == NULL)
2482 return matcher_matches_id (matcher, lookup_attribute (attribute));
2485 /* return TRUE -> all */
2487 * g_file_attribute_matcher_enumerate_namespace:
2488 * @matcher: a #GFileAttributeMatcher.
2489 * @ns: a string containing a file attribute namespace.
2491 * Checks if the matcher will match all of the keys in a given namespace.
2492 * This will always return %TRUE if a wildcard character is in use (e.g. if
2493 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2494 * using "*" and namespace is anything.)
2496 * TODO: this is awkwardly worded.
2498 * Returns: %TRUE if the matcher matches all of the entries
2499 * in the given @ns, %FALSE otherwise.
2502 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2505 SubMatcher *sub_matchers;
2509 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2511 /* We return a NULL matcher for an empty match string, so handle this */
2512 if (matcher == NULL)
2518 ns_id = lookup_namespace (ns) << NS_POS;
2520 if (matcher->sub_matchers)
2522 sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
2523 for (i = 0; i < matcher->sub_matchers->len; i++)
2525 if (sub_matchers[i].id == ns_id)
2530 matcher->iterator_ns = ns_id;
2531 matcher->iterator_pos = 0;
2537 * g_file_attribute_matcher_enumerate_next:
2538 * @matcher: a #GFileAttributeMatcher.
2540 * Gets the next matched attribute from a #GFileAttributeMatcher.
2542 * Returns: a string containing the next attribute or %NULL if
2543 * no more attribute exist.
2546 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2549 SubMatcher *sub_matcher;
2551 /* We return a NULL matcher for an empty match string, so handle this */
2552 if (matcher == NULL)
2557 i = matcher->iterator_pos++;
2559 if (matcher->sub_matchers == NULL)
2562 if (i < matcher->sub_matchers->len)
2563 sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2567 if (sub_matcher->mask == 0xffffffff &&
2568 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2569 return get_attribute_for_id (sub_matcher->id);
2574 * g_file_attribute_matcher_to_string:
2575 * @matcher: (allow-none): a #GFileAttributeMatcher.
2577 * Prints what the matcher is matching against. The format will be
2578 * equal to the format passed to g_file_attribute_matcher_new().
2579 * The output however, might not be identical, as the matcher may
2580 * decide to use a different order or omit needless parts.
2582 * Returns: a string describing the attributes the matcher matches
2583 * against or %NULL if @matcher was %NULL.
2588 g_file_attribute_matcher_to_string (GFileAttributeMatcher *matcher)
2593 if (matcher == NULL)
2597 return g_strdup ("*");
2599 string = g_string_new ("");
2600 for (i = 0; i < matcher->sub_matchers->len; i++)
2602 SubMatcher *submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2605 g_string_append_c (string, ',');
2607 g_string_append (string, get_attribute_for_id (submatcher->id));
2610 return g_string_free (string, FALSE);