gfileinfo.c 69.4 KB
Newer Older
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3 4 5 6 7 8 9 10 11 12 13 14 15
 * 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
 * version 2 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
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 22
/**
 * SECTION:gfileinfo
23
 * @short_description: File Information and Attributes
Matthias Clasen's avatar
Matthias Clasen committed
24
 * @include: gio/gio.h
25
 * @see_also: #GFile, [GFileAttribute][gio-GFileAttribute]
26
 *
27
 * Functionality for manipulating basic metadata for files. #GFileInfo
28 29
 * implements methods for getting information that all files should
 * contain, and allows for manipulation of extended attributes.
Matthias Clasen's avatar
Matthias Clasen committed
30
 *
31
 * See [GFileAttribute][gio-GFileAttribute] for more information on how
32
 * GIO handles file attributes.
Matthias Clasen's avatar
Matthias Clasen committed
33
 *
34 35 36
 * To obtain a #GFileInfo for a #GFile, use g_file_query_info() (or its
 * async variant). To obtain a #GFileInfo for a file input or output
 * stream, use g_file_input_stream_query_info() or
Matthias Clasen's avatar
Matthias Clasen committed
37 38
 * g_file_output_stream_query_info() (or their async variants).
 *
39 40
 * To change the actual attributes of a file, you should then set the
 * attribute in the #GFileInfo and call g_file_set_attributes_from_info()
41 42
 * or g_file_set_attributes_async() on a GFile.
 *
43 44 45 46
 * However, not all attributes can be changed in the file. For instance,
 * the actual size of a file cannot be changed via g_file_info_set_size().
 * You may call g_file_query_settable_attributes() and
 * g_file_query_writable_namespaces() to discover the settable attributes
47 48
 * of a particular file at runtime.
 *
49
 * #GFileAttributeMatcher allows for searching through a #GFileInfo for
50 51 52
 * attributes.
 **/

53
#include "config.h"
54 55 56 57

#include <string.h>

#include "gfileinfo.h"
58
#include "gfileinfo-priv.h"
59
#include "gfileattribute-priv.h"
60
#include "gicon.h"
61 62
#include "glibintl.h"

63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/* We use this nasty thing, because NULL is a valid attribute matcher (matches nothing) */
#define NO_ATTRIBUTE_MASK ((GFileAttributeMatcher *)1)

typedef struct  {
  guint32 attribute;
  GFileAttributeValue value;
} GFileAttribute;

struct _GFileInfo
{
  GObject parent_instance;

  GArray *attributes;
  GFileAttributeMatcher *mask;
};

struct _GFileInfoClass
{
  GObjectClass parent_class;
};

85

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
G_DEFINE_TYPE (GFileInfo, g_file_info, G_TYPE_OBJECT);

typedef struct {
  guint32 id;
  guint32 attribute_id_counter;
} NSInfo;

G_LOCK_DEFINE_STATIC (attribute_hash);
static int namespace_id_counter = 0;
static GHashTable *ns_hash = NULL;
static GHashTable *attribute_hash = NULL;
static char ***attributes = NULL;

/* Attribute ids are 32bit, we split it up like this:
 * |------------|--------------------|
101 102
 *   12 bit          20 bit
 *   namespace      attribute id
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
 *
 * This way the attributes gets sorted in namespace order
 */

#define NS_POS 20
#define NS_MASK ((guint32)((1<<12) - 1))
#define ID_POS 0
#define ID_MASK ((guint32)((1<<20) - 1))

#define GET_NS(_attr_id) \
    (((guint32) (_attr_id) >> NS_POS) & NS_MASK)
#define GET_ID(_attr_id) \
    (((guint32)(_attr_id) >> ID_POS) & ID_MASK)

#define MAKE_ATTR_ID(_ns, _id)				\
    ( ((((guint32) _ns) & NS_MASK) << NS_POS) |		\
      ((((guint32) _id) & ID_MASK) << ID_POS) )

static NSInfo *
_lookup_namespace (const char *namespace)
{
  NSInfo *ns_info;
125

126 127 128 129 130 131 132
  ns_info = g_hash_table_lookup (ns_hash, namespace);
  if (ns_info == NULL)
    {
      ns_info = g_new0 (NSInfo, 1);
      ns_info->id = ++namespace_id_counter;
      g_hash_table_insert (ns_hash, g_strdup (namespace), ns_info);
      attributes = g_realloc (attributes, (ns_info->id + 1) * sizeof (char **));
133 134
      attributes[ns_info->id] = g_new (char *, 1);
      attributes[ns_info->id][0] = g_strconcat (namespace, "::*", NULL);
135 136 137 138
    }
  return ns_info;
}

139 140 141 142 143 144 145
static guint32
_lookup_attribute (const char *attribute)
{
  guint32 attr_id, id;
  char *ns;
  const char *colon;
  NSInfo *ns_info;
146

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  attr_id = GPOINTER_TO_UINT (g_hash_table_lookup (attribute_hash, attribute));

  if (attr_id != 0)
    return attr_id;

  colon = strstr (attribute, "::");
  if (colon)
    ns = g_strndup (attribute, colon - attribute);
  else
    ns = g_strdup ("");

  ns_info = _lookup_namespace (ns);
  g_free (ns);

  id = ++ns_info->attribute_id_counter;
  attributes[ns_info->id] = g_realloc (attributes[ns_info->id], (id + 1) * sizeof (char *));
  attributes[ns_info->id][id] = g_strdup (attribute);
164

165 166 167 168 169 170 171
  attr_id = MAKE_ATTR_ID (ns_info->id, id);

  g_hash_table_insert (attribute_hash, attributes[ns_info->id][id], GUINT_TO_POINTER (attr_id));

  return attr_id;
}

