GSettings: small internal refactor
[glib.git] / gio / gfileattribute.c
blob44a512822409f0cf91f699ef454d6bf8d15d725b
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, "failed"
102 * to check if thumbnailing of the file failed, and "is-valid" to check if the thumbnail is
103 * outdated.</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>
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.
131 * <!-- TODO: Implementation note about using extended attributes on supported
132 * file systems -->
134 * <para><table>
135 * <title>GFileAttributes Built-in Keys and Value Types</title>
136 * <tgroup cols='3' align='left'><thead>
137 * <row><entry>Enum Value</entry><entry>Namespace::Key</entry><entry>Value Type</entry></row>
138 * </thead><tbody>
139 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_TYPE</entry><entry>standard::type</entry><entry>uint32 (#GFileType)</entry></row>
140 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN</entry><entry>standard::is-hidden</entry><entry>boolean</entry></row>
141 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP</entry><entry>standard::is-backup</entry><entry>boolean</entry></row>
142 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK</entry><entry>standard::is-symlink</entry><entry>boolean</entry></row>
143 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_IS_VIRTUAL</entry><entry>standard::is-virtual</entry><entry>boolean</entry></row>
144 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_NAME</entry><entry>standard::name</entry><entry>byte string</entry></row>
145 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME</entry><entry>standard::display-name</entry><entry>string</entry></row>
146 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME</entry><entry>standard::edit-name</entry><entry>string</entry></row>
147 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_ICON</entry><entry>standard::icon</entry><entry>object (#GIcon)</entry></row>
148 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE</entry><entry>standard::content-type</entry><entry>string</entry></row>
149 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE</entry><entry>standard::fast-content-type</entry><entry>string</entry></row>
150 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_SIZE</entry><entry>standard::size</entry><entry>uint64</entry></row>
151 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_ALLOCATED_SIZE</entry><entry>standard::allocated-size</entry><entry>uint64</entry></row>
152 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET</entry><entry>standard::symlink-target</entry><entry>byte string</entry></row>
153 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_TARGET_URI</entry><entry>standard::target-uri</entry><entry>string</entry></row>
154 * <row><entry>%G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER</entry><entry>standard::sort-order</entry><entry>int32</entry></row>
155 * <row><entry>%G_FILE_ATTRIBUTE_ETAG_VALUE</entry><entry>etag::value</entry><entry>string</entry></row>
156 * <row><entry>%G_FILE_ATTRIBUTE_ID_FILE</entry><entry>id::file</entry><entry>string</entry></row>
157 * <row><entry>%G_FILE_ATTRIBUTE_ID_FILESYSTEM</entry><entry>id::filesystem</entry><entry>string</entry></row>
158 * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_READ</entry><entry>access::can-read</entry><entry>boolean</entry></row>
159 * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE</entry><entry>access::can-write</entry><entry>boolean</entry></row>
160 * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE</entry><entry>access::can-execute</entry><entry>boolean</entry></row>
161 * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE</entry><entry>access::can-delete</entry><entry>boolean</entry></row>
162 * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH</entry><entry>access::can-trash</entry><entry>boolean</entry></row>
163 * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME</entry><entry>access::can-rename</entry><entry>boolean</entry></row>
164 * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_CAN_MOUNT</entry><entry>mountable::can-mount</entry><entry>boolean</entry></row>
165 * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_CAN_UNMOUNT</entry><entry>mountable::can-unmount</entry><entry>boolean</entry></row>
166 * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_CAN_EJECT</entry><entry>mountable::can-eject</entry><entry>boolean</entry></row>
167 * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_UNIX_DEVICE</entry><entry>mountable::unix-device</entry><entry>uint32</entry></row>
168 * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_UNIX_DEVICE_FILE</entry><entry>mountable::unix-device-file</entry><entry>string</entry></row>
169 * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_HAL_UDI</entry><entry>mountable::hal-udi</entry><entry>string</entry></row>
170 * <row><entry>%G_FILE_ATTRIBUTE_TIME_MODIFIED</entry><entry>time::modified</entry><entry>uint64</entry></row>
171 * <row><entry>%G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC</entry><entry>time::modified-usec</entry><entry>uint32</entry></row>
172 * <row><entry>%G_FILE_ATTRIBUTE_TIME_ACCESS</entry><entry>time::access</entry><entry>uint64</entry></row>
173 * <row><entry>%G_FILE_ATTRIBUTE_TIME_ACCESS_USEC</entry><entry>time::access-usec</entry><entry>uint32</entry></row>
174 * <row><entry>%G_FILE_ATTRIBUTE_TIME_CHANGED</entry><entry>time::changed</entry><entry>uint64</entry></row>
175 * <row><entry>%G_FILE_ATTRIBUTE_TIME_CHANGED_USEC</entry><entry>time::changed-usec</entry><entry>uint32</entry></row>
176 * <row><entry>%G_FILE_ATTRIBUTE_TIME_CREATED</entry><entry>time::created</entry><entry>uint64</entry></row>
177 * <row><entry>%G_FILE_ATTRIBUTE_TIME_CREATED_USEC</entry><entry>time::created-usec</entry><entry>uint32</entry></row>
178 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_DEVICE</entry><entry>unix::device</entry><entry>uint32</entry></row>
179 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_INODE</entry><entry>unix::inode</entry><entry>uint64</entry></row>
180 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_MODE</entry><entry>unix::mode</entry><entry>uint32</entry></row>
181 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_NLINK</entry><entry>unix::nlink</entry><entry>uint32</entry></row>
182 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_UID</entry><entry>unix::uid</entry><entry>uint32</entry></row>
183 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_GID</entry><entry>unix::gid</entry><entry>uint32</entry></row>
184 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_RDEV</entry><entry>unix::rdev</entry><entry>uint32</entry></row>
185 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE</entry><entry>unix::block-size</entry><entry>uint32</entry></row>
186 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_BLOCKS</entry><entry>unix::blocks</entry><entry>uint64</entry></row>
187 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT</entry><entry>unix::is-mountpoint</entry><entry>boolean</entry></row>
188 * <row><entry>%G_FILE_ATTRIBUTE_DOS_IS_ARCHIVE</entry><entry>dos::is-archive</entry><entry>boolean</entry></row>
189 * <row><entry>%G_FILE_ATTRIBUTE_DOS_IS_SYSTEM</entry><entry>dos::is-system</entry><entry>boolean</entry></row>
190 * <row><entry>%G_FILE_ATTRIBUTE_OWNER_USER</entry><entry>owner::user</entry><entry>string</entry></row>
191 * <row><entry>%G_FILE_ATTRIBUTE_OWNER_USER_REAL</entry><entry>owner::user-real</entry><entry>string</entry></row>
192 * <row><entry>%G_FILE_ATTRIBUTE_OWNER_GROUP</entry><entry>owner::group</entry><entry>string</entry></row>
193 * <row><entry>%G_FILE_ATTRIBUTE_THUMBNAIL_PATH</entry><entry>thumbnail::path</entry><entry>bytestring</entry></row>
194 * <row><entry>%G_FILE_ATTRIBUTE_THUMBNAILING_FAILED</entry><entry>thumbnail::failed</entry><entry>boolean</entry></row>
195 * <row><entry>%G_FILE_ATTRIBUTE_THUMBNAIL_IS_VALID</entry><entry>thumbnail::is-valid</entry><entry>boolean</entry></row>
196 * <row><entry>%G_FILE_ATTRIBUTE_PREVIEW_ICON</entry><entry>preview::icon</entry><entry>object (#GIcon)</entry></row>
197 * <row><entry>%G_FILE_ATTRIBUTE_FILESYSTEM_SIZE</entry><entry>filesystem::size</entry><entry>uint64</entry></row>
198 * <row><entry>%G_FILE_ATTRIBUTE_FILESYSTEM_FREE</entry><entry>filesystem::free</entry><entry>uint64</entry></row>
199 * <row><entry>%G_FILE_ATTRIBUTE_FILESYSTEM_USED</entry><entry>filesystem::used</entry><entry>uint64</entry></row>
200 * <row><entry>%G_FILE_ATTRIBUTE_FILESYSTEM_TYPE</entry><entry>filesystem::type</entry><entry>string</entry></row>
201 * <row><entry>%G_FILE_ATTRIBUTE_FILESYSTEM_READONLY</entry><entry>filesystem::readonly</entry><entry>boolean</entry></row>
202 * <row><entry>%G_FILE_ATTRIBUTE_GVFS_BACKEND</entry><entry>gvfs::backend</entry><entry>string</entry></row>
203 * <row><entry>%G_FILE_ATTRIBUTE_SELINUX_CONTEXT</entry><entry>selinux::context</entry><entry>string</entry></row>
204 * </tbody></tgroup></table></para>
206 * Note that there are no predefined keys in the "xattr" and "xattr-sys"
207 * namespaces. Keys for the "xattr" namespace are constructed by stripping
208 * away the "user." prefix from the extended user attribute, and prepending
209 * "xattr::". Keys for the "xattr-sys" namespace are constructed by
210 * concatenating "xattr-sys::" with the extended attribute name. All extended
211 * attribute values are returned as hex-encoded strings in which bytes outside
212 * the ASCII range are encoded as hexadecimal escape sequences of the form
213 * \x<replaceable>nn</replaceable>.
217 * _g_file_attribute_value_free:
218 * @attr: a #GFileAttributeValue.
220 * Frees the memory used by @attr.
223 void
224 _g_file_attribute_value_free (GFileAttributeValue *attr)
226 g_return_if_fail (attr != NULL);
228 _g_file_attribute_value_clear (attr);
229 g_free (attr);
233 * _g_file_attribute_value_clear:
234 * @attr: a #GFileAttributeValue.
236 * Clears the value of @attr and sets its type to
237 * %G_FILE_ATTRIBUTE_TYPE_INVALID.
240 void
241 _g_file_attribute_value_clear (GFileAttributeValue *attr)
243 g_return_if_fail (attr != NULL);
245 if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
246 attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
247 g_free (attr->u.string);
249 if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV)
250 g_strfreev (attr->u.stringv);
252 if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
253 attr->u.obj != NULL)
254 g_object_unref (attr->u.obj);
256 attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
260 * g_file_attribute_value_set:
261 * @attr: a #GFileAttributeValue to set the value in.
262 * @new_value: a #GFileAttributeValue to get the value from.
264 * Sets an attribute's value from another attribute.
266 void
267 _g_file_attribute_value_set (GFileAttributeValue *attr,
268 const GFileAttributeValue *new_value)
270 g_return_if_fail (attr != NULL);
271 g_return_if_fail (new_value != NULL);
273 _g_file_attribute_value_clear (attr);
274 *attr = *new_value;
276 if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
277 attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
278 attr->u.string = g_strdup (attr->u.string);
280 if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV)
281 attr->u.stringv = g_strdupv (attr->u.stringv);
283 if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
284 attr->u.obj != NULL)
285 g_object_ref (attr->u.obj);
289 * _g_file_attribute_value_new:
291 * Creates a new file attribute.
293 * Returns: a #GFileAttributeValue.
295 GFileAttributeValue *
296 _g_file_attribute_value_new (void)
298 GFileAttributeValue *attr;
300 attr = g_new (GFileAttributeValue, 1);
301 attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
302 return attr;
305 gpointer
306 _g_file_attribute_value_peek_as_pointer (GFileAttributeValue *attr)
308 switch (attr->type) {
309 case G_FILE_ATTRIBUTE_TYPE_STRING:
310 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
311 return attr->u.string;
312 case G_FILE_ATTRIBUTE_TYPE_STRINGV:
313 return attr->u.stringv;
314 case G_FILE_ATTRIBUTE_TYPE_OBJECT:
315 return attr->u.obj;
316 default:
317 return (gpointer) &attr->u;
322 * g_file_attribute_value_dup:
323 * @other: a #GFileAttributeValue to duplicate.
325 * Duplicates a file attribute.
327 * Returns: a duplicate of the @other.
329 GFileAttributeValue *
330 _g_file_attribute_value_dup (const GFileAttributeValue *other)
332 GFileAttributeValue *attr;
334 g_return_val_if_fail (other != NULL, NULL);
336 attr = g_new (GFileAttributeValue, 1);
337 attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
338 _g_file_attribute_value_set (attr, other);
339 return attr;
342 G_DEFINE_BOXED_TYPE (GFileAttributeInfoList, g_file_attribute_info_list,
343 g_file_attribute_info_list_dup,
344 g_file_attribute_info_list_unref)
346 static gboolean
347 valid_char (char c)
349 return c >= 32 && c <= 126 && c != '\\';
352 static char *
353 escape_byte_string (const char *str)
355 size_t len;
356 int num_invalid, i;
357 char *escaped_val, *p;
358 unsigned char c;
359 const char hex_digits[] = "0123456789abcdef";
361 len = strlen (str);
363 num_invalid = 0;
364 for (i = 0; i < len; i++)
366 if (!valid_char (str[i]))
367 num_invalid++;
370 if (num_invalid == 0)
371 return g_strdup (str);
372 else
374 escaped_val = g_malloc (len + num_invalid*3 + 1);
376 p = escaped_val;
377 for (i = 0; i < len; i++)
379 c = str[i];
380 if (valid_char (c))
381 *p++ = c;
382 else
384 *p++ = '\\';
385 *p++ = 'x';
386 *p++ = hex_digits[(c >> 4) & 0xf];
387 *p++ = hex_digits[c & 0xf];
390 *p++ = 0;
391 return escaped_val;
396 * _g_file_attribute_value_as_string:
397 * @attr: a #GFileAttributeValue.
399 * Converts a #GFileAttributeValue to a string for display.
400 * The returned string should be freed when no longer needed.
402 * Returns: a string from the @attr, %NULL on error, or "&lt;invalid&gt;"
403 * if @attr is of type %G_FILE_ATTRIBUTE_TYPE_INVALID.
405 char *
406 _g_file_attribute_value_as_string (const GFileAttributeValue *attr)
408 GString *s;
409 int i;
410 char *str;
412 g_return_val_if_fail (attr != NULL, NULL);
414 switch (attr->type)
416 case G_FILE_ATTRIBUTE_TYPE_STRING:
417 str = g_strdup (attr->u.string);
418 break;
419 case G_FILE_ATTRIBUTE_TYPE_STRINGV:
420 s = g_string_new ("[");
421 for (i = 0; attr->u.stringv[i] != NULL; i++)
423 g_string_append (s, attr->u.stringv[i]);
424 if (attr->u.stringv[i+1] != NULL)
425 g_string_append (s, ", ");
427 g_string_append (s, "]");
428 str = g_string_free (s, FALSE);
429 break;
430 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
431 str = escape_byte_string (attr->u.string);
432 break;
433 case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
434 str = g_strdup_printf ("%s", attr->u.boolean?"TRUE":"FALSE");
435 break;
436 case G_FILE_ATTRIBUTE_TYPE_UINT32:
437 str = g_strdup_printf ("%u", (unsigned int)attr->u.uint32);
438 break;
439 case G_FILE_ATTRIBUTE_TYPE_INT32:
440 str = g_strdup_printf ("%i", (int)attr->u.int32);
441 break;
442 case G_FILE_ATTRIBUTE_TYPE_UINT64:
443 str = g_strdup_printf ("%"G_GUINT64_FORMAT, attr->u.uint64);
444 break;
445 case G_FILE_ATTRIBUTE_TYPE_INT64:
446 str = g_strdup_printf ("%"G_GINT64_FORMAT, attr->u.int64);
447 break;
448 case G_FILE_ATTRIBUTE_TYPE_OBJECT:
449 str = g_strdup_printf ("%s:%p", g_type_name_from_instance
450 ((GTypeInstance *) attr->u.obj),
451 attr->u.obj);
452 break;
453 case G_FILE_ATTRIBUTE_TYPE_INVALID:
454 str = g_strdup ("<unset>");
455 break;
456 default:
457 g_warning ("Invalid type in GFileInfo attribute");
458 str = g_strdup ("<invalid>");
459 break;
462 return str;
466 * _g_file_attribute_value_get_string:
467 * @attr: a #GFileAttributeValue.
469 * Gets the string from a file attribute value. If the value is not the
470 * right type then %NULL will be returned.
472 * Returns: the UTF-8 string value contained within the attribute, or %NULL.
474 const char *
475 _g_file_attribute_value_get_string (const GFileAttributeValue *attr)
477 if (attr == NULL)
478 return NULL;
480 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING, NULL);
482 return attr->u.string;
486 * _g_file_attribute_value_get_byte_string:
487 * @attr: a #GFileAttributeValue.
489 * Gets the byte string from a file attribute value. If the value is not the
490 * right type then %NULL will be returned.
492 * Returns: the byte string contained within the attribute or %NULL.
494 const char *
495 _g_file_attribute_value_get_byte_string (const GFileAttributeValue *attr)
497 if (attr == NULL)
498 return NULL;
500 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, NULL);
502 return attr->u.string;
505 char **
506 _g_file_attribute_value_get_stringv (const GFileAttributeValue *attr)
508 if (attr == NULL)
509 return NULL;
511 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV, NULL);
513 return attr->u.stringv;
517 * _g_file_attribute_value_get_boolean:
518 * @attr: a #GFileAttributeValue.
520 * Gets the boolean value from a file attribute value. If the value is not the
521 * right type then %FALSE will be returned.
523 * Returns: the boolean value contained within the attribute, or %FALSE.
525 gboolean
526 _g_file_attribute_value_get_boolean (const GFileAttributeValue *attr)
528 if (attr == NULL)
529 return FALSE;
531 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BOOLEAN, FALSE);
533 return attr->u.boolean;
537 * _g_file_attribute_value_get_uint32:
538 * @attr: a #GFileAttributeValue.
540 * Gets the unsigned 32-bit integer from a file attribute value. If the value
541 * is not the right type then 0 will be returned.
543 * Returns: the unsigned 32-bit integer from the attribute, or 0.
545 guint32
546 _g_file_attribute_value_get_uint32 (const GFileAttributeValue *attr)
548 if (attr == NULL)
549 return 0;
551 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT32, 0);
553 return attr->u.uint32;
557 * _g_file_attribute_value_get_int32:
558 * @attr: a #GFileAttributeValue.
560 * Gets the signed 32-bit integer from a file attribute value. If the value
561 * is not the right type then 0 will be returned.
563 * Returns: the signed 32-bit integer from the attribute, or 0.
565 gint32
566 _g_file_attribute_value_get_int32 (const GFileAttributeValue *attr)
568 if (attr == NULL)
569 return 0;
571 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT32, 0);
573 return attr->u.int32;
577 * _g_file_attribute_value_get_uint64:
578 * @attr: a #GFileAttributeValue.
580 * Gets the unsigned 64-bit integer from a file attribute value. If the value
581 * is not the right type then 0 will be returned.
583 * Returns: the unsigned 64-bit integer from the attribute, or 0.
585 guint64
586 _g_file_attribute_value_get_uint64 (const GFileAttributeValue *attr)
588 if (attr == NULL)
589 return 0;
591 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT64, 0);
593 return attr->u.uint64;
597 * _g_file_attribute_value_get_int64:
598 * @attr: a #GFileAttributeValue.
600 * Gets the signed 64-bit integer from a file attribute value. If the value
601 * is not the right type then 0 will be returned.
603 * Returns: the signed 64-bit integer from the attribute, or 0.
605 gint64
606 _g_file_attribute_value_get_int64 (const GFileAttributeValue *attr)
608 if (attr == NULL)
609 return 0;
611 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT64, 0);
613 return attr->u.int64;
617 * _g_file_attribute_value_get_object:
618 * @attr: a #GFileAttributeValue.
620 * Gets the GObject from a file attribute value. If the value
621 * is not the right type then %NULL will be returned.
623 * Returns: the GObject from the attribute, or %NULL.
625 GObject *
626 _g_file_attribute_value_get_object (const GFileAttributeValue *attr)
628 if (attr == NULL)
629 return NULL;
631 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT, NULL);
633 return attr->u.obj;
637 void
638 _g_file_attribute_value_set_from_pointer (GFileAttributeValue *value,
639 GFileAttributeType type,
640 gpointer value_p,
641 gboolean dup)
643 _g_file_attribute_value_clear (value);
644 value->type = type;
645 switch (type)
647 case G_FILE_ATTRIBUTE_TYPE_STRING:
648 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
649 if (dup)
650 value->u.string = g_strdup (value_p);
651 else
652 value->u.string = value_p;
653 break;
655 case G_FILE_ATTRIBUTE_TYPE_STRINGV:
656 if (dup)
657 value->u.stringv = g_strdupv (value_p);
658 else
659 value->u.stringv = value_p;
660 break;
662 case G_FILE_ATTRIBUTE_TYPE_OBJECT:
663 if (dup)
664 value->u.obj = g_object_ref (value_p);
665 else
666 value->u.obj = value_p;
667 break;
669 case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
670 value->u.boolean = *(gboolean *)value_p;
671 break;
673 case G_FILE_ATTRIBUTE_TYPE_UINT32:
674 value->u.uint32 = *(guint32 *)value_p;
675 break;
677 case G_FILE_ATTRIBUTE_TYPE_INT32:
678 value->u.int32 = *(gint32 *)value_p;
679 break;
681 case G_FILE_ATTRIBUTE_TYPE_UINT64:
682 value->u.uint64 = *(guint64 *)value_p;
683 break;
685 case G_FILE_ATTRIBUTE_TYPE_INT64:
686 value->u.int64 = *(gint64 *)value_p;
687 break;
689 case G_FILE_ATTRIBUTE_TYPE_INVALID:
690 break;
692 default:
693 g_warning ("Unknown type specified in g_file_info_set_attribute\n");
694 break;
699 * _g_file_attribute_value_set_string:
700 * @attr: a #GFileAttributeValue.
701 * @string: a UTF-8 string to set within the type.
703 * Sets the attribute value to a given UTF-8 string.
705 void
706 _g_file_attribute_value_set_string (GFileAttributeValue *attr,
707 const char *string)
709 g_return_if_fail (attr != NULL);
710 g_return_if_fail (string != NULL);
712 _g_file_attribute_value_clear (attr);
713 attr->type = G_FILE_ATTRIBUTE_TYPE_STRING;
714 attr->u.string = g_strdup (string);
718 * _g_file_attribute_value_set_byte_string:
719 * @attr: a #GFileAttributeValue.
720 * @string: a byte string to set within the type.
722 * Sets the attribute value to a given byte string.
724 void
725 _g_file_attribute_value_set_byte_string (GFileAttributeValue *attr,
726 const char *string)
728 g_return_if_fail (attr != NULL);
729 g_return_if_fail (string != NULL);
731 _g_file_attribute_value_clear (attr);
732 attr->type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
733 attr->u.string = g_strdup (string);
736 void
737 _g_file_attribute_value_set_stringv (GFileAttributeValue *attr,
738 char **value)
740 g_return_if_fail (attr != NULL);
741 g_return_if_fail (value != NULL);
743 _g_file_attribute_value_clear (attr);
744 attr->type = G_FILE_ATTRIBUTE_TYPE_STRINGV;
745 attr->u.stringv = g_strdupv (value);
750 * _g_file_attribute_value_set_boolean:
751 * @attr: a #GFileAttributeValue.
752 * @value: a #gboolean to set within the type.
754 * Sets the attribute value to the given boolean value.
756 void
757 _g_file_attribute_value_set_boolean (GFileAttributeValue *attr,
758 gboolean value)
760 g_return_if_fail (attr != NULL);
762 _g_file_attribute_value_clear (attr);
763 attr->type = G_FILE_ATTRIBUTE_TYPE_BOOLEAN;
764 attr->u.boolean = !!value;
768 * _g_file_attribute_value_set_uint32:
769 * @attr: a #GFileAttributeValue.
770 * @value: a #guint32 to set within the type.
772 * Sets the attribute value to the given unsigned 32-bit integer.
774 void
775 _g_file_attribute_value_set_uint32 (GFileAttributeValue *attr,
776 guint32 value)
778 g_return_if_fail (attr != NULL);
780 _g_file_attribute_value_clear (attr);
781 attr->type = G_FILE_ATTRIBUTE_TYPE_UINT32;
782 attr->u.uint32 = value;
786 * _g_file_attribute_value_set_int32:
787 * @attr: a #GFileAttributeValue.
788 * @value: a #gint32 to set within the type.
790 * Sets the attribute value to the given signed 32-bit integer.
792 void
793 _g_file_attribute_value_set_int32 (GFileAttributeValue *attr,
794 gint32 value)
796 g_return_if_fail (attr != NULL);
798 _g_file_attribute_value_clear (attr);
799 attr->type = G_FILE_ATTRIBUTE_TYPE_INT32;
800 attr->u.int32 = value;
804 * _g_file_attribute_value_set_uint64:
805 * @attr: a #GFileAttributeValue.
806 * @value: a #guint64 to set within the type.
808 * Sets the attribute value to a given unsigned 64-bit integer.
810 void
811 _g_file_attribute_value_set_uint64 (GFileAttributeValue *attr,
812 guint64 value)
814 g_return_if_fail (attr != NULL);
816 _g_file_attribute_value_clear (attr);
817 attr->type = G_FILE_ATTRIBUTE_TYPE_UINT64;
818 attr->u.uint64 = value;
822 * _g_file_attribute_value_set_int64:
823 * @attr: a #GFileAttributeValue.
824 * @value: a #gint64 to set within the type.
826 * Sets the attribute value to a given signed 64-bit integer.
828 void
829 _g_file_attribute_value_set_int64 (GFileAttributeValue *attr,
830 gint64 value)
832 g_return_if_fail (attr != NULL);
834 _g_file_attribute_value_clear (attr);
835 attr->type = G_FILE_ATTRIBUTE_TYPE_INT64;
836 attr->u.int64 = value;
840 * _g_file_attribute_value_set_object:
841 * @attr: a #GFileAttributeValue.
842 * @obj: a #GObject.
844 * Sets the attribute to contain the value @obj.
845 * The @attr references the GObject internally.
847 void
848 _g_file_attribute_value_set_object (GFileAttributeValue *attr,
849 GObject *obj)
851 g_return_if_fail (attr != NULL);
852 g_return_if_fail (obj != NULL);
854 _g_file_attribute_value_clear (attr);
855 attr->type = G_FILE_ATTRIBUTE_TYPE_OBJECT;
856 attr->u.obj = g_object_ref (obj);
859 typedef struct {
860 GFileAttributeInfoList public;
861 GArray *array;
862 int ref_count;
863 } GFileAttributeInfoListPriv;
865 static void
866 list_update_public (GFileAttributeInfoListPriv *priv)
868 priv->public.infos = (GFileAttributeInfo *)priv->array->data;
869 priv->public.n_infos = priv->array->len;
873 * g_file_attribute_info_list_new:
875 * Creates a new file attribute info list.
877 * Returns: a #GFileAttributeInfoList.
879 GFileAttributeInfoList *
880 g_file_attribute_info_list_new (void)
882 GFileAttributeInfoListPriv *priv;
884 priv = g_new0 (GFileAttributeInfoListPriv, 1);
886 priv->ref_count = 1;
887 priv->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
889 list_update_public (priv);
891 return (GFileAttributeInfoList *)priv;
895 * g_file_attribute_info_list_dup:
896 * @list: a #GFileAttributeInfoList to duplicate.
898 * Makes a duplicate of a file attribute info list.
900 * Returns: a copy of the given @list.
902 GFileAttributeInfoList *
903 g_file_attribute_info_list_dup (GFileAttributeInfoList *list)
905 GFileAttributeInfoListPriv *new;
906 int i;
908 g_return_val_if_fail (list != NULL, NULL);
910 new = g_new0 (GFileAttributeInfoListPriv, 1);
911 new->ref_count = 1;
912 new->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
914 g_array_set_size (new->array, list->n_infos);
915 list_update_public (new);
916 for (i = 0; i < list->n_infos; i++)
918 new->public.infos[i].name = g_strdup (list->infos[i].name);
919 new->public.infos[i].type = list->infos[i].type;
920 new->public.infos[i].flags = list->infos[i].flags;
923 return (GFileAttributeInfoList *)new;
927 * g_file_attribute_info_list_ref:
928 * @list: a #GFileAttributeInfoList to reference.
930 * References a file attribute info list.
932 * Returns: #GFileAttributeInfoList or %NULL on error.
934 GFileAttributeInfoList *
935 g_file_attribute_info_list_ref (GFileAttributeInfoList *list)
937 GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
939 g_return_val_if_fail (list != NULL, NULL);
940 g_return_val_if_fail (priv->ref_count > 0, NULL);
942 g_atomic_int_inc (&priv->ref_count);
944 return list;
948 * g_file_attribute_info_list_unref:
949 * @list: The #GFileAttributeInfoList to unreference.
951 * Removes a reference from the given @list. If the reference count
952 * falls to zero, the @list is deleted.
954 void
955 g_file_attribute_info_list_unref (GFileAttributeInfoList *list)
957 GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
958 int i;
960 g_return_if_fail (list != NULL);
961 g_return_if_fail (priv->ref_count > 0);
963 if (g_atomic_int_dec_and_test (&priv->ref_count))
965 for (i = 0; i < list->n_infos; i++)
966 g_free (list->infos[i].name);
967 g_array_free (priv->array, TRUE);
968 g_free (list);
972 static int
973 g_file_attribute_info_list_bsearch (GFileAttributeInfoList *list,
974 const char *name)
976 int start, end, mid;
978 start = 0;
979 end = list->n_infos;
981 while (start != end)
983 mid = start + (end - start) / 2;
985 if (strcmp (name, list->infos[mid].name) < 0)
986 end = mid;
987 else if (strcmp (name, list->infos[mid].name) > 0)
988 start = mid + 1;
989 else
990 return mid;
992 return start;
996 * g_file_attribute_info_list_lookup:
997 * @list: a #GFileAttributeInfoList.
998 * @name: the name of the attribute to lookup.
1000 * Gets the file attribute with the name @name from @list.
1002 * Returns: a #GFileAttributeInfo for the @name, or %NULL if an
1003 * attribute isn't found.
1005 const GFileAttributeInfo *
1006 g_file_attribute_info_list_lookup (GFileAttributeInfoList *list,
1007 const char *name)
1009 int i;
1011 g_return_val_if_fail (list != NULL, NULL);
1012 g_return_val_if_fail (name != NULL, NULL);
1014 i = g_file_attribute_info_list_bsearch (list, name);
1016 if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
1017 return &list->infos[i];
1019 return NULL;
1023 * g_file_attribute_info_list_add:
1024 * @list: a #GFileAttributeInfoList.
1025 * @name: the name of the attribute to add.
1026 * @type: the #GFileAttributeType for the attribute.
1027 * @flags: #GFileAttributeInfoFlags for the attribute.
1029 * Adds a new attribute with @name to the @list, setting
1030 * its @type and @flags.
1032 void
1033 g_file_attribute_info_list_add (GFileAttributeInfoList *list,
1034 const char *name,
1035 GFileAttributeType type,
1036 GFileAttributeInfoFlags flags)
1038 GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
1039 GFileAttributeInfo info;
1040 int i;
1042 g_return_if_fail (list != NULL);
1043 g_return_if_fail (name != NULL);
1045 i = g_file_attribute_info_list_bsearch (list, name);
1047 if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
1049 list->infos[i].type = type;
1050 return;
1053 info.name = g_strdup (name);
1054 info.type = type;
1055 info.flags = flags;
1056 g_array_insert_vals (priv->array, i, &info, 1);
1058 list_update_public (priv);