Add G_FILE_ATTRIBUTE_PREVIEW_ICON to doc table
[platform/upstream/glib.git] / gio / gfileattribute.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 #include "config.h"
24
25 #include <string.h>
26
27 #include "gfileattribute.h"
28 #include "gfileattribute-priv.h"
29 #include <glib-object.h>
30 #include "glibintl.h"
31
32 #include "gioalias.h"
33
34 /**
35  * SECTION:gfileattribute
36  * @short_description: Key-Value Paired File Attributes
37  * @include: gio/gio.h
38  * @see_also: #GFile, #GFileInfo
39  * 
40  * File attributes in GIO consist of a list of key-value pairs. 
41  * 
42  * Keys are strings that contain a key namespace and a key name, separated
43  * by a colon, e.g. "namespace:keyname". Namespaces are included to sort
44  * key-value pairs by namespaces for relevance. Keys can be retrived 
45  * using wildcards, e.g. "standard::*" will return all of the keys in the 
46  * "standard" namespace.
47  * 
48  * Values are stored within the list in #GFileAttributeValue structures.
49  * Values can store different types, listed in the enum #GFileAttributeType.
50  * Upon creation of a #GFileAttributeValue, the type will be set to 
51  * %G_FILE_ATTRIBUTE_TYPE_INVALID. 
52  * 
53  * The list of possible attributes for a filesystem (pointed to by a #GFile) is
54  * availible as a #GFileAttributeInfoList. This list is queryable by key names 
55  * as indicated earlier.
56  * 
57  * Classes that implement #GFileIface will create a #GFileAttributeInfoList and 
58  * install default keys and values for their given file system, architecture, 
59  * and other possible implementation details (e.g., on a UNIX system, a file 
60  * attribute key will be registered for the user id for a given file).
61  * 
62  * <para>
63  * <table>
64  * <title>GFileAttributes Default Namespaces</title>
65  * <tgroup cols='2' align='left'><thead>
66  * <row><entry>Namspace</entry><entry>Description</entry></row>
67  * </thead>
68  * <tbody>
69  * <row><entry>"standard"</entry><entry>The "Standard" namespace. General file
70  * information that any application may need should be put in this namespace. 
71  * Examples include the file's name, type, and size.</entry></row> 
72  * <row><entry>"etag"</entry><entry>The <link linkend="gfile-etag">"Entity Tag"</link> 
73  * namespace. Currently, the only key in this namespace is "value", which contains 
74  * the value of the current entity tag.</entry></row>
75  * <row><entry>"id"</entry><entry>The "Identification" namespace. This 
76  * namespace is used by file managers and applications that list directories
77  * to check for loops and to uniquely identify files.</entry></row>
78  * <row><entry>"access"</entry><entry>The "Access" namespace. Used to check
79  * if a user has the proper privilidges to access files and perform
80  * file operations. Keys in this namespace are made to be generic 
81  * and easily understood, e.g. the "can_read" key is %TRUE if 
82  * the current user has permission to read the file. UNIX permissions and
83  * NTFS ACLs in Windows should be mapped to these values.</entry></row>
84  * <row><entry>"mountable"</entry><entry>The "Mountable" namespace. Includes 
85  * simple boolean keys for checking if a file or path supports mount operations, e.g.
86  * mount, unmount, eject. These are used for files of type %G_FILE_TYPE_MOUNTABLE.</entry></row>
87  * <row><entry>"time"</entry><entry>The "Time" namespace. Includes file 
88  * access, changed, created times. </entry></row>
89  * <row><entry>"unix"</entry><entry>The "Unix" namespace. Includes UNIX-specific
90  * information and may not be available for all files. Examples include 
91  * the UNIX "UID", "GID", etc.</entry></row>
92  * <row><entry>"dos"</entry><entry>The "DOS" namespace. Includes DOS-specific 
93  * information and may not be available for all files. Examples include
94  * "is_system" for checking if a file is marked as a system file, and "is_archive"
95  * for checking if a file is marked as an archive file.</entry></row>
96  * <row><entry>"owner"</entry><entry>The "Owner" namespace. Includes information
97  * about who owns a file. May not be available for all file systems. Examples include
98  * "user" for getting the user name of the file owner. This information is often mapped from
99  * some backend specific data such as a unix UID.</entry></row>
100  * <row><entry>"thumbnail"</entry><entry>The "Thumbnail" namespace. Includes 
101  * information about file thumbnails and their location within the file system. Exaples of 
102  * keys in this namespace include "path" to get the location of a thumbnail, and "failed"
103  * to check if thumbnailing of the file failed.</entry></row>
104  * <row><entry>"filesystem"</entry><entry>The "Filesystem" namespace. Gets information
105  * about the file system where a file is located, such as its type, how much
106  * space is left available, and the overall size of the file system.</entry></row>
107  * <row><entry>"gvfs"</entry><entry>The "GVFS" namespace. Keys in this namespace
108  * contain information about the current GVFS backend in use. </entry></row>
109  * <row><entry>"xattr"</entry><entry>The "xattr" namespace. Gets information 
110  * about extended user attributes. See attr(5). The "user." prefix of the
111  * extended user attribute name is stripped away when constructing keys in
112  * this namespace, e.g. "xattr::mime_type" for the extended attribute with 
113  * the name "user.mime_type". Note that this information is only available
114  * if GLib has been built with extended attribute support.</entry></row>
115  * <row><entry>"xattr-sys"</entry><entry>The "xattr-sys" namespace. 
116  * Gets information about extended attributes which are not user-specific. 
117  * See attr(5). Note that this information is only available if GLib
118  * has been built with extended attribute support.</entry></row>
119  * <row><entry>"selinux"</entry><entry>The "SELinux" namespace. Includes
120  * information about the SELinux context of files. Note that this information
121  * is only available if GLib has been built with SELinux support.</entry></row>
122  * </tbody>
123  * </tgroup>
124  * </table>
125  * </para>
126  * 
127  * Please note that these are not all of the possible namespaces.
128  * More namespaces can be added from GIO modules or by individual applications. 
129  * For more information about writing GIO modules, see #GIOModule.
130  *
131  * <!-- TODO: Implementation note about using extended attributes on supported 
132  * file systems -->
133  * 
134  * <para><table>
135  * <title>GFileAttributes Built-in Keys and Value Types</title>
136  * <tgroup cols='3' align='left'><thead>
137  * <row><entry>Enum Value</entry><entry>Namespace:Key</entry><entry>Value Type</entry></row>
138  * </thead><tbody>
139  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_TYPE</entry><entry>standard::type</entry><entry>uint32 (#GFileType)</entry></row>
140  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN</entry><entry>standard::is-hidden</entry><entry>boolean</entry></row>
141  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP</entry><entry>standard::is-backup</entry><entry>boolean</entry></row>
142  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK</entry><entry>standard::is-symlink</entry><entry>boolean</entry></row>
143  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_IS_VIRTUAL</entry><entry>standard::is-virtual</entry><entry>boolean</entry></row>
144  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_NAME</entry><entry>standard::name</entry><entry>byte string</entry></row>
145  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME</entry><entry>standard::display-name</entry><entry>string</entry></row>
146  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME</entry><entry>standard::edit-name</entry><entry>string</entry></row>
147  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_ICON</entry><entry>standard::icon</entry><entry>object (#GIcon)</entry></row>
148  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE</entry><entry>standard::content-type</entry><entry>string</entry></row>
149  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE</entry><entry>standard::fast-content-type</entry><entry>string</entry></row>
150  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_SIZE</entry><entry>standard::size</entry><entry>uint64</entry></row>
151  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET</entry><entry>standard::symlink-target</entry><entry>byte string</entry></row>
152  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_TARGET_URI</entry><entry>standard::target-uri</entry><entry>string</entry></row>
153  * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER</entry><entry>standard::sort-order</entry><entry>int32</entry></row>
154  * <row><entry>%G_FILE_ATTRIBUTE_ETAG_VALUE</entry><entry>etag::value</entry><entry>string</entry></row>
155  * <row><entry>%G_FILE_ATTRIBUTE_ID_FILE</entry><entry>id::file</entry><entry>string</entry></row>
156  * <row><entry>%G_FILE_ATTRIBUTE_ID_FILESYSTEM</entry><entry>id::filesystem</entry><entry>string</entry></row>
157  * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_READ</entry><entry>access::can-read</entry><entry>boolean</entry></row>
158  * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE</entry><entry>access::can-write</entry><entry>boolean</entry></row>
159  * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE</entry><entry>access::can-execute</entry><entry>boolean</entry></row>
160  * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE</entry><entry>access::can-delete</entry><entry>boolean</entry></row>
161  * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH</entry><entry>access::can-trash</entry><entry>boolean</entry></row>
162  * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME</entry><entry>access::can-rename</entry><entry>boolean</entry></row>
163  * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_CAN_MOUNT</entry><entry>mountable::can-mount</entry><entry>boolean</entry></row>
164  * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_CAN_UNMOUNT</entry><entry>mountable::can-unmount</entry><entry>boolean</entry></row>
165  * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_CAN_EJECT</entry><entry>mountable::can-eject</entry><entry>boolean</entry></row>
166  * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_UNIX_DEVICE</entry><entry>mountable::unix-device</entry><entry>uint32</entry></row>
167  * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_HAL_UDI</entry><entry>mountable::hal-udi</entry><entry>string</entry></row>
168  * <row><entry>%G_FILE_ATTRIBUTE_TIME_MODIFIED</entry><entry>time::modified</entry><entry>uint64</entry></row>
169  * <row><entry>%G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC</entry><entry>time::modified-usec</entry><entry>uint32</entry></row>
170  * <row><entry>%G_FILE_ATTRIBUTE_TIME_ACCESS</entry><entry>time::access</entry><entry>uint64</entry></row>
171  * <row><entry>%G_FILE_ATTRIBUTE_TIME_ACCESS_USEC</entry><entry>time::access-usec</entry><entry>uint32</entry></row>
172  * <row><entry>%G_FILE_ATTRIBUTE_TIME_CHANGED</entry><entry>time::changed</entry><entry>uint64</entry></row>
173  * <row><entry>%G_FILE_ATTRIBUTE_TIME_CHANGED_USEC</entry><entry>time::changed-usec</entry><entry>uint32</entry></row>
174  * <row><entry>%G_FILE_ATTRIBUTE_TIME_CREATED</entry><entry>time::created</entry><entry>uint64</entry></row>
175  * <row><entry>%G_FILE_ATTRIBUTE_TIME_CREATED_USEC</entry><entry>time::created-usec</entry><entry>uint32</entry></row>
176  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_DEVICE</entry><entry>unix::device</entry><entry>uint32</entry></row>
177  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_INODE</entry><entry>unix::inode</entry><entry>uint64</entry></row>
178  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_MODE</entry><entry>unix::mode</entry><entry>uint32</entry></row>
179  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_NLINK</entry><entry>unix::nlink</entry><entry>uint32</entry></row>
180  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_UID</entry><entry>unix::uid</entry><entry>uint32</entry></row>
181  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_GID</entry><entry>unix::gid</entry><entry>uint32</entry></row>
182  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_RDEV</entry><entry>unix::rdev</entry><entry>uint32</entry></row>
183  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE</entry><entry>unix::block-size</entry><entry>uint32</entry></row>
184  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_BLOCKS</entry><entry>unix::blocks</entry><entry>uint64</entry></row>
185  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT</entry><entry>unix::is-mountpoint</entry><entry>boolean</entry></row>
186  * <row><entry>%G_FILE_ATTRIBUTE_DOS_IS_ARCHIVE</entry><entry>dos::is-archive</entry><entry>boolean</entry></row>
187  * <row><entry>%G_FILE_ATTRIBUTE_DOS_IS_SYSTEM</entry><entry>dos::is-system</entry><entry>boolean</entry></row>
188  * <row><entry>%G_FILE_ATTRIBUTE_OWNER_USER</entry><entry>owner::user</entry><entry>string</entry></row>
189  * <row><entry>%G_FILE_ATTRIBUTE_OWNER_USER_REAL</entry><entry>owner::user-real</entry><entry>string</entry></row>
190  * <row><entry>%G_FILE_ATTRIBUTE_OWNER_GROUP</entry><entry>owner::group</entry><entry>string</entry></row>
191  * <row><entry>%G_FILE_ATTRIBUTE_THUMBNAIL_PATH</entry><entry>thumbnail::path</entry><entry>bytestring</entry></row>
192  * <row><entry>%G_FILE_ATTRIBUTE_THUMBNAILING_FAILED</entry><entry>thumbnail::failed</entry><entry>boolean</entry></row>
193  * <row><entry>%G_FILE_ATTRIBUTE_PREVIEW_ICON</entry><entry>preview::icon</entry><entry>object (#GIcon)</entry></row>
194  * <row><entry>%G_FILE_ATTRIBUTE_FILESYSTEM_SIZE</entry><entry>filesystem::size</entry><entry>uint64</entry></row>
195  * <row><entry>%G_FILE_ATTRIBUTE_FILESYSTEM_FREE</entry><entry>filesystem::free</entry><entry>uint64</entry></row>
196  * <row><entry>%G_FILE_ATTRIBUTE_FILESYSTEM_TYPE</entry><entry>filesystem::type</entry><entry>string</entry></row>
197  * <row><entry>%G_FILE_ATTRIBUTE_FILESYSTEM_READONLY</entry><entry>filesystem::readonly</entry><entry>boolean</entry></row>
198  * <row><entry>%G_FILE_ATTRIBUTE_GVFS_BACKEND</entry><entry>gvfs::backend</entry><entry>string</entry></row>
199  * <row><entry>%G_FILE_ATTRIBUTE_SELINUX_CONTEXT</entry><entry>selinux::context</entry><entry>string</entry></row>
200  * </tbody></tgroup></table></para>
201  *  
202  * Note that there are no predefined keys in the "xattr" and "xattr-sys" 
203  * namespaces. Keys for the "xattr" namespace are constructed by stripping
204  * away the "user." prefix from the extended user attribute, and prepending
205  * "xattr::". Keys for the "xattr-sys" namespace are constructed by 
206  * concatenating "xattr-sys::" with the extended attribute name. All extended
207  * attribute values are returned as hex-encoded strings in which bytes outside
208  * the ASCII range are encoded as hexadecimal escape sequences of the form
209  * \x<replaceable>nn</replaceable>.
210  **/ 
211
212 /*
213  * _g_file_attribute_value_free:
214  * @attr: a #GFileAttributeValue. 
215  * 
216  * Frees the memory used by @attr.
217  *
218  **/
219 void
220 _g_file_attribute_value_free (GFileAttributeValue *attr)
221 {
222   g_return_if_fail (attr != NULL);
223
224   _g_file_attribute_value_clear (attr);
225   g_free (attr);
226 }
227
228 /*
229  * _g_file_attribute_value_clear:
230  * @attr: a #GFileAttributeValue.
231  *
232  * Clears the value of @attr and sets its type to 
233  * %G_FILE_ATTRIBUTE_TYPE_INVALID.
234  * 
235  **/
236 void
237 _g_file_attribute_value_clear (GFileAttributeValue *attr)
238 {
239   g_return_if_fail (attr != NULL);
240
241   if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
242       attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
243     g_free (attr->u.string);
244   
245   if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
246       attr->u.obj != NULL)
247     g_object_unref (attr->u.obj);
248   
249   attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
250 }
251
252 /*
253  * g_file_attribute_value_set:
254  * @attr: a #GFileAttributeValue to set the value in.
255  * @new_value: a #GFileAttributeValue to get the value from.
256  * 
257  * Sets an attribute's value from another attribute.
258  **/
259 void
260 _g_file_attribute_value_set (GFileAttributeValue        *attr,
261                              const GFileAttributeValue *new_value)
262 {
263   g_return_if_fail (attr != NULL);
264   g_return_if_fail (new_value != NULL);
265
266   _g_file_attribute_value_clear (attr);
267   *attr = *new_value;
268
269   if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
270       attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
271     attr->u.string = g_strdup (attr->u.string);
272   
273   if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
274       attr->u.obj != NULL)
275     g_object_ref (attr->u.obj);
276 }
277
278 /*
279  * _g_file_attribute_value_new:
280  * 
281  * Creates a new file attribute.
282  * 
283  * Returns: a #GFileAttributeValue.
284  **/
285 GFileAttributeValue *
286 _g_file_attribute_value_new (void)
287 {
288   GFileAttributeValue *attr;
289
290   attr = g_new (GFileAttributeValue, 1);
291   attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
292   return attr;
293 }
294
295 gpointer
296 _g_file_attribute_value_peek_as_pointer (GFileAttributeValue *attr)
297 {
298   switch (attr->type) {
299   case G_FILE_ATTRIBUTE_TYPE_STRING:
300   case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
301     return attr->u.string;
302   case G_FILE_ATTRIBUTE_TYPE_OBJECT:
303     return attr->u.obj;
304   default:
305     return (gpointer) &attr->u;
306   }
307 }
308
309 /*
310  * g_file_attribute_value_dup:
311  * @other: a #GFileAttributeValue to duplicate.
312  * 
313  * Duplicates a file attribute.
314  * 
315  * Returns: a duplicate of the @other.
316  **/
317 GFileAttributeValue *
318 _g_file_attribute_value_dup (const GFileAttributeValue *other)
319 {
320   GFileAttributeValue *attr;
321
322   g_return_val_if_fail (other != NULL, NULL);
323
324   attr = g_new (GFileAttributeValue, 1);
325   attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
326   _g_file_attribute_value_set (attr, other);
327   return attr;
328 }
329
330 GType
331 g_file_attribute_info_list_get_type (void)
332 {
333   static volatile gsize g_define_type_id__volatile = 0;
334
335   if (g_once_init_enter (&g_define_type_id__volatile))
336     {
337       GType g_define_type_id =
338         g_boxed_type_register_static (I_("GFileAttributeInfoList"),
339                                       (GBoxedCopyFunc) g_file_attribute_info_list_dup,
340                                       (GBoxedFreeFunc) g_file_attribute_info_list_unref);
341
342       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
343     }
344
345   return g_define_type_id__volatile;
346 }
347
348 static gboolean
349 valid_char (char c)
350 {
351   return c >= 32 && c <= 126 && c != '\\';
352 }
353
354 static char *
355 escape_byte_string (const char *str)
356 {
357   size_t len;
358   int num_invalid, i;
359   char *escaped_val, *p;
360   unsigned char c;
361   char *hex_digits = "0123456789abcdef";
362   
363   len = strlen (str);
364   
365   num_invalid = 0;
366   for (i = 0; i < len; i++)
367     {
368       if (!valid_char (str[i]))
369         num_invalid++;
370     }
371         
372   if (num_invalid == 0)
373     return g_strdup (str);
374   else
375     {
376       escaped_val = g_malloc (len + num_invalid*3 + 1);
377
378       p = escaped_val;
379       for (i = 0; i < len; i++)
380         {
381           c = str[i];
382           if (valid_char (c))
383             *p++ = c;
384           else
385             {
386               *p++ = '\\';
387               *p++ = 'x';
388               *p++ = hex_digits[(c >> 4) & 0xf];
389               *p++ = hex_digits[c & 0xf];
390             }
391         }
392       *p++ = 0;
393       return escaped_val;
394     }
395 }
396
397 /*
398  * g_file_attribute_value_as_string:
399  * @attr: a #GFileAttributeValue.
400  *
401  * Converts a #GFileAttributeValue to a string for display.
402  * The returned string should be freed when no longer needed.
403  * 
404  * Returns: a string from the @attr, %NULL on error, or "&lt;invalid&gt;" 
405  * if @attr is of type %G_FILE_ATTRIBUTE_TYPE_INVALID.
406  **/
407 char *
408 _g_file_attribute_value_as_string (const GFileAttributeValue *attr)
409 {
410   char *str;
411
412   g_return_val_if_fail (attr != NULL, NULL);
413
414   switch (attr->type)
415     {
416     case G_FILE_ATTRIBUTE_TYPE_STRING:
417       str = g_strdup (attr->u.string);
418       break;
419     case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
420       str = escape_byte_string (attr->u.string);
421       break;
422     case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
423       str = g_strdup_printf ("%s", attr->u.boolean?"TRUE":"FALSE");
424       break;
425     case G_FILE_ATTRIBUTE_TYPE_UINT32:
426       str = g_strdup_printf ("%u", (unsigned int)attr->u.uint32);
427       break;
428     case G_FILE_ATTRIBUTE_TYPE_INT32:
429       str = g_strdup_printf ("%i", (int)attr->u.int32);
430       break;
431     case G_FILE_ATTRIBUTE_TYPE_UINT64:
432       str = g_strdup_printf ("%"G_GUINT64_FORMAT, attr->u.uint64);
433       break;
434     case G_FILE_ATTRIBUTE_TYPE_INT64:
435       str = g_strdup_printf ("%"G_GINT64_FORMAT, attr->u.int64);
436       break;
437     case G_FILE_ATTRIBUTE_TYPE_OBJECT:
438       str = g_strdup_printf ("%s:%p", g_type_name_from_instance
439                                           ((GTypeInstance *) attr->u.obj),
440                                       attr->u.obj);
441       break;
442     default:
443       g_warning ("Invalid type in GFileInfo attribute");
444       str = g_strdup ("<invalid>");
445       break;
446     }
447   
448   return str;
449 }
450
451 /*
452  * g_file_attribute_value_get_string:
453  * @attr: a #GFileAttributeValue.
454  * 
455  * Gets the string from a file attribute value. If the value is not the
456  * right type then %NULL will be returned.
457  * 
458  * Returns: the string value contained within the attribute, or %NULL.
459  **/
460 const char *
461 _g_file_attribute_value_get_string (const GFileAttributeValue *attr)
462 {
463   if (attr == NULL)
464     return NULL;
465
466   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING, NULL);
467
468   return attr->u.string;
469 }
470
471 /*
472  * g_file_attribute_value_get_byte_string:
473  * @attr: a #GFileAttributeValue.
474  * 
475  * Gets the byte string from a file attribute value. If the value is not the
476  * right type then %NULL will be returned.
477  * 
478  * Returns: the byte string contained within the attribute or %NULL.
479  **/
480 const char *
481 _g_file_attribute_value_get_byte_string (const GFileAttributeValue *attr)
482 {
483   if (attr == NULL)
484     return NULL;
485
486   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, NULL);
487
488   return attr->u.string;
489 }
490   
491 /*
492  * g_file_attribute_value_get_boolean:
493  * @attr: a #GFileAttributeValue.
494  * 
495  * Gets the boolean value from a file attribute value. If the value is not the
496  * right type then %FALSE will be returned.
497  * 
498  * Returns: the boolean value contained within the attribute, or %FALSE.
499  **/
500 gboolean
501 _g_file_attribute_value_get_boolean (const GFileAttributeValue *attr)
502 {
503   if (attr == NULL)
504     return FALSE;
505
506   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BOOLEAN, FALSE);
507
508   return attr->u.boolean;
509 }
510   
511 /*
512  * g_file_attribute_value_get_uint32:
513  * @attr: a #GFileAttributeValue.
514  * 
515  * Gets the unsigned 32-bit integer from a file attribute value. If the value 
516  * is not the right type then %0 will be returned.
517  * 
518  * Returns: the unsigned 32-bit integer from the attribute, or %0.
519  **/
520 guint32
521 _g_file_attribute_value_get_uint32 (const GFileAttributeValue *attr)
522 {
523   if (attr == NULL)
524     return 0;
525
526   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT32, 0);
527
528   return attr->u.uint32;
529 }
530
531 /*
532  * g_file_attribute_value_get_int32:
533  * @attr: a #GFileAttributeValue.
534  * 
535  * Gets the signed 32-bit integer from a file attribute value. If the value 
536  * is not the right type then %0 will be returned.
537  * 
538  * Returns: the signed 32-bit integer from the attribute, or %0.
539  **/
540 gint32
541 _g_file_attribute_value_get_int32 (const GFileAttributeValue *attr)
542 {
543   if (attr == NULL)
544     return 0;
545
546   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT32, 0);
547
548   return attr->u.int32;
549 }
550
551 /*
552  * g_file_attribute_value_get_uint64:
553  * @attr: a #GFileAttributeValue.
554  * 
555  * Gets the unsigned 64-bit integer from a file attribute value. If the value 
556  * is not the right type then %0 will be returned.
557  * 
558  * Returns: the unsigned 64-bit integer from the attribute, or %0.
559  **/  
560 guint64
561 _g_file_attribute_value_get_uint64 (const GFileAttributeValue *attr)
562 {
563   if (attr == NULL)
564     return 0;
565
566   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT64, 0);
567
568   return attr->u.uint64;
569 }
570
571 /*
572  * g_file_attribute_value_get_int64:
573  * @attr: a #GFileAttributeValue.
574  * 
575  * Gets the signed 64-bit integer from a file attribute value. If the value 
576  * is not the right type then %0 will be returned.
577  * 
578  * Returns: the signed 64-bit integer from the attribute, or %0. 
579  **/
580 gint64
581 _g_file_attribute_value_get_int64 (const GFileAttributeValue *attr)
582 {
583   if (attr == NULL)
584     return 0;
585
586   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT64, 0);
587
588   return attr->u.int64;
589 }
590
591 /*
592  * g_file_attribute_value_get_object:
593  * @attr: a #GFileAttributeValue.
594  * 
595  * Gets the GObject from a file attribute value. If the value 
596  * is not the right type then %NULL will be returned.
597  * 
598  * Returns: the GObject from the attribute, or %0.
599  **/
600 GObject *
601 _g_file_attribute_value_get_object (const GFileAttributeValue *attr)
602 {
603   if (attr == NULL)
604     return NULL;
605
606   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT, NULL);
607
608   return attr->u.obj;
609 }
610
611
612 void
613 _g_file_attribute_value_set_from_pointer (GFileAttributeValue *value,
614                                           GFileAttributeType   type,
615                                           gpointer             value_p,
616                                           gboolean             dup)
617 {
618   _g_file_attribute_value_clear (value);
619   value->type = type;
620   switch (type)
621     {
622     case G_FILE_ATTRIBUTE_TYPE_STRING:
623     case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
624       if (dup)
625         value->u.string = g_strdup (value_p);
626       else
627         value->u.string = value_p;
628       break;
629       
630     case G_FILE_ATTRIBUTE_TYPE_OBJECT:
631       if (dup)
632         value->u.obj = g_object_ref (value_p);
633       else
634         value->u.obj = value_p;
635       break;
636       
637     case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
638       value->u.boolean = *(gboolean *)value_p;
639       break;
640       
641     case G_FILE_ATTRIBUTE_TYPE_UINT32:
642       value->u.uint32 = *(guint32 *)value_p;
643       break;
644       
645     case G_FILE_ATTRIBUTE_TYPE_INT32:
646       value->u.int32 = *(gint32 *)value_p;
647       break;
648       
649     case G_FILE_ATTRIBUTE_TYPE_UINT64:
650       value->u.uint64 = *(guint64 *)value_p;
651       break;
652       
653     case G_FILE_ATTRIBUTE_TYPE_INT64:
654       value->u.int64 = *(gint64 *)value_p;
655       break;
656     default:
657       g_warning ("Unknown type specified in g_file_info_set_attribute\n");
658       break;
659     }
660 }
661   
662 /*
663  * g_file_attribute_value_set_string:
664  * @attr: a #GFileAttributeValue.
665  * @string: a string to set within the type.
666  * 
667  * Sets the attribute value to a given string.
668  * 
669  **/
670 void
671 _g_file_attribute_value_set_string (GFileAttributeValue *attr,
672                                     const char          *string)
673 {
674   g_return_if_fail (attr != NULL);
675   g_return_if_fail (string != NULL);
676
677   _g_file_attribute_value_clear (attr);
678   attr->type = G_FILE_ATTRIBUTE_TYPE_STRING;
679   attr->u.string = g_strdup (string);
680 }
681
682 /*
683  * g_file_attribute_value_set_byte_string:
684  * @attr: a #GFileAttributeValue.
685  * @string: a byte string to set within the type.
686  * 
687  * Sets the attribute value to a given byte string.
688  * 
689  **/
690 void
691 _g_file_attribute_value_set_byte_string (GFileAttributeValue *attr,
692                                          const char          *string)
693 {
694   g_return_if_fail (attr != NULL);
695   g_return_if_fail (string != NULL);
696
697   _g_file_attribute_value_clear (attr);
698   attr->type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
699   attr->u.string = g_strdup (string);
700 }
701
702 /*
703  * g_file_attribute_value_set_boolean:
704  * @attr: a #GFileAttributeValue.
705  * @value: a #gboolean to set within the type.
706  * 
707  * Sets the attribute value to the given boolean value. 
708  * 
709  **/
710 void
711 _g_file_attribute_value_set_boolean (GFileAttributeValue *attr,
712                                      gboolean             value)
713 {
714   g_return_if_fail (attr != NULL);
715
716   _g_file_attribute_value_clear (attr);
717   attr->type = G_FILE_ATTRIBUTE_TYPE_BOOLEAN;
718   attr->u.boolean = !!value;
719 }
720
721 /*
722  * g_file_attribute_value_set_uint32:
723  * @attr: a #GFileAttributeValue.
724  * @value: a #guint32 to set within the type.
725  * 
726  * Sets the attribute value to the given unsigned 32-bit integer.
727  * 
728  **/ 
729 void
730 _g_file_attribute_value_set_uint32 (GFileAttributeValue *attr,
731                                     guint32              value)
732 {
733   g_return_if_fail (attr != NULL);
734   
735   _g_file_attribute_value_clear (attr);
736   attr->type = G_FILE_ATTRIBUTE_TYPE_UINT32;
737   attr->u.uint32 = value;
738 }
739
740 /*
741  * g_file_attribute_value_set_int32:
742  * @attr: a #GFileAttributeValue.
743  * @value: a #gint32 to set within the type.
744  * 
745  * Sets the attribute value to the given signed 32-bit integer.
746  *  
747  **/
748 void
749 _g_file_attribute_value_set_int32 (GFileAttributeValue *attr,
750                                    gint32               value)
751 {
752   g_return_if_fail (attr != NULL);
753
754   _g_file_attribute_value_clear (attr);
755   attr->type = G_FILE_ATTRIBUTE_TYPE_INT32;
756   attr->u.int32 = value;
757 }
758
759 /*
760  * g_file_attribute_value_set_uint64:
761  * @attr: a #GFileAttributeValue.
762  * @value: a #guint64 to set within the type.
763  * 
764  * Sets the attribute value to a given unsigned 64-bit integer.
765  * 
766  **/
767 void
768 _g_file_attribute_value_set_uint64 (GFileAttributeValue *attr,
769                                     guint64              value)
770 {
771   g_return_if_fail (attr != NULL);
772
773   _g_file_attribute_value_clear (attr);
774   attr->type = G_FILE_ATTRIBUTE_TYPE_UINT64;
775   attr->u.uint64 = value;
776 }
777
778 /*
779  * g_file_attribute_value_set_int64:
780  * @attr: a #GFileAttributeValue.
781  * @value: a #gint64 to set within the type.
782  * 
783  * Sets the attribute value to a given signed 64-bit integer. 
784  * 
785  **/
786 void
787 _g_file_attribute_value_set_int64 (GFileAttributeValue *attr,
788                                    gint64               value)
789 {
790   g_return_if_fail (attr != NULL);
791
792   _g_file_attribute_value_clear (attr);
793   attr->type = G_FILE_ATTRIBUTE_TYPE_INT64;
794   attr->u.int64 = value;
795 }
796
797 /*
798  * g_file_attribute_value_set_object:
799  * @attr: a #GFileAttributeValue.
800  * @obj: a #GObject.
801  *
802  * Sets the attribute to contain the value @obj.
803  * The @attr references the GObject internally.
804  * 
805  **/
806 void
807 _g_file_attribute_value_set_object (GFileAttributeValue *attr,
808                                     GObject             *obj)
809 {
810   g_return_if_fail (attr != NULL);
811   g_return_if_fail (obj != NULL);
812
813   _g_file_attribute_value_clear (attr);
814   attr->type = G_FILE_ATTRIBUTE_TYPE_OBJECT;
815   attr->u.obj = g_object_ref (obj);
816 }
817
818 typedef struct {
819   GFileAttributeInfoList public;
820   GArray *array;
821   int ref_count;
822 } GFileAttributeInfoListPriv;
823
824 static void
825 list_update_public (GFileAttributeInfoListPriv *priv)
826 {
827   priv->public.infos = (GFileAttributeInfo *)priv->array->data;
828   priv->public.n_infos = priv->array->len;
829 }
830
831 /**
832  * g_file_attribute_info_list_new:
833  * 
834  * Creates a new file attribute info list.
835  * 
836  * Returns: a #GFileAttributeInfoList.
837  **/
838 GFileAttributeInfoList *
839 g_file_attribute_info_list_new (void)
840 {
841   GFileAttributeInfoListPriv *priv;
842
843   priv = g_new0 (GFileAttributeInfoListPriv, 1);
844   
845   priv->ref_count = 1;
846   priv->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
847   
848   list_update_public (priv);
849   
850   return (GFileAttributeInfoList *)priv;
851 }
852
853 /**
854  * g_file_attribute_info_list_dup:
855  * @list: a #GFileAttributeInfoList to duplicate.
856  * 
857  * Makes a duplicate of a file attribute info list.
858  * 
859  * Returns: a copy of the given @list. 
860  **/
861 GFileAttributeInfoList *
862 g_file_attribute_info_list_dup (GFileAttributeInfoList *list)
863 {
864   GFileAttributeInfoListPriv *new;
865   int i;
866   
867   g_return_val_if_fail (list != NULL, NULL);
868
869   new = g_new0 (GFileAttributeInfoListPriv, 1);
870   new->ref_count = 1;
871   new->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
872
873   g_array_set_size (new->array, list->n_infos);
874   list_update_public (new);
875   for (i = 0; i < list->n_infos; i++)
876     {
877       new->public.infos[i].name = g_strdup (list->infos[i].name);
878       new->public.infos[i].type = list->infos[i].type;
879       new->public.infos[i].flags = list->infos[i].flags;
880     }
881   
882   return (GFileAttributeInfoList *)new;
883 }
884
885 /**
886  * g_file_attribute_info_list_ref:
887  * @list: a #GFileAttributeInfoList to reference.
888  * 
889  * References a file attribute info list.
890  * 
891  * Returns: #GFileAttributeInfoList or %NULL on error.
892  **/
893 GFileAttributeInfoList *
894 g_file_attribute_info_list_ref (GFileAttributeInfoList *list)
895 {
896   GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
897   
898   g_return_val_if_fail (list != NULL, NULL);
899   g_return_val_if_fail (priv->ref_count > 0, NULL);
900   
901   g_atomic_int_inc (&priv->ref_count);
902   
903   return list;
904 }
905
906 /**
907  * g_file_attribute_info_list_unref:
908  * @list: The #GFileAttributeInfoList to unreference.
909  * 
910  * Removes a reference from the given @list. If the reference count
911  * falls to zero, the @list is deleted.
912  **/
913 void
914 g_file_attribute_info_list_unref (GFileAttributeInfoList *list)
915 {
916   GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
917   int i;
918   
919   g_return_if_fail (list != NULL);
920   g_return_if_fail (priv->ref_count > 0);
921   
922   if (g_atomic_int_dec_and_test (&priv->ref_count))
923     {
924       for (i = 0; i < list->n_infos; i++)
925         g_free (list->infos[i].name);
926       g_array_free (priv->array, TRUE);
927     }
928 }
929
930 static int
931 g_file_attribute_info_list_bsearch (GFileAttributeInfoList *list,
932                                     const char             *name)
933 {
934   int start, end, mid;
935   
936   start = 0;
937   end = list->n_infos;
938
939   while (start != end)
940     {
941       mid = start + (end - start) / 2;
942
943       if (strcmp (name, list->infos[mid].name) < 0)
944         end = mid;
945       else if (strcmp (name, list->infos[mid].name) > 0)
946         start = mid + 1;
947       else
948         return mid;
949     }
950   return start;
951 }
952
953 /**
954  * g_file_attribute_info_list_lookup:
955  * @list: a #GFileAttributeInfoList.
956  * @name: the name of the attribute to lookup.
957  * 
958  * Gets the file attribute with the name @name from @list.
959  *
960  * Returns: a #GFileAttributeInfo for the @name, or %NULL if an 
961  * attribute isn't found.
962  **/
963 const GFileAttributeInfo *
964 g_file_attribute_info_list_lookup (GFileAttributeInfoList *list,
965                                    const char             *name)
966 {
967   int i;
968   
969   g_return_val_if_fail (list != NULL, NULL);
970   g_return_val_if_fail (name != NULL, NULL);
971   
972   i = g_file_attribute_info_list_bsearch (list, name);
973
974   if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
975     return &list->infos[i];
976   
977   return NULL;
978 }
979
980 /**
981  * g_file_attribute_info_list_add:
982  * @list: a #GFileAttributeInfoList.
983  * @name: the name of the attribute to add.
984  * @type: the #GFileAttributeType for the attribute.
985  * @flags: #GFileAttributeInfoFlags for the attribute.
986  * 
987  * Adds a new attribute with @name to the @list, setting
988  * its @type and @flags. 
989  **/
990 void
991 g_file_attribute_info_list_add    (GFileAttributeInfoList *list,
992                                    const char             *name,
993                                    GFileAttributeType      type,
994                                    GFileAttributeInfoFlags flags)
995 {
996   GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
997   GFileAttributeInfo info;
998   int i;
999   
1000   g_return_if_fail (list != NULL);
1001   g_return_if_fail (name != NULL);
1002
1003   i = g_file_attribute_info_list_bsearch (list, name);
1004
1005   if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
1006     {
1007       list->infos[i].type = type;
1008       return;
1009     }
1010
1011   info.name = g_strdup (name);
1012   info.type = type;
1013   info.flags = flags;
1014   g_array_insert_vals (priv->array, i, &info, 1);
1015
1016   list_update_public (priv);
1017 }
1018
1019 #define __G_FILE_ATTRIBUTE_C__
1020 #include "gioaliasdef.c"