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.
927 g_file_info_get_attribute_stringv (GFileInfo *info,
928 const char *attribute)
930 GFileAttributeValue *value;
932 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
933 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
935 value = g_file_info_find_value_by_name (info, attribute);
936 return _g_file_attribute_value_get_stringv (value);
940 * g_file_info_get_attribute_boolean:
941 * @info: a #GFileInfo.
942 * @attribute: a file attribute key.
944 * Gets the value of a boolean attribute. If the attribute does not
945 * contain a boolean value, %FALSE will be returned.
947 * Returns: the boolean value contained within the attribute.
950 g_file_info_get_attribute_boolean (GFileInfo *info,
951 const char *attribute)
953 GFileAttributeValue *value;
955 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
956 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
958 value = g_file_info_find_value_by_name (info, attribute);
959 return _g_file_attribute_value_get_boolean (value);
963 * g_file_info_get_attribute_uint32:
964 * @info: a #GFileInfo.
965 * @attribute: a file attribute key.
967 * Gets an unsigned 32-bit integer contained within the attribute. If the
968 * attribute does not contain an unsigned 32-bit integer, or is invalid,
969 * 0 will be returned.
971 * Returns: an unsigned 32-bit integer from the attribute.
974 g_file_info_get_attribute_uint32 (GFileInfo *info,
975 const char *attribute)
977 GFileAttributeValue *value;
979 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
980 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
982 value = g_file_info_find_value_by_name (info, attribute);
983 return _g_file_attribute_value_get_uint32 (value);
987 * g_file_info_get_attribute_int32:
988 * @info: a #GFileInfo.
989 * @attribute: a file attribute key.
991 * Gets a signed 32-bit integer contained within the attribute. If the
992 * attribute does not contain a signed 32-bit integer, or is invalid,
993 * 0 will be returned.
995 * Returns: a signed 32-bit integer from the attribute.
998 g_file_info_get_attribute_int32 (GFileInfo *info,
999 const char *attribute)
1001 GFileAttributeValue *value;
1003 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1004 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1006 value = g_file_info_find_value_by_name (info, attribute);
1007 return _g_file_attribute_value_get_int32 (value);
1011 * g_file_info_get_attribute_uint64:
1012 * @info: a #GFileInfo.
1013 * @attribute: a file attribute key.
1015 * Gets a unsigned 64-bit integer contained within the attribute. If the
1016 * attribute does not contain an unsigned 64-bit integer, or is invalid,
1017 * 0 will be returned.
1019 * Returns: a unsigned 64-bit integer from the attribute.
1022 g_file_info_get_attribute_uint64 (GFileInfo *info,
1023 const char *attribute)
1025 GFileAttributeValue *value;
1027 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1028 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1030 value = g_file_info_find_value_by_name (info, attribute);
1031 return _g_file_attribute_value_get_uint64 (value);
1035 * g_file_info_get_attribute_int64:
1036 * @info: a #GFileInfo.
1037 * @attribute: a file attribute key.
1039 * Gets a signed 64-bit integer contained within the attribute. If the
1040 * attribute does not contain an signed 64-bit integer, or is invalid,
1041 * 0 will be returned.
1043 * Returns: a signed 64-bit integer from the attribute.
1046 g_file_info_get_attribute_int64 (GFileInfo *info,
1047 const char *attribute)
1049 GFileAttributeValue *value;
1051 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1052 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1054 value = g_file_info_find_value_by_name (info, attribute);
1055 return _g_file_attribute_value_get_int64 (value);
1058 static GFileAttributeValue *
1059 g_file_info_create_value (GFileInfo *info,
1062 GFileAttribute *attrs;
1065 if (info->mask != NO_ATTRIBUTE_MASK &&
1066 !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
1069 i = g_file_info_find_place (info, attr_id);
1071 attrs = (GFileAttribute *)info->attributes->data;
1072 if (i < info->attributes->len &&
1073 attrs[i].attribute == attr_id)
1074 return &attrs[i].value;
1077 GFileAttribute attr = { 0 };
1078 attr.attribute = attr_id;
1079 g_array_insert_val (info->attributes, i, attr);
1081 attrs = (GFileAttribute *)info->attributes->data;
1082 return &attrs[i].value;
1087 _g_file_info_set_attribute_by_id (GFileInfo *info,
1089 GFileAttributeType type,
1092 GFileAttributeValue *value;
1094 value = g_file_info_create_value (info, attribute);
1097 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1101 * g_file_info_set_attribute:
1102 * @info: a #GFileInfo.
1103 * @attribute: a file attribute key.
1104 * @type: a #GFileAttributeType
1105 * @value_p: pointer to the value
1107 * Sets the @attribute to contain the given value, if possible.
1110 g_file_info_set_attribute (GFileInfo *info,
1111 const char *attribute,
1112 GFileAttributeType type,
1115 g_return_if_fail (G_IS_FILE_INFO (info));
1116 g_return_if_fail (attribute != NULL && *attribute != '\0');
1118 _g_file_info_set_attribute_by_id (info, lookup_attribute (attribute), type, value_p);
1122 _g_file_info_set_attribute_object_by_id (GFileInfo *info,
1124 GObject *attr_value)
1126 GFileAttributeValue *value;
1128 value = g_file_info_create_value (info, attribute);
1130 _g_file_attribute_value_set_object (value, attr_value);
1134 * g_file_info_set_attribute_object:
1135 * @info: a #GFileInfo.
1136 * @attribute: a file attribute key.
1137 * @attr_value: a #GObject.
1139 * Sets the @attribute to contain the given @attr_value,
1143 g_file_info_set_attribute_object (GFileInfo *info,
1144 const char *attribute,
1145 GObject *attr_value)
1147 g_return_if_fail (G_IS_FILE_INFO (info));
1148 g_return_if_fail (attribute != NULL && *attribute != '\0');
1149 g_return_if_fail (G_IS_OBJECT (attr_value));
1151 _g_file_info_set_attribute_object_by_id (info,
1152 lookup_attribute (attribute),
1157 _g_file_info_set_attribute_stringv_by_id (GFileInfo *info,
1161 GFileAttributeValue *value;
1163 value = g_file_info_create_value (info, attribute);
1165 _g_file_attribute_value_set_stringv (value, attr_value);
1169 * g_file_info_set_attribute_stringv:
1170 * @info: a #GFileInfo.
1171 * @attribute: a file attribute key.
1172 * @attr_value: a %NULL terminated string array
1174 * Sets the @attribute to contain the given @attr_value,
1180 g_file_info_set_attribute_stringv (GFileInfo *info,
1181 const char *attribute,
1184 g_return_if_fail (G_IS_FILE_INFO (info));
1185 g_return_if_fail (attribute != NULL && *attribute != '\0');
1186 g_return_if_fail (attr_value != NULL);
1188 _g_file_info_set_attribute_stringv_by_id (info,
1189 lookup_attribute (attribute),
1194 _g_file_info_set_attribute_string_by_id (GFileInfo *info,
1196 const char *attr_value)
1198 GFileAttributeValue *value;
1200 value = g_file_info_create_value (info, attribute);
1202 _g_file_attribute_value_set_string (value, attr_value);
1206 * g_file_info_set_attribute_string:
1207 * @info: a #GFileInfo.
1208 * @attribute: a file attribute key.
1209 * @attr_value: a string.
1211 * Sets the @attribute to contain the given @attr_value,
1215 g_file_info_set_attribute_string (GFileInfo *info,
1216 const char *attribute,
1217 const char *attr_value)
1219 g_return_if_fail (G_IS_FILE_INFO (info));
1220 g_return_if_fail (attribute != NULL && *attribute != '\0');
1221 g_return_if_fail (attr_value != NULL);
1223 _g_file_info_set_attribute_string_by_id (info,
1224 lookup_attribute (attribute),
1229 _g_file_info_set_attribute_byte_string_by_id (GFileInfo *info,
1231 const char *attr_value)
1233 GFileAttributeValue *value;
1235 value = g_file_info_create_value (info, attribute);
1237 _g_file_attribute_value_set_byte_string (value, attr_value);
1241 * g_file_info_set_attribute_byte_string:
1242 * @info: a #GFileInfo.
1243 * @attribute: a file attribute key.
1244 * @attr_value: a byte string.
1246 * Sets the @attribute to contain the given @attr_value,
1250 g_file_info_set_attribute_byte_string (GFileInfo *info,
1251 const char *attribute,
1252 const char *attr_value)
1254 g_return_if_fail (G_IS_FILE_INFO (info));
1255 g_return_if_fail (attribute != NULL && *attribute != '\0');
1256 g_return_if_fail (attr_value != NULL);
1258 _g_file_info_set_attribute_byte_string_by_id (info,
1259 lookup_attribute (attribute),
1264 _g_file_info_set_attribute_boolean_by_id (GFileInfo *info,
1266 gboolean attr_value)
1268 GFileAttributeValue *value;
1270 value = g_file_info_create_value (info, attribute);
1272 _g_file_attribute_value_set_boolean (value, attr_value);
1276 * g_file_info_set_attribute_boolean:
1277 * @info: a #GFileInfo.
1278 * @attribute: a file attribute key.
1279 * @attr_value: a boolean value.
1281 * Sets the @attribute to contain the given @attr_value,
1285 g_file_info_set_attribute_boolean (GFileInfo *info,
1286 const char *attribute,
1287 gboolean attr_value)
1289 g_return_if_fail (G_IS_FILE_INFO (info));
1290 g_return_if_fail (attribute != NULL && *attribute != '\0');
1292 _g_file_info_set_attribute_boolean_by_id (info,
1293 lookup_attribute (attribute),
1298 _g_file_info_set_attribute_uint32_by_id (GFileInfo *info,
1302 GFileAttributeValue *value;
1304 value = g_file_info_create_value (info, attribute);
1306 _g_file_attribute_value_set_uint32 (value, attr_value);
1310 * g_file_info_set_attribute_uint32:
1311 * @info: a #GFileInfo.
1312 * @attribute: a file attribute key.
1313 * @attr_value: an unsigned 32-bit integer.
1315 * Sets the @attribute to contain the given @attr_value,
1319 g_file_info_set_attribute_uint32 (GFileInfo *info,
1320 const char *attribute,
1323 g_return_if_fail (G_IS_FILE_INFO (info));
1324 g_return_if_fail (attribute != NULL && *attribute != '\0');
1326 _g_file_info_set_attribute_uint32_by_id (info,
1327 lookup_attribute (attribute),
1332 _g_file_info_set_attribute_int32_by_id (GFileInfo *info,
1336 GFileAttributeValue *value;
1338 value = g_file_info_create_value (info, attribute);
1340 _g_file_attribute_value_set_int32 (value, attr_value);
1344 * g_file_info_set_attribute_int32:
1345 * @info: a #GFileInfo.
1346 * @attribute: a file attribute key.
1347 * @attr_value: a signed 32-bit integer
1349 * Sets the @attribute to contain the given @attr_value,
1353 g_file_info_set_attribute_int32 (GFileInfo *info,
1354 const char *attribute,
1357 g_return_if_fail (G_IS_FILE_INFO (info));
1358 g_return_if_fail (attribute != NULL && *attribute != '\0');
1360 _g_file_info_set_attribute_int32_by_id (info,
1361 lookup_attribute (attribute),
1366 _g_file_info_set_attribute_uint64_by_id (GFileInfo *info,
1370 GFileAttributeValue *value;
1372 value = g_file_info_create_value (info, attribute);
1374 _g_file_attribute_value_set_uint64 (value, attr_value);
1378 * g_file_info_set_attribute_uint64:
1379 * @info: a #GFileInfo.
1380 * @attribute: a file attribute key.
1381 * @attr_value: an unsigned 64-bit integer.
1383 * Sets the @attribute to contain the given @attr_value,
1387 g_file_info_set_attribute_uint64 (GFileInfo *info,
1388 const char *attribute,
1391 g_return_if_fail (G_IS_FILE_INFO (info));
1392 g_return_if_fail (attribute != NULL && *attribute != '\0');
1394 _g_file_info_set_attribute_uint64_by_id (info,
1395 lookup_attribute (attribute),
1400 _g_file_info_set_attribute_int64_by_id (GFileInfo *info,
1404 GFileAttributeValue *value;
1406 value = g_file_info_create_value (info, attribute);
1408 _g_file_attribute_value_set_int64 (value, attr_value);
1412 * g_file_info_set_attribute_int64:
1413 * @info: a #GFileInfo.
1414 * @attribute: attribute name to set.
1415 * @attr_value: int64 value to set attribute to.
1417 * Sets the @attribute to contain the given @attr_value,
1422 g_file_info_set_attribute_int64 (GFileInfo *info,
1423 const char *attribute,
1426 g_return_if_fail (G_IS_FILE_INFO (info));
1427 g_return_if_fail (attribute != NULL && *attribute != '\0');
1429 _g_file_info_set_attribute_int64_by_id (info,
1430 lookup_attribute (attribute),
1434 /* Helper getters */
1436 * g_file_info_get_file_type:
1437 * @info: a #GFileInfo.
1439 * Gets a file's type (whether it is a regular file, symlink, etc).
1440 * This is different from the file's content type, see g_file_info_get_content_type().
1442 * Returns: a #GFileType for the given file.
1445 g_file_info_get_file_type (GFileInfo *info)
1447 static guint32 attr = 0;
1448 GFileAttributeValue *value;
1450 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1453 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1455 value = g_file_info_find_value (info, attr);
1456 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1460 * g_file_info_get_is_hidden:
1461 * @info: a #GFileInfo.
1463 * Checks if a file is hidden.
1465 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1468 g_file_info_get_is_hidden (GFileInfo *info)
1470 static guint32 attr = 0;
1471 GFileAttributeValue *value;
1473 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1476 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1478 value = g_file_info_find_value (info, attr);
1479 return (GFileType)_g_file_attribute_value_get_boolean (value);
1483 * g_file_info_get_is_backup:
1484 * @info: a #GFileInfo.
1486 * Checks if a file is a backup file.
1488 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1491 g_file_info_get_is_backup (GFileInfo *info)
1493 static guint32 attr = 0;
1494 GFileAttributeValue *value;
1496 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1499 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1501 value = g_file_info_find_value (info, attr);
1502 return (GFileType)_g_file_attribute_value_get_boolean (value);
1506 * g_file_info_get_is_symlink:
1507 * @info: a #GFileInfo.
1509 * Checks if a file is a symlink.
1511 * Returns: %TRUE if the given @info is a symlink.
1514 g_file_info_get_is_symlink (GFileInfo *info)
1516 static guint32 attr = 0;
1517 GFileAttributeValue *value;
1519 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1522 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1524 value = g_file_info_find_value (info, attr);
1525 return (GFileType)_g_file_attribute_value_get_boolean (value);
1529 * g_file_info_get_name:
1530 * @info: a #GFileInfo.
1532 * Gets the name for a file.
1534 * Returns: a string containing the file name.
1537 g_file_info_get_name (GFileInfo *info)
1539 static guint32 attr = 0;
1540 GFileAttributeValue *value;
1542 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1545 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1547 value = g_file_info_find_value (info, attr);
1548 return _g_file_attribute_value_get_byte_string (value);
1552 * g_file_info_get_display_name:
1553 * @info: a #GFileInfo.
1555 * Gets a display name for a file.
1557 * Returns: a string containing the display name.
1560 g_file_info_get_display_name (GFileInfo *info)
1562 static guint32 attr = 0;
1563 GFileAttributeValue *value;
1565 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1568 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1570 value = g_file_info_find_value (info, attr);
1571 return _g_file_attribute_value_get_string (value);
1575 * g_file_info_get_edit_name:
1576 * @info: a #GFileInfo.
1578 * Gets the edit name for a file.
1580 * Returns: a string containing the edit name.
1583 g_file_info_get_edit_name (GFileInfo *info)
1585 static guint32 attr = 0;
1586 GFileAttributeValue *value;
1588 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1591 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1593 value = g_file_info_find_value (info, attr);
1594 return _g_file_attribute_value_get_string (value);
1598 * g_file_info_get_icon:
1599 * @info: a #GFileInfo.
1601 * Gets the icon for a file.
1603 * Returns: #GIcon for the given @info.
1606 g_file_info_get_icon (GFileInfo *info)
1608 static guint32 attr = 0;
1609 GFileAttributeValue *value;
1612 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1615 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1617 value = g_file_info_find_value (info, attr);
1618 obj = _g_file_attribute_value_get_object (value);
1619 if (G_IS_ICON (obj))
1620 return G_ICON (obj);
1625 * g_file_info_get_content_type:
1626 * @info: a #GFileInfo.
1628 * Gets the file's content type.
1630 * Returns: a string containing the file's content type.
1633 g_file_info_get_content_type (GFileInfo *info)
1635 static guint32 attr = 0;
1636 GFileAttributeValue *value;
1638 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1641 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1643 value = g_file_info_find_value (info, attr);
1644 return _g_file_attribute_value_get_string (value);
1648 * g_file_info_get_size:
1649 * @info: a #GFileInfo.
1651 * Gets the file's size.
1653 * Returns: a #goffset containing the file's size.
1656 g_file_info_get_size (GFileInfo *info)
1658 static guint32 attr = 0;
1659 GFileAttributeValue *value;
1661 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1664 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1666 value = g_file_info_find_value (info, attr);
1667 return (goffset) _g_file_attribute_value_get_uint64 (value);
1671 * g_file_info_get_modification_time:
1672 * @info: a #GFileInfo.
1673 * @result: a #GTimeVal.
1675 * Gets the modification time of the current @info and sets it
1679 g_file_info_get_modification_time (GFileInfo *info,
1682 static guint32 attr_mtime = 0, attr_mtime_usec;
1683 GFileAttributeValue *value;
1685 g_return_if_fail (G_IS_FILE_INFO (info));
1686 g_return_if_fail (result != NULL);
1688 if (attr_mtime == 0)
1690 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1691 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1694 value = g_file_info_find_value (info, attr_mtime);
1695 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1696 value = g_file_info_find_value (info, attr_mtime_usec);
1697 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1701 * g_file_info_get_symlink_target:
1702 * @info: a #GFileInfo.
1704 * Gets the symlink target for a given #GFileInfo.
1706 * Returns: a string containing the symlink target.
1709 g_file_info_get_symlink_target (GFileInfo *info)
1711 static guint32 attr = 0;
1712 GFileAttributeValue *value;
1714 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1717 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1719 value = g_file_info_find_value (info, attr);
1720 return _g_file_attribute_value_get_byte_string (value);
1724 * g_file_info_get_etag:
1725 * @info: a #GFileInfo.
1727 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1728 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1730 * Returns: a string containing the value of the "etag:value" attribute.
1733 g_file_info_get_etag (GFileInfo *info)
1735 static guint32 attr = 0;
1736 GFileAttributeValue *value;
1738 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1741 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1743 value = g_file_info_find_value (info, attr);
1744 return _g_file_attribute_value_get_string (value);
1748 * g_file_info_get_sort_order:
1749 * @info: a #GFileInfo.
1751 * Gets the value of the sort_order attribute from the #GFileInfo.
1752 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1754 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1757 g_file_info_get_sort_order (GFileInfo *info)
1759 static guint32 attr = 0;
1760 GFileAttributeValue *value;
1762 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1765 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1767 value = g_file_info_find_value (info, attr);
1768 return _g_file_attribute_value_get_int32 (value);
1771 /* Helper setters: */
1773 * g_file_info_set_file_type:
1774 * @info: a #GFileInfo.
1775 * @type: a #GFileType.
1777 * Sets the file type in a #GFileInfo to @type.
1778 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1781 g_file_info_set_file_type (GFileInfo *info,
1784 static guint32 attr = 0;
1785 GFileAttributeValue *value;
1787 g_return_if_fail (G_IS_FILE_INFO (info));
1790 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1792 value = g_file_info_create_value (info, attr);
1794 _g_file_attribute_value_set_uint32 (value, type);
1798 * g_file_info_set_is_hidden:
1799 * @info: a #GFileInfo.
1800 * @is_hidden: a #gboolean.
1802 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1803 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1806 g_file_info_set_is_hidden (GFileInfo *info,
1809 static guint32 attr = 0;
1810 GFileAttributeValue *value;
1812 g_return_if_fail (G_IS_FILE_INFO (info));
1815 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1817 value = g_file_info_create_value (info, attr);
1819 _g_file_attribute_value_set_boolean (value, is_hidden);
1823 * g_file_info_set_is_symlink:
1824 * @info: a #GFileInfo.
1825 * @is_symlink: a #gboolean.
1827 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1828 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1831 g_file_info_set_is_symlink (GFileInfo *info,
1832 gboolean is_symlink)
1834 static guint32 attr = 0;
1835 GFileAttributeValue *value;
1837 g_return_if_fail (G_IS_FILE_INFO (info));
1840 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1842 value = g_file_info_create_value (info, attr);
1844 _g_file_attribute_value_set_boolean (value, is_symlink);
1848 * g_file_info_set_name:
1849 * @info: a #GFileInfo.
1850 * @name: a string containing a name.
1852 * Sets the name attribute for the current #GFileInfo.
1853 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1856 g_file_info_set_name (GFileInfo *info,
1859 static guint32 attr = 0;
1860 GFileAttributeValue *value;
1862 g_return_if_fail (G_IS_FILE_INFO (info));
1863 g_return_if_fail (name != NULL);
1866 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1868 value = g_file_info_create_value (info, attr);
1870 _g_file_attribute_value_set_byte_string (value, name);
1874 * g_file_info_set_display_name:
1875 * @info: a #GFileInfo.
1876 * @display_name: a string containing a display name.
1878 * Sets the display name for the current #GFileInfo.
1879 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1882 g_file_info_set_display_name (GFileInfo *info,
1883 const char *display_name)
1885 static guint32 attr = 0;
1886 GFileAttributeValue *value;
1888 g_return_if_fail (G_IS_FILE_INFO (info));
1889 g_return_if_fail (display_name != NULL);
1892 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1894 value = g_file_info_create_value (info, attr);
1896 _g_file_attribute_value_set_string (value, display_name);
1900 * g_file_info_set_edit_name:
1901 * @info: a #GFileInfo.
1902 * @edit_name: a string containing an edit name.
1904 * Sets the edit name for the current file.
1905 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1908 g_file_info_set_edit_name (GFileInfo *info,
1909 const char *edit_name)
1911 static guint32 attr = 0;
1912 GFileAttributeValue *value;
1914 g_return_if_fail (G_IS_FILE_INFO (info));
1915 g_return_if_fail (edit_name != NULL);
1918 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1920 value = g_file_info_create_value (info, attr);
1922 _g_file_attribute_value_set_string (value, edit_name);
1926 * g_file_info_set_icon:
1927 * @info: a #GFileInfo.
1930 * Sets the icon for a given #GFileInfo.
1931 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1934 g_file_info_set_icon (GFileInfo *info,
1937 static guint32 attr = 0;
1938 GFileAttributeValue *value;
1940 g_return_if_fail (G_IS_FILE_INFO (info));
1941 g_return_if_fail (G_IS_ICON (icon));
1944 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1946 value = g_file_info_create_value (info, attr);
1948 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1952 * g_file_info_set_content_type:
1953 * @info: a #GFileInfo.
1954 * @content_type: a content type. See #GContentType.
1956 * Sets the content type attribute for a given #GFileInfo.
1957 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1960 g_file_info_set_content_type (GFileInfo *info,
1961 const char *content_type)
1963 static guint32 attr = 0;
1964 GFileAttributeValue *value;
1966 g_return_if_fail (G_IS_FILE_INFO (info));
1967 g_return_if_fail (content_type != NULL);
1970 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1972 value = g_file_info_create_value (info, attr);
1974 _g_file_attribute_value_set_string (value, content_type);
1978 * g_file_info_set_size:
1979 * @info: a #GFileInfo.
1980 * @size: a #goffset containing the file's size.
1982 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1983 * to the given size.
1986 g_file_info_set_size (GFileInfo *info,
1989 static guint32 attr = 0;
1990 GFileAttributeValue *value;
1992 g_return_if_fail (G_IS_FILE_INFO (info));
1995 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1997 value = g_file_info_create_value (info, attr);
1999 _g_file_attribute_value_set_uint64 (value, size);
2003 * g_file_info_set_modification_time
2004 * @info: a #GFileInfo.
2005 * @mtime: a #GTimeVal.
2007 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
2008 * info to the given time value.
2011 g_file_info_set_modification_time (GFileInfo *info,
2014 static guint32 attr_mtime = 0, attr_mtime_usec;
2015 GFileAttributeValue *value;
2017 g_return_if_fail (G_IS_FILE_INFO (info));
2018 g_return_if_fail (mtime != NULL);
2020 if (attr_mtime == 0)
2022 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
2023 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2026 value = g_file_info_create_value (info, attr_mtime);
2028 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
2029 value = g_file_info_create_value (info, attr_mtime_usec);
2031 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
2035 * g_file_info_set_symlink_target:
2036 * @info: a #GFileInfo.
2037 * @symlink_target: a static string containing a path to a symlink target.
2039 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
2040 * to the given symlink target.
2043 g_file_info_set_symlink_target (GFileInfo *info,
2044 const char *symlink_target)
2046 static guint32 attr = 0;
2047 GFileAttributeValue *value;
2049 g_return_if_fail (G_IS_FILE_INFO (info));
2050 g_return_if_fail (symlink_target != NULL);
2053 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2055 value = g_file_info_create_value (info, attr);
2057 _g_file_attribute_value_set_byte_string (value, symlink_target);
2061 * g_file_info_set_sort_order:
2062 * @info: a #GFileInfo.
2063 * @sort_order: a sort order integer.
2065 * Sets the sort order attribute in the file info structure. See
2066 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
2069 g_file_info_set_sort_order (GFileInfo *info,
2072 static guint32 attr = 0;
2073 GFileAttributeValue *value;
2075 g_return_if_fail (G_IS_FILE_INFO (info));
2078 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
2080 value = g_file_info_create_value (info, attr);
2082 _g_file_attribute_value_set_int32 (value, sort_order);
2086 #define ON_STACK_MATCHERS 5
2093 struct _GFileAttributeMatcher {
2095 SubMatcher sub_matchers[ON_STACK_MATCHERS];
2096 GArray *more_sub_matchers;
2099 guint32 iterator_ns;
2105 matcher_add (GFileAttributeMatcher *matcher,
2109 SubMatcher *sub_matchers;
2113 for (i = 0; i < ON_STACK_MATCHERS; i++)
2115 /* First empty spot, not found, use this */
2116 if (matcher->sub_matchers[i].id == 0)
2118 matcher->sub_matchers[i].id = id;
2119 matcher->sub_matchers[i].mask = mask;
2124 if (matcher->sub_matchers[i].id == id &&
2125 matcher->sub_matchers[i].mask == mask)
2129 if (matcher->more_sub_matchers == NULL)
2130 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2132 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2133 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2136 if (sub_matchers[i].id == id &&
2137 sub_matchers[i].mask == mask)
2144 g_array_append_val (matcher->more_sub_matchers, s);
2148 * g_file_attribute_matcher_new:
2149 * @attributes: an attribute string to match.
2151 * Creates a new file attribute matcher, which matches attributes
2152 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
2153 * counted structures, and are created with a reference count of 1. If
2154 * the number of references falls to 0, the #GFileAttributeMatcher is
2155 * automatically destroyed.
2157 * The @attribute string should be formatted with specific keys separated
2158 * from namespaces with a double colon. Several "namespace::key" strings may be
2159 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
2160 * The wildcard "*" may be used to match all keys and namespaces, or
2161 * "namespace::*" will match all keys in a given namespace.
2163 * Examples of strings to use:
2165 * <title>File Attribute Matcher strings and results</title>
2166 * <tgroup cols='2' align='left'><thead>
2167 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
2169 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
2170 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
2171 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
2172 * all keys in the unix namespace.</entry></row>
2176 * Returns: a #GFileAttributeMatcher.
2178 GFileAttributeMatcher *
2179 g_file_attribute_matcher_new (const char *attributes)
2184 GFileAttributeMatcher *matcher;
2186 if (attributes == NULL || *attributes == '\0')
2189 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2192 split = g_strsplit (attributes, ",", -1);
2194 for (i = 0; split[i] != NULL; i++)
2196 if (strcmp (split[i], "*") == 0)
2197 matcher->all = TRUE;
2202 colon = strstr (split[i], "::");
2203 if (colon != NULL &&
2208 id = lookup_attribute (split[i]);
2216 id = lookup_namespace (split[i]) << NS_POS;
2217 mask = NS_MASK << NS_POS;
2220 matcher_add (matcher, id, mask);
2230 * g_file_attribute_matcher_ref:
2231 * @matcher: a #GFileAttributeMatcher.
2233 * References a file attribute matcher.
2235 * Returns: a #GFileAttributeMatcher.
2237 GFileAttributeMatcher *
2238 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2242 g_return_val_if_fail (matcher->ref > 0, NULL);
2243 g_atomic_int_inc (&matcher->ref);
2249 * g_file_attribute_matcher_unref:
2250 * @matcher: a #GFileAttributeMatcher.
2252 * Unreferences @matcher. If the reference count falls below 1,
2253 * the @matcher is automatically freed.
2257 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2261 g_return_if_fail (matcher->ref > 0);
2263 if (g_atomic_int_dec_and_test (&matcher->ref))
2265 if (matcher->more_sub_matchers)
2266 g_array_free (matcher->more_sub_matchers, TRUE);
2274 * g_file_attribute_matcher_matches_only:
2275 * @matcher: a #GFileAttributeMatcher.
2276 * @attribute: a file attribute key.
2278 * Checks if a attribute matcher only matches a given attribute. Always
2279 * returns %FALSE if "*" was used when creating the matcher.
2281 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2284 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2285 const char *attribute)
2289 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2291 if (matcher == NULL ||
2295 id = lookup_attribute (attribute);
2297 if (matcher->sub_matchers[0].id != 0 &&
2298 matcher->sub_matchers[1].id == 0 &&
2299 matcher->sub_matchers[0].mask == 0xffffffff &&
2300 matcher->sub_matchers[0].id == id)
2307 matcher_matches_id (GFileAttributeMatcher *matcher,
2310 SubMatcher *sub_matchers;
2313 for (i = 0; i < ON_STACK_MATCHERS; i++)
2315 if (matcher->sub_matchers[i].id == 0)
2318 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2322 if (matcher->more_sub_matchers)
2324 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2325 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2327 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2336 _g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2339 /* We return a NULL matcher for an empty match string, so handle this */
2340 if (matcher == NULL)
2346 return matcher_matches_id (matcher, id);
2350 * g_file_attribute_matcher_matches:
2351 * @matcher: a #GFileAttributeMatcher.
2352 * @attribute: a file attribute key.
2354 * Checks if an attribute will be matched by an attribute matcher. If
2355 * the matcher was created with the "*" matching string, this function
2356 * will always return %TRUE.
2358 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2361 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2362 const char *attribute)
2364 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2366 /* We return a NULL matcher for an empty match string, so handle this */
2367 if (matcher == NULL)
2373 return matcher_matches_id (matcher, lookup_attribute (attribute));
2376 /* return TRUE -> all */
2378 * g_file_attribute_matcher_enumerate_namespace:
2379 * @matcher: a #GFileAttributeMatcher.
2380 * @ns: a string containing a file attribute namespace.
2382 * Checks if the matcher will match all of the keys in a given namespace.
2383 * This will always return %TRUE if a wildcard character is in use (e.g. if
2384 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2385 * using "*" and namespace is anything.)
2387 * TODO: this is awkwardly worded.
2389 * Returns: %TRUE if the matcher matches all of the entries
2390 * in the given @ns, %FALSE otherwise.
2393 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2396 SubMatcher *sub_matchers;
2400 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2402 /* We return a NULL matcher for an empty match string, so handle this */
2403 if (matcher == NULL)
2409 ns_id = lookup_namespace (ns) << NS_POS;
2411 for (i = 0; i < ON_STACK_MATCHERS; i++)
2413 if (matcher->sub_matchers[i].id == ns_id)
2417 if (matcher->more_sub_matchers)
2419 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2420 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2422 if (sub_matchers[i].id == ns_id)
2427 matcher->iterator_ns = ns_id;
2428 matcher->iterator_pos = 0;
2434 * g_file_attribute_matcher_enumerate_next:
2435 * @matcher: a #GFileAttributeMatcher.
2437 * Gets the next matched attribute from a #GFileAttributeMatcher.
2439 * Returns: a string containing the next attribute or %NULL if
2440 * no more attribute exist.
2443 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2446 SubMatcher *sub_matcher;
2448 /* We return a NULL matcher for an empty match string, so handle this */
2449 if (matcher == NULL)
2454 i = matcher->iterator_pos++;
2456 if (i < ON_STACK_MATCHERS)
2458 if (matcher->sub_matchers[i].id == 0)
2461 sub_matcher = &matcher->sub_matchers[i];
2465 if (matcher->more_sub_matchers == NULL)
2468 i -= ON_STACK_MATCHERS;
2469 if (i < matcher->more_sub_matchers->len)
2470 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2475 if (sub_matcher->mask == 0xffffffff &&
2476 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2477 return get_attribute_for_id (sub_matcher->id);
2481 #define __G_FILE_INFO_C__
2482 #include "gioaliasdef.c"