gfileattribute.c 26.8 KB
Newer Older
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3 4 5 6 7
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * 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
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * 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
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22 23 24 25

#include <string.h>

#include "gfileattribute.h"
26
#include "gfileattribute-priv.h"
27 28 29
#include <glib-object.h>
#include "glibintl.h"

30

31 32
/**
 * SECTION:gfileattribute
33
 * @short_description: Key-Value Paired File Attributes
Matthias Clasen's avatar
Matthias Clasen committed
34
 * @include: gio/gio.h
35
 * @see_also: #GFile, #GFileInfo
36 37 38
 *
 * File attributes in GIO consist of a list of key-value pairs.
 *
39
 * Keys are strings that contain a key namespace and a key name, separated
Matthias Clasen's avatar
Matthias Clasen committed
40
 * by a colon, e.g. "namespace::keyname". Namespaces are included to sort
41 42
 * key-value pairs by namespaces for relevance. Keys can be retrived
 * using wildcards, e.g. "standard::*" will return all of the keys in the
43
 * "standard" namespace.
44
 *
45
 * The list of possible attributes for a filesystem (pointed to by a #GFile) is
Matthias Clasen's avatar
Matthias Clasen committed
46
 * available as a #GFileAttributeInfoList. This list is queryable by key names
47
 * as indicated earlier.
48
 *
49 50 51 52 53
 * 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.
 *
54 55 56
 * 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
57
 * attribute key will be registered for the user id for a given file).
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
 * ## 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.
115
 *
116
 * Please note that these are not all of the possible namespaces.
117
 * More namespaces can be added from GIO modules or by individual applications.
118
 * For more information about writing GIO modules, see #GIOModule.
119
 *
120
 * <!-- TODO: Implementation note about using extended attributes on supported
121
 * file systems -->
122
 *
123 124 125 126
 * ## Default Keys
 *
 * For a list of the built-in keys and their types, see the
 * [GFileInfo][GFileInfo] documentation.
127 128
 *
 * Note that there are no predefined keys in the "xattr" and "xattr-sys"
129 130
 * namespaces. Keys for the "xattr" namespace are constructed by stripping
 * away the "user." prefix from the extended user attribute, and prepending
131
 * "xattr::". Keys for the "xattr-sys" namespace are constructed by
132 133
 * concatenating "xattr-sys::" with the extended attribute name. All extended
 * attribute values are returned as hex-encoded strings in which bytes outside
134 135 136
 * the ASCII range are encoded as escape sequences of the form \x`nn`
 * where `nn` is a 2-digit hexadecimal number.
 */
137

138
/**
139
 * _g_file_attribute_value_free:
140 141
 * @attr: a #GFileAttributeValue.
 *
142 143 144 145
 * Frees the memory used by @attr.
 *
 **/
void
146
_g_file_attribute_value_free (GFileAttributeValue *attr)
147 148 149
{
  g_return_if_fail (attr != NULL);

150
  _g_file_attribute_value_clear (attr);
151 152 153
  g_free (attr);
}

154
/**
155
 * _g_file_attribute_value_clear:
156 157
 * @attr: a #GFileAttributeValue.
 *
158
 * Clears the value of @attr and sets its type to
159
 * %G_FILE_ATTRIBUTE_TYPE_INVALID.
160
 *
161 162
 **/
void
163
_g_file_attribute_value_clear (GFileAttributeValue *attr)
164 165 166 167 168 169
{
  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);
170

171 172 173
  if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV)
    g_strfreev (attr->u.stringv);

174 175 176
  if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
      attr->u.obj != NULL)
    g_object_unref (attr->u.obj);
177

178 179 180
  attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
}

181
/**
182
 * g_file_attribute_value_set:
183 184
 * @attr: a #GFileAttributeValue to set the value in.
 * @new_value: a #GFileAttributeValue to get the value from.
185
 *
186
 * Sets an attribute's value from another attribute.
187 188
 **/
void
189 190
_g_file_attribute_value_set (GFileAttributeValue        *attr,
			     const GFileAttributeValue *new_value)
191 192 193 194
{
  g_return_if_fail (attr != NULL);
  g_return_if_fail (new_value != NULL);

195
  _g_file_attribute_value_clear (attr);
196 197 198 199 200
  *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);
201

202 203 204
  if (attr->type == G_FILE_ATTRIBUTE_TYPE_STRINGV)
    attr->u.stringv = g_strdupv (attr->u.stringv);

205 206 207 208 209
  if (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT &&
      attr->u.obj != NULL)
    g_object_ref (attr->u.obj);
}

