split lookup_attribute() into two functions
[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 "gfileattribute-priv.h"
61 #include "gicon.h"
62 #include "glibintl.h"
63
64 #include "gioalias.h"
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 static gboolean g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
89                                                      guint32 id);
90
91 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
92
93 typedef struct {
94   guint32 id;
95   guint32 attribute_id_counter;
96 } NSInfo;
97
98 G_LOCK_DEFINE_STATIC (attribute_hash);
99 static int namespace_id_counter = 0;
100 static GHashTable *ns_hash = NULL;
101 static GHashTable *attribute_hash = NULL;
102 static char ***attributes = NULL;
103
104 /* Attribute ids are 32bit, we split it up like this:
105  * |------------|--------------------|
106  *   12 bit          20 bit       
107  *   namespace      attribute id    
108  *
109  * This way the attributes gets sorted in namespace order
110  */
111
112 #define NS_POS 20
113 #define NS_MASK ((guint32)((1<<12) - 1))
114 #define ID_POS 0
115 #define ID_MASK ((guint32)((1<<20) - 1))
116
117 #define GET_NS(_attr_id) \
118     (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
119 #define GET_ID(_attr_id) \
120     (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
121
122 #define MAKE_ATTR_ID(_ns, _id)                          \
123     ( ((((guint32) _ns) & NS_MASK) << NS_POS) |         \
124       ((((guint32) _id) & ID_MASK) << ID_POS) )
125
126 static NSInfo *
127 _lookup_namespace (const char *namespace)
128 {
129   NSInfo *ns_info;
130   
131   ns_info = g_hash_table_lookup (ns_hash, namespace);
132   if (ns_info == NULL)
133     {
134       ns_info = g_new0 (NSInfo, 1);
135       ns_info->id = ++namespace_id_counter;
136       g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
137       attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
138       attributes[ns_info->id] = NULL;
139     }
140   return ns_info;
141 }
142
143 static guint32
144 _lookup_attribute (const char *attribute)
145 {
146   guint32 attr_id, id;
147   char *ns;
148   const char *colon;
149   NSInfo *ns_info;
150   
151   attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
152
153   if (attr_id != 0)
154     return attr_id;
155
156   colon = strstr (attribute, "::");
157   if (colon)
158     ns = g_strndup (attribute, colon - attribute);
159   else
160     ns = g_strdup ("");
161
162   ns_info = _lookup_namespace (ns);
163   g_free (ns);
164
165   id = ++ns_info->attribute_id_counter;
166   attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
167   attributes[ns_info->id][id] = g_strdup (attribute);
168   
169   attr_id = MAKE_ATTR_ID (ns_info->id, id);
170
171   g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
172
173   return attr_id;
174 }
175
176 static void
177 ensure_attribute_hash (void)
178 {
179   if (attribute_hash != NULL)
180     return;
181
182   ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
183   attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
184
185
186 }
187
188 static guint32
189 lookup_namespace (const char *namespace)
190 {
191   NSInfo *ns_info;
192   guint32 id;
193   
194   G_LOCK (attribute_hash);
195   
196   ensure_attribute_hash ();
197
198   ns_info = _lookup_namespace (namespace);
199   id = 0;
200   if (ns_info)
201     id = ns_info->id;
202   
203   G_UNLOCK (attribute_hash);
204
205   return id;
206 }
207
208 static char *
209 get_attribute_for_id (int attribute)
210 {
211   char *s;
212   G_LOCK (attribute_hash);
213   s = attributes[GET_NS(attribute)][GET_ID(attribute)];
214   G_UNLOCK (attribute_hash);
215   return s;
216 }
217
218 static guint32
219 lookup_attribute (const char *attribute)
220 {
221   guint32 attr_id;
222   
223   G_LOCK (attribute_hash);
224   ensure_attribute_hash ();
225
226   attr_id = _lookup_attribute (attribute);
227   
228   G_UNLOCK (attribute_hash);
229   
230   return attr_id;
231 }
232
233 static void
234 g_file_info_finalize (GObject *object)
235 {
236   GFileInfo *info;
237   int i;
238   GFileAttribute *attrs;
239
240   info = G_FILE_INFO (object);
241
242   attrs = (GFileAttribute *)info->attributes->data;
243   for (i = 0; i < info->attributes->len; i++)
244     _g_file_attribute_value_clear (&attrs[i].value);
245   g_array_free (info->attributes, TRUE);  
246
247   if (info->mask != NO_ATTRIBUTE_MASK)
248     g_file_attribute_matcher_unref (info->mask);
249
250   G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
251 }
252
253 static void
254 g_file_info_class_init (GFileInfoClass *klass)
255 {
256   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
257   
258   gobject_class->finalize = g_file_info_finalize;
259 }
260
261 static void
262 g_file_info_init (GFileInfo *info)
263 {
264   info->mask = NO_ATTRIBUTE_MASK;
265   info->attributes = g_array_new (FALSE, FALSE,
266                                   sizeof (GFileAttribute));
267 }
268
269 /**
270  * g_file_info_new:
271  * 
272  * Creates a new file info structure.
273  * 
274  * Returns: a #GFileInfo.
275  **/
276 GFileInfo *
277 g_file_info_new (void)
278 {
279   return g_object_new (G_TYPE_FILE_INFO, NULL);
280 }
281
282 /**
283  * g_file_info_copy_into:
284  * @src_info: source to copy attributes from.
285  * @dest_info: destination to copy attributes to.
286  * 
287  * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
288  **/
289 void
290 g_file_info_copy_into (GFileInfo *src_info, 
291                        GFileInfo *dest_info)
292 {
293   GFileAttribute *source, *dest;
294   int i;
295
296   g_return_if_fail (G_IS_FILE_INFO (src_info));
297   g_return_if_fail (G_IS_FILE_INFO (dest_info));
298
299   dest = (GFileAttribute *)dest_info->attributes->data;
300   for (i = 0; i < dest_info->attributes->len; i++)
301     _g_file_attribute_value_clear (&dest[i].value);
302   
303   g_array_set_size (dest_info->attributes,
304                     src_info->attributes->len);
305
306   source = (GFileAttribute *)src_info->attributes->data;
307   dest = (GFileAttribute *)dest_info->attributes->data;
308   
309   for (i = 0; i < src_info->attributes->len; i++)
310     {
311       dest[i].attribute = source[i].attribute;
312       dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
313       _g_file_attribute_value_set (&dest[i].value, &source[i].value);
314     }
315
316   if (dest_info->mask != NO_ATTRIBUTE_MASK)
317     g_file_attribute_matcher_unref (dest_info->mask);
318
319   if (src_info->mask == NO_ATTRIBUTE_MASK)
320     dest_info->mask = NO_ATTRIBUTE_MASK;
321   else
322     dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
323 }
324
325 /**
326  * g_file_info_dup:
327  * @other: a #GFileInfo.
328  * 
329  * Duplicates a file info structure.
330  * 
331  * Returns: a duplicate #GFileInfo of @other.
332  **/
333 GFileInfo *
334 g_file_info_dup (GFileInfo *other)
335 {
336   GFileInfo *new;
337   
338   g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
339   
340   new = g_file_info_new ();
341   g_file_info_copy_into (other, new);
342   return new;
343 }
344
345 /**
346  * g_file_info_set_attribute_mask:
347  * @info: a #GFileInfo.
348  * @mask: a #GFileAttributeMatcher.
349  *
350  * Sets @mask on @info to match specific attribute types.
351  **/
352 void
353 g_file_info_set_attribute_mask (GFileInfo             *info,
354                                 GFileAttributeMatcher *mask)
355 {
356   GFileAttribute *attr;
357   int i;
358   
359   g_return_if_fail (G_IS_FILE_INFO (info));
360   
361   if (mask != info->mask)
362     {
363       if (info->mask != NO_ATTRIBUTE_MASK)
364         g_file_attribute_matcher_unref (info->mask);
365       info->mask = g_file_attribute_matcher_ref (mask);
366
367       /* Remove non-matching attributes */
368       for (i = 0; i < info->attributes->len; i++)
369         {
370           attr = &g_array_index (info->attributes, GFileAttribute, i);
371           if (!g_file_attribute_matcher_matches_id (mask,
372                                                     attr->attribute))
373             {
374               _g_file_attribute_value_clear (&attr->value);
375               g_array_remove_index (info->attributes, i);
376               i--;
377             }
378         }
379     }
380 }
381
382 /**
383  * g_file_info_unset_attribute_mask:
384  * @info: #GFileInfo.
385  * 
386  * Unsets a mask set by g_file_info_set_attribute_mask(), if one
387  * is set.
388  **/
389 void
390 g_file_info_unset_attribute_mask (GFileInfo *info)
391 {
392   g_return_if_fail (G_IS_FILE_INFO (info));
393
394   if (info->mask != NO_ATTRIBUTE_MASK)
395     g_file_attribute_matcher_unref (info->mask);
396   info->mask = NO_ATTRIBUTE_MASK;
397 }
398
399 /**
400  * g_file_info_clear_status:
401  * @info: a #GFileInfo.
402  *
403  * Clears the status information from @info.
404  **/
405 void
406 g_file_info_clear_status (GFileInfo  *info)
407 {
408   GFileAttribute *attrs;
409   int i;
410   
411   g_return_if_fail (G_IS_FILE_INFO (info));
412
413   attrs = (GFileAttribute *)info->attributes->data;
414   for (i = 0; i < info->attributes->len; i++)
415     attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
416 }
417
418 static int
419 g_file_info_find_place (GFileInfo  *info,
420                         guint32     attribute)
421 {
422   int min, max, med;
423   GFileAttribute *attrs;
424   /* Binary search for the place where attribute would be, if it's
425      in the array */
426
427   min = 0;
428   max = info->attributes->len;
429
430   attrs = (GFileAttribute *)info->attributes->data;
431
432   while (min < max)
433     {
434       med = min + (max - min) / 2;
435       if (attrs[med].attribute == attribute)
436         {
437           min = med;
438           break;
439         }
440       else if (attrs[med].attribute < attribute)
441         min = med + 1;
442       else /* attrs[med].attribute > attribute */
443         max = med;
444     }
445
446   return min;
447 }
448
449 static GFileAttributeValue *
450 g_file_info_find_value (GFileInfo *info,
451                         guint32    attr_id)
452 {
453   GFileAttribute *attrs;
454   int i;
455
456   i = g_file_info_find_place (info, attr_id);
457   attrs = (GFileAttribute *)info->attributes->data;
458   if (i < info->attributes->len &&
459       attrs[i].attribute == attr_id)
460     return &attrs[i].value;
461   
462   return NULL;
463 }
464
465 static GFileAttributeValue *
466 g_file_info_find_value_by_name (GFileInfo  *info,
467                                 const char *attribute)
468 {
469   guint32 attr_id;
470
471   attr_id = lookup_attribute (attribute);
472   return g_file_info_find_value (info, attr_id);
473 }
474
475 /**
476  * g_file_info_has_attribute:
477  * @info: a #GFileInfo.
478  * @attribute: a file attribute key.
479  * 
480  * Checks if a file info structure has an attribute named @attribute.
481  * 
482  * Returns: %TRUE if @Ginfo has an attribute named @attribute, 
483  *     %FALSE otherwise.
484  **/
485 gboolean
486 g_file_info_has_attribute (GFileInfo  *info,
487                            const char *attribute)
488 {
489   GFileAttributeValue *value;
490
491   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
492   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
493
494   value = g_file_info_find_value_by_name (info, attribute);
495   return value != NULL;
496 }
497
498 /**
499  * g_file_info_has_namespace:
500  * @info: a #GFileInfo.
501  * @name_space: a file attribute namespace.
502  *
503  * Checks if a file info structure has an attribute in the
504  * specified @name_space.
505  *
506  * Returns: %TRUE if @Ginfo has an attribute in @name_space,
507  *     %FALSE otherwise.
508  *
509  * Since: 2.22
510  **/
511 gboolean
512 g_file_info_has_namespace (GFileInfo  *info,
513                            const char *name_space)
514 {
515   GFileAttribute *attrs;
516   guint32 ns_id;
517   int i;
518
519   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
520   g_return_val_if_fail (name_space != NULL, FALSE);
521
522   ns_id = lookup_namespace (name_space);
523
524   attrs = (GFileAttribute *)info->attributes->data;
525   for (i = 0; i < info->attributes->len; i++)
526     {
527       if (GET_NS (attrs[i].attribute) == ns_id)
528         return TRUE;
529     }
530
531   return FALSE;
532 }
533
534 /**
535  * g_file_info_list_attributes:
536  * @info: a #GFileInfo.
537  * @name_space: a file attribute key's namespace.
538  * 
539  * Lists the file info structure's attributes.
540  * 
541  * Returns: a null-terminated array of strings of all of the 
542  * possible attribute types for the given @name_space, or 
543  * %NULL on error.
544  **/
545 char **
546 g_file_info_list_attributes (GFileInfo  *info,
547                              const char *name_space)
548 {
549   GPtrArray *names;
550   GFileAttribute *attrs;
551   guint32 attribute;
552   guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
553   int i;
554  
555   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
556
557   names = g_ptr_array_new ();
558   attrs = (GFileAttribute *)info->attributes->data;
559   for (i = 0; i < info->attributes->len; i++)
560     {
561       attribute = attrs[i].attribute;
562       if (ns_id == 0 || GET_NS (attribute) == ns_id)
563         g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
564     }
565
566   /* NULL terminate */
567   g_ptr_array_add (names, NULL);
568   
569   return (char **)g_ptr_array_free (names, FALSE);
570 }
571
572 /**
573  * g_file_info_get_attribute_type:
574  * @info: a #GFileInfo.
575  * @attribute: a file attribute key.
576  * 
577  * Gets the attribute type for an attribute key.
578  * 
579  * Returns: a #GFileAttributeType for the given @attribute, or 
580  * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
581  **/
582 GFileAttributeType
583 g_file_info_get_attribute_type (GFileInfo  *info,
584                                 const char *attribute)
585 {
586   GFileAttributeValue *value;
587   
588   g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
589   g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
590
591   value = g_file_info_find_value_by_name (info, attribute);
592   if (value)
593     return value->type;
594   else
595     return G_FILE_ATTRIBUTE_TYPE_INVALID;
596 }
597
598 /**
599  * g_file_info_remove_attribute:
600  * @info: a #GFileInfo.
601  * @attribute: a file attribute key.
602  * 
603  * Removes all cases of @attribute from @info if it exists.
604  **/
605 void
606 g_file_info_remove_attribute (GFileInfo  *info,
607                               const char *attribute)
608 {
609   guint32 attr_id;
610   GFileAttribute *attrs;
611   int i;
612
613   g_return_if_fail (G_IS_FILE_INFO (info));
614   g_return_if_fail (attribute != NULL && *attribute != '\0');
615
616   attr_id = lookup_attribute (attribute);
617   
618   i = g_file_info_find_place (info, attr_id);
619   attrs = (GFileAttribute *)info->attributes->data;
620   if (i < info->attributes->len &&
621       attrs[i].attribute == attr_id)
622     {
623       _g_file_attribute_value_clear (&attrs[i].value);
624       g_array_remove_index (info->attributes, i);
625     }
626 }
627
628 /**
629  * g_file_info_get_attribute_data:
630  * @info: a #GFileInfo
631  * @attribute: a file attribute key
632  * @type: return location for the attribute type, or %NULL
633  * @value_pp: return location for the attribute value, or %NULL
634  * @status: return location for the attribute status, or %NULL
635  *
636  * Gets the attribute type, value and status for an attribute key.
637  *
638  * Returns: %TRUE if @info has an attribute named @attribute, 
639  *      %FALSE otherwise.
640  */
641 gboolean
642 g_file_info_get_attribute_data (GFileInfo            *info,
643                                 const char           *attribute,
644                                 GFileAttributeType   *type,
645                                 gpointer             *value_pp,
646                                 GFileAttributeStatus *status)
647 {
648   GFileAttributeValue *value;
649
650   value = g_file_info_find_value_by_name (info, attribute);
651   if (value == NULL)
652     return FALSE;
653
654   if (status)
655     *status = value->status;
656
657   if (type)
658     *type = value->type;
659
660   if (value_pp)
661     *value_pp = _g_file_attribute_value_peek_as_pointer (value);
662   
663   return TRUE;
664 }
665
666 /** 
667  * g_file_info_get_attribute_status:
668  * @info: a #GFileInfo
669  * @attribute: a file attribute key
670  *
671  * Gets the attribute status for an attribute key.
672  *
673  * Returns: a #GFileAttributeStatus for the given @attribute, or 
674  *    %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
675  *
676  */
677 GFileAttributeStatus
678 g_file_info_get_attribute_status (GFileInfo  *info,
679                                   const char *attribute)
680 {
681   GFileAttributeValue *val;
682   
683   g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
684   g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
685
686   val = g_file_info_find_value_by_name (info, attribute);
687   if (val)
688     return val->status;
689
690   return G_FILE_ATTRIBUTE_STATUS_UNSET;
691 }
692
693 /**
694  * g_file_info_set_attribute_status:
695  * @info: a #GFileInfo
696  * @attribute: a file attribute key
697  * @status: a #GFileAttributeStatus
698  *
699  * Sets the attribute status for an attribute key. This is only
700  * needed by external code that implement g_file_set_attributes_from_info()
701  * or similar functions.
702  *
703  * The attribute must exist in @info for this to work. Otherwise %FALSE
704  * is returned and @info is unchanged.
705  *
706  * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
707  *
708  * Since: 2.22
709  */
710 gboolean
711 g_file_info_set_attribute_status (GFileInfo  *info,
712                                   const char *attribute,
713                                   GFileAttributeStatus status)
714 {
715   GFileAttributeValue *val;
716
717   g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
718   g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
719
720   val = g_file_info_find_value_by_name (info, attribute);
721   if (val)
722     {
723       val->status = status;
724       return TRUE;
725     }
726
727   return FALSE;
728 }
729
730 GFileAttributeValue *
731 _g_file_info_get_attribute_value (GFileInfo  *info,
732                                   const char *attribute)
733   
734 {
735   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
736   g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
737
738   return g_file_info_find_value_by_name (info, attribute);
739 }
740
741 /**
742  * g_file_info_get_attribute_as_string:
743  * @info: a #GFileInfo.
744  * @attribute: a file attribute key.
745  * 
746  * Gets the value of a attribute, formated as a string.
747  * This escapes things as needed to make the string valid
748  * utf8.
749  * 
750  * Returns: a UTF-8 string associated with the given @attribute.
751  *    When you're done with the string it must be freed with g_free().
752  **/
753 char *
754 g_file_info_get_attribute_as_string (GFileInfo  *info,
755                                      const char *attribute)
756 {
757   GFileAttributeValue *val;
758   val = _g_file_info_get_attribute_value (info, attribute);
759   if (val) 
760     return _g_file_attribute_value_as_string (val);
761   return NULL;
762 }
763
764
765 /**
766  * g_file_info_get_attribute_object:
767  * @info: a #GFileInfo.
768  * @attribute: a file attribute key.
769  * 
770  * Gets the value of a #GObject attribute. If the attribute does 
771  * not contain a #GObject, %NULL will be returned.
772  * 
773  * Returns: a #GObject associated with the given @attribute, or
774  * %NULL otherwise.
775  **/
776 GObject *
777 g_file_info_get_attribute_object (GFileInfo  *info,
778                                   const char *attribute)
779 {
780   GFileAttributeValue *value;
781   
782   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
783   g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
784
785   value = g_file_info_find_value_by_name (info, attribute);
786   return _g_file_attribute_value_get_object (value);
787 }
788
789 /**
790  * g_file_info_get_attribute_string:
791  * @info: a #GFileInfo.
792  * @attribute: a file attribute key.
793  * 
794  * Gets the value of a string attribute. If the attribute does 
795  * not contain a string, %NULL will be returned.
796  * 
797  * Returns: the contents of the @attribute value as a string, or 
798  * %NULL otherwise.
799  **/
800 const char *
801 g_file_info_get_attribute_string (GFileInfo  *info,
802                                   const char *attribute)
803 {
804   GFileAttributeValue *value;
805   
806   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
807   g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
808
809   value = g_file_info_find_value_by_name (info, attribute);
810   return _g_file_attribute_value_get_string (value);
811 }
812
813 /**
814  * g_file_info_get_attribute_byte_string:
815  * @info: a #GFileInfo.
816  * @attribute: a file attribute key.
817  * 
818  * Gets the value of a byte string attribute. If the attribute does 
819  * not contain a byte string, %NULL will be returned.
820  * 
821  * Returns: the contents of the @attribute value as a byte string, or 
822  * %NULL otherwise.
823  **/
824 const char *
825 g_file_info_get_attribute_byte_string (GFileInfo  *info,
826                                        const char *attribute)
827 {
828   GFileAttributeValue *value;
829   
830   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
831   g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
832
833   value = g_file_info_find_value_by_name (info, attribute);
834   return _g_file_attribute_value_get_byte_string (value);
835 }
836
837 /**
838  * g_file_info_get_attribute_stringv:
839  * @info: a #GFileInfo.
840  * @attribute: a file attribute key.
841  *
842  * Gets the value of a stringv attribute. If the attribute does
843  * not contain a stringv, %NULL will be returned.
844  *
845  * Returns: the contents of the @attribute value as a stringv, or
846  * %NULL otherwise. Do not free.
847  **/
848 char **
849 g_file_info_get_attribute_stringv (GFileInfo  *info,
850                                    const char *attribute)
851 {
852   GFileAttributeValue *value;
853
854   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
855   g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
856
857   value = g_file_info_find_value_by_name (info, attribute);
858   return _g_file_attribute_value_get_stringv (value);
859 }
860
861 /**
862  * g_file_info_get_attribute_boolean:
863  * @info: a #GFileInfo.
864  * @attribute: a file attribute key.
865  * 
866  * Gets the value of a boolean attribute. If the attribute does not
867  * contain a boolean value, %FALSE will be returned.
868  * 
869  * Returns: the boolean value contained within the attribute. 
870  **/
871 gboolean
872 g_file_info_get_attribute_boolean (GFileInfo  *info,
873                                    const char *attribute)
874 {
875   GFileAttributeValue *value;
876   
877   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
878   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
879
880   value = g_file_info_find_value_by_name (info, attribute);
881   return _g_file_attribute_value_get_boolean (value);
882 }
883
884 /**
885  * g_file_info_get_attribute_uint32:
886  * @info: a #GFileInfo.
887  * @attribute: a file attribute key.
888  * 
889  * Gets an unsigned 32-bit integer contained within the attribute. If the 
890  * attribute does not contain an unsigned 32-bit integer, or is invalid, 
891  * 0 will be returned.
892  * 
893  * Returns: an unsigned 32-bit integer from the attribute. 
894  **/
895 guint32
896 g_file_info_get_attribute_uint32 (GFileInfo  *info,
897                                   const char *attribute)
898 {
899   GFileAttributeValue *value;
900   
901   g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
902   g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
903
904   value = g_file_info_find_value_by_name (info, attribute);
905   return _g_file_attribute_value_get_uint32 (value);
906 }
907
908 /**
909  * g_file_info_get_attribute_int32:
910  * @info: a #GFileInfo.
911  * @attribute: a file attribute key.
912  * 
913  * Gets a signed 32-bit integer contained within the attribute. If the 
914  * attribute does not contain a signed 32-bit integer, or is invalid, 
915  * 0 will be returned.
916  * 
917  * Returns: a signed 32-bit integer from the attribute.
918  **/
919 gint32
920 g_file_info_get_attribute_int32 (GFileInfo  *info,
921                                  const char *attribute)
922 {
923   GFileAttributeValue *value;
924
925   g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
926   g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
927
928   value = g_file_info_find_value_by_name (info, attribute);
929   return _g_file_attribute_value_get_int32 (value);
930 }
931
932 /**
933  * g_file_info_get_attribute_uint64:
934  * @info: a #GFileInfo.
935  * @attribute: a file attribute key.
936  * 
937  * Gets a unsigned 64-bit integer contained within the attribute. If the 
938  * attribute does not contain an unsigned 64-bit integer, or is invalid, 
939  * 0 will be returned.
940  * 
941  * Returns: a unsigned 64-bit integer from the attribute. 
942  **/
943 guint64
944 g_file_info_get_attribute_uint64 (GFileInfo  *info,
945                                   const char *attribute)
946 {
947   GFileAttributeValue *value;
948
949   g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
950   g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
951
952   value = g_file_info_find_value_by_name (info, attribute);
953   return _g_file_attribute_value_get_uint64 (value);
954 }
955
956 /**
957  * g_file_info_get_attribute_int64:
958  * @info: a #GFileInfo.
959  * @attribute: a file attribute key.
960  * 
961  * Gets a signed 64-bit integer contained within the attribute. If the 
962  * attribute does not contain an signed 64-bit integer, or is invalid, 
963  * 0 will be returned.
964  * 
965  * Returns: a signed 64-bit integer from the attribute. 
966  **/
967 gint64
968 g_file_info_get_attribute_int64  (GFileInfo  *info,
969                                   const char *attribute)
970 {
971   GFileAttributeValue *value;
972
973   g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
974   g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
975
976   value = g_file_info_find_value_by_name (info, attribute);
977   return _g_file_attribute_value_get_int64 (value);
978 }
979
980 static GFileAttributeValue *
981 g_file_info_create_value (GFileInfo *info,
982                           guint32 attr_id)
983 {
984   GFileAttribute *attrs;
985   int i;
986
987   if (info->mask != NO_ATTRIBUTE_MASK &&
988       !g_file_attribute_matcher_matches_id (info->mask, attr_id))
989     return NULL;
990   
991   i = g_file_info_find_place (info, attr_id);
992   
993   attrs = (GFileAttribute *)info->attributes->data;
994   if (i < info->attributes->len &&
995       attrs[i].attribute == attr_id)
996     return &attrs[i].value;
997   else
998     {
999       GFileAttribute attr = { 0 };
1000       attr.attribute = attr_id;
1001       g_array_insert_val (info->attributes, i, attr);
1002
1003       attrs = (GFileAttribute *)info->attributes->data;
1004       return &attrs[i].value;
1005     }
1006 }
1007
1008 static GFileAttributeValue *
1009 g_file_info_create_value_by_name (GFileInfo *info,
1010                                   const char *attribute)
1011 {
1012   guint32 attr_id;
1013
1014   attr_id = lookup_attribute (attribute);
1015
1016   return g_file_info_create_value (info, attr_id);
1017 }
1018
1019 /**
1020  * g_file_info_set_attribute:
1021  * @info: a #GFileInfo.
1022  * @attribute: a file attribute key.
1023  * @type: a #GFileAttributeType
1024  * @value_p: pointer to the value
1025  * 
1026  * Sets the @attribute to contain the given value, if possible.
1027  **/
1028 void
1029 g_file_info_set_attribute (GFileInfo                 *info,
1030                            const char                *attribute,
1031                            GFileAttributeType         type,
1032                            gpointer                   value_p)
1033 {
1034   GFileAttributeValue *value;
1035
1036   g_return_if_fail (G_IS_FILE_INFO (info));
1037   g_return_if_fail (attribute != NULL && *attribute != '\0');
1038
1039   value = g_file_info_create_value_by_name (info, attribute);
1040
1041   if (value)
1042     _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1043 }
1044
1045 /**
1046  * g_file_info_set_attribute_object:
1047  * @info: a #GFileInfo.
1048  * @attribute: a file attribute key.
1049  * @attr_value: a #GObject.
1050  * 
1051  * Sets the @attribute to contain the given @attr_value, 
1052  * if possible.
1053  **/
1054 void
1055 g_file_info_set_attribute_object (GFileInfo  *info,
1056                                   const char *attribute,
1057                                   GObject    *attr_value)
1058 {
1059   GFileAttributeValue *value;
1060
1061   g_return_if_fail (G_IS_FILE_INFO (info));
1062   g_return_if_fail (attribute != NULL && *attribute != '\0');
1063   g_return_if_fail (G_IS_OBJECT (attr_value));
1064
1065   value = g_file_info_create_value_by_name (info, attribute);
1066   if (value)
1067     _g_file_attribute_value_set_object (value, attr_value);
1068 }
1069
1070 /**
1071  * g_file_info_set_attribute_stringv:
1072  * @info: a #GFileInfo.
1073  * @attribute: a file attribute key.
1074  * @attr_value: a %NULL terminated string array
1075  *
1076  * Sets the @attribute to contain the given @attr_value,
1077  * if possible.
1078  *
1079  * Sinze: 2.22
1080  **/
1081 void
1082 g_file_info_set_attribute_stringv (GFileInfo  *info,
1083                                    const char *attribute,
1084                                    char      **attr_value)
1085 {
1086   GFileAttributeValue *value;
1087
1088   g_return_if_fail (G_IS_FILE_INFO (info));
1089   g_return_if_fail (attribute != NULL && *attribute != '\0');
1090   g_return_if_fail (attr_value != NULL);
1091
1092   value = g_file_info_create_value_by_name (info, attribute);
1093   if (value)
1094     _g_file_attribute_value_set_stringv (value, attr_value);
1095 }
1096
1097 /**
1098  * g_file_info_set_attribute_string:
1099  * @info: a #GFileInfo.
1100  * @attribute: a file attribute key.
1101  * @attr_value: a string.
1102  * 
1103  * Sets the @attribute to contain the given @attr_value, 
1104  * if possible.
1105  **/
1106 void
1107 g_file_info_set_attribute_string (GFileInfo  *info,
1108                                   const char *attribute,
1109                                   const char *attr_value)
1110 {
1111   GFileAttributeValue *value;
1112   
1113   g_return_if_fail (G_IS_FILE_INFO (info));
1114   g_return_if_fail (attribute != NULL && *attribute != '\0');
1115   g_return_if_fail (attr_value != NULL);
1116
1117   value = g_file_info_create_value_by_name (info, attribute);
1118   if (value)
1119     _g_file_attribute_value_set_string (value, attr_value);
1120 }
1121
1122 /**
1123  * g_file_info_set_attribute_byte_string:
1124  * @info: a #GFileInfo.
1125  * @attribute: a file attribute key.
1126  * @attr_value: a byte string.
1127  * 
1128  * Sets the @attribute to contain the given @attr_value, 
1129  * if possible.
1130  **/
1131 void
1132 g_file_info_set_attribute_byte_string (GFileInfo  *info,
1133                                        const char *attribute,
1134                                        const char *attr_value)
1135 {
1136   GFileAttributeValue *value;
1137
1138   g_return_if_fail (G_IS_FILE_INFO (info));
1139   g_return_if_fail (attribute != NULL && *attribute != '\0');
1140   g_return_if_fail (attr_value != NULL);
1141
1142   value = g_file_info_create_value_by_name (info, attribute);
1143   if (value)
1144     _g_file_attribute_value_set_byte_string (value, attr_value);
1145 }
1146
1147 /**
1148  * g_file_info_set_attribute_boolean:
1149  * @info: a #GFileInfo.
1150  * @attribute: a file attribute key.
1151  * @attr_value: a boolean value.
1152  * 
1153  * Sets the @attribute to contain the given @attr_value, 
1154  * if possible.
1155  **/
1156 void
1157 g_file_info_set_attribute_boolean (GFileInfo  *info,
1158                                    const char *attribute,
1159                                    gboolean    attr_value)
1160 {
1161   GFileAttributeValue *value;
1162
1163   g_return_if_fail (G_IS_FILE_INFO (info));
1164   g_return_if_fail (attribute != NULL && *attribute != '\0');
1165
1166   value = g_file_info_create_value_by_name (info, attribute);
1167   if (value)
1168     _g_file_attribute_value_set_boolean (value, attr_value);
1169 }
1170
1171 /**
1172  * g_file_info_set_attribute_uint32:
1173  * @info: a #GFileInfo.
1174  * @attribute: a file attribute key.
1175  * @attr_value: an unsigned 32-bit integer.
1176  * 
1177  * Sets the @attribute to contain the given @attr_value, 
1178  * if possible.
1179  **/
1180 void
1181 g_file_info_set_attribute_uint32 (GFileInfo  *info,
1182                                   const char *attribute,
1183                                   guint32     attr_value)
1184 {
1185   GFileAttributeValue *value;
1186
1187   g_return_if_fail (G_IS_FILE_INFO (info));
1188   g_return_if_fail (attribute != NULL && *attribute != '\0');
1189
1190   value = g_file_info_create_value_by_name (info, attribute);
1191   if (value)
1192     _g_file_attribute_value_set_uint32 (value, attr_value);
1193 }
1194
1195
1196 /**
1197  * g_file_info_set_attribute_int32:
1198  * @info: a #GFileInfo.
1199  * @attribute: a file attribute key.
1200  * @attr_value: a signed 32-bit integer
1201  * 
1202  * Sets the @attribute to contain the given @attr_value, 
1203  * if possible.
1204  **/
1205 void
1206 g_file_info_set_attribute_int32 (GFileInfo  *info,
1207                                  const char *attribute,
1208                                  gint32      attr_value)
1209 {
1210   GFileAttributeValue *value;
1211  
1212   g_return_if_fail (G_IS_FILE_INFO (info));
1213   g_return_if_fail (attribute != NULL && *attribute != '\0');
1214
1215   value = g_file_info_create_value_by_name (info, attribute);
1216   if (value)
1217     _g_file_attribute_value_set_int32 (value, attr_value);
1218 }
1219
1220 /**
1221  * g_file_info_set_attribute_uint64:
1222  * @info: a #GFileInfo.
1223  * @attribute: a file attribute key.
1224  * @attr_value: an unsigned 64-bit integer.
1225  * 
1226  * Sets the @attribute to contain the given @attr_value, 
1227  * if possible.
1228  **/
1229 void
1230 g_file_info_set_attribute_uint64 (GFileInfo  *info,
1231                                   const char *attribute,
1232                                   guint64     attr_value)
1233 {
1234   GFileAttributeValue *value;
1235
1236   g_return_if_fail (G_IS_FILE_INFO (info));
1237   g_return_if_fail (attribute != NULL && *attribute != '\0');
1238
1239   value = g_file_info_create_value_by_name (info, attribute);
1240   if (value)
1241     _g_file_attribute_value_set_uint64 (value, attr_value);
1242 }
1243
1244 /**
1245  * g_file_info_set_attribute_int64:
1246  * @info: a #GFileInfo.
1247  * @attribute: attribute name to set.
1248  * @attr_value: int64 value to set attribute to.
1249  * 
1250  * Sets the @attribute to contain the given @attr_value, 
1251  * if possible.
1252  * 
1253  **/
1254 void
1255 g_file_info_set_attribute_int64  (GFileInfo  *info,
1256                                   const char *attribute,
1257                                   gint64      attr_value)
1258 {
1259   GFileAttributeValue *value;
1260
1261   g_return_if_fail (G_IS_FILE_INFO (info));
1262   g_return_if_fail (attribute != NULL && *attribute != '\0');
1263
1264   value = g_file_info_create_value_by_name (info, attribute);
1265   if (value)
1266     _g_file_attribute_value_set_int64 (value, attr_value);
1267 }
1268
1269 /* Helper getters */
1270 /**
1271  * g_file_info_get_file_type:
1272  * @info: a #GFileInfo.
1273  * 
1274  * Gets a file's type (whether it is a regular file, symlink, etc). 
1275  * This is different from the file's content type, see g_file_info_get_content_type().
1276  * 
1277  * Returns: a #GFileType for the given file.
1278  **/
1279 GFileType
1280 g_file_info_get_file_type (GFileInfo *info)
1281 {
1282   static guint32 attr = 0;
1283   GFileAttributeValue *value;
1284
1285   g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1286   
1287   if (attr == 0)
1288     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1289   
1290   value = g_file_info_find_value (info, attr);
1291   return (GFileType)_g_file_attribute_value_get_uint32 (value);
1292 }
1293
1294 /**
1295  * g_file_info_get_is_hidden:
1296  * @info: a #GFileInfo.
1297  * 
1298  * Checks if a file is hidden.
1299  * 
1300  * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1301  **/
1302 gboolean
1303 g_file_info_get_is_hidden (GFileInfo *info)
1304 {
1305   static guint32 attr = 0;
1306   GFileAttributeValue *value;
1307   
1308   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1309   
1310   if (attr == 0)
1311     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1312   
1313   value = g_file_info_find_value (info, attr);
1314   return (GFileType)_g_file_attribute_value_get_boolean (value);
1315 }
1316
1317 /**
1318  * g_file_info_get_is_backup:
1319  * @info: a #GFileInfo.
1320  * 
1321  * Checks if a file is a backup file.
1322  * 
1323  * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1324  **/
1325 gboolean
1326 g_file_info_get_is_backup (GFileInfo *info)
1327 {
1328   static guint32 attr = 0;
1329   GFileAttributeValue *value;
1330   
1331   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1332   
1333   if (attr == 0)
1334     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1335   
1336   value = g_file_info_find_value (info, attr);
1337   return (GFileType)_g_file_attribute_value_get_boolean (value);
1338 }
1339
1340 /**
1341  * g_file_info_get_is_symlink:
1342  * @info: a #GFileInfo.
1343  * 
1344  * Checks if a file is a symlink.
1345  * 
1346  * Returns: %TRUE if the given @info is a symlink.
1347  **/
1348 gboolean
1349 g_file_info_get_is_symlink (GFileInfo *info)
1350 {
1351   static guint32 attr = 0;
1352   GFileAttributeValue *value;
1353   
1354   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1355   
1356   if (attr == 0)
1357     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1358   
1359   value = g_file_info_find_value (info, attr);
1360   return (GFileType)_g_file_attribute_value_get_boolean (value);
1361 }
1362
1363 /**
1364  * g_file_info_get_name:
1365  * @info: a #GFileInfo.
1366  * 
1367  * Gets the name for a file.
1368  * 
1369  * Returns: a string containing the file name.
1370  **/
1371 const char *
1372 g_file_info_get_name (GFileInfo *info)
1373 {
1374   static guint32 attr = 0;
1375   GFileAttributeValue *value;
1376   
1377   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1378   
1379   if (attr == 0)
1380     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1381   
1382   value = g_file_info_find_value (info, attr);
1383   return _g_file_attribute_value_get_byte_string (value);
1384 }
1385
1386 /**
1387  * g_file_info_get_display_name:
1388  * @info: a #GFileInfo.
1389  * 
1390  * Gets a display name for a file.
1391  * 
1392  * Returns: a string containing the display name.
1393  **/
1394 const char *
1395 g_file_info_get_display_name (GFileInfo *info)
1396 {
1397   static guint32 attr = 0;
1398   GFileAttributeValue *value;
1399   
1400   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1401   
1402   if (attr == 0)
1403     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1404   
1405   value = g_file_info_find_value (info, attr);
1406   return _g_file_attribute_value_get_string (value);
1407 }
1408
1409 /**
1410  * g_file_info_get_edit_name:
1411  * @info: a #GFileInfo.
1412  * 
1413  * Gets the edit name for a file.
1414  * 
1415  * Returns: a string containing the edit name.
1416  **/
1417 const char *
1418 g_file_info_get_edit_name (GFileInfo *info)
1419 {
1420   static guint32 attr = 0;
1421   GFileAttributeValue *value;
1422   
1423   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1424   
1425   if (attr == 0)
1426     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1427   
1428   value = g_file_info_find_value (info, attr);
1429   return _g_file_attribute_value_get_string (value);
1430 }
1431
1432 /**
1433  * g_file_info_get_icon:
1434  * @info: a #GFileInfo.
1435  * 
1436  * Gets the icon for a file.
1437  * 
1438  * Returns: #GIcon for the given @info.
1439  **/
1440 GIcon *
1441 g_file_info_get_icon (GFileInfo *info)
1442 {
1443   static guint32 attr = 0;
1444   GFileAttributeValue *value;
1445   GObject *obj;
1446   
1447   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1448   
1449   if (attr == 0)
1450     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1451   
1452   value = g_file_info_find_value (info, attr);
1453   obj = _g_file_attribute_value_get_object (value);
1454   if (G_IS_ICON (obj))
1455     return G_ICON (obj);
1456   return NULL;
1457 }
1458
1459 /**
1460  * g_file_info_get_content_type:
1461  * @info: a #GFileInfo.
1462  * 
1463  * Gets the file's content type.
1464  * 
1465  * Returns: a string containing the file's content type.
1466  **/
1467 const char *
1468 g_file_info_get_content_type (GFileInfo *info)
1469 {
1470   static guint32 attr = 0;
1471   GFileAttributeValue *value;
1472   
1473   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1474   
1475   if (attr == 0)
1476     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1477   
1478   value = g_file_info_find_value (info, attr);
1479   return _g_file_attribute_value_get_string (value);
1480 }
1481
1482 /**
1483  * g_file_info_get_size:
1484  * @info: a #GFileInfo.
1485  * 
1486  * Gets the file's size.
1487  * 
1488  * Returns: a #goffset containing the file's size. 
1489  **/
1490 goffset
1491 g_file_info_get_size (GFileInfo *info)
1492 {
1493   static guint32 attr = 0;
1494   GFileAttributeValue *value;
1495  
1496   g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1497   
1498   if (attr == 0)
1499     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1500   
1501   value = g_file_info_find_value (info, attr);
1502   return (goffset) _g_file_attribute_value_get_uint64 (value);
1503 }
1504
1505 /**
1506  * g_file_info_get_modification_time:
1507  * @info: a #GFileInfo.
1508  * @result: a #GTimeVal.
1509  * 
1510  * Gets the modification time of the current @info and sets it
1511  * in @result.
1512  **/
1513 void
1514 g_file_info_get_modification_time (GFileInfo *info,
1515                                    GTimeVal  *result)
1516 {
1517   static guint32 attr_mtime = 0, attr_mtime_usec;
1518   GFileAttributeValue *value;
1519
1520   g_return_if_fail (G_IS_FILE_INFO (info));
1521   g_return_if_fail (result != NULL);
1522   
1523   if (attr_mtime == 0)
1524     {
1525       attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1526       attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1527     }
1528   
1529   value = g_file_info_find_value (info, attr_mtime);
1530   result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1531   value = g_file_info_find_value (info, attr_mtime_usec);
1532   result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1533 }
1534
1535 /**
1536  * g_file_info_get_symlink_target:
1537  * @info: a #GFileInfo.
1538  * 
1539  * Gets the symlink target for a given #GFileInfo.
1540  * 
1541  * Returns: a string containing the symlink target.
1542  **/
1543 const char *
1544 g_file_info_get_symlink_target (GFileInfo *info)
1545 {
1546   static guint32 attr = 0;
1547   GFileAttributeValue *value;
1548   
1549   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1550   
1551   if (attr == 0)
1552     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1553   
1554   value = g_file_info_find_value (info, attr);
1555   return _g_file_attribute_value_get_byte_string (value);
1556 }
1557
1558 /**
1559  * g_file_info_get_etag:
1560  * @info: a #GFileInfo.
1561  * 
1562  * Gets the <link linkend="gfile-etag">entity tag</link> for a given 
1563  * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1564  * 
1565  * Returns: a string containing the value of the "etag:value" attribute.
1566  **/
1567 const char *
1568 g_file_info_get_etag (GFileInfo *info)
1569 {
1570   static guint32 attr = 0;
1571   GFileAttributeValue *value;
1572   
1573   g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1574   
1575   if (attr == 0)
1576     attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1577   
1578   value = g_file_info_find_value (info, attr);
1579   return _g_file_attribute_value_get_string (value);
1580 }
1581
1582 /**
1583  * g_file_info_get_sort_order:
1584  * @info: a #GFileInfo.
1585  * 
1586  * Gets the value of the sort_order attribute from the #GFileInfo.
1587  * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1588  * 
1589  * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1590  **/
1591 gint32
1592 g_file_info_get_sort_order (GFileInfo *info)
1593 {
1594   static guint32 attr = 0;
1595   GFileAttributeValue *value;
1596   
1597   g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1598   
1599   if (attr == 0)
1600     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1601   
1602   value = g_file_info_find_value (info, attr);
1603   return _g_file_attribute_value_get_int32 (value);
1604 }
1605
1606 /* Helper setters: */
1607 /**
1608  * g_file_info_set_file_type:
1609  * @info: a #GFileInfo.
1610  * @type: a #GFileType.
1611  * 
1612  * Sets the file type in a #GFileInfo to @type.
1613  * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1614  **/
1615 void
1616 g_file_info_set_file_type (GFileInfo *info,
1617                            GFileType  type)
1618 {
1619   static guint32 attr = 0;
1620   GFileAttributeValue *value;
1621   
1622   g_return_if_fail (G_IS_FILE_INFO (info));
1623   
1624   if (attr == 0)
1625     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1626   
1627   value = g_file_info_create_value (info, attr);
1628   if (value)
1629     _g_file_attribute_value_set_uint32 (value, type);
1630 }
1631
1632 /**
1633  * g_file_info_set_is_hidden:
1634  * @info: a #GFileInfo.
1635  * @is_hidden: a #gboolean.
1636  * 
1637  * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1638  * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1639  **/
1640 void
1641 g_file_info_set_is_hidden (GFileInfo *info,
1642                            gboolean   is_hidden)
1643 {
1644   static guint32 attr = 0;
1645   GFileAttributeValue *value;
1646   
1647   g_return_if_fail (G_IS_FILE_INFO (info));
1648   
1649   if (attr == 0)
1650     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1651   
1652   value = g_file_info_create_value (info, attr);
1653   if (value)
1654     _g_file_attribute_value_set_boolean (value, is_hidden);
1655 }
1656
1657 /**
1658  * g_file_info_set_is_symlink:
1659  * @info: a #GFileInfo.
1660  * @is_symlink: a #gboolean.
1661  * 
1662  * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1663  * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1664  **/
1665 void
1666 g_file_info_set_is_symlink (GFileInfo *info,
1667                             gboolean   is_symlink)
1668 {
1669   static guint32 attr = 0;
1670   GFileAttributeValue *value;
1671   
1672   g_return_if_fail (G_IS_FILE_INFO (info));
1673   
1674   if (attr == 0)
1675     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1676   
1677   value = g_file_info_create_value (info, attr);
1678   if (value)
1679     _g_file_attribute_value_set_boolean (value, is_symlink);
1680 }
1681
1682 /**
1683  * g_file_info_set_name:
1684  * @info: a #GFileInfo.
1685  * @name: a string containing a name.
1686  * 
1687  * Sets the name attribute for the current #GFileInfo. 
1688  * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1689  **/
1690 void
1691 g_file_info_set_name (GFileInfo  *info,
1692                       const char *name)
1693 {
1694   static guint32 attr = 0;
1695   GFileAttributeValue *value;
1696   
1697   g_return_if_fail (G_IS_FILE_INFO (info));
1698   g_return_if_fail (name != NULL);
1699   
1700   if (attr == 0)
1701     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1702   
1703   value = g_file_info_create_value (info, attr);
1704   if (value)
1705     _g_file_attribute_value_set_byte_string (value, name);
1706 }
1707
1708 /**
1709  * g_file_info_set_display_name:
1710  * @info: a #GFileInfo.
1711  * @display_name: a string containing a display name.
1712  * 
1713  * Sets the display name for the current #GFileInfo.
1714  * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1715  **/
1716 void
1717 g_file_info_set_display_name (GFileInfo  *info,
1718                               const char *display_name)
1719 {
1720   static guint32 attr = 0;
1721   GFileAttributeValue *value;
1722   
1723   g_return_if_fail (G_IS_FILE_INFO (info));
1724   g_return_if_fail (display_name != NULL);
1725   
1726   if (attr == 0)
1727     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1728   
1729   value = g_file_info_create_value (info, attr);
1730   if (value)
1731     _g_file_attribute_value_set_string (value, display_name);
1732 }
1733
1734 /**
1735  * g_file_info_set_edit_name:
1736  * @info: a #GFileInfo.
1737  * @edit_name: a string containing an edit name.
1738  * 
1739  * Sets the edit name for the current file.
1740  * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1741  **/
1742 void
1743 g_file_info_set_edit_name (GFileInfo  *info,
1744                            const char *edit_name)
1745 {
1746   static guint32 attr = 0;
1747   GFileAttributeValue *value;
1748   
1749   g_return_if_fail (G_IS_FILE_INFO (info));
1750   g_return_if_fail (edit_name != NULL);
1751   
1752   if (attr == 0)
1753     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1754   
1755   value = g_file_info_create_value (info, attr);
1756   if (value)
1757     _g_file_attribute_value_set_string (value, edit_name);
1758 }
1759
1760 /**
1761  * g_file_info_set_icon:
1762  * @info: a #GFileInfo.
1763  * @icon: a #GIcon.
1764  * 
1765  * Sets the icon for a given #GFileInfo. 
1766  * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1767  **/
1768 void
1769 g_file_info_set_icon (GFileInfo *info,
1770                       GIcon     *icon)
1771 {
1772   static guint32 attr = 0;
1773   GFileAttributeValue *value;
1774   
1775   g_return_if_fail (G_IS_FILE_INFO (info));
1776   g_return_if_fail (G_IS_ICON (icon));
1777   
1778   if (attr == 0)
1779     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1780   
1781   value = g_file_info_create_value (info, attr);
1782   if (value)
1783     _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1784 }
1785
1786 /**
1787  * g_file_info_set_content_type:
1788  * @info: a #GFileInfo.
1789  * @content_type: a content type. See #GContentType.
1790  * 
1791  * Sets the content type attribute for a given #GFileInfo.
1792  * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1793  **/
1794 void
1795 g_file_info_set_content_type (GFileInfo  *info,
1796                               const char *content_type)
1797 {
1798   static guint32 attr = 0;
1799   GFileAttributeValue *value;
1800   
1801   g_return_if_fail (G_IS_FILE_INFO (info));
1802   g_return_if_fail (content_type != NULL);
1803   
1804   if (attr == 0)
1805     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1806   
1807   value = g_file_info_create_value (info, attr);
1808   if (value)
1809     _g_file_attribute_value_set_string (value, content_type);
1810 }
1811
1812 /**
1813  * g_file_info_set_size:
1814  * @info: a #GFileInfo.
1815  * @size: a #goffset containing the file's size.
1816  * 
1817  * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info 
1818  * to the given size.
1819  **/
1820 void
1821 g_file_info_set_size (GFileInfo *info,
1822                       goffset    size)
1823 {
1824   static guint32 attr = 0;
1825   GFileAttributeValue *value;
1826   
1827   g_return_if_fail (G_IS_FILE_INFO (info));
1828   
1829   if (attr == 0)
1830     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1831   
1832   value = g_file_info_create_value (info, attr);
1833   if (value)
1834     _g_file_attribute_value_set_uint64 (value, size);
1835 }
1836
1837 /**
1838  * g_file_info_set_modification_time
1839  * @info: a #GFileInfo.
1840  * @mtime: a #GTimeVal.
1841  * 
1842  * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
1843  * info to the given time value.
1844  **/
1845 void
1846 g_file_info_set_modification_time (GFileInfo *info,
1847                                    GTimeVal  *mtime)
1848 {
1849   static guint32 attr_mtime = 0, attr_mtime_usec;
1850   GFileAttributeValue *value;
1851   
1852   g_return_if_fail (G_IS_FILE_INFO (info));
1853   g_return_if_fail (mtime != NULL);
1854   
1855   if (attr_mtime == 0)
1856     {
1857       attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1858       attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1859     }
1860   
1861   value = g_file_info_create_value (info, attr_mtime);
1862   if (value)
1863     _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
1864   value = g_file_info_create_value (info, attr_mtime_usec);
1865   if (value)
1866     _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
1867 }
1868
1869 /**
1870  * g_file_info_set_symlink_target:
1871  * @info: a #GFileInfo.
1872  * @symlink_target: a static string containing a path to a symlink target.
1873  * 
1874  * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info 
1875  * to the given symlink target.
1876  **/
1877 void
1878 g_file_info_set_symlink_target (GFileInfo  *info,
1879                                 const char *symlink_target)
1880 {
1881   static guint32 attr = 0;
1882   GFileAttributeValue *value;
1883   
1884   g_return_if_fail (G_IS_FILE_INFO (info));
1885   g_return_if_fail (symlink_target != NULL);
1886   
1887   if (attr == 0)
1888     attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1889   
1890   value = g_file_info_create_value (info, attr);
1891   if (value)
1892     _g_file_attribute_value_set_byte_string (value, symlink_target);
1893 }
1894
1895 /**
1896  * g_file_info_set_sort_order:
1897  * @info: a #GFileInfo.
1898  * @sort_order: a sort order integer.
1899  * 
1900  * Sets the sort order attribute in the file info structure. See 
1901  * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1902  **/
1903 void
1904 g_file_info_set_sort_order (GFileInfo *info,
1905                             gint32     sort_order)
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_SORT_ORDER);
1914   
1915   value = g_file_info_create_value (info, attr);
1916   if (value)
1917     _g_file_attribute_value_set_int32 (value, sort_order);
1918 }
1919
1920
1921 #define ON_STACK_MATCHERS 5
1922
1923 typedef struct {
1924   guint32 id;
1925   guint32 mask;
1926 } SubMatcher;
1927
1928 struct _GFileAttributeMatcher {
1929   gboolean all;
1930   SubMatcher sub_matchers[ON_STACK_MATCHERS];
1931   GArray *more_sub_matchers;
1932
1933   /* Interator */
1934   guint32 iterator_ns;
1935   int iterator_pos;
1936   int ref;
1937 };
1938
1939 static void
1940 matcher_add (GFileAttributeMatcher *matcher,
1941              guint                  id,
1942              guint                  mask)
1943 {
1944   SubMatcher *sub_matchers;
1945   int i;
1946   SubMatcher s;
1947
1948   for (i = 0; i < ON_STACK_MATCHERS; i++)
1949     {
1950       /* First empty spot, not found, use this */
1951       if (matcher->sub_matchers[i].id == 0)
1952         {
1953           matcher->sub_matchers[i].id = id;
1954           matcher->sub_matchers[i].mask = mask;
1955           return;
1956         }
1957       
1958       /* Already added */
1959       if (matcher->sub_matchers[i].id == id &&
1960           matcher->sub_matchers[i].mask == mask)
1961         return;
1962     }
1963
1964   if (matcher->more_sub_matchers == NULL)
1965     matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
1966       
1967   sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
1968   for (i = 0; i < matcher->more_sub_matchers->len; i++)
1969     {
1970       /* Already added */
1971       if (sub_matchers[i].id == id &&
1972           sub_matchers[i].mask == mask)
1973         return;
1974     }
1975
1976   s.id = id;
1977   s.mask = mask;
1978   
1979   g_array_append_val (matcher->more_sub_matchers, s);
1980 }
1981
1982 /**
1983  * g_file_attribute_matcher_new:
1984  * @attributes: an attribute string to match.
1985  * 
1986  * Creates a new file attribute matcher, which matches attributes 
1987  * against a given string. #GFileAttributeMatcher<!-- -->s are reference 
1988  * counted structures, and are created with a reference count of 1. If 
1989  * the number of references falls to 0, the #GFileAttributeMatcher is 
1990  * automatically destroyed.
1991  * 
1992  * The @attribute string should be formatted with specific keys separated
1993  * from namespaces with a double colon. Several "namespace::key" strings may be 
1994  * concatenated with a single comma (e.g. "standard::type,standard::is-hidden"). 
1995  * The wildcard "*" may be used to match all keys and namespaces, or 
1996  * "namespace::*" will match all keys in a given namespace. 
1997  * 
1998  * Examples of strings to use:
1999  * <table>
2000  * <title>File Attribute Matcher strings and results</title>
2001  * <tgroup cols='2' align='left'><thead>
2002  * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
2003  * <tbody>
2004  * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
2005  * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
2006  * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
2007  * all keys in the unix namespace.</entry></row>
2008  * </tbody></tgroup>
2009  * </table>
2010  * 
2011  * Returns: a #GFileAttributeMatcher.
2012  **/
2013 GFileAttributeMatcher *
2014 g_file_attribute_matcher_new (const char *attributes)
2015 {
2016   char **split;
2017   char *colon;
2018   int i;
2019   GFileAttributeMatcher *matcher;
2020
2021   if (attributes == NULL || *attributes == '\0')
2022     return NULL;
2023
2024   matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
2025   matcher->ref = 1;
2026
2027   split = g_strsplit (attributes, ",", -1);
2028
2029   for (i = 0; split[i] != NULL; i++)
2030     {
2031       if (strcmp (split[i], "*") == 0)
2032         matcher->all = TRUE;
2033       else
2034         {
2035           guint32 id, mask;
2036   
2037           colon = strstr (split[i], "::");
2038           if (colon != NULL &&
2039               !(colon[2] == 0 ||
2040                 (colon[2] == '*' &&
2041                  colon[3] == 0)))
2042             {
2043               id = lookup_attribute (split[i]);
2044               mask = 0xffffffff;
2045             }
2046           else
2047             {
2048               if (colon)
2049                 *colon = 0;
2050
2051               id = lookup_namespace (split[i]) << NS_POS;
2052               mask = NS_MASK << NS_POS;
2053             }
2054           
2055           matcher_add (matcher, id, mask);
2056         }
2057     }
2058
2059   g_strfreev (split);
2060
2061   return matcher;
2062 }
2063
2064 /**
2065  * g_file_attribute_matcher_ref:
2066  * @matcher: a #GFileAttributeMatcher.
2067  * 
2068  * References a file attribute matcher.
2069  * 
2070  * Returns: a #GFileAttributeMatcher.
2071  **/
2072 GFileAttributeMatcher *
2073 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
2074 {
2075   if (matcher)
2076     {
2077       g_return_val_if_fail (matcher->ref > 0, NULL);
2078       g_atomic_int_inc (&matcher->ref);
2079     }
2080   return matcher;
2081 }
2082
2083 /**
2084  * g_file_attribute_matcher_unref:
2085  * @matcher: a #GFileAttributeMatcher.
2086  * 
2087  * Unreferences @matcher. If the reference count falls below 1, 
2088  * the @matcher is automatically freed.
2089  * 
2090  **/
2091 void
2092 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
2093 {
2094   if (matcher)
2095     {
2096       g_return_if_fail (matcher->ref > 0);
2097       
2098       if (g_atomic_int_dec_and_test (&matcher->ref))
2099         {
2100           if (matcher->more_sub_matchers)
2101             g_array_free (matcher->more_sub_matchers, TRUE);
2102           
2103           g_free (matcher);
2104         }
2105     }
2106 }
2107
2108 /**
2109  * g_file_attribute_matcher_matches_only:
2110  * @matcher: a #GFileAttributeMatcher.
2111  * @attribute: a file attribute key.
2112  * 
2113  * Checks if a attribute matcher only matches a given attribute. Always
2114  * returns %FALSE if "*" was used when creating the matcher.
2115  * 
2116  * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
2117  **/
2118 gboolean
2119 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
2120                                        const char            *attribute)
2121 {
2122   guint32 id;
2123
2124   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2125
2126   if (matcher == NULL ||
2127       matcher->all)
2128     return FALSE;
2129   
2130   id = lookup_attribute (attribute);
2131
2132   if (matcher->sub_matchers[0].id != 0 &&
2133       matcher->sub_matchers[1].id == 0 &&
2134       matcher->sub_matchers[0].mask == 0xffffffff &&
2135       matcher->sub_matchers[0].id == id)
2136     return TRUE;
2137   
2138   return FALSE;
2139 }
2140
2141 static gboolean
2142 matcher_matches_id (GFileAttributeMatcher *matcher,
2143                     guint32                id)
2144 {
2145   SubMatcher *sub_matchers;
2146   int i;
2147   
2148   for (i = 0; i < ON_STACK_MATCHERS; i++)
2149     {
2150       if (matcher->sub_matchers[i].id == 0)
2151         return FALSE;
2152       
2153       if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2154         return TRUE;
2155     }
2156
2157   if (matcher->more_sub_matchers)
2158     {
2159       sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2160       for (i = 0; i < matcher->more_sub_matchers->len; i++)
2161         {
2162           if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2163             return TRUE;
2164         }
2165     }
2166   
2167   return FALSE;
2168 }
2169
2170 static gboolean
2171 g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2172                                      guint32                id)
2173 {
2174   /* We return a NULL matcher for an empty match string, so handle this */
2175   if (matcher == NULL)
2176     return FALSE;
2177   
2178   if (matcher->all)
2179     return TRUE;
2180   
2181   return matcher_matches_id (matcher, id);
2182 }
2183
2184 /**
2185  * g_file_attribute_matcher_matches:
2186  * @matcher: a #GFileAttributeMatcher.
2187  * @attribute: a file attribute key.
2188  *
2189  * Checks if an attribute will be matched by an attribute matcher. If 
2190  * the matcher was created with the "*" matching string, this function
2191  * will always return %TRUE.
2192  * 
2193  * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2194  **/
2195 gboolean
2196 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2197                                   const char            *attribute)
2198 {
2199   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2200
2201   /* We return a NULL matcher for an empty match string, so handle this */
2202   if (matcher == NULL)
2203     return FALSE;
2204   
2205   if (matcher->all)
2206     return TRUE;
2207   
2208   return matcher_matches_id (matcher, lookup_attribute (attribute));
2209 }
2210
2211 /* return TRUE -> all */
2212 /**
2213  * g_file_attribute_matcher_enumerate_namespace:
2214  * @matcher: a #GFileAttributeMatcher.
2215  * @ns: a string containing a file attribute namespace.
2216  * 
2217  * Checks if the matcher will match all of the keys in a given namespace.
2218  * This will always return %TRUE if a wildcard character is in use (e.g. if 
2219  * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2220  * using "*" and namespace is anything.) 
2221  * 
2222  * TODO: this is awkwardly worded.
2223  * 
2224  * Returns: %TRUE if the matcher matches all of the entries
2225  * in the given @ns, %FALSE otherwise.
2226  **/
2227 gboolean
2228 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2229                                               const char            *ns)
2230 {
2231   SubMatcher *sub_matchers;
2232   int ns_id;
2233   int i;
2234   
2235   g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2236
2237   /* We return a NULL matcher for an empty match string, so handle this */
2238   if (matcher == NULL)
2239     return FALSE;
2240   
2241   if (matcher->all)
2242     return TRUE;
2243
2244   ns_id = lookup_namespace (ns) << NS_POS;
2245
2246   for (i = 0; i < ON_STACK_MATCHERS; i++)
2247     {
2248       if (matcher->sub_matchers[i].id == ns_id)
2249         return TRUE;
2250     }
2251
2252   if (matcher->more_sub_matchers)
2253     {
2254       sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2255       for (i = 0; i < matcher->more_sub_matchers->len; i++)
2256         {
2257           if (sub_matchers[i].id == ns_id)
2258             return TRUE;
2259         }
2260     }
2261
2262   matcher->iterator_ns = ns_id;
2263   matcher->iterator_pos = 0;
2264   
2265   return FALSE;
2266 }
2267
2268 /**
2269  * g_file_attribute_matcher_enumerate_next:
2270  * @matcher: a #GFileAttributeMatcher.
2271  * 
2272  * Gets the next matched attribute from a #GFileAttributeMatcher.
2273  * 
2274  * Returns: a string containing the next attribute or %NULL if 
2275  * no more attribute exist.
2276  **/
2277 const char *
2278 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2279 {
2280   int i;
2281   SubMatcher *sub_matcher;
2282
2283   /* We return a NULL matcher for an empty match string, so handle this */
2284   if (matcher == NULL)
2285     return NULL;
2286
2287   while (1)
2288     {
2289       i = matcher->iterator_pos++;
2290
2291       if (i < ON_STACK_MATCHERS)
2292         {
2293           if (matcher->sub_matchers[i].id == 0)
2294             return NULL;
2295
2296           sub_matcher = &matcher->sub_matchers[i];
2297         }
2298       else
2299         {
2300           if (matcher->more_sub_matchers == NULL)
2301             return NULL;
2302       
2303           i -= ON_STACK_MATCHERS;
2304           if (i < matcher->more_sub_matchers->len)
2305             sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2306           else
2307             return NULL;
2308         }
2309
2310       if (sub_matcher->mask == 0xffffffff &&
2311           (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2312         return get_attribute_for_id (sub_matcher->id);
2313     }
2314 }
2315
2316 #define __G_FILE_INFO_C__
2317 #include "gioaliasdef.c"