172 173 174 175 176 177 178 179
static void
ensure_attribute_hash (void)
{
  if (attribute_hash != NULL)
    return;

  ns_hash = g_hash_table_new (g_str_hash, g_str_equal);
  attribute_hash = g_hash_table_new (g_str_hash, g_str_equal);
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
#define REGISTER_ATTRIBUTE(name) G_STMT_START{\
  guint _u = _lookup_attribute (G_FILE_ATTRIBUTE_ ## name); \
  /* use for generating the ID: g_print ("#define G_FILE_ATTRIBUTE_ID_%s (%u + %u)\n", #name + 17, _u & ~ID_MASK, _u & ID_MASK); */ \
  g_assert (_u == G_FILE_ATTRIBUTE_ID_ ## name); \
}G_STMT_END

  REGISTER_ATTRIBUTE (STANDARD_TYPE);
  REGISTER_ATTRIBUTE (STANDARD_IS_HIDDEN);
  REGISTER_ATTRIBUTE (STANDARD_IS_BACKUP);
  REGISTER_ATTRIBUTE (STANDARD_IS_SYMLINK);
  REGISTER_ATTRIBUTE (STANDARD_IS_VIRTUAL);
  REGISTER_ATTRIBUTE (STANDARD_NAME);
  REGISTER_ATTRIBUTE (STANDARD_DISPLAY_NAME);
  REGISTER_ATTRIBUTE (STANDARD_EDIT_NAME);
  REGISTER_ATTRIBUTE (STANDARD_COPY_NAME);
  REGISTER_ATTRIBUTE (STANDARD_DESCRIPTION);
  REGISTER_ATTRIBUTE (STANDARD_ICON);
  REGISTER_ATTRIBUTE (STANDARD_CONTENT_TYPE);
  REGISTER_ATTRIBUTE (STANDARD_FAST_CONTENT_TYPE);
  REGISTER_ATTRIBUTE (STANDARD_SIZE);
  REGISTER_ATTRIBUTE (STANDARD_ALLOCATED_SIZE);
  REGISTER_ATTRIBUTE (STANDARD_SYMLINK_TARGET);
  REGISTER_ATTRIBUTE (STANDARD_TARGET_URI);
  REGISTER_ATTRIBUTE (STANDARD_SORT_ORDER);
205
  REGISTER_ATTRIBUTE (STANDARD_SYMBOLIC_ICON);
206
  REGISTER_ATTRIBUTE (STANDARD_IS_VOLATILE);
207 208 209 210 211 212 213 214 215 216 217 218 219
  REGISTER_ATTRIBUTE (ETAG_VALUE);
  REGISTER_ATTRIBUTE (ID_FILE);
  REGISTER_ATTRIBUTE (ID_FILESYSTEM);
  REGISTER_ATTRIBUTE (ACCESS_CAN_READ);
  REGISTER_ATTRIBUTE (ACCESS_CAN_WRITE);
  REGISTER_ATTRIBUTE (ACCESS_CAN_EXECUTE);
  REGISTER_ATTRIBUTE (ACCESS_CAN_DELETE);
  REGISTER_ATTRIBUTE (ACCESS_CAN_TRASH);
  REGISTER_ATTRIBUTE (ACCESS_CAN_RENAME);
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_MOUNT);
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_UNMOUNT);
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_EJECT);
  REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE);
220
  REGISTER_ATTRIBUTE (MOUNTABLE_UNIX_DEVICE_FILE);
221 222
  REGISTER_ATTRIBUTE (MOUNTABLE_HAL_UDI);
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START);
223
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_START_DEGRADED);
224 225
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_STOP);
  REGISTER_ATTRIBUTE (MOUNTABLE_START_STOP_TYPE);
226 227
  REGISTER_ATTRIBUTE (MOUNTABLE_CAN_POLL);
  REGISTER_ATTRIBUTE (MOUNTABLE_IS_MEDIA_CHECK_AUTOMATIC);
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
  REGISTER_ATTRIBUTE (TIME_MODIFIED);
  REGISTER_ATTRIBUTE (TIME_MODIFIED_USEC);
  REGISTER_ATTRIBUTE (TIME_ACCESS);
  REGISTER_ATTRIBUTE (TIME_ACCESS_USEC);
  REGISTER_ATTRIBUTE (TIME_CHANGED);
  REGISTER_ATTRIBUTE (TIME_CHANGED_USEC);
  REGISTER_ATTRIBUTE (TIME_CREATED);
  REGISTER_ATTRIBUTE (TIME_CREATED_USEC);
  REGISTER_ATTRIBUTE (UNIX_DEVICE);
  REGISTER_ATTRIBUTE (UNIX_INODE);
  REGISTER_ATTRIBUTE (UNIX_MODE);
  REGISTER_ATTRIBUTE (UNIX_NLINK);
  REGISTER_ATTRIBUTE (UNIX_UID);
  REGISTER_ATTRIBUTE (UNIX_GID);
  REGISTER_ATTRIBUTE (UNIX_RDEV);
  REGISTER_ATTRIBUTE (UNIX_BLOCK_SIZE);
  REGISTER_ATTRIBUTE (UNIX_BLOCKS);
  REGISTER_ATTRIBUTE (UNIX_IS_MOUNTPOINT);
  REGISTER_ATTRIBUTE (DOS_IS_ARCHIVE);
  REGISTER_ATTRIBUTE (DOS_IS_SYSTEM);
  REGISTER_ATTRIBUTE (OWNER_USER);
  REGISTER_ATTRIBUTE (OWNER_USER_REAL);
  REGISTER_ATTRIBUTE (OWNER_GROUP);
  REGISTER_ATTRIBUTE (THUMBNAIL_PATH);
  REGISTER_ATTRIBUTE (THUMBNAILING_FAILED);
253
  REGISTER_ATTRIBUTE (THUMBNAIL_IS_VALID);
254 255 256 257 258 259 260 261 262
  REGISTER_ATTRIBUTE (PREVIEW_ICON);
  REGISTER_ATTRIBUTE (FILESYSTEM_SIZE);
  REGISTER_ATTRIBUTE (FILESYSTEM_FREE);
  REGISTER_ATTRIBUTE (FILESYSTEM_TYPE);
  REGISTER_ATTRIBUTE (FILESYSTEM_READONLY);
  REGISTER_ATTRIBUTE (FILESYSTEM_USE_PREVIEW);
  REGISTER_ATTRIBUTE (GVFS_BACKEND);
  REGISTER_ATTRIBUTE (SELINUX_CONTEXT);
  REGISTER_ATTRIBUTE (TRASH_ITEM_COUNT);
263 264
  REGISTER_ATTRIBUTE (TRASH_ORIG_PATH);
  REGISTER_ATTRIBUTE (TRASH_DELETION_DATE);
265 266

#undef REGISTER_ATTRIBUTE
267 268
}

269 270 271 272 273
static guint32
lookup_namespace (const char *namespace)
{
  NSInfo *ns_info;
  guint32 id;
274

275
  G_LOCK (attribute_hash);
276

277
  ensure_attribute_hash ();
278 279 280 281 282

  ns_info = _lookup_namespace (namespace);
  id = 0;
  if (ns_info)
    id = ns_info->id;
283

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
  G_UNLOCK (attribute_hash);

  return id;
}

static char *
get_attribute_for_id (int attribute)
{
  char *s;
  G_LOCK (attribute_hash);
  s = attributes[GET_NS(attribute)][GET_ID(attribute)];
  G_UNLOCK (attribute_hash);
  return s;
}

