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