210
/**
211
 * _g_file_attribute_value_new:
212
 *
213
 * Creates a new file attribute.
214
 *
215
 * Returns: a #GFileAttributeValue.
216 217
 **/
GFileAttributeValue *
218
_g_file_attribute_value_new (void)
219 220 221 222 223 224 225 226
{
  GFileAttributeValue *attr;

  attr = g_new (GFileAttributeValue, 1);
  attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
  return attr;
}

227 228 229 230 231 232 233
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;
234 235
  case G_FILE_ATTRIBUTE_TYPE_STRINGV:
    return attr->u.stringv;
236 237 238 239 240 241
  case G_FILE_ATTRIBUTE_TYPE_OBJECT:
    return attr->u.obj;
  default:
    return (gpointer) &attr->u;
  }
}
242

243
/**
244 245
 * g_file_attribute_value_dup:
 * @other: a #GFileAttributeValue to duplicate.
246
 *
247
 * Duplicates a file attribute.
248
 *
249 250 251
 * Returns: a duplicate of the @other.
 **/
GFileAttributeValue *
252
_g_file_attribute_value_dup (const GFileAttributeValue *other)
253 254 255 256 257 258 259
{
  GFileAttributeValue *attr;

  g_return_val_if_fail (other != NULL, NULL);

  attr = g_new (GFileAttributeValue, 1);
  attr->type = G_FILE_ATTRIBUTE_TYPE_INVALID;
260
  _g_file_attribute_value_set (attr, other);
261 262 263
  return attr;
}

264 265 266
G_DEFINE_BOXED_TYPE (GFileAttributeInfoList, g_file_attribute_info_list,
                     g_file_attribute_info_list_dup,
                     g_file_attribute_info_list_unref)
267

268 269 270 271 272 273 274 275 276 277 278 279 280
static gboolean
valid_char (char c)
{
  return c >= 32 && c <= 126 && c != '\\';
}

static char *
escape_byte_string (const char *str)
{
  size_t len;
  int num_invalid, i;
  char *escaped_val, *p;
  unsigned char c;
281 282
  const char hex_digits[] = "0123456789abcdef";

283
  len = strlen (str);
284

285 286 287 288 289 290
  num_invalid = 0;
  for (i = 0; i < len; i++)
    {
      if (!valid_char (str[i]))
	num_invalid++;
    }
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  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';
308
	      *p++ = hex_digits[(c >> 4) & 0xf];
309 310 311 312 313 314 315 316
	      *p++ = hex_digits[c & 0xf];
	    }
	}
      *p++ = 0;
      return escaped_val;
    }
}

317
/**
318
 * _g_file_attribute_value_as_string:
319 320 321
 * @attr: a #GFileAttributeValue.
 *
 * Converts a #GFileAttributeValue to a string for display.
322
 * The returned string should be freed when no longer needed.
323
 *
324
 * Returns: a string from the @attr, %NULL on error, or "<invalid>"
325
 * if @attr is of type %G_FILE_ATTRIBUTE_TYPE_INVALID.
326
 */
327
char *
328
_g_file_attribute_value_as_string (const GFileAttributeValue *attr)
329
{
330 331
  GString *s;
  int i;
332 333 334 335 336 337 338 339 340
  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;
341 342 343 344 345 346 347 348 349 350 351
    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;
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    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;
375 376 377
    case G_FILE_ATTRIBUTE_TYPE_INVALID:
      str = g_strdup ("<unset>");
      break;
378 379 380 381 382
    default:
      g_warning ("Invalid type in GFileInfo attribute");
      str = g_strdup ("<invalid>");
      break;
    }
383

384 385 386
  return str;
}

387
/**
388
 * _g_file_attribute_value_get_string:
389
 * @attr: a #GFileAttributeValue.
390
 *
391 392
 * Gets the string from a file attribute value. If the value is not the
 * right type then %NULL will be returned.
393
 *
394
 * Returns: the UTF-8 string value contained within the attribute, or %NULL.
395
 */
396
const char *
397
_g_file_attribute_value_get_string (const GFileAttributeValue *attr)
398 399 400 401 402 403 404 405 406
{
  if (attr == NULL)
    return NULL;

  g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_STRING, NULL);

  return attr->u.string;
}

407
/**
408
 * _g_file_attribute_value_get_byte_string:
409
 * @attr: a #GFileAttributeValue.
410
 *
411 412
 * Gets the byte string from a file attribute value. If the value is not the
 * right type then %NULL will be returned.
413
 *
414
 * Returns: the byte string contained within the attribute or %NULL.
415
 */
