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