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