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