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