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