Bug 547080 – g_file_copy leaks expected errors
[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 {
2061   if (as_move)
2062     return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2063   return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2064 }
2065
2066 static char *
2067 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2068                                GFileAttributeInfoList *namespaces,
2069                                gboolean                as_move)
2070 {
2071   GString *s;
2072   gboolean first;
2073   int i;
2074   
2075   first = TRUE;
2076   s = g_string_new ("");
2077
2078   if (attributes)
2079     {
2080       for (i = 0; i < attributes->n_infos; i++)
2081         {
2082           if (should_copy (&attributes->infos[i], as_move))
2083             {
2084               if (first)
2085                 first = FALSE;
2086               else
2087                 g_string_append_c (s, ',');
2088                 
2089               g_string_append (s, attributes->infos[i].name);
2090             }
2091         }
2092     }
2093
2094   if (namespaces)
2095     {
2096       for (i = 0; i < namespaces->n_infos; i++)
2097         {
2098           if (should_copy (&namespaces->infos[i], as_move))
2099             {
2100               if (first)
2101                 first = FALSE;
2102               else
2103                 g_string_append_c (s, ',');
2104                 
2105               g_string_append (s, namespaces->infos[i].name);
2106               g_string_append (s, ":*");
2107             }
2108         }
2109     }
2110
2111   return g_string_free (s, FALSE);
2112 }
2113
2114 /**
2115  * g_file_copy_attributes:
2116  * @source: a #GFile with attributes.
2117  * @destination: a #GFile to copy attributes to.
2118  * @flags: a set of #GFileCopyFlags.
2119  * @cancellable: optional #GCancellable object, %NULL to ignore.
2120  * @error: a #GError, %NULL to ignore.
2121  *
2122  * Copies the file attributes from @source to @destination. 
2123  *
2124  * Normally only a subset of the file attributes are copied,
2125  * those that are copies in a normal file copy operation
2126  * (which for instance does not include e.g. mtime). However
2127  * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2128  * all the metadata that is possible to copy is copied.
2129  *
2130  * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2131  **/
2132 gboolean
2133 g_file_copy_attributes (GFile           *source,
2134                         GFile           *destination,
2135                         GFileCopyFlags   flags,
2136                         GCancellable    *cancellable,
2137                         GError         **error)
2138 {
2139   GFileAttributeInfoList *attributes, *namespaces;
2140   char *attrs_to_read;
2141   gboolean res;
2142   GFileInfo *info;
2143   gboolean as_move;
2144   gboolean source_nofollow_symlinks;
2145
2146   as_move = flags & G_FILE_COPY_ALL_METADATA;
2147   source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2148
2149   /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2150   attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2151   namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2152
2153   if (attributes == NULL && namespaces == NULL)
2154     return TRUE;
2155
2156   attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2157
2158   /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2159    * we just don't copy it. 
2160    */
2161   info = g_file_query_info (source, attrs_to_read,
2162                             source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2163                             cancellable,
2164                             NULL);
2165
2166   g_free (attrs_to_read);
2167   
2168   res = TRUE;
2169   if  (info)
2170     {
2171       res = g_file_set_attributes_from_info (destination,
2172                                              info,
2173                          G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2174                                              cancellable,
2175                                              error);
2176       g_object_unref (info);
2177     }
2178   
2179   g_file_attribute_info_list_unref (attributes);
2180   g_file_attribute_info_list_unref (namespaces);
2181   
2182   return res;
2183 }
2184
2185 /* Closes the streams */
2186 static gboolean
2187 copy_stream_with_progress (GInputStream           *in,
2188                            GOutputStream          *out,
2189                            GFile                  *source,
2190                            GCancellable           *cancellable,
2191                            GFileProgressCallback   progress_callback,
2192                            gpointer                progress_callback_data,
2193                            GError                **error)
2194 {
2195   gssize n_read, n_written;
2196   goffset current_size;
2197   char buffer[1024*64], *p;
2198   gboolean res;
2199   goffset total_size;
2200   GFileInfo *info;
2201
2202   total_size = -1;
2203   info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2204                                          G_FILE_ATTRIBUTE_STANDARD_SIZE,
2205                                          cancellable, NULL);
2206   if (info)
2207     {
2208       if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2209         total_size = g_file_info_get_size (info);
2210       g_object_unref (info);
2211     }
2212
2213   if (total_size == -1)
2214     {
2215       info = g_file_query_info (source, 
2216                                 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2217                                 G_FILE_QUERY_INFO_NONE,
2218                                 cancellable, NULL);
2219       if (info)
2220         {
2221           if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2222             total_size = g_file_info_get_size (info);
2223           g_object_unref (info);
2224         }
2225     }
2226
2227   if (total_size == -1)
2228     total_size = 0;
2229   
2230   current_size = 0;
2231   res = TRUE;
2232   while (TRUE)
2233     {
2234       n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2235       if (n_read == -1)
2236         {
2237           res = FALSE;
2238           break;
2239         }
2240         
2241       if (n_read == 0)
2242         break;
2243
2244       current_size += n_read;
2245
2246       p = buffer;
2247       while (n_read > 0)
2248         {
2249           n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2250           if (n_written == -1)
2251             {
2252               res = FALSE;
2253               break;
2254             }
2255
2256           p += n_written;
2257           n_read -= n_written;
2258         }
2259
2260       if (!res)
2261         break;
2262
2263       if (progress_callback)
2264         progress_callback (current_size, total_size, progress_callback_data);
2265     }
2266
2267   if (!res)
2268     error = NULL; /* Ignore further errors */
2269
2270   /* Make sure we send full copied size */
2271   if (progress_callback)
2272     progress_callback (current_size, total_size, progress_callback_data);
2273   
2274   /* Don't care about errors in source here */
2275   g_input_stream_close (in, cancellable, NULL);
2276
2277   /* But write errors on close are bad! */
2278   if (!g_output_stream_close (out, cancellable, error))
2279     res = FALSE;
2280
2281   g_object_unref (in);
2282   g_object_unref (out);
2283       
2284   return res;
2285 }
2286
2287 static gboolean
2288 file_copy_fallback (GFile                  *source,
2289                     GFile                  *destination,
2290                     GFileCopyFlags          flags,
2291                     GCancellable           *cancellable,
2292                     GFileProgressCallback   progress_callback,
2293                     gpointer                progress_callback_data,
2294                     GError                **error)
2295 {
2296   GInputStream *in;
2297   GOutputStream *out;
2298   GFileInfo *info;
2299   const char *target;
2300
2301   /* Maybe copy the symlink? */
2302   if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2303     {
2304       info = g_file_query_info (source,
2305                                 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2306                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2307                                 cancellable,
2308                                 error);
2309       if (info == NULL)
2310         return FALSE;
2311
2312       if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2313           (target = g_file_info_get_symlink_target (info)) != NULL)
2314         {
2315           if (!copy_symlink (destination, flags, cancellable, target, error))
2316             {
2317               g_object_unref (info);
2318               return FALSE;
2319             }
2320           
2321           g_object_unref (info);
2322           goto copied_file;
2323         }
2324       
2325       g_object_unref (info);
2326     }
2327   
2328   in = open_source_for_copy (source, destination, flags, cancellable, error);
2329   if (in == NULL)
2330     return FALSE;
2331   
2332   if (flags & G_FILE_COPY_OVERWRITE)
2333     {
2334       out = (GOutputStream *)g_file_replace (destination,
2335                                              NULL,
2336                                              flags & G_FILE_COPY_BACKUP,
2337                                              0,
2338                                              cancellable, error);
2339     }
2340   else
2341     {
2342       out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2343     }
2344
2345   if (out == NULL)
2346     {
2347       g_object_unref (in);
2348       return FALSE;
2349     }
2350
2351   if (!copy_stream_with_progress (in, out, source, cancellable,
2352                                   progress_callback, progress_callback_data,
2353                                   error))
2354     return FALSE;
2355
2356  copied_file:
2357
2358   /* Ignore errors here. Failure to copy metadata is not a hard error */
2359   g_file_copy_attributes (source, destination,
2360                           flags, cancellable, NULL);
2361   
2362   return TRUE;
2363 }
2364
2365 /**
2366  * g_file_copy:
2367  * @source: input #GFile.
2368  * @destination: destination #GFile
2369  * @flags: set of #GFileCopyFlags
2370  * @cancellable: optional #GCancellable object, %NULL to ignore.
2371  * @progress_callback: function to callback with progress information
2372  * @progress_callback_data: user data to pass to @progress_callback
2373  * @error: #GError to set on error, or %NULL
2374  *
2375  * Copies the file @source to the location specified by @destination.
2376  * Can not handle recursive copies of directories.
2377  *
2378  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2379  * existing @destination file is overwritten.
2380  *
2381  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2382  * will be copied as symlinks, otherwise the target of the
2383  * @source symlink will be copied.
2384  *
2385  * If @cancellable is not %NULL, then the operation can be cancelled by
2386  * triggering the cancellable object from another thread. If the operation
2387  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2388  * 
2389  * If @progress_callback is not %NULL, then the operation can be monitored by
2390  * setting this to a #GFileProgressCallback function. @progress_callback_data
2391  * will be passed to this function. It is guaranteed that this callback will
2392  * be called after all data has been transferred with the total number of bytes
2393  * copied during the operation.
2394  * 
2395  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2396  * error is returned, independent on the status of the @destination.
2397  *
2398  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2399  * error G_IO_ERROR_EXISTS is returned.
2400  *
2401  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2402  * error is returned. If trying to overwrite a directory with a directory the
2403  * G_IO_ERROR_WOULD_MERGE error is returned.
2404  *
2405  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2406  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2407  * is returned.
2408  *
2409  * If you are interested in copying the #GFile object itself (not the on-disk
2410  * file), see g_file_dup().
2411  *
2412  * Returns: %TRUE on success, %FALSE otherwise.
2413  **/
2414 gboolean
2415 g_file_copy (GFile                  *source,
2416              GFile                  *destination,
2417              GFileCopyFlags          flags,
2418              GCancellable           *cancellable,
2419              GFileProgressCallback   progress_callback,
2420              gpointer                progress_callback_data,
2421              GError                **error)
2422 {
2423   GFileIface *iface;
2424   GError *my_error;
2425   gboolean res;
2426
2427   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2428   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2429
2430   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2431     return FALSE;
2432   
2433   iface = G_FILE_GET_IFACE (destination);
2434   if (iface->copy)
2435     {
2436       my_error = NULL;
2437       res = (* iface->copy) (source, destination,
2438                              flags, cancellable,
2439                              progress_callback, progress_callback_data,
2440                              &my_error);
2441       
2442       if (res)
2443         return TRUE;
2444       
2445       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2446         {
2447           g_propagate_error (error, my_error);
2448               return FALSE;
2449         }
2450       else
2451         g_clear_error (&my_error);
2452     }
2453
2454   /* If the types are different, and the destination method failed
2455      also try the source method */
2456   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2457     {
2458       iface = G_FILE_GET_IFACE (source);
2459       
2460       if (iface->copy)
2461         {
2462           my_error = NULL;
2463           res = (* iface->copy) (source, destination,
2464                                  flags, cancellable,
2465                                  progress_callback, progress_callback_data,
2466                                  &my_error);
2467           
2468           if (res)
2469             return TRUE;
2470           
2471           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2472             {
2473               g_propagate_error (error, my_error);
2474               return FALSE;
2475             }
2476           else
2477             g_clear_error (&my_error);
2478         }
2479     }
2480   
2481   return file_copy_fallback (source, destination, flags, cancellable,
2482                              progress_callback, progress_callback_data,
2483                              error);
2484 }
2485
2486 /**
2487  * g_file_copy_async:
2488  * @source: input #GFile.
2489  * @destination: destination #GFile
2490  * @flags: set of #GFileCopyFlags
2491  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2492  *     of the request. 
2493  * @cancellable: optional #GCancellable object, %NULL to ignore.
2494  * @progress_callback: function to callback with progress information
2495  * @progress_callback_data: user data to pass to @progress_callback
2496  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2497  * @user_data: the data to pass to callback function
2498  *
2499  * Copies the file @source to the location specified by @destination 
2500  * asynchronously. For details of the behaviour, see g_file_copy().
2501  *
2502  * If @progress_callback is not %NULL, then that function that will be called
2503  * just like in g_file_copy(), however the callback will run in the main loop,
2504  * not in the thread that is doing the I/O operation.
2505  *
2506  * When the operation is finished, @callback will be called. You can then call
2507  * g_file_copy_finish() to get the result of the operation.
2508  **/
2509 void
2510 g_file_copy_async (GFile                  *source,
2511                    GFile                  *destination,
2512                    GFileCopyFlags          flags,
2513                    int                     io_priority,
2514                    GCancellable           *cancellable,
2515                    GFileProgressCallback   progress_callback,
2516                    gpointer                progress_callback_data,
2517                    GAsyncReadyCallback     callback,
2518                    gpointer                user_data)
2519 {
2520   GFileIface *iface;
2521
2522   g_return_if_fail (G_IS_FILE (source));
2523   g_return_if_fail (G_IS_FILE (destination));
2524
2525   iface = G_FILE_GET_IFACE (source);
2526   (* iface->copy_async) (source,
2527                          destination,
2528                          flags,
2529                          io_priority,
2530                          cancellable,
2531                          progress_callback,
2532                          progress_callback_data,
2533                          callback,
2534                          user_data);
2535 }
2536
2537 /**
2538  * g_file_copy_finish:
2539  * @file: input #GFile.
2540  * @res: a #GAsyncResult. 
2541  * @error: a #GError, or %NULL
2542  * 
2543  * Finishes copying the file started with 
2544  * g_file_copy_async().
2545  * 
2546  * Returns: a %TRUE on success, %FALSE on error.
2547  **/
2548 gboolean
2549 g_file_copy_finish (GFile        *file,
2550                     GAsyncResult *res,
2551                     GError      **error)
2552 {
2553   GFileIface *iface;
2554   
2555   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2556   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2557
2558   if (G_IS_SIMPLE_ASYNC_RESULT (res))
2559     {
2560       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2561       
2562       if (g_simple_async_result_propagate_error (simple, error))
2563         return FALSE;
2564     }
2565   
2566   iface = G_FILE_GET_IFACE (file);
2567   return (* iface->copy_finish) (file, res, error);
2568 }
2569
2570 /**
2571  * g_file_move:
2572  * @source: #GFile pointing to the source location.
2573  * @destination: #GFile pointing to the destination location.
2574  * @flags: set of #GFileCopyFlags.
2575  * @cancellable: optional #GCancellable object, %NULL to ignore.
2576  * @progress_callback: #GFileProgressCallback function for updates.
2577  * @progress_callback_data: gpointer to user data for the callback function.
2578  * @error: #GError for returning error conditions, or %NULL
2579  *
2580  *
2581  * Tries to move the file or directory @source to the location specified by @destination.
2582  * If native move operations are supported then this is used, otherwise a copy + delete
2583  * fallback is used. The native implementation may support moving directories (for instance
2584  * on moves inside the same filesystem), but the fallback code does not.
2585  * 
2586  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2587  * existing @destination file is overwritten.
2588  *
2589  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2590  * will be copied as symlinks, otherwise the target of the
2591  * @source symlink will be copied.
2592  *
2593  * If @cancellable is not %NULL, then the operation can be cancelled by
2594  * triggering the cancellable object from another thread. If the operation
2595  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2596  * 
2597  * If @progress_callback is not %NULL, then the operation can be monitored by
2598  * setting this to a #GFileProgressCallback function. @progress_callback_data
2599  * will be passed to this function. It is guaranteed that this callback will
2600  * be called after all data has been transferred with the total number of bytes
2601  * copied during the operation.
2602  * 
2603  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2604  * error is returned, independent on the status of the @destination.
2605  *
2606  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2607  * error G_IO_ERROR_EXISTS is returned.
2608  *
2609  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2610  * error is returned. If trying to overwrite a directory with a directory the
2611  * G_IO_ERROR_WOULD_MERGE error is returned.
2612  *
2613  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2614  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2615  * may be returned (if the native move operation isn't available).
2616  *
2617  * Returns: %TRUE on successful move, %FALSE otherwise.
2618  **/
2619 gboolean
2620 g_file_move (GFile                  *source,
2621              GFile                  *destination,
2622              GFileCopyFlags          flags,
2623              GCancellable           *cancellable,
2624              GFileProgressCallback   progress_callback,
2625              gpointer                progress_callback_data,
2626              GError                **error)
2627 {
2628   GFileIface *iface;
2629   GError *my_error;
2630   gboolean res;
2631
2632   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2633   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2634
2635   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2636     return FALSE;
2637   
2638   iface = G_FILE_GET_IFACE (destination);
2639   if (iface->move)
2640     {
2641       my_error = NULL;
2642       res = (* iface->move) (source, destination,
2643                              flags, cancellable,
2644                              progress_callback, progress_callback_data,
2645                              &my_error);
2646       
2647       if (res)
2648         return TRUE;
2649       
2650       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2651         {
2652           g_propagate_error (error, my_error);
2653           return FALSE;
2654         }
2655     }
2656
2657   /* If the types are different, and the destination method failed
2658      also try the source method */
2659   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2660     {
2661       iface = G_FILE_GET_IFACE (source);
2662       
2663       if (iface->move)
2664         {
2665           my_error = NULL;
2666           res = (* iface->move) (source, destination,
2667                                  flags, cancellable,
2668                                  progress_callback, progress_callback_data,
2669                                  &my_error);
2670           
2671           if (res)
2672             return TRUE;
2673           
2674           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2675             {
2676               g_propagate_error (error, my_error);
2677               return FALSE;
2678             }
2679         }
2680     }
2681   
2682   if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2683     {  
2684       g_set_error_literal (error, G_IO_ERROR,
2685                            G_IO_ERROR_NOT_SUPPORTED,
2686                            _("Operation not supported"));
2687       return FALSE;
2688     }
2689   
2690   flags |= G_FILE_COPY_ALL_METADATA;
2691   if (!g_file_copy (source, destination, flags, cancellable,
2692                     progress_callback, progress_callback_data,
2693                     error))
2694     return FALSE;
2695
2696   return g_file_delete (source, cancellable, error);
2697 }
2698
2699 /**
2700  * g_file_make_directory
2701  * @file: input #GFile.
2702  * @cancellable: optional #GCancellable object, %NULL to ignore.
2703  * @error: a #GError, or %NULL 
2704  *
2705  * Creates a directory. Note that this will only create a child directory of
2706  * the immediate parent directory of the path or URI given by the #GFile. To 
2707  * recursively create directories, see g_file_make_directory_with_parents().
2708  * This function will fail if the parent directory does not exist, setting 
2709  * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
2710  * directories, this function will fail, setting @error to 
2711  * %G_IO_ERROR_NOT_SUPPORTED.
2712  *
2713  * For a local #GFile the newly created directory will have the default
2714  * (current) ownership and permissions of the current process.
2715  * 
2716  * If @cancellable is not %NULL, then the operation can be cancelled by
2717  * triggering the cancellable object from another thread. If the operation
2718  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2719  * 
2720  * Returns: %TRUE on successful creation, %FALSE otherwise.
2721  **/
2722 gboolean
2723 g_file_make_directory (GFile         *file,
2724                        GCancellable  *cancellable,
2725                        GError       **error)
2726 {
2727   GFileIface *iface;
2728
2729   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2730
2731   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2732     return FALSE;
2733   
2734   iface = G_FILE_GET_IFACE (file);
2735
2736   if (iface->make_directory == NULL)
2737     {
2738       g_set_error_literal (error, G_IO_ERROR,
2739                            G_IO_ERROR_NOT_SUPPORTED,
2740                            _("Operation not supported"));
2741       return FALSE;
2742     }
2743   
2744   return (* iface->make_directory) (file, cancellable, error);
2745 }
2746
2747 /**
2748  * g_file_make_directory_with_parents:
2749  * @file: input #GFile.
2750  * @cancellable: optional #GCancellable object, %NULL to ignore.
2751  * @error: a #GError, or %NULL 
2752  *
2753  * Creates a directory and any parent directories that may not exist similar to
2754  * 'mkdir -p'. If the file system does not support creating directories, this
2755  * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED.
2756  * 
2757  * For a local #GFile the newly created directories will have the default
2758  * (current) ownership and permissions of the current process.
2759  * 
2760  * If @cancellable is not %NULL, then the operation can be cancelled by
2761  * triggering the cancellable object from another thread. If the operation
2762  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2763  * 
2764  * Returns: %TRUE if all directories have been successfully created, %FALSE
2765  * otherwise.
2766  *
2767  * Since: 2.18
2768  **/
2769 gboolean
2770 g_file_make_directory_with_parents (GFile         *file,
2771                                     GCancellable  *cancellable,
2772                                     GError       **error)
2773 {
2774   gboolean result;
2775   GFile *parent_file, *work_file;
2776   GList *list = NULL, *l;
2777   GError *my_error = NULL;
2778
2779   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2780     return FALSE;
2781   
2782   result = g_file_make_directory (file, cancellable, &my_error);
2783   if (result || my_error->code != G_IO_ERROR_NOT_FOUND) 
2784     {
2785       if (my_error)
2786         g_propagate_error (error, my_error);
2787       return result;
2788     }
2789   
2790   work_file = file;
2791   
2792   while (!result && my_error->code == G_IO_ERROR_NOT_FOUND) 
2793     {
2794       g_clear_error (&my_error);
2795     
2796       parent_file = g_file_get_parent (work_file);
2797       if (parent_file == NULL)
2798         break;
2799       result = g_file_make_directory (parent_file, cancellable, &my_error);
2800     
2801       if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2802         list = g_list_prepend (list, parent_file);
2803
2804       work_file = parent_file;
2805     }
2806
2807   for (l = list; result && l; l = l->next)
2808     {
2809       result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
2810     }
2811   
2812   /* Clean up */
2813   while (list != NULL) 
2814     {
2815       g_object_unref ((GFile *) list->data);
2816       list = g_list_remove (list, list->data);
2817     }
2818
2819   if (!result) 
2820     {
2821       g_propagate_error (error, my_error);
2822       return result;
2823     }
2824   
2825   return g_file_make_directory (file, cancellable, error);
2826 }
2827
2828 /**
2829  * g_file_make_symbolic_link:
2830  * @file: input #GFile.
2831  * @symlink_value: a string with the value of the new symlink.
2832  * @cancellable: optional #GCancellable object, %NULL to ignore.
2833  * @error: a #GError. 
2834  * 
2835  * Creates a symbolic link.
2836  *
2837  * If @cancellable is not %NULL, then the operation can be cancelled by
2838  * triggering the cancellable object from another thread. If the operation
2839  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2840  * 
2841  * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2842  **/
2843 gboolean
2844 g_file_make_symbolic_link (GFile         *file,
2845                            const char    *symlink_value,
2846                            GCancellable  *cancellable,
2847                            GError       **error)
2848 {
2849   GFileIface *iface;
2850
2851   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2852   g_return_val_if_fail (symlink_value != NULL, FALSE);
2853
2854   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2855     return FALSE;
2856
2857   if (*symlink_value == '\0')
2858     {
2859       g_set_error_literal (error, G_IO_ERROR,
2860                            G_IO_ERROR_INVALID_ARGUMENT,
2861                            _("Invalid symlink value given"));
2862       return FALSE;
2863     }
2864   
2865   iface = G_FILE_GET_IFACE (file);
2866
2867   if (iface->make_symbolic_link == NULL)
2868     {
2869       g_set_error_literal (error, G_IO_ERROR,
2870                            G_IO_ERROR_NOT_SUPPORTED,
2871                            _("Operation not supported"));
2872       return FALSE;
2873     }
2874   
2875   return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2876 }
2877
2878 /**
2879  * g_file_delete:
2880  * @file: input #GFile.
2881  * @cancellable: optional #GCancellable object, %NULL to ignore.
2882  * @error: a #GError, or %NULL 
2883  * 
2884  * Deletes a file. If the @file is a directory, it will only be deleted if it 
2885  * is empty.
2886  * 
2887  * If @cancellable is not %NULL, then the operation can be cancelled by
2888  * triggering the cancellable object from another thread. If the operation
2889  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2890  * 
2891  * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2892  **/
2893 gboolean
2894 g_file_delete (GFile         *file,
2895                GCancellable  *cancellable,
2896                GError       **error)
2897 {
2898   GFileIface *iface;
2899   
2900   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2901
2902   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2903     return FALSE;
2904   
2905   iface = G_FILE_GET_IFACE (file);
2906
2907   if (iface->delete_file == NULL)
2908     {
2909       g_set_error_literal (error, G_IO_ERROR,
2910                            G_IO_ERROR_NOT_SUPPORTED,
2911                            _("Operation not supported"));
2912       return FALSE;
2913     }
2914   
2915   return (* iface->delete_file) (file, cancellable, error);
2916 }
2917
2918 /**
2919  * g_file_trash:
2920  * @file: #GFile to send to trash.
2921  * @cancellable: optional #GCancellable object, %NULL to ignore.
2922  * @error: a #GError, or %NULL
2923  *
2924  * Sends @file to the "Trashcan", if possible. This is similar to
2925  * deleting it, but the user can recover it before emptying the trashcan.
2926  * Not all file systems support trashing, so this call can return the
2927  * %G_IO_ERROR_NOT_SUPPORTED error.
2928  *
2929  *
2930  * If @cancellable is not %NULL, then the operation can be cancelled by
2931  * triggering the cancellable object from another thread. If the operation
2932  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2933  * 
2934  * Returns: %TRUE on successful trash, %FALSE otherwise.
2935  **/
2936 gboolean
2937 g_file_trash (GFile         *file,
2938               GCancellable  *cancellable,
2939               GError       **error)
2940 {
2941   GFileIface *iface;
2942   
2943   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2944
2945   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2946     return FALSE;
2947   
2948   iface = G_FILE_GET_IFACE (file);
2949
2950   if (iface->trash == NULL)
2951     {
2952       g_set_error_literal (error,
2953                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2954                            _("Trash not supported"));
2955       return FALSE;
2956     }
2957   
2958   return (* iface->trash) (file, cancellable, error);
2959 }
2960
2961 /**
2962  * g_file_set_display_name:
2963  * @file: input #GFile.
2964  * @display_name: a string.
2965  * @cancellable: optional #GCancellable object, %NULL to ignore.
2966  * @error: a #GError, or %NULL
2967  * 
2968  * Renames @file to the specified display name.
2969  *
2970  * The display name is converted from UTF8 to the correct encoding for the target
2971  * filesystem if possible and the @file is renamed to this.
2972  * 
2973  * If you want to implement a rename operation in the user interface the edit name
2974  * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2975  * widget, and then the result after editing should be passed to g_file_set_display_name().
2976  *
2977  * On success the resulting converted filename is returned.
2978  * 
2979  * If @cancellable is not %NULL, then the operation can be cancelled by
2980  * triggering the cancellable object from another thread. If the operation
2981  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2982  * 
2983  * Returns: a #GFile specifying what @file was renamed to, or %NULL 
2984  *     if there was an error.
2985  *     Free the returned object with g_object_unref().
2986  **/
2987 GFile *
2988 g_file_set_display_name (GFile         *file,
2989                          const char    *display_name,
2990                          GCancellable  *cancellable,
2991                          GError       **error)
2992 {
2993   GFileIface *iface;
2994   
2995   g_return_val_if_fail (G_IS_FILE (file), NULL);
2996   g_return_val_if_fail (display_name != NULL, NULL);
2997
2998   if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2999     {
3000       g_set_error (error,
3001                    G_IO_ERROR,
3002                    G_IO_ERROR_INVALID_ARGUMENT,
3003                    _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3004       return NULL;
3005     }
3006   
3007   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3008     return NULL;
3009   
3010   iface = G_FILE_GET_IFACE (file);
3011
3012   return (* iface->set_display_name) (file, display_name, cancellable, error);
3013 }
3014
3015 /**
3016  * g_file_set_display_name_async:
3017  * @file: input #GFile.
3018  * @display_name: a string.
3019  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
3020  *     of the request. 
3021  * @cancellable: optional #GCancellable object, %NULL to ignore.
3022  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3023  * @user_data: the data to pass to callback function
3024  * 
3025  * Asynchronously sets the display name for a given #GFile.
3026  * 
3027  * For more details, see g_set_display_name() which is
3028  * the synchronous version of this call.
3029  *
3030  * When the operation is finished, @callback will be called. You can then call
3031  * g_file_set_display_name_finish() to get the result of the operation.
3032  **/
3033 void
3034 g_file_set_display_name_async (GFile               *file,
3035                                const char          *display_name,
3036                                int                  io_priority,
3037                                GCancellable        *cancellable,
3038                                GAsyncReadyCallback  callback,
3039                                gpointer             user_data)
3040 {
3041   GFileIface *iface;
3042   
3043   g_return_if_fail (G_IS_FILE (file));
3044   g_return_if_fail (display_name != NULL);
3045
3046   iface = G_FILE_GET_IFACE (file);
3047   (* iface->set_display_name_async) (file,
3048                                      display_name,
3049                                      io_priority,
3050                                      cancellable,
3051                                      callback,
3052                                      user_data);
3053 }
3054
3055 /**
3056  * g_file_set_display_name_finish:
3057  * @file: input #GFile.
3058  * @res: a #GAsyncResult. 
3059  * @error: a #GError, or %NULL
3060  * 
3061  * Finishes setting a display name started with 
3062  * g_file_set_display_name_async().
3063  * 
3064  * Returns: a #GFile or %NULL on error.
3065  *     Free the returned object with g_object_unref().
3066  **/
3067 GFile *
3068 g_file_set_display_name_finish (GFile         *file,
3069                                 GAsyncResult  *res,
3070                                 GError       **error)
3071 {
3072   GFileIface *iface;
3073   
3074   g_return_val_if_fail (G_IS_FILE (file), NULL);
3075   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3076
3077   if (G_IS_SIMPLE_ASYNC_RESULT (res))
3078     {
3079       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3080       if (g_simple_async_result_propagate_error (simple, error))
3081         return NULL;
3082     }
3083   
3084   iface = G_FILE_GET_IFACE (file);
3085   return (* iface->set_display_name_finish) (file, res, error);
3086 }
3087
3088 /**
3089  * g_file_query_settable_attributes:
3090  * @file: input #GFile.
3091  * @cancellable: optional #GCancellable object, %NULL to ignore.
3092  * @error: a #GError, or %NULL
3093  * 
3094  * Obtain the list of settable attributes for the file.
3095  *
3096  * Returns the type and full attribute name of all the attributes 
3097  * that can be set on this file. This doesn't mean setting it will always 
3098  * succeed though, you might get an access failure, or some specific 
3099  * file may not support a specific attribute.
3100  *
3101  * If @cancellable is not %NULL, then the operation can be cancelled by
3102  * triggering the cancellable object from another thread. If the operation
3103  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3104  * 
3105  * Returns: a #GFileAttributeInfoList describing the settable attributes.
3106  * When you are done with it, release it with g_file_attribute_info_list_unref()
3107  **/
3108 GFileAttributeInfoList *
3109 g_file_query_settable_attributes (GFile         *file,
3110                                   GCancellable  *cancellable,
3111                                   GError       **error)
3112 {
3113   GFileIface *iface;
3114   GError *my_error;
3115   GFileAttributeInfoList *list;
3116
3117   g_return_val_if_fail (G_IS_FILE (file), NULL);
3118
3119   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3120     return NULL;
3121   
3122   iface = G_FILE_GET_IFACE (file);
3123
3124   if (iface->query_settable_attributes == NULL)
3125     return g_file_attribute_info_list_new ();
3126
3127   my_error = NULL;
3128   list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3129   
3130   if (list == NULL)
3131     {
3132       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3133         {
3134           list = g_file_attribute_info_list_new ();
3135           g_error_free (my_error);
3136         }
3137       else
3138         g_propagate_error (error, my_error);
3139     }
3140   
3141   return list;
3142 }
3143
3144 /**
3145  * g_file_query_writable_namespaces:
3146  * @file: input #GFile.
3147  * @cancellable: optional #GCancellable object, %NULL to ignore.
3148  * @error: a #GError, or %NULL
3149  * 
3150  * Obtain the list of attribute namespaces where new attributes 
3151  * can be created by a user. An example of this is extended
3152  * attributes (in the "xattr" namespace).
3153  *
3154  * If @cancellable is not %NULL, then the operation can be cancelled by
3155  * triggering the cancellable object from another thread. If the operation
3156  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3157  * 
3158  * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3159  * When you are done with it, release it with g_file_attribute_info_list_unref()
3160  **/
3161 GFileAttributeInfoList *
3162 g_file_query_writable_namespaces (GFile         *file,
3163                                   GCancellable  *cancellable,
3164                                   GError       **error)
3165 {
3166   GFileIface *iface;
3167   GError *my_error;
3168   GFileAttributeInfoList *list;
3169   
3170   g_return_val_if_fail (G_IS_FILE (file), NULL);
3171
3172   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3173     return NULL;
3174   
3175   iface = G_FILE_GET_IFACE (file);
3176
3177   if (iface->query_writable_namespaces == NULL)
3178     return g_file_attribute_info_list_new ();
3179
3180   my_error = NULL;
3181   list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3182   
3183   if (list == NULL)
3184     {
3185       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3186         {
3187           list = g_file_attribute_info_list_new ();
3188           g_error_free (my_error);
3189         }
3190       else
3191         g_propagate_error (error, my_error);
3192     }
3193   
3194   return list;
3195 }
3196
3197 /**
3198  * g_file_set_attribute:
3199  * @file: input #GFile.
3200  * @attribute: a string containing the attribute's name.
3201  * @type: The type of the attribute
3202  * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3203  * @flags: a set of #GFileQueryInfoFlags.
3204  * @cancellable: optional #GCancellable object, %NULL to ignore.
3205  * @error: a #GError, or %NULL
3206  * 
3207  * Sets an attribute in the file with attribute name @attribute to @value.
3208  * 
3209  * If @cancellable is not %NULL, then the operation can be cancelled by
3210  * triggering the cancellable object from another thread. If the operation
3211  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3212  * 
3213  * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3214  **/
3215 gboolean
3216 g_file_set_attribute (GFile                      *file,
3217                       const char                 *attribute,
3218                       GFileAttributeType          type,
3219                       gpointer                    value_p,
3220                       GFileQueryInfoFlags         flags,
3221                       GCancellable               *cancellable,
3222                       GError                    **error)
3223 {
3224   GFileIface *iface;
3225   
3226   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3227   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3228
3229   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3230     return FALSE;
3231   
3232   iface = G_FILE_GET_IFACE (file);
3233
3234   if (iface->set_attribute == NULL)
3235     {
3236       g_set_error_literal (error, G_IO_ERROR,
3237                            G_IO_ERROR_NOT_SUPPORTED,
3238                            _("Operation not supported"));
3239       return FALSE;
3240     }
3241
3242   return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3243 }
3244
3245 /**
3246  * g_file_set_attributes_from_info:
3247  * @file: input #GFile.
3248  * @info: a #GFileInfo.
3249  * @flags: #GFileQueryInfoFlags
3250  * @cancellable: optional #GCancellable object, %NULL to ignore.
3251  * @error: a #GError, or %NULL 
3252  * 
3253  * Tries to set all attributes in the #GFileInfo on the target values, 
3254  * not stopping on the first error.
3255  * 
3256  * If there is any error during this operation then @error will be set to
3257  * the first error. Error on particular fields are flagged by setting 
3258  * the "status" field in the attribute value to 
3259  * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3260  * further errors.
3261  *
3262  * If @cancellable is not %NULL, then the operation can be cancelled by
3263  * triggering the cancellable object from another thread. If the operation
3264  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3265  * 
3266  * Returns: %TRUE if there was any error, %FALSE otherwise.
3267  **/
3268 gboolean
3269 g_file_set_attributes_from_info (GFile                *file,
3270                                  GFileInfo            *info,
3271                                  GFileQueryInfoFlags   flags,
3272                                  GCancellable         *cancellable,
3273                                  GError              **error)
3274 {
3275   GFileIface *iface;
3276
3277   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3278   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3279
3280   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3281     return FALSE;
3282   
3283   g_file_info_clear_status (info);
3284   
3285   iface = G_FILE_GET_IFACE (file);
3286
3287   return (* iface->set_attributes_from_info) (file, 
3288                                               info, 
3289                                               flags, 
3290                                               cancellable, 
3291                                               error);
3292 }
3293
3294
3295 static gboolean
3296 g_file_real_set_attributes_from_info (GFile                *file,
3297                                       GFileInfo            *info,
3298                                       GFileQueryInfoFlags   flags,
3299                                       GCancellable         *cancellable,
3300                                       GError              **error)
3301 {
3302   char **attributes;
3303   int i;
3304   gboolean res;
3305   GFileAttributeValue *value;
3306   
3307   res = TRUE;
3308   
3309   attributes = g_file_info_list_attributes (info, NULL);
3310
3311   for (i = 0; attributes[i] != NULL; i++)
3312     {
3313       value = _g_file_info_get_attribute_value (info, attributes[i]);
3314
3315       if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3316         continue;
3317
3318       if (!g_file_set_attribute (file, attributes[i],
3319                                  value->type, _g_file_attribute_value_peek_as_pointer (value),
3320                                  flags, cancellable, error))
3321         {
3322           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3323           res = FALSE;
3324           /* Don't set error multiple times */
3325           error = NULL;
3326         }
3327       else
3328         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3329     }
3330   
3331   g_strfreev (attributes);
3332   
3333   return res;
3334 }
3335
3336 /**
3337  * g_file_set_attributes_async:
3338  * @file: input #GFile.
3339  * @info: a #GFileInfo.
3340  * @flags: a #GFileQueryInfoFlags.
3341  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
3342  *     of the request. 
3343  * @cancellable: optional #GCancellable object, %NULL to ignore.
3344  * @callback: a #GAsyncReadyCallback. 
3345  * @user_data: a #gpointer.
3346  *
3347  * Asynchronously sets the attributes of @file with @info.
3348  * 
3349  * For more details, see g_file_set_attributes_from_info() which is
3350  * the synchronous version of this call.
3351  *
3352  * When the operation is finished, @callback will be called. You can then call
3353  * g_file_set_attributes_finish() to get the result of the operation.
3354  **/
3355 void
3356 g_file_set_attributes_async (GFile               *file,
3357                              GFileInfo           *info,
3358                              GFileQueryInfoFlags  flags,
3359                              int                  io_priority,
3360                              GCancellable        *cancellable,
3361                              GAsyncReadyCallback  callback,
3362                              gpointer             user_data)
3363 {
3364   GFileIface *iface;
3365   
3366   g_return_if_fail (G_IS_FILE (file));
3367   g_return_if_fail (G_IS_FILE_INFO (info));
3368
3369   iface = G_FILE_GET_IFACE (file);
3370   (* iface->set_attributes_async) (file, 
3371                                    info, 
3372                                    flags, 
3373                                    io_priority, 
3374                                    cancellable, 
3375                                    callback, 
3376                                    user_data);
3377 }
3378
3379 /**
3380  * g_file_set_attributes_finish:
3381  * @file: input #GFile.
3382  * @result: a #GAsyncResult.
3383  * @info: a #GFileInfo.
3384  * @error: a #GError, or %NULL
3385  * 
3386  * Finishes setting an attribute started in g_file_set_attributes_async().
3387  * 
3388  * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3389  **/
3390 gboolean
3391 g_file_set_attributes_finish (GFile         *file,
3392                               GAsyncResult  *result,
3393                               GFileInfo    **info,
3394                               GError       **error)
3395 {
3396   GFileIface *iface;
3397   
3398   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3399   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3400
3401   /* No standard handling of errors here, as we must set info even
3402    * on errors 
3403    */
3404   iface = G_FILE_GET_IFACE (file);
3405   return (* iface->set_attributes_finish) (file, result, info, error);
3406 }
3407
3408 /**
3409  * g_file_set_attribute_string:
3410  * @file: input #GFile.
3411  * @attribute: a string containing the attribute's name.
3412  * @value: a string containing the attribute's value.
3413  * @flags: #GFileQueryInfoFlags.
3414  * @cancellable: optional #GCancellable object, %NULL to ignore.
3415  * @error: a #GError, or %NULL
3416  * 
3417  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value. 
3418  * If @attribute is of a different type, this operation will fail.
3419  * 
3420  * If @cancellable is not %NULL, then the operation can be cancelled by
3421  * triggering the cancellable object from another thread. If the operation
3422  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3423  * 
3424  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3425  **/
3426 gboolean
3427 g_file_set_attribute_string (GFile                *file,
3428                              const char           *attribute,
3429                              const char           *value,
3430                              GFileQueryInfoFlags   flags,
3431                              GCancellable         *cancellable,
3432                              GError              **error)
3433 {
3434   return g_file_set_attribute (file, attribute,
3435                                G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3436                                flags, cancellable, error);
3437 }
3438
3439 /**
3440  * g_file_set_attribute_byte_string:
3441  * @file: input #GFile.
3442  * @attribute: a string containing the attribute's name.
3443  * @value: a string containing the attribute's new value.
3444  * @flags: a #GFileQueryInfoFlags.
3445  * @cancellable: optional #GCancellable object, %NULL to ignore.
3446  * @error: a #GError, or %NULL
3447  * 
3448  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value. 
3449  * If @attribute is of a different type, this operation will fail, 
3450  * returning %FALSE. 
3451  * 
3452  * If @cancellable is not %NULL, then the operation can be cancelled by
3453  * triggering the cancellable object from another thread. If the operation
3454  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3455  * 
3456  * Returns: %TRUE if the @attribute was successfully set to @value 
3457  * in the @file, %FALSE otherwise.
3458  **/
3459 gboolean
3460 g_file_set_attribute_byte_string  (GFile                *file,
3461                                    const char           *attribute,
3462                                    const char           *value,
3463                                    GFileQueryInfoFlags   flags,
3464                                    GCancellable         *cancellable,
3465                                    GError              **error)
3466 {
3467   return g_file_set_attribute (file, attribute,
3468                                G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3469                                flags, cancellable, error);
3470 }
3471
3472 /**
3473  * g_file_set_attribute_uint32:
3474  * @file: input #GFile.
3475  * @attribute: a string containing the attribute's name.
3476  * @value: a #guint32 containing the attribute's new value.
3477  * @flags: a #GFileQueryInfoFlags.
3478  * @cancellable: optional #GCancellable object, %NULL to ignore.
3479  * @error: a #GError, or %NULL
3480  * 
3481  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value. 
3482  * If @attribute is of a different type, this operation will fail.
3483  * 
3484  * If @cancellable is not %NULL, then the operation can be cancelled by
3485  * triggering the cancellable object from another thread. If the operation
3486  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3487  * 
3488  * Returns: %TRUE if the @attribute was successfully set to @value 
3489  * in the @file, %FALSE otherwise.
3490  **/
3491 gboolean
3492 g_file_set_attribute_uint32 (GFile                *file,
3493                              const char           *attribute,
3494                              guint32               value,
3495                              GFileQueryInfoFlags   flags,
3496                              GCancellable         *cancellable,
3497                              GError              **error)
3498 {
3499   return g_file_set_attribute (file, attribute,
3500                                G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3501                                flags, cancellable, error);
3502 }
3503
3504 /**
3505  * g_file_set_attribute_int32:
3506  * @file: input #GFile.
3507  * @attribute: a string containing the attribute's name.
3508  * @value: a #gint32 containing the attribute's new value.
3509  * @flags: a #GFileQueryInfoFlags.
3510  * @cancellable: optional #GCancellable object, %NULL to ignore.
3511  * @error: a #GError, or %NULL
3512  * 
3513  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value. 
3514  * If @attribute is of a different type, this operation will fail.
3515  * 
3516  * If @cancellable is not %NULL, then the operation can be cancelled by
3517  * triggering the cancellable object from another thread. If the operation
3518  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3519  * 
3520  * Returns: %TRUE if the @attribute was successfully set to @value 
3521  * in the @file, %FALSE otherwise. 
3522  **/
3523 gboolean
3524 g_file_set_attribute_int32 (GFile                *file,
3525                             const char           *attribute,
3526                             gint32                value,
3527                             GFileQueryInfoFlags   flags,
3528                             GCancellable         *cancellable,
3529                             GError              **error)
3530 {
3531   return g_file_set_attribute (file, attribute,
3532                                G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3533                                flags, cancellable, error);
3534 }
3535
3536 /**
3537  * g_file_set_attribute_uint64:
3538  * @file: input #GFile. 
3539  * @attribute: a string containing the attribute's name.
3540  * @value: a #guint64 containing the attribute's new value.
3541  * @flags: a #GFileQueryInfoFlags.
3542  * @cancellable: optional #GCancellable object, %NULL to ignore.
3543  * @error: a #GError, or %NULL
3544  * 
3545  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value. 
3546  * If @attribute is of a different type, this operation will fail.
3547  * 
3548  * If @cancellable is not %NULL, then the operation can be cancelled by
3549  * triggering the cancellable object from another thread. If the operation
3550  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3551  * 
3552  * Returns: %TRUE if the @attribute was successfully set to @value 
3553  * in the @file, %FALSE otherwise.
3554  **/
3555 gboolean
3556 g_file_set_attribute_uint64 (GFile                *file,
3557                              const char           *attribute,
3558                              guint64               value,
3559                              GFileQueryInfoFlags   flags,
3560                              GCancellable         *cancellable,
3561                              GError              **error)
3562  {
3563   return g_file_set_attribute (file, attribute,
3564                                G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3565                                flags, cancellable, error);
3566 }
3567
3568 /**
3569  * g_file_set_attribute_int64:
3570  * @file: input #GFile.
3571  * @attribute: a string containing the attribute's name.
3572  * @value: a #guint64 containing the attribute's new value.
3573  * @flags: a #GFileQueryInfoFlags.
3574  * @cancellable: optional #GCancellable object, %NULL to ignore.
3575  * @error: a #GError, or %NULL
3576  * 
3577  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value. 
3578  * If @attribute is of a different type, this operation will fail.
3579  * 
3580  * If @cancellable is not %NULL, then the operation can be cancelled by
3581  * triggering the cancellable object from another thread. If the operation
3582  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3583  * 
3584  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3585  **/
3586 gboolean
3587 g_file_set_attribute_int64 (GFile                *file,
3588                             const char           *attribute,
3589                             gint64                value,
3590                             GFileQueryInfoFlags   flags,
3591                             GCancellable         *cancellable,
3592                             GError              **error)
3593 {
3594   return g_file_set_attribute (file, attribute,
3595                                G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3596                                flags, cancellable, error);
3597 }
3598
3599 /**
3600  * g_file_mount_mountable:
3601  * @file: input #GFile.
3602  * @flags: flags affecting the operation
3603  * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3604  * @cancellable: optional #GCancellable object, %NULL to ignore.
3605  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3606  * @user_data: the data to pass to callback function
3607  * 
3608  * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3609  * Using @mount_operation, you can request callbacks when, for instance, 
3610  * passwords are needed during authentication.
3611  *
3612  * If @cancellable is not %NULL, then the operation can be cancelled by
3613  * triggering the cancellable object from another thread. If the operation
3614  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3615  *
3616  * When the operation is finished, @callback will be called. You can then call
3617  * g_file_mount_mountable_finish() to get the result of the operation.
3618  **/
3619 void
3620 g_file_mount_mountable (GFile               *file,
3621                         GMountMountFlags     flags,
3622                         GMountOperation     *mount_operation,
3623                         GCancellable        *cancellable,
3624                         GAsyncReadyCallback  callback,
3625                         gpointer             user_data)
3626 {
3627   GFileIface *iface;
3628
3629   g_return_if_fail (G_IS_FILE (file));
3630
3631   iface = G_FILE_GET_IFACE (file);
3632
3633   if (iface->mount_mountable == NULL) 
3634     {
3635       g_simple_async_report_error_in_idle (G_OBJECT (file),
3636                                            callback,
3637                                            user_data,
3638                                            G_IO_ERROR,
3639                                            G_IO_ERROR_NOT_SUPPORTED,
3640                                            _("Operation not supported"));
3641       return;
3642     }
3643   
3644   (* iface->mount_mountable) (file,
3645                               flags,
3646                               mount_operation,
3647                               cancellable,
3648                               callback,
3649                               user_data);
3650 }
3651
3652 /**
3653  * g_file_mount_mountable_finish:
3654  * @file: input #GFile.
3655  * @result: a #GAsyncResult.
3656  * @error: a #GError, or %NULL
3657  *
3658  * Finishes a mount operation. See g_file_mount_mountable() for details.
3659  * 
3660  * Finish an asynchronous mount operation that was started 
3661  * with g_file_mount_mountable().
3662  *
3663  * Returns: a #GFile or %NULL on error.
3664  *     Free the returned object with g_object_unref().
3665  **/
3666 GFile *
3667 g_file_mount_mountable_finish (GFile         *file,
3668                                GAsyncResult  *result,
3669                                GError       **error)
3670 {
3671   GFileIface *iface;
3672
3673   g_return_val_if_fail (G_IS_FILE (file), NULL);
3674   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3675
3676   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3677     {
3678       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3679       if (g_simple_async_result_propagate_error (simple, error))
3680         return NULL;
3681     }
3682   
3683   iface = G_FILE_GET_IFACE (file);
3684   return (* iface->mount_mountable_finish) (file, result, error);
3685 }
3686
3687 /**
3688  * g_file_unmount_mountable:
3689  * @file: input #GFile.
3690  * @flags: flags affecting the operation
3691  * @cancellable: optional #GCancellable object, %NULL to ignore.
3692  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3693  * @user_data: the data to pass to callback function
3694  *
3695  * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3696  *
3697  * If @cancellable is not %NULL, then the operation can be cancelled by
3698  * triggering the cancellable object from another thread. If the operation
3699  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3700  *
3701  * When the operation is finished, @callback will be called. You can then call
3702  * g_file_unmount_mountable_finish() to get the result of the operation.
3703  **/
3704 void
3705 g_file_unmount_mountable (GFile               *file,
3706                           GMountUnmountFlags   flags,
3707                           GCancellable        *cancellable,
3708                           GAsyncReadyCallback  callback,
3709                           gpointer             user_data)
3710 {
3711   GFileIface *iface;
3712   
3713   g_return_if_fail (G_IS_FILE (file));
3714
3715   iface = G_FILE_GET_IFACE (file);
3716   
3717   if (iface->unmount_mountable == NULL)
3718     {
3719       g_simple_async_report_error_in_idle (G_OBJECT (file),
3720                                            callback,
3721                                            user_data,
3722                                            G_IO_ERROR,
3723                                            G_IO_ERROR_NOT_SUPPORTED,
3724                                            _("Operation not supported"));
3725       return;
3726     }
3727   
3728   (* iface->unmount_mountable) (file,
3729                                 flags,
3730                                 cancellable,
3731                                 callback,
3732                                 user_data);
3733 }
3734
3735 /**
3736  * g_file_unmount_mountable_finish:
3737  * @file: input #GFile.
3738  * @result: a #GAsyncResult.
3739  * @error: a #GError, or %NULL
3740  *
3741  * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3742  * 
3743  * Finish an asynchronous unmount operation that was started 
3744  * with g_file_unmount_mountable().
3745  *
3746  * Returns: %TRUE if the operation finished successfully. %FALSE
3747  * otherwise.
3748  **/
3749 gboolean
3750 g_file_unmount_mountable_finish (GFile         *file,
3751                                  GAsyncResult  *result,
3752                                  GError       **error)
3753 {
3754   GFileIface *iface;
3755   
3756   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3757   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3758
3759   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3760     {
3761       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3762       if (g_simple_async_result_propagate_error (simple, error))
3763         return FALSE;
3764     }
3765   
3766   iface = G_FILE_GET_IFACE (file);
3767   return (* iface->unmount_mountable_finish) (file, result, error);
3768 }
3769
3770 /**
3771  * g_file_eject_mountable:
3772  * @file: input #GFile.
3773  * @flags: flags affecting the operation
3774  * @cancellable: optional #GCancellable object, %NULL to ignore.
3775  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3776  * @user_data: the data to pass to callback function
3777  * 
3778  * Starts an asynchronous eject on a mountable.  
3779  * When this operation has completed, @callback will be called with
3780  * @user_user data, and the operation can be finalized with 
3781  * g_file_eject_mountable_finish().
3782  * 
3783  * If @cancellable is not %NULL, then the operation can be cancelled by
3784  * triggering the cancellable object from another thread. If the operation
3785  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3786  **/
3787 void
3788 g_file_eject_mountable (GFile               *file,
3789                         GMountUnmountFlags   flags,
3790                         GCancellable        *cancellable,
3791                         GAsyncReadyCallback  callback,
3792                         gpointer             user_data)
3793 {
3794   GFileIface *iface;
3795
3796   g_return_if_fail (G_IS_FILE (file));
3797
3798   iface = G_FILE_GET_IFACE (file);
3799   
3800   if (iface->eject_mountable == NULL) 
3801     {
3802       g_simple_async_report_error_in_idle (G_OBJECT (file),
3803                                            callback,
3804                                            user_data,
3805                                            G_IO_ERROR,
3806                                            G_IO_ERROR_NOT_SUPPORTED,
3807                                            _("Operation not supported"));
3808       return;
3809     }
3810   
3811   (* iface->eject_mountable) (file,
3812                               flags,
3813                               cancellable,
3814                               callback,
3815                               user_data);
3816 }
3817
3818 /**
3819  * g_file_eject_mountable_finish:
3820  * @file: input #GFile.
3821  * @result: a #GAsyncResult.
3822  * @error: a #GError, or %NULL
3823  * 
3824  * Finishes an asynchronous eject operation started by 
3825  * g_file_eject_mountable().
3826  * 
3827  * Returns: %TRUE if the @file was ejected successfully. %FALSE 
3828  * otherwise.
3829  **/
3830 gboolean
3831 g_file_eject_mountable_finish (GFile         *file,
3832                                GAsyncResult  *result,
3833                                GError       **error)
3834 {
3835   GFileIface *iface;
3836   
3837   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3838   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3839
3840   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3841     {
3842       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3843       if (g_simple_async_result_propagate_error (simple, error))
3844         return FALSE;
3845     }
3846   
3847   iface = G_FILE_GET_IFACE (file);
3848   return (* iface->eject_mountable_finish) (file, result, error);
3849 }
3850
3851 /**
3852  * g_file_monitor_directory:
3853  * @file: input #GFile.
3854  * @flags: a set of #GFileMonitorFlags.
3855  * @cancellable: optional #GCancellable object, %NULL to ignore.
3856  * @error: a #GError, or %NULL.
3857  * 
3858  * Obtains a directory monitor for the given file.
3859  * This may fail if directory monitoring is not supported.
3860  *
3861  * If @cancellable is not %NULL, then the operation can be cancelled by
3862  * triggering the cancellable object from another thread. If the operation
3863  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3864  * 
3865  * Returns: a #GFileMonitor for the given @file, or %NULL on error.
3866  *     Free the returned object with g_object_unref().
3867  **/
3868 GFileMonitor*
3869 g_file_monitor_directory (GFile             *file,
3870                           GFileMonitorFlags  flags,
3871                           GCancellable      *cancellable,
3872                           GError           **error)
3873 {
3874   GFileIface *iface;
3875
3876   g_return_val_if_fail (G_IS_FILE (file), NULL);
3877
3878   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3879     return NULL;
3880
3881   iface = G_FILE_GET_IFACE (file);
3882
3883   if (iface->monitor_dir == NULL)
3884     {
3885       g_set_error_literal (error, G_IO_ERROR,
3886                            G_IO_ERROR_NOT_SUPPORTED,
3887                            _("Operation not supported"));
3888       return NULL;
3889     }
3890
3891   return (* iface->monitor_dir) (file, flags, cancellable, error);
3892 }
3893
3894 /**
3895  * g_file_monitor_file:
3896  * @file: input #GFile.
3897  * @flags: a set of #GFileMonitorFlags.
3898  * @cancellable: optional #GCancellable object, %NULL to ignore.
3899  * @error: a #GError, or %NULL.
3900  * 
3901  * Obtains a file monitor for the given file. If no file notification
3902  * mechanism exists, then regular polling of the file is used.
3903  *
3904  * If @cancellable is not %NULL, then the operation can be cancelled by
3905  * triggering the cancellable object from another thread. If the operation
3906  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3907  * 
3908  * Returns: a #GFileMonitor for the given @file, or %NULL on error.
3909  *     Free the returned object with g_object_unref().
3910  **/
3911 GFileMonitor*
3912 g_file_monitor_file (GFile             *file,
3913                      GFileMonitorFlags  flags,
3914                      GCancellable      *cancellable,
3915                      GError           **error)
3916 {
3917   GFileIface *iface;
3918   GFileMonitor *monitor;
3919   
3920   g_return_val_if_fail (G_IS_FILE (file), NULL);
3921
3922   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3923     return NULL;
3924
3925   iface = G_FILE_GET_IFACE (file);
3926
3927   monitor = NULL;
3928   
3929   if (iface->monitor_file)
3930     monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3931
3932 /* Fallback to polling */
3933   if (monitor == NULL)
3934     monitor = _g_poll_file_monitor_new (file);
3935
3936   return monitor;
3937 }
3938
3939 /**
3940  * g_file_monitor:
3941  * @file: input #GFile
3942  * @flags: a set of #GFileMonitorFlags
3943  * @cancellable: optional #GCancellable object, %NULL to ignore
3944  * @error: a #GError, or %NULL
3945  * 
3946  * Obtains a file or directory monitor for the given file, depending
3947  * on the type of the file.
3948  *
3949  * If @cancellable is not %NULL, then the operation can be cancelled by
3950  * triggering the cancellable object from another thread. If the operation
3951  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3952  * 
3953  * Returns: a #GFileMonitor for the given @file, or %NULL on error.
3954  *     Free the returned object with g_object_unref().
3955  *
3956  * Since: 2.18
3957  */
3958 GFileMonitor*
3959 g_file_monitor (GFile             *file,
3960                 GFileMonitorFlags  flags,
3961                 GCancellable      *cancellable,
3962                 GError           **error)
3963 {
3964   if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
3965     return g_file_monitor_directory (file, flags, cancellable, error);
3966   else
3967     return g_file_monitor_file (file, flags, cancellable, error);
3968 }
3969
3970 /********************************************
3971  *   Default implementation of async ops    *
3972  ********************************************/
3973
3974 typedef struct {
3975   char *attributes;
3976   GFileQueryInfoFlags flags;
3977   GFileInfo *info;
3978 } QueryInfoAsyncData;
3979
3980 static void
3981 query_info_data_free (QueryInfoAsyncData *data)
3982 {
3983   if (data->info)
3984     g_object_unref (data->info);
3985   g_free (data->attributes);
3986   g_free (data);
3987 }
3988
3989 static void
3990 query_info_async_thread (GSimpleAsyncResult *res,
3991                          GObject            *object,
3992                          GCancellable       *cancellable)
3993 {
3994   GError *error = NULL;
3995   QueryInfoAsyncData *data;
3996   GFileInfo *info;
3997   
3998   data = g_simple_async_result_get_op_res_gpointer (res);
3999   
4000   info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4001
4002   if (info == NULL)
4003     {
4004       g_simple_async_result_set_from_error (res, error);
4005       g_error_free (error);
4006     }
4007   else
4008     data->info = info;
4009 }
4010
4011 static void
4012 g_file_real_query_info_async (GFile               *file,
4013                               const char          *attributes,
4014                               GFileQueryInfoFlags  flags,
4015                               int                  io_priority,
4016                               GCancellable        *cancellable,
4017                               GAsyncReadyCallback  callback,
4018                               gpointer             user_data)
4019 {
4020   GSimpleAsyncResult *res;
4021   QueryInfoAsyncData *data;
4022
4023   data = g_new0 (QueryInfoAsyncData, 1);
4024   data->attributes = g_strdup (attributes);
4025   data->flags = flags;
4026   
4027   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
4028   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
4029   
4030   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
4031   g_object_unref (res);
4032 }
4033
4034 static GFileInfo *
4035 g_file_real_query_info_finish (GFile         *file,
4036                                GAsyncResult  *res,
4037                                GError       **error)
4038 {
4039   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4040   QueryInfoAsyncData *data;
4041
4042   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4043
4044   data = g_simple_async_result_get_op_res_gpointer (simple);
4045   if (data->info)
4046     return g_object_ref (data->info);
4047   
4048   return NULL;
4049 }
4050
4051 typedef struct {
4052   char *attributes;
4053   GFileInfo *info;
4054 } QueryFilesystemInfoAsyncData;
4055
4056 static void
4057 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4058 {
4059   if (data->info)
4060     g_object_unref (data->info);
4061   g_free (data->attributes);
4062   g_free (data);
4063 }
4064
4065 static void
4066 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4067                                     GObject            *object,
4068                                     GCancellable       *cancellable)
4069 {
4070   GError *error = NULL;
4071   QueryFilesystemInfoAsyncData *data;
4072   GFileInfo *info;
4073   
4074   data = g_simple_async_result_get_op_res_gpointer (res);
4075   
4076   info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4077
4078   if (info == NULL)
4079     {
4080       g_simple_async_result_set_from_error (res, error);
4081       g_error_free (error);
4082     }
4083   else
4084     data->info = info;
4085 }
4086
4087 static void
4088 g_file_real_query_filesystem_info_async (GFile               *file,
4089                                          const char          *attributes,
4090                                          int                  io_priority,
4091                                          GCancellable        *cancellable,
4092                                          GAsyncReadyCallback  callback,
4093                                          gpointer             user_data)
4094 {
4095   GSimpleAsyncResult *res;
4096   QueryFilesystemInfoAsyncData *data;
4097
4098   data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4099   data->attributes = g_strdup (attributes);
4100   
4101   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4102   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4103   
4104   g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4105   g_object_unref (res);
4106 }
4107
4108 static GFileInfo *
4109 g_file_real_query_filesystem_info_finish (GFile         *file,
4110                                           GAsyncResult  *res,
4111                                           GError       **error)
4112 {
4113   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4114   QueryFilesystemInfoAsyncData *data;
4115
4116   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4117
4118   data = g_simple_async_result_get_op_res_gpointer (simple);
4119   if (data->info)
4120     return g_object_ref (data->info);
4121   
4122   return NULL;
4123 }
4124
4125 typedef struct {
4126   char *attributes;
4127   GFileQueryInfoFlags flags;
4128   GFileEnumerator *enumerator;
4129 } EnumerateChildrenAsyncData;
4130
4131 static void
4132 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4133 {
4134   if (data->enumerator)
4135     g_object_unref (data->enumerator);
4136   g_free (data->attributes);
4137   g_free (data);
4138 }
4139
4140 static void
4141 enumerate_children_async_thread (GSimpleAsyncResult *res,
4142                                  GObject            *object,
4143                                  GCancellable       *cancellable)
4144 {
4145   GError *error = NULL;
4146   EnumerateChildrenAsyncData *data;
4147   GFileEnumerator *enumerator;
4148   
4149   data = g_simple_async_result_get_op_res_gpointer (res);
4150   
4151   enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4152
4153   if (enumerator == NULL)
4154     {
4155       g_simple_async_result_set_from_error (res, error);
4156       g_error_free (error);
4157     }
4158   else
4159     data->enumerator = enumerator;
4160 }
4161
4162 static void
4163 g_file_real_enumerate_children_async (GFile               *file,
4164                                       const char          *attributes,
4165                                       GFileQueryInfoFlags  flags,
4166                                       int                  io_priority,
4167                                       GCancellable        *cancellable,
4168                                       GAsyncReadyCallback  callback,
4169                                       gpointer             user_data)
4170 {
4171   GSimpleAsyncResult *res;
4172   EnumerateChildrenAsyncData *data;
4173
4174   data = g_new0 (EnumerateChildrenAsyncData, 1);
4175   data->attributes = g_strdup (attributes);
4176   data->flags = flags;
4177   
4178   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4179   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4180   
4181   g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4182   g_object_unref (res);
4183 }
4184
4185 static GFileEnumerator *
4186 g_file_real_enumerate_children_finish (GFile         *file,
4187                                        GAsyncResult  *res,
4188                                        GError       **error)
4189 {
4190   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4191   EnumerateChildrenAsyncData *data;
4192
4193   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4194
4195   data = g_simple_async_result_get_op_res_gpointer (simple);
4196   if (data->enumerator)
4197     return g_object_ref (data->enumerator);
4198   
4199   return NULL;
4200 }
4201
4202 static void
4203 open_read_async_thread (GSimpleAsyncResult *res,
4204                         GObject            *object,
4205                         GCancellable       *cancellable)
4206 {
4207   GFileIface *iface;
4208   GFileInputStream *stream;
4209   GError *error = NULL;
4210
4211   iface = G_FILE_GET_IFACE (object);
4212
4213   if (iface->read_fn == NULL)
4214     {
4215       g_set_error_literal (&error, G_IO_ERROR,
4216                            G_IO_ERROR_NOT_SUPPORTED,
4217                            _("Operation not supported"));
4218
4219       g_simple_async_result_set_from_error (res, error);
4220       g_error_free (error);
4221
4222       return;
4223     }
4224   
4225   stream = iface->read_fn (G_FILE (object), cancellable, &error);
4226
4227   if (stream == NULL)
4228     {
4229       g_simple_async_result_set_from_error (res, error);
4230       g_error_free (error);
4231     }
4232   else
4233     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4234 }
4235
4236 static void
4237 g_file_real_read_async (GFile               *file,
4238                         int                  io_priority,
4239                         GCancellable        *cancellable,
4240                         GAsyncReadyCallback  callback,
4241                         gpointer             user_data)
4242 {
4243   GSimpleAsyncResult *res;
4244   
4245   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4246   
4247   g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4248   g_object_unref (res);
4249 }
4250
4251 static GFileInputStream *
4252 g_file_real_read_finish (GFile         *file,
4253                          GAsyncResult  *res,
4254                          GError       **error)
4255 {
4256   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4257   gpointer op;
4258
4259   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4260
4261   op = g_simple_async_result_get_op_res_gpointer (simple);
4262   if (op)
4263     return g_object_ref (op);
4264   
4265   return NULL;
4266 }
4267
4268 static void
4269 append_to_async_thread (GSimpleAsyncResult *res,
4270                         GObject            *object,
4271                         GCancellable       *cancellable)
4272 {
4273   GFileIface *iface;
4274   GFileCreateFlags *data;
4275   GFileOutputStream *stream;
4276   GError *error = NULL;
4277
4278   iface = G_FILE_GET_IFACE (object);
4279
4280   data = g_simple_async_result_get_op_res_gpointer (res);
4281
4282   stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4283
4284   if (stream == NULL)
4285     {
4286       g_simple_async_result_set_from_error (res, error);
4287       g_error_free (error);
4288     }
4289   else
4290     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4291 }
4292
4293 static void
4294 g_file_real_append_to_async (GFile               *file,
4295                              GFileCreateFlags     flags,
4296                              int                  io_priority,
4297                              GCancellable        *cancellable,
4298                              GAsyncReadyCallback  callback,
4299                              gpointer             user_data)
4300 {
4301   GFileCreateFlags *data;
4302   GSimpleAsyncResult *res;
4303
4304   data = g_new0 (GFileCreateFlags, 1);
4305   *data = flags;
4306
4307   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4308   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4309
4310   g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4311   g_object_unref (res);
4312 }
4313
4314 static GFileOutputStream *
4315 g_file_real_append_to_finish (GFile         *file,
4316                               GAsyncResult  *res,
4317                               GError       **error)
4318 {
4319   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4320   gpointer op;
4321
4322   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4323
4324   op = g_simple_async_result_get_op_res_gpointer (simple);
4325   if (op)
4326     return g_object_ref (op);
4327   
4328   return NULL;
4329 }
4330
4331 static void
4332 create_async_thread (GSimpleAsyncResult *res,
4333                      GObject            *object,
4334                      GCancellable       *cancellable)
4335 {
4336   GFileIface *iface;
4337   GFileCreateFlags *data;
4338   GFileOutputStream *stream;
4339   GError *error = NULL;
4340
4341   iface = G_FILE_GET_IFACE (object);
4342
4343   data = g_simple_async_result_get_op_res_gpointer (res);
4344
4345   stream = iface->create (G_FILE (object), *data, cancellable, &error);
4346
4347   if (stream == NULL)
4348     {
4349       g_simple_async_result_set_from_error (res, error);
4350       g_error_free (error);
4351     }
4352   else
4353     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4354 }
4355
4356 static void
4357 g_file_real_create_async (GFile               *file,
4358                           GFileCreateFlags     flags,
4359                           int                  io_priority,
4360                           GCancellable        *cancellable,
4361                           GAsyncReadyCallback  callback,
4362                           gpointer             user_data)
4363 {
4364   GFileCreateFlags *data;
4365   GSimpleAsyncResult *res;
4366
4367   data = g_new0 (GFileCreateFlags, 1);
4368   *data = flags;
4369
4370   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4371   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4372
4373   g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4374   g_object_unref (res);
4375 }
4376
4377 static GFileOutputStream *
4378 g_file_real_create_finish (GFile         *file,
4379                            GAsyncResult  *res,
4380                            GError       **error)
4381 {
4382   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4383   gpointer op;
4384
4385   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4386
4387   op = g_simple_async_result_get_op_res_gpointer (simple);
4388   if (op)
4389     return g_object_ref (op);
4390   
4391   return NULL;
4392 }
4393
4394 typedef struct {
4395   GFileOutputStream *stream;
4396   char *etag;
4397   gboolean make_backup;
4398   GFileCreateFlags flags;
4399 } ReplaceAsyncData;
4400
4401 static void
4402 replace_async_data_free (ReplaceAsyncData *data)
4403 {
4404   if (data->stream)
4405     g_object_unref (data->stream);
4406   g_free (data->etag);
4407   g_free (data);
4408 }
4409
4410 static void
4411 replace_async_thread (GSimpleAsyncResult *res,
4412                       GObject            *object,
4413                       GCancellable       *cancellable)
4414 {
4415   GFileIface *iface;
4416   GFileOutputStream *stream;
4417   GError *error = NULL;
4418   ReplaceAsyncData *data;
4419
4420   iface = G_FILE_GET_IFACE (object);
4421   
4422   data = g_simple_async_result_get_op_res_gpointer (res);
4423
4424   stream = iface->replace (G_FILE (object),
4425                            data->etag,
4426                            data->make_backup,
4427                            data->flags,
4428                            cancellable,
4429                            &error);
4430
4431   if (stream == NULL)
4432     {
4433       g_simple_async_result_set_from_error (res, error);
4434       g_error_free (error);
4435     }
4436   else
4437     data->stream = stream;
4438 }
4439
4440 static void
4441 g_file_real_replace_async (GFile               *file,
4442                            const char          *etag,
4443                            gboolean             make_backup,
4444                            GFileCreateFlags     flags,
4445                            int                  io_priority,
4446                            GCancellable        *cancellable,
4447                            GAsyncReadyCallback  callback,
4448                            gpointer             user_data)
4449 {
4450   GSimpleAsyncResult *res;
4451   ReplaceAsyncData *data;
4452
4453   data = g_new0 (ReplaceAsyncData, 1);
4454   data->etag = g_strdup (etag);
4455   data->make_backup = make_backup;
4456   data->flags = flags;
4457
4458   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4459   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4460
4461   g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4462   g_object_unref (res);
4463 }
4464
4465 static GFileOutputStream *
4466 g_file_real_replace_finish (GFile         *file,
4467                             GAsyncResult  *res,
4468                             GError       **error)
4469 {
4470   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4471   ReplaceAsyncData *data;
4472
4473   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4474
4475   data = g_simple_async_result_get_op_res_gpointer (simple);
4476   if (data->stream)
4477     return g_object_ref (data->stream);
4478   
4479   return NULL;
4480 }
4481
4482 typedef struct {
4483   char *name;
4484   GFile *file;
4485 } SetDisplayNameAsyncData;
4486
4487 static void
4488 set_display_name_data_free (SetDisplayNameAsyncData *data)
4489 {
4490   g_free (data->name);
4491   if (data->file)
4492     g_object_unref (data->file);
4493   g_free (data);
4494 }
4495
4496 static void
4497 set_display_name_async_thread (GSimpleAsyncResult *res,
4498                                GObject            *object,
4499                                GCancellable       *cancellable)
4500 {
4501   GError *error = NULL;
4502   SetDisplayNameAsyncData *data;
4503   GFile *file;
4504   
4505   data = g_simple_async_result_get_op_res_gpointer (res);
4506   
4507   file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4508
4509   if (file == NULL)
4510     {
4511       g_simple_async_result_set_from_error (res, error);
4512       g_error_free (error);
4513     }
4514   else
4515     data->file = file;
4516 }
4517
4518 static void
4519 g_file_real_set_display_name_async (GFile               *file,  
4520                                     const char          *display_name,
4521                                     int                  io_priority,
4522                                     GCancellable        *cancellable,
4523                                     GAsyncReadyCallback  callback,
4524                                     gpointer             user_data)
4525 {
4526   GSimpleAsyncResult *res;
4527   SetDisplayNameAsyncData *data;
4528
4529   data = g_new0 (SetDisplayNameAsyncData, 1);
4530   data->name = g_strdup (display_name);
4531   
4532   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4533   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4534   
4535   g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4536   g_object_unref (res);
4537 }
4538
4539 static GFile *
4540 g_file_real_set_display_name_finish (GFile         *file,
4541                                      GAsyncResult  *res,
4542                                      GError       **error)
4543 {
4544   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4545   SetDisplayNameAsyncData *data;
4546
4547   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4548
4549   data = g_simple_async_result_get_op_res_gpointer (simple);
4550   if (data->file)
4551     return g_object_ref (data->file);
4552   
4553   return NULL;
4554 }
4555
4556 typedef struct {
4557   GFileQueryInfoFlags flags;
4558   GFileInfo *info;
4559   gboolean res;
4560   GError *error;
4561 } SetInfoAsyncData;
4562
4563 static void
4564 set_info_data_free (SetInfoAsyncData *data)
4565 {
4566   if (data->info)
4567     g_object_unref (data->info);
4568   if (data->error)
4569     g_error_free (data->error);
4570   g_free (data);
4571 }
4572
4573 static void
4574 set_info_async_thread (GSimpleAsyncResult *res,
4575                        GObject            *object,
4576                        GCancellable       *cancellable)
4577 {
4578   SetInfoAsyncData *data;
4579   
4580   data = g_simple_async_result_get_op_res_gpointer (res);
4581   
4582   data->error = NULL;
4583   data->res = g_file_set_attributes_from_info (G_FILE (object),
4584                                                data->info,
4585                                                data->flags,
4586                                                cancellable,
4587                                                &data->error);
4588 }
4589
4590 static void
4591 g_file_real_set_attributes_async (GFile               *file,
4592                                   GFileInfo           *info,
4593                                   GFileQueryInfoFlags  flags,
4594                                   int                  io_priority,
4595                                   GCancellable        *cancellable,
4596                                   GAsyncReadyCallback  callback,
4597                                   gpointer             user_data)
4598 {
4599   GSimpleAsyncResult *res;
4600   SetInfoAsyncData *data;
4601
4602   data = g_new0 (SetInfoAsyncData, 1);
4603   data->info = g_file_info_dup (info);
4604   data->flags = flags;
4605   
4606   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4607   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4608   
4609   g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4610   g_object_unref (res);
4611 }
4612
4613 static gboolean
4614 g_file_real_set_attributes_finish (GFile         *file,
4615                                    GAsyncResult  *res,
4616                                    GFileInfo    **info,
4617                                    GError       **error)
4618 {
4619   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4620   SetInfoAsyncData *data;
4621   
4622   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4623
4624   data = g_simple_async_result_get_op_res_gpointer (simple);
4625
4626   if (info) 
4627     *info = g_object_ref (data->info);
4628
4629   if (error != NULL && data->error) 
4630     *error = g_error_copy (data->error);
4631   
4632   return data->res;
4633 }
4634
4635 static void
4636 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4637                                     GObject            *object,
4638                                     GCancellable       *cancellable)
4639 {
4640   GError *error = NULL;
4641   GMount *mount;
4642   
4643   mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4644
4645   if (mount == NULL)
4646     {
4647       g_simple_async_result_set_from_error (res, error);
4648       g_error_free (error);
4649     }
4650   else
4651     g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4652 }
4653
4654 static void
4655 g_file_real_find_enclosing_mount_async (GFile               *file,
4656                                         int                  io_priority,
4657                                         GCancellable        *cancellable,
4658                                         GAsyncReadyCallback  callback,
4659                                         gpointer             user_data)
4660 {
4661   GSimpleAsyncResult *res;
4662   
4663   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4664   
4665   g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4666   g_object_unref (res);
4667 }
4668
4669 static GMount *
4670 g_file_real_find_enclosing_mount_finish (GFile         *file,
4671                                           GAsyncResult  *res,
4672                                           GError       **error)
4673 {
4674   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4675   GMount *mount;
4676
4677   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4678
4679   mount = g_simple_async_result_get_op_res_gpointer (simple);
4680   return g_object_ref (mount);
4681 }
4682
4683
4684 typedef struct {
4685   GFile *source;
4686   GFile *destination;
4687   GFileCopyFlags flags;
4688   GFileProgressCallback progress_cb;
4689   gpointer progress_cb_data;
4690   GIOSchedulerJob *job;
4691 } CopyAsyncData;
4692
4693 static void
4694 copy_async_data_free (CopyAsyncData *data)
4695 {
4696   g_object_unref (data->source);
4697   g_object_unref (data->destination);
4698   g_free (data);
4699 }
4700
4701 typedef struct {
4702   CopyAsyncData *data;
4703   goffset current_num_bytes;
4704   goffset total_num_bytes;
4705 } ProgressData;
4706
4707 static gboolean
4708 copy_async_progress_in_main (gpointer user_data)
4709 {
4710   ProgressData *progress = user_data;
4711   CopyAsyncData *data = progress->data;
4712
4713   data->progress_cb (progress->current_num_bytes,
4714                      progress->total_num_bytes,
4715                      data->progress_cb_data);
4716
4717   return FALSE;
4718 }
4719
4720 static gboolean
4721 mainloop_barrier (gpointer user_data)
4722 {
4723   /* Does nothing, but ensures all queued idles before
4724      this are run */
4725   return FALSE;
4726 }
4727
4728
4729 static void
4730 copy_async_progress_callback (goffset  current_num_bytes,
4731                               goffset  total_num_bytes,
4732                               gpointer user_data)
4733 {
4734   CopyAsyncData *data = user_data;
4735   ProgressData *progress;
4736
4737   progress = g_new (ProgressData, 1);
4738   progress->data = data;
4739   progress->current_num_bytes = current_num_bytes;
4740   progress->total_num_bytes = total_num_bytes;
4741   
4742   g_io_scheduler_job_send_to_mainloop_async (data->job,
4743                                              copy_async_progress_in_main,
4744                                              progress,
4745                                              g_free);
4746 }
4747
4748 static gboolean
4749 copy_async_thread (GIOSchedulerJob *job,
4750                    GCancellable    *cancellable,
4751                    gpointer         user_data)
4752 {
4753   GSimpleAsyncResult *res;
4754   CopyAsyncData *data;
4755   gboolean result;
4756   GError *error;
4757
4758   res = user_data;
4759   data = g_simple_async_result_get_op_res_gpointer (res);
4760
4761   error = NULL;
4762   data->job = job;
4763   result = g_file_copy (data->source,
4764                         data->destination,
4765                         data->flags,
4766                         cancellable,
4767                         (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4768                         data,
4769                         &error);
4770
4771   /* Ensure all progress callbacks are done running in main thread */
4772   if (data->progress_cb != NULL)
4773     g_io_scheduler_job_send_to_mainloop (job,
4774                                          mainloop_barrier,
4775                                          NULL, NULL);
4776   
4777   if (!result)
4778     {
4779       g_simple_async_result_set_from_error (res, error);
4780       g_error_free (error);
4781     }
4782
4783   g_simple_async_result_complete_in_idle (res);
4784
4785   return FALSE;
4786 }
4787
4788 static void
4789 g_file_real_copy_async (GFile                  *source,
4790                         GFile                  *destination,
4791                         GFileCopyFlags          flags,
4792                         int                     io_priority,
4793                         GCancellable           *cancellable,
4794                         GFileProgressCallback   progress_callback,
4795                         gpointer                progress_callback_data,
4796                         GAsyncReadyCallback     callback,
4797                         gpointer                user_data)
4798 {
4799   GSimpleAsyncResult *res;
4800   CopyAsyncData *data;
4801
4802   data = g_new0 (CopyAsyncData, 1);
4803   data->source = g_object_ref (source);
4804   data->destination = g_object_ref (destination);
4805   data->flags = flags;
4806   data->progress_cb = progress_callback;
4807   data->progress_cb_data = progress_callback_data;
4808
4809   res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4810   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4811
4812   g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4813 }
4814
4815 static gboolean
4816 g_file_real_copy_finish (GFile        *file,
4817                          GAsyncResult *res,
4818                          GError      **error)
4819 {
4820   /* Error handled in g_file_copy_finish() */
4821   return TRUE;
4822 }
4823
4824
4825 /********************************************
4826  *   Default VFS operations                 *
4827  ********************************************/
4828
4829 /**
4830  * g_file_new_for_path:
4831  * @path: a string containing a relative or absolute path.
4832  * 
4833  * Constructs a #GFile for a given path. This operation never
4834  * fails, but the returned object might not support any I/O
4835  * operation if @path is malformed.
4836  * 
4837  * Returns: a new #GFile for the given @path. 
4838  **/
4839 GFile *
4840 g_file_new_for_path (const char *path)
4841 {
4842   g_return_val_if_fail (path != NULL, NULL);
4843
4844   return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4845 }
4846  
4847 /**
4848  * g_file_new_for_uri:
4849  * @uri: a string containing a URI.
4850  * 
4851  * Constructs a #GFile for a given URI. This operation never 
4852  * fails, but the returned object might not support any I/O 
4853  * operation if @uri is malformed or if the uri type is 
4854  * not supported.
4855  * 
4856  * Returns: a #GFile for the given @uri.
4857  **/ 
4858 GFile *
4859 g_file_new_for_uri (const char *uri)
4860 {
4861   g_return_val_if_fail (uri != NULL, NULL);
4862
4863   return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4864 }
4865   
4866 /**
4867  * g_file_parse_name:
4868  * @parse_name: a file name or path to be parsed.
4869  * 
4870  * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4871  * This operation never fails, but the returned object might not support any I/O
4872  * operation if the @parse_name cannot be parsed.
4873  * 
4874  * Returns: a new #GFile.
4875  **/
4876 GFile *
4877 g_file_parse_name (const char *parse_name)
4878 {
4879   g_return_val_if_fail (parse_name != NULL, NULL);
4880
4881   return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4882 }
4883
4884 static gboolean
4885 is_valid_scheme_character (char c)
4886 {
4887   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4888 }
4889
4890 /* Following RFC 2396, valid schemes are built like:
4891  *       scheme        = alpha *( alpha | digit | "+" | "-" | "." )
4892  */
4893 static gboolean
4894 has_valid_scheme (const char *uri)
4895 {
4896   const char *p;
4897   
4898   p = uri;
4899   
4900   if (!g_ascii_isalpha (*p))
4901     return FALSE;
4902
4903   do {
4904     p++;
4905   } while (is_valid_scheme_character (*p));
4906
4907   return *p == ':';
4908 }
4909
4910 /**
4911  * g_file_new_for_commandline_arg:
4912  * @arg: a command line string.
4913  * 
4914  * Creates a #GFile with the given argument from the command line. The value of
4915  * @arg can be either a URI, an absolute path or a relative path resolved
4916  * relative to the current working directory.
4917  * This operation never fails, but the returned object might not support any
4918  * I/O operation if @arg points to a malformed path.
4919  *
4920  * Returns: a new #GFile. 
4921  **/
4922 GFile *
4923 g_file_new_for_commandline_arg (const char *arg)
4924 {
4925   GFile *file;
4926   char *filename;
4927   char *current_dir;
4928   
4929   g_return_val_if_fail (arg != NULL, NULL);
4930   
4931   if (g_path_is_absolute (arg))
4932     return g_file_new_for_path (arg);
4933
4934   if (has_valid_scheme (arg))
4935     return g_file_new_for_uri (arg);
4936     
4937   current_dir = g_get_current_dir ();
4938   filename = g_build_filename (current_dir, arg, NULL);
4939   g_free (current_dir);
4940   
4941   file = g_file_new_for_path (filename);
4942   g_free (filename);
4943   
4944   return file;
4945 }
4946
4947 /**
4948  * g_file_mount_enclosing_volume:
4949  * @location: input #GFile.
4950  * @flags: flags affecting the operation
4951  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4952  * @cancellable: optional #GCancellable object, %NULL to ignore.
4953  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4954  * @user_data: the data to pass to callback function
4955  * 
4956  * Starts a @mount_operation, mounting the volume that contains the file @location. 
4957  * 
4958  * When this operation has completed, @callback will be called with
4959  * @user_user data, and the operation can be finalized with 
4960  * g_file_mount_enclosing_volume_finish().
4961  * 
4962  * If @cancellable is not %NULL, then the operation can be cancelled by
4963  * triggering the cancellable object from another thread. If the operation
4964  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4965  **/
4966 void
4967 g_file_mount_enclosing_volume (GFile               *location,
4968                                GMountMountFlags     flags,
4969                                GMountOperation     *mount_operation,
4970                                GCancellable        *cancellable,
4971                                GAsyncReadyCallback  callback,
4972                                gpointer             user_data)
4973 {
4974   GFileIface *iface;
4975
4976   g_return_if_fail (G_IS_FILE (location));
4977
4978   iface = G_FILE_GET_IFACE (location);
4979
4980   if (iface->mount_enclosing_volume == NULL)
4981     {
4982       g_simple_async_report_error_in_idle (G_OBJECT (location),
4983                                            callback, user_data,
4984                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4985                                            _("volume doesn't implement mount"));
4986       
4987       return;
4988     }
4989   
4990   (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4991
4992 }
4993
4994 /**
4995  * g_file_mount_enclosing_volume_finish:
4996  * @location: input #GFile.
4997  * @result: a #GAsyncResult.
4998  * @error: a #GError, or %NULL
4999  * 
5000  * Finishes a mount operation started by g_file_mount_enclosing_volume().
5001  * 
5002  * Returns: %TRUE if successful. If an error
5003  * has occurred, this function will return %FALSE and set @error
5004  * appropriately if present.
5005  **/
5006 gboolean
5007 g_file_mount_enclosing_volume_finish (GFile         *location,
5008                                       GAsyncResult  *result,
5009                                       GError       **error)
5010 {
5011   GFileIface *iface;
5012
5013   g_return_val_if_fail (G_IS_FILE (location), FALSE);
5014   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5015
5016   if (G_IS_SIMPLE_ASYNC_RESULT (result))
5017     {
5018       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
5019       if (g_simple_async_result_propagate_error (simple, error))
5020         return FALSE;
5021     }
5022   
5023   iface = G_FILE_GET_IFACE (location);
5024
5025   return (* iface->mount_enclosing_volume_finish) (location, result, error);
5026 }
5027
5028 /********************************************
5029  *   Utility functions                      *
5030  ********************************************/
5031
5032 /**
5033  * g_file_query_default_handler:
5034  * @file: a #GFile to open.
5035  * @cancellable: optional #GCancellable object, %NULL to ignore.
5036  * @error: a #GError, or %NULL
5037  *
5038  * Returns the #GAppInfo that is registered as the default
5039  * application to handle the file specified by @file.
5040  *
5041  * If @cancellable is not %NULL, then the operation can be cancelled by
5042  * triggering the cancellable object from another thread. If the operation
5043  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5044  *
5045  * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
5046  * When you are done with it, release it with g_object_unref()
5047  **/
5048 GAppInfo *
5049 g_file_query_default_handler (GFile                  *file,
5050                               GCancellable           *cancellable,
5051                               GError                **error)
5052 {
5053   char *uri_scheme;
5054   const char *content_type;
5055   GAppInfo *appinfo;
5056   GFileInfo *info;
5057   char *path;
5058   
5059   uri_scheme = g_file_get_uri_scheme (file);
5060   appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
5061   g_free (uri_scheme);
5062
5063   if (appinfo != NULL)
5064     return appinfo;
5065
5066   info = g_file_query_info (file,
5067                             G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
5068                             0,
5069                             cancellable,
5070                             error);
5071   if (info == NULL)
5072     return NULL;
5073
5074   appinfo = NULL;
5075
5076   content_type = g_file_info_get_content_type (info);
5077   if (content_type)
5078     {
5079       /* Don't use is_native(), as we want to support fuse paths if availible */
5080       path = g_file_get_path (file);
5081       appinfo = g_app_info_get_default_for_type (content_type,
5082                                                  path == NULL);
5083       g_free (path);
5084     }
5085   
5086   g_object_unref (info);
5087
5088   if (appinfo != NULL)
5089     return appinfo;
5090
5091   g_set_error_literal (error, G_IO_ERROR,
5092                        G_IO_ERROR_NOT_SUPPORTED,
5093                        _("No application is registered as handling this file"));
5094   return NULL;
5095   
5096 }
5097
5098
5099 #define GET_CONTENT_BLOCK_SIZE 8192
5100
5101 /**
5102  * g_file_load_contents:
5103  * @file: input #GFile.
5104  * @cancellable: optional #GCancellable object, %NULL to ignore.
5105  * @contents: a location to place the contents of the file.
5106  * @length: a location to place the length of the contents of the file,
5107  *    or %NULL if the length is not needed
5108  * @etag_out: a location to place the current entity tag for the file,
5109  *    or %NULL if the entity tag is not needed
5110  * @error: a #GError, or %NULL
5111  *
5112  * Loads the content of the file into memory. The data is always 
5113  * zero-terminated, but this is not included in the resultant @length.
5114  * The returned @content should be freed with g_free() when no longer
5115  * needed.
5116  * 
5117  * If @cancellable is not %NULL, then the operation can be cancelled by
5118  * triggering the cancellable object from another thread. If the operation
5119  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5120  * 
5121  * Returns: %TRUE if the @file's contents were successfully loaded.
5122  * %FALSE if there were errors.
5123  **/
5124 gboolean
5125 g_file_load_contents (GFile         *file,
5126                       GCancellable  *cancellable,
5127                       char         **contents,
5128                       gsize         *length,
5129                       char         **etag_out,
5130                       GError       **error)
5131 {
5132   GFileInputStream *in;
5133   GByteArray *content;
5134   gsize pos;
5135   gssize res;
5136   GFileInfo *info;
5137
5138   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5139   g_return_val_if_fail (contents != NULL, FALSE);
5140
5141   in = g_file_read (file, cancellable, error);
5142   if (in == NULL)
5143     return FALSE;
5144
5145   content = g_byte_array_new ();
5146   pos = 0;
5147   
5148   g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5149   while ((res = g_input_stream_read (G_INPUT_STREAM (in),
5150                                      content->data + pos,
5151                                      GET_CONTENT_BLOCK_SIZE,
5152                                      cancellable, error)) > 0)
5153     {
5154       pos += res;
5155       g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5156     }
5157
5158   if (etag_out)
5159     {
5160       *etag_out = NULL;
5161       
5162       info = g_file_input_stream_query_info (in,
5163                                              G_FILE_ATTRIBUTE_ETAG_VALUE,
5164                                              cancellable,
5165                                              NULL);
5166       if (info)
5167         {
5168           *etag_out = g_strdup (g_file_info_get_etag (info));
5169           g_object_unref (info);
5170         }
5171     } 
5172
5173   /* Ignore errors on close */
5174   g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
5175   g_object_unref (in);
5176
5177   if (res < 0)
5178     {
5179       /* error is set already */
5180       g_byte_array_free (content, TRUE);
5181       return FALSE;
5182     }
5183
5184   if (length)
5185     *length = pos;
5186
5187   /* Zero terminate (we got an extra byte allocated for this */
5188   content->data[pos] = 0;
5189   
5190   *contents = (char *)g_byte_array_free (content, FALSE);
5191   
5192   return TRUE;
5193 }
5194
5195 typedef struct {
5196   GFile *file;
5197   GError *error;
5198   GCancellable *cancellable;
5199   GFileReadMoreCallback read_more_callback;
5200   GAsyncReadyCallback callback;
5201   gpointer user_data;
5202   GByteArray *content;
5203   gsize pos;
5204   char *etag;
5205 } LoadContentsData;
5206
5207
5208 static void
5209 load_contents_data_free (LoadContentsData *data)
5210 {
5211   if (data->error)
5212     g_error_free (data->error);
5213   if (data->cancellable)
5214     g_object_unref (data->cancellable);
5215   if (data->content)
5216     g_byte_array_free (data->content, TRUE);
5217   g_free (data->etag);
5218   g_object_unref (data->file);
5219   g_free (data);
5220 }
5221
5222 static void
5223 load_contents_close_callback (GObject      *obj,
5224                               GAsyncResult *close_res,
5225                               gpointer      user_data)
5226 {
5227   GInputStream *stream = G_INPUT_STREAM (obj);
5228   LoadContentsData *data = user_data;
5229   GSimpleAsyncResult *res;
5230
5231   /* Ignore errors here, we're only reading anyway */
5232   g_input_stream_close_finish (stream, close_res, NULL);
5233   g_object_unref (stream);
5234
5235   res = g_simple_async_result_new (G_OBJECT (data->file),
5236                                    data->callback,
5237                                    data->user_data,
5238                                    g_file_load_contents_async);
5239   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5240   g_simple_async_result_complete (res);
5241   g_object_unref (res);
5242 }
5243
5244 static void
5245 load_contents_fstat_callback (GObject      *obj,
5246                               GAsyncResult *stat_res,
5247                               gpointer      user_data)
5248 {
5249   GInputStream *stream = G_INPUT_STREAM (obj);
5250   LoadContentsData *data = user_data;
5251   GFileInfo *info;
5252
5253   info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5254                                                    stat_res, NULL);
5255   if (info)
5256     {
5257       data->etag = g_strdup (g_file_info_get_etag (info));
5258       g_object_unref (info);
5259     }
5260
5261   g_input_stream_close_async (stream, 0,
5262                               data->cancellable,
5263                               load_contents_close_callback, data);
5264 }
5265
5266 static void
5267 load_contents_read_callback (GObject      *obj,
5268                              GAsyncResult *read_res,
5269                              gpointer      user_data)
5270 {
5271   GInputStream *stream = G_INPUT_STREAM (obj);
5272   LoadContentsData *data = user_data;
5273   GError *error = NULL;
5274   gssize read_size;
5275
5276   read_size = g_input_stream_read_finish (stream, read_res, &error);
5277
5278   if (read_size < 0) 
5279     {
5280       /* Error or EOF, close the file */
5281       data->error = error;
5282       g_input_stream_close_async (stream, 0,
5283                                   data->cancellable,
5284                                   load_contents_close_callback, data);
5285     }
5286   else if (read_size == 0)
5287     {
5288       g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5289                                             G_FILE_ATTRIBUTE_ETAG_VALUE,
5290                                             0,
5291                                             data->cancellable,
5292                                             load_contents_fstat_callback,
5293                                             data);
5294     }
5295   else if (read_size > 0)
5296     {
5297       data->pos += read_size;
5298       
5299       g_byte_array_set_size (data->content,
5300                              data->pos + GET_CONTENT_BLOCK_SIZE);
5301
5302
5303       if (data->read_more_callback &&
5304           !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5305         g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5306                                               G_FILE_ATTRIBUTE_ETAG_VALUE,
5307                                               0,
5308                                               data->cancellable,
5309                                               load_contents_fstat_callback,
5310                                               data);
5311       else 
5312         g_input_stream_read_async (stream,
5313                                    data->content->data + data->pos,
5314                                    GET_CONTENT_BLOCK_SIZE,
5315                                    0,
5316                                    data->cancellable,
5317                                    load_contents_read_callback,
5318                                    data);
5319     }
5320 }
5321
5322 static void
5323 load_contents_open_callback (GObject      *obj,
5324                              GAsyncResult *open_res,
5325                              gpointer      user_data)
5326 {
5327   GFile *file = G_FILE (obj);
5328   GFileInputStream *stream;
5329   LoadContentsData *data = user_data;
5330   GError *error = NULL;
5331   GSimpleAsyncResult *res;
5332
5333   stream = g_file_read_finish (file, open_res, &error);
5334
5335   if (stream)
5336     {
5337       g_byte_array_set_size (data->content,
5338                              data->pos + GET_CONTENT_BLOCK_SIZE);
5339       g_input_stream_read_async (G_INPUT_STREAM (stream),
5340                                  data->content->data + data->pos,
5341                                  GET_CONTENT_BLOCK_SIZE,
5342                                  0,
5343                                  data->cancellable,
5344                                  load_contents_read_callback,
5345                                  data);
5346       
5347     }
5348   else
5349     {
5350       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5351                                                   data->callback,
5352                                                   data->user_data,
5353                                                   error);
5354       g_simple_async_result_complete (res);
5355       g_error_free (error);
5356       load_contents_data_free (data);
5357       g_object_unref (res);
5358     }
5359 }
5360
5361 /**
5362  * g_file_load_partial_contents_async:
5363  * @file: input #GFile.
5364  * @cancellable: optional #GCancellable object, %NULL to ignore.
5365  * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5366  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5367  * @user_data: the data to pass to the callback functions.
5368  *
5369  * Reads the partial contents of a file. A #GFileReadMoreCallback should be 
5370  * used to stop reading from the file when appropriate, else this function
5371  * will behave exactly as g_file_load_contents_async(). This operation 
5372  * can be finished by g_file_load_partial_contents_finish().
5373  *
5374  * Users of this function should be aware that @user_data is passed to 
5375  * both the @read_more_callback and the @callback.
5376  *
5377  * If @cancellable is not %NULL, then the operation can be cancelled by
5378  * triggering the cancellable object from another thread. If the operation
5379  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5380  **/
5381 void
5382 g_file_load_partial_contents_async (GFile                 *file,
5383                                     GCancellable          *cancellable,
5384                                     GFileReadMoreCallback  read_more_callback,
5385                                     GAsyncReadyCallback    callback,
5386                                     gpointer               user_data)
5387 {
5388   LoadContentsData *data;
5389
5390   g_return_if_fail (G_IS_FILE (file));
5391
5392   data = g_new0 (LoadContentsData, 1);
5393
5394   if (cancellable)
5395     data->cancellable = g_object_ref (cancellable);
5396   data->read_more_callback = read_more_callback;
5397   data->callback = callback;
5398   data->user_data = user_data;
5399   data->content = g_byte_array_new ();
5400   data->file = g_object_ref (file);
5401
5402   g_file_read_async (file,
5403                      0,
5404                      cancellable,
5405                      load_contents_open_callback,
5406                      data);
5407 }
5408
5409 /**
5410  * g_file_load_partial_contents_finish:
5411  * @file: input #GFile.
5412  * @res: a #GAsyncResult. 
5413  * @contents: a location to place the contents of the file.
5414  * @length: a location to place the length of the contents of the file,
5415  *     or %NULL if the length is not needed
5416  * @etag_out: a location to place the current entity tag for the file,
5417  *     or %NULL if the entity tag is not needed
5418  * @error: a #GError, or %NULL
5419  * 
5420  * Finishes an asynchronous partial load operation that was started
5421  * with g_file_load_partial_contents_async(). The data is always 
5422  * zero-terminated, but this is not included in the resultant @length.
5423  * The returned @content should be freed with g_free() when no longer
5424  * needed.
5425  *
5426  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
5427  * present, it will be set appropriately. 
5428  **/
5429 gboolean
5430 g_file_load_partial_contents_finish (GFile         *file,
5431                                      GAsyncResult  *res,
5432                                      char         **contents,
5433                                      gsize         *length,
5434                                      char         **etag_out,
5435                                      GError       **error)
5436 {
5437   GSimpleAsyncResult *simple;
5438   LoadContentsData *data;
5439
5440   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5441   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5442   g_return_val_if_fail (contents != NULL, FALSE);
5443
5444   simple = G_SIMPLE_ASYNC_RESULT (res);
5445
5446   if (g_simple_async_result_propagate_error (simple, error))
5447     return FALSE;
5448   
5449   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5450   
5451   data = g_simple_async_result_get_op_res_gpointer (simple);
5452
5453   if (data->error)
5454     {
5455       g_propagate_error (error, data->error);
5456       data->error = NULL;
5457       *contents = NULL;
5458       if (length)
5459         *length = 0;
5460       return FALSE;
5461     }
5462
5463   if (length)
5464     *length = data->pos;
5465
5466   if (etag_out)
5467     {
5468       *etag_out = data->etag;
5469       data->etag = NULL;
5470     }
5471
5472   /* Zero terminate */
5473   g_byte_array_set_size (data->content, data->pos + 1);
5474   data->content->data[data->pos] = 0;
5475   
5476   *contents = (char *)g_byte_array_free (data->content, FALSE);
5477   data->content = NULL;
5478
5479   return TRUE;
5480 }
5481
5482 /**
5483  * g_file_load_contents_async:
5484  * @file: input #GFile.
5485  * @cancellable: optional #GCancellable object, %NULL to ignore.
5486  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5487  * @user_data: the data to pass to callback function
5488  * 
5489  * Starts an asynchronous load of the @file's contents.
5490  *
5491  * For more details, see g_file_load_contents() which is
5492  * the synchronous version of this call.
5493  *
5494  * When the load operation has completed, @callback will be called 
5495  * with @user data. To finish the operation, call 
5496  * g_file_load_contents_finish() with the #GAsyncResult returned by 
5497  * the @callback.
5498  * 
5499  * If @cancellable is not %NULL, then the operation can be cancelled by
5500  * triggering the cancellable object from another thread. If the operation
5501  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5502  **/
5503 void
5504 g_file_load_contents_async (GFile               *file,
5505                            GCancellable        *cancellable,
5506                            GAsyncReadyCallback  callback,
5507                            gpointer             user_data)
5508 {
5509   g_file_load_partial_contents_async (file,
5510                                       cancellable,
5511                                       NULL,
5512                                       callback, user_data);
5513 }
5514
5515 /**
5516  * g_file_load_contents_finish:
5517  * @file: input #GFile.
5518  * @res: a #GAsyncResult. 
5519  * @contents: a location to place the contents of the file.
5520  * @length: a location to place the length of the contents of the file,
5521  *     or %NULL if the length is not needed
5522  * @etag_out: a location to place the current entity tag for the file,
5523  *     or %NULL if the entity tag is not needed
5524  * @error: a #GError, or %NULL
5525  * 
5526  * Finishes an asynchronous load of the @file's contents. 
5527  * The contents are placed in @contents, and @length is set to the 
5528  * size of the @contents string. The @content should be freed with
5529  * g_free() when no longer needed. If @etag_out is present, it will be 
5530  * set to the new entity tag for the @file.
5531  * 
5532  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
5533  * present, it will be set appropriately. 
5534  **/
5535 gboolean
5536 g_file_load_contents_finish (GFile         *file,
5537                              GAsyncResult  *res,
5538                              char         **contents,
5539                              gsize         *length,
5540                              char         **etag_out,
5541                              GError       **error)
5542 {
5543   return g_file_load_partial_contents_finish (file,
5544                                               res,
5545                                               contents,
5546                                               length,
5547                                               etag_out,
5548                                               error);
5549 }
5550   
5551 /**
5552  * g_file_replace_contents:
5553  * @file: input #GFile.
5554  * @contents: a string containing the new contents for @file.
5555  * @length: the length of @contents in bytes.
5556  * @etag: the old <link linkend="gfile-etag">entity tag</link> 
5557  *     for the document, or %NULL
5558  * @make_backup: %TRUE if a backup should be created.
5559  * @flags: a set of #GFileCreateFlags.
5560  * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5561  *      for the document. This should be freed with g_free() when no longer 
5562  *      needed, or %NULL
5563  * @cancellable: optional #GCancellable object, %NULL to ignore.
5564  * @error: a #GError, or %NULL
5565  *
5566  * Replaces the contents of @file with @contents of @length bytes.
5567  
5568  * If @etag is specified (not %NULL) any existing file must have that etag, or
5569  * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5570  *
5571  * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5572  * 
5573  * If @cancellable is not %NULL, then the operation can be cancelled by
5574  * triggering the cancellable object from another thread. If the operation
5575  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5576  *
5577  * The returned @new_etag can be used to verify that the file hasn't changed the
5578  * next time it is saved over.
5579  * 
5580  * Returns: %TRUE if successful. If an error
5581  * has occurred, this function will return %FALSE and set @error
5582  * appropriately if present.
5583  **/
5584 gboolean
5585 g_file_replace_contents (GFile             *file,
5586                          const char        *contents,
5587                          gsize              length,
5588                          const char        *etag,
5589                          gboolean           make_backup,
5590                          GFileCreateFlags   flags,
5591                          char             **new_etag,
5592                          GCancellable      *cancellable,
5593                          GError           **error)
5594 {
5595   GFileOutputStream *out;
5596   gsize pos, remainder;
5597   gssize res;
5598   gboolean ret;
5599
5600   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5601   g_return_val_if_fail (contents != NULL, FALSE);
5602
5603   out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5604   if (out == NULL)
5605     return FALSE;
5606
5607   pos = 0;
5608   remainder = length;
5609   while (remainder > 0 &&
5610          (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5611                                        contents + pos,
5612                                        MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5613                                        cancellable,
5614                                        error)) > 0)
5615     {
5616       pos += res;
5617       remainder -= res;
5618     }
5619   
5620   if (remainder > 0 && res < 0)
5621     {
5622       /* Ignore errors on close */
5623       g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5624       g_object_unref (out);
5625
5626       /* error is set already */
5627       return FALSE;
5628     }
5629   
5630   ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
5631
5632   if (new_etag)
5633     *new_etag = g_file_output_stream_get_etag (out);
5634
5635   g_object_unref (out);
5636
5637   return ret;
5638 }
5639
5640 typedef struct {
5641   GFile *file;
5642   GError *error;
5643   GCancellable *cancellable;
5644   GAsyncReadyCallback callback;
5645   gpointer user_data;
5646   const char *content;
5647   gsize length;
5648   gsize pos;
5649   char *etag;
5650 } ReplaceContentsData;
5651
5652 static void
5653 replace_contents_data_free (ReplaceContentsData *data)
5654 {
5655   if (data->error)
5656     g_error_free (data->error);
5657   if (data->cancellable)
5658     g_object_unref (data->cancellable);
5659   g_object_unref (data->file);
5660   g_free (data->etag);
5661   g_free (data);
5662 }
5663
5664 static void
5665 replace_contents_close_callback (GObject      *obj,
5666                                  GAsyncResult *close_res,
5667                                  gpointer      user_data)
5668 {
5669   GOutputStream *stream = G_OUTPUT_STREAM (obj);
5670   ReplaceContentsData *data = user_data;
5671   GSimpleAsyncResult *res;
5672
5673   /* Ignore errors here, we're only reading anyway */
5674   g_output_stream_close_finish (stream, close_res, NULL);
5675   g_object_unref (stream);
5676
5677   data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5678   
5679   res = g_simple_async_result_new (G_OBJECT (data->file),
5680                                    data->callback,
5681                                    data->user_data,
5682                                    g_file_replace_contents_async);
5683   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5684   g_simple_async_result_complete (res);
5685   g_object_unref (res);
5686 }
5687
5688 static void
5689 replace_contents_write_callback (GObject      *obj,
5690                                  GAsyncResult *read_res,
5691                                  gpointer      user_data)
5692 {
5693   GOutputStream *stream = G_OUTPUT_STREAM (obj);
5694   ReplaceContentsData *data = user_data;
5695   GError *error = NULL;
5696   gssize write_size;
5697   
5698   write_size = g_output_stream_write_finish (stream, read_res, &error);
5699
5700   if (write_size <= 0) 
5701     {
5702       /* Error or EOF, close the file */
5703       if (write_size < 0)
5704         data->error = error;
5705       g_output_stream_close_async (stream, 0,
5706                                    data->cancellable,
5707                                    replace_contents_close_callback, data);
5708     }
5709   else if (write_size > 0)
5710     {
5711       data->pos += write_size;
5712
5713       if (data->pos >= data->length)
5714         g_output_stream_close_async (stream, 0,
5715                                      data->cancellable,
5716                                      replace_contents_close_callback, data);
5717       else
5718         g_output_stream_write_async (stream,
5719                                      data->content + data->pos,
5720                                      data->length - data->pos,
5721                                      0,
5722                                      data->cancellable,
5723                                      replace_contents_write_callback,
5724                                      data);
5725     }
5726 }
5727
5728 static void
5729 replace_contents_open_callback (GObject      *obj,
5730                                 GAsyncResult *open_res,
5731                                 gpointer      user_data)
5732 {
5733   GFile *file = G_FILE (obj);
5734   GFileOutputStream *stream;
5735   ReplaceContentsData *data = user_data;
5736   GError *error = NULL;
5737   GSimpleAsyncResult *res;
5738
5739   stream = g_file_replace_finish (file, open_res, &error);
5740
5741   if (stream)
5742     {
5743       g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5744                                    data->content + data->pos,
5745                                    data->length - data->pos,
5746                                    0,
5747                                    data->cancellable,
5748                                    replace_contents_write_callback,
5749                                    data);
5750       
5751     }
5752   else
5753     {
5754       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5755                                                   data->callback,
5756                                                   data->user_data,
5757                                                   error);
5758       g_simple_async_result_complete (res);
5759       g_error_free (error);
5760       replace_contents_data_free (data);
5761       g_object_unref (res);
5762     }
5763 }
5764
5765 /**
5766  * g_file_replace_contents_async:
5767  * @file: input #GFile.
5768  * @contents: string of contents to replace the file with.
5769  * @length: the length of @contents in bytes.
5770  * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
5771  * @make_backup: %TRUE if a backup should be created.
5772  * @flags: a set of #GFileCreateFlags.
5773  * @cancellable: optional #GCancellable object, %NULL to ignore.
5774  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5775  * @user_data: the data to pass to callback function
5776  * 
5777  * Starts an asynchronous replacement of @file with the given 
5778  * @contents of @length bytes. @etag will replace the document's 
5779  * current entity tag.
5780  * 
5781  * When this operation has completed, @callback will be called with
5782  * @user_user data, and the operation can be finalized with 
5783  * g_file_replace_contents_finish().
5784  * 
5785  * If @cancellable is not %NULL, then the operation can be cancelled by
5786  * triggering the cancellable object from another thread. If the operation
5787  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5788  * 
5789  * If @make_backup is %TRUE, this function will attempt to 
5790  * make a backup of @file.
5791  **/
5792 void
5793 g_file_replace_contents_async  (GFile               *file,
5794                                 const char          *contents,
5795                                 gsize                length,
5796                                 const char          *etag,
5797                                 gboolean             make_backup,
5798                                 GFileCreateFlags     flags,
5799                                 GCancellable        *cancellable,
5800                                 GAsyncReadyCallback  callback,
5801                                 gpointer             user_data)
5802 {
5803   ReplaceContentsData *data;
5804
5805   g_return_if_fail (G_IS_FILE (file));
5806   g_return_if_fail (contents != NULL);
5807
5808   data = g_new0 (ReplaceContentsData, 1);
5809
5810   if (cancellable)
5811     data->cancellable = g_object_ref (cancellable);
5812   data->callback = callback;
5813   data->user_data = user_data;
5814   data->content = contents;
5815   data->length = length;
5816   data->pos = 0;
5817   data->file = g_object_ref (file);
5818
5819   g_file_replace_async (file,
5820                         etag,
5821                         make_backup,
5822                         flags,
5823                         0,
5824                         cancellable,
5825                         replace_contents_open_callback,
5826                         data);
5827 }
5828   
5829 /**
5830  * g_file_replace_contents_finish:
5831  * @file: input #GFile.
5832  * @res: a #GAsyncResult. 
5833  * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link> 
5834  *     for the document. This should be freed with g_free() when it is no 
5835  *     longer needed, or %NULL
5836  * @error: a #GError, or %NULL 
5837  * 
5838  * Finishes an asynchronous replace of the given @file. See
5839  * g_file_replace_contents_async(). Sets @new_etag to the new entity 
5840  * tag for the document, if present.
5841  * 
5842  * Returns: %TRUE on success, %FALSE on failure.
5843  **/
5844 gboolean
5845 g_file_replace_contents_finish (GFile         *file,
5846                                 GAsyncResult  *res,
5847                                 char         **new_etag,
5848                                 GError       **error)
5849 {
5850   GSimpleAsyncResult *simple;
5851   ReplaceContentsData *data;
5852
5853   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5854   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5855
5856   simple = G_SIMPLE_ASYNC_RESULT (res);
5857
5858   if (g_simple_async_result_propagate_error (simple, error))
5859     return FALSE;
5860   
5861   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5862   
5863   data = g_simple_async_result_get_op_res_gpointer (simple);
5864
5865   if (data->error)
5866     {
5867       g_propagate_error (error, data->error);
5868       data->error = NULL;
5869       return FALSE;
5870     }
5871
5872
5873   if (new_etag)
5874     {
5875       *new_etag = data->etag;
5876       data->etag = NULL; /* Take ownership */
5877     }
5878   
5879   return TRUE;
5880 }
5881
5882 #define __G_FILE_C__
5883 #include "gioaliasdef.c"