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