Forgotten entry
[glib.git] / gio / gfileinfo.c
blob7fe6b64aa9fc16cf1b4658b0ff768ced4b7ca20a
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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 /**
24 * SECTION:gfileinfo
25 * @short_description: File Information and Attributes
26 * @include: gio/gio.h
27 * @see_also: #GFile, <link linkend="gio-GFileAttribute">GFileAttribute</link>
29 * Functionality for manipulating basic metadata for files. #GFileInfo
30 * implements methods for getting information that all files should
31 * contain, and allows for manipulation of extended attributes.
33 * See <link linkend="gio-GFileAttribute">GFileAttribute</link> for more
34 * information on how GIO handles file attributes.
36 * To obtain a #GFileInfo for a #GFile, use g_file_query_info() (or its
37 * async variant). To obtain a #GFileInfo for a file input or output
38 * stream, use g_file_input_stream_query_info() or
39 * g_file_output_stream_query_info() (or their async variants).
41 * To change the actual attributes of a file, you should then set the
42 * attribute in the #GFileInfo and call g_file_set_attributes_from_info()
43 * or g_file_set_attributes_async() on a GFile.
45 * However, not all attributes can be changed in the file. For instance,
46 * the actual size of a file cannot be changed via g_file_info_set_size().
47 * You may call g_file_query_settable_attributes() and
48 * g_file_query_writable_namespaces() to discover the settable attributes
49 * of a particular file at runtime.
51 * #GFileAttributeMatcher allows for searching through a #GFileInfo for
52 * attributes.
53 **/
55 #include "config.h"
57 #include <string.h>
59 #include "gfileinfo.h"
60 #include "gfileattribute-priv.h"
61 #include "gicon.h"
62 #include "glibintl.h"
64 #include "gioalias.h"
66 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
67 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
69 typedef struct {
70 guint32 attribute;
71 GFileAttributeValue value;
72 } GFileAttribute;
74 struct _GFileInfo
76 GObject parent_instance;
78 GArray *attributes;
79 GFileAttributeMatcher *mask;
82 struct _GFileInfoClass
84 GObjectClass parent_class;
88 static gboolean g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
89 guint32 id);
91 G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);
93 typedef struct {
94 guint32 id;
95 guint32 attribute_id_counter;
96 } NSInfo;
98 G_LOCK_DEFINE_STATIC (attribute_hash);
99 static int namespace_id_counter = 0;
100 static GHashTable *ns_hash = NULL;
101 static GHashTable *attribute_hash = NULL;
102 static char ***attributes = NULL;
104 /* Attribute ids are 32bit, we split it up like this:
105 * |------------|--------------------|
106 * 12 bit 20 bit
107 * namespace attribute id
109 * This way the attributes gets sorted in namespace order
112 #define NS_POS 20
113 #define NS_MASK ((guint32)((1<<12) - 1))
114 #define ID_POS 0
115 #define ID_MASK ((guint32)((1<<20) - 1))
117 #define GET_NS(_attr_id) \
118 (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
119 #define GET_ID(_attr_id) \
120 (((guint32)(_attr_id) >> ID_POS) & ID_MASK)
122 #define MAKE_ATTR_ID(_ns, _id) \
123 ( ((((guint32) _ns) & NS_MASK) << NS_POS) | \
124 ((((guint32) _id) & ID_MASK) << ID_POS) )
126 static NSInfo *
127 _lookup_namespace (const char *namespace)
129 NSInfo *ns_info;
131 ns_info = g_hash_table_lookup (ns_hash, namespace);
132 if (ns_info == NULL)
134 ns_info = g_new0 (NSInfo, 1);
135 ns_info->id = ++namespace_id_counter;
136 g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
137 attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
138 attributes[ns_info->id] = NULL;
140 return ns_info;
143 static guint32
144 lookup_namespace (const char *namespace)
146 NSInfo *ns_info;
147 guint32 id;
149 G_LOCK (attribute_hash);
151 if (attribute_hash == NULL)
153 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
154 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
157 ns_info = _lookup_namespace (namespace);
158 id = 0;
159 if (ns_info)
160 id = ns_info->id;
162 G_UNLOCK (attribute_hash);
164 return id;
167 static char *
168 get_attribute_for_id (int attribute)
170 char *s;
171 G_LOCK (attribute_hash);
172 s = attributes[GET_NS(attribute)][GET_ID(attribute)];
173 G_UNLOCK (attribute_hash);
174 return s;
177 static guint32
178 lookup_attribute (const char *attribute)
180 guint32 attr_id, id;
181 char *ns;
182 const char *colon;
183 NSInfo *ns_info;
185 G_LOCK (attribute_hash);
186 if (attribute_hash == NULL)
188 ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
189 attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
192 attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));
194 if (attr_id != 0)
196 G_UNLOCK (attribute_hash);
197 return attr_id;
200 colon = strstr (attribute, "::");
201 if (colon)
202 ns = g_strndup (attribute, colon - attribute);
203 else
204 ns = g_strdup ("");
206 ns_info = _lookup_namespace (ns);
207 g_free (ns);
209 id = ++ns_info->attribute_id_counter;
210 attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
211 attributes[ns_info->id][id] = g_strdup (attribute);
213 attr_id = MAKE_ATTR_ID (ns_info->id, id);
215 g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));
217 G_UNLOCK (attribute_hash);
219 return attr_id;
222 static void
223 g_file_info_finalize (GObject *object)
225 GFileInfo *info;
226 int i;
227 GFileAttribute *attrs;
229 info = G_FILE_INFO (object);
231 attrs = (GFileAttribute *)info->attributes->data;
232 for (i = 0; i < info->attributes->len; i++)
233 _g_file_attribute_value_clear (&attrs[i].value);
234 g_array_free (info->attributes, TRUE);
236 if (info->mask != NO_ATTRIBUTE_MASK)
237 g_file_attribute_matcher_unref (info->mask);
239 G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
242 static void
243 g_file_info_class_init (GFileInfoClass *klass)
245 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
247 gobject_class->finalize = g_file_info_finalize;
250 static void
251 g_file_info_init (GFileInfo *info)
253 info->mask = NO_ATTRIBUTE_MASK;
254 info->attributes = g_array_new (FALSE, FALSE,
255 sizeof (GFileAttribute));
259 * g_file_info_new:
261 * Creates a new file info structure.
263 * Returns: a #GFileInfo.
265 GFileInfo *
266 g_file_info_new (void)
268 return g_object_new (G_TYPE_FILE_INFO, NULL);
272 * g_file_info_copy_into:
273 * @src_info: source to copy attributes from.
274 * @dest_info: destination to copy attributes to.
276 * Copies all of the #GFileAttribute<!-- -->s from @src_info to @dest_info.
278 void
279 g_file_info_copy_into (GFileInfo *src_info,
280 GFileInfo *dest_info)
282 GFileAttribute *source, *dest;
283 int i;
285 g_return_if_fail (G_IS_FILE_INFO (src_info));
286 g_return_if_fail (G_IS_FILE_INFO (dest_info));
288 dest = (GFileAttribute *)dest_info->attributes->data;
289 for (i = 0; i < dest_info->attributes->len; i++)
290 _g_file_attribute_value_clear (&dest[i].value);
292 g_array_set_size (dest_info->attributes,
293 src_info->attributes->len);
295 source = (GFileAttribute *)src_info->attributes->data;
296 dest = (GFileAttribute *)dest_info->attributes->data;
298 for (i = 0; i < src_info->attributes->len; i++)
300 dest[i].attribute = source[i].attribute;
301 dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
302 _g_file_attribute_value_set (&dest[i].value, &source[i].value);
305 if (src_info->mask == NO_ATTRIBUTE_MASK)
306 dest_info->mask = NO_ATTRIBUTE_MASK;
307 else
308 dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
312 * g_file_info_dup:
313 * @other: a #GFileInfo.
315 * Duplicates a file info structure.
317 * Returns: a duplicate #GFileInfo of @other.
319 GFileInfo *
320 g_file_info_dup (GFileInfo *other)
322 GFileInfo *new;
324 g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
326 new = g_file_info_new ();
327 g_file_info_copy_into (other, new);
328 return new;
332 * g_file_info_set_attribute_mask:
333 * @info: a #GFileInfo.
334 * @mask: a #GFileAttributeMatcher.
336 * Sets @mask on @info to match specific attribute types.
338 void
339 g_file_info_set_attribute_mask (GFileInfo *info,
340 GFileAttributeMatcher *mask)
342 GFileAttribute *attr;
343 int i;
345 g_return_if_fail (G_IS_FILE_INFO (info));
347 if (mask != info->mask)
349 if (info->mask != NO_ATTRIBUTE_MASK)
350 g_file_attribute_matcher_unref (info->mask);
351 info->mask = g_file_attribute_matcher_ref (mask);
353 /* Remove non-matching attributes */
354 for (i = 0; i < info->attributes->len; i++)
356 attr = &g_array_index (info->attributes, GFileAttribute, i);
357 if (!g_file_attribute_matcher_matches_id (mask,
358 attr->attribute))
360 _g_file_attribute_value_clear (&attr->value);
361 g_array_remove_index (info->attributes, i);
362 i--;
369 * g_file_info_unset_attribute_mask:
370 * @info: #GFileInfo.
372 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
373 * is set.
375 void
376 g_file_info_unset_attribute_mask (GFileInfo *info)
378 g_return_if_fail (G_IS_FILE_INFO (info));
380 if (info->mask != NO_ATTRIBUTE_MASK)
381 g_file_attribute_matcher_unref (info->mask);
382 info->mask = NO_ATTRIBUTE_MASK;
386 * g_file_info_clear_status:
387 * @info: a #GFileInfo.
389 * Clears the status information from @info.
391 void
392 g_file_info_clear_status (GFileInfo *info)
394 GFileAttribute *attrs;
395 int i;
397 g_return_if_fail (G_IS_FILE_INFO (info));
399 attrs = (GFileAttribute *)info->attributes->data;
400 for (i = 0; i < info->attributes->len; i++)
401 attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
404 static int
405 g_file_info_find_place (GFileInfo *info,
406 guint32 attribute)
408 int min, max, med;
409 GFileAttribute *attrs;
410 /* Binary search for the place where attribute would be, if its
411 in the array */
413 min = 0;
414 max = info->attributes->len;
416 attrs = (GFileAttribute *)info->attributes->data;
418 while (min < max)
420 med = min + (max - min) / 2;
421 if (attrs[med].attribute == attribute)
423 min = med;
424 break;
426 else if (attrs[med].attribute < attribute)
427 min = med + 1;
428 else /* attrs[med].attribute > attribute */
429 max = med;
432 return min;
435 static GFileAttributeValue *
436 g_file_info_find_value (GFileInfo *info,
437 guint32 attr_id)
439 GFileAttribute *attrs;
440 int i;
442 i = g_file_info_find_place (info, attr_id);
443 attrs = (GFileAttribute *)info->attributes->data;
444 if (i < info->attributes->len &&
445 attrs[i].attribute == attr_id)
446 return &attrs[i].value;
448 return NULL;
451 static GFileAttributeValue *
452 g_file_info_find_value_by_name (GFileInfo *info,
453 const char *attribute)
455 guint32 attr_id;
457 attr_id = lookup_attribute (attribute);
458 return g_file_info_find_value (info, attr_id);
462 * g_file_info_has_attribute:
463 * @info: a #GFileInfo.
464 * @attribute: a file attribute key.
466 * Checks if a file info structure has an attribute named @attribute.
468 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
469 * %FALSE otherwise.
471 gboolean
472 g_file_info_has_attribute (GFileInfo *info,
473 const char *attribute)
475 GFileAttributeValue *value;
477 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
478 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
480 value = g_file_info_find_value_by_name (info, attribute);
481 return value != NULL;
485 * g_file_info_list_attributes:
486 * @info: a #GFileInfo.
487 * @name_space: a file attribute key's namespace.
489 * Lists the file info structure's attributes.
491 * Returns: a null-terminated array of strings of all of the
492 * possible attribute types for the given @name_space, or
493 * %NULL on error.
495 char **
496 g_file_info_list_attributes (GFileInfo *info,
497 const char *name_space)
499 GPtrArray *names;
500 GFileAttribute *attrs;
501 guint32 attribute;
502 guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
503 int i;
505 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
507 names = g_ptr_array_new ();
508 attrs = (GFileAttribute *)info->attributes->data;
509 for (i = 0; i < info->attributes->len; i++)
511 attribute = attrs[i].attribute;
512 if (ns_id == 0 || GET_NS (attribute) == ns_id)
513 g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
516 /* NULL terminate */
517 g_ptr_array_add (names, NULL);
519 return (char **)g_ptr_array_free (names, FALSE);
523 * g_file_info_get_attribute_type:
524 * @info: a #GFileInfo.
525 * @attribute: a file attribute key.
527 * Gets the attribute type for an attribute key.
529 * Returns: a #GFileAttributeType for the given @attribute, or
530 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is invalid.
532 GFileAttributeType
533 g_file_info_get_attribute_type (GFileInfo *info,
534 const char *attribute)
536 GFileAttributeValue *value;
538 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
539 g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);
541 value = g_file_info_find_value_by_name (info, attribute);
542 if (value)
543 return value->type;
544 else
545 return G_FILE_ATTRIBUTE_TYPE_INVALID;
549 * g_file_info_remove_attribute:
550 * @info: a #GFileInfo.
551 * @attribute: a file attribute key.
553 * Removes all cases of @attribute from @info if it exists.
555 void
556 g_file_info_remove_attribute (GFileInfo *info,
557 const char *attribute)
559 guint32 attr_id;
560 GFileAttribute *attrs;
561 int i;
563 g_return_if_fail (G_IS_FILE_INFO (info));
564 g_return_if_fail (attribute != NULL && *attribute != '\0');
566 attr_id = lookup_attribute (attribute);
568 i = g_file_info_find_place (info, attr_id);
569 attrs = (GFileAttribute *)info->attributes->data;
570 if (i < info->attributes->len &&
571 attrs[i].attribute == attr_id)
573 _g_file_attribute_value_clear (&attrs[i].value);
574 g_array_remove_index (info->attributes, i);
579 * g_file_info_get_attribute_data:
580 * @info: a #GFileInfo
581 * @attribute: a file attribute key
582 * @type: return location for the attribute type, or %NULL
583 * @value_pp: return location for the attribute value, or %NULL
584 * @status: return location for the attribute status, or %NULL
586 * Gets the attribute type, value and status for an attribute key.
588 * Returns: %TRUE if @info has an attribute named @attribute,
589 * %FALSE otherwise.
591 gboolean
592 g_file_info_get_attribute_data (GFileInfo *info,
593 const char *attribute,
594 GFileAttributeType *type,
595 gpointer *value_pp,
596 GFileAttributeStatus *status)
598 GFileAttributeValue *value;
600 value = g_file_info_find_value_by_name (info, attribute);
601 if (value == NULL)
602 return FALSE;
604 if (status)
605 *status = value->status;
607 if (type)
608 *type = value->type;
610 if (value_pp)
611 *value_pp = _g_file_attribute_value_peek_as_pointer (value);
613 return TRUE;
616 /**
617 * g_file_info_get_attribute_status:
618 * @info: a #GFileInfo
619 * @attribute: a file attribute key
621 * Gets the attribute status for an attribute key.
623 * Returns: a #GFileAttributeStatus for the given @attribute, or
624 * %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
627 GFileAttributeStatus
628 g_file_info_get_attribute_status (GFileInfo *info,
629 const char *attribute)
631 GFileAttributeValue *val;
633 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
634 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
636 val = g_file_info_find_value_by_name (info, attribute);
637 if (val)
638 return val->status;
640 return G_FILE_ATTRIBUTE_STATUS_UNSET;
644 GFileAttributeValue *
645 _g_file_info_get_attribute_value (GFileInfo *info,
646 const char *attribute)
649 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
650 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
652 return g_file_info_find_value_by_name (info, attribute);
656 * g_file_info_get_attribute_as_string:
657 * @info: a #GFileInfo.
658 * @attribute: a file attribute key.
660 * Gets the value of a attribute, formated as a string.
661 * This escapes things as needed to make the string valid
662 * utf8.
664 * Returns: a UTF-8 string associated with the given @attribute.
665 * When you're done with the string it must be freed with g_free().
667 char *
668 g_file_info_get_attribute_as_string (GFileInfo *info,
669 const char *attribute)
671 GFileAttributeValue *val;
672 val = _g_file_info_get_attribute_value (info, attribute);
673 if (val)
674 return _g_file_attribute_value_as_string (val);
675 return NULL;
680 * g_file_info_get_attribute_object:
681 * @info: a #GFileInfo.
682 * @attribute: a file attribute key.
684 * Gets the value of a #GObject attribute. If the attribute does
685 * not contain a #GObject, %NULL will be returned.
687 * Returns: a #GObject associated with the given @attribute, or
688 * %NULL otherwise.
690 GObject *
691 g_file_info_get_attribute_object (GFileInfo *info,
692 const char *attribute)
694 GFileAttributeValue *value;
696 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
697 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
699 value = g_file_info_find_value_by_name (info, attribute);
700 return _g_file_attribute_value_get_object (value);
704 * g_file_info_get_attribute_string:
705 * @info: a #GFileInfo.
706 * @attribute: a file attribute key.
708 * Gets the value of a string attribute. If the attribute does
709 * not contain a string, %NULL will be returned.
711 * Returns: the contents of the @attribute value as a string, or
712 * %NULL otherwise.
714 const char *
715 g_file_info_get_attribute_string (GFileInfo *info,
716 const char *attribute)
718 GFileAttributeValue *value;
720 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
721 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
723 value = g_file_info_find_value_by_name (info, attribute);
724 return _g_file_attribute_value_get_string (value);
728 * g_file_info_get_attribute_byte_string:
729 * @info: a #GFileInfo.
730 * @attribute: a file attribute key.
732 * Gets the value of a byte string attribute. If the attribute does
733 * not contain a byte string, %NULL will be returned.
735 * Returns: the contents of the @attribute value as a byte string, or
736 * %NULL otherwise.
738 const char *
739 g_file_info_get_attribute_byte_string (GFileInfo *info,
740 const char *attribute)
742 GFileAttributeValue *value;
744 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
745 g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);
747 value = g_file_info_find_value_by_name (info, attribute);
748 return _g_file_attribute_value_get_byte_string (value);
752 * g_file_info_get_attribute_boolean:
753 * @info: a #GFileInfo.
754 * @attribute: a file attribute key.
756 * Gets the value of a boolean attribute. If the attribute does not
757 * contain a boolean value, %FALSE will be returned.
759 * Returns: the boolean value contained within the attribute.
761 gboolean
762 g_file_info_get_attribute_boolean (GFileInfo *info,
763 const char *attribute)
765 GFileAttributeValue *value;
767 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
768 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
770 value = g_file_info_find_value_by_name (info, attribute);
771 return _g_file_attribute_value_get_boolean (value);
775 * g_file_info_get_attribute_uint32:
776 * @info: a #GFileInfo.
777 * @attribute: a file attribute key.
779 * Gets an unsigned 32-bit integer contained within the attribute. If the
780 * attribute does not contain an unsigned 32-bit integer, or is invalid,
781 * 0 will be returned.
783 * Returns: an unsigned 32-bit integer from the attribute.
785 guint32
786 g_file_info_get_attribute_uint32 (GFileInfo *info,
787 const char *attribute)
789 GFileAttributeValue *value;
791 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
792 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
794 value = g_file_info_find_value_by_name (info, attribute);
795 return _g_file_attribute_value_get_uint32 (value);
799 * g_file_info_get_attribute_int32:
800 * @info: a #GFileInfo.
801 * @attribute: a file attribute key.
803 * Gets a signed 32-bit integer contained within the attribute. If the
804 * attribute does not contain a signed 32-bit integer, or is invalid,
805 * 0 will be returned.
807 * Returns: a signed 32-bit integer from the attribute.
809 gint32
810 g_file_info_get_attribute_int32 (GFileInfo *info,
811 const char *attribute)
813 GFileAttributeValue *value;
815 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
816 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
818 value = g_file_info_find_value_by_name (info, attribute);
819 return _g_file_attribute_value_get_int32 (value);
823 * g_file_info_get_attribute_uint64:
824 * @info: a #GFileInfo.
825 * @attribute: a file attribute key.
827 * Gets a unsigned 64-bit integer contained within the attribute. If the
828 * attribute does not contain an unsigned 64-bit integer, or is invalid,
829 * 0 will be returned.
831 * Returns: a unsigned 64-bit integer from the attribute.
833 guint64
834 g_file_info_get_attribute_uint64 (GFileInfo *info,
835 const char *attribute)
837 GFileAttributeValue *value;
839 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
840 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
842 value = g_file_info_find_value_by_name (info, attribute);
843 return _g_file_attribute_value_get_uint64 (value);
847 * g_file_info_get_attribute_int64:
848 * @info: a #GFileInfo.
849 * @attribute: a file attribute key.
851 * Gets a signed 64-bit integer contained within the attribute. If the
852 * attribute does not contain an signed 64-bit integer, or is invalid,
853 * 0 will be returned.
855 * Returns: a signed 64-bit integer from the attribute.
857 gint64
858 g_file_info_get_attribute_int64 (GFileInfo *info,
859 const char *attribute)
861 GFileAttributeValue *value;
863 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
864 g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);
866 value = g_file_info_find_value_by_name (info, attribute);
867 return _g_file_attribute_value_get_int64 (value);
870 static GFileAttributeValue *
871 g_file_info_create_value (GFileInfo *info,
872 guint32 attr_id)
874 GFileAttribute *attrs;
875 int i;
877 if (info->mask != NO_ATTRIBUTE_MASK &&
878 !g_file_attribute_matcher_matches_id (info->mask, attr_id))
879 return NULL;
881 i = g_file_info_find_place (info, attr_id);
883 attrs = (GFileAttribute *)info->attributes->data;
884 if (i < info->attributes->len &&
885 attrs[i].attribute == attr_id)
886 return &attrs[i].value;
887 else
889 GFileAttribute attr = { 0 };
890 attr.attribute = attr_id;
891 g_array_insert_val (info->attributes, i, attr);
893 attrs = (GFileAttribute *)info->attributes->data;
894 return &attrs[i].value;
898 static GFileAttributeValue *
899 g_file_info_create_value_by_name (GFileInfo *info,
900 const char *attribute)
902 guint32 attr_id;
904 attr_id = lookup_attribute (attribute);
906 return g_file_info_create_value (info, attr_id);
910 * g_file_info_set_attribute:
911 * @info: a #GFileInfo.
912 * @attribute: a file attribute key.
913 * @type: a #GFileAttributeType
914 * @value_p: pointer to the value
916 * Sets the @attribute to contain the given value, if possible.
918 void
919 g_file_info_set_attribute (GFileInfo *info,
920 const char *attribute,
921 GFileAttributeType type,
922 gpointer value_p)
924 GFileAttributeValue *value;
926 g_return_if_fail (G_IS_FILE_INFO (info));
927 g_return_if_fail (attribute != NULL && *attribute != '\0');
929 value = g_file_info_create_value_by_name (info, attribute);
931 if (value)
932 _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
936 * g_file_info_set_attribute_object:
937 * @info: a #GFileInfo.
938 * @attribute: a file attribute key.
939 * @attr_value: a #GObject.
941 * Sets the @attribute to contain the given @attr_value,
942 * if possible.
944 void
945 g_file_info_set_attribute_object (GFileInfo *info,
946 const char *attribute,
947 GObject *attr_value)
949 GFileAttributeValue *value;
951 g_return_if_fail (G_IS_FILE_INFO (info));
952 g_return_if_fail (attribute != NULL && *attribute != '\0');
953 g_return_if_fail (G_IS_OBJECT (attr_value));
955 value = g_file_info_create_value_by_name (info, attribute);
956 if (value)
957 _g_file_attribute_value_set_object (value, attr_value);
961 * g_file_info_set_attribute_string:
962 * @info: a #GFileInfo.
963 * @attribute: a file attribute key.
964 * @attr_value: a string.
966 * Sets the @attribute to contain the given @attr_value,
967 * if possible.
969 void
970 g_file_info_set_attribute_string (GFileInfo *info,
971 const char *attribute,
972 const char *attr_value)
974 GFileAttributeValue *value;
976 g_return_if_fail (G_IS_FILE_INFO (info));
977 g_return_if_fail (attribute != NULL && *attribute != '\0');
978 g_return_if_fail (attr_value != NULL);
980 value = g_file_info_create_value_by_name (info, attribute);
981 if (value)
982 _g_file_attribute_value_set_string (value, attr_value);
986 * g_file_info_set_attribute_byte_string:
987 * @info: a #GFileInfo.
988 * @attribute: a file attribute key.
989 * @attr_value: a byte string.
991 * Sets the @attribute to contain the given @attr_value,
992 * if possible.
994 void
995 g_file_info_set_attribute_byte_string (GFileInfo *info,
996 const char *attribute,
997 const char *attr_value)
999 GFileAttributeValue *value;
1001 g_return_if_fail (G_IS_FILE_INFO (info));
1002 g_return_if_fail (attribute != NULL && *attribute != '\0');
1003 g_return_if_fail (attr_value != NULL);
1005 value = g_file_info_create_value_by_name (info, attribute);
1006 if (value)
1007 _g_file_attribute_value_set_byte_string (value, attr_value);
1011 * g_file_info_set_attribute_boolean:
1012 * @info: a #GFileInfo.
1013 * @attribute: a file attribute key.
1014 * @attr_value: a boolean value.
1016 * Sets the @attribute to contain the given @attr_value,
1017 * if possible.
1019 void
1020 g_file_info_set_attribute_boolean (GFileInfo *info,
1021 const char *attribute,
1022 gboolean attr_value)
1024 GFileAttributeValue *value;
1026 g_return_if_fail (G_IS_FILE_INFO (info));
1027 g_return_if_fail (attribute != NULL && *attribute != '\0');
1029 value = g_file_info_create_value_by_name (info, attribute);
1030 if (value)
1031 _g_file_attribute_value_set_boolean (value, attr_value);
1035 * g_file_info_set_attribute_uint32:
1036 * @info: a #GFileInfo.
1037 * @attribute: a file attribute key.
1038 * @attr_value: an unsigned 32-bit integer.
1040 * Sets the @attribute to contain the given @attr_value,
1041 * if possible.
1043 void
1044 g_file_info_set_attribute_uint32 (GFileInfo *info,
1045 const char *attribute,
1046 guint32 attr_value)
1048 GFileAttributeValue *value;
1050 g_return_if_fail (G_IS_FILE_INFO (info));
1051 g_return_if_fail (attribute != NULL && *attribute != '\0');
1053 value = g_file_info_create_value_by_name (info, attribute);
1054 if (value)
1055 _g_file_attribute_value_set_uint32 (value, attr_value);
1060 * g_file_info_set_attribute_int32:
1061 * @info: a #GFileInfo.
1062 * @attribute: a file attribute key.
1063 * @attr_value: a signed 32-bit integer
1065 * Sets the @attribute to contain the given @attr_value,
1066 * if possible.
1068 void
1069 g_file_info_set_attribute_int32 (GFileInfo *info,
1070 const char *attribute,
1071 gint32 attr_value)
1073 GFileAttributeValue *value;
1075 g_return_if_fail (G_IS_FILE_INFO (info));
1076 g_return_if_fail (attribute != NULL && *attribute != '\0');
1078 value = g_file_info_create_value_by_name (info, attribute);
1079 if (value)
1080 _g_file_attribute_value_set_int32 (value, attr_value);
1084 * g_file_info_set_attribute_uint64:
1085 * @info: a #GFileInfo.
1086 * @attribute: a file attribute key.
1087 * @attr_value: an unsigned 64-bit integer.
1089 * Sets the @attribute to contain the given @attr_value,
1090 * if possible.
1092 void
1093 g_file_info_set_attribute_uint64 (GFileInfo *info,
1094 const char *attribute,
1095 guint64 attr_value)
1097 GFileAttributeValue *value;
1099 g_return_if_fail (G_IS_FILE_INFO (info));
1100 g_return_if_fail (attribute != NULL && *attribute != '\0');
1102 value = g_file_info_create_value_by_name (info, attribute);
1103 if (value)
1104 _g_file_attribute_value_set_uint64 (value, attr_value);
1108 * g_file_info_set_attribute_int64:
1109 * @info: a #GFileInfo.
1110 * @attribute: attribute name to set.
1111 * @attr_value: int64 value to set attribute to.
1113 * Sets the @attribute to contain the given @attr_value,
1114 * if possible.
1117 void
1118 g_file_info_set_attribute_int64 (GFileInfo *info,
1119 const char *attribute,
1120 gint64 attr_value)
1122 GFileAttributeValue *value;
1124 g_return_if_fail (G_IS_FILE_INFO (info));
1125 g_return_if_fail (attribute != NULL && *attribute != '\0');
1127 value = g_file_info_create_value_by_name (info, attribute);
1128 if (value)
1129 _g_file_attribute_value_set_int64 (value, attr_value);
1132 /* Helper getters */
1134 * g_file_info_get_file_type:
1135 * @info: a #GFileInfo.
1137 * Gets a file's type (whether it is a regular file, symlink, etc).
1138 * This is different from the file's content type, see g_file_info_get_content_type().
1140 * Returns: a #GFileType for the given file.
1142 GFileType
1143 g_file_info_get_file_type (GFileInfo *info)
1145 static guint32 attr = 0;
1146 GFileAttributeValue *value;
1148 g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1150 if (attr == 0)
1151 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1153 value = g_file_info_find_value (info, attr);
1154 return (GFileType)_g_file_attribute_value_get_uint32 (value);
1158 * g_file_info_get_is_hidden:
1159 * @info: a #GFileInfo.
1161 * Checks if a file is hidden.
1163 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
1165 gboolean
1166 g_file_info_get_is_hidden (GFileInfo *info)
1168 static guint32 attr = 0;
1169 GFileAttributeValue *value;
1171 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1173 if (attr == 0)
1174 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1176 value = g_file_info_find_value (info, attr);
1177 return (GFileType)_g_file_attribute_value_get_boolean (value);
1181 * g_file_info_get_is_backup:
1182 * @info: a #GFileInfo.
1184 * Checks if a file is a backup file.
1186 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1188 gboolean
1189 g_file_info_get_is_backup (GFileInfo *info)
1191 static guint32 attr = 0;
1192 GFileAttributeValue *value;
1194 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1196 if (attr == 0)
1197 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1199 value = g_file_info_find_value (info, attr);
1200 return (GFileType)_g_file_attribute_value_get_boolean (value);
1204 * g_file_info_get_is_symlink:
1205 * @info: a #GFileInfo.
1207 * Checks if a file is a symlink.
1209 * Returns: %TRUE if the given @info is a symlink.
1211 gboolean
1212 g_file_info_get_is_symlink (GFileInfo *info)
1214 static guint32 attr = 0;
1215 GFileAttributeValue *value;
1217 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1219 if (attr == 0)
1220 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1222 value = g_file_info_find_value (info, attr);
1223 return (GFileType)_g_file_attribute_value_get_boolean (value);
1227 * g_file_info_get_name:
1228 * @info: a #GFileInfo.
1230 * Gets the name for a file.
1232 * Returns: a string containing the file name.
1234 const char *
1235 g_file_info_get_name (GFileInfo *info)
1237 static guint32 attr = 0;
1238 GFileAttributeValue *value;
1240 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1242 if (attr == 0)
1243 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1245 value = g_file_info_find_value (info, attr);
1246 return _g_file_attribute_value_get_byte_string (value);
1250 * g_file_info_get_display_name:
1251 * @info: a #GFileInfo.
1253 * Gets a display name for a file.
1255 * Returns: a string containing the display name.
1257 const char *
1258 g_file_info_get_display_name (GFileInfo *info)
1260 static guint32 attr = 0;
1261 GFileAttributeValue *value;
1263 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1265 if (attr == 0)
1266 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1268 value = g_file_info_find_value (info, attr);
1269 return _g_file_attribute_value_get_string (value);
1273 * g_file_info_get_edit_name:
1274 * @info: a #GFileInfo.
1276 * Gets the edit name for a file.
1278 * Returns: a string containing the edit name.
1280 const char *
1281 g_file_info_get_edit_name (GFileInfo *info)
1283 static guint32 attr = 0;
1284 GFileAttributeValue *value;
1286 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1288 if (attr == 0)
1289 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1291 value = g_file_info_find_value (info, attr);
1292 return _g_file_attribute_value_get_string (value);
1296 * g_file_info_get_icon:
1297 * @info: a #GFileInfo.
1299 * Gets the icon for a file.
1301 * Returns: #GIcon for the given @info.
1303 GIcon *
1304 g_file_info_get_icon (GFileInfo *info)
1306 static guint32 attr = 0;
1307 GFileAttributeValue *value;
1308 GObject *obj;
1310 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1312 if (attr == 0)
1313 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1315 value = g_file_info_find_value (info, attr);
1316 obj = _g_file_attribute_value_get_object (value);
1317 if (G_IS_ICON (obj))
1318 return G_ICON (obj);
1319 return NULL;
1323 * g_file_info_get_content_type:
1324 * @info: a #GFileInfo.
1326 * Gets the file's content type.
1328 * Returns: a string containing the file's content type.s
1330 const char *
1331 g_file_info_get_content_type (GFileInfo *info)
1333 static guint32 attr = 0;
1334 GFileAttributeValue *value;
1336 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1338 if (attr == 0)
1339 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1341 value = g_file_info_find_value (info, attr);
1342 return _g_file_attribute_value_get_string (value);
1346 * g_file_info_get_size:
1347 * @info: a #GFileInfo.
1349 * Gets the file's size.
1351 * Returns: a #goffset containing the file's size.
1353 goffset
1354 g_file_info_get_size (GFileInfo *info)
1356 static guint32 attr = 0;
1357 GFileAttributeValue *value;
1359 g_return_val_if_fail (G_IS_FILE_INFO (info), (goffset) 0);
1361 if (attr == 0)
1362 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1364 value = g_file_info_find_value (info, attr);
1365 return (goffset) _g_file_attribute_value_get_uint64 (value);
1369 * g_file_info_get_modification_time:
1370 * @info: a #GFileInfo.
1371 * @result: a #GTimeVal.
1373 * Gets the modification time of the current @info and sets it
1374 * in @result.
1376 void
1377 g_file_info_get_modification_time (GFileInfo *info,
1378 GTimeVal *result)
1380 static guint32 attr_mtime = 0, attr_mtime_usec;
1381 GFileAttributeValue *value;
1383 g_return_if_fail (G_IS_FILE_INFO (info));
1384 g_return_if_fail (result != NULL);
1386 if (attr_mtime == 0)
1388 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1389 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1392 value = g_file_info_find_value (info, attr_mtime);
1393 result->tv_sec = _g_file_attribute_value_get_uint64 (value);
1394 value = g_file_info_find_value (info, attr_mtime_usec);
1395 result->tv_usec = _g_file_attribute_value_get_uint32 (value);
1399 * g_file_info_get_symlink_target:
1400 * @info: a #GFileInfo.
1402 * Gets the symlink target for a given #GFileInfo.
1404 * Returns: a string containing the symlink target.
1406 const char *
1407 g_file_info_get_symlink_target (GFileInfo *info)
1409 static guint32 attr = 0;
1410 GFileAttributeValue *value;
1412 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1414 if (attr == 0)
1415 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1417 value = g_file_info_find_value (info, attr);
1418 return _g_file_attribute_value_get_byte_string (value);
1422 * g_file_info_get_etag:
1423 * @info: a #GFileInfo.
1425 * Gets the <link linkend="gfile-etag">entity tag</link> for a given
1426 * #GFileInfo. See %G_FILE_ATTRIBUTE_ETAG_VALUE.
1428 * Returns: a string containing the value of the "etag:value" attribute.
1430 const char *
1431 g_file_info_get_etag (GFileInfo *info)
1433 static guint32 attr = 0;
1434 GFileAttributeValue *value;
1436 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1438 if (attr == 0)
1439 attr = lookup_attribute (G_FILE_ATTRIBUTE_ETAG_VALUE);
1441 value = g_file_info_find_value (info, attr);
1442 return _g_file_attribute_value_get_string (value);
1446 * g_file_info_get_sort_order:
1447 * @info: a #GFileInfo.
1449 * Gets the value of the sort_order attribute from the #GFileInfo.
1450 * See %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1452 * Returns: a #gint32 containing the value of the "standard::sort_order" attribute.
1454 gint32
1455 g_file_info_get_sort_order (GFileInfo *info)
1457 static guint32 attr = 0;
1458 GFileAttributeValue *value;
1460 g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
1462 if (attr == 0)
1463 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1465 value = g_file_info_find_value (info, attr);
1466 return _g_file_attribute_value_get_int32 (value);
1469 /* Helper setters: */
1471 * g_file_info_set_file_type:
1472 * @info: a #GFileInfo.
1473 * @type: a #GFileType.
1475 * Sets the file type in a #GFileInfo to @type.
1476 * See %G_FILE_ATTRIBUTE_STANDARD_TYPE.
1478 void
1479 g_file_info_set_file_type (GFileInfo *info,
1480 GFileType type)
1482 static guint32 attr = 0;
1483 GFileAttributeValue *value;
1485 g_return_if_fail (G_IS_FILE_INFO (info));
1487 if (attr == 0)
1488 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1490 value = g_file_info_create_value (info, attr);
1491 if (value)
1492 _g_file_attribute_value_set_uint32 (value, type);
1496 * g_file_info_set_is_hidden:
1497 * @info: a #GFileInfo.
1498 * @is_hidden: a #gboolean.
1500 * Sets the "is_hidden" attribute in a #GFileInfo according to @is_symlink.
1501 * See %G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN.
1503 void
1504 g_file_info_set_is_hidden (GFileInfo *info,
1505 gboolean is_hidden)
1507 static guint32 attr = 0;
1508 GFileAttributeValue *value;
1510 g_return_if_fail (G_IS_FILE_INFO (info));
1512 if (attr == 0)
1513 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1515 value = g_file_info_create_value (info, attr);
1516 if (value)
1517 _g_file_attribute_value_set_boolean (value, is_hidden);
1521 * g_file_info_set_is_symlink:
1522 * @info: a #GFileInfo.
1523 * @is_symlink: a #gboolean.
1525 * Sets the "is_symlink" attribute in a #GFileInfo according to @is_symlink.
1526 * See %G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK.
1528 void
1529 g_file_info_set_is_symlink (GFileInfo *info,
1530 gboolean is_symlink)
1532 static guint32 attr = 0;
1533 GFileAttributeValue *value;
1535 g_return_if_fail (G_IS_FILE_INFO (info));
1537 if (attr == 0)
1538 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1540 value = g_file_info_create_value (info, attr);
1541 if (value)
1542 _g_file_attribute_value_set_boolean (value, is_symlink);
1546 * g_file_info_set_name:
1547 * @info: a #GFileInfo.
1548 * @name: a string containing a name.
1550 * Sets the name attribute for the current #GFileInfo.
1551 * See %G_FILE_ATTRIBUTE_STANDARD_NAME.
1553 void
1554 g_file_info_set_name (GFileInfo *info,
1555 const char *name)
1557 static guint32 attr = 0;
1558 GFileAttributeValue *value;
1560 g_return_if_fail (G_IS_FILE_INFO (info));
1561 g_return_if_fail (name != NULL);
1563 if (attr == 0)
1564 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1566 value = g_file_info_create_value (info, attr);
1567 if (value)
1568 _g_file_attribute_value_set_byte_string (value, name);
1572 * g_file_info_set_display_name:
1573 * @info: a #GFileInfo.
1574 * @display_name: a string containing a display name.
1576 * Sets the display name for the current #GFileInfo.
1577 * See %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME.
1579 void
1580 g_file_info_set_display_name (GFileInfo *info,
1581 const char *display_name)
1583 static guint32 attr = 0;
1584 GFileAttributeValue *value;
1586 g_return_if_fail (G_IS_FILE_INFO (info));
1587 g_return_if_fail (display_name != NULL);
1589 if (attr == 0)
1590 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1592 value = g_file_info_create_value (info, attr);
1593 if (value)
1594 _g_file_attribute_value_set_string (value, display_name);
1598 * g_file_info_set_edit_name:
1599 * @info: a #GFileInfo.
1600 * @edit_name: a string containing an edit name.
1602 * Sets the edit name for the current file.
1603 * See %G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME.
1605 void
1606 g_file_info_set_edit_name (GFileInfo *info,
1607 const char *edit_name)
1609 static guint32 attr = 0;
1610 GFileAttributeValue *value;
1612 g_return_if_fail (G_IS_FILE_INFO (info));
1613 g_return_if_fail (edit_name != NULL);
1615 if (attr == 0)
1616 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
1618 value = g_file_info_create_value (info, attr);
1619 if (value)
1620 _g_file_attribute_value_set_string (value, edit_name);
1624 * g_file_info_set_icon:
1625 * @info: a #GFileInfo.
1626 * @icon: a #GIcon.
1628 * Sets the icon for a given #GFileInfo.
1629 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1631 void
1632 g_file_info_set_icon (GFileInfo *info,
1633 GIcon *icon)
1635 static guint32 attr = 0;
1636 GFileAttributeValue *value;
1638 g_return_if_fail (G_IS_FILE_INFO (info));
1639 g_return_if_fail (G_IS_ICON (icon));
1641 if (attr == 0)
1642 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON);
1644 value = g_file_info_create_value (info, attr);
1645 if (value)
1646 _g_file_attribute_value_set_object (value, G_OBJECT (icon));
1650 * g_file_info_set_content_type:
1651 * @info: a #GFileInfo.
1652 * @content_type: a content type. See #GContentType.
1654 * Sets the content type attribute for a given #GFileInfo.
1655 * See %G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE.
1657 void
1658 g_file_info_set_content_type (GFileInfo *info,
1659 const char *content_type)
1661 static guint32 attr = 0;
1662 GFileAttributeValue *value;
1664 g_return_if_fail (G_IS_FILE_INFO (info));
1665 g_return_if_fail (content_type != NULL);
1667 if (attr == 0)
1668 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
1670 value = g_file_info_create_value (info, attr);
1671 if (value)
1672 _g_file_attribute_value_set_string (value, content_type);
1676 * g_file_info_set_size:
1677 * @info: a #GFileInfo.
1678 * @size: a #goffset containing the file's size.
1680 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SIZE attribute in the file info
1681 * to the given size.
1683 void
1684 g_file_info_set_size (GFileInfo *info,
1685 goffset size)
1687 static guint32 attr = 0;
1688 GFileAttributeValue *value;
1690 g_return_if_fail (G_IS_FILE_INFO (info));
1692 if (attr == 0)
1693 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE);
1695 value = g_file_info_create_value (info, attr);
1696 if (value)
1697 _g_file_attribute_value_set_uint64 (value, size);
1701 * g_file_info_set_modification_time
1702 * @info: a #GFileInfo.
1703 * @mtime: a #GTimeVal.
1705 * Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
1706 * info to the given time value.
1708 void
1709 g_file_info_set_modification_time (GFileInfo *info,
1710 GTimeVal *mtime)
1712 static guint32 attr_mtime = 0, attr_mtime_usec;
1713 GFileAttributeValue *value;
1715 g_return_if_fail (G_IS_FILE_INFO (info));
1716 g_return_if_fail (mtime != NULL);
1718 if (attr_mtime == 0)
1720 attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
1721 attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
1724 value = g_file_info_create_value (info, attr_mtime);
1725 if (value)
1726 _g_file_attribute_value_set_uint64 (value, mtime->tv_sec);
1727 value = g_file_info_create_value (info, attr_mtime_usec);
1728 if (value)
1729 _g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
1733 * g_file_info_set_symlink_target:
1734 * @info: a #GFileInfo.
1735 * @symlink_target: a static string containing a path to a symlink target.
1737 * Sets the %G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET attribute in the file info
1738 * to the given symlink target.
1740 void
1741 g_file_info_set_symlink_target (GFileInfo *info,
1742 const char *symlink_target)
1744 static guint32 attr = 0;
1745 GFileAttributeValue *value;
1747 g_return_if_fail (G_IS_FILE_INFO (info));
1748 g_return_if_fail (symlink_target != NULL);
1750 if (attr == 0)
1751 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
1753 value = g_file_info_create_value (info, attr);
1754 if (value)
1755 _g_file_attribute_value_set_byte_string (value, symlink_target);
1759 * g_file_info_set_sort_order:
1760 * @info: a #GFileInfo.
1761 * @sort_order: a sort order integer.
1763 * Sets the sort order attribute in the file info structure. See
1764 * %G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER.
1766 void
1767 g_file_info_set_sort_order (GFileInfo *info,
1768 gint32 sort_order)
1770 static guint32 attr = 0;
1771 GFileAttributeValue *value;
1773 g_return_if_fail (G_IS_FILE_INFO (info));
1775 if (attr == 0)
1776 attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
1778 value = g_file_info_create_value (info, attr);
1779 if (value)
1780 _g_file_attribute_value_set_int32 (value, sort_order);
1784 #define ON_STACK_MATCHERS 5
1786 typedef struct {
1787 guint32 id;
1788 guint32 mask;
1789 } SubMatcher;
1791 struct _GFileAttributeMatcher {
1792 gboolean all;
1793 SubMatcher sub_matchers[ON_STACK_MATCHERS];
1794 GArray *more_sub_matchers;
1796 /* Interator */
1797 guint32 iterator_ns;
1798 int iterator_pos;
1799 int ref;
1802 static void
1803 matcher_add (GFileAttributeMatcher *matcher,
1804 guint id,
1805 guint mask)
1807 SubMatcher *sub_matchers;
1808 int i;
1809 SubMatcher s;
1811 for (i = 0; i < ON_STACK_MATCHERS; i++)
1813 /* First empty spot, not found, use this */
1814 if (matcher->sub_matchers[i].id == 0)
1816 matcher->sub_matchers[i].id = id;
1817 matcher->sub_matchers[i].mask = mask;
1818 return;
1821 /* Already added */
1822 if (matcher->sub_matchers[i].id == id &&
1823 matcher->sub_matchers[i].mask == mask)
1824 return;
1827 if (matcher->more_sub_matchers == NULL)
1828 matcher->more_sub_matchers = g_array_new (FALSE, FALSE, sizeof (SubMatcher));
1830 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
1831 for (i = 0; i < matcher->more_sub_matchers->len; i++)
1833 /* Already added */
1834 if (sub_matchers[i].id == id &&
1835 sub_matchers[i].mask == mask)
1836 return;
1839 s.id = id;
1840 s.mask = mask;
1842 g_array_append_val (matcher->more_sub_matchers, s);
1846 * g_file_attribute_matcher_new:
1847 * @attributes: an attribute string to match.
1849 * Creates a new file attribute matcher, which matches attributes
1850 * against a given string. #GFileAttributeMatcher<!-- -->s are reference
1851 * counted structures, and are created with a reference count of 1. If
1852 * the number of references falls to 0, the #GFileAttributeMatcher is
1853 * automatically destroyed.
1855 * The @attribute string should be formatted with specific keys separated
1856 * from namespaces with a double colon. Several "namespace::key" strings may be
1857 * concatenated with a single comma (e.g. "standard::type,standard::is-hidden").
1858 * The wildcard "*" may be used to match all keys and namespaces, or
1859 * "namespace::*" will match all keys in a given namespace.
1861 * Examples of strings to use:
1862 * <table>
1863 * <title>File Attribute Matcher strings and results</title>
1864 * <tgroup cols='2' align='left'><thead>
1865 * <row><entry> Matcher String </entry><entry> Matches </entry></row></thead>
1866 * <tbody>
1867 * <row><entry>"*"</entry><entry>matches all attributes.</entry></row>
1868 * <row><entry>"standard::is-hidden"</entry><entry>matches only the key is-hidden in the standard namespace.</entry></row>
1869 * <row><entry>"standard::type,unix::*"</entry><entry>matches the type key in the standard namespace and
1870 * all keys in the unix namespace.</entry></row>
1871 * </tbody></tgroup>
1872 * </table>
1874 * Returns: a #GFileAttributeMatcher.
1876 GFileAttributeMatcher *
1877 g_file_attribute_matcher_new (const char *attributes)
1879 char **split;
1880 char *colon;
1881 int i;
1882 GFileAttributeMatcher *matcher;
1884 if (attributes == NULL || *attributes == '\0')
1885 return NULL;
1887 matcher = g_malloc0 (sizeof (GFileAttributeMatcher));
1888 matcher->ref = 1;
1890 split = g_strsplit (attributes, ",", -1);
1892 for (i = 0; split[i] != NULL; i++)
1894 if (strcmp (split[i], "*") == 0)
1895 matcher->all = TRUE;
1896 else
1898 guint32 id, mask;
1900 colon = strstr (split[i], "::");
1901 if (colon != NULL &&
1902 !(colon[2] == 0 ||
1903 (colon[2] == '*' &&
1904 colon[3] == 0)))
1906 id = lookup_attribute (split[i]);
1907 mask = 0xffffffff;
1909 else
1911 if (colon)
1912 *colon = 0;
1914 id = lookup_namespace (split[i]) << NS_POS;
1915 mask = NS_MASK << NS_POS;
1918 matcher_add (matcher, id, mask);
1922 g_strfreev (split);
1924 return matcher;
1928 * g_file_attribute_matcher_ref:
1929 * @matcher: a #GFileAttributeMatcher.
1931 * References a file attribute matcher.
1933 * Returns: a #GFileAttributeMatcher.
1935 GFileAttributeMatcher *
1936 g_file_attribute_matcher_ref (GFileAttributeMatcher *matcher)
1938 if (matcher)
1940 g_return_val_if_fail (matcher->ref > 0, NULL);
1941 g_atomic_int_inc (&matcher->ref);
1943 return matcher;
1947 * g_file_attribute_matcher_unref:
1948 * @matcher: a #GFileAttributeMatcher.
1950 * Unreferences @matcher. If the reference count falls below 1,
1951 * the @matcher is automatically freed.
1954 void
1955 g_file_attribute_matcher_unref (GFileAttributeMatcher *matcher)
1957 if (matcher)
1959 g_return_if_fail (matcher->ref > 0);
1961 if (g_atomic_int_dec_and_test (&matcher->ref))
1963 if (matcher->more_sub_matchers)
1964 g_array_free (matcher->more_sub_matchers, TRUE);
1966 g_free (matcher);
1972 * g_file_attribute_matcher_matches_only:
1973 * @matcher: a #GFileAttributeMatcher.
1974 * @attribute: a file attribute key.
1976 * Checks if a attribute matcher only matches a given attribute. Always
1977 * returns %FALSE if "*" was used when creating the matcher.
1979 * Returns: %TRUE if the matcher only matches @attribute. %FALSE otherwise.
1981 gboolean
1982 g_file_attribute_matcher_matches_only (GFileAttributeMatcher *matcher,
1983 const char *attribute)
1985 guint32 id;
1987 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
1989 if (matcher == NULL ||
1990 matcher->all)
1991 return FALSE;
1993 id = lookup_attribute (attribute);
1995 if (matcher->sub_matchers[0].id != 0 &&
1996 matcher->sub_matchers[1].id == 0 &&
1997 matcher->sub_matchers[0].mask == 0xffffffff &&
1998 matcher->sub_matchers[0].id == id)
1999 return TRUE;
2001 return FALSE;
2004 static gboolean
2005 matcher_matches_id (GFileAttributeMatcher *matcher,
2006 guint32 id)
2008 SubMatcher *sub_matchers;
2009 int i;
2011 for (i = 0; i < ON_STACK_MATCHERS; i++)
2013 if (matcher->sub_matchers[i].id == 0)
2014 return FALSE;
2016 if (matcher->sub_matchers[i].id == (id & matcher->sub_matchers[i].mask))
2017 return TRUE;
2020 if (matcher->more_sub_matchers)
2022 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2023 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2025 if (sub_matchers[i].id == (id & sub_matchers[i].mask))
2026 return TRUE;
2030 return FALSE;
2033 static gboolean
2034 g_file_attribute_matcher_matches_id (GFileAttributeMatcher *matcher,
2035 guint32 id)
2037 /* We return a NULL matcher for an empty match string, so handle this */
2038 if (matcher == NULL)
2039 return FALSE;
2041 if (matcher->all)
2042 return TRUE;
2044 return matcher_matches_id (matcher, id);
2048 * g_file_attribute_matcher_matches:
2049 * @matcher: a #GFileAttributeMatcher.
2050 * @attribute: a file attribute key.
2052 * Checks if an attribute will be matched by an attribute matcher. If
2053 * the matcher was created with the "*" matching string, this function
2054 * will always return %TRUE.
2056 * Returns: %TRUE if @attribute matches @matcher. %FALSE otherwise.
2058 gboolean
2059 g_file_attribute_matcher_matches (GFileAttributeMatcher *matcher,
2060 const char *attribute)
2062 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2064 /* We return a NULL matcher for an empty match string, so handle this */
2065 if (matcher == NULL)
2066 return FALSE;
2068 if (matcher->all)
2069 return TRUE;
2071 return matcher_matches_id (matcher, lookup_attribute (attribute));
2074 /* return TRUE -> all */
2076 * g_file_attribute_matcher_enumerate_namespace:
2077 * @matcher: a #GFileAttributeMatcher.
2078 * @ns: a string containing a file attribute namespace.
2080 * Checks if the matcher will match all of the keys in a given namespace.
2081 * This will always return %TRUE if a wildcard character is in use (e.g. if
2082 * matcher was created with "standard::*" and @ns is "standard", or if matcher was created
2083 * using "*" and namespace is anything.)
2085 * TODO: this is awkwardly worded.
2087 * Returns: %TRUE if the matcher matches all of the entries
2088 * in the given @ns, %FALSE otherwise.
2090 gboolean
2091 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher *matcher,
2092 const char *ns)
2094 SubMatcher *sub_matchers;
2095 int ns_id;
2096 int i;
2098 g_return_val_if_fail (ns != NULL && *ns != '\0', FALSE);
2100 /* We return a NULL matcher for an empty match string, so handle this */
2101 if (matcher == NULL)
2102 return FALSE;
2104 if (matcher->all)
2105 return TRUE;
2107 ns_id = lookup_namespace (ns) << NS_POS;
2109 for (i = 0; i < ON_STACK_MATCHERS; i++)
2111 if (matcher->sub_matchers[i].id == ns_id)
2112 return TRUE;
2115 if (matcher->more_sub_matchers)
2117 sub_matchers = (SubMatcher *)matcher->more_sub_matchers->data;
2118 for (i = 0; i < matcher->more_sub_matchers->len; i++)
2120 if (sub_matchers[i].id == ns_id)
2121 return TRUE;
2125 matcher->iterator_ns = ns_id;
2126 matcher->iterator_pos = 0;
2128 return FALSE;
2132 * g_file_attribute_matcher_enumerate_next:
2133 * @matcher: a #GFileAttributeMatcher.
2135 * Gets the next matched attribute from a #GFileAttributeMatcher.
2137 * Returns: a string containing the next attribute or %NULL if
2138 * no more attribute exist.
2140 const char *
2141 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher *matcher)
2143 int i;
2144 SubMatcher *sub_matcher;
2146 /* We return a NULL matcher for an empty match string, so handle this */
2147 if (matcher == NULL)
2148 return NULL;
2150 while (1)
2152 i = matcher->iterator_pos++;
2154 if (i < ON_STACK_MATCHERS)
2156 if (matcher->sub_matchers[i].id == 0)
2157 return NULL;
2159 sub_matcher = &matcher->sub_matchers[i];
2161 else
2163 if (matcher->more_sub_matchers == NULL)
2164 return NULL;
2166 i -= ON_STACK_MATCHERS;
2167 if (i < matcher->more_sub_matchers->len)
2168 sub_matcher = &g_array_index (matcher->more_sub_matchers, SubMatcher, i);
2169 else
2170 return NULL;
2173 if (sub_matcher->mask == 0xffffffff &&
2174 (sub_matcher->id & (NS_MASK << NS_POS)) == matcher->iterator_ns)
2175 return get_attribute_for_id (sub_matcher->id);
2179 #define __G_FILE_INFO_C__
2180 #include "gioaliasdef.c"