Tizen 2.1 base
[platform/upstream/glib2.0.git] / gio / gfileinfo.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
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>
28  *
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.
32  *
33  * See <link linkend="gio-GFileAttribute">GFileAttribute</link> for more
34  * information on how GIO handles file attributes.
35  *
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).
40  *
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.
44  *
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.
50  *
51  * #GFileAttributeMatcher allows for searching through a #GFileInfo for
52  * attributes.
53  **/
54
55 #include "config.h"
56
57 #include <string.h>
58
59 #include "gfileinfo.h"
60 #include "gfileinfo-priv.h"
61 #include "gfileattribute-priv.h"
62 #include "gicon.h"
63 #include "glibintl.h"
64
65
66 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
67 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
68
69 typedef struct  {
70   guint32 attribute;
71   GFileAttributeValue value;
72 } GFileAttribute;
73
74 struct _GFileInfo
75 {
76   GObject parent_instance;
77
78   GArray *attributes;
79   GFileAttributeMatcher *mask;
80 };
81
82 struct _GFileInfoClass
83 {
84   GObjectClass parent_class;
85 };
86
87
88 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
89
90 typedef struct {
91   guint32 id;
92   guint32 attribute_id_counter;
93 } NSInfo;
94
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;
100
101 /* Attribute ids are 32bit, we split it up like this:
102  * |------------|--------------------|
103  *   12 bit          20 bit
104  *   namespace      attribute id
105  *
106  * This way the attributes gets sorted in namespace order
107  */
108
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))
113
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)
118
119 #define MAKE_ATTR_ID(_ns, _id)                          \
120     ( ((((guint32) _ns) & NS_MASK) << NS_POS) |         \
121       ((((guint32) _id) & ID_MASK) << ID_POS) )
122
123 static NSInfo *
124 _lookup_namespace (const char *namespace)
125 {
126   NSInfo *ns_info;
127
128   ns_info = g_hash_table_lookup (ns_hash, namespace);
129   if (ns_info == NULL)
130     {
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);
137     }
138   return ns_info;
139 }
140
141 static guint32
142 _lookup_attribute (const char *attribute)
143 {
144   guint32 attr_id, id;
145   char *ns;
146   const char *colon;
147   NSInfo *ns_info;
148
149   attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
150
151   if (attr_id != 0)
152     return attr_id;
153
154   colon = strstr (attribute, "::");
155   if (colon)
156     ns = g_strndup (attribute, colon - attribute);
157   else
158     ns = g_strdup ("");
159
160   ns_info = _lookup_namespace (ns);
161   g_free (ns);
162
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);
166
167   attr_id = MAKE_ATTR_ID (ns_info->id, id);
168
169   g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
170
171   return attr_id;
172 }
173
174 static void
175 ensure_attribute_hash (void)
176 {
177   if (attribute_hash != NULL)
178     return;
179
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);
182
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
188
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);
264
265 #undef REGISTER_ATTRIBUTE
266 }
267
268 static guint32
269 lookup_namespace (const char *namespace)
270 {
271   NSInfo *ns_info;
272   guint32 id;
273
274   G_LOCK (attribute_hash);
275
276   ensure_attribute_hash ();
277
278   ns_info = _lookup_namespace (namespace);
279   id = 0;
280   if (ns_info)
281     id = ns_info->id;
282
283   G_UNLOCK (attribute_hash);
284
285   return id;
286 }
287
288 static char *
289 get_attribute_for_id (int attribute)
290 {
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;
296 }
297
298 static guint32
299 lookup_attribute (const char *attribute)
300 {
301   guint32 attr_id;
302
303   G_LOCK (attribute_hash);
304   ensure_attribute_hash ();
305
306   attr_id = _lookup_attribute (attribute);
307
308   G_UNLOCK (attribute_hash);
309
310   return attr_id;
311 }
312
313 static void
314 g_file_info_finalize (GObject *object)
315 {
316   GFileInfo *info;
317   int i;
318   GFileAttribute *attrs;
319
320   info = G_FILE_INFO (object);
321
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);
326
327   if (info->mask != NO_ATTRIBUTE_MASK)
328     g_file_attribute_matcher_unref (info->mask);
329
330   G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
331 }
332
333 static void
334 g_file_info_class_init (GFileInfoClass *klass)
335 {
336   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
337
338   gobject_class->finalize = g_file_info_finalize;
339 }
340
341 static void
342 g_file_info_init (GFileInfo *info)
343 {
344   info->mask = NO_ATTRIBUTE_MASK;
345   info->attributes = g_array_new (FALSE, FALSE,
346                                   sizeof (GFileAttribute));
347 }
348
349 /**
350  * g_file_info_new:
351  *
352  * Creates a new file info structure.
353  *
354  * Returns: a #GFileInfo.
355  **/
356 GFileInfo *
357 g_file_info_new (void)
358 {
359   return g_object_new (G_TYPE_FILE_INFO, NULL);
360 }
361
362 /**
363  * g_file_info_copy_into:
364  * @src_info: source to copy attributes from.
365  * @dest_info: destination to copy attributes to.
366  *
367  * Copies all of the <link linkend="gio-GFileAttribute">GFileAttribute</link>s
368  * from @src_info to @dest_info.
369  **/
370 void
371 g_file_info_copy_into (GFileInfo *src_info,
372                        GFileInfo *dest_info)
373 {
374   GFileAttribute *source, *dest;
375   int i;
376
377   g_return_if_fail (G_IS_FILE_INFO (src_info));
378   g_return_if_fail (G_IS_FILE_INFO (dest_info));
379
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);
383
384   g_array_set_size (dest_info->attributes,
385                     src_info->attributes->len);
386
387   source = (GFileAttribute *)src_info->attributes->data;
388   dest = (GFileAttribute *)dest_info->attributes->data;
389
390   for (i = 0; i < src_info->attributes->len; i++)
391     {
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);
395     }
396
397   if (dest_info->mask != NO_ATTRIBUTE_MASK)
398     g_file_attribute_matcher_unref (dest_info->mask);
399
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);
404 }
405
406 /**
407  * g_file_info_dup:
408  * @other: a #GFileInfo.
409  *
410  * Duplicates a file info structure.
411  *
412  * Returns: (transfer full): a duplicate #GFileInfo of @other.
413  **/
414 GFileInfo *
415 g_file_info_dup (GFileInfo *other)
416 {
417   GFileInfo *new;
418
419   g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
420
421   new = g_file_info_new ();
422   g_file_info_copy_into (other, new);
423   return new;
424 }
425
426 /**
427  * g_file_info_set_attribute_mask:
428  * @info: a #GFileInfo.
429  * @mask: a #GFileAttributeMatcher.
430  *
431  * Sets @mask on @info to match specific attribute types.
432  **/
433 void
434 g_file_info_set_attribute_mask (GFileInfo             *info,
435                                 GFileAttributeMatcher *mask)
436 {
437   GFileAttribute *attr;
438   int i;
439
440   g_return_if_fail (G_IS_FILE_INFO (info));
441
442   if (mask != info->mask)
443     {
444       if (info->mask != NO_ATTRIBUTE_MASK)
445         g_file_attribute_matcher_unref (info->mask);
446       info->mask = g_file_attribute_matcher_ref (mask);
447
448       /* Remove non-matching attributes */
449       for (i = 0; i < info->attributes->len; i++)
450         {
451           attr = &g_array_index (info->attributes, GFileAttribute, i);
452           if (!_g_file_attribute_matcher_matches_id (mask,
453                                                     attr->attribute))
454             {
455               _g_file_attribute_value_clear (&attr->value);
456               g_array_remove_index (info->attributes, i);
457               i--;
458             }
459         }
460     }
461 }
462
463 /**
464  * g_file_info_unset_attribute_mask:
465  * @info: #GFileInfo.
466  *
467  * Unsets a mask set by g_file_info_set_attribute_mask(), if one
468  * is set.
469  **/
470 void
471 g_file_info_unset_attribute_mask (GFileInfo *info)
472 {
473   g_return_if_fail (G_IS_FILE_INFO (info));
474
475   if (info->mask != NO_ATTRIBUTE_MASK)
476     g_file_attribute_matcher_unref (info->mask);
477   info->mask = NO_ATTRIBUTE_MASK;
478 }
479
480 /**
481  * g_file_info_clear_status:
482  * @info: a #GFileInfo.
483  *
484  * Clears the status information from @info.
485  **/
486 void
487 g_file_info_clear_status (GFileInfo  *info)
488 {
489   GFileAttribute *attrs;
490   int i;
491
492   g_return_if_fail (G_IS_FILE_INFO (info));
493
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;
497 }
498
499 static int
500 g_file_info_find_place (GFileInfo  *info,
501                         guint32     attribute)
502 {
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 */
507
508   min = 0;
509   max = info->attributes->len;
510
511   attrs = (GFileAttribute *)info->attributes->data;
512
513   while (min < max)
514     {
515       med = min + (max - min) / 2;
516       if (attrs[med].attribute == attribute)
517         {
518           min = med;
519           break;
520         }
521       else if (attrs[med].attribute < attribute)
522         min = med + 1;
523       else /* attrs[med].attribute > attribute */
524         max = med;
525     }
526
527   return min;
528 }
529
530 static GFileAttributeValue *
531 g_file_info_find_value (GFileInfo *info,
532                         guint32    attr_id)
533 {
534   GFileAttribute *attrs;
535   int i;
536
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;
542
543   return NULL;
544 }
545
546 static GFileAttributeValue *
547 g_file_info_find_value_by_name (GFileInfo  *info,
548                                 const char *attribute)
549 {
550   guint32 attr_id;
551
552   attr_id = lookup_attribute (attribute);
553   return g_file_info_find_value (info, attr_id);
554 }
555
556 /**
557  * g_file_info_has_attribute:
558  * @info: a #GFileInfo.
559  * @attribute: a file attribute key.
560  *
561  * Checks if a file info structure has an attribute named @attribute.
562  *
563  * Returns: %TRUE if @Ginfo has an attribute named @attribute,
564  *     %FALSE otherwise.
565  **/
566 gboolean
567 g_file_info_has_attribute (GFileInfo  *info,
568                            const char *attribute)
569 {
570   GFileAttributeValue *value;
571
572   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
573   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
574
575   value = g_file_info_find_value_by_name (info, attribute);
576   return value != NULL;
577 }
578
579 /**
580  * g_file_info_has_namespace:
581  * @info: a #GFileInfo.
582  * @name_space: a file attribute namespace.
583  *
584  * Checks if a file info structure has an attribute in the
585  * specified @name_space.
586  *
587  * Returns: %TRUE if @Ginfo has an attribute in @name_space,
588  *     %FALSE otherwise.
589  *
590  * Since: 2.22
591  **/
592 gboolean
593 g_file_info_has_namespace (GFileInfo  *info,
594                            const char *name_space)
595 {
596   GFileAttribute *attrs;
597   guint32 ns_id;
598   int i;
599
600   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
601   g_return_val_if_fail (name_space != NULL, FALSE);
602
603   ns_id = lookup_namespace (name_space);
604
605   attrs = (GFileAttribute *)info->attributes->data;
606   for (i = 0; i < info->attributes->len; i++)
607     {
608       if (GET_NS (attrs[i].attribute) == ns_id)
609         return TRUE;
610     }
611
612   return FALSE;
613 }
614
615 /**
616  * g_file_info_list_attributes:
617  * @info: a #GFileInfo.
618  * @name_space: a file attribute key's namespace.
619  *
620  * Lists the file info structure's attributes.
621  *
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.
625  **/
626 char **
627 g_file_info_list_attributes (GFileInfo  *info,
628                              const char *name_space)
629 {
630   GPtrArray *names;
631   GFileAttribute *attrs;
632   guint32 attribute;
633   guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
634   int i;
635
636   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
637
638   names = g_ptr_array_new ();
639   attrs = (GFileAttribute *)info->attributes->data;
640   for (i = 0; i < info->attributes->len; i++)
641     {
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)));
645     }
646
647   /* NULL terminate */
648   g_ptr_array_add (names, NULL);
649
650   return (char **)g_ptr_array_free (names, FALSE);
651 }
652
653 /**
654  * g_file_info_get_attribute_type:
655  * @info: a #GFileInfo.
656  * @attribute: a file attribute key.
657  *
658  * Gets the attribute type for an attribute key.
659  *
660  * Returns: a #GFileAttributeType for the given @attribute, or
661  * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
662  **/
663 GFileAttributeType
664 g_file_info_get_attribute_type (GFileInfo  *info,
665                                 const char *attribute)
666 {
667   GFileAttributeValue *value;
668
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);
671
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;
677 }
678
679 /**
680  * g_file_info_remove_attribute:
681  * @info: a #GFileInfo.
682  * @attribute: a file attribute key.
683  *
684  * Removes all cases of @attribute from @info if it exists.
685  **/
686 void
687 g_file_info_remove_attribute (GFileInfo  *info,
688                               const char *attribute)
689 {
690   guint32 attr_id;
691   GFileAttribute *attrs;
692   int i;
693
694   g_return_if_fail (G_IS_FILE_INFO (info));
695   g_return_if_fail (attribute != NULL && *attribute != '\0');
696
697   attr_id = lookup_attribute (attribute);
698
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)
703     {
704       _g_file_attribute_value_clear (&attrs[i].value);
705       g_array_remove_index (info->attributes, i);
706     }
707 }
708
709 /**
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
716  *
717  * Gets the attribute type, value and status for an attribute key.
718  *
719  * Returns: (transfer none): %TRUE if @info has an attribute named @attribute,
720  *      %FALSE otherwise.
721  */
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)
728 {
729   GFileAttributeValue *value;
730
731   value = g_file_info_find_value_by_name (info, attribute);
732   if (value == NULL)
733     return FALSE;
734
735   if (status)
736     *status = value->status;
737
738   if (type)
739     *type = value->type;
740
741   if (value_pp)
742     *value_pp = _g_file_attribute_value_peek_as_pointer (value);
743
744   return TRUE;
745 }
746
747 /**
748  * g_file_info_get_attribute_status:
749  * @info: a #GFileInfo
750  * @attribute: a file attribute key
751  *
752  * Gets the attribute status for an attribute key.
753  *
754  * Returns: a #GFileAttributeStatus for the given @attribute, or
755  *    %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
756  *
757  */
758 GFileAttributeStatus
759 g_file_info_get_attribute_status (GFileInfo  *info,
760                                   const char *attribute)
761 {
762   GFileAttributeValue *val;
763
764   g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
765   g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
766
767   val = g_file_info_find_value_by_name (info, attribute);
768   if (val)
769     return val->status;
770
771   return G_FILE_ATTRIBUTE_STATUS_UNSET;
772 }
773
774 /**
775  * g_file_info_set_attribute_status:
776  * @info: a #GFileInfo
777  * @attribute: a file attribute key
778  * @status: a #GFileAttributeStatus
779  *
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.
783  *
784  * The attribute must exist in @info for this to work. Otherwise %FALSE
785  * is returned and @info is unchanged.
786  *
787  * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
788  *
789  * Since: 2.22
790  */
791 gboolean
792 g_file_info_set_attribute_status (GFileInfo  *info,
793                                   const char *attribute,
794                                   GFileAttributeStatus status)
795 {
796   GFileAttributeValue *val;
797
798   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
799   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
800
801   val = g_file_info_find_value_by_name (info, attribute);
802   if (val)
803     {
804       val->status = status;
805       return TRUE;
806     }
807
808   return FALSE;
809 }
810
811 GFileAttributeValue *
812 _g_file_info_get_attribute_value (GFileInfo  *info,
813                                   const char *attribute)
814
815 {
816   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
817   g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
818
819   return g_file_info_find_value_by_name (info, attribute);
820 }
821
822 /**
823  * g_file_info_get_attribute_as_string:
824  * @info: a #GFileInfo.
825  * @attribute: a file attribute key.
826  *
827  * Gets the value of a attribute, formated as a string.
828  * This escapes things as needed to make the string valid
829  * utf8.
830  *
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().
833  **/
834 char *
835 g_file_info_get_attribute_as_string (GFileInfo  *info,
836                                      const char *attribute)
837 {
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;
843 }
844
845
846 /**
847  * g_file_info_get_attribute_object:
848  * @info: a #GFileInfo.
849  * @attribute: a file attribute key.
850  *
851  * Gets the value of a #GObject attribute. If the attribute does
852  * not contain a #GObject, %NULL will be returned.
853  *
854  * Returns: (transfer none): a #GObject associated with the given @attribute, or
855  * %NULL otherwise.
856  **/
857 GObject *
858 g_file_info_get_attribute_object (GFileInfo  *info,
859                                   const char *attribute)
860 {
861   GFileAttributeValue *value;
862
863   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
864   g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
865
866   value = g_file_info_find_value_by_name (info, attribute);
867   return _g_file_attribute_value_get_object (value);
868 }
869
870 /**
871  * g_file_info_get_attribute_string:
872  * @info: a #GFileInfo.
873  * @attribute: a file attribute key.
874  *
875  * Gets the value of a string attribute. If the attribute does
876  * not contain a string, %NULL will be returned.
877  *
878  * Returns: the contents of the @attribute value as a UTF-8 string, or
879  * %NULL otherwise.
880  **/
881 const char *
882 g_file_info_get_attribute_string (GFileInfo  *info,
883                                   const char *attribute)
884 {
885   GFileAttributeValue *value;
886
887   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
888   g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
889
890   value = g_file_info_find_value_by_name (info, attribute);
891   return _g_file_attribute_value_get_string (value);
892 }
893
894 /**
895  * g_file_info_get_attribute_byte_string:
896  * @info: a #GFileInfo.
897  * @attribute: a file attribute key.
898  *
899  * Gets the value of a byte string attribute. If the attribute does
900  * not contain a byte string, %NULL will be returned.
901  *
902  * Returns: the contents of the @attribute value as a byte string, or
903  * %NULL otherwise.
904  **/
905 const char *
906 g_file_info_get_attribute_byte_string (GFileInfo  *info,
907                                        const char *attribute)
908 {
909   GFileAttributeValue *value;
910
911   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
912   g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
913
914   value = g_file_info_find_value_by_name (info, attribute);
915   return _g_file_attribute_value_get_byte_string (value);
916 }
917
918 /**
919  * g_file_info_get_attribute_stringv:
920  * @info: a #GFileInfo.
921  * @attribute: a file attribute key.
922  *
923  * Gets the value of a stringv attribute. If the attribute does
924  * not contain a stringv, %NULL will be returned.
925  *
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.
928  *
929  * Since: 2.22
930  **/
931 char **
932 g_file_info_get_attribute_stringv (GFileInfo  *info,
933                                    const char *attribute)
934 {
935   GFileAttributeValue *value;
936
937   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
938   g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
939
940   value = g_file_info_find_value_by_name (info, attribute);
941   return _g_file_attribute_value_get_stringv (value);
942 }
943
944 /**
945  * g_file_info_get_attribute_boolean:
946  * @info: a #GFileInfo.
947  * @attribute: a file attribute key.
948  *
949  * Gets the value of a boolean attribute. If the attribute does not
950  * contain a boolean value, %FALSE will be returned.
951  *
952  * Returns: the boolean value contained within the attribute.
953  **/
954 gboolean
955 g_file_info_get_attribute_boolean (GFileInfo  *info,
956                                    const char *attribute)
957 {
958   GFileAttributeValue *value;
959
960   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
961   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
962
963   value = g_file_info_find_value_by_name (info, attribute);
964   return _g_file_attribute_value_get_boolean (value);
965 }
966
967 /**
968  * g_file_info_get_attribute_uint32:
969  * @info: a #GFileInfo.
970  * @attribute: a file attribute key.
971  *
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.
975  *
976  * Returns: an unsigned 32-bit integer from the attribute.
977  **/
978 guint32
979 g_file_info_get_attribute_uint32 (GFileInfo  *info,
980                                   const char *attribute)
981 {
982   GFileAttributeValue *value;
983
984   g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
985   g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
986
987   value = g_file_info_find_value_by_name (info, attribute);
988   return _g_file_attribute_value_get_uint32 (value);
989 }
990
991 /**
992  * g_file_info_get_attribute_int32:
993  * @info: a #GFileInfo.
994  * @attribute: a file attribute key.
995  *
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.
999  *
1000  * Returns: a signed 32-bit integer from the attribute.
1001  **/
1002 gint32
1003 g_file_info_get_attribute_int32 (GFileInfo  *info,
1004                                  const char *attribute)
1005 {
1006   GFileAttributeValue *value;
1007
1008   g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1009   g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1010
1011   value = g_file_info_find_value_by_name (info, attribute);
1012   return _g_file_attribute_value_get_int32 (value);
1013 }
1014
1015 /**
1016  * g_file_info_get_attribute_uint64:
1017  * @info: a #GFileInfo.
1018  * @attribute: a file attribute key.
1019  *
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.
1023  *
1024  * Returns: a unsigned 64-bit integer from the attribute.
1025  **/
1026 guint64
1027 g_file_info_get_attribute_uint64 (GFileInfo  *info,
1028                                   const char *attribute)
1029 {
1030   GFileAttributeValue *value;
1031
1032   g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1033   g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1034
1035   value = g_file_info_find_value_by_name (info, attribute);
1036   return _g_file_attribute_value_get_uint64 (value);
1037 }
1038
1039 /**
1040  * g_file_info_get_attribute_int64:
1041  * @info: a #GFileInfo.
1042  * @attribute: a file attribute key.
1043  *
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.
1047  *
1048  * Returns: a signed 64-bit integer from the attribute.
1049  **/
1050 gint64
1051 g_file_info_get_attribute_int64  (GFileInfo  *info,
1052                                   const char *attribute)
1053 {
1054   GFileAttributeValue *value;
1055
1056   g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1057   g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
1058
1059   value = g_file_info_find_value_by_name (info, attribute);
1060   return _g_file_attribute_value_get_int64 (value);
1061 }
1062
1063 static GFileAttributeValue *
1064 g_file_info_create_value (GFileInfo *info,
1065                           guint32 attr_id)
1066 {
1067   GFileAttribute *attrs;
1068   int i;
1069
1070   if (info->mask != NO_ATTRIBUTE_MASK &&
1071       !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
1072     return NULL;
1073
1074   i = g_file_info_find_place (info, attr_id);
1075
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
1081     {
1082       GFileAttribute attr = { 0 };
1083       attr.attribute = attr_id;
1084       g_array_insert_val (info->attributes, i, attr);
1085
1086       attrs = (GFileAttribute *)info->attributes->data;
1087       return &attrs[i].value;
1088     }
1089 }
1090
1091 void
1092 _g_file_info_set_attribute_by_id (GFileInfo                 *info,
1093                                   guint32                    attribute,
1094                                   GFileAttributeType         type,
1095                                   gpointer                   value_p)
1096 {
1097   GFileAttributeValue *value;
1098
1099   value = g_file_info_create_value (info, attribute);
1100
1101   if (value)
1102     _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1103 }
1104
1105 /**
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
1111  *
1112  * Sets the @attribute to contain the given value, if possible. To unset the
1113  * attribute, use %G_ATTRIBUTE_TYPE_INVALID for @type.
1114  **/
1115 void
1116 g_file_info_set_attribute (GFileInfo                 *info,
1117                            const char                *attribute,
1118                            GFileAttributeType         type,
1119                            gpointer                   value_p)
1120 {
1121   g_return_if_fail (G_IS_FILE_INFO (info));
1122   g_return_if_fail (attribute != NULL && *attribute != '\0');
1123
1124   _g_file_info_set_attribute_by_id (info, lookup_attribute (attribute), type, value_p);
1125 }
1126
1127 void
1128 _g_file_info_set_attribute_object_by_id (GFileInfo *info,
1129                                          guint32    attribute,
1130                                          GObject   *attr_value)
1131 {
1132   GFileAttributeValue *value;
1133
1134   value = g_file_info_create_value (info, attribute);
1135   if (value)
1136     _g_file_attribute_value_set_object (value, attr_value);
1137 }
1138
1139 /**
1140  * g_file_info_set_attribute_object:
1141  * @info: a #GFileInfo.
1142  * @attribute: a file attribute key.
1143  * @attr_value: a #GObject.
1144  *
1145  * Sets the @attribute to contain the given @attr_value,
1146  * if possible.
1147  **/
1148 void
1149 g_file_info_set_attribute_object (GFileInfo  *info,
1150                                   const char *attribute,
1151                                   GObject    *attr_value)
1152 {
1153   g_return_if_fail (G_IS_FILE_INFO (info));
1154   g_return_if_fail (attribute != NULL && *attribute != '\0');
1155   g_return_if_fail (G_IS_OBJECT (attr_value));
1156
1157   _g_file_info_set_attribute_object_by_id (info,
1158                                            lookup_attribute (attribute),
1159                                            attr_value);
1160 }
1161
1162 void
1163 _g_file_info_set_attribute_stringv_by_id (GFileInfo *info,
1164                                           guint32    attribute,
1165                                           char     **attr_value)
1166 {
1167   GFileAttributeValue *value;
1168
1169   value = g_file_info_create_value (info, attribute);
1170   if (value)
1171     _g_file_attribute_value_set_stringv (value, attr_value);
1172 }
1173
1174 /**
1175  * g_file_info_set_attribute_stringv:
1176  * @info: a #GFileInfo.
1177  * @attribute: a file attribute key
1178  * @attr_value: (array) (element-type utf8): a %NULL terminated array of UTF-8 strings.
1179  *
1180  * Sets the @attribute to contain the given @attr_value,
1181  * if possible.
1182  *
1183  * Sinze: 2.22
1184  **/
1185 void
1186 g_file_info_set_attribute_stringv (GFileInfo  *info,
1187                                    const char *attribute,
1188                                    char      **attr_value)
1189 {
1190   g_return_if_fail (G_IS_FILE_INFO (info));
1191   g_return_if_fail (attribute != NULL && *attribute != '\0');
1192   g_return_if_fail (attr_value != NULL);
1193
1194   _g_file_info_set_attribute_stringv_by_id (info,
1195                                             lookup_attribute (attribute),
1196                                             attr_value);
1197 }
1198
1199 void
1200 _g_file_info_set_attribute_string_by_id (GFileInfo  *info,
1201                                          guint32     attribute,
1202                                          const char *attr_value)
1203 {
1204   GFileAttributeValue *value;
1205
1206   value = g_file_info_create_value (info, attribute);
1207   if (value)
1208     _g_file_attribute_value_set_string (value, attr_value);
1209 }
1210
1211 /**
1212  * g_file_info_set_attribute_string:
1213  * @info: a #GFileInfo.
1214  * @attribute: a file attribute key.
1215  * @attr_value: a UTF-8 string.
1216  *
1217  * Sets the @attribute to contain the given @attr_value,
1218  * if possible.
1219  **/
1220 void
1221 g_file_info_set_attribute_string (GFileInfo  *info,
1222                                   const char *attribute,
1223                                   const char *attr_value)
1224 {
1225   g_return_if_fail (G_IS_FILE_INFO (info));
1226   g_return_if_fail (attribute != NULL && *attribute != '\0');
1227   g_return_if_fail (attr_value != NULL);
1228
1229   _g_file_info_set_attribute_string_by_id (info,
1230                                            lookup_attribute (attribute),
1231                                            attr_value);
1232 }
1233
1234 void
1235 _g_file_info_set_attribute_byte_string_by_id (GFileInfo  *info,
1236                                               guint32     attribute,
1237                                               const char *attr_value)
1238 {
1239   GFileAttributeValue *value;
1240
1241   value = g_file_info_create_value (info, attribute);
1242   if (value)
1243     _g_file_attribute_value_set_byte_string (value, attr_value);
1244 }
1245
1246 /**
1247  * g_file_info_set_attribute_byte_string:
1248  * @info: a #GFileInfo.
1249  * @attribute: a file attribute key.
1250  * @attr_value: a byte string.
1251  *
1252  * Sets the @attribute to contain the given @attr_value,
1253  * if possible.
1254  **/
1255 void
1256 g_file_info_set_attribute_byte_string (GFileInfo  *info,
1257                                        const char *attribute,
1258                                        const char *attr_value)
1259 {
1260   g_return_if_fail (G_IS_FILE_INFO (info));
1261   g_return_if_fail (attribute != NULL && *attribute != '\0');
1262   g_return_if_fail (attr_value != NULL);
1263
1264   _g_file_info_set_attribute_byte_string_by_id (info,
1265                                                 lookup_attribute (attribute),
1266                                                 attr_value);
1267 }
1268
1269 void
1270 _g_file_info_set_attribute_boolean_by_id (GFileInfo *info,
1271                                           guint32    attribute,
1272                                           gboolean   attr_value)
1273 {
1274   GFileAttributeValue *value;
1275
1276   value = g_file_info_create_value (info, attribute);
1277   if (value)
1278     _g_file_attribute_value_set_boolean (value, attr_value);
1279 }
1280
1281 /**
1282  * g_file_info_set_attribute_boolean:
1283  * @info: a #GFileInfo.
1284  * @attribute: a file attribute key.
1285  * @attr_value: a boolean value.
1286  *
1287  * Sets the @attribute to contain the given @attr_value,
1288  * if possible.
1289  **/
1290 void
1291 g_file_info_set_attribute_boolean (GFileInfo  *info,
1292                                    const char *attribute,
1293                                    gboolean    attr_value)
1294 {
1295   g_return_if_fail (G_IS_FILE_INFO (info));
1296   g_return_if_fail (attribute != NULL && *attribute != '\0');
1297
1298   _g_file_info_set_attribute_boolean_by_id (info,
1299                                             lookup_attribute (attribute),
1300                                             attr_value);
1301 }
1302
1303 void
1304 _g_file_info_set_attribute_uint32_by_id (GFileInfo *info,
1305                                          guint32    attribute,
1306                                          guint32    attr_value)
1307 {
1308   GFileAttributeValue *value;
1309
1310   value = g_file_info_create_value (info, attribute);
1311   if (value)
1312     _g_file_attribute_value_set_uint32 (value, attr_value);
1313 }
1314
1315 /**
1316  * g_file_info_set_attribute_uint32:
1317  * @info: a #GFileInfo.
1318  * @attribute: a file attribute key.
1319  * @attr_value: an unsigned 32-bit integer.
1320  *
1321  * Sets the @attribute to contain the given @attr_value,
1322  * if possible.
1323  **/
1324 void
1325 g_file_info_set_attribute_uint32 (GFileInfo  *info,
1326                                   const char *attribute,
1327                                   guint32     attr_value)
1328 {
1329   g_return_if_fail (G_IS_FILE_INFO (info));
1330   g_return_if_fail (attribute != NULL && *attribute != '\0');
1331
1332   _g_file_info_set_attribute_uint32_by_id (info,
1333                                            lookup_attribute (attribute),
1334                                            attr_value);
1335 }
1336
1337 void
1338 _g_file_info_set_attribute_int32_by_id (GFileInfo *info,
1339                                         guint32    attribute,
1340                                         gint32     attr_value)
1341 {
1342   GFileAttributeValue *value;
1343
1344   value = g_file_info_create_value (info, attribute);
1345   if (value)
1346     _g_file_attribute_value_set_int32 (value, attr_value);
1347 }
1348
1349 /**
1350  * g_file_info_set_attribute_int32:
1351  * @info: a #GFileInfo.
1352  * @attribute: a file attribute key.
1353  * @attr_value: a signed 32-bit integer
1354  *
1355  * Sets the @attribute to contain the given @attr_value,
1356  * if possible.
1357  **/
1358 void
1359 g_file_info_set_attribute_int32 (GFileInfo  *info,
1360                                  const char *attribute,
1361                                  gint32      attr_value)
1362 {
1363   g_return_if_fail (G_IS_FILE_INFO (info));
1364   g_return_if_fail (attribute != NULL && *attribute != '\0');
1365
1366   _g_file_info_set_attribute_int32_by_id (info,
1367                                           lookup_attribute (attribute),
1368                                           attr_value);
1369 }
1370
1371 void
1372 _g_file_info_set_attribute_uint64_by_id (GFileInfo *info,
1373                                          guint32    attribute,
1374                                          guint64    attr_value)
1375 {
1376   GFileAttributeValue *value;
1377
1378   value = g_file_info_create_value (info, attribute);
1379   if (value)
1380     _g_file_attribute_value_set_uint64 (value, attr_value);
1381 }
1382
1383 /**
1384  * g_file_info_set_attribute_uint64:
1385  * @info: a #GFileInfo.
1386  * @attribute: a file attribute key.
1387  * @attr_value: an unsigned 64-bit integer.
1388  *
1389  * Sets the @attribute to contain the given @attr_value,
1390  * if possible.
1391  **/
1392 void
1393 g_file_info_set_attribute_uint64 (GFileInfo  *info,
1394                                   const char *attribute,
1395                                   guint64     attr_value)
1396 {
1397   g_return_if_fail (G_IS_FILE_INFO (info));
1398   g_return_if_fail (attribute != NULL && *attribute != '\0');
1399
1400   _g_file_info_set_attribute_uint64_by_id (info,
1401                                            lookup_attribute (attribute),
1402                                            attr_value);
1403 }
1404
1405 void
1406 _g_file_info_set_attribute_int64_by_id (GFileInfo *info,
1407                                         guint32    attribute,
1408                                         gint64     attr_value)
1409 {
1410   GFileAttributeValue *value;
1411
1412   value = g_file_info_create_value (info, attribute);
1413   if (value)
1414     _g_file_attribute_value_set_int64 (value, attr_value);
1415 }
1416
1417 /**
1418  * g_file_info_set_attribute_int64:
1419  * @info: a #GFileInfo.
1420  * @attribute: attribute name to set.
1421  * @attr_value: int64 value to set attribute to.
1422  *
1423  * Sets the @attribute to contain the given @attr_value,
1424  * if possible.
1425  *
1426  **/
1427 void
1428 g_file_info_set_attribute_int64  (GFileInfo  *info,
1429                                   const char *attribute,
1430                                   gint64      attr_value)
1431 {
1432   g_return_if_fail (G_IS_FILE_INFO (info));
1433   g_return_if_fail (attribute != NULL && *attribute != '\0');
1434
1435   _g_file_info_set_attribute_int64_by_id (info,
1436                                           lookup_attribute (attribute),
1437                                           attr_value);
1438 }
1439
1440 /* Helper getters */
1441 /**
1442  * g_file_info_get_file_type:
1443  * @info: a #GFileInfo.
1444  *
1445  * Gets a file's type (whether it is a regular file, symlink, etc).
1446  * This is different from the file's content type, see g_file_info_get_content_type().
1447  *
1448  * Returns: a #GFileType for the given file.
1449  **/
1450 GFileType
1451 g_file_info_get_file_type (GFileInfo *info)
1452 {
1453   static guint32 attr = 0;
1454   GFileAttributeValue *value;
1455
1456   g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1457
1458   if (attr == 0)
1459     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1460
1461   value = g_file_info_find_value (info, attr);
1462   return (GFileType)_g_file_attribute_value_get_uint32 (value);
1463 }
1464
1465 /**
1466  * g_file_info_get_is_hidden:
1467  * @info: a #GFileInfo.
1468  *
1469  * Checks if a file is hidden.
1470  *
1471  * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1472  **/
1473 gboolean
1474 g_file_info_get_is_hidden (GFileInfo *info)
1475 {
1476   static guint32 attr = 0;
1477   GFileAttributeValue *value;
1478
1479   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1480
1481   if (attr == 0)
1482     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1483
1484   value = g_file_info_find_value (info, attr);
1485   return (GFileType)_g_file_attribute_value_get_boolean (value);
1486 }
1487
1488 /**
1489  * g_file_info_get_is_backup:
1490  * @info: a #GFileInfo.
1491  *
1492  * Checks if a file is a backup file.
1493  *
1494  * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1495  **/
1496 gboolean
1497 g_file_info_get_is_backup (GFileInfo *info)
1498 {
1499   static guint32 attr = 0;
1500   GFileAttributeValue *value;
1501
1502   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1503
1504   if (attr == 0)
1505     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1506
1507   value = g_file_info_find_value (info, attr);
1508   return (GFileType)_g_file_attribute_value_get_boolean (value);
1509 }
1510
1511 /**
1512  * g_file_info_get_is_symlink:
1513  * @info: a #GFileInfo.
1514  *
1515  * Checks if a file is a symlink.
1516  *
1517  * Returns: %TRUE if the given @info is a symlink.
1518  **/
1519 gboolean
1520 g_file_info_get_is_symlink (GFileInfo *info)
1521 {
1522   static guint32 attr = 0;
1523   GFileAttributeValue *value;
1524
1525   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1526
1527   if (attr == 0)
1528     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1529
1530   value = g_file_info_find_value (info, attr);
1531   return (GFileType)_g_file_attribute_value_get_boolean (value);
1532 }
1533
1534 /**
1535  * g_file_info_get_name:
1536  * @info: a #GFileInfo.
1537  *
1538  * Gets the name for a file.
1539  *
1540  * Returns: a string containing the file name.
1541  **/
1542 const char *
1543 g_file_info_get_name (GFileInfo *info)
1544 {
1545   static guint32 attr = 0;
1546   GFileAttributeValue *value;
1547
1548   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1549
1550   if (attr == 0)
1551     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1552
1553   value = g_file_info_find_value (info, attr);
1554   return _g_file_attribute_value_get_byte_string (value);
1555 }
1556
1557 /**
1558  * g_file_info_get_display_name:
1559  * @info: a #GFileInfo.
1560  *
1561  * Gets a display name for a file.
1562  *
1563  * Returns: a string containing the display name.
1564  **/
1565 const char *
1566 g_file_info_get_display_name (GFileInfo *info)
1567 {
1568   static guint32 attr = 0;
1569   GFileAttributeValue *value;
1570
1571   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1572
1573   if (attr == 0)
1574     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1575
1576   value = g_file_info_find_value (info, attr);
1577   return _g_file_attribute_value_get_string (value);
1578 }
1579
1580 /**
1581  * g_file_info_get_edit_name:
1582  * @info: a #GFileInfo.
1583  *
1584  * Gets the edit name for a file.
1585  *
1586  * Returns: a string containing the edit name.
1587  **/
1588 const char *
1589 g_file_info_get_edit_name (GFileInfo *info)
1590 {
1591   static guint32 attr = 0;
1592   GFileAttributeValue *value;
1593
1594   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1595
1596   if (attr == 0)
1597     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1598
1599   value = g_file_info_find_value (info, attr);
1600   return _g_file_attribute_value_get_string (value);
1601 }
1602
1603 /**
1604  * g_file_info_get_icon:
1605  * @info: a #GFileInfo.
1606  *
1607  * Gets the icon for a file.
1608  *
1609  * Returns: (transfer none): #GIcon for the given @info.
1610  **/
1611 GIcon *
1612 g_file_info_get_icon (GFileInfo *info)
1613 {
1614   static guint32 attr = 0;
1615   GFileAttributeValue *value;
1616   GObject *obj;
1617
1618   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1619
1620   if (attr == 0)
1621     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1622
1623   value = g_file_info_find_value (info, attr);
1624   obj = _g_file_attribute_value_get_object (value);
1625   if (G_IS_ICON (obj))
1626     return G_ICON (obj);
1627   return NULL;
1628 }
1629
1630 /**
1631  * g_file_info_get_content_type:
1632  * @info: a #GFileInfo.
1633  *
1634  * Gets the file's content type.
1635  *
1636  * Returns: a string containing the file's content type.
1637  **/
1638 const char *
1639 g_file_info_get_content_type (GFileInfo *info)
1640 {
1641   static guint32 attr = 0;
1642   GFileAttributeValue *value;
1643
1644   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1645
1646   if (attr == 0)
1647     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1648
1649   value = g_file_info_find_value (info, attr);
1650   return _g_file_attribute_value_get_string (value);
1651 }
1652
1653 /**
1654  * g_file_info_get_size:
1655  * @info: a #GFileInfo.
1656  *
1657  * Gets the file's size.
1658  *
1659  * Returns: a #goffset containing the file's size.
1660  **/
1661 goffset
1662 g_file_info_get_size (GFileInfo *info)
1663 {
1664   static guint32 attr = 0;
1665   GFileAttributeValue *value;
1666
1667   g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1668
1669   if (attr == 0)
1670     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1671
1672   value = g_file_info_find_value (info, attr);
1673   return (goffset) _g_file_attribute_value_get_uint64 (value);
1674 }
1675
1676 /**
1677  * g_file_info_get_modification_time:
1678  * @info: a #GFileInfo.
1679  * @result: (out caller-allocates): a #GTimeVal.
1680  *
1681  * Gets the modification time of the current @info and sets it
1682  * in @result.
1683  **/
1684 void
1685 g_file_info_get_modification_time (GFileInfo *info,
1686                                    GTimeVal  *result)
1687 {
1688   static guint32 attr_mtime = 0, attr_mtime_usec;
1689   GFileAttributeValue *value;
1690
1691   g_return_if_fail (G_IS_FILE_INFO (info));
1692   g_return_if_fail (result != NULL);
1693
1694   if (attr_mtime == 0)
1695     {
1696       attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1697       attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1698     }
1699
1700   value = g_file_info_find_value (info, attr_mtime);
1701   result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1702   value = g_file_info_find_value (info, attr_mtime_usec);
1703   result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1704 }
1705
1706 /**
1707  * g_file_info_get_symlink_target:
1708  * @info: a #GFileInfo.
1709  *
1710  * Gets the symlink target for a given #GFileInfo.
1711  *
1712  * Returns: a string containing the symlink target.
1713  **/
1714 const char *
1715 g_file_info_get_symlink_target (GFileInfo *info)
1716 {
1717   static guint32 attr = 0;
1718   GFileAttributeValue *value;
1719
1720   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1721
1722   if (attr == 0)
1723     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1724
1725   value = g_file_info_find_value (info, attr);
1726   return _g_file_attribute_value_get_byte_string (value);
1727 }
1728
1729 /**
1730  * g_file_info_get_etag:
1731  * @info: a #GFileInfo.
1732  *
1733  * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1734  * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1735  *
1736  * Returns: a string containing the value of the "etag:value" attribute.
1737  **/
1738 const char *
1739 g_file_info_get_etag (GFileInfo *info)
1740 {
1741   static guint32 attr = 0;
1742   GFileAttributeValue *value;
1743
1744   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1745
1746   if (attr == 0)
1747     attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1748
1749   value = g_file_info_find_value (info, attr);
1750   return _g_file_attribute_value_get_string (value);
1751 }
1752
1753 /**
1754  * g_file_info_get_sort_order:
1755  * @info: a #GFileInfo.
1756  *
1757  * Gets the value of the sort_order attribute from the #GFileInfo.
1758  * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1759  *
1760  * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1761  **/
1762 gint32
1763 g_file_info_get_sort_order (GFileInfo *info)
1764 {
1765   static guint32 attr = 0;
1766   GFileAttributeValue *value;
1767
1768   g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1769
1770   if (attr == 0)
1771     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1772
1773   value = g_file_info_find_value (info, attr);
1774   return _g_file_attribute_value_get_int32 (value);
1775 }
1776
1777 /* Helper setters: */
1778 /**
1779  * g_file_info_set_file_type:
1780  * @info: a #GFileInfo.
1781  * @type: a #GFileType.
1782  *
1783  * Sets the file type in a #GFileInfo to @type.
1784  * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1785  **/
1786 void
1787 g_file_info_set_file_type (GFileInfo *info,
1788                            GFileType  type)
1789 {
1790   static guint32 attr = 0;
1791   GFileAttributeValue *value;
1792
1793   g_return_if_fail (G_IS_FILE_INFO (info));
1794
1795   if (attr == 0)
1796     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1797
1798   value = g_file_info_create_value (info, attr);
1799   if (value)
1800     _g_file_attribute_value_set_uint32 (value, type);
1801 }
1802
1803 /**
1804  * g_file_info_set_is_hidden:
1805  * @info: a #GFileInfo.
1806  * @is_hidden: a #gboolean.
1807  *
1808  * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1809  * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1810  **/
1811 void
1812 g_file_info_set_is_hidden (GFileInfo *info,
1813                            gboolean   is_hidden)
1814 {
1815   static guint32 attr = 0;
1816   GFileAttributeValue *value;
1817
1818   g_return_if_fail (G_IS_FILE_INFO (info));
1819
1820   if (attr == 0)
1821     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1822
1823   value = g_file_info_create_value (info, attr);
1824   if (value)
1825     _g_file_attribute_value_set_boolean (value, is_hidden);
1826 }
1827
1828 /**
1829  * g_file_info_set_is_symlink:
1830  * @info: a #GFileInfo.
1831  * @is_symlink: a #gboolean.
1832  *
1833  * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1834  * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1835  **/
1836 void
1837 g_file_info_set_is_symlink (GFileInfo *info,
1838                             gboolean   is_symlink)
1839 {
1840   static guint32 attr = 0;
1841   GFileAttributeValue *value;
1842
1843   g_return_if_fail (G_IS_FILE_INFO (info));
1844
1845   if (attr == 0)
1846     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1847
1848   value = g_file_info_create_value (info, attr);
1849   if (value)
1850     _g_file_attribute_value_set_boolean (value, is_symlink);
1851 }
1852
1853 /**
1854  * g_file_info_set_name:
1855  * @info: a #GFileInfo.
1856  * @name: a string containing a name.
1857  *
1858  * Sets the name attribute for the current #GFileInfo.
1859  * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1860  **/
1861 void
1862 g_file_info_set_name (GFileInfo  *info,
1863                       const char *name)
1864 {
1865   static guint32 attr = 0;
1866   GFileAttributeValue *value;
1867
1868   g_return_if_fail (G_IS_FILE_INFO (info));
1869   g_return_if_fail (name != NULL);
1870
1871   if (attr == 0)
1872     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1873
1874   value = g_file_info_create_value (info, attr);
1875   if (value)
1876     _g_file_attribute_value_set_byte_string (value, name);
1877 }
1878
1879 /**
1880  * g_file_info_set_display_name:
1881  * @info: a #GFileInfo.
1882  * @display_name: a string containing a display name.
1883  *
1884  * Sets the display name for the current #GFileInfo.
1885  * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1886  **/
1887 void
1888 g_file_info_set_display_name (GFileInfo  *info,
1889                               const char *display_name)
1890 {
1891   static guint32 attr = 0;
1892   GFileAttributeValue *value;
1893
1894   g_return_if_fail (G_IS_FILE_INFO (info));
1895   g_return_if_fail (display_name != NULL);
1896
1897   if (attr == 0)
1898     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1899
1900   value = g_file_info_create_value (info, attr);
1901   if (value)
1902     _g_file_attribute_value_set_string (value, display_name);
1903 }
1904
1905 /**
1906  * g_file_info_set_edit_name:
1907  * @info: a #GFileInfo.
1908  * @edit_name: a string containing an edit name.
1909  *
1910  * Sets the edit name for the current file.
1911  * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1912  **/
1913 void
1914 g_file_info_set_edit_name (GFileInfo  *info,
1915                            const char *edit_name)
1916 {
1917   static guint32 attr = 0;
1918   GFileAttributeValue *value;
1919
1920   g_return_if_fail (G_IS_FILE_INFO (info));
1921   g_return_if_fail (edit_name != NULL);
1922
1923   if (attr == 0)
1924     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1925
1926   value = g_file_info_create_value (info, attr);
1927   if (value)
1928     _g_file_attribute_value_set_string (value, edit_name);
1929 }
1930
1931 /**
1932  * g_file_info_set_icon:
1933  * @info: a #GFileInfo.
1934  * @icon: a #GIcon.
1935  *
1936  * Sets the icon for a given #GFileInfo.
1937  * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1938  **/
1939 void
1940 g_file_info_set_icon (GFileInfo *info,
1941                       GIcon     *icon)
1942 {
1943   static guint32 attr = 0;
1944   GFileAttributeValue *value;
1945
1946   g_return_if_fail (G_IS_FILE_INFO (info));
1947   g_return_if_fail (G_IS_ICON (icon));
1948
1949   if (attr == 0)
1950     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1951
1952   value = g_file_info_create_value (info, attr);
1953   if (value)
1954     _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1955 }
1956
1957 /**
1958  * g_file_info_set_content_type:
1959  * @info: a #GFileInfo.
1960  * @content_type: a content type. See <link linkend="gio-GContentType">GContentType</link>.
1961  *
1962  * Sets the content type attribute for a given #GFileInfo.
1963  * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1964  **/
1965 void
1966 g_file_info_set_content_type (GFileInfo  *info,
1967                               const char *content_type)
1968 {
1969   static guint32 attr = 0;
1970   GFileAttributeValue *value;
1971
1972   g_return_if_fail (G_IS_FILE_INFO (info));
1973   g_return_if_fail (content_type != NULL);
1974
1975   if (attr == 0)
1976     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1977
1978   value = g_file_info_create_value (info, attr);
1979   if (value)
1980     _g_file_attribute_value_set_string (value, content_type);
1981 }
1982
1983 /**
1984  * g_file_info_set_size:
1985  * @info: a #GFileInfo.
1986  * @size: a #goffset containing the file's size.
1987  *
1988  * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1989  * to the given size.
1990  **/
1991 void
1992 g_file_info_set_size (GFileInfo *info,
1993                       goffset    size)
1994 {
1995   static guint32 attr = 0;
1996   GFileAttributeValue *value;
1997
1998   g_return_if_fail (G_IS_FILE_INFO (info));
1999
2000   if (attr == 0)
2001     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
2002
2003   value = g_file_info_create_value (info, attr);
2004   if (value)
2005     _g_file_attribute_value_set_uint64 (value, size);
2006 }
2007
2008 /**
2009  * g_file_info_set_modification_time:
2010  * @info: a #GFileInfo.
2011  * @mtime: a #GTimeVal.
2012  *
2013  * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
2014  * info to the given time value.
2015  **/
2016 void
2017 g_file_info_set_modification_time (GFileInfo *info,
2018                                    GTimeVal  *mtime)
2019 {
2020   static guint32 attr_mtime = 0, attr_mtime_usec;
2021   GFileAttributeValue *value;
2022
2023   g_return_if_fail (G_IS_FILE_INFO (info));
2024   g_return_if_fail (mtime != NULL);
2025
2026   if (attr_mtime == 0)
2027     {
2028       attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
2029       attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
2030     }
2031
2032   value = g_file_info_create_value (info, attr_mtime);
2033   if (value)
2034     _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
2035   value = g_file_info_create_value (info, attr_mtime_usec);
2036   if (value)
2037     _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
2038 }
2039
2040 /**
2041  * g_file_info_set_symlink_target:
2042  * @info: a #GFileInfo.
2043  * @symlink_target: a static string containing a path to a symlink target.
2044  *
2045  * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
2046  * to the given symlink target.
2047  **/
2048 void
2049 g_file_info_set_symlink_target (GFileInfo  *info,
2050                                 const char *symlink_target)
2051 {
2052   static guint32 attr = 0;
2053   GFileAttributeValue *value;
2054
2055   g_return_if_fail (G_IS_FILE_INFO (info));
2056   g_return_if_fail (symlink_target != NULL);
2057
2058   if (attr == 0)
2059     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
2060
2061   value = g_file_info_create_value (info, attr);
2062   if (value)
2063     _g_file_attribute_value_set_byte_string (value, symlink_target);
2064 }
2065
2066 /**
2067  * g_file_info_set_sort_order:
2068  * @info: a #GFileInfo.
2069  * @sort_order: a sort order integer.
2070  *
2071  * Sets the sort order attribute in the file info structure. See
2072  * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
2073  **/
2074 void
2075 g_file_info_set_sort_order (GFileInfo *info,
2076                             gint32     sort_order)
2077 {
2078   static guint32 attr = 0;
2079   GFileAttributeValue *value;
2080
2081   g_return_if_fail (G_IS_FILE_INFO (info));
2082
2083   if (attr == 0)
2084     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
2085
2086   value = g_file_info_create_value (info, attr);
2087   if (value)
2088     _g_file_attribute_value_set_int32 (value, sort_order);
2089 }
2090
2091
2092 typedef struct {
2093   guint32 id;
2094   guint32 mask;
2095 } SubMatcher;
2096
2097 struct _GFileAttributeMatcher {
2098   gboolean all;
2099   gint ref;
2100
2101   GArray *sub_matchers;
2102
2103   /* Interator */
2104   guint32 iterator_ns;
2105   gint iterator_pos;
2106 };
2107
2108 G_DEFINE_BOXED_TYPE (GFileAttributeMatcher, g_file_attribute_matcher,
2109                      g_file_attribute_matcher_ref,
2110                      g_file_attribute_matcher_unref)
2111
2112 static gint
2113 compare_sub_matchers (gconstpointer a,
2114                       gconstpointer b)
2115 {
2116   const SubMatcher *suba = a;
2117   const SubMatcher *subb = b;
2118   int diff;
2119
2120   diff = suba->id - subb->id;
2121
2122   if (diff)
2123     return diff;
2124
2125   return suba->mask - subb->mask;
2126 }
2127
2128 static gboolean
2129 sub_matcher_matches (SubMatcher *matcher,
2130                      SubMatcher *submatcher)
2131 {
2132   if ((matcher->mask & submatcher->mask) != matcher->mask)
2133     return FALSE;
2134   
2135   return matcher->id == (submatcher->id & matcher->mask);
2136 }
2137
2138 /* Call this function after modifying a matcher.
2139  * It will ensure all the invariants other functions rely on.
2140  */
2141 static GFileAttributeMatcher *
2142 matcher_optimize (GFileAttributeMatcher *matcher)
2143 {
2144   SubMatcher *submatcher, *compare;
2145   guint i, j;
2146
2147   /* remove sub_matchers if we match everything anyway */
2148   if (matcher->all)
2149     {
2150       if (matcher->sub_matchers)
2151         {
2152           g_array_free (matcher->sub_matchers, TRUE);
2153           matcher->sub_matchers = NULL;
2154         }
2155       return matcher;
2156     }
2157
2158   if (matcher->sub_matchers->len == 0)
2159     {
2160       g_file_attribute_matcher_unref (matcher);
2161       return NULL;
2162     }
2163
2164   /* sort sub_matchers by id (and then mask), so we can bsearch
2165    * and compare matchers in O(N) instead of O(N²) */
2166   g_array_sort (matcher->sub_matchers, compare_sub_matchers);
2167
2168   /* remove duplicates and specific matches when we match the whole namespace */
2169   j = 0;
2170   compare = &g_array_index (matcher->sub_matchers, SubMatcher, j);
2171
2172   for (i = 1; i < matcher->sub_matchers->len; i++)
2173     {
2174       submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2175       if (sub_matcher_matches (compare, submatcher))
2176         continue;
2177
2178       j++;
2179       compare++;
2180
2181       if (j < i)
2182         *compare = *submatcher;
2183     }
2184
2185   g_array_set_size (matcher->sub_matchers, j + 1);
2186
2187   return matcher;
2188 }
2189
2190 /**
2191  * g_file_attribute_matcher_new:
2192  * @attributes: an attribute string to match.
2193  *
2194  * Creates a new file attribute matcher, which matches attributes
2195  * against a given string. #GFileAttributeMatcher<!-- -->s are reference
2196  * counted structures, and are created with a reference count of 1. If
2197  * the number of references falls to 0, the #GFileAttributeMatcher is
2198  * automatically destroyed.
2199  *
2200  * The @attribute string should be formatted with specific keys separated
2201  * from namespaces with a double colon. Several "namespace::key" strings may be
2202  * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
2203  * The wildcard "*" may be used to match all keys and namespaces, or
2204  * "namespace::*" will match all keys in a given namespace.
2205  *
2206  * Examples of strings to use:
2207  * <table>
2208  * <title>File Attribute Matcher strings and results</title>
2209  * <tgroup cols='2' align='left'><thead>
2210  * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
2211  * <tbody>
2212  * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
2213  * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
2214  * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
2215  * all keys in the unix namespace.</entry></row>
2216  * </tbody></tgroup>
2217  * </table>
2218  *
2219  * Returns: a #GFileAttributeMatcher.
2220  **/
2221 GFileAttributeMatcher *
2222 g_file_attribute_matcher_new (const char *attributes)
2223 {
2224   char **split;
2225   char *colon;
2226   int i;
2227   GFileAttributeMatcher *matcher;
2228
2229   if (attributes == NULL || *attributes == '\0')
2230     return NULL;
2231
2232   matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2233   matcher->ref = 1;
2234   matcher->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2235
2236   split = g_strsplit (attributes, ",", -1);
2237
2238   for (i = 0; split[i] != NULL; i++)
2239     {
2240       if (strcmp (split[i], "*") == 0)
2241         matcher->all = TRUE;
2242       else
2243         {
2244           SubMatcher s;
2245
2246           colon = strstr (split[i], "::");
2247           if (colon != NULL &&
2248               !(colon[2] == 0 ||
2249                 (colon[2] == '*' &&
2250                  colon[3] == 0)))
2251             {
2252               s.id = lookup_attribute (split[i]);
2253               s.mask = 0xffffffff;
2254             }
2255           else
2256             {
2257               if (colon)
2258                 *colon = 0;
2259
2260               s.id = lookup_namespace (split[i]) << NS_POS;
2261               s.mask = NS_MASK << NS_POS;
2262             }
2263
2264           g_array_append_val (matcher->sub_matchers, s);
2265         }
2266     }
2267
2268   g_strfreev (split);
2269
2270   matcher = matcher_optimize (matcher);
2271
2272   return matcher;
2273 }
2274
2275 /**
2276  * g_file_attribute_matcher_subtract:
2277  * @matcher: Matcher to subtract from 
2278  * @subtract: The matcher to subtract
2279  *
2280  * Subtracts all attributes of @subtract from @matcher and returns
2281  * a matcher that supports those attributes.
2282  *
2283  * Note that currently it is not possible to remove a single
2284  * attribute when the @matcher matches the whole namespace - or remove
2285  * a namespace or attribute when the matcher matches everything. This
2286  * is a limitation of the current implementation, but may be fixed
2287  * in the future.
2288  *
2289  * Returns: A file attribute matcher matching all attributes of
2290  *     @matcher that are not matched by @subtract
2291  **/
2292 GFileAttributeMatcher *
2293 g_file_attribute_matcher_subtract (GFileAttributeMatcher *matcher,
2294                                    GFileAttributeMatcher *subtract)
2295 {
2296   GFileAttributeMatcher *result;
2297   guint mi, si;
2298   SubMatcher *msub, *ssub;
2299
2300   if (matcher == NULL)
2301     return NULL;
2302   if (subtract == NULL)
2303     return g_file_attribute_matcher_ref (matcher);
2304   if (subtract->all)
2305     return NULL;
2306   if (matcher->all)
2307     return g_file_attribute_matcher_ref (matcher);
2308
2309   result = g_malloc0 (sizeof (GFileAttributeMatcher));
2310   result->ref = 1;
2311   result->sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
2312
2313   si = 0;
2314   g_assert (subtract->sub_matchers->len > 0);
2315   ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);
2316
2317   for (mi = 0; mi < matcher->sub_matchers->len; mi++)
2318     {
2319       msub = &g_array_index (matcher->sub_matchers, SubMatcher, mi);
2320
2321 retry:
2322       if (sub_matcher_matches (ssub, msub))
2323         continue;
2324
2325       si++;
2326       if (si >= subtract->sub_matchers->len)
2327         break;
2328
2329       ssub = &g_array_index (subtract->sub_matchers, SubMatcher, si);
2330       if (ssub->id <= msub->id)
2331         goto retry;
2332
2333       g_array_append_val (result->sub_matchers, *msub);
2334     }
2335
2336   if (mi < matcher->sub_matchers->len)
2337     g_array_append_vals (result->sub_matchers,
2338                          &g_array_index (matcher->sub_matchers, SubMatcher, mi),
2339                          matcher->sub_matchers->len - mi);
2340
2341   result = matcher_optimize (result);
2342
2343   return result;
2344 }
2345
2346 /**
2347  * g_file_attribute_matcher_ref:
2348  * @matcher: a #GFileAttributeMatcher.
2349  *
2350  * References a file attribute matcher.
2351  *
2352  * Returns: a #GFileAttributeMatcher.
2353  **/
2354 GFileAttributeMatcher *
2355 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2356 {
2357   if (matcher)
2358     {
2359       g_return_val_if_fail (matcher->ref > 0, NULL);
2360       g_atomic_int_inc (&matcher->ref);
2361     }
2362   return matcher;
2363 }
2364
2365 /**
2366  * g_file_attribute_matcher_unref:
2367  * @matcher: a #GFileAttributeMatcher.
2368  *
2369  * Unreferences @matcher. If the reference count falls below 1,
2370  * the @matcher is automatically freed.
2371  *
2372  **/
2373 void
2374 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2375 {
2376   if (matcher)
2377     {
2378       g_return_if_fail (matcher->ref > 0);
2379
2380       if (g_atomic_int_dec_and_test (&matcher->ref))
2381         {
2382           if (matcher->sub_matchers)
2383             g_array_free (matcher->sub_matchers, TRUE);
2384
2385           g_free (matcher);
2386         }
2387     }
2388 }
2389
2390 /**
2391  * g_file_attribute_matcher_matches_only:
2392  * @matcher: a #GFileAttributeMatcher.
2393  * @attribute: a file attribute key.
2394  *
2395  * Checks if a attribute matcher only matches a given attribute. Always
2396  * returns %FALSE if "*" was used when creating the matcher.
2397  *
2398  * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2399  **/
2400 gboolean
2401 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2402                                        const char            *attribute)
2403 {
2404   SubMatcher *sub_matcher;
2405   guint32 id;
2406
2407   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2408
2409   if (matcher == NULL ||
2410       matcher->all)
2411     return FALSE;
2412
2413   if (matcher->sub_matchers->len != 1)
2414     return FALSE;
2415   
2416   id = lookup_attribute (attribute);
2417   
2418   sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, 0);
2419   
2420   return sub_matcher->id == id &&
2421          sub_matcher->mask == 0xffffffff;
2422 }
2423
2424 static gboolean
2425 matcher_matches_id (GFileAttributeMatcher *matcher,
2426                     guint32                id)
2427 {
2428   SubMatcher *sub_matchers;
2429   int i;
2430
2431   if (matcher->sub_matchers)
2432     {
2433       sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
2434       for (i = 0; i < matcher->sub_matchers->len; i++)
2435         {
2436           if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2437             return TRUE;
2438         }
2439     }
2440
2441   return FALSE;
2442 }
2443
2444 gboolean
2445 _g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2446                                       guint32                id)
2447 {
2448   /* We return a NULL matcher for an empty match string, so handle this */
2449   if (matcher == NULL)
2450     return FALSE;
2451
2452   if (matcher->all)
2453     return TRUE;
2454
2455   return matcher_matches_id (matcher, id);
2456 }
2457
2458 /**
2459  * g_file_attribute_matcher_matches:
2460  * @matcher: a #GFileAttributeMatcher.
2461  * @attribute: a file attribute key.
2462  *
2463  * Checks if an attribute will be matched by an attribute matcher. If
2464  * the matcher was created with the "*" matching string, this function
2465  * will always return %TRUE.
2466  *
2467  * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2468  **/
2469 gboolean
2470 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2471                                   const char            *attribute)
2472 {
2473   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2474
2475   /* We return a NULL matcher for an empty match string, so handle this */
2476   if (matcher == NULL)
2477     return FALSE;
2478
2479   if (matcher->all)
2480     return TRUE;
2481
2482   return matcher_matches_id (matcher, lookup_attribute (attribute));
2483 }
2484
2485 /* return TRUE -> all */
2486 /**
2487  * g_file_attribute_matcher_enumerate_namespace:
2488  * @matcher: a #GFileAttributeMatcher.
2489  * @ns: a string containing a file attribute namespace.
2490  *
2491  * Checks if the matcher will match all of the keys in a given namespace.
2492  * This will always return %TRUE if a wildcard character is in use (e.g. if
2493  * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2494  * using "*" and namespace is anything.)
2495  *
2496  * TODO: this is awkwardly worded.
2497  *
2498  * Returns: %TRUE if the matcher matches all of the entries
2499  * in the given @ns, %FALSE otherwise.
2500  **/
2501 gboolean
2502 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2503                                               const char            *ns)
2504 {
2505   SubMatcher *sub_matchers;
2506   int ns_id;
2507   int i;
2508
2509   g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2510
2511   /* We return a NULL matcher for an empty match string, so handle this */
2512   if (matcher == NULL)
2513     return FALSE;
2514
2515   if (matcher->all)
2516     return TRUE;
2517
2518   ns_id = lookup_namespace (ns) << NS_POS;
2519
2520   if (matcher->sub_matchers)
2521     {
2522       sub_matchers = (SubMatcher *)matcher->sub_matchers->data;
2523       for (i = 0; i < matcher->sub_matchers->len; i++)
2524         {
2525           if (sub_matchers[i].id == ns_id)
2526             return TRUE;
2527         }
2528     }
2529
2530   matcher->iterator_ns = ns_id;
2531   matcher->iterator_pos = 0;
2532
2533   return FALSE;
2534 }
2535
2536 /**
2537  * g_file_attribute_matcher_enumerate_next:
2538  * @matcher: a #GFileAttributeMatcher.
2539  *
2540  * Gets the next matched attribute from a #GFileAttributeMatcher.
2541  *
2542  * Returns: a string containing the next attribute or %NULL if
2543  * no more attribute exist.
2544  **/
2545 const char *
2546 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2547 {
2548   int i;
2549   SubMatcher *sub_matcher;
2550
2551   /* We return a NULL matcher for an empty match string, so handle this */
2552   if (matcher == NULL)
2553     return NULL;
2554
2555   while (1)
2556     {
2557       i = matcher->iterator_pos++;
2558
2559       if (matcher->sub_matchers == NULL)
2560         return NULL;
2561
2562       if (i < matcher->sub_matchers->len)
2563         sub_matcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2564       else
2565         return NULL;
2566
2567       if (sub_matcher->mask == 0xffffffff &&
2568           (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2569         return get_attribute_for_id (sub_matcher->id);
2570     }
2571 }
2572
2573 /**
2574  * g_file_attribute_matcher_to_string:
2575  * @matcher: (allow-none): a #GFileAttributeMatcher.
2576  *
2577  * Prints what the matcher is matching against. The format will be 
2578  * equal to the format passed to g_file_attribute_matcher_new().
2579  * The output however, might not be identical, as the matcher may
2580  * decide to use a different order or omit needless parts.
2581  *
2582  * Returns: a string describing the attributes the matcher matches
2583  *   against or %NULL if @matcher was %NULL.
2584  *
2585  * Since: 2.32
2586  **/
2587 char *
2588 g_file_attribute_matcher_to_string (GFileAttributeMatcher *matcher)
2589 {
2590   GString *string;
2591   guint i;
2592
2593   if (matcher == NULL)
2594     return NULL;
2595
2596   if (matcher->all)
2597     return g_strdup ("*");
2598
2599   string = g_string_new ("");
2600   for (i = 0; i < matcher->sub_matchers->len; i++)
2601     {
2602       SubMatcher *submatcher = &g_array_index (matcher->sub_matchers, SubMatcher, i);
2603
2604       if (i > 0)
2605         g_string_append_c (string, ',');
2606
2607       g_string_append (string, get_attribute_for_id (submatcher->id));
2608     }
2609
2610   return g_string_free (string, FALSE);
2611 }