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