Documentation updates
[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, creating 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 a 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 object that contains key-value attributes (such as 
884  * the type or size of 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 point 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 object 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  * g_file_find_enclosing_mount_async:
1113  * @file: a #GFile
1114  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1115  *     of the request.
1116  * @cancellable: optional #GCancellable object, %NULL to ignore.
1117  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1118  * @user_data: the data to pass to callback function
1119  *
1120  * Asynchronously gets the mount for the file.
1121  *
1122  * For more details, see g_file_find_enclosing_mount() which is
1123  * the synchronous version of this call.
1124  *
1125  * When the operation is finished, @callback will be called. You can then call
1126  * g_file_find_enclosing_mount_finish() to get the result of the operation.
1127  */
1128 void
1129 g_file_find_enclosing_mount_async (GFile              *file,
1130                                    int                   io_priority,
1131                                    GCancellable         *cancellable,
1132                                    GAsyncReadyCallback   callback,
1133                                    gpointer              user_data)
1134 {
1135   GFileIface *iface;
1136
1137   g_return_if_fail (G_IS_FILE (file));
1138
1139   iface = G_FILE_GET_IFACE (file);
1140   (* iface->find_enclosing_mount_async) (file,
1141                                          io_priority,
1142                                          cancellable,
1143                                          callback,
1144                                          user_data);
1145 }
1146
1147 /**
1148  * g_file_find_enclosing_mount_finish:
1149  * @file: a #GFile
1150  * @res: a #GAsyncResult
1151  * @error: a #GError
1152  * 
1153  * Finishes an asynchronous find mount request. 
1154  * See g_file_find_enclosing_mount_async().
1155  * 
1156  * Returns: #GMount for given @file or %NULL on error.
1157  **/
1158 GMount *
1159 g_file_find_enclosing_mount_finish (GFile         *file,
1160                                     GAsyncResult  *res,
1161                                     GError       **error)
1162 {
1163   GFileIface *iface;
1164   
1165   g_return_val_if_fail (G_IS_FILE (file), NULL);
1166   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1167
1168   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1169     {
1170       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1171       if (g_simple_async_result_propagate_error (simple, error))
1172         return NULL;
1173     }
1174   
1175   iface = G_FILE_GET_IFACE (file);
1176   return (* iface->find_enclosing_mount_finish) (file, res, error);
1177 }
1178
1179
1180 /**
1181  * g_file_read:
1182  * @file: #GFile to read.
1183  * @cancellable: a #GCancellable
1184  * @error: a #GError, or %NULL
1185  *
1186  * Opens a file for reading. The result is a #GFileInputStream that
1187  * can be used to read the contents of the file.
1188  *
1189  * If @cancellable is not %NULL, then the operation can be cancelled by
1190  * triggering the cancellable object from another thread. If the operation
1191  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1192  * 
1193  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1194  * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1195  * Other errors are possible too, and depend on what kind of filesystem the file is on.
1196  *
1197  * Returns: #GFileInputStream or %NULL on error.
1198  **/
1199 GFileInputStream *
1200 g_file_read (GFile         *file,
1201              GCancellable  *cancellable,
1202              GError       **error)
1203 {
1204   GFileIface *iface;
1205   
1206   g_return_val_if_fail (G_IS_FILE (file), NULL);
1207
1208   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1209     return NULL;
1210
1211   iface = G_FILE_GET_IFACE (file);
1212
1213   if (iface->read_fn == NULL)
1214     {
1215       g_set_error (error, G_IO_ERROR,
1216                    G_IO_ERROR_NOT_SUPPORTED,
1217                    _("Operation not supported"));
1218       return NULL;
1219     }
1220   
1221   return (* iface->read_fn) (file, cancellable, error);
1222 }
1223
1224 /**
1225  * g_file_append_to:
1226  * @file: input #GFile.
1227  * @flags: a set of #GFileCreateFlags.
1228  * @cancellable: optional #GCancellable object, %NULL to ignore.
1229  * @error: a #GError, or %NULL
1230  *
1231  * Gets an output stream for appending data to the file. If
1232  * the file doesn't already exist it is created.
1233  *
1234  * By default files created are generally readable by everyone,
1235  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1236  * will be made readable only to the current user, to the level that
1237  * is supported on the target filesystem.
1238  *
1239  * If @cancellable is not %NULL, then the operation can be cancelled by
1240  * triggering the cancellable object from another thread. If the operation
1241  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1242  *
1243  * Some file systems don't allow all file names, and may
1244  * return an G_IO_ERROR_INVALID_FILENAME error.
1245  * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1246  * returned. Other errors are possible too, and depend on what kind of
1247  * filesystem the file is on.
1248  * 
1249  * Returns: a #GFileOutputStream.
1250  **/
1251 GFileOutputStream *
1252 g_file_append_to (GFile             *file,
1253                   GFileCreateFlags   flags,
1254                   GCancellable      *cancellable,
1255                   GError           **error)
1256 {
1257   GFileIface *iface;
1258
1259   g_return_val_if_fail (G_IS_FILE (file), NULL);
1260
1261   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1262     return NULL;
1263   
1264   iface = G_FILE_GET_IFACE (file);
1265
1266   if (iface->append_to == NULL)
1267     {
1268       g_set_error (error, G_IO_ERROR,
1269                    G_IO_ERROR_NOT_SUPPORTED,
1270                    _("Operation not supported"));
1271       return NULL;
1272     }
1273   
1274   return (* iface->append_to) (file, flags, cancellable, error);
1275 }
1276
1277 /**
1278  * g_file_create:
1279  * @file: input #GFile.
1280  * @flags: a set of #GFileCreateFlags.
1281  * @cancellable: optional #GCancellable object, %NULL to ignore.
1282  * @error: a #GError, or %NULL
1283  *
1284  * Creates a new file and returns an output stream for writing to it.
1285  * The file must not already exists.
1286  *
1287  * By default files created are generally readable by everyone,
1288  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1289  * will be made readable only to the current user, to the level that
1290  * is supported on the target filesystem.
1291  *
1292  * If @cancellable is not %NULL, then the operation can be cancelled by
1293  * triggering the cancellable object from another thread. If the operation
1294  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1295  *
1296  * If a file with this name already exists the G_IO_ERROR_EXISTS error
1297  * will be returned. If the file is a directory the G_IO_ERROR_IS_DIRECTORY
1298  * error will be returned.
1299  * Some file systems don't allow all file names, and may
1300  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1301  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1302  * Other errors are possible too, and depend on what kind of
1303  * filesystem the file is on.
1304  * 
1305  * Returns: a #GFileOutputStream for the newly created file, or 
1306  * %NULL on error.
1307  **/
1308 GFileOutputStream *
1309 g_file_create (GFile             *file,
1310                GFileCreateFlags   flags,
1311                GCancellable      *cancellable,
1312                GError           **error)
1313 {
1314   GFileIface *iface;
1315   
1316   g_return_val_if_fail (G_IS_FILE (file), NULL);
1317
1318   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1319     return NULL;
1320   
1321   iface = G_FILE_GET_IFACE (file);
1322
1323   if (iface->create == NULL)
1324     {
1325       g_set_error (error, G_IO_ERROR,
1326                    G_IO_ERROR_NOT_SUPPORTED,
1327                    _("Operation not supported"));
1328       return NULL;
1329     }
1330   
1331   return (* iface->create) (file, flags, cancellable, error);
1332 }
1333
1334 /**
1335  * g_file_replace:
1336  * @file: input #GFile.
1337  * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the 
1338  *     current #GFile, or #NULL to ignore.
1339  * @make_backup: %TRUE if a backup should be created.
1340  * @flags: a set of #GFileCreateFlags.
1341  * @cancellable: optional #GCancellable object, %NULL to ignore.
1342  * @error: a #GError, or %NULL
1343  *
1344  * Returns an output stream for overwriting the file, possibly
1345  * creating a backup copy of the file first.
1346  *
1347  * This will try to replace the file in the safest way possible so
1348  * that any errors during the writing will not affect an already
1349  * existing copy of the file. For instance, for local files it
1350  * may write to a temporary file and then atomically rename over
1351  * the destination when the stream is closed.
1352  * 
1353  * By default files created are generally readable by everyone,
1354  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1355  * will be made readable only to the current user, to the level that
1356  * is supported on the target filesystem.
1357  *
1358  * If @cancellable is not %NULL, then the operation can be cancelled by
1359  * triggering the cancellable object from another thread. If the operation
1360  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1361  * 
1362  * If you pass in a non-#NULL @etag value, then this value is
1363  * compared to the current entity tag of the file, and if they differ
1364  * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1365  * that the file has been changed since you last read it. You can get
1366  * the new etag from g_file_output_stream_get_etag() after you've
1367  * finished writing and closed the #GFileOutputStream. When you load
1368  * a new file you can use g_file_input_stream_query_info() to get
1369  * the etag of the file.
1370  * 
1371  * If @make_backup is %TRUE, this function will attempt to make a backup
1372  * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1373  * error will be returned. If you want to replace anyway, try again with
1374  * @make_backup set to %FALSE.
1375  *
1376  * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1377  * and if the file is some other form of non-regular file then a
1378  * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1379  * Some file systems don't allow all file names, and may
1380  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1381  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1382  * Other errors are possible too, and depend on what kind of
1383  * filesystem the file is on.
1384  *
1385  * Returns: a #GFileOutputStream or %NULL on error. 
1386  **/
1387 GFileOutputStream *
1388 g_file_replace (GFile             *file,
1389                 const char        *etag,
1390                 gboolean           make_backup,
1391                 GFileCreateFlags   flags,
1392                 GCancellable      *cancellable,
1393                 GError           **error)
1394 {
1395   GFileIface *iface;
1396
1397   g_return_val_if_fail (G_IS_FILE (file), NULL);
1398
1399   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1400     return NULL;
1401   
1402   iface = G_FILE_GET_IFACE (file);
1403
1404   if (iface->replace == NULL)
1405     {
1406       g_set_error (error, G_IO_ERROR,
1407                    G_IO_ERROR_NOT_SUPPORTED,
1408                    _("Operation not supported"));
1409       return NULL;
1410     }
1411   
1412   
1413   /* Handle empty tag string as NULL in consistent way. */
1414   if (etag && *etag == 0)
1415     etag = NULL;
1416   
1417   return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1418 }
1419
1420 /**
1421  * g_file_read_async:
1422  * @file: input #GFile.
1423  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1424  *     of the request. 
1425  * @cancellable: optional #GCancellable object, %NULL to ignore.
1426  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1427  * @user_data: the data to pass to callback function
1428  *
1429  * Asynchronously opens @file for reading.
1430  *
1431  * For more details, see g_file_read() which is
1432  * the synchronous version of this call.
1433  *
1434  * When the operation is finished, @callback will be called. You can then call
1435  * g_file_read_finish() to get the result of the operation.
1436  **/
1437 void
1438 g_file_read_async (GFile               *file,
1439                    int                  io_priority,
1440                    GCancellable        *cancellable,
1441                    GAsyncReadyCallback  callback,
1442                    gpointer             user_data)
1443 {
1444   GFileIface *iface;
1445   
1446   g_return_if_fail (G_IS_FILE (file));
1447
1448   iface = G_FILE_GET_IFACE (file);
1449   (* iface->read_async) (file,
1450                          io_priority,
1451                          cancellable,
1452                          callback,
1453                          user_data);
1454 }
1455
1456 /**
1457  * g_file_read_finish:
1458  * @file: input #GFile.
1459  * @res: a #GAsyncResult. 
1460  * @error: a #GError, or %NULL
1461  *
1462  * Finishes an asynchronous file read operation started with 
1463  * g_file_read_async(). 
1464  *  
1465  * Returns: a #GFileInputStream or %NULL on error.
1466  **/
1467 GFileInputStream *
1468 g_file_read_finish (GFile         *file,
1469                     GAsyncResult  *res,
1470                     GError       **error)
1471 {
1472   GFileIface *iface;
1473   
1474   g_return_val_if_fail (G_IS_FILE (file), NULL);
1475   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1476
1477   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1478     {
1479       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1480       if (g_simple_async_result_propagate_error (simple, error))
1481         return NULL;
1482     }
1483   
1484   iface = G_FILE_GET_IFACE (file);
1485   return (* iface->read_finish) (file, res, error);
1486 }
1487
1488 /**
1489  * g_file_append_to_async:
1490  * @file: input #GFile.
1491  * @flags: a set of #GFileCreateFlags.
1492  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1493  *     of the request. 
1494  * @cancellable: optional #GCancellable object, %NULL to ignore.
1495  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1496  * @user_data: the data to pass to callback function
1497  * 
1498  * Asynchronously opens @file for appending.
1499  *
1500  * For more details, see g_file_append_to() which is
1501  * the synchronous version of this call.
1502  *
1503  * When the operation is finished, @callback will be called. You can then call
1504  * g_file_append_to_finish() to get the result of the operation.
1505  **/
1506 void
1507 g_file_append_to_async (GFile               *file,
1508                         GFileCreateFlags     flags,
1509                         int                  io_priority,
1510                         GCancellable        *cancellable,
1511                         GAsyncReadyCallback  callback,
1512                         gpointer             user_data)
1513 {
1514   GFileIface *iface;
1515   
1516   g_return_if_fail (G_IS_FILE (file));
1517
1518   iface = G_FILE_GET_IFACE (file);
1519   (* iface->append_to_async) (file,
1520                               flags,
1521                               io_priority,
1522                               cancellable,
1523                               callback,
1524                               user_data);
1525 }
1526
1527 /**
1528  * g_file_append_to_finish:
1529  * @file: input #GFile.
1530  * @res: #GAsyncResult
1531  * @error: a #GError, or %NULL
1532  * 
1533  * Finishes an asynchronous file append operation started with 
1534  * g_file_append_to_async(). 
1535  * 
1536  * Returns: a valid #GFileOutputStream or %NULL on error.
1537  **/
1538 GFileOutputStream *
1539 g_file_append_to_finish (GFile         *file,
1540                          GAsyncResult  *res,
1541                          GError       **error)
1542 {
1543   GFileIface *iface;
1544   
1545   g_return_val_if_fail (G_IS_FILE (file), NULL);
1546   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1547
1548   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1549     {
1550       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1551       if (g_simple_async_result_propagate_error (simple, error))
1552         return NULL;
1553     }
1554   
1555   iface = G_FILE_GET_IFACE (file);
1556   return (* iface->append_to_finish) (file, res, error);
1557 }
1558
1559 /**
1560  * g_file_create_async:
1561  * @file: input #GFile.
1562  * @flags: a set of #GFileCreateFlags.
1563  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1564  *     of the request.
1565  * @cancellable: optional #GCancellable object, %NULL to ignore.
1566  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1567  * @user_data: the data to pass to callback function
1568  * 
1569  * Asynchronously creates a new file and returns an output stream for writing to it.
1570  * The file must not already exists.
1571  *
1572  * For more details, see g_file_create() which is
1573  * the synchronous version of this call.
1574  *
1575  * When the operation is finished, @callback will be called. You can then call
1576  * g_file_create_finish() to get the result of the operation.
1577  **/
1578 void
1579 g_file_create_async (GFile               *file,
1580                      GFileCreateFlags     flags,
1581                      int                  io_priority,
1582                      GCancellable        *cancellable,
1583                      GAsyncReadyCallback  callback,
1584                      gpointer             user_data)
1585 {
1586   GFileIface *iface;
1587   
1588   g_return_if_fail (G_IS_FILE (file));
1589
1590   iface = G_FILE_GET_IFACE (file);
1591   (* iface->create_async) (file,
1592                            flags,
1593                            io_priority,
1594                            cancellable,
1595                            callback,
1596                            user_data);
1597 }
1598
1599 /**
1600  * g_file_create_finish:
1601  * @file: input #GFile.
1602  * @res: a #GAsyncResult. 
1603  * @error: a #GError, or %NULL
1604  * 
1605  * Finishes an asynchronous file create operation started with 
1606  * g_file_create_async(). 
1607  * 
1608  * Returns: a #GFileOutputStream or %NULL on error.
1609  **/
1610 GFileOutputStream *
1611 g_file_create_finish (GFile         *file,
1612                       GAsyncResult  *res,
1613                       GError       **error)
1614 {
1615   GFileIface *iface;
1616   
1617   g_return_val_if_fail (G_IS_FILE (file), NULL);
1618   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1619
1620   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1621     {
1622       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1623       if (g_simple_async_result_propagate_error (simple, error))
1624         return NULL;
1625     }
1626   
1627   iface = G_FILE_GET_IFACE (file);
1628   return (* iface->create_finish) (file, res, error);
1629 }
1630
1631 /**
1632  * g_file_replace_async:
1633  * @file: input #GFile.
1634  * @etag: an <link linkend="gfile-etag">entity tag</link> for the 
1635  *     current #GFile, or NULL to ignore.
1636  * @make_backup: %TRUE if a backup should be created.
1637  * @flags: a set of #GFileCreateFlags.
1638  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1639  *     of the request.
1640  * @cancellable: optional #GCancellable object, %NULL to ignore.
1641  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1642  * @user_data: the data to pass to callback function
1643  *
1644  * Asynchronously overwrites the file, replacing the contents, possibly
1645  * creating a backup copy of the file first.
1646  *
1647  * For more details, see g_file_replace() which is
1648  * the synchronous version of this call.
1649  *
1650  * When the operation is finished, @callback will be called. You can then call
1651  * g_file_replace_finish() to get the result of the operation.
1652  **/
1653 void
1654 g_file_replace_async (GFile               *file,
1655                       const char          *etag,
1656                       gboolean             make_backup,
1657                       GFileCreateFlags     flags,
1658                       int                  io_priority,
1659                       GCancellable        *cancellable,
1660                       GAsyncReadyCallback  callback,
1661                       gpointer             user_data)
1662 {
1663   GFileIface *iface;
1664   
1665   g_return_if_fail (G_IS_FILE (file));
1666
1667   iface = G_FILE_GET_IFACE (file);
1668   (* iface->replace_async) (file,
1669                             etag,
1670                             make_backup,
1671                             flags,
1672                             io_priority,
1673                             cancellable,
1674                             callback,
1675                             user_data);
1676 }
1677
1678 /**
1679  * g_file_replace_finish:
1680  * @file: input #GFile.
1681  * @res: a #GAsyncResult. 
1682  * @error: a #GError, or %NULL
1683  * 
1684  * Finishes an asynchronous file replace operation started with 
1685  * g_file_replace_async(). 
1686  * 
1687  * Returns: a #GFileOutputStream, or %NULL on error.
1688  **/
1689 GFileOutputStream *
1690 g_file_replace_finish (GFile         *file,
1691                        GAsyncResult  *res,
1692                        GError       **error)
1693 {
1694   GFileIface *iface;
1695   
1696   g_return_val_if_fail (G_IS_FILE (file), NULL);
1697   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1698
1699   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1700     {
1701       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1702       if (g_simple_async_result_propagate_error (simple, error))
1703         return NULL;
1704     }
1705   
1706   iface = G_FILE_GET_IFACE (file);
1707   return (* iface->replace_finish) (file, res, error);
1708 }
1709
1710 static gboolean
1711 copy_symlink (GFile           *destination,
1712               GFileCopyFlags   flags,
1713               GCancellable    *cancellable,
1714               const char      *target,
1715               GError         **error)
1716 {
1717   GError *my_error;
1718   gboolean tried_delete;
1719   GFileInfo *info;
1720   GFileType file_type;
1721
1722   tried_delete = FALSE;
1723
1724  retry:
1725   my_error = NULL;
1726   if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1727     {
1728       /* Maybe it already existed, and we want to overwrite? */
1729       if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) && 
1730           my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1731         {
1732           g_error_free (my_error);
1733
1734
1735           /* Don't overwrite if the destination is a directory */
1736           info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1737                                     G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1738                                     cancellable, &my_error);
1739           if (info != NULL)
1740             {
1741               file_type = g_file_info_get_file_type (info);
1742               g_object_unref (info);
1743               
1744               if (file_type == G_FILE_TYPE_DIRECTORY)
1745                 {
1746                   g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1747                                _("Can't copy over directory"));
1748                   return FALSE;
1749                 }
1750             }
1751           
1752           if (!g_file_delete (destination, cancellable, error))
1753             return FALSE;
1754           
1755           tried_delete = TRUE;
1756           goto retry;
1757         }
1758             /* Nah, fail */
1759       g_propagate_error (error, my_error);
1760       return FALSE;
1761     }
1762
1763   return TRUE;
1764 }
1765
1766 static GInputStream *
1767 open_source_for_copy (GFile           *source,
1768                       GFile           *destination,
1769                       GFileCopyFlags   flags,
1770                       GCancellable    *cancellable,
1771                       GError         **error)
1772 {
1773   GError *my_error;
1774   GInputStream *in;
1775   GFileInfo *info;
1776   GFileType file_type;
1777   
1778   my_error = NULL;
1779   in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1780   if (in != NULL)
1781     return in;
1782
1783   /* There was an error opening the source, try to set a good error for it: */
1784
1785   if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1786     {
1787       /* The source is a directory, don't fail with WOULD_RECURSE immediately, 
1788        * as that is less useful to the app. Better check for errors on the 
1789        * target instead. 
1790        */
1791       g_error_free (my_error);
1792       my_error = NULL;
1793       
1794       info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1795                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1796                                 cancellable, &my_error);
1797       if (info != NULL)
1798         {
1799           file_type = g_file_info_get_file_type (info);
1800           g_object_unref (info);
1801           
1802           if (flags & G_FILE_COPY_OVERWRITE)
1803             {
1804               if (file_type == G_FILE_TYPE_DIRECTORY)
1805                 {
1806                   g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1807                                _("Can't copy directory over directory"));
1808                   return NULL;
1809                 }
1810               /* continue to would_recurse error */
1811             }
1812           else
1813             {
1814               g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1815                            _("Target file exists"));
1816               return NULL;
1817             }
1818         }
1819       else
1820         {
1821           /* Error getting info from target, return that error 
1822            * (except for NOT_FOUND, which is no error here) 
1823            */
1824           if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1825             {
1826               g_propagate_error (error, my_error);
1827               return NULL;
1828             }
1829           g_error_free (my_error);
1830         }
1831       
1832       g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1833                    _("Can't recursively copy directory"));
1834       return NULL;
1835     }
1836
1837   g_propagate_error (error, my_error);
1838   return NULL;
1839 }
1840
1841 static gboolean
1842 should_copy (GFileAttributeInfo *info, 
1843              gboolean            as_move)
1844 {
1845   if (as_move)
1846     return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1847   return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1848 }
1849
1850 static char *
1851 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1852                                GFileAttributeInfoList *namespaces,
1853                                gboolean                as_move)
1854 {
1855   GString *s;
1856   gboolean first;
1857   int i;
1858   
1859   first = TRUE;
1860   s = g_string_new ("");
1861
1862   if (attributes)
1863     {
1864       for (i = 0; i < attributes->n_infos; i++)
1865         {
1866           if (should_copy (&attributes->infos[i], as_move))
1867             {
1868               if (first)
1869                 first = FALSE;
1870               else
1871                 g_string_append_c (s, ',');
1872                 
1873               g_string_append (s, attributes->infos[i].name);
1874             }
1875         }
1876     }
1877
1878   if (namespaces)
1879     {
1880       for (i = 0; i < namespaces->n_infos; i++)
1881         {
1882           if (should_copy (&namespaces->infos[i], as_move))
1883             {
1884               if (first)
1885                 first = FALSE;
1886               else
1887                 g_string_append_c (s, ',');
1888                 
1889               g_string_append (s, namespaces->infos[i].name);
1890               g_string_append (s, ":*");
1891             }
1892         }
1893     }
1894
1895   return g_string_free (s, FALSE);
1896 }
1897
1898 /**
1899  * g_file_copy_attributes:
1900  * @source: a #GFile with attributes.
1901  * @destination: a #GFile to copy attributes to.
1902  * @flags: a set of #GFileCopyFlags.
1903  * @cancellable: optional #GCancellable object, %NULL to ignore.
1904  * @error: a #GError, %NULL to ignore.
1905  *
1906  * Copies the file attributes from @source to @destination. 
1907  *
1908  * Normally only a subset of the file attributes are copied,
1909  * those that are copies in a normal file copy operation
1910  * (which for instance does not include e.g. mtime). However
1911  * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
1912  * all the metadata that is possible to copy is copied.
1913  *
1914  * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1915  **/
1916 gboolean
1917 g_file_copy_attributes (GFile           *source,
1918                         GFile           *destination,
1919                         GFileCopyFlags   flags,
1920                         GCancellable    *cancellable,
1921                         GError         **error)
1922 {
1923   GFileAttributeInfoList *attributes, *namespaces;
1924   char *attrs_to_read;
1925   gboolean res;
1926   GFileInfo *info;
1927   gboolean as_move;
1928   gboolean source_nofollow_symlinks;
1929
1930   as_move = flags & G_FILE_COPY_ALL_METADATA;
1931   source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1932
1933   /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1934   attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1935   namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1936
1937   if (attributes == NULL && namespaces == NULL)
1938     return TRUE;
1939
1940   attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1941
1942   /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1943    * we just don't copy it. 
1944    */
1945   info = g_file_query_info (source, attrs_to_read,
1946                             source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1947                             cancellable,
1948                             NULL);
1949
1950   g_free (attrs_to_read);
1951   
1952   res = TRUE;
1953   if  (info)
1954     {
1955       res = g_file_set_attributes_from_info (destination,
1956                                              info, 0,
1957                                              cancellable,
1958                                              error);
1959       g_object_unref (info);
1960     }
1961   
1962   g_file_attribute_info_list_unref (attributes);
1963   g_file_attribute_info_list_unref (namespaces);
1964   
1965   return res;
1966 }
1967
1968 /* Closes the streams */
1969 static gboolean
1970 copy_stream_with_progress (GInputStream           *in,
1971                            GOutputStream          *out,
1972                            GCancellable           *cancellable,
1973                            GFileProgressCallback   progress_callback,
1974                            gpointer                progress_callback_data,
1975                            GError                **error)
1976 {
1977   gssize n_read, n_written;
1978   goffset current_size;
1979   char buffer[8192], *p;
1980   gboolean res;
1981   goffset total_size;
1982   GFileInfo *info;
1983
1984   total_size = 0;
1985   info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1986                                          G_FILE_ATTRIBUTE_STANDARD_SIZE,
1987                                          cancellable, NULL);
1988   if (info)
1989     {
1990       total_size = g_file_info_get_size (info);
1991       g_object_unref (info);
1992     }
1993   
1994   current_size = 0;
1995   res = TRUE;
1996   while (TRUE)
1997     {
1998       n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
1999       if (n_read == -1)
2000         {
2001           res = FALSE;
2002           break;
2003         }
2004         
2005       if (n_read == 0)
2006         break;
2007
2008       current_size += n_read;
2009
2010       p = buffer;
2011       while (n_read > 0)
2012         {
2013           n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2014           if (n_written == -1)
2015             {
2016               res = FALSE;
2017               break;
2018             }
2019
2020           p += n_written;
2021           n_read -= n_written;
2022         }
2023
2024       if (!res)
2025         break;
2026       
2027       if (progress_callback)
2028         progress_callback (current_size, total_size, progress_callback_data);
2029     }
2030
2031   if (!res)
2032     error = NULL; /* Ignore further errors */
2033
2034   /* Make sure we send full copied size */
2035   if (progress_callback)
2036     progress_callback (current_size, total_size, progress_callback_data);
2037
2038   
2039   /* Don't care about errors in source here */
2040   g_input_stream_close (in, cancellable, NULL);
2041
2042   /* But write errors on close are bad! */
2043   if (!g_output_stream_close (out, cancellable, error))
2044     res = FALSE;
2045
2046   g_object_unref (in);
2047   g_object_unref (out);
2048       
2049   return res;
2050 }
2051
2052 static gboolean
2053 file_copy_fallback (GFile                  *source,
2054                     GFile                  *destination,
2055                     GFileCopyFlags          flags,
2056                     GCancellable           *cancellable,
2057                     GFileProgressCallback   progress_callback,
2058                     gpointer                progress_callback_data,
2059                     GError                **error)
2060 {
2061   GInputStream *in;
2062   GOutputStream *out;
2063   GFileInfo *info;
2064   const char *target;
2065
2066   /* Maybe copy the symlink? */
2067   if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2068     {
2069       info = g_file_query_info (source,
2070                                 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2071                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2072                                 cancellable,
2073                                 error);
2074       if (info == NULL)
2075         return FALSE;
2076
2077       if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2078           (target = g_file_info_get_symlink_target (info)) != NULL)
2079         {
2080           if (!copy_symlink (destination, flags, cancellable, target, error))
2081             {
2082               g_object_unref (info);
2083               return FALSE;
2084             }
2085           
2086           g_object_unref (info);
2087           goto copied_file;
2088         }
2089       
2090       g_object_unref (info);
2091     }
2092   
2093   in = open_source_for_copy (source, destination, flags, cancellable, error);
2094   if (in == NULL)
2095     return FALSE;
2096   
2097   if (flags & G_FILE_COPY_OVERWRITE)
2098     {
2099       out = (GOutputStream *)g_file_replace (destination,
2100                                              NULL, 0,
2101                                              flags & G_FILE_COPY_BACKUP,
2102                                              cancellable, error);
2103     }
2104   else
2105     {
2106       out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2107     }
2108
2109   if (out == NULL)
2110     {
2111       g_object_unref (in);
2112       return FALSE;
2113     }
2114
2115   if (!copy_stream_with_progress (in, out, cancellable,
2116                                   progress_callback, progress_callback_data,
2117                                   error))
2118     return FALSE;
2119
2120  copied_file:
2121
2122   /* Ignore errors here. Failure to copy metadata is not a hard error */
2123   g_file_copy_attributes (source, destination,
2124                           flags, cancellable, NULL);
2125   
2126   return TRUE;
2127 }
2128
2129 /**
2130  * g_file_copy:
2131  * @source: input #GFile.
2132  * @destination: destination #GFile
2133  * @flags: set of #GFileCopyFlags
2134  * @cancellable: optional #GCancellable object, %NULL to ignore.
2135  * @progress_callback: function to callback with progress information
2136  * @progress_callback_data: user data to pass to @progress_callback
2137  * @error: #GError to set on error, or %NULL
2138  *
2139  * Copies the file @source to the location specified by @destination.
2140  * Can not handle recursive copies of directories.
2141  *
2142  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2143  * existing @destination file is overwritten.
2144  *
2145  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2146  * will be copied as symlinks, otherwise the target of the
2147  * @source symlink will be copied.
2148  *
2149  * If @cancellable is not %NULL, then the operation can be cancelled by
2150  * triggering the cancellable object from another thread. If the operation
2151  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2152  * 
2153  * If @progress_callback is not %NULL, then the operation can be monitored by
2154  * setting this to a #GFileProgressCallback function. @progress_callback_data
2155  * will be passed to this function. It is guaranteed that this callback will
2156  * be called after all data has been transferred with the total number of bytes
2157  * copied during the operation.
2158  * 
2159  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2160  * error is returned, independent on the status of the @destination.
2161  *
2162  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2163  * error G_IO_ERROR_EXISTS is returned.
2164  *
2165  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2166  * error is returned. If trying to overwrite a directory with a directory the
2167  * G_IO_ERROR_WOULD_MERGE error is returned.
2168  *
2169  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2170  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2171  * is returned.
2172  *
2173  * If you are interested in copying the #GFile object itself (not the on-disk
2174  * file), see g_file_dup().
2175  *
2176  * Returns: %TRUE on success, %FALSE otherwise.
2177  **/
2178 gboolean
2179 g_file_copy (GFile                  *source,
2180              GFile                  *destination,
2181              GFileCopyFlags          flags,
2182              GCancellable           *cancellable,
2183              GFileProgressCallback   progress_callback,
2184              gpointer                progress_callback_data,
2185              GError                **error)
2186 {
2187   GFileIface *iface;
2188   GError *my_error;
2189   gboolean res;
2190
2191   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2192   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2193
2194   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2195     return FALSE;
2196   
2197   iface = G_FILE_GET_IFACE (destination);
2198   if (iface->copy)
2199     {
2200       my_error = NULL;
2201       res = (* iface->copy) (source, destination,
2202                              flags, cancellable,
2203                              progress_callback, progress_callback_data,
2204                              &my_error);
2205       
2206       if (res)
2207         return TRUE;
2208       
2209       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2210         {
2211           g_propagate_error (error, my_error);
2212               return FALSE;
2213         }
2214     }
2215
2216   /* If the types are different, and the destination method failed
2217      also try the source method */
2218   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2219     {
2220       iface = G_FILE_GET_IFACE (source);
2221       
2222       if (iface->copy)
2223         {
2224           my_error = NULL;
2225           res = (* iface->copy) (source, destination,
2226                                  flags, cancellable,
2227                                  progress_callback, progress_callback_data,
2228                                  &my_error);
2229           
2230           if (res)
2231             return TRUE;
2232           
2233           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2234             {
2235               g_propagate_error (error, my_error);
2236               return FALSE;
2237             }
2238         }
2239     }
2240   
2241   return file_copy_fallback (source, destination, flags, cancellable,
2242                              progress_callback, progress_callback_data,
2243                              error);
2244 }
2245
2246
2247 /**
2248  * g_file_move:
2249  * @source: #GFile pointing to the source location.
2250  * @destination: #GFile pointing to the destination location.
2251  * @flags: set of #GFileCopyFlags.
2252  * @cancellable: optional #GCancellable object, %NULL to ignore.
2253  * @progress_callback: #GFileProgressCallback function for updates.
2254  * @progress_callback_data: gpointer to user data for the callback function.
2255  * @error: #GError for returning error conditions, or %NULL
2256  *
2257  *
2258  * Tries to move the file or directory @source to the location specified by @destination.
2259  * If native move operations are supported then this is used, otherwise a copy + delete
2260  * fallback is used. The native implementation may support moving directories (for instance
2261  * on moves inside the same filesystem), but the fallback code does not.
2262  * 
2263  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2264  * existing @destination file is overwritten.
2265  *
2266  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2267  * will be copied as symlinks, otherwise the target of the
2268  * @source symlink will be copied.
2269  *
2270  * If @cancellable is not %NULL, then the operation can be cancelled by
2271  * triggering the cancellable object from another thread. If the operation
2272  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2273  * 
2274  * If @progress_callback is not %NULL, then the operation can be monitored by
2275  * setting this to a #GFileProgressCallback function. @progress_callback_data
2276  * will be passed to this function. It is guaranteed that this callback will
2277  * be called after all data has been transferred with the total number of bytes
2278  * copied during the operation.
2279  * 
2280  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2281  * error is returned, independent on the status of the @destination.
2282  *
2283  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2284  * error G_IO_ERROR_EXISTS is returned.
2285  *
2286  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2287  * error is returned. If trying to overwrite a directory with a directory the
2288  * G_IO_ERROR_WOULD_MERGE error is returned.
2289  *
2290  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2291  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2292  * may be returned (if the native move operation isn't available).
2293  *
2294  * Returns: %TRUE on successful move, %FALSE otherwise.
2295  **/
2296 gboolean
2297 g_file_move (GFile                  *source,
2298              GFile                  *destination,
2299              GFileCopyFlags          flags,
2300              GCancellable           *cancellable,
2301              GFileProgressCallback   progress_callback,
2302              gpointer                progress_callback_data,
2303              GError                **error)
2304 {
2305   GFileIface *iface;
2306   GError *my_error;
2307   gboolean res;
2308
2309   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2310   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2311
2312   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2313     return FALSE;
2314   
2315   iface = G_FILE_GET_IFACE (destination);
2316   if (iface->move)
2317     {
2318       my_error = NULL;
2319       res = (* iface->move) (source, destination,
2320                              flags, cancellable,
2321                              progress_callback, progress_callback_data,
2322                              &my_error);
2323       
2324       if (res)
2325         return TRUE;
2326       
2327       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2328         {
2329           g_propagate_error (error, my_error);
2330           return FALSE;
2331         }
2332     }
2333
2334   /* If the types are different, and the destination method failed
2335      also try the source method */
2336   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2337     {
2338       iface = G_FILE_GET_IFACE (source);
2339       
2340       if (iface->move)
2341         {
2342           my_error = NULL;
2343           res = (* iface->move) (source, destination,
2344                                  flags, cancellable,
2345                                  progress_callback, progress_callback_data,
2346                                  &my_error);
2347           
2348           if (res)
2349             return TRUE;
2350           
2351           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2352             {
2353               g_propagate_error (error, my_error);
2354               return FALSE;
2355             }
2356         }
2357     }
2358   
2359   if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2360     {  
2361       g_set_error (error, G_IO_ERROR,
2362                    G_IO_ERROR_NOT_SUPPORTED,
2363                    _("Operation not supported"));
2364       return FALSE;
2365     }
2366   
2367   flags |= G_FILE_COPY_ALL_METADATA;
2368   if (!g_file_copy (source, destination, flags, cancellable,
2369                     progress_callback, progress_callback_data,
2370                     error))
2371     return FALSE;
2372
2373   return g_file_delete (source, cancellable, error);
2374 }
2375
2376 /**
2377  * g_file_make_directory
2378  * @file: input #GFile.
2379  * @cancellable: optional #GCancellable object, %NULL to ignore.
2380  * @error: a #GError, or %NULL 
2381  *
2382  * Creates a directory.
2383  * 
2384  * If @cancellable is not %NULL, then the operation can be cancelled by
2385  * triggering the cancellable object from another thread. If the operation
2386  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2387  * 
2388  * Returns: %TRUE on successful creation, %FALSE otherwise.
2389  **/
2390 gboolean
2391 g_file_make_directory (GFile         *file,
2392                        GCancellable  *cancellable,
2393                        GError       **error)
2394 {
2395   GFileIface *iface;
2396
2397   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2398
2399   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2400     return FALSE;
2401   
2402   iface = G_FILE_GET_IFACE (file);
2403
2404   if (iface->make_directory == NULL)
2405     {
2406       g_set_error (error, G_IO_ERROR,
2407                    G_IO_ERROR_NOT_SUPPORTED,
2408                    _("Operation not supported"));
2409       return FALSE;
2410     }
2411   
2412   return (* iface->make_directory) (file, cancellable, error);
2413 }
2414
2415 /**
2416  * g_file_make_symbolic_link:
2417  * @file: input #GFile.
2418  * @symlink_value: a string with the value of the new symlink.
2419  * @cancellable: optional #GCancellable object, %NULL to ignore.
2420  * @error: a #GError. 
2421  * 
2422  * Creates a symbolic link.
2423  *
2424  * If @cancellable is not %NULL, then the operation can be cancelled by
2425  * triggering the cancellable object from another thread. If the operation
2426  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2427  * 
2428  * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2429  **/
2430 gboolean
2431 g_file_make_symbolic_link (GFile         *file,
2432                            const char    *symlink_value,
2433                            GCancellable  *cancellable,
2434                            GError       **error)
2435 {
2436   GFileIface *iface;
2437
2438   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2439   g_return_val_if_fail (symlink_value != NULL, FALSE);
2440
2441   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2442     return FALSE;
2443
2444   if (*symlink_value == '\0')
2445     {
2446       g_set_error (error, G_IO_ERROR,
2447                    G_IO_ERROR_INVALID_ARGUMENT,
2448                    _("Invalid symlink value given"));
2449       return FALSE;
2450     }
2451   
2452   iface = G_FILE_GET_IFACE (file);
2453
2454   if (iface->make_symbolic_link == NULL)
2455     {
2456       g_set_error (error, G_IO_ERROR,
2457                    G_IO_ERROR_NOT_SUPPORTED,
2458                    _("Operation not supported"));
2459       return FALSE;
2460     }
2461   
2462   return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2463 }
2464
2465 /**
2466  * g_file_delete:
2467  * @file: input #GFile.
2468  * @cancellable: optional #GCancellable object, %NULL to ignore.
2469  * @error: a #GError, or %NULL 
2470  * 
2471  * Deletes a file.
2472  * 
2473  * If @cancellable is not %NULL, then the operation can be cancelled by
2474  * triggering the cancellable object from another thread. If the operation
2475  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2476  * 
2477  * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2478  **/
2479 gboolean
2480 g_file_delete (GFile         *file,
2481                GCancellable  *cancellable,
2482                GError       **error)
2483 {
2484   GFileIface *iface;
2485   
2486   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2487
2488   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2489     return FALSE;
2490   
2491   iface = G_FILE_GET_IFACE (file);
2492
2493   if (iface->delete_file == NULL)
2494     {
2495       g_set_error (error, G_IO_ERROR,
2496                    G_IO_ERROR_NOT_SUPPORTED,
2497                    _("Operation not supported"));
2498       return FALSE;
2499     }
2500   
2501   return (* iface->delete_file) (file, cancellable, error);
2502 }
2503
2504 /**
2505  * g_file_trash:
2506  * @file: #GFile to send to trash.
2507  * @cancellable: optional #GCancellable object, %NULL to ignore.
2508  * @error: a #GError, or %NULL
2509  *
2510  * Sends @file to the "Trashcan", if possible. This is similar to
2511  * deleting it, but the user can recover it before emptying the trashcan.
2512  * Not all file systems support trashing, so this call can return the
2513  * %G_IO_ERROR_NOT_SUPPORTED error.
2514  *
2515  *
2516  * If @cancellable is not %NULL, then the operation can be cancelled by
2517  * triggering the cancellable object from another thread. If the operation
2518  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2519  * 
2520  * Returns: %TRUE on successful trash, %FALSE otherwise.
2521  **/
2522 gboolean
2523 g_file_trash (GFile         *file,
2524               GCancellable  *cancellable,
2525               GError       **error)
2526 {
2527   GFileIface *iface;
2528   
2529   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2530
2531   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2532     return FALSE;
2533   
2534   iface = G_FILE_GET_IFACE (file);
2535
2536   if (iface->trash == NULL)
2537     {
2538       g_set_error (error,
2539                    G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2540                    _("Trash not supported"));
2541       return FALSE;
2542     }
2543   
2544   return (* iface->trash) (file, cancellable, error);
2545 }
2546
2547 /**
2548  * g_file_set_display_name:
2549  * @file: input #GFile.
2550  * @display_name: a string.
2551  * @cancellable: optional #GCancellable object, %NULL to ignore.
2552  * @error: a #GError, or %NULL
2553  * 
2554  * Renames @file to the specified display name.
2555  *
2556  * The display name is converted from UTF8 to the correct encoding for the target
2557  * filesystem if possible and the @file is renamed to this.
2558  * 
2559  * If you want to implement a rename operation in the user interface the edit name
2560  * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2561  * widget, and then the result after editing should be passed to g_file_set_display_name().
2562  *
2563  * On success the resulting converted filename is returned.
2564  * 
2565  * If @cancellable is not %NULL, then the operation can be cancelled by
2566  * triggering the cancellable object from another thread. If the operation
2567  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2568  * 
2569  * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2570  **/
2571 GFile *
2572 g_file_set_display_name (GFile         *file,
2573                          const char    *display_name,
2574                          GCancellable  *cancellable,
2575                          GError       **error)
2576 {
2577   GFileIface *iface;
2578   
2579   g_return_val_if_fail (G_IS_FILE (file), NULL);
2580   g_return_val_if_fail (display_name != NULL, NULL);
2581
2582   if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2583     {
2584       g_set_error (error,
2585                    G_IO_ERROR,
2586                    G_IO_ERROR_INVALID_ARGUMENT,
2587                    _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2588       return NULL;
2589     }
2590   
2591   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2592     return NULL;
2593   
2594   iface = G_FILE_GET_IFACE (file);
2595
2596   return (* iface->set_display_name) (file, display_name, cancellable, error);
2597 }
2598
2599 /**
2600  * g_file_set_display_name_async:
2601  * @file: input #GFile.
2602  * @display_name: a string.
2603  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2604  *     of the request. 
2605  * @cancellable: optional #GCancellable object, %NULL to ignore.
2606  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2607  * @user_data: the data to pass to callback function
2608  * 
2609  * Asynchronously sets the display name for a given #GFile.
2610  * 
2611  * For more details, see g_set_display_name() which is
2612  * the synchronous version of this call.
2613  *
2614  * When the operation is finished, @callback will be called. You can then call
2615  * g_file_set_display_name_finish() to get the result of the operation.
2616  **/
2617 void
2618 g_file_set_display_name_async (GFile               *file,
2619                                const char          *display_name,
2620                                int                  io_priority,
2621                                GCancellable        *cancellable,
2622                                GAsyncReadyCallback  callback,
2623                                gpointer             user_data)
2624 {
2625   GFileIface *iface;
2626   
2627   g_return_if_fail (G_IS_FILE (file));
2628   g_return_if_fail (display_name != NULL);
2629
2630   iface = G_FILE_GET_IFACE (file);
2631   (* iface->set_display_name_async) (file,
2632                                      display_name,
2633                                      io_priority,
2634                                      cancellable,
2635                                      callback,
2636                                      user_data);
2637 }
2638
2639 /**
2640  * g_file_set_display_name_finish:
2641  * @file: input #GFile.
2642  * @res: a #GAsyncResult. 
2643  * @error: a #GError, or %NULL
2644  * 
2645  * Finishes setting a display name started with 
2646  * g_file_set_display_name_async().
2647  * 
2648  * Returns: a #GFile or %NULL on error.
2649  **/
2650 GFile *
2651 g_file_set_display_name_finish (GFile         *file,
2652                                 GAsyncResult  *res,
2653                                 GError       **error)
2654 {
2655   GFileIface *iface;
2656   
2657   g_return_val_if_fail (G_IS_FILE (file), NULL);
2658   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2659
2660   if (G_IS_SIMPLE_ASYNC_RESULT (res))
2661     {
2662       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2663       if (g_simple_async_result_propagate_error (simple, error))
2664         return NULL;
2665     }
2666   
2667   iface = G_FILE_GET_IFACE (file);
2668   return (* iface->set_display_name_finish) (file, res, error);
2669 }
2670
2671 /**
2672  * g_file_query_settable_attributes:
2673  * @file: input #GFile.
2674  * @cancellable: optional #GCancellable object, %NULL to ignore.
2675  * @error: a #GError, or %NULL
2676  * 
2677  * Obtain the list of settable attributes for the file.
2678  *
2679  * Returns the type and full attribute name of all the attributes 
2680  * that can be set on this file. This doesn't mean setting it will always 
2681  * succeed though, you might get an access failure, or some specific 
2682  * file may not support a specific attribute.
2683  *
2684  * If @cancellable is not %NULL, then the operation can be cancelled by
2685  * triggering the cancellable object from another thread. If the operation
2686  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2687  * 
2688  * Returns: a #GFileAttributeInfoList describing the settable attributes.
2689  * When you are done with it, release it with g_file_attribute_info_list_unref()
2690  **/
2691 GFileAttributeInfoList *
2692 g_file_query_settable_attributes (GFile         *file,
2693                                   GCancellable  *cancellable,
2694                                   GError       **error)
2695 {
2696   GFileIface *iface;
2697   GError *my_error;
2698   GFileAttributeInfoList *list;
2699
2700   g_return_val_if_fail (G_IS_FILE (file), NULL);
2701
2702   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2703     return NULL;
2704   
2705   iface = G_FILE_GET_IFACE (file);
2706
2707   if (iface->query_settable_attributes == NULL)
2708     return g_file_attribute_info_list_new ();
2709
2710   my_error = NULL;
2711   list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2712   
2713   if (list == NULL)
2714     {
2715       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2716         {
2717           list = g_file_attribute_info_list_new ();
2718           g_error_free (my_error);
2719         }
2720       else
2721         g_propagate_error (error, my_error);
2722     }
2723   
2724   return list;
2725 }
2726
2727 /**
2728  * g_file_query_writable_namespaces:
2729  * @file: input #GFile.
2730  * @cancellable: optional #GCancellable object, %NULL to ignore.
2731  * @error: a #GError, or %NULL
2732  * 
2733  * Obtain the list of attribute namespaces where new attributes 
2734  * can be created by a user. An example of this is extended
2735  * attributes (in the "xattr" namespace).
2736  *
2737  * If @cancellable is not %NULL, then the operation can be cancelled by
2738  * triggering the cancellable object from another thread. If the operation
2739  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2740  * 
2741  * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2742  * When you are done with it, release it with g_file_attribute_info_list_unref()
2743  **/
2744 GFileAttributeInfoList *
2745 g_file_query_writable_namespaces (GFile         *file,
2746                                   GCancellable  *cancellable,
2747                                   GError       **error)
2748 {
2749   GFileIface *iface;
2750   GError *my_error;
2751   GFileAttributeInfoList *list;
2752   
2753   g_return_val_if_fail (G_IS_FILE (file), NULL);
2754
2755   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2756     return NULL;
2757   
2758   iface = G_FILE_GET_IFACE (file);
2759
2760   if (iface->query_writable_namespaces == NULL)
2761     return g_file_attribute_info_list_new ();
2762
2763   my_error = NULL;
2764   list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2765   
2766   if (list == NULL)
2767     {
2768       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2769         {
2770           list = g_file_attribute_info_list_new ();
2771           g_error_free (my_error);
2772         }
2773       else
2774         g_propagate_error (error, my_error);
2775     }
2776   
2777   return list;
2778 }
2779
2780 /**
2781  * g_file_set_attribute:
2782  * @file: input #GFile.
2783  * @attribute: a string containing the attribute's name.
2784  * @type: The type of the attribute
2785  * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2786  * @flags: a set of #GFileQueryInfoFlags.
2787  * @cancellable: optional #GCancellable object, %NULL to ignore.
2788  * @error: a #GError, or %NULL
2789  * 
2790  * Sets an attribute in the file with attribute name @attribute to @value.
2791  * 
2792  * If @cancellable is not %NULL, then the operation can be cancelled by
2793  * triggering the cancellable object from another thread. If the operation
2794  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2795  * 
2796  * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2797  **/
2798 gboolean
2799 g_file_set_attribute (GFile                      *file,
2800                       const char                 *attribute,
2801                       GFileAttributeType          type,
2802                       gpointer                    value_p,
2803                       GFileQueryInfoFlags         flags,
2804                       GCancellable               *cancellable,
2805                       GError                    **error)
2806 {
2807   GFileIface *iface;
2808   
2809   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2810   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2811
2812   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2813     return FALSE;
2814   
2815   iface = G_FILE_GET_IFACE (file);
2816
2817   if (iface->set_attribute == NULL)
2818     {
2819       g_set_error (error, G_IO_ERROR,
2820                    G_IO_ERROR_NOT_SUPPORTED,
2821                    _("Operation not supported"));
2822       return FALSE;
2823     }
2824
2825   return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2826 }
2827
2828 /**
2829  * g_file_set_attributes_from_info:
2830  * @file: input #GFile.
2831  * @info: a #GFileInfo.
2832  * @flags: #GFileQueryInfoFlags
2833  * @cancellable: optional #GCancellable object, %NULL to ignore.
2834  * @error: a #GError, or %NULL 
2835  * 
2836  * Tries to set all attributes in the #GFileInfo on the target values, 
2837  * not stopping on the first error.
2838  * 
2839  * If there is any error during this operation then @error will be set to
2840  * the first error. Error on particular fields are flagged by setting 
2841  * the "status" field in the attribute value to 
2842  * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2843  * further errors.
2844  *
2845  * If @cancellable is not %NULL, then the operation can be cancelled by
2846  * triggering the cancellable object from another thread. If the operation
2847  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2848  * 
2849  * Returns: %TRUE if there was any error, %FALSE otherwise.
2850  **/
2851 gboolean
2852 g_file_set_attributes_from_info (GFile                *file,
2853                                  GFileInfo            *info,
2854                                  GFileQueryInfoFlags   flags,
2855                                  GCancellable         *cancellable,
2856                                  GError              **error)
2857 {
2858   GFileIface *iface;
2859
2860   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2861   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2862
2863   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2864     return FALSE;
2865   
2866   g_file_info_clear_status (info);
2867   
2868   iface = G_FILE_GET_IFACE (file);
2869
2870   return (* iface->set_attributes_from_info) (file, 
2871                                               info, 
2872                                               flags, 
2873                                               cancellable, 
2874                                               error);
2875 }
2876
2877
2878 static gboolean
2879 g_file_real_set_attributes_from_info (GFile                *file,
2880                                       GFileInfo            *info,
2881                                       GFileQueryInfoFlags   flags,
2882                                       GCancellable         *cancellable,
2883                                       GError              **error)
2884 {
2885   char **attributes;
2886   int i;
2887   gboolean res;
2888   GFileAttributeValue *value;
2889   
2890   res = TRUE;
2891   
2892   attributes = g_file_info_list_attributes (info, NULL);
2893
2894   for (i = 0; attributes[i] != NULL; i++)
2895     {
2896       value = _g_file_info_get_attribute_value (info, attributes[i]);
2897
2898       if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2899         continue;
2900
2901       if (!g_file_set_attribute (file, attributes[i],
2902                                  value->type, _g_file_attribute_value_peek_as_pointer (value),
2903                                  flags, cancellable, error))
2904         {
2905           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2906           res = FALSE;
2907           /* Don't set error multiple times */
2908           error = NULL;
2909         }
2910       else
2911         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2912     }
2913   
2914   g_strfreev (attributes);
2915   
2916   return res;
2917 }
2918
2919 /**
2920  * g_file_set_attributes_async:
2921  * @file: input #GFile.
2922  * @info: a #GFileInfo.
2923  * @flags: a #GFileQueryInfoFlags.
2924  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2925  *     of the request. 
2926  * @cancellable: optional #GCancellable object, %NULL to ignore.
2927  * @callback: a #GAsyncReadyCallback. 
2928  * @user_data: a #gpointer.
2929  *
2930  * Asynchronously sets the attributes of @file with @info.
2931  * 
2932  * For more details, see g_file_set_attributes_from_info() which is
2933  * the synchronous version of this call.
2934  *
2935  * When the operation is finished, @callback will be called. You can then call
2936  * g_file_set_attributes_finish() to get the result of the operation.
2937  **/
2938 void
2939 g_file_set_attributes_async (GFile               *file,
2940                              GFileInfo           *info,
2941                              GFileQueryInfoFlags  flags,
2942                              int                  io_priority,
2943                              GCancellable        *cancellable,
2944                              GAsyncReadyCallback  callback,
2945                              gpointer             user_data)
2946 {
2947   GFileIface *iface;
2948   
2949   g_return_if_fail (G_IS_FILE (file));
2950   g_return_if_fail (G_IS_FILE_INFO (info));
2951
2952   iface = G_FILE_GET_IFACE (file);
2953   (* iface->set_attributes_async) (file, 
2954                                    info, 
2955                                    flags, 
2956                                    io_priority, 
2957                                    cancellable, 
2958                                    callback, 
2959                                    user_data);
2960 }
2961
2962 /**
2963  * g_file_set_attributes_finish:
2964  * @file: input #GFile.
2965  * @result: a #GAsyncResult.
2966  * @info: a #GFileInfo.
2967  * @error: a #GError, or %NULL
2968  * 
2969  * Finishes setting an attribute started in g_file_set_attributes_async().
2970  * 
2971  * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2972  **/
2973 gboolean
2974 g_file_set_attributes_finish (GFile         *file,
2975                               GAsyncResult  *result,
2976                               GFileInfo    **info,
2977                               GError       **error)
2978 {
2979   GFileIface *iface;
2980   
2981   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2982   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2983
2984   /* No standard handling of errors here, as we must set info even
2985    * on errors 
2986    */
2987   iface = G_FILE_GET_IFACE (file);
2988   return (* iface->set_attributes_finish) (file, result, info, error);
2989 }
2990
2991 /**
2992  * g_file_set_attribute_string:
2993  * @file: input #GFile.
2994  * @attribute: a string containing the attribute's name.
2995  * @value: a string containing the attribute's value.
2996  * @flags: #GFileQueryInfoFlags.
2997  * @cancellable: optional #GCancellable object, %NULL to ignore.
2998  * @error: a #GError, or %NULL
2999  * 
3000  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value. 
3001  * If @attribute is of a different type, this operation will fail.
3002  * 
3003  * If @cancellable is not %NULL, then the operation can be cancelled by
3004  * triggering the cancellable object from another thread. If the operation
3005  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3006  * 
3007  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3008  **/
3009 gboolean
3010 g_file_set_attribute_string (GFile                *file,
3011                              const char           *attribute,
3012                              const char           *value,
3013                              GFileQueryInfoFlags   flags,
3014                              GCancellable         *cancellable,
3015                              GError              **error)
3016 {
3017   return g_file_set_attribute (file, attribute,
3018                                G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3019                                flags, cancellable, error);
3020 }
3021
3022 /**
3023  * g_file_set_attribute_byte_string:
3024  * @file: input #GFile.
3025  * @attribute: a string containing the attribute's name.
3026  * @value: a string containing the attribute's new value.
3027  * @flags: a #GFileQueryInfoFlags.
3028  * @cancellable: optional #GCancellable object, %NULL to ignore.
3029  * @error: a #GError, or %NULL
3030  * 
3031  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value. 
3032  * If @attribute is of a different type, this operation will fail, 
3033  * returning %FALSE. 
3034  * 
3035  * If @cancellable is not %NULL, then the operation can be cancelled by
3036  * triggering the cancellable object from another thread. If the operation
3037  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3038  * 
3039  * Returns: %TRUE if the @attribute was successfully set to @value 
3040  * in the @file, %FALSE otherwise.
3041  **/
3042 gboolean
3043 g_file_set_attribute_byte_string  (GFile                *file,
3044                                    const char           *attribute,
3045                                    const char           *value,
3046                                    GFileQueryInfoFlags   flags,
3047                                    GCancellable         *cancellable,
3048                                    GError              **error)
3049 {
3050   return g_file_set_attribute (file, attribute,
3051                                G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3052                                flags, cancellable, error);
3053 }
3054
3055 /**
3056  * g_file_set_attribute_uint32:
3057  * @file: input #GFile.
3058  * @attribute: a string containing the attribute's name.
3059  * @value: a #guint32 containing the attribute's new value.
3060  * @flags: a #GFileQueryInfoFlags.
3061  * @cancellable: optional #GCancellable object, %NULL to ignore.
3062  * @error: a #GError, or %NULL
3063  * 
3064  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value. 
3065  * If @attribute is of a different type, this operation will fail.
3066  * 
3067  * If @cancellable is not %NULL, then the operation can be cancelled by
3068  * triggering the cancellable object from another thread. If the operation
3069  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3070  * 
3071  * Returns: %TRUE if the @attribute was successfully set to @value 
3072  * in the @file, %FALSE otherwise.
3073  **/
3074 gboolean
3075 g_file_set_attribute_uint32 (GFile                *file,
3076                              const char           *attribute,
3077                              guint32               value,
3078                              GFileQueryInfoFlags   flags,
3079                              GCancellable         *cancellable,
3080                              GError              **error)
3081 {
3082   return g_file_set_attribute (file, attribute,
3083                                G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3084                                flags, cancellable, error);
3085 }
3086
3087 /**
3088  * g_file_set_attribute_int32:
3089  * @file: input #GFile.
3090  * @attribute: a string containing the attribute's name.
3091  * @value: a #gint32 containing the attribute's new value.
3092  * @flags: a #GFileQueryInfoFlags.
3093  * @cancellable: optional #GCancellable object, %NULL to ignore.
3094  * @error: a #GError, or %NULL
3095  * 
3096  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value. 
3097  * If @attribute is of a different type, this operation will fail.
3098  * 
3099  * If @cancellable is not %NULL, then the operation can be cancelled by
3100  * triggering the cancellable object from another thread. If the operation
3101  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3102  * 
3103  * Returns: %TRUE if the @attribute was successfully set to @value 
3104  * in the @file, %FALSE otherwise. 
3105  **/
3106 gboolean
3107 g_file_set_attribute_int32 (GFile                *file,
3108                             const char           *attribute,
3109                             gint32                value,
3110                             GFileQueryInfoFlags   flags,
3111                             GCancellable         *cancellable,
3112                             GError              **error)
3113 {
3114   return g_file_set_attribute (file, attribute,
3115                                G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3116                                flags, cancellable, error);
3117 }
3118
3119 /**
3120  * g_file_set_attribute_uint64:
3121  * @file: input #GFile. 
3122  * @attribute: a string containing the attribute's name.
3123  * @value: a #guint64 containing the attribute's new value.
3124  * @flags: a #GFileQueryInfoFlags.
3125  * @cancellable: optional #GCancellable object, %NULL to ignore.
3126  * @error: a #GError, or %NULL
3127  * 
3128  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value. 
3129  * If @attribute is of a different type, this operation will fail.
3130  * 
3131  * If @cancellable is not %NULL, then the operation can be cancelled by
3132  * triggering the cancellable object from another thread. If the operation
3133  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3134  * 
3135  * Returns: %TRUE if the @attribute was successfully set to @value 
3136  * in the @file, %FALSE otherwise.
3137  **/
3138 gboolean
3139 g_file_set_attribute_uint64 (GFile                *file,
3140                              const char           *attribute,
3141                              guint64               value,
3142                              GFileQueryInfoFlags   flags,
3143                              GCancellable         *cancellable,
3144                              GError              **error)
3145  {
3146   return g_file_set_attribute (file, attribute,
3147                                G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3148                                flags, cancellable, error);
3149 }
3150
3151 /**
3152  * g_file_set_attribute_int64:
3153  * @file: input #GFile.
3154  * @attribute: a string containing the attribute's name.
3155  * @value: a #guint64 containing the attribute's new value.
3156  * @flags: a #GFileQueryInfoFlags.
3157  * @cancellable: optional #GCancellable object, %NULL to ignore.
3158  * @error: a #GError, or %NULL
3159  * 
3160  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value. 
3161  * If @attribute is of a different type, this operation will fail.
3162  * 
3163  * If @cancellable is not %NULL, then the operation can be cancelled by
3164  * triggering the cancellable object from another thread. If the operation
3165  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3166  * 
3167  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3168  **/
3169 gboolean
3170 g_file_set_attribute_int64 (GFile                *file,
3171                             const char           *attribute,
3172                             gint64                value,
3173                             GFileQueryInfoFlags   flags,
3174                             GCancellable         *cancellable,
3175                             GError              **error)
3176 {
3177   return g_file_set_attribute (file, attribute,
3178                                G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3179                                flags, cancellable, error);
3180 }
3181
3182 /**
3183  * g_file_mount_mountable:
3184  * @file: input #GFile.
3185  * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3186  * @cancellable: optional #GCancellable object, %NULL to ignore.
3187  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3188  * @user_data: the data to pass to callback function
3189  * 
3190  * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3191  * Using @mount_operation, you can request callbacks when, for instance, 
3192  * passwords are needed during authentication.
3193  *
3194  * If @cancellable is not %NULL, then the operation can be cancelled by
3195  * triggering the cancellable object from another thread. If the operation
3196  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3197  *
3198  * When the operation is finished, @callback will be called. You can then call
3199  * g_file_mount_mountable_finish() to get the result of the operation.
3200  **/
3201 void
3202 g_file_mount_mountable (GFile               *file,
3203                         GMountOperation     *mount_operation,
3204                         GCancellable        *cancellable,
3205                         GAsyncReadyCallback  callback,
3206                         gpointer             user_data)
3207 {
3208   GFileIface *iface;
3209
3210   g_return_if_fail (G_IS_FILE (file));
3211
3212   iface = G_FILE_GET_IFACE (file);
3213
3214   if (iface->mount_mountable == NULL)
3215     g_simple_async_report_error_in_idle (G_OBJECT (file),
3216                                          callback,
3217                                          user_data,
3218                                          G_IO_ERROR,
3219                                          G_IO_ERROR_NOT_SUPPORTED,
3220                                          _("Operation not supported"));
3221   
3222   (* iface->mount_mountable) (file,
3223                               mount_operation,
3224                               cancellable,
3225                               callback,
3226                               user_data);
3227 }
3228
3229 /**
3230  * g_file_mount_mountable_finish:
3231  * @file: input #GFile.
3232  * @result: a #GAsyncResult.
3233  * @error: a #GError, or %NULL
3234  *
3235  * Finishes a mount operation. See g_file_mount_mountable() for details.
3236  * 
3237  * Finish an asynchronous mount operation that was started 
3238  * with g_file_mount_mountable().
3239  *
3240  * Returns: a #GFile or %NULL on error.
3241  **/
3242 GFile *
3243 g_file_mount_mountable_finish (GFile         *file,
3244                                GAsyncResult  *result,
3245                                GError       **error)
3246 {
3247   GFileIface *iface;
3248
3249   g_return_val_if_fail (G_IS_FILE (file), NULL);
3250   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3251
3252   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3253     {
3254       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3255       if (g_simple_async_result_propagate_error (simple, error))
3256         return NULL;
3257     }
3258   
3259   iface = G_FILE_GET_IFACE (file);
3260   return (* iface->mount_mountable_finish) (file, result, error);
3261 }
3262
3263 /**
3264  * g_file_unmount_mountable:
3265  * @file: input #GFile.
3266  * @flags: flags affecting the operation
3267  * @cancellable: optional #GCancellable object, %NULL to ignore.
3268  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3269  * @user_data: the data to pass to callback function
3270  *
3271  * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3272  *
3273  * If @cancellable is not %NULL, then the operation can be cancelled by
3274  * triggering the cancellable object from another thread. If the operation
3275  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3276  *
3277  * When the operation is finished, @callback will be called. You can then call
3278  * g_file_unmount_mountable_finish() to get the result of the operation.
3279  **/
3280 void
3281 g_file_unmount_mountable (GFile               *file,
3282                           GMountUnmountFlags   flags,
3283                           GCancellable        *cancellable,
3284                           GAsyncReadyCallback  callback,
3285                           gpointer             user_data)
3286 {
3287   GFileIface *iface;
3288   
3289   g_return_if_fail (G_IS_FILE (file));
3290
3291   iface = G_FILE_GET_IFACE (file);
3292   
3293   if (iface->unmount_mountable == NULL)
3294     g_simple_async_report_error_in_idle (G_OBJECT (file),
3295                                          callback,
3296                                          user_data,
3297                                          G_IO_ERROR,
3298                                          G_IO_ERROR_NOT_SUPPORTED,
3299                                          _("Operation not supported"));
3300   
3301   (* iface->unmount_mountable) (file,
3302                                 flags,
3303                                 cancellable,
3304                                 callback,
3305                                 user_data);
3306 }
3307
3308 /**
3309  * g_file_unmount_mountable_finish:
3310  * @file: input #GFile.
3311  * @result: a #GAsyncResult.
3312  * @error: a #GError, or %NULL
3313  *
3314  * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3315  * 
3316  * Finish an asynchronous unmount operation that was started 
3317  * with g_file_unmount_mountable().
3318  *
3319  * Returns: %TRUE if the operation finished successfully. %FALSE
3320  * otherwise.
3321  **/
3322 gboolean
3323 g_file_unmount_mountable_finish (GFile         *file,
3324                                  GAsyncResult  *result,
3325                                  GError       **error)
3326 {
3327   GFileIface *iface;
3328   
3329   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3330   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3331
3332   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3333     {
3334       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3335       if (g_simple_async_result_propagate_error (simple, error))
3336         return FALSE;
3337     }
3338   
3339   iface = G_FILE_GET_IFACE (file);
3340   return (* iface->unmount_mountable_finish) (file, result, error);
3341 }
3342
3343 /**
3344  * g_file_eject_mountable:
3345  * @file: input #GFile.
3346  * @flags: flags affecting the operation
3347  * @cancellable: optional #GCancellable object, %NULL to ignore.
3348  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3349  * @user_data: the data to pass to callback function
3350  * 
3351  * Starts an asynchronous eject on a mountable.  
3352  * When this operation has completed, @callback will be called with
3353  * @user_user data, and the operation can be finalized with 
3354  * g_file_eject_mountable_finish().
3355  * 
3356  * If @cancellable is not %NULL, then the operation can be cancelled by
3357  * triggering the cancellable object from another thread. If the operation
3358  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3359  **/
3360 void
3361 g_file_eject_mountable (GFile               *file,
3362                         GMountUnmountFlags   flags,
3363                         GCancellable        *cancellable,
3364                         GAsyncReadyCallback  callback,
3365                         gpointer             user_data)
3366 {
3367   GFileIface *iface;
3368
3369   g_return_if_fail (G_IS_FILE (file));
3370
3371   iface = G_FILE_GET_IFACE (file);
3372   
3373   if (iface->eject_mountable == NULL)
3374     g_simple_async_report_error_in_idle (G_OBJECT (file),
3375                                          callback,
3376                                          user_data,
3377                                          G_IO_ERROR,
3378                                          G_IO_ERROR_NOT_SUPPORTED,
3379                                          _("Operation not supported"));
3380   
3381   (* iface->eject_mountable) (file,
3382                               flags,
3383                               cancellable,
3384                               callback,
3385                               user_data);
3386 }
3387
3388 /**
3389  * g_file_eject_mountable_finish:
3390  * @file: input #GFile.
3391  * @result: a #GAsyncResult.
3392  * @error: a #GError, or %NULL
3393  * 
3394  * Finishes an asynchronous eject operation started by 
3395  * g_file_eject_mountable().
3396  * 
3397  * Returns: %TRUE if the @file was ejected successfully. %FALSE 
3398  * otherwise.
3399  **/
3400 gboolean
3401 g_file_eject_mountable_finish (GFile         *file,
3402                                GAsyncResult  *result,
3403                                GError       **error)
3404 {
3405   GFileIface *iface;
3406   
3407   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3408   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3409
3410   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3411     {
3412       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3413       if (g_simple_async_result_propagate_error (simple, error))
3414         return FALSE;
3415     }
3416   
3417   iface = G_FILE_GET_IFACE (file);
3418   return (* iface->eject_mountable_finish) (file, result, error);
3419 }
3420
3421 /**
3422  * g_file_monitor_directory:
3423  * @file: input #GFile.
3424  * @flags: a set of #GFileMonitorFlags.
3425  * @cancellable: optional #GCancellable object, %NULL to ignore.
3426  * @error: a #GError, or %NULL.
3427  * 
3428  * Obtains a directory monitor for the given file.
3429  * This may fail if directory monitoring is not supported.
3430  *
3431  * If @cancellable is not %NULL, then the operation can be cancelled by
3432  * triggering the cancellable object from another thread. If the operation
3433  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3434  * 
3435  * Returns: a #GFileMonitor for the given @file, 
3436  * or %NULL on error.
3437  **/
3438 GFileMonitor*
3439 g_file_monitor_directory (GFile             *file,
3440                           GFileMonitorFlags  flags,
3441                           GCancellable      *cancellable,
3442                           GError           **error)
3443 {
3444   GFileIface *iface;
3445
3446   g_return_val_if_fail (G_IS_FILE (file), NULL);
3447
3448   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3449     return NULL;
3450
3451   iface = G_FILE_GET_IFACE (file);
3452
3453   if (iface->monitor_dir == NULL)
3454     {
3455       g_set_error (error, G_IO_ERROR,
3456                    G_IO_ERROR_NOT_SUPPORTED,
3457                    _("Operation not supported"));
3458       return NULL;
3459     }
3460
3461   return (* iface->monitor_dir) (file, flags, cancellable, error);
3462 }
3463
3464 /**
3465  * g_file_monitor_file:
3466  * @file: input #GFile.
3467  * @flags: a set of #GFileMonitorFlags.
3468  * @cancellable: optional #GCancellable object, %NULL to ignore.
3469  * @error: a #GError, or %NULL.
3470  * 
3471  * Obtains a file monitor for the given file. If no file notification
3472  * mechanism exists, then regular polling of the file is used.
3473  *
3474  * If @cancellable is not %NULL, then the operation can be cancelled by
3475  * triggering the cancellable object from another thread. If the operation
3476  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3477  * 
3478  * Returns: a #GFileMonitor for the given @file.
3479  **/
3480 GFileMonitor*
3481 g_file_monitor_file (GFile             *file,
3482                      GFileMonitorFlags  flags,
3483                      GCancellable      *cancellable,
3484                      GError           **error)
3485 {
3486   GFileIface *iface;
3487   GFileMonitor *monitor;
3488   
3489   g_return_val_if_fail (G_IS_FILE (file), NULL);
3490
3491   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3492     return NULL;
3493
3494   iface = G_FILE_GET_IFACE (file);
3495
3496   monitor = NULL;
3497   
3498   if (iface->monitor_file)
3499     monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3500
3501 /* Fallback to polling */
3502   if (monitor == NULL)
3503     monitor = _g_poll_file_monitor_new (file);
3504
3505   return monitor;
3506 }
3507
3508 /********************************************
3509  *   Default implementation of async ops    *
3510  ********************************************/
3511
3512 typedef struct {
3513   char *attributes;
3514   GFileQueryInfoFlags flags;
3515   GFileInfo *info;
3516 } QueryInfoAsyncData;
3517
3518 static void
3519 query_info_data_free (QueryInfoAsyncData *data)
3520 {
3521   if (data->info)
3522     g_object_unref (data->info);
3523   g_free (data->attributes);
3524   g_free (data);
3525 }
3526
3527 static void
3528 query_info_async_thread (GSimpleAsyncResult *res,
3529                          GObject            *object,
3530                          GCancellable       *cancellable)
3531 {
3532   GError *error = NULL;
3533   QueryInfoAsyncData *data;
3534   GFileInfo *info;
3535   
3536   data = g_simple_async_result_get_op_res_gpointer (res);
3537   
3538   info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3539
3540   if (info == NULL)
3541     {
3542       g_simple_async_result_set_from_error (res, error);
3543       g_error_free (error);
3544     }
3545   else
3546     data->info = info;
3547 }
3548
3549 static void
3550 g_file_real_query_info_async (GFile               *file,
3551                               const char          *attributes,
3552                               GFileQueryInfoFlags  flags,
3553                               int                  io_priority,
3554                               GCancellable        *cancellable,
3555                               GAsyncReadyCallback  callback,
3556                               gpointer             user_data)
3557 {
3558   GSimpleAsyncResult *res;
3559   QueryInfoAsyncData *data;
3560
3561   data = g_new0 (QueryInfoAsyncData, 1);
3562   data->attributes = g_strdup (attributes);
3563   data->flags = flags;
3564   
3565   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3566   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3567   
3568   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3569   g_object_unref (res);
3570 }
3571
3572 static GFileInfo *
3573 g_file_real_query_info_finish (GFile         *file,
3574                                GAsyncResult  *res,
3575                                GError       **error)
3576 {
3577   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3578   QueryInfoAsyncData *data;
3579
3580   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3581
3582   data = g_simple_async_result_get_op_res_gpointer (simple);
3583   if (data->info)
3584     return g_object_ref (data->info);
3585   
3586   return NULL;
3587 }
3588
3589 typedef struct {
3590   char *attributes;
3591   GFileQueryInfoFlags flags;
3592   GFileEnumerator *enumerator;
3593 } EnumerateChildrenAsyncData;
3594
3595 static void
3596 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3597 {
3598   if (data->enumerator)
3599     g_object_unref (data->enumerator);
3600   g_free (data->attributes);
3601   g_free (data);
3602 }
3603
3604 static void
3605 enumerate_children_async_thread (GSimpleAsyncResult *res,
3606                                  GObject            *object,
3607                                  GCancellable       *cancellable)
3608 {
3609   GError *error = NULL;
3610   EnumerateChildrenAsyncData *data;
3611   GFileEnumerator *enumerator;
3612   
3613   data = g_simple_async_result_get_op_res_gpointer (res);
3614   
3615   enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3616
3617   if (enumerator == NULL)
3618     {
3619       g_simple_async_result_set_from_error (res, error);
3620       g_error_free (error);
3621     }
3622   else
3623     data->enumerator = enumerator;
3624 }
3625
3626 static void
3627 g_file_real_enumerate_children_async (GFile               *file,
3628                                       const char          *attributes,
3629                                       GFileQueryInfoFlags  flags,
3630                                       int                  io_priority,
3631                                       GCancellable        *cancellable,
3632                                       GAsyncReadyCallback  callback,
3633                                       gpointer             user_data)
3634 {
3635   GSimpleAsyncResult *res;
3636   EnumerateChildrenAsyncData *data;
3637
3638   data = g_new0 (EnumerateChildrenAsyncData, 1);
3639   data->attributes = g_strdup (attributes);
3640   data->flags = flags;
3641   
3642   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3643   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3644   
3645   g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3646   g_object_unref (res);
3647 }
3648
3649 static GFileEnumerator *
3650 g_file_real_enumerate_children_finish (GFile         *file,
3651                                        GAsyncResult  *res,
3652                                        GError       **error)
3653 {
3654   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3655   EnumerateChildrenAsyncData *data;
3656
3657   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3658
3659   data = g_simple_async_result_get_op_res_gpointer (simple);
3660   if (data->enumerator)
3661     return g_object_ref (data->enumerator);
3662   
3663   return NULL;
3664 }
3665
3666 static void
3667 open_read_async_thread (GSimpleAsyncResult *res,
3668                         GObject            *object,
3669                         GCancellable       *cancellable)
3670 {
3671   GFileIface *iface;
3672   GFileInputStream *stream;
3673   GError *error = NULL;
3674
3675   iface = G_FILE_GET_IFACE (object);
3676
3677   stream = iface->read_fn (G_FILE (object), cancellable, &error);
3678
3679   if (stream == NULL)
3680     {
3681       g_simple_async_result_set_from_error (res, error);
3682       g_error_free (error);
3683     }
3684   else
3685     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3686 }
3687
3688 static void
3689 g_file_real_read_async (GFile               *file,
3690                         int                  io_priority,
3691                         GCancellable        *cancellable,
3692                         GAsyncReadyCallback  callback,
3693                         gpointer             user_data)
3694 {
3695   GSimpleAsyncResult *res;
3696   
3697   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3698   
3699   g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3700   g_object_unref (res);
3701 }
3702
3703 static GFileInputStream *
3704 g_file_real_read_finish (GFile         *file,
3705                          GAsyncResult  *res,
3706                          GError       **error)
3707 {
3708   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3709   gpointer op;
3710
3711   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3712
3713   op = g_simple_async_result_get_op_res_gpointer (simple);
3714   if (op)
3715     return g_object_ref (op);
3716   
3717   return NULL;
3718 }
3719
3720 static void
3721 append_to_async_thread (GSimpleAsyncResult *res,
3722                         GObject            *object,
3723                         GCancellable       *cancellable)
3724 {
3725   GFileIface *iface;
3726   GFileCreateFlags *data;
3727   GFileOutputStream *stream;
3728   GError *error = NULL;
3729
3730   iface = G_FILE_GET_IFACE (object);
3731
3732   data = g_simple_async_result_get_op_res_gpointer (res);
3733
3734   stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3735
3736   if (stream == NULL)
3737     {
3738       g_simple_async_result_set_from_error (res, error);
3739       g_error_free (error);
3740     }
3741   else
3742     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3743 }
3744
3745 static void
3746 g_file_real_append_to_async (GFile               *file,
3747                              GFileCreateFlags     flags,
3748                              int                  io_priority,
3749                              GCancellable        *cancellable,
3750                              GAsyncReadyCallback  callback,
3751                              gpointer             user_data)
3752 {
3753   GFileCreateFlags *data;
3754   GSimpleAsyncResult *res;
3755
3756   data = g_new0 (GFileCreateFlags, 1);
3757   *data = flags;
3758
3759   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3760   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3761
3762   g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3763   g_object_unref (res);
3764 }
3765
3766 static GFileOutputStream *
3767 g_file_real_append_to_finish (GFile         *file,
3768                               GAsyncResult  *res,
3769                               GError       **error)
3770 {
3771   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3772   gpointer op;
3773
3774   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3775
3776   op = g_simple_async_result_get_op_res_gpointer (simple);
3777   if (op)
3778     return g_object_ref (op);
3779   
3780   return NULL;
3781 }
3782
3783 static void
3784 create_async_thread (GSimpleAsyncResult *res,
3785                      GObject            *object,
3786                      GCancellable       *cancellable)
3787 {
3788   GFileIface *iface;
3789   GFileCreateFlags *data;
3790   GFileOutputStream *stream;
3791   GError *error = NULL;
3792
3793   iface = G_FILE_GET_IFACE (object);
3794
3795   data = g_simple_async_result_get_op_res_gpointer (res);
3796
3797   stream = iface->create (G_FILE (object), *data, cancellable, &error);
3798
3799   if (stream == NULL)
3800     {
3801       g_simple_async_result_set_from_error (res, error);
3802       g_error_free (error);
3803     }
3804   else
3805     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3806 }
3807
3808 static void
3809 g_file_real_create_async (GFile               *file,
3810                           GFileCreateFlags     flags,
3811                           int                  io_priority,
3812                           GCancellable        *cancellable,
3813                           GAsyncReadyCallback  callback,
3814                           gpointer             user_data)
3815 {
3816   GFileCreateFlags *data;
3817   GSimpleAsyncResult *res;
3818
3819   data = g_new0 (GFileCreateFlags, 1);
3820   *data = flags;
3821
3822   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3823   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3824
3825   g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3826   g_object_unref (res);
3827 }
3828
3829 static GFileOutputStream *
3830 g_file_real_create_finish (GFile         *file,
3831                            GAsyncResult  *res,
3832                            GError       **error)
3833 {
3834   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3835   gpointer op;
3836
3837   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3838
3839   op = g_simple_async_result_get_op_res_gpointer (simple);
3840   if (op)
3841     return g_object_ref (op);
3842   
3843   return NULL;
3844 }
3845
3846 typedef struct {
3847   GFileOutputStream *stream;
3848   char *etag;
3849   gboolean make_backup;
3850   GFileCreateFlags flags;
3851 } ReplaceAsyncData;
3852
3853 static void
3854 replace_async_data_free (ReplaceAsyncData *data)
3855 {
3856   if (data->stream)
3857     g_object_unref (data->stream);
3858   g_free (data->etag);
3859   g_free (data);
3860 }
3861
3862 static void
3863 replace_async_thread (GSimpleAsyncResult *res,
3864                       GObject            *object,
3865                       GCancellable       *cancellable)
3866 {
3867   GFileIface *iface;
3868   GFileOutputStream *stream;
3869   GError *error = NULL;
3870   ReplaceAsyncData *data;
3871
3872   iface = G_FILE_GET_IFACE (object);
3873   
3874   data = g_simple_async_result_get_op_res_gpointer (res);
3875
3876   stream = iface->replace (G_FILE (object),
3877                            data->etag,
3878                            data->make_backup,
3879                            data->flags,
3880                            cancellable,
3881                            &error);
3882
3883   if (stream == NULL)
3884     {
3885       g_simple_async_result_set_from_error (res, error);
3886       g_error_free (error);
3887     }
3888   else
3889     data->stream = stream;
3890 }
3891
3892 static void
3893 g_file_real_replace_async (GFile               *file,
3894                            const char          *etag,
3895                            gboolean             make_backup,
3896                            GFileCreateFlags     flags,
3897                            int                  io_priority,
3898                            GCancellable        *cancellable,
3899                            GAsyncReadyCallback  callback,
3900                            gpointer             user_data)
3901 {
3902   GSimpleAsyncResult *res;
3903   ReplaceAsyncData *data;
3904
3905   data = g_new0 (ReplaceAsyncData, 1);
3906   data->etag = g_strdup (etag);
3907   data->make_backup = make_backup;
3908   data->flags = flags;
3909
3910   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3911   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3912
3913   g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3914   g_object_unref (res);
3915 }
3916
3917 static GFileOutputStream *
3918 g_file_real_replace_finish (GFile         *file,
3919                             GAsyncResult  *res,
3920                             GError       **error)
3921 {
3922   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3923   ReplaceAsyncData *data;
3924
3925   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3926
3927   data = g_simple_async_result_get_op_res_gpointer (simple);
3928   if (data->stream)
3929     return g_object_ref (data->stream);
3930   
3931   return NULL;
3932 }
3933
3934 typedef struct {
3935   char *name;
3936   GFile *file;
3937 } SetDisplayNameAsyncData;
3938
3939 static void
3940 set_display_name_data_free (SetDisplayNameAsyncData *data)
3941 {
3942   g_free (data->name);
3943   if (data->file)
3944     g_object_unref (data->file);
3945   g_free (data);
3946 }
3947
3948 static void
3949 set_display_name_async_thread (GSimpleAsyncResult *res,
3950                                GObject            *object,
3951                                GCancellable       *cancellable)
3952 {
3953   GError *error = NULL;
3954   SetDisplayNameAsyncData *data;
3955   GFile *file;
3956   
3957   data = g_simple_async_result_get_op_res_gpointer (res);
3958   
3959   file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3960
3961   if (file == NULL)
3962     {
3963       g_simple_async_result_set_from_error (res, error);
3964       g_error_free (error);
3965     }
3966   else
3967     data->file = file;
3968 }
3969
3970 static void
3971 g_file_real_set_display_name_async (GFile               *file,  
3972                                     const char          *display_name,
3973                                     int                  io_priority,
3974                                     GCancellable        *cancellable,
3975                                     GAsyncReadyCallback  callback,
3976                                     gpointer             user_data)
3977 {
3978   GSimpleAsyncResult *res;
3979   SetDisplayNameAsyncData *data;
3980
3981   data = g_new0 (SetDisplayNameAsyncData, 1);
3982   data->name = g_strdup (display_name);
3983   
3984   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3985   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3986   
3987   g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3988   g_object_unref (res);
3989 }
3990
3991 static GFile *
3992 g_file_real_set_display_name_finish (GFile         *file,
3993                                      GAsyncResult  *res,
3994                                      GError       **error)
3995 {
3996   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3997   SetDisplayNameAsyncData *data;
3998
3999   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4000
4001   data = g_simple_async_result_get_op_res_gpointer (simple);
4002   if (data->file)
4003     return g_object_ref (data->file);
4004   
4005   return NULL;
4006 }
4007
4008 typedef struct {
4009   GFileQueryInfoFlags flags;
4010   GFileInfo *info;
4011   gboolean res;
4012   GError *error;
4013 } SetInfoAsyncData;
4014
4015 static void
4016 set_info_data_free (SetInfoAsyncData *data)
4017 {
4018   if (data->info)
4019     g_object_unref (data->info);
4020   if (data->error)
4021     g_error_free (data->error);
4022   g_free (data);
4023 }
4024
4025 static void
4026 set_info_async_thread (GSimpleAsyncResult *res,
4027                        GObject            *object,
4028                        GCancellable       *cancellable)
4029 {
4030   SetInfoAsyncData *data;
4031   
4032   data = g_simple_async_result_get_op_res_gpointer (res);
4033   
4034   data->error = NULL;
4035   data->res = g_file_set_attributes_from_info (G_FILE (object),
4036                                                data->info,
4037                                                data->flags,
4038                                                cancellable,
4039                                                &data->error);
4040 }
4041
4042 static void
4043 g_file_real_set_attributes_async (GFile               *file,
4044                                   GFileInfo           *info,
4045                                   GFileQueryInfoFlags  flags,
4046                                   int                  io_priority,
4047                                   GCancellable        *cancellable,
4048                                   GAsyncReadyCallback  callback,
4049                                   gpointer             user_data)
4050 {
4051   GSimpleAsyncResult *res;
4052   SetInfoAsyncData *data;
4053
4054   data = g_new0 (SetInfoAsyncData, 1);
4055   data->info = g_file_info_dup (info);
4056   data->flags = flags;
4057   
4058   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4059   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4060   
4061   g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4062   g_object_unref (res);
4063 }
4064
4065 static gboolean
4066 g_file_real_set_attributes_finish (GFile         *file,
4067                                    GAsyncResult  *res,
4068                                    GFileInfo    **info,
4069                                    GError       **error)
4070 {
4071   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4072   SetInfoAsyncData *data;
4073   
4074   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4075
4076   data = g_simple_async_result_get_op_res_gpointer (simple);
4077
4078   if (info) 
4079     *info = g_object_ref (data->info);
4080
4081   if (error != NULL && data->error) 
4082     *error = g_error_copy (data->error);
4083   
4084   return data->res;
4085 }
4086
4087 static void
4088 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4089                                     GObject            *object,
4090                                     GCancellable       *cancellable)
4091 {
4092   GError *error = NULL;
4093   GMount *mount;
4094   
4095   mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4096
4097   if (mount == NULL)
4098     {
4099       g_simple_async_result_set_from_error (res, error);
4100       g_error_free (error);
4101     }
4102   else
4103     g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4104 }
4105
4106 static void
4107 g_file_real_find_enclosing_mount_async (GFile               *file,
4108                                         int                  io_priority,
4109                                         GCancellable        *cancellable,
4110                                         GAsyncReadyCallback  callback,
4111                                         gpointer             user_data)
4112 {
4113   GSimpleAsyncResult *res;
4114   
4115   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4116   
4117   g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4118   g_object_unref (res);
4119 }
4120
4121 static GMount *
4122 g_file_real_find_enclosing_mount_finish (GFile         *file,
4123                                           GAsyncResult  *res,
4124                                           GError       **error)
4125 {
4126   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4127   GMount *mount;
4128
4129   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4130
4131   mount = g_simple_async_result_get_op_res_gpointer (simple);
4132   return g_object_ref (mount);
4133 }
4134
4135
4136 /********************************************
4137  *   Default VFS operations                 *
4138  ********************************************/
4139
4140 /**
4141  * g_file_new_for_path:
4142  * @path: a string containing a relative or absolute path.
4143  * 
4144  * Constructs a #GFile for a given path. This operation never
4145  * fails, but the returned object might not support any I/O
4146  * operation if @path is malformed.
4147  * 
4148  * Returns: a new #GFile for the given @path. 
4149  **/
4150 GFile *
4151 g_file_new_for_path (const char *path)
4152 {
4153   g_return_val_if_fail (path != NULL, NULL);
4154
4155   return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4156 }
4157  
4158 /**
4159  * g_file_new_for_uri:
4160  * @uri: a string containing a URI.
4161  * 
4162  * Constructs a #GFile for a given URI. This operation never 
4163  * fails, but the returned object might not support any I/O 
4164  * operation if @uri is malformed or if the uri type is 
4165  * not supported.
4166  * 
4167  * Returns: a #GFile for the given @uri.
4168  **/ 
4169 GFile *
4170 g_file_new_for_uri (const char *uri)
4171 {
4172   g_return_val_if_fail (uri != NULL, NULL);
4173
4174   return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4175 }
4176   
4177 /**
4178  * g_file_parse_name:
4179  * @parse_name: a file name or path to be parsed.
4180  * 
4181  * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4182  * This operation never fails, but the returned object might not support any I/O
4183  * operation if the @parse_name cannot be parsed.
4184  * 
4185  * Returns: a new #GFile.
4186  **/
4187 GFile *
4188 g_file_parse_name (const char *parse_name)
4189 {
4190   g_return_val_if_fail (parse_name != NULL, NULL);
4191
4192   return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4193 }
4194
4195 static gboolean
4196 is_valid_scheme_character (char c)
4197 {
4198   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4199 }
4200
4201 static gboolean
4202 has_valid_scheme (const char *uri)
4203 {
4204   const char *p;
4205   
4206   p = uri;
4207   
4208   if (!is_valid_scheme_character (*p))
4209     return FALSE;
4210
4211   do {
4212     p++;
4213   } while (is_valid_scheme_character (*p));
4214
4215   return *p == ':';
4216 }
4217
4218 /**
4219  * g_file_new_for_commandline_arg:
4220  * @arg: a command line string.
4221  * 
4222  * Creates a #GFile with the given argument from the command line. The value of
4223  * @arg can be either a URI, an absolute path or a relative path resolved
4224  * relative to the current working directory.
4225  * This operation never fails, but the returned object might not support any
4226  * I/O operation if @arg points to a malformed path.
4227  *
4228  * Returns: a new #GFile. 
4229  **/
4230 GFile *
4231 g_file_new_for_commandline_arg (const char *arg)
4232 {
4233   GFile *file;
4234   char *filename;
4235   char *current_dir;
4236   
4237   g_return_val_if_fail (arg != NULL, NULL);
4238   
4239   if (g_path_is_absolute (arg))
4240     return g_file_new_for_path (arg);
4241
4242   if (has_valid_scheme (arg))
4243     return g_file_new_for_uri (arg);
4244     
4245   current_dir = g_get_current_dir ();
4246   filename = g_build_filename (current_dir, arg, NULL);
4247   g_free (current_dir);
4248   
4249   file = g_file_new_for_path (filename);
4250   g_free (filename);
4251   
4252   return file;
4253 }
4254
4255 /**
4256  * g_file_mount_enclosing_volume:
4257  * @location: input #GFile.
4258  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4259  * @cancellable: optional #GCancellable object, %NULL to ignore.
4260  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4261  * @user_data: the data to pass to callback function
4262  * 
4263  * Starts a @mount_operation, mounting the volume that contains the file @location. 
4264  * 
4265  * When this operation has completed, @callback will be called with
4266  * @user_user data, and the operation can be finalized with 
4267  * g_file_mount_enclosing_volume_finish().
4268  * 
4269  * If @cancellable is not %NULL, then the operation can be cancelled by
4270  * triggering the cancellable object from another thread. If the operation
4271  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4272  **/
4273 void
4274 g_file_mount_enclosing_volume (GFile               *location,
4275                                GMountOperation     *mount_operation,
4276                                GCancellable        *cancellable,
4277                                GAsyncReadyCallback  callback,
4278                                gpointer             user_data)
4279 {
4280   GFileIface *iface;
4281
4282   g_return_if_fail (G_IS_FILE (location));
4283
4284   iface = G_FILE_GET_IFACE (location);
4285
4286   if (iface->mount_enclosing_volume == NULL)
4287     {
4288       g_simple_async_report_error_in_idle (G_OBJECT (location),
4289                                            callback, user_data,
4290                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4291                                            _("volume doesn't implement mount"));
4292       
4293       return;
4294     }
4295   
4296   (* iface->mount_enclosing_volume) (location, mount_operation, cancellable, callback, user_data);
4297
4298 }
4299
4300 /**
4301  * g_file_mount_enclosing_volume_finish:
4302  * @location: input #GFile.
4303  * @result: a #GAsyncResult.
4304  * @error: a #GError, or %NULL
4305  * 
4306  * Finishes a mount operation started by g_file_mount_enclosing_volume().
4307  * 
4308  * Returns: %TRUE if successful. If an error
4309  * has occurred, this function will return %FALSE and set @error
4310  * appropriately if present.
4311  **/
4312 gboolean
4313 g_file_mount_enclosing_volume_finish (GFile         *location,
4314                                       GAsyncResult  *result,
4315                                       GError       **error)
4316 {
4317   GFileIface *iface;
4318
4319   g_return_val_if_fail (G_IS_FILE (location), FALSE);
4320   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4321
4322   if (G_IS_SIMPLE_ASYNC_RESULT (result))
4323     {
4324       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4325       if (g_simple_async_result_propagate_error (simple, error))
4326         return FALSE;
4327     }
4328   
4329   iface = G_FILE_GET_IFACE (location);
4330
4331   return (* iface->mount_enclosing_volume_finish) (location, result, error);
4332 }
4333
4334 /********************************************
4335  *   Utility functions                      *
4336  ********************************************/
4337
4338 #define GET_CONTENT_BLOCK_SIZE 8192
4339
4340 /**
4341  * g_file_load_contents:
4342  * @file: input #GFile.
4343  * @cancellable: optional #GCancellable object, %NULL to ignore.
4344  * @contents: a location to place the contents of the file.
4345  * @length: a location to place the length of the contents of the file.
4346  * @etag_out: a location to place the current entity tag for the file.
4347  * @error: a #GError, or %NULL
4348  *
4349  * Loads the content of the file into memory, returning the size of
4350  * the data. The data is always zero terminated, but this is not
4351  * included in the resultant @length.
4352  * 
4353  * If @cancellable is not %NULL, then the operation can be cancelled by
4354  * triggering the cancellable object from another thread. If the operation
4355  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4356  * 
4357  * Returns: %TRUE if the @file's contents were successfully loaded.
4358  * %FALSE if there were errors..
4359  **/
4360 gboolean
4361 g_file_load_contents (GFile         *file,
4362                       GCancellable  *cancellable,
4363                       char         **contents,
4364                       gsize         *length,
4365                       char         **etag_out,
4366                       GError       **error)
4367 {
4368   GFileInputStream *in;
4369   GByteArray *content;
4370   gsize pos;
4371   gssize res;
4372   GFileInfo *info;
4373
4374   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4375   g_return_val_if_fail (contents != NULL, FALSE);
4376
4377   in = g_file_read (file, cancellable, error);
4378   if (in == NULL)
4379     return FALSE;
4380
4381   content = g_byte_array_new ();
4382   pos = 0;
4383   
4384   g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4385   while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4386                                      content->data + pos,
4387                                      GET_CONTENT_BLOCK_SIZE,
4388                                      cancellable, error)) > 0)
4389     {
4390       pos += res;
4391       g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4392     }
4393
4394   if (etag_out)
4395     {
4396       *etag_out = NULL;
4397       
4398       info = g_file_input_stream_query_info (in,
4399                                              G_FILE_ATTRIBUTE_ETAG_VALUE,
4400                                              cancellable,
4401                                              NULL);
4402       if (info)
4403         {
4404           *etag_out = g_strdup (g_file_info_get_etag (info));
4405           g_object_unref (info);
4406         }
4407     } 
4408
4409   /* Ignore errors on close */
4410   g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4411   g_object_unref (in);
4412
4413   if (res < 0)
4414     {
4415       /* error is set already */
4416       g_byte_array_free (content, TRUE);
4417       return FALSE;
4418     }
4419
4420   if (length)
4421     *length = pos;
4422
4423   /* Zero terminate (we got an extra byte allocated for this */
4424   content->data[pos] = 0;
4425   
4426   *contents = (char *)g_byte_array_free (content, FALSE);
4427   
4428   return TRUE;
4429 }
4430
4431 typedef struct {
4432   GFile *file;
4433   GError *error;
4434   GCancellable *cancellable;
4435   GFileReadMoreCallback read_more_callback;
4436   GAsyncReadyCallback callback;
4437   gpointer user_data;
4438   GByteArray *content;
4439   gsize pos;
4440   char *etag;
4441 } LoadContentsData;
4442
4443
4444 static void
4445 load_contents_data_free (LoadContentsData *data)
4446 {
4447   if (data->error)
4448     g_error_free (data->error);
4449   if (data->cancellable)
4450     g_object_unref (data->cancellable);
4451   if (data->content)
4452     g_byte_array_free (data->content, TRUE);
4453   g_free (data->etag);
4454   g_object_unref (data->file);
4455   g_free (data);
4456 }
4457
4458 static void
4459 load_contents_close_callback (GObject      *obj,
4460                               GAsyncResult *close_res,
4461                               gpointer      user_data)
4462 {
4463   GInputStream *stream = G_INPUT_STREAM (obj);
4464   LoadContentsData *data = user_data;
4465   GSimpleAsyncResult *res;
4466
4467   /* Ignore errors here, we're only reading anyway */
4468   g_input_stream_close_finish (stream, close_res, NULL);
4469   g_object_unref (stream);
4470
4471   res = g_simple_async_result_new (G_OBJECT (data->file),
4472                                    data->callback,
4473                                    data->user_data,
4474                                    g_file_load_contents_async);
4475   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4476   g_simple_async_result_complete (res);
4477   g_object_unref (res);
4478 }
4479
4480 static void
4481 load_contents_fstat_callback (GObject      *obj,
4482                               GAsyncResult *stat_res,
4483                               gpointer      user_data)
4484 {
4485   GInputStream *stream = G_INPUT_STREAM (obj);
4486   LoadContentsData *data = user_data;
4487   GFileInfo *info;
4488
4489   info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4490                                                    stat_res, NULL);
4491   if (info)
4492     {
4493       data->etag = g_strdup (g_file_info_get_etag (info));
4494       g_object_unref (info);
4495     }
4496
4497   g_input_stream_close_async (stream, 0,
4498                               data->cancellable,
4499                               load_contents_close_callback, data);
4500 }
4501
4502 static void
4503 load_contents_read_callback (GObject      *obj,
4504                              GAsyncResult *read_res,
4505                              gpointer      user_data)
4506 {
4507   GInputStream *stream = G_INPUT_STREAM (obj);
4508   LoadContentsData *data = user_data;
4509   GError *error = NULL;
4510   gssize read_size;
4511
4512   read_size = g_input_stream_read_finish (stream, read_res, &error);
4513
4514   if (read_size < 0) 
4515     {
4516       /* Error or EOF, close the file */
4517       data->error = error;
4518       g_input_stream_close_async (stream, 0,
4519                                   data->cancellable,
4520                                   load_contents_close_callback, data);
4521     }
4522   else if (read_size == 0)
4523     {
4524       g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4525                                             G_FILE_ATTRIBUTE_ETAG_VALUE,
4526                                             0,
4527                                             data->cancellable,
4528                                             load_contents_fstat_callback,
4529                                             data);
4530     }
4531   else if (read_size > 0)
4532     {
4533       data->pos += read_size;
4534       
4535       g_byte_array_set_size (data->content,
4536                              data->pos + GET_CONTENT_BLOCK_SIZE);
4537
4538
4539       if (data->read_more_callback &&
4540           !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4541         g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4542                                               G_FILE_ATTRIBUTE_ETAG_VALUE,
4543                                               0,
4544                                               data->cancellable,
4545                                               load_contents_fstat_callback,
4546                                               data);
4547       else 
4548         g_input_stream_read_async (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
4558 static void
4559 load_contents_open_callback (GObject      *obj,
4560                              GAsyncResult *open_res,
4561                              gpointer      user_data)
4562 {
4563   GFile *file = G_FILE (obj);
4564   GFileInputStream *stream;
4565   LoadContentsData *data = user_data;
4566   GError *error = NULL;
4567   GSimpleAsyncResult *res;
4568
4569   stream = g_file_read_finish (file, open_res, &error);
4570
4571   if (stream)
4572     {
4573       g_byte_array_set_size (data->content,
4574                              data->pos + GET_CONTENT_BLOCK_SIZE);
4575       g_input_stream_read_async (G_INPUT_STREAM (stream),
4576                                  data->content->data + data->pos,
4577                                  GET_CONTENT_BLOCK_SIZE,
4578                                  0,
4579                                  data->cancellable,
4580                                  load_contents_read_callback,
4581                                  data);
4582       
4583     }
4584   else
4585     {
4586       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4587                                                   data->callback,
4588                                                   data->user_data,
4589                                                   error);
4590       g_simple_async_result_complete (res);
4591       g_error_free (error);
4592       load_contents_data_free (data);
4593       g_object_unref (res);
4594     }
4595 }
4596
4597 /**
4598  * g_file_load_partial_contents_async:
4599  * @file: input #GFile.
4600  * @cancellable: optional #GCancellable object, %NULL to ignore.
4601  * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
4602  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4603  * @user_data: the data to pass to the callback functions.
4604  *
4605  * Reads the partial contents of a file. A #GFileReadMoreCallback should be 
4606  * used to stop reading from the file when appropriate, else this function
4607  * will behave exactly as g_file_load_contents_async(). This operation 
4608  * can be finished by g_file_load_partial_contents_finish().
4609  *
4610  * Users of this function should be aware that @user_data is passed to 
4611  * both the @read_more_callback and the @callback.
4612  *
4613  * If @cancellable is not %NULL, then the operation can be cancelled by
4614  * triggering the cancellable object from another thread. If the operation
4615  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4616  **/
4617 void
4618 g_file_load_partial_contents_async (GFile                 *file,
4619                                     GCancellable          *cancellable,
4620                                     GFileReadMoreCallback  read_more_callback,
4621                                     GAsyncReadyCallback    callback,
4622                                     gpointer               user_data)
4623 {
4624   LoadContentsData *data;
4625
4626   g_return_if_fail (G_IS_FILE (file));
4627
4628   data = g_new0 (LoadContentsData, 1);
4629
4630   if (cancellable)
4631     data->cancellable = g_object_ref (cancellable);
4632   data->read_more_callback = read_more_callback;
4633   data->callback = callback;
4634   data->user_data = user_data;
4635   data->content = g_byte_array_new ();
4636   data->file = g_object_ref (file);
4637
4638   g_file_read_async (file,
4639                      0,
4640                      cancellable,
4641                      load_contents_open_callback,
4642                      data);
4643 }
4644
4645 /**
4646  * g_file_load_partial_contents_finish:
4647  * @file: input #GFile.
4648  * @res: a #GAsyncResult. 
4649  * @contents: a location to place the contents of the file.
4650  * @length: a location to place the length of the contents of the file.
4651  * @etag_out: a location to place the current entity tag for the file.
4652  * @error: a #GError, or %NULL
4653  * 
4654  * Finishes an asynchronous partial load operation that was started
4655  * with g_file_load_partial_contents_async().
4656  *
4657  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
4658  * present, it will be set appropriately. 
4659  **/
4660 gboolean
4661 g_file_load_partial_contents_finish (GFile         *file,
4662                                      GAsyncResult  *res,
4663                                      char         **contents,
4664                                      gsize         *length,
4665                                      char         **etag_out,
4666                                      GError       **error)
4667 {
4668   GSimpleAsyncResult *simple;
4669   LoadContentsData *data;
4670
4671   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4672   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4673   g_return_val_if_fail (contents != NULL, FALSE);
4674
4675   simple = G_SIMPLE_ASYNC_RESULT (res);
4676
4677   if (g_simple_async_result_propagate_error (simple, error))
4678     return FALSE;
4679   
4680   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4681   
4682   data = g_simple_async_result_get_op_res_gpointer (simple);
4683
4684   if (data->error)
4685     {
4686       g_propagate_error (error, data->error);
4687       data->error = NULL;
4688       *contents = NULL;
4689       if (length)
4690         *length = 0;
4691       return FALSE;
4692     }
4693
4694   if (length)
4695     *length = data->pos;
4696
4697   if (etag_out)
4698     {
4699       *etag_out = data->etag;
4700       data->etag = NULL;
4701     }
4702
4703   /* Zero terminate */
4704   g_byte_array_set_size (data->content, data->pos + 1);
4705   data->content->data[data->pos] = 0;
4706   
4707   *contents = (char *)g_byte_array_free (data->content, FALSE);
4708   data->content = NULL;
4709
4710   return TRUE;
4711 }
4712
4713 /**
4714  * g_file_load_contents_async:
4715  * @file: input #GFile.
4716  * @cancellable: optional #GCancellable object, %NULL to ignore.
4717  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4718  * @user_data: the data to pass to callback function
4719  * 
4720  * Starts an asynchronous load of the @file's contents.
4721  *
4722  * For more details, see g_file_load_contents() which is
4723  * the synchronous version of this call.
4724  *
4725  * When the load operation has completed, @callback will be called 
4726  * with @user data. To finish the operation, call 
4727  * g_file_load_contents_finish() with the #GAsyncResult returned by 
4728  * the @callback.
4729  * 
4730  * If @cancellable is not %NULL, then the operation can be cancelled by
4731  * triggering the cancellable object from another thread. If the operation
4732  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4733  **/
4734 void
4735 g_file_load_contents_async (GFile               *file,
4736                            GCancellable        *cancellable,
4737                            GAsyncReadyCallback  callback,
4738                            gpointer             user_data)
4739 {
4740   g_file_load_partial_contents_async (file,
4741                                       cancellable,
4742                                       NULL,
4743                                       callback, user_data);
4744 }
4745
4746 /**
4747  * g_file_load_contents_finish:
4748  * @file: input #GFile.
4749  * @res: a #GAsyncResult. 
4750  * @contents: a location to place the contents of the file.
4751  * @length: a location to place the length of the contents of the file.
4752  * @etag_out: a location to place the current entity tag for the file.
4753  * @error: a #GError, or %NULL
4754  * 
4755  * Finishes an asynchronous load of the @file's contents. 
4756  * The contents are placed in @contents, and @length is set to the 
4757  * size of the @contents string. If @etag_out is present, it will be 
4758  * set to the new entity tag for the @file.
4759  * 
4760  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
4761  * present, it will be set appropriately. 
4762  **/
4763 gboolean
4764 g_file_load_contents_finish (GFile         *file,
4765                              GAsyncResult  *res,
4766                              char         **contents,
4767                              gsize         *length,
4768                              char         **etag_out,
4769                              GError       **error)
4770 {
4771   return g_file_load_partial_contents_finish (file,
4772                                               res,
4773                                               contents,
4774                                               length,
4775                                               etag_out,
4776                                               error);
4777 }
4778   
4779 /**
4780  * g_file_replace_contents:
4781  * @file: input #GFile.
4782  * @contents: a string containing the new contents for @file.
4783  * @length: the length of @contents in bytes.
4784  * @etag: the old <link linkend="gfile-etag">entity tag</link> 
4785  *     for the document.
4786  * @make_backup: %TRUE if a backup should be created.
4787  * @flags: a set of #GFileCreateFlags.
4788  * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4789  *      for the document.
4790  * @cancellable: optional #GCancellable object, %NULL to ignore.
4791  * @error: a #GError, or %NULL
4792  *
4793  * Replaces the contents of @file with @contents of @length bytes.
4794  
4795  * If @etag is specified (not %NULL) any existing file must have that etag, or
4796  * the error %G_IO_ERROR_WRONG_ETAG will be returned.
4797  *
4798  * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
4799  * 
4800  * If @cancellable is not %NULL, then the operation can be cancelled by
4801  * triggering the cancellable object from another thread. If the operation
4802  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4803  *
4804  * The returned @new_etag can be used to verify that the file hasn't changed the
4805  * next time it is saved over.
4806  * 
4807  * Returns: %TRUE if successful. If an error
4808  * has occurred, this function will return %FALSE and set @error
4809  * appropriately if present.
4810  **/
4811 gboolean
4812 g_file_replace_contents (GFile             *file,
4813                          const char        *contents,
4814                          gsize              length,
4815                          const char        *etag,
4816                          gboolean           make_backup,
4817                          GFileCreateFlags   flags,
4818                          char             **new_etag,
4819                          GCancellable      *cancellable,
4820                          GError           **error)
4821 {
4822   GFileOutputStream *out;
4823   gsize pos, remainder;
4824   gssize res;
4825
4826   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4827   g_return_val_if_fail (contents != NULL, FALSE);
4828
4829   out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4830   if (out == NULL)
4831     return FALSE;
4832
4833   pos = 0;
4834   remainder = length;
4835   while (remainder > 0 &&
4836          (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4837                                        contents + pos,
4838                                        MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4839                                        cancellable,
4840                                        error)) > 0)
4841     {
4842       pos += res;
4843       remainder -= res;
4844     }
4845   
4846   if (remainder > 0 && res < 0)
4847     {
4848       /* Ignore errors on close */
4849       g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4850       
4851       /* error is set already */
4852       return FALSE;
4853     }
4854   
4855   if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4856     return FALSE;
4857
4858   if (new_etag)
4859     *new_etag = g_file_output_stream_get_etag (out);
4860   
4861   return TRUE;
4862 }
4863
4864 typedef struct {
4865   GFile *file;
4866   GError *error;
4867   GCancellable *cancellable;
4868   GAsyncReadyCallback callback;
4869   gpointer user_data;
4870   const char *content;
4871   gsize length;
4872   gsize pos;
4873   char *etag;
4874 } ReplaceContentsData;
4875
4876 static void
4877 replace_contents_data_free (ReplaceContentsData *data)
4878 {
4879   if (data->error)
4880     g_error_free (data->error);
4881   if (data->cancellable)
4882     g_object_unref (data->cancellable);
4883   g_object_unref (data->file);
4884   g_free (data->etag);
4885   g_free (data);
4886 }
4887
4888 static void
4889 replace_contents_close_callback (GObject      *obj,
4890                                  GAsyncResult *close_res,
4891                                  gpointer      user_data)
4892 {
4893   GOutputStream *stream = G_OUTPUT_STREAM (obj);
4894   ReplaceContentsData *data = user_data;
4895   GSimpleAsyncResult *res;
4896
4897   /* Ignore errors here, we're only reading anyway */
4898   g_output_stream_close_finish (stream, close_res, NULL);
4899   g_object_unref (stream);
4900
4901   data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4902   
4903   res = g_simple_async_result_new (G_OBJECT (data->file),
4904                                    data->callback,
4905                                    data->user_data,
4906                                    g_file_replace_contents_async);
4907   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4908   g_simple_async_result_complete (res);
4909   g_object_unref (res);
4910 }
4911
4912 static void
4913 replace_contents_write_callback (GObject      *obj,
4914                                  GAsyncResult *read_res,
4915                                  gpointer      user_data)
4916 {
4917   GOutputStream *stream = G_OUTPUT_STREAM (obj);
4918   ReplaceContentsData *data = user_data;
4919   GError *error = NULL;
4920   gssize write_size;
4921   
4922   write_size = g_output_stream_write_finish (stream, read_res, &error);
4923
4924   if (write_size <= 0) 
4925     {
4926       /* Error or EOF, close the file */
4927       if (write_size < 0)
4928         data->error = error;
4929       g_output_stream_close_async (stream, 0,
4930                                    data->cancellable,
4931                                    replace_contents_close_callback, data);
4932     }
4933   else if (write_size > 0)
4934     {
4935       data->pos += write_size;
4936
4937       if (data->pos >= data->length)
4938         g_output_stream_close_async (stream, 0,
4939                                      data->cancellable,
4940                                      replace_contents_close_callback, data);
4941       else
4942         g_output_stream_write_async (stream,
4943                                      data->content + data->pos,
4944                                      data->length - data->pos,
4945                                      0,
4946                                      data->cancellable,
4947                                      replace_contents_write_callback,
4948                                      data);
4949     }
4950 }
4951
4952 static void
4953 replace_contents_open_callback (GObject      *obj,
4954                                 GAsyncResult *open_res,
4955                                 gpointer      user_data)
4956 {
4957   GFile *file = G_FILE (obj);
4958   GFileOutputStream *stream;
4959   ReplaceContentsData *data = user_data;
4960   GError *error = NULL;
4961   GSimpleAsyncResult *res;
4962
4963   stream = g_file_replace_finish (file, open_res, &error);
4964
4965   if (stream)
4966     {
4967       g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4968                                    data->content + data->pos,
4969                                    data->length - data->pos,
4970                                    0,
4971                                    data->cancellable,
4972                                    replace_contents_write_callback,
4973                                    data);
4974       
4975     }
4976   else
4977     {
4978       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4979                                                   data->callback,
4980                                                   data->user_data,
4981                                                   error);
4982       g_simple_async_result_complete (res);
4983       g_error_free (error);
4984       replace_contents_data_free (data);
4985       g_object_unref (res);
4986     }
4987 }
4988
4989 /**
4990  * g_file_replace_contents_async:
4991  * @file: input #GFile.
4992  * @contents: string of contents to replace the file with.
4993  * @length: the length of @contents in bytes.
4994  * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4995  * @make_backup: %TRUE if a backup should be created.
4996  * @flags: a set of #GFileCreateFlags.
4997  * @cancellable: optional #GCancellable object, %NULL to ignore.
4998  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4999  * @user_data: the data to pass to callback function
5000  * 
5001  * Starts an asynchronous replacement of @file with the given 
5002  * @contents of @length bytes. @etag will replace the document's 
5003  * current entity tag.
5004  * 
5005  * When this operation has completed, @callback will be called with
5006  * @user_user data, and the operation can be finalized with 
5007  * g_file_replace_contents_finish().
5008  * 
5009  * If @cancellable is not %NULL, then the operation can be cancelled by
5010  * triggering the cancellable object from another thread. If the operation
5011  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5012  * 
5013  * If @make_backup is %TRUE, this function will attempt to 
5014  * make a backup of @file.
5015  **/
5016 void
5017 g_file_replace_contents_async  (GFile               *file,
5018                                 const char          *contents,
5019                                 gsize                length,
5020                                 const char          *etag,
5021                                 gboolean             make_backup,
5022                                 GFileCreateFlags     flags,
5023                                 GCancellable        *cancellable,
5024                                 GAsyncReadyCallback  callback,
5025                                 gpointer             user_data)
5026 {
5027   ReplaceContentsData *data;
5028
5029   g_return_if_fail (G_IS_FILE (file));
5030   g_return_if_fail (contents != NULL);
5031
5032   data = g_new0 (ReplaceContentsData, 1);
5033
5034   if (cancellable)
5035     data->cancellable = g_object_ref (cancellable);
5036   data->callback = callback;
5037   data->user_data = user_data;
5038   data->content = contents;
5039   data->length = length;
5040   data->pos = 0;
5041   data->file = g_object_ref (file);
5042
5043   g_file_replace_async (file,
5044                         etag,
5045                         make_backup,
5046                         flags,
5047                         0,
5048                         cancellable,
5049                         replace_contents_open_callback,
5050                         data);
5051 }
5052   
5053 /**
5054  * g_file_replace_contents_finish:
5055  * @file: input #GFile.
5056  * @res: a #GAsyncResult. 
5057  * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link> 
5058  *     for the document.
5059  * @error: a #GError, or %NULL 
5060  * 
5061  * Finishes an asynchronous replace of the given @file. See
5062  * g_file_replace_contents_async(). Sets @new_etag to the new entity 
5063  * tag for the document, if present.
5064  * 
5065  * Returns: %TRUE on success, %FALSE on failure.
5066  **/
5067 gboolean
5068 g_file_replace_contents_finish (GFile         *file,
5069                                 GAsyncResult  *res,
5070                                 char         **new_etag,
5071                                 GError       **error)
5072 {
5073   GSimpleAsyncResult *simple;
5074   ReplaceContentsData *data;
5075
5076   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5077   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5078
5079   simple = G_SIMPLE_ASYNC_RESULT (res);
5080
5081   if (g_simple_async_result_propagate_error (simple, error))
5082     return FALSE;
5083   
5084   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5085   
5086   data = g_simple_async_result_get_op_res_gpointer (simple);
5087
5088   if (data->error)
5089     {
5090       g_propagate_error (error, data->error);
5091       data->error = NULL;
5092       return FALSE;
5093     }
5094
5095
5096   if (new_etag)
5097     {
5098       *new_etag = data->etag;
5099       data->etag = NULL; /* Take ownership */
5100     }
5101   
5102   return TRUE;
5103 }
5104
5105 #define __G_FILE_C__
5106 #include "gioaliasdef.c"