Allow calls to implementation of copy and write even if the type of the
[platform/upstream/glib.git] / gio / gfile.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #ifdef HAVE_PWD_H
27 #include <pwd.h>
28 #endif
29 #include "gfile.h"
30 #include "gvfs.h"
31 #include "gioscheduler.h"
32 #include <glocalfile.h>
33 #include "gsimpleasyncresult.h"
34 #include "gfileattribute-priv.h"
35 #include "gpollfilemonitor.h"
36 #include "glibintl.h"
37
38 #include "gioalias.h"
39
40 /**
41  * SECTION:gfile
42  * @short_description: File and Directory Handling
43  * @include: gio.h
44  * @see_also: #GFileInfo, #GFileEnumerator
45  * 
46  * #GFile is a high level abstraction for manipulating files on a 
47  * virtual file system. #GFile<!-- -->s are lightweight, immutable 
48  * objects that do no I/O upon creation. It is necessary to understand that
49  * #GFile objects do not represent files, merely a handle to a file. All
50  * file I/O is implemented as streaming operations (see #GInputStream and 
51  * #GOutputStream).
52  * 
53  * To construct a #GFile, you can use: 
54  * g_file_new_for_path() if you have a path.
55  * g_file_new_for_uri() if you have a URI.
56  * g_file_new_for_commandline_arg() for a command line argument.
57  * 
58  * You can move through the file system with #GFile handles with
59  * g_file_get_parent() to get a handle to the parent directory.
60  * g_file_get_child() to get a handle to a child within a directory.
61  * g_file_resolve_relative_path() to resolve a relative path between
62  * two #GFile<!-- -->s.
63  * 
64  * Many #GFile operations have both synchronous and asynchronous versions 
65  * to suit your application. Asynchronous versions of synchronous functions 
66  * simply have _async() appended to their function names. The asynchronous 
67  * I/O functions call a #GAsyncReadyCallback which is then used to finalize 
68  * the operation, producing a GAsyncResult which is then passed to the 
69  * function's matching _finish() 
70  * operation. 
71  *
72  * Some #GFile operations do not have synchronous analogs, as they may
73  * take a very long time to finish, and blocking may leave an application
74  * unusable. Notable cases include:
75  * g_file_mount_mountable() to mount a mountable file.
76  * g_file_unmount_mountable() to unmount a mountable file.
77  * g_file_eject_mountable() to eject a mountable file.
78  * 
79  * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
80  * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for 
81  * short. Entity tags are somewhat like a more abstract version of the 
82  * traditional mtime, and can be used to quickly determine if the file has
83  * been modified from the version on the file system. See the HTTP 1.1 
84  * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
85  * for HTTP Etag headers, which are a very similar concept.
86  * </para>
87  **/
88
89 static void g_file_base_init (gpointer g_class);
90 static void g_file_class_init (gpointer g_class,
91                                gpointer class_data);
92
93 static void               g_file_real_query_info_async          (GFile                *file,
94                                                                  const char           *attributes,
95                                                                  GFileQueryInfoFlags   flags,
96                                                                  int                   io_priority,
97                                                                  GCancellable         *cancellable,
98                                                                  GAsyncReadyCallback   callback,
99                                                                  gpointer              user_data);
100 static GFileInfo *        g_file_real_query_info_finish         (GFile                *file,
101                                                                  GAsyncResult         *res,
102                                                                  GError              **error);
103 static void               g_file_real_enumerate_children_async  (GFile                *file,
104                                                                  const char           *attributes,
105                                                                  GFileQueryInfoFlags   flags,
106                                                                  int                   io_priority,
107                                                                  GCancellable         *cancellable,
108                                                                  GAsyncReadyCallback   callback,
109                                                                  gpointer              user_data);
110 static GFileEnumerator *  g_file_real_enumerate_children_finish (GFile                *file,
111                                                                  GAsyncResult         *res,
112                                                                  GError              **error);
113 static void               g_file_real_read_async                (GFile                *file,
114                                                                  int                   io_priority,
115                                                                  GCancellable         *cancellable,
116                                                                  GAsyncReadyCallback   callback,
117                                                                  gpointer              user_data);
118 static GFileInputStream * g_file_real_read_finish               (GFile                *file,
119                                                                  GAsyncResult         *res,
120                                                                  GError              **error);
121 static void               g_file_real_append_to_async           (GFile                *file,
122                                                                  GFileCreateFlags      flags,
123                                                                  int                   io_priority,
124                                                                  GCancellable         *cancellable,
125                                                                  GAsyncReadyCallback   callback,
126                                                                  gpointer              user_data);
127 static GFileOutputStream *g_file_real_append_to_finish          (GFile                *file,
128                                                                  GAsyncResult         *res,
129                                                                  GError              **error);
130 static void               g_file_real_create_async              (GFile                *file,
131                                                                  GFileCreateFlags      flags,
132                                                                  int                   io_priority,
133                                                                  GCancellable         *cancellable,
134                                                                  GAsyncReadyCallback   callback,
135                                                                  gpointer              user_data);
136 static GFileOutputStream *g_file_real_create_finish             (GFile                *file,
137                                                                  GAsyncResult         *res,
138                                                                  GError              **error);
139 static void               g_file_real_replace_async             (GFile                *file,
140                                                                  const char           *etag,
141                                                                  gboolean              make_backup,
142                                                                  GFileCreateFlags      flags,
143                                                                  int                   io_priority,
144                                                                  GCancellable         *cancellable,
145                                                                  GAsyncReadyCallback   callback,
146                                                                  gpointer              user_data);
147 static GFileOutputStream *g_file_real_replace_finish            (GFile                *file,
148                                                                  GAsyncResult         *res,
149                                                                  GError              **error);
150 static gboolean           g_file_real_set_attributes_from_info  (GFile                *file,
151                                                                  GFileInfo            *info,
152                                                                  GFileQueryInfoFlags   flags,
153                                                                  GCancellable         *cancellable,
154                                                                  GError              **error);
155 static void               g_file_real_set_display_name_async    (GFile                *file,
156                                                                  const char           *display_name,
157                                                                  int                   io_priority,
158                                                                  GCancellable         *cancellable,
159                                                                  GAsyncReadyCallback   callback,
160                                                                  gpointer              user_data);
161 static GFile *            g_file_real_set_display_name_finish   (GFile                *file,
162                                                                  GAsyncResult         *res,
163                                                                  GError              **error);
164 static void               g_file_real_set_attributes_async      (GFile                *file,
165                                                                  GFileInfo            *info,
166                                                                  GFileQueryInfoFlags   flags,
167                                                                  int                   io_priority,
168                                                                  GCancellable         *cancellable,
169                                                                  GAsyncReadyCallback   callback,
170                                                                  gpointer              user_data);
171 static gboolean           g_file_real_set_attributes_finish     (GFile                *file,
172                                                                  GAsyncResult         *res,
173                                                                  GFileInfo           **info,
174                                                                  GError              **error);
175
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 it's 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 it's 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 it's 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   iface = G_FILE_GET_IFACE (destination);
2119   if (iface->copy)
2120     {
2121       my_error = NULL;
2122       res = (* iface->copy) (source, destination,
2123                              flags, cancellable,
2124                              progress_callback, progress_callback_data,
2125                              &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   /* If the types are different, and the destination method failed
2138      also try the source method */
2139   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2140     {
2141       iface = G_FILE_GET_IFACE (source);
2142       
2143       if (iface->copy)
2144         {
2145           my_error = NULL;
2146           res = (* iface->copy) (source, destination,
2147                                  flags, cancellable,
2148                                  progress_callback, progress_callback_data,
2149                                  &my_error);
2150           
2151           if (res)
2152             return TRUE;
2153           
2154           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2155             {
2156               g_propagate_error (error, my_error);
2157               return FALSE;
2158             }
2159         }
2160     }
2161   
2162   return file_copy_fallback (source, destination, flags, cancellable,
2163                              progress_callback, progress_callback_data,
2164                              error);
2165 }
2166
2167
2168 /**
2169  * g_file_move:
2170  * @source: #GFile pointing to the source location.
2171  * @destination: #GFile pointing to the destination location.
2172  * @flags: set of #GFileCopyFlags.
2173  * @cancellable: optional #GCancellable object, %NULL to ignore.
2174  * @progress_callback: #GFileProgressCallback function for updates.
2175  * @progress_callback_data: gpointer to user data for the callback function.
2176  * @error: #GError for returning error conditions, or %NULL
2177  *
2178  *
2179  * Tries to move the file or directory @source to the location specified by @destination.
2180  * If native move operations is supported then this is used, otherwise a copy + delete
2181  * fallback is used. The native implementation may support moving directories (for instance
2182  * on moves inside the same filesystem), but the fallback code does not.
2183  * 
2184  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2185  * existing @destination file is overwritten.
2186  *
2187  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2188  * will be copied as symlinks, otherwise the target of the
2189  * @source symlink will be copied.
2190  *
2191  * If @cancellable is not %NULL, then the operation can be cancelled by
2192  * triggering the cancellable object from another thread. If the operation
2193  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2194  * 
2195  * If @progress_callback is not %NULL, then the operation can be monitored by
2196  * setting this to a #GFileProgressCallback function. @progress_callback_data
2197  * will be passed to this function. It is guaranteed that this callback will
2198  * be called after all data has been transferred with the total number of bytes
2199  * copied during the operation.
2200  * 
2201  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2202  * error is returned, independent on the status of the @destination.
2203  *
2204  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2205  * error G_IO_ERROR_EXISTS is returned.
2206  *
2207  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2208  * error is returned. If trying to overwrite a directory with a directory the
2209  * G_IO_ERROR_WOULD_MERGE error is returned.
2210  *
2211  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2212  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2213  * may be returned (if the native move operation isn't available).
2214  *
2215  * Returns: %TRUE on successful move, %FALSE otherwise.
2216  **/
2217 gboolean
2218 g_file_move (GFile                  *source,
2219              GFile                  *destination,
2220              GFileCopyFlags          flags,
2221              GCancellable           *cancellable,
2222              GFileProgressCallback   progress_callback,
2223              gpointer                progress_callback_data,
2224              GError                **error)
2225 {
2226   GFileIface *iface;
2227   GError *my_error;
2228   gboolean res;
2229
2230   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2231   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2232
2233   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2234     return FALSE;
2235   
2236   iface = G_FILE_GET_IFACE (destination);
2237   if (iface->move)
2238     {
2239       my_error = NULL;
2240       res = (* iface->move) (source, destination,
2241                              flags, cancellable,
2242                              progress_callback, progress_callback_data,
2243                              &my_error);
2244       
2245       if (res)
2246         return TRUE;
2247       
2248       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2249         {
2250           g_propagate_error (error, my_error);
2251           return FALSE;
2252         }
2253     }
2254
2255   /* If the types are different, and the destination method failed
2256      also try the source method */
2257   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2258     {
2259       iface = G_FILE_GET_IFACE (source);
2260       
2261       if (iface->move)
2262         {
2263           my_error = NULL;
2264           res = (* iface->move) (source, destination,
2265                                  flags, cancellable,
2266                                  progress_callback, progress_callback_data,
2267                                  &my_error);
2268           
2269           if (res)
2270             return TRUE;
2271           
2272           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2273             {
2274               g_propagate_error (error, my_error);
2275               return FALSE;
2276             }
2277         }
2278     }
2279   
2280   if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2281     {  
2282       g_set_error (error, G_IO_ERROR,
2283                    G_IO_ERROR_NOT_SUPPORTED,
2284                    _("Operation not supported"));
2285       return FALSE;
2286     }
2287   
2288   flags |= G_FILE_COPY_ALL_METADATA;
2289   if (!g_file_copy (source, destination, flags, cancellable,
2290                     progress_callback, progress_callback_data,
2291                     error))
2292     return FALSE;
2293
2294   return g_file_delete (source, cancellable, error);
2295 }
2296
2297 /**
2298  * g_file_make_directory
2299  * @file: input #GFile.
2300  * @cancellable: optional #GCancellable object, %NULL to ignore.
2301  * @error: a #GError, or %NULL 
2302  *
2303  * Creates a directory.
2304  * 
2305  * If @cancellable is not %NULL, then the operation can be cancelled by
2306  * triggering the cancellable object from another thread. If the operation
2307  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2308  * 
2309  * Returns: %TRUE on successful creation, %FALSE otherwise.
2310  **/
2311 gboolean
2312 g_file_make_directory (GFile         *file,
2313                        GCancellable  *cancellable,
2314                        GError       **error)
2315 {
2316   GFileIface *iface;
2317
2318   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2319
2320   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2321     return FALSE;
2322   
2323   iface = G_FILE_GET_IFACE (file);
2324
2325   if (iface->make_directory == NULL)
2326     {
2327       g_set_error (error, G_IO_ERROR,
2328                    G_IO_ERROR_NOT_SUPPORTED,
2329                    _("Operation not supported"));
2330       return FALSE;
2331     }
2332   
2333   return (* iface->make_directory) (file, cancellable, error);
2334 }
2335
2336 /**
2337  * g_file_make_symbolic_link:
2338  * @file: input #GFile.
2339  * @symlink_value: a string with the value of the new symlink.
2340  * @cancellable: optional #GCancellable object, %NULL to ignore.
2341  * @error: a #GError. 
2342  * 
2343  * Creates a symbolic link.
2344  *
2345  * If @cancellable is not %NULL, then the operation can be cancelled by
2346  * triggering the cancellable object from another thread. If the operation
2347  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2348  * 
2349  * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2350  **/
2351 gboolean
2352 g_file_make_symbolic_link (GFile         *file,
2353                            const char    *symlink_value,
2354                            GCancellable  *cancellable,
2355                            GError       **error)
2356 {
2357   GFileIface *iface;
2358
2359   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2360   g_return_val_if_fail (symlink_value != NULL, FALSE);
2361
2362   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2363     return FALSE;
2364
2365   if (*symlink_value == '\0')
2366     {
2367       g_set_error (error, G_IO_ERROR,
2368                    G_IO_ERROR_INVALID_ARGUMENT,
2369                    _("Invalid symlink value given"));
2370       return FALSE;
2371     }
2372   
2373   iface = G_FILE_GET_IFACE (file);
2374
2375   if (iface->make_symbolic_link == NULL)
2376     {
2377       g_set_error (error, G_IO_ERROR,
2378                    G_IO_ERROR_NOT_SUPPORTED,
2379                    _("Operation not supported"));
2380       return FALSE;
2381     }
2382   
2383   return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2384 }
2385
2386 /**
2387  * g_file_delete:
2388  * @file: input #GFile.
2389  * @cancellable: optional #GCancellable object, %NULL to ignore.
2390  * @error: a #GError, or %NULL 
2391  * 
2392  * Deletes a file.
2393  * 
2394  * If @cancellable is not %NULL, then the operation can be cancelled by
2395  * triggering the cancellable object from another thread. If the operation
2396  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2397  * 
2398  * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2399  **/
2400 gboolean
2401 g_file_delete (GFile         *file,
2402                GCancellable  *cancellable,
2403                GError       **error)
2404 {
2405   GFileIface *iface;
2406   
2407   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2408
2409   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2410     return FALSE;
2411   
2412   iface = G_FILE_GET_IFACE (file);
2413
2414   if (iface->delete_file == NULL)
2415     {
2416       g_set_error (error, G_IO_ERROR,
2417                    G_IO_ERROR_NOT_SUPPORTED,
2418                    _("Operation not supported"));
2419       return FALSE;
2420     }
2421   
2422   return (* iface->delete_file) (file, cancellable, error);
2423 }
2424
2425 /**
2426  * g_file_trash:
2427  * @file: #GFile to send to trash.
2428  * @cancellable: optional #GCancellable object, %NULL to ignore.
2429  * @error: a #GError, or %NULL
2430  *
2431  * Sends @file to the "Trashcan", if possible. This is similar to
2432  * deleting it, but the user can recover it before emptying the trashcan.
2433  * Not all file systems support trashing, so this call can return the
2434  * %G_IO_ERROR_NOT_SUPPORTED error.
2435  *
2436  *
2437  * If @cancellable is not %NULL, then the operation can be cancelled by
2438  * triggering the cancellable object from another thread. If the operation
2439  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2440  * 
2441  * Returns: %TRUE on successful trash, %FALSE otherwise.
2442  **/
2443 gboolean
2444 g_file_trash (GFile         *file,
2445               GCancellable  *cancellable,
2446               GError       **error)
2447 {
2448   GFileIface *iface;
2449   
2450   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2451
2452   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2453     return FALSE;
2454   
2455   iface = G_FILE_GET_IFACE (file);
2456
2457   if (iface->trash == NULL)
2458     {
2459       g_set_error (error,
2460                    G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2461                    _("Trash not supported"));
2462       return FALSE;
2463     }
2464   
2465   return (* iface->trash) (file, cancellable, error);
2466 }
2467
2468 /**
2469  * g_file_set_display_name:
2470  * @file: input #GFile.
2471  * @display_name: a string.
2472  * @cancellable: optional #GCancellable object, %NULL to ignore.
2473  * @error: a #GError, or %NULL
2474  * 
2475  * Renames @file to the specified display name.
2476  *
2477  * The display name is converted from UTF8 to the correct encoding for the target
2478  * filesystem if possible and the @file is renamed to this.
2479  * 
2480  * If you want to implement a rename operation in the user interface the edit name
2481  * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2482  * widget, and then the result after editing should be passed to g_file_set_display_name().
2483  *
2484  * On success the resulting converted filename is returned.
2485  * 
2486  * If @cancellable is not %NULL, then the operation can be cancelled by
2487  * triggering the cancellable object from another thread. If the operation
2488  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2489  * 
2490  * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2491  **/
2492 GFile *
2493 g_file_set_display_name (GFile         *file,
2494                          const char    *display_name,
2495                          GCancellable  *cancellable,
2496                          GError       **error)
2497 {
2498   GFileIface *iface;
2499   
2500   g_return_val_if_fail (G_IS_FILE (file), NULL);
2501   g_return_val_if_fail (display_name != NULL, NULL);
2502
2503   if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2504     {
2505       g_set_error (error,
2506                    G_IO_ERROR,
2507                    G_IO_ERROR_INVALID_ARGUMENT,
2508                    _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2509       return NULL;
2510     }
2511   
2512   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2513     return NULL;
2514   
2515   iface = G_FILE_GET_IFACE (file);
2516
2517   return (* iface->set_display_name) (file, display_name, cancellable, error);
2518 }
2519
2520 /**
2521  * g_file_set_display_name_async:
2522  * @file: input #GFile.
2523  * @display_name: a string.
2524  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2525  *     of the request. 
2526  * @cancellable: optional #GCancellable object, %NULL to ignore.
2527  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2528  * @user_data: the data to pass to callback function
2529  * 
2530  * Asynchronously sets the display name for a given #GFile.
2531  * 
2532  * For more details, see g_set_display_name() which is
2533  * the synchronous version of this call.
2534  *
2535  * When the operation is finished, @callback will be called. You can then call
2536  * g_file_set_display_name_finish() to get the result of the operation.
2537  **/
2538 void
2539 g_file_set_display_name_async (GFile               *file,
2540                                const char          *display_name,
2541                                int                  io_priority,
2542                                GCancellable        *cancellable,
2543                                GAsyncReadyCallback  callback,
2544                                gpointer             user_data)
2545 {
2546   GFileIface *iface;
2547   
2548   g_return_if_fail (G_IS_FILE (file));
2549   g_return_if_fail (display_name != NULL);
2550
2551   iface = G_FILE_GET_IFACE (file);
2552   (* iface->set_display_name_async) (file,
2553                                      display_name,
2554                                      io_priority,
2555                                      cancellable,
2556                                      callback,
2557                                      user_data);
2558 }
2559
2560 /**
2561  * g_file_set_display_name_finish:
2562  * @file: input #GFile.
2563  * @res: a #GAsyncResult. 
2564  * @error: a #GError, or %NULL
2565  * 
2566  * Finishes setting a display name started with 
2567  * g_file_set_display_name_async().
2568  * 
2569  * Returns: a #GFile or %NULL on error.
2570  **/
2571 GFile *
2572 g_file_set_display_name_finish (GFile         *file,
2573                                 GAsyncResult  *res,
2574                                 GError       **error)
2575 {
2576   GFileIface *iface;
2577   
2578   g_return_val_if_fail (G_IS_FILE (file), NULL);
2579   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2580
2581   if (G_IS_SIMPLE_ASYNC_RESULT (res))
2582     {
2583       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2584       if (g_simple_async_result_propagate_error (simple, error))
2585         return NULL;
2586     }
2587   
2588   iface = G_FILE_GET_IFACE (file);
2589   return (* iface->set_display_name_finish) (file, res, error);
2590 }
2591
2592 /**
2593  * g_file_query_settable_attributes:
2594  * @file: input #GFile.
2595  * @cancellable: optional #GCancellable object, %NULL to ignore.
2596  * @error: a #GError, or %NULL
2597  * 
2598  * Obtain the list of settable attributes for the file.
2599  *
2600  * Returns the type and full attribute name of all the attributes 
2601  * that can be set on this file. This doesn't mean setting it will always 
2602  * succeed though, you might get an access failure, or some specific 
2603  * file may not support a specific attribute.
2604  *
2605  * If @cancellable is not %NULL, then the operation can be cancelled by
2606  * triggering the cancellable object from another thread. If the operation
2607  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2608  * 
2609  * Returns: a #GFileAttributeInfoList describing the settable attributes.
2610  * When you are done with it, release it with g_file_attribute_info_list_unref()
2611  **/
2612 GFileAttributeInfoList *
2613 g_file_query_settable_attributes (GFile         *file,
2614                                   GCancellable  *cancellable,
2615                                   GError       **error)
2616 {
2617   GFileIface *iface;
2618   GError *my_error;
2619   GFileAttributeInfoList *list;
2620
2621   g_return_val_if_fail (G_IS_FILE (file), NULL);
2622
2623   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2624     return NULL;
2625   
2626   iface = G_FILE_GET_IFACE (file);
2627
2628   if (iface->query_settable_attributes == NULL)
2629     return g_file_attribute_info_list_new ();
2630
2631   my_error = NULL;
2632   list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2633   
2634   if (list == NULL)
2635     {
2636       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2637         {
2638           list = g_file_attribute_info_list_new ();
2639           g_error_free (my_error);
2640         }
2641       else
2642         g_propagate_error (error, my_error);
2643     }
2644   
2645   return list;
2646 }
2647
2648 /**
2649  * g_file_query_writable_namespaces:
2650  * @file: input #GFile.
2651  * @cancellable: optional #GCancellable object, %NULL to ignore.
2652  * @error: a #GError, or %NULL
2653  * 
2654  * Obtain the list of attribute namespaces where new attributes 
2655  * can be created by a user. An example of this is extended
2656  * attributes (in the "xattr" namespace).
2657  *
2658  * If @cancellable is not %NULL, then the operation can be cancelled by
2659  * triggering the cancellable object from another thread. If the operation
2660  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2661  * 
2662  * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2663  * When you are done with it, release it with g_file_attribute_info_list_unref()
2664  **/
2665 GFileAttributeInfoList *
2666 g_file_query_writable_namespaces (GFile         *file,
2667                                   GCancellable  *cancellable,
2668                                   GError       **error)
2669 {
2670   GFileIface *iface;
2671   GError *my_error;
2672   GFileAttributeInfoList *list;
2673   
2674   g_return_val_if_fail (G_IS_FILE (file), NULL);
2675
2676   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2677     return NULL;
2678   
2679   iface = G_FILE_GET_IFACE (file);
2680
2681   if (iface->query_writable_namespaces == NULL)
2682     return g_file_attribute_info_list_new ();
2683
2684   my_error = NULL;
2685   list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2686   
2687   if (list == NULL)
2688     {
2689       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2690         {
2691           list = g_file_attribute_info_list_new ();
2692           g_error_free (my_error);
2693         }
2694       else
2695         g_propagate_error (error, my_error);
2696     }
2697   
2698   return list;
2699 }
2700
2701 /**
2702  * g_file_set_attribute:
2703  * @file: input #GFile.
2704  * @attribute: a string containing the attribute's name.
2705  * @type: The type of the attribute
2706  * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2707  * @flags: a set of #GFileQueryInfoFlags.
2708  * @cancellable: optional #GCancellable object, %NULL to ignore.
2709  * @error: a #GError, or %NULL
2710  * 
2711  * Sets an attribute in the file with attribute name @attribute to @value.
2712  * 
2713  * If @cancellable is not %NULL, then the operation can be cancelled by
2714  * triggering the cancellable object from another thread. If the operation
2715  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2716  * 
2717  * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2718  **/
2719 gboolean
2720 g_file_set_attribute (GFile                      *file,
2721                       const char                 *attribute,
2722                       GFileAttributeType          type,
2723                       gpointer                    value_p,
2724                       GFileQueryInfoFlags         flags,
2725                       GCancellable               *cancellable,
2726                       GError                    **error)
2727 {
2728   GFileIface *iface;
2729   
2730   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2731   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2732
2733   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2734     return FALSE;
2735   
2736   iface = G_FILE_GET_IFACE (file);
2737
2738   if (iface->set_attribute == NULL)
2739     {
2740       g_set_error (error, G_IO_ERROR,
2741                    G_IO_ERROR_NOT_SUPPORTED,
2742                    _("Operation not supported"));
2743       return FALSE;
2744     }
2745
2746   return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2747 }
2748
2749 /**
2750  * g_file_set_attributes_from_info:
2751  * @file: input #GFile.
2752  * @info: a #GFileInfo.
2753  * @flags: #GFileQueryInfoFlags
2754  * @cancellable: optional #GCancellable object, %NULL to ignore.
2755  * @error: a #GError, or %NULL 
2756  * 
2757  * Tries to set all attributes in the #GFileInfo on the target values, 
2758  * not stopping on the first error.
2759  * 
2760  * If there is any error during this operation then @error will be set to
2761  * the first error. Error on particular fields are flagged by setting 
2762  * the "status" field in the attribute value to 
2763  * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2764  * further errors.
2765  *
2766  * If @cancellable is not %NULL, then the operation can be cancelled by
2767  * triggering the cancellable object from another thread. If the operation
2768  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2769  * 
2770  * Returns: %TRUE if there was any error, %FALSE otherwise.
2771  **/
2772 gboolean
2773 g_file_set_attributes_from_info (GFile                *file,
2774                                  GFileInfo            *info,
2775                                  GFileQueryInfoFlags   flags,
2776                                  GCancellable         *cancellable,
2777                                  GError              **error)
2778 {
2779   GFileIface *iface;
2780
2781   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2782   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2783
2784   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2785     return FALSE;
2786   
2787   g_file_info_clear_status (info);
2788   
2789   iface = G_FILE_GET_IFACE (file);
2790
2791   return (* iface->set_attributes_from_info) (file, 
2792                                               info, 
2793                                               flags, 
2794                                               cancellable, 
2795                                               error);
2796 }
2797
2798
2799 static gboolean
2800 g_file_real_set_attributes_from_info (GFile                *file,
2801                                       GFileInfo            *info,
2802                                       GFileQueryInfoFlags   flags,
2803                                       GCancellable         *cancellable,
2804                                       GError              **error)
2805 {
2806   char **attributes;
2807   int i;
2808   gboolean res;
2809   GFileAttributeValue *value;
2810   
2811   res = TRUE;
2812   
2813   attributes = g_file_info_list_attributes (info, NULL);
2814
2815   for (i = 0; attributes[i] != NULL; i++)
2816     {
2817       value = _g_file_info_get_attribute_value (info, attributes[i]);
2818
2819       if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2820         continue;
2821
2822       if (!g_file_set_attribute (file, attributes[i],
2823                                  value->type, _g_file_attribute_value_peek_as_pointer (value),
2824                                  flags, cancellable, error))
2825         {
2826           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2827           res = FALSE;
2828           /* Don't set error multiple times */
2829           error = NULL;
2830         }
2831       else
2832         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2833     }
2834   
2835   g_strfreev (attributes);
2836   
2837   return res;
2838 }
2839
2840 /**
2841  * g_file_set_attributes_async:
2842  * @file: input #GFile.
2843  * @info: a #GFileInfo.
2844  * @flags: a #GFileQueryInfoFlags.
2845  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2846  *     of the request. 
2847  * @cancellable: optional #GCancellable object, %NULL to ignore.
2848  * @callback: a #GAsyncReadyCallback. 
2849  * @user_data: a #gpointer.
2850  *
2851  * Asynchronously sets the attributes of @file with @info.
2852  * 
2853  * For more details, see g_file_set_attributes_from_info() which is
2854  * the synchronous version of this call.
2855  *
2856  * When the operation is finished, @callback will be called. You can then call
2857  * g_file_set_attributes_finish() to get the result of the operation.
2858  **/
2859 void
2860 g_file_set_attributes_async (GFile               *file,
2861                              GFileInfo           *info,
2862                              GFileQueryInfoFlags  flags,
2863                              int                  io_priority,
2864                              GCancellable        *cancellable,
2865                              GAsyncReadyCallback  callback,
2866                              gpointer             user_data)
2867 {
2868   GFileIface *iface;
2869   
2870   g_return_if_fail (G_IS_FILE (file));
2871   g_return_if_fail (G_IS_FILE_INFO (info));
2872
2873   iface = G_FILE_GET_IFACE (file);
2874   (* iface->set_attributes_async) (file, 
2875                                    info, 
2876                                    flags, 
2877                                    io_priority, 
2878                                    cancellable, 
2879                                    callback, 
2880                                    user_data);
2881 }
2882
2883 /**
2884  * g_file_set_attributes_finish:
2885  * @file: input #GFile.
2886  * @result: a #GAsyncResult.
2887  * @info: a #GFileInfo.
2888  * @error: a #GError, or %NULL
2889  * 
2890  * Finishes setting an attribute started in g_file_set_attributes_async().
2891  * 
2892  * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2893  **/
2894 gboolean
2895 g_file_set_attributes_finish (GFile         *file,
2896                               GAsyncResult  *result,
2897                               GFileInfo    **info,
2898                               GError       **error)
2899 {
2900   GFileIface *iface;
2901   
2902   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2903   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2904
2905   /* No standard handling of errors here, as we must set info even
2906    * on errors 
2907    */
2908   iface = G_FILE_GET_IFACE (file);
2909   return (* iface->set_attributes_finish) (file, result, info, error);
2910 }
2911
2912 /**
2913  * g_file_set_attribute_string:
2914  * @file: input #GFile.
2915  * @attribute: a string containing the attribute's name.
2916  * @value: a string containing the attribute's value.
2917  * @flags: #GFileQueryInfoFlags.
2918  * @cancellable: optional #GCancellable object, %NULL to ignore.
2919  * @error: a #GError, or %NULL
2920  * 
2921  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value. 
2922  * If @attribute is of a different type, this operation will fail.
2923  * 
2924  * If @cancellable is not %NULL, then the operation can be cancelled by
2925  * triggering the cancellable object from another thread. If the operation
2926  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2927  * 
2928  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2929  **/
2930 gboolean
2931 g_file_set_attribute_string (GFile                *file,
2932                              const char           *attribute,
2933                              const char           *value,
2934                              GFileQueryInfoFlags   flags,
2935                              GCancellable         *cancellable,
2936                              GError              **error)
2937 {
2938   return g_file_set_attribute (file, attribute,
2939                                G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
2940                                flags, cancellable, error);
2941 }
2942
2943 /**
2944  * g_file_set_attribute_byte_string:
2945  * @file: input #GFile.
2946  * @attribute: a string containing the attribute's name.
2947  * @value: a string containing the attribute's new value.
2948  * @flags: a #GFileQueryInfoFlags.
2949  * @cancellable: optional #GCancellable object, %NULL to ignore.
2950  * @error: a #GError, or %NULL
2951  * 
2952  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value. 
2953  * If @attribute is of a different type, this operation will fail, 
2954  * returning %FALSE. 
2955  * 
2956  * If @cancellable is not %NULL, then the operation can be cancelled by
2957  * triggering the cancellable object from another thread. If the operation
2958  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2959  * 
2960  * Returns: %TRUE if the @attribute was successfully set to @value 
2961  * in the @file, %FALSE otherwise.
2962  **/
2963 gboolean
2964 g_file_set_attribute_byte_string  (GFile                *file,
2965                                    const char           *attribute,
2966                                    const char           *value,
2967                                    GFileQueryInfoFlags   flags,
2968                                    GCancellable         *cancellable,
2969                                    GError              **error)
2970 {
2971   return g_file_set_attribute (file, attribute,
2972                                G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
2973                                flags, cancellable, error);
2974 }
2975
2976 /**
2977  * g_file_set_attribute_uint32:
2978  * @file: input #GFile.
2979  * @attribute: a string containing the attribute's name.
2980  * @value: a #guint32 containing the attribute's new value.
2981  * @flags: a #GFileQueryInfoFlags.
2982  * @cancellable: optional #GCancellable object, %NULL to ignore.
2983  * @error: a #GError, or %NULL
2984  * 
2985  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value. 
2986  * If @attribute is of a different type, this operation will fail.
2987  * 
2988  * If @cancellable is not %NULL, then the operation can be cancelled by
2989  * triggering the cancellable object from another thread. If the operation
2990  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2991  * 
2992  * Returns: %TRUE if the @attribute was successfully set to @value 
2993  * in the @file, %FALSE otherwise.
2994  **/
2995 gboolean
2996 g_file_set_attribute_uint32 (GFile                *file,
2997                              const char           *attribute,
2998                              guint32               value,
2999                              GFileQueryInfoFlags   flags,
3000                              GCancellable         *cancellable,
3001                              GError              **error)
3002 {
3003   return g_file_set_attribute (file, attribute,
3004                                G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3005                                flags, cancellable, error);
3006 }
3007
3008 /**
3009  * g_file_set_attribute_int32:
3010  * @file: input #GFile.
3011  * @attribute: a string containing the attribute's name.
3012  * @value: a #gint32 containing the attribute's new value.
3013  * @flags: a #GFileQueryInfoFlags.
3014  * @cancellable: optional #GCancellable object, %NULL to ignore.
3015  * @error: a #GError, or %NULL
3016  * 
3017  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value. 
3018  * If @attribute is of a different type, this operation will fail.
3019  * 
3020  * If @cancellable is not %NULL, then the operation can be cancelled by
3021  * triggering the cancellable object from another thread. If the operation
3022  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3023  * 
3024  * Returns: %TRUE if the @attribute was successfully set to @value 
3025  * in the @file, %FALSE otherwise. 
3026  **/
3027 gboolean
3028 g_file_set_attribute_int32 (GFile                *file,
3029                             const char           *attribute,
3030                             gint32                value,
3031                             GFileQueryInfoFlags   flags,
3032                             GCancellable         *cancellable,
3033                             GError              **error)
3034 {
3035   return g_file_set_attribute (file, attribute,
3036                                G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3037                                flags, cancellable, error);
3038 }
3039
3040 /**
3041  * g_file_set_attribute_uint64:
3042  * @file: input #GFile. 
3043  * @attribute: a string containing the attribute's name.
3044  * @value: a #guint64 containing the attribute's new value.
3045  * @flags: a #GFileQueryInfoFlags.
3046  * @cancellable: optional #GCancellable object, %NULL to ignore.
3047  * @error: a #GError, or %NULL
3048  * 
3049  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value. 
3050  * If @attribute is of a different type, this operation will fail.
3051  * 
3052  * If @cancellable is not %NULL, then the operation can be cancelled by
3053  * triggering the cancellable object from another thread. If the operation
3054  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3055  * 
3056  * Returns: %TRUE if the @attribute was successfully set to @value 
3057  * in the @file, %FALSE otherwise.
3058  **/
3059 gboolean
3060 g_file_set_attribute_uint64 (GFile                *file,
3061                              const char           *attribute,
3062                              guint64               value,
3063                              GFileQueryInfoFlags   flags,
3064                              GCancellable         *cancellable,
3065                              GError              **error)
3066  {
3067   return g_file_set_attribute (file, attribute,
3068                                G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3069                                flags, cancellable, error);
3070 }
3071
3072 /**
3073  * g_file_set_attribute_int64:
3074  * @file: input #GFile.
3075  * @attribute: a string containing the attribute's name.
3076  * @value: a #guint64 containing the attribute's new value.
3077  * @flags: a #GFileQueryInfoFlags.
3078  * @cancellable: optional #GCancellable object, %NULL to ignore.
3079  * @error: a #GError, or %NULL
3080  * 
3081  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value. 
3082  * If @attribute is of a different type, this operation will fail.
3083  * 
3084  * If @cancellable is not %NULL, then the operation can be cancelled by
3085  * triggering the cancellable object from another thread. If the operation
3086  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3087  * 
3088  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3089  **/
3090 gboolean
3091 g_file_set_attribute_int64 (GFile                *file,
3092                             const char           *attribute,
3093                             gint64                value,
3094                             GFileQueryInfoFlags   flags,
3095                             GCancellable         *cancellable,
3096                             GError              **error)
3097 {
3098   return g_file_set_attribute (file, attribute,
3099                                G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3100                                flags, cancellable, error);
3101 }
3102
3103 /**
3104  * g_file_mount_mountable:
3105  * @file: input #GFile.
3106  * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3107  * @cancellable: optional #GCancellable object, %NULL to ignore.
3108  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3109  * @user_data: the data to pass to callback function
3110  * 
3111  * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3112  * Using @mount_operation, you can request callbacks when, for instance, 
3113  * passwords are needed during authentication.
3114  *
3115  * If @cancellable is not %NULL, then the operation can be cancelled by
3116  * triggering the cancellable object from another thread. If the operation
3117  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3118  *
3119  * When the operation is finished, @callback will be called. You can then call
3120  * g_file_mount_mountable_finish() to get the result of the operation.
3121  **/
3122 void
3123 g_file_mount_mountable (GFile               *file,
3124                         GMountOperation     *mount_operation,
3125                         GCancellable        *cancellable,
3126                         GAsyncReadyCallback  callback,
3127                         gpointer             user_data)
3128 {
3129   GFileIface *iface;
3130
3131   g_return_if_fail (G_IS_FILE (file));
3132
3133   iface = G_FILE_GET_IFACE (file);
3134
3135   if (iface->mount_mountable == NULL)
3136     g_simple_async_report_error_in_idle (G_OBJECT (file),
3137                                          callback,
3138                                          user_data,
3139                                          G_IO_ERROR,
3140                                          G_IO_ERROR_NOT_SUPPORTED,
3141                                          _("Operation not supported"));
3142   
3143   (* iface->mount_mountable) (file,
3144                               mount_operation,
3145                               cancellable,
3146                               callback,
3147                               user_data);
3148 }
3149
3150 /**
3151  * g_file_mount_mountable_finish:
3152  * @file: input #GFile.
3153  * @result: a #GAsyncResult.
3154  * @error: a #GError, or %NULL
3155  *
3156  * Finishes a mount operation. See g_file_mount_mountable() for details.
3157  * 
3158  * Finish an asynchronous mount operation that was started 
3159  * with g_file_mount_mountable().
3160  *
3161  * Returns: a #GFile or %NULL on error.
3162  **/
3163 GFile *
3164 g_file_mount_mountable_finish (GFile         *file,
3165                                GAsyncResult  *result,
3166                                GError       **error)
3167 {
3168   GFileIface *iface;
3169
3170   g_return_val_if_fail (G_IS_FILE (file), NULL);
3171   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3172
3173   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3174     {
3175       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3176       if (g_simple_async_result_propagate_error (simple, error))
3177         return NULL;
3178     }
3179   
3180   iface = G_FILE_GET_IFACE (file);
3181   return (* iface->mount_mountable_finish) (file, result, error);
3182 }
3183
3184 /**
3185  * g_file_unmount_mountable:
3186  * @file: input #GFile.
3187  * @flags: flags affecting the operation
3188  * @cancellable: optional #GCancellable object, %NULL to ignore.
3189  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3190  * @user_data: the data to pass to callback function
3191  *
3192  * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3193  *
3194  * If @cancellable is not %NULL, then the operation can be cancelled by
3195  * triggering the cancellable object from another thread. If the operation
3196  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3197  *
3198  * When the operation is finished, @callback will be called. You can then call
3199  * g_file_unmount_mountable_finish() to get the result of the operation.
3200  **/
3201 void
3202 g_file_unmount_mountable (GFile               *file,
3203                           GMountUnmountFlags   flags,
3204                           GCancellable        *cancellable,
3205                           GAsyncReadyCallback  callback,
3206                           gpointer             user_data)
3207 {
3208   GFileIface *iface;
3209   
3210   g_return_if_fail (G_IS_FILE (file));
3211
3212   iface = G_FILE_GET_IFACE (file);
3213   
3214   if (iface->unmount_mountable == NULL)
3215     g_simple_async_report_error_in_idle (G_OBJECT (file),
3216                                          callback,
3217                                          user_data,
3218                                          G_IO_ERROR,
3219                                          G_IO_ERROR_NOT_SUPPORTED,
3220                                          _("Operation not supported"));
3221   
3222   (* iface->unmount_mountable) (file,
3223                                 flags,
3224                                 cancellable,
3225                                 callback,
3226                                 user_data);
3227 }
3228
3229 /**
3230  * g_file_unmount_mountable_finish:
3231  * @file: input #GFile.
3232  * @result: a #GAsyncResult.
3233  * @error: a #GError, or %NULL
3234  *
3235  * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3236  * 
3237  * Finish an asynchronous unmount operation that was started 
3238  * with g_file_unmount_mountable().
3239  *
3240  * Returns: %TRUE if the operation finished successfully. %FALSE
3241  * otherwise.
3242  **/
3243 gboolean
3244 g_file_unmount_mountable_finish (GFile         *file,
3245                                  GAsyncResult  *result,
3246                                  GError       **error)
3247 {
3248   GFileIface *iface;
3249   
3250   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3251   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3252
3253   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3254     {
3255       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3256       if (g_simple_async_result_propagate_error (simple, error))
3257         return FALSE;
3258     }
3259   
3260   iface = G_FILE_GET_IFACE (file);
3261   return (* iface->unmount_mountable_finish) (file, result, error);
3262 }
3263
3264 /**
3265  * g_file_eject_mountable:
3266  * @file: input #GFile.
3267  * @flags: flags affecting the operation
3268  * @cancellable: optional #GCancellable object, %NULL to ignore.
3269  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3270  * @user_data: the data to pass to callback function
3271  * 
3272  * Starts an asynchronous eject on a mountable.  
3273  * When this operation has completed, @callback will be called with
3274  * @user_user data, and the operation can be finalized with 
3275  * g_file_eject_mountable_finish().
3276  * 
3277  * If @cancellable is not %NULL, then the operation can be cancelled by
3278  * triggering the cancellable object from another thread. If the operation
3279  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3280  **/
3281 void
3282 g_file_eject_mountable (GFile               *file,
3283                         GMountUnmountFlags   flags,
3284                         GCancellable        *cancellable,
3285                         GAsyncReadyCallback  callback,
3286                         gpointer             user_data)
3287 {
3288   GFileIface *iface;
3289
3290   g_return_if_fail (G_IS_FILE (file));
3291
3292   iface = G_FILE_GET_IFACE (file);
3293   
3294   if (iface->eject_mountable == NULL)
3295     g_simple_async_report_error_in_idle (G_OBJECT (file),
3296                                          callback,
3297                                          user_data,
3298                                          G_IO_ERROR,
3299                                          G_IO_ERROR_NOT_SUPPORTED,
3300                                          _("Operation not supported"));
3301   
3302   (* iface->eject_mountable) (file,
3303                               flags,
3304                               cancellable,
3305                               callback,
3306                               user_data);
3307 }
3308
3309 /**
3310  * g_file_eject_mountable_finish:
3311  * @file: input #GFile.
3312  * @result: a #GAsyncResult.
3313  * @error: a #GError, or %NULL
3314  * 
3315  * Finishes an asynchronous eject operation started by 
3316  * g_file_eject_mountable().
3317  * 
3318  * Returns: %TRUE if the @file was ejected successfully. %FALSE 
3319  * otherwise.
3320  **/
3321 gboolean
3322 g_file_eject_mountable_finish (GFile         *file,
3323                                GAsyncResult  *result,
3324                                GError       **error)
3325 {
3326   GFileIface *iface;
3327   
3328   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3329   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3330
3331   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3332     {
3333       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3334       if (g_simple_async_result_propagate_error (simple, error))
3335         return FALSE;
3336     }
3337   
3338   iface = G_FILE_GET_IFACE (file);
3339   return (* iface->eject_mountable_finish) (file, result, error);
3340 }
3341
3342 /**
3343  * g_file_monitor_directory:
3344  * @file: input #GFile.
3345  * @flags: a set of #GFileMonitorFlags.
3346  * @cancellable: optional #GCancellable object, %NULL to ignore.
3347  * @error: a #GError, or %NULL.
3348  * 
3349  * Obtains a directory monitor for the given file.
3350  * This may fail if directory monitoring is not supported.
3351  *
3352  * If @cancellable is not %NULL, then the operation can be cancelled by
3353  * triggering the cancellable object from another thread. If the operation
3354  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3355  * 
3356  * Returns: a #GFileMonitor for the given @file, 
3357  * or %NULL on error.
3358  **/
3359 GFileMonitor*
3360 g_file_monitor_directory (GFile             *file,
3361                           GFileMonitorFlags  flags,
3362                           GCancellable      *cancellable,
3363                           GError           **error)
3364 {
3365   GFileIface *iface;
3366
3367   g_return_val_if_fail (G_IS_FILE (file), NULL);
3368
3369   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3370     return NULL;
3371
3372   iface = G_FILE_GET_IFACE (file);
3373
3374   if (iface->monitor_dir == NULL)
3375     {
3376       g_set_error (error, G_IO_ERROR,
3377                    G_IO_ERROR_NOT_SUPPORTED,
3378                    _("Operation not supported"));
3379       return NULL;
3380     }
3381
3382   return (* iface->monitor_dir) (file, flags, cancellable, error);
3383 }
3384
3385 /**
3386  * g_file_monitor_file:
3387  * @file: input #GFile.
3388  * @flags: a set of #GFileMonitorFlags.
3389  * @cancellable: optional #GCancellable object, %NULL to ignore.
3390  * @error: a #GError, or %NULL.
3391  * 
3392  * Obtains a file monitor for the given file. If no file notification
3393  * mechanism exists, then regular polling of the file is used.
3394  *
3395  * If @cancellable is not %NULL, then the operation can be cancelled by
3396  * triggering the cancellable object from another thread. If the operation
3397  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3398  * 
3399  * Returns: a #GFileMonitor for the given @file.
3400  **/
3401 GFileMonitor*
3402 g_file_monitor_file (GFile             *file,
3403                      GFileMonitorFlags  flags,
3404                      GCancellable      *cancellable,
3405                      GError           **error)
3406 {
3407   GFileIface *iface;
3408   GFileMonitor *monitor;
3409   
3410   g_return_val_if_fail (G_IS_FILE (file), NULL);
3411
3412   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3413     return NULL;
3414
3415   iface = G_FILE_GET_IFACE (file);
3416
3417   monitor = NULL;
3418   
3419   if (iface->monitor_file)
3420     monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3421
3422 /* Fallback to polling */
3423   if (monitor == NULL)
3424     monitor = _g_poll_file_monitor_new (file);
3425
3426   return monitor;
3427 }
3428
3429 /********************************************
3430  *   Default implementation of async ops    *
3431  ********************************************/
3432
3433 typedef struct {
3434   char *attributes;
3435   GFileQueryInfoFlags flags;
3436   GFileInfo *info;
3437 } QueryInfoAsyncData;
3438
3439 static void
3440 query_info_data_free (QueryInfoAsyncData *data)
3441 {
3442   if (data->info)
3443     g_object_unref (data->info);
3444   g_free (data->attributes);
3445   g_free (data);
3446 }
3447
3448 static void
3449 query_info_async_thread (GSimpleAsyncResult *res,
3450                          GObject            *object,
3451                          GCancellable       *cancellable)
3452 {
3453   GError *error = NULL;
3454   QueryInfoAsyncData *data;
3455   GFileInfo *info;
3456   
3457   data = g_simple_async_result_get_op_res_gpointer (res);
3458   
3459   info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3460
3461   if (info == NULL)
3462     {
3463       g_simple_async_result_set_from_error (res, error);
3464       g_error_free (error);
3465     }
3466   else
3467     data->info = info;
3468 }
3469
3470 static void
3471 g_file_real_query_info_async (GFile               *file,
3472                               const char          *attributes,
3473                               GFileQueryInfoFlags  flags,
3474                               int                  io_priority,
3475                               GCancellable        *cancellable,
3476                               GAsyncReadyCallback  callback,
3477                               gpointer             user_data)
3478 {
3479   GSimpleAsyncResult *res;
3480   QueryInfoAsyncData *data;
3481
3482   data = g_new0 (QueryInfoAsyncData, 1);
3483   data->attributes = g_strdup (attributes);
3484   data->flags = flags;
3485   
3486   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3487   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3488   
3489   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3490   g_object_unref (res);
3491 }
3492
3493 static GFileInfo *
3494 g_file_real_query_info_finish (GFile         *file,
3495                                GAsyncResult  *res,
3496                                GError       **error)
3497 {
3498   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3499   QueryInfoAsyncData *data;
3500
3501   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3502
3503   data = g_simple_async_result_get_op_res_gpointer (simple);
3504   if (data->info)
3505     return g_object_ref (data->info);
3506   
3507   return NULL;
3508 }
3509
3510 typedef struct {
3511   char *attributes;
3512   GFileQueryInfoFlags flags;
3513   GFileEnumerator *enumerator;
3514 } EnumerateChildrenAsyncData;
3515
3516 static void
3517 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3518 {
3519   if (data->enumerator)
3520     g_object_unref (data->enumerator);
3521   g_free (data->attributes);
3522   g_free (data);
3523 }
3524
3525 static void
3526 enumerate_children_async_thread (GSimpleAsyncResult *res,
3527                                  GObject            *object,
3528                                  GCancellable       *cancellable)
3529 {
3530   GError *error = NULL;
3531   EnumerateChildrenAsyncData *data;
3532   GFileEnumerator *enumerator;
3533   
3534   data = g_simple_async_result_get_op_res_gpointer (res);
3535   
3536   enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3537
3538   if (enumerator == NULL)
3539     {
3540       g_simple_async_result_set_from_error (res, error);
3541       g_error_free (error);
3542     }
3543   else
3544     data->enumerator = enumerator;
3545 }
3546
3547 static void
3548 g_file_real_enumerate_children_async (GFile               *file,
3549                                       const char          *attributes,
3550                                       GFileQueryInfoFlags  flags,
3551                                       int                  io_priority,
3552                                       GCancellable        *cancellable,
3553                                       GAsyncReadyCallback  callback,
3554                                       gpointer             user_data)
3555 {
3556   GSimpleAsyncResult *res;
3557   EnumerateChildrenAsyncData *data;
3558
3559   data = g_new0 (EnumerateChildrenAsyncData, 1);
3560   data->attributes = g_strdup (attributes);
3561   data->flags = flags;
3562   
3563   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3564   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3565   
3566   g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3567   g_object_unref (res);
3568 }
3569
3570 static GFileEnumerator *
3571 g_file_real_enumerate_children_finish (GFile         *file,
3572                                        GAsyncResult  *res,
3573                                        GError       **error)
3574 {
3575   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3576   EnumerateChildrenAsyncData *data;
3577
3578   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3579
3580   data = g_simple_async_result_get_op_res_gpointer (simple);
3581   if (data->enumerator)
3582     return g_object_ref (data->enumerator);
3583   
3584   return NULL;
3585 }
3586
3587 static void
3588 open_read_async_thread (GSimpleAsyncResult *res,
3589                         GObject            *object,
3590                         GCancellable       *cancellable)
3591 {
3592   GFileIface *iface;
3593   GFileInputStream *stream;
3594   GError *error = NULL;
3595
3596   iface = G_FILE_GET_IFACE (object);
3597
3598   stream = iface->read_fn (G_FILE (object), cancellable, &error);
3599
3600   if (stream == NULL)
3601     {
3602       g_simple_async_result_set_from_error (res, error);
3603       g_error_free (error);
3604     }
3605   else
3606     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3607 }
3608
3609 static void
3610 g_file_real_read_async (GFile               *file,
3611                         int                  io_priority,
3612                         GCancellable        *cancellable,
3613                         GAsyncReadyCallback  callback,
3614                         gpointer             user_data)
3615 {
3616   GSimpleAsyncResult *res;
3617   
3618   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3619   
3620   g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3621   g_object_unref (res);
3622 }
3623
3624 static GFileInputStream *
3625 g_file_real_read_finish (GFile         *file,
3626                          GAsyncResult  *res,
3627                          GError       **error)
3628 {
3629   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3630   gpointer op;
3631
3632   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3633
3634   op = g_simple_async_result_get_op_res_gpointer (simple);
3635   if (op)
3636     return g_object_ref (op);
3637   
3638   return NULL;
3639 }
3640
3641 static void
3642 append_to_async_thread (GSimpleAsyncResult *res,
3643                         GObject            *object,
3644                         GCancellable       *cancellable)
3645 {
3646   GFileIface *iface;
3647   GFileCreateFlags *data;
3648   GFileOutputStream *stream;
3649   GError *error = NULL;
3650
3651   iface = G_FILE_GET_IFACE (object);
3652
3653   data = g_simple_async_result_get_op_res_gpointer (res);
3654
3655   stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3656
3657   if (stream == NULL)
3658     {
3659       g_simple_async_result_set_from_error (res, error);
3660       g_error_free (error);
3661     }
3662   else
3663     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3664 }
3665
3666 static void
3667 g_file_real_append_to_async (GFile               *file,
3668                              GFileCreateFlags     flags,
3669                              int                  io_priority,
3670                              GCancellable        *cancellable,
3671                              GAsyncReadyCallback  callback,
3672                              gpointer             user_data)
3673 {
3674   GFileCreateFlags *data;
3675   GSimpleAsyncResult *res;
3676
3677   data = g_new0 (GFileCreateFlags, 1);
3678   *data = flags;
3679
3680   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3681   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3682
3683   g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3684   g_object_unref (res);
3685 }
3686
3687 static GFileOutputStream *
3688 g_file_real_append_to_finish (GFile         *file,
3689                               GAsyncResult  *res,
3690                               GError       **error)
3691 {
3692   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3693   gpointer op;
3694
3695   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3696
3697   op = g_simple_async_result_get_op_res_gpointer (simple);
3698   if (op)
3699     return g_object_ref (op);
3700   
3701   return NULL;
3702 }
3703
3704 static void
3705 create_async_thread (GSimpleAsyncResult *res,
3706                      GObject            *object,
3707                      GCancellable       *cancellable)
3708 {
3709   GFileIface *iface;
3710   GFileCreateFlags *data;
3711   GFileOutputStream *stream;
3712   GError *error = NULL;
3713
3714   iface = G_FILE_GET_IFACE (object);
3715
3716   data = g_simple_async_result_get_op_res_gpointer (res);
3717
3718   stream = iface->create (G_FILE (object), *data, cancellable, &error);
3719
3720   if (stream == NULL)
3721     {
3722       g_simple_async_result_set_from_error (res, error);
3723       g_error_free (error);
3724     }
3725   else
3726     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3727 }
3728
3729 static void
3730 g_file_real_create_async (GFile               *file,
3731                           GFileCreateFlags     flags,
3732                           int                  io_priority,
3733                           GCancellable        *cancellable,
3734                           GAsyncReadyCallback  callback,
3735                           gpointer             user_data)
3736 {
3737   GFileCreateFlags *data;
3738   GSimpleAsyncResult *res;
3739
3740   data = g_new0 (GFileCreateFlags, 1);
3741   *data = flags;
3742
3743   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3744   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3745
3746   g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3747   g_object_unref (res);
3748 }
3749
3750 static GFileOutputStream *
3751 g_file_real_create_finish (GFile         *file,
3752                            GAsyncResult  *res,
3753                            GError       **error)
3754 {
3755   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3756   gpointer op;
3757
3758   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3759
3760   op = g_simple_async_result_get_op_res_gpointer (simple);
3761   if (op)
3762     return g_object_ref (op);
3763   
3764   return NULL;
3765 }
3766
3767 typedef struct {
3768   GFileOutputStream *stream;
3769   char *etag;
3770   gboolean make_backup;
3771   GFileCreateFlags flags;
3772 } ReplaceAsyncData;
3773
3774 static void
3775 replace_async_data_free (ReplaceAsyncData *data)
3776 {
3777   if (data->stream)
3778     g_object_unref (data->stream);
3779   g_free (data->etag);
3780   g_free (data);
3781 }
3782
3783 static void
3784 replace_async_thread (GSimpleAsyncResult *res,
3785                       GObject            *object,
3786                       GCancellable       *cancellable)
3787 {
3788   GFileIface *iface;
3789   GFileOutputStream *stream;
3790   GError *error = NULL;
3791   ReplaceAsyncData *data;
3792
3793   iface = G_FILE_GET_IFACE (object);
3794   
3795   data = g_simple_async_result_get_op_res_gpointer (res);
3796
3797   stream = iface->replace (G_FILE (object),
3798                            data->etag,
3799                            data->make_backup,
3800                            data->flags,
3801                            cancellable,
3802                            &error);
3803
3804   if (stream == NULL)
3805     {
3806       g_simple_async_result_set_from_error (res, error);
3807       g_error_free (error);
3808     }
3809   else
3810     data->stream = stream;
3811 }
3812
3813 static void
3814 g_file_real_replace_async (GFile               *file,
3815                            const char          *etag,
3816                            gboolean             make_backup,
3817                            GFileCreateFlags     flags,
3818                            int                  io_priority,
3819                            GCancellable        *cancellable,
3820                            GAsyncReadyCallback  callback,
3821                            gpointer             user_data)
3822 {
3823   GSimpleAsyncResult *res;
3824   ReplaceAsyncData *data;
3825
3826   data = g_new0 (ReplaceAsyncData, 1);
3827   data->etag = g_strdup (etag);
3828   data->make_backup = make_backup;
3829   data->flags = flags;
3830
3831   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3832   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3833
3834   g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3835   g_object_unref (res);
3836 }
3837
3838 static GFileOutputStream *
3839 g_file_real_replace_finish (GFile         *file,
3840                             GAsyncResult  *res,
3841                             GError       **error)
3842 {
3843   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3844   ReplaceAsyncData *data;
3845
3846   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3847
3848   data = g_simple_async_result_get_op_res_gpointer (simple);
3849   if (data->stream)
3850     return g_object_ref (data->stream);
3851   
3852   return NULL;
3853 }
3854
3855 typedef struct {
3856   char *name;
3857   GFile *file;
3858 } SetDisplayNameAsyncData;
3859
3860 static void
3861 set_display_name_data_free (SetDisplayNameAsyncData *data)
3862 {
3863   g_free (data->name);
3864   if (data->file)
3865     g_object_unref (data->file);
3866   g_free (data);
3867 }
3868
3869 static void
3870 set_display_name_async_thread (GSimpleAsyncResult *res,
3871                                GObject            *object,
3872                                GCancellable       *cancellable)
3873 {
3874   GError *error = NULL;
3875   SetDisplayNameAsyncData *data;
3876   GFile *file;
3877   
3878   data = g_simple_async_result_get_op_res_gpointer (res);
3879   
3880   file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3881
3882   if (file == NULL)
3883     {
3884       g_simple_async_result_set_from_error (res, error);
3885       g_error_free (error);
3886     }
3887   else
3888     data->file = file;
3889 }
3890
3891 static void
3892 g_file_real_set_display_name_async (GFile               *file,  
3893                                     const char          *display_name,
3894                                     int                  io_priority,
3895                                     GCancellable        *cancellable,
3896                                     GAsyncReadyCallback  callback,
3897                                     gpointer             user_data)
3898 {
3899   GSimpleAsyncResult *res;
3900   SetDisplayNameAsyncData *data;
3901
3902   data = g_new0 (SetDisplayNameAsyncData, 1);
3903   data->name = g_strdup (display_name);
3904   
3905   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3906   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3907   
3908   g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3909   g_object_unref (res);
3910 }
3911
3912 static GFile *
3913 g_file_real_set_display_name_finish (GFile         *file,
3914                                      GAsyncResult  *res,
3915                                      GError       **error)
3916 {
3917   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3918   SetDisplayNameAsyncData *data;
3919
3920   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3921
3922   data = g_simple_async_result_get_op_res_gpointer (simple);
3923   if (data->file)
3924     return g_object_ref (data->file);
3925   
3926   return NULL;
3927 }
3928
3929 typedef struct {
3930   GFileQueryInfoFlags flags;
3931   GFileInfo *info;
3932   gboolean res;
3933   GError *error;
3934 } SetInfoAsyncData;
3935
3936 static void
3937 set_info_data_free (SetInfoAsyncData *data)
3938 {
3939   if (data->info)
3940     g_object_unref (data->info);
3941   if (data->error)
3942     g_error_free (data->error);
3943   g_free (data);
3944 }
3945
3946 static void
3947 set_info_async_thread (GSimpleAsyncResult *res,
3948                        GObject            *object,
3949                        GCancellable       *cancellable)
3950 {
3951   SetInfoAsyncData *data;
3952   
3953   data = g_simple_async_result_get_op_res_gpointer (res);
3954   
3955   data->error = NULL;
3956   data->res = g_file_set_attributes_from_info (G_FILE (object),
3957                                                data->info,
3958                                                data->flags,
3959                                                cancellable,
3960                                                &data->error);
3961 }
3962
3963 static void
3964 g_file_real_set_attributes_async (GFile               *file,
3965                                   GFileInfo           *info,
3966                                   GFileQueryInfoFlags  flags,
3967                                   int                  io_priority,
3968                                   GCancellable        *cancellable,
3969                                   GAsyncReadyCallback  callback,
3970                                   gpointer             user_data)
3971 {
3972   GSimpleAsyncResult *res;
3973   SetInfoAsyncData *data;
3974
3975   data = g_new0 (SetInfoAsyncData, 1);
3976   data->info = g_file_info_dup (info);
3977   data->flags = flags;
3978   
3979   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
3980   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
3981   
3982   g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
3983   g_object_unref (res);
3984 }
3985
3986 static gboolean
3987 g_file_real_set_attributes_finish (GFile         *file,
3988                                    GAsyncResult  *res,
3989                                    GFileInfo    **info,
3990                                    GError       **error)
3991 {
3992   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3993   SetInfoAsyncData *data;
3994   
3995   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
3996
3997   data = g_simple_async_result_get_op_res_gpointer (simple);
3998
3999   if (info) 
4000     *info = g_object_ref (data->info);
4001
4002   if (error != NULL && data->error) 
4003     *error = g_error_copy (data->error);
4004   
4005   return data->res;
4006 }
4007
4008 /********************************************
4009  *   Default VFS operations                 *
4010  ********************************************/
4011
4012 /**
4013  * g_file_new_for_path:
4014  * @path: a string containing a relative or absolute path.
4015  * 
4016  * Constructs a #GFile for a given path. This operation never
4017  * fails, but the returned object might not support any I/O
4018  * operation if @path is malformed.
4019  * 
4020  * Returns: a new #GFile for the given @path. 
4021  **/
4022 GFile *
4023 g_file_new_for_path (const char *path)
4024 {
4025   g_return_val_if_fail (path != NULL, NULL);
4026
4027   return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4028 }
4029  
4030 /**
4031  * g_file_new_for_uri:
4032  * @uri: a string containing a URI.
4033  * 
4034  * Constructs a #GFile for a given URI. This operation never 
4035  * fails, but the returned object might not support any I/O 
4036  * operation if @uri is malformed or if the uri type is 
4037  * not supported.
4038  * 
4039  * Returns: a #GFile for the given @uri.
4040  **/ 
4041 GFile *
4042 g_file_new_for_uri (const char *uri)
4043 {
4044   g_return_val_if_fail (uri != NULL, NULL);
4045
4046   return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4047 }
4048   
4049 /**
4050  * g_file_parse_name:
4051  * @parse_name: a file name or path to be parsed.
4052  * 
4053  * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4054  * This operation never fails, but the returned object might not support any I/O
4055  * operation if the @parse_name cannot be parsed.
4056  * 
4057  * Returns: a new #GFile.
4058  **/
4059 GFile *
4060 g_file_parse_name (const char *parse_name)
4061 {
4062   g_return_val_if_fail (parse_name != NULL, NULL);
4063
4064   return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4065 }
4066
4067 static gboolean
4068 is_valid_scheme_character (char c)
4069 {
4070   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4071 }
4072
4073 static gboolean
4074 has_valid_scheme (const char *uri)
4075 {
4076   const char *p;
4077   
4078   p = uri;
4079   
4080   if (!is_valid_scheme_character (*p))
4081     return FALSE;
4082
4083   do {
4084     p++;
4085   } while (is_valid_scheme_character (*p));
4086
4087   return *p == ':';
4088 }
4089
4090 /**
4091  * g_file_new_for_commandline_arg:
4092  * @arg: a command line string.
4093  * 
4094  * Creates a #GFile with the given argument from the command line. The value of
4095  * @arg can be either a URI, an absolute path or a relative path resolved
4096  * relative to the current working directory.
4097  * This operation never fails, but the returned object might not support any
4098  * I/O operation if @arg points to a malformed path.
4099  *
4100  * Returns: a new #GFile. 
4101  **/
4102 GFile *
4103 g_file_new_for_commandline_arg (const char *arg)
4104 {
4105   GFile *file;
4106   char *filename;
4107   char *current_dir;
4108   
4109   g_return_val_if_fail (arg != NULL, NULL);
4110   
4111   if (g_path_is_absolute (arg))
4112     return g_file_new_for_path (arg);
4113
4114   if (has_valid_scheme (arg))
4115     return g_file_new_for_uri (arg);
4116     
4117   current_dir = g_get_current_dir ();
4118   filename = g_build_filename (current_dir, arg, NULL);
4119   g_free (current_dir);
4120   
4121   file = g_file_new_for_path (filename);
4122   g_free (filename);
4123   
4124   return file;
4125 }
4126
4127 /**
4128  * g_file_mount_enclosing_volume:
4129  * @location: input #GFile.
4130  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4131  * @cancellable: optional #GCancellable object, %NULL to ignore.
4132  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4133  * @user_data: the data to pass to callback function
4134  * 
4135  * Starts a @mount_operation, mounting the volume that contains the file @location. 
4136  * 
4137  * When this operation has completed, @callback will be called with
4138  * @user_user data, and the operation can be finalized with 
4139  * g_file_mount_enclosing_volume_finish().
4140  * 
4141  * If @cancellable is not %NULL, then the operation can be cancelled by
4142  * triggering the cancellable object from another thread. If the operation
4143  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4144  **/
4145 void
4146 g_file_mount_enclosing_volume (GFile               *location,
4147                                GMountOperation     *mount_operation,
4148                                GCancellable        *cancellable,
4149                                GAsyncReadyCallback  callback,
4150                                gpointer             user_data)
4151 {
4152   GFileIface *iface;
4153
4154   g_return_if_fail (G_IS_FILE (location));
4155
4156   iface = G_FILE_GET_IFACE (location);
4157
4158   if (iface->mount_enclosing_volume == NULL)
4159     {
4160       g_simple_async_report_error_in_idle (G_OBJECT (location),
4161                                            callback, user_data,
4162                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4163                                            _("volume doesn't implement mount"));
4164       
4165       return;
4166     }
4167   
4168   (* iface->mount_enclosing_volume) (location, mount_operation, cancellable, callback, user_data);
4169
4170 }
4171
4172 /**
4173  * g_file_mount_enclosing_volume_finish:
4174  * @location: input #GFile.
4175  * @result: a #GAsyncResult.
4176  * @error: a #GError, or %NULL
4177  * 
4178  * Finishes a mount operation started by g_file_mount_enclosing_volume().
4179  * 
4180  * Returns: %TRUE if successful. If an error
4181  * has occurred, this function will return %FALSE and set @error
4182  * appropriately if present.
4183  **/
4184 gboolean
4185 g_file_mount_enclosing_volume_finish (GFile         *location,
4186                                       GAsyncResult  *result,
4187                                       GError       **error)
4188 {
4189   GFileIface *iface;
4190
4191   g_return_val_if_fail (G_IS_FILE (location), FALSE);
4192   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4193
4194   if (G_IS_SIMPLE_ASYNC_RESULT (result))
4195     {
4196       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4197       if (g_simple_async_result_propagate_error (simple, error))
4198         return FALSE;
4199     }
4200   
4201   iface = G_FILE_GET_IFACE (location);
4202
4203   return (* iface->mount_enclosing_volume_finish) (location, result, error);
4204 }
4205
4206 /********************************************
4207  *   Utility functions                      *
4208  ********************************************/
4209
4210 #define GET_CONTENT_BLOCK_SIZE 8192
4211
4212 /**
4213  * g_file_load_contents:
4214  * @file: input #GFile.
4215  * @cancellable: optional #GCancellable object, %NULL to ignore.
4216  * @contents: a location to place the contents of the file.
4217  * @length: a location to place the length of the contents of the file.
4218  * @etag_out: a location to place the current entity tag for the file.
4219  * @error: a #GError, or %NULL
4220  *
4221  * Loads the content of the file into memory, returning the size of
4222  * the data. The data is always zero terminated, but this is not
4223  * included in the resultant @length.
4224  * 
4225  * If @cancellable is not %NULL, then the operation can be cancelled by
4226  * triggering the cancellable object from another thread. If the operation
4227  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4228  * 
4229  * Returns: %TRUE if the @file's contents were successfully loaded.
4230  * %FALSE if there were errors..
4231  **/
4232 gboolean
4233 g_file_load_contents (GFile         *file,
4234                       GCancellable  *cancellable,
4235                       char         **contents,
4236                       gsize         *length,
4237                       char         **etag_out,
4238                       GError       **error)
4239 {
4240   GFileInputStream *in;
4241   GByteArray *content;
4242   gsize pos;
4243   gssize res;
4244   GFileInfo *info;
4245
4246   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4247   g_return_val_if_fail (contents != NULL, FALSE);
4248
4249   in = g_file_read (file, cancellable, error);
4250   if (in == NULL)
4251     return FALSE;
4252
4253   content = g_byte_array_new ();
4254   pos = 0;
4255   
4256   g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4257   while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4258                                      content->data + pos,
4259                                      GET_CONTENT_BLOCK_SIZE,
4260                                      cancellable, error)) > 0)
4261     {
4262       pos += res;
4263       g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4264     }
4265
4266   if (etag_out)
4267     {
4268       *etag_out = NULL;
4269       
4270       info = g_file_input_stream_query_info (in,
4271                                              G_FILE_ATTRIBUTE_ETAG_VALUE,
4272                                              cancellable,
4273                                              NULL);
4274       if (info)
4275         {
4276           *etag_out = g_strdup (g_file_info_get_etag (info));
4277           g_object_unref (info);
4278         }
4279     } 
4280
4281   /* Ignore errors on close */
4282   g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4283   g_object_unref (in);
4284
4285   if (res < 0)
4286     {
4287       /* error is set already */
4288       g_byte_array_free (content, TRUE);
4289       return FALSE;
4290     }
4291
4292   if (length)
4293     *length = pos;
4294
4295   /* Zero terminate (we got an extra byte allocated for this */
4296   content->data[pos] = 0;
4297   
4298   *contents = (char *)g_byte_array_free (content, FALSE);
4299   
4300   return TRUE;
4301 }
4302
4303 typedef struct {
4304   GFile *file;
4305   GError *error;
4306   GCancellable *cancellable;
4307   GFileReadMoreCallback read_more_callback;
4308   GAsyncReadyCallback callback;
4309   gpointer user_data;
4310   GByteArray *content;
4311   gsize pos;
4312   char *etag;
4313 } LoadContentsData;
4314
4315
4316 static void
4317 load_contents_data_free (LoadContentsData *data)
4318 {
4319   if (data->error)
4320     g_error_free (data->error);
4321   if (data->cancellable)
4322     g_object_unref (data->cancellable);
4323   if (data->content)
4324     g_byte_array_free (data->content, TRUE);
4325   g_free (data->etag);
4326   g_object_unref (data->file);
4327   g_free (data);
4328 }
4329
4330 static void
4331 load_contents_close_callback (GObject      *obj,
4332                               GAsyncResult *close_res,
4333                               gpointer      user_data)
4334 {
4335   GInputStream *stream = G_INPUT_STREAM (obj);
4336   LoadContentsData *data = user_data;
4337   GSimpleAsyncResult *res;
4338
4339   /* Ignore errors here, we're only reading anyway */
4340   g_input_stream_close_finish (stream, close_res, NULL);
4341   g_object_unref (stream);
4342
4343   res = g_simple_async_result_new (G_OBJECT (data->file),
4344                                    data->callback,
4345                                    data->user_data,
4346                                    g_file_load_contents_async);
4347   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4348   g_simple_async_result_complete (res);
4349   g_object_unref (res);
4350 }
4351
4352 static void
4353 load_contents_fstat_callback (GObject      *obj,
4354                               GAsyncResult *stat_res,
4355                               gpointer      user_data)
4356 {
4357   GInputStream *stream = G_INPUT_STREAM (obj);
4358   LoadContentsData *data = user_data;
4359   GFileInfo *info;
4360
4361   info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4362                                                    stat_res, NULL);
4363   if (info)
4364     {
4365       data->etag = g_strdup (g_file_info_get_etag (info));
4366       g_object_unref (info);
4367     }
4368
4369   g_input_stream_close_async (stream, 0,
4370                               data->cancellable,
4371                               load_contents_close_callback, data);
4372 }
4373
4374 static void
4375 load_contents_read_callback (GObject      *obj,
4376                              GAsyncResult *read_res,
4377                              gpointer      user_data)
4378 {
4379   GInputStream *stream = G_INPUT_STREAM (obj);
4380   LoadContentsData *data = user_data;
4381   GError *error = NULL;
4382   gssize read_size;
4383
4384   read_size = g_input_stream_read_finish (stream, read_res, &error);
4385
4386   if (read_size < 0) 
4387     {
4388       /* Error or EOF, close the file */
4389       data->error = error;
4390       g_input_stream_close_async (stream, 0,
4391                                   data->cancellable,
4392                                   load_contents_close_callback, data);
4393     }
4394   else if (read_size == 0)
4395     {
4396       g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4397                                             G_FILE_ATTRIBUTE_ETAG_VALUE,
4398                                             0,
4399                                             data->cancellable,
4400                                             load_contents_fstat_callback,
4401                                             data);
4402     }
4403   else if (read_size > 0)
4404     {
4405       data->pos += read_size;
4406       
4407       g_byte_array_set_size (data->content,
4408                              data->pos + GET_CONTENT_BLOCK_SIZE);
4409
4410
4411       if (data->read_more_callback &&
4412           !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4413         g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4414                                               G_FILE_ATTRIBUTE_ETAG_VALUE,
4415                                               0,
4416                                               data->cancellable,
4417                                               load_contents_fstat_callback,
4418                                               data);
4419       else 
4420         g_input_stream_read_async (stream,
4421                                    data->content->data + data->pos,
4422                                    GET_CONTENT_BLOCK_SIZE,
4423                                    0,
4424                                    data->cancellable,
4425                                    load_contents_read_callback,
4426                                    data);
4427     }
4428 }
4429
4430 static void
4431 load_contents_open_callback (GObject      *obj,
4432                              GAsyncResult *open_res,
4433                              gpointer      user_data)
4434 {
4435   GFile *file = G_FILE (obj);
4436   GFileInputStream *stream;
4437   LoadContentsData *data = user_data;
4438   GError *error = NULL;
4439   GSimpleAsyncResult *res;
4440
4441   stream = g_file_read_finish (file, open_res, &error);
4442
4443   if (stream)
4444     {
4445       g_byte_array_set_size (data->content,
4446                              data->pos + GET_CONTENT_BLOCK_SIZE);
4447       g_input_stream_read_async (G_INPUT_STREAM (stream),
4448                                  data->content->data + data->pos,
4449                                  GET_CONTENT_BLOCK_SIZE,
4450                                  0,
4451                                  data->cancellable,
4452                                  load_contents_read_callback,
4453                                  data);
4454       
4455     }
4456   else
4457     {
4458       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4459                                                   data->callback,
4460                                                   data->user_data,
4461                                                   error);
4462       g_simple_async_result_complete (res);
4463       g_error_free (error);
4464       load_contents_data_free (data);
4465       g_object_unref (res);
4466     }
4467 }
4468
4469 /**
4470  * g_file_load_partial_contents_async:
4471  * @file: input #GFile.
4472  * @cancellable: optional #GCancellable object, %NULL to ignore.
4473  * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
4474  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4475  * @user_data: the data to pass to the callback functions.
4476  *
4477  * Reads the partial contents of a file. A #GFileReadMoreCallback should be 
4478  * used to stop reading from the file when appropriate, else this function
4479  * will behave exactly as g_file_load_contents_async(). This operation 
4480  * can be finished by g_file_load_partial_contents_finish().
4481  *
4482  * Users of this function should be aware that @user_data is passed to 
4483  * both the @read_more_callback and the @callback.
4484  *
4485  * If @cancellable is not %NULL, then the operation can be cancelled by
4486  * triggering the cancellable object from another thread. If the operation
4487  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4488  **/
4489 void
4490 g_file_load_partial_contents_async (GFile                 *file,
4491                                     GCancellable          *cancellable,
4492                                     GFileReadMoreCallback  read_more_callback,
4493                                     GAsyncReadyCallback    callback,
4494                                     gpointer               user_data)
4495 {
4496   LoadContentsData *data;
4497
4498   g_return_if_fail (G_IS_FILE (file));
4499
4500   data = g_new0 (LoadContentsData, 1);
4501
4502   if (cancellable)
4503     data->cancellable = g_object_ref (cancellable);
4504   data->read_more_callback = read_more_callback;
4505   data->callback = callback;
4506   data->user_data = user_data;
4507   data->content = g_byte_array_new ();
4508   data->file = g_object_ref (file);
4509
4510   g_file_read_async (file,
4511                      0,
4512                      cancellable,
4513                      load_contents_open_callback,
4514                      data);
4515 }
4516
4517 /**
4518  * g_file_load_partial_contents_finish:
4519  * @file: input #GFile.
4520  * @res: a #GAsyncResult. 
4521  * @contents: a location to place the contents of the file.
4522  * @length: a location to place the length of the contents of the file.
4523  * @etag_out: a location to place the current entity tag for the file.
4524  * @error: a #GError, or %NULL
4525  * 
4526  * Finishes an asynchronous partial load operation that was started
4527  * with g_file_load_partial_contents_async().
4528  *
4529  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
4530  * present, it will be set appropriately. 
4531  **/
4532 gboolean
4533 g_file_load_partial_contents_finish (GFile         *file,
4534                                      GAsyncResult  *res,
4535                                      char         **contents,
4536                                      gsize         *length,
4537                                      char         **etag_out,
4538                                      GError       **error)
4539 {
4540   GSimpleAsyncResult *simple;
4541   LoadContentsData *data;
4542
4543   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4544   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4545   g_return_val_if_fail (contents != NULL, FALSE);
4546
4547   simple = G_SIMPLE_ASYNC_RESULT (res);
4548
4549   if (g_simple_async_result_propagate_error (simple, error))
4550     return FALSE;
4551   
4552   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4553   
4554   data = g_simple_async_result_get_op_res_gpointer (simple);
4555
4556   if (data->error)
4557     {
4558       g_propagate_error (error, data->error);
4559       data->error = NULL;
4560       *contents = NULL;
4561       if (length)
4562         *length = 0;
4563       return FALSE;
4564     }
4565
4566   if (length)
4567     *length = data->pos;
4568
4569   if (etag_out)
4570     {
4571       *etag_out = data->etag;
4572       data->etag = NULL;
4573     }
4574
4575   /* Zero terminate */
4576   g_byte_array_set_size (data->content, data->pos + 1);
4577   data->content->data[data->pos] = 0;
4578   
4579   *contents = (char *)g_byte_array_free (data->content, FALSE);
4580   data->content = NULL;
4581
4582   return TRUE;
4583 }
4584
4585 /**
4586  * g_file_load_contents_async:
4587  * @file: input #GFile.
4588  * @cancellable: optional #GCancellable object, %NULL to ignore.
4589  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4590  * @user_data: the data to pass to callback function
4591  * 
4592  * Starts an asynchronous load of the @file's contents.
4593  *
4594  * For more details, see g_file_load_contents() which is
4595  * the synchronous version of this call.
4596  *
4597  * When the load operation has completed, @callback will be called 
4598  * with @user data. To finish the operation, call 
4599  * g_file_load_contents_finish() with the #GAsyncResult returned by 
4600  * the @callback.
4601  * 
4602  * If @cancellable is not %NULL, then the operation can be cancelled by
4603  * triggering the cancellable object from another thread. If the operation
4604  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4605  **/
4606 void
4607 g_file_load_contents_async (GFile               *file,
4608                            GCancellable        *cancellable,
4609                            GAsyncReadyCallback  callback,
4610                            gpointer             user_data)
4611 {
4612   g_file_load_partial_contents_async (file,
4613                                       cancellable,
4614                                       NULL,
4615                                       callback, user_data);
4616 }
4617
4618 /**
4619  * g_file_load_contents_finish:
4620  * @file: input #GFile.
4621  * @res: a #GAsyncResult. 
4622  * @contents: a location to place the contents of the file.
4623  * @length: a location to place the length of the contents of the file.
4624  * @etag_out: a location to place the current entity tag for the file.
4625  * @error: a #GError, or %NULL
4626  * 
4627  * Finishes an asynchronous load of the @file's contents. 
4628  * The contents are placed in @contents, and @length is set to the 
4629  * size of the @contents string. If @etag_out is present, it will be 
4630  * set to the new entity tag for the @file.
4631  * 
4632  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
4633  * present, it will be set appropriately. 
4634  **/
4635 gboolean
4636 g_file_load_contents_finish (GFile         *file,
4637                              GAsyncResult  *res,
4638                              char         **contents,
4639                              gsize         *length,
4640                              char         **etag_out,
4641                              GError       **error)
4642 {
4643   return g_file_load_partial_contents_finish (file,
4644                                               res,
4645                                               contents,
4646                                               length,
4647                                               etag_out,
4648                                               error);
4649 }
4650   
4651 /**
4652  * g_file_replace_contents:
4653  * @file: input #GFile.
4654  * @contents: a string containing the new contents for @file.
4655  * @length: the length of @contents in bytes.
4656  * @etag: the old <link linkend="gfile-etag">entity tag</link> 
4657  *     for the document.
4658  * @make_backup: %TRUE if a backup should be created.
4659  * @flags: a set of #GFileCreateFlags.
4660  * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4661  *      for the document.
4662  * @cancellable: optional #GCancellable object, %NULL to ignore.
4663  * @error: a #GError, or %NULL
4664  *
4665  * Replaces the contents of @file with @contents of @length bytes.
4666  
4667  * If @etag is specified (not %NULL) any existing file must have that etag, or
4668  * the error %G_IO_ERROR_WRONG_ETAG will be returned.
4669  *
4670  * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
4671  * 
4672  * If @cancellable is not %NULL, then the operation can be cancelled by
4673  * triggering the cancellable object from another thread. If the operation
4674  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4675  *
4676  * The returned @new_etag can be used to verify that the file hasn't changed the
4677  * next time it is saved over.
4678  * 
4679  * Returns: %TRUE if successful. If an error
4680  * has occurred, this function will return %FALSE and set @error
4681  * appropriately if present.
4682  **/
4683 gboolean
4684 g_file_replace_contents (GFile             *file,
4685                          const char        *contents,
4686                          gsize              length,
4687                          const char        *etag,
4688                          gboolean           make_backup,
4689                          GFileCreateFlags   flags,
4690                          char             **new_etag,
4691                          GCancellable      *cancellable,
4692                          GError           **error)
4693 {
4694   GFileOutputStream *out;
4695   gsize pos, remainder;
4696   gssize res;
4697
4698   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4699   g_return_val_if_fail (contents != NULL, FALSE);
4700
4701   out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4702   if (out == NULL)
4703     return FALSE;
4704
4705   pos = 0;
4706   remainder = length;
4707   while (remainder > 0 &&
4708          (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4709                                        contents + pos,
4710                                        MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4711                                        cancellable,
4712                                        error)) > 0)
4713     {
4714       pos += res;
4715       remainder -= res;
4716     }
4717   
4718   if (remainder > 0 && res < 0)
4719     {
4720       /* Ignore errors on close */
4721       g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4722       
4723       /* error is set already */
4724       return FALSE;
4725     }
4726   
4727   if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4728     return FALSE;
4729
4730   if (new_etag)
4731     *new_etag = g_file_output_stream_get_etag (out);
4732   
4733   return TRUE;
4734 }
4735
4736 typedef struct {
4737   GFile *file;
4738   GError *error;
4739   GCancellable *cancellable;
4740   GAsyncReadyCallback callback;
4741   gpointer user_data;
4742   const char *content;
4743   gsize length;
4744   gsize pos;
4745   char *etag;
4746 } ReplaceContentsData;
4747
4748 static void
4749 replace_contents_data_free (ReplaceContentsData *data)
4750 {
4751   if (data->error)
4752     g_error_free (data->error);
4753   if (data->cancellable)
4754     g_object_unref (data->cancellable);
4755   g_object_unref (data->file);
4756   g_free (data->etag);
4757   g_free (data);
4758 }
4759
4760 static void
4761 replace_contents_close_callback (GObject      *obj,
4762                                  GAsyncResult *close_res,
4763                                  gpointer      user_data)
4764 {
4765   GOutputStream *stream = G_OUTPUT_STREAM (obj);
4766   ReplaceContentsData *data = user_data;
4767   GSimpleAsyncResult *res;
4768
4769   /* Ignore errors here, we're only reading anyway */
4770   g_output_stream_close_finish (stream, close_res, NULL);
4771   g_object_unref (stream);
4772
4773   data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4774   
4775   res = g_simple_async_result_new (G_OBJECT (data->file),
4776                                    data->callback,
4777                                    data->user_data,
4778                                    g_file_replace_contents_async);
4779   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4780   g_simple_async_result_complete (res);
4781   g_object_unref (res);
4782 }
4783
4784 static void
4785 replace_contents_write_callback (GObject      *obj,
4786                                  GAsyncResult *read_res,
4787                                  gpointer      user_data)
4788 {
4789   GOutputStream *stream = G_OUTPUT_STREAM (obj);
4790   ReplaceContentsData *data = user_data;
4791   GError *error = NULL;
4792   gssize write_size;
4793   
4794   write_size = g_output_stream_write_finish (stream, read_res, &error);
4795
4796   if (write_size <= 0) 
4797     {
4798       /* Error or EOF, close the file */
4799       if (write_size < 0)
4800         data->error = error;
4801       g_output_stream_close_async (stream, 0,
4802                                    data->cancellable,
4803                                    replace_contents_close_callback, data);
4804     }
4805   else if (write_size > 0)
4806     {
4807       data->pos += write_size;
4808
4809       if (data->pos >= data->length)
4810         g_output_stream_close_async (stream, 0,
4811                                      data->cancellable,
4812                                      replace_contents_close_callback, data);
4813       else
4814         g_output_stream_write_async (stream,
4815                                      data->content + data->pos,
4816                                      data->length - data->pos,
4817                                      0,
4818                                      data->cancellable,
4819                                      replace_contents_write_callback,
4820                                      data);
4821     }
4822 }
4823
4824 static void
4825 replace_contents_open_callback (GObject      *obj,
4826                                 GAsyncResult *open_res,
4827                                 gpointer      user_data)
4828 {
4829   GFile *file = G_FILE (obj);
4830   GFileOutputStream *stream;
4831   ReplaceContentsData *data = user_data;
4832   GError *error = NULL;
4833   GSimpleAsyncResult *res;
4834
4835   stream = g_file_replace_finish (file, open_res, &error);
4836
4837   if (stream)
4838     {
4839       g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4840                                    data->content + data->pos,
4841                                    data->length - data->pos,
4842                                    0,
4843                                    data->cancellable,
4844                                    replace_contents_write_callback,
4845                                    data);
4846       
4847     }
4848   else
4849     {
4850       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4851                                                   data->callback,
4852                                                   data->user_data,
4853                                                   error);
4854       g_simple_async_result_complete (res);
4855       g_error_free (error);
4856       replace_contents_data_free (data);
4857       g_object_unref (res);
4858     }
4859 }
4860
4861 /**
4862  * g_file_replace_contents_async:
4863  * @file: input #GFile.
4864  * @contents: string of contents to replace the file with.
4865  * @length: the length of @contents in bytes.
4866  * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4867  * @make_backup: %TRUE if a backup should be created.
4868  * @flags: a set of #GFileCreateFlags.
4869  * @cancellable: optional #GCancellable object, %NULL to ignore.
4870  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4871  * @user_data: the data to pass to callback function
4872  * 
4873  * Starts an asynchronous replacement of @file with the given 
4874  * @contents of @length bytes. @etag will replace the document's 
4875  * current entity tag.
4876  * 
4877  * When this operation has completed, @callback will be called with
4878  * @user_user data, and the operation can be finalized with 
4879  * g_file_replace_contents_finish().
4880  * 
4881  * If @cancellable is not %NULL, then the operation can be cancelled by
4882  * triggering the cancellable object from another thread. If the operation
4883  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4884  * 
4885  * If @make_backup is %TRUE, this function will attempt to 
4886  * make a backup of @file.
4887  **/
4888 void
4889 g_file_replace_contents_async  (GFile               *file,
4890                                 const char          *contents,
4891                                 gsize                length,
4892                                 const char          *etag,
4893                                 gboolean             make_backup,
4894                                 GFileCreateFlags     flags,
4895                                 GCancellable        *cancellable,
4896                                 GAsyncReadyCallback  callback,
4897                                 gpointer             user_data)
4898 {
4899   ReplaceContentsData *data;
4900
4901   g_return_if_fail (G_IS_FILE (file));
4902   g_return_if_fail (contents != NULL);
4903
4904   data = g_new0 (ReplaceContentsData, 1);
4905
4906   if (cancellable)
4907     data->cancellable = g_object_ref (cancellable);
4908   data->callback = callback;
4909   data->user_data = user_data;
4910   data->content = contents;
4911   data->length = length;
4912   data->pos = 0;
4913   data->file = g_object_ref (file);
4914
4915   g_file_replace_async (file,
4916                         etag,
4917                         make_backup,
4918                         flags,
4919                         0,
4920                         cancellable,
4921                         replace_contents_open_callback,
4922                         data);
4923 }
4924   
4925 /**
4926  * g_file_replace_contents_finish:
4927  * @file: input #GFile.
4928  * @res: a #GAsyncResult. 
4929  * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link> 
4930  *     for the document.
4931  * @error: a #GError, or %NULL 
4932  * 
4933  * Finishes an asynchronous replace of the given @file. See
4934  * g_file_replace_contents_async(). Sets @new_etag to the new entity 
4935  * tag for the document, if present.
4936  * 
4937  * Returns: %TRUE on success, %FALSE on failure.
4938  **/
4939 gboolean
4940 g_file_replace_contents_finish (GFile         *file,
4941                                 GAsyncResult  *res,
4942                                 char         **new_etag,
4943                                 GError       **error)
4944 {
4945   GSimpleAsyncResult *simple;
4946   ReplaceContentsData *data;
4947
4948   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4949   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4950
4951   simple = G_SIMPLE_ASYNC_RESULT (res);
4952
4953   if (g_simple_async_result_propagate_error (simple, error))
4954     return FALSE;
4955   
4956   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
4957   
4958   data = g_simple_async_result_get_op_res_gpointer (simple);
4959
4960   if (data->error)
4961     {
4962       g_propagate_error (error, data->error);
4963       data->error = NULL;
4964       return FALSE;
4965     }
4966
4967
4968   if (new_etag)
4969     {
4970       *new_etag = data->etag;
4971       data->etag = NULL; /* Take ownership */
4972     }
4973   
4974   return TRUE;
4975 }
4976
4977 #define __G_FILE_C__
4978 #include "gioaliasdef.c"