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