338f8093db98d0f2b78bd84f18798e7728ada2a3
[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_FILESYSTEM_SIZE</entry><entry>filesystem::size</entry><entry>uint64</entry></row>
194  * <row><entry>%G_FILE_ATTRIBUTE_FILESYSTEM_FREE</entry><entry>filesystem::free</entry><entry>uint64</entry></row>
195  * <row><entry>%G_FILE_ATTRIBUTE_FILESYSTEM_TYPE</entry><entry>filesystem::type</entry><entry>string</entry></row>
196  * <row><entry>%G_FILE_ATTRIBUTE_FILESYSTEM_READONLY</entry><entry>filesystem::readonly</entry><entry>boolean</entry></row>
197  * <row><entry>%G_FILE_ATTRIBUTE_GVFS_BACKEND</entry><entry>gvfs::backend</entry><entry>string</entry></row>
198  * <row><entry>%G_FILE_ATTRIBUTE_SELINUX_CONTEXT</entry><entry>selinux::context</entry><entry>string</entry></row>
199  * </tbody></tgroup></table></para>
200  *  
201  * Note that there are no predefined keys in the "xattr" and "xattr-sys" 
202  * namespaces. Keys for the "xattr" namespace are constructed by stripping
203  * away the "user." prefix from the extended user attribute, and prepending
204  * "xattr::". Keys for the "xattr-sys" namespace are constructed by 
205  * concatenating "xattr-sys::" with the extended attribute name. All extended
206  * attribute values are returned as hex-encoded strings in which bytes outside
207  * the ASCII range are encoded as hexadecimal escape sequences of the form
208  * \x<replaceable>nn</replaceable>.
209  **/ 
210
211 /*
212  * _g_file_attribute_value_free:
213  * @attr: a #GFileAttributeValue. 
214  * 
215  * Frees the memory used by @attr.
216  *
217  **/
218 void
219 _g_file_attribute_value_free (GFileAttributeValue *attr)
220 {
221   g_return_if_fail (attr != NULL);
222
223   _g_file_attribute_value_clear (attr);
224   g_free (attr);
225 }
226
227 /*
228  * _g_file_attribute_value_clear:
229  * @attr: a #GFileAttributeValue.
230  *
231  * Clears the value of @attr and sets its type to 
232  * %G_FILE_ATTRIBUTE_TYPE_INVALID.
233  * 
234  **/
235 void
236 _g_file_attribute_value_clear (GFileAttributeValue *attr)
237 {
238   g_return_if_fail (attr != NULL);
239
240   if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
241       attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
242     g_free (attr->u.string);
243   
244   if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
245       attr->u.obj != NULL)
246     g_object_unref (attr->u.obj);
247   
248   attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
249 }
250
251 /*
252  * g_file_attribute_value_set:
253  * @attr: a #GFileAttributeValue to set the value in.
254  * @new_value: a #GFileAttributeValue to get the value from.
255  * 
256  * Sets an attribute's value from another attribute.
257  **/
258 void
259 _g_file_attribute_value_set (GFileAttributeValue        *attr,
260                              const GFileAttributeValue *new_value)
261 {
262   g_return_if_fail (attr != NULL);
263   g_return_if_fail (new_value != NULL);
264
265   _g_file_attribute_value_clear (attr);
266   *attr = *new_value;
267
268   if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
269       attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
270     attr->u.string = g_strdup (attr->u.string);
271   
272   if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
273       attr->u.obj != NULL)
274     g_object_ref (attr->u.obj);
275 }
276
277 /*
278  * _g_file_attribute_value_new:
279  * 
280  * Creates a new file attribute.
281  * 
282  * Returns: a #GFileAttributeValue.
283  **/
284 GFileAttributeValue *
285 _g_file_attribute_value_new (void)
286 {
287   GFileAttributeValue *attr;
288
289   attr = g_new (GFileAttributeValue, 1);
290   attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
291   return attr;
292 }
293
294 gpointer
295 _g_file_attribute_value_peek_as_pointer (GFileAttributeValue *attr)
296 {
297   switch (attr->type) {
298   case G_FILE_ATTRIBUTE_TYPE_STRING:
299   case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
300     return attr->u.string;
301   case G_FILE_ATTRIBUTE_TYPE_OBJECT:
302     return attr->u.obj;
303   default:
304     return (gpointer) &attr->u;
305   }
306 }
307
308 /*
309  * g_file_attribute_value_dup:
310  * @other: a #GFileAttributeValue to duplicate.
311  * 
312  * Duplicates a file attribute.
313  * 
314  * Returns: a duplicate of the @other.
315  **/
316 GFileAttributeValue *
317 _g_file_attribute_value_dup (const GFileAttributeValue *other)
318 {
319   GFileAttributeValue *attr;
320
321   g_return_val_if_fail (other != NULL, NULL);
322
323   attr = g_new (GFileAttributeValue, 1);
324   attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
325   _g_file_attribute_value_set (attr, other);
326   return attr;
327 }
328
329 GType
330 g_file_attribute_info_list_get_type (void)
331 {
332   static volatile gsize g_define_type_id__volatile = 0;
333
334   if (g_once_init_enter (&g_define_type_id__volatile))
335     {
336       GType g_define_type_id =
337         g_boxed_type_register_static (I_("GFileAttributeInfoList"),
338                                       (GBoxedCopyFunc) g_file_attribute_info_list_dup,
339                                       (GBoxedFreeFunc) g_file_attribute_info_list_unref);
340
341       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
342     }
343
344   return g_define_type_id__volatile;
345 }
346
347 static gboolean
348 valid_char (char c)
349 {
350   return c >= 32 && c <= 126 && c != '\\';
351 }
352
353 static char *
354 escape_byte_string (const char *str)
355 {
356   size_t len;
357   int num_invalid, i;
358   char *escaped_val, *p;
359   unsigned char c;
360   char *hex_digits = "0123456789abcdef";
361   
362   len = strlen (str);
363   
364   num_invalid = 0;
365   for (i = 0; i < len; i++)
366     {
367       if (!valid_char (str[i]))
368         num_invalid++;
369     }
370         
371   if (num_invalid == 0)
372     return g_strdup (str);
373   else
374     {
375       escaped_val = g_malloc (len + num_invalid*3 + 1);
376
377       p = escaped_val;
378       for (i = 0; i < len; i++)
379         {
380           c = str[i];
381           if (valid_char (c))
382             *p++ = c;
383           else
384             {
385               *p++ = '\\';
386               *p++ = 'x';
387               *p++ = hex_digits[(c >> 4) & 0xf];
388               *p++ = hex_digits[c & 0xf];
389             }
390         }
391       *p++ = 0;
392       return escaped_val;
393     }
394 }
395
396 /*
397  * g_file_attribute_value_as_string:
398  * @attr: a #GFileAttributeValue.
399  *
400  * Converts a #GFileAttributeValue to a string for display.
401  * The returned string should be freed when no longer needed.
402  * 
403  * Returns: a string from the @attr, %NULL on error, or "&lt;invalid&gt;" 
404  * if @attr is of type %G_FILE_ATTRIBUTE_TYPE_INVALID.
405  **/
406 char *
407 _g_file_attribute_value_as_string (const GFileAttributeValue *attr)
408 {
409   char *str;
410
411   g_return_val_if_fail (attr != NULL, NULL);
412
413   switch (attr->type)
414     {
415     case G_FILE_ATTRIBUTE_TYPE_STRING:
416       str = g_strdup (attr->u.string);
417       break;
418     case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
419       str = escape_byte_string (attr->u.string);
420       break;
421     case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
422       str = g_strdup_printf ("%s", attr->u.boolean?"TRUE":"FALSE");
423       break;
424     case G_FILE_ATTRIBUTE_TYPE_UINT32:
425       str = g_strdup_printf ("%u", (unsigned int)attr->u.uint32);
426       break;
427     case G_FILE_ATTRIBUTE_TYPE_INT32:
428       str = g_strdup_printf ("%i", (int)attr->u.int32);
429       break;
430     case G_FILE_ATTRIBUTE_TYPE_UINT64:
431       str = g_strdup_printf ("%"G_GUINT64_FORMAT, attr->u.uint64);
432       break;
433     case G_FILE_ATTRIBUTE_TYPE_INT64:
434       str = g_strdup_printf ("%"G_GINT64_FORMAT, attr->u.int64);
435       break;
436     case G_FILE_ATTRIBUTE_TYPE_OBJECT:
437       str = g_strdup_printf ("%s:%p", g_type_name_from_instance
438                                           ((GTypeInstance *) attr->u.obj),
439                                       attr->u.obj);
440       break;
441     default:
442       g_warning ("Invalid type in GFileInfo attribute");
443       str = g_strdup ("<invalid>");
444       break;
445     }
446   
447   return str;
448 }
449
450 /*
451  * g_file_attribute_value_get_string:
452  * @attr: a #GFileAttributeValue.
453  * 
454  * Gets the string from a file attribute value. If the value is not the
455  * right type then %NULL will be returned.
456  * 
457  * Returns: the string value contained within the attribute, or %NULL.
458  **/
459 const char *
460 _g_file_attribute_value_get_string (const GFileAttributeValue *attr)
461 {
462   if (attr == NULL)
463     return NULL;
464
465   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING, NULL);
466
467   return attr->u.string;
468 }
469
470 /*
471  * g_file_attribute_value_get_byte_string:
472  * @attr: a #GFileAttributeValue.
473  * 
474  * Gets the byte string from a file attribute value. If the value is not the
475  * right type then %NULL will be returned.
476  * 
477  * Returns: the byte string contained within the attribute or %NULL.
478  **/
479 const char *
480 _g_file_attribute_value_get_byte_string (const GFileAttributeValue *attr)
481 {
482   if (attr == NULL)
483     return NULL;
484
485   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, NULL);
486
487   return attr->u.string;
488 }
489   
490 /*
491  * g_file_attribute_value_get_boolean:
492  * @attr: a #GFileAttributeValue.
493  * 
494  * Gets the boolean value from a file attribute value. If the value is not the
495  * right type then %FALSE will be returned.
496  * 
497  * Returns: the boolean value contained within the attribute, or %FALSE.
498  **/
499 gboolean
500 _g_file_attribute_value_get_boolean (const GFileAttributeValue *attr)
501 {
502   if (attr == NULL)
503     return FALSE;
504
505   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BOOLEAN, FALSE);
506
507   return attr->u.boolean;
508 }
509   
510 /*
511  * g_file_attribute_value_get_uint32:
512  * @attr: a #GFileAttributeValue.
513  * 
514  * Gets the unsigned 32-bit integer from a file attribute value. If the value 
515  * is not the right type then %0 will be returned.
516  * 
517  * Returns: the unsigned 32-bit integer from the attribute, or %0.
518  **/
519 guint32
520 _g_file_attribute_value_get_uint32 (const GFileAttributeValue *attr)
521 {
522   if (attr == NULL)
523     return 0;
524
525   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT32, 0);
526
527   return attr->u.uint32;
528 }
529
530 /*
531  * g_file_attribute_value_get_int32:
532  * @attr: a #GFileAttributeValue.
533  * 
534  * Gets the signed 32-bit integer from a file attribute value. If the value 
535  * is not the right type then %0 will be returned.
536  * 
537  * Returns: the signed 32-bit integer from the attribute, or %0.
538  **/
539 gint32
540 _g_file_attribute_value_get_int32 (const GFileAttributeValue *attr)
541 {
542   if (attr == NULL)
543     return 0;
544
545   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT32, 0);
546
547   return attr->u.int32;
548 }
549
550 /*
551  * g_file_attribute_value_get_uint64:
552  * @attr: a #GFileAttributeValue.
553  * 
554  * Gets the unsigned 64-bit integer from a file attribute value. If the value 
555  * is not the right type then %0 will be returned.
556  * 
557  * Returns: the unsigned 64-bit integer from the attribute, or %0.
558  **/  
559 guint64
560 _g_file_attribute_value_get_uint64 (const GFileAttributeValue *attr)
561 {
562   if (attr == NULL)
563     return 0;
564
565   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT64, 0);
566
567   return attr->u.uint64;
568 }
569
570 /*
571  * g_file_attribute_value_get_int64:
572  * @attr: a #GFileAttributeValue.
573  * 
574  * Gets the signed 64-bit integer from a file attribute value. If the value 
575  * is not the right type then %0 will be returned.
576  * 
577  * Returns: the signed 64-bit integer from the attribute, or %0. 
578  **/
579 gint64
580 _g_file_attribute_value_get_int64 (const GFileAttributeValue *attr)
581 {
582   if (attr == NULL)
583     return 0;
584
585   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT64, 0);
586
587   return attr->u.int64;
588 }
589
590 /*
591  * g_file_attribute_value_get_object:
592  * @attr: a #GFileAttributeValue.
593  * 
594  * Gets the GObject from a file attribute value. If the value 
595  * is not the right type then %NULL will be returned.
596  * 
597  * Returns: the GObject from the attribute, or %0.
598  **/
599 GObject *
600 _g_file_attribute_value_get_object (const GFileAttributeValue *attr)
601 {
602   if (attr == NULL)
603     return NULL;
604
605   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT, NULL);
606
607   return attr->u.obj;
608 }
609
610
611 void
612 _g_file_attribute_value_set_from_pointer (GFileAttributeValue *value,
613                                           GFileAttributeType   type,
614                                           gpointer             value_p,
615                                           gboolean             dup)
616 {
617   _g_file_attribute_value_clear (value);
618   value->type = type;
619   switch (type)
620     {
621     case G_FILE_ATTRIBUTE_TYPE_STRING:
622     case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
623       if (dup)
624         value->u.string = g_strdup (value_p);
625       else
626         value->u.string = value_p;
627       break;
628       
629     case G_FILE_ATTRIBUTE_TYPE_OBJECT:
630       if (dup)
631         value->u.obj = g_object_ref (value_p);
632       else
633         value->u.obj = value_p;
634       break;
635       
636     case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
637       value->u.boolean = *(gboolean *)value_p;
638       break;
639       
640     case G_FILE_ATTRIBUTE_TYPE_UINT32:
641       value->u.uint32 = *(guint32 *)value_p;
642       break;
643       
644     case G_FILE_ATTRIBUTE_TYPE_INT32:
645       value->u.int32 = *(gint32 *)value_p;
646       break;
647       
648     case G_FILE_ATTRIBUTE_TYPE_UINT64:
649       value->u.uint64 = *(guint64 *)value_p;
650       break;
651       
652     case G_FILE_ATTRIBUTE_TYPE_INT64:
653       value->u.int64 = *(gint64 *)value_p;
654       break;
655     default:
656       g_warning ("Unknown type specified in g_file_info_set_attribute\n");
657       break;
658     }
659 }
660   
661 /*
662  * g_file_attribute_value_set_string:
663  * @attr: a #GFileAttributeValue.
664  * @string: a string to set within the type.
665  * 
666  * Sets the attribute value to a given string.
667  * 
668  **/
669 void
670 _g_file_attribute_value_set_string (GFileAttributeValue *attr,
671                                     const char          *string)
672 {
673   g_return_if_fail (attr != NULL);
674   g_return_if_fail (string != NULL);
675
676   _g_file_attribute_value_clear (attr);
677   attr->type = G_FILE_ATTRIBUTE_TYPE_STRING;
678   attr->u.string = g_strdup (string);
679 }
680
681 /*
682  * g_file_attribute_value_set_byte_string:
683  * @attr: a #GFileAttributeValue.
684  * @string: a byte string to set within the type.
685  * 
686  * Sets the attribute value to a given byte string.
687  * 
688  **/
689 void
690 _g_file_attribute_value_set_byte_string (GFileAttributeValue *attr,
691                                          const char          *string)
692 {
693   g_return_if_fail (attr != NULL);
694   g_return_if_fail (string != NULL);
695
696   _g_file_attribute_value_clear (attr);
697   attr->type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
698   attr->u.string = g_strdup (string);
699 }
700
701 /*
702  * g_file_attribute_value_set_boolean:
703  * @attr: a #GFileAttributeValue.
704  * @value: a #gboolean to set within the type.
705  * 
706  * Sets the attribute value to the given boolean value. 
707  * 
708  **/
709 void
710 _g_file_attribute_value_set_boolean (GFileAttributeValue *attr,
711                                      gboolean             value)
712 {
713   g_return_if_fail (attr != NULL);
714
715   _g_file_attribute_value_clear (attr);
716   attr->type = G_FILE_ATTRIBUTE_TYPE_BOOLEAN;
717   attr->u.boolean = !!value;
718 }
719
720 /*
721  * g_file_attribute_value_set_uint32:
722  * @attr: a #GFileAttributeValue.
723  * @value: a #guint32 to set within the type.
724  * 
725  * Sets the attribute value to the given unsigned 32-bit integer.
726  * 
727  **/ 
728 void
729 _g_file_attribute_value_set_uint32 (GFileAttributeValue *attr,
730                                     guint32              value)
731 {
732   g_return_if_fail (attr != NULL);
733   
734   _g_file_attribute_value_clear (attr);
735   attr->type = G_FILE_ATTRIBUTE_TYPE_UINT32;
736   attr->u.uint32 = value;
737 }
738
739 /*
740  * g_file_attribute_value_set_int32:
741  * @attr: a #GFileAttributeValue.
742  * @value: a #gint32 to set within the type.
743  * 
744  * Sets the attribute value to the given signed 32-bit integer.
745  *  
746  **/
747 void
748 _g_file_attribute_value_set_int32 (GFileAttributeValue *attr,
749                                    gint32               value)
750 {
751   g_return_if_fail (attr != NULL);
752
753   _g_file_attribute_value_clear (attr);
754   attr->type = G_FILE_ATTRIBUTE_TYPE_INT32;
755   attr->u.int32 = value;
756 }
757
758 /*
759  * g_file_attribute_value_set_uint64:
760  * @attr: a #GFileAttributeValue.
761  * @value: a #guint64 to set within the type.
762  * 
763  * Sets the attribute value to a given unsigned 64-bit integer.
764  * 
765  **/
766 void
767 _g_file_attribute_value_set_uint64 (GFileAttributeValue *attr,
768                                     guint64              value)
769 {
770   g_return_if_fail (attr != NULL);
771
772   _g_file_attribute_value_clear (attr);
773   attr->type = G_FILE_ATTRIBUTE_TYPE_UINT64;
774   attr->u.uint64 = value;
775 }
776
777 /*
778  * g_file_attribute_value_set_int64:
779  * @attr: a #GFileAttributeValue.
780  * @value: a #gint64 to set within the type.
781  * 
782  * Sets the attribute value to a given signed 64-bit integer. 
783  * 
784  **/
785 void
786 _g_file_attribute_value_set_int64 (GFileAttributeValue *attr,
787                                    gint64               value)
788 {
789   g_return_if_fail (attr != NULL);
790
791   _g_file_attribute_value_clear (attr);
792   attr->type = G_FILE_ATTRIBUTE_TYPE_INT64;
793   attr->u.int64 = value;
794 }
795
796 /*
797  * g_file_attribute_value_set_object:
798  * @attr: a #GFileAttributeValue.
799  * @obj: a #GObject.
800  *
801  * Sets the attribute to contain the value @obj.
802  * The @attr references the GObject internally.
803  * 
804  **/
805 void
806 _g_file_attribute_value_set_object (GFileAttributeValue *attr,
807                                     GObject             *obj)
808 {
809   g_return_if_fail (attr != NULL);
810   g_return_if_fail (obj != NULL);
811
812   _g_file_attribute_value_clear (attr);
813   attr->type = G_FILE_ATTRIBUTE_TYPE_OBJECT;
814   attr->u.obj = g_object_ref (obj);
815 }
816
817 typedef struct {
818   GFileAttributeInfoList public;
819   GArray *array;
820   int ref_count;
821 } GFileAttributeInfoListPriv;
822
823 static void
824 list_update_public (GFileAttributeInfoListPriv *priv)
825 {
826   priv->public.infos = (GFileAttributeInfo *)priv->array->data;
827   priv->public.n_infos = priv->array->len;
828 }
829
830 /**
831  * g_file_attribute_info_list_new:
832  * 
833  * Creates a new file attribute info list.
834  * 
835  * Returns: a #GFileAttributeInfoList.
836  **/
837 GFileAttributeInfoList *
838 g_file_attribute_info_list_new (void)
839 {
840   GFileAttributeInfoListPriv *priv;
841
842   priv = g_new0 (GFileAttributeInfoListPriv, 1);
843   
844   priv->ref_count = 1;
845   priv->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
846   
847   list_update_public (priv);
848   
849   return (GFileAttributeInfoList *)priv;
850 }
851
852 /**
853  * g_file_attribute_info_list_dup:
854  * @list: a #GFileAttributeInfoList to duplicate.
855  * 
856  * Makes a duplicate of a file attribute info list.
857  * 
858  * Returns: a copy of the given @list. 
859  **/
860 GFileAttributeInfoList *
861 g_file_attribute_info_list_dup (GFileAttributeInfoList *list)
862 {
863   GFileAttributeInfoListPriv *new;
864   int i;
865   
866   g_return_val_if_fail (list != NULL, NULL);
867
868   new = g_new0 (GFileAttributeInfoListPriv, 1);
869   new->ref_count = 1;
870   new->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
871
872   g_array_set_size (new->array, list->n_infos);
873   list_update_public (new);
874   for (i = 0; i < list->n_infos; i++)
875     {
876       new->public.infos[i].name = g_strdup (list->infos[i].name);
877       new->public.infos[i].type = list->infos[i].type;
878       new->public.infos[i].flags = list->infos[i].flags;
879     }
880   
881   return (GFileAttributeInfoList *)new;
882 }
883
884 /**
885  * g_file_attribute_info_list_ref:
886  * @list: a #GFileAttributeInfoList to reference.
887  * 
888  * References a file attribute info list.
889  * 
890  * Returns: #GFileAttributeInfoList or %NULL on error.
891  **/
892 GFileAttributeInfoList *
893 g_file_attribute_info_list_ref (GFileAttributeInfoList *list)
894 {
895   GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
896   
897   g_return_val_if_fail (list != NULL, NULL);
898   g_return_val_if_fail (priv->ref_count > 0, NULL);
899   
900   g_atomic_int_inc (&priv->ref_count);
901   
902   return list;
903 }
904
905 /**
906  * g_file_attribute_info_list_unref:
907  * @list: The #GFileAttributeInfoList to unreference.
908  * 
909  * Removes a reference from the given @list. If the reference count
910  * falls to zero, the @list is deleted.
911  **/
912 void
913 g_file_attribute_info_list_unref (GFileAttributeInfoList *list)
914 {
915   GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
916   int i;
917   
918   g_return_if_fail (list != NULL);
919   g_return_if_fail (priv->ref_count > 0);
920   
921   if (g_atomic_int_dec_and_test (&priv->ref_count))
922     {
923       for (i = 0; i < list->n_infos; i++)
924         g_free (list->infos[i].name);
925       g_array_free (priv->array, TRUE);
926     }
927 }
928
929 static int
930 g_file_attribute_info_list_bsearch (GFileAttributeInfoList *list,
931                                     const char             *name)
932 {
933   int start, end, mid;
934   
935   start = 0;
936   end = list->n_infos;
937
938   while (start != end)
939     {
940       mid = start + (end - start) / 2;
941
942       if (strcmp (name, list->infos[mid].name) < 0)
943         end = mid;
944       else if (strcmp (name, list->infos[mid].name) > 0)
945         start = mid + 1;
946       else
947         return mid;
948     }
949   return start;
950 }
951
952 /**
953  * g_file_attribute_info_list_lookup:
954  * @list: a #GFileAttributeInfoList.
955  * @name: the name of the attribute to lookup.
956  * 
957  * Gets the file attribute with the name @name from @list.
958  *
959  * Returns: a #GFileAttributeInfo for the @name, or %NULL if an 
960  * attribute isn't found.
961  **/
962 const GFileAttributeInfo *
963 g_file_attribute_info_list_lookup (GFileAttributeInfoList *list,
964                                    const char             *name)
965 {
966   int i;
967   
968   g_return_val_if_fail (list != NULL, NULL);
969   g_return_val_if_fail (name != NULL, NULL);
970   
971   i = g_file_attribute_info_list_bsearch (list, name);
972
973   if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
974     return &list->infos[i];
975   
976   return NULL;
977 }
978
979 /**
980  * g_file_attribute_info_list_add:
981  * @list: a #GFileAttributeInfoList.
982  * @name: the name of the attribute to add.
983  * @type: the #GFileAttributeType for the attribute.
984  * @flags: #GFileAttributeInfoFlags for the attribute.
985  * 
986  * Adds a new attribute with @name to the @list, setting
987  * its @type and @flags. 
988  **/
989 void
990 g_file_attribute_info_list_add    (GFileAttributeInfoList *list,
991                                    const char             *name,
992                                    GFileAttributeType      type,
993                                    GFileAttributeInfoFlags flags)
994 {
995   GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
996   GFileAttributeInfo info;
997   int i;
998   
999   g_return_if_fail (list != NULL);
1000   g_return_if_fail (name != NULL);
1001
1002   i = g_file_attribute_info_list_bsearch (list, name);
1003
1004   if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
1005     {
1006       list->infos[i].type = type;
1007       return;
1008     }
1009
1010   info.name = g_strdup (name);
1011   info.type = type;
1012   info.flags = flags;
1013   g_array_insert_vals (priv->array, i, &info, 1);
1014
1015   list_update_public (priv);
1016 }
1017
1018 #define __G_FILE_ATTRIBUTE_C__
1019 #include "gioaliasdef.c"