static guint32
lookup_attribute (const char *attribute)
{
302
  guint32 attr_id;
303

304
  G_LOCK (attribute_hash);
305
  ensure_attribute_hash ();
306

307
  attr_id = _lookup_attribute (attribute);
308

309
  G_UNLOCK (attribute_hash);
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324
  return attr_id;
}

static void
g_file_info_finalize (GObject *object)
{
  GFileInfo *info;
  int i;
  GFileAttribute *attrs;

  info = G_FILE_INFO (object);

  attrs = (GFileAttribute *)info->attributes->data;
  for (i = 0; i < info->attributes->len; i++)
325
    _g_file_attribute_value_clear (&attrs[i].value);
326
  g_array_free (info->attributes, TRUE);
327 328 329

  if (info->mask != NO_ATTRIBUTE_MASK)
    g_file_attribute_matcher_unref (info->mask);
330 331

  G_OBJECT_CLASS (g_file_info_parent_class)->finalize (object);
332 333 334 335 336 337
}

static void
g_file_info_class_init (GFileInfoClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
338

339 340 341 342 343 344 345 346 347 348 349 350 351
  gobject_class->finalize = g_file_info_finalize;
}

static void
g_file_info_init (GFileInfo *info)
{
  info->mask = NO_ATTRIBUTE_MASK;
  info->attributes = g_array_new (FALSE, FALSE,
				  sizeof (GFileAttribute));
}

/**
 * g_file_info_new:
352
 *
353
 * Creates a new file info structure.
354
 *
355
 * Returns: a #GFileInfo.
356 357 358 359 360 361 362 363 364 365 366
 **/
GFileInfo *
g_file_info_new (void)
{
  return g_object_new (G_TYPE_FILE_INFO, NULL);
}

/**
 * g_file_info_copy_into:
 * @src_info: source to copy attributes from.
 * @dest_info: destination to copy attributes to.
367
 *
368 369
 * First clears all of the [GFileAttribute][gio-GFileAttribute] of @dest_info,
 * and then copies all of the file attributes from @src_info to @dest_info.
370 371
 **/
void
372
g_file_info_copy_into (GFileInfo *src_info,
Matthias Clasen's avatar
Matthias Clasen committed
373
                       GFileInfo *dest_info)
374 375 376 377 378 379 380 381 382
{
  GFileAttribute *source, *dest;
  int i;

  g_return_if_fail (G_IS_FILE_INFO (src_info));
  g_return_if_fail (G_IS_FILE_INFO (dest_info));

  dest = (GFileAttribute *)dest_info->attributes->data;
  for (i = 0; i < dest_info->attributes->len; i++)
383
    _g_file_attribute_value_clear (&dest[i].value);
384

385 386 387 388 389
  g_array_set_size (dest_info->attributes,
		    src_info->attributes->len);

  source = (GFileAttribute *)src_info->attributes->data;
  dest = (GFileAttribute *)dest_info->attributes->data;
390

391 392 393 394
  for (i = 0; i < src_info->attributes->len; i++)
    {
      dest[i].attribute = source[i].attribute;
      dest[i].value.type = G_FILE_ATTRIBUTE_TYPE_INVALID;
395
      _g_file_attribute_value_set (&dest[i].value, &source[i].value);
396 397
    }

398 399 400
  if (dest_info->mask != NO_ATTRIBUTE_MASK)
    g_file_attribute_matcher_unref (dest_info->mask);

401 402 403 404 405 406 407 408 409
  if (src_info->mask == NO_ATTRIBUTE_MASK)
    dest_info->mask = NO_ATTRIBUTE_MASK;
  else
    dest_info->mask = g_file_attribute_matcher_ref (src_info->mask);
}

/**
 * g_file_info_dup:
 * @other: a #GFileInfo.
410
 *
411
 * Duplicates a file info structure.
412
 *
413
 * Returns: (transfer full): a duplicate #GFileInfo of @other.
414 415
 **/
GFileInfo *
Matthias Clasen's avatar
Matthias Clasen committed
416
g_file_info_dup (GFileInfo *other)
417 418
{
  GFileInfo *new;
419

420
  g_return_val_if_fail (G_IS_FILE_INFO (other), NULL);
421

422 423 424 425 426 427 428 429 430 431 432 433 434
  new = g_file_info_new ();
  g_file_info_copy_into (other, new);
  return new;
}

/**
 * g_file_info_set_attribute_mask:
 * @info: a #GFileInfo.
 * @mask: a #GFileAttributeMatcher.
 *
 * Sets @mask on @info to match specific attribute types.
 **/
void
Matthias Clasen's avatar
Matthias Clasen committed
435
g_file_info_set_attribute_mask (GFileInfo             *info,
436 437 438 439
				GFileAttributeMatcher *mask)
{
  GFileAttribute *attr;
  int i;
440

441
  g_return_if_fail (G_IS_FILE_INFO (info));
442

443 444 445 446 447 448 449 450 451 452
  if (mask != info->mask)
    {
      if (info->mask != NO_ATTRIBUTE_MASK)
	g_file_attribute_matcher_unref (info->mask);
      info->mask = g_file_attribute_matcher_ref (mask);

      /* Remove non-matching attributes */
      for (i = 0; i < info->attributes->len; i++)
	{
	  attr = &g_array_index (info->attributes, GFileAttribute, i);
453
	  if (!_g_file_attribute_matcher_matches_id (mask,
454 455
						    attr->attribute))
	    {
456
	      _g_file_attribute_value_clear (&attr->value);
457 458 459 460 461 462 463 464 465 466
	      g_array_remove_index (info->attributes, i);
	      i--;
	    }
	}
    }
}

/**
 * g_file_info_unset_attribute_mask:
 * @info: #GFileInfo.
467
 *
468 469
 * Unsets a mask set by g_file_info_set_attribute_mask(), if one
 * is set.
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
 **/
void
g_file_info_unset_attribute_mask (GFileInfo *info)
{
  g_return_if_fail (G_IS_FILE_INFO (info));

  if (info->mask != NO_ATTRIBUTE_MASK)
    g_file_attribute_matcher_unref (info->mask);
  info->mask = NO_ATTRIBUTE_MASK;
}

/**
 * g_file_info_clear_status:
 * @info: a #GFileInfo.
 *
 * Clears the status information from @info.
 **/
void
g_file_info_clear_status (GFileInfo  *info)
{
  GFileAttribute *attrs;
  int i;
492

493 494 495 496 497 498 499 500 501
  g_return_if_fail (G_IS_FILE_INFO (info));

  attrs = (GFileAttribute *)info->attributes->data;
  for (i = 0; i < info->attributes->len; i++)
    attrs[i].value.status = G_FILE_ATTRIBUTE_STATUS_UNSET;
}

