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