1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
27 #include "gfileattribute.h"
28 #include "gfileattribute-priv.h"
29 #include <glib-object.h>
34 * _g_file_attribute_value_free:
35 * @attr: a #GFileAttributeValue.
37 * Frees the memory used by @attr.
41 _g_file_attribute_value_free (GFileAttributeValue *attr)
43 g_return_if_fail (attr != NULL);
45 _g_file_attribute_value_clear (attr);
50 * _g_file_attribute_value_clear:
51 * @attr: a #GFileAttributeValue.
53 * Clears the value of @attr and sets its type to
54 * %G_FILE_ATTRIBUTE_TYPE_INVALID.
58 _g_file_attribute_value_clear (GFileAttributeValue *attr)
60 g_return_if_fail (attr != NULL);
62 if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
63 attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
64 g_free (attr->u.string);
66 if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV)
67 g_strfreev (attr->u.stringv);
69 if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
71 g_object_unref (attr->u.obj);
73 attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
77 * g_file_attribute_value_set:
78 * @attr: a #GFileAttributeValue to set the value in.
79 * @new_value: a #GFileAttributeValue to get the value from.
81 * Sets an attribute's value from another attribute.
84 _g_file_attribute_value_set (GFileAttributeValue *attr,
85 const GFileAttributeValue *new_value)
87 g_return_if_fail (attr != NULL);
88 g_return_if_fail (new_value != NULL);
90 _g_file_attribute_value_clear (attr);
93 if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
94 attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
95 attr->u.string = g_strdup (attr->u.string);
97 if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV)
98 attr->u.stringv = g_strdupv (attr->u.stringv);
100 if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
102 g_object_ref (attr->u.obj);
106 * _g_file_attribute_value_new:
108 * Creates a new file attribute.
110 * Returns: a #GFileAttributeValue.
112 GFileAttributeValue *
113 _g_file_attribute_value_new (void)
115 GFileAttributeValue *attr;
117 attr = g_new (GFileAttributeValue, 1);
118 attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
123 _g_file_attribute_value_peek_as_pointer (GFileAttributeValue *attr)
125 switch (attr->type) {
126 case G_FILE_ATTRIBUTE_TYPE_STRING:
127 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
128 return attr->u.string;
129 case G_FILE_ATTRIBUTE_TYPE_STRINGV:
130 return attr->u.stringv;
131 case G_FILE_ATTRIBUTE_TYPE_OBJECT:
134 return (gpointer) &attr->u;
139 * g_file_attribute_value_dup:
140 * @other: a #GFileAttributeValue to duplicate.
142 * Duplicates a file attribute.
144 * Returns: a duplicate of the @other.
146 GFileAttributeValue *
147 _g_file_attribute_value_dup (const GFileAttributeValue *other)
149 GFileAttributeValue *attr;
151 g_return_val_if_fail (other != NULL, NULL);
153 attr = g_new (GFileAttributeValue, 1);
154 attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
155 _g_file_attribute_value_set (attr, other);
159 G_DEFINE_BOXED_TYPE (GFileAttributeInfoList, g_file_attribute_info_list,
160 g_file_attribute_info_list_dup,
161 g_file_attribute_info_list_unref)
166 return c >= 32 && c <= 126 && c != '\\';
170 escape_byte_string (const char *str)
174 char *escaped_val, *p;
176 const char hex_digits[] = "0123456789abcdef";
181 for (i = 0; i < len; i++)
183 if (!valid_char (str[i]))
187 if (num_invalid == 0)
188 return g_strdup (str);
191 escaped_val = g_malloc (len + num_invalid*3 + 1);
194 for (i = 0; i < len; i++)
203 *p++ = hex_digits[(c >> 4) & 0xf];
204 *p++ = hex_digits[c & 0xf];
213 * _g_file_attribute_value_as_string:
214 * @attr: a #GFileAttributeValue.
216 * Converts a #GFileAttributeValue to a string for display.
217 * The returned string should be freed when no longer needed.
219 * Returns: a string from the @attr, %NULL on error, or "<invalid>"
220 * if @attr is of type %G_FILE_ATTRIBUTE_TYPE_INVALID.
223 _g_file_attribute_value_as_string (const GFileAttributeValue *attr)
229 g_return_val_if_fail (attr != NULL, NULL);
233 case G_FILE_ATTRIBUTE_TYPE_STRING:
234 str = g_strdup (attr->u.string);
236 case G_FILE_ATTRIBUTE_TYPE_STRINGV:
237 s = g_string_new ("[");
238 for (i = 0; attr->u.stringv[i] != NULL; i++)
240 g_string_append (s, attr->u.stringv[i]);
241 if (attr->u.stringv[i+1] != NULL)
242 g_string_append (s, ", ");
244 g_string_append (s, "]");
245 str = g_string_free (s, FALSE);
247 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
248 str = escape_byte_string (attr->u.string);
250 case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
251 str = g_strdup_printf ("%s", attr->u.boolean?"TRUE":"FALSE");
253 case G_FILE_ATTRIBUTE_TYPE_UINT32:
254 str = g_strdup_printf ("%u", (unsigned int)attr->u.uint32);
256 case G_FILE_ATTRIBUTE_TYPE_INT32:
257 str = g_strdup_printf ("%i", (int)attr->u.int32);
259 case G_FILE_ATTRIBUTE_TYPE_UINT64:
260 str = g_strdup_printf ("%"G_GUINT64_FORMAT, attr->u.uint64);
262 case G_FILE_ATTRIBUTE_TYPE_INT64:
263 str = g_strdup_printf ("%"G_GINT64_FORMAT, attr->u.int64);
265 case G_FILE_ATTRIBUTE_TYPE_OBJECT:
266 str = g_strdup_printf ("%s:%p", g_type_name_from_instance
267 ((GTypeInstance *) attr->u.obj),
270 case G_FILE_ATTRIBUTE_TYPE_INVALID:
271 str = g_strdup ("<unset>");
274 g_warning ("Invalid type in GFileInfo attribute");
275 str = g_strdup ("<invalid>");
283 * _g_file_attribute_value_get_string:
284 * @attr: a #GFileAttributeValue.
286 * Gets the string from a file attribute value. If the value is not the
287 * right type then %NULL will be returned.
289 * Returns: the UTF-8 string value contained within the attribute, or %NULL.
292 _g_file_attribute_value_get_string (const GFileAttributeValue *attr)
297 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING, NULL);
299 return attr->u.string;
303 * _g_file_attribute_value_get_byte_string:
304 * @attr: a #GFileAttributeValue.
306 * Gets the byte string from a file attribute value. If the value is not the
307 * right type then %NULL will be returned.
309 * Returns: the byte string contained within the attribute or %NULL.
312 _g_file_attribute_value_get_byte_string (const GFileAttributeValue *attr)
317 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, NULL);
319 return attr->u.string;
323 _g_file_attribute_value_get_stringv (const GFileAttributeValue *attr)
328 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV, NULL);
330 return attr->u.stringv;
334 * _g_file_attribute_value_get_boolean:
335 * @attr: a #GFileAttributeValue.
337 * Gets the boolean value from a file attribute value. If the value is not the
338 * right type then %FALSE will be returned.
340 * Returns: the boolean value contained within the attribute, or %FALSE.
343 _g_file_attribute_value_get_boolean (const GFileAttributeValue *attr)
348 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BOOLEAN, FALSE);
350 return attr->u.boolean;
354 * _g_file_attribute_value_get_uint32:
355 * @attr: a #GFileAttributeValue.
357 * Gets the unsigned 32-bit integer from a file attribute value. If the value
358 * is not the right type then 0 will be returned.
360 * Returns: the unsigned 32-bit integer from the attribute, or 0.
363 _g_file_attribute_value_get_uint32 (const GFileAttributeValue *attr)
368 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT32, 0);
370 return attr->u.uint32;
374 * _g_file_attribute_value_get_int32:
375 * @attr: a #GFileAttributeValue.
377 * Gets the signed 32-bit integer from a file attribute value. If the value
378 * is not the right type then 0 will be returned.
380 * Returns: the signed 32-bit integer from the attribute, or 0.
383 _g_file_attribute_value_get_int32 (const GFileAttributeValue *attr)
388 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT32, 0);
390 return attr->u.int32;
394 * _g_file_attribute_value_get_uint64:
395 * @attr: a #GFileAttributeValue.
397 * Gets the unsigned 64-bit integer from a file attribute value. If the value
398 * is not the right type then 0 will be returned.
400 * Returns: the unsigned 64-bit integer from the attribute, or 0.
403 _g_file_attribute_value_get_uint64 (const GFileAttributeValue *attr)
408 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT64, 0);
410 return attr->u.uint64;
414 * _g_file_attribute_value_get_int64:
415 * @attr: a #GFileAttributeValue.
417 * Gets the signed 64-bit integer from a file attribute value. If the value
418 * is not the right type then 0 will be returned.
420 * Returns: the signed 64-bit integer from the attribute, or 0.
423 _g_file_attribute_value_get_int64 (const GFileAttributeValue *attr)
428 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT64, 0);
430 return attr->u.int64;
434 * _g_file_attribute_value_get_object:
435 * @attr: a #GFileAttributeValue.
437 * Gets the GObject from a file attribute value. If the value
438 * is not the right type then %NULL will be returned.
440 * Returns: the GObject from the attribute, or %NULL.
443 _g_file_attribute_value_get_object (const GFileAttributeValue *attr)
448 g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT, NULL);
455 _g_file_attribute_value_set_from_pointer (GFileAttributeValue *value,
456 GFileAttributeType type,
460 _g_file_attribute_value_clear (value);
464 case G_FILE_ATTRIBUTE_TYPE_STRING:
465 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
467 value->u.string = g_strdup (value_p);
469 value->u.string = value_p;
472 case G_FILE_ATTRIBUTE_TYPE_STRINGV:
474 value->u.stringv = g_strdupv (value_p);
476 value->u.stringv = value_p;
479 case G_FILE_ATTRIBUTE_TYPE_OBJECT:
481 value->u.obj = g_object_ref (value_p);
483 value->u.obj = value_p;
486 case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
487 value->u.boolean = *(gboolean *)value_p;
490 case G_FILE_ATTRIBUTE_TYPE_UINT32:
491 value->u.uint32 = *(guint32 *)value_p;
494 case G_FILE_ATTRIBUTE_TYPE_INT32:
495 value->u.int32 = *(gint32 *)value_p;
498 case G_FILE_ATTRIBUTE_TYPE_UINT64:
499 value->u.uint64 = *(guint64 *)value_p;
502 case G_FILE_ATTRIBUTE_TYPE_INT64:
503 value->u.int64 = *(gint64 *)value_p;
506 case G_FILE_ATTRIBUTE_TYPE_INVALID:
510 g_warning ("Unknown type specified in g_file_info_set_attribute");
516 * _g_file_attribute_value_set_string:
517 * @attr: a #GFileAttributeValue.
518 * @string: a UTF-8 string to set within the type.
520 * Sets the attribute value to a given UTF-8 string.
523 _g_file_attribute_value_set_string (GFileAttributeValue *attr,
526 g_return_if_fail (attr != NULL);
527 g_return_if_fail (string != NULL);
529 _g_file_attribute_value_clear (attr);
530 attr->type = G_FILE_ATTRIBUTE_TYPE_STRING;
531 attr->u.string = g_strdup (string);
535 * _g_file_attribute_value_set_byte_string:
536 * @attr: a #GFileAttributeValue.
537 * @string: a byte string to set within the type.
539 * Sets the attribute value to a given byte string.
542 _g_file_attribute_value_set_byte_string (GFileAttributeValue *attr,
545 g_return_if_fail (attr != NULL);
546 g_return_if_fail (string != NULL);
548 _g_file_attribute_value_clear (attr);
549 attr->type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
550 attr->u.string = g_strdup (string);
554 _g_file_attribute_value_set_stringv (GFileAttributeValue *attr,
557 g_return_if_fail (attr != NULL);
558 g_return_if_fail (value != NULL);
560 _g_file_attribute_value_clear (attr);
561 attr->type = G_FILE_ATTRIBUTE_TYPE_STRINGV;
562 attr->u.stringv = g_strdupv (value);
567 * _g_file_attribute_value_set_boolean:
568 * @attr: a #GFileAttributeValue.
569 * @value: a #gboolean to set within the type.
571 * Sets the attribute value to the given boolean value.
574 _g_file_attribute_value_set_boolean (GFileAttributeValue *attr,
577 g_return_if_fail (attr != NULL);
579 _g_file_attribute_value_clear (attr);
580 attr->type = G_FILE_ATTRIBUTE_TYPE_BOOLEAN;
581 attr->u.boolean = !!value;
585 * _g_file_attribute_value_set_uint32:
586 * @attr: a #GFileAttributeValue.
587 * @value: a #guint32 to set within the type.
589 * Sets the attribute value to the given unsigned 32-bit integer.
592 _g_file_attribute_value_set_uint32 (GFileAttributeValue *attr,
595 g_return_if_fail (attr != NULL);
597 _g_file_attribute_value_clear (attr);
598 attr->type = G_FILE_ATTRIBUTE_TYPE_UINT32;
599 attr->u.uint32 = value;
603 * _g_file_attribute_value_set_int32:
604 * @attr: a #GFileAttributeValue.
605 * @value: a #gint32 to set within the type.
607 * Sets the attribute value to the given signed 32-bit integer.
610 _g_file_attribute_value_set_int32 (GFileAttributeValue *attr,
613 g_return_if_fail (attr != NULL);
615 _g_file_attribute_value_clear (attr);
616 attr->type = G_FILE_ATTRIBUTE_TYPE_INT32;
617 attr->u.int32 = value;
621 * _g_file_attribute_value_set_uint64:
622 * @attr: a #GFileAttributeValue.
623 * @value: a #guint64 to set within the type.
625 * Sets the attribute value to a given unsigned 64-bit integer.
628 _g_file_attribute_value_set_uint64 (GFileAttributeValue *attr,
631 g_return_if_fail (attr != NULL);
633 _g_file_attribute_value_clear (attr);
634 attr->type = G_FILE_ATTRIBUTE_TYPE_UINT64;
635 attr->u.uint64 = value;
639 * _g_file_attribute_value_set_int64:
640 * @attr: a #GFileAttributeValue.
641 * @value: a #gint64 to set within the type.
643 * Sets the attribute value to a given signed 64-bit integer.
646 _g_file_attribute_value_set_int64 (GFileAttributeValue *attr,
649 g_return_if_fail (attr != NULL);
651 _g_file_attribute_value_clear (attr);
652 attr->type = G_FILE_ATTRIBUTE_TYPE_INT64;
653 attr->u.int64 = value;
657 * _g_file_attribute_value_set_object:
658 * @attr: a #GFileAttributeValue.
661 * Sets the attribute to contain the value @obj.
662 * The @attr references the GObject internally.
665 _g_file_attribute_value_set_object (GFileAttributeValue *attr,
668 g_return_if_fail (attr != NULL);
669 g_return_if_fail (obj != NULL);
671 _g_file_attribute_value_clear (attr);
672 attr->type = G_FILE_ATTRIBUTE_TYPE_OBJECT;
673 attr->u.obj = g_object_ref (obj);
677 GFileAttributeInfoList public;
680 } GFileAttributeInfoListPriv;
683 list_update_public (GFileAttributeInfoListPriv *priv)
685 priv->public.infos = (GFileAttributeInfo *)priv->array->data;
686 priv->public.n_infos = priv->array->len;
690 * g_file_attribute_info_list_new:
692 * Creates a new file attribute info list.
694 * Returns: a #GFileAttributeInfoList.
696 GFileAttributeInfoList *
697 g_file_attribute_info_list_new (void)
699 GFileAttributeInfoListPriv *priv;
701 priv = g_new0 (GFileAttributeInfoListPriv, 1);
704 priv->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
706 list_update_public (priv);
708 return (GFileAttributeInfoList *)priv;
712 * g_file_attribute_info_list_dup:
713 * @list: a #GFileAttributeInfoList to duplicate.
715 * Makes a duplicate of a file attribute info list.
717 * Returns: a copy of the given @list.
719 GFileAttributeInfoList *
720 g_file_attribute_info_list_dup (GFileAttributeInfoList *list)
722 GFileAttributeInfoListPriv *new;
725 g_return_val_if_fail (list != NULL, NULL);
727 new = g_new0 (GFileAttributeInfoListPriv, 1);
729 new->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
731 g_array_set_size (new->array, list->n_infos);
732 list_update_public (new);
733 for (i = 0; i < list->n_infos; i++)
735 new->public.infos[i].name = g_strdup (list->infos[i].name);
736 new->public.infos[i].type = list->infos[i].type;
737 new->public.infos[i].flags = list->infos[i].flags;
740 return (GFileAttributeInfoList *)new;
744 * g_file_attribute_info_list_ref:
745 * @list: a #GFileAttributeInfoList to reference.
747 * References a file attribute info list.
749 * Returns: #GFileAttributeInfoList or %NULL on error.
751 GFileAttributeInfoList *
752 g_file_attribute_info_list_ref (GFileAttributeInfoList *list)
754 GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
757 g_return_val_if_fail (list != NULL, NULL);
759 old_ref_count = g_atomic_int_add (&priv->ref_count, 1);
760 g_return_val_if_fail (old_ref_count > 0, NULL);
766 * g_file_attribute_info_list_unref:
767 * @list: The #GFileAttributeInfoList to unreference.
769 * Removes a reference from the given @list. If the reference count
770 * falls to zero, the @list is deleted.
773 g_file_attribute_info_list_unref (GFileAttributeInfoList *list)
775 GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
778 g_return_if_fail (list != NULL);
779 g_return_if_fail (priv->ref_count > 0);
781 if (g_atomic_int_dec_and_test (&priv->ref_count))
783 for (i = 0; i < list->n_infos; i++)
784 g_free (list->infos[i].name);
785 g_array_free (priv->array, TRUE);
791 g_file_attribute_info_list_bsearch (GFileAttributeInfoList *list,
801 mid = start + (end - start) / 2;
803 if (strcmp (name, list->infos[mid].name) < 0)
805 else if (strcmp (name, list->infos[mid].name) > 0)
814 * g_file_attribute_info_list_lookup:
815 * @list: a #GFileAttributeInfoList.
816 * @name: the name of the attribute to look up.
818 * Gets the file attribute with the name @name from @list.
820 * Returns: a #GFileAttributeInfo for the @name, or %NULL if an
821 * attribute isn't found.
823 const GFileAttributeInfo *
824 g_file_attribute_info_list_lookup (GFileAttributeInfoList *list,
829 g_return_val_if_fail (list != NULL, NULL);
830 g_return_val_if_fail (name != NULL, NULL);
832 i = g_file_attribute_info_list_bsearch (list, name);
834 if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
835 return &list->infos[i];
841 * g_file_attribute_info_list_add:
842 * @list: a #GFileAttributeInfoList.
843 * @name: the name of the attribute to add.
844 * @type: the #GFileAttributeType for the attribute.
845 * @flags: #GFileAttributeInfoFlags for the attribute.
847 * Adds a new attribute with @name to the @list, setting
848 * its @type and @flags.
851 g_file_attribute_info_list_add (GFileAttributeInfoList *list,
853 GFileAttributeType type,
854 GFileAttributeInfoFlags flags)
856 GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
857 GFileAttributeInfo info;
860 g_return_if_fail (list != NULL);
861 g_return_if_fail (name != NULL);
863 i = g_file_attribute_info_list_bsearch (list, name);
865 if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
867 list->infos[i].type = type;
871 info.name = g_strdup (name);
874 g_array_insert_vals (priv->array, i, &info, 1);
876 list_update_public (priv);