return from function in error path.
[platform/upstream/glib.git] / gio / gfile.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #ifdef HAVE_PWD_H
27 #include <pwd.h>
28 #endif
29 #include "gfile.h"
30 #include "gvfs.h"
31 #include "gioscheduler.h"
32 #include <glocalfile.h>
33 #include "gsimpleasyncresult.h"
34 #include "gfileattribute-priv.h"
35 #include "gpollfilemonitor.h"
36 #include "glibintl.h"
37
38 #include "gioalias.h"
39
40 /**
41  * SECTION:gfile
42  * @short_description: File and Directory Handling
43  * @include: gio.h
44  * @see_also: #GFileInfo, #GFileEnumerator
45  * 
46  * #GFile is a high level abstraction for manipulating files on a 
47  * virtual file system. #GFile<!-- -->s are lightweight, immutable 
48  * objects that do no I/O upon creation. It is necessary to understand that
49  * #GFile objects do not represent files, merely a handle to a file. All
50  * file I/O is implemented as streaming operations (see #GInputStream and 
51  * #GOutputStream).
52  * 
53  * To construct a #GFile, you can use: 
54  * g_file_new_for_path() if you have a path.
55  * g_file_new_for_uri() if you have a URI.
56  * g_file_new_for_commandline_arg() for a command line argument.
57  * 
58  * You can move through the file system with #GFile handles with
59  * g_file_get_parent() to get a handle to the parent directory.
60  * g_file_get_child() to get a handle to a child within a directory.
61  * g_file_resolve_relative_path() to resolve a relative path between
62  * two #GFile<!-- -->s.
63  * 
64  * Many #GFile operations have both synchronous and asynchronous versions 
65  * to suit your application. Asynchronous versions of synchronous functions 
66  * simply have _async() appended to their function names. The asynchronous 
67  * I/O functions call a #GAsyncReadyCallback which is then used to finalize 
68  * the operation, producing a GAsyncResult which is then passed to the 
69  * function's matching _finish() 
70  * operation. 
71  *
72  * Some #GFile operations do not have synchronous analogs, as they may
73  * take a very long time to finish, and blocking may leave an application
74  * unusable. Notable cases include:
75  * g_file_mount_mountable() to mount a mountable file.
76  * g_file_unmount_mountable() to unmount a mountable file.
77  * g_file_eject_mountable() to eject a mountable file.
78  * 
79  * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
80  * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for 
81  * short. Entity tags are somewhat like a more abstract version of the 
82  * traditional mtime, and can be used to quickly determine if the file has
83  * been modified from the version on the file system. See the HTTP 1.1 
84  * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
85  * for HTTP Etag headers, which are a very similar concept.
86  * </para>
87  **/
88
89 static void g_file_base_init (gpointer g_class);
90 static void g_file_class_init (gpointer g_class,
91                                gpointer class_data);
92
93 static void               g_file_real_query_info_async            (GFile                  *file,
94                                                                    const char             *attributes,
95                                                                    GFileQueryInfoFlags     flags,
96                                                                    int                     io_priority,
97                                                                    GCancellable           *cancellable,
98                                                                    GAsyncReadyCallback     callback,
99                                                                    gpointer                user_data);
100 static GFileInfo *        g_file_real_query_info_finish           (GFile                  *file,
101                                                                    GAsyncResult           *res,
102                                                                    GError                **error);
103 static void               g_file_real_enumerate_children_async    (GFile                  *file,
104                                                                    const char             *attributes,
105                                                                    GFileQueryInfoFlags     flags,
106                                                                    int                     io_priority,
107                                                                    GCancellable           *cancellable,
108                                                                    GAsyncReadyCallback     callback,
109                                                                    gpointer                user_data);
110 static GFileEnumerator *  g_file_real_enumerate_children_finish   (GFile                  *file,
111                                                                    GAsyncResult           *res,
112                                                                    GError                **error);
113 static void               g_file_real_read_async                  (GFile                  *file,
114                                                                    int                     io_priority,
115                                                                    GCancellable           *cancellable,
116                                                                    GAsyncReadyCallback     callback,
117                                                                    gpointer                user_data);
118 static GFileInputStream * g_file_real_read_finish                 (GFile                  *file,
119                                                                    GAsyncResult           *res,
120                                                                    GError                **error);
121 static void               g_file_real_append_to_async             (GFile                  *file,
122                                                                    GFileCreateFlags        flags,
123                                                                    int                     io_priority,
124                                                                    GCancellable           *cancellable,
125                                                                    GAsyncReadyCallback     callback,
126                                                                    gpointer                user_data);
127 static GFileOutputStream *g_file_real_append_to_finish            (GFile                  *file,
128                                                                    GAsyncResult           *res,
129                                                                    GError                **error);
130 static void               g_file_real_create_async                (GFile                  *file,
131                                                                    GFileCreateFlags        flags,
132                                                                    int                     io_priority,
133                                                                    GCancellable           *cancellable,
134                                                                    GAsyncReadyCallback     callback,
135                                                                    gpointer                user_data);
136 static GFileOutputStream *g_file_real_create_finish               (GFile                  *file,
137                                                                    GAsyncResult           *res,
138                                                                    GError                **error);
139 static void               g_file_real_replace_async               (GFile                  *file,
140                                                                    const char             *etag,
141                                                                    gboolean                make_backup,
142                                                                    GFileCreateFlags        flags,
143                                                                    int                     io_priority,
144                                                                    GCancellable           *cancellable,
145                                                                    GAsyncReadyCallback     callback,
146                                                                    gpointer                user_data);
147 static GFileOutputStream *g_file_real_replace_finish              (GFile                  *file,
148                                                                    GAsyncResult           *res,
149                                                                    GError                **error);
150 static gboolean           g_file_real_set_attributes_from_info    (GFile                  *file,
151                                                                    GFileInfo              *info,
152                                                                    GFileQueryInfoFlags     flags,
153                                                                    GCancellable           *cancellable,
154                                                                    GError                **error);
155 static void               g_file_real_set_display_name_async      (GFile                  *file,
156                                                                    const char             *display_name,
157                                                                    int                     io_priority,
158                                                                    GCancellable           *cancellable,
159                                                                    GAsyncReadyCallback     callback,
160                                                                    gpointer                user_data);
161 static GFile *            g_file_real_set_display_name_finish     (GFile                  *file,
162                                                                    GAsyncResult           *res,
163                                                                    GError                **error);
164 static void               g_file_real_set_attributes_async        (GFile                  *file,
165                                                                    GFileInfo              *info,
166                                                                    GFileQueryInfoFlags     flags,
167                                                                    int                     io_priority,
168                                                                    GCancellable           *cancellable,
169                                                                    GAsyncReadyCallback     callback,
170                                                                    gpointer                user_data);
171 static gboolean           g_file_real_set_attributes_finish       (GFile                  *file,
172                                                                    GAsyncResult           *res,
173                                                                    GFileInfo             **info,
174                                                                    GError                **error);
175 static void               g_file_real_find_enclosing_mount_async  (GFile                  *file,
176                                                                    int                     io_priority,
177                                                                    GCancellable           *cancellable,
178                                                                    GAsyncReadyCallback     callback,
179                                                                    gpointer                user_data);
180 static GMount *           g_file_real_find_enclosing_mount_finish (GFile                  *file,
181                                                                    GAsyncResult           *res,
182                                                                    GError                **error);
183 static void               g_file_real_copy_async                  (GFile                  *source,
184                                                                    GFile                  *destination,
185                                                                    GFileCopyFlags          flags,
186                                                                    int                     io_priority,
187                                                                    GCancellable           *cancellable,
188                                                                    GFileProgressCallback   progress_callback,
189                                                                    gpointer                progress_callback_data,
190                                                                    GAsyncReadyCallback     callback,
191                                                                    gpointer                user_data);
192 static gboolean           g_file_real_copy_finish                 (GFile                  *file,
193                                                                    GAsyncResult           *res,
194                                                                    GError                **error);
195
196 GType
197 g_file_get_type (void)
198 {
199   static GType file_type = 0;
200
201   if (! file_type)
202     {
203       static const GTypeInfo file_info =
204       {
205         sizeof (GFileIface), /* class_size */
206         g_file_base_init,   /* base_init */
207         NULL,           /* base_finalize */
208         g_file_class_init,
209         NULL,           /* class_finalize */
210         NULL,           /* class_data */
211         0,
212         0,              /* n_preallocs */
213         NULL
214       };
215
216       file_type =
217         g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
218                                 &file_info, 0);
219
220       g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
221     }
222
223   return file_type;
224 }
225
226 static void
227 g_file_class_init (gpointer g_class,
228                    gpointer class_data)
229 {
230   GFileIface *iface = g_class;
231
232   iface->enumerate_children_async = g_file_real_enumerate_children_async;
233   iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
234   iface->set_display_name_async = g_file_real_set_display_name_async;
235   iface->set_display_name_finish = g_file_real_set_display_name_finish;
236   iface->query_info_async = g_file_real_query_info_async;
237   iface->query_info_finish = g_file_real_query_info_finish;
238   iface->set_attributes_async = g_file_real_set_attributes_async;
239   iface->set_attributes_finish = g_file_real_set_attributes_finish;
240   iface->read_async = g_file_real_read_async;
241   iface->read_finish = g_file_real_read_finish;
242   iface->append_to_async = g_file_real_append_to_async;
243   iface->append_to_finish = g_file_real_append_to_finish;
244   iface->create_async = g_file_real_create_async;
245   iface->create_finish = g_file_real_create_finish;
246   iface->replace_async = g_file_real_replace_async;
247   iface->replace_finish = g_file_real_replace_finish;
248   iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
249   iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
250   iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
251   iface->copy_async = g_file_real_copy_async;
252   iface->copy_finish = g_file_real_copy_finish;
253 }
254
255 static void
256 g_file_base_init (gpointer g_class)
257 {
258 }
259
260
261 /**
262  * g_file_is_native:
263  * @file: input #GFile.
264  *
265  * Checks to see if a file is native to the platform.
266  *
267  * A native file s one expressed in the platform-native filename format,
268  * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
269  * as it might be on a locally mounted remote filesystem.
270  *
271  * On some systems non-native files may be available using
272  * the native filesystem via a userspace filesystem (FUSE), in
273  * these cases this call will return %FALSE, but g_file_get_path()
274  * will still return a native path.
275  *
276  * This call does no blocking i/o.
277  * 
278  * Returns: %TRUE if file is native. 
279  **/
280 gboolean
281 g_file_is_native (GFile *file)
282 {
283   GFileIface *iface;
284
285   g_return_val_if_fail (G_IS_FILE (file), FALSE);
286
287   iface = G_FILE_GET_IFACE (file);
288
289   return (* iface->is_native) (file);
290 }
291
292
293 /**
294  * g_file_has_uri_scheme: 
295  * @file: input #GFile.
296  * @uri_scheme: a string containing a URI scheme.
297  *
298  * Checks to see if a #GFile has a given URI scheme.
299  *
300  * This call does no blocking i/o.
301  * 
302  * Returns: %TRUE if #GFile's backend supports the
303  *     given URI scheme, %FALSE if URI scheme is %NULL,
304  *     not supported, or #GFile is invalid.
305  **/
306 gboolean
307 g_file_has_uri_scheme (GFile      *file,
308                        const char *uri_scheme)
309 {
310   GFileIface *iface;
311   
312   g_return_val_if_fail (G_IS_FILE (file), FALSE);
313   g_return_val_if_fail (uri_scheme != NULL, FALSE);
314
315   iface = G_FILE_GET_IFACE (file);
316
317   return (* iface->has_uri_scheme) (file, uri_scheme);
318 }
319
320
321 /**
322  * g_file_get_uri_scheme:
323  * @file: input #GFile.
324  *
325  * Gets the URI scheme for a #GFile.
326  * RFC 3986 decodes the scheme as:
327  * <programlisting>
328  * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] 
329  * </programlisting>
330  * Common schemes include "file", "http", "ftp", etc. 
331  *
332  * This call does no blocking i/o.
333  * 
334  * Returns: a string containing the URI scheme for the given 
335  *     #GFile. The returned string should be freed with g_free() 
336  *     when no longer needed.
337  **/
338 char *
339 g_file_get_uri_scheme (GFile *file)
340 {
341   GFileIface *iface;
342
343   g_return_val_if_fail (G_IS_FILE (file), NULL);
344
345   iface = G_FILE_GET_IFACE (file);
346
347   return (* iface->get_uri_scheme) (file);
348 }
349
350
351 /**
352  * g_file_get_basename:
353  * @file: input #GFile.
354  *
355  * Gets the base name (the last component of the path) for a given #GFile.
356  *
357  * If called for the top level of a system (such as the filesystem root
358  * or a uri like sftp://host/ it will return a single directory separator
359  * (and on Windows, possibly a drive letter).
360  *
361  * This call does no blocking i/o.
362  * 
363  * Returns: string containing the #GFile's base name, or %NULL 
364  *     if given #GFile is invalid. The returned string should be 
365  *     freed with g_free() when no longer needed.
366  **/
367 char *
368 g_file_get_basename (GFile *file)
369 {
370   GFileIface *iface;
371   
372   g_return_val_if_fail (G_IS_FILE (file), NULL);
373
374   iface = G_FILE_GET_IFACE (file);
375
376   return (* iface->get_basename) (file);
377 }
378
379 /**
380  * g_file_get_path:
381  * @file: input #GFile.
382  *
383  * Gets the local pathname for #GFile, if one exists. 
384  *
385  * This call does no blocking i/o.
386  * 
387  * Returns: string containing the #GFile's path, or %NULL if 
388  *     no such path exists. The returned string should be 
389  *     freed with g_free() when no longer needed.
390  **/
391 char *
392 g_file_get_path (GFile *file)
393 {
394   GFileIface *iface;
395
396   g_return_val_if_fail (G_IS_FILE (file), NULL);
397
398   iface = G_FILE_GET_IFACE (file);
399
400   return (* iface->get_path) (file);
401 }
402
403 /**
404  * g_file_get_uri:
405  * @file: input #GFile.
406  *
407  * Gets the URI for the @file.
408  *
409  * This call does no blocking i/o.
410  * 
411  * Returns: a string containing the #GFile's URI.
412  *     The returned string should be freed with g_free() when no longer needed.
413  **/
414 char *
415 g_file_get_uri (GFile *file)
416 {
417   GFileIface *iface;
418   
419   g_return_val_if_fail (G_IS_FILE (file), NULL);
420
421   iface = G_FILE_GET_IFACE (file);
422
423   return (* iface->get_uri) (file);
424 }
425
426 /**
427  * g_file_get_parse_name:
428  * @file: input #GFile.
429  *
430  * Gets the parse name of the @file.
431  * A parse name is a UTF-8 string that describes the
432  * file such that one can get the #GFile back using
433  * g_file_parse_name().
434  *
435  * This is generally used to show the #GFile as a nice
436  * string in a user interface, like in a location entry.
437  *
438  * For local files with names that can safely be converted
439  * to UTF8 the pathname is used, otherwise the IRI is used
440  * (a form of URI that allows UTF8 characters unescaped).
441  *
442  * This call does no blocking i/o.
443  * 
444  * Returns: a string containing the #GFile's parse name. The returned 
445  *     string should be freed with g_free() when no longer needed.
446  **/
447 char *
448 g_file_get_parse_name (GFile *file)
449 {
450   GFileIface *iface;
451   
452   g_return_val_if_fail (G_IS_FILE (file), NULL);
453
454   iface = G_FILE_GET_IFACE (file);
455
456   return (* iface->get_parse_name) (file);
457 }
458
459 /**
460  * g_file_dup:
461  * @file: input #GFile.
462  *
463  * Duplicates a #GFile handle. This operation does not duplicate 
464  * the actual file or directory represented by the #GFile; see 
465  * g_file_copy() if attempting to copy a file. 
466  *
467  * This call does no blocking i/o.
468  * 
469  * Returns: #GFile that is a duplicate of the given #GFile. 
470  **/
471 GFile *
472 g_file_dup (GFile *file)
473 {
474   GFileIface *iface;
475   
476   g_return_val_if_fail (G_IS_FILE (file), NULL);
477
478   iface = G_FILE_GET_IFACE (file);
479
480   return (* iface->dup) (file);
481 }
482
483 /**
484  * g_file_hash:
485  * @file: #gconstpointer to a #GFile.
486  *
487  * Creates a hash value for a #GFile.
488  *
489  * This call does no blocking i/o.
490  * 
491  * Returns: 0 if @file is not a valid #GFile, otherwise an 
492  *     integer that can be used as hash value for the #GFile. 
493  *     This function is intended for easily hashing a #GFile to 
494  *     add to a #GHashTable or similar data structure.
495  **/
496 guint
497 g_file_hash (gconstpointer file)
498 {
499   GFileIface *iface;
500   
501   g_return_val_if_fail (G_IS_FILE (file), 0);
502
503   iface = G_FILE_GET_IFACE (file);
504
505   return (* iface->hash) ((GFile *)file);
506 }
507
508 /**
509  * g_file_equal:
510  * @file1: the first #GFile.
511  * @file2: the second #GFile.
512  *
513  * Checks equality of two given #GFile<!-- -->s
514  *
515  * This call does no blocking i/o.
516  * 
517  * Returns: %TRUE if @file1 and @file2 are equal.
518  *     %FALSE if either is not a #GFile.
519  **/
520 gboolean
521 g_file_equal (GFile *file1,
522               GFile *file2)
523 {
524   GFileIface *iface;
525   
526   g_return_val_if_fail (G_IS_FILE (file1), FALSE);
527   g_return_val_if_fail (G_IS_FILE (file2), FALSE);
528   
529   if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
530     return FALSE;
531
532   iface = G_FILE_GET_IFACE (file1);
533   
534   return (* iface->equal) (file1, file2);
535 }
536
537
538 /**
539  * g_file_get_parent:
540  * @file: input #GFile.
541  *
542  * Gets the parent directory for the @file. 
543  * If the @file represents the root directory of the 
544  * file system, then %NULL will be returned.
545  *
546  * This call does no blocking i/o.
547  * 
548  * Returns: a #GFile structure to the parent of the given
549  *     #GFile or %NULL if there is no parent. 
550  **/
551 GFile *
552 g_file_get_parent (GFile *file)
553 {
554   GFileIface *iface;
555   
556   g_return_val_if_fail (G_IS_FILE (file), NULL);
557
558   iface = G_FILE_GET_IFACE (file);
559
560   return (* iface->get_parent) (file);
561 }
562
563 /**
564  * g_file_get_child:
565  * @file: input #GFile.
566  * @name: string containing the child's name.
567  *
568  * Gets a specific child of @file with name equal to @name.
569  *
570  * Note that the file with that specific name might not exist, but
571  * you can still have a #GFile that points to it. You can use this
572  * for instance to create that file.
573  *
574  * This call does no blocking i/o.
575  * 
576  * Returns: a #GFile to a child specified by @name.
577  **/
578 GFile *
579 g_file_get_child (GFile      *file,
580                   const char *name)
581 {
582   g_return_val_if_fail (G_IS_FILE (file), NULL);
583   g_return_val_if_fail (name != NULL, NULL);
584
585   return g_file_resolve_relative_path (file, name);
586 }
587
588 /**
589  * g_file_get_child_for_display_name:
590  * @file: input #GFile.
591  * @display_name: string to a possible child.
592  * @error: #GError.
593  *
594  * Gets the child of @file for a given @display_name (i.e. a UTF8
595  * version of the name). If this function fails, it returns %NULL and @error will be 
596  * set. This is very useful when constructing a GFile for a new file
597  * and the user entered the filename in the user interface, for instance
598  * when you select a directory and type a filename in the file selector.
599  * 
600  * This call does no blocking i/o.
601  * 
602  * Returns: a #GFile to the specified child, or 
603  *     %NULL if the display name couldn't be converted.  
604  **/
605 GFile *
606 g_file_get_child_for_display_name (GFile      *file,
607                                    const char *display_name,
608                                    GError **error)
609 {
610   GFileIface *iface;
611   
612   g_return_val_if_fail (G_IS_FILE (file), NULL);
613   g_return_val_if_fail (display_name != NULL, NULL);
614
615   iface = G_FILE_GET_IFACE (file);
616
617   return (* iface->get_child_for_display_name) (file, display_name, error);
618 }
619
620 /**
621  * g_file_contains_file:
622  * @parent: input #GFile.
623  * @descendant: input #GFile.
624  * 
625  * Checks whether @parent (recursively) contains the specified @descendant.
626  * 
627  * This call does no blocking i/o.
628  * 
629  * Returns:  %TRUE if the @descendant's parent, grandparent, etc is @parent. %FALSE otherwise.
630  **/
631 gboolean
632 g_file_contains_file (GFile *parent,
633                       GFile *descendant)
634 {
635   GFileIface *iface;
636   
637   g_return_val_if_fail (G_IS_FILE (parent), FALSE);
638   g_return_val_if_fail (G_IS_FILE (descendant), FALSE);
639
640   if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
641     return FALSE;
642   
643   iface = G_FILE_GET_IFACE (parent);
644
645   return (* iface->contains_file) (parent, descendant);
646 }
647
648 /**
649  * g_file_get_relative_path:
650  * @parent: input #GFile.
651  * @descendant: input #GFile.
652  *
653  * Gets the path for @descendant relative to @parent. 
654  *
655  * This call does no blocking i/o.
656  * 
657  * Returns: string with the relative path from @descendant 
658  *     to @parent, or %NULL if @descendant is not a descendant of @parent. The returned string should be freed with 
659  *     g_free() when no longer needed.
660  **/
661 char *
662 g_file_get_relative_path (GFile *parent,
663                           GFile *descendant)
664 {
665   GFileIface *iface;
666   
667   g_return_val_if_fail (G_IS_FILE (parent), NULL);
668   g_return_val_if_fail (G_IS_FILE (descendant), NULL);
669
670   if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
671     return NULL;
672   
673   iface = G_FILE_GET_IFACE (parent);
674
675   return (* iface->get_relative_path) (parent, descendant);
676 }
677
678 /**
679  * g_file_resolve_relative_path:
680  * @file: input #GFile.
681  * @relative_path: a given relative path string.
682  *
683  * Resolves a relative path for @file to an absolute path.
684  *
685  * This call does no blocking i/o.
686  * 
687  * Returns: #GFile to the resolved path. %NULL if @relative_path 
688  *     is %NULL or if @file is invalid.
689  **/
690 GFile *
691 g_file_resolve_relative_path (GFile      *file,
692                               const char *relative_path)
693 {
694   GFileIface *iface;
695
696   g_return_val_if_fail (G_IS_FILE (file), NULL);
697   g_return_val_if_fail (relative_path != NULL, NULL);
698
699   iface = G_FILE_GET_IFACE (file);
700
701   return (* iface->resolve_relative_path) (file, relative_path);
702 }
703
704 /**
705  * g_file_enumerate_children:
706  * @file: input #GFile.
707  * @attributes: an attribute query string.
708  * @flags: a set of #GFileQueryInfoFlags.
709  * @cancellable: optional #GCancellable object, %NULL to ignore.
710  * @error: #GError for error reporting.
711  *
712  * Gets the requested information about the files in a directory. The result
713  * is a #GFileEnumerator object that will give out #GFileInfo objects for
714  * all the files in the directory.
715  *
716  * The @attribute value is a string that specifies the file attributes that
717  * should be gathered. It is not an error if it's not possible to read a particular
718  * requested attribute from a file - it just won't be set. @attribute should
719  * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
720  * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
721  * namespace. An example attribute query be "standard::*,owner::user".
722  * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
723  *
724  * If @cancellable is not %NULL, then the operation can be cancelled by
725  * triggering the cancellable object from another thread. If the operation
726  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
727  * 
728  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
729  * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
730  * Other errors are possible too.
731  *
732  * Returns: A #GFileEnumerator if successful, %NULL on error. 
733  **/
734 GFileEnumerator *
735 g_file_enumerate_children (GFile                *file,
736                            const char           *attributes,
737                            GFileQueryInfoFlags   flags,
738                            GCancellable         *cancellable,
739                            GError              **error)
740                            
741 {
742   GFileIface *iface;
743   
744   g_return_val_if_fail (G_IS_FILE (file), NULL);
745
746   if (g_cancellable_set_error_if_cancelled (cancellable, error))
747     return NULL;
748   
749   iface = G_FILE_GET_IFACE (file);
750
751   if (iface->enumerate_children == NULL)
752     {
753       g_set_error (error, G_IO_ERROR,
754                    G_IO_ERROR_NOT_SUPPORTED,
755                    _("Operation not supported"));
756       return NULL;
757     }
758
759   return (* iface->enumerate_children) (file, attributes, flags,
760                                         cancellable, error);
761 }
762
763 /**
764  * g_file_enumerate_children_async:
765  * @file: input #GFile.
766  * @attributes: an attribute query string.
767  * @flags: a set of #GFileQueryInfoFlags.
768  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
769  *     of the request.
770  * @cancellable: optional #GCancellable object, %NULL to ignore.
771  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
772  * @user_data: the data to pass to callback function
773  *
774  * Asynchronously gets the requested information about the files in a directory. The result
775  * is a #GFileEnumerator object that will give out #GFileInfo objects for
776  * all the files in the directory.
777  *
778  * For more details, see g_file_enumerate_children() which is
779  * the synchronous version of this call.
780  *
781  * When the operation is finished, @callback will be called. You can then call
782  * g_file_enumerate_children_finish() to get the result of the operation.
783  **/
784 void
785 g_file_enumerate_children_async (GFile               *file,
786                                  const char          *attributes,
787                                  GFileQueryInfoFlags  flags,
788                                  int                  io_priority,
789                                  GCancellable        *cancellable,
790                                  GAsyncReadyCallback  callback,
791                                  gpointer             user_data)
792 {
793   GFileIface *iface;
794
795   g_return_if_fail (G_IS_FILE (file));
796
797   iface = G_FILE_GET_IFACE (file);
798   (* iface->enumerate_children_async) (file,
799                                        attributes,
800                                        flags,
801                                        io_priority,
802                                        cancellable,
803                                        callback,
804                                        user_data);
805 }
806
807 /**
808  * g_file_enumerate_children_finish:
809  * @file: input #GFile.
810  * @res: a #GAsyncResult.
811  * @error: a #GError.
812  * 
813  * Finishes an async enumerate children operation.
814  * See g_file_enumerate_children_async().
815  *
816  * Returns: a #GFileEnumerator or %NULL if an error occurred.
817  **/
818 GFileEnumerator *
819 g_file_enumerate_children_finish (GFile         *file,
820                                   GAsyncResult  *res,
821                                   GError       **error)
822 {
823   GFileIface *iface;
824   
825   g_return_val_if_fail (G_IS_FILE (file), NULL);
826   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
827
828   if (G_IS_SIMPLE_ASYNC_RESULT (res))
829     {
830       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
831       if (g_simple_async_result_propagate_error (simple, error))
832         return NULL;
833     }
834   
835   iface = G_FILE_GET_IFACE (file);
836   return (* iface->enumerate_children_finish) (file, res, error);
837 }
838
839 /**
840  * g_file_query_exists:
841  * @file: input #GFile.
842  * @cancellable: optional #GCancellable object, %NULL to ignore.
843  *
844  * Utility function to check if a particular file exists. This is
845  * implemented using g_file_query_info() and as such does blocking I/O.
846  *
847  * Note that in many cases it is racy to first check for file existance
848  * and then execute something based on the outcome of that, because the
849  * file might have been created or removed inbetween the operations. The
850  * general approach to handling that is to not check, but just do the
851  * operation and handle the errors as they come.
852  *
853  * As an example of race-free checking, take the case of reading a file, and
854  * if it doesn't exist, creating it. There are two racy versions: read it, and
855  * on error create it; and: check if it exists, if not create it. These
856  * can both result in two processes creating the file (with perhaps a partially
857  * written file as the result). The correct approach is to always try to create
858  * the file with g_file_create() which will either atomically create the file
859  * or fail with a G_IO_ERROR_EXISTS error.
860  *
861  * However, in many cases an existance check is useful in a user
862  * interface, for instance to make a menu item sensitive/insensitive, so that
863  * you don't have to fool users that something is possible and then just show
864  * and error dialog. If you do this, you should make sure to also handle the
865  * errors that can happen due to races when you execute the operation.
866  * 
867  * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
868  */
869 gboolean
870 g_file_query_exists (GFile *file,
871                      GCancellable *cancellable)
872 {
873   GFileInfo *info;
874
875   info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
876                             G_FILE_QUERY_INFO_NONE,
877                             cancellable, NULL);
878   if (info != NULL)
879     {
880       g_object_unref (info);
881       return TRUE;
882     }
883   
884   return FALSE;
885 }
886
887 /**
888  * g_file_query_info:
889  * @file: input #GFile.
890  * @attributes: an attribute query string.
891  * @flags: a set of #GFileQueryInfoFlags.
892  * @cancellable: optional #GCancellable object, %NULL to ignore.
893  * @error: a #GError.
894  *
895  * Gets the requested information about specified @file. The result
896  * is a #GFileInfo object that contains key-value attributes (such as 
897  * the type or size of the file).
898  *
899  * The @attribute value is a string that specifies the file attributes that
900  * should be gathered. It is not an error if it's not possible to read a particular
901  * requested attribute from a file - it just won't be set. @attribute should
902  * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
903  * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
904  * namespace. An example attribute query be "standard::*,owner::user".
905  * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
906  * 
907  * If @cancellable is not %NULL, then the operation can be cancelled by
908  * triggering the cancellable object from another thread. If the operation
909  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
910  *
911  * For symlinks, normally the information about the target of the
912  * symlink is returned, rather than information about the symlink itself.
913  * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
914  * information about the symlink itself will be returned. Also, for symlinks
915  * that point to non-existing files the information about the symlink itself
916  * will be returned.
917  *
918  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
919  * Other errors are possible too, and depend on what kind of filesystem the file is on.
920  *
921  * Returns: a #GFileInfo for the given @file, or %NULL on error.
922  **/
923 GFileInfo *
924 g_file_query_info (GFile                *file,
925                    const char           *attributes,
926                    GFileQueryInfoFlags   flags,
927                    GCancellable         *cancellable,
928                    GError              **error)
929 {
930   GFileIface *iface;
931   
932   g_return_val_if_fail (G_IS_FILE (file), NULL);
933
934   if (g_cancellable_set_error_if_cancelled (cancellable, error))
935     return NULL;
936   
937   iface = G_FILE_GET_IFACE (file);
938
939   if (iface->query_info == NULL)
940     {
941       g_set_error (error, G_IO_ERROR,
942                    G_IO_ERROR_NOT_SUPPORTED,
943                    _("Operation not supported"));
944       return NULL;
945     }
946   
947   return (* iface->query_info) (file, attributes, flags, cancellable, error);
948 }
949
950 /**
951  * g_file_query_info_async:
952  * @file: input #GFile.
953  * @attributes: an attribute query string.
954  * @flags: a set of #GFileQueryInfoFlags.
955  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
956  *     of the request.
957  * @cancellable: optional #GCancellable object, %NULL to ignore. 
958  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
959  * @user_data: the data to pass to callback function
960  * 
961  * Asynchronously gets the requested information about specified @file. The result
962  * is a #GFileInfo object that contains key-value attributes (such as type or size
963  * for the file).
964  * 
965  * For more details, see g_file_query_info() which is
966  * the synchronous version of this call.
967  *
968  * When the operation is finished, @callback will be called. You can then call
969  * g_file_query_info_finish() to get the result of the operation.
970  **/
971 void
972 g_file_query_info_async (GFile               *file,
973                          const char          *attributes,
974                          GFileQueryInfoFlags  flags,
975                          int                  io_priority,
976                          GCancellable        *cancellable,
977                          GAsyncReadyCallback  callback,
978                          gpointer             user_data)
979 {
980   GFileIface *iface;
981   
982   g_return_if_fail (G_IS_FILE (file));
983
984   iface = G_FILE_GET_IFACE (file);
985   (* iface->query_info_async) (file,
986                                attributes,
987                                flags,
988                                io_priority,
989                                cancellable,
990                                callback,
991                                user_data);
992 }
993
994 /**
995  * g_file_query_info_finish:
996  * @file: input #GFile.
997  * @res: a #GAsyncResult. 
998  * @error: a #GError. 
999  * 
1000  * Finishes an asynchronous file info query. 
1001  * See g_file_query_info_async().
1002  * 
1003  * Returns: #GFileInfo for given @file or %NULL on error.
1004  **/
1005 GFileInfo *
1006 g_file_query_info_finish (GFile         *file,
1007                           GAsyncResult  *res,
1008                           GError       **error)
1009 {
1010   GFileIface *iface;
1011
1012   g_return_val_if_fail (G_IS_FILE (file), NULL);
1013   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1014
1015   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1016     {
1017       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1018       if (g_simple_async_result_propagate_error (simple, error))
1019         return NULL;
1020     }
1021   
1022   iface = G_FILE_GET_IFACE (file);
1023   return (* iface->query_info_finish) (file, res, error);
1024 }
1025
1026 /**
1027  * g_file_query_filesystem_info:
1028  * @file: input #GFile.
1029  * @attributes:  an attribute query string.
1030  * @cancellable: optional #GCancellable object, %NULL to ignore. 
1031  * @error: a #GError. 
1032  * 
1033  * Similar to g_file_query_info(), but obtains information
1034  * about the filesystem the @file is on, rather than the file itself.
1035  * For instance the amount of space available and the type of
1036  * the filesystem.
1037  *
1038  * The @attribute value is a string that specifies the file attributes that
1039  * should be gathered. It is not an error if it's not possible to read a particular
1040  * requested attribute from a file - it just won't be set. @attribute should
1041  * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1042  * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1043  * namespace. The standard namespace for filesystem attributes is "fs".
1044  * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1045  * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1046  * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1047  * 
1048  * If @cancellable is not %NULL, then the operation can be cancelled by
1049  * triggering the cancellable object from another thread. If the operation
1050  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1051  *
1052  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1053  * Other errors are possible too, and depend on what kind of filesystem the file is on.
1054  *
1055  * Returns: a #GFileInfo or %NULL if there was an error.
1056  **/
1057 GFileInfo *
1058 g_file_query_filesystem_info (GFile         *file,
1059                               const char    *attributes,
1060                               GCancellable  *cancellable,
1061                               GError       **error)
1062 {
1063   GFileIface *iface;
1064   
1065   g_return_val_if_fail (G_IS_FILE (file), NULL);
1066
1067   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1068     return NULL;
1069   
1070   iface = G_FILE_GET_IFACE (file);
1071
1072   if (iface->query_filesystem_info == NULL)
1073     {
1074       g_set_error (error, G_IO_ERROR,
1075                    G_IO_ERROR_NOT_SUPPORTED,
1076                    _("Operation not supported"));
1077       return NULL;
1078     }
1079   
1080   return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1081 }
1082
1083 /**
1084  * g_file_find_enclosing_mount:
1085  * @file: input #GFile.
1086  * @cancellable: optional #GCancellable object, %NULL to ignore. 
1087  * @error: a #GError. 
1088  *
1089  * Gets a #GMount for the #GFile. 
1090  *
1091  * If the #GFileIface for @file does not have a mount (e.g. possibly a 
1092  * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1093  * will be returned.
1094  * 
1095  * If @cancellable is not %NULL, then the operation can be cancelled by
1096  * triggering the cancellable object from another thread. If the operation
1097  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1098  * 
1099  * Returns: a #GMount where the @file is located or %NULL on error.
1100  **/
1101 GMount *
1102 g_file_find_enclosing_mount (GFile         *file,
1103                              GCancellable  *cancellable,
1104                              GError       **error)
1105 {
1106   GFileIface *iface;
1107
1108   g_return_val_if_fail (G_IS_FILE (file), NULL);
1109   
1110   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1111     return NULL;
1112
1113   iface = G_FILE_GET_IFACE (file);
1114   if (iface->find_enclosing_mount == NULL)
1115     {
1116       g_set_error (error, G_IO_ERROR,
1117                    G_IO_ERROR_NOT_FOUND,
1118                    _("Containing mount does not exist"));
1119       return NULL;
1120     }
1121   
1122   return (* iface->find_enclosing_mount) (file, cancellable, error);
1123 }
1124 /**
1125  * g_file_find_enclosing_mount_async:
1126  * @file: a #GFile
1127  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1128  *     of the request.
1129  * @cancellable: optional #GCancellable object, %NULL to ignore.
1130  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1131  * @user_data: the data to pass to callback function
1132  *
1133  * Asynchronously gets the mount for the file.
1134  *
1135  * For more details, see g_file_find_enclosing_mount() which is
1136  * the synchronous version of this call.
1137  *
1138  * When the operation is finished, @callback will be called. You can then call
1139  * g_file_find_enclosing_mount_finish() to get the result of the operation.
1140  */
1141 void
1142 g_file_find_enclosing_mount_async (GFile              *file,
1143                                    int                   io_priority,
1144                                    GCancellable         *cancellable,
1145                                    GAsyncReadyCallback   callback,
1146                                    gpointer              user_data)
1147 {
1148   GFileIface *iface;
1149
1150   g_return_if_fail (G_IS_FILE (file));
1151
1152   iface = G_FILE_GET_IFACE (file);
1153   (* iface->find_enclosing_mount_async) (file,
1154                                          io_priority,
1155                                          cancellable,
1156                                          callback,
1157                                          user_data);
1158 }
1159
1160 /**
1161  * g_file_find_enclosing_mount_finish:
1162  * @file: a #GFile
1163  * @res: a #GAsyncResult
1164  * @error: a #GError
1165  * 
1166  * Finishes an asynchronous find mount request. 
1167  * See g_file_find_enclosing_mount_async().
1168  * 
1169  * Returns: #GMount for given @file or %NULL on error.
1170  **/
1171 GMount *
1172 g_file_find_enclosing_mount_finish (GFile         *file,
1173                                     GAsyncResult  *res,
1174                                     GError       **error)
1175 {
1176   GFileIface *iface;
1177   
1178   g_return_val_if_fail (G_IS_FILE (file), NULL);
1179   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1180
1181   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1182     {
1183       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1184       if (g_simple_async_result_propagate_error (simple, error))
1185         return NULL;
1186     }
1187   
1188   iface = G_FILE_GET_IFACE (file);
1189   return (* iface->find_enclosing_mount_finish) (file, res, error);
1190 }
1191
1192
1193 /**
1194  * g_file_read:
1195  * @file: #GFile to read.
1196  * @cancellable: a #GCancellable
1197  * @error: a #GError, or %NULL
1198  *
1199  * Opens a file for reading. The result is a #GFileInputStream that
1200  * can be used to read the contents of the file.
1201  *
1202  * If @cancellable is not %NULL, then the operation can be cancelled by
1203  * triggering the cancellable object from another thread. If the operation
1204  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1205  * 
1206  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1207  * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1208  * Other errors are possible too, and depend on what kind of filesystem the file is on.
1209  *
1210  * Returns: #GFileInputStream or %NULL on error.
1211  **/
1212 GFileInputStream *
1213 g_file_read (GFile         *file,
1214              GCancellable  *cancellable,
1215              GError       **error)
1216 {
1217   GFileIface *iface;
1218   
1219   g_return_val_if_fail (G_IS_FILE (file), NULL);
1220
1221   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1222     return NULL;
1223
1224   iface = G_FILE_GET_IFACE (file);
1225
1226   if (iface->read_fn == NULL)
1227     {
1228       g_set_error (error, G_IO_ERROR,
1229                    G_IO_ERROR_NOT_SUPPORTED,
1230                    _("Operation not supported"));
1231       return NULL;
1232     }
1233   
1234   return (* iface->read_fn) (file, cancellable, error);
1235 }
1236
1237 /**
1238  * g_file_append_to:
1239  * @file: input #GFile.
1240  * @flags: a set of #GFileCreateFlags.
1241  * @cancellable: optional #GCancellable object, %NULL to ignore.
1242  * @error: a #GError, or %NULL
1243  *
1244  * Gets an output stream for appending data to the file. If
1245  * the file doesn't already exist it is created.
1246  *
1247  * By default files created are generally readable by everyone,
1248  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1249  * will be made readable only to the current user, to the level that
1250  * is supported on the target filesystem.
1251  *
1252  * If @cancellable is not %NULL, then the operation can be cancelled by
1253  * triggering the cancellable object from another thread. If the operation
1254  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1255  *
1256  * Some file systems don't allow all file names, and may
1257  * return an G_IO_ERROR_INVALID_FILENAME error.
1258  * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1259  * returned. Other errors are possible too, and depend on what kind of
1260  * filesystem the file is on.
1261  * 
1262  * Returns: a #GFileOutputStream.
1263  **/
1264 GFileOutputStream *
1265 g_file_append_to (GFile             *file,
1266                   GFileCreateFlags   flags,
1267                   GCancellable      *cancellable,
1268                   GError           **error)
1269 {
1270   GFileIface *iface;
1271
1272   g_return_val_if_fail (G_IS_FILE (file), NULL);
1273
1274   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1275     return NULL;
1276   
1277   iface = G_FILE_GET_IFACE (file);
1278
1279   if (iface->append_to == NULL)
1280     {
1281       g_set_error (error, G_IO_ERROR,
1282                    G_IO_ERROR_NOT_SUPPORTED,
1283                    _("Operation not supported"));
1284       return NULL;
1285     }
1286   
1287   return (* iface->append_to) (file, flags, cancellable, error);
1288 }
1289
1290 /**
1291  * g_file_create:
1292  * @file: input #GFile.
1293  * @flags: a set of #GFileCreateFlags.
1294  * @cancellable: optional #GCancellable object, %NULL to ignore.
1295  * @error: a #GError, or %NULL
1296  *
1297  * Creates a new file and returns an output stream for writing to it.
1298  * The file must not already exists.
1299  *
1300  * By default files created are generally readable by everyone,
1301  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1302  * will be made readable only to the current user, to the level that
1303  * is supported on the target filesystem.
1304  *
1305  * If @cancellable is not %NULL, then the operation can be cancelled by
1306  * triggering the cancellable object from another thread. If the operation
1307  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1308  *
1309  * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1310  * error will be returned.
1311  * Some file systems don't allow all file names, and may
1312  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1313  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1314  * Other errors are possible too, and depend on what kind of
1315  * filesystem the file is on.
1316  * 
1317  * Returns: a #GFileOutputStream for the newly created file, or 
1318  * %NULL on error.
1319  **/
1320 GFileOutputStream *
1321 g_file_create (GFile             *file,
1322                GFileCreateFlags   flags,
1323                GCancellable      *cancellable,
1324                GError           **error)
1325 {
1326   GFileIface *iface;
1327   
1328   g_return_val_if_fail (G_IS_FILE (file), NULL);
1329
1330   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1331     return NULL;
1332   
1333   iface = G_FILE_GET_IFACE (file);
1334
1335   if (iface->create == NULL)
1336     {
1337       g_set_error (error, G_IO_ERROR,
1338                    G_IO_ERROR_NOT_SUPPORTED,
1339                    _("Operation not supported"));
1340       return NULL;
1341     }
1342   
1343   return (* iface->create) (file, flags, cancellable, error);
1344 }
1345
1346 /**
1347  * g_file_replace:
1348  * @file: input #GFile.
1349  * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the 
1350  *     current #GFile, or #NULL to ignore.
1351  * @make_backup: %TRUE if a backup should be created.
1352  * @flags: a set of #GFileCreateFlags.
1353  * @cancellable: optional #GCancellable object, %NULL to ignore.
1354  * @error: a #GError, or %NULL
1355  *
1356  * Returns an output stream for overwriting the file, possibly
1357  * creating a backup copy of the file first.
1358  *
1359  * This will try to replace the file in the safest way possible so
1360  * that any errors during the writing will not affect an already
1361  * existing copy of the file. For instance, for local files it
1362  * may write to a temporary file and then atomically rename over
1363  * the destination when the stream is closed.
1364  * 
1365  * By default files created are generally readable by everyone,
1366  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1367  * will be made readable only to the current user, to the level that
1368  * is supported on the target filesystem.
1369  *
1370  * If @cancellable is not %NULL, then the operation can be cancelled by
1371  * triggering the cancellable object from another thread. If the operation
1372  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1373  * 
1374  * If you pass in a non-#NULL @etag value, then this value is
1375  * compared to the current entity tag of the file, and if they differ
1376  * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1377  * that the file has been changed since you last read it. You can get
1378  * the new etag from g_file_output_stream_get_etag() after you've
1379  * finished writing and closed the #GFileOutputStream. When you load
1380  * a new file you can use g_file_input_stream_query_info() to get
1381  * the etag of the file.
1382  * 
1383  * If @make_backup is %TRUE, this function will attempt to make a backup
1384  * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1385  * error will be returned. If you want to replace anyway, try again with
1386  * @make_backup set to %FALSE.
1387  *
1388  * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1389  * and if the file is some other form of non-regular file then a
1390  * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1391  * Some file systems don't allow all file names, and may
1392  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1393  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1394  * Other errors are possible too, and depend on what kind of
1395  * filesystem the file is on.
1396  *
1397  * Returns: a #GFileOutputStream or %NULL on error. 
1398  **/
1399 GFileOutputStream *
1400 g_file_replace (GFile             *file,
1401                 const char        *etag,
1402                 gboolean           make_backup,
1403                 GFileCreateFlags   flags,
1404                 GCancellable      *cancellable,
1405                 GError           **error)
1406 {
1407   GFileIface *iface;
1408
1409   g_return_val_if_fail (G_IS_FILE (file), NULL);
1410
1411   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1412     return NULL;
1413   
1414   iface = G_FILE_GET_IFACE (file);
1415
1416   if (iface->replace == NULL)
1417     {
1418       g_set_error (error, G_IO_ERROR,
1419                    G_IO_ERROR_NOT_SUPPORTED,
1420                    _("Operation not supported"));
1421       return NULL;
1422     }
1423   
1424   
1425   /* Handle empty tag string as NULL in consistent way. */
1426   if (etag && *etag == 0)
1427     etag = NULL;
1428   
1429   return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1430 }
1431
1432 /**
1433  * g_file_read_async:
1434  * @file: input #GFile.
1435  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1436  *     of the request. 
1437  * @cancellable: optional #GCancellable object, %NULL to ignore.
1438  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1439  * @user_data: the data to pass to callback function
1440  *
1441  * Asynchronously opens @file for reading.
1442  *
1443  * For more details, see g_file_read() which is
1444  * the synchronous version of this call.
1445  *
1446  * When the operation is finished, @callback will be called. You can then call
1447  * g_file_read_finish() to get the result of the operation.
1448  **/
1449 void
1450 g_file_read_async (GFile               *file,
1451                    int                  io_priority,
1452                    GCancellable        *cancellable,
1453                    GAsyncReadyCallback  callback,
1454                    gpointer             user_data)
1455 {
1456   GFileIface *iface;
1457   
1458   g_return_if_fail (G_IS_FILE (file));
1459
1460   iface = G_FILE_GET_IFACE (file);
1461   (* iface->read_async) (file,
1462                          io_priority,
1463                          cancellable,
1464                          callback,
1465                          user_data);
1466 }
1467
1468 /**
1469  * g_file_read_finish:
1470  * @file: input #GFile.
1471  * @res: a #GAsyncResult. 
1472  * @error: a #GError, or %NULL
1473  *
1474  * Finishes an asynchronous file read operation started with 
1475  * g_file_read_async(). 
1476  *  
1477  * Returns: a #GFileInputStream or %NULL on error.
1478  **/
1479 GFileInputStream *
1480 g_file_read_finish (GFile         *file,
1481                     GAsyncResult  *res,
1482                     GError       **error)
1483 {
1484   GFileIface *iface;
1485   
1486   g_return_val_if_fail (G_IS_FILE (file), NULL);
1487   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1488
1489   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1490     {
1491       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1492       if (g_simple_async_result_propagate_error (simple, error))
1493         return NULL;
1494     }
1495   
1496   iface = G_FILE_GET_IFACE (file);
1497   return (* iface->read_finish) (file, res, error);
1498 }
1499
1500 /**
1501  * g_file_append_to_async:
1502  * @file: input #GFile.
1503  * @flags: a set of #GFileCreateFlags.
1504  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1505  *     of the request. 
1506  * @cancellable: optional #GCancellable object, %NULL to ignore.
1507  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1508  * @user_data: the data to pass to callback function
1509  * 
1510  * Asynchronously opens @file for appending.
1511  *
1512  * For more details, see g_file_append_to() which is
1513  * the synchronous version of this call.
1514  *
1515  * When the operation is finished, @callback will be called. You can then call
1516  * g_file_append_to_finish() to get the result of the operation.
1517  **/
1518 void
1519 g_file_append_to_async (GFile               *file,
1520                         GFileCreateFlags     flags,
1521                         int                  io_priority,
1522                         GCancellable        *cancellable,
1523                         GAsyncReadyCallback  callback,
1524                         gpointer             user_data)
1525 {
1526   GFileIface *iface;
1527   
1528   g_return_if_fail (G_IS_FILE (file));
1529
1530   iface = G_FILE_GET_IFACE (file);
1531   (* iface->append_to_async) (file,
1532                               flags,
1533                               io_priority,
1534                               cancellable,
1535                               callback,
1536                               user_data);
1537 }
1538
1539 /**
1540  * g_file_append_to_finish:
1541  * @file: input #GFile.
1542  * @res: #GAsyncResult
1543  * @error: a #GError, or %NULL
1544  * 
1545  * Finishes an asynchronous file append operation started with 
1546  * g_file_append_to_async(). 
1547  * 
1548  * Returns: a valid #GFileOutputStream or %NULL on error.
1549  **/
1550 GFileOutputStream *
1551 g_file_append_to_finish (GFile         *file,
1552                          GAsyncResult  *res,
1553                          GError       **error)
1554 {
1555   GFileIface *iface;
1556   
1557   g_return_val_if_fail (G_IS_FILE (file), NULL);
1558   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1559
1560   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1561     {
1562       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1563       if (g_simple_async_result_propagate_error (simple, error))
1564         return NULL;
1565     }
1566   
1567   iface = G_FILE_GET_IFACE (file);
1568   return (* iface->append_to_finish) (file, res, error);
1569 }
1570
1571 /**
1572  * g_file_create_async:
1573  * @file: input #GFile.
1574  * @flags: a set of #GFileCreateFlags.
1575  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1576  *     of the request.
1577  * @cancellable: optional #GCancellable object, %NULL to ignore.
1578  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1579  * @user_data: the data to pass to callback function
1580  * 
1581  * Asynchronously creates a new file and returns an output stream for writing to it.
1582  * The file must not already exists.
1583  *
1584  * For more details, see g_file_create() which is
1585  * the synchronous version of this call.
1586  *
1587  * When the operation is finished, @callback will be called. You can then call
1588  * g_file_create_finish() to get the result of the operation.
1589  **/
1590 void
1591 g_file_create_async (GFile               *file,
1592                      GFileCreateFlags     flags,
1593                      int                  io_priority,
1594                      GCancellable        *cancellable,
1595                      GAsyncReadyCallback  callback,
1596                      gpointer             user_data)
1597 {
1598   GFileIface *iface;
1599   
1600   g_return_if_fail (G_IS_FILE (file));
1601
1602   iface = G_FILE_GET_IFACE (file);
1603   (* iface->create_async) (file,
1604                            flags,
1605                            io_priority,
1606                            cancellable,
1607                            callback,
1608                            user_data);
1609 }
1610
1611 /**
1612  * g_file_create_finish:
1613  * @file: input #GFile.
1614  * @res: a #GAsyncResult. 
1615  * @error: a #GError, or %NULL
1616  * 
1617  * Finishes an asynchronous file create operation started with 
1618  * g_file_create_async(). 
1619  * 
1620  * Returns: a #GFileOutputStream or %NULL on error.
1621  **/
1622 GFileOutputStream *
1623 g_file_create_finish (GFile         *file,
1624                       GAsyncResult  *res,
1625                       GError       **error)
1626 {
1627   GFileIface *iface;
1628   
1629   g_return_val_if_fail (G_IS_FILE (file), NULL);
1630   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1631
1632   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1633     {
1634       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1635       if (g_simple_async_result_propagate_error (simple, error))
1636         return NULL;
1637     }
1638   
1639   iface = G_FILE_GET_IFACE (file);
1640   return (* iface->create_finish) (file, res, error);
1641 }
1642
1643 /**
1644  * g_file_replace_async:
1645  * @file: input #GFile.
1646  * @etag: an <link linkend="gfile-etag">entity tag</link> for the 
1647  *     current #GFile, or NULL to ignore.
1648  * @make_backup: %TRUE if a backup should be created.
1649  * @flags: a set of #GFileCreateFlags.
1650  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1651  *     of the request.
1652  * @cancellable: optional #GCancellable object, %NULL to ignore.
1653  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1654  * @user_data: the data to pass to callback function
1655  *
1656  * Asynchronously overwrites the file, replacing the contents, possibly
1657  * creating a backup copy of the file first.
1658  *
1659  * For more details, see g_file_replace() which is
1660  * the synchronous version of this call.
1661  *
1662  * When the operation is finished, @callback will be called. You can then call
1663  * g_file_replace_finish() to get the result of the operation.
1664  **/
1665 void
1666 g_file_replace_async (GFile               *file,
1667                       const char          *etag,
1668                       gboolean             make_backup,
1669                       GFileCreateFlags     flags,
1670                       int                  io_priority,
1671                       GCancellable        *cancellable,
1672                       GAsyncReadyCallback  callback,
1673                       gpointer             user_data)
1674 {
1675   GFileIface *iface;
1676   
1677   g_return_if_fail (G_IS_FILE (file));
1678
1679   iface = G_FILE_GET_IFACE (file);
1680   (* iface->replace_async) (file,
1681                             etag,
1682                             make_backup,
1683                             flags,
1684                             io_priority,
1685                             cancellable,
1686                             callback,
1687                             user_data);
1688 }
1689
1690 /**
1691  * g_file_replace_finish:
1692  * @file: input #GFile.
1693  * @res: a #GAsyncResult. 
1694  * @error: a #GError, or %NULL
1695  * 
1696  * Finishes an asynchronous file replace operation started with 
1697  * g_file_replace_async(). 
1698  * 
1699  * Returns: a #GFileOutputStream, or %NULL on error.
1700  **/
1701 GFileOutputStream *
1702 g_file_replace_finish (GFile         *file,
1703                        GAsyncResult  *res,
1704                        GError       **error)
1705 {
1706   GFileIface *iface;
1707   
1708   g_return_val_if_fail (G_IS_FILE (file), NULL);
1709   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1710
1711   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1712     {
1713       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1714       if (g_simple_async_result_propagate_error (simple, error))
1715         return NULL;
1716     }
1717   
1718   iface = G_FILE_GET_IFACE (file);
1719   return (* iface->replace_finish) (file, res, error);
1720 }
1721
1722 static gboolean
1723 copy_symlink (GFile           *destination,
1724               GFileCopyFlags   flags,
1725               GCancellable    *cancellable,
1726               const char      *target,
1727               GError         **error)
1728 {
1729   GError *my_error;
1730   gboolean tried_delete;
1731   GFileInfo *info;
1732   GFileType file_type;
1733
1734   tried_delete = FALSE;
1735
1736  retry:
1737   my_error = NULL;
1738   if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1739     {
1740       /* Maybe it already existed, and we want to overwrite? */
1741       if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) && 
1742           my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1743         {
1744           g_error_free (my_error);
1745
1746
1747           /* Don't overwrite if the destination is a directory */
1748           info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1749                                     G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1750                                     cancellable, &my_error);
1751           if (info != NULL)
1752             {
1753               file_type = g_file_info_get_file_type (info);
1754               g_object_unref (info);
1755               
1756               if (file_type == G_FILE_TYPE_DIRECTORY)
1757                 {
1758                   g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1759                                _("Can't copy over directory"));
1760                   return FALSE;
1761                 }
1762             }
1763           
1764           if (!g_file_delete (destination, cancellable, error))
1765             return FALSE;
1766           
1767           tried_delete = TRUE;
1768           goto retry;
1769         }
1770             /* Nah, fail */
1771       g_propagate_error (error, my_error);
1772       return FALSE;
1773     }
1774
1775   return TRUE;
1776 }
1777
1778 static GInputStream *
1779 open_source_for_copy (GFile           *source,
1780                       GFile           *destination,
1781                       GFileCopyFlags   flags,
1782                       GCancellable    *cancellable,
1783                       GError         **error)
1784 {
1785   GError *my_error;
1786   GInputStream *in;
1787   GFileInfo *info;
1788   GFileType file_type;
1789   
1790   my_error = NULL;
1791   in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1792   if (in != NULL)
1793     return in;
1794
1795   /* There was an error opening the source, try to set a good error for it: */
1796
1797   if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1798     {
1799       /* The source is a directory, don't fail with WOULD_RECURSE immediately, 
1800        * as that is less useful to the app. Better check for errors on the 
1801        * target instead. 
1802        */
1803       g_error_free (my_error);
1804       my_error = NULL;
1805       
1806       info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1807                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1808                                 cancellable, &my_error);
1809       if (info != NULL)
1810         {
1811           file_type = g_file_info_get_file_type (info);
1812           g_object_unref (info);
1813           
1814           if (flags & G_FILE_COPY_OVERWRITE)
1815             {
1816               if (file_type == G_FILE_TYPE_DIRECTORY)
1817                 {
1818                   g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1819                                _("Can't copy directory over directory"));
1820                   return NULL;
1821                 }
1822               /* continue to would_recurse error */
1823             }
1824           else
1825             {
1826               g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1827                            _("Target file exists"));
1828               return NULL;
1829             }
1830         }
1831       else
1832         {
1833           /* Error getting info from target, return that error 
1834            * (except for NOT_FOUND, which is no error here) 
1835            */
1836           if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1837             {
1838               g_propagate_error (error, my_error);
1839               return NULL;
1840             }
1841           g_error_free (my_error);
1842         }
1843       
1844       g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1845                    _("Can't recursively copy directory"));
1846       return NULL;
1847     }
1848
1849   g_propagate_error (error, my_error);
1850   return NULL;
1851 }
1852
1853 static gboolean
1854 should_copy (GFileAttributeInfo *info, 
1855              gboolean            as_move)
1856 {
1857   if (as_move)
1858     return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1859   return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1860 }
1861
1862 static char *
1863 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1864                                GFileAttributeInfoList *namespaces,
1865                                gboolean                as_move)
1866 {
1867   GString *s;
1868   gboolean first;
1869   int i;
1870   
1871   first = TRUE;
1872   s = g_string_new ("");
1873
1874   if (attributes)
1875     {
1876       for (i = 0; i < attributes->n_infos; i++)
1877         {
1878           if (should_copy (&attributes->infos[i], as_move))
1879             {
1880               if (first)
1881                 first = FALSE;
1882               else
1883                 g_string_append_c (s, ',');
1884                 
1885               g_string_append (s, attributes->infos[i].name);
1886             }
1887         }
1888     }
1889
1890   if (namespaces)
1891     {
1892       for (i = 0; i < namespaces->n_infos; i++)
1893         {
1894           if (should_copy (&namespaces->infos[i], as_move))
1895             {
1896               if (first)
1897                 first = FALSE;
1898               else
1899                 g_string_append_c (s, ',');
1900                 
1901               g_string_append (s, namespaces->infos[i].name);
1902               g_string_append (s, ":*");
1903             }
1904         }
1905     }
1906
1907   return g_string_free (s, FALSE);
1908 }
1909
1910 /**
1911  * g_file_copy_attributes:
1912  * @source: a #GFile with attributes.
1913  * @destination: a #GFile to copy attributes to.
1914  * @flags: a set of #GFileCopyFlags.
1915  * @cancellable: optional #GCancellable object, %NULL to ignore.
1916  * @error: a #GError, %NULL to ignore.
1917  *
1918  * Copies the file attributes from @source to @destination. 
1919  *
1920  * Normally only a subset of the file attributes are copied,
1921  * those that are copies in a normal file copy operation
1922  * (which for instance does not include e.g. mtime). However
1923  * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
1924  * all the metadata that is possible to copy is copied.
1925  *
1926  * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1927  **/
1928 gboolean
1929 g_file_copy_attributes (GFile           *source,
1930                         GFile           *destination,
1931                         GFileCopyFlags   flags,
1932                         GCancellable    *cancellable,
1933                         GError         **error)
1934 {
1935   GFileAttributeInfoList *attributes, *namespaces;
1936   char *attrs_to_read;
1937   gboolean res;
1938   GFileInfo *info;
1939   gboolean as_move;
1940   gboolean source_nofollow_symlinks;
1941
1942   as_move = flags & G_FILE_COPY_ALL_METADATA;
1943   source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1944
1945   /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1946   attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1947   namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1948
1949   if (attributes == NULL && namespaces == NULL)
1950     return TRUE;
1951
1952   attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1953
1954   /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1955    * we just don't copy it. 
1956    */
1957   info = g_file_query_info (source, attrs_to_read,
1958                             source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1959                             cancellable,
1960                             NULL);
1961
1962   g_free (attrs_to_read);
1963   
1964   res = TRUE;
1965   if  (info)
1966     {
1967       res = g_file_set_attributes_from_info (destination,
1968                                              info, 0,
1969                                              cancellable,
1970                                              error);
1971       g_object_unref (info);
1972     }
1973   
1974   g_file_attribute_info_list_unref (attributes);
1975   g_file_attribute_info_list_unref (namespaces);
1976   
1977   return res;
1978 }
1979
1980 /* Closes the streams */
1981 static gboolean
1982 copy_stream_with_progress (GInputStream           *in,
1983                            GOutputStream          *out,
1984                            GCancellable           *cancellable,
1985                            GFileProgressCallback   progress_callback,
1986                            gpointer                progress_callback_data,
1987                            GError                **error)
1988 {
1989   gssize n_read, n_written;
1990   goffset current_size;
1991   char buffer[8192], *p;
1992   gboolean res;
1993   goffset total_size;
1994   GFileInfo *info;
1995
1996   total_size = 0;
1997   info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1998                                          G_FILE_ATTRIBUTE_STANDARD_SIZE,
1999                                          cancellable, NULL);
2000   if (info)
2001     {
2002       total_size = g_file_info_get_size (info);
2003       g_object_unref (info);
2004     }
2005   
2006   current_size = 0;
2007   res = TRUE;
2008   while (TRUE)
2009     {
2010       n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2011       if (n_read == -1)
2012         {
2013           res = FALSE;
2014           break;
2015         }
2016         
2017       if (n_read == 0)
2018         break;
2019
2020       current_size += n_read;
2021
2022       p = buffer;
2023       while (n_read > 0)
2024         {
2025           n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2026           if (n_written == -1)
2027             {
2028               res = FALSE;
2029               break;
2030             }
2031
2032           p += n_written;
2033           n_read -= n_written;
2034         }
2035
2036       if (!res)
2037         break;
2038       
2039       if (progress_callback)
2040         progress_callback (current_size, total_size, progress_callback_data);
2041     }
2042
2043   if (!res)
2044     error = NULL; /* Ignore further errors */
2045
2046   /* Make sure we send full copied size */
2047   if (progress_callback)
2048     progress_callback (current_size, total_size, progress_callback_data);
2049
2050   
2051   /* Don't care about errors in source here */
2052   g_input_stream_close (in, cancellable, NULL);
2053
2054   /* But write errors on close are bad! */
2055   if (!g_output_stream_close (out, cancellable, error))
2056     res = FALSE;
2057
2058   g_object_unref (in);
2059   g_object_unref (out);
2060       
2061   return res;
2062 }
2063
2064 static gboolean
2065 file_copy_fallback (GFile                  *source,
2066                     GFile                  *destination,
2067                     GFileCopyFlags          flags,
2068                     GCancellable           *cancellable,
2069                     GFileProgressCallback   progress_callback,
2070                     gpointer                progress_callback_data,
2071                     GError                **error)
2072 {
2073   GInputStream *in;
2074   GOutputStream *out;
2075   GFileInfo *info;
2076   const char *target;
2077
2078   /* Maybe copy the symlink? */
2079   if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2080     {
2081       info = g_file_query_info (source,
2082                                 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2083                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2084                                 cancellable,
2085                                 error);
2086       if (info == NULL)
2087         return FALSE;
2088
2089       if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2090           (target = g_file_info_get_symlink_target (info)) != NULL)
2091         {
2092           if (!copy_symlink (destination, flags, cancellable, target, error))
2093             {
2094               g_object_unref (info);
2095               return FALSE;
2096             }
2097           
2098           g_object_unref (info);
2099           goto copied_file;
2100         }
2101       
2102       g_object_unref (info);
2103     }
2104   
2105   in = open_source_for_copy (source, destination, flags, cancellable, error);
2106   if (in == NULL)
2107     return FALSE;
2108   
2109   if (flags & G_FILE_COPY_OVERWRITE)
2110     {
2111       out = (GOutputStream *)g_file_replace (destination,
2112                                              NULL, 0,
2113                                              flags & G_FILE_COPY_BACKUP,
2114                                              cancellable, error);
2115     }
2116   else
2117     {
2118       out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2119     }
2120
2121   if (out == NULL)
2122     {
2123       g_object_unref (in);
2124       return FALSE;
2125     }
2126
2127   if (!copy_stream_with_progress (in, out, cancellable,
2128                                   progress_callback, progress_callback_data,
2129                                   error))
2130     return FALSE;
2131
2132  copied_file:
2133
2134   /* Ignore errors here. Failure to copy metadata is not a hard error */
2135   g_file_copy_attributes (source, destination,
2136                           flags, cancellable, NULL);
2137   
2138   return TRUE;
2139 }
2140
2141 /**
2142  * g_file_copy:
2143  * @source: input #GFile.
2144  * @destination: destination #GFile
2145  * @flags: set of #GFileCopyFlags
2146  * @cancellable: optional #GCancellable object, %NULL to ignore.
2147  * @progress_callback: function to callback with progress information
2148  * @progress_callback_data: user data to pass to @progress_callback
2149  * @error: #GError to set on error, or %NULL
2150  *
2151  * Copies the file @source to the location specified by @destination.
2152  * Can not handle recursive copies of directories.
2153  *
2154  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2155  * existing @destination file is overwritten.
2156  *
2157  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2158  * will be copied as symlinks, otherwise the target of the
2159  * @source symlink will be copied.
2160  *
2161  * If @cancellable is not %NULL, then the operation can be cancelled by
2162  * triggering the cancellable object from another thread. If the operation
2163  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2164  * 
2165  * If @progress_callback is not %NULL, then the operation can be monitored by
2166  * setting this to a #GFileProgressCallback function. @progress_callback_data
2167  * will be passed to this function. It is guaranteed that this callback will
2168  * be called after all data has been transferred with the total number of bytes
2169  * copied during the operation.
2170  * 
2171  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2172  * error is returned, independent on the status of the @destination.
2173  *
2174  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2175  * error G_IO_ERROR_EXISTS is returned.
2176  *
2177  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2178  * error is returned. If trying to overwrite a directory with a directory the
2179  * G_IO_ERROR_WOULD_MERGE error is returned.
2180  *
2181  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2182  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2183  * is returned.
2184  *
2185  * If you are interested in copying the #GFile object itself (not the on-disk
2186  * file), see g_file_dup().
2187  *
2188  * Returns: %TRUE on success, %FALSE otherwise.
2189  **/
2190 gboolean
2191 g_file_copy (GFile                  *source,
2192              GFile                  *destination,
2193              GFileCopyFlags          flags,
2194              GCancellable           *cancellable,
2195              GFileProgressCallback   progress_callback,
2196              gpointer                progress_callback_data,
2197              GError                **error)
2198 {
2199   GFileIface *iface;
2200   GError *my_error;
2201   gboolean res;
2202
2203   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2204   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2205
2206   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2207     return FALSE;
2208   
2209   iface = G_FILE_GET_IFACE (destination);
2210   if (iface->copy)
2211     {
2212       my_error = NULL;
2213       res = (* iface->copy) (source, destination,
2214                              flags, cancellable,
2215                              progress_callback, progress_callback_data,
2216                              &my_error);
2217       
2218       if (res)
2219         return TRUE;
2220       
2221       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2222         {
2223           g_propagate_error (error, my_error);
2224               return FALSE;
2225         }
2226     }
2227
2228   /* If the types are different, and the destination method failed
2229      also try the source method */
2230   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2231     {
2232       iface = G_FILE_GET_IFACE (source);
2233       
2234       if (iface->copy)
2235         {
2236           my_error = NULL;
2237           res = (* iface->copy) (source, destination,
2238                                  flags, cancellable,
2239                                  progress_callback, progress_callback_data,
2240                                  &my_error);
2241           
2242           if (res)
2243             return TRUE;
2244           
2245           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2246             {
2247               g_propagate_error (error, my_error);
2248               return FALSE;
2249             }
2250         }
2251     }
2252   
2253   return file_copy_fallback (source, destination, flags, cancellable,
2254                              progress_callback, progress_callback_data,
2255                              error);
2256 }
2257
2258 /**
2259  * g_file_copy_async:
2260  * @source: input #GFile.
2261  * @destination: destination #GFile
2262  * @flags: set of #GFileCopyFlags
2263  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2264  *     of the request. 
2265  * @cancellable: optional #GCancellable object, %NULL to ignore.
2266  * @progress_callback: function to callback with progress information
2267  * @progress_callback_data: user data to pass to @progress_callback
2268  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2269  * @user_data: the data to pass to callback function
2270  *
2271  * Copies the file @source to the location specified by @destination 
2272  * asynchronously. For details of the behaviour, see g_file_copy().
2273  *
2274  * If @progress_callback is not %NULL, then that function that will be called
2275  * just like in g_file_copy(), however the callback will run in the main loop,
2276  * not in the thread that is doing the I/O operation.
2277  *
2278  * When the operation is finished, @callback will be called. You can then call
2279  * g_file_copy_finish() to get the result of the operation.
2280  **/
2281 void
2282 g_file_copy_async (GFile                  *source,
2283                    GFile                  *destination,
2284                    GFileCopyFlags          flags,
2285                    int                     io_priority,
2286                    GCancellable           *cancellable,
2287                    GFileProgressCallback   progress_callback,
2288                    gpointer                progress_callback_data,
2289                    GAsyncReadyCallback     callback,
2290                    gpointer                user_data)
2291 {
2292   GFileIface *iface;
2293
2294   g_return_if_fail (G_IS_FILE (source));
2295   g_return_if_fail (G_IS_FILE (destination));
2296
2297   iface = G_FILE_GET_IFACE (source);
2298   (* iface->copy_async) (source,
2299                          destination,
2300                          flags,
2301                          io_priority,
2302                          cancellable,
2303                          progress_callback,
2304                          progress_callback_data,
2305                          callback,
2306                          user_data);
2307 }
2308
2309 /**
2310  * g_file_copy_finish:
2311  * @file: input #GFile.
2312  * @res: a #GAsyncResult. 
2313  * @error: a #GError, or %NULL
2314  * 
2315  * Finishes copying the file started with 
2316  * g_file_copy_async().
2317  * 
2318  * Returns: a %TRUE on success, %FALSE on error.
2319  **/
2320 gboolean
2321 g_file_copy_finish (GFile        *file,
2322                     GAsyncResult *res,
2323                     GError      **error)
2324 {
2325   GFileIface *iface;
2326   
2327   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2328   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2329
2330   if (G_IS_SIMPLE_ASYNC_RESULT (res))
2331     {
2332       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2333       
2334       if (g_simple_async_result_propagate_error (simple, error))
2335         return FALSE;
2336     }
2337   
2338   iface = G_FILE_GET_IFACE (file);
2339   return (* iface->copy_finish) (file, res, error);
2340 }
2341
2342 /**
2343  * g_file_move:
2344  * @source: #GFile pointing to the source location.
2345  * @destination: #GFile pointing to the destination location.
2346  * @flags: set of #GFileCopyFlags.
2347  * @cancellable: optional #GCancellable object, %NULL to ignore.
2348  * @progress_callback: #GFileProgressCallback function for updates.
2349  * @progress_callback_data: gpointer to user data for the callback function.
2350  * @error: #GError for returning error conditions, or %NULL
2351  *
2352  *
2353  * Tries to move the file or directory @source to the location specified by @destination.
2354  * If native move operations are supported then this is used, otherwise a copy + delete
2355  * fallback is used. The native implementation may support moving directories (for instance
2356  * on moves inside the same filesystem), but the fallback code does not.
2357  * 
2358  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2359  * existing @destination file is overwritten.
2360  *
2361  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2362  * will be copied as symlinks, otherwise the target of the
2363  * @source symlink will be copied.
2364  *
2365  * If @cancellable is not %NULL, then the operation can be cancelled by
2366  * triggering the cancellable object from another thread. If the operation
2367  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2368  * 
2369  * If @progress_callback is not %NULL, then the operation can be monitored by
2370  * setting this to a #GFileProgressCallback function. @progress_callback_data
2371  * will be passed to this function. It is guaranteed that this callback will
2372  * be called after all data has been transferred with the total number of bytes
2373  * copied during the operation.
2374  * 
2375  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2376  * error is returned, independent on the status of the @destination.
2377  *
2378  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2379  * error G_IO_ERROR_EXISTS is returned.
2380  *
2381  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2382  * error is returned. If trying to overwrite a directory with a directory the
2383  * G_IO_ERROR_WOULD_MERGE error is returned.
2384  *
2385  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2386  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2387  * may be returned (if the native move operation isn't available).
2388  *
2389  * Returns: %TRUE on successful move, %FALSE otherwise.
2390  **/
2391 gboolean
2392 g_file_move (GFile                  *source,
2393              GFile                  *destination,
2394              GFileCopyFlags          flags,
2395              GCancellable           *cancellable,
2396              GFileProgressCallback   progress_callback,
2397              gpointer                progress_callback_data,
2398              GError                **error)
2399 {
2400   GFileIface *iface;
2401   GError *my_error;
2402   gboolean res;
2403
2404   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2405   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2406
2407   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2408     return FALSE;
2409   
2410   iface = G_FILE_GET_IFACE (destination);
2411   if (iface->move)
2412     {
2413       my_error = NULL;
2414       res = (* iface->move) (source, destination,
2415                              flags, cancellable,
2416                              progress_callback, progress_callback_data,
2417                              &my_error);
2418       
2419       if (res)
2420         return TRUE;
2421       
2422       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2423         {
2424           g_propagate_error (error, my_error);
2425           return FALSE;
2426         }
2427     }
2428
2429   /* If the types are different, and the destination method failed
2430      also try the source method */
2431   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2432     {
2433       iface = G_FILE_GET_IFACE (source);
2434       
2435       if (iface->move)
2436         {
2437           my_error = NULL;
2438           res = (* iface->move) (source, destination,
2439                                  flags, cancellable,
2440                                  progress_callback, progress_callback_data,
2441                                  &my_error);
2442           
2443           if (res)
2444             return TRUE;
2445           
2446           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2447             {
2448               g_propagate_error (error, my_error);
2449               return FALSE;
2450             }
2451         }
2452     }
2453   
2454   if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2455     {  
2456       g_set_error (error, G_IO_ERROR,
2457                    G_IO_ERROR_NOT_SUPPORTED,
2458                    _("Operation not supported"));
2459       return FALSE;
2460     }
2461   
2462   flags |= G_FILE_COPY_ALL_METADATA;
2463   if (!g_file_copy (source, destination, flags, cancellable,
2464                     progress_callback, progress_callback_data,
2465                     error))
2466     return FALSE;
2467
2468   return g_file_delete (source, cancellable, error);
2469 }
2470
2471 /**
2472  * g_file_make_directory
2473  * @file: input #GFile.
2474  * @cancellable: optional #GCancellable object, %NULL to ignore.
2475  * @error: a #GError, or %NULL 
2476  *
2477  * Creates a directory.
2478  * 
2479  * If @cancellable is not %NULL, then the operation can be cancelled by
2480  * triggering the cancellable object from another thread. If the operation
2481  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2482  * 
2483  * Returns: %TRUE on successful creation, %FALSE otherwise.
2484  **/
2485 gboolean
2486 g_file_make_directory (GFile         *file,
2487                        GCancellable  *cancellable,
2488                        GError       **error)
2489 {
2490   GFileIface *iface;
2491
2492   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2493
2494   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2495     return FALSE;
2496   
2497   iface = G_FILE_GET_IFACE (file);
2498
2499   if (iface->make_directory == NULL)
2500     {
2501       g_set_error (error, G_IO_ERROR,
2502                    G_IO_ERROR_NOT_SUPPORTED,
2503                    _("Operation not supported"));
2504       return FALSE;
2505     }
2506   
2507   return (* iface->make_directory) (file, cancellable, error);
2508 }
2509
2510 /**
2511  * g_file_make_symbolic_link:
2512  * @file: input #GFile.
2513  * @symlink_value: a string with the value of the new symlink.
2514  * @cancellable: optional #GCancellable object, %NULL to ignore.
2515  * @error: a #GError. 
2516  * 
2517  * Creates a symbolic link.
2518  *
2519  * If @cancellable is not %NULL, then the operation can be cancelled by
2520  * triggering the cancellable object from another thread. If the operation
2521  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2522  * 
2523  * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2524  **/
2525 gboolean
2526 g_file_make_symbolic_link (GFile         *file,
2527                            const char    *symlink_value,
2528                            GCancellable  *cancellable,
2529                            GError       **error)
2530 {
2531   GFileIface *iface;
2532
2533   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2534   g_return_val_if_fail (symlink_value != NULL, FALSE);
2535
2536   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2537     return FALSE;
2538
2539   if (*symlink_value == '\0')
2540     {
2541       g_set_error (error, G_IO_ERROR,
2542                    G_IO_ERROR_INVALID_ARGUMENT,
2543                    _("Invalid symlink value given"));
2544       return FALSE;
2545     }
2546   
2547   iface = G_FILE_GET_IFACE (file);
2548
2549   if (iface->make_symbolic_link == NULL)
2550     {
2551       g_set_error (error, G_IO_ERROR,
2552                    G_IO_ERROR_NOT_SUPPORTED,
2553                    _("Operation not supported"));
2554       return FALSE;
2555     }
2556   
2557   return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2558 }
2559
2560 /**
2561  * g_file_delete:
2562  * @file: input #GFile.
2563  * @cancellable: optional #GCancellable object, %NULL to ignore.
2564  * @error: a #GError, or %NULL 
2565  * 
2566  * Deletes a file.
2567  * 
2568  * If @cancellable is not %NULL, then the operation can be cancelled by
2569  * triggering the cancellable object from another thread. If the operation
2570  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2571  * 
2572  * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2573  **/
2574 gboolean
2575 g_file_delete (GFile         *file,
2576                GCancellable  *cancellable,
2577                GError       **error)
2578 {
2579   GFileIface *iface;
2580   
2581   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2582
2583   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2584     return FALSE;
2585   
2586   iface = G_FILE_GET_IFACE (file);
2587
2588   if (iface->delete_file == NULL)
2589     {
2590       g_set_error (error, G_IO_ERROR,
2591                    G_IO_ERROR_NOT_SUPPORTED,
2592                    _("Operation not supported"));
2593       return FALSE;
2594     }
2595   
2596   return (* iface->delete_file) (file, cancellable, error);
2597 }
2598
2599 /**
2600  * g_file_trash:
2601  * @file: #GFile to send to trash.
2602  * @cancellable: optional #GCancellable object, %NULL to ignore.
2603  * @error: a #GError, or %NULL
2604  *
2605  * Sends @file to the "Trashcan", if possible. This is similar to
2606  * deleting it, but the user can recover it before emptying the trashcan.
2607  * Not all file systems support trashing, so this call can return the
2608  * %G_IO_ERROR_NOT_SUPPORTED error.
2609  *
2610  *
2611  * If @cancellable is not %NULL, then the operation can be cancelled by
2612  * triggering the cancellable object from another thread. If the operation
2613  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2614  * 
2615  * Returns: %TRUE on successful trash, %FALSE otherwise.
2616  **/
2617 gboolean
2618 g_file_trash (GFile         *file,
2619               GCancellable  *cancellable,
2620               GError       **error)
2621 {
2622   GFileIface *iface;
2623   
2624   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2625
2626   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2627     return FALSE;
2628   
2629   iface = G_FILE_GET_IFACE (file);
2630
2631   if (iface->trash == NULL)
2632     {
2633       g_set_error (error,
2634                    G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2635                    _("Trash not supported"));
2636       return FALSE;
2637     }
2638   
2639   return (* iface->trash) (file, cancellable, error);
2640 }
2641
2642 /**
2643  * g_file_set_display_name:
2644  * @file: input #GFile.
2645  * @display_name: a string.
2646  * @cancellable: optional #GCancellable object, %NULL to ignore.
2647  * @error: a #GError, or %NULL
2648  * 
2649  * Renames @file to the specified display name.
2650  *
2651  * The display name is converted from UTF8 to the correct encoding for the target
2652  * filesystem if possible and the @file is renamed to this.
2653  * 
2654  * If you want to implement a rename operation in the user interface the edit name
2655  * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2656  * widget, and then the result after editing should be passed to g_file_set_display_name().
2657  *
2658  * On success the resulting converted filename is returned.
2659  * 
2660  * If @cancellable is not %NULL, then the operation can be cancelled by
2661  * triggering the cancellable object from another thread. If the operation
2662  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2663  * 
2664  * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2665  **/
2666 GFile *
2667 g_file_set_display_name (GFile         *file,
2668                          const char    *display_name,
2669                          GCancellable  *cancellable,
2670                          GError       **error)
2671 {
2672   GFileIface *iface;
2673   
2674   g_return_val_if_fail (G_IS_FILE (file), NULL);
2675   g_return_val_if_fail (display_name != NULL, NULL);
2676
2677   if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2678     {
2679       g_set_error (error,
2680                    G_IO_ERROR,
2681                    G_IO_ERROR_INVALID_ARGUMENT,
2682                    _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2683       return NULL;
2684     }
2685   
2686   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2687     return NULL;
2688   
2689   iface = G_FILE_GET_IFACE (file);
2690
2691   return (* iface->set_display_name) (file, display_name, cancellable, error);
2692 }
2693
2694 /**
2695  * g_file_set_display_name_async:
2696  * @file: input #GFile.
2697  * @display_name: a string.
2698  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2699  *     of the request. 
2700  * @cancellable: optional #GCancellable object, %NULL to ignore.
2701  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2702  * @user_data: the data to pass to callback function
2703  * 
2704  * Asynchronously sets the display name for a given #GFile.
2705  * 
2706  * For more details, see g_set_display_name() which is
2707  * the synchronous version of this call.
2708  *
2709  * When the operation is finished, @callback will be called. You can then call
2710  * g_file_set_display_name_finish() to get the result of the operation.
2711  **/
2712 void
2713 g_file_set_display_name_async (GFile               *file,
2714                                const char          *display_name,
2715                                int                  io_priority,
2716                                GCancellable        *cancellable,
2717                                GAsyncReadyCallback  callback,
2718                                gpointer             user_data)
2719 {
2720   GFileIface *iface;
2721   
2722   g_return_if_fail (G_IS_FILE (file));
2723   g_return_if_fail (display_name != NULL);
2724
2725   iface = G_FILE_GET_IFACE (file);
2726   (* iface->set_display_name_async) (file,
2727                                      display_name,
2728                                      io_priority,
2729                                      cancellable,
2730                                      callback,
2731                                      user_data);
2732 }
2733
2734 /**
2735  * g_file_set_display_name_finish:
2736  * @file: input #GFile.
2737  * @res: a #GAsyncResult. 
2738  * @error: a #GError, or %NULL
2739  * 
2740  * Finishes setting a display name started with 
2741  * g_file_set_display_name_async().
2742  * 
2743  * Returns: a #GFile or %NULL on error.
2744  **/
2745 GFile *
2746 g_file_set_display_name_finish (GFile         *file,
2747                                 GAsyncResult  *res,
2748                                 GError       **error)
2749 {
2750   GFileIface *iface;
2751   
2752   g_return_val_if_fail (G_IS_FILE (file), NULL);
2753   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2754
2755   if (G_IS_SIMPLE_ASYNC_RESULT (res))
2756     {
2757       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2758       if (g_simple_async_result_propagate_error (simple, error))
2759         return NULL;
2760     }
2761   
2762   iface = G_FILE_GET_IFACE (file);
2763   return (* iface->set_display_name_finish) (file, res, error);
2764 }
2765
2766 /**
2767  * g_file_query_settable_attributes:
2768  * @file: input #GFile.
2769  * @cancellable: optional #GCancellable object, %NULL to ignore.
2770  * @error: a #GError, or %NULL
2771  * 
2772  * Obtain the list of settable attributes for the file.
2773  *
2774  * Returns the type and full attribute name of all the attributes 
2775  * that can be set on this file. This doesn't mean setting it will always 
2776  * succeed though, you might get an access failure, or some specific 
2777  * file may not support a specific attribute.
2778  *
2779  * If @cancellable is not %NULL, then the operation can be cancelled by
2780  * triggering the cancellable object from another thread. If the operation
2781  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2782  * 
2783  * Returns: a #GFileAttributeInfoList describing the settable attributes.
2784  * When you are done with it, release it with g_file_attribute_info_list_unref()
2785  **/
2786 GFileAttributeInfoList *
2787 g_file_query_settable_attributes (GFile         *file,
2788                                   GCancellable  *cancellable,
2789                                   GError       **error)
2790 {
2791   GFileIface *iface;
2792   GError *my_error;
2793   GFileAttributeInfoList *list;
2794
2795   g_return_val_if_fail (G_IS_FILE (file), NULL);
2796
2797   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2798     return NULL;
2799   
2800   iface = G_FILE_GET_IFACE (file);
2801
2802   if (iface->query_settable_attributes == NULL)
2803     return g_file_attribute_info_list_new ();
2804
2805   my_error = NULL;
2806   list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2807   
2808   if (list == NULL)
2809     {
2810       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2811         {
2812           list = g_file_attribute_info_list_new ();
2813           g_error_free (my_error);
2814         }
2815       else
2816         g_propagate_error (error, my_error);
2817     }
2818   
2819   return list;
2820 }
2821
2822 /**
2823  * g_file_query_writable_namespaces:
2824  * @file: input #GFile.
2825  * @cancellable: optional #GCancellable object, %NULL to ignore.
2826  * @error: a #GError, or %NULL
2827  * 
2828  * Obtain the list of attribute namespaces where new attributes 
2829  * can be created by a user. An example of this is extended
2830  * attributes (in the "xattr" namespace).
2831  *
2832  * If @cancellable is not %NULL, then the operation can be cancelled by
2833  * triggering the cancellable object from another thread. If the operation
2834  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2835  * 
2836  * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2837  * When you are done with it, release it with g_file_attribute_info_list_unref()
2838  **/
2839 GFileAttributeInfoList *
2840 g_file_query_writable_namespaces (GFile         *file,
2841                                   GCancellable  *cancellable,
2842                                   GError       **error)
2843 {
2844   GFileIface *iface;
2845   GError *my_error;
2846   GFileAttributeInfoList *list;
2847   
2848   g_return_val_if_fail (G_IS_FILE (file), NULL);
2849
2850   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2851     return NULL;
2852   
2853   iface = G_FILE_GET_IFACE (file);
2854
2855   if (iface->query_writable_namespaces == NULL)
2856     return g_file_attribute_info_list_new ();
2857
2858   my_error = NULL;
2859   list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2860   
2861   if (list == NULL)
2862     {
2863       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2864         {
2865           list = g_file_attribute_info_list_new ();
2866           g_error_free (my_error);
2867         }
2868       else
2869         g_propagate_error (error, my_error);
2870     }
2871   
2872   return list;
2873 }
2874
2875 /**
2876  * g_file_set_attribute:
2877  * @file: input #GFile.
2878  * @attribute: a string containing the attribute's name.
2879  * @type: The type of the attribute
2880  * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2881  * @flags: a set of #GFileQueryInfoFlags.
2882  * @cancellable: optional #GCancellable object, %NULL to ignore.
2883  * @error: a #GError, or %NULL
2884  * 
2885  * Sets an attribute in the file with attribute name @attribute to @value.
2886  * 
2887  * If @cancellable is not %NULL, then the operation can be cancelled by
2888  * triggering the cancellable object from another thread. If the operation
2889  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2890  * 
2891  * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2892  **/
2893 gboolean
2894 g_file_set_attribute (GFile                      *file,
2895                       const char                 *attribute,
2896                       GFileAttributeType          type,
2897                       gpointer                    value_p,
2898                       GFileQueryInfoFlags         flags,
2899                       GCancellable               *cancellable,
2900                       GError                    **error)
2901 {
2902   GFileIface *iface;
2903   
2904   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2905   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2906
2907   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2908     return FALSE;
2909   
2910   iface = G_FILE_GET_IFACE (file);
2911
2912   if (iface->set_attribute == NULL)
2913     {
2914       g_set_error (error, G_IO_ERROR,
2915                    G_IO_ERROR_NOT_SUPPORTED,
2916                    _("Operation not supported"));
2917       return FALSE;
2918     }
2919
2920   return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2921 }
2922
2923 /**
2924  * g_file_set_attributes_from_info:
2925  * @file: input #GFile.
2926  * @info: a #GFileInfo.
2927  * @flags: #GFileQueryInfoFlags
2928  * @cancellable: optional #GCancellable object, %NULL to ignore.
2929  * @error: a #GError, or %NULL 
2930  * 
2931  * Tries to set all attributes in the #GFileInfo on the target values, 
2932  * not stopping on the first error.
2933  * 
2934  * If there is any error during this operation then @error will be set to
2935  * the first error. Error on particular fields are flagged by setting 
2936  * the "status" field in the attribute value to 
2937  * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2938  * further errors.
2939  *
2940  * If @cancellable is not %NULL, then the operation can be cancelled by
2941  * triggering the cancellable object from another thread. If the operation
2942  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2943  * 
2944  * Returns: %TRUE if there was any error, %FALSE otherwise.
2945  **/
2946 gboolean
2947 g_file_set_attributes_from_info (GFile                *file,
2948                                  GFileInfo            *info,
2949                                  GFileQueryInfoFlags   flags,
2950                                  GCancellable         *cancellable,
2951                                  GError              **error)
2952 {
2953   GFileIface *iface;
2954
2955   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2956   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2957
2958   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2959     return FALSE;
2960   
2961   g_file_info_clear_status (info);
2962   
2963   iface = G_FILE_GET_IFACE (file);
2964
2965   return (* iface->set_attributes_from_info) (file, 
2966                                               info, 
2967                                               flags, 
2968                                               cancellable, 
2969                                               error);
2970 }
2971
2972
2973 static gboolean
2974 g_file_real_set_attributes_from_info (GFile                *file,
2975                                       GFileInfo            *info,
2976                                       GFileQueryInfoFlags   flags,
2977                                       GCancellable         *cancellable,
2978                                       GError              **error)
2979 {
2980   char **attributes;
2981   int i;
2982   gboolean res;
2983   GFileAttributeValue *value;
2984   
2985   res = TRUE;
2986   
2987   attributes = g_file_info_list_attributes (info, NULL);
2988
2989   for (i = 0; attributes[i] != NULL; i++)
2990     {
2991       value = _g_file_info_get_attribute_value (info, attributes[i]);
2992
2993       if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2994         continue;
2995
2996       if (!g_file_set_attribute (file, attributes[i],
2997                                  value->type, _g_file_attribute_value_peek_as_pointer (value),
2998                                  flags, cancellable, error))
2999         {
3000           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3001           res = FALSE;
3002           /* Don't set error multiple times */
3003           error = NULL;
3004         }
3005       else
3006         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3007     }
3008   
3009   g_strfreev (attributes);
3010   
3011   return res;
3012 }
3013
3014 /**
3015  * g_file_set_attributes_async:
3016  * @file: input #GFile.
3017  * @info: a #GFileInfo.
3018  * @flags: a #GFileQueryInfoFlags.
3019  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
3020  *     of the request. 
3021  * @cancellable: optional #GCancellable object, %NULL to ignore.
3022  * @callback: a #GAsyncReadyCallback. 
3023  * @user_data: a #gpointer.
3024  *
3025  * Asynchronously sets the attributes of @file with @info.
3026  * 
3027  * For more details, see g_file_set_attributes_from_info() which is
3028  * the synchronous version of this call.
3029  *
3030  * When the operation is finished, @callback will be called. You can then call
3031  * g_file_set_attributes_finish() to get the result of the operation.
3032  **/
3033 void
3034 g_file_set_attributes_async (GFile               *file,
3035                              GFileInfo           *info,
3036                              GFileQueryInfoFlags  flags,
3037                              int                  io_priority,
3038                              GCancellable        *cancellable,
3039                              GAsyncReadyCallback  callback,
3040                              gpointer             user_data)
3041 {
3042   GFileIface *iface;
3043   
3044   g_return_if_fail (G_IS_FILE (file));
3045   g_return_if_fail (G_IS_FILE_INFO (info));
3046
3047   iface = G_FILE_GET_IFACE (file);
3048   (* iface->set_attributes_async) (file, 
3049                                    info, 
3050                                    flags, 
3051                                    io_priority, 
3052                                    cancellable, 
3053                                    callback, 
3054                                    user_data);
3055 }
3056
3057 /**
3058  * g_file_set_attributes_finish:
3059  * @file: input #GFile.
3060  * @result: a #GAsyncResult.
3061  * @info: a #GFileInfo.
3062  * @error: a #GError, or %NULL
3063  * 
3064  * Finishes setting an attribute started in g_file_set_attributes_async().
3065  * 
3066  * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3067  **/
3068 gboolean
3069 g_file_set_attributes_finish (GFile         *file,
3070                               GAsyncResult  *result,
3071                               GFileInfo    **info,
3072                               GError       **error)
3073 {
3074   GFileIface *iface;
3075   
3076   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3077   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3078
3079   /* No standard handling of errors here, as we must set info even
3080    * on errors 
3081    */
3082   iface = G_FILE_GET_IFACE (file);
3083   return (* iface->set_attributes_finish) (file, result, info, error);
3084 }
3085
3086 /**
3087  * g_file_set_attribute_string:
3088  * @file: input #GFile.
3089  * @attribute: a string containing the attribute's name.
3090  * @value: a string containing the attribute's value.
3091  * @flags: #GFileQueryInfoFlags.
3092  * @cancellable: optional #GCancellable object, %NULL to ignore.
3093  * @error: a #GError, or %NULL
3094  * 
3095  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value. 
3096  * If @attribute is of a different type, this operation will fail.
3097  * 
3098  * If @cancellable is not %NULL, then the operation can be cancelled by
3099  * triggering the cancellable object from another thread. If the operation
3100  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3101  * 
3102  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3103  **/
3104 gboolean
3105 g_file_set_attribute_string (GFile                *file,
3106                              const char           *attribute,
3107                              const char           *value,
3108                              GFileQueryInfoFlags   flags,
3109                              GCancellable         *cancellable,
3110                              GError              **error)
3111 {
3112   return g_file_set_attribute (file, attribute,
3113                                G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3114                                flags, cancellable, error);
3115 }
3116
3117 /**
3118  * g_file_set_attribute_byte_string:
3119  * @file: input #GFile.
3120  * @attribute: a string containing the attribute's name.
3121  * @value: a string containing the attribute's new value.
3122  * @flags: a #GFileQueryInfoFlags.
3123  * @cancellable: optional #GCancellable object, %NULL to ignore.
3124  * @error: a #GError, or %NULL
3125  * 
3126  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value. 
3127  * If @attribute is of a different type, this operation will fail, 
3128  * returning %FALSE. 
3129  * 
3130  * If @cancellable is not %NULL, then the operation can be cancelled by
3131  * triggering the cancellable object from another thread. If the operation
3132  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3133  * 
3134  * Returns: %TRUE if the @attribute was successfully set to @value 
3135  * in the @file, %FALSE otherwise.
3136  **/
3137 gboolean
3138 g_file_set_attribute_byte_string  (GFile                *file,
3139                                    const char           *attribute,
3140                                    const char           *value,
3141                                    GFileQueryInfoFlags   flags,
3142                                    GCancellable         *cancellable,
3143                                    GError              **error)
3144 {
3145   return g_file_set_attribute (file, attribute,
3146                                G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3147                                flags, cancellable, error);
3148 }
3149
3150 /**
3151  * g_file_set_attribute_uint32:
3152  * @file: input #GFile.
3153  * @attribute: a string containing the attribute's name.
3154  * @value: a #guint32 containing the attribute's new value.
3155  * @flags: a #GFileQueryInfoFlags.
3156  * @cancellable: optional #GCancellable object, %NULL to ignore.
3157  * @error: a #GError, or %NULL
3158  * 
3159  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value. 
3160  * If @attribute is of a different type, this operation will fail.
3161  * 
3162  * If @cancellable is not %NULL, then the operation can be cancelled by
3163  * triggering the cancellable object from another thread. If the operation
3164  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3165  * 
3166  * Returns: %TRUE if the @attribute was successfully set to @value 
3167  * in the @file, %FALSE otherwise.
3168  **/
3169 gboolean
3170 g_file_set_attribute_uint32 (GFile                *file,
3171                              const char           *attribute,
3172                              guint32               value,
3173                              GFileQueryInfoFlags   flags,
3174                              GCancellable         *cancellable,
3175                              GError              **error)
3176 {
3177   return g_file_set_attribute (file, attribute,
3178                                G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3179                                flags, cancellable, error);
3180 }
3181
3182 /**
3183  * g_file_set_attribute_int32:
3184  * @file: input #GFile.
3185  * @attribute: a string containing the attribute's name.
3186  * @value: a #gint32 containing the attribute's new value.
3187  * @flags: a #GFileQueryInfoFlags.
3188  * @cancellable: optional #GCancellable object, %NULL to ignore.
3189  * @error: a #GError, or %NULL
3190  * 
3191  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value. 
3192  * If @attribute is of a different type, this operation will fail.
3193  * 
3194  * If @cancellable is not %NULL, then the operation can be cancelled by
3195  * triggering the cancellable object from another thread. If the operation
3196  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3197  * 
3198  * Returns: %TRUE if the @attribute was successfully set to @value 
3199  * in the @file, %FALSE otherwise. 
3200  **/
3201 gboolean
3202 g_file_set_attribute_int32 (GFile                *file,
3203                             const char           *attribute,
3204                             gint32                value,
3205                             GFileQueryInfoFlags   flags,
3206                             GCancellable         *cancellable,
3207                             GError              **error)
3208 {
3209   return g_file_set_attribute (file, attribute,
3210                                G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3211                                flags, cancellable, error);
3212 }
3213
3214 /**
3215  * g_file_set_attribute_uint64:
3216  * @file: input #GFile. 
3217  * @attribute: a string containing the attribute's name.
3218  * @value: a #guint64 containing the attribute's new value.
3219  * @flags: a #GFileQueryInfoFlags.
3220  * @cancellable: optional #GCancellable object, %NULL to ignore.
3221  * @error: a #GError, or %NULL
3222  * 
3223  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value. 
3224  * If @attribute is of a different type, this operation will fail.
3225  * 
3226  * If @cancellable is not %NULL, then the operation can be cancelled by
3227  * triggering the cancellable object from another thread. If the operation
3228  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3229  * 
3230  * Returns: %TRUE if the @attribute was successfully set to @value 
3231  * in the @file, %FALSE otherwise.
3232  **/
3233 gboolean
3234 g_file_set_attribute_uint64 (GFile                *file,
3235                              const char           *attribute,
3236                              guint64               value,
3237                              GFileQueryInfoFlags   flags,
3238                              GCancellable         *cancellable,
3239                              GError              **error)
3240  {
3241   return g_file_set_attribute (file, attribute,
3242                                G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3243                                flags, cancellable, error);
3244 }
3245
3246 /**
3247  * g_file_set_attribute_int64:
3248  * @file: input #GFile.
3249  * @attribute: a string containing the attribute's name.
3250  * @value: a #guint64 containing the attribute's new value.
3251  * @flags: a #GFileQueryInfoFlags.
3252  * @cancellable: optional #GCancellable object, %NULL to ignore.
3253  * @error: a #GError, or %NULL
3254  * 
3255  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value. 
3256  * If @attribute is of a different type, this operation will fail.
3257  * 
3258  * If @cancellable is not %NULL, then the operation can be cancelled by
3259  * triggering the cancellable object from another thread. If the operation
3260  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3261  * 
3262  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3263  **/
3264 gboolean
3265 g_file_set_attribute_int64 (GFile                *file,
3266                             const char           *attribute,
3267                             gint64                value,
3268                             GFileQueryInfoFlags   flags,
3269                             GCancellable         *cancellable,
3270                             GError              **error)
3271 {
3272   return g_file_set_attribute (file, attribute,
3273                                G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3274                                flags, cancellable, error);
3275 }
3276
3277 /**
3278  * g_file_mount_mountable:
3279  * @file: input #GFile.
3280  * @flags: flags affecting the operation
3281  * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3282  * @cancellable: optional #GCancellable object, %NULL to ignore.
3283  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3284  * @user_data: the data to pass to callback function
3285  * 
3286  * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3287  * Using @mount_operation, you can request callbacks when, for instance, 
3288  * passwords are needed during authentication.
3289  *
3290  * If @cancellable is not %NULL, then the operation can be cancelled by
3291  * triggering the cancellable object from another thread. If the operation
3292  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3293  *
3294  * When the operation is finished, @callback will be called. You can then call
3295  * g_file_mount_mountable_finish() to get the result of the operation.
3296  **/
3297 void
3298 g_file_mount_mountable (GFile               *file,
3299                         GMountMountFlags     flags,
3300                         GMountOperation     *mount_operation,
3301                         GCancellable        *cancellable,
3302                         GAsyncReadyCallback  callback,
3303                         gpointer             user_data)
3304 {
3305   GFileIface *iface;
3306
3307   g_return_if_fail (G_IS_FILE (file));
3308
3309   iface = G_FILE_GET_IFACE (file);
3310
3311   if (iface->mount_mountable == NULL) {
3312     g_simple_async_report_error_in_idle (G_OBJECT (file),
3313                                          callback,
3314                                          user_data,
3315                                          G_IO_ERROR,
3316                                          G_IO_ERROR_NOT_SUPPORTED,
3317                                          _("Operation not supported"));
3318     return;
3319   }
3320   
3321   (* iface->mount_mountable) (file,
3322                               flags,
3323                               mount_operation,
3324                               cancellable,
3325                               callback,
3326                               user_data);
3327 }
3328
3329 /**
3330  * g_file_mount_mountable_finish:
3331  * @file: input #GFile.
3332  * @result: a #GAsyncResult.
3333  * @error: a #GError, or %NULL
3334  *
3335  * Finishes a mount operation. See g_file_mount_mountable() for details.
3336  * 
3337  * Finish an asynchronous mount operation that was started 
3338  * with g_file_mount_mountable().
3339  *
3340  * Returns: a #GFile or %NULL on error.
3341  **/
3342 GFile *
3343 g_file_mount_mountable_finish (GFile         *file,
3344                                GAsyncResult  *result,
3345                                GError       **error)
3346 {
3347   GFileIface *iface;
3348
3349   g_return_val_if_fail (G_IS_FILE (file), NULL);
3350   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3351
3352   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3353     {
3354       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3355       if (g_simple_async_result_propagate_error (simple, error))
3356         return NULL;
3357     }
3358   
3359   iface = G_FILE_GET_IFACE (file);
3360   return (* iface->mount_mountable_finish) (file, result, error);
3361 }
3362
3363 /**
3364  * g_file_unmount_mountable:
3365  * @file: input #GFile.
3366  * @flags: flags affecting the operation
3367  * @cancellable: optional #GCancellable object, %NULL to ignore.
3368  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3369  * @user_data: the data to pass to callback function
3370  *
3371  * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3372  *
3373  * If @cancellable is not %NULL, then the operation can be cancelled by
3374  * triggering the cancellable object from another thread. If the operation
3375  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3376  *
3377  * When the operation is finished, @callback will be called. You can then call
3378  * g_file_unmount_mountable_finish() to get the result of the operation.
3379  **/
3380 void
3381 g_file_unmount_mountable (GFile               *file,
3382                           GMountUnmountFlags   flags,
3383                           GCancellable        *cancellable,
3384                           GAsyncReadyCallback  callback,
3385                           gpointer             user_data)
3386 {
3387   GFileIface *iface;
3388   
3389   g_return_if_fail (G_IS_FILE (file));
3390
3391   iface = G_FILE_GET_IFACE (file);
3392   
3393   if (iface->unmount_mountable == NULL)
3394     g_simple_async_report_error_in_idle (G_OBJECT (file),
3395                                          callback,
3396                                          user_data,
3397                                          G_IO_ERROR,
3398                                          G_IO_ERROR_NOT_SUPPORTED,
3399                                          _("Operation not supported"));
3400   
3401   (* iface->unmount_mountable) (file,
3402                                 flags,
3403                                 cancellable,
3404                                 callback,
3405                                 user_data);
3406 }
3407
3408 /**
3409  * g_file_unmount_mountable_finish:
3410  * @file: input #GFile.
3411  * @result: a #GAsyncResult.
3412  * @error: a #GError, or %NULL
3413  *
3414  * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3415  * 
3416  * Finish an asynchronous unmount operation that was started 
3417  * with g_file_unmount_mountable().
3418  *
3419  * Returns: %TRUE if the operation finished successfully. %FALSE
3420  * otherwise.
3421  **/
3422 gboolean
3423 g_file_unmount_mountable_finish (GFile         *file,
3424                                  GAsyncResult  *result,
3425                                  GError       **error)
3426 {
3427   GFileIface *iface;
3428   
3429   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3430   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3431
3432   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3433     {
3434       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3435       if (g_simple_async_result_propagate_error (simple, error))
3436         return FALSE;
3437     }
3438   
3439   iface = G_FILE_GET_IFACE (file);
3440   return (* iface->unmount_mountable_finish) (file, result, error);
3441 }
3442
3443 /**
3444  * g_file_eject_mountable:
3445  * @file: input #GFile.
3446  * @flags: flags affecting the operation
3447  * @cancellable: optional #GCancellable object, %NULL to ignore.
3448  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3449  * @user_data: the data to pass to callback function
3450  * 
3451  * Starts an asynchronous eject on a mountable.  
3452  * When this operation has completed, @callback will be called with
3453  * @user_user data, and the operation can be finalized with 
3454  * g_file_eject_mountable_finish().
3455  * 
3456  * If @cancellable is not %NULL, then the operation can be cancelled by
3457  * triggering the cancellable object from another thread. If the operation
3458  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3459  **/
3460 void
3461 g_file_eject_mountable (GFile               *file,
3462                         GMountUnmountFlags   flags,
3463                         GCancellable        *cancellable,
3464                         GAsyncReadyCallback  callback,
3465                         gpointer             user_data)
3466 {
3467   GFileIface *iface;
3468
3469   g_return_if_fail (G_IS_FILE (file));
3470
3471   iface = G_FILE_GET_IFACE (file);
3472   
3473   if (iface->eject_mountable == NULL)
3474     g_simple_async_report_error_in_idle (G_OBJECT (file),
3475                                          callback,
3476                                          user_data,
3477                                          G_IO_ERROR,
3478                                          G_IO_ERROR_NOT_SUPPORTED,
3479                                          _("Operation not supported"));
3480   
3481   (* iface->eject_mountable) (file,
3482                               flags,
3483                               cancellable,
3484                               callback,
3485                               user_data);
3486 }
3487
3488 /**
3489  * g_file_eject_mountable_finish:
3490  * @file: input #GFile.
3491  * @result: a #GAsyncResult.
3492  * @error: a #GError, or %NULL
3493  * 
3494  * Finishes an asynchronous eject operation started by 
3495  * g_file_eject_mountable().
3496  * 
3497  * Returns: %TRUE if the @file was ejected successfully. %FALSE 
3498  * otherwise.
3499  **/
3500 gboolean
3501 g_file_eject_mountable_finish (GFile         *file,
3502                                GAsyncResult  *result,
3503                                GError       **error)
3504 {
3505   GFileIface *iface;
3506   
3507   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3508   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3509
3510   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3511     {
3512       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3513       if (g_simple_async_result_propagate_error (simple, error))
3514         return FALSE;
3515     }
3516   
3517   iface = G_FILE_GET_IFACE (file);
3518   return (* iface->eject_mountable_finish) (file, result, error);
3519 }
3520
3521 /**
3522  * g_file_monitor_directory:
3523  * @file: input #GFile.
3524  * @flags: a set of #GFileMonitorFlags.
3525  * @cancellable: optional #GCancellable object, %NULL to ignore.
3526  * @error: a #GError, or %NULL.
3527  * 
3528  * Obtains a directory monitor for the given file.
3529  * This may fail if directory monitoring is not supported.
3530  *
3531  * If @cancellable is not %NULL, then the operation can be cancelled by
3532  * triggering the cancellable object from another thread. If the operation
3533  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3534  * 
3535  * Returns: a #GFileMonitor for the given @file, 
3536  * or %NULL on error.
3537  **/
3538 GFileMonitor*
3539 g_file_monitor_directory (GFile             *file,
3540                           GFileMonitorFlags  flags,
3541                           GCancellable      *cancellable,
3542                           GError           **error)
3543 {
3544   GFileIface *iface;
3545
3546   g_return_val_if_fail (G_IS_FILE (file), NULL);
3547
3548   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3549     return NULL;
3550
3551   iface = G_FILE_GET_IFACE (file);
3552
3553   if (iface->monitor_dir == NULL)
3554     {
3555       g_set_error (error, G_IO_ERROR,
3556                    G_IO_ERROR_NOT_SUPPORTED,
3557                    _("Operation not supported"));
3558       return NULL;
3559     }
3560
3561   return (* iface->monitor_dir) (file, flags, cancellable, error);
3562 }
3563
3564 /**
3565  * g_file_monitor_file:
3566  * @file: input #GFile.
3567  * @flags: a set of #GFileMonitorFlags.
3568  * @cancellable: optional #GCancellable object, %NULL to ignore.
3569  * @error: a #GError, or %NULL.
3570  * 
3571  * Obtains a file monitor for the given file. If no file notification
3572  * mechanism exists, then regular polling of the file is used.
3573  *
3574  * If @cancellable is not %NULL, then the operation can be cancelled by
3575  * triggering the cancellable object from another thread. If the operation
3576  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3577  * 
3578  * Returns: a #GFileMonitor for the given @file.
3579  **/
3580 GFileMonitor*
3581 g_file_monitor_file (GFile             *file,
3582                      GFileMonitorFlags  flags,
3583                      GCancellable      *cancellable,
3584                      GError           **error)
3585 {
3586   GFileIface *iface;
3587   GFileMonitor *monitor;
3588   
3589   g_return_val_if_fail (G_IS_FILE (file), NULL);
3590
3591   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3592     return NULL;
3593
3594   iface = G_FILE_GET_IFACE (file);
3595
3596   monitor = NULL;
3597   
3598   if (iface->monitor_file)
3599     monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3600
3601 /* Fallback to polling */
3602   if (monitor == NULL)
3603     monitor = _g_poll_file_monitor_new (file);
3604
3605   return monitor;
3606 }
3607
3608 /********************************************
3609  *   Default implementation of async ops    *
3610  ********************************************/
3611
3612 typedef struct {
3613   char *attributes;
3614   GFileQueryInfoFlags flags;
3615   GFileInfo *info;
3616 } QueryInfoAsyncData;
3617
3618 static void
3619 query_info_data_free (QueryInfoAsyncData *data)
3620 {
3621   if (data->info)
3622     g_object_unref (data->info);
3623   g_free (data->attributes);
3624   g_free (data);
3625 }
3626
3627 static void
3628 query_info_async_thread (GSimpleAsyncResult *res,
3629                          GObject            *object,
3630                          GCancellable       *cancellable)
3631 {
3632   GError *error = NULL;
3633   QueryInfoAsyncData *data;
3634   GFileInfo *info;
3635   
3636   data = g_simple_async_result_get_op_res_gpointer (res);
3637   
3638   info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3639
3640   if (info == NULL)
3641     {
3642       g_simple_async_result_set_from_error (res, error);
3643       g_error_free (error);
3644     }
3645   else
3646     data->info = info;
3647 }
3648
3649 static void
3650 g_file_real_query_info_async (GFile               *file,
3651                               const char          *attributes,
3652                               GFileQueryInfoFlags  flags,
3653                               int                  io_priority,
3654                               GCancellable        *cancellable,
3655                               GAsyncReadyCallback  callback,
3656                               gpointer             user_data)
3657 {
3658   GSimpleAsyncResult *res;
3659   QueryInfoAsyncData *data;
3660
3661   data = g_new0 (QueryInfoAsyncData, 1);
3662   data->attributes = g_strdup (attributes);
3663   data->flags = flags;
3664   
3665   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3666   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3667   
3668   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3669   g_object_unref (res);
3670 }
3671
3672 static GFileInfo *
3673 g_file_real_query_info_finish (GFile         *file,
3674                                GAsyncResult  *res,
3675                                GError       **error)
3676 {
3677   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3678   QueryInfoAsyncData *data;
3679
3680   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3681
3682   data = g_simple_async_result_get_op_res_gpointer (simple);
3683   if (data->info)
3684     return g_object_ref (data->info);
3685   
3686   return NULL;
3687 }
3688
3689 typedef struct {
3690   char *attributes;
3691   GFileQueryInfoFlags flags;
3692   GFileEnumerator *enumerator;
3693 } EnumerateChildrenAsyncData;
3694
3695 static void
3696 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3697 {
3698   if (data->enumerator)
3699     g_object_unref (data->enumerator);
3700   g_free (data->attributes);
3701   g_free (data);
3702 }
3703
3704 static void
3705 enumerate_children_async_thread (GSimpleAsyncResult *res,
3706                                  GObject            *object,
3707                                  GCancellable       *cancellable)
3708 {
3709   GError *error = NULL;
3710   EnumerateChildrenAsyncData *data;
3711   GFileEnumerator *enumerator;
3712   
3713   data = g_simple_async_result_get_op_res_gpointer (res);
3714   
3715   enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3716
3717   if (enumerator == NULL)
3718     {
3719       g_simple_async_result_set_from_error (res, error);
3720       g_error_free (error);
3721     }
3722   else
3723     data->enumerator = enumerator;
3724 }
3725
3726 static void
3727 g_file_real_enumerate_children_async (GFile               *file,
3728                                       const char          *attributes,
3729                                       GFileQueryInfoFlags  flags,
3730                                       int                  io_priority,
3731                                       GCancellable        *cancellable,
3732                                       GAsyncReadyCallback  callback,
3733                                       gpointer             user_data)
3734 {
3735   GSimpleAsyncResult *res;
3736   EnumerateChildrenAsyncData *data;
3737
3738   data = g_new0 (EnumerateChildrenAsyncData, 1);
3739   data->attributes = g_strdup (attributes);
3740   data->flags = flags;
3741   
3742   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3743   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3744   
3745   g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3746   g_object_unref (res);
3747 }
3748
3749 static GFileEnumerator *
3750 g_file_real_enumerate_children_finish (GFile         *file,
3751                                        GAsyncResult  *res,
3752                                        GError       **error)
3753 {
3754   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3755   EnumerateChildrenAsyncData *data;
3756
3757   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3758
3759   data = g_simple_async_result_get_op_res_gpointer (simple);
3760   if (data->enumerator)
3761     return g_object_ref (data->enumerator);
3762   
3763   return NULL;
3764 }
3765
3766 static void
3767 open_read_async_thread (GSimpleAsyncResult *res,
3768                         GObject            *object,
3769                         GCancellable       *cancellable)
3770 {
3771   GFileIface *iface;
3772   GFileInputStream *stream;
3773   GError *error = NULL;
3774
3775   iface = G_FILE_GET_IFACE (object);
3776
3777   stream = iface->read_fn (G_FILE (object), cancellable, &error);
3778
3779   if (stream == NULL)
3780     {
3781       g_simple_async_result_set_from_error (res, error);
3782       g_error_free (error);
3783     }
3784   else
3785     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3786 }
3787
3788 static void
3789 g_file_real_read_async (GFile               *file,
3790                         int                  io_priority,
3791                         GCancellable        *cancellable,
3792                         GAsyncReadyCallback  callback,
3793                         gpointer             user_data)
3794 {
3795   GSimpleAsyncResult *res;
3796   
3797   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3798   
3799   g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3800   g_object_unref (res);
3801 }
3802
3803 static GFileInputStream *
3804 g_file_real_read_finish (GFile         *file,
3805                          GAsyncResult  *res,
3806                          GError       **error)
3807 {
3808   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3809   gpointer op;
3810
3811   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3812
3813   op = g_simple_async_result_get_op_res_gpointer (simple);
3814   if (op)
3815     return g_object_ref (op);
3816   
3817   return NULL;
3818 }
3819
3820 static void
3821 append_to_async_thread (GSimpleAsyncResult *res,
3822                         GObject            *object,
3823                         GCancellable       *cancellable)
3824 {
3825   GFileIface *iface;
3826   GFileCreateFlags *data;
3827   GFileOutputStream *stream;
3828   GError *error = NULL;
3829
3830   iface = G_FILE_GET_IFACE (object);
3831
3832   data = g_simple_async_result_get_op_res_gpointer (res);
3833
3834   stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3835
3836   if (stream == NULL)
3837     {
3838       g_simple_async_result_set_from_error (res, error);
3839       g_error_free (error);
3840     }
3841   else
3842     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3843 }
3844
3845 static void
3846 g_file_real_append_to_async (GFile               *file,
3847                              GFileCreateFlags     flags,
3848                              int                  io_priority,
3849                              GCancellable        *cancellable,
3850                              GAsyncReadyCallback  callback,
3851                              gpointer             user_data)
3852 {
3853   GFileCreateFlags *data;
3854   GSimpleAsyncResult *res;
3855
3856   data = g_new0 (GFileCreateFlags, 1);
3857   *data = flags;
3858
3859   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3860   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3861
3862   g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3863   g_object_unref (res);
3864 }
3865
3866 static GFileOutputStream *
3867 g_file_real_append_to_finish (GFile         *file,
3868                               GAsyncResult  *res,
3869                               GError       **error)
3870 {
3871   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3872   gpointer op;
3873
3874   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3875
3876   op = g_simple_async_result_get_op_res_gpointer (simple);
3877   if (op)
3878     return g_object_ref (op);
3879   
3880   return NULL;
3881 }
3882
3883 static void
3884 create_async_thread (GSimpleAsyncResult *res,
3885                      GObject            *object,
3886                      GCancellable       *cancellable)
3887 {
3888   GFileIface *iface;
3889   GFileCreateFlags *data;
3890   GFileOutputStream *stream;
3891   GError *error = NULL;
3892
3893   iface = G_FILE_GET_IFACE (object);
3894
3895   data = g_simple_async_result_get_op_res_gpointer (res);
3896
3897   stream = iface->create (G_FILE (object), *data, cancellable, &error);
3898
3899   if (stream == NULL)
3900     {
3901       g_simple_async_result_set_from_error (res, error);
3902       g_error_free (error);
3903     }
3904   else
3905     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3906 }
3907
3908 static void
3909 g_file_real_create_async (GFile               *file,
3910                           GFileCreateFlags     flags,
3911                           int                  io_priority,
3912                           GCancellable        *cancellable,
3913                           GAsyncReadyCallback  callback,
3914                           gpointer             user_data)
3915 {
3916   GFileCreateFlags *data;
3917   GSimpleAsyncResult *res;
3918
3919   data = g_new0 (GFileCreateFlags, 1);
3920   *data = flags;
3921
3922   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3923   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3924
3925   g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3926   g_object_unref (res);
3927 }
3928
3929 static GFileOutputStream *
3930 g_file_real_create_finish (GFile         *file,
3931                            GAsyncResult  *res,
3932                            GError       **error)
3933 {
3934   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3935   gpointer op;
3936
3937   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3938
3939   op = g_simple_async_result_get_op_res_gpointer (simple);
3940   if (op)
3941     return g_object_ref (op);
3942   
3943   return NULL;
3944 }
3945
3946 typedef struct {
3947   GFileOutputStream *stream;
3948   char *etag;
3949   gboolean make_backup;
3950   GFileCreateFlags flags;
3951 } ReplaceAsyncData;
3952
3953 static void
3954 replace_async_data_free (ReplaceAsyncData *data)
3955 {
3956   if (data->stream)
3957     g_object_unref (data->stream);
3958   g_free (data->etag);
3959   g_free (data);
3960 }
3961
3962 static void
3963 replace_async_thread (GSimpleAsyncResult *res,
3964                       GObject            *object,
3965                       GCancellable       *cancellable)
3966 {
3967   GFileIface *iface;
3968   GFileOutputStream *stream;
3969   GError *error = NULL;
3970   ReplaceAsyncData *data;
3971
3972   iface = G_FILE_GET_IFACE (object);
3973   
3974   data = g_simple_async_result_get_op_res_gpointer (res);
3975
3976   stream = iface->replace (G_FILE (object),
3977                            data->etag,
3978                            data->make_backup,
3979                            data->flags,
3980                            cancellable,
3981                            &error);
3982
3983   if (stream == NULL)
3984     {
3985       g_simple_async_result_set_from_error (res, error);
3986       g_error_free (error);
3987     }
3988   else
3989     data->stream = stream;
3990 }
3991
3992 static void
3993 g_file_real_replace_async (GFile               *file,
3994                            const char          *etag,
3995                            gboolean             make_backup,
3996                            GFileCreateFlags     flags,
3997                            int                  io_priority,
3998                            GCancellable        *cancellable,
3999                            GAsyncReadyCallback  callback,
4000                            gpointer             user_data)
4001 {
4002   GSimpleAsyncResult *res;
4003   ReplaceAsyncData *data;
4004
4005   data = g_new0 (ReplaceAsyncData, 1);
4006   data->etag = g_strdup (etag);
4007   data->make_backup = make_backup;
4008   data->flags = flags;
4009
4010   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4011   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4012
4013   g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4014   g_object_unref (res);
4015 }
4016
4017 static GFileOutputStream *
4018 g_file_real_replace_finish (GFile         *file,
4019                             GAsyncResult  *res,
4020                             GError       **error)
4021 {
4022   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4023   ReplaceAsyncData *data;
4024
4025   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4026
4027   data = g_simple_async_result_get_op_res_gpointer (simple);
4028   if (data->stream)
4029     return g_object_ref (data->stream);
4030   
4031   return NULL;
4032 }
4033
4034 typedef struct {
4035   char *name;
4036   GFile *file;
4037 } SetDisplayNameAsyncData;
4038
4039 static void
4040 set_display_name_data_free (SetDisplayNameAsyncData *data)
4041 {
4042   g_free (data->name);
4043   if (data->file)
4044     g_object_unref (data->file);
4045   g_free (data);
4046 }
4047
4048 static void
4049 set_display_name_async_thread (GSimpleAsyncResult *res,
4050                                GObject            *object,
4051                                GCancellable       *cancellable)
4052 {
4053   GError *error = NULL;
4054   SetDisplayNameAsyncData *data;
4055   GFile *file;
4056   
4057   data = g_simple_async_result_get_op_res_gpointer (res);
4058   
4059   file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4060
4061   if (file == NULL)
4062     {
4063       g_simple_async_result_set_from_error (res, error);
4064       g_error_free (error);
4065     }
4066   else
4067     data->file = file;
4068 }
4069
4070 static void
4071 g_file_real_set_display_name_async (GFile               *file,  
4072                                     const char          *display_name,
4073                                     int                  io_priority,
4074                                     GCancellable        *cancellable,
4075                                     GAsyncReadyCallback  callback,
4076                                     gpointer             user_data)
4077 {
4078   GSimpleAsyncResult *res;
4079   SetDisplayNameAsyncData *data;
4080
4081   data = g_new0 (SetDisplayNameAsyncData, 1);
4082   data->name = g_strdup (display_name);
4083   
4084   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4085   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4086   
4087   g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4088   g_object_unref (res);
4089 }
4090
4091 static GFile *
4092 g_file_real_set_display_name_finish (GFile         *file,
4093                                      GAsyncResult  *res,
4094                                      GError       **error)
4095 {
4096   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4097   SetDisplayNameAsyncData *data;
4098
4099   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4100
4101   data = g_simple_async_result_get_op_res_gpointer (simple);
4102   if (data->file)
4103     return g_object_ref (data->file);
4104   
4105   return NULL;
4106 }
4107
4108 typedef struct {
4109   GFileQueryInfoFlags flags;
4110   GFileInfo *info;
4111   gboolean res;
4112   GError *error;
4113 } SetInfoAsyncData;
4114
4115 static void
4116 set_info_data_free (SetInfoAsyncData *data)
4117 {
4118   if (data->info)
4119     g_object_unref (data->info);
4120   if (data->error)
4121     g_error_free (data->error);
4122   g_free (data);
4123 }
4124
4125 static void
4126 set_info_async_thread (GSimpleAsyncResult *res,
4127                        GObject            *object,
4128                        GCancellable       *cancellable)
4129 {
4130   SetInfoAsyncData *data;
4131   
4132   data = g_simple_async_result_get_op_res_gpointer (res);
4133   
4134   data->error = NULL;
4135   data->res = g_file_set_attributes_from_info (G_FILE (object),
4136                                                data->info,
4137                                                data->flags,
4138                                                cancellable,
4139                                                &data->error);
4140 }
4141
4142 static void
4143 g_file_real_set_attributes_async (GFile               *file,
4144                                   GFileInfo           *info,
4145                                   GFileQueryInfoFlags  flags,
4146                                   int                  io_priority,
4147                                   GCancellable        *cancellable,
4148                                   GAsyncReadyCallback  callback,
4149                                   gpointer             user_data)
4150 {
4151   GSimpleAsyncResult *res;
4152   SetInfoAsyncData *data;
4153
4154   data = g_new0 (SetInfoAsyncData, 1);
4155   data->info = g_file_info_dup (info);
4156   data->flags = flags;
4157   
4158   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4159   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4160   
4161   g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4162   g_object_unref (res);
4163 }
4164
4165 static gboolean
4166 g_file_real_set_attributes_finish (GFile         *file,
4167                                    GAsyncResult  *res,
4168                                    GFileInfo    **info,
4169                                    GError       **error)
4170 {
4171   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4172   SetInfoAsyncData *data;
4173   
4174   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4175
4176   data = g_simple_async_result_get_op_res_gpointer (simple);
4177
4178   if (info) 
4179     *info = g_object_ref (data->info);
4180
4181   if (error != NULL && data->error) 
4182     *error = g_error_copy (data->error);
4183   
4184   return data->res;
4185 }
4186
4187 static void
4188 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4189                                     GObject            *object,
4190                                     GCancellable       *cancellable)
4191 {
4192   GError *error = NULL;
4193   GMount *mount;
4194   
4195   mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4196
4197   if (mount == NULL)
4198     {
4199       g_simple_async_result_set_from_error (res, error);
4200       g_error_free (error);
4201     }
4202   else
4203     g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4204 }
4205
4206 static void
4207 g_file_real_find_enclosing_mount_async (GFile               *file,
4208                                         int                  io_priority,
4209                                         GCancellable        *cancellable,
4210                                         GAsyncReadyCallback  callback,
4211                                         gpointer             user_data)
4212 {
4213   GSimpleAsyncResult *res;
4214   
4215   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4216   
4217   g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4218   g_object_unref (res);
4219 }
4220
4221 static GMount *
4222 g_file_real_find_enclosing_mount_finish (GFile         *file,
4223                                           GAsyncResult  *res,
4224                                           GError       **error)
4225 {
4226   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4227   GMount *mount;
4228
4229   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4230
4231   mount = g_simple_async_result_get_op_res_gpointer (simple);
4232   return g_object_ref (mount);
4233 }
4234
4235
4236 typedef struct {
4237   GFile *source;
4238   GFile *destination;
4239   GFileCopyFlags flags;
4240   GFileProgressCallback progress_cb;
4241   gpointer progress_cb_data;
4242   GIOSchedulerJob *job;
4243 } CopyAsyncData;
4244
4245 static void
4246 copy_async_data_free (CopyAsyncData *data)
4247 {
4248   g_object_unref (data->source);
4249   g_object_unref (data->destination);
4250   g_free (data);
4251 }
4252
4253 typedef struct {
4254   CopyAsyncData *data;
4255   goffset current_num_bytes;
4256   goffset total_num_bytes;
4257 } ProgressData;
4258
4259 static gboolean
4260 copy_async_progress_in_main (gpointer user_data)
4261 {
4262   ProgressData *progress = user_data;
4263   CopyAsyncData *data = progress->data;
4264
4265   data->progress_cb (progress->current_num_bytes,
4266                      progress->total_num_bytes,
4267                      data->progress_cb_data);
4268
4269   return FALSE;
4270 }
4271
4272 static gboolean
4273 mainloop_barrier (gpointer user_data)
4274 {
4275   /* Does nothing, but ensures all queued idles before
4276      this are run */
4277   return FALSE;
4278 }
4279
4280
4281 static void
4282 copy_async_progress_callback (goffset  current_num_bytes,
4283                               goffset  total_num_bytes,
4284                               gpointer user_data)
4285 {
4286   CopyAsyncData *data = user_data;
4287   ProgressData *progress;
4288
4289   progress = g_new (ProgressData, 1);
4290   progress->data = data;
4291   progress->current_num_bytes = current_num_bytes;
4292   progress->total_num_bytes = total_num_bytes;
4293   
4294   g_io_scheduler_job_send_to_mainloop_async (data->job,
4295                                              copy_async_progress_in_main,
4296                                              progress,
4297                                              g_free);
4298 }
4299
4300 static gboolean
4301 copy_async_thread (GIOSchedulerJob *job,
4302                    GCancellable    *cancellable,
4303                    gpointer         user_data)
4304 {
4305   GSimpleAsyncResult *res;
4306   CopyAsyncData *data;
4307   gboolean result;
4308   GError *error;
4309
4310   res = user_data;
4311   data = g_simple_async_result_get_op_res_gpointer (res);
4312
4313   error = NULL;
4314   data->job = job;
4315   result = g_file_copy (data->source,
4316                         data->destination,
4317                         data->flags,
4318                         cancellable,
4319                         (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4320                         data,
4321                         &error);
4322
4323   /* Ensure all progress callbacks are done running in main thread */
4324   if (data->progress_cb != NULL)
4325     g_io_scheduler_job_send_to_mainloop (job,
4326                                          mainloop_barrier,
4327                                          NULL, NULL);
4328   
4329   if (!result)
4330     {
4331       g_simple_async_result_set_from_error (res, error);
4332       g_error_free (error);
4333     }
4334
4335   g_simple_async_result_complete_in_idle (res);
4336
4337   return FALSE;
4338 }
4339
4340 static void
4341 g_file_real_copy_async (GFile                  *source,
4342                         GFile                  *destination,
4343                         GFileCopyFlags          flags,
4344                         int                     io_priority,
4345                         GCancellable           *cancellable,
4346                         GFileProgressCallback   progress_callback,
4347                         gpointer                progress_callback_data,
4348                         GAsyncReadyCallback     callback,
4349                         gpointer                user_data)
4350 {
4351   GSimpleAsyncResult *res;
4352   CopyAsyncData *data;
4353
4354   data = g_new0 (CopyAsyncData, 1);
4355   data->source = g_object_ref (source);
4356   data->destination = g_object_ref (destination);
4357   data->flags = flags;
4358   data->progress_cb = progress_callback;
4359   data->progress_cb_data = progress_callback_data;
4360
4361   res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4362   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4363
4364   g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4365 }
4366
4367 static gboolean
4368 g_file_real_copy_finish (GFile        *file,
4369                          GAsyncResult *res,
4370                          GError      **error)
4371 {
4372   /* Error handled in g_file_copy_finish() */
4373   return TRUE;
4374 }
4375
4376
4377 /********************************************
4378  *   Default VFS operations                 *
4379  ********************************************/
4380
4381 /**
4382  * g_file_new_for_path:
4383  * @path: a string containing a relative or absolute path.
4384  * 
4385  * Constructs a #GFile for a given path. This operation never
4386  * fails, but the returned object might not support any I/O
4387  * operation if @path is malformed.
4388  * 
4389  * Returns: a new #GFile for the given @path. 
4390  **/
4391 GFile *
4392 g_file_new_for_path (const char *path)
4393 {
4394   g_return_val_if_fail (path != NULL, NULL);
4395
4396   return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4397 }
4398  
4399 /**
4400  * g_file_new_for_uri:
4401  * @uri: a string containing a URI.
4402  * 
4403  * Constructs a #GFile for a given URI. This operation never 
4404  * fails, but the returned object might not support any I/O 
4405  * operation if @uri is malformed or if the uri type is 
4406  * not supported.
4407  * 
4408  * Returns: a #GFile for the given @uri.
4409  **/ 
4410 GFile *
4411 g_file_new_for_uri (const char *uri)
4412 {
4413   g_return_val_if_fail (uri != NULL, NULL);
4414
4415   return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4416 }
4417   
4418 /**
4419  * g_file_parse_name:
4420  * @parse_name: a file name or path to be parsed.
4421  * 
4422  * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4423  * This operation never fails, but the returned object might not support any I/O
4424  * operation if the @parse_name cannot be parsed.
4425  * 
4426  * Returns: a new #GFile.
4427  **/
4428 GFile *
4429 g_file_parse_name (const char *parse_name)
4430 {
4431   g_return_val_if_fail (parse_name != NULL, NULL);
4432
4433   return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4434 }
4435
4436 static gboolean
4437 is_valid_scheme_character (char c)
4438 {
4439   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4440 }
4441
4442 static gboolean
4443 has_valid_scheme (const char *uri)
4444 {
4445   const char *p;
4446   
4447   p = uri;
4448   
4449   if (!is_valid_scheme_character (*p))
4450     return FALSE;
4451
4452   do {
4453     p++;
4454   } while (is_valid_scheme_character (*p));
4455
4456   return *p == ':';
4457 }
4458
4459 /**
4460  * g_file_new_for_commandline_arg:
4461  * @arg: a command line string.
4462  * 
4463  * Creates a #GFile with the given argument from the command line. The value of
4464  * @arg can be either a URI, an absolute path or a relative path resolved
4465  * relative to the current working directory.
4466  * This operation never fails, but the returned object might not support any
4467  * I/O operation if @arg points to a malformed path.
4468  *
4469  * Returns: a new #GFile. 
4470  **/
4471 GFile *
4472 g_file_new_for_commandline_arg (const char *arg)
4473 {
4474   GFile *file;
4475   char *filename;
4476   char *current_dir;
4477   
4478   g_return_val_if_fail (arg != NULL, NULL);
4479   
4480   if (g_path_is_absolute (arg))
4481     return g_file_new_for_path (arg);
4482
4483   if (has_valid_scheme (arg))
4484     return g_file_new_for_uri (arg);
4485     
4486   current_dir = g_get_current_dir ();
4487   filename = g_build_filename (current_dir, arg, NULL);
4488   g_free (current_dir);
4489   
4490   file = g_file_new_for_path (filename);
4491   g_free (filename);
4492   
4493   return file;
4494 }
4495
4496 /**
4497  * g_file_mount_enclosing_volume:
4498  * @location: input #GFile.
4499  * @flags: flags affecting the operation
4500  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4501  * @cancellable: optional #GCancellable object, %NULL to ignore.
4502  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4503  * @user_data: the data to pass to callback function
4504  * 
4505  * Starts a @mount_operation, mounting the volume that contains the file @location. 
4506  * 
4507  * When this operation has completed, @callback will be called with
4508  * @user_user data, and the operation can be finalized with 
4509  * g_file_mount_enclosing_volume_finish().
4510  * 
4511  * If @cancellable is not %NULL, then the operation can be cancelled by
4512  * triggering the cancellable object from another thread. If the operation
4513  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4514  **/
4515 void
4516 g_file_mount_enclosing_volume (GFile               *location,
4517                                GMountMountFlags     flags,
4518                                GMountOperation     *mount_operation,
4519                                GCancellable        *cancellable,
4520                                GAsyncReadyCallback  callback,
4521                                gpointer             user_data)
4522 {
4523   GFileIface *iface;
4524
4525   g_return_if_fail (G_IS_FILE (location));
4526
4527   iface = G_FILE_GET_IFACE (location);
4528
4529   if (iface->mount_enclosing_volume == NULL)
4530     {
4531       g_simple_async_report_error_in_idle (G_OBJECT (location),
4532                                            callback, user_data,
4533                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4534                                            _("volume doesn't implement mount"));
4535       
4536       return;
4537     }
4538   
4539   (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4540
4541 }
4542
4543 /**
4544  * g_file_mount_enclosing_volume_finish:
4545  * @location: input #GFile.
4546  * @result: a #GAsyncResult.
4547  * @error: a #GError, or %NULL
4548  * 
4549  * Finishes a mount operation started by g_file_mount_enclosing_volume().
4550  * 
4551  * Returns: %TRUE if successful. If an error
4552  * has occurred, this function will return %FALSE and set @error
4553  * appropriately if present.
4554  **/
4555 gboolean
4556 g_file_mount_enclosing_volume_finish (GFile         *location,
4557                                       GAsyncResult  *result,
4558                                       GError       **error)
4559 {
4560   GFileIface *iface;
4561
4562   g_return_val_if_fail (G_IS_FILE (location), FALSE);
4563   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4564
4565   if (G_IS_SIMPLE_ASYNC_RESULT (result))
4566     {
4567       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4568       if (g_simple_async_result_propagate_error (simple, error))
4569         return FALSE;
4570     }
4571   
4572   iface = G_FILE_GET_IFACE (location);
4573
4574   return (* iface->mount_enclosing_volume_finish) (location, result, error);
4575 }
4576
4577 /********************************************
4578  *   Utility functions                      *
4579  ********************************************/
4580
4581 /**
4582  * g_file_query_default_handler:
4583  * @file: a #GFile to open.
4584  * @cancellable: optional #GCancellable object, %NULL to ignore.
4585  * @error: a #GError, or %NULL
4586  *
4587  * Returns the #GAppInfo that is registered as the default
4588  * application to handle the file specified by @file.
4589  *
4590  * If @cancellable is not %NULL, then the operation can be cancelled by
4591  * triggering the cancellable object from another thread. If the operation
4592  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4593  *
4594  * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4595  * When you are done with it, release it with g_object_unref()
4596  **/
4597 GAppInfo *
4598 g_file_query_default_handler (GFile                  *file,
4599                               GCancellable           *cancellable,
4600                               GError                **error)
4601 {
4602   char *uri_scheme;
4603   const char *content_type;
4604   GAppInfo *appinfo;
4605   GFileInfo *info;
4606   char *path;
4607   
4608   uri_scheme = g_file_get_uri_scheme (file);
4609   appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4610   g_free (uri_scheme);
4611
4612   if (appinfo != NULL)
4613     return appinfo;
4614
4615   info = g_file_query_info (file,
4616                             G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4617                             0,
4618                             cancellable,
4619                             error);
4620   if (info == NULL)
4621     return NULL;
4622
4623   appinfo = NULL;
4624
4625   content_type = g_file_info_get_content_type (info);
4626   if (content_type)
4627     {
4628       /* Don't use is_native(), as we want to support fuse paths if availible */
4629       path = g_file_get_path (file);
4630       appinfo = g_app_info_get_default_for_type (content_type,
4631                                                  path == NULL);
4632       g_free (path);
4633     }
4634   
4635   g_object_unref (info);
4636
4637   if (appinfo != NULL)
4638     return appinfo;
4639
4640   g_set_error (error, G_IO_ERROR,
4641                G_IO_ERROR_NOT_SUPPORTED,
4642                _("No application is registered as handling this file"));
4643   return NULL;
4644   
4645 }
4646
4647
4648 #define GET_CONTENT_BLOCK_SIZE 8192
4649
4650 /**
4651  * g_file_load_contents:
4652  * @file: input #GFile.
4653  * @cancellable: optional #GCancellable object, %NULL to ignore.
4654  * @contents: a location to place the contents of the file.
4655  * @length: a location to place the length of the contents of the file.
4656  * @etag_out: a location to place the current entity tag for the file.
4657  * @error: a #GError, or %NULL
4658  *
4659  * Loads the content of the file into memory, returning the size of
4660  * the data. The data is always zero terminated, but this is not
4661  * included in the resultant @length.
4662  * 
4663  * If @cancellable is not %NULL, then the operation can be cancelled by
4664  * triggering the cancellable object from another thread. If the operation
4665  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4666  * 
4667  * Returns: %TRUE if the @file's contents were successfully loaded.
4668  * %FALSE if there were errors..
4669  **/
4670 gboolean
4671 g_file_load_contents (GFile         *file,
4672                       GCancellable  *cancellable,
4673                       char         **contents,
4674                       gsize         *length,
4675                       char         **etag_out,
4676                       GError       **error)
4677 {
4678   GFileInputStream *in;
4679   GByteArray *content;
4680   gsize pos;
4681   gssize res;
4682   GFileInfo *info;
4683
4684   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4685   g_return_val_if_fail (contents != NULL, FALSE);
4686
4687   in = g_file_read (file, cancellable, error);
4688   if (in == NULL)
4689     return FALSE;
4690
4691   content = g_byte_array_new ();
4692   pos = 0;
4693   
4694   g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4695   while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4696                                      content->data + pos,
4697                                      GET_CONTENT_BLOCK_SIZE,
4698                                      cancellable, error)) > 0)
4699     {
4700       pos += res;
4701       g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4702     }
4703
4704   if (etag_out)
4705     {
4706       *etag_out = NULL;
4707       
4708       info = g_file_input_stream_query_info (in,
4709                                              G_FILE_ATTRIBUTE_ETAG_VALUE,
4710                                              cancellable,
4711                                              NULL);
4712       if (info)
4713         {
4714           *etag_out = g_strdup (g_file_info_get_etag (info));
4715           g_object_unref (info);
4716         }
4717     } 
4718
4719   /* Ignore errors on close */
4720   g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4721   g_object_unref (in);
4722
4723   if (res < 0)
4724     {
4725       /* error is set already */
4726       g_byte_array_free (content, TRUE);
4727       return FALSE;
4728     }
4729
4730   if (length)
4731     *length = pos;
4732
4733   /* Zero terminate (we got an extra byte allocated for this */
4734   content->data[pos] = 0;
4735   
4736   *contents = (char *)g_byte_array_free (content, FALSE);
4737   
4738   return TRUE;
4739 }
4740
4741 typedef struct {
4742   GFile *file;
4743   GError *error;
4744   GCancellable *cancellable;
4745   GFileReadMoreCallback read_more_callback;
4746   GAsyncReadyCallback callback;
4747   gpointer user_data;
4748   GByteArray *content;
4749   gsize pos;
4750   char *etag;
4751 } LoadContentsData;
4752
4753
4754 static void
4755 load_contents_data_free (LoadContentsData *data)
4756 {
4757   if (data->error)
4758     g_error_free (data->error);
4759   if (data->cancellable)
4760     g_object_unref (data->cancellable);
4761   if (data->content)
4762     g_byte_array_free (data->content, TRUE);
4763   g_free (data->etag);
4764   g_object_unref (data->file);
4765   g_free (data);
4766 }
4767
4768 static void
4769 load_contents_close_callback (GObject      *obj,
4770                               GAsyncResult *close_res,
4771                               gpointer      user_data)
4772 {
4773   GInputStream *stream = G_INPUT_STREAM (obj);
4774   LoadContentsData *data = user_data;
4775   GSimpleAsyncResult *res;
4776
4777   /* Ignore errors here, we're only reading anyway */
4778   g_input_stream_close_finish (stream, close_res, NULL);
4779   g_object_unref (stream);
4780
4781   res = g_simple_async_result_new (G_OBJECT (data->file),
4782                                    data->callback,
4783                                    data->user_data,
4784                                    g_file_load_contents_async);
4785   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4786   g_simple_async_result_complete (res);
4787   g_object_unref (res);
4788 }
4789
4790 static void
4791 load_contents_fstat_callback (GObject      *obj,
4792                               GAsyncResult *stat_res,
4793                               gpointer      user_data)
4794 {
4795   GInputStream *stream = G_INPUT_STREAM (obj);
4796   LoadContentsData *data = user_data;
4797   GFileInfo *info;
4798
4799   info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4800                                                    stat_res, NULL);
4801   if (info)
4802     {
4803       data->etag = g_strdup (g_file_info_get_etag (info));
4804       g_object_unref (info);
4805     }
4806
4807   g_input_stream_close_async (stream, 0,
4808                               data->cancellable,
4809                               load_contents_close_callback, data);
4810 }
4811
4812 static void
4813 load_contents_read_callback (GObject      *obj,
4814                              GAsyncResult *read_res,
4815                              gpointer      user_data)
4816 {
4817   GInputStream *stream = G_INPUT_STREAM (obj);
4818   LoadContentsData *data = user_data;
4819   GError *error = NULL;
4820   gssize read_size;
4821
4822   read_size = g_input_stream_read_finish (stream, read_res, &error);
4823
4824   if (read_size < 0) 
4825     {
4826       /* Error or EOF, close the file */
4827       data->error = error;
4828       g_input_stream_close_async (stream, 0,
4829                                   data->cancellable,
4830                                   load_contents_close_callback, data);
4831     }
4832   else if (read_size == 0)
4833     {
4834       g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4835                                             G_FILE_ATTRIBUTE_ETAG_VALUE,
4836                                             0,
4837                                             data->cancellable,
4838                                             load_contents_fstat_callback,
4839                                             data);
4840     }
4841   else if (read_size > 0)
4842     {
4843       data->pos += read_size;
4844       
4845       g_byte_array_set_size (data->content,
4846                              data->pos + GET_CONTENT_BLOCK_SIZE);
4847
4848
4849       if (data->read_more_callback &&
4850           !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4851         g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4852                                               G_FILE_ATTRIBUTE_ETAG_VALUE,
4853                                               0,
4854                                               data->cancellable,
4855                                               load_contents_fstat_callback,
4856                                               data);
4857       else 
4858         g_input_stream_read_async (stream,
4859                                    data->content->data + data->pos,
4860                                    GET_CONTENT_BLOCK_SIZE,
4861                                    0,
4862                                    data->cancellable,
4863                                    load_contents_read_callback,
4864                                    data);
4865     }
4866 }
4867
4868 static void
4869 load_contents_open_callback (GObject      *obj,
4870                              GAsyncResult *open_res,
4871                              gpointer      user_data)
4872 {
4873   GFile *file = G_FILE (obj);
4874   GFileInputStream *stream;
4875   LoadContentsData *data = user_data;
4876   GError *error = NULL;
4877   GSimpleAsyncResult *res;
4878
4879   stream = g_file_read_finish (file, open_res, &error);
4880
4881   if (stream)
4882     {
4883       g_byte_array_set_size (data->content,
4884                              data->pos + GET_CONTENT_BLOCK_SIZE);
4885       g_input_stream_read_async (G_INPUT_STREAM (stream),
4886                                  data->content->data + data->pos,
4887                                  GET_CONTENT_BLOCK_SIZE,
4888                                  0,
4889                                  data->cancellable,
4890                                  load_contents_read_callback,
4891                                  data);
4892       
4893     }
4894   else
4895     {
4896       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4897                                                   data->callback,
4898                                                   data->user_data,
4899                                                   error);
4900       g_simple_async_result_complete (res);
4901       g_error_free (error);
4902       load_contents_data_free (data);
4903       g_object_unref (res);
4904     }
4905 }
4906
4907 /**
4908  * g_file_load_partial_contents_async:
4909  * @file: input #GFile.
4910  * @cancellable: optional #GCancellable object, %NULL to ignore.
4911  * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
4912  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4913  * @user_data: the data to pass to the callback functions.
4914  *
4915  * Reads the partial contents of a file. A #GFileReadMoreCallback should be 
4916  * used to stop reading from the file when appropriate, else this function
4917  * will behave exactly as g_file_load_contents_async(). This operation 
4918  * can be finished by g_file_load_partial_contents_finish().
4919  *
4920  * Users of this function should be aware that @user_data is passed to 
4921  * both the @read_more_callback and the @callback.
4922  *
4923  * If @cancellable is not %NULL, then the operation can be cancelled by
4924  * triggering the cancellable object from another thread. If the operation
4925  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4926  **/
4927 void
4928 g_file_load_partial_contents_async (GFile                 *file,
4929                                     GCancellable          *cancellable,
4930                                     GFileReadMoreCallback  read_more_callback,
4931                                     GAsyncReadyCallback    callback,
4932                                     gpointer               user_data)
4933 {
4934   LoadContentsData *data;
4935
4936   g_return_if_fail (G_IS_FILE (file));
4937
4938   data = g_new0 (LoadContentsData, 1);
4939
4940   if (cancellable)
4941     data->cancellable = g_object_ref (cancellable);
4942   data->read_more_callback = read_more_callback;
4943   data->callback = callback;
4944   data->user_data = user_data;
4945   data->content = g_byte_array_new ();
4946   data->file = g_object_ref (file);
4947
4948   g_file_read_async (file,
4949                      0,
4950                      cancellable,
4951                      load_contents_open_callback,
4952                      data);
4953 }
4954
4955 /**
4956  * g_file_load_partial_contents_finish:
4957  * @file: input #GFile.
4958  * @res: a #GAsyncResult. 
4959  * @contents: a location to place the contents of the file.
4960  * @length: a location to place the length of the contents of the file.
4961  * @etag_out: a location to place the current entity tag for the file.
4962  * @error: a #GError, or %NULL
4963  * 
4964  * Finishes an asynchronous partial load operation that was started
4965  * with g_file_load_partial_contents_async().
4966  *
4967  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
4968  * present, it will be set appropriately. 
4969  **/
4970 gboolean
4971 g_file_load_partial_contents_finish (GFile         *file,
4972                                      GAsyncResult  *res,
4973                                      char         **contents,
4974                                      gsize         *length,
4975                                      char         **etag_out,
4976                                      GError       **error)
4977 {
4978   GSimpleAsyncResult *simple;
4979   LoadContentsData *data;
4980
4981   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4982   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4983   g_return_val_if_fail (contents != NULL, FALSE);
4984
4985   simple = G_SIMPLE_ASYNC_RESULT (res);
4986
4987   if (g_simple_async_result_propagate_error (simple, error))
4988     return FALSE;
4989   
4990   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4991   
4992   data = g_simple_async_result_get_op_res_gpointer (simple);
4993
4994   if (data->error)
4995     {
4996       g_propagate_error (error, data->error);
4997       data->error = NULL;
4998       *contents = NULL;
4999       if (length)
5000         *length = 0;
5001       return FALSE;
5002     }
5003
5004   if (length)
5005     *length = data->pos;
5006
5007   if (etag_out)
5008     {
5009       *etag_out = data->etag;
5010       data->etag = NULL;
5011     }
5012
5013   /* Zero terminate */
5014   g_byte_array_set_size (data->content, data->pos + 1);
5015   data->content->data[data->pos] = 0;
5016   
5017   *contents = (char *)g_byte_array_free (data->content, FALSE);
5018   data->content = NULL;
5019
5020   return TRUE;
5021 }
5022
5023 /**
5024  * g_file_load_contents_async:
5025  * @file: input #GFile.
5026  * @cancellable: optional #GCancellable object, %NULL to ignore.
5027  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5028  * @user_data: the data to pass to callback function
5029  * 
5030  * Starts an asynchronous load of the @file's contents.
5031  *
5032  * For more details, see g_file_load_contents() which is
5033  * the synchronous version of this call.
5034  *
5035  * When the load operation has completed, @callback will be called 
5036  * with @user data. To finish the operation, call 
5037  * g_file_load_contents_finish() with the #GAsyncResult returned by 
5038  * the @callback.
5039  * 
5040  * If @cancellable is not %NULL, then the operation can be cancelled by
5041  * triggering the cancellable object from another thread. If the operation
5042  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5043  **/
5044 void
5045 g_file_load_contents_async (GFile               *file,
5046                            GCancellable        *cancellable,
5047                            GAsyncReadyCallback  callback,
5048                            gpointer             user_data)
5049 {
5050   g_file_load_partial_contents_async (file,
5051                                       cancellable,
5052                                       NULL,
5053                                       callback, user_data);
5054 }
5055
5056 /**
5057  * g_file_load_contents_finish:
5058  * @file: input #GFile.
5059  * @res: a #GAsyncResult. 
5060  * @contents: a location to place the contents of the file.
5061  * @length: a location to place the length of the contents of the file.
5062  * @etag_out: a location to place the current entity tag for the file.
5063  * @error: a #GError, or %NULL
5064  * 
5065  * Finishes an asynchronous load of the @file's contents. 
5066  * The contents are placed in @contents, and @length is set to the 
5067  * size of the @contents string. If @etag_out is present, it will be 
5068  * set to the new entity tag for the @file.
5069  * 
5070  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
5071  * present, it will be set appropriately. 
5072  **/
5073 gboolean
5074 g_file_load_contents_finish (GFile         *file,
5075                              GAsyncResult  *res,
5076                              char         **contents,
5077                              gsize         *length,
5078                              char         **etag_out,
5079                              GError       **error)
5080 {
5081   return g_file_load_partial_contents_finish (file,
5082                                               res,
5083                                               contents,
5084                                               length,
5085                                               etag_out,
5086                                               error);
5087 }
5088   
5089 /**
5090  * g_file_replace_contents:
5091  * @file: input #GFile.
5092  * @contents: a string containing the new contents for @file.
5093  * @length: the length of @contents in bytes.
5094  * @etag: the old <link linkend="gfile-etag">entity tag</link> 
5095  *     for the document.
5096  * @make_backup: %TRUE if a backup should be created.
5097  * @flags: a set of #GFileCreateFlags.
5098  * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5099  *      for the document. This should be freed with g_free() when no longer 
5100  *      needed.
5101  * @cancellable: optional #GCancellable object, %NULL to ignore.
5102  * @error: a #GError, or %NULL
5103  *
5104  * Replaces the contents of @file with @contents of @length bytes.
5105  
5106  * If @etag is specified (not %NULL) any existing file must have that etag, or
5107  * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5108  *
5109  * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5110  * 
5111  * If @cancellable is not %NULL, then the operation can be cancelled by
5112  * triggering the cancellable object from another thread. If the operation
5113  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5114  *
5115  * The returned @new_etag can be used to verify that the file hasn't changed the
5116  * next time it is saved over.
5117  * 
5118  * Returns: %TRUE if successful. If an error
5119  * has occurred, this function will return %FALSE and set @error
5120  * appropriately if present.
5121  **/
5122 gboolean
5123 g_file_replace_contents (GFile             *file,
5124                          const char        *contents,
5125                          gsize              length,
5126                          const char        *etag,
5127                          gboolean           make_backup,
5128                          GFileCreateFlags   flags,
5129                          char             **new_etag,
5130                          GCancellable      *cancellable,
5131                          GError           **error)
5132 {
5133   GFileOutputStream *out;
5134   gsize pos, remainder;
5135   gssize res;
5136
5137   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5138   g_return_val_if_fail (contents != NULL, FALSE);
5139
5140   out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5141   if (out == NULL)
5142     return FALSE;
5143
5144   pos = 0;
5145   remainder = length;
5146   while (remainder > 0 &&
5147          (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5148                                        contents + pos,
5149                                        MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5150                                        cancellable,
5151                                        error)) > 0)
5152     {
5153       pos += res;
5154       remainder -= res;
5155     }
5156   
5157   if (remainder > 0 && res < 0)
5158     {
5159       /* Ignore errors on close */
5160       g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5161       
5162       /* error is set already */
5163       return FALSE;
5164     }
5165   
5166   if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5167     return FALSE;
5168
5169   if (new_etag)
5170     *new_etag = g_file_output_stream_get_etag (out);
5171   
5172   return TRUE;
5173 }
5174
5175 typedef struct {
5176   GFile *file;
5177   GError *error;
5178   GCancellable *cancellable;
5179   GAsyncReadyCallback callback;
5180   gpointer user_data;
5181   const char *content;
5182   gsize length;
5183   gsize pos;
5184   char *etag;
5185 } ReplaceContentsData;
5186
5187 static void
5188 replace_contents_data_free (ReplaceContentsData *data)
5189 {
5190   if (data->error)
5191     g_error_free (data->error);
5192   if (data->cancellable)
5193     g_object_unref (data->cancellable);
5194   g_object_unref (data->file);
5195   g_free (data->etag);
5196   g_free (data);
5197 }
5198
5199 static void
5200 replace_contents_close_callback (GObject      *obj,
5201                                  GAsyncResult *close_res,
5202                                  gpointer      user_data)
5203 {
5204   GOutputStream *stream = G_OUTPUT_STREAM (obj);
5205   ReplaceContentsData *data = user_data;
5206   GSimpleAsyncResult *res;
5207
5208   /* Ignore errors here, we're only reading anyway */
5209   g_output_stream_close_finish (stream, close_res, NULL);
5210   g_object_unref (stream);
5211
5212   data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5213   
5214   res = g_simple_async_result_new (G_OBJECT (data->file),
5215                                    data->callback,
5216                                    data->user_data,
5217                                    g_file_replace_contents_async);
5218   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5219   g_simple_async_result_complete (res);
5220   g_object_unref (res);
5221 }
5222
5223 static void
5224 replace_contents_write_callback (GObject      *obj,
5225                                  GAsyncResult *read_res,
5226                                  gpointer      user_data)
5227 {
5228   GOutputStream *stream = G_OUTPUT_STREAM (obj);
5229   ReplaceContentsData *data = user_data;
5230   GError *error = NULL;
5231   gssize write_size;
5232   
5233   write_size = g_output_stream_write_finish (stream, read_res, &error);
5234
5235   if (write_size <= 0) 
5236     {
5237       /* Error or EOF, close the file */
5238       if (write_size < 0)
5239         data->error = error;
5240       g_output_stream_close_async (stream, 0,
5241                                    data->cancellable,
5242                                    replace_contents_close_callback, data);
5243     }
5244   else if (write_size > 0)
5245     {
5246       data->pos += write_size;
5247
5248       if (data->pos >= data->length)
5249         g_output_stream_close_async (stream, 0,
5250                                      data->cancellable,
5251                                      replace_contents_close_callback, data);
5252       else
5253         g_output_stream_write_async (stream,
5254                                      data->content + data->pos,
5255                                      data->length - data->pos,
5256                                      0,
5257                                      data->cancellable,
5258                                      replace_contents_write_callback,
5259                                      data);
5260     }
5261 }
5262
5263 static void
5264 replace_contents_open_callback (GObject      *obj,
5265                                 GAsyncResult *open_res,
5266                                 gpointer      user_data)
5267 {
5268   GFile *file = G_FILE (obj);
5269   GFileOutputStream *stream;
5270   ReplaceContentsData *data = user_data;
5271   GError *error = NULL;
5272   GSimpleAsyncResult *res;
5273
5274   stream = g_file_replace_finish (file, open_res, &error);
5275
5276   if (stream)
5277     {
5278       g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5279                                    data->content + data->pos,
5280                                    data->length - data->pos,
5281                                    0,
5282                                    data->cancellable,
5283                                    replace_contents_write_callback,
5284                                    data);
5285       
5286     }
5287   else
5288     {
5289       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5290                                                   data->callback,
5291                                                   data->user_data,
5292                                                   error);
5293       g_simple_async_result_complete (res);
5294       g_error_free (error);
5295       replace_contents_data_free (data);
5296       g_object_unref (res);
5297     }
5298 }
5299
5300 /**
5301  * g_file_replace_contents_async:
5302  * @file: input #GFile.
5303  * @contents: string of contents to replace the file with.
5304  * @length: the length of @contents in bytes.
5305  * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5306  * @make_backup: %TRUE if a backup should be created.
5307  * @flags: a set of #GFileCreateFlags.
5308  * @cancellable: optional #GCancellable object, %NULL to ignore.
5309  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5310  * @user_data: the data to pass to callback function
5311  * 
5312  * Starts an asynchronous replacement of @file with the given 
5313  * @contents of @length bytes. @etag will replace the document's 
5314  * current entity tag.
5315  * 
5316  * When this operation has completed, @callback will be called with
5317  * @user_user data, and the operation can be finalized with 
5318  * g_file_replace_contents_finish().
5319  * 
5320  * If @cancellable is not %NULL, then the operation can be cancelled by
5321  * triggering the cancellable object from another thread. If the operation
5322  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5323  * 
5324  * If @make_backup is %TRUE, this function will attempt to 
5325  * make a backup of @file.
5326  **/
5327 void
5328 g_file_replace_contents_async  (GFile               *file,
5329                                 const char          *contents,
5330                                 gsize                length,
5331                                 const char          *etag,
5332                                 gboolean             make_backup,
5333                                 GFileCreateFlags     flags,
5334                                 GCancellable        *cancellable,
5335                                 GAsyncReadyCallback  callback,
5336                                 gpointer             user_data)
5337 {
5338   ReplaceContentsData *data;
5339
5340   g_return_if_fail (G_IS_FILE (file));
5341   g_return_if_fail (contents != NULL);
5342
5343   data = g_new0 (ReplaceContentsData, 1);
5344
5345   if (cancellable)
5346     data->cancellable = g_object_ref (cancellable);
5347   data->callback = callback;
5348   data->user_data = user_data;
5349   data->content = contents;
5350   data->length = length;
5351   data->pos = 0;
5352   data->file = g_object_ref (file);
5353
5354   g_file_replace_async (file,
5355                         etag,
5356                         make_backup,
5357                         flags,
5358                         0,
5359                         cancellable,
5360                         replace_contents_open_callback,
5361                         data);
5362 }
5363   
5364 /**
5365  * g_file_replace_contents_finish:
5366  * @file: input #GFile.
5367  * @res: a #GAsyncResult. 
5368  * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link> 
5369  *     for the document. This should be freed with g_free() when it is no 
5370  *     longer needed.
5371  * @error: a #GError, or %NULL 
5372  * 
5373  * Finishes an asynchronous replace of the given @file. See
5374  * g_file_replace_contents_async(). Sets @new_etag to the new entity 
5375  * tag for the document, if present.
5376  * 
5377  * Returns: %TRUE on success, %FALSE on failure.
5378  **/
5379 gboolean
5380 g_file_replace_contents_finish (GFile         *file,
5381                                 GAsyncResult  *res,
5382                                 char         **new_etag,
5383                                 GError       **error)
5384 {
5385   GSimpleAsyncResult *simple;
5386   ReplaceContentsData *data;
5387
5388   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5389   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5390
5391   simple = G_SIMPLE_ASYNC_RESULT (res);
5392
5393   if (g_simple_async_result_propagate_error (simple, error))
5394     return FALSE;
5395   
5396   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5397   
5398   data = g_simple_async_result_get_op_res_gpointer (simple);
5399
5400   if (data->error)
5401     {
5402       g_propagate_error (error, data->error);
5403       data->error = NULL;
5404       return FALSE;
5405     }
5406
5407
5408   if (new_etag)
5409     {
5410       *new_etag = data->etag;
5411       data->etag = NULL; /* Take ownership */
5412     }
5413   
5414   return TRUE;
5415 }
5416
5417 #define __G_FILE_C__
5418 #include "gioaliasdef.c"