Add async version of find_enclosing_mount with default implementation.
[platform/upstream/glib.git] / gio / gfile.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #ifdef HAVE_PWD_H
27 #include <pwd.h>
28 #endif
29 #include "gfile.h"
30 #include "gvfs.h"
31 #include "gioscheduler.h"
32 #include <glocalfile.h>
33 #include "gsimpleasyncresult.h"
34 #include "gfileattribute-priv.h"
35 #include "gpollfilemonitor.h"
36 #include "glibintl.h"
37
38 #include "gioalias.h"
39
40 /**
41  * SECTION:gfile
42  * @short_description: File and Directory Handling
43  * @include: gio.h
44  * @see_also: #GFileInfo, #GFileEnumerator
45  * 
46  * #GFile is a high level abstraction for manipulating files on a 
47  * virtual file system. #GFile<!-- -->s are lightweight, immutable 
48  * objects that do no I/O upon creation. It is necessary to understand that
49  * #GFile objects do not represent files, merely a handle to a file. All
50  * file I/O is implemented as streaming operations (see #GInputStream and 
51  * #GOutputStream).
52  * 
53  * To construct a #GFile, you can use: 
54  * g_file_new_for_path() if you have a path.
55  * g_file_new_for_uri() if you have a URI.
56  * g_file_new_for_commandline_arg() for a command line argument.
57  * 
58  * You can move through the file system with #GFile handles with
59  * g_file_get_parent() to get a handle to the parent directory.
60  * g_file_get_child() to get a handle to a child within a directory.
61  * g_file_resolve_relative_path() to resolve a relative path between
62  * two #GFile<!-- -->s.
63  * 
64  * Many #GFile operations have both synchronous and asynchronous versions 
65  * to suit your application. Asynchronous versions of synchronous functions 
66  * simply have _async() appended to their function names. The asynchronous 
67  * I/O functions call a #GAsyncReadyCallback which is then used to finalize 
68  * the operation, producing a GAsyncResult which is then passed to the 
69  * function's matching _finish() 
70  * operation. 
71  *
72  * Some #GFile operations do not have synchronous analogs, as they may
73  * take a very long time to finish, and blocking may leave an application
74  * unusable. Notable cases include:
75  * g_file_mount_mountable() to mount a mountable file.
76  * g_file_unmount_mountable() to unmount a mountable file.
77  * g_file_eject_mountable() to eject a mountable file.
78  * 
79  * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
80  * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for 
81  * short. Entity tags are somewhat like a more abstract version of the 
82  * traditional mtime, and can be used to quickly determine if the file has
83  * been modified from the version on the file system. See the HTTP 1.1 
84  * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
85  * for HTTP Etag headers, which are a very similar concept.
86  * </para>
87  **/
88
89 static void g_file_base_init (gpointer g_class);
90 static void g_file_class_init (gpointer g_class,
91                                gpointer class_data);
92
93 static void               g_file_real_query_info_async            (GFile                *file,
94                                                                    const char           *attributes,
95                                                                    GFileQueryInfoFlags   flags,
96                                                                    int                   io_priority,
97                                                                    GCancellable         *cancellable,
98                                                                    GAsyncReadyCallback   callback,
99                                                                    gpointer              user_data);
100 static GFileInfo *        g_file_real_query_info_finish           (GFile                *file,
101                                                                    GAsyncResult         *res,
102                                                                    GError              **error);
103 static void               g_file_real_enumerate_children_async    (GFile                *file,
104                                                                    const char           *attributes,
105                                                                    GFileQueryInfoFlags   flags,
106                                                                    int                   io_priority,
107                                                                    GCancellable         *cancellable,
108                                                                    GAsyncReadyCallback   callback,
109                                                                    gpointer              user_data);
110 static GFileEnumerator *  g_file_real_enumerate_children_finish   (GFile                *file,
111                                                                    GAsyncResult         *res,
112                                                                    GError              **error);
113 static void               g_file_real_read_async                  (GFile                *file,
114                                                                    int                   io_priority,
115                                                                    GCancellable         *cancellable,
116                                                                    GAsyncReadyCallback   callback,
117                                                                    gpointer              user_data);
118 static GFileInputStream * g_file_real_read_finish                 (GFile                *file,
119                                                                    GAsyncResult         *res,
120                                                                    GError              **error);
121 static void               g_file_real_append_to_async             (GFile                *file,
122                                                                    GFileCreateFlags      flags,
123                                                                    int                   io_priority,
124                                                                    GCancellable         *cancellable,
125                                                                    GAsyncReadyCallback   callback,
126                                                                    gpointer              user_data);
127 static GFileOutputStream *g_file_real_append_to_finish            (GFile                *file,
128                                                                    GAsyncResult         *res,
129                                                                    GError              **error);
130 static void               g_file_real_create_async                (GFile                *file,
131                                                                    GFileCreateFlags      flags,
132                                                                    int                   io_priority,
133                                                                    GCancellable         *cancellable,
134                                                                    GAsyncReadyCallback   callback,
135                                                                    gpointer              user_data);
136 static GFileOutputStream *g_file_real_create_finish               (GFile                *file,
137                                                                    GAsyncResult         *res,
138                                                                    GError              **error);
139 static void               g_file_real_replace_async               (GFile                *file,
140                                                                    const char           *etag,
141                                                                    gboolean              make_backup,
142                                                                    GFileCreateFlags      flags,
143                                                                    int                   io_priority,
144                                                                    GCancellable         *cancellable,
145                                                                    GAsyncReadyCallback   callback,
146                                                                    gpointer              user_data);
147 static GFileOutputStream *g_file_real_replace_finish              (GFile                *file,
148                                                                    GAsyncResult         *res,
149                                                                    GError              **error);
150 static gboolean           g_file_real_set_attributes_from_info    (GFile                *file,
151                                                                    GFileInfo            *info,
152                                                                    GFileQueryInfoFlags   flags,
153                                                                    GCancellable         *cancellable,
154                                                                    GError              **error);
155 static void               g_file_real_set_display_name_async      (GFile                *file,
156                                                                    const char           *display_name,
157                                                                    int                   io_priority,
158                                                                    GCancellable         *cancellable,
159                                                                    GAsyncReadyCallback   callback,
160                                                                    gpointer              user_data);
161 static GFile *            g_file_real_set_display_name_finish     (GFile                *file,
162                                                                    GAsyncResult         *res,
163                                                                    GError              **error);
164 static void               g_file_real_set_attributes_async        (GFile                *file,
165                                                                    GFileInfo            *info,
166                                                                    GFileQueryInfoFlags   flags,
167                                                                    int                   io_priority,
168                                                                    GCancellable         *cancellable,
169                                                                    GAsyncReadyCallback   callback,
170                                                                    gpointer              user_data);
171 static gboolean           g_file_real_set_attributes_finish       (GFile                *file,
172                                                                    GAsyncResult         *res,
173                                                                    GFileInfo           **info,
174                                                                    GError              **error);
175 static void               g_file_real_find_enclosing_mount_async  (GFile                *file,
176                                                                    int                   io_priority,
177                                                                    GCancellable         *cancellable,
178                                                                    GAsyncReadyCallback   callback,
179                                                                    gpointer              user_data);
180 static GMount *           g_file_real_find_enclosing_mount_finish (GFile                *file,
181                                                                    GAsyncResult         *res,
182                                                                    GError              **error);
183
184
185 GType
186 g_file_get_type (void)
187 {
188   static GType file_type = 0;
189
190   if (! file_type)
191     {
192       static const GTypeInfo file_info =
193       {
194         sizeof (GFileIface), /* class_size */
195         g_file_base_init,   /* base_init */
196         NULL,           /* base_finalize */
197         g_file_class_init,
198         NULL,           /* class_finalize */
199         NULL,           /* class_data */
200         0,
201         0,              /* n_preallocs */
202         NULL
203       };
204
205       file_type =
206         g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
207                                 &file_info, 0);
208
209       g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
210     }
211
212   return file_type;
213 }
214
215 static void
216 g_file_class_init (gpointer g_class,
217                    gpointer class_data)
218 {
219   GFileIface *iface = g_class;
220
221   iface->enumerate_children_async = g_file_real_enumerate_children_async;
222   iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
223   iface->set_display_name_async = g_file_real_set_display_name_async;
224   iface->set_display_name_finish = g_file_real_set_display_name_finish;
225   iface->query_info_async = g_file_real_query_info_async;
226   iface->query_info_finish = g_file_real_query_info_finish;
227   iface->set_attributes_async = g_file_real_set_attributes_async;
228   iface->set_attributes_finish = g_file_real_set_attributes_finish;
229   iface->read_async = g_file_real_read_async;
230   iface->read_finish = g_file_real_read_finish;
231   iface->append_to_async = g_file_real_append_to_async;
232   iface->append_to_finish = g_file_real_append_to_finish;
233   iface->create_async = g_file_real_create_async;
234   iface->create_finish = g_file_real_create_finish;
235   iface->replace_async = g_file_real_replace_async;
236   iface->replace_finish = g_file_real_replace_finish;
237   iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
238   iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
239   iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
240 }
241
242 static void
243 g_file_base_init (gpointer g_class)
244 {
245 }
246
247
248 /**
249  * g_file_is_native:
250  * @file: input #GFile.
251  *
252  * Checks to see if a file is native to the platform.
253  *
254  * A native file s one expressed in the platform-native filename format,
255  * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
256  * as it might be on a locally mounted remote filesystem.
257  *
258  * On some systems non-native files may be available using
259  * the native filesystem via a userspace filesystem (FUSE), in
260  * these cases this call will return %FALSE, but g_file_get_path()
261  * will still return a native path.
262  *
263  * This call does no blocking i/o.
264  * 
265  * Returns: %TRUE if file is native. 
266  **/
267 gboolean
268 g_file_is_native (GFile *file)
269 {
270   GFileIface *iface;
271
272   g_return_val_if_fail (G_IS_FILE (file), FALSE);
273
274   iface = G_FILE_GET_IFACE (file);
275
276   return (* iface->is_native) (file);
277 }
278
279
280 /**
281  * g_file_has_uri_scheme: 
282  * @file: input #GFile.
283  * @uri_scheme: a string containing a URI scheme.
284  *
285  * Checks to see if a #GFile has a given URI scheme.
286  *
287  * This call does no blocking i/o.
288  * 
289  * Returns: %TRUE if #GFile's backend supports the
290  *     given URI scheme, %FALSE if URI scheme is %NULL,
291  *     not supported, or #GFile is invalid.
292  **/
293 gboolean
294 g_file_has_uri_scheme (GFile      *file,
295                        const char *uri_scheme)
296 {
297   GFileIface *iface;
298   
299   g_return_val_if_fail (G_IS_FILE (file), FALSE);
300   g_return_val_if_fail (uri_scheme != NULL, FALSE);
301
302   iface = G_FILE_GET_IFACE (file);
303
304   return (* iface->has_uri_scheme) (file, uri_scheme);
305 }
306
307
308 /**
309  * g_file_get_uri_scheme:
310  * @file: input #GFile.
311  *
312  * Gets the URI scheme for a #GFile.
313  * RFC 3986 decodes the scheme as:
314  * <programlisting>
315  * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] 
316  * </programlisting>
317  * Common schemes include "file", "http", "ftp", etc. 
318  *
319  * This call does no blocking i/o.
320  * 
321  * Returns: a string containing the URI scheme for the given 
322  *     #GFile. The returned string should be freed with g_free() 
323  *     when no longer needed.
324  **/
325 char *
326 g_file_get_uri_scheme (GFile *file)
327 {
328   GFileIface *iface;
329
330   g_return_val_if_fail (G_IS_FILE (file), NULL);
331
332   iface = G_FILE_GET_IFACE (file);
333
334   return (* iface->get_uri_scheme) (file);
335 }
336
337
338 /**
339  * g_file_get_basename:
340  * @file: input #GFile.
341  *
342  * Gets the base name (the last component of the path) for a given #GFile.
343  *
344  * If called for the top level of a system (such as the filesystem root
345  * or a uri like sftp://host/ it will return a single directory separator
346  * (and on Windows, possibly a drive letter).
347  *
348  * This call does no blocking i/o.
349  * 
350  * Returns: string containing the #GFile's base name, or %NULL 
351  *     if given #GFile is invalid. The returned string should be 
352  *     freed with g_free() when no longer needed.
353  **/
354 char *
355 g_file_get_basename (GFile *file)
356 {
357   GFileIface *iface;
358   
359   g_return_val_if_fail (G_IS_FILE (file), NULL);
360
361   iface = G_FILE_GET_IFACE (file);
362
363   return (* iface->get_basename) (file);
364 }
365
366 /**
367  * g_file_get_path:
368  * @file: input #GFile.
369  *
370  * Gets the local pathname for #GFile, if one exists. 
371  *
372  * This call does no blocking i/o.
373  * 
374  * Returns: string containing the #GFile's path, or %NULL if 
375  *     no such path exists. The returned string should be 
376  *     freed with g_free() when no longer needed.
377  **/
378 char *
379 g_file_get_path (GFile *file)
380 {
381   GFileIface *iface;
382
383   g_return_val_if_fail (G_IS_FILE (file), NULL);
384
385   iface = G_FILE_GET_IFACE (file);
386
387   return (* iface->get_path) (file);
388 }
389
390 /**
391  * g_file_get_uri:
392  * @file: input #GFile.
393  *
394  * Gets the URI for the @file.
395  *
396  * This call does no blocking i/o.
397  * 
398  * Returns: a string containing the #GFile's URI.
399  *     The returned string should be freed with g_free() when no longer needed.
400  **/
401 char *
402 g_file_get_uri (GFile *file)
403 {
404   GFileIface *iface;
405   
406   g_return_val_if_fail (G_IS_FILE (file), NULL);
407
408   iface = G_FILE_GET_IFACE (file);
409
410   return (* iface->get_uri) (file);
411 }
412
413 /**
414  * g_file_get_parse_name:
415  * @file: input #GFile.
416  *
417  * Gets the parse name of the @file.
418  * A parse name is a UTF-8 string that describes the
419  * file such that one can get the #GFile back using
420  * g_file_parse_name().
421  *
422  * This is generally used to show the #GFile as a nice
423  * string in a user interface, like in a location entry.
424  *
425  * For local files with names that can safely be converted
426  * to UTF8 the pathname is used, otherwise the IRI is used
427  * (a form of URI that allows UTF8 characters unescaped).
428  *
429  * This call does no blocking i/o.
430  * 
431  * Returns: a string containing the #GFile's parse name. The returned 
432  *     string should be freed with g_free() when no longer needed.
433  **/
434 char *
435 g_file_get_parse_name (GFile *file)
436 {
437   GFileIface *iface;
438   
439   g_return_val_if_fail (G_IS_FILE (file), NULL);
440
441   iface = G_FILE_GET_IFACE (file);
442
443   return (* iface->get_parse_name) (file);
444 }
445
446 /**
447  * g_file_dup:
448  * @file: input #GFile.
449  *
450  * Duplicates a #GFile handle. This operation does not duplicate 
451  * the actual file or directory represented by the #GFile; see 
452  * g_file_copy() if attempting to copy a file. 
453  *
454  * This call does no blocking i/o.
455  * 
456  * Returns: #GFile that is a duplicate of the given #GFile. 
457  **/
458 GFile *
459 g_file_dup (GFile *file)
460 {
461   GFileIface *iface;
462   
463   g_return_val_if_fail (G_IS_FILE (file), NULL);
464
465   iface = G_FILE_GET_IFACE (file);
466
467   return (* iface->dup) (file);
468 }
469
470 /**
471  * g_file_hash:
472  * @file: #gconstpointer to a #GFile.
473  *
474  * Creates a hash value for a #GFile.
475  *
476  * This call does no blocking i/o.
477  * 
478  * Returns: 0 if @file is not a valid #GFile, otherwise an 
479  *     integer that can be used as hash value for the #GFile. 
480  *     This function is intended for easily hashing a #GFile to 
481  *     add to a #GHashTable or similar data structure.
482  **/
483 guint
484 g_file_hash (gconstpointer file)
485 {
486   GFileIface *iface;
487   
488   g_return_val_if_fail (G_IS_FILE (file), 0);
489
490   iface = G_FILE_GET_IFACE (file);
491
492   return (* iface->hash) ((GFile *)file);
493 }
494
495 /**
496  * g_file_equal:
497  * @file1: the first #GFile.
498  * @file2: the second #GFile.
499  *
500  * Checks equality of two given #GFile<!-- -->s
501  *
502  * This call does no blocking i/o.
503  * 
504  * Returns: %TRUE if @file1 and @file2 are equal.
505  *     %FALSE if either is not a #GFile.
506  **/
507 gboolean
508 g_file_equal (GFile *file1,
509               GFile *file2)
510 {
511   GFileIface *iface;
512   
513   g_return_val_if_fail (G_IS_FILE (file1), FALSE);
514   g_return_val_if_fail (G_IS_FILE (file2), FALSE);
515   
516   if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
517     return FALSE;
518
519   iface = G_FILE_GET_IFACE (file1);
520   
521   return (* iface->equal) (file1, file2);
522 }
523
524
525 /**
526  * g_file_get_parent:
527  * @file: input #GFile.
528  *
529  * Gets the parent directory for the @file. 
530  * If the @file represents the root directory of the 
531  * file system, then %NULL will be returned.
532  *
533  * This call does no blocking i/o.
534  * 
535  * Returns: a #GFile structure to the parent of the given
536  *     #GFile or %NULL if there is no parent. 
537  **/
538 GFile *
539 g_file_get_parent (GFile *file)
540 {
541   GFileIface *iface;
542   
543   g_return_val_if_fail (G_IS_FILE (file), NULL);
544
545   iface = G_FILE_GET_IFACE (file);
546
547   return (* iface->get_parent) (file);
548 }
549
550 /**
551  * g_file_get_child:
552  * @file: input #GFile.
553  * @name: string containing the child's name.
554  *
555  * Gets a specific child of @file with name equal to @name.
556  *
557  * Note that the file with that specific name might not exist, but
558  * you can still have a #GFile that points to it. You can use this
559  * for instance to create that file.
560  *
561  * This call does no blocking i/o.
562  * 
563  * Returns: a #GFile to a child specified by @name.
564  **/
565 GFile *
566 g_file_get_child (GFile      *file,
567                   const char *name)
568 {
569   g_return_val_if_fail (G_IS_FILE (file), NULL);
570   g_return_val_if_fail (name != NULL, NULL);
571
572   return g_file_resolve_relative_path (file, name);
573 }
574
575 /**
576  * g_file_get_child_for_display_name:
577  * @file: input #GFile.
578  * @display_name: string to a possible child.
579  * @error: #GError.
580  *
581  * Gets the child of @file for a given @display_name (i.e. a UTF8
582  * version of the name). If this function fails, it returns %NULL and @error will be 
583  * set. This is very useful when constructing a GFile for a new file
584  * and the user entered the filename in the user interface, for instance
585  * when you select a directory and type a filename in the file selector.
586  * 
587  * This call does no blocking i/o.
588  * 
589  * Returns: a #GFile to the specified child, or 
590  *     %NULL if the display name couldn't be converted.  
591  **/
592 GFile *
593 g_file_get_child_for_display_name (GFile      *file,
594                                    const char *display_name,
595                                    GError **error)
596 {
597   GFileIface *iface;
598   
599   g_return_val_if_fail (G_IS_FILE (file), NULL);
600   g_return_val_if_fail (display_name != NULL, NULL);
601
602   iface = G_FILE_GET_IFACE (file);
603
604   return (* iface->get_child_for_display_name) (file, display_name, error);
605 }
606
607 /**
608  * g_file_contains_file:
609  * @parent: input #GFile.
610  * @descendant: input #GFile.
611  * 
612  * Checks whether @parent (recursively) contains the specified @descendant.
613  * 
614  * This call does no blocking i/o.
615  * 
616  * Returns:  %TRUE if the @descendant's parent, grandparent, etc is @parent. %FALSE otherwise.
617  **/
618 gboolean
619 g_file_contains_file (GFile *parent,
620                       GFile *descendant)
621 {
622   GFileIface *iface;
623   
624   g_return_val_if_fail (G_IS_FILE (parent), FALSE);
625   g_return_val_if_fail (G_IS_FILE (descendant), FALSE);
626
627   if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
628     return FALSE;
629   
630   iface = G_FILE_GET_IFACE (parent);
631
632   return (* iface->contains_file) (parent, descendant);
633 }
634
635 /**
636  * g_file_get_relative_path:
637  * @parent: input #GFile.
638  * @descendant: input #GFile.
639  *
640  * Gets the path for @descendant relative to @parent. 
641  *
642  * This call does no blocking i/o.
643  * 
644  * Returns: string with the relative path from @descendant 
645  *     to @parent, or %NULL if @descendant is not a descendant of @parent. The returned string should be freed with 
646  *     g_free() when no longer needed.
647  **/
648 char *
649 g_file_get_relative_path (GFile *parent,
650                           GFile *descendant)
651 {
652   GFileIface *iface;
653   
654   g_return_val_if_fail (G_IS_FILE (parent), NULL);
655   g_return_val_if_fail (G_IS_FILE (descendant), NULL);
656
657   if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
658     return NULL;
659   
660   iface = G_FILE_GET_IFACE (parent);
661
662   return (* iface->get_relative_path) (parent, descendant);
663 }
664
665 /**
666  * g_file_resolve_relative_path:
667  * @file: input #GFile.
668  * @relative_path: a given relative path string.
669  *
670  * Resolves a relative path for @file to an absolute path.
671  *
672  * This call does no blocking i/o.
673  * 
674  * Returns: #GFile to the resolved path. %NULL if @relative_path 
675  *     is %NULL or if @file is invalid.
676  **/
677 GFile *
678 g_file_resolve_relative_path (GFile      *file,
679                               const char *relative_path)
680 {
681   GFileIface *iface;
682
683   g_return_val_if_fail (G_IS_FILE (file), NULL);
684   g_return_val_if_fail (relative_path != NULL, NULL);
685
686   iface = G_FILE_GET_IFACE (file);
687
688   return (* iface->resolve_relative_path) (file, relative_path);
689 }
690
691 /**
692  * g_file_enumerate_children:
693  * @file: input #GFile.
694  * @attributes: an attribute query string.
695  * @flags: a set of #GFileQueryInfoFlags.
696  * @cancellable: optional #GCancellable object, %NULL to ignore.
697  * @error: #GError for error reporting.
698  *
699  * Gets the requested information about the files in a directory. The result
700  * is a #GFileEnumerator object that will give out #GFileInfo objects for
701  * all the files in the directory.
702  *
703  * The @attribute value is a string that specifies the file attributes that
704  * should be gathered. It is not an error if it's not possible to read a particular
705  * requested attribute from a file - it just won't be set. @attribute should
706  * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
707  * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
708  * namespace. An example attribute query be "standard::*,owner::user".
709  * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
710  *
711  * If @cancellable is not %NULL, then the operation can be cancelled by
712  * triggering the cancellable object from another thread. If the operation
713  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
714  * 
715  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
716  * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
717  * Other errors are possible too.
718  *
719  * Returns: A #GFileEnumerator if successful, %NULL on error. 
720  **/
721 GFileEnumerator *
722 g_file_enumerate_children (GFile                *file,
723                            const char           *attributes,
724                            GFileQueryInfoFlags   flags,
725                            GCancellable         *cancellable,
726                            GError              **error)
727                            
728 {
729   GFileIface *iface;
730   
731   g_return_val_if_fail (G_IS_FILE (file), NULL);
732
733   if (g_cancellable_set_error_if_cancelled (cancellable, error))
734     return NULL;
735   
736   iface = G_FILE_GET_IFACE (file);
737
738   if (iface->enumerate_children == NULL)
739     {
740       g_set_error (error, G_IO_ERROR,
741                    G_IO_ERROR_NOT_SUPPORTED,
742                    _("Operation not supported"));
743       return NULL;
744     }
745
746   return (* iface->enumerate_children) (file, attributes, flags,
747                                         cancellable, error);
748 }
749
750 /**
751  * g_file_enumerate_children_async:
752  * @file: input #GFile.
753  * @attributes: an attribute query string.
754  * @flags: a set of #GFileQueryInfoFlags.
755  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
756  *     of the request.
757  * @cancellable: optional #GCancellable object, %NULL to ignore.
758  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
759  * @user_data: the data to pass to callback function
760  *
761  * Asynchronously gets the requested information about the files in a directory. The result
762  * is a #GFileEnumerator object that will give out #GFileInfo objects for
763  * all the files in the directory.
764  *
765  * For more details, see g_file_enumerate_children() which is
766  * the synchronous version of this call.
767  *
768  * When the operation is finished, @callback will be called. You can then call
769  * g_file_enumerate_children_finish() to get the result of the operation.
770  **/
771 void
772 g_file_enumerate_children_async (GFile               *file,
773                                  const char          *attributes,
774                                  GFileQueryInfoFlags  flags,
775                                  int                  io_priority,
776                                  GCancellable        *cancellable,
777                                  GAsyncReadyCallback  callback,
778                                  gpointer             user_data)
779 {
780   GFileIface *iface;
781
782   g_return_if_fail (G_IS_FILE (file));
783
784   iface = G_FILE_GET_IFACE (file);
785   (* iface->enumerate_children_async) (file,
786                                        attributes,
787                                        flags,
788                                        io_priority,
789                                        cancellable,
790                                        callback,
791                                        user_data);
792 }
793
794 /**
795  * g_file_enumerate_children_finish:
796  * @file: input #GFile.
797  * @res: a #GAsyncResult.
798  * @error: a #GError.
799  * 
800  * Finishes an async enumerate children operation.
801  * See g_file_enumerate_children_async().
802  *
803  * Returns: a #GFileEnumerator or %NULL if an error occurred.
804  **/
805 GFileEnumerator *
806 g_file_enumerate_children_finish (GFile         *file,
807                                   GAsyncResult  *res,
808                                   GError       **error)
809 {
810   GFileIface *iface;
811   
812   g_return_val_if_fail (G_IS_FILE (file), NULL);
813   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
814
815   if (G_IS_SIMPLE_ASYNC_RESULT (res))
816     {
817       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
818       if (g_simple_async_result_propagate_error (simple, error))
819         return NULL;
820     }
821   
822   iface = G_FILE_GET_IFACE (file);
823   return (* iface->enumerate_children_finish) (file, res, error);
824 }
825
826 /**
827  * g_file_query_exists:
828  * @file: input #GFile.
829  * @cancellable: optional #GCancellable object, %NULL to ignore.
830  *
831  * Utility function to check if a particular file exists. This is
832  * implemented using g_file_query_info() and as such does blocking I/O.
833  *
834  * Note that in many cases it is racy to first check for file existance
835  * and then execute something based on the outcome of that, because the
836  * file might have been created or removed inbetween the operations. The
837  * general approach to handling that is to not check, but just do the
838  * operation and handle the errors as they come.
839  *
840  * As an example of race-free checking. Take the case of reading a file, and
841  * if it doesn't exist, create it. There are two racy versions: read it, and
842  * on error create it; and: check if it exists, if not create it. These
843  * can both result in two processes creating the file (with perhaps a partially
844  * written file as the result). The correct approach is to always try to create
845  * the file with g_file_create() which will either atomically create the file
846  * or fail with an G_IO_ERROR_EXISTS error.
847  *
848  * However, in many cases an existance check is useful in a user
849  * interface, for instance to make a menu item sensitive/insensitive, so that
850  * you don't have to fool users that something is possible and then just show
851  * and error dialog. If you do this, you should make sure to also handle the
852  * errors that can happen due to races when you execute the operation.
853  * 
854  * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
855  */
856 gboolean
857 g_file_query_exists (GFile *file,
858                      GCancellable *cancellable)
859 {
860   GFileInfo *info;
861
862   info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
863                             G_FILE_QUERY_INFO_NONE,
864                             cancellable, NULL);
865   if (info != NULL)
866     {
867       g_object_unref (info);
868       return TRUE;
869     }
870   
871   return FALSE;
872 }
873
874 /**
875  * g_file_query_info:
876  * @file: input #GFile.
877  * @attributes: an attribute query string.
878  * @flags: a set of #GFileQueryInfoFlags.
879  * @cancellable: optional #GCancellable object, %NULL to ignore.
880  * @error: a #GError.
881  *
882  * Gets the requested information about specified @file. The result
883  * is a #GFileInfo objects that contains key-value attributes (like type or size
884  * for the file.
885  *
886  * The @attribute value is a string that specifies the file attributes that
887  * should be gathered. It is not an error if it's not possible to read a particular
888  * requested attribute from a file - it just won't be set. @attribute should
889  * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
890  * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
891  * namespace. An example attribute query be "standard::*,owner::user".
892  * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
893  * 
894  * If @cancellable is not %NULL, then the operation can be cancelled by
895  * triggering the cancellable object from another thread. If the operation
896  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
897  *
898  * For symlinks, normally the information about the target of the
899  * symlink is returned, rather than information about the symlink itself.
900  * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
901  * information about the symlink itself will be returned. Also, for symlinks
902  * that points to non-existing files the information about the symlink itself
903  * will be returned.
904  *
905  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
906  * Other errors are possible too, and depend on what kind of filesystem the file is on.
907  *
908  * Returns: a #GFileInfo for the given @file, or %NULL on error.
909  **/
910 GFileInfo *
911 g_file_query_info (GFile                *file,
912                    const char           *attributes,
913                    GFileQueryInfoFlags   flags,
914                    GCancellable         *cancellable,
915                    GError              **error)
916 {
917   GFileIface *iface;
918   
919   g_return_val_if_fail (G_IS_FILE (file), NULL);
920
921   if (g_cancellable_set_error_if_cancelled (cancellable, error))
922     return NULL;
923   
924   iface = G_FILE_GET_IFACE (file);
925
926   if (iface->query_info == NULL)
927     {
928       g_set_error (error, G_IO_ERROR,
929                    G_IO_ERROR_NOT_SUPPORTED,
930                    _("Operation not supported"));
931       return NULL;
932     }
933   
934   return (* iface->query_info) (file, attributes, flags, cancellable, error);
935 }
936
937 /**
938  * g_file_query_info_async:
939  * @file: input #GFile.
940  * @attributes: an attribute query string.
941  * @flags: a set of #GFileQueryInfoFlags.
942  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
943  *     of the request.
944  * @cancellable: optional #GCancellable object, %NULL to ignore. 
945  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
946  * @user_data: the data to pass to callback function
947  * 
948  * Asynchronously gets the requested information about specified @file. The result
949  * is a #GFileInfo objects that contains key-value attributes (such as type or size
950  * for the file).
951  * 
952  * For more details, see g_file_query_info() which is
953  * the synchronous version of this call.
954  *
955  * When the operation is finished, @callback will be called. You can then call
956  * g_file_query_info_finish() to get the result of the operation.
957  **/
958 void
959 g_file_query_info_async (GFile               *file,
960                          const char          *attributes,
961                          GFileQueryInfoFlags  flags,
962                          int                  io_priority,
963                          GCancellable        *cancellable,
964                          GAsyncReadyCallback  callback,
965                          gpointer             user_data)
966 {
967   GFileIface *iface;
968   
969   g_return_if_fail (G_IS_FILE (file));
970
971   iface = G_FILE_GET_IFACE (file);
972   (* iface->query_info_async) (file,
973                                attributes,
974                                flags,
975                                io_priority,
976                                cancellable,
977                                callback,
978                                user_data);
979 }
980
981 /**
982  * g_file_query_info_finish:
983  * @file: input #GFile.
984  * @res: a #GAsyncResult. 
985  * @error: a #GError. 
986  * 
987  * Finishes an asynchronous file info query. 
988  * See g_file_query_info_async().
989  * 
990  * Returns: #GFileInfo for given @file or %NULL on error.
991  **/
992 GFileInfo *
993 g_file_query_info_finish (GFile         *file,
994                           GAsyncResult  *res,
995                           GError       **error)
996 {
997   GFileIface *iface;
998
999   g_return_val_if_fail (G_IS_FILE (file), NULL);
1000   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1001
1002   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1003     {
1004       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1005       if (g_simple_async_result_propagate_error (simple, error))
1006         return NULL;
1007     }
1008   
1009   iface = G_FILE_GET_IFACE (file);
1010   return (* iface->query_info_finish) (file, res, error);
1011 }
1012
1013 /**
1014  * g_file_query_filesystem_info:
1015  * @file: input #GFile.
1016  * @attributes:  an attribute query string.
1017  * @cancellable: optional #GCancellable object, %NULL to ignore. 
1018  * @error: a #GError. 
1019  * 
1020  * Similar to g_file_query_info(), but obtains information
1021  * about the filesystem the @file is on, rather than the file itself.
1022  * For instance the amount of space available and the type of
1023  * the filesystem.
1024  *
1025  * The @attribute value is a string that specifies the file attributes that
1026  * should be gathered. It is not an error if it's not possible to read a particular
1027  * requested attribute from a file - it just won't be set. @attribute should
1028  * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1029  * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1030  * namespace. The standard namespace for filesystem attributes is "fs".
1031  * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1032  * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1033  * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1034  * 
1035  * If @cancellable is not %NULL, then the operation can be cancelled by
1036  * triggering the cancellable object from another thread. If the operation
1037  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1038  *
1039  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1040  * Other errors are possible too, and depend on what kind of filesystem the file is on.
1041  *
1042  * Returns: a #GFileInfo or %NULL if there was an error.
1043  **/
1044 GFileInfo *
1045 g_file_query_filesystem_info (GFile         *file,
1046                               const char    *attributes,
1047                               GCancellable  *cancellable,
1048                               GError       **error)
1049 {
1050   GFileIface *iface;
1051   
1052   g_return_val_if_fail (G_IS_FILE (file), NULL);
1053
1054   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1055     return NULL;
1056   
1057   iface = G_FILE_GET_IFACE (file);
1058
1059   if (iface->query_filesystem_info == NULL)
1060     {
1061       g_set_error (error, G_IO_ERROR,
1062                    G_IO_ERROR_NOT_SUPPORTED,
1063                    _("Operation not supported"));
1064       return NULL;
1065     }
1066   
1067   return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1068 }
1069
1070 /**
1071  * g_file_find_enclosing_mount:
1072  * @file: input #GFile.
1073  * @cancellable: optional #GCancellable object, %NULL to ignore. 
1074  * @error: a #GError. 
1075  *
1076  * Gets a #GMount for the #GFile. 
1077  *
1078  * If the #GFileIface for @file does not have a mount (e.g. possibly a 
1079  * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1080  * will be returned.
1081  * 
1082  * If @cancellable is not %NULL, then the operation can be cancelled by
1083  * triggering the cancellable object from another thread. If the operation
1084  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1085  * 
1086  * Returns: a #GMount where the @file is located or %NULL on error.
1087  **/
1088 GMount *
1089 g_file_find_enclosing_mount (GFile         *file,
1090                              GCancellable  *cancellable,
1091                              GError       **error)
1092 {
1093   GFileIface *iface;
1094
1095   g_return_val_if_fail (G_IS_FILE (file), NULL);
1096   
1097   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1098     return NULL;
1099
1100   iface = G_FILE_GET_IFACE (file);
1101   if (iface->find_enclosing_mount == NULL)
1102     {
1103       g_set_error (error, G_IO_ERROR,
1104                    G_IO_ERROR_NOT_FOUND,
1105                    _("Containing mount does not exist"));
1106       return NULL;
1107     }
1108   
1109   return (* iface->find_enclosing_mount) (file, cancellable, error);
1110 }
1111
1112 void
1113 g_file_find_enclosing_mount_async (GFile              *file,
1114                                    int                   io_priority,
1115                                    GCancellable         *cancellable,
1116                                    GAsyncReadyCallback   callback,
1117                                    gpointer              user_data)
1118 {
1119   GFileIface *iface;
1120
1121   g_return_if_fail (G_IS_FILE (file));
1122
1123   iface = G_FILE_GET_IFACE (file);
1124   (* iface->find_enclosing_mount_async) (file,
1125                                          io_priority,
1126                                          cancellable,
1127                                          callback,
1128                                          user_data);
1129 }
1130
1131 GMount *
1132 g_file_find_enclosing_mount_finish (GFile              *file,
1133                                     GAsyncResult         *res,
1134                                     GError            **error)
1135 {
1136   GFileIface *iface;
1137   
1138   g_return_val_if_fail (G_IS_FILE (file), NULL);
1139   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1140
1141   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1142     {
1143       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1144       if (g_simple_async_result_propagate_error (simple, error))
1145         return NULL;
1146     }
1147   
1148   iface = G_FILE_GET_IFACE (file);
1149   return (* iface->find_enclosing_mount_finish) (file, res, error);
1150 }
1151
1152
1153 /**
1154  * g_file_read:
1155  * @file: #GFile to read.
1156  * @cancellable: a #GCancellable
1157  * @error: a #GError, or %NULL
1158  *
1159  * Opens a file for reading. The result is a #GFileInputStream that
1160  * can be used to read the contents of the file.
1161  *
1162  * If @cancellable is not %NULL, then the operation can be cancelled by
1163  * triggering the cancellable object from another thread. If the operation
1164  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1165  * 
1166  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1167  * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1168  * Other errors are possible too, and depend on what kind of filesystem the file is on.
1169  *
1170  * Returns: #GFileInputStream or %NULL on error.
1171  **/
1172 GFileInputStream *
1173 g_file_read (GFile         *file,
1174              GCancellable  *cancellable,
1175              GError       **error)
1176 {
1177   GFileIface *iface;
1178   
1179   g_return_val_if_fail (G_IS_FILE (file), NULL);
1180
1181   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1182     return NULL;
1183
1184   iface = G_FILE_GET_IFACE (file);
1185
1186   if (iface->read_fn == NULL)
1187     {
1188       g_set_error (error, G_IO_ERROR,
1189                    G_IO_ERROR_NOT_SUPPORTED,
1190                    _("Operation not supported"));
1191       return NULL;
1192     }
1193   
1194   return (* iface->read_fn) (file, cancellable, error);
1195 }
1196
1197 /**
1198  * g_file_append_to:
1199  * @file: input #GFile.
1200  * @flags: a set of #GFileCreateFlags.
1201  * @cancellable: optional #GCancellable object, %NULL to ignore.
1202  * @error: a #GError, or %NULL
1203  *
1204  * Gets an output stream for appending data to the file. If
1205  * the file doesn't already exist it is created.
1206  *
1207  * By default files created are generally readable by everyone,
1208  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1209  * will be made readable only to the current user, to the level that
1210  * is supported on the target filesystem.
1211  *
1212  * If @cancellable is not %NULL, then the operation can be cancelled by
1213  * triggering the cancellable object from another thread. If the operation
1214  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1215  *
1216  * Some file systems don't allow all file names, and may
1217  * return an G_IO_ERROR_INVALID_FILENAME error.
1218  * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1219  * returned. Other errors are possible too, and depend on what kind of
1220  * filesystem the file is on.
1221  * 
1222  * Returns: a #GFileOutputStream.
1223  **/
1224 GFileOutputStream *
1225 g_file_append_to (GFile             *file,
1226                   GFileCreateFlags   flags,
1227                   GCancellable      *cancellable,
1228                   GError           **error)
1229 {
1230   GFileIface *iface;
1231
1232   g_return_val_if_fail (G_IS_FILE (file), NULL);
1233
1234   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1235     return NULL;
1236   
1237   iface = G_FILE_GET_IFACE (file);
1238
1239   if (iface->append_to == NULL)
1240     {
1241       g_set_error (error, G_IO_ERROR,
1242                    G_IO_ERROR_NOT_SUPPORTED,
1243                    _("Operation not supported"));
1244       return NULL;
1245     }
1246   
1247   return (* iface->append_to) (file, flags, cancellable, error);
1248 }
1249
1250 /**
1251  * g_file_create:
1252  * @file: input #GFile.
1253  * @flags: a set of #GFileCreateFlags.
1254  * @cancellable: optional #GCancellable object, %NULL to ignore.
1255  * @error: a #GError, or %NULL
1256  *
1257  * Creates a new file and returns an output stream for writing to it.
1258  * The file must not already exists.
1259  *
1260  * By default files created are generally readable by everyone,
1261  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1262  * will be made readable only to the current user, to the level that
1263  * is supported on the target filesystem.
1264  *
1265  * If @cancellable is not %NULL, then the operation can be cancelled by
1266  * triggering the cancellable object from another thread. If the operation
1267  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1268  *
1269  * If a file with this name already exists the G_IO_ERROR_EXISTS error
1270  * will be returned. If the file is a directory the G_IO_ERROR_IS_DIRECTORY
1271  * error will be returned.
1272  * Some file systems don't allow all file names, and may
1273  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1274  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1275  * Other errors are possible too, and depend on what kind of
1276  * filesystem the file is on.
1277  * 
1278  * Returns: a #GFileOutputStream for the newly created file, or 
1279  * %NULL on error.
1280  **/
1281 GFileOutputStream *
1282 g_file_create (GFile             *file,
1283                GFileCreateFlags   flags,
1284                GCancellable      *cancellable,
1285                GError           **error)
1286 {
1287   GFileIface *iface;
1288   
1289   g_return_val_if_fail (G_IS_FILE (file), NULL);
1290
1291   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1292     return NULL;
1293   
1294   iface = G_FILE_GET_IFACE (file);
1295
1296   if (iface->create == NULL)
1297     {
1298       g_set_error (error, G_IO_ERROR,
1299                    G_IO_ERROR_NOT_SUPPORTED,
1300                    _("Operation not supported"));
1301       return NULL;
1302     }
1303   
1304   return (* iface->create) (file, flags, cancellable, error);
1305 }
1306
1307 /**
1308  * g_file_replace:
1309  * @file: input #GFile.
1310  * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the 
1311  *     current #GFile, or #NULL to ignore.
1312  * @make_backup: %TRUE if a backup should be created.
1313  * @flags: a set of #GFileCreateFlags.
1314  * @cancellable: optional #GCancellable object, %NULL to ignore.
1315  * @error: a #GError, or %NULL
1316  *
1317  * Returns an output stream for overwriting the file, possibly
1318  * creating a backup copy of the file first.
1319  *
1320  * This will try to replace the file in the safest way possible so
1321  * that any errors during the writing will not affect an already
1322  * existing copy of the file. For instance, for local files it
1323  * may write to a temporary file and then atomically rename over
1324  * the destination when the stream is closed.
1325  * 
1326  * By default files created are generally readable by everyone,
1327  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1328  * will be made readable only to the current user, to the level that
1329  * is supported on the target filesystem.
1330  *
1331  * If @cancellable is not %NULL, then the operation can be cancelled by
1332  * triggering the cancellable object from another thread. If the operation
1333  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1334  * 
1335  * If you pass in a non-#NULL @etag value, then this value is
1336  * compared to the current entity tag of the file, and if they differ
1337  * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1338  * that the file has been changed since you last read it. You can get
1339  * the new etag from g_file_output_stream_get_etag() after you've
1340  * finished writing and closed the #GFileOutputStream. When you load
1341  * a new file you can use g_file_input_stream_query_info() to get
1342  * the etag of the file.
1343  * 
1344  * If @make_backup is %TRUE, this function will attempt to make a backup
1345  * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1346  * error will be returned. If you want to replace anyway, try again with
1347  * @make_backup set to %FALSE.
1348  *
1349  * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1350  * and if the file is some other form of non-regular file then a
1351  * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1352  * Some file systems don't allow all file names, and may
1353  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1354  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1355  * Other errors are possible too, and depend on what kind of
1356  * filesystem the file is on.
1357  *
1358  * Returns: a #GFileOutputStream or %NULL on error. 
1359  **/
1360 GFileOutputStream *
1361 g_file_replace (GFile             *file,
1362                 const char        *etag,
1363                 gboolean           make_backup,
1364                 GFileCreateFlags   flags,
1365                 GCancellable      *cancellable,
1366                 GError           **error)
1367 {
1368   GFileIface *iface;
1369
1370   g_return_val_if_fail (G_IS_FILE (file), NULL);
1371
1372   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1373     return NULL;
1374   
1375   iface = G_FILE_GET_IFACE (file);
1376
1377   if (iface->replace == NULL)
1378     {
1379       g_set_error (error, G_IO_ERROR,
1380                    G_IO_ERROR_NOT_SUPPORTED,
1381                    _("Operation not supported"));
1382       return NULL;
1383     }
1384   
1385   
1386   /* Handle empty tag string as NULL in consistent way. */
1387   if (etag && *etag == 0)
1388     etag = NULL;
1389   
1390   return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1391 }
1392
1393 /**
1394  * g_file_read_async:
1395  * @file: input #GFile.
1396  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1397  *     of the request. 
1398  * @cancellable: optional #GCancellable object, %NULL to ignore.
1399  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1400  * @user_data: the data to pass to callback function
1401  *
1402  * Asynchronously opens @file for reading.
1403  *
1404  * For more details, see g_file_read() which is
1405  * the synchronous version of this call.
1406  *
1407  * When the operation is finished, @callback will be called. You can then call
1408  * g_file_read_finish() to get the result of the operation.
1409  **/
1410 void
1411 g_file_read_async (GFile               *file,
1412                    int                  io_priority,
1413                    GCancellable        *cancellable,
1414                    GAsyncReadyCallback  callback,
1415                    gpointer             user_data)
1416 {
1417   GFileIface *iface;
1418   
1419   g_return_if_fail (G_IS_FILE (file));
1420
1421   iface = G_FILE_GET_IFACE (file);
1422   (* iface->read_async) (file,
1423                          io_priority,
1424                          cancellable,
1425                          callback,
1426                          user_data);
1427 }
1428
1429 /**
1430  * g_file_read_finish:
1431  * @file: input #GFile.
1432  * @res: a #GAsyncResult. 
1433  * @error: a #GError, or %NULL
1434  *
1435  * Finishes an asynchronous file read operation started with 
1436  * g_file_read_async(). 
1437  *  
1438  * Returns: a #GFileInputStream or %NULL on error.
1439  **/
1440 GFileInputStream *
1441 g_file_read_finish (GFile         *file,
1442                     GAsyncResult  *res,
1443                     GError       **error)
1444 {
1445   GFileIface *iface;
1446   
1447   g_return_val_if_fail (G_IS_FILE (file), NULL);
1448   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1449
1450   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1451     {
1452       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1453       if (g_simple_async_result_propagate_error (simple, error))
1454         return NULL;
1455     }
1456   
1457   iface = G_FILE_GET_IFACE (file);
1458   return (* iface->read_finish) (file, res, error);
1459 }
1460
1461 /**
1462  * g_file_append_to_async:
1463  * @file: input #GFile.
1464  * @flags: a set of #GFileCreateFlags.
1465  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1466  *     of the request. 
1467  * @cancellable: optional #GCancellable object, %NULL to ignore.
1468  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1469  * @user_data: the data to pass to callback function
1470  * 
1471  * Asynchronously opens @file for appending.
1472  *
1473  * For more details, see g_file_append_to() which is
1474  * the synchronous version of this call.
1475  *
1476  * When the operation is finished, @callback will be called. You can then call
1477  * g_file_append_to_finish() to get the result of the operation.
1478  **/
1479 void
1480 g_file_append_to_async (GFile               *file,
1481                         GFileCreateFlags     flags,
1482                         int                  io_priority,
1483                         GCancellable        *cancellable,
1484                         GAsyncReadyCallback  callback,
1485                         gpointer             user_data)
1486 {
1487   GFileIface *iface;
1488   
1489   g_return_if_fail (G_IS_FILE (file));
1490
1491   iface = G_FILE_GET_IFACE (file);
1492   (* iface->append_to_async) (file,
1493                               flags,
1494                               io_priority,
1495                               cancellable,
1496                               callback,
1497                               user_data);
1498 }
1499
1500 /**
1501  * g_file_append_to_finish:
1502  * @file: input #GFile.
1503  * @res: #GAsyncResult
1504  * @error: a #GError, or %NULL
1505  * 
1506  * Finishes an asynchronous file append operation started with 
1507  * g_file_append_to_async(). 
1508  * 
1509  * Returns: a valid #GFileOutputStream or %NULL on error.
1510  **/
1511 GFileOutputStream *
1512 g_file_append_to_finish (GFile         *file,
1513                          GAsyncResult  *res,
1514                          GError       **error)
1515 {
1516   GFileIface *iface;
1517   
1518   g_return_val_if_fail (G_IS_FILE (file), NULL);
1519   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1520
1521   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1522     {
1523       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1524       if (g_simple_async_result_propagate_error (simple, error))
1525         return NULL;
1526     }
1527   
1528   iface = G_FILE_GET_IFACE (file);
1529   return (* iface->append_to_finish) (file, res, error);
1530 }
1531
1532 /**
1533  * g_file_create_async:
1534  * @file: input #GFile.
1535  * @flags: a set of #GFileCreateFlags.
1536  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1537  *     of the request.
1538  * @cancellable: optional #GCancellable object, %NULL to ignore.
1539  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1540  * @user_data: the data to pass to callback function
1541  * 
1542  * Asynchronously creates a new file and returns an output stream for writing to it.
1543  * The file must not already exists.
1544  *
1545  * For more details, see g_file_create() which is
1546  * the synchronous version of this call.
1547  *
1548  * When the operation is finished, @callback will be called. You can then call
1549  * g_file_create_finish() to get the result of the operation.
1550  **/
1551 void
1552 g_file_create_async (GFile               *file,
1553                      GFileCreateFlags     flags,
1554                      int                  io_priority,
1555                      GCancellable        *cancellable,
1556                      GAsyncReadyCallback  callback,
1557                      gpointer             user_data)
1558 {
1559   GFileIface *iface;
1560   
1561   g_return_if_fail (G_IS_FILE (file));
1562
1563   iface = G_FILE_GET_IFACE (file);
1564   (* iface->create_async) (file,
1565                            flags,
1566                            io_priority,
1567                            cancellable,
1568                            callback,
1569                            user_data);
1570 }
1571
1572 /**
1573  * g_file_create_finish:
1574  * @file: input #GFile.
1575  * @res: a #GAsyncResult. 
1576  * @error: a #GError, or %NULL
1577  * 
1578  * Finishes an asynchronous file create operation started with 
1579  * g_file_create_async(). 
1580  * 
1581  * Returns: a #GFileOutputStream or %NULL on error.
1582  **/
1583 GFileOutputStream *
1584 g_file_create_finish (GFile         *file,
1585                       GAsyncResult  *res,
1586                       GError       **error)
1587 {
1588   GFileIface *iface;
1589   
1590   g_return_val_if_fail (G_IS_FILE (file), NULL);
1591   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1592
1593   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1594     {
1595       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1596       if (g_simple_async_result_propagate_error (simple, error))
1597         return NULL;
1598     }
1599   
1600   iface = G_FILE_GET_IFACE (file);
1601   return (* iface->create_finish) (file, res, error);
1602 }
1603
1604 /**
1605  * g_file_replace_async:
1606  * @file: input #GFile.
1607  * @etag: an <link linkend="gfile-etag">entity tag</link> for the 
1608  *     current #GFile, or NULL to ignore.
1609  * @make_backup: %TRUE if a backup should be created.
1610  * @flags: a set of #GFileCreateFlags.
1611  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1612  *     of the request.
1613  * @cancellable: optional #GCancellable object, %NULL to ignore.
1614  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1615  * @user_data: the data to pass to callback function
1616  *
1617  * Asynchronously overwrites the file, replacing the contents, possibly
1618  * creating a backup copy of the file first.
1619  *
1620  * For more details, see g_file_replace() which is
1621  * the synchronous version of this call.
1622  *
1623  * When the operation is finished, @callback will be called. You can then call
1624  * g_file_replace_finish() to get the result of the operation.
1625  **/
1626 void
1627 g_file_replace_async (GFile               *file,
1628                       const char          *etag,
1629                       gboolean             make_backup,
1630                       GFileCreateFlags     flags,
1631                       int                  io_priority,
1632                       GCancellable        *cancellable,
1633                       GAsyncReadyCallback  callback,
1634                       gpointer             user_data)
1635 {
1636   GFileIface *iface;
1637   
1638   g_return_if_fail (G_IS_FILE (file));
1639
1640   iface = G_FILE_GET_IFACE (file);
1641   (* iface->replace_async) (file,
1642                             etag,
1643                             make_backup,
1644                             flags,
1645                             io_priority,
1646                             cancellable,
1647                             callback,
1648                             user_data);
1649 }
1650
1651 /**
1652  * g_file_replace_finish:
1653  * @file: input #GFile.
1654  * @res: a #GAsyncResult. 
1655  * @error: a #GError, or %NULL
1656  * 
1657  * Finishes an asynchronous file replace operation started with 
1658  * g_file_replace_async(). 
1659  * 
1660  * Returns: a #GFileOutputStream, or %NULL on error.
1661  **/
1662 GFileOutputStream *
1663 g_file_replace_finish (GFile         *file,
1664                        GAsyncResult  *res,
1665                        GError       **error)
1666 {
1667   GFileIface *iface;
1668   
1669   g_return_val_if_fail (G_IS_FILE (file), NULL);
1670   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1671
1672   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1673     {
1674       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1675       if (g_simple_async_result_propagate_error (simple, error))
1676         return NULL;
1677     }
1678   
1679   iface = G_FILE_GET_IFACE (file);
1680   return (* iface->replace_finish) (file, res, error);
1681 }
1682
1683 static gboolean
1684 copy_symlink (GFile           *destination,
1685               GFileCopyFlags   flags,
1686               GCancellable    *cancellable,
1687               const char      *target,
1688               GError         **error)
1689 {
1690   GError *my_error;
1691   gboolean tried_delete;
1692   GFileInfo *info;
1693   GFileType file_type;
1694
1695   tried_delete = FALSE;
1696
1697  retry:
1698   my_error = NULL;
1699   if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1700     {
1701       /* Maybe it already existed, and we want to overwrite? */
1702       if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) && 
1703           my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1704         {
1705           g_error_free (my_error);
1706
1707
1708           /* Don't overwrite if the destination is a directory */
1709           info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1710                                     G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1711                                     cancellable, &my_error);
1712           if (info != NULL)
1713             {
1714               file_type = g_file_info_get_file_type (info);
1715               g_object_unref (info);
1716               
1717               if (file_type == G_FILE_TYPE_DIRECTORY)
1718                 {
1719                   g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1720                                _("Can't copy over directory"));
1721                   return FALSE;
1722                 }
1723             }
1724           
1725           if (!g_file_delete (destination, cancellable, error))
1726             return FALSE;
1727           
1728           tried_delete = TRUE;
1729           goto retry;
1730         }
1731             /* Nah, fail */
1732       g_propagate_error (error, my_error);
1733       return FALSE;
1734     }
1735
1736   return TRUE;
1737 }
1738
1739 static GInputStream *
1740 open_source_for_copy (GFile           *source,
1741                       GFile           *destination,
1742                       GFileCopyFlags   flags,
1743                       GCancellable    *cancellable,
1744                       GError         **error)
1745 {
1746   GError *my_error;
1747   GInputStream *in;
1748   GFileInfo *info;
1749   GFileType file_type;
1750   
1751   my_error = NULL;
1752   in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1753   if (in != NULL)
1754     return in;
1755
1756   /* There was an error opening the source, try to set a good error for it: */
1757
1758   if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1759     {
1760       /* The source is a directory, don't fail with WOULD_RECURSE immediately, 
1761        * as that is less useful to the app. Better check for errors on the 
1762        * target instead. 
1763        */
1764       g_error_free (my_error);
1765       my_error = NULL;
1766       
1767       info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1768                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1769                                 cancellable, &my_error);
1770       if (info != NULL)
1771         {
1772           file_type = g_file_info_get_file_type (info);
1773           g_object_unref (info);
1774           
1775           if (flags & G_FILE_COPY_OVERWRITE)
1776             {
1777               if (file_type == G_FILE_TYPE_DIRECTORY)
1778                 {
1779                   g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1780                                _("Can't copy directory over directory"));
1781                   return NULL;
1782                 }
1783               /* continue to would_recurse error */
1784             }
1785           else
1786             {
1787               g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1788                            _("Target file exists"));
1789               return NULL;
1790             }
1791         }
1792       else
1793         {
1794           /* Error getting info from target, return that error 
1795            * (except for NOT_FOUND, which is no error here) 
1796            */
1797           if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1798             {
1799               g_propagate_error (error, my_error);
1800               return NULL;
1801             }
1802           g_error_free (my_error);
1803         }
1804       
1805       g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1806                    _("Can't recursively copy directory"));
1807       return NULL;
1808     }
1809
1810   g_propagate_error (error, my_error);
1811   return NULL;
1812 }
1813
1814 static gboolean
1815 should_copy (GFileAttributeInfo *info, 
1816              gboolean            as_move)
1817 {
1818   if (as_move)
1819     return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1820   return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1821 }
1822
1823 static char *
1824 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1825                                GFileAttributeInfoList *namespaces,
1826                                gboolean                as_move)
1827 {
1828   GString *s;
1829   gboolean first;
1830   int i;
1831   
1832   first = TRUE;
1833   s = g_string_new ("");
1834
1835   if (attributes)
1836     {
1837       for (i = 0; i < attributes->n_infos; i++)
1838         {
1839           if (should_copy (&attributes->infos[i], as_move))
1840             {
1841               if (first)
1842                 first = FALSE;
1843               else
1844                 g_string_append_c (s, ',');
1845                 
1846               g_string_append (s, attributes->infos[i].name);
1847             }
1848         }
1849     }
1850
1851   if (namespaces)
1852     {
1853       for (i = 0; i < namespaces->n_infos; i++)
1854         {
1855           if (should_copy (&namespaces->infos[i], as_move))
1856             {
1857               if (first)
1858                 first = FALSE;
1859               else
1860                 g_string_append_c (s, ',');
1861                 
1862               g_string_append (s, namespaces->infos[i].name);
1863               g_string_append (s, ":*");
1864             }
1865         }
1866     }
1867
1868   return g_string_free (s, FALSE);
1869 }
1870
1871 /**
1872  * g_file_copy_attributes:
1873  * @source: a #GFile with attributes.
1874  * @destination: a #GFile to copy attributes to.
1875  * @flags: a set of #GFileCopyFlags.
1876  * @cancellable: optional #GCancellable object, %NULL to ignore.
1877  * @error: a #GError, %NULL to ignore.
1878  *
1879  * Copies the file attributes from @source to @destination. 
1880  *
1881  * Normally only a subset of the file attributes are copied,
1882  * those that are copies in a normal file copy operation
1883  * (which for instance does not include e.g. mtime). However
1884  * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
1885  * all the metadata that is possible to copy is copied.
1886  *
1887  * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1888  **/
1889 gboolean
1890 g_file_copy_attributes (GFile           *source,
1891                         GFile           *destination,
1892                         GFileCopyFlags   flags,
1893                         GCancellable    *cancellable,
1894                         GError         **error)
1895 {
1896   GFileAttributeInfoList *attributes, *namespaces;
1897   char *attrs_to_read;
1898   gboolean res;
1899   GFileInfo *info;
1900   gboolean as_move;
1901   gboolean source_nofollow_symlinks;
1902
1903   as_move = flags & G_FILE_COPY_ALL_METADATA;
1904   source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1905
1906   /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1907   attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1908   namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1909
1910   if (attributes == NULL && namespaces == NULL)
1911     return TRUE;
1912
1913   attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1914
1915   /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1916    * we just don't copy it. 
1917    */
1918   info = g_file_query_info (source, attrs_to_read,
1919                             source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1920                             cancellable,
1921                             NULL);
1922
1923   g_free (attrs_to_read);
1924   
1925   res = TRUE;
1926   if  (info)
1927     {
1928       res = g_file_set_attributes_from_info (destination,
1929                                              info, 0,
1930                                              cancellable,
1931                                              error);
1932       g_object_unref (info);
1933     }
1934   
1935   g_file_attribute_info_list_unref (attributes);
1936   g_file_attribute_info_list_unref (namespaces);
1937   
1938   return res;
1939 }
1940
1941 /* Closes the streams */
1942 static gboolean
1943 copy_stream_with_progress (GInputStream           *in,
1944                            GOutputStream          *out,
1945                            GCancellable           *cancellable,
1946                            GFileProgressCallback   progress_callback,
1947                            gpointer                progress_callback_data,
1948                            GError                **error)
1949 {
1950   gssize n_read, n_written;
1951   goffset current_size;
1952   char buffer[8192], *p;
1953   gboolean res;
1954   goffset total_size;
1955   GFileInfo *info;
1956
1957   total_size = 0;
1958   info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1959                                          G_FILE_ATTRIBUTE_STANDARD_SIZE,
1960                                          cancellable, NULL);
1961   if (info)
1962     {
1963       total_size = g_file_info_get_size (info);
1964       g_object_unref (info);
1965     }
1966   
1967   current_size = 0;
1968   res = TRUE;
1969   while (TRUE)
1970     {
1971       n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
1972       if (n_read == -1)
1973         {
1974           res = FALSE;
1975           break;
1976         }
1977         
1978       if (n_read == 0)
1979         break;
1980
1981       current_size += n_read;
1982
1983       p = buffer;
1984       while (n_read > 0)
1985         {
1986           n_written = g_output_stream_write (out, p, n_read, cancellable, error);
1987           if (n_written == -1)
1988             {
1989               res = FALSE;
1990               break;
1991             }
1992
1993           p += n_written;
1994           n_read -= n_written;
1995         }
1996
1997       if (!res)
1998         break;
1999       
2000       if (progress_callback)
2001         progress_callback (current_size, total_size, progress_callback_data);
2002     }
2003
2004   if (!res)
2005     error = NULL; /* Ignore further errors */
2006
2007   /* Make sure we send full copied size */
2008   if (progress_callback)
2009     progress_callback (current_size, total_size, progress_callback_data);
2010
2011   
2012   /* Don't care about errors in source here */
2013   g_input_stream_close (in, cancellable, NULL);
2014
2015   /* But write errors on close are bad! */
2016   if (!g_output_stream_close (out, cancellable, error))
2017     res = FALSE;
2018
2019   g_object_unref (in);
2020   g_object_unref (out);
2021       
2022   return res;
2023 }
2024
2025 static gboolean
2026 file_copy_fallback (GFile                  *source,
2027                     GFile                  *destination,
2028                     GFileCopyFlags          flags,
2029                     GCancellable           *cancellable,
2030                     GFileProgressCallback   progress_callback,
2031                     gpointer                progress_callback_data,
2032                     GError                **error)
2033 {
2034   GInputStream *in;
2035   GOutputStream *out;
2036   GFileInfo *info;
2037   const char *target;
2038
2039   /* Maybe copy the symlink? */
2040   if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2041     {
2042       info = g_file_query_info (source,
2043                                 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2044                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2045                                 cancellable,
2046                                 error);
2047       if (info == NULL)
2048         return FALSE;
2049
2050       if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2051           (target = g_file_info_get_symlink_target (info)) != NULL)
2052         {
2053           if (!copy_symlink (destination, flags, cancellable, target, error))
2054             {
2055               g_object_unref (info);
2056               return FALSE;
2057             }
2058           
2059           g_object_unref (info);
2060           goto copied_file;
2061         }
2062       
2063       g_object_unref (info);
2064     }
2065   
2066   in = open_source_for_copy (source, destination, flags, cancellable, error);
2067   if (in == NULL)
2068     return FALSE;
2069   
2070   if (flags & G_FILE_COPY_OVERWRITE)
2071     {
2072       out = (GOutputStream *)g_file_replace (destination,
2073                                              NULL, 0,
2074                                              flags & G_FILE_COPY_BACKUP,
2075                                              cancellable, error);
2076     }
2077   else
2078     {
2079       out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2080     }
2081
2082   if (out == NULL)
2083     {
2084       g_object_unref (in);
2085       return FALSE;
2086     }
2087
2088   if (!copy_stream_with_progress (in, out, cancellable,
2089                                   progress_callback, progress_callback_data,
2090                                   error))
2091     return FALSE;
2092
2093  copied_file:
2094
2095   /* Ignore errors here. Failure to copy metadata is not a hard error */
2096   g_file_copy_attributes (source, destination,
2097                           flags, cancellable, NULL);
2098   
2099   return TRUE;
2100 }
2101
2102 /**
2103  * g_file_copy:
2104  * @source: input #GFile.
2105  * @destination: destination #GFile
2106  * @flags: set of #GFileCopyFlags
2107  * @cancellable: optional #GCancellable object, %NULL to ignore.
2108  * @progress_callback: function to callback with progress information
2109  * @progress_callback_data: user data to pass to @progress_callback
2110  * @error: #GError to set on error, or %NULL
2111  *
2112  * Copies the file @source to the location specified by @destination.
2113  * Can not handle recursive copies of directories.
2114  *
2115  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2116  * existing @destination file is overwritten.
2117  *
2118  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2119  * will be copied as symlinks, otherwise the target of the
2120  * @source symlink will be copied.
2121  *
2122  * If @cancellable is not %NULL, then the operation can be cancelled by
2123  * triggering the cancellable object from another thread. If the operation
2124  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2125  * 
2126  * If @progress_callback is not %NULL, then the operation can be monitored by
2127  * setting this to a #GFileProgressCallback function. @progress_callback_data
2128  * will be passed to this function. It is guaranteed that this callback will
2129  * be called after all data has been transferred with the total number of bytes
2130  * copied during the operation.
2131  * 
2132  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2133  * error is returned, independent on the status of the @destination.
2134  *
2135  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2136  * error G_IO_ERROR_EXISTS is returned.
2137  *
2138  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2139  * error is returned. If trying to overwrite a directory with a directory the
2140  * G_IO_ERROR_WOULD_MERGE error is returned.
2141  *
2142  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2143  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2144  * is returned.
2145  *
2146  * If you are interested in copying the #GFile object itself (not the on-disk
2147  * file), see g_file_dup().
2148  *
2149  * Returns: %TRUE on success, %FALSE otherwise.
2150  **/
2151 gboolean
2152 g_file_copy (GFile                  *source,
2153              GFile                  *destination,
2154              GFileCopyFlags          flags,
2155              GCancellable           *cancellable,
2156              GFileProgressCallback   progress_callback,
2157              gpointer                progress_callback_data,
2158              GError                **error)
2159 {
2160   GFileIface *iface;
2161   GError *my_error;
2162   gboolean res;
2163
2164   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2165   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2166
2167   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2168     return FALSE;
2169   
2170   iface = G_FILE_GET_IFACE (destination);
2171   if (iface->copy)
2172     {
2173       my_error = NULL;
2174       res = (* iface->copy) (source, destination,
2175                              flags, cancellable,
2176                              progress_callback, progress_callback_data,
2177                              &my_error);
2178       
2179       if (res)
2180         return TRUE;
2181       
2182       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2183         {
2184           g_propagate_error (error, my_error);
2185               return FALSE;
2186         }
2187     }
2188
2189   /* If the types are different, and the destination method failed
2190      also try the source method */
2191   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2192     {
2193       iface = G_FILE_GET_IFACE (source);
2194       
2195       if (iface->copy)
2196         {
2197           my_error = NULL;
2198           res = (* iface->copy) (source, destination,
2199                                  flags, cancellable,
2200                                  progress_callback, progress_callback_data,
2201                                  &my_error);
2202           
2203           if (res)
2204             return TRUE;
2205           
2206           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2207             {
2208               g_propagate_error (error, my_error);
2209               return FALSE;
2210             }
2211         }
2212     }
2213   
2214   return file_copy_fallback (source, destination, flags, cancellable,
2215                              progress_callback, progress_callback_data,
2216                              error);
2217 }
2218
2219
2220 /**
2221  * g_file_move:
2222  * @source: #GFile pointing to the source location.
2223  * @destination: #GFile pointing to the destination location.
2224  * @flags: set of #GFileCopyFlags.
2225  * @cancellable: optional #GCancellable object, %NULL to ignore.
2226  * @progress_callback: #GFileProgressCallback function for updates.
2227  * @progress_callback_data: gpointer to user data for the callback function.
2228  * @error: #GError for returning error conditions, or %NULL
2229  *
2230  *
2231  * Tries to move the file or directory @source to the location specified by @destination.
2232  * If native move operations is supported then this is used, otherwise a copy + delete
2233  * fallback is used. The native implementation may support moving directories (for instance
2234  * on moves inside the same filesystem), but the fallback code does not.
2235  * 
2236  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2237  * existing @destination file is overwritten.
2238  *
2239  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2240  * will be copied as symlinks, otherwise the target of the
2241  * @source symlink will be copied.
2242  *
2243  * If @cancellable is not %NULL, then the operation can be cancelled by
2244  * triggering the cancellable object from another thread. If the operation
2245  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2246  * 
2247  * If @progress_callback is not %NULL, then the operation can be monitored by
2248  * setting this to a #GFileProgressCallback function. @progress_callback_data
2249  * will be passed to this function. It is guaranteed that this callback will
2250  * be called after all data has been transferred with the total number of bytes
2251  * copied during the operation.
2252  * 
2253  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2254  * error is returned, independent on the status of the @destination.
2255  *
2256  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2257  * error G_IO_ERROR_EXISTS is returned.
2258  *
2259  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2260  * error is returned. If trying to overwrite a directory with a directory the
2261  * G_IO_ERROR_WOULD_MERGE error is returned.
2262  *
2263  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2264  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2265  * may be returned (if the native move operation isn't available).
2266  *
2267  * Returns: %TRUE on successful move, %FALSE otherwise.
2268  **/
2269 gboolean
2270 g_file_move (GFile                  *source,
2271              GFile                  *destination,
2272              GFileCopyFlags          flags,
2273              GCancellable           *cancellable,
2274              GFileProgressCallback   progress_callback,
2275              gpointer                progress_callback_data,
2276              GError                **error)
2277 {
2278   GFileIface *iface;
2279   GError *my_error;
2280   gboolean res;
2281
2282   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2283   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2284
2285   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2286     return FALSE;
2287   
2288   iface = G_FILE_GET_IFACE (destination);
2289   if (iface->move)
2290     {
2291       my_error = NULL;
2292       res = (* iface->move) (source, destination,
2293                              flags, cancellable,
2294                              progress_callback, progress_callback_data,
2295                              &my_error);
2296       
2297       if (res)
2298         return TRUE;
2299       
2300       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2301         {
2302           g_propagate_error (error, my_error);
2303           return FALSE;
2304         }
2305     }
2306
2307   /* If the types are different, and the destination method failed
2308      also try the source method */
2309   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2310     {
2311       iface = G_FILE_GET_IFACE (source);
2312       
2313       if (iface->move)
2314         {
2315           my_error = NULL;
2316           res = (* iface->move) (source, destination,
2317                                  flags, cancellable,
2318                                  progress_callback, progress_callback_data,
2319                                  &my_error);
2320           
2321           if (res)
2322             return TRUE;
2323           
2324           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2325             {
2326               g_propagate_error (error, my_error);
2327               return FALSE;
2328             }
2329         }
2330     }
2331   
2332   if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2333     {  
2334       g_set_error (error, G_IO_ERROR,
2335                    G_IO_ERROR_NOT_SUPPORTED,
2336                    _("Operation not supported"));
2337       return FALSE;
2338     }
2339   
2340   flags |= G_FILE_COPY_ALL_METADATA;
2341   if (!g_file_copy (source, destination, flags, cancellable,
2342                     progress_callback, progress_callback_data,
2343                     error))
2344     return FALSE;
2345
2346   return g_file_delete (source, cancellable, error);
2347 }
2348
2349 /**
2350  * g_file_make_directory
2351  * @file: input #GFile.
2352  * @cancellable: optional #GCancellable object, %NULL to ignore.
2353  * @error: a #GError, or %NULL 
2354  *
2355  * Creates a directory.
2356  * 
2357  * If @cancellable is not %NULL, then the operation can be cancelled by
2358  * triggering the cancellable object from another thread. If the operation
2359  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2360  * 
2361  * Returns: %TRUE on successful creation, %FALSE otherwise.
2362  **/
2363 gboolean
2364 g_file_make_directory (GFile         *file,
2365                        GCancellable  *cancellable,
2366                        GError       **error)
2367 {
2368   GFileIface *iface;
2369
2370   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2371
2372   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2373     return FALSE;
2374   
2375   iface = G_FILE_GET_IFACE (file);
2376
2377   if (iface->make_directory == NULL)
2378     {
2379       g_set_error (error, G_IO_ERROR,
2380                    G_IO_ERROR_NOT_SUPPORTED,
2381                    _("Operation not supported"));
2382       return FALSE;
2383     }
2384   
2385   return (* iface->make_directory) (file, cancellable, error);
2386 }
2387
2388 /**
2389  * g_file_make_symbolic_link:
2390  * @file: input #GFile.
2391  * @symlink_value: a string with the value of the new symlink.
2392  * @cancellable: optional #GCancellable object, %NULL to ignore.
2393  * @error: a #GError. 
2394  * 
2395  * Creates a symbolic link.
2396  *
2397  * If @cancellable is not %NULL, then the operation can be cancelled by
2398  * triggering the cancellable object from another thread. If the operation
2399  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2400  * 
2401  * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2402  **/
2403 gboolean
2404 g_file_make_symbolic_link (GFile         *file,
2405                            const char    *symlink_value,
2406                            GCancellable  *cancellable,
2407                            GError       **error)
2408 {
2409   GFileIface *iface;
2410
2411   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2412   g_return_val_if_fail (symlink_value != NULL, FALSE);
2413
2414   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2415     return FALSE;
2416
2417   if (*symlink_value == '\0')
2418     {
2419       g_set_error (error, G_IO_ERROR,
2420                    G_IO_ERROR_INVALID_ARGUMENT,
2421                    _("Invalid symlink value given"));
2422       return FALSE;
2423     }
2424   
2425   iface = G_FILE_GET_IFACE (file);
2426
2427   if (iface->make_symbolic_link == NULL)
2428     {
2429       g_set_error (error, G_IO_ERROR,
2430                    G_IO_ERROR_NOT_SUPPORTED,
2431                    _("Operation not supported"));
2432       return FALSE;
2433     }
2434   
2435   return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2436 }
2437
2438 /**
2439  * g_file_delete:
2440  * @file: input #GFile.
2441  * @cancellable: optional #GCancellable object, %NULL to ignore.
2442  * @error: a #GError, or %NULL 
2443  * 
2444  * Deletes a file.
2445  * 
2446  * If @cancellable is not %NULL, then the operation can be cancelled by
2447  * triggering the cancellable object from another thread. If the operation
2448  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2449  * 
2450  * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2451  **/
2452 gboolean
2453 g_file_delete (GFile         *file,
2454                GCancellable  *cancellable,
2455                GError       **error)
2456 {
2457   GFileIface *iface;
2458   
2459   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2460
2461   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2462     return FALSE;
2463   
2464   iface = G_FILE_GET_IFACE (file);
2465
2466   if (iface->delete_file == NULL)
2467     {
2468       g_set_error (error, G_IO_ERROR,
2469                    G_IO_ERROR_NOT_SUPPORTED,
2470                    _("Operation not supported"));
2471       return FALSE;
2472     }
2473   
2474   return (* iface->delete_file) (file, cancellable, error);
2475 }
2476
2477 /**
2478  * g_file_trash:
2479  * @file: #GFile to send to trash.
2480  * @cancellable: optional #GCancellable object, %NULL to ignore.
2481  * @error: a #GError, or %NULL
2482  *
2483  * Sends @file to the "Trashcan", if possible. This is similar to
2484  * deleting it, but the user can recover it before emptying the trashcan.
2485  * Not all file systems support trashing, so this call can return the
2486  * %G_IO_ERROR_NOT_SUPPORTED error.
2487  *
2488  *
2489  * If @cancellable is not %NULL, then the operation can be cancelled by
2490  * triggering the cancellable object from another thread. If the operation
2491  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2492  * 
2493  * Returns: %TRUE on successful trash, %FALSE otherwise.
2494  **/
2495 gboolean
2496 g_file_trash (GFile         *file,
2497               GCancellable  *cancellable,
2498               GError       **error)
2499 {
2500   GFileIface *iface;
2501   
2502   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2503
2504   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2505     return FALSE;
2506   
2507   iface = G_FILE_GET_IFACE (file);
2508
2509   if (iface->trash == NULL)
2510     {
2511       g_set_error (error,
2512                    G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2513                    _("Trash not supported"));
2514       return FALSE;
2515     }
2516   
2517   return (* iface->trash) (file, cancellable, error);
2518 }
2519
2520 /**
2521  * g_file_set_display_name:
2522  * @file: input #GFile.
2523  * @display_name: a string.
2524  * @cancellable: optional #GCancellable object, %NULL to ignore.
2525  * @error: a #GError, or %NULL
2526  * 
2527  * Renames @file to the specified display name.
2528  *
2529  * The display name is converted from UTF8 to the correct encoding for the target
2530  * filesystem if possible and the @file is renamed to this.
2531  * 
2532  * If you want to implement a rename operation in the user interface the edit name
2533  * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2534  * widget, and then the result after editing should be passed to g_file_set_display_name().
2535  *
2536  * On success the resulting converted filename is returned.
2537  * 
2538  * If @cancellable is not %NULL, then the operation can be cancelled by
2539  * triggering the cancellable object from another thread. If the operation
2540  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2541  * 
2542  * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2543  **/
2544 GFile *
2545 g_file_set_display_name (GFile         *file,
2546                          const char    *display_name,
2547                          GCancellable  *cancellable,
2548                          GError       **error)
2549 {
2550   GFileIface *iface;
2551   
2552   g_return_val_if_fail (G_IS_FILE (file), NULL);
2553   g_return_val_if_fail (display_name != NULL, NULL);
2554
2555   if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2556     {
2557       g_set_error (error,
2558                    G_IO_ERROR,
2559                    G_IO_ERROR_INVALID_ARGUMENT,
2560                    _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2561       return NULL;
2562     }
2563   
2564   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2565     return NULL;
2566   
2567   iface = G_FILE_GET_IFACE (file);
2568
2569   return (* iface->set_display_name) (file, display_name, cancellable, error);
2570 }
2571
2572 /**
2573  * g_file_set_display_name_async:
2574  * @file: input #GFile.
2575  * @display_name: a string.
2576  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2577  *     of the request. 
2578  * @cancellable: optional #GCancellable object, %NULL to ignore.
2579  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2580  * @user_data: the data to pass to callback function
2581  * 
2582  * Asynchronously sets the display name for a given #GFile.
2583  * 
2584  * For more details, see g_set_display_name() which is
2585  * the synchronous version of this call.
2586  *
2587  * When the operation is finished, @callback will be called. You can then call
2588  * g_file_set_display_name_finish() to get the result of the operation.
2589  **/
2590 void
2591 g_file_set_display_name_async (GFile               *file,
2592                                const char          *display_name,
2593                                int                  io_priority,
2594                                GCancellable        *cancellable,
2595                                GAsyncReadyCallback  callback,
2596                                gpointer             user_data)
2597 {
2598   GFileIface *iface;
2599   
2600   g_return_if_fail (G_IS_FILE (file));
2601   g_return_if_fail (display_name != NULL);
2602
2603   iface = G_FILE_GET_IFACE (file);
2604   (* iface->set_display_name_async) (file,
2605                                      display_name,
2606                                      io_priority,
2607                                      cancellable,
2608                                      callback,
2609                                      user_data);
2610 }
2611
2612 /**
2613  * g_file_set_display_name_finish:
2614  * @file: input #GFile.
2615  * @res: a #GAsyncResult. 
2616  * @error: a #GError, or %NULL
2617  * 
2618  * Finishes setting a display name started with 
2619  * g_file_set_display_name_async().
2620  * 
2621  * Returns: a #GFile or %NULL on error.
2622  **/
2623 GFile *
2624 g_file_set_display_name_finish (GFile         *file,
2625                                 GAsyncResult  *res,
2626                                 GError       **error)
2627 {
2628   GFileIface *iface;
2629   
2630   g_return_val_if_fail (G_IS_FILE (file), NULL);
2631   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2632
2633   if (G_IS_SIMPLE_ASYNC_RESULT (res))
2634     {
2635       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2636       if (g_simple_async_result_propagate_error (simple, error))
2637         return NULL;
2638     }
2639   
2640   iface = G_FILE_GET_IFACE (file);
2641   return (* iface->set_display_name_finish) (file, res, error);
2642 }
2643
2644 /**
2645  * g_file_query_settable_attributes:
2646  * @file: input #GFile.
2647  * @cancellable: optional #GCancellable object, %NULL to ignore.
2648  * @error: a #GError, or %NULL
2649  * 
2650  * Obtain the list of settable attributes for the file.
2651  *
2652  * Returns the type and full attribute name of all the attributes 
2653  * that can be set on this file. This doesn't mean setting it will always 
2654  * succeed though, you might get an access failure, or some specific 
2655  * file may not support a specific attribute.
2656  *
2657  * If @cancellable is not %NULL, then the operation can be cancelled by
2658  * triggering the cancellable object from another thread. If the operation
2659  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2660  * 
2661  * Returns: a #GFileAttributeInfoList describing the settable attributes.
2662  * When you are done with it, release it with g_file_attribute_info_list_unref()
2663  **/
2664 GFileAttributeInfoList *
2665 g_file_query_settable_attributes (GFile         *file,
2666                                   GCancellable  *cancellable,
2667                                   GError       **error)
2668 {
2669   GFileIface *iface;
2670   GError *my_error;
2671   GFileAttributeInfoList *list;
2672
2673   g_return_val_if_fail (G_IS_FILE (file), NULL);
2674
2675   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2676     return NULL;
2677   
2678   iface = G_FILE_GET_IFACE (file);
2679
2680   if (iface->query_settable_attributes == NULL)
2681     return g_file_attribute_info_list_new ();
2682
2683   my_error = NULL;
2684   list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2685   
2686   if (list == NULL)
2687     {
2688       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2689         {
2690           list = g_file_attribute_info_list_new ();
2691           g_error_free (my_error);
2692         }
2693       else
2694         g_propagate_error (error, my_error);
2695     }
2696   
2697   return list;
2698 }
2699
2700 /**
2701  * g_file_query_writable_namespaces:
2702  * @file: input #GFile.
2703  * @cancellable: optional #GCancellable object, %NULL to ignore.
2704  * @error: a #GError, or %NULL
2705  * 
2706  * Obtain the list of attribute namespaces where new attributes 
2707  * can be created by a user. An example of this is extended
2708  * attributes (in the "xattr" namespace).
2709  *
2710  * If @cancellable is not %NULL, then the operation can be cancelled by
2711  * triggering the cancellable object from another thread. If the operation
2712  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2713  * 
2714  * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2715  * When you are done with it, release it with g_file_attribute_info_list_unref()
2716  **/
2717 GFileAttributeInfoList *
2718 g_file_query_writable_namespaces (GFile         *file,
2719                                   GCancellable  *cancellable,
2720                                   GError       **error)
2721 {
2722   GFileIface *iface;
2723   GError *my_error;
2724   GFileAttributeInfoList *list;
2725   
2726   g_return_val_if_fail (G_IS_FILE (file), NULL);
2727
2728   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2729     return NULL;
2730   
2731   iface = G_FILE_GET_IFACE (file);
2732
2733   if (iface->query_writable_namespaces == NULL)
2734     return g_file_attribute_info_list_new ();
2735
2736   my_error = NULL;
2737   list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2738   
2739   if (list == NULL)
2740     {
2741       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2742         {
2743           list = g_file_attribute_info_list_new ();
2744           g_error_free (my_error);
2745         }
2746       else
2747         g_propagate_error (error, my_error);
2748     }
2749   
2750   return list;
2751 }
2752
2753 /**
2754  * g_file_set_attribute:
2755  * @file: input #GFile.
2756  * @attribute: a string containing the attribute's name.
2757  * @type: The type of the attribute
2758  * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2759  * @flags: a set of #GFileQueryInfoFlags.
2760  * @cancellable: optional #GCancellable object, %NULL to ignore.
2761  * @error: a #GError, or %NULL
2762  * 
2763  * Sets an attribute in the file with attribute name @attribute to @value.
2764  * 
2765  * If @cancellable is not %NULL, then the operation can be cancelled by
2766  * triggering the cancellable object from another thread. If the operation
2767  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2768  * 
2769  * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2770  **/
2771 gboolean
2772 g_file_set_attribute (GFile                      *file,
2773                       const char                 *attribute,
2774                       GFileAttributeType          type,
2775                       gpointer                    value_p,
2776                       GFileQueryInfoFlags         flags,
2777                       GCancellable               *cancellable,
2778                       GError                    **error)
2779 {
2780   GFileIface *iface;
2781   
2782   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2783   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2784
2785   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2786     return FALSE;
2787   
2788   iface = G_FILE_GET_IFACE (file);
2789
2790   if (iface->set_attribute == NULL)
2791     {
2792       g_set_error (error, G_IO_ERROR,
2793                    G_IO_ERROR_NOT_SUPPORTED,
2794                    _("Operation not supported"));
2795       return FALSE;
2796     }
2797
2798   return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2799 }
2800
2801 /**
2802  * g_file_set_attributes_from_info:
2803  * @file: input #GFile.
2804  * @info: a #GFileInfo.
2805  * @flags: #GFileQueryInfoFlags
2806  * @cancellable: optional #GCancellable object, %NULL to ignore.
2807  * @error: a #GError, or %NULL 
2808  * 
2809  * Tries to set all attributes in the #GFileInfo on the target values, 
2810  * not stopping on the first error.
2811  * 
2812  * If there is any error during this operation then @error will be set to
2813  * the first error. Error on particular fields are flagged by setting 
2814  * the "status" field in the attribute value to 
2815  * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2816  * further errors.
2817  *
2818  * If @cancellable is not %NULL, then the operation can be cancelled by
2819  * triggering the cancellable object from another thread. If the operation
2820  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2821  * 
2822  * Returns: %TRUE if there was any error, %FALSE otherwise.
2823  **/
2824 gboolean
2825 g_file_set_attributes_from_info (GFile                *file,
2826                                  GFileInfo            *info,
2827                                  GFileQueryInfoFlags   flags,
2828                                  GCancellable         *cancellable,
2829                                  GError              **error)
2830 {
2831   GFileIface *iface;
2832
2833   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2834   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2835
2836   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2837     return FALSE;
2838   
2839   g_file_info_clear_status (info);
2840   
2841   iface = G_FILE_GET_IFACE (file);
2842
2843   return (* iface->set_attributes_from_info) (file, 
2844                                               info, 
2845                                               flags, 
2846                                               cancellable, 
2847                                               error);
2848 }
2849
2850
2851 static gboolean
2852 g_file_real_set_attributes_from_info (GFile                *file,
2853                                       GFileInfo            *info,
2854                                       GFileQueryInfoFlags   flags,
2855                                       GCancellable         *cancellable,
2856                                       GError              **error)
2857 {
2858   char **attributes;
2859   int i;
2860   gboolean res;
2861   GFileAttributeValue *value;
2862   
2863   res = TRUE;
2864   
2865   attributes = g_file_info_list_attributes (info, NULL);
2866
2867   for (i = 0; attributes[i] != NULL; i++)
2868     {
2869       value = _g_file_info_get_attribute_value (info, attributes[i]);
2870
2871       if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2872         continue;
2873
2874       if (!g_file_set_attribute (file, attributes[i],
2875                                  value->type, _g_file_attribute_value_peek_as_pointer (value),
2876                                  flags, cancellable, error))
2877         {
2878           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2879           res = FALSE;
2880           /* Don't set error multiple times */
2881           error = NULL;
2882         }
2883       else
2884         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2885     }
2886   
2887   g_strfreev (attributes);
2888   
2889   return res;
2890 }
2891
2892 /**
2893  * g_file_set_attributes_async:
2894  * @file: input #GFile.
2895  * @info: a #GFileInfo.
2896  * @flags: a #GFileQueryInfoFlags.
2897  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2898  *     of the request. 
2899  * @cancellable: optional #GCancellable object, %NULL to ignore.
2900  * @callback: a #GAsyncReadyCallback. 
2901  * @user_data: a #gpointer.
2902  *
2903  * Asynchronously sets the attributes of @file with @info.
2904  * 
2905  * For more details, see g_file_set_attributes_from_info() which is
2906  * the synchronous version of this call.
2907  *
2908  * When the operation is finished, @callback will be called. You can then call
2909  * g_file_set_attributes_finish() to get the result of the operation.
2910  **/
2911 void
2912 g_file_set_attributes_async (GFile               *file,
2913                              GFileInfo           *info,
2914                              GFileQueryInfoFlags  flags,
2915                              int                  io_priority,
2916                              GCancellable        *cancellable,
2917                              GAsyncReadyCallback  callback,
2918                              gpointer             user_data)
2919 {
2920   GFileIface *iface;
2921   
2922   g_return_if_fail (G_IS_FILE (file));
2923   g_return_if_fail (G_IS_FILE_INFO (info));
2924
2925   iface = G_FILE_GET_IFACE (file);
2926   (* iface->set_attributes_async) (file, 
2927                                    info, 
2928                                    flags, 
2929                                    io_priority, 
2930                                    cancellable, 
2931                                    callback, 
2932                                    user_data);
2933 }
2934
2935 /**
2936  * g_file_set_attributes_finish:
2937  * @file: input #GFile.
2938  * @result: a #GAsyncResult.
2939  * @info: a #GFileInfo.
2940  * @error: a #GError, or %NULL
2941  * 
2942  * Finishes setting an attribute started in g_file_set_attributes_async().
2943  * 
2944  * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2945  **/
2946 gboolean
2947 g_file_set_attributes_finish (GFile         *file,
2948                               GAsyncResult  *result,
2949                               GFileInfo    **info,
2950                               GError       **error)
2951 {
2952   GFileIface *iface;
2953   
2954   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2955   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2956
2957   /* No standard handling of errors here, as we must set info even
2958    * on errors 
2959    */
2960   iface = G_FILE_GET_IFACE (file);
2961   return (* iface->set_attributes_finish) (file, result, info, error);
2962 }
2963
2964 /**
2965  * g_file_set_attribute_string:
2966  * @file: input #GFile.
2967  * @attribute: a string containing the attribute's name.
2968  * @value: a string containing the attribute's value.
2969  * @flags: #GFileQueryInfoFlags.
2970  * @cancellable: optional #GCancellable object, %NULL to ignore.
2971  * @error: a #GError, or %NULL
2972  * 
2973  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value. 
2974  * If @attribute is of a different type, this operation will fail.
2975  * 
2976  * If @cancellable is not %NULL, then the operation can be cancelled by
2977  * triggering the cancellable object from another thread. If the operation
2978  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2979  * 
2980  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2981  **/
2982 gboolean
2983 g_file_set_attribute_string (GFile                *file,
2984                              const char           *attribute,
2985                              const char           *value,
2986                              GFileQueryInfoFlags   flags,
2987                              GCancellable         *cancellable,
2988                              GError              **error)
2989 {
2990   return g_file_set_attribute (file, attribute,
2991                                G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
2992                                flags, cancellable, error);
2993 }
2994
2995 /**
2996  * g_file_set_attribute_byte_string:
2997  * @file: input #GFile.
2998  * @attribute: a string containing the attribute's name.
2999  * @value: a string containing the attribute's new value.
3000  * @flags: a #GFileQueryInfoFlags.
3001  * @cancellable: optional #GCancellable object, %NULL to ignore.
3002  * @error: a #GError, or %NULL
3003  * 
3004  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value. 
3005  * If @attribute is of a different type, this operation will fail, 
3006  * returning %FALSE. 
3007  * 
3008  * If @cancellable is not %NULL, then the operation can be cancelled by
3009  * triggering the cancellable object from another thread. If the operation
3010  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3011  * 
3012  * Returns: %TRUE if the @attribute was successfully set to @value 
3013  * in the @file, %FALSE otherwise.
3014  **/
3015 gboolean
3016 g_file_set_attribute_byte_string  (GFile                *file,
3017                                    const char           *attribute,
3018                                    const char           *value,
3019                                    GFileQueryInfoFlags   flags,
3020                                    GCancellable         *cancellable,
3021                                    GError              **error)
3022 {
3023   return g_file_set_attribute (file, attribute,
3024                                G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3025                                flags, cancellable, error);
3026 }
3027
3028 /**
3029  * g_file_set_attribute_uint32:
3030  * @file: input #GFile.
3031  * @attribute: a string containing the attribute's name.
3032  * @value: a #guint32 containing the attribute's new value.
3033  * @flags: a #GFileQueryInfoFlags.
3034  * @cancellable: optional #GCancellable object, %NULL to ignore.
3035  * @error: a #GError, or %NULL
3036  * 
3037  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value. 
3038  * If @attribute is of a different type, this operation will fail.
3039  * 
3040  * If @cancellable is not %NULL, then the operation can be cancelled by
3041  * triggering the cancellable object from another thread. If the operation
3042  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3043  * 
3044  * Returns: %TRUE if the @attribute was successfully set to @value 
3045  * in the @file, %FALSE otherwise.
3046  **/
3047 gboolean
3048 g_file_set_attribute_uint32 (GFile                *file,
3049                              const char           *attribute,
3050                              guint32               value,
3051                              GFileQueryInfoFlags   flags,
3052                              GCancellable         *cancellable,
3053                              GError              **error)
3054 {
3055   return g_file_set_attribute (file, attribute,
3056                                G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3057                                flags, cancellable, error);
3058 }
3059
3060 /**
3061  * g_file_set_attribute_int32:
3062  * @file: input #GFile.
3063  * @attribute: a string containing the attribute's name.
3064  * @value: a #gint32 containing the attribute's new value.
3065  * @flags: a #GFileQueryInfoFlags.
3066  * @cancellable: optional #GCancellable object, %NULL to ignore.
3067  * @error: a #GError, or %NULL
3068  * 
3069  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value. 
3070  * If @attribute is of a different type, this operation will fail.
3071  * 
3072  * If @cancellable is not %NULL, then the operation can be cancelled by
3073  * triggering the cancellable object from another thread. If the operation
3074  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3075  * 
3076  * Returns: %TRUE if the @attribute was successfully set to @value 
3077  * in the @file, %FALSE otherwise. 
3078  **/
3079 gboolean
3080 g_file_set_attribute_int32 (GFile                *file,
3081                             const char           *attribute,
3082                             gint32                value,
3083                             GFileQueryInfoFlags   flags,
3084                             GCancellable         *cancellable,
3085                             GError              **error)
3086 {
3087   return g_file_set_attribute (file, attribute,
3088                                G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3089                                flags, cancellable, error);
3090 }
3091
3092 /**
3093  * g_file_set_attribute_uint64:
3094  * @file: input #GFile. 
3095  * @attribute: a string containing the attribute's name.
3096  * @value: a #guint64 containing the attribute's new value.
3097  * @flags: a #GFileQueryInfoFlags.
3098  * @cancellable: optional #GCancellable object, %NULL to ignore.
3099  * @error: a #GError, or %NULL
3100  * 
3101  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value. 
3102  * If @attribute is of a different type, this operation will fail.
3103  * 
3104  * If @cancellable is not %NULL, then the operation can be cancelled by
3105  * triggering the cancellable object from another thread. If the operation
3106  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3107  * 
3108  * Returns: %TRUE if the @attribute was successfully set to @value 
3109  * in the @file, %FALSE otherwise.
3110  **/
3111 gboolean
3112 g_file_set_attribute_uint64 (GFile                *file,
3113                              const char           *attribute,
3114                              guint64               value,
3115                              GFileQueryInfoFlags   flags,
3116                              GCancellable         *cancellable,
3117                              GError              **error)
3118  {
3119   return g_file_set_attribute (file, attribute,
3120                                G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3121                                flags, cancellable, error);
3122 }
3123
3124 /**
3125  * g_file_set_attribute_int64:
3126  * @file: input #GFile.
3127  * @attribute: a string containing the attribute's name.
3128  * @value: a #guint64 containing the attribute's new value.
3129  * @flags: a #GFileQueryInfoFlags.
3130  * @cancellable: optional #GCancellable object, %NULL to ignore.
3131  * @error: a #GError, or %NULL
3132  * 
3133  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value. 
3134  * If @attribute is of a different type, this operation will fail.
3135  * 
3136  * If @cancellable is not %NULL, then the operation can be cancelled by
3137  * triggering the cancellable object from another thread. If the operation
3138  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3139  * 
3140  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3141  **/
3142 gboolean
3143 g_file_set_attribute_int64 (GFile                *file,
3144                             const char           *attribute,
3145                             gint64                value,
3146                             GFileQueryInfoFlags   flags,
3147                             GCancellable         *cancellable,
3148                             GError              **error)
3149 {
3150   return g_file_set_attribute (file, attribute,
3151                                G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3152                                flags, cancellable, error);
3153 }
3154
3155 /**
3156  * g_file_mount_mountable:
3157  * @file: input #GFile.
3158  * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3159  * @cancellable: optional #GCancellable object, %NULL to ignore.
3160  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3161  * @user_data: the data to pass to callback function
3162  * 
3163  * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3164  * Using @mount_operation, you can request callbacks when, for instance, 
3165  * passwords are needed during authentication.
3166  *
3167  * If @cancellable is not %NULL, then the operation can be cancelled by
3168  * triggering the cancellable object from another thread. If the operation
3169  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3170  *
3171  * When the operation is finished, @callback will be called. You can then call
3172  * g_file_mount_mountable_finish() to get the result of the operation.
3173  **/
3174 void
3175 g_file_mount_mountable (GFile               *file,
3176                         GMountOperation     *mount_operation,
3177                         GCancellable        *cancellable,
3178                         GAsyncReadyCallback  callback,
3179                         gpointer             user_data)
3180 {
3181   GFileIface *iface;
3182
3183   g_return_if_fail (G_IS_FILE (file));
3184
3185   iface = G_FILE_GET_IFACE (file);
3186
3187   if (iface->mount_mountable == NULL)
3188     g_simple_async_report_error_in_idle (G_OBJECT (file),
3189                                          callback,
3190                                          user_data,
3191                                          G_IO_ERROR,
3192                                          G_IO_ERROR_NOT_SUPPORTED,
3193                                          _("Operation not supported"));
3194   
3195   (* iface->mount_mountable) (file,
3196                               mount_operation,
3197                               cancellable,
3198                               callback,
3199                               user_data);
3200 }
3201
3202 /**
3203  * g_file_mount_mountable_finish:
3204  * @file: input #GFile.
3205  * @result: a #GAsyncResult.
3206  * @error: a #GError, or %NULL
3207  *
3208  * Finishes a mount operation. See g_file_mount_mountable() for details.
3209  * 
3210  * Finish an asynchronous mount operation that was started 
3211  * with g_file_mount_mountable().
3212  *
3213  * Returns: a #GFile or %NULL on error.
3214  **/
3215 GFile *
3216 g_file_mount_mountable_finish (GFile         *file,
3217                                GAsyncResult  *result,
3218                                GError       **error)
3219 {
3220   GFileIface *iface;
3221
3222   g_return_val_if_fail (G_IS_FILE (file), NULL);
3223   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3224
3225   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3226     {
3227       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3228       if (g_simple_async_result_propagate_error (simple, error))
3229         return NULL;
3230     }
3231   
3232   iface = G_FILE_GET_IFACE (file);
3233   return (* iface->mount_mountable_finish) (file, result, error);
3234 }
3235
3236 /**
3237  * g_file_unmount_mountable:
3238  * @file: input #GFile.
3239  * @flags: flags affecting the operation
3240  * @cancellable: optional #GCancellable object, %NULL to ignore.
3241  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3242  * @user_data: the data to pass to callback function
3243  *
3244  * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3245  *
3246  * If @cancellable is not %NULL, then the operation can be cancelled by
3247  * triggering the cancellable object from another thread. If the operation
3248  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3249  *
3250  * When the operation is finished, @callback will be called. You can then call
3251  * g_file_unmount_mountable_finish() to get the result of the operation.
3252  **/
3253 void
3254 g_file_unmount_mountable (GFile               *file,
3255                           GMountUnmountFlags   flags,
3256                           GCancellable        *cancellable,
3257                           GAsyncReadyCallback  callback,
3258                           gpointer             user_data)
3259 {
3260   GFileIface *iface;
3261   
3262   g_return_if_fail (G_IS_FILE (file));
3263
3264   iface = G_FILE_GET_IFACE (file);
3265   
3266   if (iface->unmount_mountable == NULL)
3267     g_simple_async_report_error_in_idle (G_OBJECT (file),
3268                                          callback,
3269                                          user_data,
3270                                          G_IO_ERROR,
3271                                          G_IO_ERROR_NOT_SUPPORTED,
3272                                          _("Operation not supported"));
3273   
3274   (* iface->unmount_mountable) (file,
3275                                 flags,
3276                                 cancellable,
3277                                 callback,
3278                                 user_data);
3279 }
3280
3281 /**
3282  * g_file_unmount_mountable_finish:
3283  * @file: input #GFile.
3284  * @result: a #GAsyncResult.
3285  * @error: a #GError, or %NULL
3286  *
3287  * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3288  * 
3289  * Finish an asynchronous unmount operation that was started 
3290  * with g_file_unmount_mountable().
3291  *
3292  * Returns: %TRUE if the operation finished successfully. %FALSE
3293  * otherwise.
3294  **/
3295 gboolean
3296 g_file_unmount_mountable_finish (GFile         *file,
3297                                  GAsyncResult  *result,
3298                                  GError       **error)
3299 {
3300   GFileIface *iface;
3301   
3302   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3303   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3304
3305   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3306     {
3307       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3308       if (g_simple_async_result_propagate_error (simple, error))
3309         return FALSE;
3310     }
3311   
3312   iface = G_FILE_GET_IFACE (file);
3313   return (* iface->unmount_mountable_finish) (file, result, error);
3314 }
3315
3316 /**
3317  * g_file_eject_mountable:
3318  * @file: input #GFile.
3319  * @flags: flags affecting the operation
3320  * @cancellable: optional #GCancellable object, %NULL to ignore.
3321  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3322  * @user_data: the data to pass to callback function
3323  * 
3324  * Starts an asynchronous eject on a mountable.  
3325  * When this operation has completed, @callback will be called with
3326  * @user_user data, and the operation can be finalized with 
3327  * g_file_eject_mountable_finish().
3328  * 
3329  * If @cancellable is not %NULL, then the operation can be cancelled by
3330  * triggering the cancellable object from another thread. If the operation
3331  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3332  **/
3333 void
3334 g_file_eject_mountable (GFile               *file,
3335                         GMountUnmountFlags   flags,
3336                         GCancellable        *cancellable,
3337                         GAsyncReadyCallback  callback,
3338                         gpointer             user_data)
3339 {
3340   GFileIface *iface;
3341
3342   g_return_if_fail (G_IS_FILE (file));
3343
3344   iface = G_FILE_GET_IFACE (file);
3345   
3346   if (iface->eject_mountable == NULL)
3347     g_simple_async_report_error_in_idle (G_OBJECT (file),
3348                                          callback,
3349                                          user_data,
3350                                          G_IO_ERROR,
3351                                          G_IO_ERROR_NOT_SUPPORTED,
3352                                          _("Operation not supported"));
3353   
3354   (* iface->eject_mountable) (file,
3355                               flags,
3356                               cancellable,
3357                               callback,
3358                               user_data);
3359 }
3360
3361 /**
3362  * g_file_eject_mountable_finish:
3363  * @file: input #GFile.
3364  * @result: a #GAsyncResult.
3365  * @error: a #GError, or %NULL
3366  * 
3367  * Finishes an asynchronous eject operation started by 
3368  * g_file_eject_mountable().
3369  * 
3370  * Returns: %TRUE if the @file was ejected successfully. %FALSE 
3371  * otherwise.
3372  **/
3373 gboolean
3374 g_file_eject_mountable_finish (GFile         *file,
3375                                GAsyncResult  *result,
3376                                GError       **error)
3377 {
3378   GFileIface *iface;
3379   
3380   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3381   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3382
3383   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3384     {
3385       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3386       if (g_simple_async_result_propagate_error (simple, error))
3387         return FALSE;
3388     }
3389   
3390   iface = G_FILE_GET_IFACE (file);
3391   return (* iface->eject_mountable_finish) (file, result, error);
3392 }
3393
3394 /**
3395  * g_file_monitor_directory:
3396  * @file: input #GFile.
3397  * @flags: a set of #GFileMonitorFlags.
3398  * @cancellable: optional #GCancellable object, %NULL to ignore.
3399  * @error: a #GError, or %NULL.
3400  * 
3401  * Obtains a directory monitor for the given file.
3402  * This may fail if directory monitoring is not supported.
3403  *
3404  * If @cancellable is not %NULL, then the operation can be cancelled by
3405  * triggering the cancellable object from another thread. If the operation
3406  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3407  * 
3408  * Returns: a #GFileMonitor for the given @file, 
3409  * or %NULL on error.
3410  **/
3411 GFileMonitor*
3412 g_file_monitor_directory (GFile             *file,
3413                           GFileMonitorFlags  flags,
3414                           GCancellable      *cancellable,
3415                           GError           **error)
3416 {
3417   GFileIface *iface;
3418
3419   g_return_val_if_fail (G_IS_FILE (file), NULL);
3420
3421   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3422     return NULL;
3423
3424   iface = G_FILE_GET_IFACE (file);
3425
3426   if (iface->monitor_dir == NULL)
3427     {
3428       g_set_error (error, G_IO_ERROR,
3429                    G_IO_ERROR_NOT_SUPPORTED,
3430                    _("Operation not supported"));
3431       return NULL;
3432     }
3433
3434   return (* iface->monitor_dir) (file, flags, cancellable, error);
3435 }
3436
3437 /**
3438  * g_file_monitor_file:
3439  * @file: input #GFile.
3440  * @flags: a set of #GFileMonitorFlags.
3441  * @cancellable: optional #GCancellable object, %NULL to ignore.
3442  * @error: a #GError, or %NULL.
3443  * 
3444  * Obtains a file monitor for the given file. If no file notification
3445  * mechanism exists, then regular polling of the file is used.
3446  *
3447  * If @cancellable is not %NULL, then the operation can be cancelled by
3448  * triggering the cancellable object from another thread. If the operation
3449  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3450  * 
3451  * Returns: a #GFileMonitor for the given @file.
3452  **/
3453 GFileMonitor*
3454 g_file_monitor_file (GFile             *file,
3455                      GFileMonitorFlags  flags,
3456                      GCancellable      *cancellable,
3457                      GError           **error)
3458 {
3459   GFileIface *iface;
3460   GFileMonitor *monitor;
3461   
3462   g_return_val_if_fail (G_IS_FILE (file), NULL);
3463
3464   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3465     return NULL;
3466
3467   iface = G_FILE_GET_IFACE (file);
3468
3469   monitor = NULL;
3470   
3471   if (iface->monitor_file)
3472     monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3473
3474 /* Fallback to polling */
3475   if (monitor == NULL)
3476     monitor = _g_poll_file_monitor_new (file);
3477
3478   return monitor;
3479 }
3480
3481 /********************************************
3482  *   Default implementation of async ops    *
3483  ********************************************/
3484
3485 typedef struct {
3486   char *attributes;
3487   GFileQueryInfoFlags flags;
3488   GFileInfo *info;
3489 } QueryInfoAsyncData;
3490
3491 static void
3492 query_info_data_free (QueryInfoAsyncData *data)
3493 {
3494   if (data->info)
3495     g_object_unref (data->info);
3496   g_free (data->attributes);
3497   g_free (data);
3498 }
3499
3500 static void
3501 query_info_async_thread (GSimpleAsyncResult *res,
3502                          GObject            *object,
3503                          GCancellable       *cancellable)
3504 {
3505   GError *error = NULL;
3506   QueryInfoAsyncData *data;
3507   GFileInfo *info;
3508   
3509   data = g_simple_async_result_get_op_res_gpointer (res);
3510   
3511   info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3512
3513   if (info == NULL)
3514     {
3515       g_simple_async_result_set_from_error (res, error);
3516       g_error_free (error);
3517     }
3518   else
3519     data->info = info;
3520 }
3521
3522 static void
3523 g_file_real_query_info_async (GFile               *file,
3524                               const char          *attributes,
3525                               GFileQueryInfoFlags  flags,
3526                               int                  io_priority,
3527                               GCancellable        *cancellable,
3528                               GAsyncReadyCallback  callback,
3529                               gpointer             user_data)
3530 {
3531   GSimpleAsyncResult *res;
3532   QueryInfoAsyncData *data;
3533
3534   data = g_new0 (QueryInfoAsyncData, 1);
3535   data->attributes = g_strdup (attributes);
3536   data->flags = flags;
3537   
3538   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3539   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3540   
3541   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3542   g_object_unref (res);
3543 }
3544
3545 static GFileInfo *
3546 g_file_real_query_info_finish (GFile         *file,
3547                                GAsyncResult  *res,
3548                                GError       **error)
3549 {
3550   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3551   QueryInfoAsyncData *data;
3552
3553   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3554
3555   data = g_simple_async_result_get_op_res_gpointer (simple);
3556   if (data->info)
3557     return g_object_ref (data->info);
3558   
3559   return NULL;
3560 }
3561
3562 typedef struct {
3563   char *attributes;
3564   GFileQueryInfoFlags flags;
3565   GFileEnumerator *enumerator;
3566 } EnumerateChildrenAsyncData;
3567
3568 static void
3569 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3570 {
3571   if (data->enumerator)
3572     g_object_unref (data->enumerator);
3573   g_free (data->attributes);
3574   g_free (data);
3575 }
3576
3577 static void
3578 enumerate_children_async_thread (GSimpleAsyncResult *res,
3579                                  GObject            *object,
3580                                  GCancellable       *cancellable)
3581 {
3582   GError *error = NULL;
3583   EnumerateChildrenAsyncData *data;
3584   GFileEnumerator *enumerator;
3585   
3586   data = g_simple_async_result_get_op_res_gpointer (res);
3587   
3588   enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3589
3590   if (enumerator == NULL)
3591     {
3592       g_simple_async_result_set_from_error (res, error);
3593       g_error_free (error);
3594     }
3595   else
3596     data->enumerator = enumerator;
3597 }
3598
3599 static void
3600 g_file_real_enumerate_children_async (GFile               *file,
3601                                       const char          *attributes,
3602                                       GFileQueryInfoFlags  flags,
3603                                       int                  io_priority,
3604                                       GCancellable        *cancellable,
3605                                       GAsyncReadyCallback  callback,
3606                                       gpointer             user_data)
3607 {
3608   GSimpleAsyncResult *res;
3609   EnumerateChildrenAsyncData *data;
3610
3611   data = g_new0 (EnumerateChildrenAsyncData, 1);
3612   data->attributes = g_strdup (attributes);
3613   data->flags = flags;
3614   
3615   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3616   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3617   
3618   g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3619   g_object_unref (res);
3620 }
3621
3622 static GFileEnumerator *
3623 g_file_real_enumerate_children_finish (GFile         *file,
3624                                        GAsyncResult  *res,
3625                                        GError       **error)
3626 {
3627   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3628   EnumerateChildrenAsyncData *data;
3629
3630   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3631
3632   data = g_simple_async_result_get_op_res_gpointer (simple);
3633   if (data->enumerator)
3634     return g_object_ref (data->enumerator);
3635   
3636   return NULL;
3637 }
3638
3639 static void
3640 open_read_async_thread (GSimpleAsyncResult *res,
3641                         GObject            *object,
3642                         GCancellable       *cancellable)
3643 {
3644   GFileIface *iface;
3645   GFileInputStream *stream;
3646   GError *error = NULL;
3647
3648   iface = G_FILE_GET_IFACE (object);
3649
3650   stream = iface->read_fn (G_FILE (object), cancellable, &error);
3651
3652   if (stream == NULL)
3653     {
3654       g_simple_async_result_set_from_error (res, error);
3655       g_error_free (error);
3656     }
3657   else
3658     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3659 }
3660
3661 static void
3662 g_file_real_read_async (GFile               *file,
3663                         int                  io_priority,
3664                         GCancellable        *cancellable,
3665                         GAsyncReadyCallback  callback,
3666                         gpointer             user_data)
3667 {
3668   GSimpleAsyncResult *res;
3669   
3670   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3671   
3672   g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3673   g_object_unref (res);
3674 }
3675
3676 static GFileInputStream *
3677 g_file_real_read_finish (GFile         *file,
3678                          GAsyncResult  *res,
3679                          GError       **error)
3680 {
3681   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3682   gpointer op;
3683
3684   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3685
3686   op = g_simple_async_result_get_op_res_gpointer (simple);
3687   if (op)
3688     return g_object_ref (op);
3689   
3690   return NULL;
3691 }
3692
3693 static void
3694 append_to_async_thread (GSimpleAsyncResult *res,
3695                         GObject            *object,
3696                         GCancellable       *cancellable)
3697 {
3698   GFileIface *iface;
3699   GFileCreateFlags *data;
3700   GFileOutputStream *stream;
3701   GError *error = NULL;
3702
3703   iface = G_FILE_GET_IFACE (object);
3704
3705   data = g_simple_async_result_get_op_res_gpointer (res);
3706
3707   stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3708
3709   if (stream == NULL)
3710     {
3711       g_simple_async_result_set_from_error (res, error);
3712       g_error_free (error);
3713     }
3714   else
3715     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3716 }
3717
3718 static void
3719 g_file_real_append_to_async (GFile               *file,
3720                              GFileCreateFlags     flags,
3721                              int                  io_priority,
3722                              GCancellable        *cancellable,
3723                              GAsyncReadyCallback  callback,
3724                              gpointer             user_data)
3725 {
3726   GFileCreateFlags *data;
3727   GSimpleAsyncResult *res;
3728
3729   data = g_new0 (GFileCreateFlags, 1);
3730   *data = flags;
3731
3732   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3733   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3734
3735   g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3736   g_object_unref (res);
3737 }
3738
3739 static GFileOutputStream *
3740 g_file_real_append_to_finish (GFile         *file,
3741                               GAsyncResult  *res,
3742                               GError       **error)
3743 {
3744   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3745   gpointer op;
3746
3747   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3748
3749   op = g_simple_async_result_get_op_res_gpointer (simple);
3750   if (op)
3751     return g_object_ref (op);
3752   
3753   return NULL;
3754 }
3755
3756 static void
3757 create_async_thread (GSimpleAsyncResult *res,
3758                      GObject            *object,
3759                      GCancellable       *cancellable)
3760 {
3761   GFileIface *iface;
3762   GFileCreateFlags *data;
3763   GFileOutputStream *stream;
3764   GError *error = NULL;
3765
3766   iface = G_FILE_GET_IFACE (object);
3767
3768   data = g_simple_async_result_get_op_res_gpointer (res);
3769
3770   stream = iface->create (G_FILE (object), *data, cancellable, &error);
3771
3772   if (stream == NULL)
3773     {
3774       g_simple_async_result_set_from_error (res, error);
3775       g_error_free (error);
3776     }
3777   else
3778     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3779 }
3780
3781 static void
3782 g_file_real_create_async (GFile               *file,
3783                           GFileCreateFlags     flags,
3784                           int                  io_priority,
3785                           GCancellable        *cancellable,
3786                           GAsyncReadyCallback  callback,
3787                           gpointer             user_data)
3788 {
3789   GFileCreateFlags *data;
3790   GSimpleAsyncResult *res;
3791
3792   data = g_new0 (GFileCreateFlags, 1);
3793   *data = flags;
3794
3795   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3796   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3797
3798   g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3799   g_object_unref (res);
3800 }
3801
3802 static GFileOutputStream *
3803 g_file_real_create_finish (GFile         *file,
3804                            GAsyncResult  *res,
3805                            GError       **error)
3806 {
3807   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3808   gpointer op;
3809
3810   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3811
3812   op = g_simple_async_result_get_op_res_gpointer (simple);
3813   if (op)
3814     return g_object_ref (op);
3815   
3816   return NULL;
3817 }
3818
3819 typedef struct {
3820   GFileOutputStream *stream;
3821   char *etag;
3822   gboolean make_backup;
3823   GFileCreateFlags flags;
3824 } ReplaceAsyncData;
3825
3826 static void
3827 replace_async_data_free (ReplaceAsyncData *data)
3828 {
3829   if (data->stream)
3830     g_object_unref (data->stream);
3831   g_free (data->etag);
3832   g_free (data);
3833 }
3834
3835 static void
3836 replace_async_thread (GSimpleAsyncResult *res,
3837                       GObject            *object,
3838                       GCancellable       *cancellable)
3839 {
3840   GFileIface *iface;
3841   GFileOutputStream *stream;
3842   GError *error = NULL;
3843   ReplaceAsyncData *data;
3844
3845   iface = G_FILE_GET_IFACE (object);
3846   
3847   data = g_simple_async_result_get_op_res_gpointer (res);
3848
3849   stream = iface->replace (G_FILE (object),
3850                            data->etag,
3851                            data->make_backup,
3852                            data->flags,
3853                            cancellable,
3854                            &error);
3855
3856   if (stream == NULL)
3857     {
3858       g_simple_async_result_set_from_error (res, error);
3859       g_error_free (error);
3860     }
3861   else
3862     data->stream = stream;
3863 }
3864
3865 static void
3866 g_file_real_replace_async (GFile               *file,
3867                            const char          *etag,
3868                            gboolean             make_backup,
3869                            GFileCreateFlags     flags,
3870                            int                  io_priority,
3871                            GCancellable        *cancellable,
3872                            GAsyncReadyCallback  callback,
3873                            gpointer             user_data)
3874 {
3875   GSimpleAsyncResult *res;
3876   ReplaceAsyncData *data;
3877
3878   data = g_new0 (ReplaceAsyncData, 1);
3879   data->etag = g_strdup (etag);
3880   data->make_backup = make_backup;
3881   data->flags = flags;
3882
3883   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3884   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3885
3886   g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3887   g_object_unref (res);
3888 }
3889
3890 static GFileOutputStream *
3891 g_file_real_replace_finish (GFile         *file,
3892                             GAsyncResult  *res,
3893                             GError       **error)
3894 {
3895   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3896   ReplaceAsyncData *data;
3897
3898   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3899
3900   data = g_simple_async_result_get_op_res_gpointer (simple);
3901   if (data->stream)
3902     return g_object_ref (data->stream);
3903   
3904   return NULL;
3905 }
3906
3907 typedef struct {
3908   char *name;
3909   GFile *file;
3910 } SetDisplayNameAsyncData;
3911
3912 static void
3913 set_display_name_data_free (SetDisplayNameAsyncData *data)
3914 {
3915   g_free (data->name);
3916   if (data->file)
3917     g_object_unref (data->file);
3918   g_free (data);
3919 }
3920
3921 static void
3922 set_display_name_async_thread (GSimpleAsyncResult *res,
3923                                GObject            *object,
3924                                GCancellable       *cancellable)
3925 {
3926   GError *error = NULL;
3927   SetDisplayNameAsyncData *data;
3928   GFile *file;
3929   
3930   data = g_simple_async_result_get_op_res_gpointer (res);
3931   
3932   file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3933
3934   if (file == NULL)
3935     {
3936       g_simple_async_result_set_from_error (res, error);
3937       g_error_free (error);
3938     }
3939   else
3940     data->file = file;
3941 }
3942
3943 static void
3944 g_file_real_set_display_name_async (GFile               *file,  
3945                                     const char          *display_name,
3946                                     int                  io_priority,
3947                                     GCancellable        *cancellable,
3948                                     GAsyncReadyCallback  callback,
3949                                     gpointer             user_data)
3950 {
3951   GSimpleAsyncResult *res;
3952   SetDisplayNameAsyncData *data;
3953
3954   data = g_new0 (SetDisplayNameAsyncData, 1);
3955   data->name = g_strdup (display_name);
3956   
3957   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3958   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3959   
3960   g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3961   g_object_unref (res);
3962 }
3963
3964 static GFile *
3965 g_file_real_set_display_name_finish (GFile         *file,
3966                                      GAsyncResult  *res,
3967                                      GError       **error)
3968 {
3969   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3970   SetDisplayNameAsyncData *data;
3971
3972   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3973
3974   data = g_simple_async_result_get_op_res_gpointer (simple);
3975   if (data->file)
3976     return g_object_ref (data->file);
3977   
3978   return NULL;
3979 }
3980
3981 typedef struct {
3982   GFileQueryInfoFlags flags;
3983   GFileInfo *info;
3984   gboolean res;
3985   GError *error;
3986 } SetInfoAsyncData;
3987
3988 static void
3989 set_info_data_free (SetInfoAsyncData *data)
3990 {
3991   if (data->info)
3992     g_object_unref (data->info);
3993   if (data->error)
3994     g_error_free (data->error);
3995   g_free (data);
3996 }
3997
3998 static void
3999 set_info_async_thread (GSimpleAsyncResult *res,
4000                        GObject            *object,
4001                        GCancellable       *cancellable)
4002 {
4003   SetInfoAsyncData *data;
4004   
4005   data = g_simple_async_result_get_op_res_gpointer (res);
4006   
4007   data->error = NULL;
4008   data->res = g_file_set_attributes_from_info (G_FILE (object),
4009                                                data->info,
4010                                                data->flags,
4011                                                cancellable,
4012                                                &data->error);
4013 }
4014
4015 static void
4016 g_file_real_set_attributes_async (GFile               *file,
4017                                   GFileInfo           *info,
4018                                   GFileQueryInfoFlags  flags,
4019                                   int                  io_priority,
4020                                   GCancellable        *cancellable,
4021                                   GAsyncReadyCallback  callback,
4022                                   gpointer             user_data)
4023 {
4024   GSimpleAsyncResult *res;
4025   SetInfoAsyncData *data;
4026
4027   data = g_new0 (SetInfoAsyncData, 1);
4028   data->info = g_file_info_dup (info);
4029   data->flags = flags;
4030   
4031   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4032   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4033   
4034   g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4035   g_object_unref (res);
4036 }
4037
4038 static gboolean
4039 g_file_real_set_attributes_finish (GFile         *file,
4040                                    GAsyncResult  *res,
4041                                    GFileInfo    **info,
4042                                    GError       **error)
4043 {
4044   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4045   SetInfoAsyncData *data;
4046   
4047   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4048
4049   data = g_simple_async_result_get_op_res_gpointer (simple);
4050
4051   if (info) 
4052     *info = g_object_ref (data->info);
4053
4054   if (error != NULL && data->error) 
4055     *error = g_error_copy (data->error);
4056   
4057   return data->res;
4058 }
4059
4060 static void
4061 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4062                                     GObject            *object,
4063                                     GCancellable       *cancellable)
4064 {
4065   GError *error = NULL;
4066   GMount *mount;
4067   
4068   mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4069
4070   if (mount == NULL)
4071     {
4072       g_simple_async_result_set_from_error (res, error);
4073       g_error_free (error);
4074     }
4075   else
4076     g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4077 }
4078
4079 static void
4080 g_file_real_find_enclosing_mount_async (GFile               *file,
4081                                         int                  io_priority,
4082                                         GCancellable        *cancellable,
4083                                         GAsyncReadyCallback  callback,
4084                                         gpointer             user_data)
4085 {
4086   GSimpleAsyncResult *res;
4087   
4088   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4089   
4090   g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4091   g_object_unref (res);
4092 }
4093
4094 static GMount *
4095 g_file_real_find_enclosing_mount_finish (GFile         *file,
4096                                           GAsyncResult  *res,
4097                                           GError       **error)
4098 {
4099   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4100   GMount *mount;
4101
4102   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4103
4104   mount = g_simple_async_result_get_op_res_gpointer (simple);
4105   return g_object_ref (mount);
4106 }
4107
4108
4109 /********************************************
4110  *   Default VFS operations                 *
4111  ********************************************/
4112
4113 /**
4114  * g_file_new_for_path:
4115  * @path: a string containing a relative or absolute path.
4116  * 
4117  * Constructs a #GFile for a given path. This operation never
4118  * fails, but the returned object might not support any I/O
4119  * operation if @path is malformed.
4120  * 
4121  * Returns: a new #GFile for the given @path. 
4122  **/
4123 GFile *
4124 g_file_new_for_path (const char *path)
4125 {
4126   g_return_val_if_fail (path != NULL, NULL);
4127
4128   return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4129 }
4130  
4131 /**
4132  * g_file_new_for_uri:
4133  * @uri: a string containing a URI.
4134  * 
4135  * Constructs a #GFile for a given URI. This operation never 
4136  * fails, but the returned object might not support any I/O 
4137  * operation if @uri is malformed or if the uri type is 
4138  * not supported.
4139  * 
4140  * Returns: a #GFile for the given @uri.
4141  **/ 
4142 GFile *
4143 g_file_new_for_uri (const char *uri)
4144 {
4145   g_return_val_if_fail (uri != NULL, NULL);
4146
4147   return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4148 }
4149   
4150 /**
4151  * g_file_parse_name:
4152  * @parse_name: a file name or path to be parsed.
4153  * 
4154  * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4155  * This operation never fails, but the returned object might not support any I/O
4156  * operation if the @parse_name cannot be parsed.
4157  * 
4158  * Returns: a new #GFile.
4159  **/
4160 GFile *
4161 g_file_parse_name (const char *parse_name)
4162 {
4163   g_return_val_if_fail (parse_name != NULL, NULL);
4164
4165   return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4166 }
4167
4168 static gboolean
4169 is_valid_scheme_character (char c)
4170 {
4171   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4172 }
4173
4174 static gboolean
4175 has_valid_scheme (const char *uri)
4176 {
4177   const char *p;
4178   
4179   p = uri;
4180   
4181   if (!is_valid_scheme_character (*p))
4182     return FALSE;
4183
4184   do {
4185     p++;
4186   } while (is_valid_scheme_character (*p));
4187
4188   return *p == ':';
4189 }
4190
4191 /**
4192  * g_file_new_for_commandline_arg:
4193  * @arg: a command line string.
4194  * 
4195  * Creates a #GFile with the given argument from the command line. The value of
4196  * @arg can be either a URI, an absolute path or a relative path resolved
4197  * relative to the current working directory.
4198  * This operation never fails, but the returned object might not support any
4199  * I/O operation if @arg points to a malformed path.
4200  *
4201  * Returns: a new #GFile. 
4202  **/
4203 GFile *
4204 g_file_new_for_commandline_arg (const char *arg)
4205 {
4206   GFile *file;
4207   char *filename;
4208   char *current_dir;
4209   
4210   g_return_val_if_fail (arg != NULL, NULL);
4211   
4212   if (g_path_is_absolute (arg))
4213     return g_file_new_for_path (arg);
4214
4215   if (has_valid_scheme (arg))
4216     return g_file_new_for_uri (arg);
4217     
4218   current_dir = g_get_current_dir ();
4219   filename = g_build_filename (current_dir, arg, NULL);
4220   g_free (current_dir);
4221   
4222   file = g_file_new_for_path (filename);
4223   g_free (filename);
4224   
4225   return file;
4226 }
4227
4228 /**
4229  * g_file_mount_enclosing_volume:
4230  * @location: input #GFile.
4231  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4232  * @cancellable: optional #GCancellable object, %NULL to ignore.
4233  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4234  * @user_data: the data to pass to callback function
4235  * 
4236  * Starts a @mount_operation, mounting the volume that contains the file @location. 
4237  * 
4238  * When this operation has completed, @callback will be called with
4239  * @user_user data, and the operation can be finalized with 
4240  * g_file_mount_enclosing_volume_finish().
4241  * 
4242  * If @cancellable is not %NULL, then the operation can be cancelled by
4243  * triggering the cancellable object from another thread. If the operation
4244  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4245  **/
4246 void
4247 g_file_mount_enclosing_volume (GFile               *location,
4248                                GMountOperation     *mount_operation,
4249                                GCancellable        *cancellable,
4250                                GAsyncReadyCallback  callback,
4251                                gpointer             user_data)
4252 {
4253   GFileIface *iface;
4254
4255   g_return_if_fail (G_IS_FILE (location));
4256
4257   iface = G_FILE_GET_IFACE (location);
4258
4259   if (iface->mount_enclosing_volume == NULL)
4260     {
4261       g_simple_async_report_error_in_idle (G_OBJECT (location),
4262                                            callback, user_data,
4263                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4264                                            _("volume doesn't implement mount"));
4265       
4266       return;
4267     }
4268   
4269   (* iface->mount_enclosing_volume) (location, mount_operation, cancellable, callback, user_data);
4270
4271 }
4272
4273 /**
4274  * g_file_mount_enclosing_volume_finish:
4275  * @location: input #GFile.
4276  * @result: a #GAsyncResult.
4277  * @error: a #GError, or %NULL
4278  * 
4279  * Finishes a mount operation started by g_file_mount_enclosing_volume().
4280  * 
4281  * Returns: %TRUE if successful. If an error
4282  * has occurred, this function will return %FALSE and set @error
4283  * appropriately if present.
4284  **/
4285 gboolean
4286 g_file_mount_enclosing_volume_finish (GFile         *location,
4287                                       GAsyncResult  *result,
4288                                       GError       **error)
4289 {
4290   GFileIface *iface;
4291
4292   g_return_val_if_fail (G_IS_FILE (location), FALSE);
4293   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4294
4295   if (G_IS_SIMPLE_ASYNC_RESULT (result))
4296     {
4297       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4298       if (g_simple_async_result_propagate_error (simple, error))
4299         return FALSE;
4300     }
4301   
4302   iface = G_FILE_GET_IFACE (location);
4303
4304   return (* iface->mount_enclosing_volume_finish) (location, result, error);
4305 }
4306
4307 /********************************************
4308  *   Utility functions                      *
4309  ********************************************/
4310
4311 #define GET_CONTENT_BLOCK_SIZE 8192
4312
4313 /**
4314  * g_file_load_contents:
4315  * @file: input #GFile.
4316  * @cancellable: optional #GCancellable object, %NULL to ignore.
4317  * @contents: a location to place the contents of the file.
4318  * @length: a location to place the length of the contents of the file.
4319  * @etag_out: a location to place the current entity tag for the file.
4320  * @error: a #GError, or %NULL
4321  *
4322  * Loads the content of the file into memory, returning the size of
4323  * the data. The data is always zero terminated, but this is not
4324  * included in the resultant @length.
4325  * 
4326  * If @cancellable is not %NULL, then the operation can be cancelled by
4327  * triggering the cancellable object from another thread. If the operation
4328  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4329  * 
4330  * Returns: %TRUE if the @file's contents were successfully loaded.
4331  * %FALSE if there were errors..
4332  **/
4333 gboolean
4334 g_file_load_contents (GFile         *file,
4335                       GCancellable  *cancellable,
4336                       char         **contents,
4337                       gsize         *length,
4338                       char         **etag_out,
4339                       GError       **error)
4340 {
4341   GFileInputStream *in;
4342   GByteArray *content;
4343   gsize pos;
4344   gssize res;
4345   GFileInfo *info;
4346
4347   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4348   g_return_val_if_fail (contents != NULL, FALSE);
4349
4350   in = g_file_read (file, cancellable, error);
4351   if (in == NULL)
4352     return FALSE;
4353
4354   content = g_byte_array_new ();
4355   pos = 0;
4356   
4357   g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4358   while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4359                                      content->data + pos,
4360                                      GET_CONTENT_BLOCK_SIZE,
4361                                      cancellable, error)) > 0)
4362     {
4363       pos += res;
4364       g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4365     }
4366
4367   if (etag_out)
4368     {
4369       *etag_out = NULL;
4370       
4371       info = g_file_input_stream_query_info (in,
4372                                              G_FILE_ATTRIBUTE_ETAG_VALUE,
4373                                              cancellable,
4374                                              NULL);
4375       if (info)
4376         {
4377           *etag_out = g_strdup (g_file_info_get_etag (info));
4378           g_object_unref (info);
4379         }
4380     } 
4381
4382   /* Ignore errors on close */
4383   g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4384   g_object_unref (in);
4385
4386   if (res < 0)
4387     {
4388       /* error is set already */
4389       g_byte_array_free (content, TRUE);
4390       return FALSE;
4391     }
4392
4393   if (length)
4394     *length = pos;
4395
4396   /* Zero terminate (we got an extra byte allocated for this */
4397   content->data[pos] = 0;
4398   
4399   *contents = (char *)g_byte_array_free (content, FALSE);
4400   
4401   return TRUE;
4402 }
4403
4404 typedef struct {
4405   GFile *file;
4406   GError *error;
4407   GCancellable *cancellable;
4408   GFileReadMoreCallback read_more_callback;
4409   GAsyncReadyCallback callback;
4410   gpointer user_data;
4411   GByteArray *content;
4412   gsize pos;
4413   char *etag;
4414 } LoadContentsData;
4415
4416
4417 static void
4418 load_contents_data_free (LoadContentsData *data)
4419 {
4420   if (data->error)
4421     g_error_free (data->error);
4422   if (data->cancellable)
4423     g_object_unref (data->cancellable);
4424   if (data->content)
4425     g_byte_array_free (data->content, TRUE);
4426   g_free (data->etag);
4427   g_object_unref (data->file);
4428   g_free (data);
4429 }
4430
4431 static void
4432 load_contents_close_callback (GObject      *obj,
4433                               GAsyncResult *close_res,
4434                               gpointer      user_data)
4435 {
4436   GInputStream *stream = G_INPUT_STREAM (obj);
4437   LoadContentsData *data = user_data;
4438   GSimpleAsyncResult *res;
4439
4440   /* Ignore errors here, we're only reading anyway */
4441   g_input_stream_close_finish (stream, close_res, NULL);
4442   g_object_unref (stream);
4443
4444   res = g_simple_async_result_new (G_OBJECT (data->file),
4445                                    data->callback,
4446                                    data->user_data,
4447                                    g_file_load_contents_async);
4448   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4449   g_simple_async_result_complete (res);
4450   g_object_unref (res);
4451 }
4452
4453 static void
4454 load_contents_fstat_callback (GObject      *obj,
4455                               GAsyncResult *stat_res,
4456                               gpointer      user_data)
4457 {
4458   GInputStream *stream = G_INPUT_STREAM (obj);
4459   LoadContentsData *data = user_data;
4460   GFileInfo *info;
4461
4462   info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4463                                                    stat_res, NULL);
4464   if (info)
4465     {
4466       data->etag = g_strdup (g_file_info_get_etag (info));
4467       g_object_unref (info);
4468     }
4469
4470   g_input_stream_close_async (stream, 0,
4471                               data->cancellable,
4472                               load_contents_close_callback, data);
4473 }
4474
4475 static void
4476 load_contents_read_callback (GObject      *obj,
4477                              GAsyncResult *read_res,
4478                              gpointer      user_data)
4479 {
4480   GInputStream *stream = G_INPUT_STREAM (obj);
4481   LoadContentsData *data = user_data;
4482   GError *error = NULL;
4483   gssize read_size;
4484
4485   read_size = g_input_stream_read_finish (stream, read_res, &error);
4486
4487   if (read_size < 0) 
4488     {
4489       /* Error or EOF, close the file */
4490       data->error = error;
4491       g_input_stream_close_async (stream, 0,
4492                                   data->cancellable,
4493                                   load_contents_close_callback, data);
4494     }
4495   else if (read_size == 0)
4496     {
4497       g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4498                                             G_FILE_ATTRIBUTE_ETAG_VALUE,
4499                                             0,
4500                                             data->cancellable,
4501                                             load_contents_fstat_callback,
4502                                             data);
4503     }
4504   else if (read_size > 0)
4505     {
4506       data->pos += read_size;
4507       
4508       g_byte_array_set_size (data->content,
4509                              data->pos + GET_CONTENT_BLOCK_SIZE);
4510
4511
4512       if (data->read_more_callback &&
4513           !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4514         g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4515                                               G_FILE_ATTRIBUTE_ETAG_VALUE,
4516                                               0,
4517                                               data->cancellable,
4518                                               load_contents_fstat_callback,
4519                                               data);
4520       else 
4521         g_input_stream_read_async (stream,
4522                                    data->content->data + data->pos,
4523                                    GET_CONTENT_BLOCK_SIZE,
4524                                    0,
4525                                    data->cancellable,
4526                                    load_contents_read_callback,
4527                                    data);
4528     }
4529 }
4530
4531 static void
4532 load_contents_open_callback (GObject      *obj,
4533                              GAsyncResult *open_res,
4534                              gpointer      user_data)
4535 {
4536   GFile *file = G_FILE (obj);
4537   GFileInputStream *stream;
4538   LoadContentsData *data = user_data;
4539   GError *error = NULL;
4540   GSimpleAsyncResult *res;
4541
4542   stream = g_file_read_finish (file, open_res, &error);
4543
4544   if (stream)
4545     {
4546       g_byte_array_set_size (data->content,
4547                              data->pos + GET_CONTENT_BLOCK_SIZE);
4548       g_input_stream_read_async (G_INPUT_STREAM (stream),
4549                                  data->content->data + data->pos,
4550                                  GET_CONTENT_BLOCK_SIZE,
4551                                  0,
4552                                  data->cancellable,
4553                                  load_contents_read_callback,
4554                                  data);
4555       
4556     }
4557   else
4558     {
4559       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4560                                                   data->callback,
4561                                                   data->user_data,
4562                                                   error);
4563       g_simple_async_result_complete (res);
4564       g_error_free (error);
4565       load_contents_data_free (data);
4566       g_object_unref (res);
4567     }
4568 }
4569
4570 /**
4571  * g_file_load_partial_contents_async:
4572  * @file: input #GFile.
4573  * @cancellable: optional #GCancellable object, %NULL to ignore.
4574  * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
4575  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4576  * @user_data: the data to pass to the callback functions.
4577  *
4578  * Reads the partial contents of a file. A #GFileReadMoreCallback should be 
4579  * used to stop reading from the file when appropriate, else this function
4580  * will behave exactly as g_file_load_contents_async(). This operation 
4581  * can be finished by g_file_load_partial_contents_finish().
4582  *
4583  * Users of this function should be aware that @user_data is passed to 
4584  * both the @read_more_callback and the @callback.
4585  *
4586  * If @cancellable is not %NULL, then the operation can be cancelled by
4587  * triggering the cancellable object from another thread. If the operation
4588  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4589  **/
4590 void
4591 g_file_load_partial_contents_async (GFile                 *file,
4592                                     GCancellable          *cancellable,
4593                                     GFileReadMoreCallback  read_more_callback,
4594                                     GAsyncReadyCallback    callback,
4595                                     gpointer               user_data)
4596 {
4597   LoadContentsData *data;
4598
4599   g_return_if_fail (G_IS_FILE (file));
4600
4601   data = g_new0 (LoadContentsData, 1);
4602
4603   if (cancellable)
4604     data->cancellable = g_object_ref (cancellable);
4605   data->read_more_callback = read_more_callback;
4606   data->callback = callback;
4607   data->user_data = user_data;
4608   data->content = g_byte_array_new ();
4609   data->file = g_object_ref (file);
4610
4611   g_file_read_async (file,
4612                      0,
4613                      cancellable,
4614                      load_contents_open_callback,
4615                      data);
4616 }
4617
4618 /**
4619  * g_file_load_partial_contents_finish:
4620  * @file: input #GFile.
4621  * @res: a #GAsyncResult. 
4622  * @contents: a location to place the contents of the file.
4623  * @length: a location to place the length of the contents of the file.
4624  * @etag_out: a location to place the current entity tag for the file.
4625  * @error: a #GError, or %NULL
4626  * 
4627  * Finishes an asynchronous partial load operation that was started
4628  * with g_file_load_partial_contents_async().
4629  *
4630  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
4631  * present, it will be set appropriately. 
4632  **/
4633 gboolean
4634 g_file_load_partial_contents_finish (GFile         *file,
4635                                      GAsyncResult  *res,
4636                                      char         **contents,
4637                                      gsize         *length,
4638                                      char         **etag_out,
4639                                      GError       **error)
4640 {
4641   GSimpleAsyncResult *simple;
4642   LoadContentsData *data;
4643
4644   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4645   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4646   g_return_val_if_fail (contents != NULL, FALSE);
4647
4648   simple = G_SIMPLE_ASYNC_RESULT (res);
4649
4650   if (g_simple_async_result_propagate_error (simple, error))
4651     return FALSE;
4652   
4653   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4654   
4655   data = g_simple_async_result_get_op_res_gpointer (simple);
4656
4657   if (data->error)
4658     {
4659       g_propagate_error (error, data->error);
4660       data->error = NULL;
4661       *contents = NULL;
4662       if (length)
4663         *length = 0;
4664       return FALSE;
4665     }
4666
4667   if (length)
4668     *length = data->pos;
4669
4670   if (etag_out)
4671     {
4672       *etag_out = data->etag;
4673       data->etag = NULL;
4674     }
4675
4676   /* Zero terminate */
4677   g_byte_array_set_size (data->content, data->pos + 1);
4678   data->content->data[data->pos] = 0;
4679   
4680   *contents = (char *)g_byte_array_free (data->content, FALSE);
4681   data->content = NULL;
4682
4683   return TRUE;
4684 }
4685
4686 /**
4687  * g_file_load_contents_async:
4688  * @file: input #GFile.
4689  * @cancellable: optional #GCancellable object, %NULL to ignore.
4690  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4691  * @user_data: the data to pass to callback function
4692  * 
4693  * Starts an asynchronous load of the @file's contents.
4694  *
4695  * For more details, see g_file_load_contents() which is
4696  * the synchronous version of this call.
4697  *
4698  * When the load operation has completed, @callback will be called 
4699  * with @user data. To finish the operation, call 
4700  * g_file_load_contents_finish() with the #GAsyncResult returned by 
4701  * the @callback.
4702  * 
4703  * If @cancellable is not %NULL, then the operation can be cancelled by
4704  * triggering the cancellable object from another thread. If the operation
4705  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4706  **/
4707 void
4708 g_file_load_contents_async (GFile               *file,
4709                            GCancellable        *cancellable,
4710                            GAsyncReadyCallback  callback,
4711                            gpointer             user_data)
4712 {
4713   g_file_load_partial_contents_async (file,
4714                                       cancellable,
4715                                       NULL,
4716                                       callback, user_data);
4717 }
4718
4719 /**
4720  * g_file_load_contents_finish:
4721  * @file: input #GFile.
4722  * @res: a #GAsyncResult. 
4723  * @contents: a location to place the contents of the file.
4724  * @length: a location to place the length of the contents of the file.
4725  * @etag_out: a location to place the current entity tag for the file.
4726  * @error: a #GError, or %NULL
4727  * 
4728  * Finishes an asynchronous load of the @file's contents. 
4729  * The contents are placed in @contents, and @length is set to the 
4730  * size of the @contents string. If @etag_out is present, it will be 
4731  * set to the new entity tag for the @file.
4732  * 
4733  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
4734  * present, it will be set appropriately. 
4735  **/
4736 gboolean
4737 g_file_load_contents_finish (GFile         *file,
4738                              GAsyncResult  *res,
4739                              char         **contents,
4740                              gsize         *length,
4741                              char         **etag_out,
4742                              GError       **error)
4743 {
4744   return g_file_load_partial_contents_finish (file,
4745                                               res,
4746                                               contents,
4747                                               length,
4748                                               etag_out,
4749                                               error);
4750 }
4751   
4752 /**
4753  * g_file_replace_contents:
4754  * @file: input #GFile.
4755  * @contents: a string containing the new contents for @file.
4756  * @length: the length of @contents in bytes.
4757  * @etag: the old <link linkend="gfile-etag">entity tag</link> 
4758  *     for the document.
4759  * @make_backup: %TRUE if a backup should be created.
4760  * @flags: a set of #GFileCreateFlags.
4761  * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4762  *      for the document.
4763  * @cancellable: optional #GCancellable object, %NULL to ignore.
4764  * @error: a #GError, or %NULL
4765  *
4766  * Replaces the contents of @file with @contents of @length bytes.
4767  
4768  * If @etag is specified (not %NULL) any existing file must have that etag, or
4769  * the error %G_IO_ERROR_WRONG_ETAG will be returned.
4770  *
4771  * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
4772  * 
4773  * If @cancellable is not %NULL, then the operation can be cancelled by
4774  * triggering the cancellable object from another thread. If the operation
4775  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4776  *
4777  * The returned @new_etag can be used to verify that the file hasn't changed the
4778  * next time it is saved over.
4779  * 
4780  * Returns: %TRUE if successful. If an error
4781  * has occurred, this function will return %FALSE and set @error
4782  * appropriately if present.
4783  **/
4784 gboolean
4785 g_file_replace_contents (GFile             *file,
4786                          const char        *contents,
4787                          gsize              length,
4788                          const char        *etag,
4789                          gboolean           make_backup,
4790                          GFileCreateFlags   flags,
4791                          char             **new_etag,
4792                          GCancellable      *cancellable,
4793                          GError           **error)
4794 {
4795   GFileOutputStream *out;
4796   gsize pos, remainder;
4797   gssize res;
4798
4799   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4800   g_return_val_if_fail (contents != NULL, FALSE);
4801
4802   out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4803   if (out == NULL)
4804     return FALSE;
4805
4806   pos = 0;
4807   remainder = length;
4808   while (remainder > 0 &&
4809          (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4810                                        contents + pos,
4811                                        MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4812                                        cancellable,
4813                                        error)) > 0)
4814     {
4815       pos += res;
4816       remainder -= res;
4817     }
4818   
4819   if (remainder > 0 && res < 0)
4820     {
4821       /* Ignore errors on close */
4822       g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4823       
4824       /* error is set already */
4825       return FALSE;
4826     }
4827   
4828   if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4829     return FALSE;
4830
4831   if (new_etag)
4832     *new_etag = g_file_output_stream_get_etag (out);
4833   
4834   return TRUE;
4835 }
4836
4837 typedef struct {
4838   GFile *file;
4839   GError *error;
4840   GCancellable *cancellable;
4841   GAsyncReadyCallback callback;
4842   gpointer user_data;
4843   const char *content;
4844   gsize length;
4845   gsize pos;
4846   char *etag;
4847 } ReplaceContentsData;
4848
4849 static void
4850 replace_contents_data_free (ReplaceContentsData *data)
4851 {
4852   if (data->error)
4853     g_error_free (data->error);
4854   if (data->cancellable)
4855     g_object_unref (data->cancellable);
4856   g_object_unref (data->file);
4857   g_free (data->etag);
4858   g_free (data);
4859 }
4860
4861 static void
4862 replace_contents_close_callback (GObject      *obj,
4863                                  GAsyncResult *close_res,
4864                                  gpointer      user_data)
4865 {
4866   GOutputStream *stream = G_OUTPUT_STREAM (obj);
4867   ReplaceContentsData *data = user_data;
4868   GSimpleAsyncResult *res;
4869
4870   /* Ignore errors here, we're only reading anyway */
4871   g_output_stream_close_finish (stream, close_res, NULL);
4872   g_object_unref (stream);
4873
4874   data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4875   
4876   res = g_simple_async_result_new (G_OBJECT (data->file),
4877                                    data->callback,
4878                                    data->user_data,
4879                                    g_file_replace_contents_async);
4880   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4881   g_simple_async_result_complete (res);
4882   g_object_unref (res);
4883 }
4884
4885 static void
4886 replace_contents_write_callback (GObject      *obj,
4887                                  GAsyncResult *read_res,
4888                                  gpointer      user_data)
4889 {
4890   GOutputStream *stream = G_OUTPUT_STREAM (obj);
4891   ReplaceContentsData *data = user_data;
4892   GError *error = NULL;
4893   gssize write_size;
4894   
4895   write_size = g_output_stream_write_finish (stream, read_res, &error);
4896
4897   if (write_size <= 0) 
4898     {
4899       /* Error or EOF, close the file */
4900       if (write_size < 0)
4901         data->error = error;
4902       g_output_stream_close_async (stream, 0,
4903                                    data->cancellable,
4904                                    replace_contents_close_callback, data);
4905     }
4906   else if (write_size > 0)
4907     {
4908       data->pos += write_size;
4909
4910       if (data->pos >= data->length)
4911         g_output_stream_close_async (stream, 0,
4912                                      data->cancellable,
4913                                      replace_contents_close_callback, data);
4914       else
4915         g_output_stream_write_async (stream,
4916                                      data->content + data->pos,
4917                                      data->length - data->pos,
4918                                      0,
4919                                      data->cancellable,
4920                                      replace_contents_write_callback,
4921                                      data);
4922     }
4923 }
4924
4925 static void
4926 replace_contents_open_callback (GObject      *obj,
4927                                 GAsyncResult *open_res,
4928                                 gpointer      user_data)
4929 {
4930   GFile *file = G_FILE (obj);
4931   GFileOutputStream *stream;
4932   ReplaceContentsData *data = user_data;
4933   GError *error = NULL;
4934   GSimpleAsyncResult *res;
4935
4936   stream = g_file_replace_finish (file, open_res, &error);
4937
4938   if (stream)
4939     {
4940       g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4941                                    data->content + data->pos,
4942                                    data->length - data->pos,
4943                                    0,
4944                                    data->cancellable,
4945                                    replace_contents_write_callback,
4946                                    data);
4947       
4948     }
4949   else
4950     {
4951       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4952                                                   data->callback,
4953                                                   data->user_data,
4954                                                   error);
4955       g_simple_async_result_complete (res);
4956       g_error_free (error);
4957       replace_contents_data_free (data);
4958       g_object_unref (res);
4959     }
4960 }
4961
4962 /**
4963  * g_file_replace_contents_async:
4964  * @file: input #GFile.
4965  * @contents: string of contents to replace the file with.
4966  * @length: the length of @contents in bytes.
4967  * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4968  * @make_backup: %TRUE if a backup should be created.
4969  * @flags: a set of #GFileCreateFlags.
4970  * @cancellable: optional #GCancellable object, %NULL to ignore.
4971  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4972  * @user_data: the data to pass to callback function
4973  * 
4974  * Starts an asynchronous replacement of @file with the given 
4975  * @contents of @length bytes. @etag will replace the document's 
4976  * current entity tag.
4977  * 
4978  * When this operation has completed, @callback will be called with
4979  * @user_user data, and the operation can be finalized with 
4980  * g_file_replace_contents_finish().
4981  * 
4982  * If @cancellable is not %NULL, then the operation can be cancelled by
4983  * triggering the cancellable object from another thread. If the operation
4984  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4985  * 
4986  * If @make_backup is %TRUE, this function will attempt to 
4987  * make a backup of @file.
4988  **/
4989 void
4990 g_file_replace_contents_async  (GFile               *file,
4991                                 const char          *contents,
4992                                 gsize                length,
4993                                 const char          *etag,
4994                                 gboolean             make_backup,
4995                                 GFileCreateFlags     flags,
4996                                 GCancellable        *cancellable,
4997                                 GAsyncReadyCallback  callback,
4998                                 gpointer             user_data)
4999 {
5000   ReplaceContentsData *data;
5001
5002   g_return_if_fail (G_IS_FILE (file));
5003   g_return_if_fail (contents != NULL);
5004
5005   data = g_new0 (ReplaceContentsData, 1);
5006
5007   if (cancellable)
5008     data->cancellable = g_object_ref (cancellable);
5009   data->callback = callback;
5010   data->user_data = user_data;
5011   data->content = contents;
5012   data->length = length;
5013   data->pos = 0;
5014   data->file = g_object_ref (file);
5015
5016   g_file_replace_async (file,
5017                         etag,
5018                         make_backup,
5019                         flags,
5020                         0,
5021                         cancellable,
5022                         replace_contents_open_callback,
5023                         data);
5024 }
5025   
5026 /**
5027  * g_file_replace_contents_finish:
5028  * @file: input #GFile.
5029  * @res: a #GAsyncResult. 
5030  * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link> 
5031  *     for the document.
5032  * @error: a #GError, or %NULL 
5033  * 
5034  * Finishes an asynchronous replace of the given @file. See
5035  * g_file_replace_contents_async(). Sets @new_etag to the new entity 
5036  * tag for the document, if present.
5037  * 
5038  * Returns: %TRUE on success, %FALSE on failure.
5039  **/
5040 gboolean
5041 g_file_replace_contents_finish (GFile         *file,
5042                                 GAsyncResult  *res,
5043                                 char         **new_etag,
5044                                 GError       **error)
5045 {
5046   GSimpleAsyncResult *simple;
5047   ReplaceContentsData *data;
5048
5049   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5050   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5051
5052   simple = G_SIMPLE_ASYNC_RESULT (res);
5053
5054   if (g_simple_async_result_propagate_error (simple, error))
5055     return FALSE;
5056   
5057   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5058   
5059   data = g_simple_async_result_get_op_res_gpointer (simple);
5060
5061   if (data->error)
5062     {
5063       g_propagate_error (error, data->error);
5064       data->error = NULL;
5065       return FALSE;
5066     }
5067
5068
5069   if (new_etag)
5070     {
5071       *new_etag = data->etag;
5072       data->etag = NULL; /* Take ownership */
5073     }
5074   
5075   return TRUE;
5076 }
5077
5078 #define __G_FILE_C__
5079 #include "gioaliasdef.c"