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