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_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);
263 #undef REGISTER_ATTRIBUTE
267 lookup_namespace (const char *namespace)
272 G_LOCK (attribute_hash);
274 ensure_attribute_hash ();
276 ns_info = _lookup_namespace (namespace);
281 G_UNLOCK (attribute_hash);
287 get_attribute_for_id (int attribute)
290 G_LOCK (attribute_hash);
291 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
292 G_UNLOCK (attribute_hash);
297 lookup_attribute (const char *attribute)
301 G_LOCK (attribute_hash);
302 ensure_attribute_hash ();
304 attr_id = _lookup_attribute (attribute);
306 G_UNLOCK (attribute_hash);
312 g_file_info_finalize (GObject *object)
316 GFileAttribute *attrs;
318 info = G_FILE_INFO (object);
320 attrs = (GFileAttribute *)info->attributes->data;
321 for (i = 0; i < info->attributes->len; i++)
322 _g_file_attribute_value_clear (&attrs[i].value);
323 g_array_free (info->attributes, TRUE);
325 if (info->mask != NO_ATTRIBUTE_MASK)
326 g_file_attribute_matcher_unref (info->mask);
328 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
332 g_file_info_class_init (GFileInfoClass *klass)
334 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
336 gobject_class->finalize = g_file_info_finalize;
340 g_file_info_init (GFileInfo *info)
342 info->mask = NO_ATTRIBUTE_MASK;
343 info->attributes = g_array_new (FALSE, FALSE,
344 sizeof (GFileAttribute));
350 * Creates a new file info structure.
352 * Returns: a #GFileInfo.
355 g_file_info_new (void)
357 return g_object_new (G_TYPE_FILE_INFO, NULL);
361 * g_file_info_copy_into:
362 * @src_info: source to copy attributes from.
363 * @dest_info: destination to copy attributes to.
365 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
368 g_file_info_copy_into (GFileInfo *src_info,
369 GFileInfo *dest_info)
371 GFileAttribute *source, *dest;
374 g_return_if_fail (G_IS_FILE_INFO (src_info));
375 g_return_if_fail (G_IS_FILE_INFO (dest_info));
377 dest = (GFileAttribute *)dest_info->attributes->data;
378 for (i = 0; i < dest_info->attributes->len; i++)
379 _g_file_attribute_value_clear (&dest[i].value);
381 g_array_set_size (dest_info->attributes,
382 src_info->attributes->len);
384 source = (GFileAttribute *)src_info->attributes->data;
385 dest = (GFileAttribute *)dest_info->attributes->data;
387 for (i = 0; i < src_info->attributes->len; i++)
389 dest[i].attribute = source[i].attribute;
390 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
391 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
394 if (dest_info->mask != NO_ATTRIBUTE_MASK)
395 g_file_attribute_matcher_unref (dest_info->mask);
397 if (src_info->mask == NO_ATTRIBUTE_MASK)
398 dest_info->mask = NO_ATTRIBUTE_MASK;
400 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
405 * @other: a #GFileInfo.
407 * Duplicates a file info structure.
409 * Returns: a duplicate #GFileInfo of @other.
412 g_file_info_dup (GFileInfo *other)
416 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
418 new = g_file_info_new ();
419 g_file_info_copy_into (other, new);
424 * g_file_info_set_attribute_mask:
425 * @info: a #GFileInfo.
426 * @mask: a #GFileAttributeMatcher.
428 * Sets @mask on @info to match specific attribute types.
431 g_file_info_set_attribute_mask (GFileInfo *info,
432 GFileAttributeMatcher *mask)
434 GFileAttribute *attr;
437 g_return_if_fail (G_IS_FILE_INFO (info));
439 if (mask != info->mask)
441 if (info->mask != NO_ATTRIBUTE_MASK)
442 g_file_attribute_matcher_unref (info->mask);
443 info->mask = g_file_attribute_matcher_ref (mask);
445 /* Remove non-matching attributes */
446 for (i = 0; i < info->attributes->len; i++)
448 attr = &g_array_index (info->attributes, GFileAttribute, i);
449 if (!_g_file_attribute_matcher_matches_id (mask,
452 _g_file_attribute_value_clear (&attr->value);
453 g_array_remove_index (info->attributes, i);
461 * g_file_info_unset_attribute_mask:
464 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
468 g_file_info_unset_attribute_mask (GFileInfo *info)
470 g_return_if_fail (G_IS_FILE_INFO (info));
472 if (info->mask != NO_ATTRIBUTE_MASK)
473 g_file_attribute_matcher_unref (info->mask);
474 info->mask = NO_ATTRIBUTE_MASK;
478 * g_file_info_clear_status:
479 * @info: a #GFileInfo.
481 * Clears the status information from @info.
484 g_file_info_clear_status (GFileInfo *info)
486 GFileAttribute *attrs;
489 g_return_if_fail (G_IS_FILE_INFO (info));
491 attrs = (GFileAttribute *)info->attributes->data;
492 for (i = 0; i < info->attributes->len; i++)
493 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
497 g_file_info_find_place (GFileInfo *info,
501 GFileAttribute *attrs;
502 /* Binary search for the place where attribute would be, if it's
506 max = info->attributes->len;
508 attrs = (GFileAttribute *)info->attributes->data;
512 med = min + (max - min) / 2;
513 if (attrs[med].attribute == attribute)
518 else if (attrs[med].attribute < attribute)
520 else /* attrs[med].attribute > attribute */
527 static GFileAttributeValue *
528 g_file_info_find_value (GFileInfo *info,
531 GFileAttribute *attrs;
534 i = g_file_info_find_place (info, attr_id);
535 attrs = (GFileAttribute *)info->attributes->data;
536 if (i < info->attributes->len &&
537 attrs[i].attribute == attr_id)
538 return &attrs[i].value;
543 static GFileAttributeValue *
544 g_file_info_find_value_by_name (GFileInfo *info,
545 const char *attribute)
549 attr_id = lookup_attribute (attribute);
550 return g_file_info_find_value (info, attr_id);
554 * g_file_info_has_attribute:
555 * @info: a #GFileInfo.
556 * @attribute: a file attribute key.
558 * Checks if a file info structure has an attribute named @attribute.
560 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
564 g_file_info_has_attribute (GFileInfo *info,
565 const char *attribute)
567 GFileAttributeValue *value;
569 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
570 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
572 value = g_file_info_find_value_by_name (info, attribute);
573 return value != NULL;
577 * g_file_info_has_namespace:
578 * @info: a #GFileInfo.
579 * @name_space: a file attribute namespace.
581 * Checks if a file info structure has an attribute in the
582 * specified @name_space.
584 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
590 g_file_info_has_namespace (GFileInfo *info,
591 const char *name_space)
593 GFileAttribute *attrs;
597 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
598 g_return_val_if_fail (name_space != NULL, FALSE);
600 ns_id = lookup_namespace (name_space);
602 attrs = (GFileAttribute *)info->attributes->data;
603 for (i = 0; i < info->attributes->len; i++)
605 if (GET_NS (attrs[i].attribute) == ns_id)
613 * g_file_info_list_attributes:
614 * @info: a #GFileInfo.
615 * @name_space: a file attribute key's namespace.
617 * Lists the file info structure's attributes.
619 * Returns: a null-terminated array of strings of all of the
620 * possible attribute types for the given @name_space, or
624 g_file_info_list_attributes (GFileInfo *info,
625 const char *name_space)
628 GFileAttribute *attrs;
630 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
633 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
635 names = g_ptr_array_new ();
636 attrs = (GFileAttribute *)info->attributes->data;
637 for (i = 0; i < info->attributes->len; i++)
639 attribute = attrs[i].attribute;
640 if (ns_id == 0 || GET_NS (attribute) == ns_id)
641 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
645 g_ptr_array_add (names, NULL);
647 return (char **)g_ptr_array_free (names, FALSE);
651 * g_file_info_get_attribute_type:
652 * @info: a #GFileInfo.
653 * @attribute: a file attribute key.
655 * Gets the attribute type for an attribute key.
657 * Returns: a #GFileAttributeType for the given @attribute, or
658 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
661 g_file_info_get_attribute_type (GFileInfo *info,
662 const char *attribute)
664 GFileAttributeValue *value;
666 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
667 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
669 value = g_file_info_find_value_by_name (info, attribute);
673 return G_FILE_ATTRIBUTE_TYPE_INVALID;
677 * g_file_info_remove_attribute:
678 * @info: a #GFileInfo.
679 * @attribute: a file attribute key.
681 * Removes all cases of @attribute from @info if it exists.
684 g_file_info_remove_attribute (GFileInfo *info,
685 const char *attribute)
688 GFileAttribute *attrs;
691 g_return_if_fail (G_IS_FILE_INFO (info));
692 g_return_if_fail (attribute != NULL && *attribute != '\0');
694 attr_id = lookup_attribute (attribute);
696 i = g_file_info_find_place (info, attr_id);
697 attrs = (GFileAttribute *)info->attributes->data;
698 if (i < info->attributes->len &&
699 attrs[i].attribute == attr_id)
701 _g_file_attribute_value_clear (&attrs[i].value);
702 g_array_remove_index (info->attributes, i);
707 * g_file_info_get_attribute_data:
708 * @info: a #GFileInfo
709 * @attribute: a file attribute key
710 * @type: return location for the attribute type, or %NULL
711 * @value_pp: return location for the attribute value, or %NULL
712 * @status: return location for the attribute status, or %NULL
714 * Gets the attribute type, value and status for an attribute key.
716 * Returns: %TRUE if @info has an attribute named @attribute,
720 g_file_info_get_attribute_data (GFileInfo *info,
721 const char *attribute,
722 GFileAttributeType *type,
724 GFileAttributeStatus *status)
726 GFileAttributeValue *value;
728 value = g_file_info_find_value_by_name (info, attribute);
733 *status = value->status;
739 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
745 * g_file_info_get_attribute_status:
746 * @info: a #GFileInfo
747 * @attribute: a file attribute key
749 * Gets the attribute status for an attribute key.
751 * Returns: a #GFileAttributeStatus for the given @attribute, or
752 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
756 g_file_info_get_attribute_status (GFileInfo *info,
757 const char *attribute)
759 GFileAttributeValue *val;
761 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
762 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
764 val = g_file_info_find_value_by_name (info, attribute);
768 return G_FILE_ATTRIBUTE_STATUS_UNSET;
772 * g_file_info_set_attribute_status:
773 * @info: a #GFileInfo
774 * @attribute: a file attribute key
775 * @status: a #GFileAttributeStatus
777 * Sets the attribute status for an attribute key. This is only
778 * needed by external code that implement g_file_set_attributes_from_info()
779 * or similar functions.
781 * The attribute must exist in @info for this to work. Otherwise %FALSE
782 * is returned and @info is unchanged.
784 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
789 g_file_info_set_attribute_status (GFileInfo *info,
790 const char *attribute,
791 GFileAttributeStatus status)
793 GFileAttributeValue *val;
795 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
796 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
798 val = g_file_info_find_value_by_name (info, attribute);
801 val->status = status;
808 GFileAttributeValue *
809 _g_file_info_get_attribute_value (GFileInfo *info,
810 const char *attribute)
813 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
814 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
816 return g_file_info_find_value_by_name (info, attribute);
820 * g_file_info_get_attribute_as_string:
821 * @info: a #GFileInfo.
822 * @attribute: a file attribute key.
824 * Gets the value of a attribute, formated as a string.
825 * This escapes things as needed to make the string valid
828 * Returns: a UTF-8 string associated with the given @attribute.
829 * When you're done with the string it must be freed with g_free().
832 g_file_info_get_attribute_as_string (GFileInfo *info,
833 const char *attribute)
835 GFileAttributeValue *val;
836 val = _g_file_info_get_attribute_value (info, attribute);
838 return _g_file_attribute_value_as_string (val);
844 * g_file_info_get_attribute_object:
845 * @info: a #GFileInfo.
846 * @attribute: a file attribute key.
848 * Gets the value of a #GObject attribute. If the attribute does
849 * not contain a #GObject, %NULL will be returned.
851 * Returns: a #GObject associated with the given @attribute, or
855 g_file_info_get_attribute_object (GFileInfo *info,
856 const char *attribute)
858 GFileAttributeValue *value;
860 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
861 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
863 value = g_file_info_find_value_by_name (info, attribute);
864 return _g_file_attribute_value_get_object (value);
868 * g_file_info_get_attribute_string:
869 * @info: a #GFileInfo.
870 * @attribute: a file attribute key.
872 * Gets the value of a string attribute. If the attribute does
873 * not contain a string, %NULL will be returned.
875 * Returns: the contents of the @attribute value as a string, or
879 g_file_info_get_attribute_string (GFileInfo *info,
880 const char *attribute)
882 GFileAttributeValue *value;
884 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
885 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
887 value = g_file_info_find_value_by_name (info, attribute);
888 return _g_file_attribute_value_get_string (value);
892 * g_file_info_get_attribute_byte_string:
893 * @info: a #GFileInfo.
894 * @attribute: a file attribute key.
896 * Gets the value of a byte string attribute. If the attribute does
897 * not contain a byte string, %NULL will be returned.
899 * Returns: the contents of the @attribute value as a byte string, or
903 g_file_info_get_attribute_byte_string (GFileInfo *info,
904 const char *attribute)
906 GFileAttributeValue *value;
908 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
909 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
911 value = g_file_info_find_value_by_name (info, attribute);
912 return _g_file_attribute_value_get_byte_string (value);
916 * g_file_info_get_attribute_stringv:
917 * @info: a #GFileInfo.
918 * @attribute: a file attribute key.
920 * Gets the value of a stringv attribute. If the attribute does
921 * not contain a stringv, %NULL will be returned.
923 * Returns: the contents of the @attribute value as a stringv, or
924 * %NULL otherwise. Do not free.
929 g_file_info_get_attribute_stringv (GFileInfo *info,
930 const char *attribute)
932 GFileAttributeValue *value;
934 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
935 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
937 value = g_file_info_find_value_by_name (info, attribute);
938 return _g_file_attribute_value_get_stringv (value);
942 * g_file_info_get_attribute_boolean:
943 * @info: a #GFileInfo.
944 * @attribute: a file attribute key.
946 * Gets the value of a boolean attribute. If the attribute does not
947 * contain a boolean value, %FALSE will be returned.
949 * Returns: the boolean value contained within the attribute.
952 g_file_info_get_attribute_boolean (GFileInfo *info,
953 const char *attribute)
955 GFileAttributeValue *value;
957 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
958 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
960 value = g_file_info_find_value_by_name (info, attribute);
961 return _g_file_attribute_value_get_boolean (value);
965 * g_file_info_get_attribute_uint32:
966 * @info: a #GFileInfo.
967 * @attribute: a file attribute key.
969 * Gets an unsigned 32-bit integer contained within the attribute. If the
970 * attribute does not contain an unsigned 32-bit integer, or is invalid,
971 * 0 will be returned.
973 * Returns: an unsigned 32-bit integer from the attribute.
976 g_file_info_get_attribute_uint32 (GFileInfo *info,
977 const char *attribute)
979 GFileAttributeValue *value;
981 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
982 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
984 value = g_file_info_find_value_by_name (info, attribute);
985 return _g_file_attribute_value_get_uint32 (value);
989 * g_file_info_get_attribute_int32:
990 * @info: a #GFileInfo.
991 * @attribute: a file attribute key.
993 * Gets a signed 32-bit integer contained within the attribute. If the
994 * attribute does not contain a signed 32-bit integer, or is invalid,
995 * 0 will be returned.
997 * Returns: a signed 32-bit integer from the attribute.
1000 g_file_info_get_attribute_int32 (GFileInfo *info,
1001 const char *attribute)
1003 GFileAttributeValue *value;
1005 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1006 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1008 value = g_file_info_find_value_by_name (info, attribute);
1009 return _g_file_attribute_value_get_int32 (value);
1013 * g_file_info_get_attribute_uint64:
1014 * @info: a #GFileInfo.
1015 * @attribute: a file attribute key.
1017 * Gets a unsigned 64-bit integer contained within the attribute. If the
1018 * attribute does not contain an unsigned 64-bit integer, or is invalid,
1019 * 0 will be returned.
1021 * Returns: a unsigned 64-bit integer from the attribute.
1024 g_file_info_get_attribute_uint64 (GFileInfo *info,
1025 const char *attribute)
1027 GFileAttributeValue *value;
1029 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1030 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1032 value = g_file_info_find_value_by_name (info, attribute);
1033 return _g_file_attribute_value_get_uint64 (value);
1037 * g_file_info_get_attribute_int64:
1038 * @info: a #GFileInfo.
1039 * @attribute: a file attribute key.
1041 * Gets a signed 64-bit integer contained within the attribute. If the
1042 * attribute does not contain an signed 64-bit integer, or is invalid,
1043 * 0 will be returned.
1045 * Returns: a signed 64-bit integer from the attribute.
1048 g_file_info_get_attribute_int64 (GFileInfo *info,
1049 const char *attribute)
1051 GFileAttributeValue *value;
1053 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1054 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1056 value = g_file_info_find_value_by_name (info, attribute);
1057 return _g_file_attribute_value_get_int64 (value);
1060 static GFileAttributeValue *
1061 g_file_info_create_value (GFileInfo *info,
1064 GFileAttribute *attrs;
1067 if (info->mask != NO_ATTRIBUTE_MASK &&
1068 !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
1071 i = g_file_info_find_place (info, attr_id);
1073 attrs = (GFileAttribute *)info->attributes->data;
1074 if (i < info->attributes->len &&
1075 attrs[i].attribute == attr_id)
1076 return &attrs[i].value;
1079 GFileAttribute attr = { 0 };
1080 attr.attribute = attr_id;
1081 g_array_insert_val (info->attributes, i, attr);
1083 attrs = (GFileAttribute *)info->attributes->data;
1084 return &attrs[i].value;
1089 _g_file_info_set_attribute_by_id (GFileInfo *info,
1091 GFileAttributeType type,
1094 GFileAttributeValue *value;
1096 value = g_file_info_create_value (info, attribute);
1099 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1103 * g_file_info_set_attribute:
1104 * @info: a #GFileInfo.
1105 * @attribute: a file attribute key.
1106 * @type: a #GFileAttributeType
1107 * @value_p: pointer to the value
1109 * Sets the @attribute to contain the given value, if possible.
1112 g_file_info_set_attribute (GFileInfo *info,
1113 const char *attribute,
1114 GFileAttributeType type,
1117 g_return_if_fail (G_IS_FILE_INFO (info));
1118 g_return_if_fail (attribute != NULL && *attribute != '\0');
1120 _g_file_info_set_attribute_by_id (info, lookup_attribute (attribute), type, value_p);
1124 _g_file_info_set_attribute_object_by_id (GFileInfo *info,
1126 GObject *attr_value)
1128 GFileAttributeValue *value;
1130 value = g_file_info_create_value (info, attribute);
1132 _g_file_attribute_value_set_object (value, attr_value);
1136 * g_file_info_set_attribute_object:
1137 * @info: a #GFileInfo.
1138 * @attribute: a file attribute key.
1139 * @attr_value: a #GObject.
1141 * Sets the @attribute to contain the given @attr_value,
1145 g_file_info_set_attribute_object (GFileInfo *info,
1146 const char *attribute,
1147 GObject *attr_value)
1149 g_return_if_fail (G_IS_FILE_INFO (info));
1150 g_return_if_fail (attribute != NULL && *attribute != '\0');
1151 g_return_if_fail (G_IS_OBJECT (attr_value));
1153 _g_file_info_set_attribute_object_by_id (info,
1154 lookup_attribute (attribute),
1159 _g_file_info_set_attribute_stringv_by_id (GFileInfo *info,
1163 GFileAttributeValue *value;
1165 value = g_file_info_create_value (info, attribute);
1167 _g_file_attribute_value_set_stringv (value, attr_value);
1171 * g_file_info_set_attribute_stringv:
1172 * @info: a #GFileInfo.
1173 * @attribute: a file attribute key.
1174 * @attr_value: a %NULL terminated string array
1176 * Sets the @attribute to contain the given @attr_value,
1182 g_file_info_set_attribute_stringv (GFileInfo *info,
1183 const char *attribute,
1186 g_return_if_fail (G_IS_FILE_INFO (info));
1187 g_return_if_fail (attribute != NULL && *attribute != '\0');
1188 g_return_if_fail (attr_value != NULL);
1190 _g_file_info_set_attribute_stringv_by_id (info,
1191 lookup_attribute (attribute),
1196 _g_file_info_set_attribute_string_by_id (GFileInfo *info,
1198 const char *attr_value)
1200 GFileAttributeValue *value;
1202 value = g_file_info_create_value (info, attribute);
1204 _g_file_attribute_value_set_string (value, attr_value);
1208 * g_file_info_set_attribute_string:
1209 * @info: a #GFileInfo.
1210 * @attribute: a file attribute key.
1211 * @attr_value: a string.
1213 * Sets the @attribute to contain the given @attr_value,
1217 g_file_info_set_attribute_string (GFileInfo *info,
1218 const char *attribute,
1219 const char *attr_value)
1221 g_return_if_fail (G_IS_FILE_INFO (info));
1222 g_return_if_fail (attribute != NULL && *attribute != '\0');
1223 g_return_if_fail (attr_value != NULL);
1225 _g_file_info_set_attribute_string_by_id (info,
1226 lookup_attribute (attribute),
1231 _g_file_info_set_attribute_byte_string_by_id (GFileInfo *info,
1233 const char *attr_value)
1235 GFileAttributeValue *value;
1237 value = g_file_info_create_value (info, attribute);
1239 _g_file_attribute_value_set_byte_string (value, attr_value);
1243 * g_file_info_set_attribute_byte_string:
1244 * @info: a #GFileInfo.
1245 * @attribute: a file attribute key.
1246 * @attr_value: a byte string.
1248 * Sets the @attribute to contain the given @attr_value,
1252 g_file_info_set_attribute_byte_string (GFileInfo *info,
1253 const char *attribute,
1254 const char *attr_value)
1256 g_return_if_fail (G_IS_FILE_INFO (info));
1257 g_return_if_fail (attribute != NULL && *attribute != '\0');
1258 g_return_if_fail (attr_value != NULL);
1260 _g_file_info_set_attribute_byte_string_by_id (info,
1261 lookup_attribute (attribute),
1266 _g_file_info_set_attribute_boolean_by_id (GFileInfo *info,
1268 gboolean attr_value)
1270 GFileAttributeValue *value;
1272 value = g_file_info_create_value (info, attribute);
1274 _g_file_attribute_value_set_boolean (value, attr_value);
1278 * g_file_info_set_attribute_boolean:
1279 * @info: a #GFileInfo.
1280 * @attribute: a file attribute key.
1281 * @attr_value: a boolean value.
1283 * Sets the @attribute to contain the given @attr_value,
1287 g_file_info_set_attribute_boolean (GFileInfo *info,
1288 const char *attribute,
1289 gboolean attr_value)
1291 g_return_if_fail (G_IS_FILE_INFO (info));
1292 g_return_if_fail (attribute != NULL && *attribute != '\0');
1294 _g_file_info_set_attribute_boolean_by_id (info,
1295 lookup_attribute (attribute),
1300 _g_file_info_set_attribute_uint32_by_id (GFileInfo *info,
1304 GFileAttributeValue *value;
1306 value = g_file_info_create_value (info, attribute);
1308 _g_file_attribute_value_set_uint32 (value, attr_value);
1312 * g_file_info_set_attribute_uint32:
1313 * @info: a #GFileInfo.
1314 * @attribute: a file attribute key.
1315 * @attr_value: an unsigned 32-bit integer.
1317 * Sets the @attribute to contain the given @attr_value,
1321 g_file_info_set_attribute_uint32 (GFileInfo *info,
1322 const char *attribute,
1325 g_return_if_fail (G_IS_FILE_INFO (info));
1326 g_return_if_fail (attribute != NULL && *attribute != '\0');
1328 _g_file_info_set_attribute_uint32_by_id (info,
1329 lookup_attribute (attribute),
1334 _g_file_info_set_attribute_int32_by_id (GFileInfo *info,
1338 GFileAttributeValue *value;
1340 value = g_file_info_create_value (info, attribute);
1342 _g_file_attribute_value_set_int32 (value, attr_value);
1346 * g_file_info_set_attribute_int32:
1347 * @info: a #GFileInfo.
1348 * @attribute: a file attribute key.
1349 * @attr_value: a signed 32-bit integer
1351 * Sets the @attribute to contain the given @attr_value,
1355 g_file_info_set_attribute_int32 (GFileInfo *info,
1356 const char *attribute,
1359 g_return_if_fail (G_IS_FILE_INFO (info));
1360 g_return_if_fail (attribute != NULL && *attribute != '\0');
1362 _g_file_info_set_attribute_int32_by_id (info,
1363 lookup_attribute (attribute),
1368 _g_file_info_set_attribute_uint64_by_id (GFileInfo *info,
1372 GFileAttributeValue *value;
1374 value = g_file_info_create_value (info, attribute);
1376 _g_file_attribute_value_set_uint64 (value, attr_value);
1380 * g_file_info_set_attribute_uint64:
1381 * @info: a #GFileInfo.
1382 * @attribute: a file attribute key.
1383 * @attr_value: an unsigned 64-bit integer.
1385 * Sets the @attribute to contain the given @attr_value,
1389 g_file_info_set_attribute_uint64 (GFileInfo *info,
1390 const char *attribute,
1393 g_return_if_fail (G_IS_FILE_INFO (info));
1394 g_return_if_fail (attribute != NULL && *attribute != '\0');
1396 _g_file_info_set_attribute_uint64_by_id (info,
1397 lookup_attribute (attribute),
1402 _g_file_info_set_attribute_int64_by_id (GFileInfo *info,
1406 GFileAttributeValue *value;
1408 value = g_file_info_create_value (info, attribute);
1410 _g_file_attribute_value_set_int64 (value, attr_value);
1414 * g_file_info_set_attribute_int64:
1415 * @info: a #GFileInfo.
1416 * @attribute: attribute name to set.
1417 * @attr_value: int64 value to set attribute to.
1419 * Sets the @attribute to contain the given @attr_value,
1424 g_file_info_set_attribute_int64 (GFileInfo *info,
1425 const char *attribute,
1428 g_return_if_fail (G_IS_FILE_INFO (info));
1429 g_return_if_fail (attribute != NULL && *attribute != '\0');
1431 _g_file_info_set_attribute_int64_by_id (info,
1432 lookup_attribute (attribute),
1436 /* Helper getters */
1438 * g_file_info_get_file_type:
1439 * @info: a #GFileInfo.
1441 * Gets a file's type (whether it is a regular file, symlink, etc).
1442 * This is different from the file's content type, see g_file_info_get_content_type().
1444 * Returns: a #GFileType for the given file.
1447 g_file_info_get_file_type (GFileInfo *info)
1449 static guint32 attr = 0;
1450 GFileAttributeValue *value;
1452 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1455 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1457 value = g_file_info_find_value (info, attr);
1458 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1462 * g_file_info_get_is_hidden:
1463 * @info: a #GFileInfo.
1465 * Checks if a file is hidden.
1467 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1470 g_file_info_get_is_hidden (GFileInfo *info)
1472 static guint32 attr = 0;
1473 GFileAttributeValue *value;
1475 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1478 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1480 value = g_file_info_find_value (info, attr);
1481 return (GFileType)_g_file_attribute_value_get_boolean (value);
1485 * g_file_info_get_is_backup:
1486 * @info: a #GFileInfo.
1488 * Checks if a file is a backup file.
1490 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1493 g_file_info_get_is_backup (GFileInfo *info)
1495 static guint32 attr = 0;
1496 GFileAttributeValue *value;
1498 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1501 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1503 value = g_file_info_find_value (info, attr);
1504 return (GFileType)_g_file_attribute_value_get_boolean (value);
1508 * g_file_info_get_is_symlink:
1509 * @info: a #GFileInfo.
1511 * Checks if a file is a symlink.
1513 * Returns: %TRUE if the given @info is a symlink.
1516 g_file_info_get_is_symlink (GFileInfo *info)
1518 static guint32 attr = 0;
1519 GFileAttributeValue *value;
1521 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1524 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1526 value = g_file_info_find_value (info, attr);
1527 return (GFileType)_g_file_attribute_value_get_boolean (value);
1531 * g_file_info_get_name:
1532 * @info: a #GFileInfo.
1534 * Gets the name for a file.
1536 * Returns: a string containing the file name.
1539 g_file_info_get_name (GFileInfo *info)
1541 static guint32 attr = 0;
1542 GFileAttributeValue *value;
1544 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1547 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1549 value = g_file_info_find_value (info, attr);
1550 return _g_file_attribute_value_get_byte_string (value);
1554 * g_file_info_get_display_name:
1555 * @info: a #GFileInfo.
1557 * Gets a display name for a file.
1559 * Returns: a string containing the display name.
1562 g_file_info_get_display_name (GFileInfo *info)
1564 static guint32 attr = 0;
1565 GFileAttributeValue *value;
1567 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1570 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1572 value = g_file_info_find_value (info, attr);
1573 return _g_file_attribute_value_get_string (value);
1577 * g_file_info_get_edit_name:
1578 * @info: a #GFileInfo.
1580 * Gets the edit name for a file.
1582 * Returns: a string containing the edit name.
1585 g_file_info_get_edit_name (GFileInfo *info)
1587 static guint32 attr = 0;
1588 GFileAttributeValue *value;
1590 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1593 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1595 value = g_file_info_find_value (info, attr);
1596 return _g_file_attribute_value_get_string (value);
1600 * g_file_info_get_icon:
1601 * @info: a #GFileInfo.
1603 * Gets the icon for a file.
1605 * Returns: #GIcon for the given @info.
1608 g_file_info_get_icon (GFileInfo *info)
1610 static guint32 attr = 0;
1611 GFileAttributeValue *value;
1614 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1617 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1619 value = g_file_info_find_value (info, attr);
1620 obj = _g_file_attribute_value_get_object (value);
1621 if (G_IS_ICON (obj))
1622 return G_ICON (obj);
1627 * g_file_info_get_content_type:
1628 * @info: a #GFileInfo.
1630 * Gets the file's content type.
1632 * Returns: a string containing the file's content type.
1635 g_file_info_get_content_type (GFileInfo *info)
1637 static guint32 attr = 0;
1638 GFileAttributeValue *value;
1640 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1643 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1645 value = g_file_info_find_value (info, attr);
1646 return _g_file_attribute_value_get_string (value);
1650 * g_file_info_get_size:
1651 * @info: a #GFileInfo.
1653 * Gets the file's size.
1655 * Returns: a #goffset containing the file's size.
1658 g_file_info_get_size (GFileInfo *info)
1660 static guint32 attr = 0;
1661 GFileAttributeValue *value;
1663 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1666 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1668 value = g_file_info_find_value (info, attr);
1669 return (goffset) _g_file_attribute_value_get_uint64 (value);
1673 * g_file_info_get_modification_time:
1674 * @info: a #GFileInfo.
1675 * @result: a #GTimeVal.
1677 * Gets the modification time of the current @info and sets it
1681 g_file_info_get_modification_time (GFileInfo *info,
1684 static guint32 attr_mtime = 0, attr_mtime_usec;
1685 GFileAttributeValue *value;
1687 g_return_if_fail (G_IS_FILE_INFO (info));
1688 g_return_if_fail (result != NULL);
1690 if (attr_mtime == 0)
1692 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1693 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1696 value = g_file_info_find_value (info, attr_mtime);
1697 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1698 value = g_file_info_find_value (info, attr_mtime_usec);
1699 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1703 * g_file_info_get_symlink_target:
1704 * @info: a #GFileInfo.
1706 * Gets the symlink target for a given #GFileInfo.
1708 * Returns: a string containing the symlink target.
1711 g_file_info_get_symlink_target (GFileInfo *info)
1713 static guint32 attr = 0;
1714 GFileAttributeValue *value;
1716 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1719 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1721 value = g_file_info_find_value (info, attr);
1722 return _g_file_attribute_value_get_byte_string (value);
1726 * g_file_info_get_etag:
1727 * @info: a #GFileInfo.
1729 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1730 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1732 * Returns: a string containing the value of the "etag:value" attribute.
1735 g_file_info_get_etag (GFileInfo *info)
1737 static guint32 attr = 0;
1738 GFileAttributeValue *value;
1740 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1743 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1745 value = g_file_info_find_value (info, attr);
1746 return _g_file_attribute_value_get_string (value);
1750 * g_file_info_get_sort_order:
1751 * @info: a #GFileInfo.
1753 * Gets the value of the sort_order attribute from the #GFileInfo.
1754 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1756 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1759 g_file_info_get_sort_order (GFileInfo *info)
1761 static guint32 attr = 0;
1762 GFileAttributeValue *value;
1764 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1767 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1769 value = g_file_info_find_value (info, attr);
1770 return _g_file_attribute_value_get_int32 (value);
1773 /* Helper setters: */
1775 * g_file_info_set_file_type:
1776 * @info: a #GFileInfo.
1777 * @type: a #GFileType.
1779 * Sets the file type in a #GFileInfo to @type.
1780 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1783 g_file_info_set_file_type (GFileInfo *info,
1786 static guint32 attr = 0;
1787 GFileAttributeValue *value;
1789 g_return_if_fail (G_IS_FILE_INFO (info));
1792 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1794 value = g_file_info_create_value (info, attr);
1796 _g_file_attribute_value_set_uint32 (value, type);
1800 * g_file_info_set_is_hidden:
1801 * @info: a #GFileInfo.
1802 * @is_hidden: a #gboolean.
1804 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1805 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1808 g_file_info_set_is_hidden (GFileInfo *info,
1811 static guint32 attr = 0;
1812 GFileAttributeValue *value;
1814 g_return_if_fail (G_IS_FILE_INFO (info));
1817 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1819 value = g_file_info_create_value (info, attr);
1821 _g_file_attribute_value_set_boolean (value, is_hidden);
1825 * g_file_info_set_is_symlink:
1826 * @info: a #GFileInfo.
1827 * @is_symlink: a #gboolean.
1829 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1830 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1833 g_file_info_set_is_symlink (GFileInfo *info,
1834 gboolean is_symlink)
1836 static guint32 attr = 0;
1837 GFileAttributeValue *value;
1839 g_return_if_fail (G_IS_FILE_INFO (info));
1842 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1844 value = g_file_info_create_value (info, attr);
1846 _g_file_attribute_value_set_boolean (value, is_symlink);
1850 * g_file_info_set_name:
1851 * @info: a #GFileInfo.
1852 * @name: a string containing a name.
1854 * Sets the name attribute for the current #GFileInfo.
1855 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1858 g_file_info_set_name (GFileInfo *info,
1861 static guint32 attr = 0;
1862 GFileAttributeValue *value;
1864 g_return_if_fail (G_IS_FILE_INFO (info));
1865 g_return_if_fail (name != NULL);
1868 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1870 value = g_file_info_create_value (info, attr);
1872 _g_file_attribute_value_set_byte_string (value, name);
1876 * g_file_info_set_display_name:
1877 * @info: a #GFileInfo.
1878 * @display_name: a string containing a display name.
1880 * Sets the display name for the current #GFileInfo.
1881 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1884 g_file_info_set_display_name (GFileInfo *info,
1885 const char *display_name)
1887 static guint32 attr = 0;
1888 GFileAttributeValue *value;
1890 g_return_if_fail (G_IS_FILE_INFO (info));
1891 g_return_if_fail (display_name != NULL);
1894 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1896 value = g_file_info_create_value (info, attr);
1898 _g_file_attribute_value_set_string (value, display_name);
1902 * g_file_info_set_edit_name:
1903 * @info: a #GFileInfo.
1904 * @edit_name: a string containing an edit name.
1906 * Sets the edit name for the current file.
1907 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1910 g_file_info_set_edit_name (GFileInfo *info,
1911 const char *edit_name)
1913 static guint32 attr = 0;
1914 GFileAttributeValue *value;
1916 g_return_if_fail (G_IS_FILE_INFO (info));
1917 g_return_if_fail (edit_name != NULL);
1920 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1922 value = g_file_info_create_value (info, attr);
1924 _g_file_attribute_value_set_string (value, edit_name);
1928 * g_file_info_set_icon:
1929 * @info: a #GFileInfo.
1932 * Sets the icon for a given #GFileInfo.
1933 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1936 g_file_info_set_icon (GFileInfo *info,
1939 static guint32 attr = 0;
1940 GFileAttributeValue *value;
1942 g_return_if_fail (G_IS_FILE_INFO (info));
1943 g_return_if_fail (G_IS_ICON (icon));
1946 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1948 value = g_file_info_create_value (info, attr);
1950 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1954 * g_file_info_set_content_type:
1955 * @info: a #GFileInfo.
1956 * @content_type: a content type. See #GContentType.
1958 * Sets the content type attribute for a given #GFileInfo.
1959 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1962 g_file_info_set_content_type (GFileInfo *info,
1963 const char *content_type)
1965 static guint32 attr = 0;
1966 GFileAttributeValue *value;
1968 g_return_if_fail (G_IS_FILE_INFO (info));
1969 g_return_if_fail (content_type != NULL);
1972 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1974 value = g_file_info_create_value (info, attr);
1976 _g_file_attribute_value_set_string (value, content_type);
1980 * g_file_info_set_size:
1981 * @info: a #GFileInfo.
1982 * @size: a #goffset containing the file's size.
1984 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1985 * to the given size.
1988 g_file_info_set_size (GFileInfo *info,
1991 static guint32 attr = 0;
1992 GFileAttributeValue *value;
1994 g_return_if_fail (G_IS_FILE_INFO (info));
1997 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1999 value = g_file_info_create_value (info, attr);
2001 _g_file_attribute_value_set_uint64 (value, size);
2005 * g_file_info_set_modification_time
2006 * @info: a #GFileInfo.
2007 * @mtime: a #GTimeVal.
2009 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
2010 * info to the given time value.
2013 g_file_info_set_modification_time (GFileInfo *info,
2016 static guint32 attr_mtime = 0, attr_mtime_usec;
2017 GFileAttributeValue *value;
2019 g_return_if_fail (G_IS_FILE_INFO (info));
2020 g_return_if_fail (mtime != NULL);
2022 if (attr_mtime == 0)
2024 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
2025 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2028 value = g_file_info_create_value (info, attr_mtime);
2030 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
2031 value = g_file_info_create_value (info, attr_mtime_usec);
2033 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
2037 * g_file_info_set_symlink_target:
2038 * @info: a #GFileInfo.
2039 * @symlink_target: a static string containing a path to a symlink target.
2041 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
2042 * to the given symlink target.
2045 g_file_info_set_symlink_target (GFileInfo *info,
2046 const char *symlink_target)
2048 static guint32 attr = 0;
2049 GFileAttributeValue *value;
2051 g_return_if_fail (G_IS_FILE_INFO (info));
2052 g_return_if_fail (symlink_target != NULL);
2055 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2057 value = g_file_info_create_value (info, attr);
2059 _g_file_attribute_value_set_byte_string (value, symlink_target);
2063 * g_file_info_set_sort_order:
2064 * @info: a #GFileInfo.
2065 * @sort_order: a sort order integer.
2067 * Sets the sort order attribute in the file info structure. See
2068 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
2071 g_file_info_set_sort_order (GFileInfo *info,
2074 static guint32 attr = 0;
2075 GFileAttributeValue *value;
2077 g_return_if_fail (G_IS_FILE_INFO (info));
2080 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
2082 value = g_file_info_create_value (info, attr);
2084 _g_file_attribute_value_set_int32 (value, sort_order);
2088 #define ON_STACK_MATCHERS 5
2095 struct _GFileAttributeMatcher {
2097 SubMatcher sub_matchers[ON_STACK_MATCHERS];
2098 GArray *more_sub_matchers;
2101 guint32 iterator_ns;
2107 matcher_add (GFileAttributeMatcher *matcher,
2111 SubMatcher *sub_matchers;
2115 for (i = 0; i < ON_STACK_MATCHERS; i++)
2117 /* First empty spot, not found, use this */
2118 if (matcher->sub_matchers[i].id == 0)
2120 matcher->sub_matchers[i].id = id;
2121 matcher->sub_matchers[i].mask = mask;
2126 if (matcher->sub_matchers[i].id == id &&
2127 matcher->sub_matchers[i].mask == mask)
2131 if (matcher->more_sub_matchers == NULL)
2132 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2134 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2135 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2138 if (sub_matchers[i].id == id &&
2139 sub_matchers[i].mask == mask)
2146 g_array_append_val (matcher->more_sub_matchers, s);
2150 * g_file_attribute_matcher_new:
2151 * @attributes: an attribute string to match.
2153 * Creates a new file attribute matcher, which matches attributes
2154 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
2155 * counted structures, and are created with a reference count of 1. If
2156 * the number of references falls to 0, the #GFileAttributeMatcher is
2157 * automatically destroyed.
2159 * The @attribute string should be formatted with specific keys separated
2160 * from namespaces with a double colon. Several "namespace::key" strings may be
2161 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
2162 * The wildcard "*" may be used to match all keys and namespaces, or
2163 * "namespace::*" will match all keys in a given namespace.
2165 * Examples of strings to use:
2167 * <title>File Attribute Matcher strings and results</title>
2168 * <tgroup cols='2' align='left'><thead>
2169 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
2171 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
2172 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
2173 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
2174 * all keys in the unix namespace.</entry></row>
2178 * Returns: a #GFileAttributeMatcher.
2180 GFileAttributeMatcher *
2181 g_file_attribute_matcher_new (const char *attributes)
2186 GFileAttributeMatcher *matcher;
2188 if (attributes == NULL || *attributes == '\0')
2191 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2194 split = g_strsplit (attributes, ",", -1);
2196 for (i = 0; split[i] != NULL; i++)
2198 if (strcmp (split[i], "*") == 0)
2199 matcher->all = TRUE;
2204 colon = strstr (split[i], "::");
2205 if (colon != NULL &&
2210 id = lookup_attribute (split[i]);
2218 id = lookup_namespace (split[i]) << NS_POS;
2219 mask = NS_MASK << NS_POS;
2222 matcher_add (matcher, id, mask);
2232 * g_file_attribute_matcher_ref:
2233 * @matcher: a #GFileAttributeMatcher.
2235 * References a file attribute matcher.
2237 * Returns: a #GFileAttributeMatcher.
2239 GFileAttributeMatcher *
2240 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2244 g_return_val_if_fail (matcher->ref > 0, NULL);
2245 g_atomic_int_inc (&matcher->ref);
2251 * g_file_attribute_matcher_unref:
2252 * @matcher: a #GFileAttributeMatcher.
2254 * Unreferences @matcher. If the reference count falls below 1,
2255 * the @matcher is automatically freed.
2259 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2263 g_return_if_fail (matcher->ref > 0);
2265 if (g_atomic_int_dec_and_test (&matcher->ref))
2267 if (matcher->more_sub_matchers)
2268 g_array_free (matcher->more_sub_matchers, TRUE);
2276 * g_file_attribute_matcher_matches_only:
2277 * @matcher: a #GFileAttributeMatcher.
2278 * @attribute: a file attribute key.
2280 * Checks if a attribute matcher only matches a given attribute. Always
2281 * returns %FALSE if "*" was used when creating the matcher.
2283 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2286 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2287 const char *attribute)
2291 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2293 if (matcher == NULL ||
2297 id = lookup_attribute (attribute);
2299 if (matcher->sub_matchers[0].id != 0 &&
2300 matcher->sub_matchers[1].id == 0 &&
2301 matcher->sub_matchers[0].mask == 0xffffffff &&
2302 matcher->sub_matchers[0].id == id)
2309 matcher_matches_id (GFileAttributeMatcher *matcher,
2312 SubMatcher *sub_matchers;
2315 for (i = 0; i < ON_STACK_MATCHERS; i++)
2317 if (matcher->sub_matchers[i].id == 0)
2320 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2324 if (matcher->more_sub_matchers)
2326 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2327 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2329 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2338 _g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2341 /* We return a NULL matcher for an empty match string, so handle this */
2342 if (matcher == NULL)
2348 return matcher_matches_id (matcher, id);
2352 * g_file_attribute_matcher_matches:
2353 * @matcher: a #GFileAttributeMatcher.
2354 * @attribute: a file attribute key.
2356 * Checks if an attribute will be matched by an attribute matcher. If
2357 * the matcher was created with the "*" matching string, this function
2358 * will always return %TRUE.
2360 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2363 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2364 const char *attribute)
2366 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2368 /* We return a NULL matcher for an empty match string, so handle this */
2369 if (matcher == NULL)
2375 return matcher_matches_id (matcher, lookup_attribute (attribute));
2378 /* return TRUE -> all */
2380 * g_file_attribute_matcher_enumerate_namespace:
2381 * @matcher: a #GFileAttributeMatcher.
2382 * @ns: a string containing a file attribute namespace.
2384 * Checks if the matcher will match all of the keys in a given namespace.
2385 * This will always return %TRUE if a wildcard character is in use (e.g. if
2386 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2387 * using "*" and namespace is anything.)
2389 * TODO: this is awkwardly worded.
2391 * Returns: %TRUE if the matcher matches all of the entries
2392 * in the given @ns, %FALSE otherwise.
2395 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2398 SubMatcher *sub_matchers;
2402 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2404 /* We return a NULL matcher for an empty match string, so handle this */
2405 if (matcher == NULL)
2411 ns_id = lookup_namespace (ns) << NS_POS;
2413 for (i = 0; i < ON_STACK_MATCHERS; i++)
2415 if (matcher->sub_matchers[i].id == ns_id)
2419 if (matcher->more_sub_matchers)
2421 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2422 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2424 if (sub_matchers[i].id == ns_id)
2429 matcher->iterator_ns = ns_id;
2430 matcher->iterator_pos = 0;
2436 * g_file_attribute_matcher_enumerate_next:
2437 * @matcher: a #GFileAttributeMatcher.
2439 * Gets the next matched attribute from a #GFileAttributeMatcher.
2441 * Returns: a string containing the next attribute or %NULL if
2442 * no more attribute exist.
2445 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2448 SubMatcher *sub_matcher;
2450 /* We return a NULL matcher for an empty match string, so handle this */
2451 if (matcher == NULL)
2456 i = matcher->iterator_pos++;
2458 if (i < ON_STACK_MATCHERS)
2460 if (matcher->sub_matchers[i].id == 0)
2463 sub_matcher = &matcher->sub_matchers[i];
2467 if (matcher->more_sub_matchers == NULL)
2470 i -= ON_STACK_MATCHERS;
2471 if (i < matcher->more_sub_matchers->len)
2472 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2477 if (sub_matcher->mask == 0xffffffff &&
2478 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2479 return get_attribute_for_id (sub_matcher->id);
2483 #define __G_FILE_INFO_C__
2484 #include "gioaliasdef.c"