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