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