GIcon: add g_icon_[de]serialize()
[glib.git] / gio / gfileinfo.c
blob30bed5c44c548cf2826f26885cd43b8921dfef2a
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>
23 /**
24 * SECTION:gfileinfo
25 * @short_description: File Information and Attributes
26 * @include: gio/gio.h
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
52 * attributes.
53 **/
55 #include "config.h"
57 #include <string.h>
59 #include "gfileinfo.h"
60 #include "gfileinfo-priv.h"
61 #include "gfileattribute-priv.h"
62 #include "gicon.h"
63 #include "glibintl.h"
66 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
67 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
69 typedef struct {
70 guint32 attribute;
71 GFileAttributeValue value;
72 } GFileAttribute;
74 struct _GFileInfo
76 GObject parent_instance;
78 GArray *attributes;
79 GFileAttributeMatcher *mask;
82 struct _GFileInfoClass
84 GObjectClass parent_class;
88 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
90 typedef struct {
91 guint32 id;
92 guint32 attribute_id_counter;
93 } NSInfo;
95 G_LOCK_DEFINE_STATIC (attribute_hash);
96 static int namespace_id_counter = 0;
97 static GHashTable *ns_hash = NULL;
98 static GHashTable *attribute_hash = NULL;
99 static char ***attributes = NULL;
101 /* Attribute ids are 32bit, we split it up like this:
102 * |------------|--------------------|
103 * 12 bit 20 bit
104 * namespace attribute id
106 * This way the attributes gets sorted in namespace order
109 #define NS_POS 20
110 #define NS_MASK ((guint32)((1<<12) - 1))
111 #define ID_POS 0
112 #define ID_MASK ((guint32)((1<<20) - 1))
114 #define GET_NS(_attr_id) \
115 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
116 #define GET_ID(_attr_id) \
117 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
119 #define MAKE_ATTR_ID(_ns, _id) \
120 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
121 ((((guint32) _id) & ID_MASK) << ID_POS) )
123 static NSInfo *
124 _lookup_namespace (const char *namespace)
126 NSInfo *ns_info;
128 ns_info = g_hash_table_lookup (ns_hash, namespace);
129 if (ns_info == NULL)
131 ns_info = g_new0 (NSInfo, 1);
132 ns_info->id = ++namespace_id_counter;
133 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
134 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
135 attributes[ns_info->id] = g_new (char *, 1);
136 attributes[ns_info->id][0] = g_strconcat (namespace, "::*", NULL);
138 return ns_info;
141 static guint32
142 _lookup_attribute (const char *attribute)
144 guint32 attr_id, id;
145 char *ns;
146 const char *colon;
147 NSInfo *ns_info;
149 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
151 if (attr_id != 0)
152 return attr_id;
154 colon = strstr (attribute, "::");
155 if (colon)
156 ns = g_strndup (attribute, colon - attribute);
157 else
158 ns = g_strdup ("");
160 ns_info = _lookup_namespace (ns);
161 g_free (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));
171 return attr_id;
174 static void
175 ensure_attribute_hash (void)
177 if (attribute_hash != NULL)
178 return;
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); \
187 }G_STMT_END
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 (STANDARD_SYMBOLIC_ICON);
208 REGISTER_ATTRIBUTE (ETAG_VALUE);
209 REGISTER_ATTRIBUTE (ID_FILE);
210 REGISTER_ATTRIBUTE (ID_FILESYSTEM);
211 REGISTER_ATTRIBUTE (ACCESS_CAN_READ);
212 REGISTER_ATTRIBUTE (ACCESS_CAN_WRITE);
213 REGISTER_ATTRIBUTE (ACCESS_CAN_EXECUTE);
214 REGISTER_ATTRIBUTE (ACCESS_CAN_DELETE);
215 REGISTER_ATTRIBUTE (ACCESS_CAN_TRASH);
216 REGISTER_ATTRIBUTE (ACCESS_CAN_RENAME);
217 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_MOUNT);
218 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_UNMOUNT);
219 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_EJECT);
220 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE);
221 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE_FILE);
222 REGISTER_ATTRIBUTE (MOUNTABLE_HAL_UDI);
223 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START);
224 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START_DEGRADED);
225 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_STOP);
226 REGISTER_ATTRIBUTE (MOUNTABLE_START_STOP_TYPE);
227 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_POLL);
228 REGISTER_ATTRIBUTE (MOUNTABLE_IS_MEDIA_CHECK_AUTOMATIC);
229 REGISTER_ATTRIBUTE (TIME_MODIFIED);
230 REGISTER_ATTRIBUTE (TIME_MODIFIED_USEC);
231 REGISTER_ATTRIBUTE (TIME_ACCESS);
232 REGISTER_ATTRIBUTE (TIME_ACCESS_USEC);
233 REGISTER_ATTRIBUTE (TIME_CHANGED);
234 REGISTER_ATTRIBUTE (TIME_CHANGED_USEC);
235 REGISTER_ATTRIBUTE (TIME_CREATED);
236 REGISTER_ATTRIBUTE (TIME_CREATED_USEC);
237 REGISTER_ATTRIBUTE (UNIX_DEVICE);
238 REGISTER_ATTRIBUTE (UNIX_INODE);
239 REGISTER_ATTRIBUTE (UNIX_MODE);
240 REGISTER_ATTRIBUTE (UNIX_NLINK);
241 REGISTER_ATTRIBUTE (UNIX_UID);
242 REGISTER_ATTRIBUTE (UNIX_GID);
243 REGISTER_ATTRIBUTE (UNIX_RDEV);
244 REGISTER_ATTRIBUTE (UNIX_BLOCK_SIZE);
245 REGISTER_ATTRIBUTE (UNIX_BLOCKS);
246 REGISTER_ATTRIBUTE (UNIX_IS_MOUNTPOINT);
247 REGISTER_ATTRIBUTE (DOS_IS_ARCHIVE);
248 REGISTER_ATTRIBUTE (DOS_IS_SYSTEM);
249 REGISTER_ATTRIBUTE (OWNER_USER);
250 REGISTER_ATTRIBUTE (OWNER_USER_REAL);
251 REGISTER_ATTRIBUTE (OWNER_GROUP);
252 REGISTER_ATTRIBUTE (THUMBNAIL_PATH);
253 REGISTER_ATTRIBUTE (THUMBNAILING_FAILED);
254 REGISTER_ATTRIBUTE (PREVIEW_ICON);
255 REGISTER_ATTRIBUTE (FILESYSTEM_SIZE);
256 REGISTER_ATTRIBUTE (FILESYSTEM_FREE);
257 REGISTER_ATTRIBUTE (FILESYSTEM_TYPE);
258 REGISTER_ATTRIBUTE (FILESYSTEM_READONLY);
259 REGISTER_ATTRIBUTE (FILESYSTEM_USE_PREVIEW);
260 REGISTER_ATTRIBUTE (GVFS_BACKEND);
261 REGISTER_ATTRIBUTE (SELINUX_CONTEXT);
262 REGISTER_ATTRIBUTE (TRASH_ITEM_COUNT);
263 REGISTER_ATTRIBUTE (TRASH_ORIG_PATH);
264 REGISTER_ATTRIBUTE (TRASH_DELETION_DATE);
266 #undef REGISTER_ATTRIBUTE
269 static guint32
270 lookup_namespace (const char *namespace)
272 NSInfo *ns_info;
273 guint32 id;
275 G_LOCK (attribute_hash);
277 ensure_attribute_hash ();
279 ns_info = _lookup_namespace (namespace);
280 id = 0;
281 if (ns_info)
282 id = ns_info->id;
284 G_UNLOCK (attribute_hash);
286 return id;
289 static char *
290 get_attribute_for_id (int attribute)
292 char *s;
293 G_LOCK (attribute_hash);
294 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
295 G_UNLOCK (attribute_hash);
296 return s;
299 static guint32
300 lookup_attribute (const char *attribute)
302 guint32 attr_id;
304 G_LOCK (attribute_hash);
305 ensure_attribute_hash ();
307 attr_id = _lookup_attribute (attribute);
309 G_UNLOCK (attribute_hash);
311 return attr_id;
314 static void
315 g_file_info_finalize (GObject *object)
317 GFileInfo *info;
318 int i;
319 GFileAttribute *attrs;
321 info = G_FILE_INFO (object);
323 attrs = (GFileAttribute *)info->attributes->data;
324 for (i = 0; i < info->attributes->len; i++)
325 _g_file_attribute_value_clear (&attrs[i].value);
326 g_array_free (info->attributes, TRUE);
328 if (info->mask != NO_ATTRIBUTE_MASK)
329 g_file_attribute_matcher_unref (info->mask);
331 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
334 static void
335 g_file_info_class_init (GFileInfoClass *klass)
337 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
339 gobject_class->finalize = g_file_info_finalize;
342 static void
343 g_file_info_init (GFileInfo *info)
345 info->mask = NO_ATTRIBUTE_MASK;
346 info->attributes = g_array_new (FALSE, FALSE,
347 sizeof (GFileAttribute));
351 * g_file_info_new:
353 * Creates a new file info structure.
355 * Returns: a #GFileInfo.
357 GFileInfo *
358 g_file_info_new (void)
360 return g_object_new (G_TYPE_FILE_INFO, NULL);
364 * g_file_info_copy_into:
365 * @src_info: source to copy attributes from.
366 * @dest_info: destination to copy attributes to.
368 * Copies all of the <link linkend="gio-GFileAttribute">GFileAttribute</link>s
369 * from @src_info to @dest_info.
371 void
372 g_file_info_copy_into (GFileInfo *src_info,
373 GFileInfo *dest_info)
375 GFileAttribute *source, *dest;
376 int i;
378 g_return_if_fail (G_IS_FILE_INFO (src_info));
379 g_return_if_fail (G_IS_FILE_INFO (dest_info));
381 dest = (GFileAttribute *)dest_info->attributes->data;
382 for (i = 0; i < dest_info->attributes->len; i++)
383 _g_file_attribute_value_clear (&dest[i].value);
385 g_array_set_size (dest_info->attributes,
386 src_info->attributes->len);
388 source = (GFileAttribute *)src_info->attributes->data;
389 dest = (GFileAttribute *)dest_info->attributes->data;
391 for (i = 0; i < src_info->attributes->len; i++)
393 dest[i].attribute = source[i].attribute;
394 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
395 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
398 if (dest_info->mask != NO_ATTRIBUTE_MASK)
399 g_file_attribute_matcher_unref (dest_info->mask);
401 if (src_info->mask == NO_ATTRIBUTE_MASK)
402 dest_info->mask = NO_ATTRIBUTE_MASK;
403 else
404 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
408 * g_file_info_dup:
409 * @other: a #GFileInfo.
411 * Duplicates a file info structure.
413 * Returns: (transfer full): a duplicate #GFileInfo of @other.
415 GFileInfo *
416 g_file_info_dup (GFileInfo *other)
418 GFileInfo *new;
420 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
422 new = g_file_info_new ();
423 g_file_info_copy_into (other, new);
424 return new;
428 * g_file_info_set_attribute_mask:
429 * @info: a #GFileInfo.
430 * @mask: a #GFileAttributeMatcher.
432 * Sets @mask on @info to match specific attribute types.
434 void
435 g_file_info_set_attribute_mask (GFileInfo *info,
436 GFileAttributeMatcher *mask)
438 GFileAttribute *attr;
439 int i;
441 g_return_if_fail (G_IS_FILE_INFO (info));
443 if (mask != info->mask)
445 if (info->mask != NO_ATTRIBUTE_MASK)
446 g_file_attribute_matcher_unref (info->mask);
447 info->mask = g_file_attribute_matcher_ref (mask);
449 /* Remove non-matching attributes */
450 for (i = 0; i < info->attributes->len; i++)
452 attr = &g_array_index (info->attributes, GFileAttribute, i);
453 if (!_g_file_attribute_matcher_matches_id (mask,
454 attr->attribute))
456 _g_file_attribute_value_clear (&attr->value);
457 g_array_remove_index (info->attributes, i);
458 i--;
465 * g_file_info_unset_attribute_mask:
466 * @info: #GFileInfo.
468 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
469 * is set.
471 void
472 g_file_info_unset_attribute_mask (GFileInfo *info)
474 g_return_if_fail (G_IS_FILE_INFO (info));
476 if (info->mask != NO_ATTRIBUTE_MASK)
477 g_file_attribute_matcher_unref (info->mask);
478 info->mask = NO_ATTRIBUTE_MASK;
482 * g_file_info_clear_status:
483 * @info: a #GFileInfo.
485 * Clears the status information from @info.
487 void
488 g_file_info_clear_status (GFileInfo *info)
490 GFileAttribute *attrs;
491 int i;
493 g_return_if_fail (G_IS_FILE_INFO (info));
495 attrs = (GFileAttribute *)info->attributes->data;
496 for (i = 0; i < info->attributes->len; i++)
497 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
500 static int
501 g_file_info_find_place (GFileInfo *info,
502 guint32 attribute)
504 int min, max, med;
505 GFileAttribute *attrs;
506 /* Binary search for the place where attribute would be, if it's
507 in the array */
509 min = 0;
510 max = info->attributes->len;
512 attrs = (GFileAttribute *)info->attributes->data;
514 while (min < max)
516 med = min + (max - min) / 2;
517 if (attrs[med].attribute == attribute)
519 min = med;
520 break;
522 else if (attrs[med].attribute < attribute)
523 min = med + 1;
524 else /* attrs[med].attribute > attribute */
525 max = med;
528 return min;
531 static GFileAttributeValue *
532 g_file_info_find_value (GFileInfo *info,
533 guint32 attr_id)
535 GFileAttribute *attrs;
536 int i;
538 i = g_file_info_find_place (info, attr_id);
539 attrs = (GFileAttribute *)info->attributes->data;
540 if (i < info->attributes->len &&
541 attrs[i].attribute == attr_id)
542 return &attrs[i].value;
544 return NULL;
547 static GFileAttributeValue *
548 g_file_info_find_value_by_name (GFileInfo *info,
549 const char *attribute)
551 guint32 attr_id;
553 attr_id = lookup_attribute (attribute);
554 return g_file_info_find_value (info, attr_id);
558 * g_file_info_has_attribute:
559 * @info: a #GFileInfo.
560 * @attribute: a file attribute key.
562 * Checks if a file info structure has an attribute named @attribute.
564 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
565 * %FALSE otherwise.
567 gboolean
568 g_file_info_has_attribute (GFileInfo *info,
569 const char *attribute)
571 GFileAttributeValue *value;
573 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
574 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
576 value = g_file_info_find_value_by_name (info, attribute);
577 return value != NULL;
581 * g_file_info_has_namespace:
582 * @info: a #GFileInfo.
583 * @name_space: a file attribute namespace.
585 * Checks if a file info structure has an attribute in the
586 * specified @name_space.
588 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
589 * %FALSE otherwise.
591 * Since: 2.22
593 gboolean
594 g_file_info_has_namespace (GFileInfo *info,
595 const char *name_space)
597 GFileAttribute *attrs;
598 guint32 ns_id;
599 int i;
601 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
602 g_return_val_if_fail (name_space != NULL, FALSE);
604 ns_id = lookup_namespace (name_space);
606 attrs = (GFileAttribute *)info->attributes->data;
607 for (i = 0; i < info->attributes->len; i++)
609 if (GET_NS (attrs[i].attribute) == ns_id)
610 return TRUE;
613 return FALSE;
617 * g_file_info_list_attributes:
618 * @info: a #GFileInfo.
619 * @name_space: a file attribute key's namespace.
621 * Lists the file info structure's attributes.
623 * Returns: (array zero-terminated=1) (transfer full): a null-terminated array of strings of all of the
624 * possible attribute types for the given @name_space, or
625 * %NULL on error.
627 char **
628 g_file_info_list_attributes (GFileInfo *info,
629 const char *name_space)
631 GPtrArray *names;
632 GFileAttribute *attrs;
633 guint32 attribute;
634 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
635 int i;
637 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
639 names = g_ptr_array_new ();
640 attrs = (GFileAttribute *)info->attributes->data;
641 for (i = 0; i < info->attributes->len; i++)
643 attribute = attrs[i].attribute;
644 if (ns_id == 0 || GET_NS (attribute) == ns_id)
645 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
648 /* NULL terminate */
649 g_ptr_array_add (names, NULL);
651 return (char **)g_ptr_array_free (names, FALSE);
655 * g_file_info_get_attribute_type:
656 * @info: a #GFileInfo.
657 * @attribute: a file attribute key.
659 * Gets the attribute type for an attribute key.
661 * Returns: a #GFileAttributeType for the given @attribute, or
662 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
664 GFileAttributeType
665 g_file_info_get_attribute_type (GFileInfo *info,
666 const char *attribute)
668 GFileAttributeValue *value;
670 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
671 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
673 value = g_file_info_find_value_by_name (info, attribute);
674 if (value)
675 return value->type;
676 else
677 return G_FILE_ATTRIBUTE_TYPE_INVALID;
681 * g_file_info_remove_attribute:
682 * @info: a #GFileInfo.
683 * @attribute: a file attribute key.
685 * Removes all cases of @attribute from @info if it exists.
687 void
688 g_file_info_remove_attribute (GFileInfo *info,
689 const char *attribute)
691 guint32 attr_id;
692 GFileAttribute *attrs;
693 int i;
695 g_return_if_fail (G_IS_FILE_INFO (info));
696 g_return_if_fail (attribute != NULL && *attribute != '\0');
698 attr_id = lookup_attribute (attribute);
700 i = g_file_info_find_place (info, attr_id);
701 attrs = (GFileAttribute *)info->attributes->data;
702 if (i < info->attributes->len &&
703 attrs[i].attribute == attr_id)
705 _g_file_attribute_value_clear (&attrs[i].value);
706 g_array_remove_index (info->attributes, i);
711 * g_file_info_get_attribute_data:
712 * @info: a #GFileInfo
713 * @attribute: a file attribute key
714 * @type: (out) (allow-none): return location for the attribute type, or %NULL
715 * @value_pp: (out) (allow-none): return location for the attribute value, or %NULL
716 * @status: (out) (allow-none): return location for the attribute status, or %NULL
718 * Gets the attribute type, value and status for an attribute key.
720 * Returns: (transfer none): %TRUE if @info has an attribute named @attribute,
721 * %FALSE otherwise.
723 gboolean
724 g_file_info_get_attribute_data (GFileInfo *info,
725 const char *attribute,
726 GFileAttributeType *type,
727 gpointer *value_pp,
728 GFileAttributeStatus *status)
730 GFileAttributeValue *value;
732 value = g_file_info_find_value_by_name (info, attribute);
733 if (value == NULL)
734 return FALSE;
736 if (status)
737 *status = value->status;
739 if (type)
740 *type = value->type;
742 if (value_pp)
743 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
745 return TRUE;
749 * g_file_info_get_attribute_status:
750 * @info: a #GFileInfo
751 * @attribute: a file attribute key
753 * Gets the attribute status for an attribute key.
755 * Returns: a #GFileAttributeStatus for the given @attribute, or
756 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
759 GFileAttributeStatus
760 g_file_info_get_attribute_status (GFileInfo *info,
761 const char *attribute)
763 GFileAttributeValue *val;
765 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
766 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
768 val = g_file_info_find_value_by_name (info, attribute);
769 if (val)
770 return val->status;
772 return G_FILE_ATTRIBUTE_STATUS_UNSET;
776 * g_file_info_set_attribute_status:
777 * @info: a #GFileInfo
778 * @attribute: a file attribute key
779 * @status: a #GFileAttributeStatus
781 * Sets the attribute status for an attribute key. This is only
782 * needed by external code that implement g_file_set_attributes_from_info()
783 * or similar functions.
785 * The attribute must exist in @info for this to work. Otherwise %FALSE
786 * is returned and @info is unchanged.
788 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
790 * Since: 2.22
792 gboolean
793 g_file_info_set_attribute_status (GFileInfo *info,
794 const char *attribute,
795 GFileAttributeStatus status)
797 GFileAttributeValue *val;
799 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
800 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
802 val = g_file_info_find_value_by_name (info, attribute);
803 if (val)
805 val->status = status;
806 return TRUE;
809 return FALSE;
812 GFileAttributeValue *
813 _g_file_info_get_attribute_value (GFileInfo *info,
814 const char *attribute)
817 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
818 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
820 return g_file_info_find_value_by_name (info, attribute);
824 * g_file_info_get_attribute_as_string:
825 * @info: a #GFileInfo.
826 * @attribute: a file attribute key.
828 * Gets the value of a attribute, formated as a string.
829 * This escapes things as needed to make the string valid
830 * utf8.
832 * Returns: a UTF-8 string associated with the given @attribute.
833 * When you're done with the string it must be freed with g_free().
835 char *
836 g_file_info_get_attribute_as_string (GFileInfo *info,
837 const char *attribute)
839 GFileAttributeValue *val;
840 val = _g_file_info_get_attribute_value (info, attribute);
841 if (val)
842 return _g_file_attribute_value_as_string (val);
843 return NULL;
848 * g_file_info_get_attribute_object:
849 * @info: a #GFileInfo.
850 * @attribute: a file attribute key.
852 * Gets the value of a #GObject attribute. If the attribute does
853 * not contain a #GObject, %NULL will be returned.
855 * Returns: (transfer none): a #GObject associated with the given @attribute, or
856 * %NULL otherwise.
858 GObject *
859 g_file_info_get_attribute_object (GFileInfo *info,
860 const char *attribute)
862 GFileAttributeValue *value;
864 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
865 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
867 value = g_file_info_find_value_by_name (info, attribute);
868 return _g_file_attribute_value_get_object (value);
872 * g_file_info_get_attribute_string:
873 * @info: a #GFileInfo.
874 * @attribute: a file attribute key.
876 * Gets the value of a string attribute. If the attribute does
877 * not contain a string, %NULL will be returned.
879 * Returns: the contents of the @attribute value as a UTF-8 string, or
880 * %NULL otherwise.
882 const char *
883 g_file_info_get_attribute_string (GFileInfo *info,
884 const char *attribute)
886 GFileAttributeValue *value;
888 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
889 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
891 value = g_file_info_find_value_by_name (info, attribute);
892 return _g_file_attribute_value_get_string (value);
896 * g_file_info_get_attribute_byte_string:
897 * @info: a #GFileInfo.
898 * @attribute: a file attribute key.
900 * Gets the value of a byte string attribute. If the attribute does
901 * not contain a byte string, %NULL will be returned.
903 * Returns: the contents of the @attribute value as a byte string, or
904 * %NULL otherwise.
906 const char *
907 g_file_info_get_attribute_byte_string (GFileInfo *info,
908 const char *attribute)
910 GFileAttributeValue *value;
912 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
913 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
915 value = g_file_info_find_value_by_name (info, attribute);
916 return _g_file_attribute_value_get_byte_string (value);
920 * g_file_info_get_attribute_stringv:
921 * @info: a #GFileInfo.
922 * @attribute: a file attribute key.
924 * Gets the value of a stringv attribute. If the attribute does
925 * not contain a stringv, %NULL will be returned.
927 * Returns: (transfer none): the contents of the @attribute value as a stringv, or
928 * %NULL otherwise. Do not free. These returned strings are UTF-8.
930 * Since: 2.22
932 char **
933 g_file_info_get_attribute_stringv (GFileInfo *info,
934 const char *attribute)
936 GFileAttributeValue *value;
938 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
939 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
941 value = g_file_info_find_value_by_name (info, attribute);
942 return _g_file_attribute_value_get_stringv (value);
946 * g_file_info_get_attribute_boolean:
947 * @info: a #GFileInfo.
948 * @attribute: a file attribute key.
950 * Gets the value of a boolean attribute. If the attribute does not
951 * contain a boolean value, %FALSE will be returned.
953 * Returns: the boolean value contained within the attribute.
955 gboolean
956 g_file_info_get_attribute_boolean (GFileInfo *info,
957 const char *attribute)
959 GFileAttributeValue *value;
961 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
962 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
964 value = g_file_info_find_value_by_name (info, attribute);
965 return _g_file_attribute_value_get_boolean (value);
969 * g_file_info_get_attribute_uint32:
970 * @info: a #GFileInfo.
971 * @attribute: a file attribute key.
973 * Gets an unsigned 32-bit integer contained within the attribute. If the
974 * attribute does not contain an unsigned 32-bit integer, or is invalid,
975 * 0 will be returned.
977 * Returns: an unsigned 32-bit integer from the attribute.
979 guint32
980 g_file_info_get_attribute_uint32 (GFileInfo *info,
981 const char *attribute)
983 GFileAttributeValue *value;
985 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
986 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
988 value = g_file_info_find_value_by_name (info, attribute);
989 return _g_file_attribute_value_get_uint32 (value);
993 * g_file_info_get_attribute_int32:
994 * @info: a #GFileInfo.
995 * @attribute: a file attribute key.
997 * Gets a signed 32-bit integer contained within the attribute. If the
998 * attribute does not contain a signed 32-bit integer, or is invalid,
999 * 0 will be returned.
1001 * Returns: a signed 32-bit integer from the attribute.
1003 gint32
1004 g_file_info_get_attribute_int32 (GFileInfo *info,
1005 const char *attribute)
1007 GFileAttributeValue *value;
1009 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1010 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1012 value = g_file_info_find_value_by_name (info, attribute);
1013 return _g_file_attribute_value_get_int32 (value);
1017 * g_file_info_get_attribute_uint64:
1018 * @info: a #GFileInfo.
1019 * @attribute: a file attribute key.
1021 * Gets a unsigned 64-bit integer contained within the attribute. If the
1022 * attribute does not contain an unsigned 64-bit integer, or is invalid,
1023 * 0 will be returned.
1025 * Returns: a unsigned 64-bit integer from the attribute.
1027 guint64
1028 g_file_info_get_attribute_uint64 (GFileInfo *info,
1029 const char *attribute)
1031 GFileAttributeValue *value;
1033 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1034 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1036 value = g_file_info_find_value_by_name (info, attribute);
1037 return _g_file_attribute_value_get_uint64 (value);
1041 * g_file_info_get_attribute_int64:
1042 * @info: a #GFileInfo.
1043 * @attribute: a file attribute key.
1045 * Gets a signed 64-bit integer contained within the attribute. If the
1046 * attribute does not contain an signed 64-bit integer, or is invalid,
1047 * 0 will be returned.
1049 * Returns: a signed 64-bit integer from the attribute.
1051 gint64
1052 g_file_info_get_attribute_int64 (GFileInfo *info,
1053 const char *attribute)
1055 GFileAttributeValue *value;
1057 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1058 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1060 value = g_file_info_find_value_by_name (info, attribute);
1061 return _g_file_attribute_value_get_int64 (value);
1064 static GFileAttributeValue *
1065 g_file_info_create_value (GFileInfo *info,
1066 guint32 attr_id)
1068 GFileAttribute *attrs;
1069 int i;
1071 if (info->mask != NO_ATTRIBUTE_MASK &&
1072 !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
1073 return NULL;
1075 i = g_file_info_find_place (info, attr_id);
1077 attrs = (GFileAttribute *)info->attributes->data;
1078 if (i < info->attributes->len &&
1079 attrs[i].attribute == attr_id)
1080 return &attrs[i].value;
1081 else
1083 GFileAttribute attr = { 0 };
1084 attr.attribute = attr_id;
1085 g_array_insert_val (info->attributes, i, attr);
1087 attrs = (GFileAttribute *)info->attributes->data;
1088 return &attrs[i].value;
1092 void
1093 _g_file_info_set_attribute_by_id (GFileInfo *info,
1094 guint32 attribute,
1095 GFileAttributeType type,
1096 gpointer value_p)
1098 GFileAttributeValue *value;
1100 value = g_file_info_create_value (info, attribute);
1102 if (value)
1103 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1107 * g_file_info_set_attribute:
1108 * @info: a #GFileInfo.
1109 * @attribute: a file attribute key.
1110 * @type: a #GFileAttributeType
1111 * @value_p: pointer to the value
1113 * Sets the @attribute to contain the given value, if possible. To unset the
1114 * attribute, use %G_ATTRIBUTE_TYPE_INVALID for @type.
1116 void
1117 g_file_info_set_attribute (GFileInfo *info,
1118 const char *attribute,
1119 GFileAttributeType type,
1120 gpointer value_p)
1122 g_return_if_fail (G_IS_FILE_INFO (info));
1123 g_return_if_fail (attribute != NULL && *attribute != '\0');
1125 _g_file_info_set_attribute_by_id (info, lookup_attribute (attribute), type, value_p);
1128 void
1129 _g_file_info_set_attribute_object_by_id (GFileInfo *info,
1130 guint32 attribute,
1131 GObject *attr_value)
1133 GFileAttributeValue *value;
1135 value = g_file_info_create_value (info, attribute);
1136 if (value)
1137 _g_file_attribute_value_set_object (value, attr_value);
1141 * g_file_info_set_attribute_object:
1142 * @info: a #GFileInfo.
1143 * @attribute: a file attribute key.
1144 * @attr_value: a #GObject.
1146 * Sets the @attribute to contain the given @attr_value,
1147 * if possible.
1149 void
1150 g_file_info_set_attribute_object (GFileInfo *info,
1151 const char *attribute,
1152 GObject *attr_value)
1154 g_return_if_fail (G_IS_FILE_INFO (info));
1155 g_return_if_fail (attribute != NULL && *attribute != '\0');
1156 g_return_if_fail (G_IS_OBJECT (attr_value));
1158 _g_file_info_set_attribute_object_by_id (info,
1159 lookup_attribute (attribute),
1160 attr_value);
1163 void
1164 _g_file_info_set_attribute_stringv_by_id (GFileInfo *info,
1165 guint32 attribute,
1166 char **attr_value)
1168 GFileAttributeValue *value;
1170 value = g_file_info_create_value (info, attribute);
1171 if (value)
1172 _g_file_attribute_value_set_stringv (value, attr_value);
1176 * g_file_info_set_attribute_stringv:
1177 * @info: a #GFileInfo.
1178 * @attribute: a file attribute key
1179 * @attr_value: (array) (element-type utf8): a %NULL terminated array of UTF-8 strings.
1181 * Sets the @attribute to contain the given @attr_value,
1182 * if possible.
1184 * Sinze: 2.22
1186 void
1187 g_file_info_set_attribute_stringv (GFileInfo *info,
1188 const char *attribute,
1189 char **attr_value)
1191 g_return_if_fail (G_IS_FILE_INFO (info));
1192 g_return_if_fail (attribute != NULL && *attribute != '\0');
1193 g_return_if_fail (attr_value != NULL);
1195 _g_file_info_set_attribute_stringv_by_id (info,
1196 lookup_attribute (attribute),
1197 attr_value);
1200 void
1201 _g_file_info_set_attribute_string_by_id (GFileInfo *info,
1202 guint32 attribute,
1203 const char *attr_value)
1205 GFileAttributeValue *value;
1207 value = g_file_info_create_value (info, attribute);
1208 if (value)
1209 _g_file_attribute_value_set_string (value, attr_value);
1213 * g_file_info_set_attribute_string:
1214 * @info: a #GFileInfo.
1215 * @attribute: a file attribute key.
1216 * @attr_value: a UTF-8 string.
1218 * Sets the @attribute to contain the given @attr_value,
1219 * if possible.
1221 void
1222 g_file_info_set_attribute_string (GFileInfo *info,
1223 const char *attribute,
1224 const char *attr_value)
1226 g_return_if_fail (G_IS_FILE_INFO (info));
1227 g_return_if_fail (attribute != NULL && *attribute != '\0');
1228 g_return_if_fail (attr_value != NULL);
1230 _g_file_info_set_attribute_string_by_id (info,
1231 lookup_attribute (attribute),
1232 attr_value);
1235 void
1236 _g_file_info_set_attribute_byte_string_by_id (GFileInfo *info,
1237 guint32 attribute,
1238 const char *attr_value)
1240 GFileAttributeValue *value;
1242 value = g_file_info_create_value (info, attribute);
1243 if (value)
1244 _g_file_attribute_value_set_byte_string (value, attr_value);
1248 * g_file_info_set_attribute_byte_string:
1249 * @info: a #GFileInfo.
1250 * @attribute: a file attribute key.
1251 * @attr_value: a byte string.
1253 * Sets the @attribute to contain the given @attr_value,
1254 * if possible.
1256 void
1257 g_file_info_set_attribute_byte_string (GFileInfo *info,
1258 const char *attribute,
1259 const char *attr_value)
1261 g_return_if_fail (G_IS_FILE_INFO (info));
1262 g_return_if_fail (attribute != NULL && *attribute != '\0');
1263 g_return_if_fail (attr_value != NULL);
1265 _g_file_info_set_attribute_byte_string_by_id (info,
1266 lookup_attribute (attribute),
1267 attr_value);
1270 void
1271 _g_file_info_set_attribute_boolean_by_id (GFileInfo *info,
1272 guint32 attribute,
1273 gboolean attr_value)
1275 GFileAttributeValue *value;
1277 value = g_file_info_create_value (info, attribute);
1278 if (value)
1279 _g_file_attribute_value_set_boolean (value, attr_value);
1283 * g_file_info_set_attribute_boolean:
1284 * @info: a #GFileInfo.
1285 * @attribute: a file attribute key.
1286 * @attr_value: a boolean value.
1288 * Sets the @attribute to contain the given @attr_value,
1289 * if possible.
1291 void
1292 g_file_info_set_attribute_boolean (GFileInfo *info,
1293 const char *attribute,
1294 gboolean attr_value)
1296 g_return_if_fail (G_IS_FILE_INFO (info));
1297 g_return_if_fail (attribute != NULL && *attribute != '\0');
1299 _g_file_info_set_attribute_boolean_by_id (info,
1300 lookup_attribute (attribute),
1301 attr_value);
1304 void
1305 _g_file_info_set_attribute_uint32_by_id (GFileInfo *info,
1306 guint32 attribute,
1307 guint32 attr_value)
1309 GFileAttributeValue *value;
1311 value = g_file_info_create_value (info, attribute);
1312 if (value)
1313 _g_file_attribute_value_set_uint32 (value, attr_value);
1317 * g_file_info_set_attribute_uint32:
1318 * @info: a #GFileInfo.
1319 * @attribute: a file attribute key.
1320 * @attr_value: an unsigned 32-bit integer.
1322 * Sets the @attribute to contain the given @attr_value,
1323 * if possible.
1325 void
1326 g_file_info_set_attribute_uint32 (GFileInfo *info,
1327 const char *attribute,
1328 guint32 attr_value)
1330 g_return_if_fail (G_IS_FILE_INFO (info));
1331 g_return_if_fail (attribute != NULL && *attribute != '\0');
1333 _g_file_info_set_attribute_uint32_by_id (info,
1334 lookup_attribute (attribute),
1335 attr_value);
1338 void
1339 _g_file_info_set_attribute_int32_by_id (GFileInfo *info,
1340 guint32 attribute,
1341 gint32 attr_value)
1343 GFileAttributeValue *value;
1345 value = g_file_info_create_value (info, attribute);
1346 if (value)
1347 _g_file_attribute_value_set_int32 (value, attr_value);
1351 * g_file_info_set_attribute_int32:
1352 * @info: a #GFileInfo.
1353 * @attribute: a file attribute key.
1354 * @attr_value: a signed 32-bit integer
1356 * Sets the @attribute to contain the given @attr_value,
1357 * if possible.
1359 void
1360 g_file_info_set_attribute_int32 (GFileInfo *info,
1361 const char *attribute,
1362 gint32 attr_value)
1364 g_return_if_fail (G_IS_FILE_INFO (info));
1365 g_return_if_fail (attribute != NULL && *attribute != '\0');
1367 _g_file_info_set_attribute_int32_by_id (info,
1368 lookup_attribute (attribute),
1369 attr_value);
1372 void
1373 _g_file_info_set_attribute_uint64_by_id (GFileInfo *info,
1374 guint32 attribute,
1375 guint64 attr_value)
1377 GFileAttributeValue *value;
1379 value = g_file_info_create_value (info, attribute);
1380 if (value)
1381 _g_file_attribute_value_set_uint64 (value, attr_value);
1385 * g_file_info_set_attribute_uint64:
1386 * @info: a #GFileInfo.
1387 * @attribute: a file attribute key.
1388 * @attr_value: an unsigned 64-bit integer.
1390 * Sets the @attribute to contain the given @attr_value,
1391 * if possible.
1393 void
1394 g_file_info_set_attribute_uint64 (GFileInfo *info,
1395 const char *attribute,
1396 guint64 attr_value)
1398 g_return_if_fail (G_IS_FILE_INFO (info));
1399 g_return_if_fail (attribute != NULL && *attribute != '\0');
1401 _g_file_info_set_attribute_uint64_by_id (info,
1402 lookup_attribute (attribute),
1403 attr_value);
1406 void
1407 _g_file_info_set_attribute_int64_by_id (GFileInfo *info,
1408 guint32 attribute,
1409 gint64 attr_value)
1411 GFileAttributeValue *value;
1413 value = g_file_info_create_value (info, attribute);
1414 if (value)
1415 _g_file_attribute_value_set_int64 (value, attr_value);
1419 * g_file_info_set_attribute_int64:
1420 * @info: a #GFileInfo.
1421 * @attribute: attribute name to set.
1422 * @attr_value: int64 value to set attribute to.
1424 * Sets the @attribute to contain the given @attr_value,
1425 * if possible.
1428 void
1429 g_file_info_set_attribute_int64 (GFileInfo *info,
1430 const char *attribute,
1431 gint64 attr_value)
1433 g_return_if_fail (G_IS_FILE_INFO (info));
1434 g_return_if_fail (attribute != NULL && *attribute != '\0');
1436 _g_file_info_set_attribute_int64_by_id (info,
1437 lookup_attribute (attribute),
1438 attr_value);
1441 /* Helper getters */
1443 * g_file_info_get_deletion_date:
1444 * @info: a #GFileInfo.
1446 * Returns the #GDateTime representing the deletion date of the file, as
1447 * available in G_FILE_ATTRIBUTE_TRASH_DELETION_DATE. If the
1448 * G_FILE_ATTRIBUTE_TRASH_DELETION_DATE attribute is unset, %NULL is returned.
1450 * Returns: a #GDateTime, or %NULL.
1452 * Since: 2.36
1454 GDateTime *
1455 g_file_info_get_deletion_date (GFileInfo *info)
1457 static guint32 attr = 0;
1458 GFileAttributeValue *value;
1459 const char *date_str;
1460 GTimeVal tv;
1462 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1464 if (attr == 0)
1465 attr = lookup_attribute (G_FILE_ATTRIBUTE_TRASH_DELETION_DATE);
1467 value = g_file_info_find_value (info, attr);
1468 date_str = _g_file_attribute_value_get_string (value);
1469 if (!date_str)
1470 return NULL;
1472 if (g_time_val_from_iso8601 (date_str, &tv) == FALSE)
1473 return NULL;
1475 return g_date_time_new_from_timeval_local (&tv);
1479 * g_file_info_get_file_type:
1480 * @info: a #GFileInfo.
1482 * Gets a file's type (whether it is a regular file, symlink, etc).
1483 * This is different from the file's content type, see g_file_info_get_content_type().
1485 * Returns: a #GFileType for the given file.
1487 GFileType
1488 g_file_info_get_file_type (GFileInfo *info)
1490 static guint32 attr = 0;
1491 GFileAttributeValue *value;
1493 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1495 if (attr == 0)
1496 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1498 value = g_file_info_find_value (info, attr);
1499 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1503 * g_file_info_get_is_hidden:
1504 * @info: a #GFileInfo.
1506 * Checks if a file is hidden.
1508 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1510 gboolean
1511 g_file_info_get_is_hidden (GFileInfo *info)
1513 static guint32 attr = 0;
1514 GFileAttributeValue *value;
1516 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1518 if (attr == 0)
1519 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1521 value = g_file_info_find_value (info, attr);
1522 return (GFileType)_g_file_attribute_value_get_boolean (value);
1526 * g_file_info_get_is_backup:
1527 * @info: a #GFileInfo.
1529 * Checks if a file is a backup file.
1531 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1533 gboolean
1534 g_file_info_get_is_backup (GFileInfo *info)
1536 static guint32 attr = 0;
1537 GFileAttributeValue *value;
1539 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1541 if (attr == 0)
1542 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1544 value = g_file_info_find_value (info, attr);
1545 return (GFileType)_g_file_attribute_value_get_boolean (value);
1549 * g_file_info_get_is_symlink:
1550 * @info: a #GFileInfo.
1552 * Checks if a file is a symlink.
1554 * Returns: %TRUE if the given @info is a symlink.
1556 gboolean
1557 g_file_info_get_is_symlink (GFileInfo *info)
1559 static guint32 attr = 0;
1560 GFileAttributeValue *value;
1562 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1564 if (attr == 0)
1565 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1567 value = g_file_info_find_value (info, attr);
1568 return (GFileType)_g_file_attribute_value_get_boolean (value);
1572 * g_file_info_get_name:
1573 * @info: a #GFileInfo.
1575 * Gets the name for a file.
1577 * Returns: a string containing the file name.
1579 const char *
1580 g_file_info_get_name (GFileInfo *info)
1582 static guint32 attr = 0;
1583 GFileAttributeValue *value;
1585 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1587 if (attr == 0)
1588 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1590 value = g_file_info_find_value (info, attr);
1591 return _g_file_attribute_value_get_byte_string (value);
1595 * g_file_info_get_display_name:
1596 * @info: a #GFileInfo.
1598 * Gets a display name for a file.
1600 * Returns: a string containing the display name.
1602 const char *
1603 g_file_info_get_display_name (GFileInfo *info)
1605 static guint32 attr = 0;
1606 GFileAttributeValue *value;
1608 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1610 if (attr == 0)
1611 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1613 value = g_file_info_find_value (info, attr);
1614 return _g_file_attribute_value_get_string (value);
1618 * g_file_info_get_edit_name:
1619 * @info: a #GFileInfo.
1621 * Gets the edit name for a file.
1623 * Returns: a string containing the edit name.
1625 const char *
1626 g_file_info_get_edit_name (GFileInfo *info)
1628 static guint32 attr = 0;
1629 GFileAttributeValue *value;
1631 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1633 if (attr == 0)
1634 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1636 value = g_file_info_find_value (info, attr);
1637 return _g_file_attribute_value_get_string (value);
1641 * g_file_info_get_icon:
1642 * @info: a #GFileInfo.
1644 * Gets the icon for a file.
1646 * Returns: (transfer none): #GIcon for the given @info.
1648 GIcon *
1649 g_file_info_get_icon (GFileInfo *info)
1651 static guint32 attr = 0;
1652 GFileAttributeValue *value;
1653 GObject *obj;
1655 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1657 if (attr == 0)
1658 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1660 value = g_file_info_find_value (info, attr);
1661 obj = _g_file_attribute_value_get_object (value);
1662 if (G_IS_ICON (obj))
1663 return G_ICON (obj);
1664 return NULL;
1668 * g_file_info_get_symbolic_icon:
1669 * @info: a #GFileInfo.
1671 * Gets the symbolic icon for a file.
1673 * Returns: (transfer none): #GIcon for the given @info.
1675 * Since: 2.34
1677 GIcon *
1678 g_file_info_get_symbolic_icon (GFileInfo *info)
1680 static guint32 attr = 0;
1681 GFileAttributeValue *value;
1682 GObject *obj;
1684 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1686 if (attr == 0)
1687 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON);
1689 value = g_file_info_find_value (info, attr);
1690 obj = _g_file_attribute_value_get_object (value);
1691 if (G_IS_ICON (obj))
1692 return G_ICON (obj);
1693 return NULL;
1697 * g_file_info_get_content_type:
1698 * @info: a #GFileInfo.
1700 * Gets the file's content type.
1702 * Returns: a string containing the file's content type.
1704 const char *
1705 g_file_info_get_content_type (GFileInfo *info)
1707 static guint32 attr = 0;
1708 GFileAttributeValue *value;
1710 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1712 if (attr == 0)
1713 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1715 value = g_file_info_find_value (info, attr);
1716 return _g_file_attribute_value_get_string (value);
1720 * g_file_info_get_size:
1721 * @info: a #GFileInfo.
1723 * Gets the file's size.
1725 * Returns: a #goffset containing the file's size.
1727 goffset
1728 g_file_info_get_size (GFileInfo *info)
1730 static guint32 attr = 0;
1731 GFileAttributeValue *value;
1733 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1735 if (attr == 0)
1736 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1738 value = g_file_info_find_value (info, attr);
1739 return (goffset) _g_file_attribute_value_get_uint64 (value);
1743 * g_file_info_get_modification_time:
1744 * @info: a #GFileInfo.
1745 * @result: (out caller-allocates): a #GTimeVal.
1747 * Gets the modification time of the current @info and sets it
1748 * in @result.
1750 void
1751 g_file_info_get_modification_time (GFileInfo *info,
1752 GTimeVal *result)
1754 static guint32 attr_mtime = 0, attr_mtime_usec;
1755 GFileAttributeValue *value;
1757 g_return_if_fail (G_IS_FILE_INFO (info));
1758 g_return_if_fail (result != NULL);
1760 if (attr_mtime == 0)
1762 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1763 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1766 value = g_file_info_find_value (info, attr_mtime);
1767 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1768 value = g_file_info_find_value (info, attr_mtime_usec);
1769 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1773 * g_file_info_get_symlink_target:
1774 * @info: a #GFileInfo.
1776 * Gets the symlink target for a given #GFileInfo.
1778 * Returns: a string containing the symlink target.
1780 const char *
1781 g_file_info_get_symlink_target (GFileInfo *info)
1783 static guint32 attr = 0;
1784 GFileAttributeValue *value;
1786 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1788 if (attr == 0)
1789 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1791 value = g_file_info_find_value (info, attr);
1792 return _g_file_attribute_value_get_byte_string (value);
1796 * g_file_info_get_etag:
1797 * @info: a #GFileInfo.
1799 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1800 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1802 * Returns: a string containing the value of the "etag:value" attribute.
1804 const char *
1805 g_file_info_get_etag (GFileInfo *info)
1807 static guint32 attr = 0;
1808 GFileAttributeValue *value;
1810 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1812 if (attr == 0)
1813 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1815 value = g_file_info_find_value (info, attr);
1816 return _g_file_attribute_value_get_string (value);
1820 * g_file_info_get_sort_order:
1821 * @info: a #GFileInfo.
1823 * Gets the value of the sort_order attribute from the #GFileInfo.
1824 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1826 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1828 gint32
1829 g_file_info_get_sort_order (GFileInfo *info)
1831 static guint32 attr = 0;
1832 GFileAttributeValue *value;
1834 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1836 if (attr == 0)
1837 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1839 value = g_file_info_find_value (info, attr);
1840 return _g_file_attribute_value_get_int32 (value);
1843 /* Helper setters: */
1845 * g_file_info_set_file_type:
1846 * @info: a #GFileInfo.
1847 * @type: a #GFileType.
1849 * Sets the file type in a #GFileInfo to @type.
1850 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1852 void
1853 g_file_info_set_file_type (GFileInfo *info,
1854 GFileType type)
1856 static guint32 attr = 0;
1857 GFileAttributeValue *value;
1859 g_return_if_fail (G_IS_FILE_INFO (info));
1861 if (attr == 0)
1862 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1864 value = g_file_info_create_value (info, attr);
1865 if (value)
1866 _g_file_attribute_value_set_uint32 (value, type);
1870 * g_file_info_set_is_hidden:
1871 * @info: a #GFileInfo.
1872 * @is_hidden: a #gboolean.
1874 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1875 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1877 void
1878 g_file_info_set_is_hidden (GFileInfo *info,
1879 gboolean is_hidden)
1881 static guint32 attr = 0;
1882 GFileAttributeValue *value;
1884 g_return_if_fail (G_IS_FILE_INFO (info));
1886 if (attr == 0)
1887 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1889 value = g_file_info_create_value (info, attr);
1890 if (value)
1891 _g_file_attribute_value_set_boolean (value, is_hidden);
1895 * g_file_info_set_is_symlink:
1896 * @info: a #GFileInfo.
1897 * @is_symlink: a #gboolean.
1899 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1900 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1902 void
1903 g_file_info_set_is_symlink (GFileInfo *info,
1904 gboolean is_symlink)
1906 static guint32 attr = 0;
1907 GFileAttributeValue *value;
1909 g_return_if_fail (G_IS_FILE_INFO (info));
1911 if (attr == 0)
1912 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1914 value = g_file_info_create_value (info, attr);
1915 if (value)
1916 _g_file_attribute_value_set_boolean (value, is_symlink);
1920 * g_file_info_set_name:
1921 * @info: a #GFileInfo.
1922 * @name: a string containing a name.
1924 * Sets the name attribute for the current #GFileInfo.
1925 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1927 void
1928 g_file_info_set_name (GFileInfo *info,
1929 const char *name)
1931 static guint32 attr = 0;
1932 GFileAttributeValue *value;
1934 g_return_if_fail (G_IS_FILE_INFO (info));
1935 g_return_if_fail (name != NULL);
1937 if (attr == 0)
1938 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1940 value = g_file_info_create_value (info, attr);
1941 if (value)
1942 _g_file_attribute_value_set_byte_string (value, name);
1946 * g_file_info_set_display_name:
1947 * @info: a #GFileInfo.
1948 * @display_name: a string containing a display name.
1950 * Sets the display name for the current #GFileInfo.
1951 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1953 void
1954 g_file_info_set_display_name (GFileInfo *info,
1955 const char *display_name)
1957 static guint32 attr = 0;
1958 GFileAttributeValue *value;
1960 g_return_if_fail (G_IS_FILE_INFO (info));
1961 g_return_if_fail (display_name != NULL);
1963 if (attr == 0)
1964 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1966 value = g_file_info_create_value (info, attr);
1967 if (value)
1968 _g_file_attribute_value_set_string (value, display_name);
1972 * g_file_info_set_edit_name:
1973 * @info: a #GFileInfo.
1974 * @edit_name: a string containing an edit name.
1976 * Sets the edit name for the current file.
1977 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1979 void
1980 g_file_info_set_edit_name (GFileInfo *info,
1981 const char *edit_name)
1983 static guint32 attr = 0;
1984 GFileAttributeValue *value;
1986 g_return_if_fail (G_IS_FILE_INFO (info));
1987 g_return_if_fail (edit_name != NULL);
1989 if (attr == 0)
1990 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1992 value = g_file_info_create_value (info, attr);
1993 if (value)
1994 _g_file_attribute_value_set_string (value, edit_name);
1998 * g_file_info_set_icon:
1999 * @info: a #GFileInfo.
2000 * @icon: a #GIcon.
2002 * Sets the icon for a given #GFileInfo.
2003 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
2005 void
2006 g_file_info_set_icon (GFileInfo *info,
2007 GIcon *icon)
2009 static guint32 attr = 0;
2010 GFileAttributeValue *value;
2012 g_return_if_fail (G_IS_FILE_INFO (info));
2013 g_return_if_fail (G_IS_ICON (icon));
2015 if (attr == 0)
2016 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
2018 value = g_file_info_create_value (info, attr);
2019 if (value)
2020 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
2024 * g_file_info_set_symbolic_icon:
2025 * @info: a #GFileInfo.
2026 * @icon: a #GIcon.
2028 * Sets the symbolic icon for a given #GFileInfo.
2029 * See %G_FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON.
2031 * Since: 2.34
2033 void
2034 g_file_info_set_symbolic_icon (GFileInfo *info,
2035 GIcon *icon)
2037 static guint32 attr = 0;
2038 GFileAttributeValue *value;
2040 g_return_if_fail (G_IS_FILE_INFO (info));
2041 g_return_if_fail (G_IS_ICON (icon));
2043 if (attr == 0)
2044 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON);
2046 value = g_file_info_create_value (info, attr);
2047 if (value)
2048 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
2052 * g_file_info_set_content_type:
2053 * @info: a #GFileInfo.
2054 * @content_type: a content type. See <link linkend="gio-GContentType">GContentType</link>.
2056 * Sets the content type attribute for a given #GFileInfo.
2057 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
2059 void
2060 g_file_info_set_content_type (GFileInfo *info,
2061 const char *content_type)
2063 static guint32 attr = 0;
2064 GFileAttributeValue *value;
2066 g_return_if_fail (G_IS_FILE_INFO (info));
2067 g_return_if_fail (content_type != NULL);
2069 if (attr == 0)
2070 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
2072 value = g_file_info_create_value (info, attr);
2073 if (value)
2074 _g_file_attribute_value_set_string (value, content_type);
2078 * g_file_info_set_size:
2079 * @info: a #GFileInfo.
2080 * @size: a #goffset containing the file's size.
2082 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
2083 * to the given size.
2085 void
2086 g_file_info_set_size (GFileInfo *info,
2087 goffset size)
2089 static guint32 attr = 0;
2090 GFileAttributeValue *value;
2092 g_return_if_fail (G_IS_FILE_INFO (info));
2094 if (attr == 0)
2095 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
2097 value = g_file_info_create_value (info, attr);
2098 if (value)
2099 _g_file_attribute_value_set_uint64 (value, size);
2103 * g_file_info_set_modification_time:
2104 * @info: a #GFileInfo.
2105 * @mtime: a #GTimeVal.
2107 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
2108 * info to the given time value.
2110 void
2111 g_file_info_set_modification_time (GFileInfo *info,
2112 GTimeVal *mtime)
2114 static guint32 attr_mtime = 0, attr_mtime_usec;
2115 GFileAttributeValue *value;
2117 g_return_if_fail (G_IS_FILE_INFO (info));
2118 g_return_if_fail (mtime != NULL);
2120 if (attr_mtime == 0)
2122 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
2123 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2126 value = g_file_info_create_value (info, attr_mtime);
2127 if (value)
2128 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
2129 value = g_file_info_create_value (info, attr_mtime_usec);
2130 if (value)
2131 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
2135 * g_file_info_set_symlink_target:
2136 * @info: a #GFileInfo.
2137 * @symlink_target: a static string containing a path to a symlink target.
2139 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
2140 * to the given symlink target.
2142 void
2143 g_file_info_set_symlink_target (GFileInfo *info,
2144 const char *symlink_target)
2146 static guint32 attr = 0;
2147 GFileAttributeValue *value;
2149 g_return_if_fail (G_IS_FILE_INFO (info));
2150 g_return_if_fail (symlink_target != NULL);
2152 if (attr == 0)
2153 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2155 value = g_file_info_create_value (info, attr);
2156 if (value)
2157 _g_file_attribute_value_set_byte_string (value, symlink_target);
2161 * g_file_info_set_sort_order:
2162 * @info: a #GFileInfo.
2163 * @sort_order: a sort order integer.
2165 * Sets the sort order attribute in the file info structure. See
2166 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
2168 void
2169 g_file_info_set_sort_order (GFileInfo *info,
2170 gint32 sort_order)
2172 static guint32 attr = 0;
2173 GFileAttributeValue *value;
2175 g_return_if_fail (G_IS_FILE_INFO (info));
2177 if (attr == 0)
2178 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
2180 value = g_file_info_create_value (info, attr);
2181 if (value)
2182 _g_file_attribute_value_set_int32 (value, sort_order);
2186 typedef struct {
2187 guint32 id;
2188 guint32 mask;
2189 } SubMatcher;
2191 struct _GFileAttributeMatcher {
2192 gboolean all;
2193 gint ref;
2195 GArray *sub_matchers;
2197 /* Interator */
2198 guint32 iterator_ns;
2199 gint iterator_pos;
2202 G_DEFINE_BOXED_TYPE (GFileAttributeMatcher, g_file_attribute_matcher,
2203 g_file_attribute_matcher_ref,
2204 g_file_attribute_matcher_unref)
2206 static gint
2207 compare_sub_matchers (gconstpointer a,
2208 gconstpointer b)
2210 const SubMatcher *suba = a;
2211 const SubMatcher *subb = b;
2212 int diff;
2214 diff = suba->id - subb->id;
2216 if (diff)
2217 return diff;
2219 return suba->mask - subb->mask;
2222 static gboolean
2223 sub_matcher_matches (SubMatcher *matcher,
2224 SubMatcher *submatcher)
2226 if ((matcher->mask & submatcher->mask) != matcher->mask)
2227 return FALSE;
2229 return matcher->id == (submatcher->id & matcher->mask);
2232 /* Call this function after modifying a matcher.
2233 * It will ensure all the invariants other functions rely on.
2235 static GFileAttributeMatcher *
2236 matcher_optimize (GFileAttributeMatcher *matcher)
2238 SubMatcher *submatcher, *compare;
2239 guint i, j;
2241 /* remove sub_matchers if we match everything anyway */
2242 if (matcher->all)
2244 if (matcher->sub_matchers)
2246 g_array_free (matcher->sub_matchers, TRUE);
2247 matcher->sub_matchers = NULL;
2249 return matcher;
2252 if (matcher->sub_matchers->len == 0)
2254 g_file_attribute_matcher_unref (matcher);
2255 return NULL;
2258 /* sort sub_matchers by id (and then mask), so we can bsearch
2259 * and compare matchers in O(N) instead of O(N²) */
2260 g_array_sort (matcher->sub_matchers, compare_sub_matchers);
2262 /* remove duplicates and specific matches when we match the whole namespace */
2263 j = 0;
2264 compare = &g_array_index (matcher->sub_matchers, SubMatcher, j);
2266 for (i = 1; i < matcher->sub_matchers->len; i++)
2268 submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2269 if (sub_matcher_matches (compare, submatcher))
2270 continue;
2272 j++;
2273 compare++;
2275 if (j < i)
2276 *compare = *submatcher;
2279 g_array_set_size (matcher->sub_matchers, j + 1);
2281 return matcher;
2285 * g_file_attribute_matcher_new:
2286 * @attributes: an attribute string to match.
2288 * Creates a new file attribute matcher, which matches attributes
2289 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
2290 * counted structures, and are created with a reference count of 1. If
2291 * the number of references falls to 0, the #GFileAttributeMatcher is
2292 * automatically destroyed.
2294 * The @attribute string should be formatted with specific keys separated
2295 * from namespaces with a double colon. Several "namespace::key" strings may be
2296 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
2297 * The wildcard "*" may be used to match all keys and namespaces, or
2298 * "namespace::*" will match all keys in a given namespace.
2300 * Examples of strings to use:
2301 * <table>
2302 * <title>File Attribute Matcher strings and results</title>
2303 * <tgroup cols='2' align='left'><thead>
2304 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
2305 * <tbody>
2306 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
2307 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
2308 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
2309 * all keys in the unix namespace.</entry></row>
2310 * </tbody></tgroup>
2311 * </table>
2313 * Returns: a #GFileAttributeMatcher.
2315 GFileAttributeMatcher *
2316 g_file_attribute_matcher_new (const char *attributes)
2318 char **split;
2319 char *colon;
2320 int i;
2321 GFileAttributeMatcher *matcher;
2323 if (attributes == NULL || *attributes == '\0')
2324 return NULL;
2326 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2327 matcher->ref = 1;
2328 matcher->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2330 split = g_strsplit (attributes, ",", -1);
2332 for (i = 0; split[i] != NULL; i++)
2334 if (strcmp (split[i], "*") == 0)
2335 matcher->all = TRUE;
2336 else
2338 SubMatcher s;
2340 colon = strstr (split[i], "::");
2341 if (colon != NULL &&
2342 !(colon[2] == 0 ||
2343 (colon[2] == '*' &&
2344 colon[3] == 0)))
2346 s.id = lookup_attribute (split[i]);
2347 s.mask = 0xffffffff;
2349 else
2351 if (colon)
2352 *colon = 0;
2354 s.id = lookup_namespace (split[i]) << NS_POS;
2355 s.mask = NS_MASK << NS_POS;
2358 g_array_append_val (matcher->sub_matchers, s);
2362 g_strfreev (split);
2364 matcher = matcher_optimize (matcher);
2366 return matcher;
2370 * g_file_attribute_matcher_subtract:
2371 * @matcher: Matcher to subtract from
2372 * @subtract: The matcher to subtract
2374 * Subtracts all attributes of @subtract from @matcher and returns
2375 * a matcher that supports those attributes.
2377 * Note that currently it is not possible to remove a single
2378 * attribute when the @matcher matches the whole namespace - or remove
2379 * a namespace or attribute when the matcher matches everything. This
2380 * is a limitation of the current implementation, but may be fixed
2381 * in the future.
2383 * Returns: A file attribute matcher matching all attributes of
2384 * @matcher that are not matched by @subtract
2386 GFileAttributeMatcher *
2387 g_file_attribute_matcher_subtract (GFileAttributeMatcher *matcher,
2388 GFileAttributeMatcher *subtract)
2390 GFileAttributeMatcher *result;
2391 guint mi, si;
2392 SubMatcher *msub, *ssub;
2394 if (matcher == NULL)
2395 return NULL;
2396 if (subtract == NULL)
2397 return g_file_attribute_matcher_ref (matcher);
2398 if (subtract->all)
2399 return NULL;
2400 if (matcher->all)
2401 return g_file_attribute_matcher_ref (matcher);
2403 result = g_malloc0 (sizeof (GFileAttributeMatcher));
2404 result->ref = 1;
2405 result->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2407 si = 0;
2408 g_assert (subtract->sub_matchers->len > 0);
2409 ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);
2411 for (mi = 0; mi < matcher->sub_matchers->len; mi++)
2413 msub = &g_array_index (matcher->sub_matchers, SubMatcher, mi);
2415 retry:
2416 if (sub_matcher_matches (ssub, msub))
2417 continue;
2419 si++;
2420 if (si >= subtract->sub_matchers->len)
2421 break;
2423 ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);
2424 if (ssub->id <= msub->id)
2425 goto retry;
2427 g_array_append_val (result->sub_matchers, *msub);
2430 if (mi < matcher->sub_matchers->len)
2431 g_array_append_vals (result->sub_matchers,
2432 &g_array_index (matcher->sub_matchers, SubMatcher, mi),
2433 matcher->sub_matchers->len - mi);
2435 result = matcher_optimize (result);
2437 return result;
2441 * g_file_attribute_matcher_ref:
2442 * @matcher: a #GFileAttributeMatcher.
2444 * References a file attribute matcher.
2446 * Returns: a #GFileAttributeMatcher.
2448 GFileAttributeMatcher *
2449 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2451 if (matcher)
2453 g_return_val_if_fail (matcher->ref > 0, NULL);
2454 g_atomic_int_inc (&matcher->ref);
2456 return matcher;
2460 * g_file_attribute_matcher_unref:
2461 * @matcher: a #GFileAttributeMatcher.
2463 * Unreferences @matcher. If the reference count falls below 1,
2464 * the @matcher is automatically freed.
2467 void
2468 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2470 if (matcher)
2472 g_return_if_fail (matcher->ref > 0);
2474 if (g_atomic_int_dec_and_test (&matcher->ref))
2476 if (matcher->sub_matchers)
2477 g_array_free (matcher->sub_matchers, TRUE);
2479 g_free (matcher);
2485 * g_file_attribute_matcher_matches_only:
2486 * @matcher: a #GFileAttributeMatcher.
2487 * @attribute: a file attribute key.
2489 * Checks if a attribute matcher only matches a given attribute. Always
2490 * returns %FALSE if "*" was used when creating the matcher.
2492 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2494 gboolean
2495 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2496 const char *attribute)
2498 SubMatcher *sub_matcher;
2499 guint32 id;
2501 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2503 if (matcher == NULL ||
2504 matcher->all)
2505 return FALSE;
2507 if (matcher->sub_matchers->len != 1)
2508 return FALSE;
2510 id = lookup_attribute (attribute);
2512 sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, 0);
2514 return sub_matcher->id == id &&
2515 sub_matcher->mask == 0xffffffff;
2518 static gboolean
2519 matcher_matches_id (GFileAttributeMatcher *matcher,
2520 guint32 id)
2522 SubMatcher *sub_matchers;
2523 int i;
2525 if (matcher->sub_matchers)
2527 sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
2528 for (i = 0; i < matcher->sub_matchers->len; i++)
2530 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2531 return TRUE;
2535 return FALSE;
2538 gboolean
2539 _g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2540 guint32 id)
2542 /* We return a NULL matcher for an empty match string, so handle this */
2543 if (matcher == NULL)
2544 return FALSE;
2546 if (matcher->all)
2547 return TRUE;
2549 return matcher_matches_id (matcher, id);
2553 * g_file_attribute_matcher_matches:
2554 * @matcher: a #GFileAttributeMatcher.
2555 * @attribute: a file attribute key.
2557 * Checks if an attribute will be matched by an attribute matcher. If
2558 * the matcher was created with the "*" matching string, this function
2559 * will always return %TRUE.
2561 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2563 gboolean
2564 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2565 const char *attribute)
2567 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2569 /* We return a NULL matcher for an empty match string, so handle this */
2570 if (matcher == NULL)
2571 return FALSE;
2573 if (matcher->all)
2574 return TRUE;
2576 return matcher_matches_id (matcher, lookup_attribute (attribute));
2579 /* return TRUE -> all */
2581 * g_file_attribute_matcher_enumerate_namespace:
2582 * @matcher: a #GFileAttributeMatcher.
2583 * @ns: a string containing a file attribute namespace.
2585 * Checks if the matcher will match all of the keys in a given namespace.
2586 * This will always return %TRUE if a wildcard character is in use (e.g. if
2587 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2588 * using "*" and namespace is anything.)
2590 * TODO: this is awkwardly worded.
2592 * Returns: %TRUE if the matcher matches all of the entries
2593 * in the given @ns, %FALSE otherwise.
2595 gboolean
2596 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2597 const char *ns)
2599 SubMatcher *sub_matchers;
2600 int ns_id;
2601 int i;
2603 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2605 /* We return a NULL matcher for an empty match string, so handle this */
2606 if (matcher == NULL)
2607 return FALSE;
2609 if (matcher->all)
2610 return TRUE;
2612 ns_id = lookup_namespace (ns) << NS_POS;
2614 if (matcher->sub_matchers)
2616 sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
2617 for (i = 0; i < matcher->sub_matchers->len; i++)
2619 if (sub_matchers[i].id == ns_id)
2620 return TRUE;
2624 matcher->iterator_ns = ns_id;
2625 matcher->iterator_pos = 0;
2627 return FALSE;
2631 * g_file_attribute_matcher_enumerate_next:
2632 * @matcher: a #GFileAttributeMatcher.
2634 * Gets the next matched attribute from a #GFileAttributeMatcher.
2636 * Returns: a string containing the next attribute or %NULL if
2637 * no more attribute exist.
2639 const char *
2640 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2642 int i;
2643 SubMatcher *sub_matcher;
2645 /* We return a NULL matcher for an empty match string, so handle this */
2646 if (matcher == NULL)
2647 return NULL;
2649 while (1)
2651 i = matcher->iterator_pos++;
2653 if (matcher->sub_matchers == NULL)
2654 return NULL;
2656 if (i < matcher->sub_matchers->len)
2657 sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2658 else
2659 return NULL;
2661 if (sub_matcher->mask == 0xffffffff &&
2662 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2663 return get_attribute_for_id (sub_matcher->id);
2668 * g_file_attribute_matcher_to_string:
2669 * @matcher: (allow-none): a #GFileAttributeMatcher.
2671 * Prints what the matcher is matching against. The format will be
2672 * equal to the format passed to g_file_attribute_matcher_new().
2673 * The output however, might not be identical, as the matcher may
2674 * decide to use a different order or omit needless parts.
2676 * Returns: a string describing the attributes the matcher matches
2677 * against or %NULL if @matcher was %NULL.
2679 * Since: 2.32
2681 char *
2682 g_file_attribute_matcher_to_string (GFileAttributeMatcher *matcher)
2684 GString *string;
2685 guint i;
2687 if (matcher == NULL)
2688 return NULL;
2690 if (matcher->all)
2691 return g_strdup ("*");
2693 string = g_string_new ("");
2694 for (i = 0; i < matcher->sub_matchers->len; i++)
2696 SubMatcher *submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2698 if (i > 0)
2699 g_string_append_c (string, ',');
2701 g_string_append (string, get_attribute_for_id (submatcher->id));
2704 return g_string_free (string, FALSE);