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