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