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>
25 * @short_description: File Information and Attributes
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
59 #include "gfileinfo.h"
60 #include "gfileattribute-priv.h"
66 /* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
67 #define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)
71 GFileAttributeValue value
;
76 GObject parent_instance
;
79 GFileAttributeMatcher
*mask
;
82 struct _GFileInfoClass
84 GObjectClass parent_class
;
88 static gboolean
g_file_attribute_matcher_matches_id (GFileAttributeMatcher
*matcher
,
91 G_DEFINE_TYPE (GFileInfo
, g_file_info
, G_TYPE_OBJECT
);
95 guint32 attribute_id_counter
;
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 * |------------|--------------------|
107 * namespace attribute id
109 * This way the attributes gets sorted in namespace order
113 #define NS_MASK ((guint32)((1<<12) - 1))
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) )
127 _lookup_namespace (const char *namespace)
131 ns_info
= g_hash_table_lookup (ns_hash
, namespace);
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
;
144 lookup_namespace (const char *namespace)
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);
162 G_UNLOCK (attribute_hash
);
168 get_attribute_for_id (int attribute
)
171 G_LOCK (attribute_hash
);
172 s
= attributes
[GET_NS(attribute
)][GET_ID(attribute
)];
173 G_UNLOCK (attribute_hash
);
178 lookup_attribute (const char *attribute
)
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
));
196 G_UNLOCK (attribute_hash
);
200 colon
= strstr (attribute
, "::");
202 ns
= g_strndup (attribute
, colon
- attribute
);
206 ns_info
= _lookup_namespace (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
);
223 g_file_info_finalize (GObject
*object
)
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
);
243 g_file_info_class_init (GFileInfoClass
*klass
)
245 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
247 gobject_class
->finalize
= g_file_info_finalize
;
251 g_file_info_init (GFileInfo
*info
)
253 info
->mask
= NO_ATTRIBUTE_MASK
;
254 info
->attributes
= g_array_new (FALSE
, FALSE
,
255 sizeof (GFileAttribute
));
261 * Creates a new file info structure.
263 * Returns: a #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.
279 g_file_info_copy_into (GFileInfo
*src_info
,
280 GFileInfo
*dest_info
)
282 GFileAttribute
*source
, *dest
;
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
;
308 dest_info
->mask
= g_file_attribute_matcher_ref (src_info
->mask
);
313 * @other: a #GFileInfo.
315 * Duplicates a file info structure.
317 * Returns: a duplicate #GFileInfo of @other.
320 g_file_info_dup (GFileInfo
*other
)
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);
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.
339 g_file_info_set_attribute_mask (GFileInfo
*info
,
340 GFileAttributeMatcher
*mask
)
342 GFileAttribute
*attr
;
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
,
360 _g_file_attribute_value_clear (&attr
->value
);
361 g_array_remove_index (info
->attributes
, i
);
369 * g_file_info_unset_attribute_mask:
372 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
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.
392 g_file_info_clear_status (GFileInfo
*info
)
394 GFileAttribute
*attrs
;
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
;
405 g_file_info_find_place (GFileInfo
*info
,
409 GFileAttribute
*attrs
;
410 /* Binary search for the place where attribute would be, if its
414 max
= info
->attributes
->len
;
416 attrs
= (GFileAttribute
*)info
->attributes
->data
;
420 med
= min
+ (max
- min
) / 2;
421 if (attrs
[med
].attribute
== attribute
)
426 else if (attrs
[med
].attribute
< attribute
)
428 else /* attrs[med].attribute > attribute */
435 static GFileAttributeValue
*
436 g_file_info_find_value (GFileInfo
*info
,
439 GFileAttribute
*attrs
;
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
;
451 static GFileAttributeValue
*
452 g_file_info_find_value_by_name (GFileInfo
*info
,
453 const char *attribute
)
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,
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
496 g_file_info_list_attributes (GFileInfo
*info
,
497 const char *name_space
)
500 GFileAttribute
*attrs
;
502 guint32 ns_id
= (name_space
) ? lookup_namespace (name_space
) : 0;
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
)));
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.
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
);
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.
556 g_file_info_remove_attribute (GFileInfo
*info
,
557 const char *attribute
)
560 GFileAttribute
*attrs
;
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,
592 g_file_info_get_attribute_data (GFileInfo
*info
,
593 const char *attribute
,
594 GFileAttributeType
*type
,
596 GFileAttributeStatus
*status
)
598 GFileAttributeValue
*value
;
600 value
= g_file_info_find_value_by_name (info
, attribute
);
605 *status
= value
->status
;
611 *value_pp
= _g_file_attribute_value_peek_as_pointer (value
);
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.
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
);
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
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().
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
);
674 return _g_file_attribute_value_as_string (val
);
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
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
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
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.
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.
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.
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.
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.
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
,
874 GFileAttribute
*attrs
;
877 if (info
->mask
!= NO_ATTRIBUTE_MASK
&&
878 !g_file_attribute_matcher_matches_id (info
->mask
, attr_id
))
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
;
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
)
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.
919 g_file_info_set_attribute (GFileInfo
*info
,
920 const char *attribute
,
921 GFileAttributeType type
,
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
);
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,
945 g_file_info_set_attribute_object (GFileInfo
*info
,
946 const char *attribute
,
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
);
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,
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
);
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,
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
);
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,
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
);
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,
1044 g_file_info_set_attribute_uint32 (GFileInfo
*info
,
1045 const char *attribute
,
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
);
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,
1069 g_file_info_set_attribute_int32 (GFileInfo
*info
,
1070 const char *attribute
,
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
);
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,
1093 g_file_info_set_attribute_uint64 (GFileInfo
*info
,
1094 const char *attribute
,
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
);
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,
1118 g_file_info_set_attribute_int64 (GFileInfo
*info
,
1119 const char *attribute
,
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
);
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.
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
);
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.
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
);
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.
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
);
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.
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
);
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.
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
);
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.
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
);
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.
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
);
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.
1304 g_file_info_get_icon (GFileInfo
*info
)
1306 static guint32 attr
= 0;
1307 GFileAttributeValue
*value
;
1310 g_return_val_if_fail (G_IS_FILE_INFO (info
), NULL
);
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
);
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
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
);
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.
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);
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
1377 g_file_info_get_modification_time (GFileInfo
*info
,
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.
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
);
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.
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
);
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.
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);
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.
1479 g_file_info_set_file_type (GFileInfo
*info
,
1482 static guint32 attr
= 0;
1483 GFileAttributeValue
*value
;
1485 g_return_if_fail (G_IS_FILE_INFO (info
));
1488 attr
= lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE
);
1490 value
= g_file_info_create_value (info
, attr
);
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.
1504 g_file_info_set_is_hidden (GFileInfo
*info
,
1507 static guint32 attr
= 0;
1508 GFileAttributeValue
*value
;
1510 g_return_if_fail (G_IS_FILE_INFO (info
));
1513 attr
= lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN
);
1515 value
= g_file_info_create_value (info
, attr
);
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.
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
));
1538 attr
= lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK
);
1540 value
= g_file_info_create_value (info
, attr
);
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.
1554 g_file_info_set_name (GFileInfo
*info
,
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
);
1564 attr
= lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME
);
1566 value
= g_file_info_create_value (info
, attr
);
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.
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
);
1590 attr
= lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
);
1592 value
= g_file_info_create_value (info
, attr
);
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.
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
);
1616 attr
= lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME
);
1618 value
= g_file_info_create_value (info
, attr
);
1620 _g_file_attribute_value_set_string (value
, edit_name
);
1624 * g_file_info_set_icon:
1625 * @info: a #GFileInfo.
1628 * Sets the icon for a given #GFileInfo.
1629 * See %G_FILE_ATTRIBUTE_STANDARD_ICON.
1632 g_file_info_set_icon (GFileInfo
*info
,
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
));
1642 attr
= lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_ICON
);
1644 value
= g_file_info_create_value (info
, attr
);
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.
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
);
1668 attr
= lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
);
1670 value
= g_file_info_create_value (info
, attr
);
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.
1684 g_file_info_set_size (GFileInfo
*info
,
1687 static guint32 attr
= 0;
1688 GFileAttributeValue
*value
;
1690 g_return_if_fail (G_IS_FILE_INFO (info
));
1693 attr
= lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SIZE
);
1695 value
= g_file_info_create_value (info
, attr
);
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.
1709 g_file_info_set_modification_time (GFileInfo
*info
,
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
);
1726 _g_file_attribute_value_set_uint64 (value
, mtime
->tv_sec
);
1727 value
= g_file_info_create_value (info
, attr_mtime_usec
);
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.
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
);
1751 attr
= lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET
);
1753 value
= g_file_info_create_value (info
, attr
);
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.
1767 g_file_info_set_sort_order (GFileInfo
*info
,
1770 static guint32 attr
= 0;
1771 GFileAttributeValue
*value
;
1773 g_return_if_fail (G_IS_FILE_INFO (info
));
1776 attr
= lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER
);
1778 value
= g_file_info_create_value (info
, attr
);
1780 _g_file_attribute_value_set_int32 (value
, sort_order
);
1784 #define ON_STACK_MATCHERS 5
1791 struct _GFileAttributeMatcher
{
1793 SubMatcher sub_matchers
[ON_STACK_MATCHERS
];
1794 GArray
*more_sub_matchers
;
1797 guint32 iterator_ns
;
1803 matcher_add (GFileAttributeMatcher
*matcher
,
1807 SubMatcher
*sub_matchers
;
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
;
1822 if (matcher
->sub_matchers
[i
].id
== id
&&
1823 matcher
->sub_matchers
[i
].mask
== mask
)
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
++)
1834 if (sub_matchers
[i
].id
== id
&&
1835 sub_matchers
[i
].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:
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>
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>
1874 * Returns: a #GFileAttributeMatcher.
1876 GFileAttributeMatcher
*
1877 g_file_attribute_matcher_new (const char *attributes
)
1882 GFileAttributeMatcher
*matcher
;
1884 if (attributes
== NULL
|| *attributes
== '\0')
1887 matcher
= g_malloc0 (sizeof (GFileAttributeMatcher
));
1890 split
= g_strsplit (attributes
, ",", -1);
1892 for (i
= 0; split
[i
] != NULL
; i
++)
1894 if (strcmp (split
[i
], "*") == 0)
1895 matcher
->all
= TRUE
;
1900 colon
= strstr (split
[i
], "::");
1901 if (colon
!= NULL
&&
1906 id
= lookup_attribute (split
[i
]);
1914 id
= lookup_namespace (split
[i
]) << NS_POS
;
1915 mask
= NS_MASK
<< NS_POS
;
1918 matcher_add (matcher
, id
, mask
);
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
)
1940 g_return_val_if_fail (matcher
->ref
> 0, NULL
);
1941 g_atomic_int_inc (&matcher
->ref
);
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.
1955 g_file_attribute_matcher_unref (GFileAttributeMatcher
*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
);
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.
1982 g_file_attribute_matcher_matches_only (GFileAttributeMatcher
*matcher
,
1983 const char *attribute
)
1987 g_return_val_if_fail (attribute
!= NULL
&& *attribute
!= '\0', FALSE
);
1989 if (matcher
== NULL
||
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
)
2005 matcher_matches_id (GFileAttributeMatcher
*matcher
,
2008 SubMatcher
*sub_matchers
;
2011 for (i
= 0; i
< ON_STACK_MATCHERS
; i
++)
2013 if (matcher
->sub_matchers
[i
].id
== 0)
2016 if (matcher
->sub_matchers
[i
].id
== (id
& matcher
->sub_matchers
[i
].mask
))
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
))
2034 g_file_attribute_matcher_matches_id (GFileAttributeMatcher
*matcher
,
2037 /* We return a NULL matcher for an empty match string, so handle this */
2038 if (matcher
== NULL
)
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.
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
)
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.
2091 g_file_attribute_matcher_enumerate_namespace (GFileAttributeMatcher
*matcher
,
2094 SubMatcher
*sub_matchers
;
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
)
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
)
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
)
2125 matcher
->iterator_ns
= ns_id
;
2126 matcher
->iterator_pos
= 0;
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.
2141 g_file_attribute_matcher_enumerate_next (GFileAttributeMatcher
*matcher
)
2144 SubMatcher
*sub_matcher
;
2146 /* We return a NULL matcher for an empty match string, so handle this */
2147 if (matcher
== NULL
)
2152 i
= matcher
->iterator_pos
++;
2154 if (i
< ON_STACK_MATCHERS
)
2156 if (matcher
->sub_matchers
[i
].id
== 0)
2159 sub_matcher
= &matcher
->sub_matchers
[i
];
2163 if (matcher
->more_sub_matchers
== 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
);
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"