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