416
const char *
417
_g_file_attribute_value_get_byte_string (const GFileAttributeValue *attr)
418 419 420 421 422 423 424 425
{
  if (attr == NULL)
    return NULL;

  g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, NULL);

  return attr->u.string;
}
426

427 428 429 430 431 432 433 434 435 436 437
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;
}

438
/**
439
 * _g_file_attribute_value_get_boolean:
440
 * @attr: a #GFileAttributeValue.
441
 *
442 443
 * Gets the boolean value from a file attribute value. If the value is not the
 * right type then %FALSE will be returned.
444
 *
445
 * Returns: the boolean value contained within the attribute, or %FALSE.
446
 */
447
gboolean
448
_g_file_attribute_value_get_boolean (const GFileAttributeValue *attr)
449 450 451 452 453 454 455 456
{
  if (attr == NULL)
    return FALSE;

  g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_BOOLEAN, FALSE);

  return attr->u.boolean;
}
457

458
/**
459
 * _g_file_attribute_value_get_uint32:
460
 * @attr: a #GFileAttributeValue.
461 462 463 464 465 466
 *
 * 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.
 */
467
guint32
468
_g_file_attribute_value_get_uint32 (const GFileAttributeValue *attr)
469 470 471 472 473 474 475 476 477
{
  if (attr == NULL)
    return 0;

  g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT32, 0);

  return attr->u.uint32;
}

478
/**
479
 * _g_file_attribute_value_get_int32:
480
 * @attr: a #GFileAttributeValue.
481 482 483 484 485 486
 *
 * 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.
 */
487
gint32
488
_g_file_attribute_value_get_int32 (const GFileAttributeValue *attr)
489 490 491 492 493 494 495 496 497
{
  if (attr == NULL)
    return 0;

  g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT32, 0);

  return attr->u.int32;
}

498
/**
499
 * _g_file_attribute_value_get_uint64:
500
 * @attr: a #GFileAttributeValue.
501 502 503 504 505 506
 *
 * 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.
 */
507
guint64
508
_g_file_attribute_value_get_uint64 (const GFileAttributeValue *attr)
509 510 511 512 513 514 515 516 517
{
  if (attr == NULL)
    return 0;

  g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_UINT64, 0);

  return attr->u.uint64;
}

518
/**
519
 * _g_file_attribute_value_get_int64:
520
 * @attr: a #GFileAttributeValue.
521 522 523 524 525 526
 *
 * 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.
 */
527
gint64
528
_g_file_attribute_value_get_int64 (const GFileAttributeValue *attr)
529 530 531 532 533 534 535 536 537
{
  if (attr == NULL)
    return 0;

  g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_INT64, 0);

  return attr->u.int64;
}

538
/**
539
 * _g_file_attribute_value_get_object:
540
 * @attr: a #GFileAttributeValue.
541 542
 *
 * Gets the GObject from a file attribute value. If the value
543
 * is not the right type then %NULL will be returned.
544 545
 *
 * Returns: the GObject from the attribute, or %NULL.
546 547
 **/
GObject *
548
_g_file_attribute_value_get_object (const GFileAttributeValue *attr)
549 550 551 552 553 554 555 556
{
  if (attr == NULL)
    return NULL;

  g_return_val_if_fail (attr->type == G_FILE_ATTRIBUTE_TYPE_OBJECT, NULL);

  return attr->u.obj;
}
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575


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;
576

577 578 579 580 581 582 583
    case G_FILE_ATTRIBUTE_TYPE_STRINGV:
      if (dup)
	value->u.stringv = g_strdupv (value_p);
      else
	value->u.stringv = value_p;
      break;

584 585 586 587 588 589
    case G_FILE_ATTRIBUTE_TYPE_OBJECT:
      if (dup)
	value->u.obj = g_object_ref (value_p);
      else
	value->u.obj = value_p;
      break;
590

591 592 593
    case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
      value->u.boolean = *(gboolean *)value_p;
      break;
594

595 596 597
    case G_FILE_ATTRIBUTE_TYPE_UINT32:
      value->u.uint32 = *(guint32 *)value_p;
      break;
598

599 600 601
    case G_FILE_ATTRIBUTE_TYPE_INT32:
      value->u.int32 = *(gint32 *)value_p;
      break;
602

603 604 605
    case G_FILE_ATTRIBUTE_TYPE_UINT64:
      value->u.uint64 = *(guint64 *)value_p;
      break;
606

607 608 609
    case G_FILE_ATTRIBUTE_TYPE_INT64:
      value->u.int64 = *(gint64 *)value_p;
      break;
610 611 612 613

    case G_FILE_ATTRIBUTE_TYPE_INVALID:
      break;

614
    default:
