Add g_file_query_default_handler utility to easily look up the GAppInfo
[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 with this name already exists the G_IO_ERROR_EXISTS error
1310  * will be returned. If the file is a directory the G_IO_ERROR_IS_DIRECTORY
1311  * error will be returned.
1312  * Some file systems don't allow all file names, and may
1313  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1314  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1315  * Other errors are possible too, and depend on what kind of
1316  * filesystem the file is on.
1317  * 
1318  * Returns: a #GFileOutputStream for the newly created file, or 
1319  * %NULL on error.
1320  **/
1321 GFileOutputStream *
1322 g_file_create (GFile             *file,
1323                GFileCreateFlags   flags,
1324                GCancellable      *cancellable,
1325                GError           **error)
1326 {
1327   GFileIface *iface;
1328   
1329   g_return_val_if_fail (G_IS_FILE (file), NULL);
1330
1331   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1332     return NULL;
1333   
1334   iface = G_FILE_GET_IFACE (file);
1335
1336   if (iface->create == NULL)
1337     {
1338       g_set_error (error, G_IO_ERROR,
1339                    G_IO_ERROR_NOT_SUPPORTED,
1340                    _("Operation not supported"));
1341       return NULL;
1342     }
1343   
1344   return (* iface->create) (file, flags, cancellable, error);
1345 }
1346
1347 /**
1348  * g_file_replace:
1349  * @file: input #GFile.
1350  * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the 
1351  *     current #GFile, or #NULL to ignore.
1352  * @make_backup: %TRUE if a backup should be created.
1353  * @flags: a set of #GFileCreateFlags.
1354  * @cancellable: optional #GCancellable object, %NULL to ignore.
1355  * @error: a #GError, or %NULL
1356  *
1357  * Returns an output stream for overwriting the file, possibly
1358  * creating a backup copy of the file first.
1359  *
1360  * This will try to replace the file in the safest way possible so
1361  * that any errors during the writing will not affect an already
1362  * existing copy of the file. For instance, for local files it
1363  * may write to a temporary file and then atomically rename over
1364  * the destination when the stream is closed.
1365  * 
1366  * By default files created are generally readable by everyone,
1367  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1368  * will be made readable only to the current user, to the level that
1369  * is supported on the target filesystem.
1370  *
1371  * If @cancellable is not %NULL, then the operation can be cancelled by
1372  * triggering the cancellable object from another thread. If the operation
1373  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1374  * 
1375  * If you pass in a non-#NULL @etag value, then this value is
1376  * compared to the current entity tag of the file, and if they differ
1377  * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1378  * that the file has been changed since you last read it. You can get
1379  * the new etag from g_file_output_stream_get_etag() after you've
1380  * finished writing and closed the #GFileOutputStream. When you load
1381  * a new file you can use g_file_input_stream_query_info() to get
1382  * the etag of the file.
1383  * 
1384  * If @make_backup is %TRUE, this function will attempt to make a backup
1385  * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1386  * error will be returned. If you want to replace anyway, try again with
1387  * @make_backup set to %FALSE.
1388  *
1389  * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1390  * and if the file is some other form of non-regular file then a
1391  * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1392  * Some file systems don't allow all file names, and may
1393  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1394  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1395  * Other errors are possible too, and depend on what kind of
1396  * filesystem the file is on.
1397  *
1398  * Returns: a #GFileOutputStream or %NULL on error. 
1399  **/
1400 GFileOutputStream *
1401 g_file_replace (GFile             *file,
1402                 const char        *etag,
1403                 gboolean           make_backup,
1404                 GFileCreateFlags   flags,
1405                 GCancellable      *cancellable,
1406                 GError           **error)
1407 {
1408   GFileIface *iface;
1409
1410   g_return_val_if_fail (G_IS_FILE (file), NULL);
1411
1412   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1413     return NULL;
1414   
1415   iface = G_FILE_GET_IFACE (file);
1416
1417   if (iface->replace == NULL)
1418     {
1419       g_set_error (error, G_IO_ERROR,
1420                    G_IO_ERROR_NOT_SUPPORTED,
1421                    _("Operation not supported"));
1422       return NULL;
1423     }
1424   
1425   
1426   /* Handle empty tag string as NULL in consistent way. */
1427   if (etag && *etag == 0)
1428     etag = NULL;
1429   
1430   return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1431 }
1432
1433 /**
1434  * g_file_read_async:
1435  * @file: input #GFile.
1436  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1437  *     of the request. 
1438  * @cancellable: optional #GCancellable object, %NULL to ignore.
1439  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1440  * @user_data: the data to pass to callback function
1441  *
1442  * Asynchronously opens @file for reading.
1443  *
1444  * For more details, see g_file_read() which is
1445  * the synchronous version of this call.
1446  *
1447  * When the operation is finished, @callback will be called. You can then call
1448  * g_file_read_finish() to get the result of the operation.
1449  **/
1450 void
1451 g_file_read_async (GFile               *file,
1452                    int                  io_priority,
1453                    GCancellable        *cancellable,
1454                    GAsyncReadyCallback  callback,
1455                    gpointer             user_data)
1456 {
1457   GFileIface *iface;
1458   
1459   g_return_if_fail (G_IS_FILE (file));
1460
1461   iface = G_FILE_GET_IFACE (file);
1462   (* iface->read_async) (file,
1463                          io_priority,
1464                          cancellable,
1465                          callback,
1466                          user_data);
1467 }
1468
1469 /**
1470  * g_file_read_finish:
1471  * @file: input #GFile.
1472  * @res: a #GAsyncResult. 
1473  * @error: a #GError, or %NULL
1474  *
1475  * Finishes an asynchronous file read operation started with 
1476  * g_file_read_async(). 
1477  *  
1478  * Returns: a #GFileInputStream or %NULL on error.
1479  **/
1480 GFileInputStream *
1481 g_file_read_finish (GFile         *file,
1482                     GAsyncResult  *res,
1483                     GError       **error)
1484 {
1485   GFileIface *iface;
1486   
1487   g_return_val_if_fail (G_IS_FILE (file), NULL);
1488   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1489
1490   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1491     {
1492       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1493       if (g_simple_async_result_propagate_error (simple, error))
1494         return NULL;
1495     }
1496   
1497   iface = G_FILE_GET_IFACE (file);
1498   return (* iface->read_finish) (file, res, error);
1499 }
1500
1501 /**
1502  * g_file_append_to_async:
1503  * @file: input #GFile.
1504  * @flags: a set of #GFileCreateFlags.
1505  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1506  *     of the request. 
1507  * @cancellable: optional #GCancellable object, %NULL to ignore.
1508  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1509  * @user_data: the data to pass to callback function
1510  * 
1511  * Asynchronously opens @file for appending.
1512  *
1513  * For more details, see g_file_append_to() which is
1514  * the synchronous version of this call.
1515  *
1516  * When the operation is finished, @callback will be called. You can then call
1517  * g_file_append_to_finish() to get the result of the operation.
1518  **/
1519 void
1520 g_file_append_to_async (GFile               *file,
1521                         GFileCreateFlags     flags,
1522                         int                  io_priority,
1523                         GCancellable        *cancellable,
1524                         GAsyncReadyCallback  callback,
1525                         gpointer             user_data)
1526 {
1527   GFileIface *iface;
1528   
1529   g_return_if_fail (G_IS_FILE (file));
1530
1531   iface = G_FILE_GET_IFACE (file);
1532   (* iface->append_to_async) (file,
1533                               flags,
1534                               io_priority,
1535                               cancellable,
1536                               callback,
1537                               user_data);
1538 }
1539
1540 /**
1541  * g_file_append_to_finish:
1542  * @file: input #GFile.
1543  * @res: #GAsyncResult
1544  * @error: a #GError, or %NULL
1545  * 
1546  * Finishes an asynchronous file append operation started with 
1547  * g_file_append_to_async(). 
1548  * 
1549  * Returns: a valid #GFileOutputStream or %NULL on error.
1550  **/
1551 GFileOutputStream *
1552 g_file_append_to_finish (GFile         *file,
1553                          GAsyncResult  *res,
1554                          GError       **error)
1555 {
1556   GFileIface *iface;
1557   
1558   g_return_val_if_fail (G_IS_FILE (file), NULL);
1559   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1560
1561   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1562     {
1563       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1564       if (g_simple_async_result_propagate_error (simple, error))
1565         return NULL;
1566     }
1567   
1568   iface = G_FILE_GET_IFACE (file);
1569   return (* iface->append_to_finish) (file, res, error);
1570 }
1571
1572 /**
1573  * g_file_create_async:
1574  * @file: input #GFile.
1575  * @flags: a set of #GFileCreateFlags.
1576  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1577  *     of the request.
1578  * @cancellable: optional #GCancellable object, %NULL to ignore.
1579  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1580  * @user_data: the data to pass to callback function
1581  * 
1582  * Asynchronously creates a new file and returns an output stream for writing to it.
1583  * The file must not already exists.
1584  *
1585  * For more details, see g_file_create() which is
1586  * the synchronous version of this call.
1587  *
1588  * When the operation is finished, @callback will be called. You can then call
1589  * g_file_create_finish() to get the result of the operation.
1590  **/
1591 void
1592 g_file_create_async (GFile               *file,
1593                      GFileCreateFlags     flags,
1594                      int                  io_priority,
1595                      GCancellable        *cancellable,
1596                      GAsyncReadyCallback  callback,
1597                      gpointer             user_data)
1598 {
1599   GFileIface *iface;
1600   
1601   g_return_if_fail (G_IS_FILE (file));
1602
1603   iface = G_FILE_GET_IFACE (file);
1604   (* iface->create_async) (file,
1605                            flags,
1606                            io_priority,
1607                            cancellable,
1608                            callback,
1609                            user_data);
1610 }
1611
1612 /**
1613  * g_file_create_finish:
1614  * @file: input #GFile.
1615  * @res: a #GAsyncResult. 
1616  * @error: a #GError, or %NULL
1617  * 
1618  * Finishes an asynchronous file create operation started with 
1619  * g_file_create_async(). 
1620  * 
1621  * Returns: a #GFileOutputStream or %NULL on error.
1622  **/
1623 GFileOutputStream *
1624 g_file_create_finish (GFile         *file,
1625                       GAsyncResult  *res,
1626                       GError       **error)
1627 {
1628   GFileIface *iface;
1629   
1630   g_return_val_if_fail (G_IS_FILE (file), NULL);
1631   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1632
1633   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1634     {
1635       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1636       if (g_simple_async_result_propagate_error (simple, error))
1637         return NULL;
1638     }
1639   
1640   iface = G_FILE_GET_IFACE (file);
1641   return (* iface->create_finish) (file, res, error);
1642 }
1643
1644 /**
1645  * g_file_replace_async:
1646  * @file: input #GFile.
1647  * @etag: an <link linkend="gfile-etag">entity tag</link> for the 
1648  *     current #GFile, or NULL to ignore.
1649  * @make_backup: %TRUE if a backup should be created.
1650  * @flags: a set of #GFileCreateFlags.
1651  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1652  *     of the request.
1653  * @cancellable: optional #GCancellable object, %NULL to ignore.
1654  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1655  * @user_data: the data to pass to callback function
1656  *
1657  * Asynchronously overwrites the file, replacing the contents, possibly
1658  * creating a backup copy of the file first.
1659  *
1660  * For more details, see g_file_replace() which is
1661  * the synchronous version of this call.
1662  *
1663  * When the operation is finished, @callback will be called. You can then call
1664  * g_file_replace_finish() to get the result of the operation.
1665  **/
1666 void
1667 g_file_replace_async (GFile               *file,
1668                       const char          *etag,
1669                       gboolean             make_backup,
1670                       GFileCreateFlags     flags,
1671                       int                  io_priority,
1672                       GCancellable        *cancellable,
1673                       GAsyncReadyCallback  callback,
1674                       gpointer             user_data)
1675 {
1676   GFileIface *iface;
1677   
1678   g_return_if_fail (G_IS_FILE (file));
1679
1680   iface = G_FILE_GET_IFACE (file);
1681   (* iface->replace_async) (file,
1682                             etag,
1683                             make_backup,
1684                             flags,
1685                             io_priority,
1686                             cancellable,
1687                             callback,
1688                             user_data);
1689 }
1690
1691 /**
1692  * g_file_replace_finish:
1693  * @file: input #GFile.
1694  * @res: a #GAsyncResult. 
1695  * @error: a #GError, or %NULL
1696  * 
1697  * Finishes an asynchronous file replace operation started with 
1698  * g_file_replace_async(). 
1699  * 
1700  * Returns: a #GFileOutputStream, or %NULL on error.
1701  **/
1702 GFileOutputStream *
1703 g_file_replace_finish (GFile         *file,
1704                        GAsyncResult  *res,
1705                        GError       **error)
1706 {
1707   GFileIface *iface;
1708   
1709   g_return_val_if_fail (G_IS_FILE (file), NULL);
1710   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1711
1712   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1713     {
1714       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1715       if (g_simple_async_result_propagate_error (simple, error))
1716         return NULL;
1717     }
1718   
1719   iface = G_FILE_GET_IFACE (file);
1720   return (* iface->replace_finish) (file, res, error);
1721 }
1722
1723 static gboolean
1724 copy_symlink (GFile           *destination,
1725               GFileCopyFlags   flags,
1726               GCancellable    *cancellable,
1727               const char      *target,
1728               GError         **error)
1729 {
1730   GError *my_error;
1731   gboolean tried_delete;
1732   GFileInfo *info;
1733   GFileType file_type;
1734
1735   tried_delete = FALSE;
1736
1737  retry:
1738   my_error = NULL;
1739   if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1740     {
1741       /* Maybe it already existed, and we want to overwrite? */
1742       if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) && 
1743           my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1744         {
1745           g_error_free (my_error);
1746
1747
1748           /* Don't overwrite if the destination is a directory */
1749           info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1750                                     G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1751                                     cancellable, &my_error);
1752           if (info != NULL)
1753             {
1754               file_type = g_file_info_get_file_type (info);
1755               g_object_unref (info);
1756               
1757               if (file_type == G_FILE_TYPE_DIRECTORY)
1758                 {
1759                   g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1760                                _("Can't copy over directory"));
1761                   return FALSE;
1762                 }
1763             }
1764           
1765           if (!g_file_delete (destination, cancellable, error))
1766             return FALSE;
1767           
1768           tried_delete = TRUE;
1769           goto retry;
1770         }
1771             /* Nah, fail */
1772       g_propagate_error (error, my_error);
1773       return FALSE;
1774     }
1775
1776   return TRUE;
1777 }
1778
1779 static GInputStream *
1780 open_source_for_copy (GFile           *source,
1781                       GFile           *destination,
1782                       GFileCopyFlags   flags,
1783                       GCancellable    *cancellable,
1784                       GError         **error)
1785 {
1786   GError *my_error;
1787   GInputStream *in;
1788   GFileInfo *info;
1789   GFileType file_type;
1790   
1791   my_error = NULL;
1792   in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1793   if (in != NULL)
1794     return in;
1795
1796   /* There was an error opening the source, try to set a good error for it: */
1797
1798   if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1799     {
1800       /* The source is a directory, don't fail with WOULD_RECURSE immediately, 
1801        * as that is less useful to the app. Better check for errors on the 
1802        * target instead. 
1803        */
1804       g_error_free (my_error);
1805       my_error = NULL;
1806       
1807       info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1808                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1809                                 cancellable, &my_error);
1810       if (info != NULL)
1811         {
1812           file_type = g_file_info_get_file_type (info);
1813           g_object_unref (info);
1814           
1815           if (flags & G_FILE_COPY_OVERWRITE)
1816             {
1817               if (file_type == G_FILE_TYPE_DIRECTORY)
1818                 {
1819                   g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1820                                _("Can't copy directory over directory"));
1821                   return NULL;
1822                 }
1823               /* continue to would_recurse error */
1824             }
1825           else
1826             {
1827               g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1828                            _("Target file exists"));
1829               return NULL;
1830             }
1831         }
1832       else
1833         {
1834           /* Error getting info from target, return that error 
1835            * (except for NOT_FOUND, which is no error here) 
1836            */
1837           if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1838             {
1839               g_propagate_error (error, my_error);
1840               return NULL;
1841             }
1842           g_error_free (my_error);
1843         }
1844       
1845       g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1846                    _("Can't recursively copy directory"));
1847       return NULL;
1848     }
1849
1850   g_propagate_error (error, my_error);
1851   return NULL;
1852 }
1853
1854 static gboolean
1855 should_copy (GFileAttributeInfo *info, 
1856              gboolean            as_move)
1857 {
1858   if (as_move)
1859     return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1860   return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1861 }
1862
1863 static char *
1864 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1865                                GFileAttributeInfoList *namespaces,
1866                                gboolean                as_move)
1867 {
1868   GString *s;
1869   gboolean first;
1870   int i;
1871   
1872   first = TRUE;
1873   s = g_string_new ("");
1874
1875   if (attributes)
1876     {
1877       for (i = 0; i < attributes->n_infos; i++)
1878         {
1879           if (should_copy (&attributes->infos[i], as_move))
1880             {
1881               if (first)
1882                 first = FALSE;
1883               else
1884                 g_string_append_c (s, ',');
1885                 
1886               g_string_append (s, attributes->infos[i].name);
1887             }
1888         }
1889     }
1890
1891   if (namespaces)
1892     {
1893       for (i = 0; i < namespaces->n_infos; i++)
1894         {
1895           if (should_copy (&namespaces->infos[i], as_move))
1896             {
1897               if (first)
1898                 first = FALSE;
1899               else
1900                 g_string_append_c (s, ',');
1901                 
1902               g_string_append (s, namespaces->infos[i].name);
1903               g_string_append (s, ":*");
1904             }
1905         }
1906     }
1907
1908   return g_string_free (s, FALSE);
1909 }
1910
1911 /**
1912  * g_file_copy_attributes:
1913  * @source: a #GFile with attributes.
1914  * @destination: a #GFile to copy attributes to.
1915  * @flags: a set of #GFileCopyFlags.
1916  * @cancellable: optional #GCancellable object, %NULL to ignore.
1917  * @error: a #GError, %NULL to ignore.
1918  *
1919  * Copies the file attributes from @source to @destination. 
1920  *
1921  * Normally only a subset of the file attributes are copied,
1922  * those that are copies in a normal file copy operation
1923  * (which for instance does not include e.g. mtime). However
1924  * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
1925  * all the metadata that is possible to copy is copied.
1926  *
1927  * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1928  **/
1929 gboolean
1930 g_file_copy_attributes (GFile           *source,
1931                         GFile           *destination,
1932                         GFileCopyFlags   flags,
1933                         GCancellable    *cancellable,
1934                         GError         **error)
1935 {
1936   GFileAttributeInfoList *attributes, *namespaces;
1937   char *attrs_to_read;
1938   gboolean res;
1939   GFileInfo *info;
1940   gboolean as_move;
1941   gboolean source_nofollow_symlinks;
1942
1943   as_move = flags & G_FILE_COPY_ALL_METADATA;
1944   source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1945
1946   /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1947   attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1948   namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1949
1950   if (attributes == NULL && namespaces == NULL)
1951     return TRUE;
1952
1953   attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1954
1955   /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1956    * we just don't copy it. 
1957    */
1958   info = g_file_query_info (source, attrs_to_read,
1959                             source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1960                             cancellable,
1961                             NULL);
1962
1963   g_free (attrs_to_read);
1964   
1965   res = TRUE;
1966   if  (info)
1967     {
1968       res = g_file_set_attributes_from_info (destination,
1969                                              info, 0,
1970                                              cancellable,
1971                                              error);
1972       g_object_unref (info);
1973     }
1974   
1975   g_file_attribute_info_list_unref (attributes);
1976   g_file_attribute_info_list_unref (namespaces);
1977   
1978   return res;
1979 }
1980
1981 /* Closes the streams */
1982 static gboolean
1983 copy_stream_with_progress (GInputStream           *in,
1984                            GOutputStream          *out,
1985                            GCancellable           *cancellable,
1986                            GFileProgressCallback   progress_callback,
1987                            gpointer                progress_callback_data,
1988                            GError                **error)
1989 {
1990   gssize n_read, n_written;
1991   goffset current_size;
1992   char buffer[8192], *p;
1993   gboolean res;
1994   goffset total_size;
1995   GFileInfo *info;
1996
1997   total_size = 0;
1998   info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1999                                          G_FILE_ATTRIBUTE_STANDARD_SIZE,
2000                                          cancellable, NULL);
2001   if (info)
2002     {
2003       total_size = g_file_info_get_size (info);
2004       g_object_unref (info);
2005     }
2006   
2007   current_size = 0;
2008   res = TRUE;
2009   while (TRUE)
2010     {
2011       n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2012       if (n_read == -1)
2013         {
2014           res = FALSE;
2015           break;
2016         }
2017         
2018       if (n_read == 0)
2019         break;
2020
2021       current_size += n_read;
2022
2023       p = buffer;
2024       while (n_read > 0)
2025         {
2026           n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2027           if (n_written == -1)
2028             {
2029               res = FALSE;
2030               break;
2031             }
2032
2033           p += n_written;
2034           n_read -= n_written;
2035         }
2036
2037       if (!res)
2038         break;
2039       
2040       if (progress_callback)
2041         progress_callback (current_size, total_size, progress_callback_data);
2042     }
2043
2044   if (!res)
2045     error = NULL; /* Ignore further errors */
2046
2047   /* Make sure we send full copied size */
2048   if (progress_callback)
2049     progress_callback (current_size, total_size, progress_callback_data);
2050
2051   
2052   /* Don't care about errors in source here */
2053   g_input_stream_close (in, cancellable, NULL);
2054
2055   /* But write errors on close are bad! */
2056   if (!g_output_stream_close (out, cancellable, error))
2057     res = FALSE;
2058
2059   g_object_unref (in);
2060   g_object_unref (out);
2061       
2062   return res;
2063 }
2064
2065 static gboolean
2066 file_copy_fallback (GFile                  *source,
2067                     GFile                  *destination,
2068                     GFileCopyFlags          flags,
2069                     GCancellable           *cancellable,
2070                     GFileProgressCallback   progress_callback,
2071                     gpointer                progress_callback_data,
2072                     GError                **error)
2073 {
2074   GInputStream *in;
2075   GOutputStream *out;
2076   GFileInfo *info;
2077   const char *target;
2078
2079   /* Maybe copy the symlink? */
2080   if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2081     {
2082       info = g_file_query_info (source,
2083                                 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2084                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2085                                 cancellable,
2086                                 error);
2087       if (info == NULL)
2088         return FALSE;
2089
2090       if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2091           (target = g_file_info_get_symlink_target (info)) != NULL)
2092         {
2093           if (!copy_symlink (destination, flags, cancellable, target, error))
2094             {
2095               g_object_unref (info);
2096               return FALSE;
2097             }
2098           
2099           g_object_unref (info);
2100           goto copied_file;
2101         }
2102       
2103       g_object_unref (info);
2104     }
2105   
2106   in = open_source_for_copy (source, destination, flags, cancellable, error);
2107   if (in == NULL)
2108     return FALSE;
2109   
2110   if (flags & G_FILE_COPY_OVERWRITE)
2111     {
2112       out = (GOutputStream *)g_file_replace (destination,
2113                                              NULL, 0,
2114                                              flags & G_FILE_COPY_BACKUP,
2115                                              cancellable, error);
2116     }
2117   else
2118     {
2119       out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2120     }
2121
2122   if (out == NULL)
2123     {
2124       g_object_unref (in);
2125       return FALSE;
2126     }
2127
2128   if (!copy_stream_with_progress (in, out, cancellable,
2129                                   progress_callback, progress_callback_data,
2130                                   error))
2131     return FALSE;
2132
2133  copied_file:
2134
2135   /* Ignore errors here. Failure to copy metadata is not a hard error */
2136   g_file_copy_attributes (source, destination,
2137                           flags, cancellable, NULL);
2138   
2139   return TRUE;
2140 }
2141
2142 /**
2143  * g_file_copy:
2144  * @source: input #GFile.
2145  * @destination: destination #GFile
2146  * @flags: set of #GFileCopyFlags
2147  * @cancellable: optional #GCancellable object, %NULL to ignore.
2148  * @progress_callback: function to callback with progress information
2149  * @progress_callback_data: user data to pass to @progress_callback
2150  * @error: #GError to set on error, or %NULL
2151  *
2152  * Copies the file @source to the location specified by @destination.
2153  * Can not handle recursive copies of directories.
2154  *
2155  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2156  * existing @destination file is overwritten.
2157  *
2158  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2159  * will be copied as symlinks, otherwise the target of the
2160  * @source symlink will be copied.
2161  *
2162  * If @cancellable is not %NULL, then the operation can be cancelled by
2163  * triggering the cancellable object from another thread. If the operation
2164  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2165  * 
2166  * If @progress_callback is not %NULL, then the operation can be monitored by
2167  * setting this to a #GFileProgressCallback function. @progress_callback_data
2168  * will be passed to this function. It is guaranteed that this callback will
2169  * be called after all data has been transferred with the total number of bytes
2170  * copied during the operation.
2171  * 
2172  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2173  * error is returned, independent on the status of the @destination.
2174  *
2175  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2176  * error G_IO_ERROR_EXISTS is returned.
2177  *
2178  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2179  * error is returned. If trying to overwrite a directory with a directory the
2180  * G_IO_ERROR_WOULD_MERGE error is returned.
2181  *
2182  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2183  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2184  * is returned.
2185  *
2186  * If you are interested in copying the #GFile object itself (not the on-disk
2187  * file), see g_file_dup().
2188  *
2189  * Returns: %TRUE on success, %FALSE otherwise.
2190  **/
2191 gboolean
2192 g_file_copy (GFile                  *source,
2193              GFile                  *destination,
2194              GFileCopyFlags          flags,
2195              GCancellable           *cancellable,
2196              GFileProgressCallback   progress_callback,
2197              gpointer                progress_callback_data,
2198              GError                **error)
2199 {
2200   GFileIface *iface;
2201   GError *my_error;
2202   gboolean res;
2203
2204   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2205   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2206
2207   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2208     return FALSE;
2209   
2210   iface = G_FILE_GET_IFACE (destination);
2211   if (iface->copy)
2212     {
2213       my_error = NULL;
2214       res = (* iface->copy) (source, destination,
2215                              flags, cancellable,
2216                              progress_callback, progress_callback_data,
2217                              &my_error);
2218       
2219       if (res)
2220         return TRUE;
2221       
2222       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2223         {
2224           g_propagate_error (error, my_error);
2225               return FALSE;
2226         }
2227     }
2228
2229   /* If the types are different, and the destination method failed
2230      also try the source method */
2231   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2232     {
2233       iface = G_FILE_GET_IFACE (source);
2234       
2235       if (iface->copy)
2236         {
2237           my_error = NULL;
2238           res = (* iface->copy) (source, destination,
2239                                  flags, cancellable,
2240                                  progress_callback, progress_callback_data,
2241                                  &my_error);
2242           
2243           if (res)
2244             return TRUE;
2245           
2246           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2247             {
2248               g_propagate_error (error, my_error);
2249               return FALSE;
2250             }
2251         }
2252     }
2253   
2254   return file_copy_fallback (source, destination, flags, cancellable,
2255                              progress_callback, progress_callback_data,
2256                              error);
2257 }
2258
2259 /**
2260  * g_file_copy_async:
2261  * @source: input #GFile.
2262  * @destination: destination #GFile
2263  * @flags: set of #GFileCopyFlags
2264  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2265  *     of the request. 
2266  * @cancellable: optional #GCancellable object, %NULL to ignore.
2267  * @progress_callback: function to callback with progress information
2268  * @progress_callback_data: user data to pass to @progress_callback
2269  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2270  * @user_data: the data to pass to callback function
2271  *
2272  * Copies the file @source to the location specified by @destination 
2273  * asynchronously. For details of the behaviour, see g_file_copy().
2274  *
2275  * If @progress_callback is not %NULL, then that function that will be called
2276  * just like in g_file_copy(), however the callback will run in the main loop,
2277  * not in the thread that is doing the I/O operation.
2278  *
2279  * When the operation is finished, @callback will be called. You can then call
2280  * g_file_copy_finish() to get the result of the operation.
2281  **/
2282 void
2283 g_file_copy_async (GFile                  *source,
2284                    GFile                  *destination,
2285                    GFileCopyFlags          flags,
2286                    int                     io_priority,
2287                    GCancellable           *cancellable,
2288                    GFileProgressCallback   progress_callback,
2289                    gpointer                progress_callback_data,
2290                    GAsyncReadyCallback     callback,
2291                    gpointer                user_data)
2292 {
2293   GFileIface *iface;
2294
2295   g_return_if_fail (G_IS_FILE (source));
2296   g_return_if_fail (G_IS_FILE (destination));
2297
2298   iface = G_FILE_GET_IFACE (source);
2299   (* iface->copy_async) (source,
2300                          destination,
2301                          flags,
2302                          io_priority,
2303                          cancellable,
2304                          progress_callback,
2305                          progress_callback_data,
2306                          callback,
2307                          user_data);
2308 }
2309
2310 /**
2311  * g_file_copy_finish:
2312  * @file: input #GFile.
2313  * @res: a #GAsyncResult. 
2314  * @error: a #GError, or %NULL
2315  * 
2316  * Finishes copying the file started with 
2317  * g_file_copy_async().
2318  * 
2319  * Returns: a %TRUE on success, %FALSE on error.
2320  **/
2321 gboolean
2322 g_file_copy_finish (GFile        *file,
2323                     GAsyncResult *res,
2324                     GError      **error)
2325 {
2326   GFileIface *iface;
2327   
2328   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2329   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2330
2331   if (G_IS_SIMPLE_ASYNC_RESULT (res))
2332     {
2333       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2334       
2335       if (g_simple_async_result_propagate_error (simple, error))
2336         return FALSE;
2337     }
2338   
2339   iface = G_FILE_GET_IFACE (file);
2340   return (* iface->copy_finish) (file, res, error);
2341 }
2342
2343 /**
2344  * g_file_move:
2345  * @source: #GFile pointing to the source location.
2346  * @destination: #GFile pointing to the destination location.
2347  * @flags: set of #GFileCopyFlags.
2348  * @cancellable: optional #GCancellable object, %NULL to ignore.
2349  * @progress_callback: #GFileProgressCallback function for updates.
2350  * @progress_callback_data: gpointer to user data for the callback function.
2351  * @error: #GError for returning error conditions, or %NULL
2352  *
2353  *
2354  * Tries to move the file or directory @source to the location specified by @destination.
2355  * If native move operations are supported then this is used, otherwise a copy + delete
2356  * fallback is used. The native implementation may support moving directories (for instance
2357  * on moves inside the same filesystem), but the fallback code does not.
2358  * 
2359  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2360  * existing @destination file is overwritten.
2361  *
2362  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2363  * will be copied as symlinks, otherwise the target of the
2364  * @source symlink will be copied.
2365  *
2366  * If @cancellable is not %NULL, then the operation can be cancelled by
2367  * triggering the cancellable object from another thread. If the operation
2368  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2369  * 
2370  * If @progress_callback is not %NULL, then the operation can be monitored by
2371  * setting this to a #GFileProgressCallback function. @progress_callback_data
2372  * will be passed to this function. It is guaranteed that this callback will
2373  * be called after all data has been transferred with the total number of bytes
2374  * copied during the operation.
2375  * 
2376  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2377  * error is returned, independent on the status of the @destination.
2378  *
2379  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2380  * error G_IO_ERROR_EXISTS is returned.
2381  *
2382  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2383  * error is returned. If trying to overwrite a directory with a directory the
2384  * G_IO_ERROR_WOULD_MERGE error is returned.
2385  *
2386  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2387  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2388  * may be returned (if the native move operation isn't available).
2389  *
2390  * Returns: %TRUE on successful move, %FALSE otherwise.
2391  **/
2392 gboolean
2393 g_file_move (GFile                  *source,
2394              GFile                  *destination,
2395              GFileCopyFlags          flags,
2396              GCancellable           *cancellable,
2397              GFileProgressCallback   progress_callback,
2398              gpointer                progress_callback_data,
2399              GError                **error)
2400 {
2401   GFileIface *iface;
2402   GError *my_error;
2403   gboolean res;
2404
2405   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2406   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2407
2408   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2409     return FALSE;
2410   
2411   iface = G_FILE_GET_IFACE (destination);
2412   if (iface->move)
2413     {
2414       my_error = NULL;
2415       res = (* iface->move) (source, destination,
2416                              flags, cancellable,
2417                              progress_callback, progress_callback_data,
2418                              &my_error);
2419       
2420       if (res)
2421         return TRUE;
2422       
2423       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2424         {
2425           g_propagate_error (error, my_error);
2426           return FALSE;
2427         }
2428     }
2429
2430   /* If the types are different, and the destination method failed
2431      also try the source method */
2432   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2433     {
2434       iface = G_FILE_GET_IFACE (source);
2435       
2436       if (iface->move)
2437         {
2438           my_error = NULL;
2439           res = (* iface->move) (source, destination,
2440                                  flags, cancellable,
2441                                  progress_callback, progress_callback_data,
2442                                  &my_error);
2443           
2444           if (res)
2445             return TRUE;
2446           
2447           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2448             {
2449               g_propagate_error (error, my_error);
2450               return FALSE;
2451             }
2452         }
2453     }
2454   
2455   if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2456     {  
2457       g_set_error (error, G_IO_ERROR,
2458                    G_IO_ERROR_NOT_SUPPORTED,
2459                    _("Operation not supported"));
2460       return FALSE;
2461     }
2462   
2463   flags |= G_FILE_COPY_ALL_METADATA;
2464   if (!g_file_copy (source, destination, flags, cancellable,
2465                     progress_callback, progress_callback_data,
2466                     error))
2467     return FALSE;
2468
2469   return g_file_delete (source, cancellable, error);
2470 }
2471
2472 /**
2473  * g_file_make_directory
2474  * @file: input #GFile.
2475  * @cancellable: optional #GCancellable object, %NULL to ignore.
2476  * @error: a #GError, or %NULL 
2477  *
2478  * Creates a directory.
2479  * 
2480  * If @cancellable is not %NULL, then the operation can be cancelled by
2481  * triggering the cancellable object from another thread. If the operation
2482  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2483  * 
2484  * Returns: %TRUE on successful creation, %FALSE otherwise.
2485  **/
2486 gboolean
2487 g_file_make_directory (GFile         *file,
2488                        GCancellable  *cancellable,
2489                        GError       **error)
2490 {
2491   GFileIface *iface;
2492
2493   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2494
2495   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2496     return FALSE;
2497   
2498   iface = G_FILE_GET_IFACE (file);
2499
2500   if (iface->make_directory == NULL)
2501     {
2502       g_set_error (error, G_IO_ERROR,
2503                    G_IO_ERROR_NOT_SUPPORTED,
2504                    _("Operation not supported"));
2505       return FALSE;
2506     }
2507   
2508   return (* iface->make_directory) (file, cancellable, error);
2509 }
2510
2511 /**
2512  * g_file_make_symbolic_link:
2513  * @file: input #GFile.
2514  * @symlink_value: a string with the value of the new symlink.
2515  * @cancellable: optional #GCancellable object, %NULL to ignore.
2516  * @error: a #GError. 
2517  * 
2518  * Creates a symbolic link.
2519  *
2520  * If @cancellable is not %NULL, then the operation can be cancelled by
2521  * triggering the cancellable object from another thread. If the operation
2522  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2523  * 
2524  * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2525  **/
2526 gboolean
2527 g_file_make_symbolic_link (GFile         *file,
2528                            const char    *symlink_value,
2529                            GCancellable  *cancellable,
2530                            GError       **error)
2531 {
2532   GFileIface *iface;
2533
2534   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2535   g_return_val_if_fail (symlink_value != NULL, FALSE);
2536
2537   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2538     return FALSE;
2539
2540   if (*symlink_value == '\0')
2541     {
2542       g_set_error (error, G_IO_ERROR,
2543                    G_IO_ERROR_INVALID_ARGUMENT,
2544                    _("Invalid symlink value given"));
2545       return FALSE;
2546     }
2547   
2548   iface = G_FILE_GET_IFACE (file);
2549
2550   if (iface->make_symbolic_link == NULL)
2551     {
2552       g_set_error (error, G_IO_ERROR,
2553                    G_IO_ERROR_NOT_SUPPORTED,
2554                    _("Operation not supported"));
2555       return FALSE;
2556     }
2557   
2558   return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2559 }
2560
2561 /**
2562  * g_file_delete:
2563  * @file: input #GFile.
2564  * @cancellable: optional #GCancellable object, %NULL to ignore.
2565  * @error: a #GError, or %NULL 
2566  * 
2567  * Deletes a file.
2568  * 
2569  * If @cancellable is not %NULL, then the operation can be cancelled by
2570  * triggering the cancellable object from another thread. If the operation
2571  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2572  * 
2573  * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2574  **/
2575 gboolean
2576 g_file_delete (GFile         *file,
2577                GCancellable  *cancellable,
2578                GError       **error)
2579 {
2580   GFileIface *iface;
2581   
2582   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2583
2584   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2585     return FALSE;
2586   
2587   iface = G_FILE_GET_IFACE (file);
2588
2589   if (iface->delete_file == NULL)
2590     {
2591       g_set_error (error, G_IO_ERROR,
2592                    G_IO_ERROR_NOT_SUPPORTED,
2593                    _("Operation not supported"));
2594       return FALSE;
2595     }
2596   
2597   return (* iface->delete_file) (file, cancellable, error);
2598 }
2599
2600 /**
2601  * g_file_trash:
2602  * @file: #GFile to send to trash.
2603  * @cancellable: optional #GCancellable object, %NULL to ignore.
2604  * @error: a #GError, or %NULL
2605  *
2606  * Sends @file to the "Trashcan", if possible. This is similar to
2607  * deleting it, but the user can recover it before emptying the trashcan.
2608  * Not all file systems support trashing, so this call can return the
2609  * %G_IO_ERROR_NOT_SUPPORTED error.
2610  *
2611  *
2612  * If @cancellable is not %NULL, then the operation can be cancelled by
2613  * triggering the cancellable object from another thread. If the operation
2614  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2615  * 
2616  * Returns: %TRUE on successful trash, %FALSE otherwise.
2617  **/
2618 gboolean
2619 g_file_trash (GFile         *file,
2620               GCancellable  *cancellable,
2621               GError       **error)
2622 {
2623   GFileIface *iface;
2624   
2625   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2626
2627   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2628     return FALSE;
2629   
2630   iface = G_FILE_GET_IFACE (file);
2631
2632   if (iface->trash == NULL)
2633     {
2634       g_set_error (error,
2635                    G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2636                    _("Trash not supported"));
2637       return FALSE;
2638     }
2639   
2640   return (* iface->trash) (file, cancellable, error);
2641 }
2642
2643 /**
2644  * g_file_set_display_name:
2645  * @file: input #GFile.
2646  * @display_name: a string.
2647  * @cancellable: optional #GCancellable object, %NULL to ignore.
2648  * @error: a #GError, or %NULL
2649  * 
2650  * Renames @file to the specified display name.
2651  *
2652  * The display name is converted from UTF8 to the correct encoding for the target
2653  * filesystem if possible and the @file is renamed to this.
2654  * 
2655  * If you want to implement a rename operation in the user interface the edit name
2656  * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2657  * widget, and then the result after editing should be passed to g_file_set_display_name().
2658  *
2659  * On success the resulting converted filename is returned.
2660  * 
2661  * If @cancellable is not %NULL, then the operation can be cancelled by
2662  * triggering the cancellable object from another thread. If the operation
2663  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2664  * 
2665  * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2666  **/
2667 GFile *
2668 g_file_set_display_name (GFile         *file,
2669                          const char    *display_name,
2670                          GCancellable  *cancellable,
2671                          GError       **error)
2672 {
2673   GFileIface *iface;
2674   
2675   g_return_val_if_fail (G_IS_FILE (file), NULL);
2676   g_return_val_if_fail (display_name != NULL, NULL);
2677
2678   if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2679     {
2680       g_set_error (error,
2681                    G_IO_ERROR,
2682                    G_IO_ERROR_INVALID_ARGUMENT,
2683                    _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2684       return NULL;
2685     }
2686   
2687   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2688     return NULL;
2689   
2690   iface = G_FILE_GET_IFACE (file);
2691
2692   return (* iface->set_display_name) (file, display_name, cancellable, error);
2693 }
2694
2695 /**
2696  * g_file_set_display_name_async:
2697  * @file: input #GFile.
2698  * @display_name: a string.
2699  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2700  *     of the request. 
2701  * @cancellable: optional #GCancellable object, %NULL to ignore.
2702  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2703  * @user_data: the data to pass to callback function
2704  * 
2705  * Asynchronously sets the display name for a given #GFile.
2706  * 
2707  * For more details, see g_set_display_name() which is
2708  * the synchronous version of this call.
2709  *
2710  * When the operation is finished, @callback will be called. You can then call
2711  * g_file_set_display_name_finish() to get the result of the operation.
2712  **/
2713 void
2714 g_file_set_display_name_async (GFile               *file,
2715                                const char          *display_name,
2716                                int                  io_priority,
2717                                GCancellable        *cancellable,
2718                                GAsyncReadyCallback  callback,
2719                                gpointer             user_data)
2720 {
2721   GFileIface *iface;
2722   
2723   g_return_if_fail (G_IS_FILE (file));
2724   g_return_if_fail (display_name != NULL);
2725
2726   iface = G_FILE_GET_IFACE (file);
2727   (* iface->set_display_name_async) (file,
2728                                      display_name,
2729                                      io_priority,
2730                                      cancellable,
2731                                      callback,
2732                                      user_data);
2733 }
2734
2735 /**
2736  * g_file_set_display_name_finish:
2737  * @file: input #GFile.
2738  * @res: a #GAsyncResult. 
2739  * @error: a #GError, or %NULL
2740  * 
2741  * Finishes setting a display name started with 
2742  * g_file_set_display_name_async().
2743  * 
2744  * Returns: a #GFile or %NULL on error.
2745  **/
2746 GFile *
2747 g_file_set_display_name_finish (GFile         *file,
2748                                 GAsyncResult  *res,
2749                                 GError       **error)
2750 {
2751   GFileIface *iface;
2752   
2753   g_return_val_if_fail (G_IS_FILE (file), NULL);
2754   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2755
2756   if (G_IS_SIMPLE_ASYNC_RESULT (res))
2757     {
2758       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2759       if (g_simple_async_result_propagate_error (simple, error))
2760         return NULL;
2761     }
2762   
2763   iface = G_FILE_GET_IFACE (file);
2764   return (* iface->set_display_name_finish) (file, res, error);
2765 }
2766
2767 /**
2768  * g_file_query_settable_attributes:
2769  * @file: input #GFile.
2770  * @cancellable: optional #GCancellable object, %NULL to ignore.
2771  * @error: a #GError, or %NULL
2772  * 
2773  * Obtain the list of settable attributes for the file.
2774  *
2775  * Returns the type and full attribute name of all the attributes 
2776  * that can be set on this file. This doesn't mean setting it will always 
2777  * succeed though, you might get an access failure, or some specific 
2778  * file may not support a specific attribute.
2779  *
2780  * If @cancellable is not %NULL, then the operation can be cancelled by
2781  * triggering the cancellable object from another thread. If the operation
2782  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2783  * 
2784  * Returns: a #GFileAttributeInfoList describing the settable attributes.
2785  * When you are done with it, release it with g_file_attribute_info_list_unref()
2786  **/
2787 GFileAttributeInfoList *
2788 g_file_query_settable_attributes (GFile         *file,
2789                                   GCancellable  *cancellable,
2790                                   GError       **error)
2791 {
2792   GFileIface *iface;
2793   GError *my_error;
2794   GFileAttributeInfoList *list;
2795
2796   g_return_val_if_fail (G_IS_FILE (file), NULL);
2797
2798   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2799     return NULL;
2800   
2801   iface = G_FILE_GET_IFACE (file);
2802
2803   if (iface->query_settable_attributes == NULL)
2804     return g_file_attribute_info_list_new ();
2805
2806   my_error = NULL;
2807   list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2808   
2809   if (list == NULL)
2810     {
2811       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2812         {
2813           list = g_file_attribute_info_list_new ();
2814           g_error_free (my_error);
2815         }
2816       else
2817         g_propagate_error (error, my_error);
2818     }
2819   
2820   return list;
2821 }
2822
2823 /**
2824  * g_file_query_writable_namespaces:
2825  * @file: input #GFile.
2826  * @cancellable: optional #GCancellable object, %NULL to ignore.
2827  * @error: a #GError, or %NULL
2828  * 
2829  * Obtain the list of attribute namespaces where new attributes 
2830  * can be created by a user. An example of this is extended
2831  * attributes (in the "xattr" namespace).
2832  *
2833  * If @cancellable is not %NULL, then the operation can be cancelled by
2834  * triggering the cancellable object from another thread. If the operation
2835  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2836  * 
2837  * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2838  * When you are done with it, release it with g_file_attribute_info_list_unref()
2839  **/
2840 GFileAttributeInfoList *
2841 g_file_query_writable_namespaces (GFile         *file,
2842                                   GCancellable  *cancellable,
2843                                   GError       **error)
2844 {
2845   GFileIface *iface;
2846   GError *my_error;
2847   GFileAttributeInfoList *list;
2848   
2849   g_return_val_if_fail (G_IS_FILE (file), NULL);
2850
2851   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2852     return NULL;
2853   
2854   iface = G_FILE_GET_IFACE (file);
2855
2856   if (iface->query_writable_namespaces == NULL)
2857     return g_file_attribute_info_list_new ();
2858
2859   my_error = NULL;
2860   list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2861   
2862   if (list == NULL)
2863     {
2864       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2865         {
2866           list = g_file_attribute_info_list_new ();
2867           g_error_free (my_error);
2868         }
2869       else
2870         g_propagate_error (error, my_error);
2871     }
2872   
2873   return list;
2874 }
2875
2876 /**
2877  * g_file_set_attribute:
2878  * @file: input #GFile.
2879  * @attribute: a string containing the attribute's name.
2880  * @type: The type of the attribute
2881  * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2882  * @flags: a set of #GFileQueryInfoFlags.
2883  * @cancellable: optional #GCancellable object, %NULL to ignore.
2884  * @error: a #GError, or %NULL
2885  * 
2886  * Sets an attribute in the file with attribute name @attribute to @value.
2887  * 
2888  * If @cancellable is not %NULL, then the operation can be cancelled by
2889  * triggering the cancellable object from another thread. If the operation
2890  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2891  * 
2892  * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2893  **/
2894 gboolean
2895 g_file_set_attribute (GFile                      *file,
2896                       const char                 *attribute,
2897                       GFileAttributeType          type,
2898                       gpointer                    value_p,
2899                       GFileQueryInfoFlags         flags,
2900                       GCancellable               *cancellable,
2901                       GError                    **error)
2902 {
2903   GFileIface *iface;
2904   
2905   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2906   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2907
2908   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2909     return FALSE;
2910   
2911   iface = G_FILE_GET_IFACE (file);
2912
2913   if (iface->set_attribute == NULL)
2914     {
2915       g_set_error (error, G_IO_ERROR,
2916                    G_IO_ERROR_NOT_SUPPORTED,
2917                    _("Operation not supported"));
2918       return FALSE;
2919     }
2920
2921   return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2922 }
2923
2924 /**
2925  * g_file_set_attributes_from_info:
2926  * @file: input #GFile.
2927  * @info: a #GFileInfo.
2928  * @flags: #GFileQueryInfoFlags
2929  * @cancellable: optional #GCancellable object, %NULL to ignore.
2930  * @error: a #GError, or %NULL 
2931  * 
2932  * Tries to set all attributes in the #GFileInfo on the target values, 
2933  * not stopping on the first error.
2934  * 
2935  * If there is any error during this operation then @error will be set to
2936  * the first error. Error on particular fields are flagged by setting 
2937  * the "status" field in the attribute value to 
2938  * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2939  * further errors.
2940  *
2941  * If @cancellable is not %NULL, then the operation can be cancelled by
2942  * triggering the cancellable object from another thread. If the operation
2943  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2944  * 
2945  * Returns: %TRUE if there was any error, %FALSE otherwise.
2946  **/
2947 gboolean
2948 g_file_set_attributes_from_info (GFile                *file,
2949                                  GFileInfo            *info,
2950                                  GFileQueryInfoFlags   flags,
2951                                  GCancellable         *cancellable,
2952                                  GError              **error)
2953 {
2954   GFileIface *iface;
2955
2956   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2957   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2958
2959   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2960     return FALSE;
2961   
2962   g_file_info_clear_status (info);
2963   
2964   iface = G_FILE_GET_IFACE (file);
2965
2966   return (* iface->set_attributes_from_info) (file, 
2967                                               info, 
2968                                               flags, 
2969                                               cancellable, 
2970                                               error);
2971 }
2972
2973
2974 static gboolean
2975 g_file_real_set_attributes_from_info (GFile                *file,
2976                                       GFileInfo            *info,
2977                                       GFileQueryInfoFlags   flags,
2978                                       GCancellable         *cancellable,
2979                                       GError              **error)
2980 {
2981   char **attributes;
2982   int i;
2983   gboolean res;
2984   GFileAttributeValue *value;
2985   
2986   res = TRUE;
2987   
2988   attributes = g_file_info_list_attributes (info, NULL);
2989
2990   for (i = 0; attributes[i] != NULL; i++)
2991     {
2992       value = _g_file_info_get_attribute_value (info, attributes[i]);
2993
2994       if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2995         continue;
2996
2997       if (!g_file_set_attribute (file, attributes[i],
2998                                  value->type, _g_file_attribute_value_peek_as_pointer (value),
2999                                  flags, cancellable, error))
3000         {
3001           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3002           res = FALSE;
3003           /* Don't set error multiple times */
3004           error = NULL;
3005         }
3006       else
3007         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3008     }
3009   
3010   g_strfreev (attributes);
3011   
3012   return res;
3013 }
3014
3015 /**
3016  * g_file_set_attributes_async:
3017  * @file: input #GFile.
3018  * @info: a #GFileInfo.
3019  * @flags: a #GFileQueryInfoFlags.
3020  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
3021  *     of the request. 
3022  * @cancellable: optional #GCancellable object, %NULL to ignore.
3023  * @callback: a #GAsyncReadyCallback. 
3024  * @user_data: a #gpointer.
3025  *
3026  * Asynchronously sets the attributes of @file with @info.
3027  * 
3028  * For more details, see g_file_set_attributes_from_info() which is
3029  * the synchronous version of this call.
3030  *
3031  * When the operation is finished, @callback will be called. You can then call
3032  * g_file_set_attributes_finish() to get the result of the operation.
3033  **/
3034 void
3035 g_file_set_attributes_async (GFile               *file,
3036                              GFileInfo           *info,
3037                              GFileQueryInfoFlags  flags,
3038                              int                  io_priority,
3039                              GCancellable        *cancellable,
3040                              GAsyncReadyCallback  callback,
3041                              gpointer             user_data)
3042 {
3043   GFileIface *iface;
3044   
3045   g_return_if_fail (G_IS_FILE (file));
3046   g_return_if_fail (G_IS_FILE_INFO (info));
3047
3048   iface = G_FILE_GET_IFACE (file);
3049   (* iface->set_attributes_async) (file, 
3050                                    info, 
3051                                    flags, 
3052                                    io_priority, 
3053                                    cancellable, 
3054                                    callback, 
3055                                    user_data);
3056 }
3057
3058 /**
3059  * g_file_set_attributes_finish:
3060  * @file: input #GFile.
3061  * @result: a #GAsyncResult.
3062  * @info: a #GFileInfo.
3063  * @error: a #GError, or %NULL
3064  * 
3065  * Finishes setting an attribute started in g_file_set_attributes_async().
3066  * 
3067  * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3068  **/
3069 gboolean
3070 g_file_set_attributes_finish (GFile         *file,
3071                               GAsyncResult  *result,
3072                               GFileInfo    **info,
3073                               GError       **error)
3074 {
3075   GFileIface *iface;
3076   
3077   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3078   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3079
3080   /* No standard handling of errors here, as we must set info even
3081    * on errors 
3082    */
3083   iface = G_FILE_GET_IFACE (file);
3084   return (* iface->set_attributes_finish) (file, result, info, error);
3085 }
3086
3087 /**
3088  * g_file_set_attribute_string:
3089  * @file: input #GFile.
3090  * @attribute: a string containing the attribute's name.
3091  * @value: a string containing the attribute's value.
3092  * @flags: #GFileQueryInfoFlags.
3093  * @cancellable: optional #GCancellable object, %NULL to ignore.
3094  * @error: a #GError, or %NULL
3095  * 
3096  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value. 
3097  * If @attribute is of a different type, this operation will fail.
3098  * 
3099  * If @cancellable is not %NULL, then the operation can be cancelled by
3100  * triggering the cancellable object from another thread. If the operation
3101  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3102  * 
3103  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3104  **/
3105 gboolean
3106 g_file_set_attribute_string (GFile                *file,
3107                              const char           *attribute,
3108                              const char           *value,
3109                              GFileQueryInfoFlags   flags,
3110                              GCancellable         *cancellable,
3111                              GError              **error)
3112 {
3113   return g_file_set_attribute (file, attribute,
3114                                G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3115                                flags, cancellable, error);
3116 }
3117
3118 /**
3119  * g_file_set_attribute_byte_string:
3120  * @file: input #GFile.
3121  * @attribute: a string containing the attribute's name.
3122  * @value: a string containing the attribute's new value.
3123  * @flags: a #GFileQueryInfoFlags.
3124  * @cancellable: optional #GCancellable object, %NULL to ignore.
3125  * @error: a #GError, or %NULL
3126  * 
3127  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value. 
3128  * If @attribute is of a different type, this operation will fail, 
3129  * returning %FALSE. 
3130  * 
3131  * If @cancellable is not %NULL, then the operation can be cancelled by
3132  * triggering the cancellable object from another thread. If the operation
3133  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3134  * 
3135  * Returns: %TRUE if the @attribute was successfully set to @value 
3136  * in the @file, %FALSE otherwise.
3137  **/
3138 gboolean
3139 g_file_set_attribute_byte_string  (GFile                *file,
3140                                    const char           *attribute,
3141                                    const char           *value,
3142                                    GFileQueryInfoFlags   flags,
3143                                    GCancellable         *cancellable,
3144                                    GError              **error)
3145 {
3146   return g_file_set_attribute (file, attribute,
3147                                G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3148                                flags, cancellable, error);
3149 }
3150
3151 /**
3152  * g_file_set_attribute_uint32:
3153  * @file: input #GFile.
3154  * @attribute: a string containing the attribute's name.
3155  * @value: a #guint32 containing the attribute's new value.
3156  * @flags: a #GFileQueryInfoFlags.
3157  * @cancellable: optional #GCancellable object, %NULL to ignore.
3158  * @error: a #GError, or %NULL
3159  * 
3160  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value. 
3161  * If @attribute is of a different type, this operation will fail.
3162  * 
3163  * If @cancellable is not %NULL, then the operation can be cancelled by
3164  * triggering the cancellable object from another thread. If the operation
3165  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3166  * 
3167  * Returns: %TRUE if the @attribute was successfully set to @value 
3168  * in the @file, %FALSE otherwise.
3169  **/
3170 gboolean
3171 g_file_set_attribute_uint32 (GFile                *file,
3172                              const char           *attribute,
3173                              guint32               value,
3174                              GFileQueryInfoFlags   flags,
3175                              GCancellable         *cancellable,
3176                              GError              **error)
3177 {
3178   return g_file_set_attribute (file, attribute,
3179                                G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3180                                flags, cancellable, error);
3181 }
3182
3183 /**
3184  * g_file_set_attribute_int32:
3185  * @file: input #GFile.
3186  * @attribute: a string containing the attribute's name.
3187  * @value: a #gint32 containing the attribute's new value.
3188  * @flags: a #GFileQueryInfoFlags.
3189  * @cancellable: optional #GCancellable object, %NULL to ignore.
3190  * @error: a #GError, or %NULL
3191  * 
3192  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value. 
3193  * If @attribute is of a different type, this operation will fail.
3194  * 
3195  * If @cancellable is not %NULL, then the operation can be cancelled by
3196  * triggering the cancellable object from another thread. If the operation
3197  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3198  * 
3199  * Returns: %TRUE if the @attribute was successfully set to @value 
3200  * in the @file, %FALSE otherwise. 
3201  **/
3202 gboolean
3203 g_file_set_attribute_int32 (GFile                *file,
3204                             const char           *attribute,
3205                             gint32                value,
3206                             GFileQueryInfoFlags   flags,
3207                             GCancellable         *cancellable,
3208                             GError              **error)
3209 {
3210   return g_file_set_attribute (file, attribute,
3211                                G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3212                                flags, cancellable, error);
3213 }
3214
3215 /**
3216  * g_file_set_attribute_uint64:
3217  * @file: input #GFile. 
3218  * @attribute: a string containing the attribute's name.
3219  * @value: a #guint64 containing the attribute's new value.
3220  * @flags: a #GFileQueryInfoFlags.
3221  * @cancellable: optional #GCancellable object, %NULL to ignore.
3222  * @error: a #GError, or %NULL
3223  * 
3224  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value. 
3225  * If @attribute is of a different type, this operation will fail.
3226  * 
3227  * If @cancellable is not %NULL, then the operation can be cancelled by
3228  * triggering the cancellable object from another thread. If the operation
3229  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3230  * 
3231  * Returns: %TRUE if the @attribute was successfully set to @value 
3232  * in the @file, %FALSE otherwise.
3233  **/
3234 gboolean
3235 g_file_set_attribute_uint64 (GFile                *file,
3236                              const char           *attribute,
3237                              guint64               value,
3238                              GFileQueryInfoFlags   flags,
3239                              GCancellable         *cancellable,
3240                              GError              **error)
3241  {
3242   return g_file_set_attribute (file, attribute,
3243                                G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3244                                flags, cancellable, error);
3245 }
3246
3247 /**
3248  * g_file_set_attribute_int64:
3249  * @file: input #GFile.
3250  * @attribute: a string containing the attribute's name.
3251  * @value: a #guint64 containing the attribute's new value.
3252  * @flags: a #GFileQueryInfoFlags.
3253  * @cancellable: optional #GCancellable object, %NULL to ignore.
3254  * @error: a #GError, or %NULL
3255  * 
3256  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value. 
3257  * If @attribute is of a different type, this operation will fail.
3258  * 
3259  * If @cancellable is not %NULL, then the operation can be cancelled by
3260  * triggering the cancellable object from another thread. If the operation
3261  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3262  * 
3263  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3264  **/
3265 gboolean
3266 g_file_set_attribute_int64 (GFile                *file,
3267                             const char           *attribute,
3268                             gint64                value,
3269                             GFileQueryInfoFlags   flags,
3270                             GCancellable         *cancellable,
3271                             GError              **error)
3272 {
3273   return g_file_set_attribute (file, attribute,
3274                                G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3275                                flags, cancellable, error);
3276 }
3277
3278 /**
3279  * g_file_mount_mountable:
3280  * @file: input #GFile.
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                         GMountOperation     *mount_operation,
3300                         GCancellable        *cancellable,
3301                         GAsyncReadyCallback  callback,
3302                         gpointer             user_data)
3303 {
3304   GFileIface *iface;
3305
3306   g_return_if_fail (G_IS_FILE (file));
3307
3308   iface = G_FILE_GET_IFACE (file);
3309
3310   if (iface->mount_mountable == NULL)
3311     g_simple_async_report_error_in_idle (G_OBJECT (file),
3312                                          callback,
3313                                          user_data,
3314                                          G_IO_ERROR,
3315                                          G_IO_ERROR_NOT_SUPPORTED,
3316                                          _("Operation not supported"));
3317   
3318   (* iface->mount_mountable) (file,
3319                               mount_operation,
3320                               cancellable,
3321                               callback,
3322                               user_data);
3323 }
3324
3325 /**
3326  * g_file_mount_mountable_finish:
3327  * @file: input #GFile.
3328  * @result: a #GAsyncResult.
3329  * @error: a #GError, or %NULL
3330  *
3331  * Finishes a mount operation. See g_file_mount_mountable() for details.
3332  * 
3333  * Finish an asynchronous mount operation that was started 
3334  * with g_file_mount_mountable().
3335  *
3336  * Returns: a #GFile or %NULL on error.
3337  **/
3338 GFile *
3339 g_file_mount_mountable_finish (GFile         *file,
3340                                GAsyncResult  *result,
3341                                GError       **error)
3342 {
3343   GFileIface *iface;
3344
3345   g_return_val_if_fail (G_IS_FILE (file), NULL);
3346   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3347
3348   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3349     {
3350       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3351       if (g_simple_async_result_propagate_error (simple, error))
3352         return NULL;
3353     }
3354   
3355   iface = G_FILE_GET_IFACE (file);
3356   return (* iface->mount_mountable_finish) (file, result, error);
3357 }
3358
3359 /**
3360  * g_file_unmount_mountable:
3361  * @file: input #GFile.
3362  * @flags: flags affecting the operation
3363  * @cancellable: optional #GCancellable object, %NULL to ignore.
3364  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3365  * @user_data: the data to pass to callback function
3366  *
3367  * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3368  *
3369  * If @cancellable is not %NULL, then the operation can be cancelled by
3370  * triggering the cancellable object from another thread. If the operation
3371  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3372  *
3373  * When the operation is finished, @callback will be called. You can then call
3374  * g_file_unmount_mountable_finish() to get the result of the operation.
3375  **/
3376 void
3377 g_file_unmount_mountable (GFile               *file,
3378                           GMountUnmountFlags   flags,
3379                           GCancellable        *cancellable,
3380                           GAsyncReadyCallback  callback,
3381                           gpointer             user_data)
3382 {
3383   GFileIface *iface;
3384   
3385   g_return_if_fail (G_IS_FILE (file));
3386
3387   iface = G_FILE_GET_IFACE (file);
3388   
3389   if (iface->unmount_mountable == NULL)
3390     g_simple_async_report_error_in_idle (G_OBJECT (file),
3391                                          callback,
3392                                          user_data,
3393                                          G_IO_ERROR,
3394                                          G_IO_ERROR_NOT_SUPPORTED,
3395                                          _("Operation not supported"));
3396   
3397   (* iface->unmount_mountable) (file,
3398                                 flags,
3399                                 cancellable,
3400                                 callback,
3401                                 user_data);
3402 }
3403
3404 /**
3405  * g_file_unmount_mountable_finish:
3406  * @file: input #GFile.
3407  * @result: a #GAsyncResult.
3408  * @error: a #GError, or %NULL
3409  *
3410  * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3411  * 
3412  * Finish an asynchronous unmount operation that was started 
3413  * with g_file_unmount_mountable().
3414  *
3415  * Returns: %TRUE if the operation finished successfully. %FALSE
3416  * otherwise.
3417  **/
3418 gboolean
3419 g_file_unmount_mountable_finish (GFile         *file,
3420                                  GAsyncResult  *result,
3421                                  GError       **error)
3422 {
3423   GFileIface *iface;
3424   
3425   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3426   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3427
3428   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3429     {
3430       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3431       if (g_simple_async_result_propagate_error (simple, error))
3432         return FALSE;
3433     }
3434   
3435   iface = G_FILE_GET_IFACE (file);
3436   return (* iface->unmount_mountable_finish) (file, result, error);
3437 }
3438
3439 /**
3440  * g_file_eject_mountable:
3441  * @file: input #GFile.
3442  * @flags: flags affecting the operation
3443  * @cancellable: optional #GCancellable object, %NULL to ignore.
3444  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3445  * @user_data: the data to pass to callback function
3446  * 
3447  * Starts an asynchronous eject on a mountable.  
3448  * When this operation has completed, @callback will be called with
3449  * @user_user data, and the operation can be finalized with 
3450  * g_file_eject_mountable_finish().
3451  * 
3452  * If @cancellable is not %NULL, then the operation can be cancelled by
3453  * triggering the cancellable object from another thread. If the operation
3454  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3455  **/
3456 void
3457 g_file_eject_mountable (GFile               *file,
3458                         GMountUnmountFlags   flags,
3459                         GCancellable        *cancellable,
3460                         GAsyncReadyCallback  callback,
3461                         gpointer             user_data)
3462 {
3463   GFileIface *iface;
3464
3465   g_return_if_fail (G_IS_FILE (file));
3466
3467   iface = G_FILE_GET_IFACE (file);
3468   
3469   if (iface->eject_mountable == NULL)
3470     g_simple_async_report_error_in_idle (G_OBJECT (file),
3471                                          callback,
3472                                          user_data,
3473                                          G_IO_ERROR,
3474                                          G_IO_ERROR_NOT_SUPPORTED,
3475                                          _("Operation not supported"));
3476   
3477   (* iface->eject_mountable) (file,
3478                               flags,
3479                               cancellable,
3480                               callback,
3481                               user_data);
3482 }
3483
3484 /**
3485  * g_file_eject_mountable_finish:
3486  * @file: input #GFile.
3487  * @result: a #GAsyncResult.
3488  * @error: a #GError, or %NULL
3489  * 
3490  * Finishes an asynchronous eject operation started by 
3491  * g_file_eject_mountable().
3492  * 
3493  * Returns: %TRUE if the @file was ejected successfully. %FALSE 
3494  * otherwise.
3495  **/
3496 gboolean
3497 g_file_eject_mountable_finish (GFile         *file,
3498                                GAsyncResult  *result,
3499                                GError       **error)
3500 {
3501   GFileIface *iface;
3502   
3503   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3504   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3505
3506   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3507     {
3508       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3509       if (g_simple_async_result_propagate_error (simple, error))
3510         return FALSE;
3511     }
3512   
3513   iface = G_FILE_GET_IFACE (file);
3514   return (* iface->eject_mountable_finish) (file, result, error);
3515 }
3516
3517 /**
3518  * g_file_monitor_directory:
3519  * @file: input #GFile.
3520  * @flags: a set of #GFileMonitorFlags.
3521  * @cancellable: optional #GCancellable object, %NULL to ignore.
3522  * @error: a #GError, or %NULL.
3523  * 
3524  * Obtains a directory monitor for the given file.
3525  * This may fail if directory monitoring is not supported.
3526  *
3527  * If @cancellable is not %NULL, then the operation can be cancelled by
3528  * triggering the cancellable object from another thread. If the operation
3529  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3530  * 
3531  * Returns: a #GFileMonitor for the given @file, 
3532  * or %NULL on error.
3533  **/
3534 GFileMonitor*
3535 g_file_monitor_directory (GFile             *file,
3536                           GFileMonitorFlags  flags,
3537                           GCancellable      *cancellable,
3538                           GError           **error)
3539 {
3540   GFileIface *iface;
3541
3542   g_return_val_if_fail (G_IS_FILE (file), NULL);
3543
3544   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3545     return NULL;
3546
3547   iface = G_FILE_GET_IFACE (file);
3548
3549   if (iface->monitor_dir == NULL)
3550     {
3551       g_set_error (error, G_IO_ERROR,
3552                    G_IO_ERROR_NOT_SUPPORTED,
3553                    _("Operation not supported"));
3554       return NULL;
3555     }
3556
3557   return (* iface->monitor_dir) (file, flags, cancellable, error);
3558 }
3559
3560 /**
3561  * g_file_monitor_file:
3562  * @file: input #GFile.
3563  * @flags: a set of #GFileMonitorFlags.
3564  * @cancellable: optional #GCancellable object, %NULL to ignore.
3565  * @error: a #GError, or %NULL.
3566  * 
3567  * Obtains a file monitor for the given file. If no file notification
3568  * mechanism exists, then regular polling of the file is used.
3569  *
3570  * If @cancellable is not %NULL, then the operation can be cancelled by
3571  * triggering the cancellable object from another thread. If the operation
3572  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3573  * 
3574  * Returns: a #GFileMonitor for the given @file.
3575  **/
3576 GFileMonitor*
3577 g_file_monitor_file (GFile             *file,
3578                      GFileMonitorFlags  flags,
3579                      GCancellable      *cancellable,
3580                      GError           **error)
3581 {
3582   GFileIface *iface;
3583   GFileMonitor *monitor;
3584   
3585   g_return_val_if_fail (G_IS_FILE (file), NULL);
3586
3587   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3588     return NULL;
3589
3590   iface = G_FILE_GET_IFACE (file);
3591
3592   monitor = NULL;
3593   
3594   if (iface->monitor_file)
3595     monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3596
3597 /* Fallback to polling */
3598   if (monitor == NULL)
3599     monitor = _g_poll_file_monitor_new (file);
3600
3601   return monitor;
3602 }
3603
3604 /********************************************
3605  *   Default implementation of async ops    *
3606  ********************************************/
3607
3608 typedef struct {
3609   char *attributes;
3610   GFileQueryInfoFlags flags;
3611   GFileInfo *info;
3612 } QueryInfoAsyncData;
3613
3614 static void
3615 query_info_data_free (QueryInfoAsyncData *data)
3616 {
3617   if (data->info)
3618     g_object_unref (data->info);
3619   g_free (data->attributes);
3620   g_free (data);
3621 }
3622
3623 static void
3624 query_info_async_thread (GSimpleAsyncResult *res,
3625                          GObject            *object,
3626                          GCancellable       *cancellable)
3627 {
3628   GError *error = NULL;
3629   QueryInfoAsyncData *data;
3630   GFileInfo *info;
3631   
3632   data = g_simple_async_result_get_op_res_gpointer (res);
3633   
3634   info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3635
3636   if (info == NULL)
3637     {
3638       g_simple_async_result_set_from_error (res, error);
3639       g_error_free (error);
3640     }
3641   else
3642     data->info = info;
3643 }
3644
3645 static void
3646 g_file_real_query_info_async (GFile               *file,
3647                               const char          *attributes,
3648                               GFileQueryInfoFlags  flags,
3649                               int                  io_priority,
3650                               GCancellable        *cancellable,
3651                               GAsyncReadyCallback  callback,
3652                               gpointer             user_data)
3653 {
3654   GSimpleAsyncResult *res;
3655   QueryInfoAsyncData *data;
3656
3657   data = g_new0 (QueryInfoAsyncData, 1);
3658   data->attributes = g_strdup (attributes);
3659   data->flags = flags;
3660   
3661   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3662   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3663   
3664   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3665   g_object_unref (res);
3666 }
3667
3668 static GFileInfo *
3669 g_file_real_query_info_finish (GFile         *file,
3670                                GAsyncResult  *res,
3671                                GError       **error)
3672 {
3673   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3674   QueryInfoAsyncData *data;
3675
3676   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3677
3678   data = g_simple_async_result_get_op_res_gpointer (simple);
3679   if (data->info)
3680     return g_object_ref (data->info);
3681   
3682   return NULL;
3683 }
3684
3685 typedef struct {
3686   char *attributes;
3687   GFileQueryInfoFlags flags;
3688   GFileEnumerator *enumerator;
3689 } EnumerateChildrenAsyncData;
3690
3691 static void
3692 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3693 {
3694   if (data->enumerator)
3695     g_object_unref (data->enumerator);
3696   g_free (data->attributes);
3697   g_free (data);
3698 }
3699
3700 static void
3701 enumerate_children_async_thread (GSimpleAsyncResult *res,
3702                                  GObject            *object,
3703                                  GCancellable       *cancellable)
3704 {
3705   GError *error = NULL;
3706   EnumerateChildrenAsyncData *data;
3707   GFileEnumerator *enumerator;
3708   
3709   data = g_simple_async_result_get_op_res_gpointer (res);
3710   
3711   enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3712
3713   if (enumerator == NULL)
3714     {
3715       g_simple_async_result_set_from_error (res, error);
3716       g_error_free (error);
3717     }
3718   else
3719     data->enumerator = enumerator;
3720 }
3721
3722 static void
3723 g_file_real_enumerate_children_async (GFile               *file,
3724                                       const char          *attributes,
3725                                       GFileQueryInfoFlags  flags,
3726                                       int                  io_priority,
3727                                       GCancellable        *cancellable,
3728                                       GAsyncReadyCallback  callback,
3729                                       gpointer             user_data)
3730 {
3731   GSimpleAsyncResult *res;
3732   EnumerateChildrenAsyncData *data;
3733
3734   data = g_new0 (EnumerateChildrenAsyncData, 1);
3735   data->attributes = g_strdup (attributes);
3736   data->flags = flags;
3737   
3738   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3739   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3740   
3741   g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3742   g_object_unref (res);
3743 }
3744
3745 static GFileEnumerator *
3746 g_file_real_enumerate_children_finish (GFile         *file,
3747                                        GAsyncResult  *res,
3748                                        GError       **error)
3749 {
3750   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3751   EnumerateChildrenAsyncData *data;
3752
3753   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3754
3755   data = g_simple_async_result_get_op_res_gpointer (simple);
3756   if (data->enumerator)
3757     return g_object_ref (data->enumerator);
3758   
3759   return NULL;
3760 }
3761
3762 static void
3763 open_read_async_thread (GSimpleAsyncResult *res,
3764                         GObject            *object,
3765                         GCancellable       *cancellable)
3766 {
3767   GFileIface *iface;
3768   GFileInputStream *stream;
3769   GError *error = NULL;
3770
3771   iface = G_FILE_GET_IFACE (object);
3772
3773   stream = iface->read_fn (G_FILE (object), cancellable, &error);
3774
3775   if (stream == NULL)
3776     {
3777       g_simple_async_result_set_from_error (res, error);
3778       g_error_free (error);
3779     }
3780   else
3781     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3782 }
3783
3784 static void
3785 g_file_real_read_async (GFile               *file,
3786                         int                  io_priority,
3787                         GCancellable        *cancellable,
3788                         GAsyncReadyCallback  callback,
3789                         gpointer             user_data)
3790 {
3791   GSimpleAsyncResult *res;
3792   
3793   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3794   
3795   g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3796   g_object_unref (res);
3797 }
3798
3799 static GFileInputStream *
3800 g_file_real_read_finish (GFile         *file,
3801                          GAsyncResult  *res,
3802                          GError       **error)
3803 {
3804   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3805   gpointer op;
3806
3807   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3808
3809   op = g_simple_async_result_get_op_res_gpointer (simple);
3810   if (op)
3811     return g_object_ref (op);
3812   
3813   return NULL;
3814 }
3815
3816 static void
3817 append_to_async_thread (GSimpleAsyncResult *res,
3818                         GObject            *object,
3819                         GCancellable       *cancellable)
3820 {
3821   GFileIface *iface;
3822   GFileCreateFlags *data;
3823   GFileOutputStream *stream;
3824   GError *error = NULL;
3825
3826   iface = G_FILE_GET_IFACE (object);
3827
3828   data = g_simple_async_result_get_op_res_gpointer (res);
3829
3830   stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3831
3832   if (stream == NULL)
3833     {
3834       g_simple_async_result_set_from_error (res, error);
3835       g_error_free (error);
3836     }
3837   else
3838     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3839 }
3840
3841 static void
3842 g_file_real_append_to_async (GFile               *file,
3843                              GFileCreateFlags     flags,
3844                              int                  io_priority,
3845                              GCancellable        *cancellable,
3846                              GAsyncReadyCallback  callback,
3847                              gpointer             user_data)
3848 {
3849   GFileCreateFlags *data;
3850   GSimpleAsyncResult *res;
3851
3852   data = g_new0 (GFileCreateFlags, 1);
3853   *data = flags;
3854
3855   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3856   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3857
3858   g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3859   g_object_unref (res);
3860 }
3861
3862 static GFileOutputStream *
3863 g_file_real_append_to_finish (GFile         *file,
3864                               GAsyncResult  *res,
3865                               GError       **error)
3866 {
3867   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3868   gpointer op;
3869
3870   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3871
3872   op = g_simple_async_result_get_op_res_gpointer (simple);
3873   if (op)
3874     return g_object_ref (op);
3875   
3876   return NULL;
3877 }
3878
3879 static void
3880 create_async_thread (GSimpleAsyncResult *res,
3881                      GObject            *object,
3882                      GCancellable       *cancellable)
3883 {
3884   GFileIface *iface;
3885   GFileCreateFlags *data;
3886   GFileOutputStream *stream;
3887   GError *error = NULL;
3888
3889   iface = G_FILE_GET_IFACE (object);
3890
3891   data = g_simple_async_result_get_op_res_gpointer (res);
3892
3893   stream = iface->create (G_FILE (object), *data, cancellable, &error);
3894
3895   if (stream == NULL)
3896     {
3897       g_simple_async_result_set_from_error (res, error);
3898       g_error_free (error);
3899     }
3900   else
3901     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3902 }
3903
3904 static void
3905 g_file_real_create_async (GFile               *file,
3906                           GFileCreateFlags     flags,
3907                           int                  io_priority,
3908                           GCancellable        *cancellable,
3909                           GAsyncReadyCallback  callback,
3910                           gpointer             user_data)
3911 {
3912   GFileCreateFlags *data;
3913   GSimpleAsyncResult *res;
3914
3915   data = g_new0 (GFileCreateFlags, 1);
3916   *data = flags;
3917
3918   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3919   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3920
3921   g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3922   g_object_unref (res);
3923 }
3924
3925 static GFileOutputStream *
3926 g_file_real_create_finish (GFile         *file,
3927                            GAsyncResult  *res,
3928                            GError       **error)
3929 {
3930   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3931   gpointer op;
3932
3933   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3934
3935   op = g_simple_async_result_get_op_res_gpointer (simple);
3936   if (op)
3937     return g_object_ref (op);
3938   
3939   return NULL;
3940 }
3941
3942 typedef struct {
3943   GFileOutputStream *stream;
3944   char *etag;
3945   gboolean make_backup;
3946   GFileCreateFlags flags;
3947 } ReplaceAsyncData;
3948
3949 static void
3950 replace_async_data_free (ReplaceAsyncData *data)
3951 {
3952   if (data->stream)
3953     g_object_unref (data->stream);
3954   g_free (data->etag);
3955   g_free (data);
3956 }
3957
3958 static void
3959 replace_async_thread (GSimpleAsyncResult *res,
3960                       GObject            *object,
3961                       GCancellable       *cancellable)
3962 {
3963   GFileIface *iface;
3964   GFileOutputStream *stream;
3965   GError *error = NULL;
3966   ReplaceAsyncData *data;
3967
3968   iface = G_FILE_GET_IFACE (object);
3969   
3970   data = g_simple_async_result_get_op_res_gpointer (res);
3971
3972   stream = iface->replace (G_FILE (object),
3973                            data->etag,
3974                            data->make_backup,
3975                            data->flags,
3976                            cancellable,
3977                            &error);
3978
3979   if (stream == NULL)
3980     {
3981       g_simple_async_result_set_from_error (res, error);
3982       g_error_free (error);
3983     }
3984   else
3985     data->stream = stream;
3986 }
3987
3988 static void
3989 g_file_real_replace_async (GFile               *file,
3990                            const char          *etag,
3991                            gboolean             make_backup,
3992                            GFileCreateFlags     flags,
3993                            int                  io_priority,
3994                            GCancellable        *cancellable,
3995                            GAsyncReadyCallback  callback,
3996                            gpointer             user_data)
3997 {
3998   GSimpleAsyncResult *res;
3999   ReplaceAsyncData *data;
4000
4001   data = g_new0 (ReplaceAsyncData, 1);
4002   data->etag = g_strdup (etag);
4003   data->make_backup = make_backup;
4004   data->flags = flags;
4005
4006   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4007   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4008
4009   g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4010   g_object_unref (res);
4011 }
4012
4013 static GFileOutputStream *
4014 g_file_real_replace_finish (GFile         *file,
4015                             GAsyncResult  *res,
4016                             GError       **error)
4017 {
4018   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4019   ReplaceAsyncData *data;
4020
4021   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4022
4023   data = g_simple_async_result_get_op_res_gpointer (simple);
4024   if (data->stream)
4025     return g_object_ref (data->stream);
4026   
4027   return NULL;
4028 }
4029
4030 typedef struct {
4031   char *name;
4032   GFile *file;
4033 } SetDisplayNameAsyncData;
4034
4035 static void
4036 set_display_name_data_free (SetDisplayNameAsyncData *data)
4037 {
4038   g_free (data->name);
4039   if (data->file)
4040     g_object_unref (data->file);
4041   g_free (data);
4042 }
4043
4044 static void
4045 set_display_name_async_thread (GSimpleAsyncResult *res,
4046                                GObject            *object,
4047                                GCancellable       *cancellable)
4048 {
4049   GError *error = NULL;
4050   SetDisplayNameAsyncData *data;
4051   GFile *file;
4052   
4053   data = g_simple_async_result_get_op_res_gpointer (res);
4054   
4055   file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4056
4057   if (file == NULL)
4058     {
4059       g_simple_async_result_set_from_error (res, error);
4060       g_error_free (error);
4061     }
4062   else
4063     data->file = file;
4064 }
4065
4066 static void
4067 g_file_real_set_display_name_async (GFile               *file,  
4068                                     const char          *display_name,
4069                                     int                  io_priority,
4070                                     GCancellable        *cancellable,
4071                                     GAsyncReadyCallback  callback,
4072                                     gpointer             user_data)
4073 {
4074   GSimpleAsyncResult *res;
4075   SetDisplayNameAsyncData *data;
4076
4077   data = g_new0 (SetDisplayNameAsyncData, 1);
4078   data->name = g_strdup (display_name);
4079   
4080   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4081   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4082   
4083   g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4084   g_object_unref (res);
4085 }
4086
4087 static GFile *
4088 g_file_real_set_display_name_finish (GFile         *file,
4089                                      GAsyncResult  *res,
4090                                      GError       **error)
4091 {
4092   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4093   SetDisplayNameAsyncData *data;
4094
4095   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4096
4097   data = g_simple_async_result_get_op_res_gpointer (simple);
4098   if (data->file)
4099     return g_object_ref (data->file);
4100   
4101   return NULL;
4102 }
4103
4104 typedef struct {
4105   GFileQueryInfoFlags flags;
4106   GFileInfo *info;
4107   gboolean res;
4108   GError *error;
4109 } SetInfoAsyncData;
4110
4111 static void
4112 set_info_data_free (SetInfoAsyncData *data)
4113 {
4114   if (data->info)
4115     g_object_unref (data->info);
4116   if (data->error)
4117     g_error_free (data->error);
4118   g_free (data);
4119 }
4120
4121 static void
4122 set_info_async_thread (GSimpleAsyncResult *res,
4123                        GObject            *object,
4124                        GCancellable       *cancellable)
4125 {
4126   SetInfoAsyncData *data;
4127   
4128   data = g_simple_async_result_get_op_res_gpointer (res);
4129   
4130   data->error = NULL;
4131   data->res = g_file_set_attributes_from_info (G_FILE (object),
4132                                                data->info,
4133                                                data->flags,
4134                                                cancellable,
4135                                                &data->error);
4136 }
4137
4138 static void
4139 g_file_real_set_attributes_async (GFile               *file,
4140                                   GFileInfo           *info,
4141                                   GFileQueryInfoFlags  flags,
4142                                   int                  io_priority,
4143                                   GCancellable        *cancellable,
4144                                   GAsyncReadyCallback  callback,
4145                                   gpointer             user_data)
4146 {
4147   GSimpleAsyncResult *res;
4148   SetInfoAsyncData *data;
4149
4150   data = g_new0 (SetInfoAsyncData, 1);
4151   data->info = g_file_info_dup (info);
4152   data->flags = flags;
4153   
4154   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4155   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4156   
4157   g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4158   g_object_unref (res);
4159 }
4160
4161 static gboolean
4162 g_file_real_set_attributes_finish (GFile         *file,
4163                                    GAsyncResult  *res,
4164                                    GFileInfo    **info,
4165                                    GError       **error)
4166 {
4167   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4168   SetInfoAsyncData *data;
4169   
4170   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4171
4172   data = g_simple_async_result_get_op_res_gpointer (simple);
4173
4174   if (info) 
4175     *info = g_object_ref (data->info);
4176
4177   if (error != NULL && data->error) 
4178     *error = g_error_copy (data->error);
4179   
4180   return data->res;
4181 }
4182
4183 static void
4184 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4185                                     GObject            *object,
4186                                     GCancellable       *cancellable)
4187 {
4188   GError *error = NULL;
4189   GMount *mount;
4190   
4191   mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4192
4193   if (mount == NULL)
4194     {
4195       g_simple_async_result_set_from_error (res, error);
4196       g_error_free (error);
4197     }
4198   else
4199     g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4200 }
4201
4202 static void
4203 g_file_real_find_enclosing_mount_async (GFile               *file,
4204                                         int                  io_priority,
4205                                         GCancellable        *cancellable,
4206                                         GAsyncReadyCallback  callback,
4207                                         gpointer             user_data)
4208 {
4209   GSimpleAsyncResult *res;
4210   
4211   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4212   
4213   g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4214   g_object_unref (res);
4215 }
4216
4217 static GMount *
4218 g_file_real_find_enclosing_mount_finish (GFile         *file,
4219                                           GAsyncResult  *res,
4220                                           GError       **error)
4221 {
4222   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4223   GMount *mount;
4224
4225   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4226
4227   mount = g_simple_async_result_get_op_res_gpointer (simple);
4228   return g_object_ref (mount);
4229 }
4230
4231
4232 typedef struct {
4233   GFile *source;
4234   GFile *destination;
4235   GFileCopyFlags flags;
4236   GFileProgressCallback progress_cb;
4237   gpointer progress_cb_data;
4238   GIOSchedulerJob *job;
4239 } CopyAsyncData;
4240
4241 static void
4242 copy_async_data_free (CopyAsyncData *data)
4243 {
4244   g_object_unref (data->source);
4245   g_object_unref (data->destination);
4246   g_free (data);
4247 }
4248
4249 typedef struct {
4250   CopyAsyncData *data;
4251   goffset current_num_bytes;
4252   goffset total_num_bytes;
4253 } ProgressData;
4254
4255 static gboolean
4256 copy_async_progress_in_main (gpointer user_data)
4257 {
4258   ProgressData *progress = user_data;
4259   CopyAsyncData *data = progress->data;
4260
4261   data->progress_cb (progress->current_num_bytes,
4262                      progress->total_num_bytes,
4263                      data->progress_cb_data);
4264
4265   return FALSE;
4266 }
4267
4268 static gboolean
4269 mainloop_barrier (gpointer user_data)
4270 {
4271   /* Does nothing, but ensures all queued idles before
4272      this are run */
4273   return FALSE;
4274 }
4275
4276
4277 static void
4278 copy_async_progress_callback (goffset  current_num_bytes,
4279                               goffset  total_num_bytes,
4280                               gpointer user_data)
4281 {
4282   CopyAsyncData *data = user_data;
4283   ProgressData *progress;
4284
4285   progress = g_new (ProgressData, 1);
4286   progress->data = data;
4287   progress->current_num_bytes = current_num_bytes;
4288   progress->total_num_bytes = total_num_bytes;
4289   
4290   g_io_scheduler_job_send_to_mainloop_async (data->job,
4291                                              copy_async_progress_in_main,
4292                                              progress,
4293                                              g_free);
4294 }
4295
4296 static gboolean
4297 copy_async_thread (GIOSchedulerJob *job,
4298                    GCancellable    *cancellable,
4299                    gpointer         user_data)
4300 {
4301   GSimpleAsyncResult *res;
4302   CopyAsyncData *data;
4303   gboolean result;
4304   GError *error;
4305
4306   res = user_data;
4307   data = g_simple_async_result_get_op_res_gpointer (res);
4308
4309   error = NULL;
4310   data->job = job;
4311   result = g_file_copy (data->source,
4312                         data->destination,
4313                         data->flags,
4314                         cancellable,
4315                         (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4316                         data,
4317                         &error);
4318
4319   /* Ensure all progress callbacks are done running in main thread */
4320   if (data->progress_cb != NULL)
4321     g_io_scheduler_job_send_to_mainloop (job,
4322                                          mainloop_barrier,
4323                                          NULL, NULL);
4324   
4325   if (!result)
4326     {
4327       g_simple_async_result_set_from_error (res, error);
4328       g_error_free (error);
4329     }
4330
4331   g_simple_async_result_complete_in_idle (res);
4332
4333   return FALSE;
4334 }
4335
4336 static void
4337 g_file_real_copy_async (GFile                  *source,
4338                         GFile                  *destination,
4339                         GFileCopyFlags          flags,
4340                         int                     io_priority,
4341                         GCancellable           *cancellable,
4342                         GFileProgressCallback   progress_callback,
4343                         gpointer                progress_callback_data,
4344                         GAsyncReadyCallback     callback,
4345                         gpointer                user_data)
4346 {
4347   GSimpleAsyncResult *res;
4348   CopyAsyncData *data;
4349
4350   data = g_new0 (CopyAsyncData, 1);
4351   data->source = g_object_ref (source);
4352   data->destination = g_object_ref (destination);
4353   data->flags = flags;
4354   data->progress_cb = progress_callback;
4355   data->progress_cb_data = progress_callback_data;
4356
4357   res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4358   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4359
4360   g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4361 }
4362
4363 static gboolean
4364 g_file_real_copy_finish (GFile        *file,
4365                          GAsyncResult *res,
4366                          GError      **error)
4367 {
4368   /* Error handled in g_file_copy_finish() */
4369   return TRUE;
4370 }
4371
4372
4373 /********************************************
4374  *   Default VFS operations                 *
4375  ********************************************/
4376
4377 /**
4378  * g_file_new_for_path:
4379  * @path: a string containing a relative or absolute path.
4380  * 
4381  * Constructs a #GFile for a given path. This operation never
4382  * fails, but the returned object might not support any I/O
4383  * operation if @path is malformed.
4384  * 
4385  * Returns: a new #GFile for the given @path. 
4386  **/
4387 GFile *
4388 g_file_new_for_path (const char *path)
4389 {
4390   g_return_val_if_fail (path != NULL, NULL);
4391
4392   return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4393 }
4394  
4395 /**
4396  * g_file_new_for_uri:
4397  * @uri: a string containing a URI.
4398  * 
4399  * Constructs a #GFile for a given URI. This operation never 
4400  * fails, but the returned object might not support any I/O 
4401  * operation if @uri is malformed or if the uri type is 
4402  * not supported.
4403  * 
4404  * Returns: a #GFile for the given @uri.
4405  **/ 
4406 GFile *
4407 g_file_new_for_uri (const char *uri)
4408 {
4409   g_return_val_if_fail (uri != NULL, NULL);
4410
4411   return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4412 }
4413   
4414 /**
4415  * g_file_parse_name:
4416  * @parse_name: a file name or path to be parsed.
4417  * 
4418  * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4419  * This operation never fails, but the returned object might not support any I/O
4420  * operation if the @parse_name cannot be parsed.
4421  * 
4422  * Returns: a new #GFile.
4423  **/
4424 GFile *
4425 g_file_parse_name (const char *parse_name)
4426 {
4427   g_return_val_if_fail (parse_name != NULL, NULL);
4428
4429   return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4430 }
4431
4432 static gboolean
4433 is_valid_scheme_character (char c)
4434 {
4435   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4436 }
4437
4438 static gboolean
4439 has_valid_scheme (const char *uri)
4440 {
4441   const char *p;
4442   
4443   p = uri;
4444   
4445   if (!is_valid_scheme_character (*p))
4446     return FALSE;
4447
4448   do {
4449     p++;
4450   } while (is_valid_scheme_character (*p));
4451
4452   return *p == ':';
4453 }
4454
4455 /**
4456  * g_file_new_for_commandline_arg:
4457  * @arg: a command line string.
4458  * 
4459  * Creates a #GFile with the given argument from the command line. The value of
4460  * @arg can be either a URI, an absolute path or a relative path resolved
4461  * relative to the current working directory.
4462  * This operation never fails, but the returned object might not support any
4463  * I/O operation if @arg points to a malformed path.
4464  *
4465  * Returns: a new #GFile. 
4466  **/
4467 GFile *
4468 g_file_new_for_commandline_arg (const char *arg)
4469 {
4470   GFile *file;
4471   char *filename;
4472   char *current_dir;
4473   
4474   g_return_val_if_fail (arg != NULL, NULL);
4475   
4476   if (g_path_is_absolute (arg))
4477     return g_file_new_for_path (arg);
4478
4479   if (has_valid_scheme (arg))
4480     return g_file_new_for_uri (arg);
4481     
4482   current_dir = g_get_current_dir ();
4483   filename = g_build_filename (current_dir, arg, NULL);
4484   g_free (current_dir);
4485   
4486   file = g_file_new_for_path (filename);
4487   g_free (filename);
4488   
4489   return file;
4490 }
4491
4492 /**
4493  * g_file_mount_enclosing_volume:
4494  * @location: input #GFile.
4495  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4496  * @cancellable: optional #GCancellable object, %NULL to ignore.
4497  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4498  * @user_data: the data to pass to callback function
4499  * 
4500  * Starts a @mount_operation, mounting the volume that contains the file @location. 
4501  * 
4502  * When this operation has completed, @callback will be called with
4503  * @user_user data, and the operation can be finalized with 
4504  * g_file_mount_enclosing_volume_finish().
4505  * 
4506  * If @cancellable is not %NULL, then the operation can be cancelled by
4507  * triggering the cancellable object from another thread. If the operation
4508  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4509  **/
4510 void
4511 g_file_mount_enclosing_volume (GFile               *location,
4512                                GMountOperation     *mount_operation,
4513                                GCancellable        *cancellable,
4514                                GAsyncReadyCallback  callback,
4515                                gpointer             user_data)
4516 {
4517   GFileIface *iface;
4518
4519   g_return_if_fail (G_IS_FILE (location));
4520
4521   iface = G_FILE_GET_IFACE (location);
4522
4523   if (iface->mount_enclosing_volume == NULL)
4524     {
4525       g_simple_async_report_error_in_idle (G_OBJECT (location),
4526                                            callback, user_data,
4527                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4528                                            _("volume doesn't implement mount"));
4529       
4530       return;
4531     }
4532   
4533   (* iface->mount_enclosing_volume) (location, mount_operation, cancellable, callback, user_data);
4534
4535 }
4536
4537 /**
4538  * g_file_mount_enclosing_volume_finish:
4539  * @location: input #GFile.
4540  * @result: a #GAsyncResult.
4541  * @error: a #GError, or %NULL
4542  * 
4543  * Finishes a mount operation started by g_file_mount_enclosing_volume().
4544  * 
4545  * Returns: %TRUE if successful. If an error
4546  * has occurred, this function will return %FALSE and set @error
4547  * appropriately if present.
4548  **/
4549 gboolean
4550 g_file_mount_enclosing_volume_finish (GFile         *location,
4551                                       GAsyncResult  *result,
4552                                       GError       **error)
4553 {
4554   GFileIface *iface;
4555
4556   g_return_val_if_fail (G_IS_FILE (location), FALSE);
4557   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4558
4559   if (G_IS_SIMPLE_ASYNC_RESULT (result))
4560     {
4561       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4562       if (g_simple_async_result_propagate_error (simple, error))
4563         return FALSE;
4564     }
4565   
4566   iface = G_FILE_GET_IFACE (location);
4567
4568   return (* iface->mount_enclosing_volume_finish) (location, result, error);
4569 }
4570
4571 /********************************************
4572  *   Utility functions                      *
4573  ********************************************/
4574
4575 GAppInfo *
4576 g_file_query_default_handler (GFile                  *file,
4577                               GCancellable           *cancellable,
4578                               GError                **error)
4579 {
4580   char *uri_scheme;
4581   const char *content_type;
4582   GAppInfo *appinfo;
4583   GFileInfo *info;
4584   char *path;
4585   
4586   uri_scheme = g_file_get_uri_scheme (file);
4587   appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4588   g_free (uri_scheme);
4589
4590   if (appinfo != NULL)
4591     return appinfo;
4592
4593   info = g_file_query_info (file,
4594                             G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4595                             0,
4596                             cancellable,
4597                             error);
4598   if (info == NULL)
4599     return NULL;
4600
4601   appinfo = NULL;
4602
4603   content_type = g_file_info_get_content_type (info);
4604   if (content_type)
4605     {
4606       /* Don't use is_native(), as we want to support fuse paths if availible */
4607       path = g_file_get_path (file);
4608       appinfo = g_app_info_get_default_for_type (content_type,
4609                                                  path == NULL);
4610       g_free (path);
4611     }
4612   
4613   g_object_unref (info);
4614
4615   if (appinfo != NULL)
4616     return appinfo;
4617
4618   g_set_error (error, G_IO_ERROR,
4619                G_IO_ERROR_NOT_SUPPORTED,
4620                _("No application is registered as handling this file"));
4621   return NULL;
4622   
4623 }
4624
4625
4626 #define GET_CONTENT_BLOCK_SIZE 8192
4627
4628 /**
4629  * g_file_load_contents:
4630  * @file: input #GFile.
4631  * @cancellable: optional #GCancellable object, %NULL to ignore.
4632  * @contents: a location to place the contents of the file.
4633  * @length: a location to place the length of the contents of the file.
4634  * @etag_out: a location to place the current entity tag for the file.
4635  * @error: a #GError, or %NULL
4636  *
4637  * Loads the content of the file into memory, returning the size of
4638  * the data. The data is always zero terminated, but this is not
4639  * included in the resultant @length.
4640  * 
4641  * If @cancellable is not %NULL, then the operation can be cancelled by
4642  * triggering the cancellable object from another thread. If the operation
4643  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4644  * 
4645  * Returns: %TRUE if the @file's contents were successfully loaded.
4646  * %FALSE if there were errors..
4647  **/
4648 gboolean
4649 g_file_load_contents (GFile         *file,
4650                       GCancellable  *cancellable,
4651                       char         **contents,
4652                       gsize         *length,
4653                       char         **etag_out,
4654                       GError       **error)
4655 {
4656   GFileInputStream *in;
4657   GByteArray *content;
4658   gsize pos;
4659   gssize res;
4660   GFileInfo *info;
4661
4662   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4663   g_return_val_if_fail (contents != NULL, FALSE);
4664
4665   in = g_file_read (file, cancellable, error);
4666   if (in == NULL)
4667     return FALSE;
4668
4669   content = g_byte_array_new ();
4670   pos = 0;
4671   
4672   g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4673   while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4674                                      content->data + pos,
4675                                      GET_CONTENT_BLOCK_SIZE,
4676                                      cancellable, error)) > 0)
4677     {
4678       pos += res;
4679       g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4680     }
4681
4682   if (etag_out)
4683     {
4684       *etag_out = NULL;
4685       
4686       info = g_file_input_stream_query_info (in,
4687                                              G_FILE_ATTRIBUTE_ETAG_VALUE,
4688                                              cancellable,
4689                                              NULL);
4690       if (info)
4691         {
4692           *etag_out = g_strdup (g_file_info_get_etag (info));
4693           g_object_unref (info);
4694         }
4695     } 
4696
4697   /* Ignore errors on close */
4698   g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4699   g_object_unref (in);
4700
4701   if (res < 0)
4702     {
4703       /* error is set already */
4704       g_byte_array_free (content, TRUE);
4705       return FALSE;
4706     }
4707
4708   if (length)
4709     *length = pos;
4710
4711   /* Zero terminate (we got an extra byte allocated for this */
4712   content->data[pos] = 0;
4713   
4714   *contents = (char *)g_byte_array_free (content, FALSE);
4715   
4716   return TRUE;
4717 }
4718
4719 typedef struct {
4720   GFile *file;
4721   GError *error;
4722   GCancellable *cancellable;
4723   GFileReadMoreCallback read_more_callback;
4724   GAsyncReadyCallback callback;
4725   gpointer user_data;
4726   GByteArray *content;
4727   gsize pos;
4728   char *etag;
4729 } LoadContentsData;
4730
4731
4732 static void
4733 load_contents_data_free (LoadContentsData *data)
4734 {
4735   if (data->error)
4736     g_error_free (data->error);
4737   if (data->cancellable)
4738     g_object_unref (data->cancellable);
4739   if (data->content)
4740     g_byte_array_free (data->content, TRUE);
4741   g_free (data->etag);
4742   g_object_unref (data->file);
4743   g_free (data);
4744 }
4745
4746 static void
4747 load_contents_close_callback (GObject      *obj,
4748                               GAsyncResult *close_res,
4749                               gpointer      user_data)
4750 {
4751   GInputStream *stream = G_INPUT_STREAM (obj);
4752   LoadContentsData *data = user_data;
4753   GSimpleAsyncResult *res;
4754
4755   /* Ignore errors here, we're only reading anyway */
4756   g_input_stream_close_finish (stream, close_res, NULL);
4757   g_object_unref (stream);
4758
4759   res = g_simple_async_result_new (G_OBJECT (data->file),
4760                                    data->callback,
4761                                    data->user_data,
4762                                    g_file_load_contents_async);
4763   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4764   g_simple_async_result_complete (res);
4765   g_object_unref (res);
4766 }
4767
4768 static void
4769 load_contents_fstat_callback (GObject      *obj,
4770                               GAsyncResult *stat_res,
4771                               gpointer      user_data)
4772 {
4773   GInputStream *stream = G_INPUT_STREAM (obj);
4774   LoadContentsData *data = user_data;
4775   GFileInfo *info;
4776
4777   info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4778                                                    stat_res, NULL);
4779   if (info)
4780     {
4781       data->etag = g_strdup (g_file_info_get_etag (info));
4782       g_object_unref (info);
4783     }
4784
4785   g_input_stream_close_async (stream, 0,
4786                               data->cancellable,
4787                               load_contents_close_callback, data);
4788 }
4789
4790 static void
4791 load_contents_read_callback (GObject      *obj,
4792                              GAsyncResult *read_res,
4793                              gpointer      user_data)
4794 {
4795   GInputStream *stream = G_INPUT_STREAM (obj);
4796   LoadContentsData *data = user_data;
4797   GError *error = NULL;
4798   gssize read_size;
4799
4800   read_size = g_input_stream_read_finish (stream, read_res, &error);
4801
4802   if (read_size < 0) 
4803     {
4804       /* Error or EOF, close the file */
4805       data->error = error;
4806       g_input_stream_close_async (stream, 0,
4807                                   data->cancellable,
4808                                   load_contents_close_callback, data);
4809     }
4810   else if (read_size == 0)
4811     {
4812       g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4813                                             G_FILE_ATTRIBUTE_ETAG_VALUE,
4814                                             0,
4815                                             data->cancellable,
4816                                             load_contents_fstat_callback,
4817                                             data);
4818     }
4819   else if (read_size > 0)
4820     {
4821       data->pos += read_size;
4822       
4823       g_byte_array_set_size (data->content,
4824                              data->pos + GET_CONTENT_BLOCK_SIZE);
4825
4826
4827       if (data->read_more_callback &&
4828           !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4829         g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4830                                               G_FILE_ATTRIBUTE_ETAG_VALUE,
4831                                               0,
4832                                               data->cancellable,
4833                                               load_contents_fstat_callback,
4834                                               data);
4835       else 
4836         g_input_stream_read_async (stream,
4837                                    data->content->data + data->pos,
4838                                    GET_CONTENT_BLOCK_SIZE,
4839                                    0,
4840                                    data->cancellable,
4841                                    load_contents_read_callback,
4842                                    data);
4843     }
4844 }
4845
4846 static void
4847 load_contents_open_callback (GObject      *obj,
4848                              GAsyncResult *open_res,
4849                              gpointer      user_data)
4850 {
4851   GFile *file = G_FILE (obj);
4852   GFileInputStream *stream;
4853   LoadContentsData *data = user_data;
4854   GError *error = NULL;
4855   GSimpleAsyncResult *res;
4856
4857   stream = g_file_read_finish (file, open_res, &error);
4858
4859   if (stream)
4860     {
4861       g_byte_array_set_size (data->content,
4862                              data->pos + GET_CONTENT_BLOCK_SIZE);
4863       g_input_stream_read_async (G_INPUT_STREAM (stream),
4864                                  data->content->data + data->pos,
4865                                  GET_CONTENT_BLOCK_SIZE,
4866                                  0,
4867                                  data->cancellable,
4868                                  load_contents_read_callback,
4869                                  data);
4870       
4871     }
4872   else
4873     {
4874       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4875                                                   data->callback,
4876                                                   data->user_data,
4877                                                   error);
4878       g_simple_async_result_complete (res);
4879       g_error_free (error);
4880       load_contents_data_free (data);
4881       g_object_unref (res);
4882     }
4883 }
4884
4885 /**
4886  * g_file_load_partial_contents_async:
4887  * @file: input #GFile.
4888  * @cancellable: optional #GCancellable object, %NULL to ignore.
4889  * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
4890  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4891  * @user_data: the data to pass to the callback functions.
4892  *
4893  * Reads the partial contents of a file. A #GFileReadMoreCallback should be 
4894  * used to stop reading from the file when appropriate, else this function
4895  * will behave exactly as g_file_load_contents_async(). This operation 
4896  * can be finished by g_file_load_partial_contents_finish().
4897  *
4898  * Users of this function should be aware that @user_data is passed to 
4899  * both the @read_more_callback and the @callback.
4900  *
4901  * If @cancellable is not %NULL, then the operation can be cancelled by
4902  * triggering the cancellable object from another thread. If the operation
4903  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4904  **/
4905 void
4906 g_file_load_partial_contents_async (GFile                 *file,
4907                                     GCancellable          *cancellable,
4908                                     GFileReadMoreCallback  read_more_callback,
4909                                     GAsyncReadyCallback    callback,
4910                                     gpointer               user_data)
4911 {
4912   LoadContentsData *data;
4913
4914   g_return_if_fail (G_IS_FILE (file));
4915
4916   data = g_new0 (LoadContentsData, 1);
4917
4918   if (cancellable)
4919     data->cancellable = g_object_ref (cancellable);
4920   data->read_more_callback = read_more_callback;
4921   data->callback = callback;
4922   data->user_data = user_data;
4923   data->content = g_byte_array_new ();
4924   data->file = g_object_ref (file);
4925
4926   g_file_read_async (file,
4927                      0,
4928                      cancellable,
4929                      load_contents_open_callback,
4930                      data);
4931 }
4932
4933 /**
4934  * g_file_load_partial_contents_finish:
4935  * @file: input #GFile.
4936  * @res: a #GAsyncResult. 
4937  * @contents: a location to place the contents of the file.
4938  * @length: a location to place the length of the contents of the file.
4939  * @etag_out: a location to place the current entity tag for the file.
4940  * @error: a #GError, or %NULL
4941  * 
4942  * Finishes an asynchronous partial load operation that was started
4943  * with g_file_load_partial_contents_async().
4944  *
4945  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
4946  * present, it will be set appropriately. 
4947  **/
4948 gboolean
4949 g_file_load_partial_contents_finish (GFile         *file,
4950                                      GAsyncResult  *res,
4951                                      char         **contents,
4952                                      gsize         *length,
4953                                      char         **etag_out,
4954                                      GError       **error)
4955 {
4956   GSimpleAsyncResult *simple;
4957   LoadContentsData *data;
4958
4959   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4960   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4961   g_return_val_if_fail (contents != NULL, FALSE);
4962
4963   simple = G_SIMPLE_ASYNC_RESULT (res);
4964
4965   if (g_simple_async_result_propagate_error (simple, error))
4966     return FALSE;
4967   
4968   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4969   
4970   data = g_simple_async_result_get_op_res_gpointer (simple);
4971
4972   if (data->error)
4973     {
4974       g_propagate_error (error, data->error);
4975       data->error = NULL;
4976       *contents = NULL;
4977       if (length)
4978         *length = 0;
4979       return FALSE;
4980     }
4981
4982   if (length)
4983     *length = data->pos;
4984
4985   if (etag_out)
4986     {
4987       *etag_out = data->etag;
4988       data->etag = NULL;
4989     }
4990
4991   /* Zero terminate */
4992   g_byte_array_set_size (data->content, data->pos + 1);
4993   data->content->data[data->pos] = 0;
4994   
4995   *contents = (char *)g_byte_array_free (data->content, FALSE);
4996   data->content = NULL;
4997
4998   return TRUE;
4999 }
5000
5001 /**
5002  * g_file_load_contents_async:
5003  * @file: input #GFile.
5004  * @cancellable: optional #GCancellable object, %NULL to ignore.
5005  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5006  * @user_data: the data to pass to callback function
5007  * 
5008  * Starts an asynchronous load of the @file's contents.
5009  *
5010  * For more details, see g_file_load_contents() which is
5011  * the synchronous version of this call.
5012  *
5013  * When the load operation has completed, @callback will be called 
5014  * with @user data. To finish the operation, call 
5015  * g_file_load_contents_finish() with the #GAsyncResult returned by 
5016  * the @callback.
5017  * 
5018  * If @cancellable is not %NULL, then the operation can be cancelled by
5019  * triggering the cancellable object from another thread. If the operation
5020  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5021  **/
5022 void
5023 g_file_load_contents_async (GFile               *file,
5024                            GCancellable        *cancellable,
5025                            GAsyncReadyCallback  callback,
5026                            gpointer             user_data)
5027 {
5028   g_file_load_partial_contents_async (file,
5029                                       cancellable,
5030                                       NULL,
5031                                       callback, user_data);
5032 }
5033
5034 /**
5035  * g_file_load_contents_finish:
5036  * @file: input #GFile.
5037  * @res: a #GAsyncResult. 
5038  * @contents: a location to place the contents of the file.
5039  * @length: a location to place the length of the contents of the file.
5040  * @etag_out: a location to place the current entity tag for the file.
5041  * @error: a #GError, or %NULL
5042  * 
5043  * Finishes an asynchronous load of the @file's contents. 
5044  * The contents are placed in @contents, and @length is set to the 
5045  * size of the @contents string. If @etag_out is present, it will be 
5046  * set to the new entity tag for the @file.
5047  * 
5048  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
5049  * present, it will be set appropriately. 
5050  **/
5051 gboolean
5052 g_file_load_contents_finish (GFile         *file,
5053                              GAsyncResult  *res,
5054                              char         **contents,
5055                              gsize         *length,
5056                              char         **etag_out,
5057                              GError       **error)
5058 {
5059   return g_file_load_partial_contents_finish (file,
5060                                               res,
5061                                               contents,
5062                                               length,
5063                                               etag_out,
5064                                               error);
5065 }
5066   
5067 /**
5068  * g_file_replace_contents:
5069  * @file: input #GFile.
5070  * @contents: a string containing the new contents for @file.
5071  * @length: the length of @contents in bytes.
5072  * @etag: the old <link linkend="gfile-etag">entity tag</link> 
5073  *     for the document.
5074  * @make_backup: %TRUE if a backup should be created.
5075  * @flags: a set of #GFileCreateFlags.
5076  * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5077  *      for the document. This should be freed with g_free() when no longer 
5078  *      needed.
5079  * @cancellable: optional #GCancellable object, %NULL to ignore.
5080  * @error: a #GError, or %NULL
5081  *
5082  * Replaces the contents of @file with @contents of @length bytes.
5083  
5084  * If @etag is specified (not %NULL) any existing file must have that etag, or
5085  * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5086  *
5087  * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5088  * 
5089  * If @cancellable is not %NULL, then the operation can be cancelled by
5090  * triggering the cancellable object from another thread. If the operation
5091  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5092  *
5093  * The returned @new_etag can be used to verify that the file hasn't changed the
5094  * next time it is saved over.
5095  * 
5096  * Returns: %TRUE if successful. If an error
5097  * has occurred, this function will return %FALSE and set @error
5098  * appropriately if present.
5099  **/
5100 gboolean
5101 g_file_replace_contents (GFile             *file,
5102                          const char        *contents,
5103                          gsize              length,
5104                          const char        *etag,
5105                          gboolean           make_backup,
5106                          GFileCreateFlags   flags,
5107                          char             **new_etag,
5108                          GCancellable      *cancellable,
5109                          GError           **error)
5110 {
5111   GFileOutputStream *out;
5112   gsize pos, remainder;
5113   gssize res;
5114
5115   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5116   g_return_val_if_fail (contents != NULL, FALSE);
5117
5118   out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5119   if (out == NULL)
5120     return FALSE;
5121
5122   pos = 0;
5123   remainder = length;
5124   while (remainder > 0 &&
5125          (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5126                                        contents + pos,
5127                                        MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5128                                        cancellable,
5129                                        error)) > 0)
5130     {
5131       pos += res;
5132       remainder -= res;
5133     }
5134   
5135   if (remainder > 0 && res < 0)
5136     {
5137       /* Ignore errors on close */
5138       g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5139       
5140       /* error is set already */
5141       return FALSE;
5142     }
5143   
5144   if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5145     return FALSE;
5146
5147   if (new_etag)
5148     *new_etag = g_file_output_stream_get_etag (out);
5149   
5150   return TRUE;
5151 }
5152
5153 typedef struct {
5154   GFile *file;
5155   GError *error;
5156   GCancellable *cancellable;
5157   GAsyncReadyCallback callback;
5158   gpointer user_data;
5159   const char *content;
5160   gsize length;
5161   gsize pos;
5162   char *etag;
5163 } ReplaceContentsData;
5164
5165 static void
5166 replace_contents_data_free (ReplaceContentsData *data)
5167 {
5168   if (data->error)
5169     g_error_free (data->error);
5170   if (data->cancellable)
5171     g_object_unref (data->cancellable);
5172   g_object_unref (data->file);
5173   g_free (data->etag);
5174   g_free (data);
5175 }
5176
5177 static void
5178 replace_contents_close_callback (GObject      *obj,
5179                                  GAsyncResult *close_res,
5180                                  gpointer      user_data)
5181 {
5182   GOutputStream *stream = G_OUTPUT_STREAM (obj);
5183   ReplaceContentsData *data = user_data;
5184   GSimpleAsyncResult *res;
5185
5186   /* Ignore errors here, we're only reading anyway */
5187   g_output_stream_close_finish (stream, close_res, NULL);
5188   g_object_unref (stream);
5189
5190   data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5191   
5192   res = g_simple_async_result_new (G_OBJECT (data->file),
5193                                    data->callback,
5194                                    data->user_data,
5195                                    g_file_replace_contents_async);
5196   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5197   g_simple_async_result_complete (res);
5198   g_object_unref (res);
5199 }
5200
5201 static void
5202 replace_contents_write_callback (GObject      *obj,
5203                                  GAsyncResult *read_res,
5204                                  gpointer      user_data)
5205 {
5206   GOutputStream *stream = G_OUTPUT_STREAM (obj);
5207   ReplaceContentsData *data = user_data;
5208   GError *error = NULL;
5209   gssize write_size;
5210   
5211   write_size = g_output_stream_write_finish (stream, read_res, &error);
5212
5213   if (write_size <= 0) 
5214     {
5215       /* Error or EOF, close the file */
5216       if (write_size < 0)
5217         data->error = error;
5218       g_output_stream_close_async (stream, 0,
5219                                    data->cancellable,
5220                                    replace_contents_close_callback, data);
5221     }
5222   else if (write_size > 0)
5223     {
5224       data->pos += write_size;
5225
5226       if (data->pos >= data->length)
5227         g_output_stream_close_async (stream, 0,
5228                                      data->cancellable,
5229                                      replace_contents_close_callback, data);
5230       else
5231         g_output_stream_write_async (stream,
5232                                      data->content + data->pos,
5233                                      data->length - data->pos,
5234                                      0,
5235                                      data->cancellable,
5236                                      replace_contents_write_callback,
5237                                      data);
5238     }
5239 }
5240
5241 static void
5242 replace_contents_open_callback (GObject      *obj,
5243                                 GAsyncResult *open_res,
5244                                 gpointer      user_data)
5245 {
5246   GFile *file = G_FILE (obj);
5247   GFileOutputStream *stream;
5248   ReplaceContentsData *data = user_data;
5249   GError *error = NULL;
5250   GSimpleAsyncResult *res;
5251
5252   stream = g_file_replace_finish (file, open_res, &error);
5253
5254   if (stream)
5255     {
5256       g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5257                                    data->content + data->pos,
5258                                    data->length - data->pos,
5259                                    0,
5260                                    data->cancellable,
5261                                    replace_contents_write_callback,
5262                                    data);
5263       
5264     }
5265   else
5266     {
5267       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5268                                                   data->callback,
5269                                                   data->user_data,
5270                                                   error);
5271       g_simple_async_result_complete (res);
5272       g_error_free (error);
5273       replace_contents_data_free (data);
5274       g_object_unref (res);
5275     }
5276 }
5277
5278 /**
5279  * g_file_replace_contents_async:
5280  * @file: input #GFile.
5281  * @contents: string of contents to replace the file with.
5282  * @length: the length of @contents in bytes.
5283  * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5284  * @make_backup: %TRUE if a backup should be created.
5285  * @flags: a set of #GFileCreateFlags.
5286  * @cancellable: optional #GCancellable object, %NULL to ignore.
5287  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5288  * @user_data: the data to pass to callback function
5289  * 
5290  * Starts an asynchronous replacement of @file with the given 
5291  * @contents of @length bytes. @etag will replace the document's 
5292  * current entity tag.
5293  * 
5294  * When this operation has completed, @callback will be called with
5295  * @user_user data, and the operation can be finalized with 
5296  * g_file_replace_contents_finish().
5297  * 
5298  * If @cancellable is not %NULL, then the operation can be cancelled by
5299  * triggering the cancellable object from another thread. If the operation
5300  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5301  * 
5302  * If @make_backup is %TRUE, this function will attempt to 
5303  * make a backup of @file.
5304  **/
5305 void
5306 g_file_replace_contents_async  (GFile               *file,
5307                                 const char          *contents,
5308                                 gsize                length,
5309                                 const char          *etag,
5310                                 gboolean             make_backup,
5311                                 GFileCreateFlags     flags,
5312                                 GCancellable        *cancellable,
5313                                 GAsyncReadyCallback  callback,
5314                                 gpointer             user_data)
5315 {
5316   ReplaceContentsData *data;
5317
5318   g_return_if_fail (G_IS_FILE (file));
5319   g_return_if_fail (contents != NULL);
5320
5321   data = g_new0 (ReplaceContentsData, 1);
5322
5323   if (cancellable)
5324     data->cancellable = g_object_ref (cancellable);
5325   data->callback = callback;
5326   data->user_data = user_data;
5327   data->content = contents;
5328   data->length = length;
5329   data->pos = 0;
5330   data->file = g_object_ref (file);
5331
5332   g_file_replace_async (file,
5333                         etag,
5334                         make_backup,
5335                         flags,
5336                         0,
5337                         cancellable,
5338                         replace_contents_open_callback,
5339                         data);
5340 }
5341   
5342 /**
5343  * g_file_replace_contents_finish:
5344  * @file: input #GFile.
5345  * @res: a #GAsyncResult. 
5346  * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link> 
5347  *     for the document. This should be freed with g_free() when it is no 
5348  *     longer needed.
5349  * @error: a #GError, or %NULL 
5350  * 
5351  * Finishes an asynchronous replace of the given @file. See
5352  * g_file_replace_contents_async(). Sets @new_etag to the new entity 
5353  * tag for the document, if present.
5354  * 
5355  * Returns: %TRUE on success, %FALSE on failure.
5356  **/
5357 gboolean
5358 g_file_replace_contents_finish (GFile         *file,
5359                                 GAsyncResult  *res,
5360                                 char         **new_etag,
5361                                 GError       **error)
5362 {
5363   GSimpleAsyncResult *simple;
5364   ReplaceContentsData *data;
5365
5366   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5367   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5368
5369   simple = G_SIMPLE_ASYNC_RESULT (res);
5370
5371   if (g_simple_async_result_propagate_error (simple, error))
5372     return FALSE;
5373   
5374   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5375   
5376   data = g_simple_async_result_get_op_res_gpointer (simple);
5377
5378   if (data->error)
5379     {
5380       g_propagate_error (error, data->error);
5381       data->error = NULL;
5382       return FALSE;
5383     }
5384
5385
5386   if (new_etag)
5387     {
5388       *new_etag = data->etag;
5389       data->etag = NULL; /* Take ownership */
5390     }
5391   
5392   return TRUE;
5393 }
5394
5395 #define __G_FILE_C__
5396 #include "gioaliasdef.c"