Fix a segfault
[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   if (iface->read_fn == NULL)
4202     {
4203       g_set_error_literal (error, G_IO_ERROR,
4204                            G_IO_ERROR_NOT_SUPPORTED,
4205                            _("Operation not supported"));
4206
4207       g_simple_async_result_set_from_error (res, error);
4208       g_error_free (error);
4209
4210       return;
4211     }
4212   
4213   stream = iface->read_fn (G_FILE (object), cancellable, &error);
4214
4215   if (stream == NULL)
4216     {
4217       g_simple_async_result_set_from_error (res, error);
4218       g_error_free (error);
4219     }
4220   else
4221     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4222 }
4223
4224 static void
4225 g_file_real_read_async (GFile               *file,
4226                         int                  io_priority,
4227                         GCancellable        *cancellable,
4228                         GAsyncReadyCallback  callback,
4229                         gpointer             user_data)
4230 {
4231   GSimpleAsyncResult *res;
4232   
4233   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4234   
4235   g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4236   g_object_unref (res);
4237 }
4238
4239 static GFileInputStream *
4240 g_file_real_read_finish (GFile         *file,
4241                          GAsyncResult  *res,
4242                          GError       **error)
4243 {
4244   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4245   gpointer op;
4246
4247   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4248
4249   op = g_simple_async_result_get_op_res_gpointer (simple);
4250   if (op)
4251     return g_object_ref (op);
4252   
4253   return NULL;
4254 }
4255
4256 static void
4257 append_to_async_thread (GSimpleAsyncResult *res,
4258                         GObject            *object,
4259                         GCancellable       *cancellable)
4260 {
4261   GFileIface *iface;
4262   GFileCreateFlags *data;
4263   GFileOutputStream *stream;
4264   GError *error = NULL;
4265
4266   iface = G_FILE_GET_IFACE (object);
4267
4268   data = g_simple_async_result_get_op_res_gpointer (res);
4269
4270   stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4271
4272   if (stream == NULL)
4273     {
4274       g_simple_async_result_set_from_error (res, error);
4275       g_error_free (error);
4276     }
4277   else
4278     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4279 }
4280
4281 static void
4282 g_file_real_append_to_async (GFile               *file,
4283                              GFileCreateFlags     flags,
4284                              int                  io_priority,
4285                              GCancellable        *cancellable,
4286                              GAsyncReadyCallback  callback,
4287                              gpointer             user_data)
4288 {
4289   GFileCreateFlags *data;
4290   GSimpleAsyncResult *res;
4291
4292   data = g_new0 (GFileCreateFlags, 1);
4293   *data = flags;
4294
4295   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4296   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4297
4298   g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4299   g_object_unref (res);
4300 }
4301
4302 static GFileOutputStream *
4303 g_file_real_append_to_finish (GFile         *file,
4304                               GAsyncResult  *res,
4305                               GError       **error)
4306 {
4307   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4308   gpointer op;
4309
4310   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4311
4312   op = g_simple_async_result_get_op_res_gpointer (simple);
4313   if (op)
4314     return g_object_ref (op);
4315   
4316   return NULL;
4317 }
4318
4319 static void
4320 create_async_thread (GSimpleAsyncResult *res,
4321                      GObject            *object,
4322                      GCancellable       *cancellable)
4323 {
4324   GFileIface *iface;
4325   GFileCreateFlags *data;
4326   GFileOutputStream *stream;
4327   GError *error = NULL;
4328
4329   iface = G_FILE_GET_IFACE (object);
4330
4331   data = g_simple_async_result_get_op_res_gpointer (res);
4332
4333   stream = iface->create (G_FILE (object), *data, cancellable, &error);
4334
4335   if (stream == NULL)
4336     {
4337       g_simple_async_result_set_from_error (res, error);
4338       g_error_free (error);
4339     }
4340   else
4341     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4342 }
4343
4344 static void
4345 g_file_real_create_async (GFile               *file,
4346                           GFileCreateFlags     flags,
4347                           int                  io_priority,
4348                           GCancellable        *cancellable,
4349                           GAsyncReadyCallback  callback,
4350                           gpointer             user_data)
4351 {
4352   GFileCreateFlags *data;
4353   GSimpleAsyncResult *res;
4354
4355   data = g_new0 (GFileCreateFlags, 1);
4356   *data = flags;
4357
4358   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4359   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4360
4361   g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4362   g_object_unref (res);
4363 }
4364
4365 static GFileOutputStream *
4366 g_file_real_create_finish (GFile         *file,
4367                            GAsyncResult  *res,
4368                            GError       **error)
4369 {
4370   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4371   gpointer op;
4372
4373   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4374
4375   op = g_simple_async_result_get_op_res_gpointer (simple);
4376   if (op)
4377     return g_object_ref (op);
4378   
4379   return NULL;
4380 }
4381
4382 typedef struct {
4383   GFileOutputStream *stream;
4384   char *etag;
4385   gboolean make_backup;
4386   GFileCreateFlags flags;
4387 } ReplaceAsyncData;
4388
4389 static void
4390 replace_async_data_free (ReplaceAsyncData *data)
4391 {
4392   if (data->stream)
4393     g_object_unref (data->stream);
4394   g_free (data->etag);
4395   g_free (data);
4396 }
4397
4398 static void
4399 replace_async_thread (GSimpleAsyncResult *res,
4400                       GObject            *object,
4401                       GCancellable       *cancellable)
4402 {
4403   GFileIface *iface;
4404   GFileOutputStream *stream;
4405   GError *error = NULL;
4406   ReplaceAsyncData *data;
4407
4408   iface = G_FILE_GET_IFACE (object);
4409   
4410   data = g_simple_async_result_get_op_res_gpointer (res);
4411
4412   stream = iface->replace (G_FILE (object),
4413                            data->etag,
4414                            data->make_backup,
4415                            data->flags,
4416                            cancellable,
4417                            &error);
4418
4419   if (stream == NULL)
4420     {
4421       g_simple_async_result_set_from_error (res, error);
4422       g_error_free (error);
4423     }
4424   else
4425     data->stream = stream;
4426 }
4427
4428 static void
4429 g_file_real_replace_async (GFile               *file,
4430                            const char          *etag,
4431                            gboolean             make_backup,
4432                            GFileCreateFlags     flags,
4433                            int                  io_priority,
4434                            GCancellable        *cancellable,
4435                            GAsyncReadyCallback  callback,
4436                            gpointer             user_data)
4437 {
4438   GSimpleAsyncResult *res;
4439   ReplaceAsyncData *data;
4440
4441   data = g_new0 (ReplaceAsyncData, 1);
4442   data->etag = g_strdup (etag);
4443   data->make_backup = make_backup;
4444   data->flags = flags;
4445
4446   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4447   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4448
4449   g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4450   g_object_unref (res);
4451 }
4452
4453 static GFileOutputStream *
4454 g_file_real_replace_finish (GFile         *file,
4455                             GAsyncResult  *res,
4456                             GError       **error)
4457 {
4458   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4459   ReplaceAsyncData *data;
4460
4461   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4462
4463   data = g_simple_async_result_get_op_res_gpointer (simple);
4464   if (data->stream)
4465     return g_object_ref (data->stream);
4466   
4467   return NULL;
4468 }
4469
4470 typedef struct {
4471   char *name;
4472   GFile *file;
4473 } SetDisplayNameAsyncData;
4474
4475 static void
4476 set_display_name_data_free (SetDisplayNameAsyncData *data)
4477 {
4478   g_free (data->name);
4479   if (data->file)
4480     g_object_unref (data->file);
4481   g_free (data);
4482 }
4483
4484 static void
4485 set_display_name_async_thread (GSimpleAsyncResult *res,
4486                                GObject            *object,
4487                                GCancellable       *cancellable)
4488 {
4489   GError *error = NULL;
4490   SetDisplayNameAsyncData *data;
4491   GFile *file;
4492   
4493   data = g_simple_async_result_get_op_res_gpointer (res);
4494   
4495   file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4496
4497   if (file == NULL)
4498     {
4499       g_simple_async_result_set_from_error (res, error);
4500       g_error_free (error);
4501     }
4502   else
4503     data->file = file;
4504 }
4505
4506 static void
4507 g_file_real_set_display_name_async (GFile               *file,  
4508                                     const char          *display_name,
4509                                     int                  io_priority,
4510                                     GCancellable        *cancellable,
4511                                     GAsyncReadyCallback  callback,
4512                                     gpointer             user_data)
4513 {
4514   GSimpleAsyncResult *res;
4515   SetDisplayNameAsyncData *data;
4516
4517   data = g_new0 (SetDisplayNameAsyncData, 1);
4518   data->name = g_strdup (display_name);
4519   
4520   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4521   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4522   
4523   g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4524   g_object_unref (res);
4525 }
4526
4527 static GFile *
4528 g_file_real_set_display_name_finish (GFile         *file,
4529                                      GAsyncResult  *res,
4530                                      GError       **error)
4531 {
4532   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4533   SetDisplayNameAsyncData *data;
4534
4535   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4536
4537   data = g_simple_async_result_get_op_res_gpointer (simple);
4538   if (data->file)
4539     return g_object_ref (data->file);
4540   
4541   return NULL;
4542 }
4543
4544 typedef struct {
4545   GFileQueryInfoFlags flags;
4546   GFileInfo *info;
4547   gboolean res;
4548   GError *error;
4549 } SetInfoAsyncData;
4550
4551 static void
4552 set_info_data_free (SetInfoAsyncData *data)
4553 {
4554   if (data->info)
4555     g_object_unref (data->info);
4556   if (data->error)
4557     g_error_free (data->error);
4558   g_free (data);
4559 }
4560
4561 static void
4562 set_info_async_thread (GSimpleAsyncResult *res,
4563                        GObject            *object,
4564                        GCancellable       *cancellable)
4565 {
4566   SetInfoAsyncData *data;
4567   
4568   data = g_simple_async_result_get_op_res_gpointer (res);
4569   
4570   data->error = NULL;
4571   data->res = g_file_set_attributes_from_info (G_FILE (object),
4572                                                data->info,
4573                                                data->flags,
4574                                                cancellable,
4575                                                &data->error);
4576 }
4577
4578 static void
4579 g_file_real_set_attributes_async (GFile               *file,
4580                                   GFileInfo           *info,
4581                                   GFileQueryInfoFlags  flags,
4582                                   int                  io_priority,
4583                                   GCancellable        *cancellable,
4584                                   GAsyncReadyCallback  callback,
4585                                   gpointer             user_data)
4586 {
4587   GSimpleAsyncResult *res;
4588   SetInfoAsyncData *data;
4589
4590   data = g_new0 (SetInfoAsyncData, 1);
4591   data->info = g_file_info_dup (info);
4592   data->flags = flags;
4593   
4594   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4595   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4596   
4597   g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4598   g_object_unref (res);
4599 }
4600
4601 static gboolean
4602 g_file_real_set_attributes_finish (GFile         *file,
4603                                    GAsyncResult  *res,
4604                                    GFileInfo    **info,
4605                                    GError       **error)
4606 {
4607   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4608   SetInfoAsyncData *data;
4609   
4610   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4611
4612   data = g_simple_async_result_get_op_res_gpointer (simple);
4613
4614   if (info) 
4615     *info = g_object_ref (data->info);
4616
4617   if (error != NULL && data->error) 
4618     *error = g_error_copy (data->error);
4619   
4620   return data->res;
4621 }
4622
4623 static void
4624 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4625                                     GObject            *object,
4626                                     GCancellable       *cancellable)
4627 {
4628   GError *error = NULL;
4629   GMount *mount;
4630   
4631   mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4632
4633   if (mount == NULL)
4634     {
4635       g_simple_async_result_set_from_error (res, error);
4636       g_error_free (error);
4637     }
4638   else
4639     g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4640 }
4641
4642 static void
4643 g_file_real_find_enclosing_mount_async (GFile               *file,
4644                                         int                  io_priority,
4645                                         GCancellable        *cancellable,
4646                                         GAsyncReadyCallback  callback,
4647                                         gpointer             user_data)
4648 {
4649   GSimpleAsyncResult *res;
4650   
4651   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4652   
4653   g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4654   g_object_unref (res);
4655 }
4656
4657 static GMount *
4658 g_file_real_find_enclosing_mount_finish (GFile         *file,
4659                                           GAsyncResult  *res,
4660                                           GError       **error)
4661 {
4662   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4663   GMount *mount;
4664
4665   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4666
4667   mount = g_simple_async_result_get_op_res_gpointer (simple);
4668   return g_object_ref (mount);
4669 }
4670
4671
4672 typedef struct {
4673   GFile *source;
4674   GFile *destination;
4675   GFileCopyFlags flags;
4676   GFileProgressCallback progress_cb;
4677   gpointer progress_cb_data;
4678   GIOSchedulerJob *job;
4679 } CopyAsyncData;
4680
4681 static void
4682 copy_async_data_free (CopyAsyncData *data)
4683 {
4684   g_object_unref (data->source);
4685   g_object_unref (data->destination);
4686   g_free (data);
4687 }
4688
4689 typedef struct {
4690   CopyAsyncData *data;
4691   goffset current_num_bytes;
4692   goffset total_num_bytes;
4693 } ProgressData;
4694
4695 static gboolean
4696 copy_async_progress_in_main (gpointer user_data)
4697 {
4698   ProgressData *progress = user_data;
4699   CopyAsyncData *data = progress->data;
4700
4701   data->progress_cb (progress->current_num_bytes,
4702                      progress->total_num_bytes,
4703                      data->progress_cb_data);
4704
4705   return FALSE;
4706 }
4707
4708 static gboolean
4709 mainloop_barrier (gpointer user_data)
4710 {
4711   /* Does nothing, but ensures all queued idles before
4712      this are run */
4713   return FALSE;
4714 }
4715
4716
4717 static void
4718 copy_async_progress_callback (goffset  current_num_bytes,
4719                               goffset  total_num_bytes,
4720                               gpointer user_data)
4721 {
4722   CopyAsyncData *data = user_data;
4723   ProgressData *progress;
4724
4725   progress = g_new (ProgressData, 1);
4726   progress->data = data;
4727   progress->current_num_bytes = current_num_bytes;
4728   progress->total_num_bytes = total_num_bytes;
4729   
4730   g_io_scheduler_job_send_to_mainloop_async (data->job,
4731                                              copy_async_progress_in_main,
4732                                              progress,
4733                                              g_free);
4734 }
4735
4736 static gboolean
4737 copy_async_thread (GIOSchedulerJob *job,
4738                    GCancellable    *cancellable,
4739                    gpointer         user_data)
4740 {
4741   GSimpleAsyncResult *res;
4742   CopyAsyncData *data;
4743   gboolean result;
4744   GError *error;
4745
4746   res = user_data;
4747   data = g_simple_async_result_get_op_res_gpointer (res);
4748
4749   error = NULL;
4750   data->job = job;
4751   result = g_file_copy (data->source,
4752                         data->destination,
4753                         data->flags,
4754                         cancellable,
4755                         (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4756                         data,
4757                         &error);
4758
4759   /* Ensure all progress callbacks are done running in main thread */
4760   if (data->progress_cb != NULL)
4761     g_io_scheduler_job_send_to_mainloop (job,
4762                                          mainloop_barrier,
4763                                          NULL, NULL);
4764   
4765   if (!result)
4766     {
4767       g_simple_async_result_set_from_error (res, error);
4768       g_error_free (error);
4769     }
4770
4771   g_simple_async_result_complete_in_idle (res);
4772
4773   return FALSE;
4774 }
4775
4776 static void
4777 g_file_real_copy_async (GFile                  *source,
4778                         GFile                  *destination,
4779                         GFileCopyFlags          flags,
4780                         int                     io_priority,
4781                         GCancellable           *cancellable,
4782                         GFileProgressCallback   progress_callback,
4783                         gpointer                progress_callback_data,
4784                         GAsyncReadyCallback     callback,
4785                         gpointer                user_data)
4786 {
4787   GSimpleAsyncResult *res;
4788   CopyAsyncData *data;
4789
4790   data = g_new0 (CopyAsyncData, 1);
4791   data->source = g_object_ref (source);
4792   data->destination = g_object_ref (destination);
4793   data->flags = flags;
4794   data->progress_cb = progress_callback;
4795   data->progress_cb_data = progress_callback_data;
4796
4797   res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4798   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4799
4800   g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4801 }
4802
4803 static gboolean
4804 g_file_real_copy_finish (GFile        *file,
4805                          GAsyncResult *res,
4806                          GError      **error)
4807 {
4808   /* Error handled in g_file_copy_finish() */
4809   return TRUE;
4810 }
4811
4812
4813 /********************************************
4814  *   Default VFS operations                 *
4815  ********************************************/
4816
4817 /**
4818  * g_file_new_for_path:
4819  * @path: a string containing a relative or absolute path.
4820  * 
4821  * Constructs a #GFile for a given path. This operation never
4822  * fails, but the returned object might not support any I/O
4823  * operation if @path is malformed.
4824  * 
4825  * Returns: a new #GFile for the given @path. 
4826  **/
4827 GFile *
4828 g_file_new_for_path (const char *path)
4829 {
4830   g_return_val_if_fail (path != NULL, NULL);
4831
4832   return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4833 }
4834  
4835 /**
4836  * g_file_new_for_uri:
4837  * @uri: a string containing a URI.
4838  * 
4839  * Constructs a #GFile for a given URI. This operation never 
4840  * fails, but the returned object might not support any I/O 
4841  * operation if @uri is malformed or if the uri type is 
4842  * not supported.
4843  * 
4844  * Returns: a #GFile for the given @uri.
4845  **/ 
4846 GFile *
4847 g_file_new_for_uri (const char *uri)
4848 {
4849   g_return_val_if_fail (uri != NULL, NULL);
4850
4851   return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4852 }
4853   
4854 /**
4855  * g_file_parse_name:
4856  * @parse_name: a file name or path to be parsed.
4857  * 
4858  * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4859  * This operation never fails, but the returned object might not support any I/O
4860  * operation if the @parse_name cannot be parsed.
4861  * 
4862  * Returns: a new #GFile.
4863  **/
4864 GFile *
4865 g_file_parse_name (const char *parse_name)
4866 {
4867   g_return_val_if_fail (parse_name != NULL, NULL);
4868
4869   return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4870 }
4871
4872 static gboolean
4873 is_valid_scheme_character (char c)
4874 {
4875   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4876 }
4877
4878 /* Following RFC 2396, valid schemes are built like:
4879  *       scheme        = alpha *( alpha | digit | "+" | "-" | "." )
4880  */
4881 static gboolean
4882 has_valid_scheme (const char *uri)
4883 {
4884   const char *p;
4885   
4886   p = uri;
4887   
4888   if (!g_ascii_isalpha (*p))
4889     return FALSE;
4890
4891   do {
4892     p++;
4893   } while (is_valid_scheme_character (*p));
4894
4895   return *p == ':';
4896 }
4897
4898 /**
4899  * g_file_new_for_commandline_arg:
4900  * @arg: a command line string.
4901  * 
4902  * Creates a #GFile with the given argument from the command line. The value of
4903  * @arg can be either a URI, an absolute path or a relative path resolved
4904  * relative to the current working directory.
4905  * This operation never fails, but the returned object might not support any
4906  * I/O operation if @arg points to a malformed path.
4907  *
4908  * Returns: a new #GFile. 
4909  **/
4910 GFile *
4911 g_file_new_for_commandline_arg (const char *arg)
4912 {
4913   GFile *file;
4914   char *filename;
4915   char *current_dir;
4916   
4917   g_return_val_if_fail (arg != NULL, NULL);
4918   
4919   if (g_path_is_absolute (arg))
4920     return g_file_new_for_path (arg);
4921
4922   if (has_valid_scheme (arg))
4923     return g_file_new_for_uri (arg);
4924     
4925   current_dir = g_get_current_dir ();
4926   filename = g_build_filename (current_dir, arg, NULL);
4927   g_free (current_dir);
4928   
4929   file = g_file_new_for_path (filename);
4930   g_free (filename);
4931   
4932   return file;
4933 }
4934
4935 /**
4936  * g_file_mount_enclosing_volume:
4937  * @location: input #GFile.
4938  * @flags: flags affecting the operation
4939  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4940  * @cancellable: optional #GCancellable object, %NULL to ignore.
4941  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4942  * @user_data: the data to pass to callback function
4943  * 
4944  * Starts a @mount_operation, mounting the volume that contains the file @location. 
4945  * 
4946  * When this operation has completed, @callback will be called with
4947  * @user_user data, and the operation can be finalized with 
4948  * g_file_mount_enclosing_volume_finish().
4949  * 
4950  * If @cancellable is not %NULL, then the operation can be cancelled by
4951  * triggering the cancellable object from another thread. If the operation
4952  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4953  **/
4954 void
4955 g_file_mount_enclosing_volume (GFile               *location,
4956                                GMountMountFlags     flags,
4957                                GMountOperation     *mount_operation,
4958                                GCancellable        *cancellable,
4959                                GAsyncReadyCallback  callback,
4960                                gpointer             user_data)
4961 {
4962   GFileIface *iface;
4963
4964   g_return_if_fail (G_IS_FILE (location));
4965
4966   iface = G_FILE_GET_IFACE (location);
4967
4968   if (iface->mount_enclosing_volume == NULL)
4969     {
4970       g_simple_async_report_error_in_idle (G_OBJECT (location),
4971                                            callback, user_data,
4972                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4973                                            _("volume doesn't implement mount"));
4974       
4975       return;
4976     }
4977   
4978   (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4979
4980 }
4981
4982 /**
4983  * g_file_mount_enclosing_volume_finish:
4984  * @location: input #GFile.
4985  * @result: a #GAsyncResult.
4986  * @error: a #GError, or %NULL
4987  * 
4988  * Finishes a mount operation started by g_file_mount_enclosing_volume().
4989  * 
4990  * Returns: %TRUE if successful. If an error
4991  * has occurred, this function will return %FALSE and set @error
4992  * appropriately if present.
4993  **/
4994 gboolean
4995 g_file_mount_enclosing_volume_finish (GFile         *location,
4996                                       GAsyncResult  *result,
4997                                       GError       **error)
4998 {
4999   GFileIface *iface;
5000
5001   g_return_val_if_fail (G_IS_FILE (location), FALSE);
5002   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5003
5004   if (G_IS_SIMPLE_ASYNC_RESULT (result))
5005     {
5006       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
5007       if (g_simple_async_result_propagate_error (simple, error))
5008         return FALSE;
5009     }
5010   
5011   iface = G_FILE_GET_IFACE (location);
5012
5013   return (* iface->mount_enclosing_volume_finish) (location, result, error);
5014 }
5015
5016 /********************************************
5017  *   Utility functions                      *
5018  ********************************************/
5019
5020 /**
5021  * g_file_query_default_handler:
5022  * @file: a #GFile to open.
5023  * @cancellable: optional #GCancellable object, %NULL to ignore.
5024  * @error: a #GError, or %NULL
5025  *
5026  * Returns the #GAppInfo that is registered as the default
5027  * application to handle the file specified by @file.
5028  *
5029  * If @cancellable is not %NULL, then the operation can be cancelled by
5030  * triggering the cancellable object from another thread. If the operation
5031  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5032  *
5033  * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
5034  * When you are done with it, release it with g_object_unref()
5035  **/
5036 GAppInfo *
5037 g_file_query_default_handler (GFile                  *file,
5038                               GCancellable           *cancellable,
5039                               GError                **error)
5040 {
5041   char *uri_scheme;
5042   const char *content_type;
5043   GAppInfo *appinfo;
5044   GFileInfo *info;
5045   char *path;
5046   
5047   uri_scheme = g_file_get_uri_scheme (file);
5048   appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
5049   g_free (uri_scheme);
5050
5051   if (appinfo != NULL)
5052     return appinfo;
5053
5054   info = g_file_query_info (file,
5055                             G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
5056                             0,
5057                             cancellable,
5058                             error);
5059   if (info == NULL)
5060     return NULL;
5061
5062   appinfo = NULL;
5063
5064   content_type = g_file_info_get_content_type (info);
5065   if (content_type)
5066     {
5067       /* Don't use is_native(), as we want to support fuse paths if availible */
5068       path = g_file_get_path (file);
5069       appinfo = g_app_info_get_default_for_type (content_type,
5070                                                  path == NULL);
5071       g_free (path);
5072     }
5073   
5074   g_object_unref (info);
5075
5076   if (appinfo != NULL)
5077     return appinfo;
5078
5079   g_set_error_literal (error, G_IO_ERROR,
5080                        G_IO_ERROR_NOT_SUPPORTED,
5081                        _("No application is registered as handling this file"));
5082   return NULL;
5083   
5084 }
5085
5086
5087 #define GET_CONTENT_BLOCK_SIZE 8192
5088
5089 /**
5090  * g_file_load_contents:
5091  * @file: input #GFile.
5092  * @cancellable: optional #GCancellable object, %NULL to ignore.
5093  * @contents: a location to place the contents of the file.
5094  * @length: a location to place the length of the contents of the file,
5095  *    or %NULL if the length is not needed
5096  * @etag_out: a location to place the current entity tag for the file,
5097  *    or %NULL if the entity tag is not needed
5098  * @error: a #GError, or %NULL
5099  *
5100  * Loads the content of the file into memory. The data is always 
5101  * zero-terminated, but this is not included in the resultant @length.
5102  * The returned @content should be freed with g_free() when no longer
5103  * needed.
5104  * 
5105  * If @cancellable is not %NULL, then the operation can be cancelled by
5106  * triggering the cancellable object from another thread. If the operation
5107  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5108  * 
5109  * Returns: %TRUE if the @file's contents were successfully loaded.
5110  * %FALSE if there were errors.
5111  **/
5112 gboolean
5113 g_file_load_contents (GFile         *file,
5114                       GCancellable  *cancellable,
5115                       char         **contents,
5116                       gsize         *length,
5117                       char         **etag_out,
5118                       GError       **error)
5119 {
5120   GFileInputStream *in;
5121   GByteArray *content;
5122   gsize pos;
5123   gssize res;
5124   GFileInfo *info;
5125
5126   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5127   g_return_val_if_fail (contents != NULL, FALSE);
5128
5129   in = g_file_read (file, cancellable, error);
5130   if (in == NULL)
5131     return FALSE;
5132
5133   content = g_byte_array_new ();
5134   pos = 0;
5135   
5136   g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5137   while ((res = g_input_stream_read (G_INPUT_STREAM (in),
5138                                      content->data + pos,
5139                                      GET_CONTENT_BLOCK_SIZE,
5140                                      cancellable, error)) > 0)
5141     {
5142       pos += res;
5143       g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5144     }
5145
5146   if (etag_out)
5147     {
5148       *etag_out = NULL;
5149       
5150       info = g_file_input_stream_query_info (in,
5151                                              G_FILE_ATTRIBUTE_ETAG_VALUE,
5152                                              cancellable,
5153                                              NULL);
5154       if (info)
5155         {
5156           *etag_out = g_strdup (g_file_info_get_etag (info));
5157           g_object_unref (info);
5158         }
5159     } 
5160
5161   /* Ignore errors on close */
5162   g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
5163   g_object_unref (in);
5164
5165   if (res < 0)
5166     {
5167       /* error is set already */
5168       g_byte_array_free (content, TRUE);
5169       return FALSE;
5170     }
5171
5172   if (length)
5173     *length = pos;
5174
5175   /* Zero terminate (we got an extra byte allocated for this */
5176   content->data[pos] = 0;
5177   
5178   *contents = (char *)g_byte_array_free (content, FALSE);
5179   
5180   return TRUE;
5181 }
5182
5183 typedef struct {
5184   GFile *file;
5185   GError *error;
5186   GCancellable *cancellable;
5187   GFileReadMoreCallback read_more_callback;
5188   GAsyncReadyCallback callback;
5189   gpointer user_data;
5190   GByteArray *content;
5191   gsize pos;
5192   char *etag;
5193 } LoadContentsData;
5194
5195
5196 static void
5197 load_contents_data_free (LoadContentsData *data)
5198 {
5199   if (data->error)
5200     g_error_free (data->error);
5201   if (data->cancellable)
5202     g_object_unref (data->cancellable);
5203   if (data->content)
5204     g_byte_array_free (data->content, TRUE);
5205   g_free (data->etag);
5206   g_object_unref (data->file);
5207   g_free (data);
5208 }
5209
5210 static void
5211 load_contents_close_callback (GObject      *obj,
5212                               GAsyncResult *close_res,
5213                               gpointer      user_data)
5214 {
5215   GInputStream *stream = G_INPUT_STREAM (obj);
5216   LoadContentsData *data = user_data;
5217   GSimpleAsyncResult *res;
5218
5219   /* Ignore errors here, we're only reading anyway */
5220   g_input_stream_close_finish (stream, close_res, NULL);
5221   g_object_unref (stream);
5222
5223   res = g_simple_async_result_new (G_OBJECT (data->file),
5224                                    data->callback,
5225                                    data->user_data,
5226                                    g_file_load_contents_async);
5227   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5228   g_simple_async_result_complete (res);
5229   g_object_unref (res);
5230 }
5231
5232 static void
5233 load_contents_fstat_callback (GObject      *obj,
5234                               GAsyncResult *stat_res,
5235                               gpointer      user_data)
5236 {
5237   GInputStream *stream = G_INPUT_STREAM (obj);
5238   LoadContentsData *data = user_data;
5239   GFileInfo *info;
5240
5241   info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5242                                                    stat_res, NULL);
5243   if (info)
5244     {
5245       data->etag = g_strdup (g_file_info_get_etag (info));
5246       g_object_unref (info);
5247     }
5248
5249   g_input_stream_close_async (stream, 0,
5250                               data->cancellable,
5251                               load_contents_close_callback, data);
5252 }
5253
5254 static void
5255 load_contents_read_callback (GObject      *obj,
5256                              GAsyncResult *read_res,
5257                              gpointer      user_data)
5258 {
5259   GInputStream *stream = G_INPUT_STREAM (obj);
5260   LoadContentsData *data = user_data;
5261   GError *error = NULL;
5262   gssize read_size;
5263
5264   read_size = g_input_stream_read_finish (stream, read_res, &error);
5265
5266   if (read_size < 0) 
5267     {
5268       /* Error or EOF, close the file */
5269       data->error = error;
5270       g_input_stream_close_async (stream, 0,
5271                                   data->cancellable,
5272                                   load_contents_close_callback, data);
5273     }
5274   else if (read_size == 0)
5275     {
5276       g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5277                                             G_FILE_ATTRIBUTE_ETAG_VALUE,
5278                                             0,
5279                                             data->cancellable,
5280                                             load_contents_fstat_callback,
5281                                             data);
5282     }
5283   else if (read_size > 0)
5284     {
5285       data->pos += read_size;
5286       
5287       g_byte_array_set_size (data->content,
5288                              data->pos + GET_CONTENT_BLOCK_SIZE);
5289
5290
5291       if (data->read_more_callback &&
5292           !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5293         g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5294                                               G_FILE_ATTRIBUTE_ETAG_VALUE,
5295                                               0,
5296                                               data->cancellable,
5297                                               load_contents_fstat_callback,
5298                                               data);
5299       else 
5300         g_input_stream_read_async (stream,
5301                                    data->content->data + data->pos,
5302                                    GET_CONTENT_BLOCK_SIZE,
5303                                    0,
5304                                    data->cancellable,
5305                                    load_contents_read_callback,
5306                                    data);
5307     }
5308 }
5309
5310 static void
5311 load_contents_open_callback (GObject      *obj,
5312                              GAsyncResult *open_res,
5313                              gpointer      user_data)
5314 {
5315   GFile *file = G_FILE (obj);
5316   GFileInputStream *stream;
5317   LoadContentsData *data = user_data;
5318   GError *error = NULL;
5319   GSimpleAsyncResult *res;
5320
5321   stream = g_file_read_finish (file, open_res, &error);
5322
5323   if (stream)
5324     {
5325       g_byte_array_set_size (data->content,
5326                              data->pos + GET_CONTENT_BLOCK_SIZE);
5327       g_input_stream_read_async (G_INPUT_STREAM (stream),
5328                                  data->content->data + data->pos,
5329                                  GET_CONTENT_BLOCK_SIZE,
5330                                  0,
5331                                  data->cancellable,
5332                                  load_contents_read_callback,
5333                                  data);
5334       
5335     }
5336   else
5337     {
5338       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5339                                                   data->callback,
5340                                                   data->user_data,
5341                                                   error);
5342       g_simple_async_result_complete (res);
5343       g_error_free (error);
5344       load_contents_data_free (data);
5345       g_object_unref (res);
5346     }
5347 }
5348
5349 /**
5350  * g_file_load_partial_contents_async:
5351  * @file: input #GFile.
5352  * @cancellable: optional #GCancellable object, %NULL to ignore.
5353  * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5354  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5355  * @user_data: the data to pass to the callback functions.
5356  *
5357  * Reads the partial contents of a file. A #GFileReadMoreCallback should be 
5358  * used to stop reading from the file when appropriate, else this function
5359  * will behave exactly as g_file_load_contents_async(). This operation 
5360  * can be finished by g_file_load_partial_contents_finish().
5361  *
5362  * Users of this function should be aware that @user_data is passed to 
5363  * both the @read_more_callback and the @callback.
5364  *
5365  * If @cancellable is not %NULL, then the operation can be cancelled by
5366  * triggering the cancellable object from another thread. If the operation
5367  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5368  **/
5369 void
5370 g_file_load_partial_contents_async (GFile                 *file,
5371                                     GCancellable          *cancellable,
5372                                     GFileReadMoreCallback  read_more_callback,
5373                                     GAsyncReadyCallback    callback,
5374                                     gpointer               user_data)
5375 {
5376   LoadContentsData *data;
5377
5378   g_return_if_fail (G_IS_FILE (file));
5379
5380   data = g_new0 (LoadContentsData, 1);
5381
5382   if (cancellable)
5383     data->cancellable = g_object_ref (cancellable);
5384   data->read_more_callback = read_more_callback;
5385   data->callback = callback;
5386   data->user_data = user_data;
5387   data->content = g_byte_array_new ();
5388   data->file = g_object_ref (file);
5389
5390   g_file_read_async (file,
5391                      0,
5392                      cancellable,
5393                      load_contents_open_callback,
5394                      data);
5395 }
5396
5397 /**
5398  * g_file_load_partial_contents_finish:
5399  * @file: input #GFile.
5400  * @res: a #GAsyncResult. 
5401  * @contents: a location to place the contents of the file.
5402  * @length: a location to place the length of the contents of the file,
5403  *     or %NULL if the length is not needed
5404  * @etag_out: a location to place the current entity tag for the file,
5405  *     or %NULL if the entity tag is not needed
5406  * @error: a #GError, or %NULL
5407  * 
5408  * Finishes an asynchronous partial load operation that was started
5409  * with g_file_load_partial_contents_async(). The data is always 
5410  * zero-terminated, but this is not included in the resultant @length.
5411  * The returned @content should be freed with g_free() when no longer
5412  * needed.
5413  *
5414  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
5415  * present, it will be set appropriately. 
5416  **/
5417 gboolean
5418 g_file_load_partial_contents_finish (GFile         *file,
5419                                      GAsyncResult  *res,
5420                                      char         **contents,
5421                                      gsize         *length,
5422                                      char         **etag_out,
5423                                      GError       **error)
5424 {
5425   GSimpleAsyncResult *simple;
5426   LoadContentsData *data;
5427
5428   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5429   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5430   g_return_val_if_fail (contents != NULL, FALSE);
5431
5432   simple = G_SIMPLE_ASYNC_RESULT (res);
5433
5434   if (g_simple_async_result_propagate_error (simple, error))
5435     return FALSE;
5436   
5437   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5438   
5439   data = g_simple_async_result_get_op_res_gpointer (simple);
5440
5441   if (data->error)
5442     {
5443       g_propagate_error (error, data->error);
5444       data->error = NULL;
5445       *contents = NULL;
5446       if (length)
5447         *length = 0;
5448       return FALSE;
5449     }
5450
5451   if (length)
5452     *length = data->pos;
5453
5454   if (etag_out)
5455     {
5456       *etag_out = data->etag;
5457       data->etag = NULL;
5458     }
5459
5460   /* Zero terminate */
5461   g_byte_array_set_size (data->content, data->pos + 1);
5462   data->content->data[data->pos] = 0;
5463   
5464   *contents = (char *)g_byte_array_free (data->content, FALSE);
5465   data->content = NULL;
5466
5467   return TRUE;
5468 }
5469
5470 /**
5471  * g_file_load_contents_async:
5472  * @file: input #GFile.
5473  * @cancellable: optional #GCancellable object, %NULL to ignore.
5474  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5475  * @user_data: the data to pass to callback function
5476  * 
5477  * Starts an asynchronous load of the @file's contents.
5478  *
5479  * For more details, see g_file_load_contents() which is
5480  * the synchronous version of this call.
5481  *
5482  * When the load operation has completed, @callback will be called 
5483  * with @user data. To finish the operation, call 
5484  * g_file_load_contents_finish() with the #GAsyncResult returned by 
5485  * the @callback.
5486  * 
5487  * If @cancellable is not %NULL, then the operation can be cancelled by
5488  * triggering the cancellable object from another thread. If the operation
5489  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5490  **/
5491 void
5492 g_file_load_contents_async (GFile               *file,
5493                            GCancellable        *cancellable,
5494                            GAsyncReadyCallback  callback,
5495                            gpointer             user_data)
5496 {
5497   g_file_load_partial_contents_async (file,
5498                                       cancellable,
5499                                       NULL,
5500                                       callback, user_data);
5501 }
5502
5503 /**
5504  * g_file_load_contents_finish:
5505  * @file: input #GFile.
5506  * @res: a #GAsyncResult. 
5507  * @contents: a location to place the contents of the file.
5508  * @length: a location to place the length of the contents of the file,
5509  *     or %NULL if the length is not needed
5510  * @etag_out: a location to place the current entity tag for the file,
5511  *     or %NULL if the entity tag is not needed
5512  * @error: a #GError, or %NULL
5513  * 
5514  * Finishes an asynchronous load of the @file's contents. 
5515  * The contents are placed in @contents, and @length is set to the 
5516  * size of the @contents string. The @content should be freed with
5517  * g_free() when no longer needed. If @etag_out is present, it will be 
5518  * set to the new entity tag for the @file.
5519  * 
5520  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
5521  * present, it will be set appropriately. 
5522  **/
5523 gboolean
5524 g_file_load_contents_finish (GFile         *file,
5525                              GAsyncResult  *res,
5526                              char         **contents,
5527                              gsize         *length,
5528                              char         **etag_out,
5529                              GError       **error)
5530 {
5531   return g_file_load_partial_contents_finish (file,
5532                                               res,
5533                                               contents,
5534                                               length,
5535                                               etag_out,
5536                                               error);
5537 }
5538   
5539 /**
5540  * g_file_replace_contents:
5541  * @file: input #GFile.
5542  * @contents: a string containing the new contents for @file.
5543  * @length: the length of @contents in bytes.
5544  * @etag: the old <link linkend="gfile-etag">entity tag</link> 
5545  *     for the document, or %NULL
5546  * @make_backup: %TRUE if a backup should be created.
5547  * @flags: a set of #GFileCreateFlags.
5548  * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5549  *      for the document. This should be freed with g_free() when no longer 
5550  *      needed, or %NULL
5551  * @cancellable: optional #GCancellable object, %NULL to ignore.
5552  * @error: a #GError, or %NULL
5553  *
5554  * Replaces the contents of @file with @contents of @length bytes.
5555  
5556  * If @etag is specified (not %NULL) any existing file must have that etag, or
5557  * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5558  *
5559  * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5560  * 
5561  * If @cancellable is not %NULL, then the operation can be cancelled by
5562  * triggering the cancellable object from another thread. If the operation
5563  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5564  *
5565  * The returned @new_etag can be used to verify that the file hasn't changed the
5566  * next time it is saved over.
5567  * 
5568  * Returns: %TRUE if successful. If an error
5569  * has occurred, this function will return %FALSE and set @error
5570  * appropriately if present.
5571  **/
5572 gboolean
5573 g_file_replace_contents (GFile             *file,
5574                          const char        *contents,
5575                          gsize              length,
5576                          const char        *etag,
5577                          gboolean           make_backup,
5578                          GFileCreateFlags   flags,
5579                          char             **new_etag,
5580                          GCancellable      *cancellable,
5581                          GError           **error)
5582 {
5583   GFileOutputStream *out;
5584   gsize pos, remainder;
5585   gssize res;
5586   gboolean ret;
5587
5588   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5589   g_return_val_if_fail (contents != NULL, FALSE);
5590
5591   out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5592   if (out == NULL)
5593     return FALSE;
5594
5595   pos = 0;
5596   remainder = length;
5597   while (remainder > 0 &&
5598          (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5599                                        contents + pos,
5600                                        MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5601                                        cancellable,
5602                                        error)) > 0)
5603     {
5604       pos += res;
5605       remainder -= res;
5606     }
5607   
5608   if (remainder > 0 && res < 0)
5609     {
5610       /* Ignore errors on close */
5611       g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5612       g_object_unref (out);
5613
5614       /* error is set already */
5615       return FALSE;
5616     }
5617   
5618   ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
5619
5620   if (new_etag)
5621     *new_etag = g_file_output_stream_get_etag (out);
5622
5623   g_object_unref (out);
5624
5625   return ret;
5626 }
5627
5628 typedef struct {
5629   GFile *file;
5630   GError *error;
5631   GCancellable *cancellable;
5632   GAsyncReadyCallback callback;
5633   gpointer user_data;
5634   const char *content;
5635   gsize length;
5636   gsize pos;
5637   char *etag;
5638 } ReplaceContentsData;
5639
5640 static void
5641 replace_contents_data_free (ReplaceContentsData *data)
5642 {
5643   if (data->error)
5644     g_error_free (data->error);
5645   if (data->cancellable)
5646     g_object_unref (data->cancellable);
5647   g_object_unref (data->file);
5648   g_free (data->etag);
5649   g_free (data);
5650 }
5651
5652 static void
5653 replace_contents_close_callback (GObject      *obj,
5654                                  GAsyncResult *close_res,
5655                                  gpointer      user_data)
5656 {
5657   GOutputStream *stream = G_OUTPUT_STREAM (obj);
5658   ReplaceContentsData *data = user_data;
5659   GSimpleAsyncResult *res;
5660
5661   /* Ignore errors here, we're only reading anyway */
5662   g_output_stream_close_finish (stream, close_res, NULL);
5663   g_object_unref (stream);
5664
5665   data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5666   
5667   res = g_simple_async_result_new (G_OBJECT (data->file),
5668                                    data->callback,
5669                                    data->user_data,
5670                                    g_file_replace_contents_async);
5671   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5672   g_simple_async_result_complete (res);
5673   g_object_unref (res);
5674 }
5675
5676 static void
5677 replace_contents_write_callback (GObject      *obj,
5678                                  GAsyncResult *read_res,
5679                                  gpointer      user_data)
5680 {
5681   GOutputStream *stream = G_OUTPUT_STREAM (obj);
5682   ReplaceContentsData *data = user_data;
5683   GError *error = NULL;
5684   gssize write_size;
5685   
5686   write_size = g_output_stream_write_finish (stream, read_res, &error);
5687
5688   if (write_size <= 0) 
5689     {
5690       /* Error or EOF, close the file */
5691       if (write_size < 0)
5692         data->error = error;
5693       g_output_stream_close_async (stream, 0,
5694                                    data->cancellable,
5695                                    replace_contents_close_callback, data);
5696     }
5697   else if (write_size > 0)
5698     {
5699       data->pos += write_size;
5700
5701       if (data->pos >= data->length)
5702         g_output_stream_close_async (stream, 0,
5703                                      data->cancellable,
5704                                      replace_contents_close_callback, data);
5705       else
5706         g_output_stream_write_async (stream,
5707                                      data->content + data->pos,
5708                                      data->length - data->pos,
5709                                      0,
5710                                      data->cancellable,
5711                                      replace_contents_write_callback,
5712                                      data);
5713     }
5714 }
5715
5716 static void
5717 replace_contents_open_callback (GObject      *obj,
5718                                 GAsyncResult *open_res,
5719                                 gpointer      user_data)
5720 {
5721   GFile *file = G_FILE (obj);
5722   GFileOutputStream *stream;
5723   ReplaceContentsData *data = user_data;
5724   GError *error = NULL;
5725   GSimpleAsyncResult *res;
5726
5727   stream = g_file_replace_finish (file, open_res, &error);
5728
5729   if (stream)
5730     {
5731       g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5732                                    data->content + data->pos,
5733                                    data->length - data->pos,
5734                                    0,
5735                                    data->cancellable,
5736                                    replace_contents_write_callback,
5737                                    data);
5738       
5739     }
5740   else
5741     {
5742       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5743                                                   data->callback,
5744                                                   data->user_data,
5745                                                   error);
5746       g_simple_async_result_complete (res);
5747       g_error_free (error);
5748       replace_contents_data_free (data);
5749       g_object_unref (res);
5750     }
5751 }
5752
5753 /**
5754  * g_file_replace_contents_async:
5755  * @file: input #GFile.
5756  * @contents: string of contents to replace the file with.
5757  * @length: the length of @contents in bytes.
5758  * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
5759  * @make_backup: %TRUE if a backup should be created.
5760  * @flags: a set of #GFileCreateFlags.
5761  * @cancellable: optional #GCancellable object, %NULL to ignore.
5762  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5763  * @user_data: the data to pass to callback function
5764  * 
5765  * Starts an asynchronous replacement of @file with the given 
5766  * @contents of @length bytes. @etag will replace the document's 
5767  * current entity tag.
5768  * 
5769  * When this operation has completed, @callback will be called with
5770  * @user_user data, and the operation can be finalized with 
5771  * g_file_replace_contents_finish().
5772  * 
5773  * If @cancellable is not %NULL, then the operation can be cancelled by
5774  * triggering the cancellable object from another thread. If the operation
5775  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5776  * 
5777  * If @make_backup is %TRUE, this function will attempt to 
5778  * make a backup of @file.
5779  **/
5780 void
5781 g_file_replace_contents_async  (GFile               *file,
5782                                 const char          *contents,
5783                                 gsize                length,
5784                                 const char          *etag,
5785                                 gboolean             make_backup,
5786                                 GFileCreateFlags     flags,
5787                                 GCancellable        *cancellable,
5788                                 GAsyncReadyCallback  callback,
5789                                 gpointer             user_data)
5790 {
5791   ReplaceContentsData *data;
5792
5793   g_return_if_fail (G_IS_FILE (file));
5794   g_return_if_fail (contents != NULL);
5795
5796   data = g_new0 (ReplaceContentsData, 1);
5797
5798   if (cancellable)
5799     data->cancellable = g_object_ref (cancellable);
5800   data->callback = callback;
5801   data->user_data = user_data;
5802   data->content = contents;
5803   data->length = length;
5804   data->pos = 0;
5805   data->file = g_object_ref (file);
5806
5807   g_file_replace_async (file,
5808                         etag,
5809                         make_backup,
5810                         flags,
5811                         0,
5812                         cancellable,
5813                         replace_contents_open_callback,
5814                         data);
5815 }
5816   
5817 /**
5818  * g_file_replace_contents_finish:
5819  * @file: input #GFile.
5820  * @res: a #GAsyncResult. 
5821  * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link> 
5822  *     for the document. This should be freed with g_free() when it is no 
5823  *     longer needed, or %NULL
5824  * @error: a #GError, or %NULL 
5825  * 
5826  * Finishes an asynchronous replace of the given @file. See
5827  * g_file_replace_contents_async(). Sets @new_etag to the new entity 
5828  * tag for the document, if present.
5829  * 
5830  * Returns: %TRUE on success, %FALSE on failure.
5831  **/
5832 gboolean
5833 g_file_replace_contents_finish (GFile         *file,
5834                                 GAsyncResult  *res,
5835                                 char         **new_etag,
5836                                 GError       **error)
5837 {
5838   GSimpleAsyncResult *simple;
5839   ReplaceContentsData *data;
5840
5841   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5842   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5843
5844   simple = G_SIMPLE_ASYNC_RESULT (res);
5845
5846   if (g_simple_async_result_propagate_error (simple, error))
5847     return FALSE;
5848   
5849   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5850   
5851   data = g_simple_async_result_get_op_res_gpointer (simple);
5852
5853   if (data->error)
5854     {
5855       g_propagate_error (error, data->error);
5856       data->error = NULL;
5857       return FALSE;
5858     }
5859
5860
5861   if (new_etag)
5862     {
5863       *new_etag = data->etag;
5864       data->etag = NULL; /* Take ownership */
5865     }
5866   
5867   return TRUE;
5868 }
5869
5870 #define __G_FILE_C__
5871 #include "gioaliasdef.c"