1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 * @short_description: File Information and Attributes
27 * @see_also: #GFile, <link linkend="gio-GFileAttribute">GFileAttribute</link>
29 * Functionality for manipulating basic metadata for files. #GFileInfo
30 * implements methods for getting information that all files should
31 * contain, and allows for manipulation of extended attributes.
33 * See <link linkend="gio-GFileAttribute">GFileAttribute</link> for more
34 * information on how GIO handles file attributes.
36 * To obtain a #GFileInfo for a #GFile, use g_file_query_info() (or its
37 * async variant). To obtain a #GFileInfo for a file input or output
38 * stream, use g_file_input_stream_query_info() or
39 * g_file_output_stream_query_info() (or their async variants).
41 * To change the actual attributes of a file, you should then set the
42 * attribute in the #GFileInfo and call g_file_set_attributes_from_info()
43 * or g_file_set_attributes_async() on a GFile.
45 * However, not all attributes can be changed in the file. For instance,
46 * the actual size of a file cannot be changed via g_file_info_set_size().
47 * You may call g_file_query_settable_attributes() and
48 * g_file_query_writable_namespaces() to discover the settable attributes
49 * of a particular file at runtime.
51 * #GFileAttributeMatcher allows for searching through a #GFileInfo for
59 #include "gfileinfo.h"
60 #include "gfileinfo-priv.h"
61 #include "gfileattribute-priv.h"
67 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
68 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
72 GFileAttributeValue value;
77 GObject parent_instance;
80 GFileAttributeMatcher *mask;
83 struct _GFileInfoClass
85 GObjectClass parent_class;
89 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
93 guint32 attribute_id_counter;
96 G_LOCK_DEFINE_STATIC (attribute_hash);
97 static int namespace_id_counter = 0;
98 static GHashTable *ns_hash = NULL;
99 static GHashTable *attribute_hash = NULL;
100 static char ***attributes = NULL;
102 /* Attribute ids are 32bit, we split it up like this:
103 * |------------|--------------------|
105 * namespace attribute id
107 * This way the attributes gets sorted in namespace order
111 #define NS_MASK ((guint32)((1<<12) - 1))
113 #define ID_MASK ((guint32)((1<<20) - 1))
115 #define GET_NS(_attr_id) \
116 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
117 #define GET_ID(_attr_id) \
118 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
120 #define MAKE_ATTR_ID(_ns, _id) \
121 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
122 ((((guint32) _id) & ID_MASK) << ID_POS) )
125 _lookup_namespace (const char *namespace)
129 ns_info = g_hash_table_lookup (ns_hash, namespace);
132 ns_info = g_new0 (NSInfo, 1);
133 ns_info->id = ++namespace_id_counter;
134 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
135 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
136 attributes[ns_info->id] = NULL;
142 _lookup_attribute (const char *attribute)
149 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
154 colon = strstr (attribute, "::");
156 ns = g_strndup (attribute, colon - attribute);
160 ns_info = _lookup_namespace (ns);
163 id = ++ns_info->attribute_id_counter;
164 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
165 attributes[ns_info->id][id] = g_strdup (attribute);
167 attr_id = MAKE_ATTR_ID (ns_info->id, id);
169 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
175 ensure_attribute_hash (void)
177 if (attribute_hash != NULL)
180 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
181 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
183 #define REGISTER_ATTRIBUTE(name) G_STMT_START{\
184 guint _u = _lookup_attribute (G_FILE_ATTRIBUTE_ ## name); \
185 /* use for generating the ID: g_print ("#define G_FILE_ATTRIBUTE_ID_%s (%u + %u)\n", #name + 17, _u & ~ID_MASK, _u & ID_MASK); */ \
186 g_assert (_u == G_FILE_ATTRIBUTE_ID_ ## name); \
189 REGISTER_ATTRIBUTE (STANDARD_TYPE);
190 REGISTER_ATTRIBUTE (STANDARD_IS_HIDDEN);
191 REGISTER_ATTRIBUTE (STANDARD_IS_BACKUP);
192 REGISTER_ATTRIBUTE (STANDARD_IS_SYMLINK);
193 REGISTER_ATTRIBUTE (STANDARD_IS_VIRTUAL);
194 REGISTER_ATTRIBUTE (STANDARD_NAME);
195 REGISTER_ATTRIBUTE (STANDARD_DISPLAY_NAME);
196 REGISTER_ATTRIBUTE (STANDARD_EDIT_NAME);
197 REGISTER_ATTRIBUTE (STANDARD_COPY_NAME);
198 REGISTER_ATTRIBUTE (STANDARD_DESCRIPTION);
199 REGISTER_ATTRIBUTE (STANDARD_ICON);
200 REGISTER_ATTRIBUTE (STANDARD_CONTENT_TYPE);
201 REGISTER_ATTRIBUTE (STANDARD_FAST_CONTENT_TYPE);
202 REGISTER_ATTRIBUTE (STANDARD_SIZE);
203 REGISTER_ATTRIBUTE (STANDARD_ALLOCATED_SIZE);
204 REGISTER_ATTRIBUTE (STANDARD_SYMLINK_TARGET);
205 REGISTER_ATTRIBUTE (STANDARD_TARGET_URI);
206 REGISTER_ATTRIBUTE (STANDARD_SORT_ORDER);
207 REGISTER_ATTRIBUTE (ETAG_VALUE);
208 REGISTER_ATTRIBUTE (ID_FILE);
209 REGISTER_ATTRIBUTE (ID_FILESYSTEM);
210 REGISTER_ATTRIBUTE (ACCESS_CAN_READ);
211 REGISTER_ATTRIBUTE (ACCESS_CAN_WRITE);
212 REGISTER_ATTRIBUTE (ACCESS_CAN_EXECUTE);
213 REGISTER_ATTRIBUTE (ACCESS_CAN_DELETE);
214 REGISTER_ATTRIBUTE (ACCESS_CAN_TRASH);
215 REGISTER_ATTRIBUTE (ACCESS_CAN_RENAME);
216 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_MOUNT);
217 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_UNMOUNT);
218 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_EJECT);
219 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE);
220 REGISTER_ATTRIBUTE (MOUNTABLE_HAL_UDI);
221 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START);
222 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_STOP);
223 REGISTER_ATTRIBUTE (MOUNTABLE_START_STOP_TYPE);
224 REGISTER_ATTRIBUTE (TIME_MODIFIED);
225 REGISTER_ATTRIBUTE (TIME_MODIFIED_USEC);
226 REGISTER_ATTRIBUTE (TIME_ACCESS);
227 REGISTER_ATTRIBUTE (TIME_ACCESS_USEC);
228 REGISTER_ATTRIBUTE (TIME_CHANGED);
229 REGISTER_ATTRIBUTE (TIME_CHANGED_USEC);
230 REGISTER_ATTRIBUTE (TIME_CREATED);
231 REGISTER_ATTRIBUTE (TIME_CREATED_USEC);
232 REGISTER_ATTRIBUTE (UNIX_DEVICE);
233 REGISTER_ATTRIBUTE (UNIX_INODE);
234 REGISTER_ATTRIBUTE (UNIX_MODE);
235 REGISTER_ATTRIBUTE (UNIX_NLINK);
236 REGISTER_ATTRIBUTE (UNIX_UID);
237 REGISTER_ATTRIBUTE (UNIX_GID);
238 REGISTER_ATTRIBUTE (UNIX_RDEV);
239 REGISTER_ATTRIBUTE (UNIX_BLOCK_SIZE);
240 REGISTER_ATTRIBUTE (UNIX_BLOCKS);
241 REGISTER_ATTRIBUTE (UNIX_IS_MOUNTPOINT);
242 REGISTER_ATTRIBUTE (DOS_IS_ARCHIVE);
243 REGISTER_ATTRIBUTE (DOS_IS_SYSTEM);
244 REGISTER_ATTRIBUTE (OWNER_USER);
245 REGISTER_ATTRIBUTE (OWNER_USER_REAL);
246 REGISTER_ATTRIBUTE (OWNER_GROUP);
247 REGISTER_ATTRIBUTE (THUMBNAIL_PATH);
248 REGISTER_ATTRIBUTE (THUMBNAILING_FAILED);
249 REGISTER_ATTRIBUTE (PREVIEW_ICON);
250 REGISTER_ATTRIBUTE (FILESYSTEM_SIZE);
251 REGISTER_ATTRIBUTE (FILESYSTEM_FREE);
252 REGISTER_ATTRIBUTE (FILESYSTEM_TYPE);
253 REGISTER_ATTRIBUTE (FILESYSTEM_READONLY);
254 REGISTER_ATTRIBUTE (FILESYSTEM_USE_PREVIEW);
255 REGISTER_ATTRIBUTE (GVFS_BACKEND);
256 REGISTER_ATTRIBUTE (SELINUX_CONTEXT);
257 REGISTER_ATTRIBUTE (TRASH_ITEM_COUNT);
259 #undef REGISTER_ATTRIBUTE
263 lookup_namespace (const char *namespace)
268 G_LOCK (attribute_hash);
270 ensure_attribute_hash ();
272 ns_info = _lookup_namespace (namespace);
277 G_UNLOCK (attribute_hash);
283 get_attribute_for_id (int attribute)
286 G_LOCK (attribute_hash);
287 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
288 G_UNLOCK (attribute_hash);
293 lookup_attribute (const char *attribute)
297 G_LOCK (attribute_hash);
298 ensure_attribute_hash ();
300 attr_id = _lookup_attribute (attribute);
302 G_UNLOCK (attribute_hash);
308 g_file_info_finalize (GObject *object)
312 GFileAttribute *attrs;
314 info = G_FILE_INFO (object);
316 attrs = (GFileAttribute *)info->attributes->data;
317 for (i = 0; i < info->attributes->len; i++)
318 _g_file_attribute_value_clear (&attrs[i].value);
319 g_array_free (info->attributes, TRUE);
321 if (info->mask != NO_ATTRIBUTE_MASK)
322 g_file_attribute_matcher_unref (info->mask);
324 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
328 g_file_info_class_init (GFileInfoClass *klass)
330 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
332 gobject_class->finalize = g_file_info_finalize;
336 g_file_info_init (GFileInfo *info)
338 info->mask = NO_ATTRIBUTE_MASK;
339 info->attributes = g_array_new (FALSE, FALSE,
340 sizeof (GFileAttribute));
346 * Creates a new file info structure.
348 * Returns: a #GFileInfo.
351 g_file_info_new (void)
353 return g_object_new (G_TYPE_FILE_INFO, NULL);
357 * g_file_info_copy_into:
358 * @src_info: source to copy attributes from.
359 * @dest_info: destination to copy attributes to.
361 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
364 g_file_info_copy_into (GFileInfo *src_info,
365 GFileInfo *dest_info)
367 GFileAttribute *source, *dest;
370 g_return_if_fail (G_IS_FILE_INFO (src_info));
371 g_return_if_fail (G_IS_FILE_INFO (dest_info));
373 dest = (GFileAttribute *)dest_info->attributes->data;
374 for (i = 0; i < dest_info->attributes->len; i++)
375 _g_file_attribute_value_clear (&dest[i].value);
377 g_array_set_size (dest_info->attributes,
378 src_info->attributes->len);
380 source = (GFileAttribute *)src_info->attributes->data;
381 dest = (GFileAttribute *)dest_info->attributes->data;
383 for (i = 0; i < src_info->attributes->len; i++)
385 dest[i].attribute = source[i].attribute;
386 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
387 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
390 if (dest_info->mask != NO_ATTRIBUTE_MASK)
391 g_file_attribute_matcher_unref (dest_info->mask);
393 if (src_info->mask == NO_ATTRIBUTE_MASK)
394 dest_info->mask = NO_ATTRIBUTE_MASK;
396 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
401 * @other: a #GFileInfo.
403 * Duplicates a file info structure.
405 * Returns: a duplicate #GFileInfo of @other.
408 g_file_info_dup (GFileInfo *other)
412 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
414 new = g_file_info_new ();
415 g_file_info_copy_into (other, new);
420 * g_file_info_set_attribute_mask:
421 * @info: a #GFileInfo.
422 * @mask: a #GFileAttributeMatcher.
424 * Sets @mask on @info to match specific attribute types.
427 g_file_info_set_attribute_mask (GFileInfo *info,
428 GFileAttributeMatcher *mask)
430 GFileAttribute *attr;
433 g_return_if_fail (G_IS_FILE_INFO (info));
435 if (mask != info->mask)
437 if (info->mask != NO_ATTRIBUTE_MASK)
438 g_file_attribute_matcher_unref (info->mask);
439 info->mask = g_file_attribute_matcher_ref (mask);
441 /* Remove non-matching attributes */
442 for (i = 0; i < info->attributes->len; i++)
444 attr = &g_array_index (info->attributes, GFileAttribute, i);
445 if (!_g_file_attribute_matcher_matches_id (mask,
448 _g_file_attribute_value_clear (&attr->value);
449 g_array_remove_index (info->attributes, i);
457 * g_file_info_unset_attribute_mask:
460 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
464 g_file_info_unset_attribute_mask (GFileInfo *info)
466 g_return_if_fail (G_IS_FILE_INFO (info));
468 if (info->mask != NO_ATTRIBUTE_MASK)
469 g_file_attribute_matcher_unref (info->mask);
470 info->mask = NO_ATTRIBUTE_MASK;
474 * g_file_info_clear_status:
475 * @info: a #GFileInfo.
477 * Clears the status information from @info.
480 g_file_info_clear_status (GFileInfo *info)
482 GFileAttribute *attrs;
485 g_return_if_fail (G_IS_FILE_INFO (info));
487 attrs = (GFileAttribute *)info->attributes->data;
488 for (i = 0; i < info->attributes->len; i++)
489 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
493 g_file_info_find_place (GFileInfo *info,
497 GFileAttribute *attrs;
498 /* Binary search for the place where attribute would be, if it's
502 max = info->attributes->len;
504 attrs = (GFileAttribute *)info->attributes->data;
508 med = min + (max - min) / 2;
509 if (attrs[med].attribute == attribute)
514 else if (attrs[med].attribute < attribute)
516 else /* attrs[med].attribute > attribute */
523 static GFileAttributeValue *
524 g_file_info_find_value (GFileInfo *info,
527 GFileAttribute *attrs;
530 i = g_file_info_find_place (info, attr_id);
531 attrs = (GFileAttribute *)info->attributes->data;
532 if (i < info->attributes->len &&
533 attrs[i].attribute == attr_id)
534 return &attrs[i].value;
539 static GFileAttributeValue *
540 g_file_info_find_value_by_name (GFileInfo *info,
541 const char *attribute)
545 attr_id = lookup_attribute (attribute);
546 return g_file_info_find_value (info, attr_id);
550 * g_file_info_has_attribute:
551 * @info: a #GFileInfo.
552 * @attribute: a file attribute key.
554 * Checks if a file info structure has an attribute named @attribute.
556 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
560 g_file_info_has_attribute (GFileInfo *info,
561 const char *attribute)
563 GFileAttributeValue *value;
565 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
566 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
568 value = g_file_info_find_value_by_name (info, attribute);
569 return value != NULL;
573 * g_file_info_has_namespace:
574 * @info: a #GFileInfo.
575 * @name_space: a file attribute namespace.
577 * Checks if a file info structure has an attribute in the
578 * specified @name_space.
580 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
586 g_file_info_has_namespace (GFileInfo *info,
587 const char *name_space)
589 GFileAttribute *attrs;
593 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
594 g_return_val_if_fail (name_space != NULL, FALSE);
596 ns_id = lookup_namespace (name_space);
598 attrs = (GFileAttribute *)info->attributes->data;
599 for (i = 0; i < info->attributes->len; i++)
601 if (GET_NS (attrs[i].attribute) == ns_id)
609 * g_file_info_list_attributes:
610 * @info: a #GFileInfo.
611 * @name_space: a file attribute key's namespace.
613 * Lists the file info structure's attributes.
615 * Returns: a null-terminated array of strings of all of the
616 * possible attribute types for the given @name_space, or
620 g_file_info_list_attributes (GFileInfo *info,
621 const char *name_space)
624 GFileAttribute *attrs;
626 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
629 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
631 names = g_ptr_array_new ();
632 attrs = (GFileAttribute *)info->attributes->data;
633 for (i = 0; i < info->attributes->len; i++)
635 attribute = attrs[i].attribute;
636 if (ns_id == 0 || GET_NS (attribute) == ns_id)
637 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
641 g_ptr_array_add (names, NULL);
643 return (char **)g_ptr_array_free (names, FALSE);
647 * g_file_info_get_attribute_type:
648 * @info: a #GFileInfo.
649 * @attribute: a file attribute key.
651 * Gets the attribute type for an attribute key.
653 * Returns: a #GFileAttributeType for the given @attribute, or
654 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
657 g_file_info_get_attribute_type (GFileInfo *info,
658 const char *attribute)
660 GFileAttributeValue *value;
662 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
663 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
665 value = g_file_info_find_value_by_name (info, attribute);
669 return G_FILE_ATTRIBUTE_TYPE_INVALID;
673 * g_file_info_remove_attribute:
674 * @info: a #GFileInfo.
675 * @attribute: a file attribute key.
677 * Removes all cases of @attribute from @info if it exists.
680 g_file_info_remove_attribute (GFileInfo *info,
681 const char *attribute)
684 GFileAttribute *attrs;
687 g_return_if_fail (G_IS_FILE_INFO (info));
688 g_return_if_fail (attribute != NULL && *attribute != '\0');
690 attr_id = lookup_attribute (attribute);
692 i = g_file_info_find_place (info, attr_id);
693 attrs = (GFileAttribute *)info->attributes->data;
694 if (i < info->attributes->len &&
695 attrs[i].attribute == attr_id)
697 _g_file_attribute_value_clear (&attrs[i].value);
698 g_array_remove_index (info->attributes, i);
703 * g_file_info_get_attribute_data:
704 * @info: a #GFileInfo
705 * @attribute: a file attribute key
706 * @type: return location for the attribute type, or %NULL
707 * @value_pp: return location for the attribute value, or %NULL
708 * @status: return location for the attribute status, or %NULL
710 * Gets the attribute type, value and status for an attribute key.
712 * Returns: %TRUE if @info has an attribute named @attribute,
716 g_file_info_get_attribute_data (GFileInfo *info,
717 const char *attribute,
718 GFileAttributeType *type,
720 GFileAttributeStatus *status)
722 GFileAttributeValue *value;
724 value = g_file_info_find_value_by_name (info, attribute);
729 *status = value->status;
735 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
741 * g_file_info_get_attribute_status:
742 * @info: a #GFileInfo
743 * @attribute: a file attribute key
745 * Gets the attribute status for an attribute key.
747 * Returns: a #GFileAttributeStatus for the given @attribute, or
748 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
752 g_file_info_get_attribute_status (GFileInfo *info,
753 const char *attribute)
755 GFileAttributeValue *val;
757 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
758 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
760 val = g_file_info_find_value_by_name (info, attribute);
764 return G_FILE_ATTRIBUTE_STATUS_UNSET;
768 * g_file_info_set_attribute_status:
769 * @info: a #GFileInfo
770 * @attribute: a file attribute key
771 * @status: a #GFileAttributeStatus
773 * Sets the attribute status for an attribute key. This is only
774 * needed by external code that implement g_file_set_attributes_from_info()
775 * or similar functions.
777 * The attribute must exist in @info for this to work. Otherwise %FALSE
778 * is returned and @info is unchanged.
780 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
785 g_file_info_set_attribute_status (GFileInfo *info,
786 const char *attribute,
787 GFileAttributeStatus status)
789 GFileAttributeValue *val;
791 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
792 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
794 val = g_file_info_find_value_by_name (info, attribute);
797 val->status = status;
804 GFileAttributeValue *
805 _g_file_info_get_attribute_value (GFileInfo *info,
806 const char *attribute)
809 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
810 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
812 return g_file_info_find_value_by_name (info, attribute);
816 * g_file_info_get_attribute_as_string:
817 * @info: a #GFileInfo.
818 * @attribute: a file attribute key.
820 * Gets the value of a attribute, formated as a string.
821 * This escapes things as needed to make the string valid
824 * Returns: a UTF-8 string associated with the given @attribute.
825 * When you're done with the string it must be freed with g_free().
828 g_file_info_get_attribute_as_string (GFileInfo *info,
829 const char *attribute)
831 GFileAttributeValue *val;
832 val = _g_file_info_get_attribute_value (info, attribute);
834 return _g_file_attribute_value_as_string (val);
840 * g_file_info_get_attribute_object:
841 * @info: a #GFileInfo.
842 * @attribute: a file attribute key.
844 * Gets the value of a #GObject attribute. If the attribute does
845 * not contain a #GObject, %NULL will be returned.
847 * Returns: a #GObject associated with the given @attribute, or
851 g_file_info_get_attribute_object (GFileInfo *info,
852 const char *attribute)
854 GFileAttributeValue *value;
856 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
857 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
859 value = g_file_info_find_value_by_name (info, attribute);
860 return _g_file_attribute_value_get_object (value);
864 * g_file_info_get_attribute_string:
865 * @info: a #GFileInfo.
866 * @attribute: a file attribute key.
868 * Gets the value of a string attribute. If the attribute does
869 * not contain a string, %NULL will be returned.
871 * Returns: the contents of the @attribute value as a string, or
875 g_file_info_get_attribute_string (GFileInfo *info,
876 const char *attribute)
878 GFileAttributeValue *value;
880 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
881 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
883 value = g_file_info_find_value_by_name (info, attribute);
884 return _g_file_attribute_value_get_string (value);
888 * g_file_info_get_attribute_byte_string:
889 * @info: a #GFileInfo.
890 * @attribute: a file attribute key.
892 * Gets the value of a byte string attribute. If the attribute does
893 * not contain a byte string, %NULL will be returned.
895 * Returns: the contents of the @attribute value as a byte string, or
899 g_file_info_get_attribute_byte_string (GFileInfo *info,
900 const char *attribute)
902 GFileAttributeValue *value;
904 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
905 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
907 value = g_file_info_find_value_by_name (info, attribute);
908 return _g_file_attribute_value_get_byte_string (value);
912 * g_file_info_get_attribute_stringv:
913 * @info: a #GFileInfo.
914 * @attribute: a file attribute key.
916 * Gets the value of a stringv attribute. If the attribute does
917 * not contain a stringv, %NULL will be returned.
919 * Returns: the contents of the @attribute value as a stringv, or
920 * %NULL otherwise. Do not free.
923 g_file_info_get_attribute_stringv (GFileInfo *info,
924 const char *attribute)
926 GFileAttributeValue *value;
928 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
929 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
931 value = g_file_info_find_value_by_name (info, attribute);
932 return _g_file_attribute_value_get_stringv (value);
936 * g_file_info_get_attribute_boolean:
937 * @info: a #GFileInfo.
938 * @attribute: a file attribute key.
940 * Gets the value of a boolean attribute. If the attribute does not
941 * contain a boolean value, %FALSE will be returned.
943 * Returns: the boolean value contained within the attribute.
946 g_file_info_get_attribute_boolean (GFileInfo *info,
947 const char *attribute)
949 GFileAttributeValue *value;
951 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
952 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
954 value = g_file_info_find_value_by_name (info, attribute);
955 return _g_file_attribute_value_get_boolean (value);
959 * g_file_info_get_attribute_uint32:
960 * @info: a #GFileInfo.
961 * @attribute: a file attribute key.
963 * Gets an unsigned 32-bit integer contained within the attribute. If the
964 * attribute does not contain an unsigned 32-bit integer, or is invalid,
965 * 0 will be returned.
967 * Returns: an unsigned 32-bit integer from the attribute.
970 g_file_info_get_attribute_uint32 (GFileInfo *info,
971 const char *attribute)
973 GFileAttributeValue *value;
975 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
976 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
978 value = g_file_info_find_value_by_name (info, attribute);
979 return _g_file_attribute_value_get_uint32 (value);
983 * g_file_info_get_attribute_int32:
984 * @info: a #GFileInfo.
985 * @attribute: a file attribute key.
987 * Gets a signed 32-bit integer contained within the attribute. If the
988 * attribute does not contain a signed 32-bit integer, or is invalid,
989 * 0 will be returned.
991 * Returns: a signed 32-bit integer from the attribute.
994 g_file_info_get_attribute_int32 (GFileInfo *info,
995 const char *attribute)
997 GFileAttributeValue *value;
999 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1000 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1002 value = g_file_info_find_value_by_name (info, attribute);
1003 return _g_file_attribute_value_get_int32 (value);
1007 * g_file_info_get_attribute_uint64:
1008 * @info: a #GFileInfo.
1009 * @attribute: a file attribute key.
1011 * Gets a unsigned 64-bit integer contained within the attribute. If the
1012 * attribute does not contain an unsigned 64-bit integer, or is invalid,
1013 * 0 will be returned.
1015 * Returns: a unsigned 64-bit integer from the attribute.
1018 g_file_info_get_attribute_uint64 (GFileInfo *info,
1019 const char *attribute)
1021 GFileAttributeValue *value;
1023 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1024 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1026 value = g_file_info_find_value_by_name (info, attribute);
1027 return _g_file_attribute_value_get_uint64 (value);
1031 * g_file_info_get_attribute_int64:
1032 * @info: a #GFileInfo.
1033 * @attribute: a file attribute key.
1035 * Gets a signed 64-bit integer contained within the attribute. If the
1036 * attribute does not contain an signed 64-bit integer, or is invalid,
1037 * 0 will be returned.
1039 * Returns: a signed 64-bit integer from the attribute.
1042 g_file_info_get_attribute_int64 (GFileInfo *info,
1043 const char *attribute)
1045 GFileAttributeValue *value;
1047 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1048 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1050 value = g_file_info_find_value_by_name (info, attribute);
1051 return _g_file_attribute_value_get_int64 (value);
1054 static GFileAttributeValue *
1055 g_file_info_create_value (GFileInfo *info,
1058 GFileAttribute *attrs;
1061 if (info->mask != NO_ATTRIBUTE_MASK &&
1062 !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
1065 i = g_file_info_find_place (info, attr_id);
1067 attrs = (GFileAttribute *)info->attributes->data;
1068 if (i < info->attributes->len &&
1069 attrs[i].attribute == attr_id)
1070 return &attrs[i].value;
1073 GFileAttribute attr = { 0 };
1074 attr.attribute = attr_id;
1075 g_array_insert_val (info->attributes, i, attr);
1077 attrs = (GFileAttribute *)info->attributes->data;
1078 return &attrs[i].value;
1082 static GFileAttributeValue *
1083 g_file_info_create_value_by_name (GFileInfo *info,
1084 const char *attribute)
1088 attr_id = lookup_attribute (attribute);
1090 return g_file_info_create_value (info, attr_id);
1094 * g_file_info_set_attribute:
1095 * @info: a #GFileInfo.
1096 * @attribute: a file attribute key.
1097 * @type: a #GFileAttributeType
1098 * @value_p: pointer to the value
1100 * Sets the @attribute to contain the given value, if possible.
1103 g_file_info_set_attribute (GFileInfo *info,
1104 const char *attribute,
1105 GFileAttributeType type,
1108 GFileAttributeValue *value;
1110 g_return_if_fail (G_IS_FILE_INFO (info));
1111 g_return_if_fail (attribute != NULL && *attribute != '\0');
1113 value = g_file_info_create_value_by_name (info, attribute);
1116 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1120 * g_file_info_set_attribute_object:
1121 * @info: a #GFileInfo.
1122 * @attribute: a file attribute key.
1123 * @attr_value: a #GObject.
1125 * Sets the @attribute to contain the given @attr_value,
1129 g_file_info_set_attribute_object (GFileInfo *info,
1130 const char *attribute,
1131 GObject *attr_value)
1133 GFileAttributeValue *value;
1135 g_return_if_fail (G_IS_FILE_INFO (info));
1136 g_return_if_fail (attribute != NULL && *attribute != '\0');
1137 g_return_if_fail (G_IS_OBJECT (attr_value));
1139 value = g_file_info_create_value_by_name (info, attribute);
1141 _g_file_attribute_value_set_object (value, attr_value);
1145 * g_file_info_set_attribute_stringv:
1146 * @info: a #GFileInfo.
1147 * @attribute: a file attribute key.
1148 * @attr_value: a %NULL terminated string array
1150 * Sets the @attribute to contain the given @attr_value,
1156 g_file_info_set_attribute_stringv (GFileInfo *info,
1157 const char *attribute,
1160 GFileAttributeValue *value;
1162 g_return_if_fail (G_IS_FILE_INFO (info));
1163 g_return_if_fail (attribute != NULL && *attribute != '\0');
1164 g_return_if_fail (attr_value != NULL);
1166 value = g_file_info_create_value_by_name (info, attribute);
1168 _g_file_attribute_value_set_stringv (value, attr_value);
1172 * g_file_info_set_attribute_string:
1173 * @info: a #GFileInfo.
1174 * @attribute: a file attribute key.
1175 * @attr_value: a string.
1177 * Sets the @attribute to contain the given @attr_value,
1181 g_file_info_set_attribute_string (GFileInfo *info,
1182 const char *attribute,
1183 const char *attr_value)
1185 GFileAttributeValue *value;
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 value = g_file_info_create_value_by_name (info, attribute);
1193 _g_file_attribute_value_set_string (value, attr_value);
1197 * g_file_info_set_attribute_byte_string:
1198 * @info: a #GFileInfo.
1199 * @attribute: a file attribute key.
1200 * @attr_value: a byte string.
1202 * Sets the @attribute to contain the given @attr_value,
1206 g_file_info_set_attribute_byte_string (GFileInfo *info,
1207 const char *attribute,
1208 const char *attr_value)
1210 GFileAttributeValue *value;
1212 g_return_if_fail (G_IS_FILE_INFO (info));
1213 g_return_if_fail (attribute != NULL && *attribute != '\0');
1214 g_return_if_fail (attr_value != NULL);
1216 value = g_file_info_create_value_by_name (info, attribute);
1218 _g_file_attribute_value_set_byte_string (value, attr_value);
1222 * g_file_info_set_attribute_boolean:
1223 * @info: a #GFileInfo.
1224 * @attribute: a file attribute key.
1225 * @attr_value: a boolean value.
1227 * Sets the @attribute to contain the given @attr_value,
1231 g_file_info_set_attribute_boolean (GFileInfo *info,
1232 const char *attribute,
1233 gboolean attr_value)
1235 GFileAttributeValue *value;
1237 g_return_if_fail (G_IS_FILE_INFO (info));
1238 g_return_if_fail (attribute != NULL && *attribute != '\0');
1240 value = g_file_info_create_value_by_name (info, attribute);
1242 _g_file_attribute_value_set_boolean (value, attr_value);
1246 * g_file_info_set_attribute_uint32:
1247 * @info: a #GFileInfo.
1248 * @attribute: a file attribute key.
1249 * @attr_value: an unsigned 32-bit integer.
1251 * Sets the @attribute to contain the given @attr_value,
1255 g_file_info_set_attribute_uint32 (GFileInfo *info,
1256 const char *attribute,
1259 GFileAttributeValue *value;
1261 g_return_if_fail (G_IS_FILE_INFO (info));
1262 g_return_if_fail (attribute != NULL && *attribute != '\0');
1264 value = g_file_info_create_value_by_name (info, attribute);
1266 _g_file_attribute_value_set_uint32 (value, attr_value);
1271 * g_file_info_set_attribute_int32:
1272 * @info: a #GFileInfo.
1273 * @attribute: a file attribute key.
1274 * @attr_value: a signed 32-bit integer
1276 * Sets the @attribute to contain the given @attr_value,
1280 g_file_info_set_attribute_int32 (GFileInfo *info,
1281 const char *attribute,
1284 GFileAttributeValue *value;
1286 g_return_if_fail (G_IS_FILE_INFO (info));
1287 g_return_if_fail (attribute != NULL && *attribute != '\0');
1289 value = g_file_info_create_value_by_name (info, attribute);
1291 _g_file_attribute_value_set_int32 (value, attr_value);
1295 * g_file_info_set_attribute_uint64:
1296 * @info: a #GFileInfo.
1297 * @attribute: a file attribute key.
1298 * @attr_value: an unsigned 64-bit integer.
1300 * Sets the @attribute to contain the given @attr_value,
1304 g_file_info_set_attribute_uint64 (GFileInfo *info,
1305 const char *attribute,
1308 GFileAttributeValue *value;
1310 g_return_if_fail (G_IS_FILE_INFO (info));
1311 g_return_if_fail (attribute != NULL && *attribute != '\0');
1313 value = g_file_info_create_value_by_name (info, attribute);
1315 _g_file_attribute_value_set_uint64 (value, attr_value);
1319 * g_file_info_set_attribute_int64:
1320 * @info: a #GFileInfo.
1321 * @attribute: attribute name to set.
1322 * @attr_value: int64 value to set attribute to.
1324 * Sets the @attribute to contain the given @attr_value,
1329 g_file_info_set_attribute_int64 (GFileInfo *info,
1330 const char *attribute,
1333 GFileAttributeValue *value;
1335 g_return_if_fail (G_IS_FILE_INFO (info));
1336 g_return_if_fail (attribute != NULL && *attribute != '\0');
1338 value = g_file_info_create_value_by_name (info, attribute);
1340 _g_file_attribute_value_set_int64 (value, attr_value);
1343 /* Helper getters */
1345 * g_file_info_get_file_type:
1346 * @info: a #GFileInfo.
1348 * Gets a file's type (whether it is a regular file, symlink, etc).
1349 * This is different from the file's content type, see g_file_info_get_content_type().
1351 * Returns: a #GFileType for the given file.
1354 g_file_info_get_file_type (GFileInfo *info)
1356 static guint32 attr = 0;
1357 GFileAttributeValue *value;
1359 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1362 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1364 value = g_file_info_find_value (info, attr);
1365 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1369 * g_file_info_get_is_hidden:
1370 * @info: a #GFileInfo.
1372 * Checks if a file is hidden.
1374 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1377 g_file_info_get_is_hidden (GFileInfo *info)
1379 static guint32 attr = 0;
1380 GFileAttributeValue *value;
1382 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1385 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1387 value = g_file_info_find_value (info, attr);
1388 return (GFileType)_g_file_attribute_value_get_boolean (value);
1392 * g_file_info_get_is_backup:
1393 * @info: a #GFileInfo.
1395 * Checks if a file is a backup file.
1397 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1400 g_file_info_get_is_backup (GFileInfo *info)
1402 static guint32 attr = 0;
1403 GFileAttributeValue *value;
1405 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1408 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1410 value = g_file_info_find_value (info, attr);
1411 return (GFileType)_g_file_attribute_value_get_boolean (value);
1415 * g_file_info_get_is_symlink:
1416 * @info: a #GFileInfo.
1418 * Checks if a file is a symlink.
1420 * Returns: %TRUE if the given @info is a symlink.
1423 g_file_info_get_is_symlink (GFileInfo *info)
1425 static guint32 attr = 0;
1426 GFileAttributeValue *value;
1428 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1431 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1433 value = g_file_info_find_value (info, attr);
1434 return (GFileType)_g_file_attribute_value_get_boolean (value);
1438 * g_file_info_get_name:
1439 * @info: a #GFileInfo.
1441 * Gets the name for a file.
1443 * Returns: a string containing the file name.
1446 g_file_info_get_name (GFileInfo *info)
1448 static guint32 attr = 0;
1449 GFileAttributeValue *value;
1451 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1454 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1456 value = g_file_info_find_value (info, attr);
1457 return _g_file_attribute_value_get_byte_string (value);
1461 * g_file_info_get_display_name:
1462 * @info: a #GFileInfo.
1464 * Gets a display name for a file.
1466 * Returns: a string containing the display name.
1469 g_file_info_get_display_name (GFileInfo *info)
1471 static guint32 attr = 0;
1472 GFileAttributeValue *value;
1474 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1477 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1479 value = g_file_info_find_value (info, attr);
1480 return _g_file_attribute_value_get_string (value);
1484 * g_file_info_get_edit_name:
1485 * @info: a #GFileInfo.
1487 * Gets the edit name for a file.
1489 * Returns: a string containing the edit name.
1492 g_file_info_get_edit_name (GFileInfo *info)
1494 static guint32 attr = 0;
1495 GFileAttributeValue *value;
1497 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1500 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1502 value = g_file_info_find_value (info, attr);
1503 return _g_file_attribute_value_get_string (value);
1507 * g_file_info_get_icon:
1508 * @info: a #GFileInfo.
1510 * Gets the icon for a file.
1512 * Returns: #GIcon for the given @info.
1515 g_file_info_get_icon (GFileInfo *info)
1517 static guint32 attr = 0;
1518 GFileAttributeValue *value;
1521 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1524 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1526 value = g_file_info_find_value (info, attr);
1527 obj = _g_file_attribute_value_get_object (value);
1528 if (G_IS_ICON (obj))
1529 return G_ICON (obj);
1534 * g_file_info_get_content_type:
1535 * @info: a #GFileInfo.
1537 * Gets the file's content type.
1539 * Returns: a string containing the file's content type.
1542 g_file_info_get_content_type (GFileInfo *info)
1544 static guint32 attr = 0;
1545 GFileAttributeValue *value;
1547 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1550 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1552 value = g_file_info_find_value (info, attr);
1553 return _g_file_attribute_value_get_string (value);
1557 * g_file_info_get_size:
1558 * @info: a #GFileInfo.
1560 * Gets the file's size.
1562 * Returns: a #goffset containing the file's size.
1565 g_file_info_get_size (GFileInfo *info)
1567 static guint32 attr = 0;
1568 GFileAttributeValue *value;
1570 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1573 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1575 value = g_file_info_find_value (info, attr);
1576 return (goffset) _g_file_attribute_value_get_uint64 (value);
1580 * g_file_info_get_modification_time:
1581 * @info: a #GFileInfo.
1582 * @result: a #GTimeVal.
1584 * Gets the modification time of the current @info and sets it
1588 g_file_info_get_modification_time (GFileInfo *info,
1591 static guint32 attr_mtime = 0, attr_mtime_usec;
1592 GFileAttributeValue *value;
1594 g_return_if_fail (G_IS_FILE_INFO (info));
1595 g_return_if_fail (result != NULL);
1597 if (attr_mtime == 0)
1599 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1600 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1603 value = g_file_info_find_value (info, attr_mtime);
1604 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1605 value = g_file_info_find_value (info, attr_mtime_usec);
1606 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1610 * g_file_info_get_symlink_target:
1611 * @info: a #GFileInfo.
1613 * Gets the symlink target for a given #GFileInfo.
1615 * Returns: a string containing the symlink target.
1618 g_file_info_get_symlink_target (GFileInfo *info)
1620 static guint32 attr = 0;
1621 GFileAttributeValue *value;
1623 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1626 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1628 value = g_file_info_find_value (info, attr);
1629 return _g_file_attribute_value_get_byte_string (value);
1633 * g_file_info_get_etag:
1634 * @info: a #GFileInfo.
1636 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1637 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1639 * Returns: a string containing the value of the "etag:value" attribute.
1642 g_file_info_get_etag (GFileInfo *info)
1644 static guint32 attr = 0;
1645 GFileAttributeValue *value;
1647 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1650 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1652 value = g_file_info_find_value (info, attr);
1653 return _g_file_attribute_value_get_string (value);
1657 * g_file_info_get_sort_order:
1658 * @info: a #GFileInfo.
1660 * Gets the value of the sort_order attribute from the #GFileInfo.
1661 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1663 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1666 g_file_info_get_sort_order (GFileInfo *info)
1668 static guint32 attr = 0;
1669 GFileAttributeValue *value;
1671 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1674 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1676 value = g_file_info_find_value (info, attr);
1677 return _g_file_attribute_value_get_int32 (value);
1680 /* Helper setters: */
1682 * g_file_info_set_file_type:
1683 * @info: a #GFileInfo.
1684 * @type: a #GFileType.
1686 * Sets the file type in a #GFileInfo to @type.
1687 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1690 g_file_info_set_file_type (GFileInfo *info,
1693 static guint32 attr = 0;
1694 GFileAttributeValue *value;
1696 g_return_if_fail (G_IS_FILE_INFO (info));
1699 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1701 value = g_file_info_create_value (info, attr);
1703 _g_file_attribute_value_set_uint32 (value, type);
1707 * g_file_info_set_is_hidden:
1708 * @info: a #GFileInfo.
1709 * @is_hidden: a #gboolean.
1711 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1712 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1715 g_file_info_set_is_hidden (GFileInfo *info,
1718 static guint32 attr = 0;
1719 GFileAttributeValue *value;
1721 g_return_if_fail (G_IS_FILE_INFO (info));
1724 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1726 value = g_file_info_create_value (info, attr);
1728 _g_file_attribute_value_set_boolean (value, is_hidden);
1732 * g_file_info_set_is_symlink:
1733 * @info: a #GFileInfo.
1734 * @is_symlink: a #gboolean.
1736 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1737 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1740 g_file_info_set_is_symlink (GFileInfo *info,
1741 gboolean is_symlink)
1743 static guint32 attr = 0;
1744 GFileAttributeValue *value;
1746 g_return_if_fail (G_IS_FILE_INFO (info));
1749 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1751 value = g_file_info_create_value (info, attr);
1753 _g_file_attribute_value_set_boolean (value, is_symlink);
1757 * g_file_info_set_name:
1758 * @info: a #GFileInfo.
1759 * @name: a string containing a name.
1761 * Sets the name attribute for the current #GFileInfo.
1762 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1765 g_file_info_set_name (GFileInfo *info,
1768 static guint32 attr = 0;
1769 GFileAttributeValue *value;
1771 g_return_if_fail (G_IS_FILE_INFO (info));
1772 g_return_if_fail (name != NULL);
1775 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1777 value = g_file_info_create_value (info, attr);
1779 _g_file_attribute_value_set_byte_string (value, name);
1783 * g_file_info_set_display_name:
1784 * @info: a #GFileInfo.
1785 * @display_name: a string containing a display name.
1787 * Sets the display name for the current #GFileInfo.
1788 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1791 g_file_info_set_display_name (GFileInfo *info,
1792 const char *display_name)
1794 static guint32 attr = 0;
1795 GFileAttributeValue *value;
1797 g_return_if_fail (G_IS_FILE_INFO (info));
1798 g_return_if_fail (display_name != NULL);
1801 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1803 value = g_file_info_create_value (info, attr);
1805 _g_file_attribute_value_set_string (value, display_name);
1809 * g_file_info_set_edit_name:
1810 * @info: a #GFileInfo.
1811 * @edit_name: a string containing an edit name.
1813 * Sets the edit name for the current file.
1814 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1817 g_file_info_set_edit_name (GFileInfo *info,
1818 const char *edit_name)
1820 static guint32 attr = 0;
1821 GFileAttributeValue *value;
1823 g_return_if_fail (G_IS_FILE_INFO (info));
1824 g_return_if_fail (edit_name != NULL);
1827 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1829 value = g_file_info_create_value (info, attr);
1831 _g_file_attribute_value_set_string (value, edit_name);
1835 * g_file_info_set_icon:
1836 * @info: a #GFileInfo.
1839 * Sets the icon for a given #GFileInfo.
1840 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1843 g_file_info_set_icon (GFileInfo *info,
1846 static guint32 attr = 0;
1847 GFileAttributeValue *value;
1849 g_return_if_fail (G_IS_FILE_INFO (info));
1850 g_return_if_fail (G_IS_ICON (icon));
1853 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1855 value = g_file_info_create_value (info, attr);
1857 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1861 * g_file_info_set_content_type:
1862 * @info: a #GFileInfo.
1863 * @content_type: a content type. See #GContentType.
1865 * Sets the content type attribute for a given #GFileInfo.
1866 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1869 g_file_info_set_content_type (GFileInfo *info,
1870 const char *content_type)
1872 static guint32 attr = 0;
1873 GFileAttributeValue *value;
1875 g_return_if_fail (G_IS_FILE_INFO (info));
1876 g_return_if_fail (content_type != NULL);
1879 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1881 value = g_file_info_create_value (info, attr);
1883 _g_file_attribute_value_set_string (value, content_type);
1887 * g_file_info_set_size:
1888 * @info: a #GFileInfo.
1889 * @size: a #goffset containing the file's size.
1891 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1892 * to the given size.
1895 g_file_info_set_size (GFileInfo *info,
1898 static guint32 attr = 0;
1899 GFileAttributeValue *value;
1901 g_return_if_fail (G_IS_FILE_INFO (info));
1904 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1906 value = g_file_info_create_value (info, attr);
1908 _g_file_attribute_value_set_uint64 (value, size);
1912 * g_file_info_set_modification_time
1913 * @info: a #GFileInfo.
1914 * @mtime: a #GTimeVal.
1916 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
1917 * info to the given time value.
1920 g_file_info_set_modification_time (GFileInfo *info,
1923 static guint32 attr_mtime = 0, attr_mtime_usec;
1924 GFileAttributeValue *value;
1926 g_return_if_fail (G_IS_FILE_INFO (info));
1927 g_return_if_fail (mtime != NULL);
1929 if (attr_mtime == 0)
1931 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1932 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1935 value = g_file_info_create_value (info, attr_mtime);
1937 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
1938 value = g_file_info_create_value (info, attr_mtime_usec);
1940 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
1944 * g_file_info_set_symlink_target:
1945 * @info: a #GFileInfo.
1946 * @symlink_target: a static string containing a path to a symlink target.
1948 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
1949 * to the given symlink target.
1952 g_file_info_set_symlink_target (GFileInfo *info,
1953 const char *symlink_target)
1955 static guint32 attr = 0;
1956 GFileAttributeValue *value;
1958 g_return_if_fail (G_IS_FILE_INFO (info));
1959 g_return_if_fail (symlink_target != NULL);
1962 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1964 value = g_file_info_create_value (info, attr);
1966 _g_file_attribute_value_set_byte_string (value, symlink_target);
1970 * g_file_info_set_sort_order:
1971 * @info: a #GFileInfo.
1972 * @sort_order: a sort order integer.
1974 * Sets the sort order attribute in the file info structure. See
1975 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1978 g_file_info_set_sort_order (GFileInfo *info,
1981 static guint32 attr = 0;
1982 GFileAttributeValue *value;
1984 g_return_if_fail (G_IS_FILE_INFO (info));
1987 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1989 value = g_file_info_create_value (info, attr);
1991 _g_file_attribute_value_set_int32 (value, sort_order);
1995 #define ON_STACK_MATCHERS 5
2002 struct _GFileAttributeMatcher {
2004 SubMatcher sub_matchers[ON_STACK_MATCHERS];
2005 GArray *more_sub_matchers;
2008 guint32 iterator_ns;
2014 matcher_add (GFileAttributeMatcher *matcher,
2018 SubMatcher *sub_matchers;
2022 for (i = 0; i < ON_STACK_MATCHERS; i++)
2024 /* First empty spot, not found, use this */
2025 if (matcher->sub_matchers[i].id == 0)
2027 matcher->sub_matchers[i].id = id;
2028 matcher->sub_matchers[i].mask = mask;
2033 if (matcher->sub_matchers[i].id == id &&
2034 matcher->sub_matchers[i].mask == mask)
2038 if (matcher->more_sub_matchers == NULL)
2039 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2041 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2042 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2045 if (sub_matchers[i].id == id &&
2046 sub_matchers[i].mask == mask)
2053 g_array_append_val (matcher->more_sub_matchers, s);
2057 * g_file_attribute_matcher_new:
2058 * @attributes: an attribute string to match.
2060 * Creates a new file attribute matcher, which matches attributes
2061 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
2062 * counted structures, and are created with a reference count of 1. If
2063 * the number of references falls to 0, the #GFileAttributeMatcher is
2064 * automatically destroyed.
2066 * The @attribute string should be formatted with specific keys separated
2067 * from namespaces with a double colon. Several "namespace::key" strings may be
2068 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
2069 * The wildcard "*" may be used to match all keys and namespaces, or
2070 * "namespace::*" will match all keys in a given namespace.
2072 * Examples of strings to use:
2074 * <title>File Attribute Matcher strings and results</title>
2075 * <tgroup cols='2' align='left'><thead>
2076 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
2078 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
2079 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
2080 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
2081 * all keys in the unix namespace.</entry></row>
2085 * Returns: a #GFileAttributeMatcher.
2087 GFileAttributeMatcher *
2088 g_file_attribute_matcher_new (const char *attributes)
2093 GFileAttributeMatcher *matcher;
2095 if (attributes == NULL || *attributes == '\0')
2098 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2101 split = g_strsplit (attributes, ",", -1);
2103 for (i = 0; split[i] != NULL; i++)
2105 if (strcmp (split[i], "*") == 0)
2106 matcher->all = TRUE;
2111 colon = strstr (split[i], "::");
2112 if (colon != NULL &&
2117 id = lookup_attribute (split[i]);
2125 id = lookup_namespace (split[i]) << NS_POS;
2126 mask = NS_MASK << NS_POS;
2129 matcher_add (matcher, id, mask);
2139 * g_file_attribute_matcher_ref:
2140 * @matcher: a #GFileAttributeMatcher.
2142 * References a file attribute matcher.
2144 * Returns: a #GFileAttributeMatcher.
2146 GFileAttributeMatcher *
2147 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2151 g_return_val_if_fail (matcher->ref > 0, NULL);
2152 g_atomic_int_inc (&matcher->ref);
2158 * g_file_attribute_matcher_unref:
2159 * @matcher: a #GFileAttributeMatcher.
2161 * Unreferences @matcher. If the reference count falls below 1,
2162 * the @matcher is automatically freed.
2166 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2170 g_return_if_fail (matcher->ref > 0);
2172 if (g_atomic_int_dec_and_test (&matcher->ref))
2174 if (matcher->more_sub_matchers)
2175 g_array_free (matcher->more_sub_matchers, TRUE);
2183 * g_file_attribute_matcher_matches_only:
2184 * @matcher: a #GFileAttributeMatcher.
2185 * @attribute: a file attribute key.
2187 * Checks if a attribute matcher only matches a given attribute. Always
2188 * returns %FALSE if "*" was used when creating the matcher.
2190 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2193 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2194 const char *attribute)
2198 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2200 if (matcher == NULL ||
2204 id = lookup_attribute (attribute);
2206 if (matcher->sub_matchers[0].id != 0 &&
2207 matcher->sub_matchers[1].id == 0 &&
2208 matcher->sub_matchers[0].mask == 0xffffffff &&
2209 matcher->sub_matchers[0].id == id)
2216 matcher_matches_id (GFileAttributeMatcher *matcher,
2219 SubMatcher *sub_matchers;
2222 for (i = 0; i < ON_STACK_MATCHERS; i++)
2224 if (matcher->sub_matchers[i].id == 0)
2227 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2231 if (matcher->more_sub_matchers)
2233 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2234 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2236 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2245 _g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2248 /* We return a NULL matcher for an empty match string, so handle this */
2249 if (matcher == NULL)
2255 return matcher_matches_id (matcher, id);
2259 * g_file_attribute_matcher_matches:
2260 * @matcher: a #GFileAttributeMatcher.
2261 * @attribute: a file attribute key.
2263 * Checks if an attribute will be matched by an attribute matcher. If
2264 * the matcher was created with the "*" matching string, this function
2265 * will always return %TRUE.
2267 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2270 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2271 const char *attribute)
2273 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2275 /* We return a NULL matcher for an empty match string, so handle this */
2276 if (matcher == NULL)
2282 return matcher_matches_id (matcher, lookup_attribute (attribute));
2285 /* return TRUE -> all */
2287 * g_file_attribute_matcher_enumerate_namespace:
2288 * @matcher: a #GFileAttributeMatcher.
2289 * @ns: a string containing a file attribute namespace.
2291 * Checks if the matcher will match all of the keys in a given namespace.
2292 * This will always return %TRUE if a wildcard character is in use (e.g. if
2293 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2294 * using "*" and namespace is anything.)
2296 * TODO: this is awkwardly worded.
2298 * Returns: %TRUE if the matcher matches all of the entries
2299 * in the given @ns, %FALSE otherwise.
2302 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2305 SubMatcher *sub_matchers;
2309 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2311 /* We return a NULL matcher for an empty match string, so handle this */
2312 if (matcher == NULL)
2318 ns_id = lookup_namespace (ns) << NS_POS;
2320 for (i = 0; i < ON_STACK_MATCHERS; i++)
2322 if (matcher->sub_matchers[i].id == ns_id)
2326 if (matcher->more_sub_matchers)
2328 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2329 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2331 if (sub_matchers[i].id == ns_id)
2336 matcher->iterator_ns = ns_id;
2337 matcher->iterator_pos = 0;
2343 * g_file_attribute_matcher_enumerate_next:
2344 * @matcher: a #GFileAttributeMatcher.
2346 * Gets the next matched attribute from a #GFileAttributeMatcher.
2348 * Returns: a string containing the next attribute or %NULL if
2349 * no more attribute exist.
2352 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2355 SubMatcher *sub_matcher;
2357 /* We return a NULL matcher for an empty match string, so handle this */
2358 if (matcher == NULL)
2363 i = matcher->iterator_pos++;
2365 if (i < ON_STACK_MATCHERS)
2367 if (matcher->sub_matchers[i].id == 0)
2370 sub_matcher = &matcher->sub_matchers[i];
2374 if (matcher->more_sub_matchers == NULL)
2377 i -= ON_STACK_MATCHERS;
2378 if (i < matcher->more_sub_matchers->len)
2379 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2384 if (sub_matcher->mask == 0xffffffff &&
2385 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2386 return get_attribute_for_id (sub_matcher->id);
2390 #define __G_FILE_INFO_C__
2391 #include "gioaliasdef.c"