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