Doc improvement
[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  * g_file_find_enclosing_mount_async:
1311  * @file: a #GFile
1312  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1313  *     of the request.
1314  * @cancellable: optional #GCancellable object, %NULL to ignore.
1315  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1316  * @user_data: the data to pass to callback function
1317  *
1318  * Asynchronously gets the mount for the file.
1319  *
1320  * For more details, see g_file_find_enclosing_mount() which is
1321  * the synchronous version of this call.
1322  *
1323  * When the operation is finished, @callback will be called. You can then call
1324  * g_file_find_enclosing_mount_finish() to get the result of the operation.
1325  */
1326 void
1327 g_file_find_enclosing_mount_async (GFile              *file,
1328                                    int                   io_priority,
1329                                    GCancellable         *cancellable,
1330                                    GAsyncReadyCallback   callback,
1331                                    gpointer              user_data)
1332 {
1333   GFileIface *iface;
1334
1335   g_return_if_fail (G_IS_FILE (file));
1336
1337   iface = G_FILE_GET_IFACE (file);
1338   (* iface->find_enclosing_mount_async) (file,
1339                                          io_priority,
1340                                          cancellable,
1341                                          callback,
1342                                          user_data);
1343 }
1344
1345 /**
1346  * g_file_find_enclosing_mount_finish:
1347  * @file: a #GFile
1348  * @res: a #GAsyncResult
1349  * @error: a #GError
1350  * 
1351  * Finishes an asynchronous find mount request. 
1352  * See g_file_find_enclosing_mount_async().
1353  * 
1354  * Returns: #GMount for given @file or %NULL on error.
1355  **/
1356 GMount *
1357 g_file_find_enclosing_mount_finish (GFile         *file,
1358                                     GAsyncResult  *res,
1359                                     GError       **error)
1360 {
1361   GFileIface *iface;
1362   
1363   g_return_val_if_fail (G_IS_FILE (file), NULL);
1364   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1365
1366   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1367     {
1368       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1369       if (g_simple_async_result_propagate_error (simple, error))
1370         return NULL;
1371     }
1372   
1373   iface = G_FILE_GET_IFACE (file);
1374   return (* iface->find_enclosing_mount_finish) (file, res, error);
1375 }
1376
1377
1378 /**
1379  * g_file_read:
1380  * @file: #GFile to read.
1381  * @cancellable: a #GCancellable
1382  * @error: a #GError, or %NULL
1383  *
1384  * Opens a file for reading. The result is a #GFileInputStream that
1385  * can be used to read the contents of the file.
1386  *
1387  * If @cancellable is not %NULL, then the operation can be cancelled by
1388  * triggering the cancellable object from another thread. If the operation
1389  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1390  * 
1391  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1392  * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1393  * Other errors are possible too, and depend on what kind of filesystem the file is on.
1394  *
1395  * Returns: #GFileInputStream or %NULL on error.
1396  **/
1397 GFileInputStream *
1398 g_file_read (GFile         *file,
1399              GCancellable  *cancellable,
1400              GError       **error)
1401 {
1402   GFileIface *iface;
1403   
1404   g_return_val_if_fail (G_IS_FILE (file), NULL);
1405
1406   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1407     return NULL;
1408
1409   iface = G_FILE_GET_IFACE (file);
1410
1411   if (iface->read_fn == NULL)
1412     {
1413       g_set_error_literal (error, G_IO_ERROR,
1414                            G_IO_ERROR_NOT_SUPPORTED,
1415                            _("Operation not supported"));
1416       return NULL;
1417     }
1418   
1419   return (* iface->read_fn) (file, cancellable, error);
1420 }
1421
1422 /**
1423  * g_file_append_to:
1424  * @file: input #GFile.
1425  * @flags: a set of #GFileCreateFlags.
1426  * @cancellable: optional #GCancellable object, %NULL to ignore.
1427  * @error: a #GError, or %NULL
1428  *
1429  * Gets an output stream for appending data to the file. If
1430  * the file doesn't already exist it is created.
1431  *
1432  * By default files created are generally readable by everyone,
1433  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1434  * will be made readable only to the current user, to the level that
1435  * is supported on the target filesystem.
1436  *
1437  * If @cancellable is not %NULL, then the operation can be cancelled by
1438  * triggering the cancellable object from another thread. If the operation
1439  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1440  *
1441  * Some file systems don't allow all file names, and may
1442  * return an %G_IO_ERROR_INVALID_FILENAME error.
1443  * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1444  * returned. Other errors are possible too, and depend on what kind of
1445  * filesystem the file is on.
1446  * 
1447  * Returns: a #GFileOutputStream, or %NULL on error
1448  **/
1449 GFileOutputStream *
1450 g_file_append_to (GFile             *file,
1451                   GFileCreateFlags   flags,
1452                   GCancellable      *cancellable,
1453                   GError           **error)
1454 {
1455   GFileIface *iface;
1456
1457   g_return_val_if_fail (G_IS_FILE (file), NULL);
1458
1459   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1460     return NULL;
1461   
1462   iface = G_FILE_GET_IFACE (file);
1463
1464   if (iface->append_to == NULL)
1465     {
1466       g_set_error_literal (error, G_IO_ERROR,
1467                            G_IO_ERROR_NOT_SUPPORTED,
1468                            _("Operation not supported"));
1469       return NULL;
1470     }
1471   
1472   return (* iface->append_to) (file, flags, cancellable, error);
1473 }
1474
1475 /**
1476  * g_file_create:
1477  * @file: input #GFile.
1478  * @flags: a set of #GFileCreateFlags.
1479  * @cancellable: optional #GCancellable object, %NULL to ignore.
1480  * @error: a #GError, or %NULL
1481  *
1482  * Creates a new file and returns an output stream for writing to it.
1483  * The file must not already exist.
1484  *
1485  * By default files created are generally readable by everyone,
1486  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1487  * will be made readable only to the current user, to the level that
1488  * is supported on the target filesystem.
1489  *
1490  * If @cancellable is not %NULL, then the operation can be cancelled by
1491  * triggering the cancellable object from another thread. If the operation
1492  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1493  *
1494  * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1495  * error will be returned.
1496  * Some file systems don't allow all file names, and may
1497  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1498  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1499  * Other errors are possible too, and depend on what kind of
1500  * filesystem the file is on.
1501  * 
1502  * Returns: a #GFileOutputStream for the newly created file, or 
1503  * %NULL on error.
1504  **/
1505 GFileOutputStream *
1506 g_file_create (GFile             *file,
1507                GFileCreateFlags   flags,
1508                GCancellable      *cancellable,
1509                GError           **error)
1510 {
1511   GFileIface *iface;
1512   
1513   g_return_val_if_fail (G_IS_FILE (file), NULL);
1514
1515   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1516     return NULL;
1517   
1518   iface = G_FILE_GET_IFACE (file);
1519
1520   if (iface->create == NULL)
1521     {
1522       g_set_error_literal (error, G_IO_ERROR,
1523                            G_IO_ERROR_NOT_SUPPORTED,
1524                            _("Operation not supported"));
1525       return NULL;
1526     }
1527   
1528   return (* iface->create) (file, flags, cancellable, error);
1529 }
1530
1531 /**
1532  * g_file_replace:
1533  * @file: input #GFile.
1534  * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the 
1535  *     current #GFile, or #NULL to ignore.
1536  * @make_backup: %TRUE if a backup should be created.
1537  * @flags: a set of #GFileCreateFlags.
1538  * @cancellable: optional #GCancellable object, %NULL to ignore.
1539  * @error: a #GError, or %NULL
1540  *
1541  * Returns an output stream for overwriting the file, possibly
1542  * creating a backup copy of the file first. If the file doesn't exist,
1543  * it will be created.
1544  *
1545  * This will try to replace the file in the safest way possible so
1546  * that any errors during the writing will not affect an already
1547  * existing copy of the file. For instance, for local files it
1548  * may write to a temporary file and then atomically rename over
1549  * the destination when the stream is closed.
1550  * 
1551  * By default files created are generally readable by everyone,
1552  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1553  * will be made readable only to the current user, to the level that
1554  * is supported on the target filesystem.
1555  *
1556  * If @cancellable is not %NULL, then the operation can be cancelled by
1557  * triggering the cancellable object from another thread. If the operation
1558  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1559  * 
1560  * If you pass in a non-#NULL @etag value, then this value is
1561  * compared to the current entity tag of the file, and if they differ
1562  * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1563  * that the file has been changed since you last read it. You can get
1564  * the new etag from g_file_output_stream_get_etag() after you've
1565  * finished writing and closed the #GFileOutputStream. When you load
1566  * a new file you can use g_file_input_stream_query_info() to get
1567  * the etag of the file.
1568  * 
1569  * If @make_backup is %TRUE, this function will attempt to make a backup
1570  * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1571  * error will be returned. If you want to replace anyway, try again with
1572  * @make_backup set to %FALSE.
1573  *
1574  * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1575  * and if the file is some other form of non-regular file then a
1576  * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1577  * Some file systems don't allow all file names, and may
1578  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1579  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1580  * Other errors are possible too, and depend on what kind of
1581  * filesystem the file is on.
1582  *
1583  * Returns: a #GFileOutputStream or %NULL on error. 
1584  **/
1585 GFileOutputStream *
1586 g_file_replace (GFile             *file,
1587                 const char        *etag,
1588                 gboolean           make_backup,
1589                 GFileCreateFlags   flags,
1590                 GCancellable      *cancellable,
1591                 GError           **error)
1592 {
1593   GFileIface *iface;
1594
1595   g_return_val_if_fail (G_IS_FILE (file), NULL);
1596
1597   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1598     return NULL;
1599   
1600   iface = G_FILE_GET_IFACE (file);
1601
1602   if (iface->replace == NULL)
1603     {
1604       g_set_error_literal (error, G_IO_ERROR,
1605                            G_IO_ERROR_NOT_SUPPORTED,
1606                            _("Operation not supported"));
1607       return NULL;
1608     }
1609   
1610   
1611   /* Handle empty tag string as NULL in consistent way. */
1612   if (etag && *etag == 0)
1613     etag = NULL;
1614   
1615   return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1616 }
1617
1618 /**
1619  * g_file_read_async:
1620  * @file: input #GFile.
1621  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1622  *     of the request. 
1623  * @cancellable: optional #GCancellable object, %NULL to ignore.
1624  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1625  * @user_data: the data to pass to callback function
1626  *
1627  * Asynchronously opens @file for reading.
1628  *
1629  * For more details, see g_file_read() which is
1630  * the synchronous version of this call.
1631  *
1632  * When the operation is finished, @callback will be called. You can then call
1633  * g_file_read_finish() to get the result of the operation.
1634  **/
1635 void
1636 g_file_read_async (GFile               *file,
1637                    int                  io_priority,
1638                    GCancellable        *cancellable,
1639                    GAsyncReadyCallback  callback,
1640                    gpointer             user_data)
1641 {
1642   GFileIface *iface;
1643   
1644   g_return_if_fail (G_IS_FILE (file));
1645
1646   iface = G_FILE_GET_IFACE (file);
1647   (* iface->read_async) (file,
1648                          io_priority,
1649                          cancellable,
1650                          callback,
1651                          user_data);
1652 }
1653
1654 /**
1655  * g_file_read_finish:
1656  * @file: input #GFile.
1657  * @res: a #GAsyncResult. 
1658  * @error: a #GError, or %NULL
1659  *
1660  * Finishes an asynchronous file read operation started with 
1661  * g_file_read_async(). 
1662  *  
1663  * Returns: a #GFileInputStream or %NULL on error.
1664  **/
1665 GFileInputStream *
1666 g_file_read_finish (GFile         *file,
1667                     GAsyncResult  *res,
1668                     GError       **error)
1669 {
1670   GFileIface *iface;
1671   
1672   g_return_val_if_fail (G_IS_FILE (file), NULL);
1673   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1674
1675   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1676     {
1677       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1678       if (g_simple_async_result_propagate_error (simple, error))
1679         return NULL;
1680     }
1681   
1682   iface = G_FILE_GET_IFACE (file);
1683   return (* iface->read_finish) (file, res, error);
1684 }
1685
1686 /**
1687  * g_file_append_to_async:
1688  * @file: input #GFile.
1689  * @flags: a set of #GFileCreateFlags.
1690  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1691  *     of the request. 
1692  * @cancellable: optional #GCancellable object, %NULL to ignore.
1693  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1694  * @user_data: the data to pass to callback function
1695  * 
1696  * Asynchronously opens @file for appending.
1697  *
1698  * For more details, see g_file_append_to() which is
1699  * the synchronous version of this call.
1700  *
1701  * When the operation is finished, @callback will be called. You can then call
1702  * g_file_append_to_finish() to get the result of the operation.
1703  **/
1704 void
1705 g_file_append_to_async (GFile               *file,
1706                         GFileCreateFlags     flags,
1707                         int                  io_priority,
1708                         GCancellable        *cancellable,
1709                         GAsyncReadyCallback  callback,
1710                         gpointer             user_data)
1711 {
1712   GFileIface *iface;
1713   
1714   g_return_if_fail (G_IS_FILE (file));
1715
1716   iface = G_FILE_GET_IFACE (file);
1717   (* iface->append_to_async) (file,
1718                               flags,
1719                               io_priority,
1720                               cancellable,
1721                               callback,
1722                               user_data);
1723 }
1724
1725 /**
1726  * g_file_append_to_finish:
1727  * @file: input #GFile.
1728  * @res: #GAsyncResult
1729  * @error: a #GError, or %NULL
1730  * 
1731  * Finishes an asynchronous file append operation started with 
1732  * g_file_append_to_async(). 
1733  * 
1734  * Returns: a valid #GFileOutputStream or %NULL on error.
1735  **/
1736 GFileOutputStream *
1737 g_file_append_to_finish (GFile         *file,
1738                          GAsyncResult  *res,
1739                          GError       **error)
1740 {
1741   GFileIface *iface;
1742   
1743   g_return_val_if_fail (G_IS_FILE (file), NULL);
1744   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1745
1746   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1747     {
1748       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1749       if (g_simple_async_result_propagate_error (simple, error))
1750         return NULL;
1751     }
1752   
1753   iface = G_FILE_GET_IFACE (file);
1754   return (* iface->append_to_finish) (file, res, error);
1755 }
1756
1757 /**
1758  * g_file_create_async:
1759  * @file: input #GFile.
1760  * @flags: a set of #GFileCreateFlags.
1761  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1762  *     of the request.
1763  * @cancellable: optional #GCancellable object, %NULL to ignore.
1764  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1765  * @user_data: the data to pass to callback function
1766  * 
1767  * Asynchronously creates a new file and returns an output stream for writing to it.
1768  * The file must not already exist.
1769  *
1770  * For more details, see g_file_create() which is
1771  * the synchronous version of this call.
1772  *
1773  * When the operation is finished, @callback will be called. You can then call
1774  * g_file_create_finish() to get the result of the operation.
1775  **/
1776 void
1777 g_file_create_async (GFile               *file,
1778                      GFileCreateFlags     flags,
1779                      int                  io_priority,
1780                      GCancellable        *cancellable,
1781                      GAsyncReadyCallback  callback,
1782                      gpointer             user_data)
1783 {
1784   GFileIface *iface;
1785   
1786   g_return_if_fail (G_IS_FILE (file));
1787
1788   iface = G_FILE_GET_IFACE (file);
1789   (* iface->create_async) (file,
1790                            flags,
1791                            io_priority,
1792                            cancellable,
1793                            callback,
1794                            user_data);
1795 }
1796
1797 /**
1798  * g_file_create_finish:
1799  * @file: input #GFile.
1800  * @res: a #GAsyncResult. 
1801  * @error: a #GError, or %NULL
1802  * 
1803  * Finishes an asynchronous file create operation started with 
1804  * g_file_create_async(). 
1805  * 
1806  * Returns: a #GFileOutputStream or %NULL on error.
1807  **/
1808 GFileOutputStream *
1809 g_file_create_finish (GFile         *file,
1810                       GAsyncResult  *res,
1811                       GError       **error)
1812 {
1813   GFileIface *iface;
1814   
1815   g_return_val_if_fail (G_IS_FILE (file), NULL);
1816   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1817
1818   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1819     {
1820       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1821       if (g_simple_async_result_propagate_error (simple, error))
1822         return NULL;
1823     }
1824   
1825   iface = G_FILE_GET_IFACE (file);
1826   return (* iface->create_finish) (file, res, error);
1827 }
1828
1829 /**
1830  * g_file_replace_async:
1831  * @file: input #GFile.
1832  * @etag: an <link linkend="gfile-etag">entity tag</link> for the 
1833  *     current #GFile, or NULL to ignore.
1834  * @make_backup: %TRUE if a backup should be created.
1835  * @flags: a set of #GFileCreateFlags.
1836  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1837  *     of the request.
1838  * @cancellable: optional #GCancellable object, %NULL to ignore.
1839  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1840  * @user_data: the data to pass to callback function
1841  *
1842  * Asynchronously overwrites the file, replacing the contents, possibly
1843  * creating a backup copy of the file first.
1844  *
1845  * For more details, see g_file_replace() which is
1846  * the synchronous version of this call.
1847  *
1848  * When the operation is finished, @callback will be called. You can then call
1849  * g_file_replace_finish() to get the result of the operation.
1850  **/
1851 void
1852 g_file_replace_async (GFile               *file,
1853                       const char          *etag,
1854                       gboolean             make_backup,
1855                       GFileCreateFlags     flags,
1856                       int                  io_priority,
1857                       GCancellable        *cancellable,
1858                       GAsyncReadyCallback  callback,
1859                       gpointer             user_data)
1860 {
1861   GFileIface *iface;
1862   
1863   g_return_if_fail (G_IS_FILE (file));
1864
1865   iface = G_FILE_GET_IFACE (file);
1866   (* iface->replace_async) (file,
1867                             etag,
1868                             make_backup,
1869                             flags,
1870                             io_priority,
1871                             cancellable,
1872                             callback,
1873                             user_data);
1874 }
1875
1876 /**
1877  * g_file_replace_finish:
1878  * @file: input #GFile.
1879  * @res: a #GAsyncResult. 
1880  * @error: a #GError, or %NULL
1881  * 
1882  * Finishes an asynchronous file replace operation started with 
1883  * g_file_replace_async(). 
1884  * 
1885  * Returns: a #GFileOutputStream, or %NULL on error.
1886  **/
1887 GFileOutputStream *
1888 g_file_replace_finish (GFile         *file,
1889                        GAsyncResult  *res,
1890                        GError       **error)
1891 {
1892   GFileIface *iface;
1893   
1894   g_return_val_if_fail (G_IS_FILE (file), NULL);
1895   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1896
1897   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1898     {
1899       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1900       if (g_simple_async_result_propagate_error (simple, error))
1901         return NULL;
1902     }
1903   
1904   iface = G_FILE_GET_IFACE (file);
1905   return (* iface->replace_finish) (file, res, error);
1906 }
1907
1908 static gboolean
1909 copy_symlink (GFile           *destination,
1910               GFileCopyFlags   flags,
1911               GCancellable    *cancellable,
1912               const char      *target,
1913               GError         **error)
1914 {
1915   GError *my_error;
1916   gboolean tried_delete;
1917   GFileInfo *info;
1918   GFileType file_type;
1919
1920   tried_delete = FALSE;
1921
1922  retry:
1923   my_error = NULL;
1924   if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1925     {
1926       /* Maybe it already existed, and we want to overwrite? */
1927       if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) && 
1928           my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1929         {
1930           g_error_free (my_error);
1931
1932
1933           /* Don't overwrite if the destination is a directory */
1934           info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1935                                     G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1936                                     cancellable, &my_error);
1937           if (info != NULL)
1938             {
1939               file_type = g_file_info_get_file_type (info);
1940               g_object_unref (info);
1941               
1942               if (file_type == G_FILE_TYPE_DIRECTORY)
1943                 {
1944                   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1945                                        _("Can't copy over directory"));
1946                   return FALSE;
1947                 }
1948             }
1949           
1950           if (!g_file_delete (destination, cancellable, error))
1951             return FALSE;
1952           
1953           tried_delete = TRUE;
1954           goto retry;
1955         }
1956             /* Nah, fail */
1957       g_propagate_error (error, my_error);
1958       return FALSE;
1959     }
1960
1961   return TRUE;
1962 }
1963
1964 static GInputStream *
1965 open_source_for_copy (GFile           *source,
1966                       GFile           *destination,
1967                       GFileCopyFlags   flags,
1968                       GCancellable    *cancellable,
1969                       GError         **error)
1970 {
1971   GError *my_error;
1972   GInputStream *in;
1973   GFileInfo *info;
1974   GFileType file_type;
1975   
1976   my_error = NULL;
1977   in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1978   if (in != NULL)
1979     return in;
1980
1981   /* There was an error opening the source, try to set a good error for it: */
1982
1983   if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1984     {
1985       /* The source is a directory, don't fail with WOULD_RECURSE immediately, 
1986        * as that is less useful to the app. Better check for errors on the 
1987        * target instead. 
1988        */
1989       g_error_free (my_error);
1990       my_error = NULL;
1991       
1992       info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1993                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1994                                 cancellable, &my_error);
1995       if (info != NULL)
1996         {
1997           file_type = g_file_info_get_file_type (info);
1998           g_object_unref (info);
1999           
2000           if (flags & G_FILE_COPY_OVERWRITE)
2001             {
2002               if (file_type == G_FILE_TYPE_DIRECTORY)
2003                 {
2004                   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2005                                        _("Can't copy directory over directory"));
2006                   return NULL;
2007                 }
2008               /* continue to would_recurse error */
2009             }
2010           else
2011             {
2012               g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2013                                    _("Target file exists"));
2014               return NULL;
2015             }
2016         }
2017       else
2018         {
2019           /* Error getting info from target, return that error 
2020            * (except for NOT_FOUND, which is no error here) 
2021            */
2022           if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
2023             {
2024               g_propagate_error (error, my_error);
2025               return NULL;
2026             }
2027           g_error_free (my_error);
2028         }
2029       
2030       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2031                            _("Can't recursively copy directory"));
2032       return NULL;
2033     }
2034
2035   g_propagate_error (error, my_error);
2036   return NULL;
2037 }
2038
2039 static gboolean
2040 should_copy (GFileAttributeInfo *info, 
2041              gboolean            as_move)
2042 {
2043   if (as_move)
2044     return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2045   return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2046 }
2047
2048 static char *
2049 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2050                                GFileAttributeInfoList *namespaces,
2051                                gboolean                as_move)
2052 {
2053   GString *s;
2054   gboolean first;
2055   int i;
2056   
2057   first = TRUE;
2058   s = g_string_new ("");
2059
2060   if (attributes)
2061     {
2062       for (i = 0; i < attributes->n_infos; i++)
2063         {
2064           if (should_copy (&attributes->infos[i], as_move))
2065             {
2066               if (first)
2067                 first = FALSE;
2068               else
2069                 g_string_append_c (s, ',');
2070                 
2071               g_string_append (s, attributes->infos[i].name);
2072             }
2073         }
2074     }
2075
2076   if (namespaces)
2077     {
2078       for (i = 0; i < namespaces->n_infos; i++)
2079         {
2080           if (should_copy (&namespaces->infos[i], as_move))
2081             {
2082               if (first)
2083                 first = FALSE;
2084               else
2085                 g_string_append_c (s, ',');
2086                 
2087               g_string_append (s, namespaces->infos[i].name);
2088               g_string_append (s, ":*");
2089             }
2090         }
2091     }
2092
2093   return g_string_free (s, FALSE);
2094 }
2095
2096 /**
2097  * g_file_copy_attributes:
2098  * @source: a #GFile with attributes.
2099  * @destination: a #GFile to copy attributes to.
2100  * @flags: a set of #GFileCopyFlags.
2101  * @cancellable: optional #GCancellable object, %NULL to ignore.
2102  * @error: a #GError, %NULL to ignore.
2103  *
2104  * Copies the file attributes from @source to @destination. 
2105  *
2106  * Normally only a subset of the file attributes are copied,
2107  * those that are copies in a normal file copy operation
2108  * (which for instance does not include e.g. mtime). However
2109  * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2110  * all the metadata that is possible to copy is copied.
2111  *
2112  * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2113  **/
2114 gboolean
2115 g_file_copy_attributes (GFile           *source,
2116                         GFile           *destination,
2117                         GFileCopyFlags   flags,
2118                         GCancellable    *cancellable,
2119                         GError         **error)
2120 {
2121   GFileAttributeInfoList *attributes, *namespaces;
2122   char *attrs_to_read;
2123   gboolean res;
2124   GFileInfo *info;
2125   gboolean as_move;
2126   gboolean source_nofollow_symlinks;
2127
2128   as_move = flags & G_FILE_COPY_ALL_METADATA;
2129   source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2130
2131   /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2132   attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2133   namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2134
2135   if (attributes == NULL && namespaces == NULL)
2136     return TRUE;
2137
2138   attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2139
2140   /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2141    * we just don't copy it. 
2142    */
2143   info = g_file_query_info (source, attrs_to_read,
2144                             source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2145                             cancellable,
2146                             NULL);
2147
2148   g_free (attrs_to_read);
2149   
2150   res = TRUE;
2151   if  (info)
2152     {
2153       res = g_file_set_attributes_from_info (destination,
2154                                              info, 0,
2155                                              cancellable,
2156                                              error);
2157       g_object_unref (info);
2158     }
2159   
2160   g_file_attribute_info_list_unref (attributes);
2161   g_file_attribute_info_list_unref (namespaces);
2162   
2163   return res;
2164 }
2165
2166 /* Closes the streams */
2167 static gboolean
2168 copy_stream_with_progress (GInputStream           *in,
2169                            GOutputStream          *out,
2170                            GFile                  *source,
2171                            GCancellable           *cancellable,
2172                            GFileProgressCallback   progress_callback,
2173                            gpointer                progress_callback_data,
2174                            GError                **error)
2175 {
2176   gssize n_read, n_written;
2177   goffset current_size;
2178   char buffer[1024*64], *p;
2179   gboolean res;
2180   goffset total_size;
2181   GFileInfo *info;
2182
2183   total_size = -1;
2184   info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2185                                          G_FILE_ATTRIBUTE_STANDARD_SIZE,
2186                                          cancellable, NULL);
2187   if (info)
2188     {
2189       if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2190         total_size = g_file_info_get_size (info);
2191       g_object_unref (info);
2192     }
2193
2194   if (total_size == -1)
2195     {
2196       info = g_file_query_info (source, 
2197                                 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2198                                 G_FILE_QUERY_INFO_NONE,
2199                                 cancellable, NULL);
2200       if (info)
2201         {
2202           if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2203             total_size = g_file_info_get_size (info);
2204           g_object_unref (info);
2205         }
2206     }
2207
2208   if (total_size == -1)
2209     total_size = 0;
2210   
2211   current_size = 0;
2212   res = TRUE;
2213   while (TRUE)
2214     {
2215       n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2216       if (n_read == -1)
2217         {
2218           res = FALSE;
2219           break;
2220         }
2221         
2222       if (n_read == 0)
2223         break;
2224
2225       current_size += n_read;
2226
2227       p = buffer;
2228       while (n_read > 0)
2229         {
2230           n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2231           if (n_written == -1)
2232             {
2233               res = FALSE;
2234               break;
2235             }
2236
2237           p += n_written;
2238           n_read -= n_written;
2239         }
2240
2241       if (!res)
2242         break;
2243
2244       if (progress_callback)
2245         progress_callback (current_size, total_size, progress_callback_data);
2246     }
2247
2248   if (!res)
2249     error = NULL; /* Ignore further errors */
2250
2251   /* Make sure we send full copied size */
2252   if (progress_callback)
2253     progress_callback (current_size, total_size, progress_callback_data);
2254   
2255   /* Don't care about errors in source here */
2256   g_input_stream_close (in, cancellable, NULL);
2257
2258   /* But write errors on close are bad! */
2259   if (!g_output_stream_close (out, cancellable, error))
2260     res = FALSE;
2261
2262   g_object_unref (in);
2263   g_object_unref (out);
2264       
2265   return res;
2266 }
2267
2268 static gboolean
2269 file_copy_fallback (GFile                  *source,
2270                     GFile                  *destination,
2271                     GFileCopyFlags          flags,
2272                     GCancellable           *cancellable,
2273                     GFileProgressCallback   progress_callback,
2274                     gpointer                progress_callback_data,
2275                     GError                **error)
2276 {
2277   GInputStream *in;
2278   GOutputStream *out;
2279   GFileInfo *info;
2280   const char *target;
2281
2282   /* Maybe copy the symlink? */
2283   if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2284     {
2285       info = g_file_query_info (source,
2286                                 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2287                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2288                                 cancellable,
2289                                 error);
2290       if (info == NULL)
2291         return FALSE;
2292
2293       if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2294           (target = g_file_info_get_symlink_target (info)) != NULL)
2295         {
2296           if (!copy_symlink (destination, flags, cancellable, target, error))
2297             {
2298               g_object_unref (info);
2299               return FALSE;
2300             }
2301           
2302           g_object_unref (info);
2303           goto copied_file;
2304         }
2305       
2306       g_object_unref (info);
2307     }
2308   
2309   in = open_source_for_copy (source, destination, flags, cancellable, error);
2310   if (in == NULL)
2311     return FALSE;
2312   
2313   if (flags & G_FILE_COPY_OVERWRITE)
2314     {
2315       out = (GOutputStream *)g_file_replace (destination,
2316                                              NULL,
2317                                              flags & G_FILE_COPY_BACKUP,
2318                                              0,
2319                                              cancellable, error);
2320     }
2321   else
2322     {
2323       out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2324     }
2325
2326   if (out == NULL)
2327     {
2328       g_object_unref (in);
2329       return FALSE;
2330     }
2331
2332   if (!copy_stream_with_progress (in, out, source, cancellable,
2333                                   progress_callback, progress_callback_data,
2334                                   error))
2335     return FALSE;
2336
2337  copied_file:
2338
2339   /* Ignore errors here. Failure to copy metadata is not a hard error */
2340   g_file_copy_attributes (source, destination,
2341                           flags, cancellable, NULL);
2342   
2343   return TRUE;
2344 }
2345
2346 /**
2347  * g_file_copy:
2348  * @source: input #GFile.
2349  * @destination: destination #GFile
2350  * @flags: set of #GFileCopyFlags
2351  * @cancellable: optional #GCancellable object, %NULL to ignore.
2352  * @progress_callback: function to callback with progress information
2353  * @progress_callback_data: user data to pass to @progress_callback
2354  * @error: #GError to set on error, or %NULL
2355  *
2356  * Copies the file @source to the location specified by @destination.
2357  * Can not handle recursive copies of directories.
2358  *
2359  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2360  * existing @destination file is overwritten.
2361  *
2362  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2363  * will be copied as symlinks, otherwise the target of the
2364  * @source symlink will be copied.
2365  *
2366  * If @cancellable is not %NULL, then the operation can be cancelled by
2367  * triggering the cancellable object from another thread. If the operation
2368  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2369  * 
2370  * If @progress_callback is not %NULL, then the operation can be monitored by
2371  * setting this to a #GFileProgressCallback function. @progress_callback_data
2372  * will be passed to this function. It is guaranteed that this callback will
2373  * be called after all data has been transferred with the total number of bytes
2374  * copied during the operation.
2375  * 
2376  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2377  * error is returned, independent on the status of the @destination.
2378  *
2379  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2380  * error G_IO_ERROR_EXISTS is returned.
2381  *
2382  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2383  * error is returned. If trying to overwrite a directory with a directory the
2384  * G_IO_ERROR_WOULD_MERGE error is returned.
2385  *
2386  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2387  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2388  * is returned.
2389  *
2390  * If you are interested in copying the #GFile object itself (not the on-disk
2391  * file), see g_file_dup().
2392  *
2393  * Returns: %TRUE on success, %FALSE otherwise.
2394  **/
2395 gboolean
2396 g_file_copy (GFile                  *source,
2397              GFile                  *destination,
2398              GFileCopyFlags          flags,
2399              GCancellable           *cancellable,
2400              GFileProgressCallback   progress_callback,
2401              gpointer                progress_callback_data,
2402              GError                **error)
2403 {
2404   GFileIface *iface;
2405   GError *my_error;
2406   gboolean res;
2407
2408   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2409   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2410
2411   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2412     return FALSE;
2413   
2414   iface = G_FILE_GET_IFACE (destination);
2415   if (iface->copy)
2416     {
2417       my_error = NULL;
2418       res = (* iface->copy) (source, destination,
2419                              flags, cancellable,
2420                              progress_callback, progress_callback_data,
2421                              &my_error);
2422       
2423       if (res)
2424         return TRUE;
2425       
2426       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2427         {
2428           g_propagate_error (error, my_error);
2429               return FALSE;
2430         }
2431     }
2432
2433   /* If the types are different, and the destination method failed
2434      also try the source method */
2435   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2436     {
2437       iface = G_FILE_GET_IFACE (source);
2438       
2439       if (iface->copy)
2440         {
2441           my_error = NULL;
2442           res = (* iface->copy) (source, destination,
2443                                  flags, cancellable,
2444                                  progress_callback, progress_callback_data,
2445                                  &my_error);
2446           
2447           if (res)
2448             return TRUE;
2449           
2450           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2451             {
2452               g_propagate_error (error, my_error);
2453               return FALSE;
2454             }
2455         }
2456     }
2457   
2458   return file_copy_fallback (source, destination, flags, cancellable,
2459                              progress_callback, progress_callback_data,
2460                              error);
2461 }
2462
2463 /**
2464  * g_file_copy_async:
2465  * @source: input #GFile.
2466  * @destination: destination #GFile
2467  * @flags: set of #GFileCopyFlags
2468  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2469  *     of the request. 
2470  * @cancellable: optional #GCancellable object, %NULL to ignore.
2471  * @progress_callback: function to callback with progress information
2472  * @progress_callback_data: user data to pass to @progress_callback
2473  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2474  * @user_data: the data to pass to callback function
2475  *
2476  * Copies the file @source to the location specified by @destination 
2477  * asynchronously. For details of the behaviour, see g_file_copy().
2478  *
2479  * If @progress_callback is not %NULL, then that function that will be called
2480  * just like in g_file_copy(), however the callback will run in the main loop,
2481  * not in the thread that is doing the I/O operation.
2482  *
2483  * When the operation is finished, @callback will be called. You can then call
2484  * g_file_copy_finish() to get the result of the operation.
2485  **/
2486 void
2487 g_file_copy_async (GFile                  *source,
2488                    GFile                  *destination,
2489                    GFileCopyFlags          flags,
2490                    int                     io_priority,
2491                    GCancellable           *cancellable,
2492                    GFileProgressCallback   progress_callback,
2493                    gpointer                progress_callback_data,
2494                    GAsyncReadyCallback     callback,
2495                    gpointer                user_data)
2496 {
2497   GFileIface *iface;
2498
2499   g_return_if_fail (G_IS_FILE (source));
2500   g_return_if_fail (G_IS_FILE (destination));
2501
2502   iface = G_FILE_GET_IFACE (source);
2503   (* iface->copy_async) (source,
2504                          destination,
2505                          flags,
2506                          io_priority,
2507                          cancellable,
2508                          progress_callback,
2509                          progress_callback_data,
2510                          callback,
2511                          user_data);
2512 }
2513
2514 /**
2515  * g_file_copy_finish:
2516  * @file: input #GFile.
2517  * @res: a #GAsyncResult. 
2518  * @error: a #GError, or %NULL
2519  * 
2520  * Finishes copying the file started with 
2521  * g_file_copy_async().
2522  * 
2523  * Returns: a %TRUE on success, %FALSE on error.
2524  **/
2525 gboolean
2526 g_file_copy_finish (GFile        *file,
2527                     GAsyncResult *res,
2528                     GError      **error)
2529 {
2530   GFileIface *iface;
2531   
2532   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2533   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2534
2535   if (G_IS_SIMPLE_ASYNC_RESULT (res))
2536     {
2537       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2538       
2539       if (g_simple_async_result_propagate_error (simple, error))
2540         return FALSE;
2541     }
2542   
2543   iface = G_FILE_GET_IFACE (file);
2544   return (* iface->copy_finish) (file, res, error);
2545 }
2546
2547 /**
2548  * g_file_move:
2549  * @source: #GFile pointing to the source location.
2550  * @destination: #GFile pointing to the destination location.
2551  * @flags: set of #GFileCopyFlags.
2552  * @cancellable: optional #GCancellable object, %NULL to ignore.
2553  * @progress_callback: #GFileProgressCallback function for updates.
2554  * @progress_callback_data: gpointer to user data for the callback function.
2555  * @error: #GError for returning error conditions, or %NULL
2556  *
2557  *
2558  * Tries to move the file or directory @source to the location specified by @destination.
2559  * If native move operations are supported then this is used, otherwise a copy + delete
2560  * fallback is used. The native implementation may support moving directories (for instance
2561  * on moves inside the same filesystem), but the fallback code does not.
2562  * 
2563  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2564  * existing @destination file is overwritten.
2565  *
2566  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2567  * will be copied as symlinks, otherwise the target of the
2568  * @source symlink will be copied.
2569  *
2570  * If @cancellable is not %NULL, then the operation can be cancelled by
2571  * triggering the cancellable object from another thread. If the operation
2572  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2573  * 
2574  * If @progress_callback is not %NULL, then the operation can be monitored by
2575  * setting this to a #GFileProgressCallback function. @progress_callback_data
2576  * will be passed to this function. It is guaranteed that this callback will
2577  * be called after all data has been transferred with the total number of bytes
2578  * copied during the operation.
2579  * 
2580  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2581  * error is returned, independent on the status of the @destination.
2582  *
2583  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2584  * error G_IO_ERROR_EXISTS is returned.
2585  *
2586  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2587  * error is returned. If trying to overwrite a directory with a directory the
2588  * G_IO_ERROR_WOULD_MERGE error is returned.
2589  *
2590  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2591  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2592  * may be returned (if the native move operation isn't available).
2593  *
2594  * Returns: %TRUE on successful move, %FALSE otherwise.
2595  **/
2596 gboolean
2597 g_file_move (GFile                  *source,
2598              GFile                  *destination,
2599              GFileCopyFlags          flags,
2600              GCancellable           *cancellable,
2601              GFileProgressCallback   progress_callback,
2602              gpointer                progress_callback_data,
2603              GError                **error)
2604 {
2605   GFileIface *iface;
2606   GError *my_error;
2607   gboolean res;
2608
2609   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2610   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2611
2612   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2613     return FALSE;
2614   
2615   iface = G_FILE_GET_IFACE (destination);
2616   if (iface->move)
2617     {
2618       my_error = NULL;
2619       res = (* iface->move) (source, destination,
2620                              flags, cancellable,
2621                              progress_callback, progress_callback_data,
2622                              &my_error);
2623       
2624       if (res)
2625         return TRUE;
2626       
2627       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2628         {
2629           g_propagate_error (error, my_error);
2630           return FALSE;
2631         }
2632     }
2633
2634   /* If the types are different, and the destination method failed
2635      also try the source method */
2636   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2637     {
2638       iface = G_FILE_GET_IFACE (source);
2639       
2640       if (iface->move)
2641         {
2642           my_error = NULL;
2643           res = (* iface->move) (source, destination,
2644                                  flags, cancellable,
2645                                  progress_callback, progress_callback_data,
2646                                  &my_error);
2647           
2648           if (res)
2649             return TRUE;
2650           
2651           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2652             {
2653               g_propagate_error (error, my_error);
2654               return FALSE;
2655             }
2656         }
2657     }
2658   
2659   if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2660     {  
2661       g_set_error_literal (error, G_IO_ERROR,
2662                            G_IO_ERROR_NOT_SUPPORTED,
2663                            _("Operation not supported"));
2664       return FALSE;
2665     }
2666   
2667   flags |= G_FILE_COPY_ALL_METADATA;
2668   if (!g_file_copy (source, destination, flags, cancellable,
2669                     progress_callback, progress_callback_data,
2670                     error))
2671     return FALSE;
2672
2673   return g_file_delete (source, cancellable, error);
2674 }
2675
2676 /**
2677  * g_file_make_directory
2678  * @file: input #GFile.
2679  * @cancellable: optional #GCancellable object, %NULL to ignore.
2680  * @error: a #GError, or %NULL 
2681  *
2682  * Creates a directory. Note that this will only create a child directory of
2683  * the immediate parent directory of the path or URI given by the #GFile. To 
2684  * recursively create directories, see g_file_make_directory_with_parents().
2685  * This function will fail if the parent directory does not exist, setting 
2686  * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
2687  * directories, this function will fail, setting @error to 
2688  * %G_IO_ERROR_NOT_SUPPORTED.
2689  * 
2690  * If @cancellable is not %NULL, then the operation can be cancelled by
2691  * triggering the cancellable object from another thread. If the operation
2692  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2693  * 
2694  * Returns: %TRUE on successful creation, %FALSE otherwise.
2695  **/
2696 gboolean
2697 g_file_make_directory (GFile         *file,
2698                        GCancellable  *cancellable,
2699                        GError       **error)
2700 {
2701   GFileIface *iface;
2702
2703   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2704
2705   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2706     return FALSE;
2707   
2708   iface = G_FILE_GET_IFACE (file);
2709
2710   if (iface->make_directory == NULL)
2711     {
2712       g_set_error_literal (error, G_IO_ERROR,
2713                            G_IO_ERROR_NOT_SUPPORTED,
2714                            _("Operation not supported"));
2715       return FALSE;
2716     }
2717   
2718   return (* iface->make_directory) (file, cancellable, error);
2719 }
2720
2721 /**
2722  * g_file_make_directory_with_parents:
2723  * @file: input #GFile.
2724  * @cancellable: optional #GCancellable object, %NULL to ignore.
2725  * @error: a #GError, or %NULL 
2726  *
2727  * Creates a directory and any parent directories that may not exist similar to
2728  * 'mkdir -p'. If the file system does not support creating directories, this
2729  * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED.
2730  * 
2731  * If @cancellable is not %NULL, then the operation can be cancelled by
2732  * triggering the cancellable object from another thread. If the operation
2733  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2734  * 
2735  * Returns: %TRUE if all directories have been successfully created, %FALSE
2736  * otherwise.
2737  *
2738  * Since: 2.18
2739  **/
2740 gboolean
2741 g_file_make_directory_with_parents (GFile         *file,
2742                                     GCancellable  *cancellable,
2743                                     GError       **error)
2744 {
2745   gboolean result;
2746   GFile *parent_file, *work_file;
2747   GList *list = NULL, *l;
2748   GError *my_error = NULL;
2749
2750   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2751     return FALSE;
2752   
2753   result = g_file_make_directory (file, cancellable, &my_error);
2754   if (result || my_error->code != G_IO_ERROR_NOT_FOUND) 
2755     {
2756       if (my_error)
2757         g_propagate_error (error, my_error);
2758       return result;
2759     }
2760   
2761   work_file = file;
2762   
2763   while (!result && my_error->code == G_IO_ERROR_NOT_FOUND) 
2764     {
2765       g_clear_error (&my_error);
2766     
2767       parent_file = g_file_get_parent (work_file);
2768       if (parent_file == NULL)
2769         break;
2770       result = g_file_make_directory (parent_file, cancellable, &my_error);
2771     
2772       if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2773         list = g_list_prepend (list, parent_file);
2774
2775       work_file = parent_file;
2776     }
2777
2778   for (l = list; result && l; l = l->next)
2779     {
2780       result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
2781     }
2782   
2783   /* Clean up */
2784   while (list != NULL) 
2785     {
2786       g_object_unref ((GFile *) list->data);
2787       list = g_list_remove (list, list->data);
2788     }
2789
2790   if (!result) 
2791     {
2792       g_propagate_error (error, my_error);
2793       return result;
2794     }
2795   
2796   return g_file_make_directory (file, cancellable, error);
2797 }
2798
2799 /**
2800  * g_file_make_symbolic_link:
2801  * @file: input #GFile.
2802  * @symlink_value: a string with the value of the new symlink.
2803  * @cancellable: optional #GCancellable object, %NULL to ignore.
2804  * @error: a #GError. 
2805  * 
2806  * Creates a symbolic link.
2807  *
2808  * If @cancellable is not %NULL, then the operation can be cancelled by
2809  * triggering the cancellable object from another thread. If the operation
2810  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2811  * 
2812  * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2813  **/
2814 gboolean
2815 g_file_make_symbolic_link (GFile         *file,
2816                            const char    *symlink_value,
2817                            GCancellable  *cancellable,
2818                            GError       **error)
2819 {
2820   GFileIface *iface;
2821
2822   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2823   g_return_val_if_fail (symlink_value != NULL, FALSE);
2824
2825   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2826     return FALSE;
2827
2828   if (*symlink_value == '\0')
2829     {
2830       g_set_error_literal (error, G_IO_ERROR,
2831                            G_IO_ERROR_INVALID_ARGUMENT,
2832                            _("Invalid symlink value given"));
2833       return FALSE;
2834     }
2835   
2836   iface = G_FILE_GET_IFACE (file);
2837
2838   if (iface->make_symbolic_link == NULL)
2839     {
2840       g_set_error_literal (error, G_IO_ERROR,
2841                            G_IO_ERROR_NOT_SUPPORTED,
2842                            _("Operation not supported"));
2843       return FALSE;
2844     }
2845   
2846   return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2847 }
2848
2849 /**
2850  * g_file_delete:
2851  * @file: input #GFile.
2852  * @cancellable: optional #GCancellable object, %NULL to ignore.
2853  * @error: a #GError, or %NULL 
2854  * 
2855  * Deletes a file. If the @file is a directory, it will only be deleted if it 
2856  * is empty.
2857  * 
2858  * If @cancellable is not %NULL, then the operation can be cancelled by
2859  * triggering the cancellable object from another thread. If the operation
2860  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2861  * 
2862  * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2863  **/
2864 gboolean
2865 g_file_delete (GFile         *file,
2866                GCancellable  *cancellable,
2867                GError       **error)
2868 {
2869   GFileIface *iface;
2870   
2871   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2872
2873   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2874     return FALSE;
2875   
2876   iface = G_FILE_GET_IFACE (file);
2877
2878   if (iface->delete_file == NULL)
2879     {
2880       g_set_error_literal (error, G_IO_ERROR,
2881                            G_IO_ERROR_NOT_SUPPORTED,
2882                            _("Operation not supported"));
2883       return FALSE;
2884     }
2885   
2886   return (* iface->delete_file) (file, cancellable, error);
2887 }
2888
2889 /**
2890  * g_file_trash:
2891  * @file: #GFile to send to trash.
2892  * @cancellable: optional #GCancellable object, %NULL to ignore.
2893  * @error: a #GError, or %NULL
2894  *
2895  * Sends @file to the "Trashcan", if possible. This is similar to
2896  * deleting it, but the user can recover it before emptying the trashcan.
2897  * Not all file systems support trashing, so this call can return the
2898  * %G_IO_ERROR_NOT_SUPPORTED error.
2899  *
2900  *
2901  * If @cancellable is not %NULL, then the operation can be cancelled by
2902  * triggering the cancellable object from another thread. If the operation
2903  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2904  * 
2905  * Returns: %TRUE on successful trash, %FALSE otherwise.
2906  **/
2907 gboolean
2908 g_file_trash (GFile         *file,
2909               GCancellable  *cancellable,
2910               GError       **error)
2911 {
2912   GFileIface *iface;
2913   
2914   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2915
2916   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2917     return FALSE;
2918   
2919   iface = G_FILE_GET_IFACE (file);
2920
2921   if (iface->trash == NULL)
2922     {
2923       g_set_error_literal (error,
2924                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2925                            _("Trash not supported"));
2926       return FALSE;
2927     }
2928   
2929   return (* iface->trash) (file, cancellable, error);
2930 }
2931
2932 /**
2933  * g_file_set_display_name:
2934  * @file: input #GFile.
2935  * @display_name: a string.
2936  * @cancellable: optional #GCancellable object, %NULL to ignore.
2937  * @error: a #GError, or %NULL
2938  * 
2939  * Renames @file to the specified display name.
2940  *
2941  * The display name is converted from UTF8 to the correct encoding for the target
2942  * filesystem if possible and the @file is renamed to this.
2943  * 
2944  * If you want to implement a rename operation in the user interface the edit name
2945  * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2946  * widget, and then the result after editing should be passed to g_file_set_display_name().
2947  *
2948  * On success the resulting converted filename is returned.
2949  * 
2950  * If @cancellable is not %NULL, then the operation can be cancelled by
2951  * triggering the cancellable object from another thread. If the operation
2952  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2953  * 
2954  * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2955  **/
2956 GFile *
2957 g_file_set_display_name (GFile         *file,
2958                          const char    *display_name,
2959                          GCancellable  *cancellable,
2960                          GError       **error)
2961 {
2962   GFileIface *iface;
2963   
2964   g_return_val_if_fail (G_IS_FILE (file), NULL);
2965   g_return_val_if_fail (display_name != NULL, NULL);
2966
2967   if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2968     {
2969       g_set_error (error,
2970                    G_IO_ERROR,
2971                    G_IO_ERROR_INVALID_ARGUMENT,
2972                    _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2973       return NULL;
2974     }
2975   
2976   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2977     return NULL;
2978   
2979   iface = G_FILE_GET_IFACE (file);
2980
2981   return (* iface->set_display_name) (file, display_name, cancellable, error);
2982 }
2983
2984 /**
2985  * g_file_set_display_name_async:
2986  * @file: input #GFile.
2987  * @display_name: a string.
2988  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2989  *     of the request. 
2990  * @cancellable: optional #GCancellable object, %NULL to ignore.
2991  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2992  * @user_data: the data to pass to callback function
2993  * 
2994  * Asynchronously sets the display name for a given #GFile.
2995  * 
2996  * For more details, see g_set_display_name() which is
2997  * the synchronous version of this call.
2998  *
2999  * When the operation is finished, @callback will be called. You can then call
3000  * g_file_set_display_name_finish() to get the result of the operation.
3001  **/
3002 void
3003 g_file_set_display_name_async (GFile               *file,
3004                                const char          *display_name,
3005                                int                  io_priority,
3006                                GCancellable        *cancellable,
3007                                GAsyncReadyCallback  callback,
3008                                gpointer             user_data)
3009 {
3010   GFileIface *iface;
3011   
3012   g_return_if_fail (G_IS_FILE (file));
3013   g_return_if_fail (display_name != NULL);
3014
3015   iface = G_FILE_GET_IFACE (file);
3016   (* iface->set_display_name_async) (file,
3017                                      display_name,
3018                                      io_priority,
3019                                      cancellable,
3020                                      callback,
3021                                      user_data);
3022 }
3023
3024 /**
3025  * g_file_set_display_name_finish:
3026  * @file: input #GFile.
3027  * @res: a #GAsyncResult. 
3028  * @error: a #GError, or %NULL
3029  * 
3030  * Finishes setting a display name started with 
3031  * g_file_set_display_name_async().
3032  * 
3033  * Returns: a #GFile or %NULL on error.
3034  **/
3035 GFile *
3036 g_file_set_display_name_finish (GFile         *file,
3037                                 GAsyncResult  *res,
3038                                 GError       **error)
3039 {
3040   GFileIface *iface;
3041   
3042   g_return_val_if_fail (G_IS_FILE (file), NULL);
3043   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3044
3045   if (G_IS_SIMPLE_ASYNC_RESULT (res))
3046     {
3047       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3048       if (g_simple_async_result_propagate_error (simple, error))
3049         return NULL;
3050     }
3051   
3052   iface = G_FILE_GET_IFACE (file);
3053   return (* iface->set_display_name_finish) (file, res, error);
3054 }
3055
3056 /**
3057  * g_file_query_settable_attributes:
3058  * @file: input #GFile.
3059  * @cancellable: optional #GCancellable object, %NULL to ignore.
3060  * @error: a #GError, or %NULL
3061  * 
3062  * Obtain the list of settable attributes for the file.
3063  *
3064  * Returns the type and full attribute name of all the attributes 
3065  * that can be set on this file. This doesn't mean setting it will always 
3066  * succeed though, you might get an access failure, or some specific 
3067  * file may not support a specific attribute.
3068  *
3069  * If @cancellable is not %NULL, then the operation can be cancelled by
3070  * triggering the cancellable object from another thread. If the operation
3071  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3072  * 
3073  * Returns: a #GFileAttributeInfoList describing the settable attributes.
3074  * When you are done with it, release it with g_file_attribute_info_list_unref()
3075  **/
3076 GFileAttributeInfoList *
3077 g_file_query_settable_attributes (GFile         *file,
3078                                   GCancellable  *cancellable,
3079                                   GError       **error)
3080 {
3081   GFileIface *iface;
3082   GError *my_error;
3083   GFileAttributeInfoList *list;
3084
3085   g_return_val_if_fail (G_IS_FILE (file), NULL);
3086
3087   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3088     return NULL;
3089   
3090   iface = G_FILE_GET_IFACE (file);
3091
3092   if (iface->query_settable_attributes == NULL)
3093     return g_file_attribute_info_list_new ();
3094
3095   my_error = NULL;
3096   list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3097   
3098   if (list == NULL)
3099     {
3100       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3101         {
3102           list = g_file_attribute_info_list_new ();
3103           g_error_free (my_error);
3104         }
3105       else
3106         g_propagate_error (error, my_error);
3107     }
3108   
3109   return list;
3110 }
3111
3112 /**
3113  * g_file_query_writable_namespaces:
3114  * @file: input #GFile.
3115  * @cancellable: optional #GCancellable object, %NULL to ignore.
3116  * @error: a #GError, or %NULL
3117  * 
3118  * Obtain the list of attribute namespaces where new attributes 
3119  * can be created by a user. An example of this is extended
3120  * attributes (in the "xattr" namespace).
3121  *
3122  * If @cancellable is not %NULL, then the operation can be cancelled by
3123  * triggering the cancellable object from another thread. If the operation
3124  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3125  * 
3126  * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3127  * When you are done with it, release it with g_file_attribute_info_list_unref()
3128  **/
3129 GFileAttributeInfoList *
3130 g_file_query_writable_namespaces (GFile         *file,
3131                                   GCancellable  *cancellable,
3132                                   GError       **error)
3133 {
3134   GFileIface *iface;
3135   GError *my_error;
3136   GFileAttributeInfoList *list;
3137   
3138   g_return_val_if_fail (G_IS_FILE (file), NULL);
3139
3140   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3141     return NULL;
3142   
3143   iface = G_FILE_GET_IFACE (file);
3144
3145   if (iface->query_writable_namespaces == NULL)
3146     return g_file_attribute_info_list_new ();
3147
3148   my_error = NULL;
3149   list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3150   
3151   if (list == NULL)
3152     {
3153       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3154         {
3155           list = g_file_attribute_info_list_new ();
3156           g_error_free (my_error);
3157         }
3158       else
3159         g_propagate_error (error, my_error);
3160     }
3161   
3162   return list;
3163 }
3164
3165 /**
3166  * g_file_set_attribute:
3167  * @file: input #GFile.
3168  * @attribute: a string containing the attribute's name.
3169  * @type: The type of the attribute
3170  * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3171  * @flags: a set of #GFileQueryInfoFlags.
3172  * @cancellable: optional #GCancellable object, %NULL to ignore.
3173  * @error: a #GError, or %NULL
3174  * 
3175  * Sets an attribute in the file with attribute name @attribute to @value.
3176  * 
3177  * If @cancellable is not %NULL, then the operation can be cancelled by
3178  * triggering the cancellable object from another thread. If the operation
3179  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3180  * 
3181  * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3182  **/
3183 gboolean
3184 g_file_set_attribute (GFile                      *file,
3185                       const char                 *attribute,
3186                       GFileAttributeType          type,
3187                       gpointer                    value_p,
3188                       GFileQueryInfoFlags         flags,
3189                       GCancellable               *cancellable,
3190                       GError                    **error)
3191 {
3192   GFileIface *iface;
3193   
3194   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3195   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3196
3197   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3198     return FALSE;
3199   
3200   iface = G_FILE_GET_IFACE (file);
3201
3202   if (iface->set_attribute == NULL)
3203     {
3204       g_set_error_literal (error, G_IO_ERROR,
3205                            G_IO_ERROR_NOT_SUPPORTED,
3206                            _("Operation not supported"));
3207       return FALSE;
3208     }
3209
3210   return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3211 }
3212
3213 /**
3214  * g_file_set_attributes_from_info:
3215  * @file: input #GFile.
3216  * @info: a #GFileInfo.
3217  * @flags: #GFileQueryInfoFlags
3218  * @cancellable: optional #GCancellable object, %NULL to ignore.
3219  * @error: a #GError, or %NULL 
3220  * 
3221  * Tries to set all attributes in the #GFileInfo on the target values, 
3222  * not stopping on the first error.
3223  * 
3224  * If there is any error during this operation then @error will be set to
3225  * the first error. Error on particular fields are flagged by setting 
3226  * the "status" field in the attribute value to 
3227  * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3228  * further errors.
3229  *
3230  * If @cancellable is not %NULL, then the operation can be cancelled by
3231  * triggering the cancellable object from another thread. If the operation
3232  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3233  * 
3234  * Returns: %TRUE if there was any error, %FALSE otherwise.
3235  **/
3236 gboolean
3237 g_file_set_attributes_from_info (GFile                *file,
3238                                  GFileInfo            *info,
3239                                  GFileQueryInfoFlags   flags,
3240                                  GCancellable         *cancellable,
3241                                  GError              **error)
3242 {
3243   GFileIface *iface;
3244
3245   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3246   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3247
3248   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3249     return FALSE;
3250   
3251   g_file_info_clear_status (info);
3252   
3253   iface = G_FILE_GET_IFACE (file);
3254
3255   return (* iface->set_attributes_from_info) (file, 
3256                                               info, 
3257                                               flags, 
3258                                               cancellable, 
3259                                               error);
3260 }
3261
3262
3263 static gboolean
3264 g_file_real_set_attributes_from_info (GFile                *file,
3265                                       GFileInfo            *info,
3266                                       GFileQueryInfoFlags   flags,
3267                                       GCancellable         *cancellable,
3268                                       GError              **error)
3269 {
3270   char **attributes;
3271   int i;
3272   gboolean res;
3273   GFileAttributeValue *value;
3274   
3275   res = TRUE;
3276   
3277   attributes = g_file_info_list_attributes (info, NULL);
3278
3279   for (i = 0; attributes[i] != NULL; i++)
3280     {
3281       value = _g_file_info_get_attribute_value (info, attributes[i]);
3282
3283       if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3284         continue;
3285
3286       if (!g_file_set_attribute (file, attributes[i],
3287                                  value->type, _g_file_attribute_value_peek_as_pointer (value),
3288                                  flags, cancellable, error))
3289         {
3290           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3291           res = FALSE;
3292           /* Don't set error multiple times */
3293           error = NULL;
3294         }
3295       else
3296         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3297     }
3298   
3299   g_strfreev (attributes);
3300   
3301   return res;
3302 }
3303
3304 /**
3305  * g_file_set_attributes_async:
3306  * @file: input #GFile.
3307  * @info: a #GFileInfo.
3308  * @flags: a #GFileQueryInfoFlags.
3309  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
3310  *     of the request. 
3311  * @cancellable: optional #GCancellable object, %NULL to ignore.
3312  * @callback: a #GAsyncReadyCallback. 
3313  * @user_data: a #gpointer.
3314  *
3315  * Asynchronously sets the attributes of @file with @info.
3316  * 
3317  * For more details, see g_file_set_attributes_from_info() which is
3318  * the synchronous version of this call.
3319  *
3320  * When the operation is finished, @callback will be called. You can then call
3321  * g_file_set_attributes_finish() to get the result of the operation.
3322  **/
3323 void
3324 g_file_set_attributes_async (GFile               *file,
3325                              GFileInfo           *info,
3326                              GFileQueryInfoFlags  flags,
3327                              int                  io_priority,
3328                              GCancellable        *cancellable,
3329                              GAsyncReadyCallback  callback,
3330                              gpointer             user_data)
3331 {
3332   GFileIface *iface;
3333   
3334   g_return_if_fail (G_IS_FILE (file));
3335   g_return_if_fail (G_IS_FILE_INFO (info));
3336
3337   iface = G_FILE_GET_IFACE (file);
3338   (* iface->set_attributes_async) (file, 
3339                                    info, 
3340                                    flags, 
3341                                    io_priority, 
3342                                    cancellable, 
3343                                    callback, 
3344                                    user_data);
3345 }
3346
3347 /**
3348  * g_file_set_attributes_finish:
3349  * @file: input #GFile.
3350  * @result: a #GAsyncResult.
3351  * @info: a #GFileInfo.
3352  * @error: a #GError, or %NULL
3353  * 
3354  * Finishes setting an attribute started in g_file_set_attributes_async().
3355  * 
3356  * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3357  **/
3358 gboolean
3359 g_file_set_attributes_finish (GFile         *file,
3360                               GAsyncResult  *result,
3361                               GFileInfo    **info,
3362                               GError       **error)
3363 {
3364   GFileIface *iface;
3365   
3366   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3367   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3368
3369   /* No standard handling of errors here, as we must set info even
3370    * on errors 
3371    */
3372   iface = G_FILE_GET_IFACE (file);
3373   return (* iface->set_attributes_finish) (file, result, info, error);
3374 }
3375
3376 /**
3377  * g_file_set_attribute_string:
3378  * @file: input #GFile.
3379  * @attribute: a string containing the attribute's name.
3380  * @value: a string containing the attribute's value.
3381  * @flags: #GFileQueryInfoFlags.
3382  * @cancellable: optional #GCancellable object, %NULL to ignore.
3383  * @error: a #GError, or %NULL
3384  * 
3385  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value. 
3386  * If @attribute is of a different type, this operation will fail.
3387  * 
3388  * If @cancellable is not %NULL, then the operation can be cancelled by
3389  * triggering the cancellable object from another thread. If the operation
3390  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3391  * 
3392  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3393  **/
3394 gboolean
3395 g_file_set_attribute_string (GFile                *file,
3396                              const char           *attribute,
3397                              const char           *value,
3398                              GFileQueryInfoFlags   flags,
3399                              GCancellable         *cancellable,
3400                              GError              **error)
3401 {
3402   return g_file_set_attribute (file, attribute,
3403                                G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3404                                flags, cancellable, error);
3405 }
3406
3407 /**
3408  * g_file_set_attribute_byte_string:
3409  * @file: input #GFile.
3410  * @attribute: a string containing the attribute's name.
3411  * @value: a string containing the attribute's new value.
3412  * @flags: a #GFileQueryInfoFlags.
3413  * @cancellable: optional #GCancellable object, %NULL to ignore.
3414  * @error: a #GError, or %NULL
3415  * 
3416  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value. 
3417  * If @attribute is of a different type, this operation will fail, 
3418  * returning %FALSE. 
3419  * 
3420  * If @cancellable is not %NULL, then the operation can be cancelled by
3421  * triggering the cancellable object from another thread. If the operation
3422  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3423  * 
3424  * Returns: %TRUE if the @attribute was successfully set to @value 
3425  * in the @file, %FALSE otherwise.
3426  **/
3427 gboolean
3428 g_file_set_attribute_byte_string  (GFile                *file,
3429                                    const char           *attribute,
3430                                    const char           *value,
3431                                    GFileQueryInfoFlags   flags,
3432                                    GCancellable         *cancellable,
3433                                    GError              **error)
3434 {
3435   return g_file_set_attribute (file, attribute,
3436                                G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3437                                flags, cancellable, error);
3438 }
3439
3440 /**
3441  * g_file_set_attribute_uint32:
3442  * @file: input #GFile.
3443  * @attribute: a string containing the attribute's name.
3444  * @value: a #guint32 containing the attribute's new value.
3445  * @flags: a #GFileQueryInfoFlags.
3446  * @cancellable: optional #GCancellable object, %NULL to ignore.
3447  * @error: a #GError, or %NULL
3448  * 
3449  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value. 
3450  * If @attribute is of a different type, this operation will fail.
3451  * 
3452  * If @cancellable is not %NULL, then the operation can be cancelled by
3453  * triggering the cancellable object from another thread. If the operation
3454  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3455  * 
3456  * Returns: %TRUE if the @attribute was successfully set to @value 
3457  * in the @file, %FALSE otherwise.
3458  **/
3459 gboolean
3460 g_file_set_attribute_uint32 (GFile                *file,
3461                              const char           *attribute,
3462                              guint32               value,
3463                              GFileQueryInfoFlags   flags,
3464                              GCancellable         *cancellable,
3465                              GError              **error)
3466 {
3467   return g_file_set_attribute (file, attribute,
3468                                G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3469                                flags, cancellable, error);
3470 }
3471
3472 /**
3473  * g_file_set_attribute_int32:
3474  * @file: input #GFile.
3475  * @attribute: a string containing the attribute's name.
3476  * @value: a #gint32 containing the attribute's new value.
3477  * @flags: a #GFileQueryInfoFlags.
3478  * @cancellable: optional #GCancellable object, %NULL to ignore.
3479  * @error: a #GError, or %NULL
3480  * 
3481  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value. 
3482  * If @attribute is of a different type, this operation will fail.
3483  * 
3484  * If @cancellable is not %NULL, then the operation can be cancelled by
3485  * triggering the cancellable object from another thread. If the operation
3486  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3487  * 
3488  * Returns: %TRUE if the @attribute was successfully set to @value 
3489  * in the @file, %FALSE otherwise. 
3490  **/
3491 gboolean
3492 g_file_set_attribute_int32 (GFile                *file,
3493                             const char           *attribute,
3494                             gint32                value,
3495                             GFileQueryInfoFlags   flags,
3496                             GCancellable         *cancellable,
3497                             GError              **error)
3498 {
3499   return g_file_set_attribute (file, attribute,
3500                                G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3501                                flags, cancellable, error);
3502 }
3503
3504 /**
3505  * g_file_set_attribute_uint64:
3506  * @file: input #GFile. 
3507  * @attribute: a string containing the attribute's name.
3508  * @value: a #guint64 containing the attribute's new value.
3509  * @flags: a #GFileQueryInfoFlags.
3510  * @cancellable: optional #GCancellable object, %NULL to ignore.
3511  * @error: a #GError, or %NULL
3512  * 
3513  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value. 
3514  * If @attribute is of a different type, this operation will fail.
3515  * 
3516  * If @cancellable is not %NULL, then the operation can be cancelled by
3517  * triggering the cancellable object from another thread. If the operation
3518  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3519  * 
3520  * Returns: %TRUE if the @attribute was successfully set to @value 
3521  * in the @file, %FALSE otherwise.
3522  **/
3523 gboolean
3524 g_file_set_attribute_uint64 (GFile                *file,
3525                              const char           *attribute,
3526                              guint64               value,
3527                              GFileQueryInfoFlags   flags,
3528                              GCancellable         *cancellable,
3529                              GError              **error)
3530  {
3531   return g_file_set_attribute (file, attribute,
3532                                G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3533                                flags, cancellable, error);
3534 }
3535
3536 /**
3537  * g_file_set_attribute_int64:
3538  * @file: input #GFile.
3539  * @attribute: a string containing the attribute's name.
3540  * @value: a #guint64 containing the attribute's new value.
3541  * @flags: a #GFileQueryInfoFlags.
3542  * @cancellable: optional #GCancellable object, %NULL to ignore.
3543  * @error: a #GError, or %NULL
3544  * 
3545  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value. 
3546  * If @attribute is of a different type, this operation will fail.
3547  * 
3548  * If @cancellable is not %NULL, then the operation can be cancelled by
3549  * triggering the cancellable object from another thread. If the operation
3550  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3551  * 
3552  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3553  **/
3554 gboolean
3555 g_file_set_attribute_int64 (GFile                *file,
3556                             const char           *attribute,
3557                             gint64                value,
3558                             GFileQueryInfoFlags   flags,
3559                             GCancellable         *cancellable,
3560                             GError              **error)
3561 {
3562   return g_file_set_attribute (file, attribute,
3563                                G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3564                                flags, cancellable, error);
3565 }
3566
3567 /**
3568  * g_file_mount_mountable:
3569  * @file: input #GFile.
3570  * @flags: flags affecting the operation
3571  * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3572  * @cancellable: optional #GCancellable object, %NULL to ignore.
3573  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3574  * @user_data: the data to pass to callback function
3575  * 
3576  * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3577  * Using @mount_operation, you can request callbacks when, for instance, 
3578  * passwords are needed during authentication.
3579  *
3580  * If @cancellable is not %NULL, then the operation can be cancelled by
3581  * triggering the cancellable object from another thread. If the operation
3582  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3583  *
3584  * When the operation is finished, @callback will be called. You can then call
3585  * g_file_mount_mountable_finish() to get the result of the operation.
3586  **/
3587 void
3588 g_file_mount_mountable (GFile               *file,
3589                         GMountMountFlags     flags,
3590                         GMountOperation     *mount_operation,
3591                         GCancellable        *cancellable,
3592                         GAsyncReadyCallback  callback,
3593                         gpointer             user_data)
3594 {
3595   GFileIface *iface;
3596
3597   g_return_if_fail (G_IS_FILE (file));
3598
3599   iface = G_FILE_GET_IFACE (file);
3600
3601   if (iface->mount_mountable == NULL) 
3602     {
3603       g_simple_async_report_error_in_idle (G_OBJECT (file),
3604                                            callback,
3605                                            user_data,
3606                                            G_IO_ERROR,
3607                                            G_IO_ERROR_NOT_SUPPORTED,
3608                                            _("Operation not supported"));
3609       return;
3610     }
3611   
3612   (* iface->mount_mountable) (file,
3613                               flags,
3614                               mount_operation,
3615                               cancellable,
3616                               callback,
3617                               user_data);
3618 }
3619
3620 /**
3621  * g_file_mount_mountable_finish:
3622  * @file: input #GFile.
3623  * @result: a #GAsyncResult.
3624  * @error: a #GError, or %NULL
3625  *
3626  * Finishes a mount operation. See g_file_mount_mountable() for details.
3627  * 
3628  * Finish an asynchronous mount operation that was started 
3629  * with g_file_mount_mountable().
3630  *
3631  * Returns: a #GFile or %NULL on error.
3632  **/
3633 GFile *
3634 g_file_mount_mountable_finish (GFile         *file,
3635                                GAsyncResult  *result,
3636                                GError       **error)
3637 {
3638   GFileIface *iface;
3639
3640   g_return_val_if_fail (G_IS_FILE (file), NULL);
3641   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3642
3643   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3644     {
3645       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3646       if (g_simple_async_result_propagate_error (simple, error))
3647         return NULL;
3648     }
3649   
3650   iface = G_FILE_GET_IFACE (file);
3651   return (* iface->mount_mountable_finish) (file, result, error);
3652 }
3653
3654 /**
3655  * g_file_unmount_mountable:
3656  * @file: input #GFile.
3657  * @flags: flags affecting the operation
3658  * @cancellable: optional #GCancellable object, %NULL to ignore.
3659  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3660  * @user_data: the data to pass to callback function
3661  *
3662  * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3663  *
3664  * If @cancellable is not %NULL, then the operation can be cancelled by
3665  * triggering the cancellable object from another thread. If the operation
3666  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3667  *
3668  * When the operation is finished, @callback will be called. You can then call
3669  * g_file_unmount_mountable_finish() to get the result of the operation.
3670  **/
3671 void
3672 g_file_unmount_mountable (GFile               *file,
3673                           GMountUnmountFlags   flags,
3674                           GCancellable        *cancellable,
3675                           GAsyncReadyCallback  callback,
3676                           gpointer             user_data)
3677 {
3678   GFileIface *iface;
3679   
3680   g_return_if_fail (G_IS_FILE (file));
3681
3682   iface = G_FILE_GET_IFACE (file);
3683   
3684   if (iface->unmount_mountable == NULL)
3685     {
3686       g_simple_async_report_error_in_idle (G_OBJECT (file),
3687                                            callback,
3688                                            user_data,
3689                                            G_IO_ERROR,
3690                                            G_IO_ERROR_NOT_SUPPORTED,
3691                                            _("Operation not supported"));
3692       return;
3693     }
3694   
3695   (* iface->unmount_mountable) (file,
3696                                 flags,
3697                                 cancellable,
3698                                 callback,
3699                                 user_data);
3700 }
3701
3702 /**
3703  * g_file_unmount_mountable_finish:
3704  * @file: input #GFile.
3705  * @result: a #GAsyncResult.
3706  * @error: a #GError, or %NULL
3707  *
3708  * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3709  * 
3710  * Finish an asynchronous unmount operation that was started 
3711  * with g_file_unmount_mountable().
3712  *
3713  * Returns: %TRUE if the operation finished successfully. %FALSE
3714  * otherwise.
3715  **/
3716 gboolean
3717 g_file_unmount_mountable_finish (GFile         *file,
3718                                  GAsyncResult  *result,
3719                                  GError       **error)
3720 {
3721   GFileIface *iface;
3722   
3723   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3724   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3725
3726   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3727     {
3728       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3729       if (g_simple_async_result_propagate_error (simple, error))
3730         return FALSE;
3731     }
3732   
3733   iface = G_FILE_GET_IFACE (file);
3734   return (* iface->unmount_mountable_finish) (file, result, error);
3735 }
3736
3737 /**
3738  * g_file_eject_mountable:
3739  * @file: input #GFile.
3740  * @flags: flags affecting the operation
3741  * @cancellable: optional #GCancellable object, %NULL to ignore.
3742  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3743  * @user_data: the data to pass to callback function
3744  * 
3745  * Starts an asynchronous eject on a mountable.  
3746  * When this operation has completed, @callback will be called with
3747  * @user_user data, and the operation can be finalized with 
3748  * g_file_eject_mountable_finish().
3749  * 
3750  * If @cancellable is not %NULL, then the operation can be cancelled by
3751  * triggering the cancellable object from another thread. If the operation
3752  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3753  **/
3754 void
3755 g_file_eject_mountable (GFile               *file,
3756                         GMountUnmountFlags   flags,
3757                         GCancellable        *cancellable,
3758                         GAsyncReadyCallback  callback,
3759                         gpointer             user_data)
3760 {
3761   GFileIface *iface;
3762
3763   g_return_if_fail (G_IS_FILE (file));
3764
3765   iface = G_FILE_GET_IFACE (file);
3766   
3767   if (iface->eject_mountable == NULL) 
3768     {
3769       g_simple_async_report_error_in_idle (G_OBJECT (file),
3770                                            callback,
3771                                            user_data,
3772                                            G_IO_ERROR,
3773                                            G_IO_ERROR_NOT_SUPPORTED,
3774                                            _("Operation not supported"));
3775       return;
3776     }
3777   
3778   (* iface->eject_mountable) (file,
3779                               flags,
3780                               cancellable,
3781                               callback,
3782                               user_data);
3783 }
3784
3785 /**
3786  * g_file_eject_mountable_finish:
3787  * @file: input #GFile.
3788  * @result: a #GAsyncResult.
3789  * @error: a #GError, or %NULL
3790  * 
3791  * Finishes an asynchronous eject operation started by 
3792  * g_file_eject_mountable().
3793  * 
3794  * Returns: %TRUE if the @file was ejected successfully. %FALSE 
3795  * otherwise.
3796  **/
3797 gboolean
3798 g_file_eject_mountable_finish (GFile         *file,
3799                                GAsyncResult  *result,
3800                                GError       **error)
3801 {
3802   GFileIface *iface;
3803   
3804   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3805   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3806
3807   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3808     {
3809       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3810       if (g_simple_async_result_propagate_error (simple, error))
3811         return FALSE;
3812     }
3813   
3814   iface = G_FILE_GET_IFACE (file);
3815   return (* iface->eject_mountable_finish) (file, result, error);
3816 }
3817
3818 /**
3819  * g_file_monitor_directory:
3820  * @file: input #GFile.
3821  * @flags: a set of #GFileMonitorFlags.
3822  * @cancellable: optional #GCancellable object, %NULL to ignore.
3823  * @error: a #GError, or %NULL.
3824  * 
3825  * Obtains a directory monitor for the given file.
3826  * This may fail if directory monitoring is not supported.
3827  *
3828  * If @cancellable is not %NULL, then the operation can be cancelled by
3829  * triggering the cancellable object from another thread. If the operation
3830  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3831  * 
3832  * Returns: a #GFileMonitor for the given @file, 
3833  * or %NULL on error.
3834  **/
3835 GFileMonitor*
3836 g_file_monitor_directory (GFile             *file,
3837                           GFileMonitorFlags  flags,
3838                           GCancellable      *cancellable,
3839                           GError           **error)
3840 {
3841   GFileIface *iface;
3842
3843   g_return_val_if_fail (G_IS_FILE (file), NULL);
3844
3845   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3846     return NULL;
3847
3848   iface = G_FILE_GET_IFACE (file);
3849
3850   if (iface->monitor_dir == NULL)
3851     {
3852       g_set_error_literal (error, G_IO_ERROR,
3853                            G_IO_ERROR_NOT_SUPPORTED,
3854                            _("Operation not supported"));
3855       return NULL;
3856     }
3857
3858   return (* iface->monitor_dir) (file, flags, cancellable, error);
3859 }
3860
3861 /**
3862  * g_file_monitor_file:
3863  * @file: input #GFile.
3864  * @flags: a set of #GFileMonitorFlags.
3865  * @cancellable: optional #GCancellable object, %NULL to ignore.
3866  * @error: a #GError, or %NULL.
3867  * 
3868  * Obtains a file monitor for the given file. If no file notification
3869  * mechanism exists, then regular polling of the file is used.
3870  *
3871  * If @cancellable is not %NULL, then the operation can be cancelled by
3872  * triggering the cancellable object from another thread. If the operation
3873  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3874  * 
3875  * Returns: a #GFileMonitor for the given @file.
3876  **/
3877 GFileMonitor*
3878 g_file_monitor_file (GFile             *file,
3879                      GFileMonitorFlags  flags,
3880                      GCancellable      *cancellable,
3881                      GError           **error)
3882 {
3883   GFileIface *iface;
3884   GFileMonitor *monitor;
3885   
3886   g_return_val_if_fail (G_IS_FILE (file), NULL);
3887
3888   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3889     return NULL;
3890
3891   iface = G_FILE_GET_IFACE (file);
3892
3893   monitor = NULL;
3894   
3895   if (iface->monitor_file)
3896     monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3897
3898 /* Fallback to polling */
3899   if (monitor == NULL)
3900     monitor = _g_poll_file_monitor_new (file);
3901
3902   return monitor;
3903 }
3904
3905 /**
3906  * g_file_monitor:
3907  * @file: input #GFile
3908  * @flags: a set of #GFileMonitorFlags
3909  * @cancellable: optional #GCancellable object, %NULL to ignore
3910  * @error: a #GError, or %NULL
3911  * 
3912  * Obtains a file or directory monitor for the given file, depending
3913  * on the type of the file.
3914  *
3915  * If @cancellable is not %NULL, then the operation can be cancelled by
3916  * triggering the cancellable object from another thread. If the operation
3917  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3918  * 
3919  * Returns: a #GFileMonitor for the given @file.
3920  *
3921  * Since: 2.18
3922  */
3923 GFileMonitor*
3924 g_file_monitor (GFile             *file,
3925                 GFileMonitorFlags  flags,
3926                 GCancellable      *cancellable,
3927                 GError           **error)
3928 {
3929   if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
3930     return g_file_monitor_directory (file, flags, cancellable, error);
3931   else
3932     return g_file_monitor_file (file, flags, cancellable, error);
3933 }
3934
3935 /********************************************
3936  *   Default implementation of async ops    *
3937  ********************************************/
3938
3939 typedef struct {
3940   char *attributes;
3941   GFileQueryInfoFlags flags;
3942   GFileInfo *info;
3943 } QueryInfoAsyncData;
3944
3945 static void
3946 query_info_data_free (QueryInfoAsyncData *data)
3947 {
3948   if (data->info)
3949     g_object_unref (data->info);
3950   g_free (data->attributes);
3951   g_free (data);
3952 }
3953
3954 static void
3955 query_info_async_thread (GSimpleAsyncResult *res,
3956                          GObject            *object,
3957                          GCancellable       *cancellable)
3958 {
3959   GError *error = NULL;
3960   QueryInfoAsyncData *data;
3961   GFileInfo *info;
3962   
3963   data = g_simple_async_result_get_op_res_gpointer (res);
3964   
3965   info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3966
3967   if (info == NULL)
3968     {
3969       g_simple_async_result_set_from_error (res, error);
3970       g_error_free (error);
3971     }
3972   else
3973     data->info = info;
3974 }
3975
3976 static void
3977 g_file_real_query_info_async (GFile               *file,
3978                               const char          *attributes,
3979                               GFileQueryInfoFlags  flags,
3980                               int                  io_priority,
3981                               GCancellable        *cancellable,
3982                               GAsyncReadyCallback  callback,
3983                               gpointer             user_data)
3984 {
3985   GSimpleAsyncResult *res;
3986   QueryInfoAsyncData *data;
3987
3988   data = g_new0 (QueryInfoAsyncData, 1);
3989   data->attributes = g_strdup (attributes);
3990   data->flags = flags;
3991   
3992   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3993   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3994   
3995   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3996   g_object_unref (res);
3997 }
3998
3999 static GFileInfo *
4000 g_file_real_query_info_finish (GFile         *file,
4001                                GAsyncResult  *res,
4002                                GError       **error)
4003 {
4004   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4005   QueryInfoAsyncData *data;
4006
4007   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4008
4009   data = g_simple_async_result_get_op_res_gpointer (simple);
4010   if (data->info)
4011     return g_object_ref (data->info);
4012   
4013   return NULL;
4014 }
4015
4016 typedef struct {
4017   char *attributes;
4018   GFileInfo *info;
4019 } QueryFilesystemInfoAsyncData;
4020
4021 static void
4022 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4023 {
4024   if (data->info)
4025     g_object_unref (data->info);
4026   g_free (data->attributes);
4027   g_free (data);
4028 }
4029
4030 static void
4031 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4032                                     GObject            *object,
4033                                     GCancellable       *cancellable)
4034 {
4035   GError *error = NULL;
4036   QueryFilesystemInfoAsyncData *data;
4037   GFileInfo *info;
4038   
4039   data = g_simple_async_result_get_op_res_gpointer (res);
4040   
4041   info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4042
4043   if (info == NULL)
4044     {
4045       g_simple_async_result_set_from_error (res, error);
4046       g_error_free (error);
4047     }
4048   else
4049     data->info = info;
4050 }
4051
4052 static void
4053 g_file_real_query_filesystem_info_async (GFile               *file,
4054                                          const char          *attributes,
4055                                          int                  io_priority,
4056                                          GCancellable        *cancellable,
4057                                          GAsyncReadyCallback  callback,
4058                                          gpointer             user_data)
4059 {
4060   GSimpleAsyncResult *res;
4061   QueryFilesystemInfoAsyncData *data;
4062
4063   data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4064   data->attributes = g_strdup (attributes);
4065   
4066   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4067   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4068   
4069   g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4070   g_object_unref (res);
4071 }
4072
4073 static GFileInfo *
4074 g_file_real_query_filesystem_info_finish (GFile         *file,
4075                                           GAsyncResult  *res,
4076                                           GError       **error)
4077 {
4078   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4079   QueryFilesystemInfoAsyncData *data;
4080
4081   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4082
4083   data = g_simple_async_result_get_op_res_gpointer (simple);
4084   if (data->info)
4085     return g_object_ref (data->info);
4086   
4087   return NULL;
4088 }
4089
4090 typedef struct {
4091   char *attributes;
4092   GFileQueryInfoFlags flags;
4093   GFileEnumerator *enumerator;
4094 } EnumerateChildrenAsyncData;
4095
4096 static void
4097 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4098 {
4099   if (data->enumerator)
4100     g_object_unref (data->enumerator);
4101   g_free (data->attributes);
4102   g_free (data);
4103 }
4104
4105 static void
4106 enumerate_children_async_thread (GSimpleAsyncResult *res,
4107                                  GObject            *object,
4108                                  GCancellable       *cancellable)
4109 {
4110   GError *error = NULL;
4111   EnumerateChildrenAsyncData *data;
4112   GFileEnumerator *enumerator;
4113   
4114   data = g_simple_async_result_get_op_res_gpointer (res);
4115   
4116   enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4117
4118   if (enumerator == NULL)
4119     {
4120       g_simple_async_result_set_from_error (res, error);
4121       g_error_free (error);
4122     }
4123   else
4124     data->enumerator = enumerator;
4125 }
4126
4127 static void
4128 g_file_real_enumerate_children_async (GFile               *file,
4129                                       const char          *attributes,
4130                                       GFileQueryInfoFlags  flags,
4131                                       int                  io_priority,
4132                                       GCancellable        *cancellable,
4133                                       GAsyncReadyCallback  callback,
4134                                       gpointer             user_data)
4135 {
4136   GSimpleAsyncResult *res;
4137   EnumerateChildrenAsyncData *data;
4138
4139   data = g_new0 (EnumerateChildrenAsyncData, 1);
4140   data->attributes = g_strdup (attributes);
4141   data->flags = flags;
4142   
4143   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4144   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4145   
4146   g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4147   g_object_unref (res);
4148 }
4149
4150 static GFileEnumerator *
4151 g_file_real_enumerate_children_finish (GFile         *file,
4152                                        GAsyncResult  *res,
4153                                        GError       **error)
4154 {
4155   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4156   EnumerateChildrenAsyncData *data;
4157
4158   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4159
4160   data = g_simple_async_result_get_op_res_gpointer (simple);
4161   if (data->enumerator)
4162     return g_object_ref (data->enumerator);
4163   
4164   return NULL;
4165 }
4166
4167 static void
4168 open_read_async_thread (GSimpleAsyncResult *res,
4169                         GObject            *object,
4170                         GCancellable       *cancellable)
4171 {
4172   GFileIface *iface;
4173   GFileInputStream *stream;
4174   GError *error = NULL;
4175
4176   iface = G_FILE_GET_IFACE (object);
4177
4178   stream = iface->read_fn (G_FILE (object), cancellable, &error);
4179
4180   if (stream == NULL)
4181     {
4182       g_simple_async_result_set_from_error (res, error);
4183       g_error_free (error);
4184     }
4185   else
4186     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4187 }
4188
4189 static void
4190 g_file_real_read_async (GFile               *file,
4191                         int                  io_priority,
4192                         GCancellable        *cancellable,
4193                         GAsyncReadyCallback  callback,
4194                         gpointer             user_data)
4195 {
4196   GSimpleAsyncResult *res;
4197   
4198   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4199   
4200   g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4201   g_object_unref (res);
4202 }
4203
4204 static GFileInputStream *
4205 g_file_real_read_finish (GFile         *file,
4206                          GAsyncResult  *res,
4207                          GError       **error)
4208 {
4209   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4210   gpointer op;
4211
4212   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4213
4214   op = g_simple_async_result_get_op_res_gpointer (simple);
4215   if (op)
4216     return g_object_ref (op);
4217   
4218   return NULL;
4219 }
4220
4221 static void
4222 append_to_async_thread (GSimpleAsyncResult *res,
4223                         GObject            *object,
4224                         GCancellable       *cancellable)
4225 {
4226   GFileIface *iface;
4227   GFileCreateFlags *data;
4228   GFileOutputStream *stream;
4229   GError *error = NULL;
4230
4231   iface = G_FILE_GET_IFACE (object);
4232
4233   data = g_simple_async_result_get_op_res_gpointer (res);
4234
4235   stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4236
4237   if (stream == NULL)
4238     {
4239       g_simple_async_result_set_from_error (res, error);
4240       g_error_free (error);
4241     }
4242   else
4243     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4244 }
4245
4246 static void
4247 g_file_real_append_to_async (GFile               *file,
4248                              GFileCreateFlags     flags,
4249                              int                  io_priority,
4250                              GCancellable        *cancellable,
4251                              GAsyncReadyCallback  callback,
4252                              gpointer             user_data)
4253 {
4254   GFileCreateFlags *data;
4255   GSimpleAsyncResult *res;
4256
4257   data = g_new0 (GFileCreateFlags, 1);
4258   *data = flags;
4259
4260   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4261   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4262
4263   g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4264   g_object_unref (res);
4265 }
4266
4267 static GFileOutputStream *
4268 g_file_real_append_to_finish (GFile         *file,
4269                               GAsyncResult  *res,
4270                               GError       **error)
4271 {
4272   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4273   gpointer op;
4274
4275   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4276
4277   op = g_simple_async_result_get_op_res_gpointer (simple);
4278   if (op)
4279     return g_object_ref (op);
4280   
4281   return NULL;
4282 }
4283
4284 static void
4285 create_async_thread (GSimpleAsyncResult *res,
4286                      GObject            *object,
4287                      GCancellable       *cancellable)
4288 {
4289   GFileIface *iface;
4290   GFileCreateFlags *data;
4291   GFileOutputStream *stream;
4292   GError *error = NULL;
4293
4294   iface = G_FILE_GET_IFACE (object);
4295
4296   data = g_simple_async_result_get_op_res_gpointer (res);
4297
4298   stream = iface->create (G_FILE (object), *data, cancellable, &error);
4299
4300   if (stream == NULL)
4301     {
4302       g_simple_async_result_set_from_error (res, error);
4303       g_error_free (error);
4304     }
4305   else
4306     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4307 }
4308
4309 static void
4310 g_file_real_create_async (GFile               *file,
4311                           GFileCreateFlags     flags,
4312                           int                  io_priority,
4313                           GCancellable        *cancellable,
4314                           GAsyncReadyCallback  callback,
4315                           gpointer             user_data)
4316 {
4317   GFileCreateFlags *data;
4318   GSimpleAsyncResult *res;
4319
4320   data = g_new0 (GFileCreateFlags, 1);
4321   *data = flags;
4322
4323   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4324   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4325
4326   g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4327   g_object_unref (res);
4328 }
4329
4330 static GFileOutputStream *
4331 g_file_real_create_finish (GFile         *file,
4332                            GAsyncResult  *res,
4333                            GError       **error)
4334 {
4335   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4336   gpointer op;
4337
4338   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4339
4340   op = g_simple_async_result_get_op_res_gpointer (simple);
4341   if (op)
4342     return g_object_ref (op);
4343   
4344   return NULL;
4345 }
4346
4347 typedef struct {
4348   GFileOutputStream *stream;
4349   char *etag;
4350   gboolean make_backup;
4351   GFileCreateFlags flags;
4352 } ReplaceAsyncData;
4353
4354 static void
4355 replace_async_data_free (ReplaceAsyncData *data)
4356 {
4357   if (data->stream)
4358     g_object_unref (data->stream);
4359   g_free (data->etag);
4360   g_free (data);
4361 }
4362
4363 static void
4364 replace_async_thread (GSimpleAsyncResult *res,
4365                       GObject            *object,
4366                       GCancellable       *cancellable)
4367 {
4368   GFileIface *iface;
4369   GFileOutputStream *stream;
4370   GError *error = NULL;
4371   ReplaceAsyncData *data;
4372
4373   iface = G_FILE_GET_IFACE (object);
4374   
4375   data = g_simple_async_result_get_op_res_gpointer (res);
4376
4377   stream = iface->replace (G_FILE (object),
4378                            data->etag,
4379                            data->make_backup,
4380                            data->flags,
4381                            cancellable,
4382                            &error);
4383
4384   if (stream == NULL)
4385     {
4386       g_simple_async_result_set_from_error (res, error);
4387       g_error_free (error);
4388     }
4389   else
4390     data->stream = stream;
4391 }
4392
4393 static void
4394 g_file_real_replace_async (GFile               *file,
4395                            const char          *etag,
4396                            gboolean             make_backup,
4397                            GFileCreateFlags     flags,
4398                            int                  io_priority,
4399                            GCancellable        *cancellable,
4400                            GAsyncReadyCallback  callback,
4401                            gpointer             user_data)
4402 {
4403   GSimpleAsyncResult *res;
4404   ReplaceAsyncData *data;
4405
4406   data = g_new0 (ReplaceAsyncData, 1);
4407   data->etag = g_strdup (etag);
4408   data->make_backup = make_backup;
4409   data->flags = flags;
4410
4411   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4412   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4413
4414   g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4415   g_object_unref (res);
4416 }
4417
4418 static GFileOutputStream *
4419 g_file_real_replace_finish (GFile         *file,
4420                             GAsyncResult  *res,
4421                             GError       **error)
4422 {
4423   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4424   ReplaceAsyncData *data;
4425
4426   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4427
4428   data = g_simple_async_result_get_op_res_gpointer (simple);
4429   if (data->stream)
4430     return g_object_ref (data->stream);
4431   
4432   return NULL;
4433 }
4434
4435 typedef struct {
4436   char *name;
4437   GFile *file;
4438 } SetDisplayNameAsyncData;
4439
4440 static void
4441 set_display_name_data_free (SetDisplayNameAsyncData *data)
4442 {
4443   g_free (data->name);
4444   if (data->file)
4445     g_object_unref (data->file);
4446   g_free (data);
4447 }
4448
4449 static void
4450 set_display_name_async_thread (GSimpleAsyncResult *res,
4451                                GObject            *object,
4452                                GCancellable       *cancellable)
4453 {
4454   GError *error = NULL;
4455   SetDisplayNameAsyncData *data;
4456   GFile *file;
4457   
4458   data = g_simple_async_result_get_op_res_gpointer (res);
4459   
4460   file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4461
4462   if (file == NULL)
4463     {
4464       g_simple_async_result_set_from_error (res, error);
4465       g_error_free (error);
4466     }
4467   else
4468     data->file = file;
4469 }
4470
4471 static void
4472 g_file_real_set_display_name_async (GFile               *file,  
4473                                     const char          *display_name,
4474                                     int                  io_priority,
4475                                     GCancellable        *cancellable,
4476                                     GAsyncReadyCallback  callback,
4477                                     gpointer             user_data)
4478 {
4479   GSimpleAsyncResult *res;
4480   SetDisplayNameAsyncData *data;
4481
4482   data = g_new0 (SetDisplayNameAsyncData, 1);
4483   data->name = g_strdup (display_name);
4484   
4485   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4486   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4487   
4488   g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4489   g_object_unref (res);
4490 }
4491
4492 static GFile *
4493 g_file_real_set_display_name_finish (GFile         *file,
4494                                      GAsyncResult  *res,
4495                                      GError       **error)
4496 {
4497   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4498   SetDisplayNameAsyncData *data;
4499
4500   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4501
4502   data = g_simple_async_result_get_op_res_gpointer (simple);
4503   if (data->file)
4504     return g_object_ref (data->file);
4505   
4506   return NULL;
4507 }
4508
4509 typedef struct {
4510   GFileQueryInfoFlags flags;
4511   GFileInfo *info;
4512   gboolean res;
4513   GError *error;
4514 } SetInfoAsyncData;
4515
4516 static void
4517 set_info_data_free (SetInfoAsyncData *data)
4518 {
4519   if (data->info)
4520     g_object_unref (data->info);
4521   if (data->error)
4522     g_error_free (data->error);
4523   g_free (data);
4524 }
4525
4526 static void
4527 set_info_async_thread (GSimpleAsyncResult *res,
4528                        GObject            *object,
4529                        GCancellable       *cancellable)
4530 {
4531   SetInfoAsyncData *data;
4532   
4533   data = g_simple_async_result_get_op_res_gpointer (res);
4534   
4535   data->error = NULL;
4536   data->res = g_file_set_attributes_from_info (G_FILE (object),
4537                                                data->info,
4538                                                data->flags,
4539                                                cancellable,
4540                                                &data->error);
4541 }
4542
4543 static void
4544 g_file_real_set_attributes_async (GFile               *file,
4545                                   GFileInfo           *info,
4546                                   GFileQueryInfoFlags  flags,
4547                                   int                  io_priority,
4548                                   GCancellable        *cancellable,
4549                                   GAsyncReadyCallback  callback,
4550                                   gpointer             user_data)
4551 {
4552   GSimpleAsyncResult *res;
4553   SetInfoAsyncData *data;
4554
4555   data = g_new0 (SetInfoAsyncData, 1);
4556   data->info = g_file_info_dup (info);
4557   data->flags = flags;
4558   
4559   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4560   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4561   
4562   g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4563   g_object_unref (res);
4564 }
4565
4566 static gboolean
4567 g_file_real_set_attributes_finish (GFile         *file,
4568                                    GAsyncResult  *res,
4569                                    GFileInfo    **info,
4570                                    GError       **error)
4571 {
4572   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4573   SetInfoAsyncData *data;
4574   
4575   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4576
4577   data = g_simple_async_result_get_op_res_gpointer (simple);
4578
4579   if (info) 
4580     *info = g_object_ref (data->info);
4581
4582   if (error != NULL && data->error) 
4583     *error = g_error_copy (data->error);
4584   
4585   return data->res;
4586 }
4587
4588 static void
4589 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4590                                     GObject            *object,
4591                                     GCancellable       *cancellable)
4592 {
4593   GError *error = NULL;
4594   GMount *mount;
4595   
4596   mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4597
4598   if (mount == NULL)
4599     {
4600       g_simple_async_result_set_from_error (res, error);
4601       g_error_free (error);
4602     }
4603   else
4604     g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4605 }
4606
4607 static void
4608 g_file_real_find_enclosing_mount_async (GFile               *file,
4609                                         int                  io_priority,
4610                                         GCancellable        *cancellable,
4611                                         GAsyncReadyCallback  callback,
4612                                         gpointer             user_data)
4613 {
4614   GSimpleAsyncResult *res;
4615   
4616   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4617   
4618   g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4619   g_object_unref (res);
4620 }
4621
4622 static GMount *
4623 g_file_real_find_enclosing_mount_finish (GFile         *file,
4624                                           GAsyncResult  *res,
4625                                           GError       **error)
4626 {
4627   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4628   GMount *mount;
4629
4630   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4631
4632   mount = g_simple_async_result_get_op_res_gpointer (simple);
4633   return g_object_ref (mount);
4634 }
4635
4636
4637 typedef struct {
4638   GFile *source;
4639   GFile *destination;
4640   GFileCopyFlags flags;
4641   GFileProgressCallback progress_cb;
4642   gpointer progress_cb_data;
4643   GIOSchedulerJob *job;
4644 } CopyAsyncData;
4645
4646 static void
4647 copy_async_data_free (CopyAsyncData *data)
4648 {
4649   g_object_unref (data->source);
4650   g_object_unref (data->destination);
4651   g_free (data);
4652 }
4653
4654 typedef struct {
4655   CopyAsyncData *data;
4656   goffset current_num_bytes;
4657   goffset total_num_bytes;
4658 } ProgressData;
4659
4660 static gboolean
4661 copy_async_progress_in_main (gpointer user_data)
4662 {
4663   ProgressData *progress = user_data;
4664   CopyAsyncData *data = progress->data;
4665
4666   data->progress_cb (progress->current_num_bytes,
4667                      progress->total_num_bytes,
4668                      data->progress_cb_data);
4669
4670   return FALSE;
4671 }
4672
4673 static gboolean
4674 mainloop_barrier (gpointer user_data)
4675 {
4676   /* Does nothing, but ensures all queued idles before
4677      this are run */
4678   return FALSE;
4679 }
4680
4681
4682 static void
4683 copy_async_progress_callback (goffset  current_num_bytes,
4684                               goffset  total_num_bytes,
4685                               gpointer user_data)
4686 {
4687   CopyAsyncData *data = user_data;
4688   ProgressData *progress;
4689
4690   progress = g_new (ProgressData, 1);
4691   progress->data = data;
4692   progress->current_num_bytes = current_num_bytes;
4693   progress->total_num_bytes = total_num_bytes;
4694   
4695   g_io_scheduler_job_send_to_mainloop_async (data->job,
4696                                              copy_async_progress_in_main,
4697                                              progress,
4698                                              g_free);
4699 }
4700
4701 static gboolean
4702 copy_async_thread (GIOSchedulerJob *job,
4703                    GCancellable    *cancellable,
4704                    gpointer         user_data)
4705 {
4706   GSimpleAsyncResult *res;
4707   CopyAsyncData *data;
4708   gboolean result;
4709   GError *error;
4710
4711   res = user_data;
4712   data = g_simple_async_result_get_op_res_gpointer (res);
4713
4714   error = NULL;
4715   data->job = job;
4716   result = g_file_copy (data->source,
4717                         data->destination,
4718                         data->flags,
4719                         cancellable,
4720                         (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4721                         data,
4722                         &error);
4723
4724   /* Ensure all progress callbacks are done running in main thread */
4725   if (data->progress_cb != NULL)
4726     g_io_scheduler_job_send_to_mainloop (job,
4727                                          mainloop_barrier,
4728                                          NULL, NULL);
4729   
4730   if (!result)
4731     {
4732       g_simple_async_result_set_from_error (res, error);
4733       g_error_free (error);
4734     }
4735
4736   g_simple_async_result_complete_in_idle (res);
4737
4738   return FALSE;
4739 }
4740
4741 static void
4742 g_file_real_copy_async (GFile                  *source,
4743                         GFile                  *destination,
4744                         GFileCopyFlags          flags,
4745                         int                     io_priority,
4746                         GCancellable           *cancellable,
4747                         GFileProgressCallback   progress_callback,
4748                         gpointer                progress_callback_data,
4749                         GAsyncReadyCallback     callback,
4750                         gpointer                user_data)
4751 {
4752   GSimpleAsyncResult *res;
4753   CopyAsyncData *data;
4754
4755   data = g_new0 (CopyAsyncData, 1);
4756   data->source = g_object_ref (source);
4757   data->destination = g_object_ref (destination);
4758   data->flags = flags;
4759   data->progress_cb = progress_callback;
4760   data->progress_cb_data = progress_callback_data;
4761
4762   res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4763   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4764
4765   g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4766 }
4767
4768 static gboolean
4769 g_file_real_copy_finish (GFile        *file,
4770                          GAsyncResult *res,
4771                          GError      **error)
4772 {
4773   /* Error handled in g_file_copy_finish() */
4774   return TRUE;
4775 }
4776
4777
4778 /********************************************
4779  *   Default VFS operations                 *
4780  ********************************************/
4781
4782 /**
4783  * g_file_new_for_path:
4784  * @path: a string containing a relative or absolute path.
4785  * 
4786  * Constructs a #GFile for a given path. This operation never
4787  * fails, but the returned object might not support any I/O
4788  * operation if @path is malformed.
4789  * 
4790  * Returns: a new #GFile for the given @path. 
4791  **/
4792 GFile *
4793 g_file_new_for_path (const char *path)
4794 {
4795   g_return_val_if_fail (path != NULL, NULL);
4796
4797   return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4798 }
4799  
4800 /**
4801  * g_file_new_for_uri:
4802  * @uri: a string containing a URI.
4803  * 
4804  * Constructs a #GFile for a given URI. This operation never 
4805  * fails, but the returned object might not support any I/O 
4806  * operation if @uri is malformed or if the uri type is 
4807  * not supported.
4808  * 
4809  * Returns: a #GFile for the given @uri.
4810  **/ 
4811 GFile *
4812 g_file_new_for_uri (const char *uri)
4813 {
4814   g_return_val_if_fail (uri != NULL, NULL);
4815
4816   return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4817 }
4818   
4819 /**
4820  * g_file_parse_name:
4821  * @parse_name: a file name or path to be parsed.
4822  * 
4823  * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4824  * This operation never fails, but the returned object might not support any I/O
4825  * operation if the @parse_name cannot be parsed.
4826  * 
4827  * Returns: a new #GFile.
4828  **/
4829 GFile *
4830 g_file_parse_name (const char *parse_name)
4831 {
4832   g_return_val_if_fail (parse_name != NULL, NULL);
4833
4834   return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4835 }
4836
4837 static gboolean
4838 is_valid_scheme_character (char c)
4839 {
4840   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4841 }
4842
4843 /* Following RFC 2396, valid schemes are built like:
4844  *       scheme        = alpha *( alpha | digit | "+" | "-" | "." )
4845  */
4846 static gboolean
4847 has_valid_scheme (const char *uri)
4848 {
4849   const char *p;
4850   
4851   p = uri;
4852   
4853   if (!g_ascii_isalpha (*p))
4854     return FALSE;
4855
4856   do {
4857     p++;
4858   } while (is_valid_scheme_character (*p));
4859
4860   return *p == ':';
4861 }
4862
4863 /**
4864  * g_file_new_for_commandline_arg:
4865  * @arg: a command line string.
4866  * 
4867  * Creates a #GFile with the given argument from the command line. The value of
4868  * @arg can be either a URI, an absolute path or a relative path resolved
4869  * relative to the current working directory.
4870  * This operation never fails, but the returned object might not support any
4871  * I/O operation if @arg points to a malformed path.
4872  *
4873  * Returns: a new #GFile. 
4874  **/
4875 GFile *
4876 g_file_new_for_commandline_arg (const char *arg)
4877 {
4878   GFile *file;
4879   char *filename;
4880   char *current_dir;
4881   
4882   g_return_val_if_fail (arg != NULL, NULL);
4883   
4884   if (g_path_is_absolute (arg))
4885     return g_file_new_for_path (arg);
4886
4887   if (has_valid_scheme (arg))
4888     return g_file_new_for_uri (arg);
4889     
4890   current_dir = g_get_current_dir ();
4891   filename = g_build_filename (current_dir, arg, NULL);
4892   g_free (current_dir);
4893   
4894   file = g_file_new_for_path (filename);
4895   g_free (filename);
4896   
4897   return file;
4898 }
4899
4900 /**
4901  * g_file_mount_enclosing_volume:
4902  * @location: input #GFile.
4903  * @flags: flags affecting the operation
4904  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4905  * @cancellable: optional #GCancellable object, %NULL to ignore.
4906  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4907  * @user_data: the data to pass to callback function
4908  * 
4909  * Starts a @mount_operation, mounting the volume that contains the file @location. 
4910  * 
4911  * When this operation has completed, @callback will be called with
4912  * @user_user data, and the operation can be finalized with 
4913  * g_file_mount_enclosing_volume_finish().
4914  * 
4915  * If @cancellable is not %NULL, then the operation can be cancelled by
4916  * triggering the cancellable object from another thread. If the operation
4917  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4918  **/
4919 void
4920 g_file_mount_enclosing_volume (GFile               *location,
4921                                GMountMountFlags     flags,
4922                                GMountOperation     *mount_operation,
4923                                GCancellable        *cancellable,
4924                                GAsyncReadyCallback  callback,
4925                                gpointer             user_data)
4926 {
4927   GFileIface *iface;
4928
4929   g_return_if_fail (G_IS_FILE (location));
4930
4931   iface = G_FILE_GET_IFACE (location);
4932
4933   if (iface->mount_enclosing_volume == NULL)
4934     {
4935       g_simple_async_report_error_in_idle (G_OBJECT (location),
4936                                            callback, user_data,
4937                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4938                                            _("volume doesn't implement mount"));
4939       
4940       return;
4941     }
4942   
4943   (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4944
4945 }
4946
4947 /**
4948  * g_file_mount_enclosing_volume_finish:
4949  * @location: input #GFile.
4950  * @result: a #GAsyncResult.
4951  * @error: a #GError, or %NULL
4952  * 
4953  * Finishes a mount operation started by g_file_mount_enclosing_volume().
4954  * 
4955  * Returns: %TRUE if successful. If an error
4956  * has occurred, this function will return %FALSE and set @error
4957  * appropriately if present.
4958  **/
4959 gboolean
4960 g_file_mount_enclosing_volume_finish (GFile         *location,
4961                                       GAsyncResult  *result,
4962                                       GError       **error)
4963 {
4964   GFileIface *iface;
4965
4966   g_return_val_if_fail (G_IS_FILE (location), FALSE);
4967   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4968
4969   if (G_IS_SIMPLE_ASYNC_RESULT (result))
4970     {
4971       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4972       if (g_simple_async_result_propagate_error (simple, error))
4973         return FALSE;
4974     }
4975   
4976   iface = G_FILE_GET_IFACE (location);
4977
4978   return (* iface->mount_enclosing_volume_finish) (location, result, error);
4979 }
4980
4981 /********************************************
4982  *   Utility functions                      *
4983  ********************************************/
4984
4985 /**
4986  * g_file_query_default_handler:
4987  * @file: a #GFile to open.
4988  * @cancellable: optional #GCancellable object, %NULL to ignore.
4989  * @error: a #GError, or %NULL
4990  *
4991  * Returns the #GAppInfo that is registered as the default
4992  * application to handle the file specified by @file.
4993  *
4994  * If @cancellable is not %NULL, then the operation can be cancelled by
4995  * triggering the cancellable object from another thread. If the operation
4996  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4997  *
4998  * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4999  * When you are done with it, release it with g_object_unref()
5000  **/
5001 GAppInfo *
5002 g_file_query_default_handler (GFile                  *file,
5003                               GCancellable           *cancellable,
5004                               GError                **error)
5005 {
5006   char *uri_scheme;
5007   const char *content_type;
5008   GAppInfo *appinfo;
5009   GFileInfo *info;
5010   char *path;
5011   
5012   uri_scheme = g_file_get_uri_scheme (file);
5013   appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
5014   g_free (uri_scheme);
5015
5016   if (appinfo != NULL)
5017     return appinfo;
5018
5019   info = g_file_query_info (file,
5020                             G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
5021                             0,
5022                             cancellable,
5023                             error);
5024   if (info == NULL)
5025     return NULL;
5026
5027   appinfo = NULL;
5028
5029   content_type = g_file_info_get_content_type (info);
5030   if (content_type)
5031     {
5032       /* Don't use is_native(), as we want to support fuse paths if availible */
5033       path = g_file_get_path (file);
5034       appinfo = g_app_info_get_default_for_type (content_type,
5035                                                  path == NULL);
5036       g_free (path);
5037     }
5038   
5039   g_object_unref (info);
5040
5041   if (appinfo != NULL)
5042     return appinfo;
5043
5044   g_set_error_literal (error, G_IO_ERROR,
5045                        G_IO_ERROR_NOT_SUPPORTED,
5046                        _("No application is registered as handling this file"));
5047   return NULL;
5048   
5049 }
5050
5051
5052 #define GET_CONTENT_BLOCK_SIZE 8192
5053
5054 /**
5055  * g_file_load_contents:
5056  * @file: input #GFile.
5057  * @cancellable: optional #GCancellable object, %NULL to ignore.
5058  * @contents: a location to place the contents of the file.
5059  * @length: a location to place the length of the contents of the file.
5060  * @etag_out: a location to place the current entity tag for the file.
5061  * @error: a #GError, or %NULL
5062  *
5063  * Loads the content of the file into memory, returning the size of
5064  * the data. The data is always zero terminated, but this is not
5065  * included in the resultant @length.
5066  * 
5067  * If @cancellable is not %NULL, then the operation can be cancelled by
5068  * triggering the cancellable object from another thread. If the operation
5069  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5070  * 
5071  * Returns: %TRUE if the @file's contents were successfully loaded.
5072  * %FALSE if there were errors..
5073  **/
5074 gboolean
5075 g_file_load_contents (GFile         *file,
5076                       GCancellable  *cancellable,
5077                       char         **contents,
5078                       gsize         *length,
5079                       char         **etag_out,
5080                       GError       **error)
5081 {
5082   GFileInputStream *in;
5083   GByteArray *content;
5084   gsize pos;
5085   gssize res;
5086   GFileInfo *info;
5087
5088   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5089   g_return_val_if_fail (contents != NULL, FALSE);
5090
5091   in = g_file_read (file, cancellable, error);
5092   if (in == NULL)
5093     return FALSE;
5094
5095   content = g_byte_array_new ();
5096   pos = 0;
5097   
5098   g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5099   while ((res = g_input_stream_read (G_INPUT_STREAM (in),
5100                                      content->data + pos,
5101                                      GET_CONTENT_BLOCK_SIZE,
5102                                      cancellable, error)) > 0)
5103     {
5104       pos += res;
5105       g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5106     }
5107
5108   if (etag_out)
5109     {
5110       *etag_out = NULL;
5111       
5112       info = g_file_input_stream_query_info (in,
5113                                              G_FILE_ATTRIBUTE_ETAG_VALUE,
5114                                              cancellable,
5115                                              NULL);
5116       if (info)
5117         {
5118           *etag_out = g_strdup (g_file_info_get_etag (info));
5119           g_object_unref (info);
5120         }
5121     } 
5122
5123   /* Ignore errors on close */
5124   g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
5125   g_object_unref (in);
5126
5127   if (res < 0)
5128     {
5129       /* error is set already */
5130       g_byte_array_free (content, TRUE);
5131       return FALSE;
5132     }
5133
5134   if (length)
5135     *length = pos;
5136
5137   /* Zero terminate (we got an extra byte allocated for this */
5138   content->data[pos] = 0;
5139   
5140   *contents = (char *)g_byte_array_free (content, FALSE);
5141   
5142   return TRUE;
5143 }
5144
5145 typedef struct {
5146   GFile *file;
5147   GError *error;
5148   GCancellable *cancellable;
5149   GFileReadMoreCallback read_more_callback;
5150   GAsyncReadyCallback callback;
5151   gpointer user_data;
5152   GByteArray *content;
5153   gsize pos;
5154   char *etag;
5155 } LoadContentsData;
5156
5157
5158 static void
5159 load_contents_data_free (LoadContentsData *data)
5160 {
5161   if (data->error)
5162     g_error_free (data->error);
5163   if (data->cancellable)
5164     g_object_unref (data->cancellable);
5165   if (data->content)
5166     g_byte_array_free (data->content, TRUE);
5167   g_free (data->etag);
5168   g_object_unref (data->file);
5169   g_free (data);
5170 }
5171
5172 static void
5173 load_contents_close_callback (GObject      *obj,
5174                               GAsyncResult *close_res,
5175                               gpointer      user_data)
5176 {
5177   GInputStream *stream = G_INPUT_STREAM (obj);
5178   LoadContentsData *data = user_data;
5179   GSimpleAsyncResult *res;
5180
5181   /* Ignore errors here, we're only reading anyway */
5182   g_input_stream_close_finish (stream, close_res, NULL);
5183   g_object_unref (stream);
5184
5185   res = g_simple_async_result_new (G_OBJECT (data->file),
5186                                    data->callback,
5187                                    data->user_data,
5188                                    g_file_load_contents_async);
5189   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5190   g_simple_async_result_complete (res);
5191   g_object_unref (res);
5192 }
5193
5194 static void
5195 load_contents_fstat_callback (GObject      *obj,
5196                               GAsyncResult *stat_res,
5197                               gpointer      user_data)
5198 {
5199   GInputStream *stream = G_INPUT_STREAM (obj);
5200   LoadContentsData *data = user_data;
5201   GFileInfo *info;
5202
5203   info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5204                                                    stat_res, NULL);
5205   if (info)
5206     {
5207       data->etag = g_strdup (g_file_info_get_etag (info));
5208       g_object_unref (info);
5209     }
5210
5211   g_input_stream_close_async (stream, 0,
5212                               data->cancellable,
5213                               load_contents_close_callback, data);
5214 }
5215
5216 static void
5217 load_contents_read_callback (GObject      *obj,
5218                              GAsyncResult *read_res,
5219                              gpointer      user_data)
5220 {
5221   GInputStream *stream = G_INPUT_STREAM (obj);
5222   LoadContentsData *data = user_data;
5223   GError *error = NULL;
5224   gssize read_size;
5225
5226   read_size = g_input_stream_read_finish (stream, read_res, &error);
5227
5228   if (read_size < 0) 
5229     {
5230       /* Error or EOF, close the file */
5231       data->error = error;
5232       g_input_stream_close_async (stream, 0,
5233                                   data->cancellable,
5234                                   load_contents_close_callback, data);
5235     }
5236   else if (read_size == 0)
5237     {
5238       g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5239                                             G_FILE_ATTRIBUTE_ETAG_VALUE,
5240                                             0,
5241                                             data->cancellable,
5242                                             load_contents_fstat_callback,
5243                                             data);
5244     }
5245   else if (read_size > 0)
5246     {
5247       data->pos += read_size;
5248       
5249       g_byte_array_set_size (data->content,
5250                              data->pos + GET_CONTENT_BLOCK_SIZE);
5251
5252
5253       if (data->read_more_callback &&
5254           !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5255         g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5256                                               G_FILE_ATTRIBUTE_ETAG_VALUE,
5257                                               0,
5258                                               data->cancellable,
5259                                               load_contents_fstat_callback,
5260                                               data);
5261       else 
5262         g_input_stream_read_async (stream,
5263                                    data->content->data + data->pos,
5264                                    GET_CONTENT_BLOCK_SIZE,
5265                                    0,
5266                                    data->cancellable,
5267                                    load_contents_read_callback,
5268                                    data);
5269     }
5270 }
5271
5272 static void
5273 load_contents_open_callback (GObject      *obj,
5274                              GAsyncResult *open_res,
5275                              gpointer      user_data)
5276 {
5277   GFile *file = G_FILE (obj);
5278   GFileInputStream *stream;
5279   LoadContentsData *data = user_data;
5280   GError *error = NULL;
5281   GSimpleAsyncResult *res;
5282
5283   stream = g_file_read_finish (file, open_res, &error);
5284
5285   if (stream)
5286     {
5287       g_byte_array_set_size (data->content,
5288                              data->pos + GET_CONTENT_BLOCK_SIZE);
5289       g_input_stream_read_async (G_INPUT_STREAM (stream),
5290                                  data->content->data + data->pos,
5291                                  GET_CONTENT_BLOCK_SIZE,
5292                                  0,
5293                                  data->cancellable,
5294                                  load_contents_read_callback,
5295                                  data);
5296       
5297     }
5298   else
5299     {
5300       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5301                                                   data->callback,
5302                                                   data->user_data,
5303                                                   error);
5304       g_simple_async_result_complete (res);
5305       g_error_free (error);
5306       load_contents_data_free (data);
5307       g_object_unref (res);
5308     }
5309 }
5310
5311 /**
5312  * g_file_load_partial_contents_async:
5313  * @file: input #GFile.
5314  * @cancellable: optional #GCancellable object, %NULL to ignore.
5315  * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5316  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5317  * @user_data: the data to pass to the callback functions.
5318  *
5319  * Reads the partial contents of a file. A #GFileReadMoreCallback should be 
5320  * used to stop reading from the file when appropriate, else this function
5321  * will behave exactly as g_file_load_contents_async(). This operation 
5322  * can be finished by g_file_load_partial_contents_finish().
5323  *
5324  * Users of this function should be aware that @user_data is passed to 
5325  * both the @read_more_callback and the @callback.
5326  *
5327  * If @cancellable is not %NULL, then the operation can be cancelled by
5328  * triggering the cancellable object from another thread. If the operation
5329  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5330  **/
5331 void
5332 g_file_load_partial_contents_async (GFile                 *file,
5333                                     GCancellable          *cancellable,
5334                                     GFileReadMoreCallback  read_more_callback,
5335                                     GAsyncReadyCallback    callback,
5336                                     gpointer               user_data)
5337 {
5338   LoadContentsData *data;
5339
5340   g_return_if_fail (G_IS_FILE (file));
5341
5342   data = g_new0 (LoadContentsData, 1);
5343
5344   if (cancellable)
5345     data->cancellable = g_object_ref (cancellable);
5346   data->read_more_callback = read_more_callback;
5347   data->callback = callback;
5348   data->user_data = user_data;
5349   data->content = g_byte_array_new ();
5350   data->file = g_object_ref (file);
5351
5352   g_file_read_async (file,
5353                      0,
5354                      cancellable,
5355                      load_contents_open_callback,
5356                      data);
5357 }
5358
5359 /**
5360  * g_file_load_partial_contents_finish:
5361  * @file: input #GFile.
5362  * @res: a #GAsyncResult. 
5363  * @contents: a location to place the contents of the file.
5364  * @length: a location to place the length of the contents of the file.
5365  * @etag_out: a location to place the current entity tag for the file.
5366  * @error: a #GError, or %NULL
5367  * 
5368  * Finishes an asynchronous partial load operation that was started
5369  * with g_file_load_partial_contents_async().
5370  *
5371  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
5372  * present, it will be set appropriately. 
5373  **/
5374 gboolean
5375 g_file_load_partial_contents_finish (GFile         *file,
5376                                      GAsyncResult  *res,
5377                                      char         **contents,
5378                                      gsize         *length,
5379                                      char         **etag_out,
5380                                      GError       **error)
5381 {
5382   GSimpleAsyncResult *simple;
5383   LoadContentsData *data;
5384
5385   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5386   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5387   g_return_val_if_fail (contents != NULL, FALSE);
5388
5389   simple = G_SIMPLE_ASYNC_RESULT (res);
5390
5391   if (g_simple_async_result_propagate_error (simple, error))
5392     return FALSE;
5393   
5394   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5395   
5396   data = g_simple_async_result_get_op_res_gpointer (simple);
5397
5398   if (data->error)
5399     {
5400       g_propagate_error (error, data->error);
5401       data->error = NULL;
5402       *contents = NULL;
5403       if (length)
5404         *length = 0;
5405       return FALSE;
5406     }
5407
5408   if (length)
5409     *length = data->pos;
5410
5411   if (etag_out)
5412     {
5413       *etag_out = data->etag;
5414       data->etag = NULL;
5415     }
5416
5417   /* Zero terminate */
5418   g_byte_array_set_size (data->content, data->pos + 1);
5419   data->content->data[data->pos] = 0;
5420   
5421   *contents = (char *)g_byte_array_free (data->content, FALSE);
5422   data->content = NULL;
5423
5424   return TRUE;
5425 }
5426
5427 /**
5428  * g_file_load_contents_async:
5429  * @file: input #GFile.
5430  * @cancellable: optional #GCancellable object, %NULL to ignore.
5431  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5432  * @user_data: the data to pass to callback function
5433  * 
5434  * Starts an asynchronous load of the @file's contents.
5435  *
5436  * For more details, see g_file_load_contents() which is
5437  * the synchronous version of this call.
5438  *
5439  * When the load operation has completed, @callback will be called 
5440  * with @user data. To finish the operation, call 
5441  * g_file_load_contents_finish() with the #GAsyncResult returned by 
5442  * the @callback.
5443  * 
5444  * If @cancellable is not %NULL, then the operation can be cancelled by
5445  * triggering the cancellable object from another thread. If the operation
5446  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5447  **/
5448 void
5449 g_file_load_contents_async (GFile               *file,
5450                            GCancellable        *cancellable,
5451                            GAsyncReadyCallback  callback,
5452                            gpointer             user_data)
5453 {
5454   g_file_load_partial_contents_async (file,
5455                                       cancellable,
5456                                       NULL,
5457                                       callback, user_data);
5458 }
5459
5460 /**
5461  * g_file_load_contents_finish:
5462  * @file: input #GFile.
5463  * @res: a #GAsyncResult. 
5464  * @contents: a location to place the contents of the file.
5465  * @length: a location to place the length of the contents of the file.
5466  * @etag_out: a location to place the current entity tag for the file.
5467  * @error: a #GError, or %NULL
5468  * 
5469  * Finishes an asynchronous load of the @file's contents. 
5470  * The contents are placed in @contents, and @length is set to the 
5471  * size of the @contents string. If @etag_out is present, it will be 
5472  * set to the new entity tag for the @file.
5473  * 
5474  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
5475  * present, it will be set appropriately. 
5476  **/
5477 gboolean
5478 g_file_load_contents_finish (GFile         *file,
5479                              GAsyncResult  *res,
5480                              char         **contents,
5481                              gsize         *length,
5482                              char         **etag_out,
5483                              GError       **error)
5484 {
5485   return g_file_load_partial_contents_finish (file,
5486                                               res,
5487                                               contents,
5488                                               length,
5489                                               etag_out,
5490                                               error);
5491 }
5492   
5493 /**
5494  * g_file_replace_contents:
5495  * @file: input #GFile.
5496  * @contents: a string containing the new contents for @file.
5497  * @length: the length of @contents in bytes.
5498  * @etag: the old <link linkend="gfile-etag">entity tag</link> 
5499  *     for the document.
5500  * @make_backup: %TRUE if a backup should be created.
5501  * @flags: a set of #GFileCreateFlags.
5502  * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5503  *      for the document. This should be freed with g_free() when no longer 
5504  *      needed.
5505  * @cancellable: optional #GCancellable object, %NULL to ignore.
5506  * @error: a #GError, or %NULL
5507  *
5508  * Replaces the contents of @file with @contents of @length bytes.
5509  
5510  * If @etag is specified (not %NULL) any existing file must have that etag, or
5511  * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5512  *
5513  * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5514  * 
5515  * If @cancellable is not %NULL, then the operation can be cancelled by
5516  * triggering the cancellable object from another thread. If the operation
5517  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5518  *
5519  * The returned @new_etag can be used to verify that the file hasn't changed the
5520  * next time it is saved over.
5521  * 
5522  * Returns: %TRUE if successful. If an error
5523  * has occurred, this function will return %FALSE and set @error
5524  * appropriately if present.
5525  **/
5526 gboolean
5527 g_file_replace_contents (GFile             *file,
5528                          const char        *contents,
5529                          gsize              length,
5530                          const char        *etag,
5531                          gboolean           make_backup,
5532                          GFileCreateFlags   flags,
5533                          char             **new_etag,
5534                          GCancellable      *cancellable,
5535                          GError           **error)
5536 {
5537   GFileOutputStream *out;
5538   gsize pos, remainder;
5539   gssize res;
5540   gboolean ret;
5541
5542   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5543   g_return_val_if_fail (contents != NULL, FALSE);
5544
5545   out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5546   if (out == NULL)
5547     return FALSE;
5548
5549   pos = 0;
5550   remainder = length;
5551   while (remainder > 0 &&
5552          (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5553                                        contents + pos,
5554                                        MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5555                                        cancellable,
5556                                        error)) > 0)
5557     {
5558       pos += res;
5559       remainder -= res;
5560     }
5561   
5562   if (remainder > 0 && res < 0)
5563     {
5564       /* Ignore errors on close */
5565       g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5566       g_object_unref (out);
5567
5568       /* error is set already */
5569       return FALSE;
5570     }
5571   
5572   ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
5573
5574   if (new_etag)
5575     *new_etag = g_file_output_stream_get_etag (out);
5576
5577   g_object_unref (out);
5578
5579   return ret;
5580 }
5581
5582 typedef struct {
5583   GFile *file;
5584   GError *error;
5585   GCancellable *cancellable;
5586   GAsyncReadyCallback callback;
5587   gpointer user_data;
5588   const char *content;
5589   gsize length;
5590   gsize pos;
5591   char *etag;
5592 } ReplaceContentsData;
5593
5594 static void
5595 replace_contents_data_free (ReplaceContentsData *data)
5596 {
5597   if (data->error)
5598     g_error_free (data->error);
5599   if (data->cancellable)
5600     g_object_unref (data->cancellable);
5601   g_object_unref (data->file);
5602   g_free (data->etag);
5603   g_free (data);
5604 }
5605
5606 static void
5607 replace_contents_close_callback (GObject      *obj,
5608                                  GAsyncResult *close_res,
5609                                  gpointer      user_data)
5610 {
5611   GOutputStream *stream = G_OUTPUT_STREAM (obj);
5612   ReplaceContentsData *data = user_data;
5613   GSimpleAsyncResult *res;
5614
5615   /* Ignore errors here, we're only reading anyway */
5616   g_output_stream_close_finish (stream, close_res, NULL);
5617   g_object_unref (stream);
5618
5619   data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5620   
5621   res = g_simple_async_result_new (G_OBJECT (data->file),
5622                                    data->callback,
5623                                    data->user_data,
5624                                    g_file_replace_contents_async);
5625   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5626   g_simple_async_result_complete (res);
5627   g_object_unref (res);
5628 }
5629
5630 static void
5631 replace_contents_write_callback (GObject      *obj,
5632                                  GAsyncResult *read_res,
5633                                  gpointer      user_data)
5634 {
5635   GOutputStream *stream = G_OUTPUT_STREAM (obj);
5636   ReplaceContentsData *data = user_data;
5637   GError *error = NULL;
5638   gssize write_size;
5639   
5640   write_size = g_output_stream_write_finish (stream, read_res, &error);
5641
5642   if (write_size <= 0) 
5643     {
5644       /* Error or EOF, close the file */
5645       if (write_size < 0)
5646         data->error = error;
5647       g_output_stream_close_async (stream, 0,
5648                                    data->cancellable,
5649                                    replace_contents_close_callback, data);
5650     }
5651   else if (write_size > 0)
5652     {
5653       data->pos += write_size;
5654
5655       if (data->pos >= data->length)
5656         g_output_stream_close_async (stream, 0,
5657                                      data->cancellable,
5658                                      replace_contents_close_callback, data);
5659       else
5660         g_output_stream_write_async (stream,
5661                                      data->content + data->pos,
5662                                      data->length - data->pos,
5663                                      0,
5664                                      data->cancellable,
5665                                      replace_contents_write_callback,
5666                                      data);
5667     }
5668 }
5669
5670 static void
5671 replace_contents_open_callback (GObject      *obj,
5672                                 GAsyncResult *open_res,
5673                                 gpointer      user_data)
5674 {
5675   GFile *file = G_FILE (obj);
5676   GFileOutputStream *stream;
5677   ReplaceContentsData *data = user_data;
5678   GError *error = NULL;
5679   GSimpleAsyncResult *res;
5680
5681   stream = g_file_replace_finish (file, open_res, &error);
5682
5683   if (stream)
5684     {
5685       g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5686                                    data->content + data->pos,
5687                                    data->length - data->pos,
5688                                    0,
5689                                    data->cancellable,
5690                                    replace_contents_write_callback,
5691                                    data);
5692       
5693     }
5694   else
5695     {
5696       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5697                                                   data->callback,
5698                                                   data->user_data,
5699                                                   error);
5700       g_simple_async_result_complete (res);
5701       g_error_free (error);
5702       replace_contents_data_free (data);
5703       g_object_unref (res);
5704     }
5705 }
5706
5707 /**
5708  * g_file_replace_contents_async:
5709  * @file: input #GFile.
5710  * @contents: string of contents to replace the file with.
5711  * @length: the length of @contents in bytes.
5712  * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5713  * @make_backup: %TRUE if a backup should be created.
5714  * @flags: a set of #GFileCreateFlags.
5715  * @cancellable: optional #GCancellable object, %NULL to ignore.
5716  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5717  * @user_data: the data to pass to callback function
5718  * 
5719  * Starts an asynchronous replacement of @file with the given 
5720  * @contents of @length bytes. @etag will replace the document's 
5721  * current entity tag.
5722  * 
5723  * When this operation has completed, @callback will be called with
5724  * @user_user data, and the operation can be finalized with 
5725  * g_file_replace_contents_finish().
5726  * 
5727  * If @cancellable is not %NULL, then the operation can be cancelled by
5728  * triggering the cancellable object from another thread. If the operation
5729  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5730  * 
5731  * If @make_backup is %TRUE, this function will attempt to 
5732  * make a backup of @file.
5733  **/
5734 void
5735 g_file_replace_contents_async  (GFile               *file,
5736                                 const char          *contents,
5737                                 gsize                length,
5738                                 const char          *etag,
5739                                 gboolean             make_backup,
5740                                 GFileCreateFlags     flags,
5741                                 GCancellable        *cancellable,
5742                                 GAsyncReadyCallback  callback,
5743                                 gpointer             user_data)
5744 {
5745   ReplaceContentsData *data;
5746
5747   g_return_if_fail (G_IS_FILE (file));
5748   g_return_if_fail (contents != NULL);
5749
5750   data = g_new0 (ReplaceContentsData, 1);
5751
5752   if (cancellable)
5753     data->cancellable = g_object_ref (cancellable);
5754   data->callback = callback;
5755   data->user_data = user_data;
5756   data->content = contents;
5757   data->length = length;
5758   data->pos = 0;
5759   data->file = g_object_ref (file);
5760
5761   g_file_replace_async (file,
5762                         etag,
5763                         make_backup,
5764                         flags,
5765                         0,
5766                         cancellable,
5767                         replace_contents_open_callback,
5768                         data);
5769 }
5770   
5771 /**
5772  * g_file_replace_contents_finish:
5773  * @file: input #GFile.
5774  * @res: a #GAsyncResult. 
5775  * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link> 
5776  *     for the document. This should be freed with g_free() when it is no 
5777  *     longer needed.
5778  * @error: a #GError, or %NULL 
5779  * 
5780  * Finishes an asynchronous replace of the given @file. See
5781  * g_file_replace_contents_async(). Sets @new_etag to the new entity 
5782  * tag for the document, if present.
5783  * 
5784  * Returns: %TRUE on success, %FALSE on failure.
5785  **/
5786 gboolean
5787 g_file_replace_contents_finish (GFile         *file,
5788                                 GAsyncResult  *res,
5789                                 char         **new_etag,
5790                                 GError       **error)
5791 {
5792   GSimpleAsyncResult *simple;
5793   ReplaceContentsData *data;
5794
5795   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5796   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5797
5798   simple = G_SIMPLE_ASYNC_RESULT (res);
5799
5800   if (g_simple_async_result_propagate_error (simple, error))
5801     return FALSE;
5802   
5803   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5804   
5805   data = g_simple_async_result_get_op_res_gpointer (simple);
5806
5807   if (data->error)
5808     {
5809       g_propagate_error (error, data->error);
5810       data->error = NULL;
5811       return FALSE;
5812     }
5813
5814
5815   if (new_etag)
5816     {
5817       *new_etag = data->etag;
5818       data->etag = NULL; /* Take ownership */
5819     }
5820   
5821   return TRUE;
5822 }
5823
5824 #define __G_FILE_C__
5825 #include "gioaliasdef.c"