1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
|
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2006-2007 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Alexander Larsson <alexl@redhat.com>
*/
#include "config.h"
#include <string.h>
#include "gfileattribute.h"
#include "gfileattribute-priv.h"
#include <glib-object.h>
#include "glibintl.h"
/**
* SECTION:gfileattribute
* @short_description: Key-Value Paired File Attributes
* @include: gio/gio.h
* @see_also: #GFile, #GFileInfo
*
* File attributes in GIO consist of a list of key-value pairs.
*
* Keys are strings that contain a key namespace and a key name, separated
* by a colon, e.g. "namespace::keyname". Namespaces are included to sort
* key-value pairs by namespaces for relevance. Keys can be retrieved
* using wildcards, e.g. "standard::*" will return all of the keys in the
* "standard" namespace.
*
* The list of possible attributes for a filesystem (pointed to by a #GFile) is
* available as a #GFileAttributeInfoList. This list is queryable by key names
* as indicated earlier.
*
* Information is stored within the list in #GFileAttributeInfo structures.
* The info structure can store different types, listed in the enum
* #GFileAttributeType. Upon creation of a #GFileAttributeInfo, the type will
* be set to %G_FILE_ATTRIBUTE_TYPE_INVALID.
*
* Classes that implement #GFileIface will create a #GFileAttributeInfoList and
* install default keys and values for their given file system, architecture,
* and other possible implementation details (e.g., on a UNIX system, a file
* attribute key will be registered for the user id for a given file).
*
* ## Default Namespaces
*
* - `"standard"`: The "Standard" namespace. General file information that
* any application may need should be put in this namespace. Examples
* include the file's name, type, and size.
* - `"etag`: The [Entity Tag][gfile-etag] namespace. Currently, the only key
* in this namespace is "value", which contains the value of the current
* entity tag.
* - `"id"`: The "Identification" namespace. This namespace is used by file
* managers and applications that list directories to check for loops and
* to uniquely identify files.
* - `"access"`: The "Access" namespace. Used to check if a user has the
* proper privileges to access files and perform file operations. Keys in
* this namespace are made to be generic and easily understood, e.g. the
* "can_read" key is %TRUE if the current user has permission to read the
* file. UNIX permissions and NTFS ACLs in Windows should be mapped to
* these values.
* - `"mountable"`: The "Mountable" namespace. Includes simple boolean keys
* for checking if a file or path supports mount operations, e.g. mount,
* unmount, eject. These are used for files of type %G_FILE_TYPE_MOUNTABLE.
* - `"time"`: The "Time" namespace. Includes file access, changed, created
* times.
* - `"unix"`: The "Unix" namespace. Includes UNIX-specific information and
* may not be available for all files. Examples include the UNIX "UID",
* "GID", etc.
* - `"dos"`: The "DOS" namespace. Includes DOS-specific information and may
* not be available for all files. Examples include "is_system" for checking
* if a file is marked as a system file, and "is_archive" for checking if a
* file is marked as an archive file.
* - `"owner"`: The "Owner" namespace. Includes information about who owns a
* file. May not be available for all file systems. Examples include "user"
* for getting the user name of the file owner. This information is often
* mapped from some backend specific data such as a UNIX UID.
* - `"thumbnail"`: The "Thumbnail" namespace. Includes information about file
* thumbnails and their location within the file system. Examples of keys in
* this namespace include "path" to get the location of a thumbnail, "failed"
* to check if thumbnailing of the file failed, and "is-valid" to check if
* the thumbnail is outdated.
* - `"filesystem"`: The "Filesystem" namespace. Gets information about the
* file system where a file is located, such as its type, how much space is
* left available, and the overall size of the file system.
* - `"gvfs"`: The "GVFS" namespace. Keys in this namespace contain information
* about the current GVFS backend in use.
* - `"xattr"`: The "xattr" namespace. Gets information about extended
* user attributes. See attr(5). The "user." prefix of the extended user
* attribute name is stripped away when constructing keys in this namespace,
* e.g. "xattr::mime_type" for the extended attribute with the name
* "user.mime_type". Note that this information is only available if
* GLib has been built with extended attribute support.
* - `"xattr-sys"`: The "xattr-sys" namespace. Gets information about
* extended attributes which are not user-specific. See attr(5). Note
* that this information is only available if GLib has been built with
* extended attribute support.
* - `"selinux"`: The "SELinux" namespace. Includes information about the
* SELinux context of files. Note that this information is only available
* if GLib has been built with SELinux support.
*
* Please note that these are not all of the possible namespaces.
* More namespaces can be added from GIO modules or by individual applications.
* For more information about writing GIO modules, see #GIOModule.
*
* <!-- TODO: Implementation note about using extended attributes on supported
* file systems -->
*
* ## Default Keys
*
* For a list of the built-in keys and their types, see the
* [GFileInfo][GFileInfo] documentation.
*
* Note that there are no predefined keys in the "xattr" and "xattr-sys"
* namespaces. Keys for the "xattr" namespace are constructed by stripping
* away the "user." prefix from the extended user attribute, and prepending
* "xattr::". Keys for the "xattr-sys" namespace are constructed by
* concatenating "xattr-sys::" with the extended attribute name. All extended
* attribute values are returned as hex-encoded strings in which bytes outside
* the ASCII range are encoded as escape sequences of the form \x`nn`
* where `nn` is a 2-digit hexadecimal number.
*/
/**
* _g_file_attribute_value_free:
* @attr: a #GFileAttributeValue.
*
* Frees the memory used by @attr.
*
**/
void
_g_file_attribute_value_free (GFileAttributeValue *attr)
{
g_return_if_fail (attr != NULL);
_g_file_attribute_value_clear (attr);
g_free (attr);
}
/**
* _g_file_attribute_value_clear:
* @attr: a #GFileAttributeValue.
*
* Clears the value of @attr and sets its type to
* %G_FILE_ATTRIBUTE_TYPE_INVALID.
*
**/
void
_g_file_attribute_value_clear (GFileAttributeValue *attr)
{
g_return_if_fail (attr != NULL);
if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
g_free (attr->u.string);
if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV)
g_strfreev (attr->u.stringv);
if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
attr->u.obj != NULL)
g_object_unref (attr->u.obj);
attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
}
/**
* g_file_attribute_value_set:
* @attr: a #GFileAttributeValue to set the value in.
* @new_value: a #GFileAttributeValue to get the value from.
*
* Sets an attribute's value from another attribute.
**/
void
_g_file_attribute_value_set (GFileAttributeValue *attr,
const GFileAttributeValue *new_value)
{
g_return_if_fail (attr != NULL);
g_return_if_fail (new_value != NULL);
_g_file_attribute_value_clear (attr);
*attr = *new_value;
if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING ||
attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
attr->u.string = g_strdup (attr->u.string);
if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV)
attr->u.stringv = g_strdupv (attr->u.stringv);
if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
attr->u.obj != NULL)
g_object_ref (attr->u.obj);
}
/**
* _g_file_attribute_value_new:
*
* Creates a new file attribute.
*
* Returns: a #GFileAttributeValue.
**/
GFileAttributeValue *
_g_file_attribute_value_new (void)
{
GFileAttributeValue *attr;
attr = g_new (GFileAttributeValue, 1);
attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
return attr;
}
gpointer
_g_file_attribute_value_peek_as_pointer (GFileAttributeValue *attr)
{
switch (attr->type) {
case G_FILE_ATTRIBUTE_TYPE_STRING:
case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
return attr->u.string;
case G_FILE_ATTRIBUTE_TYPE_STRINGV:
return attr->u.stringv;
case G_FILE_ATTRIBUTE_TYPE_OBJECT:
return attr->u.obj;
default:
return (gpointer) &attr->u;
}
}
/**
* g_file_attribute_value_dup:
* @other: a #GFileAttributeValue to duplicate.
*
* Duplicates a file attribute.
*
* Returns: a duplicate of the @other.
**/
GFileAttributeValue *
_g_file_attribute_value_dup (const GFileAttributeValue *other)
{
GFileAttributeValue *attr;
g_return_val_if_fail (other != NULL, NULL);
attr = g_new (GFileAttributeValue, 1);
attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
_g_file_attribute_value_set (attr, other);
return attr;
}
G_DEFINE_BOXED_TYPE (GFileAttributeInfoList, g_file_attribute_info_list,
g_file_attribute_info_list_dup,
g_file_attribute_info_list_unref)
static gboolean
valid_char (char c)
{
return c >= 32 && c <= 126 && c != '\\';
}
static char *
escape_byte_string (const char *str)
{
size_t i, len;
int num_invalid;
char *escaped_val, *p;
unsigned char c;
const char hex_digits[] = "0123456789abcdef";
len = strlen (str);
num_invalid = 0;
for (i = 0; i < len; i++)
{
if (!valid_char (str[i]))
num_invalid++;
}
if (num_invalid == 0)
return g_strdup (str);
else
{
escaped_val = g_malloc (len + num_invalid*3 + 1);
p = escaped_val;
for (i = 0; i < len; i++)
{
c = str[i];
if (valid_char (c))
*p++ = c;
else
{
*p++ = '\\';
*p++ = 'x';
*p++ = hex_digits[(c >> 4) & 0xf];
*p++ = hex_digits[c & 0xf];
}
}
*p++ = 0;
return escaped_val;
}
}
/**
* _g_file_attribute_value_as_string:
* @attr: a #GFileAttributeValue.
*
* Converts a #GFileAttributeValue to a string for display.
* The returned string should be freed when no longer needed.
*
* Returns: a string from the @attr, %NULL on error, or "<invalid>"
* if @attr is of type %G_FILE_ATTRIBUTE_TYPE_INVALID.
*/
char *
_g_file_attribute_value_as_string (const GFileAttributeValue *attr)
{
GString *s;
int i;
char *str;
g_return_val_if_fail (attr != NULL, NULL);
switch (attr->type)
{
case G_FILE_ATTRIBUTE_TYPE_STRING:
str = g_strdup (attr->u.string);
break;
case G_FILE_ATTRIBUTE_TYPE_STRINGV:
s = g_string_new ("[");
for (i = 0; attr->u.stringv[i] != NULL; i++)
{
g_string_append (s, attr->u.stringv[i]);
if (attr->u.stringv[i+1] != NULL)
g_string_append (s, ", ");
}
g_string_append (s, "]");
str = g_string_free (s, FALSE);
break;
case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
str = escape_byte_string (attr->u.string);
break;
case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
str = g_strdup_printf ("%s", attr->u.boolean?"TRUE":"FALSE");
break;
case G_FILE_ATTRIBUTE_TYPE_UINT32:
str = g_strdup_printf ("%u", (unsigned int)attr->u.uint32);
break;
case G_FILE_ATTRIBUTE_TYPE_INT32:
str = g_strdup_printf ("%i", (int)attr->u.int32);
break;
case G_FILE_ATTRIBUTE_TYPE_UINT64:
str = g_strdup_printf ("%"G_GUINT64_FORMAT, attr->u.uint64);
break;
case G_FILE_ATTRIBUTE_TYPE_INT64:
str = g_strdup_printf ("%"G_GINT64_FORMAT, attr->u.int64);
break;
case G_FILE_ATTRIBUTE_TYPE_OBJECT:
str = g_strdup_printf ("%s:%p", g_type_name_from_instance
((GTypeInstance *) attr->u.obj),
attr->u.obj);
break;
case G_FILE_ATTRIBUTE_TYPE_INVALID:
str = g_strdup ("<unset>");
break;
default:
g_warning ("Invalid type in GFileInfo attribute");
str = g_strdup ("<invalid>");
break;
}
return str;
}
/**
* _g_file_attribute_value_get_string:
* @attr: a #GFileAttributeValue.
*
* Gets the string from a file attribute value. If the value is not the
* right type then %NULL will be returned.
*
* Returns: the UTF-8 string value contained within the attribute, or %NULL.
*/
const char *
_g_file_attribute_value_get_string (const GFileAttributeValue *attr)
{
if (attr == NULL)
return NULL;
g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING, NULL);
return attr->u.string;
}
/**
* _g_file_attribute_value_get_byte_string:
* @attr: a #GFileAttributeValue.
*
* Gets the byte string from a file attribute value. If the value is not the
* right type then %NULL will be returned.
*
* Returns: the byte string contained within the attribute or %NULL.
*/
const char *
_g_file_attribute_value_get_byte_string (const GFileAttributeValue *attr)
{
if (attr == NULL)
return NULL;
g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, NULL);
return attr->u.string;
}
char **
_g_file_attribute_value_get_stringv (const GFileAttributeValue *attr)
{
if (attr == NULL)
return NULL;
g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV, NULL);
return attr->u.stringv;
}
/**
* _g_file_attribute_value_get_boolean:
* @attr: a #GFileAttributeValue.
*
* Gets the boolean value from a file attribute value. If the value is not the
* right type then %FALSE will be returned.
*
* Returns: the boolean value contained within the attribute, or %FALSE.
*/
gboolean
_g_file_attribute_value_get_boolean (const GFileAttributeValue *attr)
{
if (attr == NULL)
return FALSE;
g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BOOLEAN, FALSE);
return attr->u.boolean;
}
/**
* _g_file_attribute_value_get_uint32:
* @attr: a #GFileAttributeValue.
*
* Gets the unsigned 32-bit integer from a file attribute value. If the value
* is not the right type then 0 will be returned.
*
* Returns: the unsigned 32-bit integer from the attribute, or 0.
*/
guint32
_g_file_attribute_value_get_uint32 (const GFileAttributeValue *attr)
{
if (attr == NULL)
return 0;
g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT32, 0);
return attr->u.uint32;
}
/**
* _g_file_attribute_value_get_int32:
* @attr: a #GFileAttributeValue.
*
* Gets the signed 32-bit integer from a file attribute value. If the value
* is not the right type then 0 will be returned.
*
* Returns: the signed 32-bit integer from the attribute, or 0.
*/
gint32
_g_file_attribute_value_get_int32 (const GFileAttributeValue *attr)
{
if (attr == NULL)
return 0;
g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT32, 0);
return attr->u.int32;
}
/**
* _g_file_attribute_value_get_uint64:
* @attr: a #GFileAttributeValue.
*
* Gets the unsigned 64-bit integer from a file attribute value. If the value
* is not the right type then 0 will be returned.
*
* Returns: the unsigned 64-bit integer from the attribute, or 0.
*/
guint64
_g_file_attribute_value_get_uint64 (const GFileAttributeValue *attr)
{
if (attr == NULL)
return 0;
g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT64, 0);
return attr->u.uint64;
}
/**
* _g_file_attribute_value_get_int64:
* @attr: a #GFileAttributeValue.
*
* Gets the signed 64-bit integer from a file attribute value. If the value
* is not the right type then 0 will be returned.
*
* Returns: the signed 64-bit integer from the attribute, or 0.
*/
gint64
_g_file_attribute_value_get_int64 (const GFileAttributeValue *attr)
{
if (attr == NULL)
return 0;
g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT64, 0);
return attr->u.int64;
}
/**
* _g_file_attribute_value_get_object:
* @attr: a #GFileAttributeValue.
*
* Gets the GObject from a file attribute value. If the value
* is not the right type then %NULL will be returned.
*
* Returns: the GObject from the attribute, or %NULL.
**/
GObject *
_g_file_attribute_value_get_object (const GFileAttributeValue *attr)
{
if (attr == NULL)
return NULL;
g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT, NULL);
return attr->u.obj;
}
void
_g_file_attribute_value_set_from_pointer (GFileAttributeValue *value,
GFileAttributeType type,
gpointer value_p,
gboolean dup)
{
_g_file_attribute_value_clear (value);
value->type = type;
switch (type)
{
case G_FILE_ATTRIBUTE_TYPE_STRING:
case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
if (dup)
value->u.string = g_strdup (value_p);
else
value->u.string = value_p;
break;
case G_FILE_ATTRIBUTE_TYPE_STRINGV:
if (dup)
value->u.stringv = g_strdupv (value_p);
else
value->u.stringv = value_p;
break;
case G_FILE_ATTRIBUTE_TYPE_OBJECT:
if (dup)
value->u.obj = g_object_ref (value_p);
else
value->u.obj = value_p;
break;
case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
value->u.boolean = *(gboolean *)value_p;
break;
case G_FILE_ATTRIBUTE_TYPE_UINT32:
value->u.uint32 = *(guint32 *)value_p;
break;
case G_FILE_ATTRIBUTE_TYPE_INT32:
value->u.int32 = *(gint32 *)value_p;
break;
case G_FILE_ATTRIBUTE_TYPE_UINT64:
value->u.uint64 = *(guint64 *)value_p;
break;
case G_FILE_ATTRIBUTE_TYPE_INT64:
value->u.int64 = *(gint64 *)value_p;
break;
case G_FILE_ATTRIBUTE_TYPE_INVALID:
break;
default:
g_warning ("Unknown type specified in g_file_info_set_attribute");
break;
}
}
/**
* _g_file_attribute_value_set_string:
* @attr: a #GFileAttributeValue.
* @string: a UTF-8 string to set within the type.
*
* Sets the attribute value to a given UTF-8 string.
*/
void
_g_file_attribute_value_set_string (GFileAttributeValue *attr,
const char *string)
{
g_return_if_fail (attr != NULL);
g_return_if_fail (string != NULL);
_g_file_attribute_value_clear (attr);
attr->type = G_FILE_ATTRIBUTE_TYPE_STRING;
attr->u.string = g_strdup (string);
}
/**
* _g_file_attribute_value_set_byte_string:
* @attr: a #GFileAttributeValue.
* @string: a byte string to set within the type.
*
* Sets the attribute value to a given byte string.
*/
void
_g_file_attribute_value_set_byte_string (GFileAttributeValue *attr,
const char *string)
{
g_return_if_fail (attr != NULL);
g_return_if_fail (string != NULL);
_g_file_attribute_value_clear (attr);
attr->type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
attr->u.string = g_strdup (string);
}
void
_g_file_attribute_value_set_stringv (GFileAttributeValue *attr,
char **value)
{
g_return_if_fail (attr != NULL);
g_return_if_fail (value != NULL);
_g_file_attribute_value_clear (attr);
attr->type = G_FILE_ATTRIBUTE_TYPE_STRINGV;
attr->u.stringv = g_strdupv (value);
}
/**
* _g_file_attribute_value_set_boolean:
* @attr: a #GFileAttributeValue.
* @value: a #gboolean to set within the type.
*
* Sets the attribute value to the given boolean value.
*/
void
_g_file_attribute_value_set_boolean (GFileAttributeValue *attr,
gboolean value)
{
g_return_if_fail (attr != NULL);
_g_file_attribute_value_clear (attr);
attr->type = G_FILE_ATTRIBUTE_TYPE_BOOLEAN;
attr->u.boolean = !!value;
}
/**
* _g_file_attribute_value_set_uint32:
* @attr: a #GFileAttributeValue.
* @value: a #guint32 to set within the type.
*
* Sets the attribute value to the given unsigned 32-bit integer.
*/
void
_g_file_attribute_value_set_uint32 (GFileAttributeValue *attr,
guint32 value)
{
g_return_if_fail (attr != NULL);
_g_file_attribute_value_clear (attr);
attr->type = G_FILE_ATTRIBUTE_TYPE_UINT32;
attr->u.uint32 = value;
}
/**
* _g_file_attribute_value_set_int32:
* @attr: a #GFileAttributeValue.
* @value: a #gint32 to set within the type.
*
* Sets the attribute value to the given signed 32-bit integer.
*/
void
_g_file_attribute_value_set_int32 (GFileAttributeValue *attr,
gint32 value)
{
g_return_if_fail (attr != NULL);
_g_file_attribute_value_clear (attr);
attr->type = G_FILE_ATTRIBUTE_TYPE_INT32;
attr->u.int32 = value;
}
/**
* _g_file_attribute_value_set_uint64:
* @attr: a #GFileAttributeValue.
* @value: a #guint64 to set within the type.
*
* Sets the attribute value to a given unsigned 64-bit integer.
*/
void
_g_file_attribute_value_set_uint64 (GFileAttributeValue *attr,
guint64 value)
{
g_return_if_fail (attr != NULL);
_g_file_attribute_value_clear (attr);
attr->type = G_FILE_ATTRIBUTE_TYPE_UINT64;
attr->u.uint64 = value;
}
/**
* _g_file_attribute_value_set_int64:
* @attr: a #GFileAttributeValue.
* @value: a #gint64 to set within the type.
*
* Sets the attribute value to a given signed 64-bit integer.
*/
void
_g_file_attribute_value_set_int64 (GFileAttributeValue *attr,
gint64 value)
{
g_return_if_fail (attr != NULL);
_g_file_attribute_value_clear (attr);
attr->type = G_FILE_ATTRIBUTE_TYPE_INT64;
attr->u.int64 = value;
}
/**
* _g_file_attribute_value_set_object:
* @attr: a #GFileAttributeValue.
* @obj: a #GObject.
*
* Sets the attribute to contain the value @obj.
* The @attr references the GObject internally.
*/
void
_g_file_attribute_value_set_object (GFileAttributeValue *attr,
GObject *obj)
{
g_return_if_fail (attr != NULL);
g_return_if_fail (obj != NULL);
_g_file_attribute_value_clear (attr);
attr->type = G_FILE_ATTRIBUTE_TYPE_OBJECT;
attr->u.obj = g_object_ref (obj);
}
typedef struct {
GFileAttributeInfoList public;
GArray *array;
int ref_count;
} GFileAttributeInfoListPriv;
static void
list_update_public (GFileAttributeInfoListPriv *priv)
{
priv->public.infos = (GFileAttributeInfo *)priv->array->data;
priv->public.n_infos = priv->array->len;
}
/**
* g_file_attribute_info_list_new:
*
* Creates a new file attribute info list.
*
* Returns: a #GFileAttributeInfoList.
*/
GFileAttributeInfoList *
g_file_attribute_info_list_new (void)
{
GFileAttributeInfoListPriv *priv;
priv = g_new0 (GFileAttributeInfoListPriv, 1);
priv->ref_count = 1;
priv->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
list_update_public (priv);
return (GFileAttributeInfoList *)priv;
}
/**
* g_file_attribute_info_list_dup:
* @list: a #GFileAttributeInfoList to duplicate.
*
* Makes a duplicate of a file attribute info list.
*
* Returns: a copy of the given @list.
*/
GFileAttributeInfoList *
g_file_attribute_info_list_dup (GFileAttributeInfoList *list)
{
GFileAttributeInfoListPriv *new;
int i;
g_return_val_if_fail (list != NULL, NULL);
new = g_new0 (GFileAttributeInfoListPriv, 1);
new->ref_count = 1;
new->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
g_array_set_size (new->array, list->n_infos);
list_update_public (new);
for (i = 0; i < list->n_infos; i++)
{
new->public.infos[i].name = g_strdup (list->infos[i].name);
new->public.infos[i].type = list->infos[i].type;
new->public.infos[i].flags = list->infos[i].flags;
}
return (GFileAttributeInfoList *)new;
}
/**
* g_file_attribute_info_list_ref:
* @list: a #GFileAttributeInfoList to reference.
*
* References a file attribute info list.
*
* Returns: #GFileAttributeInfoList or %NULL on error.
*/
GFileAttributeInfoList *
g_file_attribute_info_list_ref (GFileAttributeInfoList *list)
{
GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
int old_ref_count;
g_return_val_if_fail (list != NULL, NULL);
old_ref_count = g_atomic_int_add (&priv->ref_count, 1);
g_return_val_if_fail (old_ref_count > 0, NULL);
return list;
}
/**
* g_file_attribute_info_list_unref:
* @list: The #GFileAttributeInfoList to unreference.
*
* Removes a reference from the given @list. If the reference count
* falls to zero, the @list is deleted.
*/
void
g_file_attribute_info_list_unref (GFileAttributeInfoList *list)
{
GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
int i;
g_return_if_fail (list != NULL);
g_return_if_fail (priv->ref_count > 0);
if (g_atomic_int_dec_and_test (&priv->ref_count))
{
for (i = 0; i < list->n_infos; i++)
g_free (list->infos[i].name);
g_array_free (priv->array, TRUE);
g_free (list);
}
}
static int
g_file_attribute_info_list_bsearch (GFileAttributeInfoList *list,
const char *name)
{
int start, end, mid;
start = 0;
end = list->n_infos;
while (start != end)
{
mid = start + (end - start) / 2;
if (strcmp (name, list->infos[mid].name) < 0)
end = mid;
else if (strcmp (name, list->infos[mid].name) > 0)
start = mid + 1;
else
return mid;
}
return start;
}
/**
* g_file_attribute_info_list_lookup:
* @list: a #GFileAttributeInfoList.
* @name: the name of the attribute to look up.
*
* Gets the file attribute with the name @name from @list.
*
* Returns: a #GFileAttributeInfo for the @name, or %NULL if an
* attribute isn't found.
*/
const GFileAttributeInfo *
g_file_attribute_info_list_lookup (GFileAttributeInfoList *list,
const char *name)
{
int i;
g_return_val_if_fail (list != NULL, NULL);
g_return_val_if_fail (name != NULL, NULL);
i = g_file_attribute_info_list_bsearch (list, name);
if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
return &list->infos[i];
return NULL;
}
/**
* g_file_attribute_info_list_add:
* @list: a #GFileAttributeInfoList.
* @name: the name of the attribute to add.
* @type: the #GFileAttributeType for the attribute.
* @flags: #GFileAttributeInfoFlags for the attribute.
*
* Adds a new attribute with @name to the @list, setting
* its @type and @flags.
*/
void
g_file_attribute_info_list_add (GFileAttributeInfoList *list,
const char *name,
GFileAttributeType type,
GFileAttributeInfoFlags flags)
{
GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
GFileAttributeInfo info;
int i;
g_return_if_fail (list != NULL);
g_return_if_fail (name != NULL);
i = g_file_attribute_info_list_bsearch (list, name);
if (i < list->n_infos && strcmp (list->infos[i].name, name) == 0)
{
list->infos[i].type = type;
return;
}
info.name = g_strdup (name);
info.type = type;
info.flags = flags;
g_array_insert_vals (priv->array, i, &info, 1);
list_update_public (priv);
}
|