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