98c8beea009f8667cdac08b21f00d14df2a67d54
[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.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. "std:*" will return all of the keys in the 
46  * "std" 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>"std"</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>"fs"</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  * </tbody>
110  * </tgroup>
111  * </table>
112  * </para>
113  * 
114  * Please note that these are not all of the possible namespaces.
115  * More namespaces can be added from GIO modules or by individual applications. 
116  * For more information about writing GIO modules, see #GIOModule.
117  *
118  * <!-- TODO: Implementation note about using extended attributes on supported 
119  * file systems -->
120  * 
121  * <para><table>
122  * <title>GFileAttributes Built-in Keys and Value Types</title>
123  * <tgroup cols='3' align='left'><thead>
124  * <row><entry>Enum Value</entry><entry>Namespace:Key</entry><entry>Value Type</entry></row>
125  * </thead><tbody>
126  * <row><entry>%G_FILE_ATTRIBUTE_STD_TYPE</entry><entry>std::type</entry><entry>uint32 (#GFileType)</entry></row>
127  * <row><entry>%G_FILE_ATTRIBUTE_STD_IS_HIDDEN</entry><entry>std::is-hidden</entry><entry>boolean</entry></row>
128  * <row><entry>%G_FILE_ATTRIBUTE_STD_IS_BACKUP</entry><entry>std::is-backup</entry><entry>boolean</entry></row>
129  * <row><entry>%G_FILE_ATTRIBUTE_STD_IS_SYMLINK</entry><entry>std::is-symlink</entry><entry>boolean</entry></row>
130  * <row><entry>%G_FILE_ATTRIBUTE_STD_IS_VIRTUAL</entry><entry>std::is-virtual</entry><entry>boolean</entry></row>
131  * <row><entry>%G_FILE_ATTRIBUTE_STD_NAME</entry><entry>std::name</entry><entry>byte string</entry></row>
132  * <row><entry>%G_FILE_ATTRIBUTE_STD_DISPLAY_NAME</entry><entry>std::display-name</entry><entry>string</entry></row>
133  * <row><entry>%G_FILE_ATTRIBUTE_STD_EDIT_NAME</entry><entry>std::edit-name</entry><entry>string</entry></row>
134  * <row><entry>%G_FILE_ATTRIBUTE_STD_ICON</entry><entry>std::icon</entry><entry>object (#GIcon)</entry></row>
135  * <row><entry>%G_FILE_ATTRIBUTE_STD_CONTENT_TYPE</entry><entry>std::content-type</entry><entry>string</entry></row>
136  * <row><entry>%G_FILE_ATTRIBUTE_STD_FAST_CONTENT_TYPE</entry><entry>std::fast-content-type</entry><entry>string</entry></row>
137  * <row><entry>%G_FILE_ATTRIBUTE_STD_SIZE</entry><entry>std::size</entry><entry>uint64</entry></row>
138  * <row><entry>%G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET</entry><entry>std::symlink-target</entry><entry>byte string</entry></row>
139  * <row><entry>%G_FILE_ATTRIBUTE_STD_TARGET_URI</entry><entry>std::target-uri</entry><entry>string</entry></row>
140  * <row><entry>%G_FILE_ATTRIBUTE_STD_SORT_ORDER</entry><entry>std::sort-order</entry><entry>int32</entry></row>
141  * <row><entry>%G_FILE_ATTRIBUTE_ETAG_VALUE</entry><entry>etag::value</entry><entry>string</entry></row>
142  * <row><entry>%G_FILE_ATTRIBUTE_ID_FILE</entry><entry>id::file</entry><entry>string</entry></row>
143  * <row><entry>%G_FILE_ATTRIBUTE_ID_FS</entry><entry>id::fs</entry><entry>string</entry></row>
144  * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_READ</entry><entry>access::can-read</entry><entry>boolean</entry></row>
145  * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE</entry><entry>access::can-write</entry><entry>boolean</entry></row>
146  * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE</entry><entry>access::can-execute</entry><entry>boolean</entry></row>
147  * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE</entry><entry>access::can-delete</entry><entry>boolean</entry></row>
148  * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH</entry><entry>access::can-trash</entry><entry>boolean</entry></row>
149  * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME</entry><entry>access::can-rename</entry><entry>boolean</entry></row>
150  * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_CAN_MOUNT</entry><entry>mountable::can-mount</entry><entry>boolean</entry></row>
151  * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_CAN_UNMOUNT</entry><entry>mountable::can-unmount</entry><entry>boolean</entry></row>
152  * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_CAN_EJECT</entry><entry>mountable::can-eject</entry><entry>boolean</entry></row>
153  * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_UNIX_DEVICE</entry><entry>mountable::unix-device</entry><entry>uint32</entry></row>
154  * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_HAL_UDI</entry><entry>mountable::hal-udi</entry><entry>string</entry></row>
155  * <row><entry>%G_FILE_ATTRIBUTE_TIME_MODIFIED</entry><entry>time::modified</entry><entry>uint64</entry></row>
156  * <row><entry>%G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC</entry><entry>time::modified-usec</entry><entry>uint32</entry></row>
157  * <row><entry>%G_FILE_ATTRIBUTE_TIME_ACCESS</entry><entry>time::access</entry><entry>uint64</entry></row>
158  * <row><entry>%G_FILE_ATTRIBUTE_TIME_ACCESS_USEC</entry><entry>time::access-usec</entry><entry>uint32</entry></row>
159  * <row><entry>%G_FILE_ATTRIBUTE_TIME_CHANGED</entry><entry>time::changed</entry><entry>uint64</entry></row>
160  * <row><entry>%G_FILE_ATTRIBUTE_TIME_CHANGED_USEC</entry><entry>time::changed-usec</entry><entry>uint32</entry></row>
161  * <row><entry>%G_FILE_ATTRIBUTE_TIME_CREATED</entry><entry>time::created</entry><entry>uint64</entry></row>
162  * <row><entry>%G_FILE_ATTRIBUTE_TIME_CREATED_USEC</entry><entry>time::created-usec</entry><entry>uint32</entry></row>
163  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_DEVICE</entry><entry>unix::device</entry><entry>uint32</entry></row>
164  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_INODE</entry><entry>unix::inode</entry><entry>uint64</entry></row>
165  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_MODE</entry><entry>unix::mode</entry><entry>uint32</entry></row>
166  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_NLINK</entry><entry>unix::nlink</entry><entry>uint32</entry></row>
167  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_UID</entry><entry>unix::uid</entry><entry>uint32</entry></row>
168  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_GID</entry><entry>unix::gid</entry><entry>uint32</entry></row>
169  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_RDEV</entry><entry>unix::rdev</entry><entry>uint32</entry></row>
170  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE</entry><entry>unix::block-size</entry><entry>uint32</entry></row>
171  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_BLOCKS</entry><entry>unix::blocks</entry><entry>uint64</entry></row>
172  * <row><entry>%G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT</entry><entry>unix::is-mountpoint</entry><entry>boolean</entry></row>
173  * <row><entry>%G_FILE_ATTRIBUTE_DOS_IS_ARCHIVE</entry><entry>dos::is-archive</entry><entry>boolean</entry></row>
174  * <row><entry>%G_FILE_ATTRIBUTE_DOS_IS_SYSTEM</entry><entry>dos::is-system</entry><entry>boolean</entry></row>
175  * <row><entry>%G_FILE_ATTRIBUTE_OWNER_USER</entry><entry>owner::user</entry><entry>string</entry></row>
176  * <row><entry>%G_FILE_ATTRIBUTE_OWNER_USER_REAL</entry><entry>owner::user-real</entry><entry>string</entry></row>
177  * <row><entry>%G_FILE_ATTRIBUTE_OWNER_GROUP</entry><entry>owner::group</entry><entry>string</entry></row>
178  * <row><entry>%G_FILE_ATTRIBUTE_THUMBNAIL_PATH</entry><entry>thumbnail::path</entry><entry>bytestring</entry></row>
179  * <row><entry>%G_FILE_ATTRIBUTE_THUMBNAILING_FAILED</entry><entry>thumbnail::failed</entry><entry>boolean</entry></row>
180  * <row><entry>%G_FILE_ATTRIBUTE_FS_SIZE</entry><entry>fs::size</entry><entry>uint64</entry></row>
181  * <row><entry>%G_FILE_ATTRIBUTE_FS_FREE</entry><entry>fs::free</entry><entry>uint64</entry></row>
182  * <row><entry>%G_FILE_ATTRIBUTE_FS_TYPE</entry><entry>fs::type</entry><entry>string</entry></row>
183  * <row><entry>%G_FILE_ATTRIBUTE_FS_READONLY</entry><entry>fs::readonly</entry><entry>boolean</entry></row>
184  * <row><entry>%G_FILE_ATTRIBUTE_GVFS_BACKEND</entry><entry>gvfs::backend</entry><entry>string</entry></row>
185  * </tbody></tgroup></table></para>
186  *  
187  **/ 
188
189 /*
190  * _g_file_attribute_value_free:
191  * @attr: a #GFileAttributeValue. 
192  * 
193  * Frees the memory used by @attr.
194  *
195  **/
196 void
197 _g_file_attribute_value_free (GFileAttributeValue *attr)
198 {
199   g_return_if_fail (attr != NULL);
200
201   _g_file_attribute_value_clear (attr);
202   g_free (attr);
203 }
204
205 /*
206  * _g_file_attribute_value_clear:
207  * @attr: a #GFileAttributeValue.
208  *
209  * Clears the value of @attr and sets its type to 
210  * %G_FILE_ATTRIBUTE_TYPE_INVALID.
211  * 
212  **/
213 void
214 _g_file_attribute_value_clear (GFileAttributeValue *attr)
215 {
216   g_return_if_fail (attr != NULL);
217
218   if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
219       attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
220     g_free (attr->u.string);
221   
222   if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
223       attr->u.obj != NULL)
224     g_object_unref (attr->u.obj);
225   
226   attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
227 }
228
229 /*
230  * g_file_attribute_value_set:
231  * @attr: a #GFileAttributeValue to set the value in.
232  * @new_value: a #GFileAttributeValue to get the value from.
233  * 
234  * Sets an attribute's value from another attribute.
235  **/
236 void
237 _g_file_attribute_value_set (GFileAttributeValue        *attr,
238                              const GFileAttributeValue *new_value)
239 {
240   g_return_if_fail (attr != NULL);
241   g_return_if_fail (new_value != NULL);
242
243   _g_file_attribute_value_clear (attr);
244   *attr = *new_value;
245
246   if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
247       attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
248     attr->u.string = g_strdup (attr->u.string);
249   
250   if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
251       attr->u.obj != NULL)
252     g_object_ref (attr->u.obj);
253 }
254
255 /*
256  * _g_file_attribute_value_new:
257  * 
258  * Creates a new file attribute.
259  * 
260  * Returns: a #GFileAttributeValue.
261  **/
262 GFileAttributeValue *
263 _g_file_attribute_value_new (void)
264 {
265   GFileAttributeValue *attr;
266
267   attr = g_new (GFileAttributeValue, 1);
268   attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
269   return attr;
270 }
271
272 gpointer
273 _g_file_attribute_value_peek_as_pointer (GFileAttributeValue *attr)
274 {
275   switch (attr->type) {
276   case G_FILE_ATTRIBUTE_TYPE_STRING:
277   case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
278     return attr->u.string;
279   case G_FILE_ATTRIBUTE_TYPE_OBJECT:
280     return attr->u.obj;
281   default:
282     return (gpointer) &attr->u;
283   }
284 }
285
286 /*
287  * g_file_attribute_value_dup:
288  * @other: a #GFileAttributeValue to duplicate.
289  * 
290  * Duplicates a file attribute.
291  * 
292  * Returns: a duplicate of the @other.
293  **/
294 GFileAttributeValue *
295 _g_file_attribute_value_dup (const GFileAttributeValue *other)
296 {
297   GFileAttributeValue *attr;
298
299   g_return_val_if_fail (other != NULL, NULL);
300
301   attr = g_new (GFileAttributeValue, 1);
302   attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
303   _g_file_attribute_value_set (attr, other);
304   return attr;
305 }
306
307 static gboolean
308 valid_char (char c)
309 {
310   return c >= 32 && c <= 126 && c != '\\';
311 }
312
313 static char *
314 escape_byte_string (const char *str)
315 {
316   size_t len;
317   int num_invalid, i;
318   char *escaped_val, *p;
319   unsigned char c;
320   char *hex_digits = "0123456789abcdef";
321   
322   len = strlen (str);
323   
324   num_invalid = 0;
325   for (i = 0; i < len; i++)
326     {
327       if (!valid_char (str[i]))
328         num_invalid++;
329     }
330         
331   if (num_invalid == 0)
332     return g_strdup (str);
333   else
334     {
335       escaped_val = g_malloc (len + num_invalid*3 + 1);
336
337       p = escaped_val;
338       for (i = 0; i < len; i++)
339         {
340           c = str[i];
341           if (valid_char (c))
342             *p++ = c;
343           else
344             {
345               *p++ = '\\';
346               *p++ = 'x';
347               *p++ = hex_digits[(c >> 8) & 0xf];
348               *p++ = hex_digits[c & 0xf];
349             }
350         }
351       *p++ = 0;
352       return escaped_val;
353     }
354 }
355
356 /*
357  * g_file_attribute_value_as_string:
358  * @attr: a #GFileAttributeValue.
359  *
360  * Converts a #GFileAttributeValue to a string for display.
361  * The returned string should be freed when no longer needed.
362  * 
363  * Returns: a string from the @attr, %NULL on error, or "&lt;invalid&gt;" 
364  * if @attr is of type %G_FILE_ATTRIBUTE_TYPE_INVALID.
365  **/
366 char *
367 _g_file_attribute_value_as_string (const GFileAttributeValue *attr)
368 {
369   char *str;
370
371   g_return_val_if_fail (attr != NULL, NULL);
372
373   switch (attr->type)
374     {
375     case G_FILE_ATTRIBUTE_TYPE_STRING:
376       str = g_strdup (attr->u.string);
377       break;
378     case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
379       str = escape_byte_string (attr->u.string);
380       break;
381     case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
382       str = g_strdup_printf ("%s", attr->u.boolean?"TRUE":"FALSE");
383       break;
384     case G_FILE_ATTRIBUTE_TYPE_UINT32:
385       str = g_strdup_printf ("%u", (unsigned int)attr->u.uint32);
386       break;
387     case G_FILE_ATTRIBUTE_TYPE_INT32:
388       str = g_strdup_printf ("%i", (int)attr->u.int32);
389       break;
390     case G_FILE_ATTRIBUTE_TYPE_UINT64:
391       str = g_strdup_printf ("%"G_GUINT64_FORMAT, attr->u.uint64);
392       break;
393     case G_FILE_ATTRIBUTE_TYPE_INT64:
394       str = g_strdup_printf ("%"G_GINT64_FORMAT, attr->u.int64);
395       break;
396     case G_FILE_ATTRIBUTE_TYPE_OBJECT:
397       str = g_strdup_printf ("%s:%p", g_type_name_from_instance
398                                           ((GTypeInstance *) attr->u.obj),
399                                       attr->u.obj);
400       break;
401     default:
402       g_warning ("Invalid type in GFileInfo attribute");
403       str = g_strdup ("<invalid>");
404       break;
405     }
406   
407   return str;
408 }
409
410 /*
411  * g_file_attribute_value_get_string:
412  * @attr: a #GFileAttributeValue.
413  * 
414  * Gets the string from a file attribute value. If the value is not the
415  * right type then %NULL will be returned.
416  * 
417  * Returns: the string value contained within the attribute, or %NULL.
418  **/
419 const char *
420 _g_file_attribute_value_get_string (const GFileAttributeValue *attr)
421 {
422   if (attr == NULL)
423     return NULL;
424
425   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING, NULL);
426
427   return attr->u.string;
428 }
429
430 /*
431  * g_file_attribute_value_get_byte_string:
432  * @attr: a #GFileAttributeValue.
433  * 
434  * Gets the byte string from a file attribute value. If the value is not the
435  * right type then %NULL will be returned.
436  * 
437  * Returns: the byte string contained within the attribute or %NULL.
438  **/
439 const char *
440 _g_file_attribute_value_get_byte_string (const GFileAttributeValue *attr)
441 {
442   if (attr == NULL)
443     return NULL;
444
445   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, NULL);
446
447   return attr->u.string;
448 }
449   
450 /*
451  * g_file_attribute_value_get_boolean:
452  * @attr: a #GFileAttributeValue.
453  * 
454  * Gets the boolean value from a file attribute value. If the value is not the
455  * right type then %FALSE will be returned.
456  * 
457  * Returns: the boolean value contained within the attribute, or %FALSE.
458  **/
459 gboolean
460 _g_file_attribute_value_get_boolean (const GFileAttributeValue *attr)
461 {
462   if (attr == NULL)
463     return FALSE;
464
465   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BOOLEAN, FALSE);
466
467   return attr->u.boolean;
468 }
469   
470 /*
471  * g_file_attribute_value_get_uint32:
472  * @attr: a #GFileAttributeValue.
473  * 
474  * Gets the unsigned 32-bit integer from a file attribute value. If the value 
475  * is not the right type then %0 will be returned.
476  * 
477  * Returns: the unsigned 32-bit integer from the attribute, or %0.
478  **/
479 guint32
480 _g_file_attribute_value_get_uint32 (const GFileAttributeValue *attr)
481 {
482   if (attr == NULL)
483     return 0;
484
485   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT32, 0);
486
487   return attr->u.uint32;
488 }
489
490 /*
491  * g_file_attribute_value_get_int32:
492  * @attr: a #GFileAttributeValue.
493  * 
494  * Gets the signed 32-bit integer from a file attribute value. If the value 
495  * is not the right type then %0 will be returned.
496  * 
497  * Returns: the signed 32-bit integer from the attribute, or %0.
498  **/
499 gint32
500 _g_file_attribute_value_get_int32 (const GFileAttributeValue *attr)
501 {
502   if (attr == NULL)
503     return 0;
504
505   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT32, 0);
506
507   return attr->u.int32;
508 }
509
510 /*
511  * g_file_attribute_value_get_uint64:
512  * @attr: a #GFileAttributeValue.
513  * 
514  * Gets the unsigned 64-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 64-bit integer from the attribute, or %0.
518  **/  
519 guint64
520 _g_file_attribute_value_get_uint64 (const GFileAttributeValue *attr)
521 {
522   if (attr == NULL)
523     return 0;
524
525   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT64, 0);
526
527   return attr->u.uint64;
528 }
529
530 /*
531  * g_file_attribute_value_get_int64:
532  * @attr: a #GFileAttributeValue.
533  * 
534  * Gets the signed 64-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 64-bit integer from the attribute, or %0. 
538  **/
539 gint64
540 _g_file_attribute_value_get_int64 (const GFileAttributeValue *attr)
541 {
542   if (attr == NULL)
543     return 0;
544
545   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT64, 0);
546
547   return attr->u.int64;
548 }
549
550 /*
551  * g_file_attribute_value_get_object:
552  * @attr: a #GFileAttributeValue.
553  * 
554  * Gets the GObject from a file attribute value. If the value 
555  * is not the right type then %NULL will be returned.
556  * 
557  * Returns: the GObject from the attribute, or %0.
558  **/
559 GObject *
560 _g_file_attribute_value_get_object (const GFileAttributeValue *attr)
561 {
562   if (attr == NULL)
563     return NULL;
564
565   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT, NULL);
566
567   return attr->u.obj;
568 }
569
570
571 void
572 _g_file_attribute_value_set_from_pointer (GFileAttributeValue *value,
573                                           GFileAttributeType   type,
574                                           gpointer             value_p,
575                                           gboolean             dup)
576 {
577   _g_file_attribute_value_clear (value);
578   value->type = type;
579   switch (type)
580     {
581     case G_FILE_ATTRIBUTE_TYPE_STRING:
582     case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
583       if (dup)
584         value->u.string = g_strdup (value_p);
585       else
586         value->u.string = value_p;
587       break;
588       
589     case G_FILE_ATTRIBUTE_TYPE_OBJECT:
590       if (dup)
591         value->u.obj = g_object_ref (value_p);
592       else
593         value->u.obj = value_p;
594       break;
595       
596     case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
597       value->u.boolean = *(gboolean *)value_p;
598       break;
599       
600     case G_FILE_ATTRIBUTE_TYPE_UINT32:
601       value->u.uint32 = *(guint32 *)value_p;
602       break;
603       
604     case G_FILE_ATTRIBUTE_TYPE_INT32:
605       value->u.int32 = *(gint32 *)value_p;
606       break;
607       
608     case G_FILE_ATTRIBUTE_TYPE_UINT64:
609       value->u.uint64 = *(guint64 *)value_p;
610       break;
611       
612     case G_FILE_ATTRIBUTE_TYPE_INT64:
613       value->u.int64 = *(gint64 *)value_p;
614       break;
615     default:
616       g_warning ("Unknown type specified in g_file_info_set_attribute\n");
617       break;
618     }
619 }
620   
621 /*
622  * g_file_attribute_value_set_string:
623  * @attr: a #GFileAttributeValue.
624  * @string: a string to set within the type.
625  * 
626  * Sets the attribute value to a given string.
627  * 
628  **/
629 void
630 _g_file_attribute_value_set_string (GFileAttributeValue *attr,
631                                     const char          *string)
632 {
633   g_return_if_fail (attr != NULL);
634   g_return_if_fail (string != NULL);
635
636   _g_file_attribute_value_clear (attr);
637   attr->type = G_FILE_ATTRIBUTE_TYPE_STRING;
638   attr->u.string = g_strdup (string);
639 }
640
641 /*
642  * g_file_attribute_value_set_byte_string:
643  * @attr: a #GFileAttributeValue.
644  * @string: a byte string to set within the type.
645  * 
646  * Sets the attribute value to a given byte string.
647  * 
648  **/
649 void
650 _g_file_attribute_value_set_byte_string (GFileAttributeValue *attr,
651                                          const char          *string)
652 {
653   g_return_if_fail (attr != NULL);
654   g_return_if_fail (string != NULL);
655
656   _g_file_attribute_value_clear (attr);
657   attr->type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
658   attr->u.string = g_strdup (string);
659 }
660
661 /*
662  * g_file_attribute_value_set_boolean:
663  * @attr: a #GFileAttributeValue.
664  * @value: a #gboolean to set within the type.
665  * 
666  * Sets the attribute value to the given boolean value. 
667  * 
668  **/
669 void
670 _g_file_attribute_value_set_boolean (GFileAttributeValue *attr,
671                                      gboolean             value)
672 {
673   g_return_if_fail (attr != NULL);
674
675   _g_file_attribute_value_clear (attr);
676   attr->type = G_FILE_ATTRIBUTE_TYPE_BOOLEAN;
677   attr->u.boolean = !!value;
678 }
679
680 /*
681  * g_file_attribute_value_set_uint32:
682  * @attr: a #GFileAttributeValue.
683  * @value: a #guint32 to set within the type.
684  * 
685  * Sets the attribute value to the given unsigned 32-bit integer.
686  * 
687  **/ 
688 void
689 _g_file_attribute_value_set_uint32 (GFileAttributeValue *attr,
690                                     guint32              value)
691 {
692   g_return_if_fail (attr != NULL);
693   
694   _g_file_attribute_value_clear (attr);
695   attr->type = G_FILE_ATTRIBUTE_TYPE_UINT32;
696   attr->u.uint32 = value;
697 }
698
699 /*
700  * g_file_attribute_value_set_int32:
701  * @attr: a #GFileAttributeValue.
702  * @value: a #gint32 to set within the type.
703  * 
704  * Sets the attribute value to the given signed 32-bit integer.
705  *  
706  **/
707 void
708 _g_file_attribute_value_set_int32 (GFileAttributeValue *attr,
709                                    gint32               value)
710 {
711   g_return_if_fail (attr != NULL);
712
713   _g_file_attribute_value_clear (attr);
714   attr->type = G_FILE_ATTRIBUTE_TYPE_INT32;
715   attr->u.int32 = value;
716 }
717
718 /*
719  * g_file_attribute_value_set_uint64:
720  * @attr: a #GFileAttributeValue.
721  * @value: a #guint64 to set within the type.
722  * 
723  * Sets the attribute value to a given unsigned 64-bit integer.
724  * 
725  **/
726 void
727 _g_file_attribute_value_set_uint64 (GFileAttributeValue *attr,
728                                     guint64              value)
729 {
730   g_return_if_fail (attr != NULL);
731
732   _g_file_attribute_value_clear (attr);
733   attr->type = G_FILE_ATTRIBUTE_TYPE_UINT64;
734   attr->u.uint64 = value;
735 }
736
737 /*
738  * g_file_attribute_value_set_int64:
739  * @attr: a #GFileAttributeValue.
740  * @value: a #gint64 to set within the type.
741  * 
742  * Sets the attribute value to a given signed 64-bit integer. 
743  * 
744  **/
745 void
746 _g_file_attribute_value_set_int64 (GFileAttributeValue *attr,
747                                    gint64               value)
748 {
749   g_return_if_fail (attr != NULL);
750
751   _g_file_attribute_value_clear (attr);
752   attr->type = G_FILE_ATTRIBUTE_TYPE_INT64;
753   attr->u.int64 = value;
754 }
755
756 /*
757  * g_file_attribute_value_set_object:
758  * @attr: a #GFileAttributeValue.
759  * @obj: a #GObject.
760  *
761  * Sets the attribute to contain the value @obj.
762  * The @attr references the GObject internally.
763  * 
764  **/
765 void
766 _g_file_attribute_value_set_object (GFileAttributeValue *attr,
767                                     GObject             *obj)
768 {
769   g_return_if_fail (attr != NULL);
770   g_return_if_fail (obj != NULL);
771
772   _g_file_attribute_value_clear (attr);
773   attr->type = G_FILE_ATTRIBUTE_TYPE_OBJECT;
774   attr->u.obj = g_object_ref (obj);
775 }
776
777 typedef struct {
778   GFileAttributeInfoList public;
779   GArray *array;
780   int ref_count;
781 } GFileAttributeInfoListPriv;
782
783 static void
784 list_update_public (GFileAttributeInfoListPriv *priv)
785 {
786   priv->public.infos = (GFileAttributeInfo *)priv->array->data;
787   priv->public.n_infos = priv->array->len;
788 }
789
790 /**
791  * g_file_attribute_info_list_new:
792  * 
793  * Creates a new file attribute info list.
794  * 
795  * Returns: a #GFileAttributeInfoList.
796  **/
797 GFileAttributeInfoList *
798 g_file_attribute_info_list_new (void)
799 {
800   GFileAttributeInfoListPriv *priv;
801
802   priv = g_new0 (GFileAttributeInfoListPriv, 1);
803   
804   priv->ref_count = 1;
805   priv->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
806   
807   list_update_public (priv);
808   
809   return (GFileAttributeInfoList *)priv;
810 }
811
812 /**
813  * g_file_attribute_info_list_dup:
814  * @list: a #GFileAttributeInfoList to duplicate.
815  * 
816  * Makes a duplicate of a file attribute info list.
817  * 
818  * Returns: a copy of the given @list. 
819  **/
820 GFileAttributeInfoList *
821 g_file_attribute_info_list_dup (GFileAttributeInfoList *list)
822 {
823   GFileAttributeInfoListPriv *new;
824   int i;
825   
826   g_return_val_if_fail (list != NULL, NULL);
827
828   new = g_new0 (GFileAttributeInfoListPriv, 1);
829   new->ref_count = 1;
830   new->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
831
832   g_array_set_size (new->array, list->n_infos);
833   list_update_public (new);
834   for (i = 0; i < list->n_infos; i++)
835     {
836       new->public.infos[i].name = g_strdup (list->infos[i].name);
837       new->public.infos[i].type = list->infos[i].type;
838       new->public.infos[i].flags = list->infos[i].flags;
839     }
840   
841   return (GFileAttributeInfoList *)new;
842 }
843
844 /**
845  * g_file_attribute_info_list_ref:
846  * @list: a #GFileAttributeInfoList to reference.
847  * 
848  * References a file attribute info list.
849  * 
850  * Returns: #GFileAttributeInfoList or %NULL on error.
851  **/
852 GFileAttributeInfoList *
853 g_file_attribute_info_list_ref (GFileAttributeInfoList *list)
854 {
855   GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
856   
857   g_return_val_if_fail (list != NULL, NULL);
858   g_return_val_if_fail (priv->ref_count > 0, NULL);
859   
860   g_atomic_int_inc (&priv->ref_count);
861   
862   return list;
863 }
864
865 /**
866  * g_file_attribute_info_list_unref:
867  * @list: The #GFileAttributeInfoList to unreference.
868  * 
869  * Removes a reference from the given @list. If the reference count
870  * falls to zero, the @list is deleted.
871  **/
872 void
873 g_file_attribute_info_list_unref (GFileAttributeInfoList *list)
874 {
875   GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
876   int i;
877   
878   g_return_if_fail (list != NULL);
879   g_return_if_fail (priv->ref_count > 0);
880   
881   if (g_atomic_int_dec_and_test (&priv->ref_count))
882     {
883       for (i = 0; i < list->n_infos; i++)
884         g_free (list->infos[i].name);
885       g_array_free (priv->array, TRUE);
886     }
887 }
888
889 static int
890 g_file_attribute_info_list_bsearch (GFileAttributeInfoList *list,
891                                     const char             *name)
892 {
893   int start, end, mid;
894   
895   start = 0;
896   end = list->n_infos;
897
898   while (start != end)
899     {
900       mid = start + (end - start) / 2;
901
902       if (strcmp (name, list->infos[mid].name) < 0)
903         end = mid;
904       else if (strcmp (name, list->infos[mid].name) > 0)
905         start = mid + 1;
906       else
907         return mid;
908     }
909   return start;
910 }
911
912 /**
913  * g_file_attribute_info_list_lookup:
914  * @list: a #GFileAttributeInfoList.
915  * @name: the name of the attribute to lookup.
916  * 
917  * Gets the file attribute with the name @name from @list.
918  *
919  * Returns: a #GFileAttributeInfo for the @name, or %NULL if an 
920  * attribute isn't found.
921  **/
922 const GFileAttributeInfo *
923 g_file_attribute_info_list_lookup (GFileAttributeInfoList *list,
924                                    const char             *name)
925 {
926   int i;
927   
928   g_return_val_if_fail (list != NULL, NULL);
929   g_return_val_if_fail (name != NULL, NULL);
930   
931   i = g_file_attribute_info_list_bsearch (list, name);
932
933   if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
934     return &list->infos[i];
935   
936   return NULL;
937 }
938
939 /**
940  * g_file_attribute_info_list_add:
941  * @list: a #GFileAttributeInfoList.
942  * @name: the name of the attribute to add.
943  * @type: the #GFileAttributeType for the attribute.
944  * @flags: #GFileAttributeInfoFlags for the attribute.
945  * 
946  * Adds a new attribute with @name to the @list, setting
947  * its @type and @flags. 
948  **/
949 void
950 g_file_attribute_info_list_add    (GFileAttributeInfoList *list,
951                                    const char             *name,
952                                    GFileAttributeType      type,
953                                    GFileAttributeInfoFlags flags)
954 {
955   GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
956   GFileAttributeInfo info;
957   int i;
958   
959   g_return_if_fail (list != NULL);
960   g_return_if_fail (name != NULL);
961
962   i = g_file_attribute_info_list_bsearch (list, name);
963
964   if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
965     {
966       list->infos[i].type = type;
967       return;
968     }
969
970   info.name = g_strdup (name);
971   info.type = type;
972   info.flags = flags;
973   g_array_insert_vals (priv->array, i, &info, 1);
974
975   list_update_public (priv);
976 }
977
978 #define __G_FILE_ATTRIBUTE_C__
979 #include "gioaliasdef.c"