615
      g_warning ("Unknown type specified in g_file_info_set_attribute");
616 617 618
      break;
    }
}
619

620
/**
621
 * _g_file_attribute_value_set_string:
622
 * @attr: a #GFileAttributeValue.
623
 * @string: a UTF-8 string to set within the type.
624
 *
625
 * Sets the attribute value to a given UTF-8 string.
626
 */
627
void
628 629
_g_file_attribute_value_set_string (GFileAttributeValue *attr,
				    const char          *string)
630 631 632 633
{
  g_return_if_fail (attr != NULL);
  g_return_if_fail (string != NULL);

634
  _g_file_attribute_value_clear (attr);
635 636 637 638
  attr->type = G_FILE_ATTRIBUTE_TYPE_STRING;
  attr->u.string = g_strdup (string);
}

639
/**
640
 * _g_file_attribute_value_set_byte_string:
641
 * @attr: a #GFileAttributeValue.
642
 * @string: a byte string to set within the type.
643
 *
644
 * Sets the attribute value to a given byte string.
645
 */
646
void
647 648
_g_file_attribute_value_set_byte_string (GFileAttributeValue *attr,
					 const char          *string)
649 650 651 652
{
  g_return_if_fail (attr != NULL);
  g_return_if_fail (string != NULL);

653
  _g_file_attribute_value_clear (attr);
654 655 656 657
  attr->type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
  attr->u.string = g_strdup (string);
}

658 659 660 661 662 663 664 665 666 667 668 669 670
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);
}


671
/**
672
 * _g_file_attribute_value_set_boolean:
673
 * @attr: a #GFileAttributeValue.
674
 * @value: a #gboolean to set within the type.
675 676 677
 *
 * Sets the attribute value to the given boolean value.
 */
678
void
679 680
_g_file_attribute_value_set_boolean (GFileAttributeValue *attr,
				     gboolean             value)
681 682 683
{
  g_return_if_fail (attr != NULL);

684
  _g_file_attribute_value_clear (attr);
685 686 687 688
  attr->type = G_FILE_ATTRIBUTE_TYPE_BOOLEAN;
  attr->u.boolean = !!value;
}

689
/**
690
 * _g_file_attribute_value_set_uint32:
691
 * @attr: a #GFileAttributeValue.
692
 * @value: a #guint32 to set within the type.
693
 *
694
 * Sets the attribute value to the given unsigned 32-bit integer.
695
 */
696
void
697 698
_g_file_attribute_value_set_uint32 (GFileAttributeValue *attr,
				    guint32              value)
699 700
{
  g_return_if_fail (attr != NULL);
701

702
  _g_file_attribute_value_clear (attr);
703 704 705 706
  attr->type = G_FILE_ATTRIBUTE_TYPE_UINT32;
  attr->u.uint32 = value;
}

707
/**
708
 * _g_file_attribute_value_set_int32:
709
 * @attr: a #GFileAttributeValue.
710
 * @value: a #gint32 to set within the type.
711
 *
712
 * Sets the attribute value to the given signed 32-bit integer.
713
 */
714
void
715 716
_g_file_attribute_value_set_int32 (GFileAttributeValue *attr,
				   gint32               value)
717 718 719
{
  g_return_if_fail (attr != NULL);

720
  _g_file_attribute_value_clear (attr);
721 722 723 724
  attr->type = G_FILE_ATTRIBUTE_TYPE_INT32;
  attr->u.int32 = value;
}

725
/**
726
 * _g_file_attribute_value_set_uint64:
727
 * @attr: a #GFileAttributeValue.
728
 * @value: a #guint64 to set within the type.
729
 *
730
 * Sets the attribute value to a given unsigned 64-bit integer.
731
 */
732
void
733 734
_g_file_attribute_value_set_uint64 (GFileAttributeValue *attr,
				    guint64              value)
735 736 737
{
  g_return_if_fail (attr != NULL);

738
  _g_file_attribute_value_clear (attr);
739 740 741 742
  attr->type = G_FILE_ATTRIBUTE_TYPE_UINT64;
  attr->u.uint64 = value;
}

743
/**
744
 * _g_file_attribute_value_set_int64:
745
 * @attr: a #GFileAttributeValue.
746
 * @value: a #gint64 to set within the type.
747 748 749
 *
 * Sets the attribute value to a given signed 64-bit integer.
 */
750
void
751 752
_g_file_attribute_value_set_int64 (GFileAttributeValue *attr,
				   gint64               value)
753 754 755
{
  g_return_if_fail (attr != NULL);

756
  _g_file_attribute_value_clear (attr);
757 758 759 760
  attr->type = G_FILE_ATTRIBUTE_TYPE_INT64;
  attr->u.int64 = value;
}

