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