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