static int
g_file_info_find_place (GFileInfo  *info,
Matthias Clasen's avatar
Matthias Clasen committed
502
			guint32     attribute)
503 504 505
{
  int min, max, med;
  GFileAttribute *attrs;
506
  /* Binary search for the place where attribute would be, if it's
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
     in the array */

  min = 0;
  max = info->attributes->len;

  attrs = (GFileAttribute *)info->attributes->data;

  while (min < max)
    {
      med = min + (max - min) / 2;
      if (attrs[med].attribute == attribute)
	{
	  min = med;
	  break;
	}
      else if (attrs[med].attribute < attribute)
	min = med + 1;
      else /* attrs[med].attribute > attribute */
	max = med;
    }

  return min;
}

static GFileAttributeValue *
g_file_info_find_value (GFileInfo *info,
Matthias Clasen's avatar
Matthias Clasen committed
533
			guint32    attr_id)
534 535 536 537 538 539 540 541 542
{
  GFileAttribute *attrs;
  int i;

  i = g_file_info_find_place (info, attr_id);
  attrs = (GFileAttribute *)info->attributes->data;
  if (i < info->attributes->len &&
      attrs[i].attribute == attr_id)
    return &attrs[i].value;
543

544 545 546 547
  return NULL;
}

static GFileAttributeValue *
Matthias Clasen's avatar
Matthias Clasen committed
548
g_file_info_find_value_by_name (GFileInfo  *info,
549 550 551 552 553 554 555 556 557 558 559
				const char *attribute)
{
  guint32 attr_id;

  attr_id = lookup_attribute (attribute);
  return g_file_info_find_value (info, attr_id);
}

/**
 * g_file_info_has_attribute:
 * @info: a #GFileInfo.
560
 * @attribute: a file attribute key.
561
 *
562
 * Checks if a file info structure has an attribute named @attribute.
563 564
 *
 * Returns: %TRUE if @Ginfo has an attribute named @attribute,
Matthias Clasen's avatar
Matthias Clasen committed
565
 *     %FALSE otherwise.
566 567 568 569 570 571 572 573 574 575 576 577 578 579
 **/
gboolean
g_file_info_has_attribute (GFileInfo  *info,
			   const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);

  value = g_file_info_find_value_by_name (info, attribute);
  return value != NULL;
}

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
/**
 * g_file_info_has_namespace:
 * @info: a #GFileInfo.
 * @name_space: a file attribute namespace.
 *
 * Checks if a file info structure has an attribute in the
 * specified @name_space.
 *
 * Returns: %TRUE if @Ginfo has an attribute in @name_space,
 *     %FALSE otherwise.
 *
 * Since: 2.22
 **/
gboolean
g_file_info_has_namespace (GFileInfo  *info,
			   const char *name_space)
{
  GFileAttribute *attrs;
  guint32 ns_id;
  int i;

  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
  g_return_val_if_fail (name_space != NULL, FALSE);

  ns_id = lookup_namespace (name_space);

  attrs = (GFileAttribute *)info->attributes->data;
  for (i = 0; i < info->attributes->len; i++)
    {
      if (GET_NS (attrs[i].attribute) == ns_id)
	return TRUE;
    }

  return FALSE;
}

616 617 618
/**
 * g_file_info_list_attributes:
 * @info: a #GFileInfo.
619 620
 * @name_space: (nullable): a file attribute key's namespace, or %NULL to list
 *   all attributes.
621
 *
622
 * Lists the file info structure's attributes.
623
 *
624 625 626
 * Returns: (nullable) (array zero-terminated=1) (transfer full): a
 * null-terminated array of strings of all of the possible attribute
 * types for the given @name_space, or %NULL on error.
627 628 629 630 631 632 633 634 635 636
 **/
char **
g_file_info_list_attributes (GFileInfo  *info,
			     const char *name_space)
{
  GPtrArray *names;
  GFileAttribute *attrs;
  guint32 attribute;
  guint32 ns_id = (name_space) ? lookup_namespace (name_space) : 0;
  int i;
637

638 639 640 641 642 643 644 645 646 647 648 649 650
  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);

  names = g_ptr_array_new ();
  attrs = (GFileAttribute *)info->attributes->data;
  for (i = 0; i < info->attributes->len; i++)
    {
      attribute = attrs[i].attribute;
      if (ns_id == 0 || GET_NS (attribute) == ns_id)
        g_ptr_array_add (names, g_strdup (get_attribute_for_id (attribute)));
    }

  /* NULL terminate */
  g_ptr_array_add (names, NULL);
651

652 653 654 655 656 657
  return (char **)g_ptr_array_free (names, FALSE);
}

/**
 * g_file_info_get_attribute_type:
 * @info: a #GFileInfo.
658
 * @attribute: a file attribute key.
659
 *
660
 * Gets the attribute type for an attribute key.
661 662
 *
 * Returns: a #GFileAttributeType for the given @attribute, or
663
 * %G_FILE_ATTRIBUTE_TYPE_INVALID if the key is not set.
664 665 666 667 668 669
 **/
GFileAttributeType
g_file_info_get_attribute_type (GFileInfo  *info,
				const char *attribute)
{
  GFileAttributeValue *value;
670

671 672 673 674 675 676 677 678 679 680 681 682 683
  g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_ATTRIBUTE_TYPE_INVALID);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', G_FILE_ATTRIBUTE_TYPE_INVALID);

  value = g_file_info_find_value_by_name (info, attribute);
  if (value)
    return value->type;
  else
    return G_FILE_ATTRIBUTE_TYPE_INVALID;
}

/**
 * g_file_info_remove_attribute:
 * @info: a #GFileInfo.
684
 * @attribute: a file attribute key.
685
 *
686
 * Removes all cases of @attribute from @info if it exists.
687 688 689 690 691 692 693 694 695 696 697 698 699
 **/
