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