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