void
g_file_info_remove_attribute (GFileInfo  *info,
			      const char *attribute)
{
  guint32 attr_id;
  GFileAttribute *attrs;
  int i;

  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

  attr_id = lookup_attribute (attribute);
700

701 702 703 704 705
  i = g_file_info_find_place (info, attr_id);
  attrs = (GFileAttribute *)info->attributes->data;
  if (i < info->attributes->len &&
      attrs[i].attribute == attr_id)
    {
706
      _g_file_attribute_value_clear (&attrs[i].value);
707 708 709 710
      g_array_remove_index (info->attributes, i);
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
711 712 713 714
/**
 * g_file_info_get_attribute_data:
 * @info: a #GFileInfo
 * @attribute: a file attribute key
715 716 717 718
 * @type: (out) (optional): return location for the attribute type, or %NULL
 * @value_pp: (out) (optional) (not nullable): return location for the
 *    attribute value, or %NULL; the attribute value will not be %NULL
 * @status: (out) (optional): return location for the attribute status, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
719 720 721
 *
 * Gets the attribute type, value and status for an attribute key.
 *
722
 * Returns: (transfer none): %TRUE if @info has an attribute named @attribute,
Matthias Clasen's avatar
Matthias Clasen committed
723 724
 *      %FALSE otherwise.
 */
725
gboolean
Matthias Clasen's avatar
Matthias Clasen committed
726 727 728 729
g_file_info_get_attribute_data (GFileInfo            *info,
				const char           *attribute,
				GFileAttributeType   *type,
				gpointer             *value_pp,
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
				GFileAttributeStatus *status)
{
  GFileAttributeValue *value;

  value = g_file_info_find_value_by_name (info, attribute);
  if (value == NULL)
    return FALSE;

  if (status)
    *status = value->status;

  if (type)
    *type = value->type;

  if (value_pp)
    *value_pp = _g_file_attribute_value_peek_as_pointer (value);
746

747 748 749
  return TRUE;
}

750
/**
Matthias Clasen's avatar
Matthias Clasen committed
751 752 753 754 755 756
 * g_file_info_get_attribute_status:
 * @info: a #GFileInfo
 * @attribute: a file attribute key
 *
 * Gets the attribute status for an attribute key.
 *
757
 * Returns: a #GFileAttributeStatus for the given @attribute, or
Matthias Clasen's avatar
Matthias Clasen committed
758 759 760
 *    %G_FILE_ATTRIBUTE_STATUS_UNSET if the key is invalid.
 *
 */
761 762 763 764 765
GFileAttributeStatus
g_file_info_get_attribute_status (GFileInfo  *info,
				  const char *attribute)
{
  GFileAttributeValue *val;
766

767 768 769 770 771 772 773
  g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);

  val = g_file_info_find_value_by_name (info, attribute);
  if (val)
    return val->status;

Matthias Clasen's avatar
Matthias Clasen committed
774
  return G_FILE_ATTRIBUTE_STATUS_UNSET;
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
/**
 * g_file_info_set_attribute_status:
 * @info: a #GFileInfo
 * @attribute: a file attribute key
 * @status: a #GFileAttributeStatus
 *
 * Sets the attribute status for an attribute key. This is only
 * needed by external code that implement g_file_set_attributes_from_info()
 * or similar functions.
 *
 * The attribute must exist in @info for this to work. Otherwise %FALSE
 * is returned and @info is unchanged.
 *
 * Returns: %TRUE if the status was changed, %FALSE if the key was not set.
 *
 * Since: 2.22
 */
gboolean
g_file_info_set_attribute_status (GFileInfo  *info,
				  const char *attribute,
				  GFileAttributeStatus status)
{
  GFileAttributeValue *val;

Matthias Clasen's avatar
Matthias Clasen committed
801 802
  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
803 804 805 806 807 808 809 810 811 812

  val = g_file_info_find_value_by_name (info, attribute);
  if (val)
    {
      val->status = status;
      return TRUE;
    }

  return FALSE;
}
813

814
GFileAttributeValue *
815 816
_g_file_info_get_attribute_value (GFileInfo  *info,
				  const char *attribute)
817

818 819 820 821 822 823 824
{
  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);

  return g_file_info_find_value_by_name (info, attribute);
}

825 826 827 828
/**
 * g_file_info_get_attribute_as_string:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
829
 *
830 831 832
 * Gets the value of a attribute, formated as a string.
 * This escapes things as needed to make the string valid
 * utf8.
833
 *
Matthias Clasen's avatar
Matthias Clasen committed
834 835
 * Returns: a UTF-8 string associated with the given @attribute.
 *    When you're done with the string it must be freed with g_free().
836 837 838 839 840 841 842
 **/
char *
g_file_info_get_attribute_as_string (GFileInfo  *info,
				     const char *attribute)
{
  GFileAttributeValue *val;
  val = _g_file_info_get_attribute_value (info, attribute);
843
  if (val)
844 845 846 847 848
    return _g_file_attribute_value_as_string (val);
  return NULL;
}


849 850 851
/**
 * g_file_info_get_attribute_object:
 * @info: a #GFileInfo.
852
 * @attribute: a file attribute key.
853 854
 *
 * Gets the value of a #GObject attribute. If the attribute does
855
 * not contain a #GObject, %NULL will be returned.
856
 *
857
 * Returns: (transfer none): a #GObject associated with the given @attribute, or
858 859 860 861 862 863 864
 * %NULL otherwise.
 **/
GObject *
g_file_info_get_attribute_object (GFileInfo  *info,
				  const char *attribute)
{
  GFileAttributeValue *value;
865

866 867 868 869
  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);

  value = g_file_info_find_value_by_name (info, attribute);
870
  return _g_file_attribute_value_get_object (value);
871 872 873 874 875
}

/**
 * g_file_info_get_attribute_string:
 * @info: a #GFileInfo.
876
 * @attribute: a file attribute key.
877 878
 *
 * Gets the value of a string attribute. If the attribute does
879
 * not contain a string, %NULL will be returned.
880 881
 *
 * Returns: the contents of the @attribute value as a UTF-8 string, or
882 883 884 885 886 887 888
 * %NULL otherwise.
 **/
const char *
g_file_info_get_attribute_string (GFileInfo  *info,
				  const char *attribute)
{
  GFileAttributeValue *value;
889

890 891 892 893
  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);

  value = g_file_info_find_value_by_name (info, attribute);
894
  return _g_file_attribute_value_get_string (value);
895 896 897 898 899
}

/**
 * g_file_info_get_attribute_byte_string:
 * @info: a #GFileInfo.
900
 * @attribute: a file attribute key.
901 902
 *
 * Gets the value of a byte string attribute. If the attribute does
903
 * not contain a byte string, %NULL will be returned.
904 905
 *
 * Returns: the contents of the @attribute value as a byte string, or
906 907 908 909 910 911 912
 * %NULL otherwise.
 **/
const char *
g_file_info_get_attribute_byte_string (GFileInfo  *info,
				       const char *attribute)
{
  GFileAttributeValue *value;
913

914 915 916 917
  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);

  value = g_file_info_find_value_by_name (info, attribute);
918
  return _g_file_attribute_value_get_byte_string (value);
919 920
}

