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