Implement this function by moving bits from glocalfileinfo.c
[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.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 interanally, 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 #undef g_file_contains_file
673
674 /**
675  * g_file_contains_file:
676  * @parent: input #GFile.
677  * @descendant: input #GFile.
678  *
679  * Deprecated version of g_file_has_prefix().
680  *
681  * Returns:  %TRUE if the @descendant's parent, grandparent, etc is @parent. %FALSE otherwise.
682  *
683  * Deprecated:2.16: The initial chosen name was unfortunate, as it
684  * may cause you to think this function did more than just
685  * filename comparisons.
686  */
687 gboolean
688 g_file_contains_file (GFile *parent,
689                       GFile *descendant)
690 {
691   /* This function is not in the header and will not be referenced by newly built code */
692   return g_file_has_prefix (descendant, parent);
693 }
694
695 /**
696  * g_file_has_prefix:
697  * @file: input #GFile.
698  * @prefix: input #GFile.
699  * 
700  * Checks whether @file has the prefix specified by @prefix. In other word, if the
701  * names of inital elements of @file<!-- -->s pathname match @prefix.
702  * 
703  * This call does no i/o, as it works purely on names. As such it can sometimes
704  * return %FALSE even if @file is inside a @prefix (from a filesystem point of view),
705  * because the prefix of @file is an alias of @prefix.
706  *
707  * Returns:  %TRUE if the @files's parent, grandparent, etc is @prefix. %FALSE otherwise.
708  **/
709 gboolean
710 g_file_has_prefix (GFile *file,
711                    GFile *prefix)
712 {
713   GFileIface *iface;
714   
715   g_return_val_if_fail (G_IS_FILE (file), FALSE);
716   g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
717
718   if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
719     return FALSE;
720   
721   iface = G_FILE_GET_IFACE (file);
722
723   /* The vtable function differs in arg order since we're
724      using the old contains_file call */
725   return (* iface->prefix_matches) (prefix, file);
726 }
727
728 /**
729  * g_file_get_relative_path:
730  * @parent: input #GFile.
731  * @descendant: input #GFile.
732  *
733  * Gets the path for @descendant relative to @parent. 
734  *
735  * This call does no blocking i/o.
736  * 
737  * Returns: string with the relative path from @descendant 
738  *     to @parent, or %NULL if @descendant doesn't have @parent as prefix. The returned string should be freed with 
739  *     g_free() when no longer needed.
740  **/
741 char *
742 g_file_get_relative_path (GFile *parent,
743                           GFile *descendant)
744 {
745   GFileIface *iface;
746   
747   g_return_val_if_fail (G_IS_FILE (parent), NULL);
748   g_return_val_if_fail (G_IS_FILE (descendant), NULL);
749
750   if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
751     return NULL;
752   
753   iface = G_FILE_GET_IFACE (parent);
754
755   return (* iface->get_relative_path) (parent, descendant);
756 }
757
758 /**
759  * g_file_resolve_relative_path:
760  * @file: input #GFile.
761  * @relative_path: a given relative path string.
762  *
763  * Resolves a relative path for @file to an absolute path.
764  *
765  * This call does no blocking i/o.
766  * 
767  * Returns: #GFile to the resolved path. %NULL if @relative_path 
768  *     is %NULL or if @file is invalid.
769  **/
770 GFile *
771 g_file_resolve_relative_path (GFile      *file,
772                               const char *relative_path)
773 {
774   GFileIface *iface;
775
776   g_return_val_if_fail (G_IS_FILE (file), NULL);
777   g_return_val_if_fail (relative_path != NULL, NULL);
778
779   iface = G_FILE_GET_IFACE (file);
780
781   return (* iface->resolve_relative_path) (file, relative_path);
782 }
783
784 /**
785  * g_file_enumerate_children:
786  * @file: input #GFile.
787  * @attributes: an attribute query string.
788  * @flags: a set of #GFileQueryInfoFlags.
789  * @cancellable: optional #GCancellable object, %NULL to ignore.
790  * @error: #GError for error reporting.
791  *
792  * Gets the requested information about the files in a directory. The result
793  * is a #GFileEnumerator object that will give out #GFileInfo objects for
794  * all the files in the directory.
795  *
796  * The @attribute value is a string that specifies the file attributes that
797  * should be gathered. It is not an error if it's not possible to read a particular
798  * requested attribute from a file - it just won't be set. @attribute should
799  * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
800  * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
801  * namespace. An example attribute query be "standard::*,owner::user".
802  * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
803  *
804  * If @cancellable is not %NULL, then the operation can be cancelled by
805  * triggering the cancellable object from another thread. If the operation
806  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
807  * 
808  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
809  * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
810  * Other errors are possible too.
811  *
812  * Returns: A #GFileEnumerator if successful, %NULL on error. 
813  **/
814 GFileEnumerator *
815 g_file_enumerate_children (GFile                *file,
816                            const char           *attributes,
817                            GFileQueryInfoFlags   flags,
818                            GCancellable         *cancellable,
819                            GError              **error)
820                            
821 {
822   GFileIface *iface;
823   
824   g_return_val_if_fail (G_IS_FILE (file), NULL);
825
826   if (g_cancellable_set_error_if_cancelled (cancellable, error))
827     return NULL;
828   
829   iface = G_FILE_GET_IFACE (file);
830
831   if (iface->enumerate_children == NULL)
832     {
833       g_set_error (error, G_IO_ERROR,
834                    G_IO_ERROR_NOT_SUPPORTED,
835                    _("Operation not supported"));
836       return NULL;
837     }
838
839   return (* iface->enumerate_children) (file, attributes, flags,
840                                         cancellable, error);
841 }
842
843 /**
844  * g_file_enumerate_children_async:
845  * @file: input #GFile.
846  * @attributes: an attribute query string.
847  * @flags: a set of #GFileQueryInfoFlags.
848  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
849  *     of the request.
850  * @cancellable: optional #GCancellable object, %NULL to ignore.
851  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
852  * @user_data: the data to pass to callback function
853  *
854  * Asynchronously gets the requested information about the files in a directory. The result
855  * is a #GFileEnumerator object that will give out #GFileInfo objects for
856  * all the files in the directory.
857  *
858  * For more details, see g_file_enumerate_children() which is
859  * the synchronous version of this call.
860  *
861  * When the operation is finished, @callback will be called. You can then call
862  * g_file_enumerate_children_finish() to get the result of the operation.
863  **/
864 void
865 g_file_enumerate_children_async (GFile               *file,
866                                  const char          *attributes,
867                                  GFileQueryInfoFlags  flags,
868                                  int                  io_priority,
869                                  GCancellable        *cancellable,
870                                  GAsyncReadyCallback  callback,
871                                  gpointer             user_data)
872 {
873   GFileIface *iface;
874
875   g_return_if_fail (G_IS_FILE (file));
876
877   iface = G_FILE_GET_IFACE (file);
878   (* iface->enumerate_children_async) (file,
879                                        attributes,
880                                        flags,
881                                        io_priority,
882                                        cancellable,
883                                        callback,
884                                        user_data);
885 }
886
887 /**
888  * g_file_enumerate_children_finish:
889  * @file: input #GFile.
890  * @res: a #GAsyncResult.
891  * @error: a #GError.
892  * 
893  * Finishes an async enumerate children operation.
894  * See g_file_enumerate_children_async().
895  *
896  * Returns: a #GFileEnumerator or %NULL if an error occurred.
897  **/
898 GFileEnumerator *
899 g_file_enumerate_children_finish (GFile         *file,
900                                   GAsyncResult  *res,
901                                   GError       **error)
902 {
903   GFileIface *iface;
904   
905   g_return_val_if_fail (G_IS_FILE (file), NULL);
906   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
907
908   if (G_IS_SIMPLE_ASYNC_RESULT (res))
909     {
910       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
911       if (g_simple_async_result_propagate_error (simple, error))
912         return NULL;
913     }
914   
915   iface = G_FILE_GET_IFACE (file);
916   return (* iface->enumerate_children_finish) (file, res, error);
917 }
918
919 /**
920  * g_file_query_exists:
921  * @file: input #GFile.
922  * @cancellable: optional #GCancellable object, %NULL to ignore.
923  *
924  * Utility function to check if a particular file exists. This is
925  * implemented using g_file_query_info() and as such does blocking I/O.
926  *
927  * Note that in many cases it is racy to first check for file existance
928  * and then execute something based on the outcome of that, because the
929  * file might have been created or removed inbetween the operations. The
930  * general approach to handling that is to not check, but just do the
931  * operation and handle the errors as they come.
932  *
933  * As an example of race-free checking, take the case of reading a file, and
934  * if it doesn't exist, creating it. There are two racy versions: read it, and
935  * on error create it; and: check if it exists, if not create it. These
936  * can both result in two processes creating the file (with perhaps a partially
937  * written file as the result). The correct approach is to always try to create
938  * the file with g_file_create() which will either atomically create the file
939  * or fail with a G_IO_ERROR_EXISTS error.
940  *
941  * However, in many cases an existance check is useful in a user
942  * interface, for instance to make a menu item sensitive/insensitive, so that
943  * you don't have to fool users that something is possible and then just show
944  * and error dialog. If you do this, you should make sure to also handle the
945  * errors that can happen due to races when you execute the operation.
946  * 
947  * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
948  */
949 gboolean
950 g_file_query_exists (GFile *file,
951                      GCancellable *cancellable)
952 {
953   GFileInfo *info;
954
955   info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
956                             G_FILE_QUERY_INFO_NONE,
957                             cancellable, NULL);
958   if (info != NULL)
959     {
960       g_object_unref (info);
961       return TRUE;
962     }
963   
964   return FALSE;
965 }
966
967 /**
968  * g_file_query_info:
969  * @file: input #GFile.
970  * @attributes: an attribute query string.
971  * @flags: a set of #GFileQueryInfoFlags.
972  * @cancellable: optional #GCancellable object, %NULL to ignore.
973  * @error: a #GError.
974  *
975  * Gets the requested information about specified @file. The result
976  * is a #GFileInfo object that contains key-value attributes (such as 
977  * the type or size of the file).
978  *
979  * The @attribute value is a string that specifies the file attributes that
980  * should be gathered. It is not an error if it's not possible to read a particular
981  * requested attribute from a file - it just won't be set. @attribute should
982  * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
983  * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
984  * namespace. An example attribute query be "standard::*,owner::user".
985  * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
986  * 
987  * If @cancellable is not %NULL, then the operation can be cancelled by
988  * triggering the cancellable object from another thread. If the operation
989  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
990  *
991  * For symlinks, normally the information about the target of the
992  * symlink is returned, rather than information about the symlink itself.
993  * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
994  * information about the symlink itself will be returned. Also, for symlinks
995  * that point to non-existing files the information about the symlink itself
996  * will be returned.
997  *
998  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
999  * Other errors are possible too, and depend on what kind of filesystem the file is on.
1000  *
1001  * Returns: a #GFileInfo for the given @file, or %NULL on error.
1002  **/
1003 GFileInfo *
1004 g_file_query_info (GFile                *file,
1005                    const char           *attributes,
1006                    GFileQueryInfoFlags   flags,
1007                    GCancellable         *cancellable,
1008                    GError              **error)
1009 {
1010   GFileIface *iface;
1011   
1012   g_return_val_if_fail (G_IS_FILE (file), NULL);
1013
1014   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1015     return NULL;
1016   
1017   iface = G_FILE_GET_IFACE (file);
1018
1019   if (iface->query_info == NULL)
1020     {
1021       g_set_error (error, G_IO_ERROR,
1022                    G_IO_ERROR_NOT_SUPPORTED,
1023                    _("Operation not supported"));
1024       return NULL;
1025     }
1026   
1027   return (* iface->query_info) (file, attributes, flags, cancellable, error);
1028 }
1029
1030 /**
1031  * g_file_query_info_async:
1032  * @file: input #GFile.
1033  * @attributes: an attribute query string.
1034  * @flags: a set of #GFileQueryInfoFlags.
1035  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1036  *     of the request.
1037  * @cancellable: optional #GCancellable object, %NULL to ignore. 
1038  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1039  * @user_data: the data to pass to callback function
1040  * 
1041  * Asynchronously gets the requested information about specified @file. The result
1042  * is a #GFileInfo object that contains key-value attributes (such as type or size
1043  * for the file).
1044  * 
1045  * For more details, see g_file_query_info() which is
1046  * the synchronous version of this call.
1047  *
1048  * When the operation is finished, @callback will be called. You can then call
1049  * g_file_query_info_finish() to get the result of the operation.
1050  **/
1051 void
1052 g_file_query_info_async (GFile               *file,
1053                          const char          *attributes,
1054                          GFileQueryInfoFlags  flags,
1055                          int                  io_priority,
1056                          GCancellable        *cancellable,
1057                          GAsyncReadyCallback  callback,
1058                          gpointer             user_data)
1059 {
1060   GFileIface *iface;
1061   
1062   g_return_if_fail (G_IS_FILE (file));
1063
1064   iface = G_FILE_GET_IFACE (file);
1065   (* iface->query_info_async) (file,
1066                                attributes,
1067                                flags,
1068                                io_priority,
1069                                cancellable,
1070                                callback,
1071                                user_data);
1072 }
1073
1074 /**
1075  * g_file_query_info_finish:
1076  * @file: input #GFile.
1077  * @res: a #GAsyncResult. 
1078  * @error: a #GError. 
1079  * 
1080  * Finishes an asynchronous file info query. 
1081  * See g_file_query_info_async().
1082  * 
1083  * Returns: #GFileInfo for given @file or %NULL on error.
1084  **/
1085 GFileInfo *
1086 g_file_query_info_finish (GFile         *file,
1087                           GAsyncResult  *res,
1088                           GError       **error)
1089 {
1090   GFileIface *iface;
1091
1092   g_return_val_if_fail (G_IS_FILE (file), NULL);
1093   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1094
1095   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1096     {
1097       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1098       if (g_simple_async_result_propagate_error (simple, error))
1099         return NULL;
1100     }
1101   
1102   iface = G_FILE_GET_IFACE (file);
1103   return (* iface->query_info_finish) (file, res, error);
1104 }
1105
1106 /**
1107  * g_file_query_filesystem_info:
1108  * @file: input #GFile.
1109  * @attributes:  an attribute query string.
1110  * @cancellable: optional #GCancellable object, %NULL to ignore. 
1111  * @error: a #GError. 
1112  * 
1113  * Similar to g_file_query_info(), but obtains information
1114  * about the filesystem the @file is on, rather than the file itself.
1115  * For instance the amount of space available and the type of
1116  * the filesystem.
1117  *
1118  * The @attribute value is a string that specifies the file attributes that
1119  * should be gathered. It is not an error if it's not possible to read a particular
1120  * requested attribute from a file - it just won't be set. @attribute should
1121  * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1122  * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1123  * namespace. The standard namespace for filesystem attributes is "fs".
1124  * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1125  * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1126  * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1127  * 
1128  * If @cancellable is not %NULL, then the operation can be cancelled by
1129  * triggering the cancellable object from another thread. If the operation
1130  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1131  *
1132  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1133  * Other errors are possible too, and depend on what kind of filesystem the file is on.
1134  *
1135  * Returns: a #GFileInfo or %NULL if there was an error.
1136  **/
1137 GFileInfo *
1138 g_file_query_filesystem_info (GFile         *file,
1139                               const char    *attributes,
1140                               GCancellable  *cancellable,
1141                               GError       **error)
1142 {
1143   GFileIface *iface;
1144   
1145   g_return_val_if_fail (G_IS_FILE (file), NULL);
1146
1147   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1148     return NULL;
1149   
1150   iface = G_FILE_GET_IFACE (file);
1151
1152   if (iface->query_filesystem_info == NULL)
1153     {
1154       g_set_error (error, G_IO_ERROR,
1155                    G_IO_ERROR_NOT_SUPPORTED,
1156                    _("Operation not supported"));
1157       return NULL;
1158     }
1159   
1160   return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1161 }
1162
1163 /**
1164  * g_file_query_filesystem_info_async:
1165  * @file: input #GFile.
1166  * @attributes: an attribute query string.
1167  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1168  *     of the request.
1169  * @cancellable: optional #GCancellable object, %NULL to ignore. 
1170  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1171  * @user_data: the data to pass to callback function
1172  * 
1173  * Asynchronously gets the requested information about the filesystem
1174  * that the specified @file is on. The result is a #GFileInfo object
1175  * that contains key-value attributes (such as type or size for the
1176  * file).
1177  * 
1178  * For more details, see g_file_query_filesystem_info() which is the
1179  * synchronous version of this call.
1180  *
1181  * When the operation is finished, @callback will be called. You can
1182  * then call g_file_query_info_finish() to get the result of the
1183  * operation.
1184  **/
1185 void
1186 g_file_query_filesystem_info_async (GFile               *file,
1187                                     const char          *attributes,
1188                                     int                  io_priority,
1189                                     GCancellable        *cancellable,
1190                                     GAsyncReadyCallback  callback,
1191                                     gpointer             user_data)
1192 {
1193   GFileIface *iface;
1194   
1195   g_return_if_fail (G_IS_FILE (file));
1196
1197   iface = G_FILE_GET_IFACE (file);
1198   (* iface->query_filesystem_info_async) (file,
1199                                           attributes,
1200                                           io_priority,
1201                                           cancellable,
1202                                           callback,
1203                                           user_data);
1204 }
1205
1206 /**
1207  * g_file_query_filesystem_info_finish:
1208  * @file: input #GFile.
1209  * @res: a #GAsyncResult. 
1210  * @error: a #GError. 
1211  * 
1212  * Finishes an asynchronous filesystem info query.  See
1213  * g_file_query_filesystem_info_async().
1214  * 
1215  * Returns: #GFileInfo for given @file or %NULL on error.
1216  **/
1217 GFileInfo *
1218 g_file_query_filesystem_info_finish (GFile         *file,
1219                                      GAsyncResult  *res,
1220                                      GError       **error)
1221 {
1222   GFileIface *iface;
1223
1224   g_return_val_if_fail (G_IS_FILE (file), NULL);
1225   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1226
1227   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1228     {
1229       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1230       if (g_simple_async_result_propagate_error (simple, error))
1231         return NULL;
1232     }
1233   
1234   iface = G_FILE_GET_IFACE (file);
1235   return (* iface->query_filesystem_info_finish) (file, res, error);
1236 }
1237
1238 /**
1239  * g_file_find_enclosing_mount:
1240  * @file: input #GFile.
1241  * @cancellable: optional #GCancellable object, %NULL to ignore. 
1242  * @error: a #GError. 
1243  *
1244  * Gets a #GMount for the #GFile. 
1245  *
1246  * If the #GFileIface for @file does not have a mount (e.g. possibly a 
1247  * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1248  * will be returned.
1249  * 
1250  * If @cancellable is not %NULL, then the operation can be cancelled by
1251  * triggering the cancellable object from another thread. If the operation
1252  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1253  * 
1254  * Returns: a #GMount where the @file is located or %NULL on error.
1255  **/
1256 GMount *
1257 g_file_find_enclosing_mount (GFile         *file,
1258                              GCancellable  *cancellable,
1259                              GError       **error)
1260 {
1261   GFileIface *iface;
1262
1263   g_return_val_if_fail (G_IS_FILE (file), NULL);
1264   
1265   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1266     return NULL;
1267
1268   iface = G_FILE_GET_IFACE (file);
1269   if (iface->find_enclosing_mount == NULL)
1270     {
1271       g_set_error (error, G_IO_ERROR,
1272                    G_IO_ERROR_NOT_FOUND,
1273                    _("Containing mount does not exist"));
1274       return NULL;
1275     }
1276   
1277   return (* iface->find_enclosing_mount) (file, cancellable, error);
1278 }
1279 /**
1280  * g_file_find_enclosing_mount_async:
1281  * @file: a #GFile
1282  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1283  *     of the request.
1284  * @cancellable: optional #GCancellable object, %NULL to ignore.
1285  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1286  * @user_data: the data to pass to callback function
1287  *
1288  * Asynchronously gets the mount for the file.
1289  *
1290  * For more details, see g_file_find_enclosing_mount() which is
1291  * the synchronous version of this call.
1292  *
1293  * When the operation is finished, @callback will be called. You can then call
1294  * g_file_find_enclosing_mount_finish() to get the result of the operation.
1295  */
1296 void
1297 g_file_find_enclosing_mount_async (GFile              *file,
1298                                    int                   io_priority,
1299                                    GCancellable         *cancellable,
1300                                    GAsyncReadyCallback   callback,
1301                                    gpointer              user_data)
1302 {
1303   GFileIface *iface;
1304
1305   g_return_if_fail (G_IS_FILE (file));
1306
1307   iface = G_FILE_GET_IFACE (file);
1308   (* iface->find_enclosing_mount_async) (file,
1309                                          io_priority,
1310                                          cancellable,
1311                                          callback,
1312                                          user_data);
1313 }
1314
1315 /**
1316  * g_file_find_enclosing_mount_finish:
1317  * @file: a #GFile
1318  * @res: a #GAsyncResult
1319  * @error: a #GError
1320  * 
1321  * Finishes an asynchronous find mount request. 
1322  * See g_file_find_enclosing_mount_async().
1323  * 
1324  * Returns: #GMount for given @file or %NULL on error.
1325  **/
1326 GMount *
1327 g_file_find_enclosing_mount_finish (GFile         *file,
1328                                     GAsyncResult  *res,
1329                                     GError       **error)
1330 {
1331   GFileIface *iface;
1332   
1333   g_return_val_if_fail (G_IS_FILE (file), NULL);
1334   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1335
1336   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1337     {
1338       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1339       if (g_simple_async_result_propagate_error (simple, error))
1340         return NULL;
1341     }
1342   
1343   iface = G_FILE_GET_IFACE (file);
1344   return (* iface->find_enclosing_mount_finish) (file, res, error);
1345 }
1346
1347
1348 /**
1349  * g_file_read:
1350  * @file: #GFile to read.
1351  * @cancellable: a #GCancellable
1352  * @error: a #GError, or %NULL
1353  *
1354  * Opens a file for reading. The result is a #GFileInputStream that
1355  * can be used to read the contents of the file.
1356  *
1357  * If @cancellable is not %NULL, then the operation can be cancelled by
1358  * triggering the cancellable object from another thread. If the operation
1359  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1360  * 
1361  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1362  * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1363  * Other errors are possible too, and depend on what kind of filesystem the file is on.
1364  *
1365  * Returns: #GFileInputStream or %NULL on error.
1366  **/
1367 GFileInputStream *
1368 g_file_read (GFile         *file,
1369              GCancellable  *cancellable,
1370              GError       **error)
1371 {
1372   GFileIface *iface;
1373   
1374   g_return_val_if_fail (G_IS_FILE (file), NULL);
1375
1376   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1377     return NULL;
1378
1379   iface = G_FILE_GET_IFACE (file);
1380
1381   if (iface->read_fn == NULL)
1382     {
1383       g_set_error (error, G_IO_ERROR,
1384                    G_IO_ERROR_NOT_SUPPORTED,
1385                    _("Operation not supported"));
1386       return NULL;
1387     }
1388   
1389   return (* iface->read_fn) (file, cancellable, error);
1390 }
1391
1392 /**
1393  * g_file_append_to:
1394  * @file: input #GFile.
1395  * @flags: a set of #GFileCreateFlags.
1396  * @cancellable: optional #GCancellable object, %NULL to ignore.
1397  * @error: a #GError, or %NULL
1398  *
1399  * Gets an output stream for appending data to the file. If
1400  * the file doesn't already exist it is created.
1401  *
1402  * By default files created are generally readable by everyone,
1403  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1404  * will be made readable only to the current user, to the level that
1405  * is supported on the target filesystem.
1406  *
1407  * If @cancellable is not %NULL, then the operation can be cancelled by
1408  * triggering the cancellable object from another thread. If the operation
1409  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1410  *
1411  * Some file systems don't allow all file names, and may
1412  * return an G_IO_ERROR_INVALID_FILENAME error.
1413  * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1414  * returned. Other errors are possible too, and depend on what kind of
1415  * filesystem the file is on.
1416  * 
1417  * Returns: a #GFileOutputStream.
1418  **/
1419 GFileOutputStream *
1420 g_file_append_to (GFile             *file,
1421                   GFileCreateFlags   flags,
1422                   GCancellable      *cancellable,
1423                   GError           **error)
1424 {
1425   GFileIface *iface;
1426
1427   g_return_val_if_fail (G_IS_FILE (file), NULL);
1428
1429   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1430     return NULL;
1431   
1432   iface = G_FILE_GET_IFACE (file);
1433
1434   if (iface->append_to == NULL)
1435     {
1436       g_set_error (error, G_IO_ERROR,
1437                    G_IO_ERROR_NOT_SUPPORTED,
1438                    _("Operation not supported"));
1439       return NULL;
1440     }
1441   
1442   return (* iface->append_to) (file, flags, cancellable, error);
1443 }
1444
1445 /**
1446  * g_file_create:
1447  * @file: input #GFile.
1448  * @flags: a set of #GFileCreateFlags.
1449  * @cancellable: optional #GCancellable object, %NULL to ignore.
1450  * @error: a #GError, or %NULL
1451  *
1452  * Creates a new file and returns an output stream for writing to it.
1453  * The file must not already exists.
1454  *
1455  * By default files created are generally readable by everyone,
1456  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1457  * will be made readable only to the current user, to the level that
1458  * is supported on the target filesystem.
1459  *
1460  * If @cancellable is not %NULL, then the operation can be cancelled by
1461  * triggering the cancellable object from another thread. If the operation
1462  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1463  *
1464  * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1465  * error will be returned.
1466  * Some file systems don't allow all file names, and may
1467  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1468  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1469  * Other errors are possible too, and depend on what kind of
1470  * filesystem the file is on.
1471  * 
1472  * Returns: a #GFileOutputStream for the newly created file, or 
1473  * %NULL on error.
1474  **/
1475 GFileOutputStream *
1476 g_file_create (GFile             *file,
1477                GFileCreateFlags   flags,
1478                GCancellable      *cancellable,
1479                GError           **error)
1480 {
1481   GFileIface *iface;
1482   
1483   g_return_val_if_fail (G_IS_FILE (file), NULL);
1484
1485   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1486     return NULL;
1487   
1488   iface = G_FILE_GET_IFACE (file);
1489
1490   if (iface->create == NULL)
1491     {
1492       g_set_error (error, G_IO_ERROR,
1493                    G_IO_ERROR_NOT_SUPPORTED,
1494                    _("Operation not supported"));
1495       return NULL;
1496     }
1497   
1498   return (* iface->create) (file, flags, cancellable, error);
1499 }
1500
1501 /**
1502  * g_file_replace:
1503  * @file: input #GFile.
1504  * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the 
1505  *     current #GFile, or #NULL to ignore.
1506  * @make_backup: %TRUE if a backup should be created.
1507  * @flags: a set of #GFileCreateFlags.
1508  * @cancellable: optional #GCancellable object, %NULL to ignore.
1509  * @error: a #GError, or %NULL
1510  *
1511  * Returns an output stream for overwriting the file, possibly
1512  * creating a backup copy of the file first.
1513  *
1514  * This will try to replace the file in the safest way possible so
1515  * that any errors during the writing will not affect an already
1516  * existing copy of the file. For instance, for local files it
1517  * may write to a temporary file and then atomically rename over
1518  * the destination when the stream is closed.
1519  * 
1520  * By default files created are generally readable by everyone,
1521  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1522  * will be made readable only to the current user, to the level that
1523  * is supported on the target filesystem.
1524  *
1525  * If @cancellable is not %NULL, then the operation can be cancelled by
1526  * triggering the cancellable object from another thread. If the operation
1527  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1528  * 
1529  * If you pass in a non-#NULL @etag value, then this value is
1530  * compared to the current entity tag of the file, and if they differ
1531  * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1532  * that the file has been changed since you last read it. You can get
1533  * the new etag from g_file_output_stream_get_etag() after you've
1534  * finished writing and closed the #GFileOutputStream. When you load
1535  * a new file you can use g_file_input_stream_query_info() to get
1536  * the etag of the file.
1537  * 
1538  * If @make_backup is %TRUE, this function will attempt to make a backup
1539  * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1540  * error will be returned. If you want to replace anyway, try again with
1541  * @make_backup set to %FALSE.
1542  *
1543  * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1544  * and if the file is some other form of non-regular file then a
1545  * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1546  * Some file systems don't allow all file names, and may
1547  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1548  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1549  * Other errors are possible too, and depend on what kind of
1550  * filesystem the file is on.
1551  *
1552  * Returns: a #GFileOutputStream or %NULL on error. 
1553  **/
1554 GFileOutputStream *
1555 g_file_replace (GFile             *file,
1556                 const char        *etag,
1557                 gboolean           make_backup,
1558                 GFileCreateFlags   flags,
1559                 GCancellable      *cancellable,
1560                 GError           **error)
1561 {
1562   GFileIface *iface;
1563
1564   g_return_val_if_fail (G_IS_FILE (file), NULL);
1565
1566   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1567     return NULL;
1568   
1569   iface = G_FILE_GET_IFACE (file);
1570
1571   if (iface->replace == NULL)
1572     {
1573       g_set_error (error, G_IO_ERROR,
1574                    G_IO_ERROR_NOT_SUPPORTED,
1575                    _("Operation not supported"));
1576       return NULL;
1577     }
1578   
1579   
1580   /* Handle empty tag string as NULL in consistent way. */
1581   if (etag && *etag == 0)
1582     etag = NULL;
1583   
1584   return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1585 }
1586
1587 /**
1588  * g_file_read_async:
1589  * @file: input #GFile.
1590  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1591  *     of the request. 
1592  * @cancellable: optional #GCancellable object, %NULL to ignore.
1593  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1594  * @user_data: the data to pass to callback function
1595  *
1596  * Asynchronously opens @file for reading.
1597  *
1598  * For more details, see g_file_read() which is
1599  * the synchronous version of this call.
1600  *
1601  * When the operation is finished, @callback will be called. You can then call
1602  * g_file_read_finish() to get the result of the operation.
1603  **/
1604 void
1605 g_file_read_async (GFile               *file,
1606                    int                  io_priority,
1607                    GCancellable        *cancellable,
1608                    GAsyncReadyCallback  callback,
1609                    gpointer             user_data)
1610 {
1611   GFileIface *iface;
1612   
1613   g_return_if_fail (G_IS_FILE (file));
1614
1615   iface = G_FILE_GET_IFACE (file);
1616   (* iface->read_async) (file,
1617                          io_priority,
1618                          cancellable,
1619                          callback,
1620                          user_data);
1621 }
1622
1623 /**
1624  * g_file_read_finish:
1625  * @file: input #GFile.
1626  * @res: a #GAsyncResult. 
1627  * @error: a #GError, or %NULL
1628  *
1629  * Finishes an asynchronous file read operation started with 
1630  * g_file_read_async(). 
1631  *  
1632  * Returns: a #GFileInputStream or %NULL on error.
1633  **/
1634 GFileInputStream *
1635 g_file_read_finish (GFile         *file,
1636                     GAsyncResult  *res,
1637                     GError       **error)
1638 {
1639   GFileIface *iface;
1640   
1641   g_return_val_if_fail (G_IS_FILE (file), NULL);
1642   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1643
1644   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1645     {
1646       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1647       if (g_simple_async_result_propagate_error (simple, error))
1648         return NULL;
1649     }
1650   
1651   iface = G_FILE_GET_IFACE (file);
1652   return (* iface->read_finish) (file, res, error);
1653 }
1654
1655 /**
1656  * g_file_append_to_async:
1657  * @file: input #GFile.
1658  * @flags: a set of #GFileCreateFlags.
1659  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1660  *     of the request. 
1661  * @cancellable: optional #GCancellable object, %NULL to ignore.
1662  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1663  * @user_data: the data to pass to callback function
1664  * 
1665  * Asynchronously opens @file for appending.
1666  *
1667  * For more details, see g_file_append_to() which is
1668  * the synchronous version of this call.
1669  *
1670  * When the operation is finished, @callback will be called. You can then call
1671  * g_file_append_to_finish() to get the result of the operation.
1672  **/
1673 void
1674 g_file_append_to_async (GFile               *file,
1675                         GFileCreateFlags     flags,
1676                         int                  io_priority,
1677                         GCancellable        *cancellable,
1678                         GAsyncReadyCallback  callback,
1679                         gpointer             user_data)
1680 {
1681   GFileIface *iface;
1682   
1683   g_return_if_fail (G_IS_FILE (file));
1684
1685   iface = G_FILE_GET_IFACE (file);
1686   (* iface->append_to_async) (file,
1687                               flags,
1688                               io_priority,
1689                               cancellable,
1690                               callback,
1691                               user_data);
1692 }
1693
1694 /**
1695  * g_file_append_to_finish:
1696  * @file: input #GFile.
1697  * @res: #GAsyncResult
1698  * @error: a #GError, or %NULL
1699  * 
1700  * Finishes an asynchronous file append operation started with 
1701  * g_file_append_to_async(). 
1702  * 
1703  * Returns: a valid #GFileOutputStream or %NULL on error.
1704  **/
1705 GFileOutputStream *
1706 g_file_append_to_finish (GFile         *file,
1707                          GAsyncResult  *res,
1708                          GError       **error)
1709 {
1710   GFileIface *iface;
1711   
1712   g_return_val_if_fail (G_IS_FILE (file), NULL);
1713   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1714
1715   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1716     {
1717       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1718       if (g_simple_async_result_propagate_error (simple, error))
1719         return NULL;
1720     }
1721   
1722   iface = G_FILE_GET_IFACE (file);
1723   return (* iface->append_to_finish) (file, res, error);
1724 }
1725
1726 /**
1727  * g_file_create_async:
1728  * @file: input #GFile.
1729  * @flags: a set of #GFileCreateFlags.
1730  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1731  *     of the request.
1732  * @cancellable: optional #GCancellable object, %NULL to ignore.
1733  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1734  * @user_data: the data to pass to callback function
1735  * 
1736  * Asynchronously creates a new file and returns an output stream for writing to it.
1737  * The file must not already exists.
1738  *
1739  * For more details, see g_file_create() which is
1740  * the synchronous version of this call.
1741  *
1742  * When the operation is finished, @callback will be called. You can then call
1743  * g_file_create_finish() to get the result of the operation.
1744  **/
1745 void
1746 g_file_create_async (GFile               *file,
1747                      GFileCreateFlags     flags,
1748                      int                  io_priority,
1749                      GCancellable        *cancellable,
1750                      GAsyncReadyCallback  callback,
1751                      gpointer             user_data)
1752 {
1753   GFileIface *iface;
1754   
1755   g_return_if_fail (G_IS_FILE (file));
1756
1757   iface = G_FILE_GET_IFACE (file);
1758   (* iface->create_async) (file,
1759                            flags,
1760                            io_priority,
1761                            cancellable,
1762                            callback,
1763                            user_data);
1764 }
1765
1766 /**
1767  * g_file_create_finish:
1768  * @file: input #GFile.
1769  * @res: a #GAsyncResult. 
1770  * @error: a #GError, or %NULL
1771  * 
1772  * Finishes an asynchronous file create operation started with 
1773  * g_file_create_async(). 
1774  * 
1775  * Returns: a #GFileOutputStream or %NULL on error.
1776  **/
1777 GFileOutputStream *
1778 g_file_create_finish (GFile         *file,
1779                       GAsyncResult  *res,
1780                       GError       **error)
1781 {
1782   GFileIface *iface;
1783   
1784   g_return_val_if_fail (G_IS_FILE (file), NULL);
1785   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1786
1787   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1788     {
1789       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1790       if (g_simple_async_result_propagate_error (simple, error))
1791         return NULL;
1792     }
1793   
1794   iface = G_FILE_GET_IFACE (file);
1795   return (* iface->create_finish) (file, res, error);
1796 }
1797
1798 /**
1799  * g_file_replace_async:
1800  * @file: input #GFile.
1801  * @etag: an <link linkend="gfile-etag">entity tag</link> for the 
1802  *     current #GFile, or NULL to ignore.
1803  * @make_backup: %TRUE if a backup should be created.
1804  * @flags: a set of #GFileCreateFlags.
1805  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1806  *     of the request.
1807  * @cancellable: optional #GCancellable object, %NULL to ignore.
1808  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1809  * @user_data: the data to pass to callback function
1810  *
1811  * Asynchronously overwrites the file, replacing the contents, possibly
1812  * creating a backup copy of the file first.
1813  *
1814  * For more details, see g_file_replace() which is
1815  * the synchronous version of this call.
1816  *
1817  * When the operation is finished, @callback will be called. You can then call
1818  * g_file_replace_finish() to get the result of the operation.
1819  **/
1820 void
1821 g_file_replace_async (GFile               *file,
1822                       const char          *etag,
1823                       gboolean             make_backup,
1824                       GFileCreateFlags     flags,
1825                       int                  io_priority,
1826                       GCancellable        *cancellable,
1827                       GAsyncReadyCallback  callback,
1828                       gpointer             user_data)
1829 {
1830   GFileIface *iface;
1831   
1832   g_return_if_fail (G_IS_FILE (file));
1833
1834   iface = G_FILE_GET_IFACE (file);
1835   (* iface->replace_async) (file,
1836                             etag,
1837                             make_backup,
1838                             flags,
1839                             io_priority,
1840                             cancellable,
1841                             callback,
1842                             user_data);
1843 }
1844
1845 /**
1846  * g_file_replace_finish:
1847  * @file: input #GFile.
1848  * @res: a #GAsyncResult. 
1849  * @error: a #GError, or %NULL
1850  * 
1851  * Finishes an asynchronous file replace operation started with 
1852  * g_file_replace_async(). 
1853  * 
1854  * Returns: a #GFileOutputStream, or %NULL on error.
1855  **/
1856 GFileOutputStream *
1857 g_file_replace_finish (GFile         *file,
1858                        GAsyncResult  *res,
1859                        GError       **error)
1860 {
1861   GFileIface *iface;
1862   
1863   g_return_val_if_fail (G_IS_FILE (file), NULL);
1864   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1865
1866   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1867     {
1868       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1869       if (g_simple_async_result_propagate_error (simple, error))
1870         return NULL;
1871     }
1872   
1873   iface = G_FILE_GET_IFACE (file);
1874   return (* iface->replace_finish) (file, res, error);
1875 }
1876
1877 static gboolean
1878 copy_symlink (GFile           *destination,
1879               GFileCopyFlags   flags,
1880               GCancellable    *cancellable,
1881               const char      *target,
1882               GError         **error)
1883 {
1884   GError *my_error;
1885   gboolean tried_delete;
1886   GFileInfo *info;
1887   GFileType file_type;
1888
1889   tried_delete = FALSE;
1890
1891  retry:
1892   my_error = NULL;
1893   if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1894     {
1895       /* Maybe it already existed, and we want to overwrite? */
1896       if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) && 
1897           my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1898         {
1899           g_error_free (my_error);
1900
1901
1902           /* Don't overwrite if the destination is a directory */
1903           info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1904                                     G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1905                                     cancellable, &my_error);
1906           if (info != NULL)
1907             {
1908               file_type = g_file_info_get_file_type (info);
1909               g_object_unref (info);
1910               
1911               if (file_type == G_FILE_TYPE_DIRECTORY)
1912                 {
1913                   g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1914                                _("Can't copy over directory"));
1915                   return FALSE;
1916                 }
1917             }
1918           
1919           if (!g_file_delete (destination, cancellable, error))
1920             return FALSE;
1921           
1922           tried_delete = TRUE;
1923           goto retry;
1924         }
1925             /* Nah, fail */
1926       g_propagate_error (error, my_error);
1927       return FALSE;
1928     }
1929
1930   return TRUE;
1931 }
1932
1933 static GInputStream *
1934 open_source_for_copy (GFile           *source,
1935                       GFile           *destination,
1936                       GFileCopyFlags   flags,
1937                       GCancellable    *cancellable,
1938                       GError         **error)
1939 {
1940   GError *my_error;
1941   GInputStream *in;
1942   GFileInfo *info;
1943   GFileType file_type;
1944   
1945   my_error = NULL;
1946   in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1947   if (in != NULL)
1948     return in;
1949
1950   /* There was an error opening the source, try to set a good error for it: */
1951
1952   if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1953     {
1954       /* The source is a directory, don't fail with WOULD_RECURSE immediately, 
1955        * as that is less useful to the app. Better check for errors on the 
1956        * target instead. 
1957        */
1958       g_error_free (my_error);
1959       my_error = NULL;
1960       
1961       info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1962                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1963                                 cancellable, &my_error);
1964       if (info != NULL)
1965         {
1966           file_type = g_file_info_get_file_type (info);
1967           g_object_unref (info);
1968           
1969           if (flags & G_FILE_COPY_OVERWRITE)
1970             {
1971               if (file_type == G_FILE_TYPE_DIRECTORY)
1972                 {
1973                   g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1974                                _("Can't copy directory over directory"));
1975                   return NULL;
1976                 }
1977               /* continue to would_recurse error */
1978             }
1979           else
1980             {
1981               g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1982                            _("Target file exists"));
1983               return NULL;
1984             }
1985         }
1986       else
1987         {
1988           /* Error getting info from target, return that error 
1989            * (except for NOT_FOUND, which is no error here) 
1990            */
1991           if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1992             {
1993               g_propagate_error (error, my_error);
1994               return NULL;
1995             }
1996           g_error_free (my_error);
1997         }
1998       
1999       g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2000                    _("Can't recursively copy directory"));
2001       return NULL;
2002     }
2003
2004   g_propagate_error (error, my_error);
2005   return NULL;
2006 }
2007
2008 static gboolean
2009 should_copy (GFileAttributeInfo *info, 
2010              gboolean            as_move)
2011 {
2012   if (as_move)
2013     return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2014   return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2015 }
2016
2017 static char *
2018 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2019                                GFileAttributeInfoList *namespaces,
2020                                gboolean                as_move)
2021 {
2022   GString *s;
2023   gboolean first;
2024   int i;
2025   
2026   first = TRUE;
2027   s = g_string_new ("");
2028
2029   if (attributes)
2030     {
2031       for (i = 0; i < attributes->n_infos; i++)
2032         {
2033           if (should_copy (&attributes->infos[i], as_move))
2034             {
2035               if (first)
2036                 first = FALSE;
2037               else
2038                 g_string_append_c (s, ',');
2039                 
2040               g_string_append (s, attributes->infos[i].name);
2041             }
2042         }
2043     }
2044
2045   if (namespaces)
2046     {
2047       for (i = 0; i < namespaces->n_infos; i++)
2048         {
2049           if (should_copy (&namespaces->infos[i], as_move))
2050             {
2051               if (first)
2052                 first = FALSE;
2053               else
2054                 g_string_append_c (s, ',');
2055                 
2056               g_string_append (s, namespaces->infos[i].name);
2057               g_string_append (s, ":*");
2058             }
2059         }
2060     }
2061
2062   return g_string_free (s, FALSE);
2063 }
2064
2065 /**
2066  * g_file_copy_attributes:
2067  * @source: a #GFile with attributes.
2068  * @destination: a #GFile to copy attributes to.
2069  * @flags: a set of #GFileCopyFlags.
2070  * @cancellable: optional #GCancellable object, %NULL to ignore.
2071  * @error: a #GError, %NULL to ignore.
2072  *
2073  * Copies the file attributes from @source to @destination. 
2074  *
2075  * Normally only a subset of the file attributes are copied,
2076  * those that are copies in a normal file copy operation
2077  * (which for instance does not include e.g. mtime). However
2078  * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2079  * all the metadata that is possible to copy is copied.
2080  *
2081  * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2082  **/
2083 gboolean
2084 g_file_copy_attributes (GFile           *source,
2085                         GFile           *destination,
2086                         GFileCopyFlags   flags,
2087                         GCancellable    *cancellable,
2088                         GError         **error)
2089 {
2090   GFileAttributeInfoList *attributes, *namespaces;
2091   char *attrs_to_read;
2092   gboolean res;
2093   GFileInfo *info;
2094   gboolean as_move;
2095   gboolean source_nofollow_symlinks;
2096
2097   as_move = flags & G_FILE_COPY_ALL_METADATA;
2098   source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2099
2100   /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2101   attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2102   namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2103
2104   if (attributes == NULL && namespaces == NULL)
2105     return TRUE;
2106
2107   attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2108
2109   /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2110    * we just don't copy it. 
2111    */
2112   info = g_file_query_info (source, attrs_to_read,
2113                             source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2114                             cancellable,
2115                             NULL);
2116
2117   g_free (attrs_to_read);
2118   
2119   res = TRUE;
2120   if  (info)
2121     {
2122       res = g_file_set_attributes_from_info (destination,
2123                                              info, 0,
2124                                              cancellable,
2125                                              error);
2126       g_object_unref (info);
2127     }
2128   
2129   g_file_attribute_info_list_unref (attributes);
2130   g_file_attribute_info_list_unref (namespaces);
2131   
2132   return res;
2133 }
2134
2135 /* Closes the streams */
2136 static gboolean
2137 copy_stream_with_progress (GInputStream           *in,
2138                            GOutputStream          *out,
2139                            GCancellable           *cancellable,
2140                            GFileProgressCallback   progress_callback,
2141                            gpointer                progress_callback_data,
2142                            GError                **error)
2143 {
2144   gssize n_read, n_written;
2145   goffset current_size;
2146   char buffer[8192], *p;
2147   gboolean res;
2148   goffset total_size;
2149   GFileInfo *info;
2150
2151   total_size = 0;
2152   info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2153                                          G_FILE_ATTRIBUTE_STANDARD_SIZE,
2154                                          cancellable, NULL);
2155   if (info)
2156     {
2157       total_size = g_file_info_get_size (info);
2158       g_object_unref (info);
2159     }
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   
2206   /* Don't care about errors in source here */
2207   g_input_stream_close (in, cancellable, NULL);
2208
2209   /* But write errors on close are bad! */
2210   if (!g_output_stream_close (out, cancellable, error))
2211     res = FALSE;
2212
2213   g_object_unref (in);
2214   g_object_unref (out);
2215       
2216   return res;
2217 }
2218
2219 static gboolean
2220 file_copy_fallback (GFile                  *source,
2221                     GFile                  *destination,
2222                     GFileCopyFlags          flags,
2223                     GCancellable           *cancellable,
2224                     GFileProgressCallback   progress_callback,
2225                     gpointer                progress_callback_data,
2226                     GError                **error)
2227 {
2228   GInputStream *in;
2229   GOutputStream *out;
2230   GFileInfo *info;
2231   const char *target;
2232
2233   /* Maybe copy the symlink? */
2234   if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2235     {
2236       info = g_file_query_info (source,
2237                                 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2238                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2239                                 cancellable,
2240                                 error);
2241       if (info == NULL)
2242         return FALSE;
2243
2244       if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2245           (target = g_file_info_get_symlink_target (info)) != NULL)
2246         {
2247           if (!copy_symlink (destination, flags, cancellable, target, error))
2248             {
2249               g_object_unref (info);
2250               return FALSE;
2251             }
2252           
2253           g_object_unref (info);
2254           goto copied_file;
2255         }
2256       
2257       g_object_unref (info);
2258     }
2259   
2260   in = open_source_for_copy (source, destination, flags, cancellable, error);
2261   if (in == NULL)
2262     return FALSE;
2263   
2264   if (flags & G_FILE_COPY_OVERWRITE)
2265     {
2266       out = (GOutputStream *)g_file_replace (destination,
2267                                              NULL, 0,
2268                                              flags & G_FILE_COPY_BACKUP,
2269                                              cancellable, error);
2270     }
2271   else
2272     {
2273       out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2274     }
2275
2276   if (out == NULL)
2277     {
2278       g_object_unref (in);
2279       return FALSE;
2280     }
2281
2282   if (!copy_stream_with_progress (in, out, cancellable,
2283                                   progress_callback, progress_callback_data,
2284                                   error))
2285     return FALSE;
2286
2287  copied_file:
2288
2289   /* Ignore errors here. Failure to copy metadata is not a hard error */
2290   g_file_copy_attributes (source, destination,
2291                           flags, cancellable, NULL);
2292   
2293   return TRUE;
2294 }
2295
2296 /**
2297  * g_file_copy:
2298  * @source: input #GFile.
2299  * @destination: destination #GFile
2300  * @flags: set of #GFileCopyFlags
2301  * @cancellable: optional #GCancellable object, %NULL to ignore.
2302  * @progress_callback: function to callback with progress information
2303  * @progress_callback_data: user data to pass to @progress_callback
2304  * @error: #GError to set on error, or %NULL
2305  *
2306  * Copies the file @source to the location specified by @destination.
2307  * Can not handle recursive copies of directories.
2308  *
2309  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2310  * existing @destination file is overwritten.
2311  *
2312  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2313  * will be copied as symlinks, otherwise the target of the
2314  * @source symlink will be copied.
2315  *
2316  * If @cancellable is not %NULL, then the operation can be cancelled by
2317  * triggering the cancellable object from another thread. If the operation
2318  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2319  * 
2320  * If @progress_callback is not %NULL, then the operation can be monitored by
2321  * setting this to a #GFileProgressCallback function. @progress_callback_data
2322  * will be passed to this function. It is guaranteed that this callback will
2323  * be called after all data has been transferred with the total number of bytes
2324  * copied during the operation.
2325  * 
2326  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2327  * error is returned, independent on the status of the @destination.
2328  *
2329  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2330  * error G_IO_ERROR_EXISTS is returned.
2331  *
2332  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2333  * error is returned. If trying to overwrite a directory with a directory the
2334  * G_IO_ERROR_WOULD_MERGE error is returned.
2335  *
2336  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2337  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2338  * is returned.
2339  *
2340  * If you are interested in copying the #GFile object itself (not the on-disk
2341  * file), see g_file_dup().
2342  *
2343  * Returns: %TRUE on success, %FALSE otherwise.
2344  **/
2345 gboolean
2346 g_file_copy (GFile                  *source,
2347              GFile                  *destination,
2348              GFileCopyFlags          flags,
2349              GCancellable           *cancellable,
2350              GFileProgressCallback   progress_callback,
2351              gpointer                progress_callback_data,
2352              GError                **error)
2353 {
2354   GFileIface *iface;
2355   GError *my_error;
2356   gboolean res;
2357
2358   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2359   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2360
2361   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2362     return FALSE;
2363   
2364   iface = G_FILE_GET_IFACE (destination);
2365   if (iface->copy)
2366     {
2367       my_error = NULL;
2368       res = (* iface->copy) (source, destination,
2369                              flags, cancellable,
2370                              progress_callback, progress_callback_data,
2371                              &my_error);
2372       
2373       if (res)
2374         return TRUE;
2375       
2376       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2377         {
2378           g_propagate_error (error, my_error);
2379               return FALSE;
2380         }
2381     }
2382
2383   /* If the types are different, and the destination method failed
2384      also try the source method */
2385   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2386     {
2387       iface = G_FILE_GET_IFACE (source);
2388       
2389       if (iface->copy)
2390         {
2391           my_error = NULL;
2392           res = (* iface->copy) (source, destination,
2393                                  flags, cancellable,
2394                                  progress_callback, progress_callback_data,
2395                                  &my_error);
2396           
2397           if (res)
2398             return TRUE;
2399           
2400           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2401             {
2402               g_propagate_error (error, my_error);
2403               return FALSE;
2404             }
2405         }
2406     }
2407   
2408   return file_copy_fallback (source, destination, flags, cancellable,
2409                              progress_callback, progress_callback_data,
2410                              error);
2411 }
2412
2413 /**
2414  * g_file_copy_async:
2415  * @source: input #GFile.
2416  * @destination: destination #GFile
2417  * @flags: set of #GFileCopyFlags
2418  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2419  *     of the request. 
2420  * @cancellable: optional #GCancellable object, %NULL to ignore.
2421  * @progress_callback: function to callback with progress information
2422  * @progress_callback_data: user data to pass to @progress_callback
2423  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2424  * @user_data: the data to pass to callback function
2425  *
2426  * Copies the file @source to the location specified by @destination 
2427  * asynchronously. For details of the behaviour, see g_file_copy().
2428  *
2429  * If @progress_callback is not %NULL, then that function that will be called
2430  * just like in g_file_copy(), however the callback will run in the main loop,
2431  * not in the thread that is doing the I/O operation.
2432  *
2433  * When the operation is finished, @callback will be called. You can then call
2434  * g_file_copy_finish() to get the result of the operation.
2435  **/
2436 void
2437 g_file_copy_async (GFile                  *source,
2438                    GFile                  *destination,
2439                    GFileCopyFlags          flags,
2440                    int                     io_priority,
2441                    GCancellable           *cancellable,
2442                    GFileProgressCallback   progress_callback,
2443                    gpointer                progress_callback_data,
2444                    GAsyncReadyCallback     callback,
2445                    gpointer                user_data)
2446 {
2447   GFileIface *iface;
2448
2449   g_return_if_fail (G_IS_FILE (source));
2450   g_return_if_fail (G_IS_FILE (destination));
2451
2452   iface = G_FILE_GET_IFACE (source);
2453   (* iface->copy_async) (source,
2454                          destination,
2455                          flags,
2456                          io_priority,
2457                          cancellable,
2458                          progress_callback,
2459                          progress_callback_data,
2460                          callback,
2461                          user_data);
2462 }
2463
2464 /**
2465  * g_file_copy_finish:
2466  * @file: input #GFile.
2467  * @res: a #GAsyncResult. 
2468  * @error: a #GError, or %NULL
2469  * 
2470  * Finishes copying the file started with 
2471  * g_file_copy_async().
2472  * 
2473  * Returns: a %TRUE on success, %FALSE on error.
2474  **/
2475 gboolean
2476 g_file_copy_finish (GFile        *file,
2477                     GAsyncResult *res,
2478                     GError      **error)
2479 {
2480   GFileIface *iface;
2481   
2482   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2483   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2484
2485   if (G_IS_SIMPLE_ASYNC_RESULT (res))
2486     {
2487       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2488       
2489       if (g_simple_async_result_propagate_error (simple, error))
2490         return FALSE;
2491     }
2492   
2493   iface = G_FILE_GET_IFACE (file);
2494   return (* iface->copy_finish) (file, res, error);
2495 }
2496
2497 /**
2498  * g_file_move:
2499  * @source: #GFile pointing to the source location.
2500  * @destination: #GFile pointing to the destination location.
2501  * @flags: set of #GFileCopyFlags.
2502  * @cancellable: optional #GCancellable object, %NULL to ignore.
2503  * @progress_callback: #GFileProgressCallback function for updates.
2504  * @progress_callback_data: gpointer to user data for the callback function.
2505  * @error: #GError for returning error conditions, or %NULL
2506  *
2507  *
2508  * Tries to move the file or directory @source to the location specified by @destination.
2509  * If native move operations are supported then this is used, otherwise a copy + delete
2510  * fallback is used. The native implementation may support moving directories (for instance
2511  * on moves inside the same filesystem), but the fallback code does not.
2512  * 
2513  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2514  * existing @destination file is overwritten.
2515  *
2516  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2517  * will be copied as symlinks, otherwise the target of the
2518  * @source symlink will be copied.
2519  *
2520  * If @cancellable is not %NULL, then the operation can be cancelled by
2521  * triggering the cancellable object from another thread. If the operation
2522  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2523  * 
2524  * If @progress_callback is not %NULL, then the operation can be monitored by
2525  * setting this to a #GFileProgressCallback function. @progress_callback_data
2526  * will be passed to this function. It is guaranteed that this callback will
2527  * be called after all data has been transferred with the total number of bytes
2528  * copied during the operation.
2529  * 
2530  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2531  * error is returned, independent on the status of the @destination.
2532  *
2533  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2534  * error G_IO_ERROR_EXISTS is returned.
2535  *
2536  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2537  * error is returned. If trying to overwrite a directory with a directory the
2538  * G_IO_ERROR_WOULD_MERGE error is returned.
2539  *
2540  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2541  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2542  * may be returned (if the native move operation isn't available).
2543  *
2544  * Returns: %TRUE on successful move, %FALSE otherwise.
2545  **/
2546 gboolean
2547 g_file_move (GFile                  *source,
2548              GFile                  *destination,
2549              GFileCopyFlags          flags,
2550              GCancellable           *cancellable,
2551              GFileProgressCallback   progress_callback,
2552              gpointer                progress_callback_data,
2553              GError                **error)
2554 {
2555   GFileIface *iface;
2556   GError *my_error;
2557   gboolean res;
2558
2559   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2560   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2561
2562   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2563     return FALSE;
2564   
2565   iface = G_FILE_GET_IFACE (destination);
2566   if (iface->move)
2567     {
2568       my_error = NULL;
2569       res = (* iface->move) (source, destination,
2570                              flags, cancellable,
2571                              progress_callback, progress_callback_data,
2572                              &my_error);
2573       
2574       if (res)
2575         return TRUE;
2576       
2577       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2578         {
2579           g_propagate_error (error, my_error);
2580           return FALSE;
2581         }
2582     }
2583
2584   /* If the types are different, and the destination method failed
2585      also try the source method */
2586   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2587     {
2588       iface = G_FILE_GET_IFACE (source);
2589       
2590       if (iface->move)
2591         {
2592           my_error = NULL;
2593           res = (* iface->move) (source, destination,
2594                                  flags, cancellable,
2595                                  progress_callback, progress_callback_data,
2596                                  &my_error);
2597           
2598           if (res)
2599             return TRUE;
2600           
2601           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2602             {
2603               g_propagate_error (error, my_error);
2604               return FALSE;
2605             }
2606         }
2607     }
2608   
2609   if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2610     {  
2611       g_set_error (error, G_IO_ERROR,
2612                    G_IO_ERROR_NOT_SUPPORTED,
2613                    _("Operation not supported"));
2614       return FALSE;
2615     }
2616   
2617   flags |= G_FILE_COPY_ALL_METADATA;
2618   if (!g_file_copy (source, destination, flags, cancellable,
2619                     progress_callback, progress_callback_data,
2620                     error))
2621     return FALSE;
2622
2623   return g_file_delete (source, cancellable, error);
2624 }
2625
2626 /**
2627  * g_file_make_directory
2628  * @file: input #GFile.
2629  * @cancellable: optional #GCancellable object, %NULL to ignore.
2630  * @error: a #GError, or %NULL 
2631  *
2632  * Creates a directory.
2633  * 
2634  * If @cancellable is not %NULL, then the operation can be cancelled by
2635  * triggering the cancellable object from another thread. If the operation
2636  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2637  * 
2638  * Returns: %TRUE on successful creation, %FALSE otherwise.
2639  **/
2640 gboolean
2641 g_file_make_directory (GFile         *file,
2642                        GCancellable  *cancellable,
2643                        GError       **error)
2644 {
2645   GFileIface *iface;
2646
2647   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2648
2649   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2650     return FALSE;
2651   
2652   iface = G_FILE_GET_IFACE (file);
2653
2654   if (iface->make_directory == NULL)
2655     {
2656       g_set_error (error, G_IO_ERROR,
2657                    G_IO_ERROR_NOT_SUPPORTED,
2658                    _("Operation not supported"));
2659       return FALSE;
2660     }
2661   
2662   return (* iface->make_directory) (file, cancellable, error);
2663 }
2664
2665 /**
2666  * g_file_make_symbolic_link:
2667  * @file: input #GFile.
2668  * @symlink_value: a string with the value of the new symlink.
2669  * @cancellable: optional #GCancellable object, %NULL to ignore.
2670  * @error: a #GError. 
2671  * 
2672  * Creates a symbolic link.
2673  *
2674  * If @cancellable is not %NULL, then the operation can be cancelled by
2675  * triggering the cancellable object from another thread. If the operation
2676  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2677  * 
2678  * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2679  **/
2680 gboolean
2681 g_file_make_symbolic_link (GFile         *file,
2682                            const char    *symlink_value,
2683                            GCancellable  *cancellable,
2684                            GError       **error)
2685 {
2686   GFileIface *iface;
2687
2688   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2689   g_return_val_if_fail (symlink_value != NULL, FALSE);
2690
2691   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2692     return FALSE;
2693
2694   if (*symlink_value == '\0')
2695     {
2696       g_set_error (error, G_IO_ERROR,
2697                    G_IO_ERROR_INVALID_ARGUMENT,
2698                    _("Invalid symlink value given"));
2699       return FALSE;
2700     }
2701   
2702   iface = G_FILE_GET_IFACE (file);
2703
2704   if (iface->make_symbolic_link == NULL)
2705     {
2706       g_set_error (error, G_IO_ERROR,
2707                    G_IO_ERROR_NOT_SUPPORTED,
2708                    _("Operation not supported"));
2709       return FALSE;
2710     }
2711   
2712   return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2713 }
2714
2715 /**
2716  * g_file_delete:
2717  * @file: input #GFile.
2718  * @cancellable: optional #GCancellable object, %NULL to ignore.
2719  * @error: a #GError, or %NULL 
2720  * 
2721  * Deletes a file.
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"