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