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