docs: Fix GFileAttribute link in GFileInfo
[glib.git] / gio / gfileinfo.c
blob9456afa872cda923e846f0bf69507f8dda8ffbfc
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 (ETAG_VALUE);
208 REGISTER_ATTRIBUTE (ID_FILE);
209 REGISTER_ATTRIBUTE (ID_FILESYSTEM);
210 REGISTER_ATTRIBUTE (ACCESS_CAN_READ);
211 REGISTER_ATTRIBUTE (ACCESS_CAN_WRITE);
212 REGISTER_ATTRIBUTE (ACCESS_CAN_EXECUTE);
213 REGISTER_ATTRIBUTE (ACCESS_CAN_DELETE);
214 REGISTER_ATTRIBUTE (ACCESS_CAN_TRASH);
215 REGISTER_ATTRIBUTE (ACCESS_CAN_RENAME);
216 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_MOUNT);
217 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_UNMOUNT);
218 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_EJECT);
219 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE);
220 REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE_FILE);
221 REGISTER_ATTRIBUTE (MOUNTABLE_HAL_UDI);
222 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START);
223 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START_DEGRADED);
224 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_STOP);
225 REGISTER_ATTRIBUTE (MOUNTABLE_START_STOP_TYPE);
226 REGISTER_ATTRIBUTE (MOUNTABLE_CAN_POLL);
227 REGISTER_ATTRIBUTE (MOUNTABLE_IS_MEDIA_CHECK_AUTOMATIC);
228 REGISTER_ATTRIBUTE (TIME_MODIFIED);
229 REGISTER_ATTRIBUTE (TIME_MODIFIED_USEC);
230 REGISTER_ATTRIBUTE (TIME_ACCESS);
231 REGISTER_ATTRIBUTE (TIME_ACCESS_USEC);
232 REGISTER_ATTRIBUTE (TIME_CHANGED);
233 REGISTER_ATTRIBUTE (TIME_CHANGED_USEC);
234 REGISTER_ATTRIBUTE (TIME_CREATED);
235 REGISTER_ATTRIBUTE (TIME_CREATED_USEC);
236 REGISTER_ATTRIBUTE (UNIX_DEVICE);
237 REGISTER_ATTRIBUTE (UNIX_INODE);
238 REGISTER_ATTRIBUTE (UNIX_MODE);
239 REGISTER_ATTRIBUTE (UNIX_NLINK);
240 REGISTER_ATTRIBUTE (UNIX_UID);
241 REGISTER_ATTRIBUTE (UNIX_GID);
242 REGISTER_ATTRIBUTE (UNIX_RDEV);
243 REGISTER_ATTRIBUTE (UNIX_BLOCK_SIZE);
244 REGISTER_ATTRIBUTE (UNIX_BLOCKS);
245 REGISTER_ATTRIBUTE (UNIX_IS_MOUNTPOINT);
246 REGISTER_ATTRIBUTE (DOS_IS_ARCHIVE);
247 REGISTER_ATTRIBUTE (DOS_IS_SYSTEM);
248 REGISTER_ATTRIBUTE (OWNER_USER);
249 REGISTER_ATTRIBUTE (OWNER_USER_REAL);
250 REGISTER_ATTRIBUTE (OWNER_GROUP);
251 REGISTER_ATTRIBUTE (THUMBNAIL_PATH);
252 REGISTER_ATTRIBUTE (THUMBNAILING_FAILED);
253 REGISTER_ATTRIBUTE (PREVIEW_ICON);
254 REGISTER_ATTRIBUTE (FILESYSTEM_SIZE);
255 REGISTER_ATTRIBUTE (FILESYSTEM_FREE);
256 REGISTER_ATTRIBUTE (FILESYSTEM_TYPE);
257 REGISTER_ATTRIBUTE (FILESYSTEM_READONLY);
258 REGISTER_ATTRIBUTE (FILESYSTEM_USE_PREVIEW);
259 REGISTER_ATTRIBUTE (GVFS_BACKEND);
260 REGISTER_ATTRIBUTE (SELINUX_CONTEXT);
261 REGISTER_ATTRIBUTE (TRASH_ITEM_COUNT);
262 REGISTER_ATTRIBUTE (TRASH_ORIG_PATH);
263 REGISTER_ATTRIBUTE (TRASH_DELETION_DATE);
265 #undef REGISTER_ATTRIBUTE
268 static guint32
269 lookup_namespace (const char *namespace)
271 NSInfo *ns_info;
272 guint32 id;
274 G_LOCK (attribute_hash);
276 ensure_attribute_hash ();
278 ns_info = _lookup_namespace (namespace);
279 id = 0;
280 if (ns_info)
281 id = ns_info->id;
283 G_UNLOCK (attribute_hash);
285 return id;
288 static char *
289 get_attribute_for_id (int attribute)
291 char *s;
292 G_LOCK (attribute_hash);
293 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
294 G_UNLOCK (attribute_hash);
295 return s;
298 static guint32
299 lookup_attribute (const char *attribute)
301 guint32 attr_id;
303 G_LOCK (attribute_hash);
304 ensure_attribute_hash ();
306 attr_id = _lookup_attribute (attribute);
308 G_UNLOCK (attribute_hash);
310 return attr_id;
313 static void
314 g_file_info_finalize (GObject *object)
316 GFileInfo *info;
317 int i;
318 GFileAttribute *attrs;
320 info = G_FILE_INFO (object);
322 attrs = (GFileAttribute *)info->attributes->data;
323 for (i = 0; i < info->attributes->len; i++)
324 _g_file_attribute_value_clear (&attrs[i].value);
325 g_array_free (info->attributes, TRUE);
327 if (info->mask != NO_ATTRIBUTE_MASK)
328 g_file_attribute_matcher_unref (info->mask);
330 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
333 static void
334 g_file_info_class_init (GFileInfoClass *klass)
336 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
338 gobject_class->finalize = g_file_info_finalize;
341 static void
342 g_file_info_init (GFileInfo *info)
344 info->mask = NO_ATTRIBUTE_MASK;
345 info->attributes = g_array_new (FALSE, FALSE,
346 sizeof (GFileAttribute));
350 * g_file_info_new:
352 * Creates a new file info structure.
354 * Returns: a #GFileInfo.
356 GFileInfo *
357 g_file_info_new (void)
359 return g_object_new (G_TYPE_FILE_INFO, NULL);
363 * g_file_info_copy_into:
364 * @src_info: source to copy attributes from.
365 * @dest_info: destination to copy attributes to.
367 * Copies all of the <link linkend="gio-GFileAttribute">GFileAttribute</link>s
368 * from @src_info to @dest_info.
370 void
371 g_file_info_copy_into (GFileInfo *src_info,
372 GFileInfo *dest_info)
374 GFileAttribute *source, *dest;
375 int i;
377 g_return_if_fail (G_IS_FILE_INFO (src_info));
378 g_return_if_fail (G_IS_FILE_INFO (dest_info));
380 dest = (GFileAttribute *)dest_info->attributes->data;
381 for (i = 0; i < dest_info->attributes->len; i++)
382 _g_file_attribute_value_clear (&dest[i].value);
384 g_array_set_size (dest_info->attributes,
385 src_info->attributes->len);
387 source = (GFileAttribute *)src_info->attributes->data;
388 dest = (GFileAttribute *)dest_info->attributes->data;
390 for (i = 0; i < src_info->attributes->len; i++)
392 dest[i].attribute = source[i].attribute;
393 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
394 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
397 if (dest_info->mask != NO_ATTRIBUTE_MASK)
398 g_file_attribute_matcher_unref (dest_info->mask);
400 if (src_info->mask == NO_ATTRIBUTE_MASK)
401 dest_info->mask = NO_ATTRIBUTE_MASK;
402 else
403 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
407 * g_file_info_dup:
408 * @other: a #GFileInfo.
410 * Duplicates a file info structure.
412 * Returns: (transfer full): a duplicate #GFileInfo of @other.
414 GFileInfo *
415 g_file_info_dup (GFileInfo *other)
417 GFileInfo *new;
419 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
421 new = g_file_info_new ();
422 g_file_info_copy_into (other, new);
423 return new;
427 * g_file_info_set_attribute_mask:
428 * @info: a #GFileInfo.
429 * @mask: a #GFileAttributeMatcher.
431 * Sets @mask on @info to match specific attribute types.
433 void
434 g_file_info_set_attribute_mask (GFileInfo *info,
435 GFileAttributeMatcher *mask)
437 GFileAttribute *attr;
438 int i;
440 g_return_if_fail (G_IS_FILE_INFO (info));
442 if (mask != info->mask)
444 if (info->mask != NO_ATTRIBUTE_MASK)
445 g_file_attribute_matcher_unref (info->mask);
446 info->mask = g_file_attribute_matcher_ref (mask);
448 /* Remove non-matching attributes */
449 for (i = 0; i < info->attributes->len; i++)
451 attr = &g_array_index (info->attributes, GFileAttribute, i);
452 if (!_g_file_attribute_matcher_matches_id (mask,
453 attr->attribute))
455 _g_file_attribute_value_clear (&attr->value);
456 g_array_remove_index (info->attributes, i);
457 i--;
464 * g_file_info_unset_attribute_mask:
465 * @info: #GFileInfo.
467 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
468 * is set.
470 void
471 g_file_info_unset_attribute_mask (GFileInfo *info)
473 g_return_if_fail (G_IS_FILE_INFO (info));
475 if (info->mask != NO_ATTRIBUTE_MASK)
476 g_file_attribute_matcher_unref (info->mask);
477 info->mask = NO_ATTRIBUTE_MASK;
481 * g_file_info_clear_status:
482 * @info: a #GFileInfo.
484 * Clears the status information from @info.
486 void
487 g_file_info_clear_status (GFileInfo *info)
489 GFileAttribute *attrs;
490 int i;
492 g_return_if_fail (G_IS_FILE_INFO (info));
494 attrs = (GFileAttribute *)info->attributes->data;
495 for (i = 0; i < info->attributes->len; i++)
496 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
499 static int
500 g_file_info_find_place (GFileInfo *info,
501 guint32 attribute)
503 int min, max, med;
504 GFileAttribute *attrs;
505 /* Binary search for the place where attribute would be, if it's
506 in the array */
508 min = 0;
509 max = info->attributes->len;
511 attrs = (GFileAttribute *)info->attributes->data;
513 while (min < max)
515 med = min + (max - min) / 2;
516 if (attrs[med].attribute == attribute)
518 min = med;
519 break;
521 else if (attrs[med].attribute < attribute)
522 min = med + 1;
523 else /* attrs[med].attribute > attribute */
524 max = med;
527 return min;
530 static GFileAttributeValue *
531 g_file_info_find_value (GFileInfo *info,
532 guint32 attr_id)
534 GFileAttribute *attrs;
535 int i;
537 i = g_file_info_find_place (info, attr_id);
538 attrs = (GFileAttribute *)info->attributes->data;
539 if (i < info->attributes->len &&
540 attrs[i].attribute == attr_id)
541 return &attrs[i].value;
543 return NULL;
546 static GFileAttributeValue *
547 g_file_info_find_value_by_name (GFileInfo *info,
548 const char *attribute)
550 guint32 attr_id;
552 attr_id = lookup_attribute (attribute);
553 return g_file_info_find_value (info, attr_id);
557 * g_file_info_has_attribute:
558 * @info: a #GFileInfo.
559 * @attribute: a file attribute key.
561 * Checks if a file info structure has an attribute named @attribute.
563 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
564 * %FALSE otherwise.
566 gboolean
567 g_file_info_has_attribute (GFileInfo *info,
568 const char *attribute)
570 GFileAttributeValue *value;
572 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
573 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
575 value = g_file_info_find_value_by_name (info, attribute);
576 return value != NULL;
580 * g_file_info_has_namespace:
581 * @info: a #GFileInfo.
582 * @name_space: a file attribute namespace.
584 * Checks if a file info structure has an attribute in the
585 * specified @name_space.
587 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
588 * %FALSE otherwise.
590 * Since: 2.22
592 gboolean
593 g_file_info_has_namespace (GFileInfo *info,
594 const char *name_space)
596 GFileAttribute *attrs;
597 guint32 ns_id;
598 int i;
600 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
601 g_return_val_if_fail (name_space != NULL, FALSE);
603 ns_id = lookup_namespace (name_space);
605 attrs = (GFileAttribute *)info->attributes->data;
606 for (i = 0; i < info->attributes->len; i++)
608 if (GET_NS (attrs[i].attribute) == ns_id)
609 return TRUE;
612 return FALSE;
616 * g_file_info_list_attributes:
617 * @info: a #GFileInfo.
618 * @name_space: a file attribute key's namespace.
620 * Lists the file info structure's attributes.
622 * Returns: (array zero-terminated=1) (transfer full): a null-terminated array of strings of all of the
623 * possible attribute types for the given @name_space, or
624 * %NULL on error.
626 char **
627 g_file_info_list_attributes (GFileInfo *info,
628 const char *name_space)
630 GPtrArray *names;
631 GFileAttribute *attrs;
632 guint32 attribute;
633 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
634 int i;
636 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
638 names = g_ptr_array_new ();
639 attrs = (GFileAttribute *)info->attributes->data;
640 for (i = 0; i < info->attributes->len; i++)
642 attribute = attrs[i].attribute;
643 if (ns_id == 0 || GET_NS (attribute) == ns_id)
644 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
647 /* NULL terminate */
648 g_ptr_array_add (names, NULL);
650 return (char **)g_ptr_array_free (names, FALSE);
654 * g_file_info_get_attribute_type:
655 * @info: a #GFileInfo.
656 * @attribute: a file attribute key.
658 * Gets the attribute type for an attribute key.
660 * Returns: a #GFileAttributeType for the given @attribute, or
661 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
663 GFileAttributeType
664 g_file_info_get_attribute_type (GFileInfo *info,
665 const char *attribute)
667 GFileAttributeValue *value;
669 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
670 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
672 value = g_file_info_find_value_by_name (info, attribute);
673 if (value)
674 return value->type;
675 else
676 return G_FILE_ATTRIBUTE_TYPE_INVALID;
680 * g_file_info_remove_attribute:
681 * @info: a #GFileInfo.
682 * @attribute: a file attribute key.
684 * Removes all cases of @attribute from @info if it exists.
686 void
687 g_file_info_remove_attribute (GFileInfo *info,
688 const char *attribute)
690 guint32 attr_id;
691 GFileAttribute *attrs;
692 int i;
694 g_return_if_fail (G_IS_FILE_INFO (info));
695 g_return_if_fail (attribute != NULL && *attribute != '\0');
697 attr_id = lookup_attribute (attribute);
699 i = g_file_info_find_place (info, attr_id);
700 attrs = (GFileAttribute *)info->attributes->data;
701 if (i < info->attributes->len &&
702 attrs[i].attribute == attr_id)
704 _g_file_attribute_value_clear (&attrs[i].value);
705 g_array_remove_index (info->attributes, i);
710 * g_file_info_get_attribute_data:
711 * @info: a #GFileInfo
712 * @attribute: a file attribute key
713 * @type: (out) (allow-none): return location for the attribute type, or %NULL
714 * @value_pp: (out) (allow-none): return location for the attribute value, or %NULL
715 * @status: (out) (allow-none): return location for the attribute status, or %NULL
717 * Gets the attribute type, value and status for an attribute key.
719 * Returns: (transfer none): %TRUE if @info has an attribute named @attribute,
720 * %FALSE otherwise.
722 gboolean
723 g_file_info_get_attribute_data (GFileInfo *info,
724 const char *attribute,
725 GFileAttributeType *type,
726 gpointer *value_pp,
727 GFileAttributeStatus *status)
729 GFileAttributeValue *value;
731 value = g_file_info_find_value_by_name (info, attribute);
732 if (value == NULL)
733 return FALSE;
735 if (status)
736 *status = value->status;
738 if (type)
739 *type = value->type;
741 if (value_pp)
742 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
744 return TRUE;
748 * g_file_info_get_attribute_status:
749 * @info: a #GFileInfo
750 * @attribute: a file attribute key
752 * Gets the attribute status for an attribute key.
754 * Returns: a #GFileAttributeStatus for the given @attribute, or
755 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
758 GFileAttributeStatus
759 g_file_info_get_attribute_status (GFileInfo *info,
760 const char *attribute)
762 GFileAttributeValue *val;
764 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
765 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
767 val = g_file_info_find_value_by_name (info, attribute);
768 if (val)
769 return val->status;
771 return G_FILE_ATTRIBUTE_STATUS_UNSET;
775 * g_file_info_set_attribute_status:
776 * @info: a #GFileInfo
777 * @attribute: a file attribute key
778 * @status: a #GFileAttributeStatus
780 * Sets the attribute status for an attribute key. This is only
781 * needed by external code that implement g_file_set_attributes_from_info()
782 * or similar functions.
784 * The attribute must exist in @info for this to work. Otherwise %FALSE
785 * is returned and @info is unchanged.
787 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
789 * Since: 2.22
791 gboolean
792 g_file_info_set_attribute_status (GFileInfo *info,
793 const char *attribute,
794 GFileAttributeStatus status)
796 GFileAttributeValue *val;
798 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
799 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
801 val = g_file_info_find_value_by_name (info, attribute);
802 if (val)
804 val->status = status;
805 return TRUE;
808 return FALSE;
811 GFileAttributeValue *
812 _g_file_info_get_attribute_value (GFileInfo *info,
813 const char *attribute)
816 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
817 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
819 return g_file_info_find_value_by_name (info, attribute);
823 * g_file_info_get_attribute_as_string:
824 * @info: a #GFileInfo.
825 * @attribute: a file attribute key.
827 * Gets the value of a attribute, formated as a string.
828 * This escapes things as needed to make the string valid
829 * utf8.
831 * Returns: a UTF-8 string associated with the given @attribute.
832 * When you're done with the string it must be freed with g_free().
834 char *
835 g_file_info_get_attribute_as_string (GFileInfo *info,
836 const char *attribute)
838 GFileAttributeValue *val;
839 val = _g_file_info_get_attribute_value (info, attribute);
840 if (val)
841 return _g_file_attribute_value_as_string (val);
842 return NULL;
847 * g_file_info_get_attribute_object:
848 * @info: a #GFileInfo.
849 * @attribute: a file attribute key.
851 * Gets the value of a #GObject attribute. If the attribute does
852 * not contain a #GObject, %NULL will be returned.
854 * Returns: (transfer none): a #GObject associated with the given @attribute, or
855 * %NULL otherwise.
857 GObject *
858 g_file_info_get_attribute_object (GFileInfo *info,
859 const char *attribute)
861 GFileAttributeValue *value;
863 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
864 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
866 value = g_file_info_find_value_by_name (info, attribute);
867 return _g_file_attribute_value_get_object (value);
871 * g_file_info_get_attribute_string:
872 * @info: a #GFileInfo.
873 * @attribute: a file attribute key.
875 * Gets the value of a string attribute. If the attribute does
876 * not contain a string, %NULL will be returned.
878 * Returns: the contents of the @attribute value as a UTF-8 string, or
879 * %NULL otherwise.
881 const char *
882 g_file_info_get_attribute_string (GFileInfo *info,
883 const char *attribute)
885 GFileAttributeValue *value;
887 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
888 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
890 value = g_file_info_find_value_by_name (info, attribute);
891 return _g_file_attribute_value_get_string (value);
895 * g_file_info_get_attribute_byte_string:
896 * @info: a #GFileInfo.
897 * @attribute: a file attribute key.
899 * Gets the value of a byte string attribute. If the attribute does
900 * not contain a byte string, %NULL will be returned.
902 * Returns: the contents of the @attribute value as a byte string, or
903 * %NULL otherwise.
905 const char *
906 g_file_info_get_attribute_byte_string (GFileInfo *info,
907 const char *attribute)
909 GFileAttributeValue *value;
911 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
912 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
914 value = g_file_info_find_value_by_name (info, attribute);
915 return _g_file_attribute_value_get_byte_string (value);
919 * g_file_info_get_attribute_stringv:
920 * @info: a #GFileInfo.
921 * @attribute: a file attribute key.
923 * Gets the value of a stringv attribute. If the attribute does
924 * not contain a stringv, %NULL will be returned.
926 * Returns: (transfer none): the contents of the @attribute value as a stringv, or
927 * %NULL otherwise. Do not free. These returned strings are UTF-8.
929 * Since: 2.22
931 char **
932 g_file_info_get_attribute_stringv (GFileInfo *info,
933 const char *attribute)
935 GFileAttributeValue *value;
937 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
938 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
940 value = g_file_info_find_value_by_name (info, attribute);
941 return _g_file_attribute_value_get_stringv (value);
945 * g_file_info_get_attribute_boolean:
946 * @info: a #GFileInfo.
947 * @attribute: a file attribute key.
949 * Gets the value of a boolean attribute. If the attribute does not
950 * contain a boolean value, %FALSE will be returned.
952 * Returns: the boolean value contained within the attribute.
954 gboolean
955 g_file_info_get_attribute_boolean (GFileInfo *info,
956 const char *attribute)
958 GFileAttributeValue *value;
960 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
961 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
963 value = g_file_info_find_value_by_name (info, attribute);
964 return _g_file_attribute_value_get_boolean (value);
968 * g_file_info_get_attribute_uint32:
969 * @info: a #GFileInfo.
970 * @attribute: a file attribute key.
972 * Gets an unsigned 32-bit integer contained within the attribute. If the
973 * attribute does not contain an unsigned 32-bit integer, or is invalid,
974 * 0 will be returned.
976 * Returns: an unsigned 32-bit integer from the attribute.
978 guint32
979 g_file_info_get_attribute_uint32 (GFileInfo *info,
980 const char *attribute)
982 GFileAttributeValue *value;
984 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
985 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
987 value = g_file_info_find_value_by_name (info, attribute);
988 return _g_file_attribute_value_get_uint32 (value);
992 * g_file_info_get_attribute_int32:
993 * @info: a #GFileInfo.
994 * @attribute: a file attribute key.
996 * Gets a signed 32-bit integer contained within the attribute. If the
997 * attribute does not contain a signed 32-bit integer, or is invalid,
998 * 0 will be returned.
1000 * Returns: a signed 32-bit integer from the attribute.
1002 gint32
1003 g_file_info_get_attribute_int32 (GFileInfo *info,
1004 const char *attribute)
1006 GFileAttributeValue *value;
1008 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1009 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1011 value = g_file_info_find_value_by_name (info, attribute);
1012 return _g_file_attribute_value_get_int32 (value);
1016 * g_file_info_get_attribute_uint64:
1017 * @info: a #GFileInfo.
1018 * @attribute: a file attribute key.
1020 * Gets a unsigned 64-bit integer contained within the attribute. If the
1021 * attribute does not contain an unsigned 64-bit integer, or is invalid,
1022 * 0 will be returned.
1024 * Returns: a unsigned 64-bit integer from the attribute.
1026 guint64
1027 g_file_info_get_attribute_uint64 (GFileInfo *info,
1028 const char *attribute)
1030 GFileAttributeValue *value;
1032 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1033 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1035 value = g_file_info_find_value_by_name (info, attribute);
1036 return _g_file_attribute_value_get_uint64 (value);
1040 * g_file_info_get_attribute_int64:
1041 * @info: a #GFileInfo.
1042 * @attribute: a file attribute key.
1044 * Gets a signed 64-bit integer contained within the attribute. If the
1045 * attribute does not contain an signed 64-bit integer, or is invalid,
1046 * 0 will be returned.
1048 * Returns: a signed 64-bit integer from the attribute.
1050 gint64
1051 g_file_info_get_attribute_int64 (GFileInfo *info,
1052 const char *attribute)
1054 GFileAttributeValue *value;
1056 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1057 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1059 value = g_file_info_find_value_by_name (info, attribute);
1060 return _g_file_attribute_value_get_int64 (value);
1063 static GFileAttributeValue *
1064 g_file_info_create_value (GFileInfo *info,
1065 guint32 attr_id)
1067 GFileAttribute *attrs;
1068 int i;
1070 if (info->mask != NO_ATTRIBUTE_MASK &&
1071 !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
1072 return NULL;
1074 i = g_file_info_find_place (info, attr_id);
1076 attrs = (GFileAttribute *)info->attributes->data;
1077 if (i < info->attributes->len &&
1078 attrs[i].attribute == attr_id)
1079 return &attrs[i].value;
1080 else
1082 GFileAttribute attr = { 0 };
1083 attr.attribute = attr_id;
1084 g_array_insert_val (info->attributes, i, attr);
1086 attrs = (GFileAttribute *)info->attributes->data;
1087 return &attrs[i].value;
1091 void
1092 _g_file_info_set_attribute_by_id (GFileInfo *info,
1093 guint32 attribute,
1094 GFileAttributeType type,
1095 gpointer value_p)
1097 GFileAttributeValue *value;
1099 value = g_file_info_create_value (info, attribute);
1101 if (value)
1102 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1106 * g_file_info_set_attribute:
1107 * @info: a #GFileInfo.
1108 * @attribute: a file attribute key.
1109 * @type: a #GFileAttributeType
1110 * @value_p: pointer to the value
1112 * Sets the @attribute to contain the given value, if possible.
1114 void
1115 g_file_info_set_attribute (GFileInfo *info,
1116 const char *attribute,
1117 GFileAttributeType type,
1118 gpointer value_p)
1120 g_return_if_fail (G_IS_FILE_INFO (info));
1121 g_return_if_fail (attribute != NULL && *attribute != '\0');
1123 _g_file_info_set_attribute_by_id (info, lookup_attribute (attribute), type, value_p);
1126 void
1127 _g_file_info_set_attribute_object_by_id (GFileInfo *info,
1128 guint32 attribute,
1129 GObject *attr_value)
1131 GFileAttributeValue *value;
1133 value = g_file_info_create_value (info, attribute);
1134 if (value)
1135 _g_file_attribute_value_set_object (value, attr_value);
1139 * g_file_info_set_attribute_object:
1140 * @info: a #GFileInfo.
1141 * @attribute: a file attribute key.
1142 * @attr_value: a #GObject.
1144 * Sets the @attribute to contain the given @attr_value,
1145 * if possible.
1147 void
1148 g_file_info_set_attribute_object (GFileInfo *info,
1149 const char *attribute,
1150 GObject *attr_value)
1152 g_return_if_fail (G_IS_FILE_INFO (info));
1153 g_return_if_fail (attribute != NULL && *attribute != '\0');
1154 g_return_if_fail (G_IS_OBJECT (attr_value));
1156 _g_file_info_set_attribute_object_by_id (info,
1157 lookup_attribute (attribute),
1158 attr_value);
1161 void
1162 _g_file_info_set_attribute_stringv_by_id (GFileInfo *info,
1163 guint32 attribute,
1164 char **attr_value)
1166 GFileAttributeValue *value;
1168 value = g_file_info_create_value (info, attribute);
1169 if (value)
1170 _g_file_attribute_value_set_stringv (value, attr_value);
1174 * g_file_info_set_attribute_stringv:
1175 * @info: a #GFileInfo.
1176 * @attribute: a file attribute key
1177 * @attr_value: (array) (element-type utf8): a %NULL terminated array of UTF-8 strings.
1179 * Sets the @attribute to contain the given @attr_value,
1180 * if possible.
1182 * Sinze: 2.22
1184 void
1185 g_file_info_set_attribute_stringv (GFileInfo *info,
1186 const char *attribute,
1187 char **attr_value)
1189 g_return_if_fail (G_IS_FILE_INFO (info));
1190 g_return_if_fail (attribute != NULL && *attribute != '\0');
1191 g_return_if_fail (attr_value != NULL);
1193 _g_file_info_set_attribute_stringv_by_id (info,
1194 lookup_attribute (attribute),
1195 attr_value);
1198 void
1199 _g_file_info_set_attribute_string_by_id (GFileInfo *info,
1200 guint32 attribute,
1201 const char *attr_value)
1203 GFileAttributeValue *value;
1205 value = g_file_info_create_value (info, attribute);
1206 if (value)
1207 _g_file_attribute_value_set_string (value, attr_value);
1211 * g_file_info_set_attribute_string:
1212 * @info: a #GFileInfo.
1213 * @attribute: a file attribute key.
1214 * @attr_value: a UTF-8 string.
1216 * Sets the @attribute to contain the given @attr_value,
1217 * if possible.
1219 void
1220 g_file_info_set_attribute_string (GFileInfo *info,
1221 const char *attribute,
1222 const char *attr_value)
1224 g_return_if_fail (G_IS_FILE_INFO (info));
1225 g_return_if_fail (attribute != NULL && *attribute != '\0');
1226 g_return_if_fail (attr_value != NULL);
1228 _g_file_info_set_attribute_string_by_id (info,
1229 lookup_attribute (attribute),
1230 attr_value);
1233 void
1234 _g_file_info_set_attribute_byte_string_by_id (GFileInfo *info,
1235 guint32 attribute,
1236 const char *attr_value)
1238 GFileAttributeValue *value;
1240 value = g_file_info_create_value (info, attribute);
1241 if (value)
1242 _g_file_attribute_value_set_byte_string (value, attr_value);
1246 * g_file_info_set_attribute_byte_string:
1247 * @info: a #GFileInfo.
1248 * @attribute: a file attribute key.
1249 * @attr_value: a byte string.
1251 * Sets the @attribute to contain the given @attr_value,
1252 * if possible.
1254 void
1255 g_file_info_set_attribute_byte_string (GFileInfo *info,
1256 const char *attribute,
1257 const char *attr_value)
1259 g_return_if_fail (G_IS_FILE_INFO (info));
1260 g_return_if_fail (attribute != NULL && *attribute != '\0');
1261 g_return_if_fail (attr_value != NULL);
1263 _g_file_info_set_attribute_byte_string_by_id (info,
1264 lookup_attribute (attribute),
1265 attr_value);
1268 void
1269 _g_file_info_set_attribute_boolean_by_id (GFileInfo *info,
1270 guint32 attribute,
1271 gboolean attr_value)
1273 GFileAttributeValue *value;
1275 value = g_file_info_create_value (info, attribute);
1276 if (value)
1277 _g_file_attribute_value_set_boolean (value, attr_value);
1281 * g_file_info_set_attribute_boolean:
1282 * @info: a #GFileInfo.
1283 * @attribute: a file attribute key.
1284 * @attr_value: a boolean value.
1286 * Sets the @attribute to contain the given @attr_value,
1287 * if possible.
1289 void
1290 g_file_info_set_attribute_boolean (GFileInfo *info,
1291 const char *attribute,
1292 gboolean attr_value)
1294 g_return_if_fail (G_IS_FILE_INFO (info));
1295 g_return_if_fail (attribute != NULL && *attribute != '\0');
1297 _g_file_info_set_attribute_boolean_by_id (info,
1298 lookup_attribute (attribute),
1299 attr_value);
1302 void
1303 _g_file_info_set_attribute_uint32_by_id (GFileInfo *info,
1304 guint32 attribute,
1305 guint32 attr_value)
1307 GFileAttributeValue *value;
1309 value = g_file_info_create_value (info, attribute);
1310 if (value)
1311 _g_file_attribute_value_set_uint32 (value, attr_value);
1315 * g_file_info_set_attribute_uint32:
1316 * @info: a #GFileInfo.
1317 * @attribute: a file attribute key.
1318 * @attr_value: an unsigned 32-bit integer.
1320 * Sets the @attribute to contain the given @attr_value,
1321 * if possible.
1323 void
1324 g_file_info_set_attribute_uint32 (GFileInfo *info,
1325 const char *attribute,
1326 guint32 attr_value)
1328 g_return_if_fail (G_IS_FILE_INFO (info));
1329 g_return_if_fail (attribute != NULL && *attribute != '\0');
1331 _g_file_info_set_attribute_uint32_by_id (info,
1332 lookup_attribute (attribute),
1333 attr_value);
1336 void
1337 _g_file_info_set_attribute_int32_by_id (GFileInfo *info,
1338 guint32 attribute,
1339 gint32 attr_value)
1341 GFileAttributeValue *value;
1343 value = g_file_info_create_value (info, attribute);
1344 if (value)
1345 _g_file_attribute_value_set_int32 (value, attr_value);
1349 * g_file_info_set_attribute_int32:
1350 * @info: a #GFileInfo.
1351 * @attribute: a file attribute key.
1352 * @attr_value: a signed 32-bit integer
1354 * Sets the @attribute to contain the given @attr_value,
1355 * if possible.
1357 void
1358 g_file_info_set_attribute_int32 (GFileInfo *info,
1359 const char *attribute,
1360 gint32 attr_value)
1362 g_return_if_fail (G_IS_FILE_INFO (info));
1363 g_return_if_fail (attribute != NULL && *attribute != '\0');
1365 _g_file_info_set_attribute_int32_by_id (info,
1366 lookup_attribute (attribute),
1367 attr_value);
1370 void
1371 _g_file_info_set_attribute_uint64_by_id (GFileInfo *info,
1372 guint32 attribute,
1373 guint64 attr_value)
1375 GFileAttributeValue *value;
1377 value = g_file_info_create_value (info, attribute);
1378 if (value)
1379 _g_file_attribute_value_set_uint64 (value, attr_value);
1383 * g_file_info_set_attribute_uint64:
1384 * @info: a #GFileInfo.
1385 * @attribute: a file attribute key.
1386 * @attr_value: an unsigned 64-bit integer.
1388 * Sets the @attribute to contain the given @attr_value,
1389 * if possible.
1391 void
1392 g_file_info_set_attribute_uint64 (GFileInfo *info,
1393 const char *attribute,
1394 guint64 attr_value)
1396 g_return_if_fail (G_IS_FILE_INFO (info));
1397 g_return_if_fail (attribute != NULL && *attribute != '\0');
1399 _g_file_info_set_attribute_uint64_by_id (info,
1400 lookup_attribute (attribute),
1401 attr_value);
1404 void
1405 _g_file_info_set_attribute_int64_by_id (GFileInfo *info,
1406 guint32 attribute,
1407 gint64 attr_value)
1409 GFileAttributeValue *value;
1411 value = g_file_info_create_value (info, attribute);
1412 if (value)
1413 _g_file_attribute_value_set_int64 (value, attr_value);
1417 * g_file_info_set_attribute_int64:
1418 * @info: a #GFileInfo.
1419 * @attribute: attribute name to set.
1420 * @attr_value: int64 value to set attribute to.
1422 * Sets the @attribute to contain the given @attr_value,
1423 * if possible.
1426 void
1427 g_file_info_set_attribute_int64 (GFileInfo *info,
1428 const char *attribute,
1429 gint64 attr_value)
1431 g_return_if_fail (G_IS_FILE_INFO (info));
1432 g_return_if_fail (attribute != NULL && *attribute != '\0');
1434 _g_file_info_set_attribute_int64_by_id (info,
1435 lookup_attribute (attribute),
1436 attr_value);
1439 /* Helper getters */
1441 * g_file_info_get_file_type:
1442 * @info: a #GFileInfo.
1444 * Gets a file's type (whether it is a regular file, symlink, etc).
1445 * This is different from the file's content type, see g_file_info_get_content_type().
1447 * Returns: a #GFileType for the given file.
1449 GFileType
1450 g_file_info_get_file_type (GFileInfo *info)
1452 static guint32 attr = 0;
1453 GFileAttributeValue *value;
1455 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1457 if (attr == 0)
1458 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1460 value = g_file_info_find_value (info, attr);
1461 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1465 * g_file_info_get_is_hidden:
1466 * @info: a #GFileInfo.
1468 * Checks if a file is hidden.
1470 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1472 gboolean
1473 g_file_info_get_is_hidden (GFileInfo *info)
1475 static guint32 attr = 0;
1476 GFileAttributeValue *value;
1478 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1480 if (attr == 0)
1481 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1483 value = g_file_info_find_value (info, attr);
1484 return (GFileType)_g_file_attribute_value_get_boolean (value);
1488 * g_file_info_get_is_backup:
1489 * @info: a #GFileInfo.
1491 * Checks if a file is a backup file.
1493 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1495 gboolean
1496 g_file_info_get_is_backup (GFileInfo *info)
1498 static guint32 attr = 0;
1499 GFileAttributeValue *value;
1501 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1503 if (attr == 0)
1504 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1506 value = g_file_info_find_value (info, attr);
1507 return (GFileType)_g_file_attribute_value_get_boolean (value);
1511 * g_file_info_get_is_symlink:
1512 * @info: a #GFileInfo.
1514 * Checks if a file is a symlink.
1516 * Returns: %TRUE if the given @info is a symlink.
1518 gboolean
1519 g_file_info_get_is_symlink (GFileInfo *info)
1521 static guint32 attr = 0;
1522 GFileAttributeValue *value;
1524 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1526 if (attr == 0)
1527 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1529 value = g_file_info_find_value (info, attr);
1530 return (GFileType)_g_file_attribute_value_get_boolean (value);
1534 * g_file_info_get_name:
1535 * @info: a #GFileInfo.
1537 * Gets the name for a file.
1539 * Returns: a string containing the file name.
1541 const char *
1542 g_file_info_get_name (GFileInfo *info)
1544 static guint32 attr = 0;
1545 GFileAttributeValue *value;
1547 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1549 if (attr == 0)
1550 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1552 value = g_file_info_find_value (info, attr);
1553 return _g_file_attribute_value_get_byte_string (value);
1557 * g_file_info_get_display_name:
1558 * @info: a #GFileInfo.
1560 * Gets a display name for a file.
1562 * Returns: a string containing the display name.
1564 const char *
1565 g_file_info_get_display_name (GFileInfo *info)
1567 static guint32 attr = 0;
1568 GFileAttributeValue *value;
1570 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1572 if (attr == 0)
1573 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1575 value = g_file_info_find_value (info, attr);
1576 return _g_file_attribute_value_get_string (value);
1580 * g_file_info_get_edit_name:
1581 * @info: a #GFileInfo.
1583 * Gets the edit name for a file.
1585 * Returns: a string containing the edit name.
1587 const char *
1588 g_file_info_get_edit_name (GFileInfo *info)
1590 static guint32 attr = 0;
1591 GFileAttributeValue *value;
1593 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1595 if (attr == 0)
1596 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1598 value = g_file_info_find_value (info, attr);
1599 return _g_file_attribute_value_get_string (value);
1603 * g_file_info_get_icon:
1604 * @info: a #GFileInfo.
1606 * Gets the icon for a file.
1608 * Returns: (transfer none): #GIcon for the given @info.
1610 GIcon *
1611 g_file_info_get_icon (GFileInfo *info)
1613 static guint32 attr = 0;
1614 GFileAttributeValue *value;
1615 GObject *obj;
1617 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1619 if (attr == 0)
1620 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1622 value = g_file_info_find_value (info, attr);
1623 obj = _g_file_attribute_value_get_object (value);
1624 if (G_IS_ICON (obj))
1625 return G_ICON (obj);
1626 return NULL;
1630 * g_file_info_get_content_type:
1631 * @info: a #GFileInfo.
1633 * Gets the file's content type.
1635 * Returns: a string containing the file's content type.
1637 const char *
1638 g_file_info_get_content_type (GFileInfo *info)
1640 static guint32 attr = 0;
1641 GFileAttributeValue *value;
1643 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1645 if (attr == 0)
1646 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1648 value = g_file_info_find_value (info, attr);
1649 return _g_file_attribute_value_get_string (value);
1653 * g_file_info_get_size:
1654 * @info: a #GFileInfo.
1656 * Gets the file's size.
1658 * Returns: a #goffset containing the file's size.
1660 goffset
1661 g_file_info_get_size (GFileInfo *info)
1663 static guint32 attr = 0;
1664 GFileAttributeValue *value;
1666 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1668 if (attr == 0)
1669 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1671 value = g_file_info_find_value (info, attr);
1672 return (goffset) _g_file_attribute_value_get_uint64 (value);
1676 * g_file_info_get_modification_time:
1677 * @info: a #GFileInfo.
1678 * @result: (out caller-allocates): a #GTimeVal.
1680 * Gets the modification time of the current @info and sets it
1681 * in @result.
1683 void
1684 g_file_info_get_modification_time (GFileInfo *info,
1685 GTimeVal *result)
1687 static guint32 attr_mtime = 0, attr_mtime_usec;
1688 GFileAttributeValue *value;
1690 g_return_if_fail (G_IS_FILE_INFO (info));
1691 g_return_if_fail (result != NULL);
1693 if (attr_mtime == 0)
1695 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1696 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1699 value = g_file_info_find_value (info, attr_mtime);
1700 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1701 value = g_file_info_find_value (info, attr_mtime_usec);
1702 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1706 * g_file_info_get_symlink_target:
1707 * @info: a #GFileInfo.
1709 * Gets the symlink target for a given #GFileInfo.
1711 * Returns: a string containing the symlink target.
1713 const char *
1714 g_file_info_get_symlink_target (GFileInfo *info)
1716 static guint32 attr = 0;
1717 GFileAttributeValue *value;
1719 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1721 if (attr == 0)
1722 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1724 value = g_file_info_find_value (info, attr);
1725 return _g_file_attribute_value_get_byte_string (value);
1729 * g_file_info_get_etag:
1730 * @info: a #GFileInfo.
1732 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1733 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1735 * Returns: a string containing the value of the "etag:value" attribute.
1737 const char *
1738 g_file_info_get_etag (GFileInfo *info)
1740 static guint32 attr = 0;
1741 GFileAttributeValue *value;
1743 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1745 if (attr == 0)
1746 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1748 value = g_file_info_find_value (info, attr);
1749 return _g_file_attribute_value_get_string (value);
1753 * g_file_info_get_sort_order:
1754 * @info: a #GFileInfo.
1756 * Gets the value of the sort_order attribute from the #GFileInfo.
1757 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1759 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1761 gint32
1762 g_file_info_get_sort_order (GFileInfo *info)
1764 static guint32 attr = 0;
1765 GFileAttributeValue *value;
1767 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1769 if (attr == 0)
1770 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1772 value = g_file_info_find_value (info, attr);
1773 return _g_file_attribute_value_get_int32 (value);
1776 /* Helper setters: */
1778 * g_file_info_set_file_type:
1779 * @info: a #GFileInfo.
1780 * @type: a #GFileType.
1782 * Sets the file type in a #GFileInfo to @type.
1783 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1785 void
1786 g_file_info_set_file_type (GFileInfo *info,
1787 GFileType type)
1789 static guint32 attr = 0;
1790 GFileAttributeValue *value;
1792 g_return_if_fail (G_IS_FILE_INFO (info));
1794 if (attr == 0)
1795 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1797 value = g_file_info_create_value (info, attr);
1798 if (value)
1799 _g_file_attribute_value_set_uint32 (value, type);
1803 * g_file_info_set_is_hidden:
1804 * @info: a #GFileInfo.
1805 * @is_hidden: a #gboolean.
1807 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1808 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1810 void
1811 g_file_info_set_is_hidden (GFileInfo *info,
1812 gboolean is_hidden)
1814 static guint32 attr = 0;
1815 GFileAttributeValue *value;
1817 g_return_if_fail (G_IS_FILE_INFO (info));
1819 if (attr == 0)
1820 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1822 value = g_file_info_create_value (info, attr);
1823 if (value)
1824 _g_file_attribute_value_set_boolean (value, is_hidden);
1828 * g_file_info_set_is_symlink:
1829 * @info: a #GFileInfo.
1830 * @is_symlink: a #gboolean.
1832 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1833 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1835 void
1836 g_file_info_set_is_symlink (GFileInfo *info,
1837 gboolean is_symlink)
1839 static guint32 attr = 0;
1840 GFileAttributeValue *value;
1842 g_return_if_fail (G_IS_FILE_INFO (info));
1844 if (attr == 0)
1845 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1847 value = g_file_info_create_value (info, attr);
1848 if (value)
1849 _g_file_attribute_value_set_boolean (value, is_symlink);
1853 * g_file_info_set_name:
1854 * @info: a #GFileInfo.
1855 * @name: a string containing a name.
1857 * Sets the name attribute for the current #GFileInfo.
1858 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1860 void
1861 g_file_info_set_name (GFileInfo *info,
1862 const char *name)
1864 static guint32 attr = 0;
1865 GFileAttributeValue *value;
1867 g_return_if_fail (G_IS_FILE_INFO (info));
1868 g_return_if_fail (name != NULL);
1870 if (attr == 0)
1871 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1873 value = g_file_info_create_value (info, attr);
1874 if (value)
1875 _g_file_attribute_value_set_byte_string (value, name);
1879 * g_file_info_set_display_name:
1880 * @info: a #GFileInfo.
1881 * @display_name: a string containing a display name.
1883 * Sets the display name for the current #GFileInfo.
1884 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1886 void
1887 g_file_info_set_display_name (GFileInfo *info,
1888 const char *display_name)
1890 static guint32 attr = 0;
1891 GFileAttributeValue *value;
1893 g_return_if_fail (G_IS_FILE_INFO (info));
1894 g_return_if_fail (display_name != NULL);
1896 if (attr == 0)
1897 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1899 value = g_file_info_create_value (info, attr);
1900 if (value)
1901 _g_file_attribute_value_set_string (value, display_name);
1905 * g_file_info_set_edit_name:
1906 * @info: a #GFileInfo.
1907 * @edit_name: a string containing an edit name.
1909 * Sets the edit name for the current file.
1910 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1912 void
1913 g_file_info_set_edit_name (GFileInfo *info,
1914 const char *edit_name)
1916 static guint32 attr = 0;
1917 GFileAttributeValue *value;
1919 g_return_if_fail (G_IS_FILE_INFO (info));
1920 g_return_if_fail (edit_name != NULL);
1922 if (attr == 0)
1923 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1925 value = g_file_info_create_value (info, attr);
1926 if (value)
1927 _g_file_attribute_value_set_string (value, edit_name);
1931 * g_file_info_set_icon:
1932 * @info: a #GFileInfo.
1933 * @icon: a #GIcon.
1935 * Sets the icon for a given #GFileInfo.
1936 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1938 void
1939 g_file_info_set_icon (GFileInfo *info,
1940 GIcon *icon)
1942 static guint32 attr = 0;
1943 GFileAttributeValue *value;
1945 g_return_if_fail (G_IS_FILE_INFO (info));
1946 g_return_if_fail (G_IS_ICON (icon));
1948 if (attr == 0)
1949 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1951 value = g_file_info_create_value (info, attr);
1952 if (value)
1953 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1957 * g_file_info_set_content_type:
1958 * @info: a #GFileInfo.
1959 * @content_type: a content type. See <link linkend="gio-GContentType">GContentType</link>.
1961 * Sets the content type attribute for a given #GFileInfo.
1962 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1964 void
1965 g_file_info_set_content_type (GFileInfo *info,
1966 const char *content_type)
1968 static guint32 attr = 0;
1969 GFileAttributeValue *value;
1971 g_return_if_fail (G_IS_FILE_INFO (info));
1972 g_return_if_fail (content_type != NULL);
1974 if (attr == 0)
1975 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1977 value = g_file_info_create_value (info, attr);
1978 if (value)
1979 _g_file_attribute_value_set_string (value, content_type);
1983 * g_file_info_set_size:
1984 * @info: a #GFileInfo.
1985 * @size: a #goffset containing the file's size.
1987 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1988 * to the given size.
1990 void
1991 g_file_info_set_size (GFileInfo *info,
1992 goffset size)
1994 static guint32 attr = 0;
1995 GFileAttributeValue *value;
1997 g_return_if_fail (G_IS_FILE_INFO (info));
1999 if (attr == 0)
2000 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
2002 value = g_file_info_create_value (info, attr);
2003 if (value)
2004 _g_file_attribute_value_set_uint64 (value, size);
2008 * g_file_info_set_modification_time
2009 * @info: a #GFileInfo.
2010 * @mtime: a #GTimeVal.
2012 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
2013 * info to the given time value.
2015 void
2016 g_file_info_set_modification_time (GFileInfo *info,
2017 GTimeVal *mtime)
2019 static guint32 attr_mtime = 0, attr_mtime_usec;
2020 GFileAttributeValue *value;
2022 g_return_if_fail (G_IS_FILE_INFO (info));
2023 g_return_if_fail (mtime != NULL);
2025 if (attr_mtime == 0)
2027 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
2028 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2031 value = g_file_info_create_value (info, attr_mtime);
2032 if (value)
2033 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
2034 value = g_file_info_create_value (info, attr_mtime_usec);
2035 if (value)
2036 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
2040 * g_file_info_set_symlink_target:
2041 * @info: a #GFileInfo.
2042 * @symlink_target: a static string containing a path to a symlink target.
2044 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
2045 * to the given symlink target.
2047 void
2048 g_file_info_set_symlink_target (GFileInfo *info,
2049 const char *symlink_target)
2051 static guint32 attr = 0;
2052 GFileAttributeValue *value;
2054 g_return_if_fail (G_IS_FILE_INFO (info));
2055 g_return_if_fail (symlink_target != NULL);
2057 if (attr == 0)
2058 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2060 value = g_file_info_create_value (info, attr);
2061 if (value)
2062 _g_file_attribute_value_set_byte_string (value, symlink_target);
2066 * g_file_info_set_sort_order:
2067 * @info: a #GFileInfo.
2068 * @sort_order: a sort order integer.
2070 * Sets the sort order attribute in the file info structure. See
2071 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
2073 void
2074 g_file_info_set_sort_order (GFileInfo *info,
2075 gint32 sort_order)
2077 static guint32 attr = 0;
2078 GFileAttributeValue *value;
2080 g_return_if_fail (G_IS_FILE_INFO (info));
2082 if (attr == 0)
2083 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
2085 value = g_file_info_create_value (info, attr);
2086 if (value)
2087 _g_file_attribute_value_set_int32 (value, sort_order);
2091 typedef struct {
2092 guint32 id;
2093 guint32 mask;
2094 } SubMatcher;
2096 struct _GFileAttributeMatcher {
2097 gboolean all;
2098 gint ref;
2100 GArray *sub_matchers;
2102 /* Interator */
2103 guint32 iterator_ns;
2104 gint iterator_pos;
2107 G_DEFINE_BOXED_TYPE (GFileAttributeMatcher, g_file_attribute_matcher,
2108 g_file_attribute_matcher_ref,
2109 g_file_attribute_matcher_unref)
2111 static gint
2112 compare_sub_matchers (gconstpointer a,
2113 gconstpointer b)
2115 const SubMatcher *suba = a;
2116 const SubMatcher *subb = b;
2117 int diff;
2119 diff = suba->id - subb->id;
2121 if (diff)
2122 return diff;
2124 return suba->mask - subb->mask;
2127 static gboolean
2128 sub_matcher_matches (SubMatcher *matcher,
2129 SubMatcher *submatcher)
2131 if ((matcher->mask & submatcher->mask) != matcher->mask)
2132 return FALSE;
2134 return matcher->id == (submatcher->id & matcher->mask);
2137 /* Call this function after modifying a matcher.
2138 * It will ensure all the invariants other functions rely on.
2140 static GFileAttributeMatcher *
2141 matcher_optimize (GFileAttributeMatcher *matcher)
2143 SubMatcher *submatcher, *compare;
2144 guint i, j;
2146 /* remove sub_matchers if we match everything anyway */
2147 if (matcher->all)
2149 if (matcher->sub_matchers)
2151 g_array_free (matcher->sub_matchers, TRUE);
2152 matcher->sub_matchers = NULL;
2154 return matcher;
2157 if (matcher->sub_matchers->len == 0)
2159 g_file_attribute_matcher_unref (matcher);
2160 return NULL;
2163 /* sort sub_matchers by id (and then mask), so we can bsearch
2164 * and compare matchers in O(N) instead of O(N²) */
2165 g_array_sort (matcher->sub_matchers, compare_sub_matchers);
2167 /* remove duplicates and specific matches when we match the whole namespace */
2168 j = 0;
2169 compare = &g_array_index (matcher->sub_matchers, SubMatcher, j);
2171 for (i = 1; i < matcher->sub_matchers->len; i++)
2173 submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2174 if (sub_matcher_matches (compare, submatcher))
2175 continue;
2177 j++;
2178 compare++;
2180 if (j < i)
2181 *compare = *submatcher;
2184 g_array_set_size (matcher->sub_matchers, j + 1);
2186 return matcher;
2190 * g_file_attribute_matcher_new:
2191 * @attributes: an attribute string to match.
2193 * Creates a new file attribute matcher, which matches attributes
2194 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
2195 * counted structures, and are created with a reference count of 1. If
2196 * the number of references falls to 0, the #GFileAttributeMatcher is
2197 * automatically destroyed.
2199 * The @attribute string should be formatted with specific keys separated
2200 * from namespaces with a double colon. Several "namespace::key" strings may be
2201 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
2202 * The wildcard "*" may be used to match all keys and namespaces, or
2203 * "namespace::*" will match all keys in a given namespace.
2205 * Examples of strings to use:
2206 * <table>
2207 * <title>File Attribute Matcher strings and results</title>
2208 * <tgroup cols='2' align='left'><thead>
2209 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
2210 * <tbody>
2211 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
2212 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
2213 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
2214 * all keys in the unix namespace.</entry></row>
2215 * </tbody></tgroup>
2216 * </table>
2218 * Returns: a #GFileAttributeMatcher.
2220 GFileAttributeMatcher *
2221 g_file_attribute_matcher_new (const char *attributes)
2223 char **split;
2224 char *colon;
2225 int i;
2226 GFileAttributeMatcher *matcher;
2228 if (attributes == NULL || *attributes == '\0')
2229 return NULL;
2231 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2232 matcher->ref = 1;
2233 matcher->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2235 split = g_strsplit (attributes, ",", -1);
2237 for (i = 0; split[i] != NULL; i++)
2239 if (strcmp (split[i], "*") == 0)
2240 matcher->all = TRUE;
2241 else
2243 SubMatcher s;
2245 colon = strstr (split[i], "::");
2246 if (colon != NULL &&
2247 !(colon[2] == 0 ||
2248 (colon[2] == '*' &&
2249 colon[3] == 0)))
2251 s.id = lookup_attribute (split[i]);
2252 s.mask = 0xffffffff;
2254 else
2256 if (colon)
2257 *colon = 0;
2259 s.id = lookup_namespace (split[i]) << NS_POS;
2260 s.mask = NS_MASK << NS_POS;
2263 g_array_append_val (matcher->sub_matchers, s);
2267 g_strfreev (split);
2269 matcher = matcher_optimize (matcher);
2271 return matcher;
2275 * g_file_attribute_matcher_subtract:
2276 * @matcher: Matcher to subtract from
2277 * @subtract: The matcher to subtract
2279 * Subtracts all attributes of @subtract from @matcher and returns
2280 * a matcher that supports those attributes.
2282 * Note that currently it is not possible to remove a single
2283 * attribute when the @matcher matches the whole namespace - or remove
2284 * a namespace or attribute when the matcher matches everything. This
2285 * is a limitation of the current implementation, but may be fixed
2286 * in the future.
2288 * Returns: A file attribute matcher matching all attributes of
2289 * @matcher that are not matched by @subtract
2291 GFileAttributeMatcher *
2292 g_file_attribute_matcher_subtract (GFileAttributeMatcher *matcher,
2293 GFileAttributeMatcher *subtract)
2295 GFileAttributeMatcher *result;
2296 guint mi, si;
2297 SubMatcher *msub, *ssub;
2299 if (matcher == NULL)
2300 return NULL;
2301 if (subtract == NULL)
2302 return g_file_attribute_matcher_ref (matcher);
2303 if (subtract->all)
2304 return NULL;
2305 if (matcher->all)
2306 return g_file_attribute_matcher_ref (matcher);
2308 result = g_malloc0 (sizeof (GFileAttributeMatcher));
2309 result->ref = 1;
2310 result->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2312 si = 0;
2313 g_assert (subtract->sub_matchers->len > 0);
2314 ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);
2316 for (mi = 0; mi < matcher->sub_matchers->len; mi++)
2318 msub = &g_array_index (matcher->sub_matchers, SubMatcher, mi);
2320 retry:
2321 if (sub_matcher_matches (ssub, msub))
2322 continue;
2324 si++;
2325 if (si >= subtract->sub_matchers->len)
2326 break;
2328 ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);
2329 if (ssub->id <= msub->id)
2330 goto retry;
2332 g_array_append_val (result->sub_matchers, *msub);
2335 if (mi < matcher->sub_matchers->len)
2336 g_array_append_vals (result->sub_matchers,
2337 &g_array_index (matcher->sub_matchers, SubMatcher, mi),
2338 matcher->sub_matchers->len - mi);
2340 result = matcher_optimize (result);
2342 return result;
2346 * g_file_attribute_matcher_ref:
2347 * @matcher: a #GFileAttributeMatcher.
2349 * References a file attribute matcher.
2351 * Returns: a #GFileAttributeMatcher.
2353 GFileAttributeMatcher *
2354 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2356 if (matcher)
2358 g_return_val_if_fail (matcher->ref > 0, NULL);
2359 g_atomic_int_inc (&matcher->ref);
2361 return matcher;
2365 * g_file_attribute_matcher_unref:
2366 * @matcher: a #GFileAttributeMatcher.
2368 * Unreferences @matcher. If the reference count falls below 1,
2369 * the @matcher is automatically freed.
2372 void
2373 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2375 if (matcher)
2377 g_return_if_fail (matcher->ref > 0);
2379 if (g_atomic_int_dec_and_test (&matcher->ref))
2381 if (matcher->sub_matchers)
2382 g_array_free (matcher->sub_matchers, TRUE);
2384 g_free (matcher);
2390 * g_file_attribute_matcher_matches_only:
2391 * @matcher: a #GFileAttributeMatcher.
2392 * @attribute: a file attribute key.
2394 * Checks if a attribute matcher only matches a given attribute. Always
2395 * returns %FALSE if "*" was used when creating the matcher.
2397 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2399 gboolean
2400 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2401 const char *attribute)
2403 SubMatcher *sub_matcher;
2404 guint32 id;
2406 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2408 if (matcher == NULL ||
2409 matcher->all)
2410 return FALSE;
2412 if (matcher->sub_matchers->len != 1)
2413 return FALSE;
2415 id = lookup_attribute (attribute);
2417 sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, 0);
2419 return sub_matcher->id == id &&
2420 sub_matcher->mask == 0xffffffff;
2423 static gboolean
2424 matcher_matches_id (GFileAttributeMatcher *matcher,
2425 guint32 id)
2427 SubMatcher *sub_matchers;
2428 int i;
2430 if (matcher->sub_matchers)
2432 sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
2433 for (i = 0; i < matcher->sub_matchers->len; i++)
2435 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2436 return TRUE;
2440 return FALSE;
2443 gboolean
2444 _g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2445 guint32 id)
2447 /* We return a NULL matcher for an empty match string, so handle this */
2448 if (matcher == NULL)
2449 return FALSE;
2451 if (matcher->all)
2452 return TRUE;
2454 return matcher_matches_id (matcher, id);
2458 * g_file_attribute_matcher_matches:
2459 * @matcher: a #GFileAttributeMatcher.
2460 * @attribute: a file attribute key.
2462 * Checks if an attribute will be matched by an attribute matcher. If
2463 * the matcher was created with the "*" matching string, this function
2464 * will always return %TRUE.
2466 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2468 gboolean
2469 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2470 const char *attribute)
2472 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2474 /* We return a NULL matcher for an empty match string, so handle this */
2475 if (matcher == NULL)
2476 return FALSE;
2478 if (matcher->all)
2479 return TRUE;
2481 return matcher_matches_id (matcher, lookup_attribute (attribute));
2484 /* return TRUE -> all */
2486 * g_file_attribute_matcher_enumerate_namespace:
2487 * @matcher: a #GFileAttributeMatcher.
2488 * @ns: a string containing a file attribute namespace.
2490 * Checks if the matcher will match all of the keys in a given namespace.
2491 * This will always return %TRUE if a wildcard character is in use (e.g. if
2492 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2493 * using "*" and namespace is anything.)
2495 * TODO: this is awkwardly worded.
2497 * Returns: %TRUE if the matcher matches all of the entries
2498 * in the given @ns, %FALSE otherwise.
2500 gboolean
2501 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2502 const char *ns)
2504 SubMatcher *sub_matchers;
2505 int ns_id;
2506 int i;
2508 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2510 /* We return a NULL matcher for an empty match string, so handle this */
2511 if (matcher == NULL)
2512 return FALSE;
2514 if (matcher->all)
2515 return TRUE;
2517 ns_id = lookup_namespace (ns) << NS_POS;
2519 if (matcher->sub_matchers)
2521 sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
2522 for (i = 0; i < matcher->sub_matchers->len; i++)
2524 if (sub_matchers[i].id == ns_id)
2525 return TRUE;
2529 matcher->iterator_ns = ns_id;
2530 matcher->iterator_pos = 0;
2532 return FALSE;
2536 * g_file_attribute_matcher_enumerate_next:
2537 * @matcher: a #GFileAttributeMatcher.
2539 * Gets the next matched attribute from a #GFileAttributeMatcher.
2541 * Returns: a string containing the next attribute or %NULL if
2542 * no more attribute exist.
2544 const char *
2545 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2547 int i;
2548 SubMatcher *sub_matcher;
2550 /* We return a NULL matcher for an empty match string, so handle this */
2551 if (matcher == NULL)
2552 return NULL;
2554 while (1)
2556 i = matcher->iterator_pos++;
2558 if (matcher->sub_matchers == NULL)
2559 return NULL;
2561 if (i < matcher->sub_matchers->len)
2562 sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2563 else
2564 return NULL;
2566 if (sub_matcher->mask == 0xffffffff &&
2567 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2568 return get_attribute_for_id (sub_matcher->id);
2573 * g_file_attribute_matcher_to_string:
2574 * @matcher: (allow-none): a #GFileAttributeMatcher.
2576 * Prints what the matcher is matching against. The format will be
2577 * equal to the format passed to g_file_attribute_matcher_new().
2578 * The output however, might not be identical, as the matcher may
2579 * decide to use a different order or omit needless parts.
2581 * Returns: a string describing the attributes the matcher matches
2582 * against or %NULL if @matcher was %NULL.
2584 * Since: 2.32
2586 char *
2587 g_file_attribute_matcher_to_string (GFileAttributeMatcher *matcher)
2589 GString *string;
2590 guint i;
2592 if (matcher == NULL)
2593 return NULL;
2595 if (matcher->all)
2596 return g_strdup ("*");
2598 string = g_string_new ("");
2599 for (i = 0; i < matcher->sub_matchers->len; i++)
2601 SubMatcher *submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2603 if (i > 0)
2604 g_string_append_c (string, ',');
2606 g_string_append (string, get_attribute_for_id (submatcher->id));
2609 return g_string_free (string, FALSE);