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