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