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