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