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