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