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