Formatting cleanup
[glib.git] / gio / gfileattribute.c
blob2b298e268392c2b3d4b08ca08fad6b6674e581de
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "config.h"
25 #include <string.h>
27 #include "gfileattribute.h"
28 #include "gfileattribute-priv.h"
29 #include <glib-object.h>
30 #include "glibintl.h"
33 /**
34 * SECTION:gfileattribute
35 * @short_description: Key-Value Paired File Attributes
36 * @include: gio/gio.h
37 * @see_also: #GFile, #GFileInfo
39 * File attributes in GIO consist of a list of key-value pairs.
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.
47 * The list of possible attributes for a filesystem (pointed to by a #GFile) is
48 * available as a #GFileAttributeInfoList. This list is queryable by key names
49 * as indicated earlier.
51 * Information is stored within the list in #GFileAttributeInfo structures.
52 * The info structure can store different types, listed in the enum
53 * #GFileAttributeType. Upon creation of a #GFileAttributeInfo, the type will
54 * be set to %G_FILE_ATTRIBUTE_TYPE_INVALID.
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).
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. Examples 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>
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.
130 * <!-- TODO: Implementation note about using extended attributes on supported
131 * file systems -->
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_USED</entry><entry>filesystem::used</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>
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>.
215 * _g_file_attribute_value_free:
216 * @attr: a #GFileAttributeValue.
218 * Frees the memory used by @attr.
221 void
222 _g_file_attribute_value_free (GFileAttributeValue *attr)
224 g_return_if_fail (attr != NULL);
226 _g_file_attribute_value_clear (attr);
227 g_free (attr);
231 * _g_file_attribute_value_clear:
232 * @attr: a #GFileAttributeValue.
234 * Clears the value of @attr and sets its type to
235 * %G_FILE_ATTRIBUTE_TYPE_INVALID.
238 void
239 _g_file_attribute_value_clear (GFileAttributeValue *attr)
241 g_return_if_fail (attr != NULL);
243 if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
244 attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
245 g_free (attr->u.string);
247 if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV)
248 g_strfreev (attr->u.stringv);
250 if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
251 attr->u.obj != NULL)
252 g_object_unref (attr->u.obj);
254 attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
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.
262 * Sets an attribute's value from another attribute.
264 void
265 _g_file_attribute_value_set (GFileAttributeValue *attr,
266 const GFileAttributeValue *new_value)
268 g_return_if_fail (attr != NULL);
269 g_return_if_fail (new_value != NULL);
271 _g_file_attribute_value_clear (attr);
272 *attr = *new_value;
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);
278 if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV)
279 attr->u.stringv = g_strdupv (attr->u.stringv);
281 if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
282 attr->u.obj != NULL)
283 g_object_ref (attr->u.obj);
287 * _g_file_attribute_value_new:
289 * Creates a new file attribute.
291 * Returns: a #GFileAttributeValue.
293 GFileAttributeValue *
294 _g_file_attribute_value_new (void)
296 GFileAttributeValue *attr;
298 attr = g_new (GFileAttributeValue, 1);
299 attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
300 return attr;
303 gpointer
304 _g_file_attribute_value_peek_as_pointer (GFileAttributeValue *attr)
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;
320 * g_file_attribute_value_dup:
321 * @other: a #GFileAttributeValue to duplicate.
323 * Duplicates a file attribute.
325 * Returns: a duplicate of the @other.
327 GFileAttributeValue *
328 _g_file_attribute_value_dup (const GFileAttributeValue *other)
330 GFileAttributeValue *attr;
332 g_return_val_if_fail (other != NULL, NULL);
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;
340 G_DEFINE_BOXED_TYPE (GFileAttributeInfoList, g_file_attribute_info_list,
341 g_file_attribute_info_list_dup,
342 g_file_attribute_info_list_unref)
344 static gboolean
345 valid_char (char c)
347 return c >= 32 && c <= 126 && c != '\\';
350 static char *
351 escape_byte_string (const char *str)
353 size_t len;
354 int num_invalid, i;
355 char *escaped_val, *p;
356 unsigned char c;
357 const char hex_digits[] = "0123456789abcdef";
359 len = strlen (str);
361 num_invalid = 0;
362 for (i = 0; i < len; i++)
364 if (!valid_char (str[i]))
365 num_invalid++;
368 if (num_invalid == 0)
369 return g_strdup (str);
370 else
372 escaped_val = g_malloc (len + num_invalid*3 + 1);
374 p = escaped_val;
375 for (i = 0; i < len; i++)
377 c = str[i];
378 if (valid_char (c))
379 *p++ = c;
380 else
382 *p++ = '\\';
383 *p++ = 'x';
384 *p++ = hex_digits[(c >> 4) & 0xf];
385 *p++ = hex_digits[c & 0xf];
388 *p++ = 0;
389 return escaped_val;
394 * _g_file_attribute_value_as_string:
395 * @attr: a #GFileAttributeValue.
397 * Converts a #GFileAttributeValue to a string for display.
398 * The returned string should be freed when no longer needed.
400 * Returns: a string from the @attr, %NULL on error, or "&lt;invalid&gt;"
401 * if @attr is of type %G_FILE_ATTRIBUTE_TYPE_INVALID.
403 char *
404 _g_file_attribute_value_as_string (const GFileAttributeValue *attr)
406 GString *s;
407 int i;
408 char *str;
410 g_return_val_if_fail (attr != NULL, NULL);
412 switch (attr->type)
414 case G_FILE_ATTRIBUTE_TYPE_STRING:
415 str = g_strdup (attr->u.string);
416 break;
417 case G_FILE_ATTRIBUTE_TYPE_STRINGV:
418 s = g_string_new ("[");
419 for (i = 0; attr->u.stringv[i] != NULL; i++)
421 g_string_append (s, attr->u.stringv[i]);
422 if (attr->u.stringv[i+1] != NULL)
423 g_string_append (s, ", ");
425 g_string_append (s, "]");
426 str = g_string_free (s, FALSE);
427 break;
428 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
429 str = escape_byte_string (attr->u.string);
430 break;
431 case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
432 str = g_strdup_printf ("%s", attr->u.boolean?"TRUE":"FALSE");
433 break;
434 case G_FILE_ATTRIBUTE_TYPE_UINT32:
435 str = g_strdup_printf ("%u", (unsigned int)attr->u.uint32);
436 break;
437 case G_FILE_ATTRIBUTE_TYPE_INT32:
438 str = g_strdup_printf ("%i", (int)attr->u.int32);
439 break;
440 case G_FILE_ATTRIBUTE_TYPE_UINT64:
441 str = g_strdup_printf ("%"G_GUINT64_FORMAT, attr->u.uint64);
442 break;
443 case G_FILE_ATTRIBUTE_TYPE_INT64:
444 str = g_strdup_printf ("%"G_GINT64_FORMAT, attr->u.int64);
445 break;
446 case G_FILE_ATTRIBUTE_TYPE_OBJECT:
447 str = g_strdup_printf ("%s:%p", g_type_name_from_instance
448 ((GTypeInstance *) attr->u.obj),
449 attr->u.obj);
450 break;
451 case G_FILE_ATTRIBUTE_TYPE_INVALID:
452 str = g_strdup ("<unset>");
453 break;
454 default:
455 g_warning ("Invalid type in GFileInfo attribute");
456 str = g_strdup ("<invalid>");
457 break;
460 return str;
464 * _g_file_attribute_value_get_string:
465 * @attr: a #GFileAttributeValue.
467 * Gets the string from a file attribute value. If the value is not the
468 * right type then %NULL will be returned.
470 * Returns: the UTF-8 string value contained within the attribute, or %NULL.
472 const char *
473 _g_file_attribute_value_get_string (const GFileAttributeValue *attr)
475 if (attr == NULL)
476 return NULL;
478 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING, NULL);
480 return attr->u.string;
484 * _g_file_attribute_value_get_byte_string:
485 * @attr: a #GFileAttributeValue.
487 * Gets the byte string from a file attribute value. If the value is not the
488 * right type then %NULL will be returned.
490 * Returns: the byte string contained within the attribute or %NULL.
492 const char *
493 _g_file_attribute_value_get_byte_string (const GFileAttributeValue *attr)
495 if (attr == NULL)
496 return NULL;
498 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, NULL);
500 return attr->u.string;
503 char **
504 _g_file_attribute_value_get_stringv (const GFileAttributeValue *attr)
506 if (attr == NULL)
507 return NULL;
509 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV, NULL);
511 return attr->u.stringv;
515 * _g_file_attribute_value_get_boolean:
516 * @attr: a #GFileAttributeValue.
518 * Gets the boolean value from a file attribute value. If the value is not the
519 * right type then %FALSE will be returned.
521 * Returns: the boolean value contained within the attribute, or %FALSE.
523 gboolean
524 _g_file_attribute_value_get_boolean (const GFileAttributeValue *attr)
526 if (attr == NULL)
527 return FALSE;
529 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BOOLEAN, FALSE);
531 return attr->u.boolean;
535 * _g_file_attribute_value_get_uint32:
536 * @attr: a #GFileAttributeValue.
538 * Gets the unsigned 32-bit integer from a file attribute value. If the value
539 * is not the right type then 0 will be returned.
541 * Returns: the unsigned 32-bit integer from the attribute, or 0.
543 guint32
544 _g_file_attribute_value_get_uint32 (const GFileAttributeValue *attr)
546 if (attr == NULL)
547 return 0;
549 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT32, 0);
551 return attr->u.uint32;
555 * _g_file_attribute_value_get_int32:
556 * @attr: a #GFileAttributeValue.
558 * Gets the signed 32-bit integer from a file attribute value. If the value
559 * is not the right type then 0 will be returned.
561 * Returns: the signed 32-bit integer from the attribute, or 0.
563 gint32
564 _g_file_attribute_value_get_int32 (const GFileAttributeValue *attr)
566 if (attr == NULL)
567 return 0;
569 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT32, 0);
571 return attr->u.int32;
575 * _g_file_attribute_value_get_uint64:
576 * @attr: a #GFileAttributeValue.
578 * Gets the unsigned 64-bit integer from a file attribute value. If the value
579 * is not the right type then 0 will be returned.
581 * Returns: the unsigned 64-bit integer from the attribute, or 0.
583 guint64
584 _g_file_attribute_value_get_uint64 (const GFileAttributeValue *attr)
586 if (attr == NULL)
587 return 0;
589 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT64, 0);
591 return attr->u.uint64;
595 * _g_file_attribute_value_get_int64:
596 * @attr: a #GFileAttributeValue.
598 * Gets the signed 64-bit integer from a file attribute value. If the value
599 * is not the right type then 0 will be returned.
601 * Returns: the signed 64-bit integer from the attribute, or 0.
603 gint64
604 _g_file_attribute_value_get_int64 (const GFileAttributeValue *attr)
606 if (attr == NULL)
607 return 0;
609 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT64, 0);
611 return attr->u.int64;
615 * _g_file_attribute_value_get_object:
616 * @attr: a #GFileAttributeValue.
618 * Gets the GObject from a file attribute value. If the value
619 * is not the right type then %NULL will be returned.
621 * Returns: the GObject from the attribute, or %NULL.
623 GObject *
624 _g_file_attribute_value_get_object (const GFileAttributeValue *attr)
626 if (attr == NULL)
627 return NULL;
629 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT, NULL);
631 return attr->u.obj;
635 void
636 _g_file_attribute_value_set_from_pointer (GFileAttributeValue *value,
637 GFileAttributeType type,
638 gpointer value_p,
639 gboolean dup)
641 _g_file_attribute_value_clear (value);
642 value->type = type;
643 switch (type)
645 case G_FILE_ATTRIBUTE_TYPE_STRING:
646 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
647 if (dup)
648 value->u.string = g_strdup (value_p);
649 else
650 value->u.string = value_p;
651 break;
653 case G_FILE_ATTRIBUTE_TYPE_STRINGV:
654 if (dup)
655 value->u.stringv = g_strdupv (value_p);
656 else
657 value->u.stringv = value_p;
658 break;
660 case G_FILE_ATTRIBUTE_TYPE_OBJECT:
661 if (dup)
662 value->u.obj = g_object_ref (value_p);
663 else
664 value->u.obj = value_p;
665 break;
667 case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
668 value->u.boolean = *(gboolean *)value_p;
669 break;
671 case G_FILE_ATTRIBUTE_TYPE_UINT32:
672 value->u.uint32 = *(guint32 *)value_p;
673 break;
675 case G_FILE_ATTRIBUTE_TYPE_INT32:
676 value->u.int32 = *(gint32 *)value_p;
677 break;
679 case G_FILE_ATTRIBUTE_TYPE_UINT64:
680 value->u.uint64 = *(guint64 *)value_p;
681 break;
683 case G_FILE_ATTRIBUTE_TYPE_INT64:
684 value->u.int64 = *(gint64 *)value_p;
685 break;
687 case G_FILE_ATTRIBUTE_TYPE_INVALID:
688 break;
690 default:
691 g_warning ("Unknown type specified in g_file_info_set_attribute\n");
692 break;
697 * _g_file_attribute_value_set_string:
698 * @attr: a #GFileAttributeValue.
699 * @string: a UTF-8 string to set within the type.
701 * Sets the attribute value to a given UTF-8 string.
703 void
704 _g_file_attribute_value_set_string (GFileAttributeValue *attr,
705 const char *string)
707 g_return_if_fail (attr != NULL);
708 g_return_if_fail (string != NULL);
710 _g_file_attribute_value_clear (attr);
711 attr->type = G_FILE_ATTRIBUTE_TYPE_STRING;
712 attr->u.string = g_strdup (string);
716 * _g_file_attribute_value_set_byte_string:
717 * @attr: a #GFileAttributeValue.
718 * @string: a byte string to set within the type.
720 * Sets the attribute value to a given byte string.
722 void
723 _g_file_attribute_value_set_byte_string (GFileAttributeValue *attr,
724 const char *string)
726 g_return_if_fail (attr != NULL);
727 g_return_if_fail (string != NULL);
729 _g_file_attribute_value_clear (attr);
730 attr->type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
731 attr->u.string = g_strdup (string);
734 void
735 _g_file_attribute_value_set_stringv (GFileAttributeValue *attr,
736 char **value)
738 g_return_if_fail (attr != NULL);
739 g_return_if_fail (value != NULL);
741 _g_file_attribute_value_clear (attr);
742 attr->type = G_FILE_ATTRIBUTE_TYPE_STRINGV;
743 attr->u.stringv = g_strdupv (value);
748 * _g_file_attribute_value_set_boolean:
749 * @attr: a #GFileAttributeValue.
750 * @value: a #gboolean to set within the type.
752 * Sets the attribute value to the given boolean value.
754 void
755 _g_file_attribute_value_set_boolean (GFileAttributeValue *attr,
756 gboolean value)
758 g_return_if_fail (attr != NULL);
760 _g_file_attribute_value_clear (attr);
761 attr->type = G_FILE_ATTRIBUTE_TYPE_BOOLEAN;
762 attr->u.boolean = !!value;
766 * _g_file_attribute_value_set_uint32:
767 * @attr: a #GFileAttributeValue.
768 * @value: a #guint32 to set within the type.
770 * Sets the attribute value to the given unsigned 32-bit integer.
772 void
773 _g_file_attribute_value_set_uint32 (GFileAttributeValue *attr,
774 guint32 value)
776 g_return_if_fail (attr != NULL);
778 _g_file_attribute_value_clear (attr);
779 attr->type = G_FILE_ATTRIBUTE_TYPE_UINT32;
780 attr->u.uint32 = value;
784 * _g_file_attribute_value_set_int32:
785 * @attr: a #GFileAttributeValue.
786 * @value: a #gint32 to set within the type.
788 * Sets the attribute value to the given signed 32-bit integer.
790 void
791 _g_file_attribute_value_set_int32 (GFileAttributeValue *attr,
792 gint32 value)
794 g_return_if_fail (attr != NULL);
796 _g_file_attribute_value_clear (attr);
797 attr->type = G_FILE_ATTRIBUTE_TYPE_INT32;
798 attr->u.int32 = value;
802 * _g_file_attribute_value_set_uint64:
803 * @attr: a #GFileAttributeValue.
804 * @value: a #guint64 to set within the type.
806 * Sets the attribute value to a given unsigned 64-bit integer.
808 void
809 _g_file_attribute_value_set_uint64 (GFileAttributeValue *attr,
810 guint64 value)
812 g_return_if_fail (attr != NULL);
814 _g_file_attribute_value_clear (attr);
815 attr->type = G_FILE_ATTRIBUTE_TYPE_UINT64;
816 attr->u.uint64 = value;
820 * _g_file_attribute_value_set_int64:
821 * @attr: a #GFileAttributeValue.
822 * @value: a #gint64 to set within the type.
824 * Sets the attribute value to a given signed 64-bit integer.
826 void
827 _g_file_attribute_value_set_int64 (GFileAttributeValue *attr,
828 gint64 value)
830 g_return_if_fail (attr != NULL);
832 _g_file_attribute_value_clear (attr);
833 attr->type = G_FILE_ATTRIBUTE_TYPE_INT64;
834 attr->u.int64 = value;
838 * _g_file_attribute_value_set_object:
839 * @attr: a #GFileAttributeValue.
840 * @obj: a #GObject.
842 * Sets the attribute to contain the value @obj.
843 * The @attr references the GObject internally.
845 void
846 _g_file_attribute_value_set_object (GFileAttributeValue *attr,
847 GObject *obj)
849 g_return_if_fail (attr != NULL);
850 g_return_if_fail (obj != NULL);
852 _g_file_attribute_value_clear (attr);
853 attr->type = G_FILE_ATTRIBUTE_TYPE_OBJECT;
854 attr->u.obj = g_object_ref (obj);
857 typedef struct {
858 GFileAttributeInfoList public;
859 GArray *array;
860 int ref_count;
861 } GFileAttributeInfoListPriv;
863 static void
864 list_update_public (GFileAttributeInfoListPriv *priv)
866 priv->public.infos = (GFileAttributeInfo *)priv->array->data;
867 priv->public.n_infos = priv->array->len;
871 * g_file_attribute_info_list_new:
873 * Creates a new file attribute info list.
875 * Returns: a #GFileAttributeInfoList.
877 GFileAttributeInfoList *
878 g_file_attribute_info_list_new (void)
880 GFileAttributeInfoListPriv *priv;
882 priv = g_new0 (GFileAttributeInfoListPriv, 1);
884 priv->ref_count = 1;
885 priv->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
887 list_update_public (priv);
889 return (GFileAttributeInfoList *)priv;
893 * g_file_attribute_info_list_dup:
894 * @list: a #GFileAttributeInfoList to duplicate.
896 * Makes a duplicate of a file attribute info list.
898 * Returns: a copy of the given @list.
900 GFileAttributeInfoList *
901 g_file_attribute_info_list_dup (GFileAttributeInfoList *list)
903 GFileAttributeInfoListPriv *new;
904 int i;
906 g_return_val_if_fail (list != NULL, NULL);
908 new = g_new0 (GFileAttributeInfoListPriv, 1);
909 new->ref_count = 1;
910 new->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
912 g_array_set_size (new->array, list->n_infos);
913 list_update_public (new);
914 for (i = 0; i < list->n_infos; i++)
916 new->public.infos[i].name = g_strdup (list->infos[i].name);
917 new->public.infos[i].type = list->infos[i].type;
918 new->public.infos[i].flags = list->infos[i].flags;
921 return (GFileAttributeInfoList *)new;
925 * g_file_attribute_info_list_ref:
926 * @list: a #GFileAttributeInfoList to reference.
928 * References a file attribute info list.
930 * Returns: #GFileAttributeInfoList or %NULL on error.
932 GFileAttributeInfoList *
933 g_file_attribute_info_list_ref (GFileAttributeInfoList *list)
935 GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
937 g_return_val_if_fail (list != NULL, NULL);
938 g_return_val_if_fail (priv->ref_count > 0, NULL);
940 g_atomic_int_inc (&priv->ref_count);
942 return list;
946 * g_file_attribute_info_list_unref:
947 * @list: The #GFileAttributeInfoList to unreference.
949 * Removes a reference from the given @list. If the reference count
950 * falls to zero, the @list is deleted.
952 void
953 g_file_attribute_info_list_unref (GFileAttributeInfoList *list)
955 GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
956 int i;
958 g_return_if_fail (list != NULL);
959 g_return_if_fail (priv->ref_count > 0);
961 if (g_atomic_int_dec_and_test (&priv->ref_count))
963 for (i = 0; i < list->n_infos; i++)
964 g_free (list->infos[i].name);
965 g_array_free (priv->array, TRUE);
969 static int
970 g_file_attribute_info_list_bsearch (GFileAttributeInfoList *list,
971 const char *name)
973 int start, end, mid;
975 start = 0;
976 end = list->n_infos;
978 while (start != end)
980 mid = start + (end - start) / 2;
982 if (strcmp (name, list->infos[mid].name) < 0)
983 end = mid;
984 else if (strcmp (name, list->infos[mid].name) > 0)
985 start = mid + 1;
986 else
987 return mid;
989 return start;
993 * g_file_attribute_info_list_lookup:
994 * @list: a #GFileAttributeInfoList.
995 * @name: the name of the attribute to lookup.
997 * Gets the file attribute with the name @name from @list.
999 * Returns: a #GFileAttributeInfo for the @name, or %NULL if an
1000 * attribute isn't found.
1002 const GFileAttributeInfo *
1003 g_file_attribute_info_list_lookup (GFileAttributeInfoList *list,
1004 const char *name)
1006 int i;
1008 g_return_val_if_fail (list != NULL, NULL);
1009 g_return_val_if_fail (name != NULL, NULL);
1011 i = g_file_attribute_info_list_bsearch (list, name);
1013 if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
1014 return &list->infos[i];
1016 return NULL;
1020 * g_file_attribute_info_list_add:
1021 * @list: a #GFileAttributeInfoList.
1022 * @name: the name of the attribute to add.
1023 * @type: the #GFileAttributeType for the attribute.
1024 * @flags: #GFileAttributeInfoFlags for the attribute.
1026 * Adds a new attribute with @name to the @list, setting
1027 * its @type and @flags.
1029 void
1030 g_file_attribute_info_list_add (GFileAttributeInfoList *list,
1031 const char *name,
1032 GFileAttributeType type,
1033 GFileAttributeInfoFlags flags)
1035 GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
1036 GFileAttributeInfo info;
1037 int i;
1039 g_return_if_fail (list != NULL);
1040 g_return_if_fail (name != NULL);
1042 i = g_file_attribute_info_list_bsearch (list, name);
1044 if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
1046 list->infos[i].type = type;
1047 return;
1050 info.name = g_strdup (name);
1051 info.type = type;
1052 info.flags = flags;
1053 g_array_insert_vals (priv->array, i, &info, 1);
1055 list_update_public (priv);