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