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