Allow setting G_FILE_ATTRIBUTE_TYPE_INVALID attributes in GFileInfo
[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_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 GType
340 g_file_attribute_info_list_get_type (void)
341 {
342   static volatile gsize g_define_type_id__volatile = 0;
343
344   if (g_once_init_enter (&g_define_type_id__volatile))
345     {
346       GType g_define_type_id =
347         g_boxed_type_register_static (I_("GFileAttributeInfoList"),
348                                       (GBoxedCopyFunc) g_file_attribute_info_list_dup,
349                                       (GBoxedFreeFunc) g_file_attribute_info_list_unref);
350
351       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
352     }
353
354   return g_define_type_id__volatile;
355 }
356
357 static gboolean
358 valid_char (char c)
359 {
360   return c >= 32 && c <= 126 && c != '\\';
361 }
362
363 static char *
364 escape_byte_string (const char *str)
365 {
366   size_t len;
367   int num_invalid, i;
368   char *escaped_val, *p;
369   unsigned char c;
370   const char hex_digits[] = "0123456789abcdef";
371
372   len = strlen (str);
373
374   num_invalid = 0;
375   for (i = 0; i < len; i++)
376     {
377       if (!valid_char (str[i]))
378         num_invalid++;
379     }
380
381   if (num_invalid == 0)
382     return g_strdup (str);
383   else
384     {
385       escaped_val = g_malloc (len + num_invalid*3 + 1);
386
387       p = escaped_val;
388       for (i = 0; i < len; i++)
389         {
390           c = str[i];
391           if (valid_char (c))
392             *p++ = c;
393           else
394             {
395               *p++ = '\\';
396               *p++ = 'x';
397               *p++ = hex_digits[(c >> 4) & 0xf];
398               *p++ = hex_digits[c & 0xf];
399             }
400         }
401       *p++ = 0;
402       return escaped_val;
403     }
404 }
405
406 /*
407  * _g_file_attribute_value_as_string:
408  * @attr: a #GFileAttributeValue.
409  *
410  * Converts a #GFileAttributeValue to a string for display.
411  * The returned string should be freed when no longer needed.
412  *
413  * Returns: a string from the @attr, %NULL on error, or "&lt;invalid&gt;"
414  * if @attr is of type %G_FILE_ATTRIBUTE_TYPE_INVALID.
415  */
416 char *
417 _g_file_attribute_value_as_string (const GFileAttributeValue *attr)
418 {
419   GString *s;
420   int i;
421   char *str;
422
423   g_return_val_if_fail (attr != NULL, NULL);
424
425   switch (attr->type)
426     {
427     case G_FILE_ATTRIBUTE_TYPE_STRING:
428       str = g_strdup (attr->u.string);
429       break;
430     case G_FILE_ATTRIBUTE_TYPE_STRINGV:
431       s = g_string_new ("[");
432       for (i = 0; attr->u.stringv[i] != NULL; i++)
433         {
434           g_string_append (s, attr->u.stringv[i]);
435           if (attr->u.stringv[i+1] != NULL)
436             g_string_append (s, ", ");
437         }
438       g_string_append (s, "]");
439       str = g_string_free (s, FALSE);
440       break;
441     case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
442       str = escape_byte_string (attr->u.string);
443       break;
444     case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
445       str = g_strdup_printf ("%s", attr->u.boolean?"TRUE":"FALSE");
446       break;
447     case G_FILE_ATTRIBUTE_TYPE_UINT32:
448       str = g_strdup_printf ("%u", (unsigned int)attr->u.uint32);
449       break;
450     case G_FILE_ATTRIBUTE_TYPE_INT32:
451       str = g_strdup_printf ("%i", (int)attr->u.int32);
452       break;
453     case G_FILE_ATTRIBUTE_TYPE_UINT64:
454       str = g_strdup_printf ("%"G_GUINT64_FORMAT, attr->u.uint64);
455       break;
456     case G_FILE_ATTRIBUTE_TYPE_INT64:
457       str = g_strdup_printf ("%"G_GINT64_FORMAT, attr->u.int64);
458       break;
459     case G_FILE_ATTRIBUTE_TYPE_OBJECT:
460       str = g_strdup_printf ("%s:%p", g_type_name_from_instance
461                                           ((GTypeInstance *) attr->u.obj),
462                                       attr->u.obj);
463       break;
464     case G_FILE_ATTRIBUTE_TYPE_INVALID:
465       str = g_strdup ("<unset>");
466       break;
467     default:
468       g_warning ("Invalid type in GFileInfo attribute");
469       str = g_strdup ("<invalid>");
470       break;
471     }
472
473   return str;
474 }
475
476 /*
477  * _g_file_attribute_value_get_string:
478  * @attr: a #GFileAttributeValue.
479  *
480  * Gets the string from a file attribute value. If the value is not the
481  * right type then %NULL will be returned.
482  *
483  * Returns: the string value contained within the attribute, or %NULL.
484  */
485 const char *
486 _g_file_attribute_value_get_string (const GFileAttributeValue *attr)
487 {
488   if (attr == NULL)
489     return NULL;
490
491   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING, NULL);
492
493   return attr->u.string;
494 }
495
496 /*
497  * _g_file_attribute_value_get_byte_string:
498  * @attr: a #GFileAttributeValue.
499  *
500  * Gets the byte string from a file attribute value. If the value is not the
501  * right type then %NULL will be returned.
502  *
503  * Returns: the byte string contained within the attribute or %NULL.
504  */
505 const char *
506 _g_file_attribute_value_get_byte_string (const GFileAttributeValue *attr)
507 {
508   if (attr == NULL)
509     return NULL;
510
511   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, NULL);
512
513   return attr->u.string;
514 }
515
516 char **
517 _g_file_attribute_value_get_stringv (const GFileAttributeValue *attr)
518 {
519   if (attr == NULL)
520     return NULL;
521
522   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV, NULL);
523
524   return attr->u.stringv;
525 }
526
527 /*
528  * _g_file_attribute_value_get_boolean:
529  * @attr: a #GFileAttributeValue.
530  *
531  * Gets the boolean value from a file attribute value. If the value is not the
532  * right type then %FALSE will be returned.
533  *
534  * Returns: the boolean value contained within the attribute, or %FALSE.
535  */
536 gboolean
537 _g_file_attribute_value_get_boolean (const GFileAttributeValue *attr)
538 {
539   if (attr == NULL)
540     return FALSE;
541
542   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BOOLEAN, FALSE);
543
544   return attr->u.boolean;
545 }
546
547 /*
548  * _g_file_attribute_value_get_uint32:
549  * @attr: a #GFileAttributeValue.
550  *
551  * Gets the unsigned 32-bit integer from a file attribute value. If the value
552  * is not the right type then 0 will be returned.
553  *
554  * Returns: the unsigned 32-bit integer from the attribute, or 0.
555  */
556 guint32
557 _g_file_attribute_value_get_uint32 (const GFileAttributeValue *attr)
558 {
559   if (attr == NULL)
560     return 0;
561
562   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT32, 0);
563
564   return attr->u.uint32;
565 }
566
567 /*
568  * _g_file_attribute_value_get_int32:
569  * @attr: a #GFileAttributeValue.
570  *
571  * Gets the signed 32-bit integer from a file attribute value. If the value
572  * is not the right type then 0 will be returned.
573  *
574  * Returns: the signed 32-bit integer from the attribute, or 0.
575  */
576 gint32
577 _g_file_attribute_value_get_int32 (const GFileAttributeValue *attr)
578 {
579   if (attr == NULL)
580     return 0;
581
582   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT32, 0);
583
584   return attr->u.int32;
585 }
586
587 /*
588  * _g_file_attribute_value_get_uint64:
589  * @attr: a #GFileAttributeValue.
590  *
591  * Gets the unsigned 64-bit integer from a file attribute value. If the value
592  * is not the right type then 0 will be returned.
593  *
594  * Returns: the unsigned 64-bit integer from the attribute, or 0.
595  */
596 guint64
597 _g_file_attribute_value_get_uint64 (const GFileAttributeValue *attr)
598 {
599   if (attr == NULL)
600     return 0;
601
602   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT64, 0);
603
604   return attr->u.uint64;
605 }
606
607 /*
608  * _g_file_attribute_value_get_int64:
609  * @attr: a #GFileAttributeValue.
610  *
611  * Gets the signed 64-bit integer from a file attribute value. If the value
612  * is not the right type then 0 will be returned.
613  *
614  * Returns: the signed 64-bit integer from the attribute, or 0.
615  */
616 gint64
617 _g_file_attribute_value_get_int64 (const GFileAttributeValue *attr)
618 {
619   if (attr == NULL)
620     return 0;
621
622   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT64, 0);
623
624   return attr->u.int64;
625 }
626
627 /*
628  * _g_file_attribute_value_get_object:
629  * @attr: a #GFileAttributeValue.
630  *
631  * Gets the GObject from a file attribute value. If the value
632  * is not the right type then %NULL will be returned.
633  *
634  * Returns: the GObject from the attribute, or %NULL.
635  **/
636 GObject *
637 _g_file_attribute_value_get_object (const GFileAttributeValue *attr)
638 {
639   if (attr == NULL)
640     return NULL;
641
642   g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT, NULL);
643
644   return attr->u.obj;
645 }
646
647
648 void
649 _g_file_attribute_value_set_from_pointer (GFileAttributeValue *value,
650                                           GFileAttributeType   type,
651                                           gpointer             value_p,
652                                           gboolean             dup)
653 {
654   _g_file_attribute_value_clear (value);
655   value->type = type;
656   switch (type)
657     {
658     case G_FILE_ATTRIBUTE_TYPE_STRING:
659     case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
660       if (dup)
661         value->u.string = g_strdup (value_p);
662       else
663         value->u.string = value_p;
664       break;
665
666     case G_FILE_ATTRIBUTE_TYPE_STRINGV:
667       if (dup)
668         value->u.stringv = g_strdupv (value_p);
669       else
670         value->u.stringv = value_p;
671       break;
672
673     case G_FILE_ATTRIBUTE_TYPE_OBJECT:
674       if (dup)
675         value->u.obj = g_object_ref (value_p);
676       else
677         value->u.obj = value_p;
678       break;
679
680     case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
681       value->u.boolean = *(gboolean *)value_p;
682       break;
683
684     case G_FILE_ATTRIBUTE_TYPE_UINT32:
685       value->u.uint32 = *(guint32 *)value_p;
686       break;
687
688     case G_FILE_ATTRIBUTE_TYPE_INT32:
689       value->u.int32 = *(gint32 *)value_p;
690       break;
691
692     case G_FILE_ATTRIBUTE_TYPE_UINT64:
693       value->u.uint64 = *(guint64 *)value_p;
694       break;
695
696     case G_FILE_ATTRIBUTE_TYPE_INT64:
697       value->u.int64 = *(gint64 *)value_p;
698       break;
699
700     case G_FILE_ATTRIBUTE_TYPE_INVALID:
701       break;
702
703     default:
704       g_warning ("Unknown type specified in g_file_info_set_attribute\n");
705       break;
706     }
707 }
708
709 /*
710  * _g_file_attribute_value_set_string:
711  * @attr: a #GFileAttributeValue.
712  * @string: a string to set within the type.
713  *
714  * Sets the attribute value to a given string.
715  */
716 void
717 _g_file_attribute_value_set_string (GFileAttributeValue *attr,
718                                     const char          *string)
719 {
720   g_return_if_fail (attr != NULL);
721   g_return_if_fail (string != NULL);
722
723   _g_file_attribute_value_clear (attr);
724   attr->type = G_FILE_ATTRIBUTE_TYPE_STRING;
725   attr->u.string = g_strdup (string);
726 }
727
728 /*
729  * _g_file_attribute_value_set_byte_string:
730  * @attr: a #GFileAttributeValue.
731  * @string: a byte string to set within the type.
732  *
733  * Sets the attribute value to a given byte string.
734  */
735 void
736 _g_file_attribute_value_set_byte_string (GFileAttributeValue *attr,
737                                          const char          *string)
738 {
739   g_return_if_fail (attr != NULL);
740   g_return_if_fail (string != NULL);
741
742   _g_file_attribute_value_clear (attr);
743   attr->type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
744   attr->u.string = g_strdup (string);
745 }
746
747 void
748 _g_file_attribute_value_set_stringv (GFileAttributeValue *attr,
749                                      char               **value)
750 {
751   g_return_if_fail (attr != NULL);
752   g_return_if_fail (value != NULL);
753
754   _g_file_attribute_value_clear (attr);
755   attr->type = G_FILE_ATTRIBUTE_TYPE_STRINGV;
756   attr->u.stringv = g_strdupv (value);
757 }
758
759
760 /*
761  * _g_file_attribute_value_set_boolean:
762  * @attr: a #GFileAttributeValue.
763  * @value: a #gboolean to set within the type.
764  *
765  * Sets the attribute value to the given boolean value.
766  */
767 void
768 _g_file_attribute_value_set_boolean (GFileAttributeValue *attr,
769                                      gboolean             value)
770 {
771   g_return_if_fail (attr != NULL);
772
773   _g_file_attribute_value_clear (attr);
774   attr->type = G_FILE_ATTRIBUTE_TYPE_BOOLEAN;
775   attr->u.boolean = !!value;
776 }
777
778 /*
779  * _g_file_attribute_value_set_uint32:
780  * @attr: a #GFileAttributeValue.
781  * @value: a #guint32 to set within the type.
782  *
783  * Sets the attribute value to the given unsigned 32-bit integer.
784  */
785 void
786 _g_file_attribute_value_set_uint32 (GFileAttributeValue *attr,
787                                     guint32              value)
788 {
789   g_return_if_fail (attr != NULL);
790
791   _g_file_attribute_value_clear (attr);
792   attr->type = G_FILE_ATTRIBUTE_TYPE_UINT32;
793   attr->u.uint32 = value;
794 }
795
796 /*
797  * _g_file_attribute_value_set_int32:
798  * @attr: a #GFileAttributeValue.
799  * @value: a #gint32 to set within the type.
800  *
801  * Sets the attribute value to the given signed 32-bit integer.
802  */
803 void
804 _g_file_attribute_value_set_int32 (GFileAttributeValue *attr,
805                                    gint32               value)
806 {
807   g_return_if_fail (attr != NULL);
808
809   _g_file_attribute_value_clear (attr);
810   attr->type = G_FILE_ATTRIBUTE_TYPE_INT32;
811   attr->u.int32 = value;
812 }
813
814 /*
815  * _g_file_attribute_value_set_uint64:
816  * @attr: a #GFileAttributeValue.
817  * @value: a #guint64 to set within the type.
818  *
819  * Sets the attribute value to a given unsigned 64-bit integer.
820  */
821 void
822 _g_file_attribute_value_set_uint64 (GFileAttributeValue *attr,
823                                     guint64              value)
824 {
825   g_return_if_fail (attr != NULL);
826
827   _g_file_attribute_value_clear (attr);
828   attr->type = G_FILE_ATTRIBUTE_TYPE_UINT64;
829   attr->u.uint64 = value;
830 }
831
832 /*
833  * _g_file_attribute_value_set_int64:
834  * @attr: a #GFileAttributeValue.
835  * @value: a #gint64 to set within the type.
836  *
837  * Sets the attribute value to a given signed 64-bit integer.
838  */
839 void
840 _g_file_attribute_value_set_int64 (GFileAttributeValue *attr,
841                                    gint64               value)
842 {
843   g_return_if_fail (attr != NULL);
844
845   _g_file_attribute_value_clear (attr);
846   attr->type = G_FILE_ATTRIBUTE_TYPE_INT64;
847   attr->u.int64 = value;
848 }
849
850 /*
851  * _g_file_attribute_value_set_object:
852  * @attr: a #GFileAttributeValue.
853  * @obj: a #GObject.
854  *
855  * Sets the attribute to contain the value @obj.
856  * The @attr references the GObject internally.
857  */
858 void
859 _g_file_attribute_value_set_object (GFileAttributeValue *attr,
860                                     GObject             *obj)
861 {
862   g_return_if_fail (attr != NULL);
863   g_return_if_fail (obj != NULL);
864
865   _g_file_attribute_value_clear (attr);
866   attr->type = G_FILE_ATTRIBUTE_TYPE_OBJECT;
867   attr->u.obj = g_object_ref (obj);
868 }
869
870 typedef struct {
871   GFileAttributeInfoList public;
872   GArray *array;
873   int ref_count;
874 } GFileAttributeInfoListPriv;
875
876 static void
877 list_update_public (GFileAttributeInfoListPriv *priv)
878 {
879   priv->public.infos = (GFileAttributeInfo *)priv->array->data;
880   priv->public.n_infos = priv->array->len;
881 }
882
883 /**
884  * g_file_attribute_info_list_new:
885  *
886  * Creates a new file attribute info list.
887  *
888  * Returns: a #GFileAttributeInfoList.
889  */
890 GFileAttributeInfoList *
891 g_file_attribute_info_list_new (void)
892 {
893   GFileAttributeInfoListPriv *priv;
894
895   priv = g_new0 (GFileAttributeInfoListPriv, 1);
896
897   priv->ref_count = 1;
898   priv->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
899
900   list_update_public (priv);
901
902   return (GFileAttributeInfoList *)priv;
903 }
904
905 /**
906  * g_file_attribute_info_list_dup:
907  * @list: a #GFileAttributeInfoList to duplicate.
908  *
909  * Makes a duplicate of a file attribute info list.
910  *
911  * Returns: a copy of the given @list.
912  */
913 GFileAttributeInfoList *
914 g_file_attribute_info_list_dup (GFileAttributeInfoList *list)
915 {
916   GFileAttributeInfoListPriv *new;
917   int i;
918
919   g_return_val_if_fail (list != NULL, NULL);
920
921   new = g_new0 (GFileAttributeInfoListPriv, 1);
922   new->ref_count = 1;
923   new->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
924
925   g_array_set_size (new->array, list->n_infos);
926   list_update_public (new);
927   for (i = 0; i < list->n_infos; i++)
928     {
929       new->public.infos[i].name = g_strdup (list->infos[i].name);
930       new->public.infos[i].type = list->infos[i].type;
931       new->public.infos[i].flags = list->infos[i].flags;
932     }
933
934   return (GFileAttributeInfoList *)new;
935 }
936
937 /**
938  * g_file_attribute_info_list_ref:
939  * @list: a #GFileAttributeInfoList to reference.
940  *
941  * References a file attribute info list.
942  *
943  * Returns: #GFileAttributeInfoList or %NULL on error.
944  */
945 GFileAttributeInfoList *
946 g_file_attribute_info_list_ref (GFileAttributeInfoList *list)
947 {
948   GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
949
950   g_return_val_if_fail (list != NULL, NULL);
951   g_return_val_if_fail (priv->ref_count > 0, NULL);
952
953   g_atomic_int_inc (&priv->ref_count);
954
955   return list;
956 }
957
958 /**
959  * g_file_attribute_info_list_unref:
960  * @list: The #GFileAttributeInfoList to unreference.
961  *
962  * Removes a reference from the given @list. If the reference count
963  * falls to zero, the @list is deleted.
964  */
965 void
966 g_file_attribute_info_list_unref (GFileAttributeInfoList *list)
967 {
968   GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
969   int i;
970
971   g_return_if_fail (list != NULL);
972   g_return_if_fail (priv->ref_count > 0);
973
974   if (g_atomic_int_dec_and_test (&priv->ref_count))
975     {
976       for (i = 0; i < list->n_infos; i++)
977         g_free (list->infos[i].name);
978       g_array_free (priv->array, TRUE);
979     }
980 }
981
982 static int
983 g_file_attribute_info_list_bsearch (GFileAttributeInfoList *list,
984                                     const char             *name)
985 {
986   int start, end, mid;
987
988   start = 0;
989   end = list->n_infos;
990
991   while (start != end)
992     {
993       mid = start + (end - start) / 2;
994
995       if (strcmp (name, list->infos[mid].name) < 0)
996         end = mid;
997       else if (strcmp (name, list->infos[mid].name) > 0)
998         start = mid + 1;
999       else
1000         return mid;
1001     }
1002   return start;
1003 }
1004
1005 /**
1006  * g_file_attribute_info_list_lookup:
1007  * @list: a #GFileAttributeInfoList.
1008  * @name: the name of the attribute to lookup.
1009  *
1010  * Gets the file attribute with the name @name from @list.
1011  *
1012  * Returns: a #GFileAttributeInfo for the @name, or %NULL if an
1013  * attribute isn't found.
1014  */
1015 const GFileAttributeInfo *
1016 g_file_attribute_info_list_lookup (GFileAttributeInfoList *list,
1017                                    const char             *name)
1018 {
1019   int i;
1020
1021   g_return_val_if_fail (list != NULL, NULL);
1022   g_return_val_if_fail (name != NULL, NULL);
1023
1024   i = g_file_attribute_info_list_bsearch (list, name);
1025
1026   if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
1027     return &list->infos[i];
1028
1029   return NULL;
1030 }
1031
1032 /**
1033  * g_file_attribute_info_list_add:
1034  * @list: a #GFileAttributeInfoList.
1035  * @name: the name of the attribute to add.
1036  * @type: the #GFileAttributeType for the attribute.
1037  * @flags: #GFileAttributeInfoFlags for the attribute.
1038  *
1039  * Adds a new attribute with @name to the @list, setting
1040  * its @type and @flags.
1041  */
1042 void
1043 g_file_attribute_info_list_add (GFileAttributeInfoList *list,
1044                                 const char             *name,
1045                                 GFileAttributeType      type,
1046                                 GFileAttributeInfoFlags flags)
1047 {
1048   GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
1049   GFileAttributeInfo info;
1050   int i;
1051
1052   g_return_if_fail (list != NULL);
1053   g_return_if_fail (name != NULL);
1054
1055   i = g_file_attribute_info_list_bsearch (list, name);
1056
1057   if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
1058     {
1059       list->infos[i].type = type;
1060       return;
1061     }
1062
1063   info.name = g_strdup (name);
1064   info.type = type;
1065   info.flags = flags;
1066   g_array_insert_vals (priv->array, i, &info, 1);
1067
1068   list_update_public (priv);
1069 }
1070
1071 #define __G_FILE_ATTRIBUTE_C__
1072 #include "gioaliasdef.c"