gfile.c 248 KB
Newer Older
1 2
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* GIO - GLib Input, Output and Streaming Library
 * 
 * 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
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

25
#include "config.h"
26 27 28 29 30 31
#ifdef HAVE_SPLICE
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#endif
32 33 34 35 36 37 38 39 40
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#include "gfile.h"
#include "gvfs.h"
#include "gioscheduler.h"
#include "gsimpleasyncresult.h"
41
#include "gfileattribute-priv.h"
42
#include "gfiledescriptorbased.h"
43
#include "gpollfilemonitor.h"
44 45 46
#include "gappinfo.h"
#include "gfileinputstream.h"
#include "gfileoutputstream.h"
47 48
#include "glocalfileoutputstream.h"
#include "glocalfileiostream.h"
49 50 51
#include "gcancellable.h"
#include "gasyncresult.h"
#include "gioerror.h"
52 53
#include "glibintl.h"

54

55 56 57
/**
 * SECTION:gfile
 * @short_description: File and Directory Handling
Matthias Clasen's avatar
Matthias Clasen committed
58
 * @include: gio/gio.h
59
 * @see_also: #GFileInfo, #GFileEnumerator
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
 *
 * #GFile is a high level abstraction for manipulating files on a
 * virtual file system. #GFiles are lightweight, immutable objects
 * that do no I/O upon creation. It is necessary to understand that
 * #GFile objects do not represent files, merely an identifier for a
 * file. All file content I/O is implemented as streaming operations
 * (see #GInputStream and #GOutputStream).
 *
 * To construct a #GFile, you can use:
 * <simplelist>
 * <member>g_file_new_for_path() if you have a path.</member>
 * <member>g_file_new_for_uri() if you have a URI.</member>
 * <member>g_file_new_for_commandline_arg() for a command line argument.</member>
 * <member>g_file_new_tmp() to create a temporary file from a template.</member>
 * <member>g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().</member>
 * </simplelist>
 *
 * One way to think of a #GFile is as an abstraction of a pathname. For
 * normal files the system pathname is what is stored internally, but as
 * #GFiles are extensible it could also be something else that corresponds
 * to a pathname in a userspace implementation of a filesystem.
 *
 * #GFiles make up hierarchies of directories and files that correspond to
 * the files on a filesystem. You can move through the file system with
 * #GFile using g_file_get_parent() to get an identifier for the parent
 * directory, g_file_get_child() to get a child within a directory,
 * g_file_resolve_relative_path() to resolve a relative path between two
 * #GFiles. There can be multiple hierarchies, so you may not end up at
 * the same root if you repeatedly call g_file_get_parent() on two different
89 90
 * files.
 *
91 92 93 94 95 96
 * All #GFiles have a basename (get with g_file_get_basename()). These names
 * are byte strings that are used to identify the file on the filesystem
 * (relative to its parent directory) and there is no guarantees that they
 * have any particular charset encoding or even make any sense at all. If
 * you want to use filenames in a user interface you should use the display
 * name that you can get by requesting the
97
 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
98 99 100 101 102 103 104 105 106 107 108
 * This is guaranteed to be in UTF-8 and can be used in a user interface.
 * But always store the real basename or the #GFile to use to actually
 * access the file, because there is no way to go from a display name to
 * the actual name.
 *
 * Using #GFile as an identifier has the same weaknesses as using a path
 * in that there may be multiple aliases for the same file. For instance,
 * hard or soft links may cause two different #GFiles to refer to the same
 * file. Other possible causes for aliases are: case insensitive filesystems,
 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
 * check if two #GFiles point to the same file you can query for the
109
 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
110 111 112 113 114 115 116 117 118 119
 * canonicalization of pathnames passed in, so that trivial differences in
 * the path string used at creation (duplicated slashes, slash at end of
 * path, "." or ".." path segments, etc) does not create different #GFiles.
 *
 * Many #GFile operations have both synchronous and asynchronous versions
 * to suit your application. Asynchronous versions of synchronous functions
 * simply have _async() appended to their function names. The asynchronous
 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
 * the operation, producing a GAsyncResult which is then passed to the
 * function's matching _finish() operation.
120 121 122 123
 *
 * Some #GFile operations do not have synchronous analogs, as they may
 * take a very long time to finish, and blocking may leave an application
 * unusable. Notable cases include:
124 125 126 127 128 129
 * <simplelist>
 * <member>g_file_mount_mountable() to mount a mountable file.</member>
 * <member>g_file_unmount_mountable_with_operation() to unmount a mountable file.</member>
 * <member>g_file_eject_mountable_with_operation() to eject a mountable file.</member>
 * </simplelist>
 *
Matthias Clasen's avatar
Matthias Clasen committed
130
 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
131 132
 * One notable feature of #GFiles are entity tags, or "etags" for
 * short. Entity tags are somewhat like a more abstract version of the
Matthias Clasen's avatar
Matthias Clasen committed
133
 * traditional mtime, and can be used to quickly determine if the file has
134
 * been modified from the version on the file system. See the HTTP 1.1
Matthias Clasen's avatar
Matthias Clasen committed
135
 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
Matthias Clasen's avatar
Matthias Clasen committed
136 137
 * for HTTP Etag headers, which are a very similar concept.
 * </para>
138 139
 **/

