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