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