140
static void               g_file_real_query_info_async            (GFile                  *file,
141 142 143 144 145 146
                                                                   const char             *attributes,
                                                                   GFileQueryInfoFlags     flags,
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
147
static GFileInfo *        g_file_real_query_info_finish           (GFile                  *file,
148 149
                                                                   GAsyncResult           *res,
                                                                   GError                **error);
150
static void               g_file_real_query_filesystem_info_async (GFile                  *file,
151 152 153 154 155
                                                                   const char             *attributes,
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
156
static GFileInfo *        g_file_real_query_filesystem_info_finish (GFile                  *file,
157 158
                                                                   GAsyncResult           *res,
                                                                   GError                **error);
159
static void               g_file_real_enumerate_children_async    (GFile                  *file,
160 161 162 163 164 165
                                                                   const char             *attributes,
                                                                   GFileQueryInfoFlags     flags,
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
166
static GFileEnumerator *  g_file_real_enumerate_children_finish   (GFile                  *file,
167 168
                                                                   GAsyncResult           *res,
                                                                   GError                **error);
169
static void               g_file_real_read_async                  (GFile                  *file,
170 171 172 173
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
174
static GFileInputStream * g_file_real_read_finish                 (GFile                  *file,
175 176
                                                                   GAsyncResult           *res,
                                                                   GError                **error);
177
static void               g_file_real_append_to_async             (GFile                  *file,
178 179 180 181 182
                                                                   GFileCreateFlags        flags,
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
183
static GFileOutputStream *g_file_real_append_to_finish            (GFile                  *file,
184 185
                                                                   GAsyncResult           *res,
                                                                   GError                **error);
186
static void               g_file_real_create_async                (GFile                  *file,
187 188 189 190 191
                                                                   GFileCreateFlags        flags,
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
192
static GFileOutputStream *g_file_real_create_finish               (GFile                  *file,
193 194
                                                                   GAsyncResult           *res,
                                                                   GError                **error);
195
static void               g_file_real_replace_async               (GFile                  *file,
196 197 198 199 200 201 202
                                                                   const char             *etag,
                                                                   gboolean                make_backup,
                                                                   GFileCreateFlags        flags,
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
203
static GFileOutputStream *g_file_real_replace_finish              (GFile                  *file,
204 205
                                                                   GAsyncResult           *res,
                                                                   GError                **error);
206
static void               g_file_real_delete_async                (GFile                  *file,
207 208 209 210
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
211
static gboolean           g_file_real_delete_finish               (GFile                  *file,
212 213
                                                                   GAsyncResult           *res,
                                                                   GError                **error);
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
static void               g_file_real_open_readwrite_async        (GFile                  *file,
                                                                   int                  io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
static GFileIOStream *    g_file_real_open_readwrite_finish       (GFile                  *file,
                                                                   GAsyncResult           *res,
                                                                   GError                **error);
static void               g_file_real_create_readwrite_async      (GFile                  *file,
                                                                   GFileCreateFlags        flags,
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
static GFileIOStream *    g_file_real_create_readwrite_finish     (GFile                  *file,
                                                                   GAsyncResult           *res,
                                                                   GError                **error);
static void               g_file_real_replace_readwrite_async     (GFile                  *file,
                                                                   const char             *etag,
                                                                   gboolean                make_backup,
                                                                   GFileCreateFlags        flags,
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
static GFileIOStream *    g_file_real_replace_readwrite_finish    (GFile                  *file,
                                                                  GAsyncResult            *res,
                                                                  GError                 **error);
242
static gboolean           g_file_real_set_attributes_from_info    (GFile                  *file,
243 244 245 246
                                                                   GFileInfo              *info,
                                                                   GFileQueryInfoFlags     flags,
                                                                   GCancellable           *cancellable,
                                                                   GError                **error);
247
static void               g_file_real_set_display_name_async      (GFile                  *file,
248 249 250 251 252
                                                                   const char             *display_name,
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
253
static GFile *            g_file_real_set_display_name_finish     (GFile                  *file,
254 255
                                                                   GAsyncResult           *res,
                                                                   GError                **error);
256
static void               g_file_real_set_attributes_async        (GFile                  *file,
257 258 259 260 261 262
                                                                   GFileInfo              *info,
                                                                   GFileQueryInfoFlags     flags,
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
263
static gboolean           g_file_real_set_attributes_finish       (GFile                  *file,
264 265 266
                                                                   GAsyncResult           *res,
                                                                   GFileInfo             **info,
                                                                   GError                **error);
267
static void               g_file_real_find_enclosing_mount_async  (GFile                  *file,
268 269 270 271
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
272
static GMount *           g_file_real_find_enclosing_mount_finish (GFile                  *file,
273 274
                                                                   GAsyncResult           *res,
                                                                   GError                **error);
275
static void               g_file_real_copy_async                  (GFile                  *source,
276 277 278 279 280 281 282 283
                                                                   GFile                  *destination,
                                                                   GFileCopyFlags          flags,
                                                                   int                     io_priority,
                                                                   GCancellable           *cancellable,
                                                                   GFileProgressCallback   progress_callback,
                                                                   gpointer                progress_callback_data,
                                                                   GAsyncReadyCallback     callback,
                                                                   gpointer                user_data);
284
static gboolean           g_file_real_copy_finish                 (GFile                  *file,
285 286
                                                                   GAsyncResult           *res,
                                                                   GError                **error);
287

Dan Winship's avatar
Dan Winship committed
288 289
typedef GFileIface GFileInterface;
G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
290 291

static void
Dan Winship's avatar
Dan Winship committed
292
g_file_default_init (GFileIface *iface)
293 294 295 296 297 298 299
{
  iface->enumerate_children_async = g_file_real_enumerate_children_async;
  iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
  iface->set_display_name_async = g_file_real_set_display_name_async;
  iface->set_display_name_finish = g_file_real_set_display_name_finish;
  iface->query_info_async = g_file_real_query_info_async;
  iface->query_info_finish = g_file_real_query_info_finish;
300 301
  iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
  iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
302 303 304 305 306 307 308 309 310 311
  iface->set_attributes_async = g_file_real_set_attributes_async;
  iface->set_attributes_finish = g_file_real_set_attributes_finish;
  iface->read_async = g_file_real_read_async;
  iface->read_finish = g_file_real_read_finish;
  iface->append_to_async = g_file_real_append_to_async;
  iface->append_to_finish = g_file_real_append_to_finish;
  iface->create_async = g_file_real_create_async;
  iface->create_finish = g_file_real_create_finish;
  iface->replace_async = g_file_real_replace_async;
  iface->replace_finish = g_file_real_replace_finish;
312 313
  iface->delete_file_async = g_file_real_delete_async;
  iface->delete_file_finish = g_file_real_delete_finish;
314 315 316 317 318 319
  iface->open_readwrite_async = g_file_real_open_readwrite_async;
  iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
  iface->create_readwrite_async = g_file_real_create_readwrite_async;
  iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
  iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
  iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
320 321
  iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
  iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
322
  iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
323 324
  iface->copy_async = g_file_real_copy_async;
  iface->copy_finish = g_file_real_copy_finish;
325 326 327 328 329
}


/**
 * g_file_is_native:
330
 * @file: input #GFile
331
 *
A. Walton's avatar
A. Walton committed
332
 * Checks to see if a file is native to the platform.
333
 *
334 335 336 337
 * A native file s one expressed in the platform-native filename format,
 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
 * as it might be on a locally mounted remote filesystem.
 *
338 339 340
 * On some systems non-native files may be available using the native
 * filesystem via a userspace filesystem (FUSE), in these cases this call
 * will return %FALSE, but g_file_get_path() will still return a native path.
341
 *
342 343 344 345
 * This call does no blocking I/O.
 *
 * Returns: %TRUE if @file is native
 */
346 347 348 349 350 351 352 353 354 355 356 357 358 359
gboolean
g_file_is_native (GFile *file)
{
  GFileIface *iface;

  g_return_val_if_fail (G_IS_FILE (file), FALSE);

  iface = G_FILE_GET_IFACE (file);

  return (* iface->is_native) (file);
}


/**
360 361 362
 * g_file_has_uri_scheme:
 * @file: input #GFile
 * @uri_scheme: a string containing a URI scheme
363
 *
364 365
 * Checks to see if a #GFile has a given URI scheme.
 *
366 367
 * This call does no blocking I/O.
 *
368
 * Returns: %TRUE if #GFile's backend supports the
Matthias Clasen's avatar
Matthias Clasen committed
369 370
 *     given URI scheme, %FALSE if URI scheme is %NULL,
 *     not supported, or #GFile is invalid.
371
 */
372
gboolean
Matthias Clasen's avatar
Matthias Clasen committed
373
g_file_has_uri_scheme (GFile      *file,
374
                       const char *uri_scheme)
375 376
{
  GFileIface *iface;
377

378 379 380 381 382 383 384 385 386 387 388
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
  g_return_val_if_fail (uri_scheme != NULL, FALSE);

  iface = G_FILE_GET_IFACE (file);

  return (* iface->has_uri_scheme) (file, uri_scheme);
}


/**
 * g_file_get_uri_scheme:
389
 * @file: input #GFile
390
 *
391
 * Gets the URI scheme for a #GFile.
392
 * RFC 3986 decodes the scheme as:
393
 * <programlisting>
394
 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
395
 * </programlisting>
396
 * Common schemes include "file", "http", "ftp", etc.
397
 *
398 399 400 401
 * This call does no blocking I/O.
 *
 * Returns: a string containing the URI scheme for the given
 *     #GFile. The returned string should be freed with g_free()
402
 *     when no longer needed.
403
 */
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
char *
g_file_get_uri_scheme (GFile *file)
{
  GFileIface *iface;

  g_return_val_if_fail (G_IS_FILE (file), NULL);

  iface = G_FILE_GET_IFACE (file);

  return (* iface->get_uri_scheme) (file);
}


/**
 * g_file_get_basename:
419
 * @file: input #GFile
420
 *
421
 * Gets the base name (the last component of the path) for a given #GFile.
422
 *
423
 * If called for the top level of a system (such as the filesystem root
424
 * or a uri like sftp://host/) it will return a single directory separator
425
 * (and on Windows, possibly a drive letter).
426
 *
427
 * The base name is a byte string (not UTF-8). It has no defined encoding
428 429 430 431
 * or rules other than it may not contain zero bytes.  If you want to use
 * filenames in a user interface you should use the display name that you
 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
 * attribute with g_file_query_info().
432 433 434 435 436
 *
 * This call does no blocking I/O.
 *
 * Returns: string containing the #GFile's base name, or %NULL
 *     if given #GFile is invalid. The returned string should be
437
 *     freed with g_free() when no longer needed.
438
 */
439 440 441 442
char *
g_file_get_basename (GFile *file)
{
  GFileIface *iface;
443

444 445 446 447 448 449 450 451 452
  g_return_val_if_fail (G_IS_FILE (file), NULL);

  iface = G_FILE_GET_IFACE (file);

  return (* iface->get_basename) (file);
}

/**
 * g_file_get_path:
453
 * @file: input #GFile
454
 *
455
 * Gets the local pathname for #GFile, if one exists.
456
 *
457 458 459 460
 * This call does no blocking I/O.
 *
 * Returns: string containing the #GFile's path, or %NULL if
 *     no such path exists. The returned string should be
461
 *     freed with g_free() when no longer needed.
462
 */
463 464 465 466 467 468 469 470 471 472 473 474 475 476
char *
g_file_get_path (GFile *file)
{
  GFileIface *iface;

  g_return_val_if_fail (G_IS_FILE (file), NULL);

  iface = G_FILE_GET_IFACE (file);

  return (* iface->get_path) (file);
}

/**
 * g_file_get_uri:
477
 * @file: input #GFile
478
 *
479
 * Gets the URI for the @file.
480
 *
481 482
 * This call does no blocking I/O.
 *
483
 * Returns: a string containing the #GFile's URI.
484 485 486
 *     The returned string should be freed with g_free()
 *     when no longer needed.
 */
487 488 489 490
char *
g_file_get_uri (GFile *file)
{
  GFileIface *iface;
491

492 493 494 495 496 497 498 499
  g_return_val_if_fail (G_IS_FILE (file), NULL);

  iface = G_FILE_GET_IFACE (file);

  return (* iface->get_uri) (file);
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
500
 * g_file_get_parse_name:
501
 * @file: input #GFile
502
 *
503 504 505 506 507 508
 * Gets the parse name of the @file.
 * A parse name is a UTF-8 string that describes the
 * file such that one can get the #GFile back using
 * g_file_parse_name().
 *
 * This is generally used to show the #GFile as a nice
509 510
 * full-pathname kind of string in a user interface,
 * like in a location entry.
511
 *
512
 * For local files with names that can safely be converted
513 514
 * to UTF-8 the pathname is used, otherwise the IRI is used
 * (a form of URI that allows UTF-8 characters unescaped).
515
 *
516 517 518 519 520 521
 * This call does no blocking I/O.
 *
 * Returns: a string containing the #GFile's parse name.
 *     The returned string should be freed with g_free()
 *     when no longer needed.
 */
522 523 524 525
char *
g_file_get_parse_name (GFile *file)
{
  GFileIface *iface;
526

527 528 529 530 531 532 533 534 535
  g_return_val_if_fail (G_IS_FILE (file), NULL);

  iface = G_FILE_GET_IFACE (file);

  return (* iface->get_parse_name) (file);
}

/**
 * g_file_dup:
536
 * @file: input #GFile
537
 *
538 539 540
 * Duplicates a #GFile handle. This operation does not duplicate
 * the actual file or directory represented by the #GFile; see
 * g_file_copy() if attempting to copy a file.
541
 *
542 543 544 545 546
 * This call does no blocking I/O.
 *
 * Returns: (transfer full): a new #GFile that is a duplicate
 *     of the given #GFile.
 */
547 548 549 550
GFile *
g_file_dup (GFile *file)
{
  GFileIface *iface;
551

552 553 554 555 556 557 558 559 560
  g_return_val_if_fail (G_IS_FILE (file), NULL);

  iface = G_FILE_GET_IFACE (file);

  return (* iface->dup) (file);
}

/**
 * g_file_hash:
561
 * @file: (type GFile): #gconstpointer to a #GFile
562
 *
563
 * Creates a hash value for a #GFile.
564
 *
565
 * This call does no blocking I/O.
566 567
 *
 * Virtual: hash
568 569 570
 * Returns: 0 if @file is not a valid #GFile, otherwise an
 *     integer that can be used as hash value for the #GFile.
 *     This function is intended for easily hashing a #GFile to
571
 *     add to a #GHashTable or similar data structure.
572
 */
573 574 575 576
guint
g_file_hash (gconstpointer file)
{
  GFileIface *iface;
577

578 579 580 581 582 583 584 585 586
  g_return_val_if_fail (G_IS_FILE (file), 0);

  iface = G_FILE_GET_IFACE (file);

  return (* iface->hash) ((GFile *)file);
}

/**
 * g_file_equal:
587 588 589 590
 * @file1: the first #GFile
 * @file2: the second #GFile
 *
 * Checks equality of two given #GFiles.
591
 *
592
 * Note that two #GFiles that differ can still refer to the same
593 594
 * file on the filesystem due to various forms of filename
 * aliasing.
595
 *
596 597
 * This call does no blocking I/O.
 *
598
 * Returns: %TRUE if @file1 and @file2 are equal.
599
 *     %FALSE if either is not a #GFile.
600
 */
601 602
gboolean
g_file_equal (GFile *file1,
603
              GFile *file2)
604 605
{
  GFileIface *iface;
606

607 608
  g_return_val_if_fail (G_IS_FILE (file1), FALSE);
  g_return_val_if_fail (G_IS_FILE (file2), FALSE);
609

610 611 612 613
  if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
    return FALSE;

  iface = G_FILE_GET_IFACE (file1);
614

615 616 617 618 619 620
  return (* iface->equal) (file1, file2);
}


/**
 * g_file_get_parent:
621
 * @file: input #GFile
622
 *
623 624
 * Gets the parent directory for the @file.
 * If the @file represents the root directory of the
625
 * file system, then %NULL will be returned.
626
 *
627 628 629 630 631 632
 * This call does no blocking I/O.
 *
 * Returns: (transfer full): a #GFile structure to the
 *     parent of the given #GFile or %NULL if there is
 *     no parent. Free the returned object with g_object_unref().
 */
633 634 635 636
GFile *
g_file_get_parent (GFile *file)
{
  GFileIface *iface;
637

638 639 640 641 642 643 644
  g_return_val_if_fail (G_IS_FILE (file), NULL);

  iface = G_FILE_GET_IFACE (file);

  return (* iface->get_parent) (file);
}

645 646 647
/**
 * g_file_has_parent:
 * @file: input #GFile
648
 * @parent: (allow-none): the parent to check for, or %NULL
649 650 651 652 653 654 655 656 657 658 659
 *
 * Checks if @file has a parent, and optionally, if it is @parent.
 *
 * If @parent is %NULL then this function returns %TRUE if @file has any
 * parent at all.  If @parent is non-%NULL then %TRUE is only returned
 * if @file is a child of @parent.
 *
 * Returns: %TRUE if @file is a child of @parent (or any parent in the
 *          case that @parent is %NULL).
 *
 * Since: 2.24
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
gboolean
g_file_has_parent (GFile *file,
                   GFile *parent)
{
  GFile *actual_parent;
  gboolean result;

  g_return_val_if_fail (G_IS_FILE (file), FALSE);
  g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);

  actual_parent = g_file_get_parent (file);

  if (actual_parent != NULL)
    {
      if (parent != NULL)
        result = g_file_equal (parent, actual_parent);
      else
        result = TRUE;

      g_object_unref (actual_parent);
    }
  else
    result = FALSE;

  return result;
}

688 689
/**
 * g_file_get_child:
690 691
 * @file: input #GFile
 * @name: string containing the child's basename
692
 *
693
 * Gets a child of @file with basename equal to @name.
694 695 696 697
 *
 * Note that the file with that specific name might not exist, but
 * you can still have a #GFile that points to it. You can use this
 * for instance to create that file.
698
 *
699 700
 * This call does no blocking I/O.
 *
701
 * Returns: (transfer full): a #GFile to a child specified by @name.
Matthias Clasen's avatar
Matthias Clasen committed
702
 *     Free the returned object with g_object_unref().
703
 */
704
GFile *
Matthias Clasen's avatar
Matthias Clasen committed
705
g_file_get_child (GFile      *file,
706
                  const char *name)
707 708 709 710 711 712 713 714 715
{
  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (name != NULL, NULL);

  return g_file_resolve_relative_path (file, name);
}

/**
 * g_file_get_child_for_display_name:
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
 * @file: input #GFile
 * @display_name: string to a possible child
 * @error: return location for an error
 *
 * Gets the child of @file for a given @display_name (i.e. a UTF-8
 * version of the name). If this function fails, it returns %NULL
 * and @error will be set. This is very useful when constructing a
 * #GFile for a new file and the user entered the filename in the
 * user interface, for instance when you select a directory and
 * type a filename in the file selector.
 *
 * This call does no blocking I/O.
 *
 * Returns: (transfer full): a #GFile to the specified child, or
 *     %NULL if the display name couldn't be converted.
Matthias Clasen's avatar
Matthias Clasen committed
731
 *     Free the returned object with g_object_unref().
732
 */
733
GFile *
Matthias Clasen's avatar
Matthias Clasen committed
734
g_file_get_child_for_display_name (GFile      *file,
735 736
                                   const char *display_name,
                                   GError **error)
737 738
{
  GFileIface *iface;
739

740 741 742 743 744 745 746 747
  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (display_name != NULL, NULL);

  iface = G_FILE_GET_IFACE (file);

  return (* iface->get_child_for_display_name) (file, display_name, error);
}

748 749
/**
 * g_file_has_prefix:
750 751 752 753 754 755 756 757 758 759 760 761 762
 * @file: input #GFile
 * @prefix: input #GFile
 *
 * Checks whether @file has the prefix specified by @prefix.
 *
 * In other words, if the names of initial elements of @file's
 * pathname match @prefix. Only full pathname elements are matched,
 * so a path like /foo is not considered a prefix of /foobar, only
 * of /foo/bar.
 *
 * This call does no I/O, as it works purely on names. As such it can
 * sometimes return %FALSE even if @file is inside a @prefix (from a
 * filesystem point of view), because the prefix of @file is an alias
Matthias Clasen's avatar
Matthias Clasen committed
763
 * of @prefix.
764
 *
765
 * Virtual: prefix_matches
766
 * Returns:  %TRUE if the @files's parent, grandparent, etc is @prefix,
Matthias Clasen's avatar
Matthias Clasen committed
767
 *     %FALSE otherwise.
768
 */
769 770
gboolean
g_file_has_prefix (GFile *file,
771
                   GFile *prefix)
772 773
{
  GFileIface *iface;
774

775 776
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
  g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
777

778
  if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
779
    return FALSE;
780

781
  iface = G_FILE_GET_IFACE (file);
782

783 784 785
  /* The vtable function differs in arg order since
   * we're using the old contains_file call
   */
786
  return (* iface->prefix_matches) (prefix, file);
787 788 789 790
}

/**
 * g_file_get_relative_path:
791 792
 * @parent: input #GFile
 * @descendant: input #GFile
793
 *
794
 * Gets the path for @descendant relative to @parent.
795
 *
796 797 798 799 800 801 802
 * This call does no blocking I/O.
 *
 * Returns: string with the relative path from @descendant
 *     to @parent, or %NULL if @descendant doesn't have @parent
 *     as prefix. The returned string should be freed with g_free()
 *     when no longer needed.
 */
803 804
char *
g_file_get_relative_path (GFile *parent,
805
                          GFile *descendant)
806 807
{
  GFileIface *iface;
808

809 810 811 812 813
  g_return_val_if_fail (G_IS_FILE (parent), NULL);
  g_return_val_if_fail (G_IS_FILE (descendant), NULL);

  if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
    return NULL;
814

815 816 817 818 819 820 821
  iface = G_FILE_GET_IFACE (parent);

  return (* iface->get_relative_path) (parent, descendant);
}

/**
 * g_file_resolve_relative_path:
822 823
 * @file: input #GFile
 * @relative_path: a given relative path string
824
 *
825 826
 * Resolves a relative path for @file to an absolute path.
 *
827 828 829 830
 * This call does no blocking I/O.
 *
 * Returns: (transfer full): #GFile to the resolved path.
 *     %NULL if @relative_path is %NULL or if @file is invalid.
Matthias Clasen's avatar
Matthias Clasen committed
831
 *     Free the returned object with g_object_unref().
832
 */
833
GFile *
Matthias Clasen's avatar
Matthias Clasen committed
834
g_file_resolve_relative_path (GFile      *file,
835
                              const char *relative_path)
836 837 838 839 840 841 842 843 844 845 846 847 848
{
  GFileIface *iface;

  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (relative_path != NULL, NULL);

  iface = G_FILE_GET_IFACE (file);

  return (* iface->resolve_relative_path) (file, relative_path);
}

/**
 * g_file_enumerate_children:
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
 * @file: input #GFile
 * @attributes: an attribute query string
 * @flags: a set of #GFileQueryInfoFlags
 * @cancellable: (allow-none): optional #GCancellable object,
 *     %NULL to ignore
 * @error: #GError for error reporting
 *
 * Gets the requested information about the files in a directory.
 * The result is a #GFileEnumerator object that will give out
 * #GFileInfo objects for all the files in the directory.
 *
 * The @attributes value is a string that specifies the file
 * attributes that should be gathered. It is not an error if
 * it's not possible to read a particular requested attribute
 * from a file - it just won't be set. @attributes should
 * be a comma-separated list of attributes or attribute wildcards.
 * The wildcard "*" means all attributes, and a wildcard like
 * "standard::*" means all attributes in the standard namespace.
 * An example attribute query be "standard::*,owner::user".
 * The standard attributes are available as defines, like
 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
 *
 * If @cancellable is not %NULL, then the operation can be cancelled
 * by triggering the cancellable object from another thread. If the
 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
 * returned.
 *
 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
 * be returned. If the file is not a directory, the %G_FILE_ERROR_NOTDIR
 * error will be returned. Other errors are possible too.
 *
 * Returns: (transfer full): A #GFileEnumerator if successful,
 *     %NULL on error. Free the returned object with g_object_unref().
 */
883
GFileEnumerator *
Matthias Clasen's avatar
Matthias Clasen committed
884
g_file_enumerate_children (GFile                *file,
885 886 887 888
                           const char           *attributes,
                           GFileQueryInfoFlags   flags,
                           GCancellable         *cancellable,
                           GError              **error)
889 890
{
  GFileIface *iface;
891

892 893 894 895
  g_return_val_if_fail (G_IS_FILE (file), NULL);

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return NULL;
896

897 898 899 900
  iface = G_FILE_GET_IFACE (file);

  if (iface->enumerate_children == NULL)
    {
901 902 903
      g_set_error_literal (error, G_IO_ERROR,
                           G_IO_ERROR_NOT_SUPPORTED,
                           _("Operation not supported"));
904 905 906 907
      return NULL;
    }

  return (* iface->enumerate_children) (file, attributes, flags,
908
                                        cancellable, error);
909 910 911 912
}

/**
 * g_file_enumerate_children_async:
913 914 915 916 917 918 919 920
 * @file: input #GFile
 * @attributes: an attribute query string
 * @flags: a set of #GFileQueryInfoFlags
 * @io_priority: the <link linkend="io-priority">I/O priority</link>
 *     of the request
 * @cancellable: (allow-none): optional #GCancellable object,
 *     %NULL to ignore
 * @callback: (scope async): a #GAsyncReadyCallback to call when the
921 922
 *     request is satisfied
 * @user_data: (closure): the data to pass to callback function
923
 *
924 925 926
 * Asynchronously gets the requested information about the files
 * in a directory. The result is a #GFileEnumerator object that will
 * give out #GFileInfo objects for all the files in the directory.
927 928 929 930
 *
 * For more details, see g_file_enumerate_children() which is
 * the synchronous version of this call.
 *
931 932 933 934
 * When the operation is finished, @callback will be called. You can
 * then call g_file_enumerate_children_finish() to get the result of
 * the operation.
 */
935
void
Matthias Clasen's avatar
Matthias Clasen committed
936
g_file_enumerate_children_async (GFile               *file,
937 938 939 940 941 942
                                 const char          *attributes,
                                 GFileQueryInfoFlags  flags,
                                 int                  io_priority,
                                 GCancellable        *cancellable,
                                 GAsyncReadyCallback  callback,
                                 gpointer             user_data)
943 944 945 946 947 948 949
{
  GFileIface *iface;

  g_return_if_fail (G_IS_FILE (file));

  iface = G_FILE_GET_IFACE (file);
  (* iface->enumerate_children_async) (file,
950 951 952 953 954 955
                                       attributes,
                                       flags,
                                       io_priority,
                                       cancellable,
                                       callback,
                                       user_data);
956 957 958 959
}

/**
 * g_file_enumerate_children_finish:
960 961 962 963
 * @file: input #GFile
 * @res: a #GAsyncResult
 * @error: a #GError
 *
Matthias Clasen's avatar
Matthias Clasen committed
964
 * Finishes an async enumerate children operation.
965
 * See g_file_enumerate_children_async().
Matthias Clasen's avatar
Matthias Clasen committed
966
 *
967 968
 * Returns: (transfer full): a #GFileEnumerator or %NULL
 *     if an error occurred.
Matthias Clasen's avatar
Matthias Clasen committed
969
 *     Free the returned object with g_object_unref().
970
 */
971
GFileEnumerator *
Matthias Clasen's avatar
Matthias Clasen committed
972
g_file_enumerate_children_finish (GFile         *file,
973 974
                                  GAsyncResult  *res,
                                  GError       **error)
975 976
{
  GFileIface *iface;
977

978 979 980
  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);

981 982
  if (g_async_result_legacy_propagate_error (res, error))
    return NULL;
983

984 985 986 987
  iface = G_FILE_GET_IFACE (file);
  return (* iface->enumerate_children_finish) (file, res, error);
}

988 989
/**
 * g_file_query_exists:
990 991 992
 * @file: input #GFile
 * @cancellable: (allow-none): optional #GCancellable object,
 *     %NULL to ignore
993 994 995 996
 *
 * Utility function to check if a particular file exists. This is
 * implemented using g_file_query_info() and as such does blocking I/O.
 *
997
 * Note that in many cases it is racy to first check for file existence
998
 * and then execute something based on the outcome of that, because the
999
 * file might have been created or removed in between the operations. The
1000 1001 1002
 * general approach to handling that is to not check, but just do the
 * operation and handle the errors as they come.
 *
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
 * As an example of race-free checking, take the case of reading a file,
 * and if it doesn't exist, creating it. There are two racy versions: read
 * it, and on error create it; and: check if it exists, if not create it.
 * These can both result in two processes creating the file (with perhaps
 * a partially written file as the result). The correct approach is to
 * always try to create the file with g_file_create() which will either
 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
 *
 * However, in many cases an existence check is useful in a user interface,
 * for instance to make a menu item sensitive/insensitive, so that you don't
 * have to fool users that something is possible and then just show an error
 * dialog. If you do this, you should make sure to also handle the errors
 * that can happen due to races when you execute the operation.
 *
 * Returns: %TRUE if the file exists (and can be detected without error),
 *     %FALSE otherwise (or if cancelled).
1019 1020
 */
gboolean
1021 1022
g_file_query_exists (GFile        *file,
                     GCancellable *cancellable)
1023 1024
{
  GFileInfo *info;
1025

1026
  g_return_val_if_fail (G_IS_FILE(file), FALSE);
1027

1028
  info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1029
                            G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1030 1031 1032 1033 1034
  if (info != NULL)
    {
      g_object_unref (info);
      return TRUE;
    }
1035

1036 1037
  return FALSE;
}
1038

1039 1040
/**
 * g_file_query_file_type:
1041 1042 1043 1044
 * @file: input #GFile
 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
 * @cancellable: (allow-none): optional #GCancellable object,
 *     %NULL to ignore
1045 1046 1047 1048
 *
 * Utility function to inspect the #GFileType of a file. This is
 * implemented using g_file_query_info() and as such does blocking I/O.
 *
1049 1050 1051 1052 1053
 * The primary use case of this method is to check if a file is
 * a regular file, directory, or symlink.
 *
 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
 *     if the file does not exist
1054 1055 1056 1057
 *
 * Since: 2.18
 */
GFileType
1058 1059 1060
g_file_query_file_type (GFile               *file,
                        GFileQueryInfoFlags  flags,
                        GCancellable        *cancellable)
1061 1062 1063
{
  GFileInfo *info;
  GFileType file_type;
1064

1065
  g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1066
  info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1067
                            cancellable, NULL);
1068 1069 1070 1071 1072 1073 1074
  if (info != NULL)
    {
      file_type = g_file_info_get_file_type (info);
      g_object_unref (info);
    }
  else
    file_type = G_FILE_TYPE_UNKNOWN;
1075

1076 1077 1078
  return file_type;
}

1079 1080
/**
 * g_file_query_info:
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
 * @file: input #GFile
 * @attributes: an attribute query string
 * @flags: a set of #GFileQueryInfoFlags
 * @cancellable: (allow-none): optional #GCancellable object,
 *     %NULL to ignore
 * @error: a #GError
 *
 * Gets the requested information about specified @file.
 * The result is a #GFileInfo object that contains key-value
 * attributes (such as the type or size of the file).
 *
 * The @attributes value is a string that specifies the file
 * attributes that should be gathered. It is not an error if
 * it's not possible to read a particular requested attribute
 * from a file - it just won't be set. @attributes should be a
 * comma-separated list of attributes or attribute wildcards.
 * The wildcard "*" means all attributes, and a wildcard like
 * "standard::*" means all attributes in the standard namespace.
 * An example attribute query be "standard::*,owner::user".
 * The standard attributes are available as defines, like
 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
 *
 * If @cancellable is not %NULL, then the operation can be cancelled
 * by triggering the cancellable object from another thread. If the
 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
 * returned.
1107 1108
 *
 * For symlinks, normally the information about the target of the
1109 1110 1111 1112 1113
 * symlink is returned, rather than information about the symlink
 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
 * in @flags the information about the symlink itself will be returned.
 * Also, for symlinks that point to non-existing files the information
 * about the symlink itself will be returned.
1114
 *
1115 1116 1117
 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
 * returned. Other errors are possible too, and depend on what kind of
 * filesystem the file is on.
1118
 *
1119 1120 1121
 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
 *     on error. Free the returned object with g_object_unref().
 */
1122
GFileInfo *
Matthias Clasen's avatar
Matthias Clasen committed
1123
g_file_query_info (GFile                *file,
1124 1125 1126 1127
                   const char           *attributes,
                   GFileQueryInfoFlags   flags,
                   GCancellable         *cancellable,
                   GError              **error)
1128 1129
{
  GFileIface *iface;
1130

1131 1132 1133 1134
  g_return_val_if_fail (G_IS_FILE (file), NULL);

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return NULL;
1135

1136 1137 1138 1139
  iface = G_FILE_GET_IFACE (file);

  if (iface->query_info == NULL)
    {
1140 1141 1142
      g_set_error_literal (error, G_IO_ERROR,
                           G_IO_ERROR_NOT_SUPPORTED,
                           _("Operation not supported"));
1143 1144
      return NULL;
    }
1145

1146 1147 1148 1149 1150
  return (* iface->query_info) (file, attributes, flags, cancellable, error);
}

/**
 * g_file_query_info_async:
1151 1152 1153 1154 1155 1156 1157 1158 1159
 * @file: input #GFile
 * @attributes: an attribute query string
 * @flags: a set of #GFileQueryInfoFlags
 * @io_priority: the <link linkend="io-priority">I/O priority</link>
 *     of the request
 * @cancellable: (allow-none): optional #GCancellable object,
 *     %NULL to ignore
 * @callback: (scope async): a #GAsyncReadyCallback to call when the
 *     request is satisfied
1160
 * @user_data: (closure): the data to pass to callback function
1161
 *
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
 * Asynchronously gets the requested information about specified @file.
 * The result is a #GFileInfo object that contains key-value attributes
 * (such as type or size for the file).
 *
 * For more details, see g_file_query_info() which is the synchronous
 * version of this call.
 *
 * When the operation is finished, @callback will be called. You can
 * then call g_file_query_info_finish() to get the result of the operation.
 */
1172
void
Matthias Clasen's avatar
Matthias Clasen committed
1173
g_file_query_info_async (GFile               *file,
1174 1175 1176 1177 1178 1179
                         const char          *attributes,
                         GFileQueryInfoFlags  flags,
                         int                  io_priority,
                         GCancellable        *cancellable,
                         GAsyncReadyCallback  callback,
                         gpointer             user_data)
1180 1181
{
  GFileIface *iface;
1182

1183 1184 1185 1186
  g_return_if_fail (G_IS_FILE (file));

  iface = G_FILE_GET_IFACE (file);
  (* iface->query_info_async) (file,
1187 1188 1189 1190 1191 1192
                               attributes,
                               flags,
                               io_priority,
                               cancellable,
                               callback,
                               user_data);
1193 1194 1195 1196
}

/**
 * g_file_query_info_finish:
1197 1198 1199 1200 1201
 * @file: input #GFile
 * @res: a #GAsyncResult
 * @error: a #GError
 *
 * Finishes an asynchronous file info query.
1202
 * See g_file_query_info_async().
1203 1204 1205 1206 1207
 *
 * Returns: (transfer full): #GFileInfo for given @file
 *     or %NULL on error. Free the returned object with
 *     g_object_unref().
 */
1208
GFileInfo *
Matthias Clasen's avatar
Matthias Clasen committed
1209
g_file_query_info_finish (GFile         *file,
1210 1211
                          GAsyncResult  *res,
                          GError       **error)
1212 1213 1214 1215 1216 1217
{
  GFileIface *iface;

  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);

1218 1219
  if (g_async_result_legacy_propagate_error (res, error))
    return NULL;
1220

1221 1222 1223 1224 1225 1226
  iface = G_FILE_GET_IFACE (file);
  return (* iface->query_info_finish) (file, res, error);
}

/**
 * g_file_query_filesystem_info:
1227 1228 1229 1230 1231 1232
 * @file: input #GFile
 * @attributes:  an attribute query string
 * @cancellable: (allow-none): optional #GCancellable object,
 *     %NULL to ignore
 * @error: a #GError
 *
1233 1234
 * Similar to g_file_query_info(), but obtains information
 * about the filesystem the @file is on, rather than the file itself.
1235
 * For instance the amount of space available and the type of
1236 1237
 * the filesystem.
 *
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
 * The @attributes value is a string that specifies the attributes
 * that should be gathered. It is not an error if it's not possible
 * to read a particular requested attribute from a file - it just
 * won't be set. @attributes should be a comma-separated list of
 * attributes or attribute wildcards. The wildcard "*" means all
 * attributes, and a wildcard like "filesystem::*" means all attributes
 * in the filesystem namespace. The standard namespace for filesystem
 * attributes is "filesystem". Common attributes of interest are
 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
 *
 * If @cancellable is not %NULL, then the operation can be cancelled
 * by triggering the cancellable object from another thread. If the
 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
 * returned.
 *
 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
 * be returned. Other errors are possible too, and depend on what
 * kind of filesystem the file is on.
1258
 *
1259
 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
Matthias Clasen's avatar
Matthias Clasen committed
1260
 *     Free the returned object with g_object_unref().
1261
 */
1262
GFileInfo *
Matthias Clasen's avatar
Matthias Clasen committed
1263
g_file_query_filesystem_info (GFile         *file,
1264 1265 1266
                              const char    *attributes,
                              GCancellable  *cancellable,
                              GError       **error)
1267 1268
{
  GFileIface *iface;
1269

1270 1271 1272 1273
  g_return_val_if_fail (G_IS_FILE (file), NULL);

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return NULL;
1274

1275 1276 1277 1278
  iface = G_FILE_GET_IFACE (file);

  if (iface->query_filesystem_info == NULL)
    {
1279 1280 1281
      g_set_error_literal (error, G_IO_ERROR,
                           G_IO_ERROR_NOT_SUPPORTED,
                           _("Operation not supported"));
1282 1283
      return NULL;
    }
1284

1285 1286 1287
  return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
}

1288 1289
/**
 * g_file_query_filesystem_info_async:
1290 1291 1292 1293 1294 1295 1296 1297
 * @file: input #GFile
 * @attributes: an attribute query string
 * @io_priority: the <link linkend="io-priority">I/O priority</link>
 *     of the request
 * @cancellable: (allow-none): optional #GCancellable object,
 *     %NULL to ignore
 * @callback: (scope async): a #GAsyncReadyCallback to call
 *     when the request is satisfied
1298
 * @user_data: (closure): the data to pass to callback function
1299
 *
1300 1301 1302 1303
 * Asynchronously gets the requested information about the filesystem
 * that the specified @file is on. The result is a #GFileInfo object
 * that contains key-value attributes (such as type or size for the
 * file).
1304
 *
1305 1306 1307 1308 1309 1310
 * For more details, see g_file_query_filesystem_info() which is the
 * synchronous version of this call.
 *
 * When the operation is finished, @callback will be called. You can
 * then call g_file_query_info_finish() to get the result of the
 * operation.
1311
 */
1312 1313 1314 1315 1316 1317 1318 1319 1320
void
g_file_query_filesystem_info_async (GFile               *file,
                                    const char          *attributes,
                                    int                  io_priority,
                                    GCancellable        *cancellable,
                                    GAsyncReadyCallback  callback,
                                    gpointer             user_data)
{
  GFileIface *iface;
1321

1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
  g_return_if_fail (G_IS_FILE (file));

  iface = G_FILE_GET_IFACE (file);
  (* iface->query_filesystem_info_async) (file,
                                          attributes,
                                          io_priority,
                                          cancellable,
                                          callback,
                                          user_data);
}

/**
 * g_file_query_filesystem_info_finish:
1335 1336 1337 1338 1339 1340