761
/**
762
 * _g_file_attribute_value_set_object:
763 764 765
 * @attr: a #GFileAttributeValue.
 * @obj: a #GObject.
 *
766 767
 * Sets the attribute to contain the value @obj.
 * The @attr references the GObject internally.
768
 */
769
void
770 771
_g_file_attribute_value_set_object (GFileAttributeValue *attr,
				    GObject             *obj)
772 773 774 775
{
  g_return_if_fail (attr != NULL);
  g_return_if_fail (obj != NULL);

776
  _g_file_attribute_value_clear (attr);
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
  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:
796
 *
797
 * Creates a new file attribute info list.
798
 *
799
 * Returns: a #GFileAttributeInfoList.
800
 */
801 802 803 804 805 806
GFileAttributeInfoList *
g_file_attribute_info_list_new (void)
{
  GFileAttributeInfoListPriv *priv;

  priv = g_new0 (GFileAttributeInfoListPriv, 1);
807

808 809
  priv->ref_count = 1;
  priv->array = g_array_new (TRUE, FALSE, sizeof (GFileAttributeInfo));
810

811
  list_update_public (priv);
812

813 814 815 816 817 818
  return (GFileAttributeInfoList *)priv;
}

/**
 * g_file_attribute_info_list_dup:
 * @list: a #GFileAttributeInfoList to duplicate.
819
 *
820
 * Makes a duplicate of a file attribute info list.
821 822 823
 *
 * Returns: a copy of the given @list.
 */
824 825 826 827 828
GFileAttributeInfoList *
g_file_attribute_info_list_dup (GFileAttributeInfoList *list)
{
  GFileAttributeInfoListPriv *new;
  int i;
829

830 831 832 833 834 835 836 837 838 839 840 841 842 843
  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;
    }
844

845 846 847 848 849 850
  return (GFileAttributeInfoList *)new;
}

/**
 * g_file_attribute_info_list_ref:
 * @list: a #GFileAttributeInfoList to reference.
851
 *
852
 * References a file attribute info list.
853
 *
854
 * Returns: #GFileAttributeInfoList or %NULL on error.
855
 */
856 857 858 859
GFileAttributeInfoList *
g_file_attribute_info_list_ref (GFileAttributeInfoList *list)
{
  GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
860

861 862
  g_return_val_if_fail (list != NULL, NULL);
  g_return_val_if_fail (priv->ref_count > 0, NULL);
863

864
  g_atomic_int_inc (&priv->ref_count);
865

866 867 868 869 870 871
  return list;
}

/**
 * g_file_attribute_info_list_unref:
 * @list: The #GFileAttributeInfoList to unreference.
872
 *
873 874
 * Removes a reference from the given @list. If the reference count
 * falls to zero, the @list is deleted.
875
 */
876 877 878 879 880
void
g_file_attribute_info_list_unref (GFileAttributeInfoList *list)
{
  GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
  int i;
881

882 883
  g_return_if_fail (list != NULL);
  g_return_if_fail (priv->ref_count > 0);
884

885 886 887 888 889
  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);
890
      g_free (list);
891 892 893 894 895 896 897 898
    }
}

static int
g_file_attribute_info_list_bsearch (GFileAttributeInfoList *list,
				    const char             *name)
{
  int start, end, mid;
899

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
  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 lookup.
921
 *
Matthias Clasen's avatar
Matthias Clasen committed
922 923
 * Gets the file attribute with the name @name from @list.
 *
924
 * Returns: a #GFileAttributeInfo for the @name, or %NULL if an
925
 * attribute isn't found.
926
 */
927 928 929 930 931
const GFileAttributeInfo *
g_file_attribute_info_list_lookup (GFileAttributeInfoList *list,
				   const char             *name)
{
  int i;
932

933 934
  g_return_val_if_fail (list != NULL, NULL);
  g_return_val_if_fail (name != NULL, NULL);
935

936 937 938 939
  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];
940

941 942 943 944 945 946 947 948
  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.
949
 * @flags: #GFileAttributeInfoFlags for the attribute.
950
 *
951
 * Adds a new attribute with @name to the @list, setting
952 953
 * its @type and @flags.
 */
954
void
955 956 957 958
g_file_attribute_info_list_add (GFileAttributeInfoList *list,
				const char             *name,
				GFileAttributeType      type,
				GFileAttributeInfoFlags flags)
959 960 961 962
{
  GFileAttributeInfoListPriv *priv = (GFileAttributeInfoListPriv *)list;
  GFileAttributeInfo info;
  int i;
963

964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
  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);
}