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