Imported Upstream version 2.62.1
[platform/upstream/glib.git] / gio / gfile.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4  * 
5  * Copyright (C) 2006-2007 Red Hat, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #ifdef __linux__
26 #include <sys/ioctl.h>
27 #include <errno.h>
28 /* See linux.git/fs/btrfs/ioctl.h */
29 #define BTRFS_IOCTL_MAGIC 0x94
30 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
31 #endif
32
33 #ifdef HAVE_SPLICE
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #endif
39
40 #include <string.h>
41 #include <sys/types.h>
42
43 #include "gfile.h"
44 #include "glib/gstdio.h"
45 #ifdef G_OS_UNIX
46 #include "glib-unix.h"
47 #endif
48 #include "gvfs.h"
49 #include "gtask.h"
50 #include "gfileattribute-priv.h"
51 #include "gfiledescriptorbased.h"
52 #include "gpollfilemonitor.h"
53 #include "gappinfo.h"
54 #include "gfileinputstream.h"
55 #include "gfileoutputstream.h"
56 #include "glocalfileoutputstream.h"
57 #include "glocalfileiostream.h"
58 #include "glocalfile.h"
59 #include "gcancellable.h"
60 #include "gasyncresult.h"
61 #include "gioerror.h"
62 #include "glibintl.h"
63
64
65 /**
66  * SECTION:gfile
67  * @short_description: File and Directory Handling
68  * @include: gio/gio.h
69  * @see_also: #GFileInfo, #GFileEnumerator
70  *
71  * #GFile is a high level abstraction for manipulating files on a
72  * virtual file system. #GFiles are lightweight, immutable objects
73  * that do no I/O upon creation. It is necessary to understand that
74  * #GFile objects do not represent files, merely an identifier for a
75  * file. All file content I/O is implemented as streaming operations
76  * (see #GInputStream and #GOutputStream).
77  *
78  * To construct a #GFile, you can use:
79  * - g_file_new_for_path() if you have a path.
80  * - g_file_new_for_uri() if you have a URI.
81  * - g_file_new_for_commandline_arg() for a command line argument.
82  * - g_file_new_tmp() to create a temporary file from a template.
83  * - g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().
84  * - g_file_new_build_filename() to create a file from path elements.
85  *
86  * One way to think of a #GFile is as an abstraction of a pathname. For
87  * normal files the system pathname is what is stored internally, but as
88  * #GFiles are extensible it could also be something else that corresponds
89  * to a pathname in a userspace implementation of a filesystem.
90  *
91  * #GFiles make up hierarchies of directories and files that correspond to
92  * the files on a filesystem. You can move through the file system with
93  * #GFile using g_file_get_parent() to get an identifier for the parent
94  * directory, g_file_get_child() to get a child within a directory,
95  * g_file_resolve_relative_path() to resolve a relative path between two
96  * #GFiles. There can be multiple hierarchies, so you may not end up at
97  * the same root if you repeatedly call g_file_get_parent() on two different
98  * files.
99  *
100  * All #GFiles have a basename (get with g_file_get_basename()). These names
101  * are byte strings that are used to identify the file on the filesystem
102  * (relative to its parent directory) and there is no guarantees that they
103  * have any particular charset encoding or even make any sense at all. If
104  * you want to use filenames in a user interface you should use the display
105  * name that you can get by requesting the
106  * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
107  * This is guaranteed to be in UTF-8 and can be used in a user interface.
108  * But always store the real basename or the #GFile to use to actually
109  * access the file, because there is no way to go from a display name to
110  * the actual name.
111  *
112  * Using #GFile as an identifier has the same weaknesses as using a path
113  * in that there may be multiple aliases for the same file. For instance,
114  * hard or soft links may cause two different #GFiles to refer to the same
115  * file. Other possible causes for aliases are: case insensitive filesystems,
116  * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
117  * check if two #GFiles point to the same file you can query for the
118  * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
119  * canonicalization of pathnames passed in, so that trivial differences in
120  * the path string used at creation (duplicated slashes, slash at end of
121  * path, "." or ".." path segments, etc) does not create different #GFiles.
122  *
123  * Many #GFile operations have both synchronous and asynchronous versions
124  * to suit your application. Asynchronous versions of synchronous functions
125  * simply have _async() appended to their function names. The asynchronous
126  * I/O functions call a #GAsyncReadyCallback which is then used to finalize
127  * the operation, producing a GAsyncResult which is then passed to the
128  * function's matching _finish() operation.
129  *
130  * It is highly recommended to use asynchronous calls when running within a
131  * shared main loop, such as in the main thread of an application. This avoids
132  * I/O operations blocking other sources on the main loop from being dispatched.
133  * Synchronous I/O operations should be performed from worker threads. See the
134  * [introduction to asynchronous programming section][async-programming] for
135  * more.
136  *
137  * Some #GFile operations almost always take a noticeable amount of time, and
138  * so do not have synchronous analogs. Notable cases include:
139  * - g_file_mount_mountable() to mount a mountable file.
140  * - g_file_unmount_mountable_with_operation() to unmount a mountable file.
141  * - g_file_eject_mountable_with_operation() to eject a mountable file.
142  *
143  * ## Entity Tags # {#gfile-etag}
144  *
145  * One notable feature of #GFiles are entity tags, or "etags" for
146  * short. Entity tags are somewhat like a more abstract version of the
147  * traditional mtime, and can be used to quickly determine if the file
148  * has been modified from the version on the file system. See the
149  * HTTP 1.1 
150  * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
151  * for HTTP Etag headers, which are a very similar concept.
152  */
153
154 static void               g_file_real_query_info_async            (GFile                  *file,
155                                                                    const char             *attributes,
156                                                                    GFileQueryInfoFlags     flags,
157                                                                    int                     io_priority,
158                                                                    GCancellable           *cancellable,
159                                                                    GAsyncReadyCallback     callback,
160                                                                    gpointer                user_data);
161 static GFileInfo *        g_file_real_query_info_finish           (GFile                  *file,
162                                                                    GAsyncResult           *res,
163                                                                    GError                **error);
164 static void               g_file_real_query_filesystem_info_async (GFile                  *file,
165                                                                    const char             *attributes,
166                                                                    int                     io_priority,
167                                                                    GCancellable           *cancellable,
168                                                                    GAsyncReadyCallback     callback,
169                                                                    gpointer                user_data);
170 static GFileInfo *        g_file_real_query_filesystem_info_finish (GFile                  *file,
171                                                                    GAsyncResult           *res,
172                                                                    GError                **error);
173 static void               g_file_real_enumerate_children_async    (GFile                  *file,
174                                                                    const char             *attributes,
175                                                                    GFileQueryInfoFlags     flags,
176                                                                    int                     io_priority,
177                                                                    GCancellable           *cancellable,
178                                                                    GAsyncReadyCallback     callback,
179                                                                    gpointer                user_data);
180 static GFileEnumerator *  g_file_real_enumerate_children_finish   (GFile                  *file,
181                                                                    GAsyncResult           *res,
182                                                                    GError                **error);
183 static void               g_file_real_read_async                  (GFile                  *file,
184                                                                    int                     io_priority,
185                                                                    GCancellable           *cancellable,
186                                                                    GAsyncReadyCallback     callback,
187                                                                    gpointer                user_data);
188 static GFileInputStream * g_file_real_read_finish                 (GFile                  *file,
189                                                                    GAsyncResult           *res,
190                                                                    GError                **error);
191 static void               g_file_real_append_to_async             (GFile                  *file,
192                                                                    GFileCreateFlags        flags,
193                                                                    int                     io_priority,
194                                                                    GCancellable           *cancellable,
195                                                                    GAsyncReadyCallback     callback,
196                                                                    gpointer                user_data);
197 static GFileOutputStream *g_file_real_append_to_finish            (GFile                  *file,
198                                                                    GAsyncResult           *res,
199                                                                    GError                **error);
200 static void               g_file_real_create_async                (GFile                  *file,
201                                                                    GFileCreateFlags        flags,
202                                                                    int                     io_priority,
203                                                                    GCancellable           *cancellable,
204                                                                    GAsyncReadyCallback     callback,
205                                                                    gpointer                user_data);
206 static GFileOutputStream *g_file_real_create_finish               (GFile                  *file,
207                                                                    GAsyncResult           *res,
208                                                                    GError                **error);
209 static void               g_file_real_replace_async               (GFile                  *file,
210                                                                    const char             *etag,
211                                                                    gboolean                make_backup,
212                                                                    GFileCreateFlags        flags,
213                                                                    int                     io_priority,
214                                                                    GCancellable           *cancellable,
215                                                                    GAsyncReadyCallback     callback,
216                                                                    gpointer                user_data);
217 static GFileOutputStream *g_file_real_replace_finish              (GFile                  *file,
218                                                                    GAsyncResult           *res,
219                                                                    GError                **error);
220 static void               g_file_real_delete_async                (GFile                  *file,
221                                                                    int                     io_priority,
222                                                                    GCancellable           *cancellable,
223                                                                    GAsyncReadyCallback     callback,
224                                                                    gpointer                user_data);
225 static gboolean           g_file_real_delete_finish               (GFile                  *file,
226                                                                    GAsyncResult           *res,
227                                                                    GError                **error);
228 static void               g_file_real_trash_async                 (GFile                  *file,
229                                                                    int                     io_priority,
230                                                                    GCancellable           *cancellable,
231                                                                    GAsyncReadyCallback     callback,
232                                                                    gpointer                user_data);
233 static gboolean           g_file_real_trash_finish                (GFile                  *file,
234                                                                    GAsyncResult           *res,
235                                                                    GError                **error);
236 static void               g_file_real_make_directory_async        (GFile                  *file,
237                                                                    int                     io_priority,
238                                                                    GCancellable           *cancellable,
239                                                                    GAsyncReadyCallback     callback,
240                                                                    gpointer                user_data);
241 static gboolean           g_file_real_make_directory_finish       (GFile                  *file,
242                                                                    GAsyncResult           *res,
243                                                                    GError                **error);
244 static void               g_file_real_open_readwrite_async        (GFile                  *file,
245                                                                    int                  io_priority,
246                                                                    GCancellable           *cancellable,
247                                                                    GAsyncReadyCallback     callback,
248                                                                    gpointer                user_data);
249 static GFileIOStream *    g_file_real_open_readwrite_finish       (GFile                  *file,
250                                                                    GAsyncResult           *res,
251                                                                    GError                **error);
252 static void               g_file_real_create_readwrite_async      (GFile                  *file,
253                                                                    GFileCreateFlags        flags,
254                                                                    int                     io_priority,
255                                                                    GCancellable           *cancellable,
256                                                                    GAsyncReadyCallback     callback,
257                                                                    gpointer                user_data);
258 static GFileIOStream *    g_file_real_create_readwrite_finish     (GFile                  *file,
259                                                                    GAsyncResult           *res,
260                                                                    GError                **error);
261 static void               g_file_real_replace_readwrite_async     (GFile                  *file,
262                                                                    const char             *etag,
263                                                                    gboolean                make_backup,
264                                                                    GFileCreateFlags        flags,
265                                                                    int                     io_priority,
266                                                                    GCancellable           *cancellable,
267                                                                    GAsyncReadyCallback     callback,
268                                                                    gpointer                user_data);
269 static GFileIOStream *    g_file_real_replace_readwrite_finish    (GFile                  *file,
270                                                                   GAsyncResult            *res,
271                                                                   GError                 **error);
272 static gboolean           g_file_real_set_attributes_from_info    (GFile                  *file,
273                                                                    GFileInfo              *info,
274                                                                    GFileQueryInfoFlags     flags,
275                                                                    GCancellable           *cancellable,
276                                                                    GError                **error);
277 static void               g_file_real_set_display_name_async      (GFile                  *file,
278                                                                    const char             *display_name,
279                                                                    int                     io_priority,
280                                                                    GCancellable           *cancellable,
281                                                                    GAsyncReadyCallback     callback,
282                                                                    gpointer                user_data);
283 static GFile *            g_file_real_set_display_name_finish     (GFile                  *file,
284                                                                    GAsyncResult           *res,
285                                                                    GError                **error);
286 static void               g_file_real_set_attributes_async        (GFile                  *file,
287                                                                    GFileInfo              *info,
288                                                                    GFileQueryInfoFlags     flags,
289                                                                    int                     io_priority,
290                                                                    GCancellable           *cancellable,
291                                                                    GAsyncReadyCallback     callback,
292                                                                    gpointer                user_data);
293 static gboolean           g_file_real_set_attributes_finish       (GFile                  *file,
294                                                                    GAsyncResult           *res,
295                                                                    GFileInfo             **info,
296                                                                    GError                **error);
297 static void               g_file_real_find_enclosing_mount_async  (GFile                  *file,
298                                                                    int                     io_priority,
299                                                                    GCancellable           *cancellable,
300                                                                    GAsyncReadyCallback     callback,
301                                                                    gpointer                user_data);
302 static GMount *           g_file_real_find_enclosing_mount_finish (GFile                  *file,
303                                                                    GAsyncResult           *res,
304                                                                    GError                **error);
305 static void               g_file_real_copy_async                  (GFile                  *source,
306                                                                    GFile                  *destination,
307                                                                    GFileCopyFlags          flags,
308                                                                    int                     io_priority,
309                                                                    GCancellable           *cancellable,
310                                                                    GFileProgressCallback   progress_callback,
311                                                                    gpointer                progress_callback_data,
312                                                                    GAsyncReadyCallback     callback,
313                                                                    gpointer                user_data);
314 static gboolean           g_file_real_copy_finish                 (GFile                  *file,
315                                                                    GAsyncResult           *res,
316                                                                    GError                **error);
317
318 static gboolean           g_file_real_measure_disk_usage          (GFile                         *file,
319                                                                    GFileMeasureFlags              flags,
320                                                                    GCancellable                  *cancellable,
321                                                                    GFileMeasureProgressCallback   progress_callback,
322                                                                    gpointer                       progress_data,
323                                                                    guint64                       *disk_usage,
324                                                                    guint64                       *num_dirs,
325                                                                    guint64                       *num_files,
326                                                                    GError                       **error);
327 static void               g_file_real_measure_disk_usage_async    (GFile                         *file,
328                                                                    GFileMeasureFlags              flags,
329                                                                    gint                           io_priority,
330                                                                    GCancellable                  *cancellable,
331                                                                    GFileMeasureProgressCallback   progress_callback,
332                                                                    gpointer                       progress_data,
333                                                                    GAsyncReadyCallback            callback,
334                                                                    gpointer                       user_data);
335 static gboolean           g_file_real_measure_disk_usage_finish   (GFile                         *file,
336                                                                    GAsyncResult                  *result,
337                                                                    guint64                       *disk_usage,
338                                                                    guint64                       *num_dirs,
339                                                                    guint64                       *num_files,
340                                                                    GError                       **error);
341
342 typedef GFileIface GFileInterface;
343 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
344
345 static void
346 g_file_default_init (GFileIface *iface)
347 {
348   iface->enumerate_children_async = g_file_real_enumerate_children_async;
349   iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
350   iface->set_display_name_async = g_file_real_set_display_name_async;
351   iface->set_display_name_finish = g_file_real_set_display_name_finish;
352   iface->query_info_async = g_file_real_query_info_async;
353   iface->query_info_finish = g_file_real_query_info_finish;
354   iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
355   iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
356   iface->set_attributes_async = g_file_real_set_attributes_async;
357   iface->set_attributes_finish = g_file_real_set_attributes_finish;
358   iface->read_async = g_file_real_read_async;
359   iface->read_finish = g_file_real_read_finish;
360   iface->append_to_async = g_file_real_append_to_async;
361   iface->append_to_finish = g_file_real_append_to_finish;
362   iface->create_async = g_file_real_create_async;
363   iface->create_finish = g_file_real_create_finish;
364   iface->replace_async = g_file_real_replace_async;
365   iface->replace_finish = g_file_real_replace_finish;
366   iface->delete_file_async = g_file_real_delete_async;
367   iface->delete_file_finish = g_file_real_delete_finish;
368   iface->trash_async = g_file_real_trash_async;
369   iface->trash_finish = g_file_real_trash_finish;
370   iface->make_directory_async = g_file_real_make_directory_async;
371   iface->make_directory_finish = g_file_real_make_directory_finish;
372   iface->open_readwrite_async = g_file_real_open_readwrite_async;
373   iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
374   iface->create_readwrite_async = g_file_real_create_readwrite_async;
375   iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
376   iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
377   iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
378   iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
379   iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
380   iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
381   iface->copy_async = g_file_real_copy_async;
382   iface->copy_finish = g_file_real_copy_finish;
383   iface->measure_disk_usage = g_file_real_measure_disk_usage;
384   iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
385   iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
386 }
387
388
389 /**
390  * g_file_is_native:
391  * @file: input #GFile
392  *
393  * Checks to see if a file is native to the platform.
394  *
395  * A native file is one expressed in the platform-native filename format,
396  * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
397  * as it might be on a locally mounted remote filesystem.
398  *
399  * On some systems non-native files may be available using the native
400  * filesystem via a userspace filesystem (FUSE), in these cases this call
401  * will return %FALSE, but g_file_get_path() will still return a native path.
402  *
403  * This call does no blocking I/O.
404  *
405  * Returns: %TRUE if @file is native
406  */
407 gboolean
408 g_file_is_native (GFile *file)
409 {
410   GFileIface *iface;
411
412   g_return_val_if_fail (G_IS_FILE (file), FALSE);
413
414   iface = G_FILE_GET_IFACE (file);
415
416   return (* iface->is_native) (file);
417 }
418
419
420 /**
421  * g_file_has_uri_scheme:
422  * @file: input #GFile
423  * @uri_scheme: a string containing a URI scheme
424  *
425  * Checks to see if a #GFile has a given URI scheme.
426  *
427  * This call does no blocking I/O.
428  *
429  * Returns: %TRUE if #GFile's backend supports the
430  *     given URI scheme, %FALSE if URI scheme is %NULL,
431  *     not supported, or #GFile is invalid.
432  */
433 gboolean
434 g_file_has_uri_scheme (GFile      *file,
435                        const char *uri_scheme)
436 {
437   GFileIface *iface;
438
439   g_return_val_if_fail (G_IS_FILE (file), FALSE);
440   g_return_val_if_fail (uri_scheme != NULL, FALSE);
441
442   iface = G_FILE_GET_IFACE (file);
443
444   return (* iface->has_uri_scheme) (file, uri_scheme);
445 }
446
447
448 /**
449  * g_file_get_uri_scheme:
450  * @file: input #GFile
451  *
452  * Gets the URI scheme for a #GFile.
453  * RFC 3986 decodes the scheme as:
454  * |[
455  * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
456  * ]|
457  * Common schemes include "file", "http", "ftp", etc.
458  *
459  * This call does no blocking I/O.
460  *
461  * Returns: a string containing the URI scheme for the given
462  *     #GFile. The returned string should be freed with g_free()
463  *     when no longer needed.
464  */
465 char *
466 g_file_get_uri_scheme (GFile *file)
467 {
468   GFileIface *iface;
469
470   g_return_val_if_fail (G_IS_FILE (file), NULL);
471
472   iface = G_FILE_GET_IFACE (file);
473
474   return (* iface->get_uri_scheme) (file);
475 }
476
477
478 /**
479  * g_file_get_basename:
480  * @file: input #GFile
481  *
482  * Gets the base name (the last component of the path) for a given #GFile.
483  *
484  * If called for the top level of a system (such as the filesystem root
485  * or a uri like sftp://host/) it will return a single directory separator
486  * (and on Windows, possibly a drive letter).
487  *
488  * The base name is a byte string (not UTF-8). It has no defined encoding
489  * or rules other than it may not contain zero bytes.  If you want to use
490  * filenames in a user interface you should use the display name that you
491  * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
492  * attribute with g_file_query_info().
493  *
494  * This call does no blocking I/O.
495  *
496  * Returns: (type filename) (nullable): string containing the #GFile's
497  *     base name, or %NULL if given #GFile is invalid. The returned string
498  *     should be freed with g_free() when no longer needed.
499  */
500 char *
501 g_file_get_basename (GFile *file)
502 {
503   GFileIface *iface;
504
505   g_return_val_if_fail (G_IS_FILE (file), NULL);
506
507   iface = G_FILE_GET_IFACE (file);
508
509   return (* iface->get_basename) (file);
510 }
511
512 /**
513  * g_file_get_path:
514  * @file: input #GFile
515  *
516  * Gets the local pathname for #GFile, if one exists. If non-%NULL, this is
517  * guaranteed to be an absolute, canonical path. It might contain symlinks.
518  *
519  * This call does no blocking I/O.
520  *
521  * Returns: (type filename) (nullable): string containing the #GFile's path,
522  *     or %NULL if no such path exists. The returned string should be freed
523  *     with g_free() when no longer needed.
524  */
525 char *
526 g_file_get_path (GFile *file)
527 {
528   GFileIface *iface;
529
530   g_return_val_if_fail (G_IS_FILE (file), NULL);
531
532   iface = G_FILE_GET_IFACE (file);
533
534   return (* iface->get_path) (file);
535 }
536
537 /* Original commit introducing this in libgsystem:
538  *
539  *  fileutil: Handle recent: and trash: URIs
540  *
541  *  The gs_file_get_path_cached() was rather brittle in its handling
542  *  of URIs. It would assert() when a GFile didn't have a backing path
543  *  (such as when handling trash: or recent: URIs), and didn't know
544  *  how to get the target URI for those items either.
545  *
546  *  Make sure that we do not assert() when a backing path cannot be
547  *  found, and handle recent: and trash: URIs.
548  *
549  *  https://bugzilla.gnome.org/show_bug.cgi?id=708435
550  */
551 static char *
552 file_get_target_path (GFile *file)
553 {
554   GFileInfo *info;
555   const char *target;
556   char *path;
557
558   info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI, G_FILE_QUERY_INFO_NONE, NULL, NULL);
559   if (info == NULL)
560     return NULL;
561   target = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI);
562   path = g_filename_from_uri (target, NULL, NULL);
563   g_object_unref (info);
564
565   return path;
566 }
567
568 static const char *
569 file_peek_path_generic (GFile *file)
570 {
571   const char *path;
572   static GQuark _file_path_quark = 0;
573
574   if (G_UNLIKELY (_file_path_quark) == 0)
575     _file_path_quark = g_quark_from_static_string ("gio-file-path");
576
577   /* We need to be careful about threading, as two threads calling
578    * g_file_peek_path() on the same file could race: both would see
579    * (g_object_get_qdata(…) == NULL) to begin with, both would generate and add
580    * the path, but the second thread to add it would end up freeing the path
581    * set by the first thread. The first thread would still return the pointer
582    * to that freed path, though, resulting an a read-after-free. Handle that
583    * with a compare-and-swap loop. The g_object_*_qdata() functions are atomic. */
584
585   while (TRUE)
586     {
587       gchar *new_path = NULL;
588
589       path = g_object_get_qdata ((GObject*)file, _file_path_quark);
590
591       if (path != NULL)
592         break;
593
594       if (g_file_has_uri_scheme (file, "trash") ||
595           g_file_has_uri_scheme (file, "recent"))
596         new_path = file_get_target_path (file);
597       else
598         new_path = g_file_get_path (file);
599       if (new_path == NULL)
600         return NULL;
601
602       /* By passing NULL here, we ensure we never replace existing data: */
603       if (g_object_replace_qdata ((GObject *) file, _file_path_quark,
604                                   NULL, (gpointer) new_path,
605                                   (GDestroyNotify) g_free, NULL))
606         break;
607       else
608         g_free (new_path);
609     }
610
611   return path;
612 }
613
614 /**
615  * g_file_peek_path:
616  * @file: input #GFile
617  *
618  * Exactly like g_file_get_path(), but caches the result via
619  * g_object_set_qdata_full().  This is useful for example in C
620  * applications which mix `g_file_*` APIs with native ones.  It
621  * also avoids an extra duplicated string when possible, so will be
622  * generally more efficient.
623  *
624  * This call does no blocking I/O.
625  *
626  * Returns: (type filename) (nullable): string containing the #GFile's path,
627  *     or %NULL if no such path exists. The returned string is owned by @file.
628  * Since: 2.56
629  */
630 const char *
631 g_file_peek_path (GFile *file)
632 {
633   if (G_IS_LOCAL_FILE (file))
634     return _g_local_file_get_filename ((GLocalFile *) file);
635   return file_peek_path_generic (file);
636 }
637
638 /**
639  * g_file_get_uri:
640  * @file: input #GFile
641  *
642  * Gets the URI for the @file.
643  *
644  * This call does no blocking I/O.
645  *
646  * Returns: a string containing the #GFile's URI.
647  *     The returned string should be freed with g_free()
648  *     when no longer needed.
649  */
650 char *
651 g_file_get_uri (GFile *file)
652 {
653   GFileIface *iface;
654
655   g_return_val_if_fail (G_IS_FILE (file), NULL);
656
657   iface = G_FILE_GET_IFACE (file);
658
659   return (* iface->get_uri) (file);
660 }
661
662 /**
663  * g_file_get_parse_name:
664  * @file: input #GFile
665  *
666  * Gets the parse name of the @file.
667  * A parse name is a UTF-8 string that describes the
668  * file such that one can get the #GFile back using
669  * g_file_parse_name().
670  *
671  * This is generally used to show the #GFile as a nice
672  * full-pathname kind of string in a user interface,
673  * like in a location entry.
674  *
675  * For local files with names that can safely be converted
676  * to UTF-8 the pathname is used, otherwise the IRI is used
677  * (a form of URI that allows UTF-8 characters unescaped).
678  *
679  * This call does no blocking I/O.
680  *
681  * Returns: a string containing the #GFile's parse name.
682  *     The returned string should be freed with g_free()
683  *     when no longer needed.
684  */
685 char *
686 g_file_get_parse_name (GFile *file)
687 {
688   GFileIface *iface;
689
690   g_return_val_if_fail (G_IS_FILE (file), NULL);
691
692   iface = G_FILE_GET_IFACE (file);
693
694   return (* iface->get_parse_name) (file);
695 }
696
697 /**
698  * g_file_dup:
699  * @file: input #GFile
700  *
701  * Duplicates a #GFile handle. This operation does not duplicate
702  * the actual file or directory represented by the #GFile; see
703  * g_file_copy() if attempting to copy a file.
704  *
705  * g_file_dup() is useful when a second handle is needed to the same underlying
706  * file, for use in a separate thread (#GFile is not thread-safe). For use
707  * within the same thread, use g_object_ref() to increment the existing object’s
708  * reference count.
709  *
710  * This call does no blocking I/O.
711  *
712  * Returns: (transfer full): a new #GFile that is a duplicate
713  *     of the given #GFile.
714  */
715 GFile *
716 g_file_dup (GFile *file)
717 {
718   GFileIface *iface;
719
720   g_return_val_if_fail (G_IS_FILE (file), NULL);
721
722   iface = G_FILE_GET_IFACE (file);
723
724   return (* iface->dup) (file);
725 }
726
727 /**
728  * g_file_hash:
729  * @file: (type GFile): #gconstpointer to a #GFile
730  *
731  * Creates a hash value for a #GFile.
732  *
733  * This call does no blocking I/O.
734  *
735  * Virtual: hash
736  * Returns: 0 if @file is not a valid #GFile, otherwise an
737  *     integer that can be used as hash value for the #GFile.
738  *     This function is intended for easily hashing a #GFile to
739  *     add to a #GHashTable or similar data structure.
740  */
741 guint
742 g_file_hash (gconstpointer file)
743 {
744   GFileIface *iface;
745
746   g_return_val_if_fail (G_IS_FILE (file), 0);
747
748   iface = G_FILE_GET_IFACE (file);
749
750   return (* iface->hash) ((GFile *)file);
751 }
752
753 /**
754  * g_file_equal:
755  * @file1: the first #GFile
756  * @file2: the second #GFile
757  *
758  * Checks if the two given #GFiles refer to the same file.
759  *
760  * Note that two #GFiles that differ can still refer to the same
761  * file on the filesystem due to various forms of filename
762  * aliasing.
763  *
764  * This call does no blocking I/O.
765  *
766  * Returns: %TRUE if @file1 and @file2 are equal.
767  */
768 gboolean
769 g_file_equal (GFile *file1,
770               GFile *file2)
771 {
772   GFileIface *iface;
773
774   g_return_val_if_fail (G_IS_FILE (file1), FALSE);
775   g_return_val_if_fail (G_IS_FILE (file2), FALSE);
776
777   if (file1 == file2)
778     return TRUE;
779
780   if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
781     return FALSE;
782
783   iface = G_FILE_GET_IFACE (file1);
784
785   return (* iface->equal) (file1, file2);
786 }
787
788
789 /**
790  * g_file_get_parent:
791  * @file: input #GFile
792  *
793  * Gets the parent directory for the @file.
794  * If the @file represents the root directory of the
795  * file system, then %NULL will be returned.
796  *
797  * This call does no blocking I/O.
798  *
799  * Returns: (nullable) (transfer full): a #GFile structure to the
800  *     parent of the given #GFile or %NULL if there is no parent. Free
801  *     the returned object with g_object_unref().
802  */
803 GFile *
804 g_file_get_parent (GFile *file)
805 {
806   GFileIface *iface;
807
808   g_return_val_if_fail (G_IS_FILE (file), NULL);
809
810   iface = G_FILE_GET_IFACE (file);
811
812   return (* iface->get_parent) (file);
813 }
814
815 /**
816  * g_file_has_parent:
817  * @file: input #GFile
818  * @parent: (nullable): the parent to check for, or %NULL
819  *
820  * Checks if @file has a parent, and optionally, if it is @parent.
821  *
822  * If @parent is %NULL then this function returns %TRUE if @file has any
823  * parent at all.  If @parent is non-%NULL then %TRUE is only returned
824  * if @file is an immediate child of @parent.
825  *
826  * Returns: %TRUE if @file is an immediate child of @parent (or any parent in
827  *          the case that @parent is %NULL).
828  *
829  * Since: 2.24
830  */
831 gboolean
832 g_file_has_parent (GFile *file,
833                    GFile *parent)
834 {
835   GFile *actual_parent;
836   gboolean result;
837
838   g_return_val_if_fail (G_IS_FILE (file), FALSE);
839   g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
840
841   actual_parent = g_file_get_parent (file);
842
843   if (actual_parent != NULL)
844     {
845       if (parent != NULL)
846         result = g_file_equal (parent, actual_parent);
847       else
848         result = TRUE;
849
850       g_object_unref (actual_parent);
851     }
852   else
853     result = FALSE;
854
855   return result;
856 }
857
858 /**
859  * g_file_get_child:
860  * @file: input #GFile
861  * @name: (type filename): string containing the child's basename
862  *
863  * Gets a child of @file with basename equal to @name.
864  *
865  * Note that the file with that specific name might not exist, but
866  * you can still have a #GFile that points to it. You can use this
867  * for instance to create that file.
868  *
869  * This call does no blocking I/O.
870  *
871  * Returns: (transfer full): a #GFile to a child specified by @name.
872  *     Free the returned object with g_object_unref().
873  */
874 GFile *
875 g_file_get_child (GFile      *file,
876                   const char *name)
877 {
878   g_return_val_if_fail (G_IS_FILE (file), NULL);
879   g_return_val_if_fail (name != NULL, NULL);
880
881   return g_file_resolve_relative_path (file, name);
882 }
883
884 /**
885  * g_file_get_child_for_display_name:
886  * @file: input #GFile
887  * @display_name: string to a possible child
888  * @error: return location for an error
889  *
890  * Gets the child of @file for a given @display_name (i.e. a UTF-8
891  * version of the name). If this function fails, it returns %NULL
892  * and @error will be set. This is very useful when constructing a
893  * #GFile for a new file and the user entered the filename in the
894  * user interface, for instance when you select a directory and
895  * type a filename in the file selector.
896  *
897  * This call does no blocking I/O.
898  *
899  * Returns: (transfer full): a #GFile to the specified child, or
900  *     %NULL if the display name couldn't be converted.
901  *     Free the returned object with g_object_unref().
902  */
903 GFile *
904 g_file_get_child_for_display_name (GFile      *file,
905                                    const char *display_name,
906                                    GError **error)
907 {
908   GFileIface *iface;
909
910   g_return_val_if_fail (G_IS_FILE (file), NULL);
911   g_return_val_if_fail (display_name != NULL, NULL);
912
913   iface = G_FILE_GET_IFACE (file);
914
915   return (* iface->get_child_for_display_name) (file, display_name, error);
916 }
917
918 /**
919  * g_file_has_prefix:
920  * @file: input #GFile
921  * @prefix: input #GFile
922  *
923  * Checks whether @file has the prefix specified by @prefix.
924  *
925  * In other words, if the names of initial elements of @file's
926  * pathname match @prefix. Only full pathname elements are matched,
927  * so a path like /foo is not considered a prefix of /foobar, only
928  * of /foo/bar.
929  *
930  * A #GFile is not a prefix of itself. If you want to check for
931  * equality, use g_file_equal().
932  *
933  * This call does no I/O, as it works purely on names. As such it can
934  * sometimes return %FALSE even if @file is inside a @prefix (from a
935  * filesystem point of view), because the prefix of @file is an alias
936  * of @prefix.
937  *
938  * Virtual: prefix_matches
939  * Returns:  %TRUE if the @files's parent, grandparent, etc is @prefix,
940  *     %FALSE otherwise.
941  */
942 gboolean
943 g_file_has_prefix (GFile *file,
944                    GFile *prefix)
945 {
946   GFileIface *iface;
947
948   g_return_val_if_fail (G_IS_FILE (file), FALSE);
949   g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
950
951   if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
952     return FALSE;
953
954   iface = G_FILE_GET_IFACE (file);
955
956   /* The vtable function differs in arg order since
957    * we're using the old contains_file call
958    */
959   return (* iface->prefix_matches) (prefix, file);
960 }
961
962 /**
963  * g_file_get_relative_path:
964  * @parent: input #GFile
965  * @descendant: input #GFile
966  *
967  * Gets the path for @descendant relative to @parent.
968  *
969  * This call does no blocking I/O.
970  *
971  * Returns: (type filename) (nullable): string with the relative path from
972  *     @descendant to @parent, or %NULL if @descendant doesn't have @parent as
973  *     prefix. The returned string should be freed with g_free() when
974  *     no longer needed.
975  */
976 char *
977 g_file_get_relative_path (GFile *parent,
978                           GFile *descendant)
979 {
980   GFileIface *iface;
981
982   g_return_val_if_fail (G_IS_FILE (parent), NULL);
983   g_return_val_if_fail (G_IS_FILE (descendant), NULL);
984
985   if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
986     return NULL;
987
988   iface = G_FILE_GET_IFACE (parent);
989
990   return (* iface->get_relative_path) (parent, descendant);
991 }
992
993 /**
994  * g_file_resolve_relative_path:
995  * @file: input #GFile
996  * @relative_path: (type filename): a given relative path string
997  *
998  * Resolves a relative path for @file to an absolute path.
999  *
1000  * This call does no blocking I/O.
1001  *
1002  * Returns: (transfer full): #GFile to the resolved path.
1003  *     %NULL if @relative_path is %NULL or if @file is invalid.
1004  *     Free the returned object with g_object_unref().
1005  */
1006 GFile *
1007 g_file_resolve_relative_path (GFile      *file,
1008                               const char *relative_path)
1009 {
1010   GFileIface *iface;
1011
1012   g_return_val_if_fail (G_IS_FILE (file), NULL);
1013   g_return_val_if_fail (relative_path != NULL, NULL);
1014
1015   iface = G_FILE_GET_IFACE (file);
1016
1017   return (* iface->resolve_relative_path) (file, relative_path);
1018 }
1019
1020 /**
1021  * g_file_enumerate_children:
1022  * @file: input #GFile
1023  * @attributes: an attribute query string
1024  * @flags: a set of #GFileQueryInfoFlags
1025  * @cancellable: (nullable): optional #GCancellable object,
1026  *     %NULL to ignore
1027  * @error: #GError for error reporting
1028  *
1029  * Gets the requested information about the files in a directory.
1030  * The result is a #GFileEnumerator object that will give out
1031  * #GFileInfo objects for all the files in the directory.
1032  *
1033  * The @attributes value is a string that specifies the file
1034  * attributes that should be gathered. It is not an error if
1035  * it's not possible to read a particular requested attribute
1036  * from a file - it just won't be set. @attributes should
1037  * be a comma-separated list of attributes or attribute wildcards.
1038  * The wildcard "*" means all attributes, and a wildcard like
1039  * "standard::*" means all attributes in the standard namespace.
1040  * An example attribute query be "standard::*,owner::user".
1041  * The standard attributes are available as defines, like
1042  * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1043  *
1044  * If @cancellable is not %NULL, then the operation can be cancelled
1045  * by triggering the cancellable object from another thread. If the
1046  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1047  * returned.
1048  *
1049  * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1050  * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
1051  * error will be returned. Other errors are possible too.
1052  *
1053  * Returns: (transfer full): A #GFileEnumerator if successful,
1054  *     %NULL on error. Free the returned object with g_object_unref().
1055  */
1056 GFileEnumerator *
1057 g_file_enumerate_children (GFile                *file,
1058                            const char           *attributes,
1059                            GFileQueryInfoFlags   flags,
1060                            GCancellable         *cancellable,
1061                            GError              **error)
1062 {
1063   GFileIface *iface;
1064
1065   g_return_val_if_fail (G_IS_FILE (file), NULL);
1066
1067   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1068     return NULL;
1069
1070   iface = G_FILE_GET_IFACE (file);
1071
1072   if (iface->enumerate_children == NULL)
1073     {
1074       g_set_error_literal (error, G_IO_ERROR,
1075                            G_IO_ERROR_NOT_SUPPORTED,
1076                            _("Operation not supported"));
1077       return NULL;
1078     }
1079
1080   return (* iface->enumerate_children) (file, attributes, flags,
1081                                         cancellable, error);
1082 }
1083
1084 /**
1085  * g_file_enumerate_children_async:
1086  * @file: input #GFile
1087  * @attributes: an attribute query string
1088  * @flags: a set of #GFileQueryInfoFlags
1089  * @io_priority: the [I/O priority][io-priority] of the request
1090  * @cancellable: (nullable): optional #GCancellable object,
1091  *     %NULL to ignore
1092  * @callback: (scope async): a #GAsyncReadyCallback to call when the
1093  *     request is satisfied
1094  * @user_data: (closure): the data to pass to callback function
1095  *
1096  * Asynchronously gets the requested information about the files
1097  * in a directory. The result is a #GFileEnumerator object that will
1098  * give out #GFileInfo objects for all the files in the directory.
1099  *
1100  * For more details, see g_file_enumerate_children() which is
1101  * the synchronous version of this call.
1102  *
1103  * When the operation is finished, @callback will be called. You can
1104  * then call g_file_enumerate_children_finish() to get the result of
1105  * the operation.
1106  */
1107 void
1108 g_file_enumerate_children_async (GFile               *file,
1109                                  const char          *attributes,
1110                                  GFileQueryInfoFlags  flags,
1111                                  int                  io_priority,
1112                                  GCancellable        *cancellable,
1113                                  GAsyncReadyCallback  callback,
1114                                  gpointer             user_data)
1115 {
1116   GFileIface *iface;
1117
1118   g_return_if_fail (G_IS_FILE (file));
1119
1120   iface = G_FILE_GET_IFACE (file);
1121   (* iface->enumerate_children_async) (file,
1122                                        attributes,
1123                                        flags,
1124                                        io_priority,
1125                                        cancellable,
1126                                        callback,
1127                                        user_data);
1128 }
1129
1130 /**
1131  * g_file_enumerate_children_finish:
1132  * @file: input #GFile
1133  * @res: a #GAsyncResult
1134  * @error: a #GError
1135  *
1136  * Finishes an async enumerate children operation.
1137  * See g_file_enumerate_children_async().
1138  *
1139  * Returns: (transfer full): a #GFileEnumerator or %NULL
1140  *     if an error occurred.
1141  *     Free the returned object with g_object_unref().
1142  */
1143 GFileEnumerator *
1144 g_file_enumerate_children_finish (GFile         *file,
1145                                   GAsyncResult  *res,
1146                                   GError       **error)
1147 {
1148   GFileIface *iface;
1149
1150   g_return_val_if_fail (G_IS_FILE (file), NULL);
1151   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1152
1153   if (g_async_result_legacy_propagate_error (res, error))
1154     return NULL;
1155
1156   iface = G_FILE_GET_IFACE (file);
1157   return (* iface->enumerate_children_finish) (file, res, error);
1158 }
1159
1160 /**
1161  * g_file_query_exists:
1162  * @file: input #GFile
1163  * @cancellable: (nullable): optional #GCancellable object,
1164  *     %NULL to ignore
1165  *
1166  * Utility function to check if a particular file exists. This is
1167  * implemented using g_file_query_info() and as such does blocking I/O.
1168  *
1169  * Note that in many cases it is [racy to first check for file existence](https://en.wikipedia.org/wiki/Time_of_check_to_time_of_use)
1170  * and then execute something based on the outcome of that, because the
1171  * file might have been created or removed in between the operations. The
1172  * general approach to handling that is to not check, but just do the
1173  * operation and handle the errors as they come.
1174  *
1175  * As an example of race-free checking, take the case of reading a file,
1176  * and if it doesn't exist, creating it. There are two racy versions: read
1177  * it, and on error create it; and: check if it exists, if not create it.
1178  * These can both result in two processes creating the file (with perhaps
1179  * a partially written file as the result). The correct approach is to
1180  * always try to create the file with g_file_create() which will either
1181  * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1182  *
1183  * However, in many cases an existence check is useful in a user interface,
1184  * for instance to make a menu item sensitive/insensitive, so that you don't
1185  * have to fool users that something is possible and then just show an error
1186  * dialog. If you do this, you should make sure to also handle the errors
1187  * that can happen due to races when you execute the operation.
1188  *
1189  * Returns: %TRUE if the file exists (and can be detected without error),
1190  *     %FALSE otherwise (or if cancelled).
1191  */
1192 gboolean
1193 g_file_query_exists (GFile        *file,
1194                      GCancellable *cancellable)
1195 {
1196   GFileInfo *info;
1197
1198   g_return_val_if_fail (G_IS_FILE(file), FALSE);
1199
1200   info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1201                             G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1202   if (info != NULL)
1203     {
1204       g_object_unref (info);
1205       return TRUE;
1206     }
1207
1208   return FALSE;
1209 }
1210
1211 /**
1212  * g_file_query_file_type:
1213  * @file: input #GFile
1214  * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1215  * @cancellable: (nullable): optional #GCancellable object,
1216  *     %NULL to ignore
1217  *
1218  * Utility function to inspect the #GFileType of a file. This is
1219  * implemented using g_file_query_info() and as such does blocking I/O.
1220  *
1221  * The primary use case of this method is to check if a file is
1222  * a regular file, directory, or symlink.
1223  *
1224  * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1225  *     if the file does not exist
1226  *
1227  * Since: 2.18
1228  */
1229 GFileType
1230 g_file_query_file_type (GFile               *file,
1231                         GFileQueryInfoFlags  flags,
1232                         GCancellable        *cancellable)
1233 {
1234   GFileInfo *info;
1235   GFileType file_type;
1236
1237   g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1238   info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1239                             cancellable, NULL);
1240   if (info != NULL)
1241     {
1242       file_type = g_file_info_get_file_type (info);
1243       g_object_unref (info);
1244     }
1245   else
1246     file_type = G_FILE_TYPE_UNKNOWN;
1247
1248   return file_type;
1249 }
1250
1251 /**
1252  * g_file_query_info:
1253  * @file: input #GFile
1254  * @attributes: an attribute query string
1255  * @flags: a set of #GFileQueryInfoFlags
1256  * @cancellable: (nullable): optional #GCancellable object,
1257  *     %NULL to ignore
1258  * @error: a #GError
1259  *
1260  * Gets the requested information about specified @file.
1261  * The result is a #GFileInfo object that contains key-value
1262  * attributes (such as the type or size of the file).
1263  *
1264  * The @attributes value is a string that specifies the file
1265  * attributes that should be gathered. It is not an error if
1266  * it's not possible to read a particular requested attribute
1267  * from a file - it just won't be set. @attributes should be a
1268  * comma-separated list of attributes or attribute wildcards.
1269  * The wildcard "*" means all attributes, and a wildcard like
1270  * "standard::*" means all attributes in the standard namespace.
1271  * An example attribute query be "standard::*,owner::user".
1272  * The standard attributes are available as defines, like
1273  * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1274  *
1275  * If @cancellable is not %NULL, then the operation can be cancelled
1276  * by triggering the cancellable object from another thread. If the
1277  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1278  * returned.
1279  *
1280  * For symlinks, normally the information about the target of the
1281  * symlink is returned, rather than information about the symlink
1282  * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1283  * in @flags the information about the symlink itself will be returned.
1284  * Also, for symlinks that point to non-existing files the information
1285  * about the symlink itself will be returned.
1286  *
1287  * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1288  * returned. Other errors are possible too, and depend on what kind of
1289  * filesystem the file is on.
1290  *
1291  * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1292  *     on error. Free the returned object with g_object_unref().
1293  */
1294 GFileInfo *
1295 g_file_query_info (GFile                *file,
1296                    const char           *attributes,
1297                    GFileQueryInfoFlags   flags,
1298                    GCancellable         *cancellable,
1299                    GError              **error)
1300 {
1301   GFileIface *iface;
1302
1303   g_return_val_if_fail (G_IS_FILE (file), NULL);
1304
1305   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1306     return NULL;
1307
1308   iface = G_FILE_GET_IFACE (file);
1309
1310   if (iface->query_info == NULL)
1311     {
1312       g_set_error_literal (error, G_IO_ERROR,
1313                            G_IO_ERROR_NOT_SUPPORTED,
1314                            _("Operation not supported"));
1315       return NULL;
1316     }
1317
1318   return (* iface->query_info) (file, attributes, flags, cancellable, error);
1319 }
1320
1321 /**
1322  * g_file_query_info_async:
1323  * @file: input #GFile
1324  * @attributes: an attribute query string
1325  * @flags: a set of #GFileQueryInfoFlags
1326  * @io_priority: the [I/O priority][io-priority] of the request
1327  * @cancellable: (nullable): optional #GCancellable object,
1328  *     %NULL to ignore
1329  * @callback: (scope async): a #GAsyncReadyCallback to call when the
1330  *     request is satisfied
1331  * @user_data: (closure): the data to pass to callback function
1332  *
1333  * Asynchronously gets the requested information about specified @file.
1334  * The result is a #GFileInfo object that contains key-value attributes
1335  * (such as type or size for the file).
1336  *
1337  * For more details, see g_file_query_info() which is the synchronous
1338  * version of this call.
1339  *
1340  * When the operation is finished, @callback will be called. You can
1341  * then call g_file_query_info_finish() to get the result of the operation.
1342  */
1343 void
1344 g_file_query_info_async (GFile               *file,
1345                          const char          *attributes,
1346                          GFileQueryInfoFlags  flags,
1347                          int                  io_priority,
1348                          GCancellable        *cancellable,
1349                          GAsyncReadyCallback  callback,
1350                          gpointer             user_data)
1351 {
1352   GFileIface *iface;
1353
1354   g_return_if_fail (G_IS_FILE (file));
1355
1356   iface = G_FILE_GET_IFACE (file);
1357   (* iface->query_info_async) (file,
1358                                attributes,
1359                                flags,
1360                                io_priority,
1361                                cancellable,
1362                                callback,
1363                                user_data);
1364 }
1365
1366 /**
1367  * g_file_query_info_finish:
1368  * @file: input #GFile
1369  * @res: a #GAsyncResult
1370  * @error: a #GError
1371  *
1372  * Finishes an asynchronous file info query.
1373  * See g_file_query_info_async().
1374  *
1375  * Returns: (transfer full): #GFileInfo for given @file
1376  *     or %NULL on error. Free the returned object with
1377  *     g_object_unref().
1378  */
1379 GFileInfo *
1380 g_file_query_info_finish (GFile         *file,
1381                           GAsyncResult  *res,
1382                           GError       **error)
1383 {
1384   GFileIface *iface;
1385
1386   g_return_val_if_fail (G_IS_FILE (file), NULL);
1387   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1388
1389   if (g_async_result_legacy_propagate_error (res, error))
1390     return NULL;
1391
1392   iface = G_FILE_GET_IFACE (file);
1393   return (* iface->query_info_finish) (file, res, error);
1394 }
1395
1396 /**
1397  * g_file_query_filesystem_info:
1398  * @file: input #GFile
1399  * @attributes:  an attribute query string
1400  * @cancellable: (nullable): optional #GCancellable object,
1401  *     %NULL to ignore
1402  * @error: a #GError
1403  *
1404  * Similar to g_file_query_info(), but obtains information
1405  * about the filesystem the @file is on, rather than the file itself.
1406  * For instance the amount of space available and the type of
1407  * the filesystem.
1408  *
1409  * The @attributes value is a string that specifies the attributes
1410  * that should be gathered. It is not an error if it's not possible
1411  * to read a particular requested attribute from a file - it just
1412  * won't be set. @attributes should be a comma-separated list of
1413  * attributes or attribute wildcards. The wildcard "*" means all
1414  * attributes, and a wildcard like "filesystem::*" means all attributes
1415  * in the filesystem namespace. The standard namespace for filesystem
1416  * attributes is "filesystem". Common attributes of interest are
1417  * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1418  * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1419  * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1420  *
1421  * If @cancellable is not %NULL, then the operation can be cancelled
1422  * by triggering the cancellable object from another thread. If the
1423  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1424  * returned.
1425  *
1426  * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1427  * be returned. Other errors are possible too, and depend on what
1428  * kind of filesystem the file is on.
1429  *
1430  * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1431  *     Free the returned object with g_object_unref().
1432  */
1433 GFileInfo *
1434 g_file_query_filesystem_info (GFile         *file,
1435                               const char    *attributes,
1436                               GCancellable  *cancellable,
1437                               GError       **error)
1438 {
1439   GFileIface *iface;
1440
1441   g_return_val_if_fail (G_IS_FILE (file), NULL);
1442
1443   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1444     return NULL;
1445
1446   iface = G_FILE_GET_IFACE (file);
1447
1448   if (iface->query_filesystem_info == NULL)
1449     {
1450       g_set_error_literal (error, G_IO_ERROR,
1451                            G_IO_ERROR_NOT_SUPPORTED,
1452                            _("Operation not supported"));
1453       return NULL;
1454     }
1455
1456   return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1457 }
1458
1459 /**
1460  * g_file_query_filesystem_info_async:
1461  * @file: input #GFile
1462  * @attributes: an attribute query string
1463  * @io_priority: the [I/O priority][io-priority] of the request
1464  * @cancellable: (nullable): optional #GCancellable object,
1465  *     %NULL to ignore
1466  * @callback: (scope async): a #GAsyncReadyCallback to call
1467  *     when the request is satisfied
1468  * @user_data: (closure): the data to pass to callback function
1469  *
1470  * Asynchronously gets the requested information about the filesystem
1471  * that the specified @file is on. The result is a #GFileInfo object
1472  * that contains key-value attributes (such as type or size for the
1473  * file).
1474  *
1475  * For more details, see g_file_query_filesystem_info() which is the
1476  * synchronous version of this call.
1477  *
1478  * When the operation is finished, @callback will be called. You can
1479  * then call g_file_query_info_finish() to get the result of the
1480  * operation.
1481  */
1482 void
1483 g_file_query_filesystem_info_async (GFile               *file,
1484                                     const char          *attributes,
1485                                     int                  io_priority,
1486                                     GCancellable        *cancellable,
1487                                     GAsyncReadyCallback  callback,
1488                                     gpointer             user_data)
1489 {
1490   GFileIface *iface;
1491
1492   g_return_if_fail (G_IS_FILE (file));
1493
1494   iface = G_FILE_GET_IFACE (file);
1495   (* iface->query_filesystem_info_async) (file,
1496                                           attributes,
1497                                           io_priority,
1498                                           cancellable,
1499                                           callback,
1500                                           user_data);
1501 }
1502
1503 /**
1504  * g_file_query_filesystem_info_finish:
1505  * @file: input #GFile
1506  * @res: a #GAsyncResult
1507  * @error: a #GError
1508  *
1509  * Finishes an asynchronous filesystem info query.
1510  * See g_file_query_filesystem_info_async().
1511  *
1512  * Returns: (transfer full): #GFileInfo for given @file
1513  *     or %NULL on error.
1514  *     Free the returned object with g_object_unref().
1515  */
1516 GFileInfo *
1517 g_file_query_filesystem_info_finish (GFile         *file,
1518                                      GAsyncResult  *res,
1519                                      GError       **error)
1520 {
1521   GFileIface *iface;
1522
1523   g_return_val_if_fail (G_IS_FILE (file), NULL);
1524   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1525
1526   if (g_async_result_legacy_propagate_error (res, error))
1527     return NULL;
1528
1529   iface = G_FILE_GET_IFACE (file);
1530   return (* iface->query_filesystem_info_finish) (file, res, error);
1531 }
1532
1533 /**
1534  * g_file_find_enclosing_mount:
1535  * @file: input #GFile
1536  * @cancellable: (nullable): optional #GCancellable object,
1537  *     %NULL to ignore
1538  * @error: a #GError
1539  *
1540  * Gets a #GMount for the #GFile.
1541  *
1542  * #GMount is returned only for user interesting locations, see
1543  * #GVolumeMonitor. If the #GFileIface for @file does not have a #mount,
1544  * @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL #will be returned.
1545  *
1546  * If @cancellable is not %NULL, then the operation can be cancelled by
1547  * triggering the cancellable object from another thread. If the operation
1548  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1549  *
1550  * Returns: (transfer full): a #GMount where the @file is located
1551  *     or %NULL on error.
1552  *     Free the returned object with g_object_unref().
1553  */
1554 GMount *
1555 g_file_find_enclosing_mount (GFile         *file,
1556                              GCancellable  *cancellable,
1557                              GError       **error)
1558 {
1559   GFileIface *iface;
1560
1561   g_return_val_if_fail (G_IS_FILE (file), NULL);
1562
1563   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1564     return NULL;
1565
1566   iface = G_FILE_GET_IFACE (file);
1567   if (iface->find_enclosing_mount == NULL)
1568     {
1569
1570       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1571                            /* Translators: This is an error message when
1572                             * trying to find the enclosing (user visible)
1573                             * mount of a file, but none exists.
1574                             */
1575                            _("Containing mount does not exist"));
1576       return NULL;
1577     }
1578
1579   return (* iface->find_enclosing_mount) (file, cancellable, error);
1580 }
1581
1582 /**
1583  * g_file_find_enclosing_mount_async:
1584  * @file: a #GFile
1585  * @io_priority: the [I/O priority][io-priority] of the request
1586  * @cancellable: (nullable): optional #GCancellable object,
1587  *     %NULL to ignore
1588  * @callback: (scope async): a #GAsyncReadyCallback to call
1589  *     when the request is satisfied
1590  * @user_data: (closure): the data to pass to callback function
1591  *
1592  * Asynchronously gets the mount for the file.
1593  *
1594  * For more details, see g_file_find_enclosing_mount() which is
1595  * the synchronous version of this call.
1596  *
1597  * When the operation is finished, @callback will be called.
1598  * You can then call g_file_find_enclosing_mount_finish() to
1599  * get the result of the operation.
1600  */
1601 void
1602 g_file_find_enclosing_mount_async (GFile              *file,
1603                                    int                   io_priority,
1604                                    GCancellable         *cancellable,
1605                                    GAsyncReadyCallback   callback,
1606                                    gpointer              user_data)
1607 {
1608   GFileIface *iface;
1609
1610   g_return_if_fail (G_IS_FILE (file));
1611
1612   iface = G_FILE_GET_IFACE (file);
1613   (* iface->find_enclosing_mount_async) (file,
1614                                          io_priority,
1615                                          cancellable,
1616                                          callback,
1617                                          user_data);
1618 }
1619
1620 /**
1621  * g_file_find_enclosing_mount_finish:
1622  * @file: a #GFile
1623  * @res: a #GAsyncResult
1624  * @error: a #GError
1625  *
1626  * Finishes an asynchronous find mount request.
1627  * See g_file_find_enclosing_mount_async().
1628  *
1629  * Returns: (transfer full): #GMount for given @file or %NULL on error.
1630  *     Free the returned object with g_object_unref().
1631  */
1632 GMount *
1633 g_file_find_enclosing_mount_finish (GFile         *file,
1634                                     GAsyncResult  *res,
1635                                     GError       **error)
1636 {
1637   GFileIface *iface;
1638
1639   g_return_val_if_fail (G_IS_FILE (file), NULL);
1640   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1641
1642   if (g_async_result_legacy_propagate_error (res, error))
1643     return NULL;
1644
1645   iface = G_FILE_GET_IFACE (file);
1646   return (* iface->find_enclosing_mount_finish) (file, res, error);
1647 }
1648
1649
1650 /**
1651  * g_file_read:
1652  * @file: #GFile to read
1653  * @cancellable: (nullable): a #GCancellable
1654  * @error: a #GError, or %NULL
1655  *
1656  * Opens a file for reading. The result is a #GFileInputStream that
1657  * can be used to read the contents of the file.
1658  *
1659  * If @cancellable is not %NULL, then the operation can be cancelled by
1660  * triggering the cancellable object from another thread. If the operation
1661  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1662  *
1663  * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1664  * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1665  * error will be returned. Other errors are possible too, and depend
1666  * on what kind of filesystem the file is on.
1667  *
1668  * Virtual: read_fn
1669  * Returns: (transfer full): #GFileInputStream or %NULL on error.
1670  *     Free the returned object with g_object_unref().
1671  */
1672 GFileInputStream *
1673 g_file_read (GFile         *file,
1674              GCancellable  *cancellable,
1675              GError       **error)
1676 {
1677   GFileIface *iface;
1678
1679   g_return_val_if_fail (G_IS_FILE (file), NULL);
1680
1681   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1682     return NULL;
1683
1684   iface = G_FILE_GET_IFACE (file);
1685
1686   if (iface->read_fn == NULL)
1687     {
1688       g_set_error_literal (error, G_IO_ERROR,
1689                            G_IO_ERROR_NOT_SUPPORTED,
1690                            _("Operation not supported"));
1691       return NULL;
1692     }
1693
1694   return (* iface->read_fn) (file, cancellable, error);
1695 }
1696
1697 /**
1698  * g_file_append_to:
1699  * @file: input #GFile
1700  * @flags: a set of #GFileCreateFlags
1701  * @cancellable: (nullable): optional #GCancellable object,
1702  *     %NULL to ignore
1703  * @error: a #GError, or %NULL
1704  *
1705  * Gets an output stream for appending data to the file.
1706  * If the file doesn't already exist it is created.
1707  *
1708  * By default files created are generally readable by everyone,
1709  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1710  * will be made readable only to the current user, to the level that
1711  * is supported on the target filesystem.
1712  *
1713  * If @cancellable is not %NULL, then the operation can be cancelled
1714  * by triggering the cancellable object from another thread. If the
1715  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1716  * returned.
1717  *
1718  * Some file systems don't allow all file names, and may return an
1719  * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1720  * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1721  * possible too, and depend on what kind of filesystem the file is on.
1722  *
1723  * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1724  *     Free the returned object with g_object_unref().
1725  */
1726 GFileOutputStream *
1727 g_file_append_to (GFile             *file,
1728                   GFileCreateFlags   flags,
1729                   GCancellable      *cancellable,
1730                   GError           **error)
1731 {
1732   GFileIface *iface;
1733
1734   g_return_val_if_fail (G_IS_FILE (file), NULL);
1735
1736   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1737     return NULL;
1738
1739   iface = G_FILE_GET_IFACE (file);
1740
1741   if (iface->append_to == NULL)
1742     {
1743       g_set_error_literal (error, G_IO_ERROR,
1744                            G_IO_ERROR_NOT_SUPPORTED,
1745                            _("Operation not supported"));
1746       return NULL;
1747     }
1748
1749   return (* iface->append_to) (file, flags, cancellable, error);
1750 }
1751
1752 /**
1753  * g_file_create:
1754  * @file: input #GFile
1755  * @flags: a set of #GFileCreateFlags
1756  * @cancellable: (nullable): optional #GCancellable object,
1757  *     %NULL to ignore
1758  * @error: a #GError, or %NULL
1759  *
1760  * Creates a new file and returns an output stream for writing to it.
1761  * The file must not already exist.
1762  *
1763  * By default files created are generally readable by everyone,
1764  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1765  * will be made readable only to the current user, to the level
1766  * that is supported on the target filesystem.
1767  *
1768  * If @cancellable is not %NULL, then the operation can be cancelled
1769  * by triggering the cancellable object from another thread. If the
1770  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1771  * returned.
1772  *
1773  * If a file or directory with this name already exists the
1774  * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1775  * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1776  * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1777  * be returned. Other errors are possible too, and depend on what kind
1778  * of filesystem the file is on.
1779  *
1780  * Returns: (transfer full): a #GFileOutputStream for the newly created
1781  *     file, or %NULL on error.
1782  *     Free the returned object with g_object_unref().
1783  */
1784 GFileOutputStream *
1785 g_file_create (GFile             *file,
1786                GFileCreateFlags   flags,
1787                GCancellable      *cancellable,
1788                GError           **error)
1789 {
1790   GFileIface *iface;
1791
1792   g_return_val_if_fail (G_IS_FILE (file), NULL);
1793
1794   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1795     return NULL;
1796
1797   iface = G_FILE_GET_IFACE (file);
1798
1799   if (iface->create == NULL)
1800     {
1801       g_set_error_literal (error, G_IO_ERROR,
1802                            G_IO_ERROR_NOT_SUPPORTED,
1803                            _("Operation not supported"));
1804       return NULL;
1805     }
1806
1807   return (* iface->create) (file, flags, cancellable, error);
1808 }
1809
1810 /**
1811  * g_file_replace:
1812  * @file: input #GFile
1813  * @etag: (nullable): an optional [entity tag][gfile-etag]
1814  *     for the current #GFile, or #NULL to ignore
1815  * @make_backup: %TRUE if a backup should be created
1816  * @flags: a set of #GFileCreateFlags
1817  * @cancellable: (nullable): optional #GCancellable object,
1818  *     %NULL to ignore
1819  * @error: a #GError, or %NULL
1820  *
1821  * Returns an output stream for overwriting the file, possibly
1822  * creating a backup copy of the file first. If the file doesn't exist,
1823  * it will be created.
1824  *
1825  * This will try to replace the file in the safest way possible so
1826  * that any errors during the writing will not affect an already
1827  * existing copy of the file. For instance, for local files it
1828  * may write to a temporary file and then atomically rename over
1829  * the destination when the stream is closed.
1830  *
1831  * By default files created are generally readable by everyone,
1832  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1833  * will be made readable only to the current user, to the level that
1834  * is supported on the target filesystem.
1835  *
1836  * If @cancellable is not %NULL, then the operation can be cancelled
1837  * by triggering the cancellable object from another thread. If the
1838  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1839  * returned.
1840  *
1841  * If you pass in a non-%NULL @etag value and @file already exists, then
1842  * this value is compared to the current entity tag of the file, and if
1843  * they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
1844  * generally means that the file has been changed since you last read
1845  * it. You can get the new etag from g_file_output_stream_get_etag()
1846  * after you've finished writing and closed the #GFileOutputStream. When
1847  * you load a new file you can use g_file_input_stream_query_info() to
1848  * get the etag of the file.
1849  *
1850  * If @make_backup is %TRUE, this function will attempt to make a
1851  * backup of the current file before overwriting it. If this fails
1852  * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1853  * want to replace anyway, try again with @make_backup set to %FALSE.
1854  *
1855  * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1856  * be returned, and if the file is some other form of non-regular file
1857  * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1858  * file systems don't allow all file names, and may return an
1859  * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1860  * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1861  * possible too, and depend on what kind of filesystem the file is on.
1862  *
1863  * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1864  *     Free the returned object with g_object_unref().
1865  */
1866 GFileOutputStream *
1867 g_file_replace (GFile             *file,
1868                 const char        *etag,
1869                 gboolean           make_backup,
1870                 GFileCreateFlags   flags,
1871                 GCancellable      *cancellable,
1872                 GError           **error)
1873 {
1874   GFileIface *iface;
1875
1876   g_return_val_if_fail (G_IS_FILE (file), NULL);
1877
1878   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1879     return NULL;
1880
1881   iface = G_FILE_GET_IFACE (file);
1882
1883   if (iface->replace == NULL)
1884     {
1885       g_set_error_literal (error, G_IO_ERROR,
1886                            G_IO_ERROR_NOT_SUPPORTED,
1887                            _("Operation not supported"));
1888       return NULL;
1889     }
1890
1891   /* Handle empty tag string as NULL in consistent way. */
1892   if (etag && *etag == 0)
1893     etag = NULL;
1894
1895   return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1896 }
1897
1898 /**
1899  * g_file_open_readwrite:
1900  * @file: #GFile to open
1901  * @cancellable: (nullable): a #GCancellable
1902  * @error: a #GError, or %NULL
1903  *
1904  * Opens an existing file for reading and writing. The result is
1905  * a #GFileIOStream that can be used to read and write the contents
1906  * of the file.
1907  *
1908  * If @cancellable is not %NULL, then the operation can be cancelled
1909  * by triggering the cancellable object from another thread. If the
1910  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1911  * returned.
1912  *
1913  * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1914  * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1915  * error will be returned. Other errors are possible too, and depend on
1916  * what kind of filesystem the file is on. Note that in many non-local
1917  * file cases read and write streams are not supported, so make sure you
1918  * really need to do read and write streaming, rather than just opening
1919  * for reading or writing.
1920  *
1921  * Returns: (transfer full): #GFileIOStream or %NULL on error.
1922  *     Free the returned object with g_object_unref().
1923  *
1924  * Since: 2.22
1925  */
1926 GFileIOStream *
1927 g_file_open_readwrite (GFile         *file,
1928                        GCancellable  *cancellable,
1929                        GError       **error)
1930 {
1931   GFileIface *iface;
1932
1933   g_return_val_if_fail (G_IS_FILE (file), NULL);
1934
1935   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1936     return NULL;
1937
1938   iface = G_FILE_GET_IFACE (file);
1939
1940   if (iface->open_readwrite == NULL)
1941     {
1942       g_set_error_literal (error, G_IO_ERROR,
1943                            G_IO_ERROR_NOT_SUPPORTED,
1944                            _("Operation not supported"));
1945       return NULL;
1946     }
1947
1948   return (* iface->open_readwrite) (file, cancellable, error);
1949 }
1950
1951 /**
1952  * g_file_create_readwrite:
1953  * @file: a #GFile
1954  * @flags: a set of #GFileCreateFlags
1955  * @cancellable: (nullable): optional #GCancellable object,
1956  *     %NULL to ignore
1957  * @error: return location for a #GError, or %NULL
1958  *
1959  * Creates a new file and returns a stream for reading and
1960  * writing to it. The file must not already exist.
1961  *
1962  * By default files created are generally readable by everyone,
1963  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1964  * will be made readable only to the current user, to the level
1965  * that is supported on the target filesystem.
1966  *
1967  * If @cancellable is not %NULL, then the operation can be cancelled
1968  * by triggering the cancellable object from another thread. If the
1969  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1970  * returned.
1971  *
1972  * If a file or directory with this name already exists, the
1973  * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1974  * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1975  * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1976  * will be returned. Other errors are possible too, and depend on what
1977  * kind of filesystem the file is on.
1978  *
1979  * Note that in many non-local file cases read and write streams are
1980  * not supported, so make sure you really need to do read and write
1981  * streaming, rather than just opening for reading or writing.
1982  *
1983  * Returns: (transfer full): a #GFileIOStream for the newly created
1984  *     file, or %NULL on error.
1985  *     Free the returned object with g_object_unref().
1986  *
1987  * Since: 2.22
1988  */
1989 GFileIOStream *
1990 g_file_create_readwrite (GFile             *file,
1991                          GFileCreateFlags   flags,
1992                          GCancellable      *cancellable,
1993                          GError           **error)
1994 {
1995   GFileIface *iface;
1996
1997   g_return_val_if_fail (G_IS_FILE (file), NULL);
1998
1999   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2000     return NULL;
2001
2002   iface = G_FILE_GET_IFACE (file);
2003
2004   if (iface->create_readwrite == NULL)
2005     {
2006       g_set_error_literal (error, G_IO_ERROR,
2007                            G_IO_ERROR_NOT_SUPPORTED,
2008                            _("Operation not supported"));
2009       return NULL;
2010     }
2011
2012   return (* iface->create_readwrite) (file, flags, cancellable, error);
2013 }
2014
2015 /**
2016  * g_file_replace_readwrite:
2017  * @file: a #GFile
2018  * @etag: (nullable): an optional [entity tag][gfile-etag]
2019  *     for the current #GFile, or #NULL to ignore
2020  * @make_backup: %TRUE if a backup should be created
2021  * @flags: a set of #GFileCreateFlags
2022  * @cancellable: (nullable): optional #GCancellable object,
2023  *     %NULL to ignore
2024  * @error: return location for a #GError, or %NULL
2025  *
2026  * Returns an output stream for overwriting the file in readwrite mode,
2027  * possibly creating a backup copy of the file first. If the file doesn't
2028  * exist, it will be created.
2029  *
2030  * For details about the behaviour, see g_file_replace() which does the
2031  * same thing but returns an output stream only.
2032  *
2033  * Note that in many non-local file cases read and write streams are not
2034  * supported, so make sure you really need to do read and write streaming,
2035  * rather than just opening for reading or writing.
2036  *
2037  * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2038  *     Free the returned object with g_object_unref().
2039  *
2040  * Since: 2.22
2041  */
2042 GFileIOStream *
2043 g_file_replace_readwrite (GFile             *file,
2044                           const char        *etag,
2045                           gboolean           make_backup,
2046                           GFileCreateFlags   flags,
2047                           GCancellable      *cancellable,
2048                           GError           **error)
2049 {
2050   GFileIface *iface;
2051
2052   g_return_val_if_fail (G_IS_FILE (file), NULL);
2053
2054   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2055     return NULL;
2056
2057   iface = G_FILE_GET_IFACE (file);
2058
2059   if (iface->replace_readwrite == NULL)
2060     {
2061       g_set_error_literal (error, G_IO_ERROR,
2062                            G_IO_ERROR_NOT_SUPPORTED,
2063                            _("Operation not supported"));
2064       return NULL;
2065     }
2066
2067   return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
2068 }
2069
2070 /**
2071  * g_file_read_async:
2072  * @file: input #GFile
2073  * @io_priority: the [I/O priority][io-priority] of the request
2074  * @cancellable: (nullable): optional #GCancellable object,
2075  *     %NULL to ignore
2076  * @callback: (scope async): a #GAsyncReadyCallback to call
2077  *     when the request is satisfied
2078  * @user_data: (closure): the data to pass to callback function
2079  *
2080  * Asynchronously opens @file for reading.
2081  *
2082  * For more details, see g_file_read() which is
2083  * the synchronous version of this call.
2084  *
2085  * When the operation is finished, @callback will be called.
2086  * You can then call g_file_read_finish() to get the result
2087  * of the operation.
2088  */
2089 void
2090 g_file_read_async (GFile               *file,
2091                    int                  io_priority,
2092                    GCancellable        *cancellable,
2093                    GAsyncReadyCallback  callback,
2094                    gpointer             user_data)
2095 {
2096   GFileIface *iface;
2097
2098   g_return_if_fail (G_IS_FILE (file));
2099
2100   iface = G_FILE_GET_IFACE (file);
2101   (* iface->read_async) (file,
2102                          io_priority,
2103                          cancellable,
2104                          callback,
2105                          user_data);
2106 }
2107
2108 /**
2109  * g_file_read_finish:
2110  * @file: input #GFile
2111  * @res: a #GAsyncResult
2112  * @error: a #GError, or %NULL
2113  *
2114  * Finishes an asynchronous file read operation started with
2115  * g_file_read_async().
2116  *
2117  * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2118  *     Free the returned object with g_object_unref().
2119  */
2120 GFileInputStream *
2121 g_file_read_finish (GFile         *file,
2122                     GAsyncResult  *res,
2123                     GError       **error)
2124 {
2125   GFileIface *iface;
2126
2127   g_return_val_if_fail (G_IS_FILE (file), NULL);
2128   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2129
2130   if (g_async_result_legacy_propagate_error (res, error))
2131     return NULL;
2132
2133   iface = G_FILE_GET_IFACE (file);
2134   return (* iface->read_finish) (file, res, error);
2135 }
2136
2137 /**
2138  * g_file_append_to_async:
2139  * @file: input #GFile
2140  * @flags: a set of #GFileCreateFlags
2141  * @io_priority: the [I/O priority][io-priority] of the request
2142  * @cancellable: (nullable): optional #GCancellable object,
2143  *     %NULL to ignore
2144  * @callback: (scope async): a #GAsyncReadyCallback to call
2145  *     when the request is satisfied
2146  * @user_data: (closure): the data to pass to callback function
2147  *
2148  * Asynchronously opens @file for appending.
2149  *
2150  * For more details, see g_file_append_to() which is
2151  * the synchronous version of this call.
2152  *
2153  * When the operation is finished, @callback will be called.
2154  * You can then call g_file_append_to_finish() to get the result
2155  * of the operation.
2156  */
2157 void
2158 g_file_append_to_async (GFile               *file,
2159                         GFileCreateFlags     flags,
2160                         int                  io_priority,
2161                         GCancellable        *cancellable,
2162                         GAsyncReadyCallback  callback,
2163                         gpointer             user_data)
2164 {
2165   GFileIface *iface;
2166
2167   g_return_if_fail (G_IS_FILE (file));
2168
2169   iface = G_FILE_GET_IFACE (file);
2170   (* iface->append_to_async) (file,
2171                               flags,
2172                               io_priority,
2173                               cancellable,
2174                               callback,
2175                               user_data);
2176 }
2177
2178 /**
2179  * g_file_append_to_finish:
2180  * @file: input #GFile
2181  * @res: #GAsyncResult
2182  * @error: a #GError, or %NULL
2183  *
2184  * Finishes an asynchronous file append operation started with
2185  * g_file_append_to_async().
2186  *
2187  * Returns: (transfer full): a valid #GFileOutputStream
2188  *     or %NULL on error.
2189  *     Free the returned object with g_object_unref().
2190  */
2191 GFileOutputStream *
2192 g_file_append_to_finish (GFile         *file,
2193                          GAsyncResult  *res,
2194                          GError       **error)
2195 {
2196   GFileIface *iface;
2197
2198   g_return_val_if_fail (G_IS_FILE (file), NULL);
2199   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2200
2201   if (g_async_result_legacy_propagate_error (res, error))
2202     return NULL;
2203
2204   iface = G_FILE_GET_IFACE (file);
2205   return (* iface->append_to_finish) (file, res, error);
2206 }
2207
2208 /**
2209  * g_file_create_async:
2210  * @file: input #GFile
2211  * @flags: a set of #GFileCreateFlags
2212  * @io_priority: the [I/O priority][io-priority] of the request
2213  * @cancellable: (nullable): optional #GCancellable object,
2214  *     %NULL to ignore
2215  * @callback: (scope async): a #GAsyncReadyCallback to call
2216  *     when the request is satisfied
2217  * @user_data: (closure): the data to pass to callback function
2218  *
2219  * Asynchronously creates a new file and returns an output stream
2220  * for writing to it. The file must not already exist.
2221  *
2222  * For more details, see g_file_create() which is
2223  * the synchronous version of this call.
2224  *
2225  * When the operation is finished, @callback will be called.
2226  * You can then call g_file_create_finish() to get the result
2227  * of the operation.
2228  */
2229 void
2230 g_file_create_async (GFile               *file,
2231                      GFileCreateFlags     flags,
2232                      int                  io_priority,
2233                      GCancellable        *cancellable,
2234                      GAsyncReadyCallback  callback,
2235                      gpointer             user_data)
2236 {
2237   GFileIface *iface;
2238
2239   g_return_if_fail (G_IS_FILE (file));
2240
2241   iface = G_FILE_GET_IFACE (file);
2242   (* iface->create_async) (file,
2243                            flags,
2244                            io_priority,
2245                            cancellable,
2246                            callback,
2247                            user_data);
2248 }
2249
2250 /**
2251  * g_file_create_finish:
2252  * @file: input #GFile
2253  * @res: a #GAsyncResult
2254  * @error: a #GError, or %NULL
2255  *
2256  * Finishes an asynchronous file create operation started with
2257  * g_file_create_async().
2258  *
2259  * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2260  *     Free the returned object with g_object_unref().
2261  */
2262 GFileOutputStream *
2263 g_file_create_finish (GFile         *file,
2264                       GAsyncResult  *res,
2265                       GError       **error)
2266 {
2267   GFileIface *iface;
2268
2269   g_return_val_if_fail (G_IS_FILE (file), NULL);
2270   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2271
2272   if (g_async_result_legacy_propagate_error (res, error))
2273     return NULL;
2274
2275   iface = G_FILE_GET_IFACE (file);
2276   return (* iface->create_finish) (file, res, error);
2277 }
2278
2279 /**
2280  * g_file_replace_async:
2281  * @file: input #GFile
2282  * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2283  *     or %NULL to ignore
2284  * @make_backup: %TRUE if a backup should be created
2285  * @flags: a set of #GFileCreateFlags
2286  * @io_priority: the [I/O priority][io-priority] of the request
2287  * @cancellable: (nullable): optional #GCancellable object,
2288  *     %NULL to ignore
2289  * @callback: (scope async): a #GAsyncReadyCallback to call
2290  *     when the request is satisfied
2291  * @user_data: (closure): the data to pass to callback function
2292  *
2293  * Asynchronously overwrites the file, replacing the contents,
2294  * possibly creating a backup copy of the file first.
2295  *
2296  * For more details, see g_file_replace() which is
2297  * the synchronous version of this call.
2298  *
2299  * When the operation is finished, @callback will be called.
2300  * You can then call g_file_replace_finish() to get the result
2301  * of the operation.
2302  */
2303 void
2304 g_file_replace_async (GFile               *file,
2305                       const char          *etag,
2306                       gboolean             make_backup,
2307                       GFileCreateFlags     flags,
2308                       int                  io_priority,
2309                       GCancellable        *cancellable,
2310                       GAsyncReadyCallback  callback,
2311                       gpointer             user_data)
2312 {
2313   GFileIface *iface;
2314
2315   g_return_if_fail (G_IS_FILE (file));
2316
2317   iface = G_FILE_GET_IFACE (file);
2318   (* iface->replace_async) (file,
2319                             etag,
2320                             make_backup,
2321                             flags,
2322                             io_priority,
2323                             cancellable,
2324                             callback,
2325                             user_data);
2326 }
2327
2328 /**
2329  * g_file_replace_finish:
2330  * @file: input #GFile
2331  * @res: a #GAsyncResult
2332  * @error: a #GError, or %NULL
2333  *
2334  * Finishes an asynchronous file replace operation started with
2335  * g_file_replace_async().
2336  *
2337  * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2338  *     Free the returned object with g_object_unref().
2339  */
2340 GFileOutputStream *
2341 g_file_replace_finish (GFile         *file,
2342                        GAsyncResult  *res,
2343                        GError       **error)
2344 {
2345   GFileIface *iface;
2346
2347   g_return_val_if_fail (G_IS_FILE (file), NULL);
2348   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2349
2350   if (g_async_result_legacy_propagate_error (res, error))
2351     return NULL;
2352
2353   iface = G_FILE_GET_IFACE (file);
2354   return (* iface->replace_finish) (file, res, error);
2355 }
2356
2357 /**
2358  * g_file_open_readwrite_async
2359  * @file: input #GFile
2360  * @io_priority: the [I/O priority][io-priority] of the request
2361  * @cancellable: (nullable): optional #GCancellable object,
2362  *     %NULL to ignore
2363  * @callback: (scope async): a #GAsyncReadyCallback to call
2364  *     when the request is satisfied
2365  * @user_data: (closure): the data to pass to callback function
2366  *
2367  * Asynchronously opens @file for reading and writing.
2368  *
2369  * For more details, see g_file_open_readwrite() which is
2370  * the synchronous version of this call.
2371  *
2372  * When the operation is finished, @callback will be called.
2373  * You can then call g_file_open_readwrite_finish() to get
2374  * the result of the operation.
2375  *
2376  * Since: 2.22
2377  */
2378 void
2379 g_file_open_readwrite_async (GFile               *file,
2380                              int                  io_priority,
2381                              GCancellable        *cancellable,
2382                              GAsyncReadyCallback  callback,
2383                              gpointer             user_data)
2384 {
2385   GFileIface *iface;
2386
2387   g_return_if_fail (G_IS_FILE (file));
2388
2389   iface = G_FILE_GET_IFACE (file);
2390   (* iface->open_readwrite_async) (file,
2391                                    io_priority,
2392                                    cancellable,
2393                                    callback,
2394                                    user_data);
2395 }
2396
2397 /**
2398  * g_file_open_readwrite_finish:
2399  * @file: input #GFile
2400  * @res: a #GAsyncResult
2401  * @error: a #GError, or %NULL
2402  *
2403  * Finishes an asynchronous file read operation started with
2404  * g_file_open_readwrite_async().
2405  *
2406  * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2407  *     Free the returned object with g_object_unref().
2408  *
2409  * Since: 2.22
2410  */
2411 GFileIOStream *
2412 g_file_open_readwrite_finish (GFile         *file,
2413                               GAsyncResult  *res,
2414                               GError       **error)
2415 {
2416   GFileIface *iface;
2417
2418   g_return_val_if_fail (G_IS_FILE (file), NULL);
2419   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2420
2421   if (g_async_result_legacy_propagate_error (res, error))
2422     return NULL;
2423
2424   iface = G_FILE_GET_IFACE (file);
2425   return (* iface->open_readwrite_finish) (file, res, error);
2426 }
2427
2428 /**
2429  * g_file_create_readwrite_async:
2430  * @file: input #GFile
2431  * @flags: a set of #GFileCreateFlags
2432  * @io_priority: the [I/O priority][io-priority] of the request
2433  * @cancellable: (nullable): optional #GCancellable object,
2434  *     %NULL to ignore
2435  * @callback: (scope async): a #GAsyncReadyCallback to call
2436  *     when the request is satisfied
2437  * @user_data: (closure): the data to pass to callback function
2438  *
2439  * Asynchronously creates a new file and returns a stream
2440  * for reading and writing to it. The file must not already exist.
2441  *
2442  * For more details, see g_file_create_readwrite() which is
2443  * the synchronous version of this call.
2444  *
2445  * When the operation is finished, @callback will be called.
2446  * You can then call g_file_create_readwrite_finish() to get
2447  * the result of the operation.
2448  *
2449  * Since: 2.22
2450  */
2451 void
2452 g_file_create_readwrite_async (GFile               *file,
2453                                GFileCreateFlags     flags,
2454                                int                  io_priority,
2455                                GCancellable        *cancellable,
2456                                GAsyncReadyCallback  callback,
2457                                gpointer             user_data)
2458 {
2459   GFileIface *iface;
2460
2461   g_return_if_fail (G_IS_FILE (file));
2462
2463   iface = G_FILE_GET_IFACE (file);
2464   (* iface->create_readwrite_async) (file,
2465                                      flags,
2466                                      io_priority,
2467                                      cancellable,
2468                                      callback,
2469                                      user_data);
2470 }
2471
2472 /**
2473  * g_file_create_readwrite_finish:
2474  * @file: input #GFile
2475  * @res: a #GAsyncResult
2476  * @error: a #GError, or %NULL
2477  *
2478  * Finishes an asynchronous file create operation started with
2479  * g_file_create_readwrite_async().
2480  *
2481  * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2482  *     Free the returned object with g_object_unref().
2483  *
2484  * Since: 2.22
2485  */
2486 GFileIOStream *
2487 g_file_create_readwrite_finish (GFile         *file,
2488                                 GAsyncResult  *res,
2489                                 GError       **error)
2490 {
2491   GFileIface *iface;
2492
2493   g_return_val_if_fail (G_IS_FILE (file), NULL);
2494   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2495
2496   if (g_async_result_legacy_propagate_error (res, error))
2497     return NULL;
2498
2499   iface = G_FILE_GET_IFACE (file);
2500   return (* iface->create_readwrite_finish) (file, res, error);
2501 }
2502
2503 /**
2504  * g_file_replace_readwrite_async:
2505  * @file: input #GFile
2506  * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2507  *     or %NULL to ignore
2508  * @make_backup: %TRUE if a backup should be created
2509  * @flags: a set of #GFileCreateFlags
2510  * @io_priority: the [I/O priority][io-priority] of the request
2511  * @cancellable: (nullable): optional #GCancellable object,
2512  *     %NULL to ignore
2513  * @callback: (scope async): a #GAsyncReadyCallback to call
2514  *     when the request is satisfied
2515  * @user_data: (closure): the data to pass to callback function
2516  *
2517  * Asynchronously overwrites the file in read-write mode,
2518  * replacing the contents, possibly creating a backup copy
2519  * of the file first.
2520  *
2521  * For more details, see g_file_replace_readwrite() which is
2522  * the synchronous version of this call.
2523  *
2524  * When the operation is finished, @callback will be called.
2525  * You can then call g_file_replace_readwrite_finish() to get
2526  * the result of the operation.
2527  *
2528  * Since: 2.22
2529  */
2530 void
2531 g_file_replace_readwrite_async (GFile               *file,
2532                                 const char          *etag,
2533                                 gboolean             make_backup,
2534                                 GFileCreateFlags     flags,
2535                                 int                  io_priority,
2536                                 GCancellable        *cancellable,
2537                                 GAsyncReadyCallback  callback,
2538                                 gpointer             user_data)
2539 {
2540   GFileIface *iface;
2541
2542   g_return_if_fail (G_IS_FILE (file));
2543
2544   iface = G_FILE_GET_IFACE (file);
2545   (* iface->replace_readwrite_async) (file,
2546                                       etag,
2547                                       make_backup,
2548                                       flags,
2549                                       io_priority,
2550                                       cancellable,
2551                                       callback,
2552                                       user_data);
2553 }
2554
2555 /**
2556  * g_file_replace_readwrite_finish:
2557  * @file: input #GFile
2558  * @res: a #GAsyncResult
2559  * @error: a #GError, or %NULL
2560  *
2561  * Finishes an asynchronous file replace operation started with
2562  * g_file_replace_readwrite_async().
2563  *
2564  * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2565  *     Free the returned object with g_object_unref().
2566  *
2567  * Since: 2.22
2568  */
2569 GFileIOStream *
2570 g_file_replace_readwrite_finish (GFile         *file,
2571                                  GAsyncResult  *res,
2572                                  GError       **error)
2573 {
2574   GFileIface *iface;
2575
2576   g_return_val_if_fail (G_IS_FILE (file), NULL);
2577   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2578
2579   if (g_async_result_legacy_propagate_error (res, error))
2580     return NULL;
2581
2582   iface = G_FILE_GET_IFACE (file);
2583   return (* iface->replace_readwrite_finish) (file, res, error);
2584 }
2585
2586 static gboolean
2587 copy_symlink (GFile           *destination,
2588               GFileCopyFlags   flags,
2589               GCancellable    *cancellable,
2590               const char      *target,
2591               GError         **error)
2592 {
2593   GError *my_error;
2594   gboolean tried_delete;
2595   GFileInfo *info;
2596   GFileType file_type;
2597
2598   tried_delete = FALSE;
2599
2600  retry:
2601   my_error = NULL;
2602   if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2603     {
2604       /* Maybe it already existed, and we want to overwrite? */
2605       if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2606           my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2607         {
2608           g_clear_error (&my_error);
2609
2610           /* Don't overwrite if the destination is a directory */
2611           info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2612                                     G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2613                                     cancellable, &my_error);
2614           if (info != NULL)
2615             {
2616               file_type = g_file_info_get_file_type (info);
2617               g_object_unref (info);
2618
2619               if (file_type == G_FILE_TYPE_DIRECTORY)
2620                 {
2621                   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2622                                        _("Can’t copy over directory"));
2623                   return FALSE;
2624                 }
2625             }
2626
2627           if (!g_file_delete (destination, cancellable, error))
2628             return FALSE;
2629
2630           tried_delete = TRUE;
2631           goto retry;
2632         }
2633             /* Nah, fail */
2634       g_propagate_error (error, my_error);
2635       return FALSE;
2636     }
2637
2638   return TRUE;
2639 }
2640
2641 static GFileInputStream *
2642 open_source_for_copy (GFile           *source,
2643                       GFile           *destination,
2644                       GFileCopyFlags   flags,
2645                       GCancellable    *cancellable,
2646                       GError         **error)
2647 {
2648   GError *my_error;
2649   GFileInputStream *ret;
2650   GFileInfo *info;
2651   GFileType file_type;
2652
2653   my_error = NULL;
2654   ret = g_file_read (source, cancellable, &my_error);
2655   if (ret != NULL)
2656     return ret;
2657
2658   /* There was an error opening the source, try to set a good error for it: */
2659   if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2660     {
2661       /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2662        * as that is less useful to the app. Better check for errors on the
2663        * target instead.
2664        */
2665       g_error_free (my_error);
2666       my_error = NULL;
2667
2668       info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2669                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2670                                 cancellable, &my_error);
2671       if (info != NULL &&
2672           g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2673         {
2674           file_type = g_file_info_get_file_type (info);
2675           g_object_unref (info);
2676
2677           if (flags & G_FILE_COPY_OVERWRITE)
2678             {
2679               if (file_type == G_FILE_TYPE_DIRECTORY)
2680                 {
2681                   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2682                                        _("Can’t copy directory over directory"));
2683                   return NULL;
2684                 }
2685               /* continue to would_recurse error */
2686             }
2687           else
2688             {
2689               g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2690                                    _("Target file exists"));
2691               return NULL;
2692             }
2693         }
2694       else
2695         {
2696           /* Error getting info from target, return that error
2697            * (except for NOT_FOUND, which is no error here)
2698            */
2699           g_clear_object (&info);
2700           if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2701             {
2702               g_propagate_error (error, my_error);
2703               return NULL;
2704             }
2705           g_clear_error (&my_error);
2706         }
2707
2708       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2709                            _("Can’t recursively copy directory"));
2710       return NULL;
2711     }
2712
2713   g_propagate_error (error, my_error);
2714   return NULL;
2715 }
2716
2717 static gboolean
2718 should_copy (GFileAttributeInfo *info,
2719              gboolean            copy_all_attributes,
2720              gboolean            skip_perms)
2721 {
2722   if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2723         return FALSE;
2724
2725   if (copy_all_attributes)
2726     return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2727   return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2728 }
2729
2730 static gboolean
2731 build_attribute_list_for_copy (GFile                  *file,
2732                                GFileCopyFlags          flags,
2733                                char                  **out_attributes,
2734                                GCancellable           *cancellable,
2735                                GError                **error)
2736 {
2737   gboolean ret = FALSE;
2738   GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2739   GString *s = NULL;
2740   gboolean first;
2741   int i;
2742   gboolean copy_all_attributes;
2743   gboolean skip_perms;
2744
2745   copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2746   skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2747
2748   /* Ignore errors here, if the target supports no attributes there is
2749    * nothing to copy.  We still honor the cancellable though.
2750    */
2751   attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2752   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2753     goto out;
2754
2755   namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2756   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2757     goto out;
2758
2759   if (attributes == NULL && namespaces == NULL)
2760     goto out;
2761
2762   first = TRUE;
2763   s = g_string_new ("");
2764
2765   if (attributes)
2766     {
2767       for (i = 0; i < attributes->n_infos; i++)
2768         {
2769           if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2770             {
2771               if (first)
2772                 first = FALSE;
2773               else
2774                 g_string_append_c (s, ',');
2775
2776               g_string_append (s, attributes->infos[i].name);
2777             }
2778         }
2779     }
2780
2781   if (namespaces)
2782     {
2783       for (i = 0; i < namespaces->n_infos; i++)
2784         {
2785           if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2786             {
2787               if (first)
2788                 first = FALSE;
2789               else
2790                 g_string_append_c (s, ',');
2791
2792               g_string_append (s, namespaces->infos[i].name);
2793               g_string_append (s, "::*");
2794             }
2795         }
2796     }
2797
2798   ret = TRUE;
2799   *out_attributes = g_string_free (s, FALSE);
2800   s = NULL;
2801  out:
2802   if (s)
2803     g_string_free (s, TRUE);
2804   if (attributes)
2805     g_file_attribute_info_list_unref (attributes);
2806   if (namespaces)
2807     g_file_attribute_info_list_unref (namespaces);
2808   
2809   return ret;
2810 }
2811
2812 /**
2813  * g_file_copy_attributes:
2814  * @source: a #GFile with attributes
2815  * @destination: a #GFile to copy attributes to
2816  * @flags: a set of #GFileCopyFlags
2817  * @cancellable: (nullable): optional #GCancellable object,
2818  *     %NULL to ignore
2819  * @error: a #GError, %NULL to ignore
2820  *
2821  * Copies the file attributes from @source to @destination.
2822  *
2823  * Normally only a subset of the file attributes are copied,
2824  * those that are copies in a normal file copy operation
2825  * (which for instance does not include e.g. owner). However
2826  * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2827  * all the metadata that is possible to copy is copied. This
2828  * is useful when implementing move by copy + delete source.
2829  *
2830  * Returns: %TRUE if the attributes were copied successfully,
2831  *     %FALSE otherwise.
2832  */
2833 gboolean
2834 g_file_copy_attributes (GFile           *source,
2835                         GFile           *destination,
2836                         GFileCopyFlags   flags,
2837                         GCancellable    *cancellable,
2838                         GError         **error)
2839 {
2840   char *attrs_to_read;
2841   gboolean res;
2842   GFileInfo *info;
2843   gboolean source_nofollow_symlinks;
2844
2845   if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2846                                       cancellable, error))
2847     return FALSE;
2848
2849   source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2850
2851   /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2852    * we just don't copy it.
2853    */
2854   info = g_file_query_info (source, attrs_to_read,
2855                             source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2856                             cancellable,
2857                             NULL);
2858
2859   g_free (attrs_to_read);
2860
2861   res = TRUE;
2862   if  (info)
2863     {
2864       res = g_file_set_attributes_from_info (destination,
2865                                              info,
2866                                              G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2867                                              cancellable,
2868                                              error);
2869       g_object_unref (info);
2870     }
2871
2872   return res;
2873 }
2874
2875 /* 256k minus malloc overhead */
2876 #define STREAM_BUFFER_SIZE (1024*256 - 2 *sizeof(gpointer))
2877
2878 static gboolean
2879 copy_stream_with_progress (GInputStream           *in,
2880                            GOutputStream          *out,
2881                            GFile                  *source,
2882                            GCancellable           *cancellable,
2883                            GFileProgressCallback   progress_callback,
2884                            gpointer                progress_callback_data,
2885                            GError                **error)
2886 {
2887   gssize n_read;
2888   gsize n_written;
2889   goffset current_size;
2890   char *buffer;
2891   gboolean res;
2892   goffset total_size;
2893   GFileInfo *info;
2894
2895   total_size = -1;
2896   /* avoid performance impact of querying total size when it's not needed */
2897   if (progress_callback)
2898     {
2899       info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2900                                              G_FILE_ATTRIBUTE_STANDARD_SIZE,
2901                                              cancellable, NULL);
2902       if (info)
2903         {
2904           if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2905             total_size = g_file_info_get_size (info);
2906           g_object_unref (info);
2907         }
2908
2909       if (total_size == -1)
2910         {
2911           info = g_file_query_info (source,
2912                                     G_FILE_ATTRIBUTE_STANDARD_SIZE,
2913                                     G_FILE_QUERY_INFO_NONE,
2914                                     cancellable, NULL);
2915           if (info)
2916             {
2917               if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2918                 total_size = g_file_info_get_size (info);
2919               g_object_unref (info);
2920             }
2921         }
2922     }
2923
2924   if (total_size == -1)
2925     total_size = 0;
2926
2927   buffer = g_malloc0 (STREAM_BUFFER_SIZE);
2928   current_size = 0;
2929   res = TRUE;
2930   while (TRUE)
2931     {
2932       n_read = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, cancellable, error);
2933       if (n_read == -1)
2934         {
2935           res = FALSE;
2936           break;
2937         }
2938
2939       if (n_read == 0)
2940         break;
2941
2942       current_size += n_read;
2943
2944       res = g_output_stream_write_all (out, buffer, n_read, &n_written, cancellable, error);
2945       if (!res)
2946         break;
2947
2948       if (progress_callback)
2949         progress_callback (current_size, total_size, progress_callback_data);
2950     }
2951   g_free (buffer);
2952
2953   /* Make sure we send full copied size */
2954   if (progress_callback)
2955     progress_callback (current_size, total_size, progress_callback_data);
2956
2957   return res;
2958 }
2959
2960 #ifdef HAVE_SPLICE
2961
2962 static gboolean
2963 do_splice (int     fd_in,
2964            loff_t *off_in,
2965            int     fd_out,
2966            loff_t *off_out,
2967            size_t  len,
2968            long   *bytes_transferd,
2969            GError **error)
2970 {
2971   long result;
2972
2973 retry:
2974   result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2975
2976   if (result == -1)
2977     {
2978       int errsv = errno;
2979
2980       if (errsv == EINTR)
2981         goto retry;
2982       else if (errsv == ENOSYS || errsv == EINVAL || errsv == EOPNOTSUPP)
2983         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2984                              _("Splice not supported"));
2985       else
2986         g_set_error (error, G_IO_ERROR,
2987                      g_io_error_from_errno (errsv),
2988                      _("Error splicing file: %s"),
2989                      g_strerror (errsv));
2990
2991       return FALSE;
2992     }
2993
2994   *bytes_transferd = result;
2995   return TRUE;
2996 }
2997
2998 static gboolean
2999 splice_stream_with_progress (GInputStream           *in,
3000                              GOutputStream          *out,
3001                              GCancellable           *cancellable,
3002                              GFileProgressCallback   progress_callback,
3003                              gpointer                progress_callback_data,
3004                              GError                **error)
3005 {
3006   int buffer[2] = { -1, -1 };
3007   int buffer_size;
3008   gboolean res;
3009   goffset total_size;
3010   loff_t offset_in;
3011   loff_t offset_out;
3012   int fd_in, fd_out;
3013
3014   fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3015   fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3016
3017   if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
3018     return FALSE;
3019
3020 #if defined(F_SETPIPE_SZ) && defined(F_GETPIPE_SZ)
3021   /* Try a 1MiB buffer for improved throughput. If that fails, use the default
3022    * pipe size. See: https://bugzilla.gnome.org/791457 */
3023   buffer_size = fcntl (buffer[1], F_SETPIPE_SZ, 1024 * 1024);
3024   if (buffer_size <= 0)
3025     {
3026       int errsv;
3027       buffer_size = fcntl (buffer[1], F_GETPIPE_SZ);
3028       errsv = errno;
3029
3030       if (buffer_size <= 0)
3031         {
3032           g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
3033                        _("Error splicing file: %s"), g_strerror (errsv));
3034           res = FALSE;
3035           goto out;
3036         }
3037     }
3038 #else
3039   /* If #F_GETPIPE_SZ isn’t available, assume we’re on Linux < 2.6.35,
3040    * but ≥ 2.6.11, meaning the pipe capacity is 64KiB. Ignore the possibility of
3041    * running on Linux < 2.6.11 (where the capacity was the system page size,
3042    * typically 4KiB) because it’s ancient. See pipe(7). */
3043   buffer_size = 1024 * 64;
3044 #endif
3045
3046   g_assert (buffer_size > 0);
3047
3048   total_size = -1;
3049   /* avoid performance impact of querying total size when it's not needed */
3050   if (progress_callback)
3051     {
3052       struct stat sbuf;
3053
3054       if (fstat (fd_in, &sbuf) == 0)
3055         total_size = sbuf.st_size;
3056     }
3057
3058   if (total_size == -1)
3059     total_size = 0;
3060
3061   offset_in = offset_out = 0;
3062   res = FALSE;
3063   while (TRUE)
3064     {
3065       long n_read;
3066       long n_written;
3067
3068       if (g_cancellable_set_error_if_cancelled (cancellable, error))
3069         break;
3070
3071       if (!do_splice (fd_in, &offset_in, buffer[1], NULL, buffer_size, &n_read, error))
3072         break;
3073
3074       if (n_read == 0)
3075         {
3076           res = TRUE;
3077           break;
3078         }
3079
3080       while (n_read > 0)
3081         {
3082           if (g_cancellable_set_error_if_cancelled (cancellable, error))
3083             goto out;
3084
3085           if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
3086             goto out;
3087
3088           n_read -= n_written;
3089         }
3090
3091       if (progress_callback)
3092         progress_callback (offset_in, total_size, progress_callback_data);
3093     }
3094
3095   /* Make sure we send full copied size */
3096   if (progress_callback)
3097     progress_callback (offset_in, total_size, progress_callback_data);
3098
3099   if (!g_close (buffer[0], error))
3100     goto out;
3101   buffer[0] = -1;
3102   if (!g_close (buffer[1], error))
3103     goto out;
3104   buffer[1] = -1;
3105  out:
3106   if (buffer[0] != -1)
3107     (void) g_close (buffer[0], NULL);
3108   if (buffer[1] != -1)
3109     (void) g_close (buffer[1], NULL);
3110
3111   return res;
3112 }
3113 #endif
3114
3115 #ifdef __linux__
3116 static gboolean
3117 btrfs_reflink_with_progress (GInputStream           *in,
3118                              GOutputStream          *out,
3119                              GFileInfo              *info,
3120                              GCancellable           *cancellable,
3121                              GFileProgressCallback   progress_callback,
3122                              gpointer                progress_callback_data,
3123                              GError                **error)
3124 {
3125   goffset source_size;
3126   int fd_in, fd_out;
3127   int ret, errsv;
3128
3129   fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3130   fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3131
3132   if (progress_callback)
3133     source_size = g_file_info_get_size (info);
3134
3135   /* Btrfs clone ioctl properties:
3136    *  - Works at the inode level
3137    *  - Doesn't work with directories
3138    *  - Always follows symlinks (source and destination)
3139    *
3140    * By the time we get here, *in and *out are both regular files */
3141   ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3142   errsv = errno;
3143
3144   if (ret < 0)
3145     {
3146       if (errsv == EXDEV)
3147         g_set_error_literal (error, G_IO_ERROR,
3148                              G_IO_ERROR_NOT_SUPPORTED,
3149                              _("Copy (reflink/clone) between mounts is not supported"));
3150       else if (errsv == EINVAL)
3151         g_set_error_literal (error, G_IO_ERROR,
3152                              G_IO_ERROR_NOT_SUPPORTED,
3153                              _("Copy (reflink/clone) is not supported or invalid"));
3154       else
3155         /* Most probably something odd happened; retry with fallback */
3156         g_set_error_literal (error, G_IO_ERROR,
3157                              G_IO_ERROR_NOT_SUPPORTED,
3158                              _("Copy (reflink/clone) is not supported or didn’t work"));
3159       /* We retry with fallback for all error cases because Btrfs is currently
3160        * unstable, and so we can't trust it to do clone properly.
3161        * In addition, any hard errors here would cause the same failure in the
3162        * fallback manual copy as well. */
3163       return FALSE;
3164     }
3165
3166   /* Make sure we send full copied size */
3167   if (progress_callback)
3168     progress_callback (source_size, source_size, progress_callback_data);
3169
3170   return TRUE;
3171 }
3172 #endif
3173
3174 static gboolean
3175 file_copy_fallback (GFile                  *source,
3176                     GFile                  *destination,
3177                     GFileCopyFlags          flags,
3178                     GCancellable           *cancellable,
3179                     GFileProgressCallback   progress_callback,
3180                     gpointer                progress_callback_data,
3181                     GError                **error)
3182 {
3183   gboolean ret = FALSE;
3184   GFileInputStream *file_in = NULL;
3185   GInputStream *in = NULL;
3186   GOutputStream *out = NULL;
3187   GFileInfo *info = NULL;
3188   const char *target;
3189   char *attrs_to_read;
3190   gboolean do_set_attributes = FALSE;
3191   GFileCreateFlags create_flags;
3192
3193   /* need to know the file type */
3194   info = g_file_query_info (source,
3195                             G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3196                             G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3197                             cancellable,
3198                             error);
3199   if (!info)
3200     goto out;
3201
3202   /* Maybe copy the symlink? */
3203   if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3204       g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3205     {
3206       target = g_file_info_get_symlink_target (info);
3207       if (target)
3208         {
3209           if (!copy_symlink (destination, flags, cancellable, target, error))
3210             goto out;
3211
3212           ret = TRUE;
3213           goto out;
3214         }
3215         /* ... else fall back on a regular file copy */
3216     }
3217   /* Handle "special" files (pipes, device nodes, ...)? */
3218   else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3219     {
3220       /* FIXME: could try to recreate device nodes and others? */
3221       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3222                            _("Can’t copy special file"));
3223       goto out;
3224     }
3225
3226   /* Everything else should just fall back on a regular copy. */
3227
3228   file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3229   if (!file_in)
3230     goto out;
3231   in = G_INPUT_STREAM (file_in);
3232
3233   if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3234                                       cancellable, error))
3235     goto out;
3236
3237   if (attrs_to_read != NULL)
3238     {
3239       GError *tmp_error = NULL;
3240
3241       /* Ok, ditch the previous lightweight info (on Unix we just
3242        * called lstat()); at this point we gather all the information
3243        * we need about the source from the opened file descriptor.
3244        */
3245       g_object_unref (info);
3246
3247       info = g_file_input_stream_query_info (file_in, attrs_to_read,
3248                                              cancellable, &tmp_error);
3249       if (!info)
3250         {
3251           /* Not all gvfs backends implement query_info_on_read(), we
3252            * can just fall back to the pathname again.
3253            * https://bugzilla.gnome.org/706254
3254            */
3255           if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3256             {
3257               g_clear_error (&tmp_error);
3258               info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3259                                         cancellable, error);
3260             }
3261           else
3262             {
3263               g_free (attrs_to_read);
3264               g_propagate_error (error, tmp_error);
3265               goto out;
3266             }
3267         }
3268       g_free (attrs_to_read);
3269       if (!info)
3270         goto out;
3271
3272       do_set_attributes = TRUE;
3273     }
3274
3275   /* In the local file path, we pass down the source info which
3276    * includes things like unix::mode, to ensure that the target file
3277    * is not created with different permissions from the source file.
3278    *
3279    * If a future API like g_file_replace_with_info() is added, switch
3280    * this code to use that.
3281    *
3282    * Use %G_FILE_CREATE_PRIVATE unless
3283    *  - we were told to create the file with default permissions (i.e. the
3284    *    process’ umask),
3285    *  - or if the source file is on a file system which doesn’t support
3286    *    `unix::mode` (in which case it probably also makes sense to create the
3287    *    destination with default permissions because the source cannot be
3288    *    private),
3289    *  - or if the destination file is a `GLocalFile`, in which case we can
3290    *    directly open() it with the permissions from the source file.
3291    */
3292   create_flags = G_FILE_CREATE_NONE;
3293   if (!(flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) &&
3294       g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_UNIX_MODE) &&
3295       !G_IS_LOCAL_FILE (destination))
3296     create_flags |= G_FILE_CREATE_PRIVATE;
3297   if (flags & G_FILE_COPY_OVERWRITE)
3298     create_flags |= G_FILE_CREATE_REPLACE_DESTINATION;
3299
3300   if (G_IS_LOCAL_FILE (destination))
3301     {
3302       if (flags & G_FILE_COPY_OVERWRITE)
3303         out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3304                                                                    FALSE, NULL,
3305                                                                    flags & G_FILE_COPY_BACKUP,
3306                                                                    create_flags,
3307                                                                    (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
3308                                                                    cancellable, error);
3309       else
3310         out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3311                                                                   FALSE, create_flags,
3312                                                                   (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
3313                                                                   cancellable, error);
3314     }
3315   else if (flags & G_FILE_COPY_OVERWRITE)
3316     {
3317       out = (GOutputStream *)g_file_replace (destination,
3318                                              NULL,
3319                                              flags & G_FILE_COPY_BACKUP,
3320                                              create_flags,
3321                                              cancellable, error);
3322     }
3323   else
3324     {
3325       out = (GOutputStream *)g_file_create (destination, create_flags, cancellable, error);
3326     }
3327
3328   if (!out)
3329     goto out;
3330
3331 #ifdef __linux__
3332   if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3333     {
3334       GError *reflink_err = NULL;
3335
3336       if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3337                                         progress_callback, progress_callback_data,
3338                                         &reflink_err))
3339         {
3340           if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3341             {
3342               g_clear_error (&reflink_err);
3343             }
3344           else
3345             {
3346               g_propagate_error (error, reflink_err);
3347               goto out;
3348             }
3349         }
3350       else
3351         {
3352           ret = TRUE;
3353           goto out;
3354         }
3355     }
3356 #endif
3357
3358 #ifdef HAVE_SPLICE
3359   if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3360     {
3361       GError *splice_err = NULL;
3362
3363       if (!splice_stream_with_progress (in, out, cancellable,
3364                                         progress_callback, progress_callback_data,
3365                                         &splice_err))
3366         {
3367           if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3368             {
3369               g_clear_error (&splice_err);
3370             }
3371           else
3372             {
3373               g_propagate_error (error, splice_err);
3374               goto out;
3375             }
3376         }
3377       else
3378         {
3379           ret = TRUE;
3380           goto out;
3381         }
3382     }
3383
3384 #endif
3385
3386   /* A plain read/write loop */
3387   if (!copy_stream_with_progress (in, out, source, cancellable,
3388                                   progress_callback, progress_callback_data,
3389                                   error))
3390     goto out;
3391
3392   ret = TRUE;
3393  out:
3394   if (in)
3395     {
3396       /* Don't care about errors in source here */
3397       (void) g_input_stream_close (in, cancellable, NULL);
3398       g_object_unref (in);
3399     }
3400
3401   if (out)
3402     {
3403       /* But write errors on close are bad! */
3404       if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3405         ret = FALSE;
3406       g_object_unref (out);
3407     }
3408
3409   /* Ignore errors here. Failure to copy metadata is not a hard error */
3410   /* TODO: set these attributes /before/ we do the rename() on Unix */
3411   if (ret && do_set_attributes)
3412     {
3413       g_file_set_attributes_from_info (destination,
3414                                        info,
3415                                        G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3416                                        cancellable,
3417                                        NULL);
3418     }
3419
3420   g_clear_object (&info);
3421
3422   return ret;
3423 }
3424
3425 /**
3426  * g_file_copy:
3427  * @source: input #GFile
3428  * @destination: destination #GFile
3429  * @flags: set of #GFileCopyFlags
3430  * @cancellable: (nullable): optional #GCancellable object,
3431  *     %NULL to ignore
3432  * @progress_callback: (nullable) (scope call): function to callback with
3433  *     progress information, or %NULL if progress information is not needed
3434  * @progress_callback_data: (closure): user data to pass to @progress_callback
3435  * @error: #GError to set on error, or %NULL
3436  *
3437  * Copies the file @source to the location specified by @destination.
3438  * Can not handle recursive copies of directories.
3439  *
3440  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3441  * existing @destination file is overwritten.
3442  *
3443  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3444  * will be copied as symlinks, otherwise the target of the
3445  * @source symlink will be copied.
3446  *
3447  * If the flag #G_FILE_COPY_ALL_METADATA is specified then all the metadata
3448  * that is possible to copy is copied, not just the default subset (which,
3449  * for instance, does not include the owner, see #GFileInfo).
3450  *
3451  * If @cancellable is not %NULL, then the operation can be cancelled by
3452  * triggering the cancellable object from another thread. If the operation
3453  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3454  *
3455  * If @progress_callback is not %NULL, then the operation can be monitored
3456  * by setting this to a #GFileProgressCallback function.
3457  * @progress_callback_data will be passed to this function. It is guaranteed
3458  * that this callback will be called after all data has been transferred with
3459  * the total number of bytes copied during the operation.
3460  *
3461  * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3462  * is returned, independent on the status of the @destination.
3463  *
3464  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3465  * the error %G_IO_ERROR_EXISTS is returned.
3466  *
3467  * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3468  * error is returned. If trying to overwrite a directory with a directory the
3469  * %G_IO_ERROR_WOULD_MERGE error is returned.
3470  *
3471  * If the source is a directory and the target does not exist, or
3472  * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3473  * %G_IO_ERROR_WOULD_RECURSE error is returned.
3474  *
3475  * If you are interested in copying the #GFile object itself (not the on-disk
3476  * file), see g_file_dup().
3477  *
3478  * Returns: %TRUE on success, %FALSE otherwise.
3479  */
3480 gboolean
3481 g_file_copy (GFile                  *source,
3482              GFile                  *destination,
3483              GFileCopyFlags          flags,
3484              GCancellable           *cancellable,
3485              GFileProgressCallback   progress_callback,
3486              gpointer                progress_callback_data,
3487              GError                **error)
3488 {
3489   GFileIface *iface;
3490   GError *my_error;
3491   gboolean res;
3492
3493   g_return_val_if_fail (G_IS_FILE (source), FALSE);
3494   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3495
3496   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3497     return FALSE;
3498
3499   iface = G_FILE_GET_IFACE (destination);
3500   if (iface->copy)
3501     {
3502       my_error = NULL;
3503       res = (* iface->copy) (source, destination,
3504                              flags, cancellable,
3505                              progress_callback, progress_callback_data,
3506                              &my_error);
3507
3508       if (res)
3509         return TRUE;
3510
3511       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3512         {
3513           g_propagate_error (error, my_error);
3514               return FALSE;
3515         }
3516       else
3517         g_clear_error (&my_error);
3518     }
3519
3520   /* If the types are different, and the destination method failed
3521    * also try the source method
3522    */
3523   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3524     {
3525       iface = G_FILE_GET_IFACE (source);
3526
3527       if (iface->copy)
3528         {
3529           my_error = NULL;
3530           res = (* iface->copy) (source, destination,
3531                                  flags, cancellable,
3532                                  progress_callback, progress_callback_data,
3533                                  &my_error);
3534
3535           if (res)
3536             return TRUE;
3537
3538           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3539             {
3540               g_propagate_error (error, my_error);
3541               return FALSE;
3542             }
3543           else
3544             g_clear_error (&my_error);
3545         }
3546     }
3547
3548   return file_copy_fallback (source, destination, flags, cancellable,
3549                              progress_callback, progress_callback_data,
3550                              error);
3551 }
3552
3553 /**
3554  * g_file_copy_async:
3555  * @source: input #GFile
3556  * @destination: destination #GFile
3557  * @flags: set of #GFileCopyFlags
3558  * @io_priority: the [I/O priority][io-priority] of the request
3559  * @cancellable: (nullable): optional #GCancellable object,
3560  *     %NULL to ignore
3561  * @progress_callback: (nullable) (scope notified): function to callback with progress
3562  *     information, or %NULL if progress information is not needed
3563  * @progress_callback_data: (closure progress_callback) (nullable): user data to pass to @progress_callback
3564  * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
3565  * @user_data: (closure callback): the data to pass to callback function
3566  *
3567  * Copies the file @source to the location specified by @destination
3568  * asynchronously. For details of the behaviour, see g_file_copy().
3569  *
3570  * If @progress_callback is not %NULL, then that function that will be called
3571  * just like in g_file_copy(). The callback will run in the default main context
3572  * of the thread calling g_file_copy_async() — the same context as @callback is
3573  * run in.
3574  *
3575  * When the operation is finished, @callback will be called. You can then call
3576  * g_file_copy_finish() to get the result of the operation.
3577  */
3578 void
3579 g_file_copy_async (GFile                  *source,
3580                    GFile                  *destination,
3581                    GFileCopyFlags          flags,
3582                    int                     io_priority,
3583                    GCancellable           *cancellable,
3584                    GFileProgressCallback   progress_callback,
3585                    gpointer                progress_callback_data,
3586                    GAsyncReadyCallback     callback,
3587                    gpointer                user_data)
3588 {
3589   GFileIface *iface;
3590
3591   g_return_if_fail (G_IS_FILE (source));
3592   g_return_if_fail (G_IS_FILE (destination));
3593
3594   iface = G_FILE_GET_IFACE (source);
3595   (* iface->copy_async) (source,
3596                          destination,
3597                          flags,
3598                          io_priority,
3599                          cancellable,
3600                          progress_callback,
3601                          progress_callback_data,
3602                          callback,
3603                          user_data);
3604 }
3605
3606 /**
3607  * g_file_copy_finish:
3608  * @file: input #GFile
3609  * @res: a #GAsyncResult
3610  * @error: a #GError, or %NULL
3611  *
3612  * Finishes copying the file started with g_file_copy_async().
3613  *
3614  * Returns: a %TRUE on success, %FALSE on error.
3615  */
3616 gboolean
3617 g_file_copy_finish (GFile         *file,
3618                     GAsyncResult  *res,
3619                     GError       **error)
3620 {
3621   GFileIface *iface;
3622
3623   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3624   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3625
3626   if (g_async_result_legacy_propagate_error (res, error))
3627     return FALSE;
3628
3629   iface = G_FILE_GET_IFACE (file);
3630   return (* iface->copy_finish) (file, res, error);
3631 }
3632
3633 /**
3634  * g_file_move:
3635  * @source: #GFile pointing to the source location
3636  * @destination: #GFile pointing to the destination location
3637  * @flags: set of #GFileCopyFlags
3638  * @cancellable: (nullable): optional #GCancellable object,
3639  *     %NULL to ignore
3640  * @progress_callback: (nullable) (scope call): #GFileProgressCallback
3641  *     function for updates
3642  * @progress_callback_data: (closure): gpointer to user data for
3643  *     the callback function
3644  * @error: #GError for returning error conditions, or %NULL
3645  *
3646  * Tries to move the file or directory @source to the location specified
3647  * by @destination. If native move operations are supported then this is
3648  * used, otherwise a copy + delete fallback is used. The native
3649  * implementation may support moving directories (for instance on moves
3650  * inside the same filesystem), but the fallback code does not.
3651  *
3652  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3653  * existing @destination file is overwritten.
3654  *
3655  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3656  * will be copied as symlinks, otherwise the target of the
3657  * @source symlink will be copied.
3658  *
3659  * If @cancellable is not %NULL, then the operation can be cancelled by
3660  * triggering the cancellable object from another thread. If the operation
3661  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3662  *
3663  * If @progress_callback is not %NULL, then the operation can be monitored
3664  * by setting this to a #GFileProgressCallback function.
3665  * @progress_callback_data will be passed to this function. It is
3666  * guaranteed that this callback will be called after all data has been
3667  * transferred with the total number of bytes copied during the operation.
3668  *
3669  * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3670  * error is returned, independent on the status of the @destination.
3671  *
3672  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3673  * then the error %G_IO_ERROR_EXISTS is returned.
3674  *
3675  * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3676  * error is returned. If trying to overwrite a directory with a directory the
3677  * %G_IO_ERROR_WOULD_MERGE error is returned.
3678  *
3679  * If the source is a directory and the target does not exist, or
3680  * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3681  * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3682  * move operation isn't available).
3683  *
3684  * Returns: %TRUE on successful move, %FALSE otherwise.
3685  */
3686 gboolean
3687 g_file_move (GFile                  *source,
3688              GFile                  *destination,
3689              GFileCopyFlags          flags,
3690              GCancellable           *cancellable,
3691              GFileProgressCallback   progress_callback,
3692              gpointer                progress_callback_data,
3693              GError                **error)
3694 {
3695   GFileIface *iface;
3696   GError *my_error;
3697   gboolean res;
3698
3699   g_return_val_if_fail (G_IS_FILE (source), FALSE);
3700   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3701
3702   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3703     return FALSE;
3704
3705   iface = G_FILE_GET_IFACE (destination);
3706   if (iface->move)
3707     {
3708       my_error = NULL;
3709       res = (* iface->move) (source, destination,
3710                              flags, cancellable,
3711                              progress_callback, progress_callback_data,
3712                              &my_error);
3713
3714       if (res)
3715         return TRUE;
3716
3717       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3718         {
3719           g_propagate_error (error, my_error);
3720           return FALSE;
3721         }
3722       else
3723         g_clear_error (&my_error);
3724     }
3725
3726   /* If the types are different, and the destination method failed
3727    * also try the source method
3728    */
3729   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3730     {
3731       iface = G_FILE_GET_IFACE (source);
3732
3733       if (iface->move)
3734         {
3735           my_error = NULL;
3736           res = (* iface->move) (source, destination,
3737                                  flags, cancellable,
3738                                  progress_callback, progress_callback_data,
3739                                  &my_error);
3740
3741           if (res)
3742             return TRUE;
3743
3744           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3745             {
3746               g_propagate_error (error, my_error);
3747               return FALSE;
3748             }
3749           else
3750             g_clear_error (&my_error);
3751         }
3752     }
3753
3754   if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3755     {
3756       g_set_error_literal (error, G_IO_ERROR,
3757                            G_IO_ERROR_NOT_SUPPORTED,
3758                            _("Operation not supported"));
3759       return FALSE;
3760     }
3761
3762   flags |= G_FILE_COPY_ALL_METADATA;
3763   if (!g_file_copy (source, destination, flags, cancellable,
3764                     progress_callback, progress_callback_data,
3765                     error))
3766     return FALSE;
3767
3768   return g_file_delete (source, cancellable, error);
3769 }
3770
3771 /**
3772  * g_file_make_directory:
3773  * @file: input #GFile
3774  * @cancellable: (nullable): optional #GCancellable object,
3775  *     %NULL to ignore
3776  * @error: a #GError, or %NULL
3777  *
3778  * Creates a directory. Note that this will only create a child directory
3779  * of the immediate parent directory of the path or URI given by the #GFile.
3780  * To recursively create directories, see g_file_make_directory_with_parents().
3781  * This function will fail if the parent directory does not exist, setting
3782  * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3783  * creating directories, this function will fail, setting @error to
3784  * %G_IO_ERROR_NOT_SUPPORTED.
3785  *
3786  * For a local #GFile the newly created directory will have the default
3787  * (current) ownership and permissions of the current process.
3788  *
3789  * If @cancellable is not %NULL, then the operation can be cancelled by
3790  * triggering the cancellable object from another thread. If the operation
3791  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3792  *
3793  * Returns: %TRUE on successful creation, %FALSE otherwise.
3794  */
3795 gboolean
3796 g_file_make_directory (GFile         *file,
3797                        GCancellable  *cancellable,
3798                        GError       **error)
3799 {
3800   GFileIface *iface;
3801
3802   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3803
3804   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3805     return FALSE;
3806
3807   iface = G_FILE_GET_IFACE (file);
3808
3809   if (iface->make_directory == NULL)
3810     {
3811       g_set_error_literal (error, G_IO_ERROR,
3812                            G_IO_ERROR_NOT_SUPPORTED,
3813                            _("Operation not supported"));
3814       return FALSE;
3815     }
3816
3817   return (* iface->make_directory) (file, cancellable, error);
3818 }
3819
3820 /**
3821  * g_file_make_directory_async:
3822  * @file: input #GFile
3823  * @io_priority: the [I/O priority][io-priority] of the request
3824  * @cancellable: (nullable): optional #GCancellable object,
3825  *     %NULL to ignore
3826  * @callback: a #GAsyncReadyCallback to call
3827  *     when the request is satisfied
3828  * @user_data: the data to pass to callback function
3829  *
3830  * Asynchronously creates a directory.
3831  *
3832  * Virtual: make_directory_async
3833  * Since: 2.38
3834  */
3835 void
3836 g_file_make_directory_async (GFile               *file,
3837                              int                  io_priority,
3838                              GCancellable        *cancellable,
3839                              GAsyncReadyCallback  callback,
3840                              gpointer             user_data)
3841 {
3842   GFileIface *iface;
3843
3844   g_return_if_fail (G_IS_FILE (file));
3845
3846   iface = G_FILE_GET_IFACE (file);
3847   (* iface->make_directory_async) (file,
3848                                    io_priority,
3849                                    cancellable,
3850                                    callback,
3851                                    user_data);
3852 }
3853
3854 /**
3855  * g_file_make_directory_finish:
3856  * @file: input #GFile
3857  * @result: a #GAsyncResult
3858  * @error: a #GError, or %NULL
3859  *
3860  * Finishes an asynchronous directory creation, started with
3861  * g_file_make_directory_async().
3862  *
3863  * Virtual: make_directory_finish
3864  * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3865  * Since: 2.38
3866  */
3867 gboolean
3868 g_file_make_directory_finish (GFile         *file,
3869                               GAsyncResult  *result,
3870                               GError       **error)
3871 {
3872   GFileIface *iface;
3873
3874   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3875   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3876
3877   iface = G_FILE_GET_IFACE (file);
3878   return (* iface->make_directory_finish) (file, result, error);
3879 }
3880
3881 /**
3882  * g_file_make_directory_with_parents:
3883  * @file: input #GFile
3884  * @cancellable: (nullable): optional #GCancellable object,
3885  *     %NULL to ignore
3886  * @error: a #GError, or %NULL
3887  *
3888  * Creates a directory and any parent directories that may not
3889  * exist similar to 'mkdir -p'. If the file system does not support
3890  * creating directories, this function will fail, setting @error to
3891  * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3892  * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3893  * the similar g_mkdir_with_parents().
3894  *
3895  * For a local #GFile the newly created directories will have the default
3896  * (current) ownership and permissions of the current process.
3897  *
3898  * If @cancellable is not %NULL, then the operation can be cancelled by
3899  * triggering the cancellable object from another thread. If the operation
3900  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3901  *
3902  * Returns: %TRUE if all directories have been successfully created, %FALSE
3903  * otherwise.
3904  *
3905  * Since: 2.18
3906  */
3907 gboolean
3908 g_file_make_directory_with_parents (GFile         *file,
3909                                     GCancellable  *cancellable,
3910                                     GError       **error)
3911 {
3912   GFile *work_file = NULL;
3913   GList *list = NULL, *l;
3914   GError *my_error = NULL;
3915
3916   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3917
3918   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3919     return FALSE;
3920
3921   /* Try for the simple case of not having to create any parent
3922    * directories.  If any parent directory needs to be created, this
3923    * call will fail with NOT_FOUND. If that happens, then that value of
3924    * my_error persists into the while loop below.
3925    */
3926   g_file_make_directory (file, cancellable, &my_error);
3927   if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3928     {
3929       if (my_error)
3930         g_propagate_error (error, my_error);
3931       return my_error == NULL;
3932     }
3933
3934   work_file = g_object_ref (file);
3935
3936   /* Creates the parent directories as needed. In case any particular
3937    * creation operation fails for lack of other parent directories
3938    * (NOT_FOUND), the directory is added to a list of directories to
3939    * create later, and the value of my_error is retained until the next
3940    * iteration of the loop.  After the loop my_error should either be
3941    * empty or contain a real failure condition.
3942    */
3943   while (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3944     {
3945       GFile *parent_file;
3946
3947       parent_file = g_file_get_parent (work_file);
3948       if (parent_file == NULL)
3949         break;
3950
3951       g_clear_error (&my_error);
3952       g_file_make_directory (parent_file, cancellable, &my_error);
3953       /* Another process may have created the directory in between the
3954        * G_IO_ERROR_NOT_FOUND and now
3955        */
3956       if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3957         g_clear_error (&my_error);
3958
3959       g_object_unref (work_file);
3960       work_file = g_object_ref (parent_file);
3961
3962       if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3963         list = g_list_prepend (list, parent_file);  /* Transfer ownership of ref */
3964       else
3965         g_object_unref (parent_file);
3966     }
3967
3968   /* All directories should be able to be created now, so an error at
3969    * this point means the whole operation must fail -- except an EXISTS
3970    * error, which means that another process already created the
3971    * directory in between the previous failure and now.
3972    */
3973   for (l = list; my_error == NULL && l; l = l->next)
3974     {
3975       g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3976       if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3977         g_clear_error (&my_error);
3978     }
3979
3980   if (work_file)
3981     g_object_unref (work_file);
3982
3983   /* Clean up */
3984   while (list != NULL)
3985     {
3986       g_object_unref ((GFile *) list->data);
3987       list = g_list_remove (list, list->data);
3988     }
3989
3990   /* At this point an error in my_error means a that something
3991    * unexpected failed in either of the loops above, so the whole
3992    * operation must fail.
3993    */
3994   if (my_error != NULL)
3995     {
3996       g_propagate_error (error, my_error);
3997       return FALSE;
3998     }
3999
4000   return g_file_make_directory (file, cancellable, error);
4001 }
4002
4003 /**
4004  * g_file_make_symbolic_link:
4005  * @file: a #GFile with the name of the symlink to create
4006  * @symlink_value: (type filename): a string with the path for the target
4007  *     of the new symlink
4008  * @cancellable: (nullable): optional #GCancellable object,
4009  *     %NULL to ignore
4010  * @error: a #GError
4011  *
4012  * Creates a symbolic link named @file which contains the string
4013  * @symlink_value.
4014  *
4015  * If @cancellable is not %NULL, then the operation can be cancelled by
4016  * triggering the cancellable object from another thread. If the operation
4017  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4018  *
4019  * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
4020  */
4021 gboolean
4022 g_file_make_symbolic_link (GFile         *file,
4023                            const char    *symlink_value,
4024                            GCancellable  *cancellable,
4025                            GError       **error)
4026 {
4027   GFileIface *iface;
4028
4029   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4030   g_return_val_if_fail (symlink_value != NULL, FALSE);
4031
4032   if (g_cancellable_set_error_if_cancelled (cancellable, error))
4033     return FALSE;
4034
4035   if (*symlink_value == '\0')
4036     {
4037       g_set_error_literal (error, G_IO_ERROR,
4038                            G_IO_ERROR_INVALID_ARGUMENT,
4039                            _("Invalid symlink value given"));
4040       return FALSE;
4041     }
4042
4043   iface = G_FILE_GET_IFACE (file);
4044
4045   if (iface->make_symbolic_link == NULL)
4046     {
4047       g_set_error_literal (error, G_IO_ERROR,
4048                            G_IO_ERROR_NOT_SUPPORTED,
4049                            _("Operation not supported"));
4050       return FALSE;
4051     }
4052
4053   return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
4054 }
4055
4056 /**
4057  * g_file_delete:
4058  * @file: input #GFile
4059  * @cancellable: (nullable): optional #GCancellable object,
4060  *     %NULL to ignore
4061  * @error: a #GError, or %NULL
4062  *
4063  * Deletes a file. If the @file is a directory, it will only be
4064  * deleted if it is empty. This has the same semantics as g_unlink().
4065  *
4066  * If @cancellable is not %NULL, then the operation can be cancelled by
4067  * triggering the cancellable object from another thread. If the operation
4068  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4069  *
4070  * Virtual: delete_file
4071  * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4072  */
4073 gboolean
4074 g_file_delete (GFile         *file,
4075                GCancellable  *cancellable,
4076                GError       **error)
4077 {
4078   GFileIface *iface;
4079
4080   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4081
4082   if (g_cancellable_set_error_if_cancelled (cancellable, error))
4083     return FALSE;
4084
4085   iface = G_FILE_GET_IFACE (file);
4086
4087   if (iface->delete_file == NULL)
4088     {
4089       g_set_error_literal (error, G_IO_ERROR,
4090                            G_IO_ERROR_NOT_SUPPORTED,
4091                            _("Operation not supported"));
4092       return FALSE;
4093     }
4094
4095   return (* iface->delete_file) (file, cancellable, error);
4096 }
4097
4098 /**
4099  * g_file_delete_async:
4100  * @file: input #GFile
4101  * @io_priority: the [I/O priority][io-priority] of the request
4102  * @cancellable: (nullable): optional #GCancellable object,
4103  *     %NULL to ignore
4104  * @callback: a #GAsyncReadyCallback to call
4105  *     when the request is satisfied
4106  * @user_data: the data to pass to callback function
4107  *
4108  * Asynchronously delete a file. If the @file is a directory, it will
4109  * only be deleted if it is empty.  This has the same semantics as
4110  * g_unlink().
4111  *
4112  * Virtual: delete_file_async
4113  * Since: 2.34
4114  */
4115 void
4116 g_file_delete_async (GFile               *file,
4117                      int                  io_priority,
4118                      GCancellable        *cancellable,
4119                      GAsyncReadyCallback  callback,
4120                      gpointer             user_data)
4121 {
4122   GFileIface *iface;
4123
4124   g_return_if_fail (G_IS_FILE (file));
4125
4126   iface = G_FILE_GET_IFACE (file);
4127   (* iface->delete_file_async) (file,
4128                                 io_priority,
4129                                 cancellable,
4130                                 callback,
4131                                 user_data);
4132 }
4133
4134 /**
4135  * g_file_delete_finish:
4136  * @file: input #GFile
4137  * @result: a #GAsyncResult
4138  * @error: a #GError, or %NULL
4139  *
4140  * Finishes deleting a file started with g_file_delete_async().
4141  *
4142  * Virtual: delete_file_finish
4143  * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4144  * Since: 2.34
4145  **/
4146 gboolean
4147 g_file_delete_finish (GFile         *file,
4148                       GAsyncResult  *result,
4149                       GError       **error)
4150 {
4151   GFileIface *iface;
4152
4153   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4154   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4155
4156   if (g_async_result_legacy_propagate_error (result, error))
4157     return FALSE;
4158
4159   iface = G_FILE_GET_IFACE (file);
4160   return (* iface->delete_file_finish) (file, result, error);
4161 }
4162
4163 /**
4164  * g_file_trash:
4165  * @file: #GFile to send to trash
4166  * @cancellable: (nullable): optional #GCancellable object,
4167  *     %NULL to ignore
4168  * @error: a #GError, or %NULL
4169  *
4170  * Sends @file to the "Trashcan", if possible. This is similar to
4171  * deleting it, but the user can recover it before emptying the trashcan.
4172  * Not all file systems support trashing, so this call can return the
4173  * %G_IO_ERROR_NOT_SUPPORTED error.
4174  *
4175  * If @cancellable is not %NULL, then the operation can be cancelled by
4176  * triggering the cancellable object from another thread. If the operation
4177  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4178  *
4179  * Virtual: trash
4180  * Returns: %TRUE on successful trash, %FALSE otherwise.
4181  */
4182 gboolean
4183 g_file_trash (GFile         *file,
4184               GCancellable  *cancellable,
4185               GError       **error)
4186 {
4187   GFileIface *iface;
4188
4189   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4190
4191   if (g_cancellable_set_error_if_cancelled (cancellable, error))
4192     return FALSE;
4193
4194   iface = G_FILE_GET_IFACE (file);
4195
4196   if (iface->trash == NULL)
4197     {
4198       g_set_error_literal (error,
4199                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4200                            _("Trash not supported"));
4201       return FALSE;
4202     }
4203
4204   return (* iface->trash) (file, cancellable, error);
4205 }
4206
4207 /**
4208  * g_file_trash_async:
4209  * @file: input #GFile
4210  * @io_priority: the [I/O priority][io-priority] of the request
4211  * @cancellable: (nullable): optional #GCancellable object,
4212  *     %NULL to ignore
4213  * @callback: a #GAsyncReadyCallback to call
4214  *     when the request is satisfied
4215  * @user_data: the data to pass to callback function
4216  *
4217  * Asynchronously sends @file to the Trash location, if possible.
4218  *
4219  * Virtual: trash_async
4220  * Since: 2.38
4221  */
4222 void
4223 g_file_trash_async (GFile               *file,
4224                     int                  io_priority,
4225                     GCancellable        *cancellable,
4226                     GAsyncReadyCallback  callback,
4227                     gpointer             user_data)
4228 {
4229   GFileIface *iface;
4230
4231   g_return_if_fail (G_IS_FILE (file));
4232
4233   iface = G_FILE_GET_IFACE (file);
4234   (* iface->trash_async) (file,
4235                           io_priority,
4236                           cancellable,
4237                           callback,
4238                           user_data);
4239 }
4240
4241 /**
4242  * g_file_trash_finish:
4243  * @file: input #GFile
4244  * @result: a #GAsyncResult
4245  * @error: a #GError, or %NULL
4246  *
4247  * Finishes an asynchronous file trashing operation, started with
4248  * g_file_trash_async().
4249  *
4250  * Virtual: trash_finish
4251  * Returns: %TRUE on successful trash, %FALSE otherwise.
4252  * Since: 2.38
4253  */
4254 gboolean
4255 g_file_trash_finish (GFile         *file,
4256                      GAsyncResult  *result,
4257                      GError       **error)
4258 {
4259   GFileIface *iface;
4260
4261   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4262   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4263
4264   iface = G_FILE_GET_IFACE (file);
4265   return (* iface->trash_finish) (file, result, error);
4266 }
4267
4268 /**
4269  * g_file_set_display_name:
4270  * @file: input #GFile
4271  * @display_name: a string
4272  * @cancellable: (nullable): optional #GCancellable object,
4273  *     %NULL to ignore
4274  * @error: a #GError, or %NULL
4275  *
4276  * Renames @file to the specified display name.
4277  *
4278  * The display name is converted from UTF-8 to the correct encoding
4279  * for the target filesystem if possible and the @file is renamed to this.
4280  *
4281  * If you want to implement a rename operation in the user interface the
4282  * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4283  * initial value in the rename widget, and then the result after editing
4284  * should be passed to g_file_set_display_name().
4285  *
4286  * On success the resulting converted filename is returned.
4287  *
4288  * If @cancellable is not %NULL, then the operation can be cancelled by
4289  * triggering the cancellable object from another thread. If the operation
4290  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4291  *
4292  * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4293  *     or %NULL if there was an error.
4294  *     Free the returned object with g_object_unref().
4295  */
4296 GFile *
4297 g_file_set_display_name (GFile         *file,
4298                          const gchar   *display_name,
4299                          GCancellable  *cancellable,
4300                          GError       **error)
4301 {
4302   GFileIface *iface;
4303
4304   g_return_val_if_fail (G_IS_FILE (file), NULL);
4305   g_return_val_if_fail (display_name != NULL, NULL);
4306
4307   if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4308     {
4309       g_set_error (error,
4310                    G_IO_ERROR,
4311                    G_IO_ERROR_INVALID_ARGUMENT,
4312                    _("File names cannot contain “%c”"), G_DIR_SEPARATOR);
4313       return NULL;
4314     }
4315
4316   if (g_cancellable_set_error_if_cancelled (cancellable, error))
4317     return NULL;
4318
4319   iface = G_FILE_GET_IFACE (file);
4320
4321   return (* iface->set_display_name) (file, display_name, cancellable, error);
4322 }
4323
4324 /**
4325  * g_file_set_display_name_async:
4326  * @file: input #GFile
4327  * @display_name: a string
4328  * @io_priority: the [I/O priority][io-priority] of the request
4329  * @cancellable: (nullable): optional #GCancellable object,
4330  *     %NULL to ignore
4331  * @callback: (scope async): a #GAsyncReadyCallback to call
4332  *     when the request is satisfied
4333  * @user_data: (closure): the data to pass to callback function
4334  *
4335  * Asynchronously sets the display name for a given #GFile.
4336  *
4337  * For more details, see g_file_set_display_name() which is
4338  * the synchronous version of this call.
4339  *
4340  * When the operation is finished, @callback will be called.
4341  * You can then call g_file_set_display_name_finish() to get
4342  * the result of the operation.
4343  */
4344 void
4345 g_file_set_display_name_async (GFile               *file,
4346                                const gchar         *display_name,
4347                                gint                 io_priority,
4348                                GCancellable        *cancellable,
4349                                GAsyncReadyCallback  callback,
4350                                gpointer             user_data)
4351 {
4352   GFileIface *iface;
4353
4354   g_return_if_fail (G_IS_FILE (file));
4355   g_return_if_fail (display_name != NULL);
4356
4357   iface = G_FILE_GET_IFACE (file);
4358   (* iface->set_display_name_async) (file,
4359                                      display_name,
4360                                      io_priority,
4361                                      cancellable,
4362                                      callback,
4363                                      user_data);
4364 }
4365
4366 /**
4367  * g_file_set_display_name_finish:
4368  * @file: input #GFile
4369  * @res: a #GAsyncResult
4370  * @error: a #GError, or %NULL
4371  *
4372  * Finishes setting a display name started with
4373  * g_file_set_display_name_async().
4374  *
4375  * Returns: (transfer full): a #GFile or %NULL on error.
4376  *     Free the returned object with g_object_unref().
4377  */
4378 GFile *
4379 g_file_set_display_name_finish (GFile         *file,
4380                                 GAsyncResult  *res,
4381                                 GError       **error)
4382 {
4383   GFileIface *iface;
4384
4385   g_return_val_if_fail (G_IS_FILE (file), NULL);
4386   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4387
4388   if (g_async_result_legacy_propagate_error (res, error))
4389     return NULL;
4390
4391   iface = G_FILE_GET_IFACE (file);
4392   return (* iface->set_display_name_finish) (file, res, error);
4393 }
4394
4395 /**
4396  * g_file_query_settable_attributes:
4397  * @file: input #GFile
4398  * @cancellable: (nullable): optional #GCancellable object,
4399  *     %NULL to ignore
4400  * @error: a #GError, or %NULL
4401  *
4402  * Obtain the list of settable attributes for the file.
4403  *
4404  * Returns the type and full attribute name of all the attributes
4405  * that can be set on this file. This doesn't mean setting it will
4406  * always succeed though, you might get an access failure, or some
4407  * specific file may not support a specific attribute.
4408  *
4409  * If @cancellable is not %NULL, then the operation can be cancelled by
4410  * triggering the cancellable object from another thread. If the operation
4411  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4412  *
4413  * Returns: a #GFileAttributeInfoList describing the settable attributes.
4414  *     When you are done with it, release it with
4415  *     g_file_attribute_info_list_unref()
4416  */
4417 GFileAttributeInfoList *
4418 g_file_query_settable_attributes (GFile         *file,
4419                                   GCancellable  *cancellable,
4420                                   GError       **error)
4421 {
4422   GFileIface *iface;
4423   GError *my_error;
4424   GFileAttributeInfoList *list;
4425
4426   g_return_val_if_fail (G_IS_FILE (file), NULL);
4427
4428   if (g_cancellable_set_error_if_cancelled (cancellable, error))
4429     return NULL;
4430
4431   iface = G_FILE_GET_IFACE (file);
4432
4433   if (iface->query_settable_attributes == NULL)
4434     return g_file_attribute_info_list_new ();
4435
4436   my_error = NULL;
4437   list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4438
4439   if (list == NULL)
4440     {
4441       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4442         {
4443           list = g_file_attribute_info_list_new ();
4444           g_error_free (my_error);
4445         }
4446       else
4447         g_propagate_error (error, my_error);
4448     }
4449
4450   return list;
4451 }
4452
4453 /**
4454  * g_file_query_writable_namespaces:
4455  * @file: input #GFile
4456  * @cancellable: (nullable): optional #GCancellable object,
4457  *     %NULL to ignore
4458  * @error: a #GError, or %NULL
4459  *
4460  * Obtain the list of attribute namespaces where new attributes
4461  * can be created by a user. An example of this is extended
4462  * attributes (in the "xattr" namespace).
4463  *
4464  * If @cancellable is not %NULL, then the operation can be cancelled by
4465  * triggering the cancellable object from another thread. If the operation
4466  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4467  *
4468  * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4469  *     When you are done with it, release it with
4470  *     g_file_attribute_info_list_unref()
4471  */
4472 GFileAttributeInfoList *
4473 g_file_query_writable_namespaces (GFile         *file,
4474                                   GCancellable  *cancellable,
4475                                   GError       **error)
4476 {
4477   GFileIface *iface;
4478   GError *my_error;
4479   GFileAttributeInfoList *list;
4480
4481   g_return_val_if_fail (G_IS_FILE (file), NULL);
4482
4483   if (g_cancellable_set_error_if_cancelled (cancellable, error))
4484     return NULL;
4485
4486   iface = G_FILE_GET_IFACE (file);
4487
4488   if (iface->query_writable_namespaces == NULL)
4489     return g_file_attribute_info_list_new ();
4490
4491   my_error = NULL;
4492   list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4493
4494   if (list == NULL)
4495     {
4496       g_warn_if_reached();
4497       list = g_file_attribute_info_list_new ();
4498     }
4499
4500   if (my_error != NULL)
4501     {
4502       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4503         {
4504           g_error_free (my_error);
4505         }
4506       else
4507         g_propagate_error (error, my_error);
4508     }
4509
4510   return list;
4511 }
4512
4513 /**
4514  * g_file_set_attribute:
4515  * @file: input #GFile
4516  * @attribute: a string containing the attribute's name
4517  * @type: The type of the attribute
4518  * @value_p: (nullable): a pointer to the value (or the pointer
4519  *     itself if the type is a pointer type)
4520  * @flags: a set of #GFileQueryInfoFlags
4521  * @cancellable: (nullable): optional #GCancellable object,
4522  *     %NULL to ignore
4523  * @error: a #GError, or %NULL
4524  *
4525  * Sets an attribute in the file with attribute name @attribute to @value.
4526  *
4527  * Some attributes can be unset by setting @type to
4528  * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4529  *
4530  * If @cancellable is not %NULL, then the operation can be cancelled by
4531  * triggering the cancellable object from another thread. If the operation
4532  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4533  *
4534  * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4535  */
4536 gboolean
4537 g_file_set_attribute (GFile                *file,
4538                       const gchar          *attribute,
4539                       GFileAttributeType    type,
4540                       gpointer              value_p,
4541                       GFileQueryInfoFlags   flags,
4542                       GCancellable         *cancellable,
4543                       GError              **error)
4544 {
4545   GFileIface *iface;
4546
4547   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4548   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4549
4550   if (g_cancellable_set_error_if_cancelled (cancellable, error))
4551     return FALSE;
4552
4553   iface = G_FILE_GET_IFACE (file);
4554
4555   if (iface->set_attribute == NULL)
4556     {
4557       g_set_error_literal (error, G_IO_ERROR,
4558                            G_IO_ERROR_NOT_SUPPORTED,
4559                            _("Operation not supported"));
4560       return FALSE;
4561     }
4562
4563   return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4564 }
4565
4566 /**
4567  * g_file_set_attributes_from_info:
4568  * @file: input #GFile
4569  * @info: a #GFileInfo
4570  * @flags: #GFileQueryInfoFlags
4571  * @cancellable: (nullable): optional #GCancellable object,
4572  *     %NULL to ignore
4573  * @error: a #GError, or %NULL
4574  *
4575  * Tries to set all attributes in the #GFileInfo on the target
4576  * values, not stopping on the first error.
4577  *
4578  * If there is any error during this operation then @error will
4579  * be set to the first error. Error on particular fields are flagged
4580  * by setting the "status" field in the attribute value to
4581  * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4582  * also detect further errors.
4583  *
4584  * If @cancellable is not %NULL, then the operation can be cancelled by
4585  * triggering the cancellable object from another thread. If the operation
4586  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4587  *
4588  * Returns: %FALSE if there was any error, %TRUE otherwise.
4589  */
4590 gboolean
4591 g_file_set_attributes_from_info (GFile                *file,
4592                                  GFileInfo            *info,
4593                                  GFileQueryInfoFlags   flags,
4594                                  GCancellable         *cancellable,
4595                                  GError              **error)
4596 {
4597   GFileIface *iface;
4598
4599   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4600   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4601
4602   if (g_cancellable_set_error_if_cancelled (cancellable, error))
4603     return FALSE;
4604
4605   g_file_info_clear_status (info);
4606
4607   iface = G_FILE_GET_IFACE (file);
4608
4609   return (* iface->set_attributes_from_info) (file,
4610                                               info,
4611                                               flags,
4612                                               cancellable,
4613                                               error);
4614 }
4615
4616 static gboolean
4617 g_file_real_set_attributes_from_info (GFile                *file,
4618                                       GFileInfo            *info,
4619                                       GFileQueryInfoFlags   flags,
4620                                       GCancellable         *cancellable,
4621                                       GError              **error)
4622 {
4623   char **attributes;
4624   int i;
4625   gboolean res;
4626   GFileAttributeValue *value;
4627
4628   res = TRUE;
4629
4630   attributes = g_file_info_list_attributes (info, NULL);
4631
4632   for (i = 0; attributes[i] != NULL; i++)
4633     {
4634       value = _g_file_info_get_attribute_value (info, attributes[i]);
4635
4636       if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4637         continue;
4638
4639       if (!g_file_set_attribute (file, attributes[i],
4640                                  value->type, _g_file_attribute_value_peek_as_pointer (value),
4641                                  flags, cancellable, error))
4642         {
4643           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4644           res = FALSE;
4645           /* Don't set error multiple times */
4646           error = NULL;
4647         }
4648       else
4649         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4650     }
4651
4652   g_strfreev (attributes);
4653
4654   return res;
4655 }
4656
4657 /**
4658  * g_file_set_attributes_async:
4659  * @file: input #GFile
4660  * @info: a #GFileInfo
4661  * @flags: a #GFileQueryInfoFlags
4662  * @io_priority: the [I/O priority][io-priority] of the request
4663  * @cancellable: (nullable): optional #GCancellable object,
4664  *     %NULL to ignore
4665  * @callback: (scope async): a #GAsyncReadyCallback
4666  * @user_data: (closure): a #gpointer
4667  *
4668  * Asynchronously sets the attributes of @file with @info.
4669  *
4670  * For more details, see g_file_set_attributes_from_info(),
4671  * which is the synchronous version of this call.
4672  *
4673  * When the operation is finished, @callback will be called.
4674  * You can then call g_file_set_attributes_finish() to get
4675  * the result of the operation.
4676  */
4677 void
4678 g_file_set_attributes_async (GFile               *file,
4679                              GFileInfo           *info,
4680                              GFileQueryInfoFlags  flags,
4681                              int                  io_priority,
4682                              GCancellable        *cancellable,
4683                              GAsyncReadyCallback  callback,
4684                              gpointer             user_data)
4685 {
4686   GFileIface *iface;
4687
4688   g_return_if_fail (G_IS_FILE (file));
4689   g_return_if_fail (G_IS_FILE_INFO (info));
4690
4691   iface = G_FILE_GET_IFACE (file);
4692   (* iface->set_attributes_async) (file,
4693                                    info,
4694                                    flags,
4695                                    io_priority,
4696                                    cancellable,
4697                                    callback,
4698                                    user_data);
4699 }
4700
4701 /**
4702  * g_file_set_attributes_finish:
4703  * @file: input #GFile
4704  * @result: a #GAsyncResult
4705  * @info: (out) (transfer full): a #GFileInfo
4706  * @error: a #GError, or %NULL
4707  *
4708  * Finishes setting an attribute started in g_file_set_attributes_async().
4709  *
4710  * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4711  */
4712 gboolean
4713 g_file_set_attributes_finish (GFile         *file,
4714                               GAsyncResult  *result,
4715                               GFileInfo    **info,
4716                               GError       **error)
4717 {
4718   GFileIface *iface;
4719
4720   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4721   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4722
4723   /* No standard handling of errors here, as we must set info even
4724    * on errors
4725    */
4726   iface = G_FILE_GET_IFACE (file);
4727   return (* iface->set_attributes_finish) (file, result, info, error);
4728 }
4729
4730 /**
4731  * g_file_set_attribute_string:
4732  * @file: input #GFile
4733  * @attribute: a string containing the attribute's name
4734  * @value: a string containing the attribute's value
4735  * @flags: #GFileQueryInfoFlags
4736  * @cancellable: (nullable): optional #GCancellable object,
4737  *     %NULL to ignore
4738  * @error: a #GError, or %NULL
4739  *
4740  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4741  * If @attribute is of a different type, this operation will fail.
4742  *
4743  * If @cancellable is not %NULL, then the operation can be cancelled by
4744  * triggering the cancellable object from another thread. If the operation
4745  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4746  *
4747  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4748  */
4749 gboolean
4750 g_file_set_attribute_string (GFile                *file,
4751                              const char           *attribute,
4752                              const char           *value,
4753                              GFileQueryInfoFlags   flags,
4754                              GCancellable         *cancellable,
4755                              GError              **error)
4756 {
4757   return g_file_set_attribute (file, attribute,
4758                                G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4759                                flags, cancellable, error);
4760 }
4761
4762 /**
4763  * g_file_set_attribute_byte_string:
4764  * @file: input #GFile
4765  * @attribute: a string containing the attribute's name
4766  * @value: a string containing the attribute's new value
4767  * @flags: a #GFileQueryInfoFlags
4768  * @cancellable: (nullable): optional #GCancellable object,
4769  *     %NULL to ignore
4770  * @error: a #GError, or %NULL
4771  *
4772  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4773  * If @attribute is of a different type, this operation will fail,
4774  * returning %FALSE.
4775  *
4776  * If @cancellable is not %NULL, then the operation can be cancelled by
4777  * triggering the cancellable object from another thread. If the operation
4778  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4779  *
4780  * Returns: %TRUE if the @attribute was successfully set to @value
4781  *     in the @file, %FALSE otherwise.
4782  */
4783 gboolean
4784 g_file_set_attribute_byte_string  (GFile                *file,
4785                                    const gchar          *attribute,
4786                                    const gchar          *value,
4787                                    GFileQueryInfoFlags   flags,
4788                                    GCancellable         *cancellable,
4789                                    GError              **error)
4790 {
4791   return g_file_set_attribute (file, attribute,
4792                                G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4793                                flags, cancellable, error);
4794 }
4795
4796 /**
4797  * g_file_set_attribute_uint32:
4798  * @file: input #GFile
4799  * @attribute: a string containing the attribute's name
4800  * @value: a #guint32 containing the attribute's new value
4801  * @flags: a #GFileQueryInfoFlags
4802  * @cancellable: (nullable): optional #GCancellable object,
4803  *     %NULL to ignore
4804  * @error: a #GError, or %NULL
4805  *
4806  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4807  * If @attribute is of a different type, this operation will fail.
4808  *
4809  * If @cancellable is not %NULL, then the operation can be cancelled by
4810  * triggering the cancellable object from another thread. If the operation
4811  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4812  *
4813  * Returns: %TRUE if the @attribute was successfully set to @value
4814  *     in the @file, %FALSE otherwise.
4815  */
4816 gboolean
4817 g_file_set_attribute_uint32 (GFile                *file,
4818                              const gchar          *attribute,
4819                              guint32               value,
4820                              GFileQueryInfoFlags   flags,
4821                              GCancellable         *cancellable,
4822                              GError              **error)
4823 {
4824   return g_file_set_attribute (file, attribute,
4825                                G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4826                                flags, cancellable, error);
4827 }
4828
4829 /**
4830  * g_file_set_attribute_int32:
4831  * @file: input #GFile
4832  * @attribute: a string containing the attribute's name
4833  * @value: a #gint32 containing the attribute's new value
4834  * @flags: a #GFileQueryInfoFlags
4835  * @cancellable: (nullable): optional #GCancellable object,
4836  *     %NULL to ignore
4837  * @error: a #GError, or %NULL
4838  *
4839  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4840  * If @attribute is of a different type, this operation will fail.
4841  *
4842  * If @cancellable is not %NULL, then the operation can be cancelled by
4843  * triggering the cancellable object from another thread. If the operation
4844  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4845  *
4846  * Returns: %TRUE if the @attribute was successfully set to @value
4847  *     in the @file, %FALSE otherwise.
4848  */
4849 gboolean
4850 g_file_set_attribute_int32 (GFile                *file,
4851                             const gchar          *attribute,
4852                             gint32                value,
4853                             GFileQueryInfoFlags   flags,
4854                             GCancellable         *cancellable,
4855                             GError              **error)
4856 {
4857   return g_file_set_attribute (file, attribute,
4858                                G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4859                                flags, cancellable, error);
4860 }
4861
4862 /**
4863  * g_file_set_attribute_uint64:
4864  * @file: input #GFile
4865  * @attribute: a string containing the attribute's name
4866  * @value: a #guint64 containing the attribute's new value
4867  * @flags: a #GFileQueryInfoFlags
4868  * @cancellable: (nullable): optional #GCancellable object,
4869  *     %NULL to ignore
4870  * @error: a #GError, or %NULL
4871  *
4872  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4873  * If @attribute is of a different type, this operation will fail.
4874  *
4875  * If @cancellable is not %NULL, then the operation can be cancelled by
4876  * triggering the cancellable object from another thread. If the operation
4877  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4878  *
4879  * Returns: %TRUE if the @attribute was successfully set to @value
4880  *     in the @file, %FALSE otherwise.
4881  */
4882 gboolean
4883 g_file_set_attribute_uint64 (GFile                *file,
4884                              const gchar          *attribute,
4885                              guint64               value,
4886                              GFileQueryInfoFlags   flags,
4887                              GCancellable         *cancellable,
4888                              GError              **error)
4889  {
4890   return g_file_set_attribute (file, attribute,
4891                                G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4892                                flags, cancellable, error);
4893 }
4894
4895 /**
4896  * g_file_set_attribute_int64:
4897  * @file: input #GFile
4898  * @attribute: a string containing the attribute's name
4899  * @value: a #guint64 containing the attribute's new value
4900  * @flags: a #GFileQueryInfoFlags
4901  * @cancellable: (nullable): optional #GCancellable object,
4902  *     %NULL to ignore
4903  * @error: a #GError, or %NULL
4904  *
4905  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4906  * If @attribute is of a different type, this operation will fail.
4907  *
4908  * If @cancellable is not %NULL, then the operation can be cancelled by
4909  * triggering the cancellable object from another thread. If the operation
4910  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4911  *
4912  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4913  */
4914 gboolean
4915 g_file_set_attribute_int64 (GFile                *file,
4916                             const gchar          *attribute,
4917                             gint64                value,
4918                             GFileQueryInfoFlags   flags,
4919                             GCancellable         *cancellable,
4920                             GError              **error)
4921 {
4922   return g_file_set_attribute (file, attribute,
4923                                G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4924                                flags, cancellable, error);
4925 }
4926
4927 /**
4928  * g_file_mount_mountable:
4929  * @file: input #GFile
4930  * @flags: flags affecting the operation
4931  * @mount_operation: (nullable): a #GMountOperation,
4932  *     or %NULL to avoid user interaction
4933  * @cancellable: (nullable): optional #GCancellable object,
4934  *     %NULL to ignore
4935  * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4936  *     when the request is satisfied, or %NULL
4937  * @user_data: (closure): the data to pass to callback function
4938  *
4939  * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4940  * Using @mount_operation, you can request callbacks when, for instance,
4941  * passwords are needed during authentication.
4942  *
4943  * If @cancellable is not %NULL, then the operation can be cancelled by
4944  * triggering the cancellable object from another thread. If the operation
4945  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4946  *
4947  * When the operation is finished, @callback will be called.
4948  * You can then call g_file_mount_mountable_finish() to get
4949  * the result of the operation.
4950  */
4951 void
4952 g_file_mount_mountable (GFile               *file,
4953                         GMountMountFlags     flags,
4954                         GMountOperation     *mount_operation,
4955                         GCancellable        *cancellable,
4956                         GAsyncReadyCallback  callback,
4957                         gpointer             user_data)
4958 {
4959   GFileIface *iface;
4960
4961   g_return_if_fail (G_IS_FILE (file));
4962
4963   iface = G_FILE_GET_IFACE (file);
4964
4965   if (iface->mount_mountable == NULL)
4966     {
4967       g_task_report_new_error (file, callback, user_data,
4968                                g_file_mount_mountable,
4969                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4970                                _("Operation not supported"));
4971       return;
4972     }
4973
4974   (* iface->mount_mountable) (file,
4975                               flags,
4976                               mount_operation,
4977                               cancellable,
4978                               callback,
4979                               user_data);
4980 }
4981
4982 /**
4983  * g_file_mount_mountable_finish:
4984  * @file: input #GFile
4985  * @result: a #GAsyncResult
4986  * @error: a #GError, or %NULL
4987  *
4988  * Finishes a mount operation. See g_file_mount_mountable() for details.
4989  *
4990  * Finish an asynchronous mount operation that was started
4991  * with g_file_mount_mountable().
4992  *
4993  * Returns: (transfer full): a #GFile or %NULL on error.
4994  *     Free the returned object with g_object_unref().
4995  */
4996 GFile *
4997 g_file_mount_mountable_finish (GFile         *file,
4998                                GAsyncResult  *result,
4999                                GError       **error)
5000 {
5001   GFileIface *iface;
5002
5003   g_return_val_if_fail (G_IS_FILE (file), NULL);
5004   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
5005
5006   if (g_async_result_legacy_propagate_error (result, error))
5007     return NULL;
5008   else if (g_async_result_is_tagged (result, g_file_mount_mountable))
5009     return g_task_propagate_pointer (G_TASK (result), error);
5010
5011   iface = G_FILE_GET_IFACE (file);
5012   return (* iface->mount_mountable_finish) (file, result, error);
5013 }
5014
5015 /**
5016  * g_file_unmount_mountable:
5017  * @file: input #GFile
5018  * @flags: flags affecting the operation
5019  * @cancellable: (nullable): optional #GCancellable object,
5020  *     %NULL to ignore
5021  * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5022  *     when the request is satisfied, or %NULL
5023  * @user_data: (closure): the data to pass to callback function
5024  *
5025  * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
5026  *
5027  * If @cancellable is not %NULL, then the operation can be cancelled by
5028  * triggering the cancellable object from another thread. If the operation
5029  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5030  *
5031  * When the operation is finished, @callback will be called.
5032  * You can then call g_file_unmount_mountable_finish() to get
5033  * the result of the operation.
5034  *
5035  * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
5036  */
5037 void
5038 g_file_unmount_mountable (GFile               *file,
5039                           GMountUnmountFlags   flags,
5040                           GCancellable        *cancellable,
5041                           GAsyncReadyCallback  callback,
5042                           gpointer             user_data)
5043 {
5044   GFileIface *iface;
5045
5046   g_return_if_fail (G_IS_FILE (file));
5047
5048   iface = G_FILE_GET_IFACE (file);
5049
5050   if (iface->unmount_mountable == NULL)
5051     {
5052       g_task_report_new_error (file, callback, user_data,
5053                                g_file_unmount_mountable_with_operation,
5054                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5055                                _("Operation not supported"));
5056       return;
5057     }
5058
5059   (* iface->unmount_mountable) (file,
5060                                 flags,
5061                                 cancellable,
5062                                 callback,
5063                                 user_data);
5064 }
5065
5066 /**
5067  * g_file_unmount_mountable_finish:
5068  * @file: input #GFile
5069  * @result: a #GAsyncResult
5070  * @error: a #GError, or %NULL
5071  *
5072  * Finishes an unmount operation, see g_file_unmount_mountable() for details.
5073  *
5074  * Finish an asynchronous unmount operation that was started
5075  * with g_file_unmount_mountable().
5076  *
5077  * Returns: %TRUE if the operation finished successfully.
5078  *     %FALSE otherwise.
5079  *
5080  * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
5081  *     instead.
5082  */
5083 gboolean
5084 g_file_unmount_mountable_finish (GFile         *file,
5085                                  GAsyncResult  *result,
5086                                  GError       **error)
5087 {
5088   GFileIface *iface;
5089
5090   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5091   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5092
5093   if (g_async_result_legacy_propagate_error (result, error))
5094     return FALSE;
5095   else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5096     return g_task_propagate_boolean (G_TASK (result), error);
5097
5098   iface = G_FILE_GET_IFACE (file);
5099   return (* iface->unmount_mountable_finish) (file, result, error);
5100 }
5101
5102 /**
5103  * g_file_unmount_mountable_with_operation:
5104  * @file: input #GFile
5105  * @flags: flags affecting the operation
5106  * @mount_operation: (nullable): a #GMountOperation,
5107  *     or %NULL to avoid user interaction
5108  * @cancellable: (nullable): optional #GCancellable object,
5109  *     %NULL to ignore
5110  * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5111  *     when the request is satisfied, or %NULL
5112  * @user_data: (closure): the data to pass to callback function
5113  *
5114  * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
5115  *
5116  * If @cancellable is not %NULL, then the operation can be cancelled by
5117  * triggering the cancellable object from another thread. If the operation
5118  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5119  *
5120  * When the operation is finished, @callback will be called.
5121  * You can then call g_file_unmount_mountable_finish() to get
5122  * the result of the operation.
5123  *
5124  * Since: 2.22
5125  */
5126 void
5127 g_file_unmount_mountable_with_operation (GFile               *file,
5128                                          GMountUnmountFlags   flags,
5129                                          GMountOperation     *mount_operation,
5130                                          GCancellable        *cancellable,
5131                                          GAsyncReadyCallback  callback,
5132                                          gpointer             user_data)
5133 {
5134   GFileIface *iface;
5135
5136   g_return_if_fail (G_IS_FILE (file));
5137
5138   iface = G_FILE_GET_IFACE (file);
5139
5140   if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
5141     {
5142       g_task_report_new_error (file, callback, user_data,
5143                                g_file_unmount_mountable_with_operation,
5144                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5145                                _("Operation not supported"));
5146       return;
5147     }
5148
5149   if (iface->unmount_mountable_with_operation != NULL)
5150     (* iface->unmount_mountable_with_operation) (file,
5151                                                  flags,
5152                                                  mount_operation,
5153                                                  cancellable,
5154                                                  callback,
5155                                                  user_data);
5156   else
5157     (* iface->unmount_mountable) (file,
5158                                   flags,
5159                                   cancellable,
5160                                   callback,
5161                                   user_data);
5162 }
5163
5164 /**
5165  * g_file_unmount_mountable_with_operation_finish:
5166  * @file: input #GFile
5167  * @result: a #GAsyncResult
5168  * @error: a #GError, or %NULL
5169  *
5170  * Finishes an unmount operation,
5171  * see g_file_unmount_mountable_with_operation() for details.
5172  *
5173  * Finish an asynchronous unmount operation that was started
5174  * with g_file_unmount_mountable_with_operation().
5175  *
5176  * Returns: %TRUE if the operation finished successfully.
5177  *     %FALSE otherwise.
5178  *
5179  * Since: 2.22
5180  */
5181 gboolean
5182 g_file_unmount_mountable_with_operation_finish (GFile         *file,
5183                                                 GAsyncResult  *result,
5184                                                 GError       **error)
5185 {
5186   GFileIface *iface;
5187
5188   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5189   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5190
5191   if (g_async_result_legacy_propagate_error (result, error))
5192     return FALSE;
5193   else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5194     return g_task_propagate_boolean (G_TASK (result), error);
5195
5196   iface = G_FILE_GET_IFACE (file);
5197   if (iface->unmount_mountable_with_operation_finish != NULL)
5198     return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5199   else
5200     return (* iface->unmount_mountable_finish) (file, result, error);
5201 }
5202
5203 /**
5204  * g_file_eject_mountable:
5205  * @file: input #GFile
5206  * @flags: flags affecting the operation
5207  * @cancellable: (nullable): optional #GCancellable object,
5208  *     %NULL to ignore
5209  * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5210  *     when the request is satisfied, or %NULL
5211  * @user_data: (closure): the data to pass to callback function
5212  *
5213  * Starts an asynchronous eject on a mountable.
5214  * When this operation has completed, @callback will be called with
5215  * @user_user data, and the operation can be finalized with
5216  * g_file_eject_mountable_finish().
5217  *
5218  * If @cancellable is not %NULL, then the operation can be cancelled by
5219  * triggering the cancellable object from another thread. If the operation
5220  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5221  *
5222  * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5223  */
5224 void
5225 g_file_eject_mountable (GFile               *file,
5226                         GMountUnmountFlags   flags,
5227                         GCancellable        *cancellable,
5228                         GAsyncReadyCallback  callback,
5229                         gpointer             user_data)
5230 {
5231   GFileIface *iface;
5232
5233   g_return_if_fail (G_IS_FILE (file));
5234
5235   iface = G_FILE_GET_IFACE (file);
5236
5237   if (iface->eject_mountable == NULL)
5238     {
5239       g_task_report_new_error (file, callback, user_data,
5240                                g_file_eject_mountable_with_operation,
5241                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5242                                _("Operation not supported"));
5243       return;
5244     }
5245
5246   (* iface->eject_mountable) (file,
5247                               flags,
5248                               cancellable,
5249                               callback,
5250                               user_data);
5251 }
5252
5253 /**
5254  * g_file_eject_mountable_finish:
5255  * @file: input #GFile
5256  * @result: a #GAsyncResult
5257  * @error: a #GError, or %NULL
5258  *
5259  * Finishes an asynchronous eject operation started by
5260  * g_file_eject_mountable().
5261  *
5262  * Returns: %TRUE if the @file was ejected successfully.
5263  *     %FALSE otherwise.
5264  *
5265  * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5266  *     instead.
5267  */
5268 gboolean
5269 g_file_eject_mountable_finish (GFile         *file,
5270                                GAsyncResult  *result,
5271                                GError       **error)
5272 {
5273   GFileIface *iface;
5274
5275   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5276   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5277
5278   if (g_async_result_legacy_propagate_error (result, error))
5279     return FALSE;
5280   else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5281     return g_task_propagate_boolean (G_TASK (result), error);
5282
5283   iface = G_FILE_GET_IFACE (file);
5284   return (* iface->eject_mountable_finish) (file, result, error);
5285 }
5286
5287 /**
5288  * g_file_eject_mountable_with_operation:
5289  * @file: input #GFile
5290  * @flags: flags affecting the operation
5291  * @mount_operation: (nullable): a #GMountOperation,
5292  *     or %NULL to avoid user interaction
5293  * @cancellable: (nullable): optional #GCancellable object,
5294  *     %NULL to ignore
5295  * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5296  *     when the request is satisfied, or %NULL
5297  * @user_data: (closure): the data to pass to callback function
5298  *
5299  * Starts an asynchronous eject on a mountable.
5300  * When this operation has completed, @callback will be called with
5301  * @user_user data, and the operation can be finalized with
5302  * g_file_eject_mountable_with_operation_finish().
5303  *
5304  * If @cancellable is not %NULL, then the operation can be cancelled by
5305  * triggering the cancellable object from another thread. If the operation
5306  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5307  *
5308  * Since: 2.22
5309  */
5310 void
5311 g_file_eject_mountable_with_operation (GFile               *file,
5312                                        GMountUnmountFlags   flags,
5313                                        GMountOperation     *mount_operation,
5314                                        GCancellable        *cancellable,
5315                                        GAsyncReadyCallback  callback,
5316                                        gpointer             user_data)
5317 {
5318   GFileIface *iface;
5319
5320   g_return_if_fail (G_IS_FILE (file));
5321
5322   iface = G_FILE_GET_IFACE (file);
5323
5324   if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5325     {
5326       g_task_report_new_error (file, callback, user_data,
5327                                g_file_eject_mountable_with_operation,
5328                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5329                                _("Operation not supported"));
5330       return;
5331     }
5332
5333   if (iface->eject_mountable_with_operation != NULL)
5334     (* iface->eject_mountable_with_operation) (file,
5335                                                flags,
5336                                                mount_operation,
5337                                                cancellable,
5338                                                callback,
5339                                                user_data);
5340   else
5341     (* iface->eject_mountable) (file,
5342                                 flags,
5343                                 cancellable,
5344                                 callback,
5345                                 user_data);
5346 }
5347
5348 /**
5349  * g_file_eject_mountable_with_operation_finish:
5350  * @file: input #GFile
5351  * @result: a #GAsyncResult
5352  * @error: a #GError, or %NULL
5353  *
5354  * Finishes an asynchronous eject operation started by
5355  * g_file_eject_mountable_with_operation().
5356  *
5357  * Returns: %TRUE if the @file was ejected successfully.
5358  *     %FALSE otherwise.
5359  *
5360  * Since: 2.22
5361  */
5362 gboolean
5363 g_file_eject_mountable_with_operation_finish (GFile         *file,
5364                                               GAsyncResult  *result,
5365                                               GError       **error)
5366 {
5367   GFileIface *iface;
5368
5369   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5370   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5371
5372   if (g_async_result_legacy_propagate_error (result, error))
5373     return FALSE;
5374   else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5375     return g_task_propagate_boolean (G_TASK (result), error);
5376
5377   iface = G_FILE_GET_IFACE (file);
5378   if (iface->eject_mountable_with_operation_finish != NULL)
5379     return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5380   else
5381     return (* iface->eject_mountable_finish) (file, result, error);
5382 }
5383
5384 /**
5385  * g_file_monitor_directory:
5386  * @file: input #GFile
5387  * @flags: a set of #GFileMonitorFlags
5388  * @cancellable: (nullable): optional #GCancellable object,
5389  *     %NULL to ignore
5390  * @error: a #GError, or %NULL
5391  *
5392  * Obtains a directory monitor for the given file.
5393  * This may fail if directory monitoring is not supported.
5394  *
5395  * If @cancellable is not %NULL, then the operation can be cancelled by
5396  * triggering the cancellable object from another thread. If the operation
5397  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5398  *
5399  * It does not make sense for @flags to contain
5400  * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5401  * directories.  It is not possible to monitor all the files in a
5402  * directory for changes made via hard links; if you want to do this then
5403  * you must register individual watches with g_file_monitor().
5404  *
5405  * Virtual: monitor_dir
5406  * Returns: (transfer full): a #GFileMonitor for the given @file,
5407  *     or %NULL on error.
5408  *     Free the returned object with g_object_unref().
5409  */
5410 GFileMonitor *
5411 g_file_monitor_directory (GFile              *file,
5412                           GFileMonitorFlags   flags,
5413                           GCancellable       *cancellable,
5414                           GError            **error)
5415 {
5416   GFileIface *iface;
5417
5418   g_return_val_if_fail (G_IS_FILE (file), NULL);
5419   g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5420
5421   if (g_cancellable_set_error_if_cancelled (cancellable, error))
5422     return NULL;
5423
5424   iface = G_FILE_GET_IFACE (file);
5425
5426   if (iface->monitor_dir == NULL)
5427     {
5428       g_set_error_literal (error, G_IO_ERROR,
5429                            G_IO_ERROR_NOT_SUPPORTED,
5430                            _("Operation not supported"));
5431       return NULL;
5432     }
5433
5434   return (* iface->monitor_dir) (file, flags, cancellable, error);
5435 }
5436
5437 /**
5438  * g_file_monitor_file:
5439  * @file: input #GFile
5440  * @flags: a set of #GFileMonitorFlags
5441  * @cancellable: (nullable): optional #GCancellable object,
5442  *     %NULL to ignore
5443  * @error: a #GError, or %NULL
5444  *
5445  * Obtains a file monitor for the given file. If no file notification
5446  * mechanism exists, then regular polling of the file is used.
5447  *
5448  * If @cancellable is not %NULL, then the operation can be cancelled by
5449  * triggering the cancellable object from another thread. If the operation
5450  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5451  *
5452  * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5453  * will also attempt to report changes made to the file via another
5454  * filename (ie, a hard link). Without this flag, you can only rely on
5455  * changes made through the filename contained in @file to be
5456  * reported. Using this flag may result in an increase in resource
5457  * usage, and may not have any effect depending on the #GFileMonitor
5458  * backend and/or filesystem type.
5459  * 
5460  * Returns: (transfer full): a #GFileMonitor for the given @file,
5461  *     or %NULL on error.
5462  *     Free the returned object with g_object_unref().
5463  */
5464 GFileMonitor *
5465 g_file_monitor_file (GFile              *file,
5466                      GFileMonitorFlags   flags,
5467                      GCancellable       *cancellable,
5468                      GError            **error)
5469 {
5470   GFileIface *iface;
5471   GFileMonitor *monitor;
5472
5473   g_return_val_if_fail (G_IS_FILE (file), NULL);
5474
5475   if (g_cancellable_set_error_if_cancelled (cancellable, error))
5476     return NULL;
5477
5478   iface = G_FILE_GET_IFACE (file);
5479
5480   monitor = NULL;
5481
5482   if (iface->monitor_file)
5483     monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5484
5485   /* Fallback to polling */
5486   if (monitor == NULL)
5487     monitor = _g_poll_file_monitor_new (file);
5488
5489   return monitor;
5490 }
5491
5492 /**
5493  * g_file_monitor:
5494  * @file: input #GFile
5495  * @flags: a set of #GFileMonitorFlags
5496  * @cancellable: (nullable): optional #GCancellable object,
5497  *     %NULL to ignore
5498  * @error: a #GError, or %NULL
5499  *
5500  * Obtains a file or directory monitor for the given file,
5501  * depending on the type of the file.
5502  *
5503  * If @cancellable is not %NULL, then the operation can be cancelled by
5504  * triggering the cancellable object from another thread. If the operation
5505  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5506  *
5507  * Returns: (transfer full): a #GFileMonitor for the given @file,
5508  *     or %NULL on error.
5509  *     Free the returned object with g_object_unref().
5510  *
5511  * Since: 2.18
5512  */
5513 GFileMonitor *
5514 g_file_monitor (GFile              *file,
5515                 GFileMonitorFlags   flags,
5516                 GCancellable       *cancellable,
5517                 GError            **error)
5518 {
5519   if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5520     return g_file_monitor_directory (file,
5521                                      flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5522                                      cancellable, error);
5523   else
5524     return g_file_monitor_file (file, flags, cancellable, error);
5525 }
5526
5527 /********************************************
5528  *   Default implementation of async ops    *
5529  ********************************************/
5530
5531 typedef struct {
5532   char *attributes;
5533   GFileQueryInfoFlags flags;
5534 } QueryInfoAsyncData;
5535
5536 static void
5537 query_info_data_free (QueryInfoAsyncData *data)
5538 {
5539   g_free (data->attributes);
5540   g_free (data);
5541 }
5542
5543 static void
5544 query_info_async_thread (GTask         *task,
5545                          gpointer       object,
5546                          gpointer       task_data,
5547                          GCancellable  *cancellable)
5548 {
5549   QueryInfoAsyncData *data = task_data;
5550   GFileInfo *info;
5551   GError *error = NULL;
5552
5553   info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5554   if (info)
5555     g_task_return_pointer (task, info, g_object_unref);
5556   else
5557     g_task_return_error (task, error);
5558 }
5559
5560 static void
5561 g_file_real_query_info_async (GFile               *file,
5562                               const char          *attributes,
5563                               GFileQueryInfoFlags  flags,
5564                               int                  io_priority,
5565                               GCancellable        *cancellable,
5566                               GAsyncReadyCallback  callback,
5567                               gpointer             user_data)
5568 {
5569   GTask *task;
5570   QueryInfoAsyncData *data;
5571
5572   data = g_new0 (QueryInfoAsyncData, 1);
5573   data->attributes = g_strdup (attributes);
5574   data->flags = flags;
5575
5576   task = g_task_new (file, cancellable, callback, user_data);
5577   g_task_set_source_tag (task, g_file_real_query_info_async);
5578   g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5579   g_task_set_priority (task, io_priority);
5580   g_task_run_in_thread (task, query_info_async_thread);
5581   g_object_unref (task);
5582 }
5583
5584 static GFileInfo *
5585 g_file_real_query_info_finish (GFile         *file,
5586                                GAsyncResult  *res,
5587                                GError       **error)
5588 {
5589   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5590
5591   return g_task_propagate_pointer (G_TASK (res), error);
5592 }
5593
5594 static void
5595 query_filesystem_info_async_thread (GTask         *task,
5596                                     gpointer       object,
5597                                     gpointer       task_data,
5598                                     GCancellable  *cancellable)
5599 {
5600   const char *attributes = task_data;
5601   GFileInfo *info;
5602   GError *error = NULL;
5603
5604   info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5605   if (info)
5606     g_task_return_pointer (task, info, g_object_unref);
5607   else
5608     g_task_return_error (task, error);
5609 }
5610
5611 static void
5612 g_file_real_query_filesystem_info_async (GFile               *file,
5613                                          const char          *attributes,
5614                                          int                  io_priority,
5615                                          GCancellable        *cancellable,
5616                                          GAsyncReadyCallback  callback,
5617                                          gpointer             user_data)
5618 {
5619   GTask *task;
5620
5621   task = g_task_new (file, cancellable, callback, user_data);
5622   g_task_set_source_tag (task, g_file_real_query_filesystem_info_async);
5623   g_task_set_task_data (task, g_strdup (attributes), g_free);
5624   g_task_set_priority (task, io_priority);
5625   g_task_run_in_thread (task, query_filesystem_info_async_thread);
5626   g_object_unref (task);
5627 }
5628
5629 static GFileInfo *
5630 g_file_real_query_filesystem_info_finish (GFile         *file,
5631                                           GAsyncResult  *res,
5632                                           GError       **error)
5633 {
5634   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5635
5636   return g_task_propagate_pointer (G_TASK (res), error);
5637 }
5638
5639 static void
5640 enumerate_children_async_thread (GTask         *task,
5641                                  gpointer       object,
5642                                  gpointer       task_data,
5643                                  GCancellable  *cancellable)
5644 {
5645   QueryInfoAsyncData *data = task_data;
5646   GFileEnumerator *enumerator;
5647   GError *error = NULL;
5648
5649   enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5650   if (error)
5651     g_task_return_error (task, error);
5652   else
5653     g_task_return_pointer (task, enumerator, g_object_unref);
5654 }
5655
5656 static void
5657 g_file_real_enumerate_children_async (GFile               *file,
5658                                       const char          *attributes,
5659                                       GFileQueryInfoFlags  flags,
5660                                       int                  io_priority,
5661                                       GCancellable        *cancellable,
5662                                       GAsyncReadyCallback  callback,
5663                                       gpointer             user_data)
5664 {
5665   GTask *task;
5666   QueryInfoAsyncData *data;
5667
5668   data = g_new0 (QueryInfoAsyncData, 1);
5669   data->attributes = g_strdup (attributes);
5670   data->flags = flags;
5671
5672   task = g_task_new (file, cancellable, callback, user_data);
5673   g_task_set_source_tag (task, g_file_real_enumerate_children_async);
5674   g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5675   g_task_set_priority (task, io_priority);
5676   g_task_run_in_thread (task, enumerate_children_async_thread);
5677   g_object_unref (task);
5678 }
5679
5680 static GFileEnumerator *
5681 g_file_real_enumerate_children_finish (GFile         *file,
5682                                        GAsyncResult  *res,
5683                                        GError       **error)
5684 {
5685   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5686
5687   return g_task_propagate_pointer (G_TASK (res), error);
5688 }
5689
5690 static void
5691 open_read_async_thread (GTask         *task,
5692                         gpointer       object,
5693                         gpointer       task_data,
5694                         GCancellable  *cancellable)
5695 {
5696   GFileInputStream *stream;
5697   GError *error = NULL;
5698
5699   stream = g_file_read (G_FILE (object), cancellable, &error);
5700   if (stream)
5701     g_task_return_pointer (task, stream, g_object_unref);
5702   else
5703     g_task_return_error (task, error);
5704 }
5705
5706 static void
5707 g_file_real_read_async (GFile               *file,
5708                         int                  io_priority,
5709                         GCancellable        *cancellable,
5710                         GAsyncReadyCallback  callback,
5711                         gpointer             user_data)
5712 {
5713   GTask *task;
5714
5715   task = g_task_new (file, cancellable, callback, user_data);
5716   g_task_set_source_tag (task, g_file_real_read_async);
5717   g_task_set_priority (task, io_priority);
5718   g_task_run_in_thread (task, open_read_async_thread);
5719   g_object_unref (task);
5720 }
5721
5722 static GFileInputStream *
5723 g_file_real_read_finish (GFile         *file,
5724                          GAsyncResult  *res,
5725                          GError       **error)
5726 {
5727   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5728
5729   return g_task_propagate_pointer (G_TASK (res), error);
5730 }
5731
5732 static void
5733 append_to_async_thread (GTask         *task,
5734                         gpointer       source_object,
5735                         gpointer       task_data,
5736                         GCancellable  *cancellable)
5737 {
5738   GFileCreateFlags *data = task_data;
5739   GFileOutputStream *stream;
5740   GError *error = NULL;
5741
5742   stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5743   if (stream)
5744     g_task_return_pointer (task, stream, g_object_unref);
5745   else
5746     g_task_return_error (task, error);
5747 }
5748
5749 static void
5750 g_file_real_append_to_async (GFile               *file,
5751                              GFileCreateFlags     flags,
5752                              int                  io_priority,
5753                              GCancellable        *cancellable,
5754                              GAsyncReadyCallback  callback,
5755                              gpointer             user_data)
5756 {
5757   GFileCreateFlags *data;
5758   GTask *task;
5759
5760   data = g_new0 (GFileCreateFlags, 1);
5761   *data = flags;
5762
5763   task = g_task_new (file, cancellable, callback, user_data);
5764   g_task_set_source_tag (task, g_file_real_append_to_async);
5765   g_task_set_task_data (task, data, g_free);
5766   g_task_set_priority (task, io_priority);
5767
5768   g_task_run_in_thread (task, append_to_async_thread);
5769   g_object_unref (task);
5770 }
5771
5772 static GFileOutputStream *
5773 g_file_real_append_to_finish (GFile         *file,
5774                               GAsyncResult  *res,
5775                               GError       **error)
5776 {
5777   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5778
5779   return g_task_propagate_pointer (G_TASK (res), error);
5780 }
5781
5782 static void
5783 create_async_thread (GTask         *task,
5784                      gpointer       source_object,
5785                      gpointer       task_data,
5786                      GCancellable  *cancellable)
5787 {
5788   GFileCreateFlags *data = task_data;
5789   GFileOutputStream *stream;
5790   GError *error = NULL;
5791
5792   stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5793   if (stream)
5794     g_task_return_pointer (task, stream, g_object_unref);
5795   else
5796     g_task_return_error (task, error);
5797 }
5798
5799 static void
5800 g_file_real_create_async (GFile               *file,
5801                           GFileCreateFlags     flags,
5802                           int                  io_priority,
5803                           GCancellable        *cancellable,
5804                           GAsyncReadyCallback  callback,
5805                           gpointer             user_data)
5806 {
5807   GFileCreateFlags *data;
5808   GTask *task;
5809
5810   data = g_new0 (GFileCreateFlags, 1);
5811   *data = flags;
5812
5813   task = g_task_new (file, cancellable, callback, user_data);
5814   g_task_set_source_tag (task, g_file_real_create_async);
5815   g_task_set_task_data (task, data, g_free);
5816   g_task_set_priority (task, io_priority);
5817
5818   g_task_run_in_thread (task, create_async_thread);
5819   g_object_unref (task);
5820 }
5821
5822 static GFileOutputStream *
5823 g_file_real_create_finish (GFile         *file,
5824                            GAsyncResult  *res,
5825                            GError       **error)
5826 {
5827   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5828
5829   return g_task_propagate_pointer (G_TASK (res), error);
5830 }
5831
5832 typedef struct {
5833   GFileOutputStream *stream;
5834   char *etag;
5835   gboolean make_backup;
5836   GFileCreateFlags flags;
5837 } ReplaceAsyncData;
5838
5839 static void
5840 replace_async_data_free (ReplaceAsyncData *data)
5841 {
5842   if (data->stream)
5843     g_object_unref (data->stream);
5844   g_free (data->etag);
5845   g_free (data);
5846 }
5847
5848 static void
5849 replace_async_thread (GTask         *task,
5850                       gpointer       source_object,
5851                       gpointer       task_data,
5852                       GCancellable  *cancellable)
5853 {
5854   GFileOutputStream *stream;
5855   ReplaceAsyncData *data = task_data;
5856   GError *error = NULL;
5857
5858   stream = g_file_replace (G_FILE (source_object),
5859                            data->etag,
5860                            data->make_backup,
5861                            data->flags,
5862                            cancellable,
5863                            &error);
5864
5865   if (stream)
5866     g_task_return_pointer (task, stream, g_object_unref);
5867   else
5868     g_task_return_error (task, error);
5869 }
5870
5871 static void
5872 g_file_real_replace_async (GFile               *file,
5873                            const char          *etag,
5874                            gboolean             make_backup,
5875                            GFileCreateFlags     flags,
5876                            int                  io_priority,
5877                            GCancellable        *cancellable,
5878                            GAsyncReadyCallback  callback,
5879                            gpointer             user_data)
5880 {
5881   GTask *task;
5882   ReplaceAsyncData *data;
5883
5884   data = g_new0 (ReplaceAsyncData, 1);
5885   data->etag = g_strdup (etag);
5886   data->make_backup = make_backup;
5887   data->flags = flags;
5888
5889   task = g_task_new (file, cancellable, callback, user_data);
5890   g_task_set_source_tag (task, g_file_real_replace_async);
5891   g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5892   g_task_set_priority (task, io_priority);
5893
5894   g_task_run_in_thread (task, replace_async_thread);
5895   g_object_unref (task);
5896 }
5897
5898 static GFileOutputStream *
5899 g_file_real_replace_finish (GFile         *file,
5900                             GAsyncResult  *res,
5901                             GError       **error)
5902 {
5903   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5904
5905   return g_task_propagate_pointer (G_TASK (res), error);
5906 }
5907
5908 static void
5909 delete_async_thread (GTask        *task,
5910                      gpointer      object,
5911                      gpointer      task_data,
5912                      GCancellable *cancellable)
5913 {
5914   GError *error = NULL;
5915
5916   if (g_file_delete (G_FILE (object), cancellable, &error))
5917     g_task_return_boolean (task, TRUE);
5918   else
5919     g_task_return_error (task, error);
5920 }
5921
5922 static void
5923 g_file_real_delete_async (GFile               *file,
5924                           int                  io_priority,
5925                           GCancellable        *cancellable,
5926                           GAsyncReadyCallback  callback,
5927                           gpointer             user_data)
5928 {
5929   GTask *task;
5930
5931   task = g_task_new (file, cancellable, callback, user_data);
5932   g_task_set_source_tag (task, g_file_real_delete_async);
5933   g_task_set_priority (task, io_priority);
5934   g_task_run_in_thread (task, delete_async_thread);
5935   g_object_unref (task);
5936 }
5937
5938 static gboolean
5939 g_file_real_delete_finish (GFile         *file,
5940                            GAsyncResult  *res,
5941                            GError       **error)
5942 {
5943   g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5944
5945   return g_task_propagate_boolean (G_TASK (res), error);
5946 }
5947
5948 static void
5949 trash_async_thread (GTask        *task,
5950                     gpointer      object,
5951                     gpointer      task_data,
5952                     GCancellable *cancellable)
5953 {
5954   GError *error = NULL;
5955
5956   if (g_file_trash (G_FILE (object), cancellable, &error))
5957     g_task_return_boolean (task, TRUE);
5958   else
5959     g_task_return_error (task, error);
5960 }
5961
5962 static void
5963 g_file_real_trash_async (GFile               *file,
5964                          int                  io_priority,
5965                          GCancellable        *cancellable,
5966                          GAsyncReadyCallback  callback,
5967                          gpointer             user_data)
5968 {
5969   GTask *task;
5970
5971   task = g_task_new (file, cancellable, callback, user_data);
5972   g_task_set_source_tag (task, g_file_real_trash_async);
5973   g_task_set_priority (task, io_priority);
5974   g_task_run_in_thread (task, trash_async_thread);
5975   g_object_unref (task);
5976 }
5977
5978 static gboolean
5979 g_file_real_trash_finish (GFile         *file,
5980                           GAsyncResult  *res,
5981                           GError       **error)
5982 {
5983   g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5984
5985   return g_task_propagate_boolean (G_TASK (res), error);
5986 }
5987
5988 static void
5989 make_directory_async_thread (GTask        *task,
5990                              gpointer      object,
5991                              gpointer      task_data,
5992                              GCancellable *cancellable)
5993 {
5994   GError *error = NULL;
5995
5996   if (g_file_make_directory (G_FILE (object), cancellable, &error))
5997     g_task_return_boolean (task, TRUE);
5998   else
5999     g_task_return_error (task, error);
6000 }
6001
6002 static void
6003 g_file_real_make_directory_async (GFile               *file,
6004                                   int                  io_priority,
6005                                   GCancellable        *cancellable,
6006                                   GAsyncReadyCallback  callback,
6007                                   gpointer             user_data)
6008 {
6009   GTask *task;
6010
6011   task = g_task_new (file, cancellable, callback, user_data);
6012   g_task_set_source_tag (task, g_file_real_make_directory_async);
6013   g_task_set_priority (task, io_priority);
6014   g_task_run_in_thread (task, make_directory_async_thread);
6015   g_object_unref (task);
6016 }
6017
6018 static gboolean
6019 g_file_real_make_directory_finish (GFile         *file,
6020                                    GAsyncResult  *res,
6021                                    GError       **error)
6022 {
6023   g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6024
6025   return g_task_propagate_boolean (G_TASK (res), error);
6026 }
6027
6028 static void
6029 open_readwrite_async_thread (GTask        *task,
6030                              gpointer      object,
6031                              gpointer      task_data,
6032                              GCancellable *cancellable)
6033 {
6034   GFileIOStream *stream;
6035   GError *error = NULL;
6036
6037   stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
6038
6039   if (stream == NULL)
6040     g_task_return_error (task, error);
6041   else
6042     g_task_return_pointer (task, stream, g_object_unref);
6043 }
6044
6045 static void
6046 g_file_real_open_readwrite_async (GFile               *file,
6047                                   int                  io_priority,
6048                                   GCancellable        *cancellable,
6049                                   GAsyncReadyCallback  callback,
6050                                   gpointer             user_data)
6051 {
6052   GTask *task;
6053
6054   task = g_task_new (file, cancellable, callback, user_data);
6055   g_task_set_source_tag (task, g_file_real_open_readwrite_async);
6056   g_task_set_priority (task, io_priority);
6057
6058   g_task_run_in_thread (task, open_readwrite_async_thread);
6059   g_object_unref (task);
6060 }
6061
6062 static GFileIOStream *
6063 g_file_real_open_readwrite_finish (GFile         *file,
6064                                    GAsyncResult  *res,
6065                                    GError       **error)
6066 {
6067   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6068
6069   return g_task_propagate_pointer (G_TASK (res), error);
6070 }
6071
6072 static void
6073 create_readwrite_async_thread (GTask        *task,
6074                                gpointer      object,
6075                                gpointer      task_data,
6076                                GCancellable *cancellable)
6077 {
6078   GFileCreateFlags *data = task_data;
6079   GFileIOStream *stream;
6080   GError *error = NULL;
6081
6082   stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
6083
6084   if (stream == NULL)
6085     g_task_return_error (task, error);
6086   else
6087     g_task_return_pointer (task, stream, g_object_unref);
6088 }
6089
6090 static void
6091 g_file_real_create_readwrite_async (GFile               *file,
6092                                     GFileCreateFlags     flags,
6093                                     int                  io_priority,
6094                                     GCancellable        *cancellable,
6095                                     GAsyncReadyCallback  callback,
6096                                     gpointer             user_data)
6097 {
6098   GFileCreateFlags *data;
6099   GTask *task;
6100
6101   data = g_new0 (GFileCreateFlags, 1);
6102   *data = flags;
6103
6104   task = g_task_new (file, cancellable, callback, user_data);
6105   g_task_set_source_tag (task, g_file_real_create_readwrite_async);
6106   g_task_set_task_data (task, data, g_free);
6107   g_task_set_priority (task, io_priority);
6108
6109   g_task_run_in_thread (task, create_readwrite_async_thread);
6110   g_object_unref (task);
6111 }
6112
6113 static GFileIOStream *
6114 g_file_real_create_readwrite_finish (GFile         *file,
6115                                      GAsyncResult  *res,
6116                                      GError       **error)
6117 {
6118   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6119
6120   return g_task_propagate_pointer (G_TASK (res), error);
6121 }
6122
6123 typedef struct {
6124   char *etag;
6125   gboolean make_backup;
6126   GFileCreateFlags flags;
6127 } ReplaceRWAsyncData;
6128
6129 static void
6130 replace_rw_async_data_free (ReplaceRWAsyncData *data)
6131 {
6132   g_free (data->etag);
6133   g_free (data);
6134 }
6135
6136 static void
6137 replace_readwrite_async_thread (GTask        *task,
6138                                 gpointer      object,
6139                                 gpointer      task_data,
6140                                 GCancellable *cancellable)
6141 {
6142   GFileIOStream *stream;
6143   GError *error = NULL;
6144   ReplaceRWAsyncData *data = task_data;
6145
6146   stream = g_file_replace_readwrite (G_FILE (object),
6147                                      data->etag,
6148                                      data->make_backup,
6149                                      data->flags,
6150                                      cancellable,
6151                                      &error);
6152
6153   if (stream == NULL)
6154     g_task_return_error (task, error);
6155   else
6156     g_task_return_pointer (task, stream, g_object_unref);
6157 }
6158
6159 static void
6160 g_file_real_replace_readwrite_async (GFile               *file,
6161                                      const char          *etag,
6162                                      gboolean             make_backup,
6163                                      GFileCreateFlags     flags,
6164                                      int                  io_priority,
6165                                      GCancellable        *cancellable,
6166                                      GAsyncReadyCallback  callback,
6167                                      gpointer             user_data)
6168 {
6169   GTask *task;
6170   ReplaceRWAsyncData *data;
6171
6172   data = g_new0 (ReplaceRWAsyncData, 1);
6173   data->etag = g_strdup (etag);
6174   data->make_backup = make_backup;
6175   data->flags = flags;
6176
6177   task = g_task_new (file, cancellable, callback, user_data);
6178   g_task_set_source_tag (task, g_file_real_replace_readwrite_async);
6179   g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
6180   g_task_set_priority (task, io_priority);
6181
6182   g_task_run_in_thread (task, replace_readwrite_async_thread);
6183   g_object_unref (task);
6184 }
6185
6186 static GFileIOStream *
6187 g_file_real_replace_readwrite_finish (GFile         *file,
6188                                       GAsyncResult  *res,
6189                                       GError       **error)
6190 {
6191   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6192
6193   return g_task_propagate_pointer (G_TASK (res), error);
6194 }
6195
6196 static void
6197 set_display_name_async_thread (GTask        *task,
6198                                gpointer      object,
6199                                gpointer      task_data,
6200                                GCancellable *cancellable)
6201 {
6202   GError *error = NULL;
6203   char *name = task_data;
6204   GFile *file;
6205
6206   file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6207
6208   if (file == NULL)
6209     g_task_return_error (task, error);
6210   else
6211     g_task_return_pointer (task, file, g_object_unref);
6212 }
6213
6214 static void
6215 g_file_real_set_display_name_async (GFile               *file,
6216                                     const char          *display_name,
6217                                     int                  io_priority,
6218                                     GCancellable        *cancellable,
6219                                     GAsyncReadyCallback  callback,
6220                                     gpointer             user_data)
6221 {
6222   GTask *task;
6223
6224   task = g_task_new (file, cancellable, callback, user_data);
6225   g_task_set_source_tag (task, g_file_real_set_display_name_async);
6226   g_task_set_task_data (task, g_strdup (display_name), g_free);
6227   g_task_set_priority (task, io_priority);
6228
6229   g_task_run_in_thread (task, set_display_name_async_thread);
6230   g_object_unref (task);
6231 }
6232
6233 static GFile *
6234 g_file_real_set_display_name_finish (GFile         *file,
6235                                      GAsyncResult  *res,
6236                                      GError       **error)
6237 {
6238   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6239
6240   return g_task_propagate_pointer (G_TASK (res), error);
6241 }
6242
6243 typedef struct {
6244   GFileQueryInfoFlags flags;
6245   GFileInfo *info;
6246   gboolean res;
6247   GError *error;
6248 } SetInfoAsyncData;
6249
6250 static void
6251 set_info_data_free (SetInfoAsyncData *data)
6252 {
6253   if (data->info)
6254     g_object_unref (data->info);
6255   if (data->error)
6256     g_error_free (data->error);
6257   g_free (data);
6258 }
6259
6260 static void
6261 set_info_async_thread (GTask        *task,
6262                        gpointer      object,
6263                        gpointer      task_data,
6264                        GCancellable *cancellable)
6265 {
6266   SetInfoAsyncData *data = task_data;
6267
6268   data->error = NULL;
6269   data->res = g_file_set_attributes_from_info (G_FILE (object),
6270                                                data->info,
6271                                                data->flags,
6272                                                cancellable,
6273                                                &data->error);
6274 }
6275
6276 static void
6277 g_file_real_set_attributes_async (GFile               *file,
6278                                   GFileInfo           *info,
6279                                   GFileQueryInfoFlags  flags,
6280                                   int                  io_priority,
6281                                   GCancellable        *cancellable,
6282                                   GAsyncReadyCallback  callback,
6283                                   gpointer             user_data)
6284 {
6285   GTask *task;
6286   SetInfoAsyncData *data;
6287
6288   data = g_new0 (SetInfoAsyncData, 1);
6289   data->info = g_file_info_dup (info);
6290   data->flags = flags;
6291
6292   task = g_task_new (file, cancellable, callback, user_data);
6293   g_task_set_source_tag (task, g_file_real_set_attributes_async);
6294   g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6295   g_task_set_priority (task, io_priority);
6296
6297   g_task_run_in_thread (task, set_info_async_thread);
6298   g_object_unref (task);
6299 }
6300
6301 static gboolean
6302 g_file_real_set_attributes_finish (GFile         *file,
6303                                    GAsyncResult  *res,
6304                                    GFileInfo    **info,
6305                                    GError       **error)
6306 {
6307   SetInfoAsyncData *data;
6308
6309   g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6310
6311   data = g_task_get_task_data (G_TASK (res));
6312
6313   if (info)
6314     *info = g_object_ref (data->info);
6315
6316   if (error != NULL && data->error)
6317     *error = g_error_copy (data->error);
6318
6319   return data->res;
6320 }
6321
6322 static void
6323 find_enclosing_mount_async_thread (GTask        *task,
6324                                    gpointer      object,
6325                                    gpointer      task_data,
6326                                    GCancellable *cancellable)
6327 {
6328   GError *error = NULL;
6329   GMount *mount;
6330
6331   mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6332
6333   if (mount == NULL)
6334     g_task_return_error (task, error);
6335   else
6336     g_task_return_pointer (task, mount, g_object_unref);
6337 }
6338
6339 static void
6340 g_file_real_find_enclosing_mount_async (GFile               *file,
6341                                         int                  io_priority,
6342                                         GCancellable        *cancellable,
6343                                         GAsyncReadyCallback  callback,
6344                                         gpointer             user_data)
6345 {
6346   GTask *task;
6347
6348   task = g_task_new (file, cancellable, callback, user_data);
6349   g_task_set_source_tag (task, g_file_real_find_enclosing_mount_async);
6350   g_task_set_priority (task, io_priority);
6351
6352   g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6353   g_object_unref (task);
6354 }
6355
6356 static GMount *
6357 g_file_real_find_enclosing_mount_finish (GFile         *file,
6358                                          GAsyncResult  *res,
6359                                          GError       **error)
6360 {
6361   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6362
6363   return g_task_propagate_pointer (G_TASK (res), error);
6364 }
6365
6366
6367 typedef struct {
6368   GFile *source;
6369   GFile *destination;
6370   GFileCopyFlags flags;
6371   GFileProgressCallback progress_cb;
6372   gpointer progress_cb_data;
6373 } CopyAsyncData;
6374
6375 static void
6376 copy_async_data_free (CopyAsyncData *data)
6377 {
6378   g_object_unref (data->source);
6379   g_object_unref (data->destination);
6380   g_slice_free (CopyAsyncData, data);
6381 }
6382
6383 typedef struct {
6384   CopyAsyncData *data;
6385   goffset current_num_bytes;
6386   goffset total_num_bytes;
6387 } ProgressData;
6388
6389 static gboolean
6390 copy_async_progress_in_main (gpointer user_data)
6391 {
6392   ProgressData *progress = user_data;
6393   CopyAsyncData *data = progress->data;
6394
6395   data->progress_cb (progress->current_num_bytes,
6396                      progress->total_num_bytes,
6397                      data->progress_cb_data);
6398
6399   return FALSE;
6400 }
6401
6402 static void
6403 copy_async_progress_callback (goffset  current_num_bytes,
6404                               goffset  total_num_bytes,
6405                               gpointer user_data)
6406 {
6407   GTask *task = user_data;
6408   CopyAsyncData *data = g_task_get_task_data (task);
6409   ProgressData *progress;
6410
6411   progress = g_new (ProgressData, 1);
6412   progress->data = data;
6413   progress->current_num_bytes = current_num_bytes;
6414   progress->total_num_bytes = total_num_bytes;
6415
6416   g_main_context_invoke_full (g_task_get_context (task),
6417                               g_task_get_priority (task),
6418                               copy_async_progress_in_main,
6419                               progress,
6420                               g_free);
6421 }
6422
6423 static void
6424 copy_async_thread (GTask        *task,
6425                    gpointer      source,
6426                    gpointer      task_data,
6427                    GCancellable *cancellable)
6428 {
6429   CopyAsyncData *data = task_data;
6430   gboolean result;
6431   GError *error = NULL;
6432
6433   result = g_file_copy (data->source,
6434                         data->destination,
6435                         data->flags,
6436                         cancellable,
6437                         (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6438                         task,
6439                         &error);
6440   if (result)
6441     g_task_return_boolean (task, TRUE);
6442   else
6443     g_task_return_error (task, error);
6444 }
6445
6446 static void
6447 g_file_real_copy_async (GFile                  *source,
6448                         GFile                  *destination,
6449                         GFileCopyFlags          flags,
6450                         int                     io_priority,
6451                         GCancellable           *cancellable,
6452                         GFileProgressCallback   progress_callback,
6453                         gpointer                progress_callback_data,
6454                         GAsyncReadyCallback     callback,
6455                         gpointer                user_data)
6456 {
6457   GTask *task;
6458   CopyAsyncData *data;
6459
6460   data = g_slice_new (CopyAsyncData);
6461   data->source = g_object_ref (source);
6462   data->destination = g_object_ref (destination);
6463   data->flags = flags;
6464   data->progress_cb = progress_callback;
6465   data->progress_cb_data = progress_callback_data;
6466
6467   task = g_task_new (source, cancellable, callback, user_data);
6468   g_task_set_source_tag (task, g_file_real_copy_async);
6469   g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6470   g_task_set_priority (task, io_priority);
6471   g_task_run_in_thread (task, copy_async_thread);
6472   g_object_unref (task);
6473 }
6474
6475 static gboolean
6476 g_file_real_copy_finish (GFile        *file,
6477                          GAsyncResult *res,
6478                          GError      **error)
6479 {
6480   g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6481
6482   return g_task_propagate_boolean (G_TASK (res), error);
6483 }
6484
6485
6486 /********************************************
6487  *   Default VFS operations                 *
6488  ********************************************/
6489
6490 /**
6491  * g_file_new_for_path:
6492  * @path: (type filename): a string containing a relative or absolute path.
6493  *     The string must be encoded in the glib filename encoding.
6494  *
6495  * Constructs a #GFile for a given path. This operation never
6496  * fails, but the returned object might not support any I/O
6497  * operation if @path is malformed.
6498  *
6499  * Returns: (transfer full): a new #GFile for the given @path.
6500  *   Free the returned object with g_object_unref().
6501  */
6502 GFile *
6503 g_file_new_for_path (const char *path)
6504 {
6505   g_return_val_if_fail (path != NULL, NULL);
6506
6507   return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6508 }
6509
6510 /**
6511  * g_file_new_for_uri:
6512  * @uri: a UTF-8 string containing a URI
6513  *
6514  * Constructs a #GFile for a given URI. This operation never
6515  * fails, but the returned object might not support any I/O
6516  * operation if @uri is malformed or if the uri type is
6517  * not supported.
6518  *
6519  * Returns: (transfer full): a new #GFile for the given @uri.
6520  *     Free the returned object with g_object_unref().
6521  */
6522 GFile *
6523 g_file_new_for_uri (const char *uri)
6524 {
6525   g_return_val_if_fail (uri != NULL, NULL);
6526
6527   return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6528 }
6529
6530 /**
6531  * g_file_new_tmp:
6532  * @tmpl: (type filename) (nullable): Template for the file
6533  *   name, as in g_file_open_tmp(), or %NULL for a default template
6534  * @iostream: (out): on return, a #GFileIOStream for the created file
6535  * @error: a #GError, or %NULL
6536  *
6537  * Opens a file in the preferred directory for temporary files (as
6538  * returned by g_get_tmp_dir()) and returns a #GFile and
6539  * #GFileIOStream pointing to it.
6540  *
6541  * @tmpl should be a string in the GLib file name encoding
6542  * containing a sequence of six 'X' characters, and containing no
6543  * directory components. If it is %NULL, a default template is used.
6544  *
6545  * Unlike the other #GFile constructors, this will return %NULL if
6546  * a temporary file could not be created.
6547  *
6548  * Returns: (transfer full): a new #GFile.
6549  *     Free the returned object with g_object_unref().
6550  *
6551  * Since: 2.32
6552  */
6553 GFile *
6554 g_file_new_tmp (const char     *tmpl,
6555                 GFileIOStream **iostream,
6556                 GError        **error)
6557 {
6558   gint fd;
6559   gchar *path;
6560   GFile *file;
6561   GFileOutputStream *output;
6562
6563   g_return_val_if_fail (iostream != NULL, NULL);
6564
6565   fd = g_file_open_tmp (tmpl, &path, error);
6566   if (fd == -1)
6567     return NULL;
6568
6569   file = g_file_new_for_path (path);
6570
6571   output = _g_local_file_output_stream_new (fd);
6572   *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6573
6574   g_object_unref (output);
6575   g_free (path);
6576
6577   return file;
6578 }
6579
6580 /**
6581  * g_file_parse_name:
6582  * @parse_name: a file name or path to be parsed
6583  *
6584  * Constructs a #GFile with the given @parse_name (i.e. something
6585  * given by g_file_get_parse_name()). This operation never fails,
6586  * but the returned object might not support any I/O operation if
6587  * the @parse_name cannot be parsed.
6588  *
6589  * Returns: (transfer full): a new #GFile.
6590  */
6591 GFile *
6592 g_file_parse_name (const char *parse_name)
6593 {
6594   g_return_val_if_fail (parse_name != NULL, NULL);
6595
6596   return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6597 }
6598
6599 /**
6600  * g_file_new_build_filename:
6601  * @first_element: (type filename): the first element in the path
6602  * @...: remaining elements in path, terminated by %NULL
6603  *
6604  * Constructs a #GFile from a series of elements using the correct
6605  * separator for filenames.
6606  *
6607  * Using this function is equivalent to calling g_build_filename(),
6608  * followed by g_file_new_for_path() on the result.
6609  *
6610  * Returns: (transfer full): a new #GFile
6611  *
6612  * Since: 2.56
6613  */
6614 GFile *
6615 g_file_new_build_filename (const gchar *first_element,
6616                            ...)
6617 {
6618   gchar *str;
6619   GFile *file;
6620   va_list args;
6621
6622   g_return_val_if_fail (first_element != NULL, NULL);
6623
6624   va_start (args, first_element);
6625   str = g_build_filename_valist (first_element, &args);
6626   va_end (args);
6627
6628   file = g_file_new_for_path (str);
6629   g_free (str);
6630
6631   return file;
6632 }
6633
6634 static gboolean
6635 is_valid_scheme_character (char c)
6636 {
6637   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6638 }
6639
6640 /* Following RFC 2396, valid schemes are built like:
6641  *       scheme        = alpha *( alpha | digit | "+" | "-" | "." )
6642  */
6643 static gboolean
6644 has_valid_scheme (const char *uri)
6645 {
6646   const char *p;
6647
6648   p = uri;
6649
6650   if (!g_ascii_isalpha (*p))
6651     return FALSE;
6652
6653   do {
6654     p++;
6655   } while (is_valid_scheme_character (*p));
6656
6657   return *p == ':';
6658 }
6659
6660 static GFile *
6661 new_for_cmdline_arg (const gchar *arg,
6662                      const gchar *cwd)
6663 {
6664   GFile *file;
6665   char *filename;
6666
6667   if (g_path_is_absolute (arg))
6668     return g_file_new_for_path (arg);
6669
6670   if (has_valid_scheme (arg))
6671     return g_file_new_for_uri (arg);
6672
6673   if (cwd == NULL)
6674     {
6675       char *current_dir;
6676
6677       current_dir = g_get_current_dir ();
6678       filename = g_build_filename (current_dir, arg, NULL);
6679       g_free (current_dir);
6680     }
6681   else
6682     filename = g_build_filename (cwd, arg, NULL);
6683
6684   file = g_file_new_for_path (filename);
6685   g_free (filename);
6686
6687   return file;
6688 }
6689
6690 /**
6691  * g_file_new_for_commandline_arg:
6692  * @arg: (type filename): a command line string
6693  *
6694  * Creates a #GFile with the given argument from the command line.
6695  * The value of @arg can be either a URI, an absolute path or a
6696  * relative path resolved relative to the current working directory.
6697  * This operation never fails, but the returned object might not
6698  * support any I/O operation if @arg points to a malformed path.
6699  *
6700  * Note that on Windows, this function expects its argument to be in
6701  * UTF-8 -- not the system code page.  This means that you
6702  * should not use this function with string from argv as it is passed
6703  * to main().  g_win32_get_command_line() will return a UTF-8 version of
6704  * the commandline.  #GApplication also uses UTF-8 but
6705  * g_application_command_line_create_file_for_arg() may be more useful
6706  * for you there.  It is also always possible to use this function with
6707  * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6708  *
6709  * Returns: (transfer full): a new #GFile.
6710  *    Free the returned object with g_object_unref().
6711  */
6712 GFile *
6713 g_file_new_for_commandline_arg (const char *arg)
6714 {
6715   g_return_val_if_fail (arg != NULL, NULL);
6716
6717   return new_for_cmdline_arg (arg, NULL);
6718 }
6719
6720 /**
6721  * g_file_new_for_commandline_arg_and_cwd:
6722  * @arg: (type filename): a command line string
6723  * @cwd: (type filename): the current working directory of the commandline
6724  *
6725  * Creates a #GFile with the given argument from the command line.
6726  *
6727  * This function is similar to g_file_new_for_commandline_arg() except
6728  * that it allows for passing the current working directory as an
6729  * argument instead of using the current working directory of the
6730  * process.
6731  *
6732  * This is useful if the commandline argument was given in a context
6733  * other than the invocation of the current process.
6734  *
6735  * See also g_application_command_line_create_file_for_arg().
6736  *
6737  * Returns: (transfer full): a new #GFile
6738  *
6739  * Since: 2.36
6740  **/
6741 GFile *
6742 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6743                                         const gchar *cwd)
6744 {
6745   g_return_val_if_fail (arg != NULL, NULL);
6746   g_return_val_if_fail (cwd != NULL, NULL);
6747
6748   return new_for_cmdline_arg (arg, cwd);
6749 }
6750
6751 /**
6752  * g_file_mount_enclosing_volume:
6753  * @location: input #GFile
6754  * @flags: flags affecting the operation
6755  * @mount_operation: (nullable): a #GMountOperation
6756  *     or %NULL to avoid user interaction
6757  * @cancellable: (nullable): optional #GCancellable object,
6758  *     %NULL to ignore
6759  * @callback: (nullable): a #GAsyncReadyCallback to call
6760  *     when the request is satisfied, or %NULL
6761  * @user_data: the data to pass to callback function
6762  *
6763  * Starts a @mount_operation, mounting the volume that contains
6764  * the file @location.
6765  *
6766  * When this operation has completed, @callback will be called with
6767  * @user_user data, and the operation can be finalized with
6768  * g_file_mount_enclosing_volume_finish().
6769  *
6770  * If @cancellable is not %NULL, then the operation can be cancelled by
6771  * triggering the cancellable object from another thread. If the operation
6772  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6773  */
6774 void
6775 g_file_mount_enclosing_volume (GFile               *location,
6776                                GMountMountFlags     flags,
6777                                GMountOperation     *mount_operation,
6778                                GCancellable        *cancellable,
6779                                GAsyncReadyCallback  callback,
6780                                gpointer             user_data)
6781 {
6782   GFileIface *iface;
6783
6784   g_return_if_fail (G_IS_FILE (location));
6785
6786   iface = G_FILE_GET_IFACE (location);
6787
6788   if (iface->mount_enclosing_volume == NULL)
6789     {
6790       g_task_report_new_error (location, callback, user_data,
6791                                g_file_mount_enclosing_volume,
6792                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6793                                _("volume doesn’t implement mount"));
6794       return;
6795     }
6796
6797   (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6798
6799 }
6800
6801 /**
6802  * g_file_mount_enclosing_volume_finish:
6803  * @location: input #GFile
6804  * @result: a #GAsyncResult
6805  * @error: a #GError, or %NULL
6806  *
6807  * Finishes a mount operation started by g_file_mount_enclosing_volume().
6808  *
6809  * Returns: %TRUE if successful. If an error has occurred,
6810  *     this function will return %FALSE and set @error
6811  *     appropriately if present.
6812  */
6813 gboolean
6814 g_file_mount_enclosing_volume_finish (GFile         *location,
6815                                       GAsyncResult  *result,
6816                                       GError       **error)
6817 {
6818   GFileIface *iface;
6819
6820   g_return_val_if_fail (G_IS_FILE (location), FALSE);
6821   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6822
6823   if (g_async_result_legacy_propagate_error (result, error))
6824     return FALSE;
6825   else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6826     return g_task_propagate_boolean (G_TASK (result), error);
6827
6828   iface = G_FILE_GET_IFACE (location);
6829
6830   return (* iface->mount_enclosing_volume_finish) (location, result, error);
6831 }
6832
6833 /********************************************
6834  *   Utility functions                      *
6835  ********************************************/
6836
6837 /**
6838  * g_file_query_default_handler:
6839  * @file: a #GFile to open
6840  * @cancellable: optional #GCancellable object, %NULL to ignore
6841  * @error: a #GError, or %NULL
6842  *
6843  * Returns the #GAppInfo that is registered as the default
6844  * application to handle the file specified by @file.
6845  *
6846  * If @cancellable is not %NULL, then the operation can be cancelled by
6847  * triggering the cancellable object from another thread. If the operation
6848  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6849  *
6850  * Returns: (transfer full): a #GAppInfo if the handle was found,
6851  *     %NULL if there were errors.
6852  *     When you are done with it, release it with g_object_unref()
6853  */
6854 GAppInfo *
6855 g_file_query_default_handler (GFile         *file,
6856                               GCancellable  *cancellable,
6857                               GError       **error)
6858 {
6859   char *uri_scheme;
6860   const char *content_type;
6861   GAppInfo *appinfo;
6862   GFileInfo *info;
6863   char *path;
6864
6865   uri_scheme = g_file_get_uri_scheme (file);
6866   if (uri_scheme && uri_scheme[0] != '\0')
6867     {
6868       appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6869       g_free (uri_scheme);
6870
6871       if (appinfo != NULL)
6872         return appinfo;
6873     }
6874   else
6875     g_free (uri_scheme);
6876
6877   info = g_file_query_info (file,
6878                             G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6879                             0,
6880                             cancellable,
6881                             error);
6882   if (info == NULL)
6883     return NULL;
6884
6885   appinfo = NULL;
6886
6887   content_type = g_file_info_get_content_type (info);
6888   if (content_type)
6889     {
6890       /* Don't use is_native(), as we want to support fuse paths if available */
6891       path = g_file_get_path (file);
6892       appinfo = g_app_info_get_default_for_type (content_type,
6893                                                  path == NULL);
6894       g_free (path);
6895     }
6896
6897   g_object_unref (info);
6898
6899   if (appinfo != NULL)
6900     return appinfo;
6901
6902   g_set_error_literal (error, G_IO_ERROR,
6903                        G_IO_ERROR_NOT_SUPPORTED,
6904                        _("No application is registered as handling this file"));
6905   return NULL;
6906 }
6907
6908 static void
6909 query_default_handler_query_info_cb (GObject      *object,
6910                                      GAsyncResult *result,
6911                                      gpointer      user_data)
6912 {
6913   GFile *file = G_FILE (object);
6914   GTask *task = G_TASK (user_data);
6915   GError *error = NULL;
6916   GFileInfo *info;
6917   const char *content_type;
6918   GAppInfo *appinfo = NULL;
6919
6920   info = g_file_query_info_finish (file, result, &error);
6921   if (info == NULL)
6922     {
6923       g_task_return_error (task, g_steal_pointer (&error));
6924       g_object_unref (task);
6925       return;
6926     }
6927
6928   content_type = g_file_info_get_content_type (info);
6929   if (content_type)
6930     {
6931       char *path;
6932
6933       /* Don't use is_native(), as we want to support fuse paths if available */
6934       path = g_file_get_path (file);
6935
6936       /* FIXME: The following still uses blocking calls. */
6937       appinfo = g_app_info_get_default_for_type (content_type,
6938                                                  path == NULL);
6939       g_free (path);
6940     }
6941
6942   g_object_unref (info);
6943
6944   if (appinfo != NULL)
6945     g_task_return_pointer (task, g_steal_pointer (&appinfo), g_object_unref);
6946   else
6947     g_task_return_new_error (task,
6948                              G_IO_ERROR,
6949                              G_IO_ERROR_NOT_SUPPORTED,
6950                              _("No application is registered as handling this file"));
6951   g_object_unref (task);
6952 }
6953
6954 /**
6955  * g_file_query_default_handler_async:
6956  * @file: a #GFile to open
6957  * @io_priority: the [I/O priority][io-priority] of the request
6958  * @cancellable: optional #GCancellable object, %NULL to ignore
6959  * @callback: (nullable): a #GAsyncReadyCallback to call when the request is done
6960  * @user_data: (nullable): data to pass to @callback
6961  *
6962  * Async version of g_file_query_default_handler().
6963  *
6964  * Since: 2.60
6965  */
6966 void
6967 g_file_query_default_handler_async (GFile              *file,
6968                                     int                 io_priority,
6969                                     GCancellable       *cancellable,
6970                                     GAsyncReadyCallback callback,
6971                                     gpointer            user_data)
6972 {
6973   GTask *task;
6974   char *uri_scheme;
6975
6976   task = g_task_new (file, cancellable, callback, user_data);
6977   g_task_set_source_tag (task, g_file_query_default_handler_async);
6978
6979   uri_scheme = g_file_get_uri_scheme (file);
6980   if (uri_scheme && uri_scheme[0] != '\0')
6981     {
6982       GAppInfo *appinfo;
6983
6984       /* FIXME: The following still uses blocking calls. */
6985       appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6986       g_free (uri_scheme);
6987
6988       if (appinfo != NULL)
6989         {
6990           g_task_return_pointer (task, g_steal_pointer (&appinfo), g_object_unref);
6991           g_object_unref (task);
6992           return;
6993         }
6994     }
6995   else
6996     g_free (uri_scheme);
6997
6998   g_file_query_info_async (file,
6999                            G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
7000                            0,
7001                            io_priority,
7002                            cancellable,
7003                            query_default_handler_query_info_cb,
7004                            g_steal_pointer (&task));
7005 }
7006
7007 /**
7008  * g_file_query_default_handler_finish:
7009  * @file: a #GFile to open
7010  * @result: a #GAsyncResult
7011  * @error: (nullable): a #GError
7012  *
7013  * Finishes a g_file_query_default_handler_async() operation.
7014  *
7015  * Returns: (transfer full): a #GAppInfo if the handle was found,
7016  *     %NULL if there were errors.
7017  *     When you are done with it, release it with g_object_unref()
7018  *
7019  * Since: 2.60
7020  */
7021 GAppInfo *
7022 g_file_query_default_handler_finish (GFile        *file,
7023                                      GAsyncResult *result,
7024                                      GError      **error)
7025 {
7026   g_return_val_if_fail (G_IS_FILE (file), NULL);
7027   g_return_val_if_fail (g_task_is_valid (result, file), NULL);
7028
7029   return g_task_propagate_pointer (G_TASK (result), error);
7030 }
7031
7032 #define GET_CONTENT_BLOCK_SIZE 8192
7033
7034 /**
7035  * g_file_load_contents:
7036  * @file: input #GFile
7037  * @cancellable: optional #GCancellable object, %NULL to ignore
7038  * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7039  * @length: (out) (optional): a location to place the length of the contents of the file,
7040  *    or %NULL if the length is not needed
7041  * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7042  *    or %NULL if the entity tag is not needed
7043  * @error: a #GError, or %NULL
7044  *
7045  * Loads the content of the file into memory. The data is always
7046  * zero-terminated, but this is not included in the resultant @length.
7047  * The returned @content should be freed with g_free() when no longer
7048  * needed.
7049  *
7050  * If @cancellable is not %NULL, then the operation can be cancelled by
7051  * triggering the cancellable object from another thread. If the operation
7052  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7053  *
7054  * Returns: %TRUE if the @file's contents were successfully loaded.
7055  *     %FALSE if there were errors.
7056  */
7057 gboolean
7058 g_file_load_contents (GFile         *file,
7059                       GCancellable  *cancellable,
7060                       char         **contents,
7061                       gsize         *length,
7062                       char         **etag_out,
7063                       GError       **error)
7064 {
7065   GFileInputStream *in;
7066   GByteArray *content;
7067   gsize pos;
7068   gssize res;
7069   GFileInfo *info;
7070
7071   g_return_val_if_fail (G_IS_FILE (file), FALSE);
7072   g_return_val_if_fail (contents != NULL, FALSE);
7073
7074   in = g_file_read (file, cancellable, error);
7075   if (in == NULL)
7076     return FALSE;
7077
7078   content = g_byte_array_new ();
7079   pos = 0;
7080
7081   g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
7082   while ((res = g_input_stream_read (G_INPUT_STREAM (in),
7083                                      content->data + pos,
7084                                      GET_CONTENT_BLOCK_SIZE,
7085                                      cancellable, error)) > 0)
7086     {
7087       pos += res;
7088       g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
7089     }
7090
7091   if (etag_out)
7092     {
7093       *etag_out = NULL;
7094
7095       info = g_file_input_stream_query_info (in,
7096                                              G_FILE_ATTRIBUTE_ETAG_VALUE,
7097                                              cancellable,
7098                                              NULL);
7099       if (info)
7100         {
7101           *etag_out = g_strdup (g_file_info_get_etag (info));
7102           g_object_unref (info);
7103         }
7104     }
7105
7106   /* Ignore errors on close */
7107   g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
7108   g_object_unref (in);
7109
7110   if (res < 0)
7111     {
7112       /* error is set already */
7113       g_byte_array_free (content, TRUE);
7114       return FALSE;
7115     }
7116
7117   if (length)
7118     *length = pos;
7119
7120   /* Zero terminate (we got an extra byte allocated for this */
7121   content->data[pos] = 0;
7122
7123   *contents = (char *)g_byte_array_free (content, FALSE);
7124
7125   return TRUE;
7126 }
7127
7128 typedef struct {
7129   GTask *task;
7130   GFileReadMoreCallback read_more_callback;
7131   GByteArray *content;
7132   gsize pos;
7133   char *etag;
7134 } LoadContentsData;
7135
7136
7137 static void
7138 load_contents_data_free (LoadContentsData *data)
7139 {
7140   if (data->content)
7141     g_byte_array_free (data->content, TRUE);
7142   g_free (data->etag);
7143   g_free (data);
7144 }
7145
7146 static void
7147 load_contents_close_callback (GObject      *obj,
7148                               GAsyncResult *close_res,
7149                               gpointer      user_data)
7150 {
7151   GInputStream *stream = G_INPUT_STREAM (obj);
7152   LoadContentsData *data = user_data;
7153
7154   /* Ignore errors here, we're only reading anyway */
7155   g_input_stream_close_finish (stream, close_res, NULL);
7156   g_object_unref (stream);
7157
7158   g_task_return_boolean (data->task, TRUE);
7159   g_object_unref (data->task);
7160 }
7161
7162 static void
7163 load_contents_fstat_callback (GObject      *obj,
7164                               GAsyncResult *stat_res,
7165                               gpointer      user_data)
7166 {
7167   GInputStream *stream = G_INPUT_STREAM (obj);
7168   LoadContentsData *data = user_data;
7169   GFileInfo *info;
7170
7171   info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
7172                                                 stat_res, NULL);
7173   if (info)
7174     {
7175       data->etag = g_strdup (g_file_info_get_etag (info));
7176       g_object_unref (info);
7177     }
7178
7179   g_input_stream_close_async (stream, 0,
7180                               g_task_get_cancellable (data->task),
7181                               load_contents_close_callback, data);
7182 }
7183
7184 static void
7185 load_contents_read_callback (GObject      *obj,
7186                              GAsyncResult *read_res,
7187                              gpointer      user_data)
7188 {
7189   GInputStream *stream = G_INPUT_STREAM (obj);
7190   LoadContentsData *data = user_data;
7191   GError *error = NULL;
7192   gssize read_size;
7193
7194   read_size = g_input_stream_read_finish (stream, read_res, &error);
7195
7196   if (read_size < 0)
7197     {
7198       g_task_return_error (data->task, error);
7199       g_object_unref (data->task);
7200
7201       /* Close the file ignoring any error */
7202       g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
7203       g_object_unref (stream);
7204     }
7205   else if (read_size == 0)
7206     {
7207       g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
7208                                             G_FILE_ATTRIBUTE_ETAG_VALUE,
7209                                             0,
7210                                             g_task_get_cancellable (data->task),
7211                                             load_contents_fstat_callback,
7212                                             data);
7213     }
7214   else if (read_size > 0)
7215     {
7216       data->pos += read_size;
7217
7218       g_byte_array_set_size (data->content,
7219                              data->pos + GET_CONTENT_BLOCK_SIZE);
7220
7221
7222       if (data->read_more_callback &&
7223           !data->read_more_callback ((char *)data->content->data, data->pos,
7224                                      g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
7225         g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
7226                                               G_FILE_ATTRIBUTE_ETAG_VALUE,
7227                                               0,
7228                                               g_task_get_cancellable (data->task),
7229                                               load_contents_fstat_callback,
7230                                               data);
7231       else
7232         g_input_stream_read_async (stream,
7233                                    data->content->data + data->pos,
7234                                    GET_CONTENT_BLOCK_SIZE,
7235                                    0,
7236                                    g_task_get_cancellable (data->task),
7237                                    load_contents_read_callback,
7238                                    data);
7239     }
7240 }
7241
7242 static void
7243 load_contents_open_callback (GObject      *obj,
7244                              GAsyncResult *open_res,
7245                              gpointer      user_data)
7246 {
7247   GFile *file = G_FILE (obj);
7248   GFileInputStream *stream;
7249   LoadContentsData *data = user_data;
7250   GError *error = NULL;
7251
7252   stream = g_file_read_finish (file, open_res, &error);
7253
7254   if (stream)
7255     {
7256       g_byte_array_set_size (data->content,
7257                              data->pos + GET_CONTENT_BLOCK_SIZE);
7258       g_input_stream_read_async (G_INPUT_STREAM (stream),
7259                                  data->content->data + data->pos,
7260                                  GET_CONTENT_BLOCK_SIZE,
7261                                  0,
7262                                  g_task_get_cancellable (data->task),
7263                                  load_contents_read_callback,
7264                                  data);
7265     }
7266   else
7267     {
7268       g_task_return_error (data->task, error);
7269       g_object_unref (data->task);
7270     }
7271 }
7272
7273 /**
7274  * g_file_load_partial_contents_async: (skip)
7275  * @file: input #GFile
7276  * @cancellable: optional #GCancellable object, %NULL to ignore
7277  * @read_more_callback: (scope call) (closure user_data): a
7278  *     #GFileReadMoreCallback to receive partial data
7279  *     and to specify whether further data should be read
7280  * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback to call
7281  *     when the request is satisfied
7282  * @user_data: the data to pass to the callback functions
7283  *
7284  * Reads the partial contents of a file. A #GFileReadMoreCallback should
7285  * be used to stop reading from the file when appropriate, else this
7286  * function will behave exactly as g_file_load_contents_async(). This
7287  * operation can be finished by g_file_load_partial_contents_finish().
7288  *
7289  * Users of this function should be aware that @user_data is passed to
7290  * both the @read_more_callback and the @callback.
7291  *
7292  * If @cancellable is not %NULL, then the operation can be cancelled by
7293  * triggering the cancellable object from another thread. If the operation
7294  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7295  */
7296 void
7297 g_file_load_partial_contents_async (GFile                 *file,
7298                                     GCancellable          *cancellable,
7299                                     GFileReadMoreCallback  read_more_callback,
7300                                     GAsyncReadyCallback    callback,
7301                                     gpointer               user_data)
7302 {
7303   LoadContentsData *data;
7304
7305   g_return_if_fail (G_IS_FILE (file));
7306
7307   data = g_new0 (LoadContentsData, 1);
7308   data->read_more_callback = read_more_callback;
7309   data->content = g_byte_array_new ();
7310
7311   data->task = g_task_new (file, cancellable, callback, user_data);
7312   g_task_set_source_tag (data->task, g_file_load_partial_contents_async);
7313   g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
7314
7315   g_file_read_async (file,
7316                      0,
7317                      g_task_get_cancellable (data->task),
7318                      load_contents_open_callback,
7319                      data);
7320 }
7321
7322 /**
7323  * g_file_load_partial_contents_finish:
7324  * @file: input #GFile
7325  * @res: a #GAsyncResult
7326  * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7327  * @length: (out) (optional): a location to place the length of the contents of the file,
7328  *     or %NULL if the length is not needed
7329  * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7330  *     or %NULL if the entity tag is not needed
7331  * @error: a #GError, or %NULL
7332  *
7333  * Finishes an asynchronous partial load operation that was started
7334  * with g_file_load_partial_contents_async(). The data is always
7335  * zero-terminated, but this is not included in the resultant @length.
7336  * The returned @content should be freed with g_free() when no longer
7337  * needed.
7338  *
7339  * Returns: %TRUE if the load was successful. If %FALSE and @error is
7340  *     present, it will be set appropriately.
7341  */
7342 gboolean
7343 g_file_load_partial_contents_finish (GFile         *file,
7344                                      GAsyncResult  *res,
7345                                      char         **contents,
7346                                      gsize         *length,
7347                                      char         **etag_out,
7348                                      GError       **error)
7349 {
7350   GTask *task;
7351   LoadContentsData *data;
7352
7353   g_return_val_if_fail (G_IS_FILE (file), FALSE);
7354   g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7355   g_return_val_if_fail (contents != NULL, FALSE);
7356
7357   task = G_TASK (res);
7358
7359   if (!g_task_propagate_boolean (task, error))
7360     {
7361       if (length)
7362         *length = 0;
7363       return FALSE;
7364     }
7365
7366   data = g_task_get_task_data (task);
7367
7368   if (length)
7369     *length = data->pos;
7370
7371   if (etag_out)
7372     {
7373       *etag_out = data->etag;
7374       data->etag = NULL;
7375     }
7376
7377   /* Zero terminate */
7378   g_byte_array_set_size (data->content, data->pos + 1);
7379   data->content->data[data->pos] = 0;
7380
7381   *contents = (char *)g_byte_array_free (data->content, FALSE);
7382   data->content = NULL;
7383
7384   return TRUE;
7385 }
7386
7387 /**
7388  * g_file_load_contents_async:
7389  * @file: input #GFile
7390  * @cancellable: optional #GCancellable object, %NULL to ignore
7391  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7392  * @user_data: the data to pass to callback function
7393  *
7394  * Starts an asynchronous load of the @file's contents.
7395  *
7396  * For more details, see g_file_load_contents() which is
7397  * the synchronous version of this call.
7398  *
7399  * When the load operation has completed, @callback will be called
7400  * with @user data. To finish the operation, call
7401  * g_file_load_contents_finish() with the #GAsyncResult returned by
7402  * the @callback.
7403  *
7404  * If @cancellable is not %NULL, then the operation can be cancelled by
7405  * triggering the cancellable object from another thread. If the operation
7406  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7407  */
7408 void
7409 g_file_load_contents_async (GFile               *file,
7410                            GCancellable        *cancellable,
7411                            GAsyncReadyCallback  callback,
7412                            gpointer             user_data)
7413 {
7414   g_file_load_partial_contents_async (file,
7415                                       cancellable,
7416                                       NULL,
7417                                       callback, user_data);
7418 }
7419
7420 /**
7421  * g_file_load_contents_finish:
7422  * @file: input #GFile
7423  * @res: a #GAsyncResult
7424  * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7425  * @length: (out) (optional): a location to place the length of the contents of the file,
7426  *     or %NULL if the length is not needed
7427  * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7428  *     or %NULL if the entity tag is not needed
7429  * @error: a #GError, or %NULL
7430  *
7431  * Finishes an asynchronous load of the @file's contents.
7432  * The contents are placed in @contents, and @length is set to the
7433  * size of the @contents string. The @content should be freed with
7434  * g_free() when no longer needed. If @etag_out is present, it will be
7435  * set to the new entity tag for the @file.
7436  *
7437  * Returns: %TRUE if the load was successful. If %FALSE and @error is
7438  *     present, it will be set appropriately.
7439  */
7440 gboolean
7441 g_file_load_contents_finish (GFile         *file,
7442                              GAsyncResult  *res,
7443                              char         **contents,
7444                              gsize         *length,
7445                              char         **etag_out,
7446                              GError       **error)
7447 {
7448   return g_file_load_partial_contents_finish (file,
7449                                               res,
7450                                               contents,
7451                                               length,
7452                                               etag_out,
7453                                               error);
7454 }
7455
7456 /**
7457  * g_file_replace_contents:
7458  * @file: input #GFile
7459  * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7460  * @length: the length of @contents in bytes
7461  * @etag: (nullable): the old [entity-tag][gfile-etag] for the document,
7462  *     or %NULL
7463  * @make_backup: %TRUE if a backup should be created
7464  * @flags: a set of #GFileCreateFlags
7465  * @new_etag: (out) (optional): a location to a new [entity tag][gfile-etag]
7466  *      for the document. This should be freed with g_free() when no longer
7467  *      needed, or %NULL
7468  * @cancellable: optional #GCancellable object, %NULL to ignore
7469  * @error: a #GError, or %NULL
7470  *
7471  * Replaces the contents of @file with @contents of @length bytes.
7472  *
7473  * If @etag is specified (not %NULL), any existing file must have that etag,
7474  * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7475  *
7476  * If @make_backup is %TRUE, this function will attempt to make a backup
7477  * of @file. Internally, it uses g_file_replace(), so will try to replace the
7478  * file contents in the safest way possible. For example, atomic renames are
7479  * used when replacing local files’ contents.
7480  *
7481  * If @cancellable is not %NULL, then the operation can be cancelled by
7482  * triggering the cancellable object from another thread. If the operation
7483  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7484  *
7485  * The returned @new_etag can be used to verify that the file hasn't
7486  * changed the next time it is saved over.
7487  *
7488  * Returns: %TRUE if successful. If an error has occurred, this function
7489  *     will return %FALSE and set @error appropriately if present.
7490  */
7491 gboolean
7492 g_file_replace_contents (GFile             *file,
7493                          const char        *contents,
7494                          gsize              length,
7495                          const char        *etag,
7496                          gboolean           make_backup,
7497                          GFileCreateFlags   flags,
7498                          char             **new_etag,
7499                          GCancellable      *cancellable,
7500                          GError           **error)
7501 {
7502   GFileOutputStream *out;
7503   gsize pos, remainder;
7504   gssize res;
7505   gboolean ret;
7506
7507   g_return_val_if_fail (G_IS_FILE (file), FALSE);
7508   g_return_val_if_fail (contents != NULL, FALSE);
7509
7510   out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7511   if (out == NULL)
7512     return FALSE;
7513
7514   pos = 0;
7515   remainder = length;
7516   while (remainder > 0 &&
7517          (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7518                                        contents + pos,
7519                                        MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7520                                        cancellable,
7521                                        error)) > 0)
7522     {
7523       pos += res;
7524       remainder -= res;
7525     }
7526
7527   if (remainder > 0 && res < 0)
7528     {
7529       /* Ignore errors on close */
7530       g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7531       g_object_unref (out);
7532
7533       /* error is set already */
7534       return FALSE;
7535     }
7536
7537   ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7538
7539   if (new_etag)
7540     *new_etag = g_file_output_stream_get_etag (out);
7541
7542   g_object_unref (out);
7543
7544   return ret;
7545 }
7546
7547 typedef struct {
7548   GTask *task;
7549   GBytes *content;
7550   gsize pos;
7551   char *etag;
7552   gboolean failed;
7553 } ReplaceContentsData;
7554
7555 static void
7556 replace_contents_data_free (ReplaceContentsData *data)
7557 {
7558   g_bytes_unref (data->content);
7559   g_free (data->etag);
7560   g_free (data);
7561 }
7562
7563 static void
7564 replace_contents_close_callback (GObject      *obj,
7565                                  GAsyncResult *close_res,
7566                                  gpointer      user_data)
7567 {
7568   GOutputStream *stream = G_OUTPUT_STREAM (obj);
7569   ReplaceContentsData *data = user_data;
7570
7571   /* Ignore errors here, we're only reading anyway */
7572   g_output_stream_close_finish (stream, close_res, NULL);
7573   g_object_unref (stream);
7574
7575   if (!data->failed)
7576     {
7577       data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7578       g_task_return_boolean (data->task, TRUE);
7579     }
7580   g_object_unref (data->task);
7581 }
7582
7583 static void
7584 replace_contents_write_callback (GObject      *obj,
7585                                  GAsyncResult *read_res,
7586                                  gpointer      user_data)
7587 {
7588   GOutputStream *stream = G_OUTPUT_STREAM (obj);
7589   ReplaceContentsData *data = user_data;
7590   GError *error = NULL;
7591   gssize write_size;
7592
7593   write_size = g_output_stream_write_finish (stream, read_res, &error);
7594
7595   if (write_size <= 0)
7596     {
7597       /* Error or EOF, close the file */
7598       if (write_size < 0)
7599         {
7600           data->failed = TRUE;
7601           g_task_return_error (data->task, error);
7602         }
7603       g_output_stream_close_async (stream, 0,
7604                                    g_task_get_cancellable (data->task),
7605                                    replace_contents_close_callback, data);
7606     }
7607   else if (write_size > 0)
7608     {
7609       const gchar *content;
7610       gsize length;
7611
7612       content = g_bytes_get_data (data->content, &length);
7613       data->pos += write_size;
7614
7615       if (data->pos >= length)
7616         g_output_stream_close_async (stream, 0,
7617                                      g_task_get_cancellable (data->task),
7618                                      replace_contents_close_callback, data);
7619       else
7620         g_output_stream_write_async (stream,
7621                                      content + data->pos,
7622                                      length - data->pos,
7623                                      0,
7624                                      g_task_get_cancellable (data->task),
7625                                      replace_contents_write_callback,
7626                                      data);
7627     }
7628 }
7629
7630 static void
7631 replace_contents_open_callback (GObject      *obj,
7632                                 GAsyncResult *open_res,
7633                                 gpointer      user_data)
7634 {
7635   GFile *file = G_FILE (obj);
7636   GFileOutputStream *stream;
7637   ReplaceContentsData *data = user_data;
7638   GError *error = NULL;
7639
7640   stream = g_file_replace_finish (file, open_res, &error);
7641
7642   if (stream)
7643     {
7644       const gchar *content;
7645       gsize length;
7646
7647       content = g_bytes_get_data (data->content, &length);
7648       g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7649                                    content + data->pos,
7650                                    length - data->pos,
7651                                    0,
7652                                    g_task_get_cancellable (data->task),
7653                                    replace_contents_write_callback,
7654                                    data);
7655     }
7656   else
7657     {
7658       g_task_return_error (data->task, error);
7659       g_object_unref (data->task);
7660     }
7661 }
7662
7663 /**
7664  * g_file_replace_contents_async:
7665  * @file: input #GFile
7666  * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7667  * @length: the length of @contents in bytes
7668  * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7669  * @make_backup: %TRUE if a backup should be created
7670  * @flags: a set of #GFileCreateFlags
7671  * @cancellable: optional #GCancellable object, %NULL to ignore
7672  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7673  * @user_data: the data to pass to callback function
7674  *
7675  * Starts an asynchronous replacement of @file with the given
7676  * @contents of @length bytes. @etag will replace the document's
7677  * current entity tag.
7678  *
7679  * When this operation has completed, @callback will be called with
7680  * @user_user data, and the operation can be finalized with
7681  * g_file_replace_contents_finish().
7682  *
7683  * If @cancellable is not %NULL, then the operation can be cancelled by
7684  * triggering the cancellable object from another thread. If the operation
7685  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7686  *
7687  * If @make_backup is %TRUE, this function will attempt to
7688  * make a backup of @file.
7689  *
7690  * Note that no copy of @content will be made, so it must stay valid
7691  * until @callback is called. See g_file_replace_contents_bytes_async()
7692  * for a #GBytes version that will automatically hold a reference to the
7693  * contents (without copying) for the duration of the call.
7694  */
7695 void
7696 g_file_replace_contents_async  (GFile               *file,
7697                                 const char          *contents,
7698                                 gsize                length,
7699                                 const char          *etag,
7700                                 gboolean             make_backup,
7701                                 GFileCreateFlags     flags,
7702                                 GCancellable        *cancellable,
7703                                 GAsyncReadyCallback  callback,
7704                                 gpointer             user_data)
7705 {
7706   GBytes *bytes;
7707
7708   bytes = g_bytes_new_static (contents, length);
7709   g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7710       cancellable, callback, user_data);
7711   g_bytes_unref (bytes);
7712 }
7713
7714 /**
7715  * g_file_replace_contents_bytes_async:
7716  * @file: input #GFile
7717  * @contents: a #GBytes
7718  * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7719  * @make_backup: %TRUE if a backup should be created
7720  * @flags: a set of #GFileCreateFlags
7721  * @cancellable: optional #GCancellable object, %NULL to ignore
7722  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7723  * @user_data: the data to pass to callback function
7724  *
7725  * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7726  * This function will keep a ref on @contents until the operation is done.
7727  * Unlike g_file_replace_contents_async() this allows forgetting about the
7728  * content without waiting for the callback.
7729  *
7730  * When this operation has completed, @callback will be called with
7731  * @user_user data, and the operation can be finalized with
7732  * g_file_replace_contents_finish().
7733  *
7734  * Since: 2.40
7735  */
7736 void
7737 g_file_replace_contents_bytes_async  (GFile               *file,
7738                                       GBytes              *contents,
7739                                       const char          *etag,
7740                                       gboolean             make_backup,
7741                                       GFileCreateFlags     flags,
7742                                       GCancellable        *cancellable,
7743                                       GAsyncReadyCallback  callback,
7744                                       gpointer             user_data)
7745 {
7746   ReplaceContentsData *data;
7747
7748   g_return_if_fail (G_IS_FILE (file));
7749   g_return_if_fail (contents != NULL);
7750
7751   data = g_new0 (ReplaceContentsData, 1);
7752
7753   data->content = g_bytes_ref (contents);
7754
7755   data->task = g_task_new (file, cancellable, callback, user_data);
7756   g_task_set_source_tag (data->task, g_file_replace_contents_bytes_async);
7757   g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7758
7759   g_file_replace_async (file,
7760                         etag,
7761                         make_backup,
7762                         flags,
7763                         0,
7764                         g_task_get_cancellable (data->task),
7765                         replace_contents_open_callback,
7766                         data);
7767 }
7768
7769 /**
7770  * g_file_replace_contents_finish:
7771  * @file: input #GFile
7772  * @res: a #GAsyncResult
7773  * @new_etag: (out) (optional): a location of a new [entity tag][gfile-etag]
7774  *     for the document. This should be freed with g_free() when it is no
7775  *     longer needed, or %NULL
7776  * @error: a #GError, or %NULL
7777  *
7778  * Finishes an asynchronous replace of the given @file. See
7779  * g_file_replace_contents_async(). Sets @new_etag to the new entity
7780  * tag for the document, if present.
7781  *
7782  * Returns: %TRUE on success, %FALSE on failure.
7783  */
7784 gboolean
7785 g_file_replace_contents_finish (GFile         *file,
7786                                 GAsyncResult  *res,
7787                                 char         **new_etag,
7788                                 GError       **error)
7789 {
7790   GTask *task;
7791   ReplaceContentsData *data;
7792
7793   g_return_val_if_fail (G_IS_FILE (file), FALSE);
7794   g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7795
7796   task = G_TASK (res);
7797
7798   if (!g_task_propagate_boolean (task, error))
7799     return FALSE;
7800
7801   data = g_task_get_task_data (task);
7802
7803   if (new_etag)
7804     {
7805       *new_etag = data->etag;
7806       data->etag = NULL; /* Take ownership */
7807     }
7808
7809   return TRUE;
7810 }
7811
7812 gboolean
7813 g_file_real_measure_disk_usage (GFile                         *file,
7814                                 GFileMeasureFlags              flags,
7815                                 GCancellable                  *cancellable,
7816                                 GFileMeasureProgressCallback   progress_callback,
7817                                 gpointer                       progress_data,
7818                                 guint64                       *disk_usage,
7819                                 guint64                       *num_dirs,
7820                                 guint64                       *num_files,
7821                                 GError                       **error)
7822 {
7823   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7824                        "Operation not supported for the current backend.");
7825   return FALSE;
7826 }
7827
7828 typedef struct
7829 {
7830   GFileMeasureFlags             flags;
7831   GFileMeasureProgressCallback  progress_callback;
7832   gpointer                      progress_data;
7833 } MeasureTaskData;
7834
7835 typedef struct
7836 {
7837   guint64 disk_usage;
7838   guint64 num_dirs;
7839   guint64 num_files;
7840 } MeasureResult;
7841
7842 typedef struct
7843 {
7844   GFileMeasureProgressCallback callback;
7845   gpointer                     user_data;
7846   gboolean                     reporting;
7847   guint64                      current_size;
7848   guint64                      num_dirs;
7849   guint64                      num_files;
7850 } MeasureProgress;
7851
7852 static gboolean
7853 measure_disk_usage_invoke_progress (gpointer user_data)
7854 {
7855   MeasureProgress *progress = user_data;
7856
7857   (* progress->callback) (progress->reporting,
7858                           progress->current_size, progress->num_dirs, progress->num_files,
7859                           progress->user_data);
7860
7861   return FALSE;
7862 }
7863
7864 static void
7865 measure_disk_usage_progress (gboolean reporting,
7866                              guint64  current_size,
7867                              guint64  num_dirs,
7868                              guint64  num_files,
7869                              gpointer user_data)
7870 {
7871   MeasureProgress progress;
7872   GTask *task = user_data;
7873   MeasureTaskData *data;
7874
7875   data = g_task_get_task_data (task);
7876
7877   progress.callback = data->progress_callback;
7878   progress.user_data = data->progress_data;
7879   progress.reporting = reporting;
7880   progress.current_size = current_size;
7881   progress.num_dirs = num_dirs;
7882   progress.num_files = num_files;
7883
7884   g_main_context_invoke_full (g_task_get_context (task),
7885                               g_task_get_priority (task),
7886                               measure_disk_usage_invoke_progress,
7887                               g_memdup (&progress, sizeof progress),
7888                               g_free);
7889 }
7890
7891 static void
7892 measure_disk_usage_thread (GTask        *task,
7893                            gpointer      source_object,
7894                            gpointer      task_data,
7895                            GCancellable *cancellable)
7896 {
7897   MeasureTaskData *data = task_data;
7898   GError *error = NULL;
7899   MeasureResult result = { 0, };
7900
7901   if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7902                                  data->progress_callback ? measure_disk_usage_progress : NULL, task,
7903                                  &result.disk_usage, &result.num_dirs, &result.num_files,
7904                                  &error))
7905     g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7906   else
7907     g_task_return_error (task, error);
7908 }
7909
7910 static void
7911 g_file_real_measure_disk_usage_async (GFile                        *file,
7912                                       GFileMeasureFlags             flags,
7913                                       gint                          io_priority,
7914                                       GCancellable                 *cancellable,
7915                                       GFileMeasureProgressCallback  progress_callback,
7916                                       gpointer                      progress_data,
7917                                       GAsyncReadyCallback           callback,
7918                                       gpointer                      user_data)
7919 {
7920   MeasureTaskData data;
7921   GTask *task;
7922
7923   data.flags = flags;
7924   data.progress_callback = progress_callback;
7925   data.progress_data = progress_data;
7926
7927   task = g_task_new (file, cancellable, callback, user_data);
7928   g_task_set_source_tag (task, g_file_real_measure_disk_usage_async);
7929   g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7930   g_task_set_priority (task, io_priority);
7931
7932   g_task_run_in_thread (task, measure_disk_usage_thread);
7933   g_object_unref (task);
7934 }
7935
7936 static gboolean
7937 g_file_real_measure_disk_usage_finish (GFile         *file,
7938                                        GAsyncResult  *result,
7939                                        guint64       *disk_usage,
7940                                        guint64       *num_dirs,
7941                                        guint64       *num_files,
7942                                        GError       **error)
7943 {
7944   MeasureResult *measure_result;
7945
7946   g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7947
7948   measure_result = g_task_propagate_pointer (G_TASK (result), error);
7949
7950   if (measure_result == NULL)
7951     return FALSE;
7952
7953   if (disk_usage)
7954     *disk_usage = measure_result->disk_usage;
7955
7956   if (num_dirs)
7957     *num_dirs = measure_result->num_dirs;
7958
7959   if (num_files)
7960     *num_files = measure_result->num_files;
7961
7962   g_free (measure_result);
7963
7964   return TRUE;
7965 }
7966
7967 /**
7968  * g_file_measure_disk_usage:
7969  * @file: a #GFile
7970  * @flags: #GFileMeasureFlags
7971  * @cancellable: (nullable): optional #GCancellable
7972  * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7973  * @progress_data: user_data for @progress_callback
7974  * @disk_usage: (out) (optional): the number of bytes of disk space used
7975  * @num_dirs: (out) (optional): the number of directories encountered
7976  * @num_files: (out) (optional): the number of non-directories encountered
7977  * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7978  *
7979  * Recursively measures the disk usage of @file.
7980  *
7981  * This is essentially an analog of the 'du' command, but it also
7982  * reports the number of directories and non-directory files encountered
7983  * (including things like symbolic links).
7984  *
7985  * By default, errors are only reported against the toplevel file
7986  * itself.  Errors found while recursing are silently ignored, unless
7987  * %G_FILE_MEASURE_REPORT_ANY_ERROR is given in @flags.
7988  *
7989  * The returned size, @disk_usage, is in bytes and should be formatted
7990  * with g_format_size() in order to get something reasonable for showing
7991  * in a user interface.
7992  *
7993  * @progress_callback and @progress_data can be given to request
7994  * periodic progress updates while scanning.  See the documentation for
7995  * #GFileMeasureProgressCallback for information about when and how the
7996  * callback will be invoked.
7997  *
7998  * Returns: %TRUE if successful, with the out parameters set.
7999  *          %FALSE otherwise, with @error set.
8000  *
8001  * Since: 2.38
8002  **/
8003 gboolean
8004 g_file_measure_disk_usage (GFile                         *file,
8005                            GFileMeasureFlags              flags,
8006                            GCancellable                  *cancellable,
8007                            GFileMeasureProgressCallback   progress_callback,
8008                            gpointer                       progress_data,
8009                            guint64                       *disk_usage,
8010                            guint64                       *num_dirs,
8011                            guint64                       *num_files,
8012                            GError                       **error)
8013 {
8014   g_return_val_if_fail (G_IS_FILE (file), FALSE);
8015   g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
8016   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
8017
8018   return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
8019                                                       progress_callback, progress_data,
8020                                                       disk_usage, num_dirs, num_files,
8021                                                       error);
8022 }
8023
8024 /**
8025  * g_file_measure_disk_usage_async:
8026  * @file: a #GFile
8027  * @flags: #GFileMeasureFlags
8028  * @io_priority: the [I/O priority][io-priority] of the request
8029  * @cancellable: (nullable): optional #GCancellable
8030  * @progress_callback: (nullable): a #GFileMeasureProgressCallback
8031  * @progress_data: user_data for @progress_callback
8032  * @callback: (nullable): a #GAsyncReadyCallback to call when complete
8033  * @user_data: the data to pass to callback function
8034  *
8035  * Recursively measures the disk usage of @file.
8036  *
8037  * This is the asynchronous version of g_file_measure_disk_usage().  See
8038  * there for more information.
8039  *
8040  * Since: 2.38
8041  **/
8042 void
8043 g_file_measure_disk_usage_async (GFile                        *file,
8044                                  GFileMeasureFlags             flags,
8045                                  gint                          io_priority,
8046                                  GCancellable                 *cancellable,
8047                                  GFileMeasureProgressCallback  progress_callback,
8048                                  gpointer                      progress_data,
8049                                  GAsyncReadyCallback           callback,
8050                                  gpointer                      user_data)
8051 {
8052   g_return_if_fail (G_IS_FILE (file));
8053   g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
8054
8055   G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
8056                                                      progress_callback, progress_data,
8057                                                      callback, user_data);
8058 }
8059
8060 /**
8061  * g_file_measure_disk_usage_finish:
8062  * @file: a #GFile
8063  * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
8064  * @disk_usage: (out) (optional): the number of bytes of disk space used
8065  * @num_dirs: (out) (optional): the number of directories encountered
8066  * @num_files: (out) (optional): the number of non-directories encountered
8067  * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
8068  *
8069  * Collects the results from an earlier call to
8070  * g_file_measure_disk_usage_async().  See g_file_measure_disk_usage() for
8071  * more information.
8072  *
8073  * Returns: %TRUE if successful, with the out parameters set.
8074  *          %FALSE otherwise, with @error set.
8075  *
8076  * Since: 2.38
8077  **/
8078 gboolean
8079 g_file_measure_disk_usage_finish (GFile         *file,
8080                                   GAsyncResult  *result,
8081                                   guint64       *disk_usage,
8082                                   guint64       *num_dirs,
8083                                   guint64       *num_files,
8084                                   GError       **error)
8085 {
8086   g_return_val_if_fail (G_IS_FILE (file), FALSE);
8087   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
8088
8089   return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
8090 }
8091
8092 /**
8093  * g_file_start_mountable:
8094  * @file: input #GFile
8095  * @flags: flags affecting the operation
8096  * @start_operation: (nullable): a #GMountOperation, or %NULL to avoid user interaction
8097  * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
8098  * @callback: (nullable): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
8099  * @user_data: the data to pass to callback function
8100  *
8101  * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
8102  * Using @start_operation, you can request callbacks when, for instance,
8103  * passwords are needed during authentication.
8104  *
8105  * If @cancellable is not %NULL, then the operation can be cancelled by
8106  * triggering the cancellable object from another thread. If the operation
8107  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8108  *
8109  * When the operation is finished, @callback will be called.
8110  * You can then call g_file_mount_mountable_finish() to get
8111  * the result of the operation.
8112  *
8113  * Since: 2.22
8114  */
8115 void
8116 g_file_start_mountable (GFile               *file,
8117                         GDriveStartFlags     flags,
8118                         GMountOperation     *start_operation,
8119                         GCancellable        *cancellable,
8120                         GAsyncReadyCallback  callback,
8121                         gpointer             user_data)
8122 {
8123   GFileIface *iface;
8124
8125   g_return_if_fail (G_IS_FILE (file));
8126
8127   iface = G_FILE_GET_IFACE (file);
8128
8129   if (iface->start_mountable == NULL)
8130     {
8131       g_task_report_new_error (file, callback, user_data,
8132                                g_file_start_mountable,
8133                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8134                                _("Operation not supported"));
8135       return;
8136     }
8137
8138   (* iface->start_mountable) (file,
8139                               flags,
8140                               start_operation,
8141                               cancellable,
8142                               callback,
8143                               user_data);
8144 }
8145
8146 /**
8147  * g_file_start_mountable_finish:
8148  * @file: input #GFile
8149  * @result: a #GAsyncResult
8150  * @error: a #GError, or %NULL
8151  *
8152  * Finishes a start operation. See g_file_start_mountable() for details.
8153  *
8154  * Finish an asynchronous start operation that was started
8155  * with g_file_start_mountable().
8156  *
8157  * Returns: %TRUE if the operation finished successfully. %FALSE
8158  * otherwise.
8159  *
8160  * Since: 2.22
8161  */
8162 gboolean
8163 g_file_start_mountable_finish (GFile         *file,
8164                                GAsyncResult  *result,
8165                                GError       **error)
8166 {
8167   GFileIface *iface;
8168
8169   g_return_val_if_fail (G_IS_FILE (file), FALSE);
8170   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8171
8172   if (g_async_result_legacy_propagate_error (result, error))
8173     return FALSE;
8174   else if (g_async_result_is_tagged (result, g_file_start_mountable))
8175     return g_task_propagate_boolean (G_TASK (result), error);
8176
8177   iface = G_FILE_GET_IFACE (file);
8178   return (* iface->start_mountable_finish) (file, result, error);
8179 }
8180
8181 /**
8182  * g_file_stop_mountable:
8183  * @file: input #GFile
8184  * @flags: flags affecting the operation
8185  * @mount_operation: (nullable): a #GMountOperation,
8186  *     or %NULL to avoid user interaction.
8187  * @cancellable: (nullable): optional #GCancellable object,
8188  *     %NULL to ignore
8189  * @callback: (nullable): a #GAsyncReadyCallback to call
8190  *     when the request is satisfied, or %NULL
8191  * @user_data: the data to pass to callback function
8192  *
8193  * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
8194  *
8195  * If @cancellable is not %NULL, then the operation can be cancelled by
8196  * triggering the cancellable object from another thread. If the operation
8197  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8198  *
8199  * When the operation is finished, @callback will be called.
8200  * You can then call g_file_stop_mountable_finish() to get
8201  * the result of the operation.
8202  *
8203  * Since: 2.22
8204  */
8205 void
8206 g_file_stop_mountable (GFile               *file,
8207                        GMountUnmountFlags   flags,
8208                        GMountOperation     *mount_operation,
8209                        GCancellable        *cancellable,
8210                        GAsyncReadyCallback  callback,
8211                        gpointer             user_data)
8212 {
8213   GFileIface *iface;
8214
8215   g_return_if_fail (G_IS_FILE (file));
8216
8217   iface = G_FILE_GET_IFACE (file);
8218
8219   if (iface->stop_mountable == NULL)
8220     {
8221       g_task_report_new_error (file, callback, user_data,
8222                                g_file_stop_mountable,
8223                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8224                                _("Operation not supported"));
8225       return;
8226     }
8227
8228   (* iface->stop_mountable) (file,
8229                              flags,
8230                              mount_operation,
8231                              cancellable,
8232                              callback,
8233                              user_data);
8234 }
8235
8236 /**
8237  * g_file_stop_mountable_finish:
8238  * @file: input #GFile
8239  * @result: a #GAsyncResult
8240  * @error: a #GError, or %NULL
8241  *
8242  * Finishes a stop operation, see g_file_stop_mountable() for details.
8243  *
8244  * Finish an asynchronous stop operation that was started
8245  * with g_file_stop_mountable().
8246  *
8247  * Returns: %TRUE if the operation finished successfully.
8248  *     %FALSE otherwise.
8249  *
8250  * Since: 2.22
8251  */
8252 gboolean
8253 g_file_stop_mountable_finish (GFile         *file,
8254                               GAsyncResult  *result,
8255                               GError       **error)
8256 {
8257   GFileIface *iface;
8258
8259   g_return_val_if_fail (G_IS_FILE (file), FALSE);
8260   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8261
8262   if (g_async_result_legacy_propagate_error (result, error))
8263     return FALSE;
8264   else if (g_async_result_is_tagged (result, g_file_stop_mountable))
8265     return g_task_propagate_boolean (G_TASK (result), error);
8266
8267   iface = G_FILE_GET_IFACE (file);
8268   return (* iface->stop_mountable_finish) (file, result, error);
8269 }
8270
8271 /**
8272  * g_file_poll_mountable:
8273  * @file: input #GFile
8274  * @cancellable: optional #GCancellable object, %NULL to ignore
8275  * @callback: (nullable): a #GAsyncReadyCallback to call
8276  *     when the request is satisfied, or %NULL
8277  * @user_data: the data to pass to callback function
8278  *
8279  * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
8280  *
8281  * If @cancellable is not %NULL, then the operation can be cancelled by
8282  * triggering the cancellable object from another thread. If the operation
8283  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8284  *
8285  * When the operation is finished, @callback will be called.
8286  * You can then call g_file_mount_mountable_finish() to get
8287  * the result of the operation.
8288  *
8289  * Since: 2.22
8290  */
8291 void
8292 g_file_poll_mountable (GFile               *file,
8293                        GCancellable        *cancellable,
8294                        GAsyncReadyCallback  callback,
8295                        gpointer             user_data)
8296 {
8297   GFileIface *iface;
8298
8299   g_return_if_fail (G_IS_FILE (file));
8300
8301   iface = G_FILE_GET_IFACE (file);
8302
8303   if (iface->poll_mountable == NULL)
8304     {
8305       g_task_report_new_error (file, callback, user_data,
8306                                g_file_poll_mountable,
8307                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8308                                _("Operation not supported"));
8309       return;
8310     }
8311
8312   (* iface->poll_mountable) (file,
8313                              cancellable,
8314                              callback,
8315                              user_data);
8316 }
8317
8318 /**
8319  * g_file_poll_mountable_finish:
8320  * @file: input #GFile
8321  * @result: a #GAsyncResult
8322  * @error: a #GError, or %NULL
8323  *
8324  * Finishes a poll operation. See g_file_poll_mountable() for details.
8325  *
8326  * Finish an asynchronous poll operation that was polled
8327  * with g_file_poll_mountable().
8328  *
8329  * Returns: %TRUE if the operation finished successfully. %FALSE
8330  * otherwise.
8331  *
8332  * Since: 2.22
8333  */
8334 gboolean
8335 g_file_poll_mountable_finish (GFile         *file,
8336                               GAsyncResult  *result,
8337                               GError       **error)
8338 {
8339   GFileIface *iface;
8340
8341   g_return_val_if_fail (G_IS_FILE (file), FALSE);
8342   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8343
8344   if (g_async_result_legacy_propagate_error (result, error))
8345     return FALSE;
8346   else if (g_async_result_is_tagged (result, g_file_poll_mountable))
8347     return g_task_propagate_boolean (G_TASK (result), error);
8348
8349   iface = G_FILE_GET_IFACE (file);
8350   return (* iface->poll_mountable_finish) (file, result, error);
8351 }
8352
8353 /**
8354  * g_file_supports_thread_contexts:
8355  * @file: a #GFile
8356  *
8357  * Checks if @file supports
8358  * [thread-default contexts][g-main-context-push-thread-default-context].
8359  * If this returns %FALSE, you cannot perform asynchronous operations on
8360  * @file in a thread that has a thread-default context.
8361  *
8362  * Returns: Whether or not @file supports thread-default contexts.
8363  *
8364  * Since: 2.22
8365  */
8366 gboolean
8367 g_file_supports_thread_contexts (GFile *file)
8368 {
8369  GFileIface *iface;
8370
8371  g_return_val_if_fail (G_IS_FILE (file), FALSE);
8372
8373  iface = G_FILE_GET_IFACE (file);
8374  return iface->supports_thread_contexts;
8375 }
8376
8377 /**
8378  * g_file_load_bytes:
8379  * @file: a #GFile
8380  * @cancellable: (nullable): a #GCancellable or %NULL
8381  * @etag_out: (out) (nullable) (optional): a location to place the current
8382  *     entity tag for the file, or %NULL if the entity tag is not needed
8383  * @error: a location for a #GError or %NULL
8384  *
8385  * Loads the contents of @file and returns it as #GBytes.
8386  *
8387  * If @file is a resource:// based URI, the resulting bytes will reference the
8388  * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8389  * g_file_load_contents() and g_bytes_new_take().
8390  *
8391  * For resources, @etag_out will be set to %NULL.
8392  *
8393  * The data contained in the resulting #GBytes is always zero-terminated, but
8394  * this is not included in the #GBytes length. The resulting #GBytes should be
8395  * freed with g_bytes_unref() when no longer in use.
8396  *
8397  * Returns: (transfer full): a #GBytes or %NULL and @error is set
8398  *
8399  * Since: 2.56
8400  */
8401 GBytes *
8402 g_file_load_bytes (GFile         *file,
8403                    GCancellable  *cancellable,
8404                    gchar        **etag_out,
8405                    GError       **error)
8406 {
8407   gchar *contents;
8408   gsize len;
8409
8410   g_return_val_if_fail (G_IS_FILE (file), NULL);
8411   g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
8412   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8413
8414   if (etag_out != NULL)
8415     *etag_out = NULL;
8416
8417   if (g_file_has_uri_scheme (file, "resource"))
8418     {
8419       GBytes *bytes;
8420       gchar *uri, *unescaped;
8421
8422       uri = g_file_get_uri (file);
8423       unescaped = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
8424       g_free (uri);
8425
8426       bytes = g_resources_lookup_data (unescaped, G_RESOURCE_LOOKUP_FLAGS_NONE, error);
8427       g_free (unescaped);
8428
8429       return bytes;
8430     }
8431
8432   /* contents is guaranteed to be \0 terminated */
8433   if (g_file_load_contents (file, cancellable, &contents, &len, etag_out, error))
8434     return g_bytes_new_take (g_steal_pointer (&contents), len);
8435
8436   return NULL;
8437 }
8438
8439 static void
8440 g_file_load_bytes_cb (GObject      *object,
8441                       GAsyncResult *result,
8442                       gpointer      user_data)
8443 {
8444   GFile *file = G_FILE (object);
8445   GTask *task = user_data;
8446   GError *error = NULL;
8447   gchar *etag = NULL;
8448   gchar *contents = NULL;
8449   gsize len = 0;
8450
8451   g_file_load_contents_finish (file, result, &contents, &len, &etag, &error);
8452   g_task_set_task_data (task, g_steal_pointer (&etag), g_free);
8453
8454   if (error != NULL)
8455     g_task_return_error (task, g_steal_pointer (&error));
8456   else
8457     g_task_return_pointer (task,
8458                            g_bytes_new_take (g_steal_pointer (&contents), len),
8459                            (GDestroyNotify)g_bytes_unref);
8460
8461   g_object_unref (task);
8462 }
8463
8464 /**
8465  * g_file_load_bytes_async:
8466  * @file: a #GFile
8467  * @cancellable: (nullable): a #GCancellable or %NULL
8468  * @callback: (scope async): a #GAsyncReadyCallback to call when the
8469  *     request is satisfied
8470  * @user_data: (closure): the data to pass to callback function
8471  *
8472  * Asynchronously loads the contents of @file as #GBytes.
8473  *
8474  * If @file is a resource:// based URI, the resulting bytes will reference the
8475  * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8476  * g_file_load_contents_async() and g_bytes_new_take().
8477  *
8478  * @callback should call g_file_load_bytes_finish() to get the result of this
8479  * asynchronous operation.
8480  *
8481  * See g_file_load_bytes() for more information.
8482  *
8483  * Since: 2.56
8484  */
8485 void
8486 g_file_load_bytes_async (GFile               *file,
8487                          GCancellable        *cancellable,
8488                          GAsyncReadyCallback  callback,
8489                          gpointer             user_data)
8490 {
8491   GError *error = NULL;
8492   GBytes *bytes;
8493   GTask *task;
8494
8495   g_return_if_fail (G_IS_FILE (file));
8496   g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
8497
8498   task = g_task_new (file, cancellable, callback, user_data);
8499   g_task_set_source_tag (task, g_file_load_bytes_async);
8500
8501   if (!g_file_has_uri_scheme (file, "resource"))
8502     {
8503       g_file_load_contents_async (file,
8504                                   cancellable,
8505                                   g_file_load_bytes_cb,
8506                                   g_steal_pointer (&task));
8507       return;
8508     }
8509
8510   bytes = g_file_load_bytes (file, cancellable, NULL, &error);
8511
8512   if (bytes == NULL)
8513     g_task_return_error (task, g_steal_pointer (&error));
8514   else
8515     g_task_return_pointer (task,
8516                            g_steal_pointer (&bytes),
8517                            (GDestroyNotify)g_bytes_unref);
8518
8519   g_object_unref (task);
8520 }
8521
8522 /**
8523  * g_file_load_bytes_finish:
8524  * @file: a #GFile
8525  * @result: a #GAsyncResult provided to the callback
8526  * @etag_out: (out) (nullable) (optional): a location to place the current
8527  *     entity tag for the file, or %NULL if the entity tag is not needed
8528  * @error: a location for a #GError, or %NULL
8529  *
8530  * Completes an asynchronous request to g_file_load_bytes_async().
8531  *
8532  * For resources, @etag_out will be set to %NULL.
8533  *
8534  * The data contained in the resulting #GBytes is always zero-terminated, but
8535  * this is not included in the #GBytes length. The resulting #GBytes should be
8536  * freed with g_bytes_unref() when no longer in use.
8537  *
8538  * See g_file_load_bytes() for more information.
8539  *
8540  * Returns: (transfer full): a #GBytes or %NULL and @error is set
8541  *
8542  * Since: 2.56
8543  */
8544 GBytes *
8545 g_file_load_bytes_finish (GFile         *file,
8546                           GAsyncResult  *result,
8547                           gchar        **etag_out,
8548                           GError       **error)
8549 {
8550   GBytes *bytes;
8551
8552   g_return_val_if_fail (G_IS_FILE (file), NULL);
8553   g_return_val_if_fail (G_IS_TASK (result), NULL);
8554   g_return_val_if_fail (g_task_is_valid (G_TASK (result), file), NULL);
8555   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8556
8557   bytes = g_task_propagate_pointer (G_TASK (result), error);
8558
8559   if (etag_out != NULL)
8560     *etag_out = g_strdup (g_task_get_task_data (G_TASK (result)));
8561
8562   return bytes;
8563 }