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>
27 #include "gfileattribute.h"
28 #include <glib-object.h>
32 * SECTION:gfileattribute
33 * @short_description: Key-Value Paired File Attributes
34 * @see_also: #GFile, #GFileInfo
36 * File attributes in GIO consist of a list of key-value pairs.
38 * Keys are strings that contain a key namespace and a key name, separated
39 * by a colon, e.g. "namespace:keyname". Namespaces are included to sort
40 * key-value pairs by namespaces for relevance. Keys can be searched
41 * for using wildcards, e.g. "std:*" will return all of the keys in the
44 * Values are stored within the list in #GFileAttributeValue structures.
45 * Values can store different types, listed in the enum #GFileAttributeType.
46 * Upon creation of a #GFileAttributeValue, the type will be set to
47 * %G_FILE_ATTRIBUTE_TYPE_INVALID.
49 * The Key-value list is stored within the #GFile structure as a
50 * #GFileAttributeInfoList. This list is queryable by key names
51 * as indicated earlier.
53 * Classes that implement #GFileIface will create a #GFileAttributeInfoList and
54 * install default keys and values for their given file system, architecture,
55 * and other possible implementation details (e.g., on a UNIX system, a file
56 * attribute key will be registered for the user id for a given file). Other
57 * attributes can be appended to a #GFileAttributeList later by
58 * g_file_attribute_info_list_add().
62 * <title>GFileAttributes Namespaces</title>
63 * <tgroup cols='2' align='left'><thead>
64 * <row><entry>Namspace</entry><entry>Description</entry></row>
67 * <row><entry>"std"</entry><entry>The "Standard" namespace. General file
68 * information that any application may need should be put in this namespace. Examples
69 * include the file's name, type, and size.</entry></row>
70 * <row><entry>"etag"</entry><entry>The "Entity Tag" namespace. Remotely shared
71 * files, like those on HTTP/1.1 file systems, use "entity tags" to quickly determine
72 * if a file has been modified. Entity tags are globally unique, and should
73 * always be sent with the current revision of a file. An example of a key
74 * in this namespace is "value", which contains the value of the current
75 * entity tag.</entry></row>
76 * <row><entry>"id"</entry><entry>The "Identification" namespace. This
77 * namespace is used by file managers and applications that list directories
78 * to check for loops and to uniquely identify files.</entry></row>
79 * <row><entry>"access"</entry><entry>The "Access" namespace. Used to check
80 * if a user has the proper privilidges to access files and perform
81 * file operations. Keys in this namespace are made to be generic
82 * and easily understood, e.g. the "can_read" key is %TRUE if
83 * the current user has permission to read the file. UNIX permissions and
84 * NTFS ACLs in Windows should be mapped to these values.</entry></row>
85 * <row><entry>"mountable"</entry><entry>The "Mountable" namespace. Includes
86 * simple boolean keys for checking if a file or path supports mount operations, e.g.
87 * mount, unmount, eject.</entry></row>
88 * <row><entry>"time"</entry><entry>The "Time" namespace. Includes file
89 * access, changed, created times. </entry></row>
90 * <row><entry>"unix"</entry><entry>The "Unix" namespace. Includes UNIX-specific
91 * information and may not be available for all files. Examples include
92 * the UNIX "UID", "GID", etc.</entry></row>
93 * <row><entry>"dos"</entry><entry>The "DOS" namespace. Includes DOS-specific
94 * information and may not be available for all files. Examples include
95 * "is_system" for checking if a file is marked as a system file, and "is_archive"
96 * for checking if a file is marked as an archive file.</entry></row>
97 * <row><entry>"owner"</entry><entry>The "Owner" namespace. Includes information
98 * about who owns a file. May not be available for all file systems. Examples include
99 * "user" for getting the user name of the file owner.</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 failed.</entry></row>
104 * <row><entry>"fs"</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>
114 * More namespaces can be added from GIO modules or by individual applications.
115 * For more information about writing GIO modules, see #GIOModule.
118 * <title>GFileAttributes Built-in Keys and Value Types</title>
119 * <tgroup cols='3' align='left'><thead>
120 * <row><entry>Enum Value</entry><entry>Namespace:Key</entry><entry>Value Type</entry></row>
122 * <row><entry>%G_FILE_ATTRIBUTE_STD_TYPE</entry><entry>std:type</entry><entry>uint32 (#GFileType)</entry></row>
123 * <row><entry>%G_FILE_ATTRIBUTE_STD_IS_HIDDEN</entry><entry>std:is_hidden</entry><entry>boolean</entry></row>
124 * <row><entry>%G_FILE_ATTRIBUTE_STD_IS_BACKUP</entry><entry>std:is_backup</entry><entry>boolean</entry></row>
125 * <row><entry>%G_FILE_ATTRIBUTE_STD_IS_SYMLINK</entry><entry>std:is_symlink</entry><entry>boolean</entry></row>
126 * <row><entry>%G_FILE_ATTRIBUTE_STD_IS_VIRTUAL</entry><entry>std:is_virtual</entry><entry>boolean</entry></row>
127 * <row><entry>%G_FILE_ATTRIBUTE_STD_NAME</entry><entry>std:name</entry><entry>byte string</entry></row>
128 * <row><entry>%G_FILE_ATTRIBUTE_STD_DISPLAY_NAME</entry><entry>std:display_name</entry><entry>string</entry></row>
129 * <row><entry>%G_FILE_ATTRIBUTE_STD_EDIT_NAME</entry><entry>std:edit_name</entry><entry>string</entry></row>
130 * <row><entry>%G_FILE_ATTRIBUTE_STD_ICON</entry><entry>std:icon</entry><entry>object (#GIcon)</entry></row>
131 * <row><entry>%G_FILE_ATTRIBUTE_STD_CONTENT_TYPE</entry><entry>std:content_type</entry><entry>string</entry></row>
132 * <row><entry>%G_FILE_ATTRIBUTE_STD_FAST_CONTENT_TYPE</entry><entry>std:fast_content_type</entry><entry>string</entry></row>
133 * <row><entry>%G_FILE_ATTRIBUTE_STD_SIZE</entry><entry>std:size</entry><entry>uint64</entry></row>
134 * <row><entry>%G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET</entry><entry>std:symlink_target</entry><entry>byte string</entry></row>
135 * <row><entry>%G_FILE_ATTRIBUTE_STD_TARGET_URI</entry><entry>std:target_uri</entry><entry>string</entry></row>
136 * <row><entry>%G_FILE_ATTRIBUTE_STD_SORT_ORDER</entry><entry>std:sort_order</entry><entry>int32</entry></row>
137 * <row><entry>%G_FILE_ATTRIBUTE_ETAG_VALUE</entry><entry>etag:value</entry><entry>string</entry></row>
138 * <row><entry>%G_FILE_ATTRIBUTE_ID_FILE</entry><entry>id:file</entry><entry>string</entry></row>
139 * <row><entry>%G_FILE_ATTRIBUTE_ID_FS</entry><entry>id:fs</entry><entry>string</entry></row>
140 * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_READ</entry><entry>access:can_read</entry><entry>boolean</entry></row>
141 * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE</entry><entry>access:can_write</entry><entry>boolean</entry></row>
142 * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE</entry><entry>access:can_execute</entry><entry>boolean</entry></row>
143 * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE</entry><entry>access:can_delete</entry><entry>boolean</entry></row>
144 * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH</entry><entry>access:can_trash</entry><entry>boolean</entry></row>
145 * <row><entry>%G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME</entry><entry>access:can_rename</entry><entry>boolean</entry></row>
146 * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_CAN_MOUNT</entry><entry>mountable:can_mount</entry><entry>boolean</entry></row>
147 * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_CAN_UNMOUNT</entry><entry>mountable:can_unmount</entry><entry>boolean</entry></row>
148 * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_CAN_EJECT</entry><entry>mountable:can_eject</entry><entry>boolean</entry></row>
149 * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_UNIX_DEVICE</entry><entry>mountable:unix_device</entry><entry>uint32</entry></row>
150 * <row><entry>%G_FILE_ATTRIBUTE_MOUNTABLE_HAL_UDI</entry><entry>mountable:hal_udi</entry><entry>string</entry></row>
151 * <row><entry>%G_FILE_ATTRIBUTE_TIME_MODIFIED</entry><entry>time:modified</entry><entry>uint64</entry></row>
152 * <row><entry>%G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC</entry><entry>time:modified_usec</entry><entry>uint32</entry></row>
153 * <row><entry>%G_FILE_ATTRIBUTE_TIME_ACCESS</entry><entry>time:access</entry><entry>uint64</entry></row>
154 * <row><entry>%G_FILE_ATTRIBUTE_TIME_ACCESS_USEC</entry><entry>time:access_usec</entry><entry>uint32</entry></row>
155 * <row><entry>%G_FILE_ATTRIBUTE_TIME_CHANGED</entry><entry>time:changed</entry><entry>uint64</entry></row>
156 * <row><entry>%G_FILE_ATTRIBUTE_TIME_CHANGED_USEC</entry><entry>time:changed_usec</entry><entry>uint32</entry></row>
157 * <row><entry>%G_FILE_ATTRIBUTE_TIME_CREATED</entry><entry>time:created</entry><entry>uint64</entry></row>
158 * <row><entry>%G_FILE_ATTRIBUTE_TIME_CREATED_USEC</entry><entry>time:created_usec</entry><entry>uint32</entry></row>
159 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_DEVICE</entry><entry>unix:device</entry><entry>uint32</entry></row>
160 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_INODE</entry><entry>unix:inode</entry><entry>uint64</entry></row>
161 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_MODE</entry><entry>unix:mode</entry><entry>uint32</entry></row>
162 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_NLINK</entry><entry>unix:nlink</entry><entry>uint32</entry></row>
163 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_UID</entry><entry>unix:uid</entry><entry>uint32</entry></row>
164 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_GID</entry><entry>unix:gid</entry><entry>uint32</entry></row>
165 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_RDEV</entry><entry>unix:rdev</entry><entry>uint32</entry></row>
166 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE</entry><entry>unix:block_size</entry><entry>uint32</entry></row>
167 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_BLOCKS</entry><entry>unix:blocks</entry><entry>uint64</entry></row>
168 * <row><entry>%G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT</entry><entry>unix:is_mountpoint</entry><entry>boolean</entry></row>
169 * <row><entry>%G_FILE_ATTRIBUTE_DOS_IS_ARCHIVE</entry><entry>dos:is_archive</entry><entry>boolean</entry></row>
170 * <row><entry>%G_FILE_ATTRIBUTE_DOS_IS_SYSTEM</entry><entry>dos:is_system</entry><entry>boolean</entry></row>
171 * <row><entry>%G_FILE_ATTRIBUTE_OWNER_USER</entry><entry>owner:user</entry><entry>string</entry></row>
172 * <row><entry>%G_FILE_ATTRIBUTE_OWNER_USER_REAL</entry><entry>owner:user_real</entry><entry>string</entry></row>
173 * <row><entry>%G_FILE_ATTRIBUTE_OWNER_GROUP</entry><entry>owner:group</entry><entry>string</entry></row>
174 * <row><entry>%G_FILE_ATTRIBUTE_THUMBNAIL_PATH</entry><entry>thumbnail:path</entry><entry>bytestring</entry></row>
175 * <row><entry>%G_FILE_ATTRIBUTE_THUMBNAILING_FAILED</entry><entry>thumbnail:failed</entry><entry>boolean</entry></row>
176 * <row><entry>%G_FILE_ATTRIBUTE_FS_SIZE</entry><entry>fs:size</entry><entry>uint64</entry></row>
177 * <row><entry>%G_FILE_ATTRIBUTE_FS_FREE</entry><entry>fs:free</entry><entry>uint64</entry></row>
178 * <row><entry>%G_FILE_ATTRIBUTE_FS_TYPE</entry><entry>fs:type</entry><entry>string</entry></row>
179 * <row><entry>%G_FILE_ATTRIBUTE_FS_READONLY</entry><entry>fs:readonly</entry><entry>boolean</entry></row>
180 * <row><entry>%G_FILE_ATTRIBUTE_GVFS_BACKEND</entry><entry>gvfs:backend</entry><entry>string</entry></row>
181 * </tbody></tgroup></table></para>
186 * g_file_attribute_value_free:
187 * @attr: a #GFileAttributeValue.
189 * Frees the memory used by @attr.
193 g_file_attribute_value_free (GFileAttributeValue *attr)
195 g_return_if_fail (attr != NULL);
197 g_file_attribute_value_clear (attr);
202 * g_file_attribute_value_clear:
203 * @attr: a #GFileAttributeValue.
205 * Clears the value of @attr and sets its type to
206 * %G_FILE_ATTRIBUTE_TYPE_INVALID.
210 g_file_attribute_value_clear (GFileAttributeValue *attr)
212 g_return_if_fail (attr != NULL);
214 if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
215 attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
216 g_free (attr->u.string);
218 if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
220 g_object_unref (attr->u.obj);
222 attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
226 * g_file_attribute_value_set:
227 * @attr: a #GFileAttributeValue to set the value in.
228 * @new_value: a #GFileAttributeValue to get the value from.
230 * Sets an attribute's value from another attribute.
233 g_file_attribute_value_set (GFileAttributeValue *attr,
234 const GFileAttributeValue *new_value)
236 g_return_if_fail (attr != NULL);
237 g_return_if_fail (new_value != NULL);
239 g_file_attribute_value_clear (attr);
242 if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
243 attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
244 attr->u.string = g_strdup (attr->u.string);
246 if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
248 g_object_ref (attr->u.obj);
252 * g_file_attribute_value_new:
254 * Creates a new file attribute.
256 * Returns: a #GFileAttributeValue.
258 GFileAttributeValue *
259 g_file_attribute_value_new (void)
261 GFileAttributeValue *attr;
263 attr = g_new (GFileAttributeValue, 1);
264 attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
270 * g_file_attribute_value_dup:
271 * @other: a #GFileAttributeValue to duplicate.
273 * Duplicates a file attribute.
275 * Returns: a duplicate of the @other.
277 GFileAttributeValue *
278 g_file_attribute_value_dup (const GFileAttributeValue *other)
280 GFileAttributeValue *attr;
282 g_return_val_if_fail (other != NULL, NULL);
284 attr = g_new (GFileAttributeValue, 1);
285 attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
286 g_file_attribute_value_set (attr, other);
293 return c >= 32 && c <= 126 && c != '\\';
297 escape_byte_string (const char *str)
301 char *escaped_val, *p;
303 char *hex_digits = "0123456789abcdef";
308 for (i = 0; i < len; i++)
310 if (!valid_char (str[i]))
314 if (num_invalid == 0)
315 return g_strdup (str);
318 escaped_val = g_malloc (len + num_invalid*3 + 1);
321 for (i = 0; i < len; i++)
330 *p++ = hex_digits[(c >> 8) & 0xf];
331 *p++ = hex_digits[c & 0xf];
340 * g_file_attribute_value_as_string:
341 * @attr: a #GFileAttributeValue.
343 * Converts a #GFileAttributeValue to a string for display.
344 * The returned string should be freed when no longer needed.
346 * Returns: a string from the @attr, %NULL on error, or "<invalid>"
347 * if @attr is of type %G_FILE_ATTRIBUTE_TYPE_INVALID.
350 g_file_attribute_value_as_string (const GFileAttributeValue *attr)
354 g_return_val_if_fail (attr != NULL, NULL);
358 case G_FILE_ATTRIBUTE_TYPE_STRING:
359 str = g_strdup (attr->u.string);
361 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
362 str = escape_byte_string (attr->u.string);
364 case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
365 str = g_strdup_printf ("%s", attr->u.boolean?"TRUE":"FALSE");
367 case G_FILE_ATTRIBUTE_TYPE_UINT32:
368 str = g_strdup_printf ("%u", (unsigned int)attr->u.uint32);
370 case G_FILE_ATTRIBUTE_TYPE_INT32:
371 str = g_strdup_printf ("%i", (int)attr->u.int32);
373 case G_FILE_ATTRIBUTE_TYPE_UINT64:
374 str = g_strdup_printf ("%"G_GUINT64_FORMAT, attr->u.uint64);
376 case G_FILE_ATTRIBUTE_TYPE_INT64:
377 str = g_strdup_printf ("%"G_GINT64_FORMAT, attr->u.int64);
379 case G_FILE_ATTRIBUTE_TYPE_OBJECT:
380 str = g_strdup_printf ("%s:%p", g_type_name_from_instance
381 ((GTypeInstance *) attr->u.obj),
385 g_warning ("Invalid type in GFileInfo attribute");
386 str = g_strdup ("<invalid>");
394 * g_file_attribute_value_get_string:
395 * @attr: a #GFileAttributeValue.
397 * Gets the string from a file attribute value. If the value is not the
398 * right type then %NULL will be returned.
400 * Returns: the string value contained within the attribute, or %NULL.
403 g_file_attribute_value_get_string (const GFileAttributeValue *attr)
408 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING, NULL);
410 return attr->u.string;
414 * g_file_attribute_value_get_byte_string:
415 * @attr: a #GFileAttributeValue.
417 * Gets the byte string from a file attribute value. If the value is not the
418 * right type then %NULL will be returned.
420 * Returns: the byte string contained within the attribute or %NULL.
423 g_file_attribute_value_get_byte_string (const GFileAttributeValue *attr)
428 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, NULL);
430 return attr->u.string;
434 * g_file_attribute_value_get_boolean:
435 * @attr: a #GFileAttributeValue.
437 * Gets the boolean value from a file attribute value. If the value is not the
438 * right type then %FALSE will be returned.
440 * Returns: the boolean value contained within the attribute, or %FALSE.
443 g_file_attribute_value_get_boolean (const GFileAttributeValue *attr)
448 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BOOLEAN, FALSE);
450 return attr->u.boolean;
454 * g_file_attribute_value_get_uint32:
455 * @attr: a #GFileAttributeValue.
457 * Gets the unsigned 32-bit integer from a file attribute value. If the value
458 * is not the right type then %0 will be returned.
460 * Returns: the unsigned 32-bit integer from the attribute, or %0.
463 g_file_attribute_value_get_uint32 (const GFileAttributeValue *attr)
468 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT32, 0);
470 return attr->u.uint32;
474 * g_file_attribute_value_get_int32:
475 * @attr: a #GFileAttributeValue.
477 * Gets the signed 32-bit integer from a file attribute value. If the value
478 * is not the right type then %0 will be returned.
480 * Returns: the signed 32-bit integer from the attribute, or %0.
483 g_file_attribute_value_get_int32 (const GFileAttributeValue *attr)
488 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT32, 0);
490 return attr->u.int32;
494 * g_file_attribute_value_get_uint64:
495 * @attr: a #GFileAttributeValue.
497 * Gets the unsigned 64-bit integer from a file attribute value. If the value
498 * is not the right type then %0 will be returned.
500 * Returns: the unsigned 64-bit integer from the attribute, or %0.
503 g_file_attribute_value_get_uint64 (const GFileAttributeValue *attr)
508 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT64, 0);
510 return attr->u.uint64;
514 * g_file_attribute_value_get_int64:
515 * @attr: a #GFileAttributeValue.
517 * Gets the signed 64-bit integer from a file attribute value. If the value
518 * is not the right type then %0 will be returned.
520 * Returns: the signed 64-bit integer from the attribute, or %0.
523 g_file_attribute_value_get_int64 (const GFileAttributeValue *attr)
528 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT64, 0);
530 return attr->u.int64;
534 * g_file_attribute_value_get_object:
535 * @attr: a #GFileAttributeValue.
537 * Gets the GObject from a file attribute value. If the value
538 * is not the right type then %NULL will be returned.
540 * Returns: the GObject from the attribute, or %0.
543 g_file_attribute_value_get_object (const GFileAttributeValue *attr)
548 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT, NULL);
554 * g_file_attribute_value_set_string:
555 * @attr: a #GFileAttributeValue.
556 * @string: a string to set within the type.
558 * Sets the attribute value to a given string.
562 g_file_attribute_value_set_string (GFileAttributeValue *attr,
565 g_return_if_fail (attr != NULL);
566 g_return_if_fail (string != NULL);
568 g_file_attribute_value_clear (attr);
569 attr->type = G_FILE_ATTRIBUTE_TYPE_STRING;
570 attr->u.string = g_strdup (string);
574 * g_file_attribute_value_set_byte_string:
575 * @attr: a #GFileAttributeValue.
576 * @string: a byte string to set within the type.
578 * Sets the attribute value to a given byte string.
582 g_file_attribute_value_set_byte_string (GFileAttributeValue *attr,
585 g_return_if_fail (attr != NULL);
586 g_return_if_fail (string != NULL);
588 g_file_attribute_value_clear (attr);
589 attr->type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
590 attr->u.string = g_strdup (string);
594 * g_file_attribute_value_set_boolean:
595 * @attr: a #GFileAttributeValue.
596 * @value: a #gboolean to set within the type.
598 * Sets the attribute value to the given boolean value.
602 g_file_attribute_value_set_boolean (GFileAttributeValue *attr,
605 g_return_if_fail (attr != NULL);
607 g_file_attribute_value_clear (attr);
608 attr->type = G_FILE_ATTRIBUTE_TYPE_BOOLEAN;
609 attr->u.boolean = !!value;
613 * g_file_attribute_value_set_uint32:
614 * @attr: a #GFileAttributeValue.
615 * @value: a #guint32 to set within the type.
617 * Sets the attribute value to the given unsigned 32-bit integer.
621 g_file_attribute_value_set_uint32 (GFileAttributeValue *attr,
624 g_return_if_fail (attr != NULL);
626 g_file_attribute_value_clear (attr);
627 attr->type = G_FILE_ATTRIBUTE_TYPE_UINT32;
628 attr->u.uint32 = value;
632 * g_file_attribute_value_set_int32:
633 * @attr: a #GFileAttributeValue.
634 * @value: a #gint32 to set within the type.
636 * Sets the attribute value to the given signed 32-bit integer.
640 g_file_attribute_value_set_int32 (GFileAttributeValue *attr,
643 g_return_if_fail (attr != NULL);
645 g_file_attribute_value_clear (attr);
646 attr->type = G_FILE_ATTRIBUTE_TYPE_INT32;
647 attr->u.int32 = value;
651 * g_file_attribute_value_set_uint64:
652 * @attr: a #GFileAttributeValue.
653 * @value: a #guint64 to set within the type.
655 * Sets the attribute value to a given unsigned 64-bit integer.
659 g_file_attribute_value_set_uint64 (GFileAttributeValue *attr,
662 g_return_if_fail (attr != NULL);
664 g_file_attribute_value_clear (attr);
665 attr->type = G_FILE_ATTRIBUTE_TYPE_UINT64;
666 attr->u.uint64 = value;
670 * g_file_attribute_value_set_int64:
671 * @attr: a #GFileAttributeValue.
672 * @value: a #gint64 to set within the type.
674 * Sets the attribute value to a given signed 64-bit integer.
678 g_file_attribute_value_set_int64 (GFileAttributeValue *attr,
681 g_return_if_fail (attr != NULL);
683 g_file_attribute_value_clear (attr);
684 attr->type = G_FILE_ATTRIBUTE_TYPE_INT64;
685 attr->u.int64 = value;
689 * g_file_attribute_value_set_object:
690 * @attr: a #GFileAttributeValue.
693 * Sets the attribute to contain the value @obj.
694 * The @attr references the GObject internally.
698 g_file_attribute_value_set_object (GFileAttributeValue *attr,
701 g_return_if_fail (attr != NULL);
702 g_return_if_fail (obj != NULL);
704 g_file_attribute_value_clear (attr);
705 attr->type = G_FILE_ATTRIBUTE_TYPE_OBJECT;
706 attr->u.obj = g_object_ref (obj);
710 GFileAttributeInfoList public;
713 } GFileAttributeInfoListPriv;
716 list_update_public (GFileAttributeInfoListPriv *priv)
718 priv->public.infos = (GFileAttributeInfo *)priv->array->data;
719 priv->public.n_infos = priv->array->len;
723 * g_file_attribute_info_list_new:
725 * Creates a new file attribute info list.
727 * Returns: a #GFileAttributeInfoList.
729 GFileAttributeInfoList *
730 g_file_attribute_info_list_new (void)
732 GFileAttributeInfoListPriv *priv;
734 priv = g_new0 (GFileAttributeInfoListPriv, 1);
737 priv->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
739 list_update_public (priv);
741 return (GFileAttributeInfoList *)priv;
745 * g_file_attribute_info_list_dup:
746 * @list: a #GFileAttributeInfoList to duplicate.
748 * Makes a duplicate of a file attribute info list.
750 * Returns: a copy of the given @list.
752 GFileAttributeInfoList *
753 g_file_attribute_info_list_dup (GFileAttributeInfoList *list)
755 GFileAttributeInfoListPriv *new;
758 g_return_val_if_fail (list != NULL, NULL);
760 new = g_new0 (GFileAttributeInfoListPriv, 1);
762 new->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
764 g_array_set_size (new->array, list->n_infos);
765 list_update_public (new);
766 for (i = 0; i < list->n_infos; i++)
768 new->public.infos[i].name = g_strdup (list->infos[i].name);
769 new->public.infos[i].type = list->infos[i].type;
770 new->public.infos[i].flags = list->infos[i].flags;
773 return (GFileAttributeInfoList *)new;
777 * g_file_attribute_info_list_ref:
778 * @list: a #GFileAttributeInfoList to reference.
780 * References a file attribute info list.
782 * Returns: #GFileAttributeInfoList or %NULL on error.
784 GFileAttributeInfoList *
785 g_file_attribute_info_list_ref (GFileAttributeInfoList *list)
787 GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
789 g_return_val_if_fail (list != NULL, NULL);
790 g_return_val_if_fail (priv->ref_count > 0, NULL);
792 g_atomic_int_inc (&priv->ref_count);
798 * g_file_attribute_info_list_unref:
799 * @list: The #GFileAttributeInfoList to unreference.
801 * Removes a reference from the given @list. If the reference count
802 * falls to zero, the @list is deleted.
805 g_file_attribute_info_list_unref (GFileAttributeInfoList *list)
807 GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
810 g_return_if_fail (list != NULL);
811 g_return_if_fail (priv->ref_count > 0);
813 if (g_atomic_int_dec_and_test (&priv->ref_count))
815 for (i = 0; i < list->n_infos; i++)
816 g_free (list->infos[i].name);
817 g_array_free (priv->array, TRUE);
822 g_file_attribute_info_list_bsearch (GFileAttributeInfoList *list,
832 mid = start + (end - start) / 2;
834 if (strcmp (name, list->infos[mid].name) < 0)
836 else if (strcmp (name, list->infos[mid].name) > 0)
845 * g_file_attribute_info_list_lookup:
846 * @list: a #GFileAttributeInfoList.
847 * @name: the name of the attribute to lookup.
849 * Gets the file attribute with the name @name from @list.
851 * Returns: a #GFileAttributeInfo for the @name, or %NULL if an
852 * attribute isn't found.
854 const GFileAttributeInfo *
855 g_file_attribute_info_list_lookup (GFileAttributeInfoList *list,
860 g_return_val_if_fail (list != NULL, NULL);
861 g_return_val_if_fail (name != NULL, NULL);
863 i = g_file_attribute_info_list_bsearch (list, name);
865 if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
866 return &list->infos[i];
872 * g_file_attribute_info_list_add:
873 * @list: a #GFileAttributeInfoList.
874 * @name: the name of the attribute to add.
875 * @type: the #GFileAttributeType for the attribute.
876 * @flags: #GFileAttributeFlags for the attribute.
878 * Adds a new attribute with @name to the @list, setting
879 * its @type and @flags.
882 g_file_attribute_info_list_add (GFileAttributeInfoList *list,
884 GFileAttributeType type,
885 GFileAttributeFlags flags)
887 GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
888 GFileAttributeInfo info;
891 g_return_if_fail (list != NULL);
892 g_return_if_fail (name != NULL);
894 i = g_file_attribute_info_list_bsearch (list, name);
896 if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
898 list->infos[i].type = type;
902 info.name = g_strdup (name);
905 g_array_insert_vals (priv->array, i, &info, 1);
907 list_update_public (priv);