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