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