921 922 923 924 925 926 927 928
/**
 * g_file_info_get_attribute_stringv:
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 *
 * Gets the value of a stringv attribute. If the attribute does
 * not contain a stringv, %NULL will be returned.
 *
929
 * Returns: (transfer none): the contents of the @attribute value as a stringv, or
930
 * %NULL otherwise. Do not free. These returned strings are UTF-8.
Matthias Clasen's avatar
Matthias Clasen committed
931 932
 *
 * Since: 2.22
933 934 935 936 937 938 939 940 941 942 943 944 945 946
 **/
char **
g_file_info_get_attribute_stringv (GFileInfo  *info,
				   const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', NULL);

  value = g_file_info_find_value_by_name (info, attribute);
  return _g_file_attribute_value_get_stringv (value);
}

947 948 949
/**
 * g_file_info_get_attribute_boolean:
 * @info: a #GFileInfo.
950
 * @attribute: a file attribute key.
951
 *
952 953
 * Gets the value of a boolean attribute. If the attribute does not
 * contain a boolean value, %FALSE will be returned.
954 955
 *
 * Returns: the boolean value contained within the attribute.
956 957 958 959 960 961
 **/
gboolean
g_file_info_get_attribute_boolean (GFileInfo  *info,
				   const char *attribute)
{
  GFileAttributeValue *value;
962

963 964 965 966
  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);

  value = g_file_info_find_value_by_name (info, attribute);
967
  return _g_file_attribute_value_get_boolean (value);
968 969 970 971 972
}

/**
 * g_file_info_get_attribute_uint32:
 * @info: a #GFileInfo.
973
 * @attribute: a file attribute key.
974 975 976
 *
 * Gets an unsigned 32-bit integer contained within the attribute. If the
 * attribute does not contain an unsigned 32-bit integer, or is invalid,
Matthias Clasen's avatar
Matthias Clasen committed
977
 * 0 will be returned.
978 979
 *
 * Returns: an unsigned 32-bit integer from the attribute.
980 981 982 983 984 985
 **/
guint32
g_file_info_get_attribute_uint32 (GFileInfo  *info,
				  const char *attribute)
{
  GFileAttributeValue *value;
986

987 988 989 990
  g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);

  value = g_file_info_find_value_by_name (info, attribute);
991
  return _g_file_attribute_value_get_uint32 (value);
992 993 994 995
}

/**
 * g_file_info_get_attribute_int32:
996 997
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
998 999 1000
 *
 * Gets a signed 32-bit integer contained within the attribute. If the
 * attribute does not contain a signed 32-bit integer, or is invalid,
Matthias Clasen's avatar
Matthias Clasen committed
1001
 * 0 will be returned.
1002
 *
1003
 * Returns: a signed 32-bit integer from the attribute.
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
 **/
gint32
g_file_info_get_attribute_int32 (GFileInfo  *info,
				 const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);

  value = g_file_info_find_value_by_name (info, attribute);
1015
  return _g_file_attribute_value_get_int32 (value);
1016 1017 1018 1019
}

/**
 * g_file_info_get_attribute_uint64:
1020 1021
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
1022 1023 1024
 *
 * Gets a unsigned 64-bit integer contained within the attribute. If the
 * attribute does not contain an unsigned 64-bit integer, or is invalid,
Matthias Clasen's avatar
Matthias Clasen committed
1025
 * 0 will be returned.
1026 1027
 *
 * Returns: a unsigned 64-bit integer from the attribute.
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
 **/
guint64
g_file_info_get_attribute_uint64 (GFileInfo  *info,
				  const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);

  value = g_file_info_find_value_by_name (info, attribute);
1039
  return _g_file_attribute_value_get_uint64 (value);
1040 1041 1042 1043
}

/**
 * g_file_info_get_attribute_int64:
1044 1045
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
1046 1047 1048
 *
 * Gets a signed 64-bit integer contained within the attribute. If the
 * attribute does not contain an signed 64-bit integer, or is invalid,
Matthias Clasen's avatar
Matthias Clasen committed
1049
 * 0 will be returned.
1050 1051
 *
 * Returns: a signed 64-bit integer from the attribute.
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
 **/
gint64
g_file_info_get_attribute_int64  (GFileInfo  *info,
				  const char *attribute)
{
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), 0);
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', 0);

  value = g_file_info_find_value_by_name (info, attribute);
1063
  return _g_file_attribute_value_get_int64 (value);
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
}

static GFileAttributeValue *
g_file_info_create_value (GFileInfo *info,
			  guint32 attr_id)
{
  GFileAttribute *attrs;
  int i;

  if (info->mask != NO_ATTRIBUTE_MASK &&
1074
      !_g_file_attribute_matcher_matches_id (info->mask, attr_id))
1075
    return NULL;
1076

1077
  i = g_file_info_find_place (info, attr_id);
1078

1079 1080 1081 1082 1083 1084
  attrs = (GFileAttribute *)info->attributes->data;
  if (i < info->attributes->len &&
      attrs[i].attribute == attr_id)
    return &attrs[i].value;
  else
    {
1085
      GFileAttribute attr = { 0 };
1086 1087 1088 1089 1090 1091 1092 1093
      attr.attribute = attr_id;
      g_array_insert_val (info->attributes, i, attr);

      attrs = (GFileAttribute *)info->attributes->data;
      return &attrs[i].value;
    }
}

1094 1095 1096 1097 1098
void
_g_file_info_set_attribute_by_id (GFileInfo                 *info,
                                  guint32                    attribute,
                                  GFileAttributeType         type,
                                  gpointer                   value_p)
1099
{
1100
  GFileAttributeValue *value;
1101

1102
  value = g_file_info_create_value (info, attribute);
1103

1104 1105
  if (value)
    _g_file_attribute_value_set_from_pointer (value, type, value_p, TRUE);
1106 1107 1108 1109
}

/**
 * g_file_info_set_attribute:
1110 1111
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
Matthias Clasen's avatar
Matthias Clasen committed
1112
 * @type: a #GFileAttributeType
1113
 * @value_p: (not nullable): pointer to the value
1114
 *
1115 1116
 * Sets the @attribute to contain the given value, if possible. To unset the
 * attribute, use %G_ATTRIBUTE_TYPE_INVALID for @type.
1117 1118
 **/
void
Matthias Clasen's avatar
Matthias Clasen committed
1119 1120
g_file_info_set_attribute (GFileInfo                 *info,
			   const char                *attribute,
1121 1122
			   GFileAttributeType         type,
			   gpointer                   value_p)
1123 1124 1125 1126
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

1127 1128 1129 1130 1131 1132 1133 1134 1135
  _g_file_info_set_attribute_by_id (info, lookup_attribute (attribute), type, value_p);
}

