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