void
_g_file_info_set_attribute_object_by_id (GFileInfo *info,
                                         guint32    attribute,
				         GObject   *attr_value)
{
  GFileAttributeValue *value;
1136

1137
  value = g_file_info_create_value (info, attribute);
1138
  if (value)
1139
    _g_file_attribute_value_set_object (value, attr_value);
1140 1141 1142 1143
}

/**
 * g_file_info_set_attribute_object:
1144 1145 1146
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: a #GObject.
1147 1148
 *
 * Sets the @attribute to contain the given @attr_value,
1149
 * if possible.
1150 1151 1152 1153
 **/
void
g_file_info_set_attribute_object (GFileInfo  *info,
				  const char *attribute,
Matthias Clasen's avatar
Matthias Clasen committed
1154
				  GObject    *attr_value)
1155 1156 1157 1158 1159
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');
  g_return_if_fail (G_IS_OBJECT (attr_value));

1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
  _g_file_info_set_attribute_object_by_id (info,
                                           lookup_attribute (attribute),
                                           attr_value);
}

void
_g_file_info_set_attribute_stringv_by_id (GFileInfo *info,
                                          guint32    attribute,
				          char     **attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
1173
  if (value)
1174
    _g_file_attribute_value_set_stringv (value, attr_value);
1175 1176
}

1177 1178 1179
/**
 * g_file_info_set_attribute_stringv:
 * @info: a #GFileInfo.
1180
 * @attribute: a file attribute key
1181
 * @attr_value: (array) (element-type utf8): a %NULL terminated array of UTF-8 strings.
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
 *
 * Sets the @attribute to contain the given @attr_value,
 * if possible.
 *
 * Sinze: 2.22
 **/
void
g_file_info_set_attribute_stringv (GFileInfo  *info,
				   const char *attribute,
				   char      **attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');
  g_return_if_fail (attr_value != NULL);

1197
  _g_file_info_set_attribute_stringv_by_id (info,
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
                                            lookup_attribute (attribute),
                                            attr_value);
}

void
_g_file_info_set_attribute_string_by_id (GFileInfo  *info,
                                         guint32     attribute,
				         const char *attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
1210
  if (value)
1211
    _g_file_attribute_value_set_string (value, attr_value);
1212 1213
}

1214 1215
/**
 * g_file_info_set_attribute_string:
1216 1217
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
1218 1219 1220
 * @attr_value: a UTF-8 string.
 *
 * Sets the @attribute to contain the given @attr_value,
1221
 * if possible.
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
 **/
void
g_file_info_set_attribute_string (GFileInfo  *info,
				  const char *attribute,
				  const char *attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');
  g_return_if_fail (attr_value != NULL);

1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
  _g_file_info_set_attribute_string_by_id (info,
                                           lookup_attribute (attribute),
                                           attr_value);
}

void
_g_file_info_set_attribute_byte_string_by_id (GFileInfo  *info,
                                              guint32     attribute,
				              const char *attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
1245
  if (value)
1246
    _g_file_attribute_value_set_byte_string (value, attr_value);
1247 1248 1249 1250
}

/**
 * g_file_info_set_attribute_byte_string:
1251 1252 1253
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: a byte string.
1254 1255
 *
 * Sets the @attribute to contain the given @attr_value,
1256
 * if possible.
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
 **/
void
g_file_info_set_attribute_byte_string (GFileInfo  *info,
				       const char *attribute,
				       const char *attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');
  g_return_if_fail (attr_value != NULL);

1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
  _g_file_info_set_attribute_byte_string_by_id (info,
                                                lookup_attribute (attribute),
                                                attr_value);
}

void
_g_file_info_set_attribute_boolean_by_id (GFileInfo *info,
                                          guint32    attribute,
				          gboolean   attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
1280
  if (value)
1281
    _g_file_attribute_value_set_boolean (value, attr_value);
1282 1283 1284 1285
}

/**
 * g_file_info_set_attribute_boolean:
1286 1287 1288
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: a boolean value.
1289 1290
 *
 * Sets the @attribute to contain the given @attr_value,
1291
 * if possible.
1292 1293 1294 1295
 **/
void
g_file_info_set_attribute_boolean (GFileInfo  *info,
				   const char *attribute,
Matthias Clasen's avatar
Matthias Clasen committed
1296
				   gboolean    attr_value)
1297 1298 1299 1300
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
  _g_file_info_set_attribute_boolean_by_id (info,
                                            lookup_attribute (attribute),
                                            attr_value);
}

void
_g_file_info_set_attribute_uint32_by_id (GFileInfo *info,
                                         guint32    attribute,
				         guint32    attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
1314
  if (value)
1315
    _g_file_attribute_value_set_uint32 (value, attr_value);
1316 1317 1318 1319
}

/**
 * g_file_info_set_attribute_uint32:
1320 1321 1322
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: an unsigned 32-bit integer.
1323 1324
 *
 * Sets the @attribute to contain the given @attr_value,
1325
 * if possible.
1326 1327 1328 1329 1330 1331 1332 1333 1334
 **/
void
g_file_info_set_attribute_uint32 (GFileInfo  *info,
				  const char *attribute,
				  guint32     attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

1335 1336 1337
  _g_file_info_set_attribute_uint32_by_id (info,
                                           lookup_attribute (attribute),
                                           attr_value);
1338 1339
}

1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
void
_g_file_info_set_attribute_int32_by_id (GFileInfo *info,
                                        guint32    attribute,
				        gint32     attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
  if (value)
    _g_file_attribute_value_set_int32 (value, attr_value);
}
1351 1352 1353

/**
 * g_file_info_set_attribute_int32:
1354 1355 1356
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: a signed 32-bit integer
1357 1358
 *
 * Sets the @attribute to contain the given @attr_value,
1359
 * if possible.
1360 1361
 **/
void
Matthias Clasen's avatar
Matthias Clasen committed
1362 1363 1364
g_file_info_set_attribute_int32 (GFileInfo  *info,
                                 const char *attribute,
                                 gint32      attr_value)
1365 1366 1367 1368
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
  _g_file_info_set_attribute_int32_by_id (info,
                                          lookup_attribute (attribute),
                                          attr_value);
}

void
_g_file_info_set_attribute_uint64_by_id (GFileInfo *info,
                                         guint32    attribute,
				         guint64    attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
1382
  if (value)
1383
    _g_file_attribute_value_set_uint64 (value, attr_value);
1384 1385 1386 1387
}

/**
 * g_file_info_set_attribute_uint64:
1388 1389 1390
 * @info: a #GFileInfo.
 * @attribute: a file attribute key.
 * @attr_value: an unsigned 64-bit integer.
1391 1392
 *
 * Sets the @attribute to contain the given @attr_value,
1393
 * if possible.
1394 1395 1396 1397 1398 1399 1400 1401 1402
 **/
void
g_file_info_set_attribute_uint64 (GFileInfo  *info,
				  const char *attribute,
				  guint64     attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
  _g_file_info_set_attribute_uint64_by_id (info,
                                           lookup_attribute (attribute),
                                           attr_value);
}

void
_g_file_info_set_attribute_int64_by_id (GFileInfo *info,
                                        guint32    attribute,
				        gint64     attr_value)
{
  GFileAttributeValue *value;

  value = g_file_info_create_value (info, attribute);
1416
  if (value)
1417
    _g_file_attribute_value_set_int64 (value, attr_value);
1418 1419 1420 1421
}

/**
 * g_file_info_set_attribute_int64:
1422
 * @info: a #GFileInfo.
1423 1424
 * @attribute: attribute name to set.
 * @attr_value: int64 value to set attribute to.
1425 1426
 *
 * Sets the @attribute to contain the given @attr_value,
1427
 * if possible.
1428
 *
1429 1430 1431 1432 1433 1434 1435 1436 1437
 **/
void
g_file_info_set_attribute_int64  (GFileInfo  *info,
				  const char *attribute,
				  gint64      attr_value)
{
  g_return_if_fail (G_IS_FILE_INFO (info));
  g_return_if_fail (attribute != NULL && *attribute != '\0');

1438 1439 1440
  _g_file_info_set_attribute_int64_by_id (info,
                                          lookup_attribute (attribute),
                                          attr_value);
1441 1442 1443
}

/* Helper getters */
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
/**
 * g_file_info_get_deletion_date:
 * @info: a #GFileInfo.
 *
 * Returns the #GDateTime representing the deletion date of the file, as
 * available in G_FILE_ATTRIBUTE_TRASH_DELETION_DATE. If the
 * G_FILE_ATTRIBUTE_TRASH_DELETION_DATE attribute is unset, %NULL is returned.
 *
 * Returns: a #GDateTime, or %NULL.
 *
 * Since: 2.36
 **/
GDateTime *
1457
g_file_info_get_deletion_date (GFileInfo *info)
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
{
  static guint32 attr = 0;
  GFileAttributeValue *value;
  const char *date_str;
  GTimeVal tv;

  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);

  if (attr == 0)
    attr = lookup_attribute (G_FILE_ATTRIBUTE_TRASH_DELETION_DATE);

  value = g_file_info_find_value (info, attr);
  date_str = _g_file_attribute_value_get_string (value);
  if (!date_str)
    return NULL;

  if (g_time_val_from_iso8601 (date_str, &tv) == FALSE)
    return NULL;

  return g_date_time_new_from_timeval_local (&tv);
}

1480 1481 1482
/**
 * g_file_info_get_file_type:
 * @info: a #GFileInfo.
1483 1484
 *
 * Gets a file's type (whether it is a regular file, symlink, etc).
1485
 * This is different from the file's content type, see g_file_info_get_content_type().
1486
 *
1487 1488 1489 1490 1491 1492 1493 1494 1495
 * Returns: a #GFileType for the given file.
 **/
GFileType
g_file_info_get_file_type (GFileInfo *info)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;

  g_return_val_if_fail (G_IS_FILE_INFO (info), G_FILE_TYPE_UNKNOWN);
1496

1497
  if (attr == 0)
1498
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_TYPE);
1499

1500
  value = g_file_info_find_value (info, attr);
1501
  return (GFileType)_g_file_attribute_value_get_uint32 (value);
1502 1503 1504 1505 1506
}

/**
 * g_file_info_get_is_hidden:
 * @info: a #GFileInfo.
1507
 *
1508
 * Checks if a file is hidden.
1509
 *
1510 1511 1512 1513 1514 1515 1516
 * Returns: %TRUE if the file is a hidden file, %FALSE otherwise.
 **/
gboolean
g_file_info_get_is_hidden (GFileInfo *info)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;
1517

1518
  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1519

1520
  if (attr == 0)
1521
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
1522

1523
  value = g_file_info_find_value (info, attr);
1524
  return (GFileType)_g_file_attribute_value_get_boolean (value);
1525 1526 1527 1528 1529
}

/**
 * g_file_info_get_is_backup:
 * @info: a #GFileInfo.
1530
 *
1531
 * Checks if a file is a backup file.
1532
 *
1533
 * Returns: %TRUE if file is a backup file, %FALSE otherwise.
1534 1535 1536 1537 1538 1539
 **/
gboolean
g_file_info_get_is_backup (GFileInfo *info)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;
1540

1541
  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1542

1543
  if (attr == 0)
1544
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP);
1545

1546
  value = g_file_info_find_value (info, attr);
1547
  return (GFileType)_g_file_attribute_value_get_boolean (value);
1548 1549 1550 1551 1552
}

/**
 * g_file_info_get_is_symlink:
 * @info: a #GFileInfo.
1553
 *
1554
 * Checks if a file is a symlink.
1555
 *
1556 1557 1558 1559 1560 1561 1562
 * Returns: %TRUE if the given @info is a symlink.
 **/
gboolean
g_file_info_get_is_symlink (GFileInfo *info)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;
1563

1564
  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
1565

1566
  if (attr == 0)
1567
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK);
1568

1569
  value = g_file_info_find_value (info, attr);
1570
  return (GFileType)_g_file_attribute_value_get_boolean (value);
1571 1572 1573 1574 1575
}

/**
 * g_file_info_get_name:
 * @info: a #GFileInfo.
1576
 *
1577
 * Gets the name for a file.
1578
 *
1579
 * Returns: a string containing the file name.
1580 1581 1582 1583 1584 1585
 **/
const char *
g_file_info_get_name (GFileInfo *info)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;
1586

1587
  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1588

1589
  if (attr == 0)
1590
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_NAME);
1591

1592
  value = g_file_info_find_value (info, attr);
1593
  return _g_file_attribute_value_get_byte_string (value);
1594 1595 1596 1597 1598
}

/**
 * g_file_info_get_display_name:
 * @info: a #GFileInfo.
1599
 *
1600
 * Gets a display name for a file.
1601
 *
1602
 * Returns: a string containing the display name.
1603 1604 1605 1606 1607 1608
 **/
const char *
g_file_info_get_display_name (GFileInfo *info)
{
  static guint32 attr = 0;
  GFileAttributeValue *value;
1609

1610
  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
1611

1612
  if (attr == 0)
1613
    attr = lookup_attribute (G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
1614

1615
  value = g_file_info_find_value (info, attr);
1616
  return _g_file_attribute_value_get_string (value);