g_file_make_directory_with_parents: clean up logic
[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   GFile *work_file = NULL;
3364   GList *list = NULL, *l;
3365   GError *my_error = NULL;
3366
3367   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3368
3369   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3370     return FALSE;
3371   
3372   g_file_make_directory (file, cancellable, &my_error);
3373   if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3374     {
3375       if (my_error)
3376         g_propagate_error (error, my_error);
3377       return my_error == NULL;
3378     }
3379   
3380   work_file = g_object_ref (file);
3381   
3382   while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3383     {
3384       GFile *parent_file;
3385
3386       parent_file = g_file_get_parent (work_file);
3387       if (parent_file == NULL)
3388         break;
3389
3390       g_clear_error (&my_error);
3391       g_file_make_directory (parent_file, cancellable, &my_error);
3392
3393       g_object_unref (work_file);
3394       work_file = g_object_ref (parent_file);
3395
3396       if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3397         list = g_list_prepend (list, parent_file);  /* Transfer ownership of ref */
3398       else
3399         g_object_unref (parent_file);
3400     }
3401
3402   for (l = list; my_error == NULL && l; l = l->next)
3403     {
3404       g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3405     }
3406
3407   if (work_file)
3408     g_object_unref (work_file);
3409   
3410   /* Clean up */
3411   while (list != NULL) 
3412     {
3413       g_object_unref ((GFile *) list->data);
3414       list = g_list_remove (list, list->data);
3415     }
3416
3417   if (my_error != NULL)
3418     {
3419       g_propagate_error (error, my_error);
3420       return FALSE;
3421     }
3422   
3423   return g_file_make_directory (file, cancellable, error);
3424 }
3425
3426 /**
3427  * g_file_make_symbolic_link:
3428  * @file: a #GFile with the name of the symlink to create
3429  * @symlink_value: a string with the path for the target of the new symlink
3430  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3431  * @error: a #GError.
3432  *
3433  * Creates a symbolic link named @file which contains the string
3434  * @symlink_value.
3435  *
3436  * If @cancellable is not %NULL, then the operation can be cancelled by
3437  * triggering the cancellable object from another thread. If the operation
3438  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3439  *
3440  * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3441  */
3442 gboolean
3443 g_file_make_symbolic_link (GFile         *file,
3444                            const char    *symlink_value,
3445                            GCancellable  *cancellable,
3446                            GError       **error)
3447 {
3448   GFileIface *iface;
3449
3450   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3451   g_return_val_if_fail (symlink_value != NULL, FALSE);
3452
3453   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3454     return FALSE;
3455
3456   if (*symlink_value == '\0')
3457     {
3458       g_set_error_literal (error, G_IO_ERROR,
3459                            G_IO_ERROR_INVALID_ARGUMENT,
3460                            _("Invalid symlink value given"));
3461       return FALSE;
3462     }
3463   
3464   iface = G_FILE_GET_IFACE (file);
3465
3466   if (iface->make_symbolic_link == NULL)
3467     {
3468       g_set_error_literal (error, G_IO_ERROR,
3469                            G_IO_ERROR_NOT_SUPPORTED,
3470                            _("Operation not supported"));
3471       return FALSE;
3472     }
3473   
3474   return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3475 }
3476
3477 /**
3478  * g_file_delete:
3479  * @file: input #GFile.
3480  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3481  * @error: a #GError, or %NULL 
3482  * 
3483  * Deletes a file. If the @file is a directory, it will only be deleted if it 
3484  * is empty.  This has the same semantics as g_unlink().
3485  * 
3486  * If @cancellable is not %NULL, then the operation can be cancelled by
3487  * triggering the cancellable object from another thread. If the operation
3488  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3489  *
3490  * Virtual: delete_file
3491  * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3492  **/
3493 gboolean
3494 g_file_delete (GFile         *file,
3495                GCancellable  *cancellable,
3496                GError       **error)
3497 {
3498   GFileIface *iface;
3499   
3500   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3501
3502   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3503     return FALSE;
3504   
3505   iface = G_FILE_GET_IFACE (file);
3506
3507   if (iface->delete_file == NULL)
3508     {
3509       g_set_error_literal (error, G_IO_ERROR,
3510                            G_IO_ERROR_NOT_SUPPORTED,
3511                            _("Operation not supported"));
3512       return FALSE;
3513     }
3514   
3515   return (* iface->delete_file) (file, cancellable, error);
3516 }
3517
3518 /**
3519  * g_file_delete_async:
3520  * @file: input #GFile.
3521  * @io_priority: the <link linkend="io-priority">I/O priority</link>
3522  *     of the request
3523  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3524  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3525  * @user_data: the data to pass to callback function
3526  *
3527  * Asynchronously delete a file. If the @file is a directory, it will
3528  * only be deleted if it is empty.  This has the same semantics as
3529  * g_unlink().
3530  *
3531  * Virtual: delete_file_async
3532  * Since: 2.34
3533  **/
3534 void
3535 g_file_delete_async (GFile               *file,
3536                      int                  io_priority,
3537                      GCancellable        *cancellable,
3538                      GAsyncReadyCallback  callback,
3539                      gpointer             user_data)
3540 {
3541   GFileIface *iface;
3542
3543   g_return_if_fail (G_IS_FILE (file));
3544
3545   iface = G_FILE_GET_IFACE (file);
3546   (* iface->delete_file_async) (file,
3547                                 io_priority,
3548                                 cancellable,
3549                                 callback,
3550                                 user_data);
3551 }
3552
3553 /**
3554  * g_file_delete_finish:
3555  * @file: input #GFile.
3556  * @res: a #GAsyncResult.
3557  * @error: a #GError, or %NULL
3558  *
3559  * Finishes deleting a file started with
3560  * g_file_delete_async().
3561  *
3562  * Virtual: delete_file_finish
3563  * Since: 2.34
3564  **/
3565 gboolean
3566 g_file_delete_finish (GFile            *file,
3567                       GAsyncResult     *result,
3568                       GError          **error)
3569 {
3570   GFileIface *iface;
3571
3572   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3573   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3574
3575   if (g_async_result_legacy_propagate_error (result, error))
3576     return FALSE;
3577
3578   iface = G_FILE_GET_IFACE (file);
3579   return (* iface->delete_file_finish) (file, result, error);
3580 }
3581
3582 /**
3583  * g_file_trash:
3584  * @file: #GFile to send to trash.
3585  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3586  * @error: a #GError, or %NULL
3587  *
3588  * Sends @file to the "Trashcan", if possible. This is similar to
3589  * deleting it, but the user can recover it before emptying the trashcan.
3590  * Not all file systems support trashing, so this call can return the
3591  * %G_IO_ERROR_NOT_SUPPORTED error.
3592  *
3593  *
3594  * If @cancellable is not %NULL, then the operation can be cancelled by
3595  * triggering the cancellable object from another thread. If the operation
3596  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3597  * 
3598  * Returns: %TRUE on successful trash, %FALSE otherwise.
3599  **/
3600 gboolean
3601 g_file_trash (GFile         *file,
3602               GCancellable  *cancellable,
3603               GError       **error)
3604 {
3605   GFileIface *iface;
3606   
3607   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3608
3609   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3610     return FALSE;
3611   
3612   iface = G_FILE_GET_IFACE (file);
3613
3614   if (iface->trash == NULL)
3615     {
3616       g_set_error_literal (error,
3617                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3618                            _("Trash not supported"));
3619       return FALSE;
3620     }
3621   
3622   return (* iface->trash) (file, cancellable, error);
3623 }
3624
3625 /**
3626  * g_file_set_display_name:
3627  * @file: input #GFile.
3628  * @display_name: a string.
3629  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3630  * @error: a #GError, or %NULL
3631  * 
3632  * Renames @file to the specified display name.
3633  *
3634  * The display name is converted from UTF8 to the correct encoding for the target
3635  * filesystem if possible and the @file is renamed to this.
3636  * 
3637  * If you want to implement a rename operation in the user interface the edit name
3638  * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
3639  * widget, and then the result after editing should be passed to g_file_set_display_name().
3640  *
3641  * On success the resulting converted filename is returned.
3642  * 
3643  * If @cancellable is not %NULL, then the operation can be cancelled by
3644  * triggering the cancellable object from another thread. If the operation
3645  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3646  * 
3647  * Returns: (transfer full): a #GFile specifying what @file was renamed to, or %NULL 
3648  *     if there was an error.
3649  *     Free the returned object with g_object_unref().
3650  **/
3651 GFile *
3652 g_file_set_display_name (GFile         *file,
3653                          const char    *display_name,
3654                          GCancellable  *cancellable,
3655                          GError       **error)
3656 {
3657   GFileIface *iface;
3658   
3659   g_return_val_if_fail (G_IS_FILE (file), NULL);
3660   g_return_val_if_fail (display_name != NULL, NULL);
3661
3662   if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3663     {
3664       g_set_error (error,
3665                    G_IO_ERROR,
3666                    G_IO_ERROR_INVALID_ARGUMENT,
3667                    _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3668       return NULL;
3669     }
3670   
3671   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3672     return NULL;
3673   
3674   iface = G_FILE_GET_IFACE (file);
3675
3676   return (* iface->set_display_name) (file, display_name, cancellable, error);
3677 }
3678
3679 /**
3680  * g_file_set_display_name_async:
3681  * @file: input #GFile.
3682  * @display_name: a string.
3683  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
3684  *     of the request. 
3685  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3686  * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
3687  * @user_data: (closure): the data to pass to callback function
3688  * 
3689  * Asynchronously sets the display name for a given #GFile.
3690  * 
3691  * For more details, see g_file_set_display_name() which is
3692  * the synchronous version of this call.
3693  *
3694  * When the operation is finished, @callback will be called. You can then call
3695  * g_file_set_display_name_finish() to get the result of the operation.
3696  **/
3697 void
3698 g_file_set_display_name_async (GFile               *file,
3699                                const char          *display_name,
3700                                int                  io_priority,
3701                                GCancellable        *cancellable,
3702                                GAsyncReadyCallback  callback,
3703                                gpointer             user_data)
3704 {
3705   GFileIface *iface;
3706   
3707   g_return_if_fail (G_IS_FILE (file));
3708   g_return_if_fail (display_name != NULL);
3709
3710   iface = G_FILE_GET_IFACE (file);
3711   (* iface->set_display_name_async) (file,
3712                                      display_name,
3713                                      io_priority,
3714                                      cancellable,
3715                                      callback,
3716                                      user_data);
3717 }
3718
3719 /**
3720  * g_file_set_display_name_finish:
3721  * @file: input #GFile.
3722  * @res: a #GAsyncResult. 
3723  * @error: a #GError, or %NULL
3724  * 
3725  * Finishes setting a display name started with 
3726  * g_file_set_display_name_async().
3727  * 
3728  * Returns: (transfer full): a #GFile or %NULL on error.
3729  *     Free the returned object with g_object_unref().
3730  **/
3731 GFile *
3732 g_file_set_display_name_finish (GFile         *file,
3733                                 GAsyncResult  *res,
3734                                 GError       **error)
3735 {
3736   GFileIface *iface;
3737   
3738   g_return_val_if_fail (G_IS_FILE (file), NULL);
3739   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3740
3741   if (g_async_result_legacy_propagate_error (res, error))
3742     return NULL;
3743   
3744   iface = G_FILE_GET_IFACE (file);
3745   return (* iface->set_display_name_finish) (file, res, error);
3746 }
3747
3748 /**
3749  * g_file_query_settable_attributes:
3750  * @file: input #GFile.
3751  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3752  * @error: a #GError, or %NULL
3753  * 
3754  * Obtain the list of settable attributes for the file.
3755  *
3756  * Returns the type and full attribute name of all the attributes 
3757  * that can be set on this file. This doesn't mean setting it will always 
3758  * succeed though, you might get an access failure, or some specific 
3759  * file may not support a specific attribute.
3760  *
3761  * If @cancellable is not %NULL, then the operation can be cancelled by
3762  * triggering the cancellable object from another thread. If the operation
3763  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3764  * 
3765  * Returns: a #GFileAttributeInfoList describing the settable attributes.
3766  * When you are done with it, release it with g_file_attribute_info_list_unref()
3767  **/
3768 GFileAttributeInfoList *
3769 g_file_query_settable_attributes (GFile         *file,
3770                                   GCancellable  *cancellable,
3771                                   GError       **error)
3772 {
3773   GFileIface *iface;
3774   GError *my_error;
3775   GFileAttributeInfoList *list;
3776
3777   g_return_val_if_fail (G_IS_FILE (file), NULL);
3778
3779   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3780     return NULL;
3781   
3782   iface = G_FILE_GET_IFACE (file);
3783
3784   if (iface->query_settable_attributes == NULL)
3785     return g_file_attribute_info_list_new ();
3786
3787   my_error = NULL;
3788   list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3789   
3790   if (list == NULL)
3791     {
3792       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3793         {
3794           list = g_file_attribute_info_list_new ();
3795           g_error_free (my_error);
3796         }
3797       else
3798         g_propagate_error (error, my_error);
3799     }
3800   
3801   return list;
3802 }
3803
3804 /**
3805  * g_file_query_writable_namespaces:
3806  * @file: input #GFile.
3807  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3808  * @error: a #GError, or %NULL
3809  * 
3810  * Obtain the list of attribute namespaces where new attributes 
3811  * can be created by a user. An example of this is extended
3812  * attributes (in the "xattr" namespace).
3813  *
3814  * If @cancellable is not %NULL, then the operation can be cancelled by
3815  * triggering the cancellable object from another thread. If the operation
3816  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3817  * 
3818  * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3819  * When you are done with it, release it with g_file_attribute_info_list_unref()
3820  **/
3821 GFileAttributeInfoList *
3822 g_file_query_writable_namespaces (GFile         *file,
3823                                   GCancellable  *cancellable,
3824                                   GError       **error)
3825 {
3826   GFileIface *iface;
3827   GError *my_error;
3828   GFileAttributeInfoList *list;
3829   
3830   g_return_val_if_fail (G_IS_FILE (file), NULL);
3831
3832   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3833     return NULL;
3834   
3835   iface = G_FILE_GET_IFACE (file);
3836
3837   if (iface->query_writable_namespaces == NULL)
3838     return g_file_attribute_info_list_new ();
3839
3840   my_error = NULL;
3841   list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3842   
3843   if (list == NULL)
3844     {
3845       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3846         {
3847           list = g_file_attribute_info_list_new ();
3848           g_error_free (my_error);
3849         }
3850       else
3851         g_propagate_error (error, my_error);
3852     }
3853   
3854   return list;
3855 }
3856
3857 /**
3858  * g_file_set_attribute:
3859  * @file: input #GFile.
3860  * @attribute: a string containing the attribute's name.
3861  * @type: The type of the attribute
3862  * @value_p: (allow-none): a pointer to the value (or the pointer itself if the type is a pointer type)
3863  * @flags: a set of #GFileQueryInfoFlags.
3864  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3865  * @error: a #GError, or %NULL
3866  * 
3867  * Sets an attribute in the file with attribute name @attribute to @value.
3868  *
3869  * Some attributes can be unset by setting @attribute to
3870  * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
3871  * 
3872  * If @cancellable is not %NULL, then the operation can be cancelled by
3873  * triggering the cancellable object from another thread. If the operation
3874  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3875  * 
3876  * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3877  **/
3878 gboolean
3879 g_file_set_attribute (GFile                      *file,
3880                       const char                 *attribute,
3881                       GFileAttributeType          type,
3882                       gpointer                    value_p,
3883                       GFileQueryInfoFlags         flags,
3884                       GCancellable               *cancellable,
3885                       GError                    **error)
3886 {
3887   GFileIface *iface;
3888   
3889   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3890   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3891
3892   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3893     return FALSE;
3894   
3895   iface = G_FILE_GET_IFACE (file);
3896
3897   if (iface->set_attribute == NULL)
3898     {
3899       g_set_error_literal (error, G_IO_ERROR,
3900                            G_IO_ERROR_NOT_SUPPORTED,
3901                            _("Operation not supported"));
3902       return FALSE;
3903     }
3904
3905   return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3906 }
3907
3908 /**
3909  * g_file_set_attributes_from_info:
3910  * @file: input #GFile.
3911  * @info: a #GFileInfo.
3912  * @flags: #GFileQueryInfoFlags
3913  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3914  * @error: a #GError, or %NULL 
3915  * 
3916  * Tries to set all attributes in the #GFileInfo on the target values, 
3917  * not stopping on the first error.
3918  * 
3919  * If there is any error during this operation then @error will be set to
3920  * the first error. Error on particular fields are flagged by setting 
3921  * the "status" field in the attribute value to 
3922  * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3923  * further errors.
3924  *
3925  * If @cancellable is not %NULL, then the operation can be cancelled by
3926  * triggering the cancellable object from another thread. If the operation
3927  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3928  * 
3929  * Returns: %TRUE if there was any error, %FALSE otherwise.
3930  **/
3931 gboolean
3932 g_file_set_attributes_from_info (GFile                *file,
3933                                  GFileInfo            *info,
3934                                  GFileQueryInfoFlags   flags,
3935                                  GCancellable         *cancellable,
3936                                  GError              **error)
3937 {
3938   GFileIface *iface;
3939
3940   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3941   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3942
3943   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3944     return FALSE;
3945   
3946   g_file_info_clear_status (info);
3947   
3948   iface = G_FILE_GET_IFACE (file);
3949
3950   return (* iface->set_attributes_from_info) (file, 
3951                                               info, 
3952                                               flags, 
3953                                               cancellable, 
3954                                               error);
3955 }
3956
3957
3958 static gboolean
3959 g_file_real_set_attributes_from_info (GFile                *file,
3960                                       GFileInfo            *info,
3961                                       GFileQueryInfoFlags   flags,
3962                                       GCancellable         *cancellable,
3963                                       GError              **error)
3964 {
3965   char **attributes;
3966   int i;
3967   gboolean res;
3968   GFileAttributeValue *value;
3969   
3970   res = TRUE;
3971   
3972   attributes = g_file_info_list_attributes (info, NULL);
3973
3974   for (i = 0; attributes[i] != NULL; i++)
3975     {
3976       value = _g_file_info_get_attribute_value (info, attributes[i]);
3977
3978       if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3979         continue;
3980
3981       if (!g_file_set_attribute (file, attributes[i],
3982                                  value->type, _g_file_attribute_value_peek_as_pointer (value),
3983                                  flags, cancellable, error))
3984         {
3985           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3986           res = FALSE;
3987           /* Don't set error multiple times */
3988           error = NULL;
3989         }
3990       else
3991         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3992     }
3993   
3994   g_strfreev (attributes);
3995   
3996   return res;
3997 }
3998
3999 /**
4000  * g_file_set_attributes_async:
4001  * @file: input #GFile.
4002  * @info: a #GFileInfo.
4003  * @flags: a #GFileQueryInfoFlags.
4004  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
4005  *     of the request. 
4006  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4007  * @callback: (scope async): a #GAsyncReadyCallback. 
4008  * @user_data: (closure): a #gpointer.
4009  *
4010  * Asynchronously sets the attributes of @file with @info.
4011  * 
4012  * For more details, see g_file_set_attributes_from_info() which is
4013  * the synchronous version of this call.
4014  *
4015  * When the operation is finished, @callback will be called. You can then call
4016  * g_file_set_attributes_finish() to get the result of the operation.
4017  **/
4018 void
4019 g_file_set_attributes_async (GFile               *file,
4020                              GFileInfo           *info,
4021                              GFileQueryInfoFlags  flags,
4022                              int                  io_priority,
4023                              GCancellable        *cancellable,
4024                              GAsyncReadyCallback  callback,
4025                              gpointer             user_data)
4026 {
4027   GFileIface *iface;
4028   
4029   g_return_if_fail (G_IS_FILE (file));
4030   g_return_if_fail (G_IS_FILE_INFO (info));
4031
4032   iface = G_FILE_GET_IFACE (file);
4033   (* iface->set_attributes_async) (file, 
4034                                    info, 
4035                                    flags, 
4036                                    io_priority, 
4037                                    cancellable, 
4038                                    callback, 
4039                                    user_data);
4040 }
4041
4042 /**
4043  * g_file_set_attributes_finish:
4044  * @file: input #GFile.
4045  * @result: a #GAsyncResult.
4046  * @info: (out) (transfer full): a #GFileInfo.
4047  * @error: a #GError, or %NULL
4048  * 
4049  * Finishes setting an attribute started in g_file_set_attributes_async().
4050  * 
4051  * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4052  **/
4053 gboolean
4054 g_file_set_attributes_finish (GFile         *file,
4055                               GAsyncResult  *result,
4056                               GFileInfo    **info,
4057                               GError       **error)
4058 {
4059   GFileIface *iface;
4060   
4061   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4062   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4063
4064   /* No standard handling of errors here, as we must set info even
4065    * on errors 
4066    */
4067   iface = G_FILE_GET_IFACE (file);
4068   return (* iface->set_attributes_finish) (file, result, info, error);
4069 }
4070
4071 /**
4072  * g_file_set_attribute_string:
4073  * @file: input #GFile.
4074  * @attribute: a string containing the attribute's name.
4075  * @value: a string containing the attribute's value.
4076  * @flags: #GFileQueryInfoFlags.
4077  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4078  * @error: a #GError, or %NULL
4079  * 
4080  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value. 
4081  * If @attribute is of a different type, this operation will fail.
4082  * 
4083  * If @cancellable is not %NULL, then the operation can be cancelled by
4084  * triggering the cancellable object from another thread. If the operation
4085  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4086  * 
4087  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4088  **/
4089 gboolean
4090 g_file_set_attribute_string (GFile                *file,
4091                              const char           *attribute,
4092                              const char           *value,
4093                              GFileQueryInfoFlags   flags,
4094                              GCancellable         *cancellable,
4095                              GError              **error)
4096 {
4097   return g_file_set_attribute (file, attribute,
4098                                G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4099                                flags, cancellable, error);
4100 }
4101
4102 /**
4103  * g_file_set_attribute_byte_string:
4104  * @file: input #GFile.
4105  * @attribute: a string containing the attribute's name.
4106  * @value: a string containing the attribute's new value.
4107  * @flags: a #GFileQueryInfoFlags.
4108  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4109  * @error: a #GError, or %NULL
4110  * 
4111  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value. 
4112  * If @attribute is of a different type, this operation will fail, 
4113  * returning %FALSE. 
4114  * 
4115  * If @cancellable is not %NULL, then the operation can be cancelled by
4116  * triggering the cancellable object from another thread. If the operation
4117  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4118  * 
4119  * Returns: %TRUE if the @attribute was successfully set to @value 
4120  * in the @file, %FALSE otherwise.
4121  **/
4122 gboolean
4123 g_file_set_attribute_byte_string  (GFile                *file,
4124                                    const char           *attribute,
4125                                    const char           *value,
4126                                    GFileQueryInfoFlags   flags,
4127                                    GCancellable         *cancellable,
4128                                    GError              **error)
4129 {
4130   return g_file_set_attribute (file, attribute,
4131                                G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4132                                flags, cancellable, error);
4133 }
4134
4135 /**
4136  * g_file_set_attribute_uint32:
4137  * @file: input #GFile.
4138  * @attribute: a string containing the attribute's name.
4139  * @value: a #guint32 containing the attribute's new value.
4140  * @flags: a #GFileQueryInfoFlags.
4141  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4142  * @error: a #GError, or %NULL
4143  * 
4144  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value. 
4145  * If @attribute is of a different type, this operation will fail.
4146  * 
4147  * If @cancellable is not %NULL, then the operation can be cancelled by
4148  * triggering the cancellable object from another thread. If the operation
4149  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4150  * 
4151  * Returns: %TRUE if the @attribute was successfully set to @value 
4152  * in the @file, %FALSE otherwise.
4153  **/
4154 gboolean
4155 g_file_set_attribute_uint32 (GFile                *file,
4156                              const char           *attribute,
4157                              guint32               value,
4158                              GFileQueryInfoFlags   flags,
4159                              GCancellable         *cancellable,
4160                              GError              **error)
4161 {
4162   return g_file_set_attribute (file, attribute,
4163                                G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4164                                flags, cancellable, error);
4165 }
4166
4167 /**
4168  * g_file_set_attribute_int32:
4169  * @file: input #GFile.
4170  * @attribute: a string containing the attribute's name.
4171  * @value: a #gint32 containing the attribute's new value.
4172  * @flags: a #GFileQueryInfoFlags.
4173  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4174  * @error: a #GError, or %NULL
4175  * 
4176  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value. 
4177  * If @attribute is of a different type, this operation will fail.
4178  * 
4179  * If @cancellable is not %NULL, then the operation can be cancelled by
4180  * triggering the cancellable object from another thread. If the operation
4181  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4182  * 
4183  * Returns: %TRUE if the @attribute was successfully set to @value 
4184  * in the @file, %FALSE otherwise. 
4185  **/
4186 gboolean
4187 g_file_set_attribute_int32 (GFile                *file,
4188                             const char           *attribute,
4189                             gint32                value,
4190                             GFileQueryInfoFlags   flags,
4191                             GCancellable         *cancellable,
4192                             GError              **error)
4193 {
4194   return g_file_set_attribute (file, attribute,
4195                                G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4196                                flags, cancellable, error);
4197 }
4198
4199 /**
4200  * g_file_set_attribute_uint64:
4201  * @file: input #GFile. 
4202  * @attribute: a string containing the attribute's name.
4203  * @value: a #guint64 containing the attribute's new value.
4204  * @flags: a #GFileQueryInfoFlags.
4205  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4206  * @error: a #GError, or %NULL
4207  * 
4208  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value. 
4209  * If @attribute is of a different type, this operation will fail.
4210  * 
4211  * If @cancellable is not %NULL, then the operation can be cancelled by
4212  * triggering the cancellable object from another thread. If the operation
4213  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4214  * 
4215  * Returns: %TRUE if the @attribute was successfully set to @value 
4216  * in the @file, %FALSE otherwise.
4217  **/
4218 gboolean
4219 g_file_set_attribute_uint64 (GFile                *file,
4220                              const char           *attribute,
4221                              guint64               value,
4222                              GFileQueryInfoFlags   flags,
4223                              GCancellable         *cancellable,
4224                              GError              **error)
4225  {
4226   return g_file_set_attribute (file, attribute,
4227                                G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4228                                flags, cancellable, error);
4229 }
4230
4231 /**
4232  * g_file_set_attribute_int64:
4233  * @file: input #GFile.
4234  * @attribute: a string containing the attribute's name.
4235  * @value: a #guint64 containing the attribute's new value.
4236  * @flags: a #GFileQueryInfoFlags.
4237  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4238  * @error: a #GError, or %NULL
4239  * 
4240  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value. 
4241  * If @attribute is of a different type, this operation will fail.
4242  * 
4243  * If @cancellable is not %NULL, then the operation can be cancelled by
4244  * triggering the cancellable object from another thread. If the operation
4245  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4246  * 
4247  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4248  **/
4249 gboolean
4250 g_file_set_attribute_int64 (GFile                *file,
4251                             const char           *attribute,
4252                             gint64                value,
4253                             GFileQueryInfoFlags   flags,
4254                             GCancellable         *cancellable,
4255                             GError              **error)
4256 {
4257   return g_file_set_attribute (file, attribute,
4258                                G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4259                                flags, cancellable, error);
4260 }
4261
4262 /**
4263  * g_file_mount_mountable:
4264  * @file: input #GFile.
4265  * @flags: flags affecting the operation
4266  * @mount_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
4267  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4268  * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4269  * @user_data: (closure): the data to pass to callback function
4270  * 
4271  * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4272  * Using @mount_operation, you can request callbacks when, for instance, 
4273  * passwords are needed during authentication.
4274  *
4275  * If @cancellable is not %NULL, then the operation can be cancelled by
4276  * triggering the cancellable object from another thread. If the operation
4277  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4278  *
4279  * When the operation is finished, @callback will be called. You can then call
4280  * g_file_mount_mountable_finish() to get the result of the operation.
4281  **/
4282 void
4283 g_file_mount_mountable (GFile               *file,
4284                         GMountMountFlags     flags,
4285                         GMountOperation     *mount_operation,
4286                         GCancellable        *cancellable,
4287                         GAsyncReadyCallback  callback,
4288                         gpointer             user_data)
4289 {
4290   GFileIface *iface;
4291
4292   g_return_if_fail (G_IS_FILE (file));
4293
4294   iface = G_FILE_GET_IFACE (file);
4295
4296   if (iface->mount_mountable == NULL) 
4297     {
4298       g_simple_async_report_error_in_idle (G_OBJECT (file),
4299                                            callback,
4300                                            user_data,
4301                                            G_IO_ERROR,
4302                                            G_IO_ERROR_NOT_SUPPORTED,
4303                                            _("Operation not supported"));
4304       return;
4305     }
4306   
4307   (* iface->mount_mountable) (file,
4308                               flags,
4309                               mount_operation,
4310                               cancellable,
4311                               callback,
4312                               user_data);
4313 }
4314
4315 /**
4316  * g_file_mount_mountable_finish:
4317  * @file: input #GFile.
4318  * @result: a #GAsyncResult.
4319  * @error: a #GError, or %NULL
4320  *
4321  * Finishes a mount operation. See g_file_mount_mountable() for details.
4322  * 
4323  * Finish an asynchronous mount operation that was started 
4324  * with g_file_mount_mountable().
4325  *
4326  * Returns: (transfer full): a #GFile or %NULL on error.
4327  *     Free the returned object with g_object_unref().
4328  **/
4329 GFile *
4330 g_file_mount_mountable_finish (GFile         *file,
4331                                GAsyncResult  *result,
4332                                GError       **error)
4333 {
4334   GFileIface *iface;
4335
4336   g_return_val_if_fail (G_IS_FILE (file), NULL);
4337   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4338
4339   if (g_async_result_legacy_propagate_error (result, error))
4340     return NULL;
4341   
4342   iface = G_FILE_GET_IFACE (file);
4343   return (* iface->mount_mountable_finish) (file, result, error);
4344 }
4345
4346 /**
4347  * g_file_unmount_mountable:
4348  * @file: input #GFile.
4349  * @flags: flags affecting the operation
4350  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4351  * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4352  * @user_data: (closure): the data to pass to callback function
4353  *
4354  * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4355  *
4356  * If @cancellable is not %NULL, then the operation can be cancelled by
4357  * triggering the cancellable object from another thread. If the operation
4358  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4359  *
4360  * When the operation is finished, @callback will be called. You can then call
4361  * g_file_unmount_mountable_finish() to get the result of the operation.
4362  *
4363  * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4364  **/
4365 void
4366 g_file_unmount_mountable (GFile               *file,
4367                           GMountUnmountFlags   flags,
4368                           GCancellable        *cancellable,
4369                           GAsyncReadyCallback  callback,
4370                           gpointer             user_data)
4371 {
4372   GFileIface *iface;
4373   
4374   g_return_if_fail (G_IS_FILE (file));
4375
4376   iface = G_FILE_GET_IFACE (file);
4377   
4378   if (iface->unmount_mountable == NULL)
4379     {
4380       g_simple_async_report_error_in_idle (G_OBJECT (file),
4381                                            callback,
4382                                            user_data,
4383                                            G_IO_ERROR,
4384                                            G_IO_ERROR_NOT_SUPPORTED,
4385                                            _("Operation not supported"));
4386       return;
4387     }
4388   
4389   (* iface->unmount_mountable) (file,
4390                                 flags,
4391                                 cancellable,
4392                                 callback,
4393                                 user_data);
4394 }
4395
4396 /**
4397  * g_file_unmount_mountable_finish:
4398  * @file: input #GFile.
4399  * @result: a #GAsyncResult.
4400  * @error: a #GError, or %NULL
4401  *
4402  * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4403  * 
4404  * Finish an asynchronous unmount operation that was started 
4405  * with g_file_unmount_mountable().
4406  *
4407  * Returns: %TRUE if the operation finished successfully. %FALSE
4408  * otherwise.
4409  *
4410  * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish() instead.
4411  **/
4412 gboolean
4413 g_file_unmount_mountable_finish (GFile         *file,
4414                                  GAsyncResult  *result,
4415                                  GError       **error)
4416 {
4417   GFileIface *iface;
4418   
4419   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4420   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4421
4422   if (g_async_result_legacy_propagate_error (result, error))
4423     return FALSE;
4424   
4425   iface = G_FILE_GET_IFACE (file);
4426   return (* iface->unmount_mountable_finish) (file, result, error);
4427 }
4428
4429 /**
4430  * g_file_unmount_mountable_with_operation:
4431  * @file: input #GFile.
4432  * @flags: flags affecting the operation
4433  * @mount_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
4434  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4435  * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4436  * @user_data: (closure): the data to pass to callback function
4437  *
4438  * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4439  *
4440  * If @cancellable is not %NULL, then the operation can be cancelled by
4441  * triggering the cancellable object from another thread. If the operation
4442  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4443  *
4444  * When the operation is finished, @callback will be called. You can then call
4445  * g_file_unmount_mountable_finish() to get the result of the operation.
4446  *
4447  * Since: 2.22
4448  **/
4449 void
4450 g_file_unmount_mountable_with_operation (GFile               *file,
4451                                          GMountUnmountFlags   flags,
4452                                          GMountOperation     *mount_operation,
4453                                          GCancellable        *cancellable,
4454                                          GAsyncReadyCallback  callback,
4455                                          gpointer             user_data)
4456 {
4457   GFileIface *iface;
4458
4459   g_return_if_fail (G_IS_FILE (file));
4460
4461   iface = G_FILE_GET_IFACE (file);
4462
4463   if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4464     {
4465       g_simple_async_report_error_in_idle (G_OBJECT (file),
4466                                            callback,
4467                                            user_data,
4468                                            G_IO_ERROR,
4469                                            G_IO_ERROR_NOT_SUPPORTED,
4470                                            _("Operation not supported"));
4471       return;
4472     }
4473
4474   if (iface->unmount_mountable_with_operation != NULL)
4475     (* iface->unmount_mountable_with_operation) (file,
4476                                                  flags,
4477                                                  mount_operation,
4478                                                  cancellable,
4479                                                  callback,
4480                                                  user_data);
4481   else
4482     (* iface->unmount_mountable) (file,
4483                                   flags,
4484                                   cancellable,
4485                                   callback,
4486                                   user_data);
4487 }
4488
4489 /**
4490  * g_file_unmount_mountable_with_operation_finish:
4491  * @file: input #GFile.
4492  * @result: a #GAsyncResult.
4493  * @error: a #GError, or %NULL
4494  *
4495  * Finishes an unmount operation, see g_file_unmount_mountable_with_operation() for details.
4496  *
4497  * Finish an asynchronous unmount operation that was started
4498  * with g_file_unmount_mountable_with_operation().
4499  *
4500  * Returns: %TRUE if the operation finished successfully. %FALSE
4501  * otherwise.
4502  *
4503  * Since: 2.22
4504  **/
4505 gboolean
4506 g_file_unmount_mountable_with_operation_finish (GFile         *file,
4507                                                 GAsyncResult  *result,
4508                                                 GError       **error)
4509 {
4510   GFileIface *iface;
4511
4512   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4513   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4514
4515   if (g_async_result_legacy_propagate_error (result, error))
4516     return FALSE;
4517
4518   iface = G_FILE_GET_IFACE (file);
4519   if (iface->unmount_mountable_with_operation_finish != NULL)
4520     return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4521   else
4522     return (* iface->unmount_mountable_finish) (file, result, error);
4523 }
4524
4525 /**
4526  * g_file_eject_mountable:
4527  * @file: input #GFile.
4528  * @flags: flags affecting the operation
4529  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4530  * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4531  * @user_data: (closure): the data to pass to callback function
4532  * 
4533  * Starts an asynchronous eject on a mountable.  
4534  * When this operation has completed, @callback will be called with
4535  * @user_user data, and the operation can be finalized with 
4536  * g_file_eject_mountable_finish().
4537  * 
4538  * If @cancellable is not %NULL, then the operation can be cancelled by
4539  * triggering the cancellable object from another thread. If the operation
4540  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4541  *
4542  * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4543  **/
4544 void
4545 g_file_eject_mountable (GFile               *file,
4546                         GMountUnmountFlags   flags,
4547                         GCancellable        *cancellable,
4548                         GAsyncReadyCallback  callback,
4549                         gpointer             user_data)
4550 {
4551   GFileIface *iface;
4552
4553   g_return_if_fail (G_IS_FILE (file));
4554
4555   iface = G_FILE_GET_IFACE (file);
4556   
4557   if (iface->eject_mountable == NULL) 
4558     {
4559       g_simple_async_report_error_in_idle (G_OBJECT (file),
4560                                            callback,
4561                                            user_data,
4562                                            G_IO_ERROR,
4563                                            G_IO_ERROR_NOT_SUPPORTED,
4564                                            _("Operation not supported"));
4565       return;
4566     }
4567   
4568   (* iface->eject_mountable) (file,
4569                               flags,
4570                               cancellable,
4571                               callback,
4572                               user_data);
4573 }
4574
4575 /**
4576  * g_file_eject_mountable_finish:
4577  * @file: input #GFile.
4578  * @result: a #GAsyncResult.
4579  * @error: a #GError, or %NULL
4580  * 
4581  * Finishes an asynchronous eject operation started by 
4582  * g_file_eject_mountable().
4583  * 
4584  * Returns: %TRUE if the @file was ejected successfully. %FALSE 
4585  * otherwise.
4586  *
4587  * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish() instead.
4588  **/
4589 gboolean
4590 g_file_eject_mountable_finish (GFile         *file,
4591                                GAsyncResult  *result,
4592                                GError       **error)
4593 {
4594   GFileIface *iface;
4595   
4596   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4597   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4598
4599   if (g_async_result_legacy_propagate_error (result, error))
4600     return FALSE;
4601
4602   iface = G_FILE_GET_IFACE (file);
4603   return (* iface->eject_mountable_finish) (file, result, error);
4604 }
4605
4606 /**
4607  * g_file_eject_mountable_with_operation:
4608  * @file: input #GFile.
4609  * @flags: flags affecting the operation
4610  * @mount_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
4611  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4612  * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4613  * @user_data: (closure): the data to pass to callback function
4614  *
4615  * Starts an asynchronous eject on a mountable.
4616  * When this operation has completed, @callback will be called with
4617  * @user_user data, and the operation can be finalized with
4618  * g_file_eject_mountable_with_operation_finish().
4619  *
4620  * If @cancellable is not %NULL, then the operation can be cancelled by
4621  * triggering the cancellable object from another thread. If the operation
4622  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4623  *
4624  * Since: 2.22
4625  **/
4626 void
4627 g_file_eject_mountable_with_operation (GFile               *file,
4628                                        GMountUnmountFlags   flags,
4629                                        GMountOperation     *mount_operation,
4630                                        GCancellable        *cancellable,
4631                                        GAsyncReadyCallback  callback,
4632                                        gpointer             user_data)
4633 {
4634   GFileIface *iface;
4635
4636   g_return_if_fail (G_IS_FILE (file));
4637
4638   iface = G_FILE_GET_IFACE (file);
4639
4640   if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4641     {
4642       g_simple_async_report_error_in_idle (G_OBJECT (file),
4643                                            callback,
4644                                            user_data,
4645                                            G_IO_ERROR,
4646                                            G_IO_ERROR_NOT_SUPPORTED,
4647                                            _("Operation not supported"));
4648       return;
4649     }
4650
4651   if (iface->eject_mountable_with_operation != NULL)
4652     (* iface->eject_mountable_with_operation) (file,
4653                                                flags,
4654                                                mount_operation,
4655                                                cancellable,
4656                                                callback,
4657                                                user_data);
4658   else
4659     (* iface->eject_mountable) (file,
4660                                 flags,
4661                                 cancellable,
4662                                 callback,
4663                                 user_data);
4664 }
4665
4666 /**
4667  * g_file_eject_mountable_with_operation_finish:
4668  * @file: input #GFile.
4669  * @result: a #GAsyncResult.
4670  * @error: a #GError, or %NULL
4671  *
4672  * Finishes an asynchronous eject operation started by
4673  * g_file_eject_mountable_with_operation().
4674  *
4675  * Returns: %TRUE if the @file was ejected successfully. %FALSE
4676  * otherwise.
4677  *
4678  * Since: 2.22
4679  **/
4680 gboolean
4681 g_file_eject_mountable_with_operation_finish (GFile         *file,
4682                                               GAsyncResult  *result,
4683                                               GError       **error)
4684 {
4685   GFileIface *iface;
4686
4687   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4688   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4689
4690   if (g_async_result_legacy_propagate_error (result, error))
4691     return FALSE;
4692
4693   iface = G_FILE_GET_IFACE (file);
4694   if (iface->eject_mountable_with_operation_finish != NULL)
4695     return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4696   else
4697     return (* iface->eject_mountable_finish) (file, result, error);
4698 }
4699
4700 /**
4701  * g_file_monitor_directory:
4702  * @file: input #GFile.
4703  * @flags: a set of #GFileMonitorFlags.
4704  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4705  * @error: a #GError, or %NULL.
4706  * 
4707  * Obtains a directory monitor for the given file.
4708  * This may fail if directory monitoring is not supported.
4709  *
4710  * If @cancellable is not %NULL, then the operation can be cancelled by
4711  * triggering the cancellable object from another thread. If the operation
4712  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4713  *
4714  * Virtual: monitor_dir
4715  * Returns: (transfer full): a #GFileMonitor for the given @file, or %NULL on error.
4716  *     Free the returned object with g_object_unref().
4717  **/
4718 GFileMonitor*
4719 g_file_monitor_directory (GFile             *file,
4720                           GFileMonitorFlags  flags,
4721                           GCancellable      *cancellable,
4722                           GError           **error)
4723 {
4724   GFileIface *iface;
4725
4726   g_return_val_if_fail (G_IS_FILE (file), NULL);
4727
4728   if (g_cancellable_set_error_if_cancelled (cancellable, error))
4729     return NULL;
4730
4731   iface = G_FILE_GET_IFACE (file);
4732
4733   if (iface->monitor_dir == NULL)
4734     {
4735       g_set_error_literal (error, G_IO_ERROR,
4736                            G_IO_ERROR_NOT_SUPPORTED,
4737                            _("Operation not supported"));
4738       return NULL;
4739     }
4740
4741   return (* iface->monitor_dir) (file, flags, cancellable, error);
4742 }
4743
4744 /**
4745  * g_file_monitor_file:
4746  * @file: input #GFile.
4747  * @flags: a set of #GFileMonitorFlags.
4748  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4749  * @error: a #GError, or %NULL.
4750  * 
4751  * Obtains a file monitor for the given file. If no file notification
4752  * mechanism exists, then regular polling of the file is used.
4753  *
4754  * If @cancellable is not %NULL, then the operation can be cancelled by
4755  * triggering the cancellable object from another thread. If the operation
4756  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4757  * 
4758  * Returns: (transfer full): a #GFileMonitor for the given @file, or %NULL on error.
4759  *     Free the returned object with g_object_unref().
4760  **/
4761 GFileMonitor*
4762 g_file_monitor_file (GFile             *file,
4763                      GFileMonitorFlags  flags,
4764                      GCancellable      *cancellable,
4765                      GError           **error)
4766 {
4767   GFileIface *iface;
4768   GFileMonitor *monitor;
4769   
4770   g_return_val_if_fail (G_IS_FILE (file), NULL);
4771
4772   if (g_cancellable_set_error_if_cancelled (cancellable, error))
4773     return NULL;
4774
4775   iface = G_FILE_GET_IFACE (file);
4776
4777   monitor = NULL;
4778   
4779   if (iface->monitor_file)
4780     monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4781
4782 /* Fallback to polling */
4783   if (monitor == NULL)
4784     monitor = _g_poll_file_monitor_new (file);
4785
4786   return monitor;
4787 }
4788
4789 /**
4790  * g_file_monitor:
4791  * @file: input #GFile
4792  * @flags: a set of #GFileMonitorFlags
4793  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
4794  * @error: a #GError, or %NULL
4795  * 
4796  * Obtains a file or directory monitor for the given file, depending
4797  * on the type of the file.
4798  *
4799  * If @cancellable is not %NULL, then the operation can be cancelled by
4800  * triggering the cancellable object from another thread. If the operation
4801  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4802  * 
4803  * Returns: (transfer full): a #GFileMonitor for the given @file, or %NULL on error.
4804  *     Free the returned object with g_object_unref().
4805  *
4806  * Since: 2.18
4807  */
4808 GFileMonitor*
4809 g_file_monitor (GFile             *file,
4810                 GFileMonitorFlags  flags,
4811                 GCancellable      *cancellable,
4812                 GError           **error)
4813 {
4814   if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4815     return g_file_monitor_directory (file, flags, cancellable, error);
4816   else
4817     return g_file_monitor_file (file, flags, cancellable, error);
4818 }
4819
4820 /********************************************
4821  *   Default implementation of async ops    *
4822  ********************************************/
4823
4824 typedef struct {
4825   char *attributes;
4826   GFileQueryInfoFlags flags;
4827   GFileInfo *info;
4828 } QueryInfoAsyncData;
4829
4830 static void
4831 query_info_data_free (QueryInfoAsyncData *data)
4832 {
4833   if (data->info)
4834     g_object_unref (data->info);
4835   g_free (data->attributes);
4836   g_free (data);
4837 }
4838
4839 static void
4840 query_info_async_thread (GSimpleAsyncResult *res,
4841                          GObject            *object,
4842                          GCancellable       *cancellable)
4843 {
4844   GError *error = NULL;
4845   QueryInfoAsyncData *data;
4846   GFileInfo *info;
4847   
4848   data = g_simple_async_result_get_op_res_gpointer (res);
4849   
4850   info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4851
4852   if (info == NULL)
4853     g_simple_async_result_take_error (res, error);
4854   else
4855     data->info = info;
4856 }
4857
4858 static void
4859 g_file_real_query_info_async (GFile               *file,
4860                               const char          *attributes,
4861                               GFileQueryInfoFlags  flags,
4862                               int                  io_priority,
4863                               GCancellable        *cancellable,
4864                               GAsyncReadyCallback  callback,
4865                               gpointer             user_data)
4866 {
4867   GSimpleAsyncResult *res;
4868   QueryInfoAsyncData *data;
4869
4870   data = g_new0 (QueryInfoAsyncData, 1);
4871   data->attributes = g_strdup (attributes);
4872   data->flags = flags;
4873   
4874   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
4875   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
4876   
4877   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
4878   g_object_unref (res);
4879 }
4880
4881 static GFileInfo *
4882 g_file_real_query_info_finish (GFile         *file,
4883                                GAsyncResult  *res,
4884                                GError       **error)
4885 {
4886   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4887   QueryInfoAsyncData *data;
4888
4889   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4890
4891   if (g_simple_async_result_propagate_error (simple, error))
4892     return NULL;
4893
4894   data = g_simple_async_result_get_op_res_gpointer (simple);
4895   if (data->info)
4896     return g_object_ref (data->info);
4897   
4898   return NULL;
4899 }
4900
4901 typedef struct {
4902   char *attributes;
4903   GFileInfo *info;
4904 } QueryFilesystemInfoAsyncData;
4905
4906 static void
4907 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4908 {
4909   if (data->info)
4910     g_object_unref (data->info);
4911   g_free (data->attributes);
4912   g_free (data);
4913 }
4914
4915 static void
4916 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4917                                     GObject            *object,
4918                                     GCancellable       *cancellable)
4919 {
4920   GError *error = NULL;
4921   QueryFilesystemInfoAsyncData *data;
4922   GFileInfo *info;
4923   
4924   data = g_simple_async_result_get_op_res_gpointer (res);
4925   
4926   info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4927
4928   if (info == NULL)
4929     g_simple_async_result_take_error (res, error);
4930   else
4931     data->info = info;
4932 }
4933
4934 static void
4935 g_file_real_query_filesystem_info_async (GFile               *file,
4936                                          const char          *attributes,
4937                                          int                  io_priority,
4938                                          GCancellable        *cancellable,
4939                                          GAsyncReadyCallback  callback,
4940                                          gpointer             user_data)
4941 {
4942   GSimpleAsyncResult *res;
4943   QueryFilesystemInfoAsyncData *data;
4944
4945   data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4946   data->attributes = g_strdup (attributes);
4947   
4948   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4949   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4950   
4951   g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4952   g_object_unref (res);
4953 }
4954
4955 static GFileInfo *
4956 g_file_real_query_filesystem_info_finish (GFile         *file,
4957                                           GAsyncResult  *res,
4958                                           GError       **error)
4959 {
4960   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4961   QueryFilesystemInfoAsyncData *data;
4962
4963   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4964
4965   if (g_simple_async_result_propagate_error (simple, error))
4966     return NULL;
4967
4968   data = g_simple_async_result_get_op_res_gpointer (simple);
4969   if (data->info)
4970     return g_object_ref (data->info);
4971   
4972   return NULL;
4973 }
4974
4975 typedef struct {
4976   char *attributes;
4977   GFileQueryInfoFlags flags;
4978   GFileEnumerator *enumerator;
4979 } EnumerateChildrenAsyncData;
4980
4981 static void
4982 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4983 {
4984   if (data->enumerator)
4985     g_object_unref (data->enumerator);
4986   g_free (data->attributes);
4987   g_free (data);
4988 }
4989
4990 static void
4991 enumerate_children_async_thread (GSimpleAsyncResult *res,
4992                                  GObject            *object,
4993                                  GCancellable       *cancellable)
4994 {
4995   GError *error = NULL;
4996   EnumerateChildrenAsyncData *data;
4997   GFileEnumerator *enumerator;
4998   
4999   data = g_simple_async_result_get_op_res_gpointer (res);
5000   
5001   enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5002
5003   if (enumerator == NULL)
5004     g_simple_async_result_take_error (res, error);
5005   else
5006     data->enumerator = enumerator;
5007 }
5008
5009 static void
5010 g_file_real_enumerate_children_async (GFile               *file,
5011                                       const char          *attributes,
5012                                       GFileQueryInfoFlags  flags,
5013                                       int                  io_priority,
5014                                       GCancellable        *cancellable,
5015                                       GAsyncReadyCallback  callback,
5016                                       gpointer             user_data)
5017 {
5018   GSimpleAsyncResult *res;
5019   EnumerateChildrenAsyncData *data;
5020
5021   data = g_new0 (EnumerateChildrenAsyncData, 1);
5022   data->attributes = g_strdup (attributes);
5023   data->flags = flags;
5024   
5025   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
5026   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
5027   
5028   g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
5029   g_object_unref (res);
5030 }
5031
5032 static GFileEnumerator *
5033 g_file_real_enumerate_children_finish (GFile         *file,
5034                                        GAsyncResult  *res,
5035                                        GError       **error)
5036 {
5037   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5038   EnumerateChildrenAsyncData *data;
5039
5040   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
5041
5042   if (g_simple_async_result_propagate_error (simple, error))
5043     return NULL;
5044
5045   data = g_simple_async_result_get_op_res_gpointer (simple);
5046   if (data->enumerator)
5047     return g_object_ref (data->enumerator);
5048   
5049   return NULL;
5050 }
5051
5052 static void
5053 open_read_async_thread (GSimpleAsyncResult *res,
5054                         GObject            *object,
5055                         GCancellable       *cancellable)
5056 {
5057   GFileIface *iface;
5058   GFileInputStream *stream;
5059   GError *error = NULL;
5060
5061   iface = G_FILE_GET_IFACE (object);
5062
5063   if (iface->read_fn == NULL)
5064     {
5065       g_set_error_literal (&error, G_IO_ERROR,
5066                            G_IO_ERROR_NOT_SUPPORTED,
5067                            _("Operation not supported"));
5068
5069       g_simple_async_result_take_error (res, error);
5070
5071       return;
5072     }
5073   
5074   stream = iface->read_fn (G_FILE (object), cancellable, &error);
5075
5076   if (stream == NULL)
5077     g_simple_async_result_take_error (res, error);
5078   else
5079     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5080 }
5081
5082 static void
5083 g_file_real_read_async (GFile               *file,
5084                         int                  io_priority,
5085                         GCancellable        *cancellable,
5086                         GAsyncReadyCallback  callback,
5087                         gpointer             user_data)
5088 {
5089   GSimpleAsyncResult *res;
5090   
5091   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
5092   
5093   g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
5094   g_object_unref (res);
5095 }
5096
5097 static GFileInputStream *
5098 g_file_real_read_finish (GFile         *file,
5099                          GAsyncResult  *res,
5100                          GError       **error)
5101 {
5102   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5103   gpointer op;
5104
5105   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
5106
5107   if (g_simple_async_result_propagate_error (simple, error))
5108     return NULL;
5109
5110   op = g_simple_async_result_get_op_res_gpointer (simple);
5111   if (op)
5112     return g_object_ref (op);
5113   
5114   return NULL;
5115 }
5116
5117 static void
5118 append_to_async_thread (GSimpleAsyncResult *res,
5119                         GObject            *object,
5120                         GCancellable       *cancellable)
5121 {
5122   GFileIface *iface;
5123   GFileCreateFlags *data;
5124   GFileOutputStream *stream;
5125   GError *error = NULL;
5126
5127   iface = G_FILE_GET_IFACE (object);
5128
5129   data = g_simple_async_result_get_op_res_gpointer (res);
5130
5131   stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
5132
5133   if (stream == NULL)
5134     g_simple_async_result_take_error (res, error);
5135   else
5136     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5137 }
5138
5139 static void
5140 g_file_real_append_to_async (GFile               *file,
5141                              GFileCreateFlags     flags,
5142                              int                  io_priority,
5143                              GCancellable        *cancellable,
5144                              GAsyncReadyCallback  callback,
5145                              gpointer             user_data)
5146 {
5147   GFileCreateFlags *data;
5148   GSimpleAsyncResult *res;
5149
5150   data = g_new0 (GFileCreateFlags, 1);
5151   *data = flags;
5152
5153   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
5154   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5155
5156   g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
5157   g_object_unref (res);
5158 }
5159
5160 static GFileOutputStream *
5161 g_file_real_append_to_finish (GFile         *file,
5162                               GAsyncResult  *res,
5163                               GError       **error)
5164 {
5165   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5166   gpointer op;
5167
5168   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
5169
5170   if (g_simple_async_result_propagate_error (simple, error))
5171     return NULL;
5172
5173   op = g_simple_async_result_get_op_res_gpointer (simple);
5174   if (op)
5175     return g_object_ref (op);
5176   
5177   return NULL;
5178 }
5179
5180 static void
5181 create_async_thread (GSimpleAsyncResult *res,
5182                      GObject            *object,
5183                      GCancellable       *cancellable)
5184 {
5185   GFileIface *iface;
5186   GFileCreateFlags *data;
5187   GFileOutputStream *stream;
5188   GError *error = NULL;
5189
5190   iface = G_FILE_GET_IFACE (object);
5191
5192   data = g_simple_async_result_get_op_res_gpointer (res);
5193
5194   stream = iface->create (G_FILE (object), *data, cancellable, &error);
5195
5196   if (stream == NULL)
5197     g_simple_async_result_take_error (res, error);
5198   else
5199     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5200 }
5201
5202 static void
5203 g_file_real_create_async (GFile               *file,
5204                           GFileCreateFlags     flags,
5205                           int                  io_priority,
5206                           GCancellable        *cancellable,
5207                           GAsyncReadyCallback  callback,
5208                           gpointer             user_data)
5209 {
5210   GFileCreateFlags *data;
5211   GSimpleAsyncResult *res;
5212
5213   data = g_new0 (GFileCreateFlags, 1);
5214   *data = flags;
5215
5216   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
5217   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5218
5219   g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
5220   g_object_unref (res);
5221 }
5222
5223 static GFileOutputStream *
5224 g_file_real_create_finish (GFile         *file,
5225                            GAsyncResult  *res,
5226                            GError       **error)
5227 {
5228   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5229   gpointer op;
5230
5231   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
5232
5233   if (g_simple_async_result_propagate_error (simple, error))
5234     return NULL;
5235
5236   op = g_simple_async_result_get_op_res_gpointer (simple);
5237   if (op)
5238     return g_object_ref (op);
5239   
5240   return NULL;
5241 }
5242
5243 typedef struct {
5244   GFileOutputStream *stream;
5245   char *etag;
5246   gboolean make_backup;
5247   GFileCreateFlags flags;
5248 } ReplaceAsyncData;
5249
5250 static void
5251 replace_async_data_free (ReplaceAsyncData *data)
5252 {
5253   if (data->stream)
5254     g_object_unref (data->stream);
5255   g_free (data->etag);
5256   g_free (data);
5257 }
5258
5259 static void
5260 replace_async_thread (GSimpleAsyncResult *res,
5261                       GObject            *object,
5262                       GCancellable       *cancellable)
5263 {
5264   GFileIface *iface;
5265   GFileOutputStream *stream;
5266   GError *error = NULL;
5267   ReplaceAsyncData *data;
5268
5269   iface = G_FILE_GET_IFACE (object);
5270   
5271   data = g_simple_async_result_get_op_res_gpointer (res);
5272
5273   stream = iface->replace (G_FILE (object),
5274                            data->etag,
5275                            data->make_backup,
5276                            data->flags,
5277                            cancellable,
5278                            &error);
5279
5280   if (stream == NULL)
5281     g_simple_async_result_take_error (res, error);
5282   else
5283     data->stream = stream;
5284 }
5285
5286 static void
5287 g_file_real_replace_async (GFile               *file,
5288                            const char          *etag,
5289                            gboolean             make_backup,
5290                            GFileCreateFlags     flags,
5291                            int                  io_priority,
5292                            GCancellable        *cancellable,
5293                            GAsyncReadyCallback  callback,
5294                            gpointer             user_data)
5295 {
5296   GSimpleAsyncResult *res;
5297   ReplaceAsyncData *data;
5298
5299   data = g_new0 (ReplaceAsyncData, 1);
5300   data->etag = g_strdup (etag);
5301   data->make_backup = make_backup;
5302   data->flags = flags;
5303
5304   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
5305   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
5306
5307   g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
5308   g_object_unref (res);
5309 }
5310
5311 static GFileOutputStream *
5312 g_file_real_replace_finish (GFile         *file,
5313                             GAsyncResult  *res,
5314                             GError       **error)
5315 {
5316   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5317   ReplaceAsyncData *data;
5318
5319   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
5320
5321   if (g_simple_async_result_propagate_error (simple, error))
5322     return NULL;
5323
5324   data = g_simple_async_result_get_op_res_gpointer (simple);
5325   if (data->stream)
5326     return g_object_ref (data->stream);
5327   
5328   return NULL;
5329 }
5330
5331 static void
5332 delete_async_thread (GSimpleAsyncResult *res,
5333                      GObject            *object,
5334                      GCancellable       *cancellable)
5335 {
5336   GFileIface *iface;
5337   GError *error = NULL;
5338
5339   iface = G_FILE_GET_IFACE (object);
5340
5341   if (!iface->delete_file (G_FILE (object),
5342                            cancellable,
5343                            &error))
5344     g_simple_async_result_take_error (res, error);
5345 }
5346
5347 static void
5348 g_file_real_delete_async (GFile               *file,
5349                           int                  io_priority,
5350                           GCancellable        *cancellable,
5351                           GAsyncReadyCallback  callback,
5352                           gpointer             user_data)
5353 {
5354   GSimpleAsyncResult *res;
5355
5356   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_delete_async);
5357   g_simple_async_result_run_in_thread (res, delete_async_thread, io_priority, cancellable);
5358   g_object_unref (res);
5359 }
5360
5361 static gboolean
5362 g_file_real_delete_finish (GFile         *file,
5363                            GAsyncResult  *res,
5364                            GError       **error)
5365 {
5366   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5367
5368   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_delete_async);
5369
5370   if (g_simple_async_result_propagate_error (simple, error))
5371     return FALSE;
5372
5373   return TRUE;
5374 }
5375
5376 static void
5377 open_readwrite_async_thread (GSimpleAsyncResult *res,
5378                              GObject            *object,
5379                              GCancellable       *cancellable)
5380 {
5381   GFileIface *iface;
5382   GFileIOStream *stream;
5383   GError *error = NULL;
5384
5385   iface = G_FILE_GET_IFACE (object);
5386
5387   if (iface->open_readwrite == NULL)
5388     {
5389       g_set_error_literal (&error, G_IO_ERROR,
5390                            G_IO_ERROR_NOT_SUPPORTED,
5391                            _("Operation not supported"));
5392
5393       g_simple_async_result_take_error (res, error);
5394
5395       return;
5396     }
5397
5398   stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5399
5400   if (stream == NULL)
5401     g_simple_async_result_take_error (res, error);
5402   else
5403     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5404 }
5405
5406 static void
5407 g_file_real_open_readwrite_async (GFile               *file,
5408                                   int                  io_priority,
5409                                   GCancellable        *cancellable,
5410                                   GAsyncReadyCallback  callback,
5411                                   gpointer             user_data)
5412 {
5413   GSimpleAsyncResult *res;
5414
5415   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_open_readwrite_async);
5416
5417   g_simple_async_result_run_in_thread (res, open_readwrite_async_thread, io_priority, cancellable);
5418   g_object_unref (res);
5419 }
5420
5421 static GFileIOStream *
5422 g_file_real_open_readwrite_finish (GFile         *file,
5423                                    GAsyncResult  *res,
5424                                    GError       **error)
5425 {
5426   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5427   gpointer op;
5428
5429   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_open_readwrite_async);
5430
5431   if (g_simple_async_result_propagate_error (simple, error))
5432     return NULL;
5433
5434   op = g_simple_async_result_get_op_res_gpointer (simple);
5435   if (op)
5436     return g_object_ref (op);
5437
5438   return NULL;
5439 }
5440
5441 static void
5442 create_readwrite_async_thread (GSimpleAsyncResult *res,
5443                                GObject            *object,
5444                                GCancellable       *cancellable)
5445 {
5446   GFileIface *iface;
5447   GFileCreateFlags *data;
5448   GFileIOStream *stream;
5449   GError *error = NULL;
5450
5451   iface = G_FILE_GET_IFACE (object);
5452
5453   data = g_simple_async_result_get_op_res_gpointer (res);
5454
5455   if (iface->create_readwrite == NULL)
5456     {
5457       g_set_error_literal (&error, G_IO_ERROR,
5458                            G_IO_ERROR_NOT_SUPPORTED,
5459                            _("Operation not supported"));
5460
5461       g_simple_async_result_take_error (res, error);
5462
5463       return;
5464     }
5465
5466   stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5467
5468   if (stream == NULL)
5469     g_simple_async_result_take_error (res, error);
5470   else
5471     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5472 }
5473
5474 static void
5475 g_file_real_create_readwrite_async (GFile               *file,
5476                                     GFileCreateFlags     flags,
5477                                     int                  io_priority,
5478                                     GCancellable        *cancellable,
5479                                     GAsyncReadyCallback  callback,
5480                                     gpointer             user_data)
5481 {
5482   GFileCreateFlags *data;
5483   GSimpleAsyncResult *res;
5484
5485   data = g_new0 (GFileCreateFlags, 1);
5486   *data = flags;
5487
5488   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_readwrite_async);
5489   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5490
5491   g_simple_async_result_run_in_thread (res, create_readwrite_async_thread, io_priority, cancellable);
5492   g_object_unref (res);
5493 }
5494
5495 static GFileIOStream *
5496 g_file_real_create_readwrite_finish (GFile         *file,
5497                                      GAsyncResult  *res,
5498                                      GError       **error)
5499 {
5500   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5501   gpointer op;
5502
5503   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_readwrite_async);
5504
5505   if (g_simple_async_result_propagate_error (simple, error))
5506     return NULL;
5507
5508   op = g_simple_async_result_get_op_res_gpointer (simple);
5509   if (op)
5510     return g_object_ref (op);
5511
5512   return NULL;
5513 }
5514
5515 typedef struct {
5516   GFileIOStream *stream;
5517   char *etag;
5518   gboolean make_backup;
5519   GFileCreateFlags flags;
5520 } ReplaceRWAsyncData;
5521
5522 static void
5523 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5524 {
5525   if (data->stream)
5526     g_object_unref (data->stream);
5527   g_free (data->etag);
5528   g_free (data);
5529 }
5530
5531 static void
5532 replace_readwrite_async_thread (GSimpleAsyncResult *res,
5533                                 GObject            *object,
5534                                 GCancellable       *cancellable)
5535 {
5536   GFileIface *iface;
5537   GFileIOStream *stream;
5538   GError *error = NULL;
5539   ReplaceRWAsyncData *data;
5540
5541   iface = G_FILE_GET_IFACE (object);
5542
5543   data = g_simple_async_result_get_op_res_gpointer (res);
5544
5545   stream = iface->replace_readwrite (G_FILE (object),
5546                                      data->etag,
5547                                      data->make_backup,
5548                                      data->flags,
5549                                      cancellable,
5550                                      &error);
5551
5552   if (stream == NULL)
5553     g_simple_async_result_take_error (res, error);
5554   else
5555     data->stream = stream;
5556 }
5557
5558 static void
5559 g_file_real_replace_readwrite_async (GFile               *file,
5560                                      const char          *etag,
5561                                      gboolean             make_backup,
5562                                      GFileCreateFlags     flags,
5563                                      int                  io_priority,
5564                                      GCancellable        *cancellable,
5565                                      GAsyncReadyCallback  callback,
5566                                      gpointer             user_data)
5567 {
5568   GSimpleAsyncResult *res;
5569   ReplaceRWAsyncData *data;
5570
5571   data = g_new0 (ReplaceRWAsyncData, 1);
5572   data->etag = g_strdup (etag);
5573   data->make_backup = make_backup;
5574   data->flags = flags;
5575
5576   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_readwrite_async);
5577   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_rw_async_data_free);
5578
5579   g_simple_async_result_run_in_thread (res, replace_readwrite_async_thread, io_priority, cancellable);
5580   g_object_unref (res);
5581 }
5582
5583 static GFileIOStream *
5584 g_file_real_replace_readwrite_finish (GFile         *file,
5585                                       GAsyncResult  *res,
5586                                       GError       **error)
5587 {
5588   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5589   ReplaceRWAsyncData *data;
5590
5591   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_readwrite_async);
5592
5593   if (g_simple_async_result_propagate_error (simple, error))
5594     return NULL;
5595
5596   data = g_simple_async_result_get_op_res_gpointer (simple);
5597   if (data->stream)
5598     return g_object_ref (data->stream);
5599
5600   return NULL;
5601 }
5602
5603 typedef struct {
5604   char *name;
5605   GFile *file;
5606 } SetDisplayNameAsyncData;
5607
5608 static void
5609 set_display_name_data_free (SetDisplayNameAsyncData *data)
5610 {
5611   g_free (data->name);
5612   if (data->file)
5613     g_object_unref (data->file);
5614   g_free (data);
5615 }
5616
5617 static void
5618 set_display_name_async_thread (GSimpleAsyncResult *res,
5619                                GObject            *object,
5620                                GCancellable       *cancellable)
5621 {
5622   GError *error = NULL;
5623   SetDisplayNameAsyncData *data;
5624   GFile *file;
5625   
5626   data = g_simple_async_result_get_op_res_gpointer (res);
5627   
5628   file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
5629
5630   if (file == NULL)
5631     g_simple_async_result_take_error (res, error);
5632   else
5633     data->file = file;
5634 }
5635
5636 static void
5637 g_file_real_set_display_name_async (GFile               *file,  
5638                                     const char          *display_name,
5639                                     int                  io_priority,
5640                                     GCancellable        *cancellable,
5641                                     GAsyncReadyCallback  callback,
5642                                     gpointer             user_data)
5643 {
5644   GSimpleAsyncResult *res;
5645   SetDisplayNameAsyncData *data;
5646
5647   data = g_new0 (SetDisplayNameAsyncData, 1);
5648   data->name = g_strdup (display_name);
5649   
5650   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
5651   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
5652   
5653   g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
5654   g_object_unref (res);
5655 }
5656
5657 static GFile *
5658 g_file_real_set_display_name_finish (GFile         *file,
5659                                      GAsyncResult  *res,
5660                                      GError       **error)
5661 {
5662   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5663   SetDisplayNameAsyncData *data;
5664
5665   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
5666
5667   if (g_simple_async_result_propagate_error (simple, error))
5668     return NULL;
5669
5670   data = g_simple_async_result_get_op_res_gpointer (simple);
5671   if (data->file)
5672     return g_object_ref (data->file);
5673   
5674   return NULL;
5675 }
5676
5677 typedef struct {
5678   GFileQueryInfoFlags flags;
5679   GFileInfo *info;
5680   gboolean res;
5681   GError *error;
5682 } SetInfoAsyncData;
5683
5684 static void
5685 set_info_data_free (SetInfoAsyncData *data)
5686 {
5687   if (data->info)
5688     g_object_unref (data->info);
5689   if (data->error)
5690     g_error_free (data->error);
5691   g_free (data);
5692 }
5693
5694 static void
5695 set_info_async_thread (GSimpleAsyncResult *res,
5696                        GObject            *object,
5697                        GCancellable       *cancellable)
5698 {
5699   SetInfoAsyncData *data;
5700   
5701   data = g_simple_async_result_get_op_res_gpointer (res);
5702   
5703   data->error = NULL;
5704   data->res = g_file_set_attributes_from_info (G_FILE (object),
5705                                                data->info,
5706                                                data->flags,
5707                                                cancellable,
5708                                                &data->error);
5709 }
5710
5711 static void
5712 g_file_real_set_attributes_async (GFile               *file,
5713                                   GFileInfo           *info,
5714                                   GFileQueryInfoFlags  flags,
5715                                   int                  io_priority,
5716                                   GCancellable        *cancellable,
5717                                   GAsyncReadyCallback  callback,
5718                                   gpointer             user_data)
5719 {
5720   GSimpleAsyncResult *res;
5721   SetInfoAsyncData *data;
5722
5723   data = g_new0 (SetInfoAsyncData, 1);
5724   data->info = g_file_info_dup (info);
5725   data->flags = flags;
5726   
5727   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
5728   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
5729   
5730   g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
5731   g_object_unref (res);
5732 }
5733
5734 static gboolean
5735 g_file_real_set_attributes_finish (GFile         *file,
5736                                    GAsyncResult  *res,
5737                                    GFileInfo    **info,
5738                                    GError       **error)
5739 {
5740   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5741   SetInfoAsyncData *data;
5742   
5743   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
5744
5745   data = g_simple_async_result_get_op_res_gpointer (simple);
5746
5747   if (info) 
5748     *info = g_object_ref (data->info);
5749
5750   if (error != NULL && data->error) 
5751     *error = g_error_copy (data->error);
5752   
5753   return data->res;
5754 }
5755
5756 static void
5757 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
5758                                     GObject            *object,
5759                                     GCancellable       *cancellable)
5760 {
5761   GError *error = NULL;
5762   GMount *mount;
5763   
5764   mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5765
5766   if (mount == NULL)
5767     g_simple_async_result_take_error (res, error);
5768   else
5769     g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
5770 }
5771
5772 static void
5773 g_file_real_find_enclosing_mount_async (GFile               *file,
5774                                         int                  io_priority,
5775                                         GCancellable        *cancellable,
5776                                         GAsyncReadyCallback  callback,
5777                                         gpointer             user_data)
5778 {
5779   GSimpleAsyncResult *res;
5780   
5781   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
5782   
5783   g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
5784   g_object_unref (res);
5785 }
5786
5787 static GMount *
5788 g_file_real_find_enclosing_mount_finish (GFile         *file,
5789                                           GAsyncResult  *res,
5790                                           GError       **error)
5791 {
5792   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5793   GMount *mount;
5794
5795   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
5796
5797   if (g_simple_async_result_propagate_error (simple, error))
5798     return NULL;
5799
5800   mount = g_simple_async_result_get_op_res_gpointer (simple);
5801   return g_object_ref (mount);
5802 }
5803
5804
5805 typedef struct {
5806   GFile *source;
5807   GFile *destination;
5808   GFileCopyFlags flags;
5809   GFileProgressCallback progress_cb;
5810   gpointer progress_cb_data;
5811   GIOSchedulerJob *job;
5812 } CopyAsyncData;
5813
5814 static void
5815 copy_async_data_free (CopyAsyncData *data)
5816 {
5817   g_object_unref (data->source);
5818   g_object_unref (data->destination);
5819   g_free (data);
5820 }
5821
5822 typedef struct {
5823   CopyAsyncData *data;
5824   goffset current_num_bytes;
5825   goffset total_num_bytes;
5826 } ProgressData;
5827
5828 static gboolean
5829 copy_async_progress_in_main (gpointer user_data)
5830 {
5831   ProgressData *progress = user_data;
5832   CopyAsyncData *data = progress->data;
5833
5834   data->progress_cb (progress->current_num_bytes,
5835                      progress->total_num_bytes,
5836                      data->progress_cb_data);
5837
5838   return FALSE;
5839 }
5840
5841 static void
5842 copy_async_progress_callback (goffset  current_num_bytes,
5843                               goffset  total_num_bytes,
5844                               gpointer user_data)
5845 {
5846   CopyAsyncData *data = user_data;
5847   ProgressData *progress;
5848
5849   progress = g_new (ProgressData, 1);
5850   progress->data = data;
5851   progress->current_num_bytes = current_num_bytes;
5852   progress->total_num_bytes = total_num_bytes;
5853   
5854   g_io_scheduler_job_send_to_mainloop_async (data->job,
5855                                              copy_async_progress_in_main,
5856                                              progress,
5857                                              g_free);
5858 }
5859
5860 static gboolean
5861 copy_async_thread (GIOSchedulerJob *job,
5862                    GCancellable    *cancellable,
5863                    gpointer         user_data)
5864 {
5865   GSimpleAsyncResult *res;
5866   CopyAsyncData *data;
5867   gboolean result;
5868   GError *error;
5869
5870   res = user_data;
5871   data = g_simple_async_result_get_op_res_gpointer (res);
5872
5873   error = NULL;
5874   data->job = job;
5875   result = g_file_copy (data->source,
5876                         data->destination,
5877                         data->flags,
5878                         cancellable,
5879                         (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5880                         data,
5881                         &error);
5882
5883   if (!result)
5884     g_simple_async_result_take_error (res, error);
5885
5886   g_simple_async_result_complete_in_idle (res);
5887
5888   return FALSE;
5889 }
5890
5891 static void
5892 g_file_real_copy_async (GFile                  *source,
5893                         GFile                  *destination,
5894                         GFileCopyFlags          flags,
5895                         int                     io_priority,
5896                         GCancellable           *cancellable,
5897                         GFileProgressCallback   progress_callback,
5898                         gpointer                progress_callback_data,
5899                         GAsyncReadyCallback     callback,
5900                         gpointer                user_data)
5901 {
5902   GSimpleAsyncResult *res;
5903   CopyAsyncData *data;
5904
5905   data = g_new0 (CopyAsyncData, 1);
5906   data->source = g_object_ref (source);
5907   data->destination = g_object_ref (destination);
5908   data->flags = flags;
5909   data->progress_cb = progress_callback;
5910   data->progress_cb_data = progress_callback_data;
5911
5912   res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
5913   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
5914
5915   g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
5916 }
5917
5918 static gboolean
5919 g_file_real_copy_finish (GFile        *file,
5920                          GAsyncResult *res,
5921                          GError      **error)
5922 {
5923   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5924
5925   if (g_simple_async_result_propagate_error (simple, error))
5926     return FALSE;
5927
5928   return TRUE;
5929 }
5930
5931
5932 /********************************************
5933  *   Default VFS operations                 *
5934  ********************************************/
5935
5936 /**
5937  * g_file_new_for_path:
5938  * @path: a string containing a relative or absolute path. The string
5939  *   must be encoded in the glib filename encoding.
5940  * 
5941  * Constructs a #GFile for a given path. This operation never
5942  * fails, but the returned object might not support any I/O
5943  * operation if @path is malformed.
5944  * 
5945  * Returns: (transfer full): a new #GFile for the given @path.
5946  *   Free the returned object with g_object_unref().
5947  **/
5948 GFile *
5949 g_file_new_for_path (const char *path)
5950 {
5951   g_return_val_if_fail (path != NULL, NULL);
5952
5953   return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5954 }
5955  
5956 /**
5957  * g_file_new_for_uri:
5958  * @uri: a UTF8 string containing a URI.
5959  * 
5960  * Constructs a #GFile for a given URI. This operation never 
5961  * fails, but the returned object might not support any I/O 
5962  * operation if @uri is malformed or if the uri type is 
5963  * not supported.
5964  * 
5965  * Returns: (transfer full): a new #GFile for the given @uri.
5966  *   Free the returned object with g_object_unref().
5967  **/ 
5968 GFile *
5969 g_file_new_for_uri (const char *uri)
5970 {
5971   g_return_val_if_fail (uri != NULL, NULL);
5972
5973   return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
5974 }
5975
5976 /**
5977  * g_file_new_tmp:
5978  * @tmpl: (type filename) (allow-none): Template for the file
5979  *   name, as in g_file_open_tmp(), or %NULL for a default template.
5980  * @iostream: (out): on return, a #GFileIOStream for the created file.
5981  * @error: a #GError, or %NULL
5982  *
5983  * Opens a file in the preferred directory for temporary files (as
5984  * returned by g_get_tmp_dir()) and returns a #GFile and
5985  * #GFileIOStream pointing to it.
5986  *
5987  * @tmpl should be a string in the GLib file name encoding
5988  * containing a sequence of six 'X' characters, and containing no
5989  * directory components. If it is %NULL, a default template is used.
5990  *
5991  * Unlike the other #GFile constructors, this will return %NULL if
5992  * a temporary file could not be created.
5993  *
5994  * Returns: (transfer full): a new #GFile.
5995  *   Free the returned object with g_object_unref().
5996  *
5997  * Since: 2.32
5998  */
5999 GFile *
6000 g_file_new_tmp (const char     *tmpl,
6001                 GFileIOStream **iostream,
6002                 GError        **error)
6003 {
6004   gint fd;
6005   gchar *path;
6006   GFile *file;
6007   GFileOutputStream *output;
6008
6009   g_return_val_if_fail (iostream != NULL, NULL);
6010
6011   fd = g_file_open_tmp (tmpl, &path, error);
6012   if (fd == -1)
6013     return NULL;
6014
6015   file = g_file_new_for_path (path);
6016
6017   output = _g_local_file_output_stream_new (fd);
6018   *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6019
6020   g_object_unref (output);
6021   g_free (path);
6022
6023   return file;
6024 }
6025
6026 /**
6027  * g_file_parse_name:
6028  * @parse_name: a file name or path to be parsed.
6029  * 
6030  * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
6031  * This operation never fails, but the returned object might not support any I/O
6032  * operation if the @parse_name cannot be parsed.
6033  * 
6034  * Returns: (transfer full): a new #GFile.
6035  **/
6036 GFile *
6037 g_file_parse_name (const char *parse_name)
6038 {
6039   g_return_val_if_fail (parse_name != NULL, NULL);
6040
6041   return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6042 }
6043
6044 static gboolean
6045 is_valid_scheme_character (char c)
6046 {
6047   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6048 }
6049
6050 /* Following RFC 2396, valid schemes are built like:
6051  *       scheme        = alpha *( alpha | digit | "+" | "-" | "." )
6052  */
6053 static gboolean
6054 has_valid_scheme (const char *uri)
6055 {
6056   const char *p;
6057   
6058   p = uri;
6059   
6060   if (!g_ascii_isalpha (*p))
6061     return FALSE;
6062
6063   do {
6064     p++;
6065   } while (is_valid_scheme_character (*p));
6066
6067   return *p == ':';
6068 }
6069
6070 /**
6071  * g_file_new_for_commandline_arg:
6072  * @arg: a command line string.
6073  * 
6074  * Creates a #GFile with the given argument from the command line. The value of
6075  * @arg can be either a URI, an absolute path or a relative path resolved
6076  * relative to the current working directory.
6077  * This operation never fails, but the returned object might not support any
6078  * I/O operation if @arg points to a malformed path.
6079  *
6080  * Returns: (transfer full): a new #GFile.
6081  *  Free the returned object with g_object_unref().
6082  **/
6083 GFile *
6084 g_file_new_for_commandline_arg (const char *arg)
6085 {
6086   GFile *file;
6087   char *filename;
6088   char *current_dir;
6089   
6090   g_return_val_if_fail (arg != NULL, NULL);
6091   
6092   if (g_path_is_absolute (arg))
6093     return g_file_new_for_path (arg);
6094
6095   if (has_valid_scheme (arg))
6096     return g_file_new_for_uri (arg);
6097     
6098   current_dir = g_get_current_dir ();
6099   filename = g_build_filename (current_dir, arg, NULL);
6100   g_free (current_dir);
6101   
6102   file = g_file_new_for_path (filename);
6103   g_free (filename);
6104   
6105   return file;
6106 }
6107
6108 /**
6109  * g_file_mount_enclosing_volume:
6110  * @location: input #GFile.
6111  * @flags: flags affecting the operation
6112  * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid user interaction.
6113  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
6114  * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
6115  * @user_data: the data to pass to callback function
6116  * 
6117  * Starts a @mount_operation, mounting the volume that contains the file @location. 
6118  * 
6119  * When this operation has completed, @callback will be called with
6120  * @user_user data, and the operation can be finalized with 
6121  * g_file_mount_enclosing_volume_finish().
6122  * 
6123  * If @cancellable is not %NULL, then the operation can be cancelled by
6124  * triggering the cancellable object from another thread. If the operation
6125  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
6126  **/
6127 void
6128 g_file_mount_enclosing_volume (GFile               *location,
6129                                GMountMountFlags     flags,
6130                                GMountOperation     *mount_operation,
6131                                GCancellable        *cancellable,
6132                                GAsyncReadyCallback  callback,
6133                                gpointer             user_data)
6134 {
6135   GFileIface *iface;
6136
6137   g_return_if_fail (G_IS_FILE (location));
6138
6139   iface = G_FILE_GET_IFACE (location);
6140
6141   if (iface->mount_enclosing_volume == NULL)
6142     {
6143       g_simple_async_report_error_in_idle (G_OBJECT (location),
6144                                            callback, user_data,
6145                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6146                                            _("volume doesn't implement mount"));
6147       
6148       return;
6149     }
6150   
6151   (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6152
6153 }
6154
6155 /**
6156  * g_file_mount_enclosing_volume_finish:
6157  * @location: input #GFile.
6158  * @result: a #GAsyncResult.
6159  * @error: a #GError, or %NULL
6160  * 
6161  * Finishes a mount operation started by g_file_mount_enclosing_volume().
6162  * 
6163  * Returns: %TRUE if successful. If an error
6164  * has occurred, this function will return %FALSE and set @error
6165  * appropriately if present.
6166  **/
6167 gboolean
6168 g_file_mount_enclosing_volume_finish (GFile         *location,
6169                                       GAsyncResult  *result,
6170                                       GError       **error)
6171 {
6172   GFileIface *iface;
6173
6174   g_return_val_if_fail (G_IS_FILE (location), FALSE);
6175   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6176
6177   if (g_async_result_legacy_propagate_error (result, error))
6178     return FALSE;
6179
6180   iface = G_FILE_GET_IFACE (location);
6181
6182   return (* iface->mount_enclosing_volume_finish) (location, result, error);
6183 }
6184
6185 /********************************************
6186  *   Utility functions                      *
6187  ********************************************/
6188
6189 /**
6190  * g_file_query_default_handler:
6191  * @file: a #GFile to open.
6192  * @cancellable: optional #GCancellable object, %NULL to ignore.
6193  * @error: a #GError, or %NULL
6194  *
6195  * Returns the #GAppInfo that is registered as the default
6196  * application to handle the file specified by @file.
6197  *
6198  * If @cancellable is not %NULL, then the operation can be cancelled by
6199  * triggering the cancellable object from another thread. If the operation
6200  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
6201  *
6202  * Returns: (transfer full): a #GAppInfo if the handle was found, %NULL if there were errors.
6203  * When you are done with it, release it with g_object_unref()
6204  **/
6205 GAppInfo *
6206 g_file_query_default_handler (GFile                  *file,
6207                               GCancellable           *cancellable,
6208                               GError                **error)
6209 {
6210   char *uri_scheme;
6211   const char *content_type;
6212   GAppInfo *appinfo;
6213   GFileInfo *info;
6214   char *path;
6215   
6216   uri_scheme = g_file_get_uri_scheme (file);
6217   if (uri_scheme && uri_scheme[0] != '\0')
6218     {
6219       appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6220       g_free (uri_scheme);
6221
6222       if (appinfo != NULL)
6223         return appinfo;
6224     }
6225
6226   info = g_file_query_info (file,
6227                             G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6228                             0,
6229                             cancellable,
6230                             error);
6231   if (info == NULL)
6232     return NULL;
6233
6234   appinfo = NULL;
6235
6236   content_type = g_file_info_get_content_type (info);
6237   if (content_type)
6238     {
6239       /* Don't use is_native(), as we want to support fuse paths if available */
6240       path = g_file_get_path (file);
6241       appinfo = g_app_info_get_default_for_type (content_type,
6242                                                  path == NULL);
6243       g_free (path);
6244     }
6245   
6246   g_object_unref (info);
6247
6248   if (appinfo != NULL)
6249     return appinfo;
6250
6251   g_set_error_literal (error, G_IO_ERROR,
6252                        G_IO_ERROR_NOT_SUPPORTED,
6253                        _("No application is registered as handling this file"));
6254   return NULL;
6255   
6256 }
6257
6258
6259 #define GET_CONTENT_BLOCK_SIZE 8192
6260
6261 /**
6262  * g_file_load_contents:
6263  * @file: input #GFile.
6264  * @cancellable: optional #GCancellable object, %NULL to ignore.
6265  * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file.
6266  * @length: (out) (allow-none): a location to place the length of the contents of the file,
6267  *    or %NULL if the length is not needed
6268  * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6269  *    or %NULL if the entity tag is not needed
6270  * @error: a #GError, or %NULL
6271  *
6272  * Loads the content of the file into memory. The data is always 
6273  * zero-terminated, but this is not included in the resultant @length.
6274  * The returned @content should be freed with g_free() when no longer
6275  * needed.
6276  * 
6277  * If @cancellable is not %NULL, then the operation can be cancelled by
6278  * triggering the cancellable object from another thread. If the operation
6279  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
6280  * 
6281  * Returns: %TRUE if the @file's contents were successfully loaded.
6282  * %FALSE if there were errors.
6283  **/
6284 gboolean
6285 g_file_load_contents (GFile         *file,
6286                       GCancellable  *cancellable,
6287                       char         **contents,
6288                       gsize         *length,
6289                       char         **etag_out,
6290                       GError       **error)
6291 {
6292   GFileInputStream *in;
6293   GByteArray *content;
6294   gsize pos;
6295   gssize res;
6296   GFileInfo *info;
6297
6298   g_return_val_if_fail (G_IS_FILE (file), FALSE);
6299   g_return_val_if_fail (contents != NULL, FALSE);
6300
6301   in = g_file_read (file, cancellable, error);
6302   if (in == NULL)
6303     return FALSE;
6304
6305   content = g_byte_array_new ();
6306   pos = 0;
6307   
6308   g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6309   while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6310                                      content->data + pos,
6311                                      GET_CONTENT_BLOCK_SIZE,
6312                                      cancellable, error)) > 0)
6313     {
6314       pos += res;
6315       g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6316     }
6317
6318   if (etag_out)
6319     {
6320       *etag_out = NULL;
6321       
6322       info = g_file_input_stream_query_info (in,
6323                                              G_FILE_ATTRIBUTE_ETAG_VALUE,
6324                                              cancellable,
6325                                              NULL);
6326       if (info)
6327         {
6328           *etag_out = g_strdup (g_file_info_get_etag (info));
6329           g_object_unref (info);
6330         }
6331     } 
6332
6333   /* Ignore errors on close */
6334   g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6335   g_object_unref (in);
6336
6337   if (res < 0)
6338     {
6339       /* error is set already */
6340       g_byte_array_free (content, TRUE);
6341       return FALSE;
6342     }
6343
6344   if (length)
6345     *length = pos;
6346
6347   /* Zero terminate (we got an extra byte allocated for this */
6348   content->data[pos] = 0;
6349   
6350   *contents = (char *)g_byte_array_free (content, FALSE);
6351   
6352   return TRUE;
6353 }
6354
6355 typedef struct {
6356   GFile *file;
6357   GError *error;
6358   GCancellable *cancellable;
6359   GFileReadMoreCallback read_more_callback;
6360   GAsyncReadyCallback callback;
6361   gpointer user_data;
6362   GByteArray *content;
6363   gsize pos;
6364   char *etag;
6365 } LoadContentsData;
6366
6367
6368 static void
6369 load_contents_data_free (LoadContentsData *data)
6370 {
6371   if (data->error)
6372     g_error_free (data->error);
6373   if (data->cancellable)
6374     g_object_unref (data->cancellable);
6375   if (data->content)
6376     g_byte_array_free (data->content, TRUE);
6377   g_free (data->etag);
6378   g_object_unref (data->file);
6379   g_free (data);
6380 }
6381
6382 static void
6383 load_contents_close_callback (GObject      *obj,
6384                               GAsyncResult *close_res,
6385                               gpointer      user_data)
6386 {
6387   GInputStream *stream = G_INPUT_STREAM (obj);
6388   LoadContentsData *data = user_data;
6389   GSimpleAsyncResult *res;
6390
6391   /* Ignore errors here, we're only reading anyway */
6392   g_input_stream_close_finish (stream, close_res, NULL);
6393   g_object_unref (stream);
6394
6395   res = g_simple_async_result_new (G_OBJECT (data->file),
6396                                    data->callback,
6397                                    data->user_data,
6398                                    g_file_load_contents_async);
6399   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
6400   g_simple_async_result_complete (res);
6401   g_object_unref (res);
6402 }
6403
6404 static void
6405 load_contents_fstat_callback (GObject      *obj,
6406                               GAsyncResult *stat_res,
6407                               gpointer      user_data)
6408 {
6409   GInputStream *stream = G_INPUT_STREAM (obj);
6410   LoadContentsData *data = user_data;
6411   GFileInfo *info;
6412
6413   info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6414                                                    stat_res, NULL);
6415   if (info)
6416     {
6417       data->etag = g_strdup (g_file_info_get_etag (info));
6418       g_object_unref (info);
6419     }
6420
6421   g_input_stream_close_async (stream, 0,
6422                               data->cancellable,
6423                               load_contents_close_callback, data);
6424 }
6425
6426 static void
6427 load_contents_read_callback (GObject      *obj,
6428                              GAsyncResult *read_res,
6429                              gpointer      user_data)
6430 {
6431   GInputStream *stream = G_INPUT_STREAM (obj);
6432   LoadContentsData *data = user_data;
6433   GError *error = NULL;
6434   gssize read_size;
6435
6436   read_size = g_input_stream_read_finish (stream, read_res, &error);
6437
6438   if (read_size < 0) 
6439     {
6440       /* Error or EOF, close the file */
6441       data->error = error;
6442       g_input_stream_close_async (stream, 0,
6443                                   data->cancellable,
6444                                   load_contents_close_callback, data);
6445     }
6446   else if (read_size == 0)
6447     {
6448       g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6449                                             G_FILE_ATTRIBUTE_ETAG_VALUE,
6450                                             0,
6451                                             data->cancellable,
6452                                             load_contents_fstat_callback,
6453                                             data);
6454     }
6455   else if (read_size > 0)
6456     {
6457       data->pos += read_size;
6458       
6459       g_byte_array_set_size (data->content,
6460                              data->pos + GET_CONTENT_BLOCK_SIZE);
6461
6462
6463       if (data->read_more_callback &&
6464           !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
6465         g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6466                                               G_FILE_ATTRIBUTE_ETAG_VALUE,
6467                                               0,
6468                                               data->cancellable,
6469                                               load_contents_fstat_callback,
6470                                               data);
6471       else 
6472         g_input_stream_read_async (stream,
6473                                    data->content->data + data->pos,
6474                                    GET_CONTENT_BLOCK_SIZE,
6475                                    0,
6476                                    data->cancellable,
6477                                    load_contents_read_callback,
6478                                    data);
6479     }
6480 }
6481
6482 static void
6483 load_contents_open_callback (GObject      *obj,
6484                              GAsyncResult *open_res,
6485                              gpointer      user_data)
6486 {
6487   GFile *file = G_FILE (obj);
6488   GFileInputStream *stream;
6489   LoadContentsData *data = user_data;
6490   GError *error = NULL;
6491   GSimpleAsyncResult *res;
6492
6493   stream = g_file_read_finish (file, open_res, &error);
6494
6495   if (stream)
6496     {
6497       g_byte_array_set_size (data->content,
6498                              data->pos + GET_CONTENT_BLOCK_SIZE);
6499       g_input_stream_read_async (G_INPUT_STREAM (stream),
6500                                  data->content->data + data->pos,
6501                                  GET_CONTENT_BLOCK_SIZE,
6502                                  0,
6503                                  data->cancellable,
6504                                  load_contents_read_callback,
6505                                  data);
6506       
6507     }
6508   else
6509     {
6510       res = g_simple_async_result_new_take_error (G_OBJECT (data->file),
6511                                                   data->callback,
6512                                                   data->user_data,
6513                                                   error);
6514       g_simple_async_result_complete (res);
6515       load_contents_data_free (data);
6516       g_object_unref (res);
6517     }
6518 }
6519
6520 /**
6521  * g_file_load_partial_contents_async: (skip)
6522  * @file: input #GFile.
6523  * @cancellable: optional #GCancellable object, %NULL to ignore.
6524  * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
6525  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6526  * @user_data: the data to pass to the callback functions.
6527  *
6528  * Reads the partial contents of a file. A #GFileReadMoreCallback should be 
6529  * used to stop reading from the file when appropriate, else this function
6530  * will behave exactly as g_file_load_contents_async(). This operation 
6531  * can be finished by g_file_load_partial_contents_finish().
6532  *
6533  * Users of this function should be aware that @user_data is passed to 
6534  * both the @read_more_callback and the @callback.
6535  *
6536  * If @cancellable is not %NULL, then the operation can be cancelled by
6537  * triggering the cancellable object from another thread. If the operation
6538  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
6539  **/
6540 void
6541 g_file_load_partial_contents_async (GFile                 *file,
6542                                     GCancellable          *cancellable,
6543                                     GFileReadMoreCallback  read_more_callback,
6544                                     GAsyncReadyCallback    callback,
6545                                     gpointer               user_data)
6546 {
6547   LoadContentsData *data;
6548
6549   g_return_if_fail (G_IS_FILE (file));
6550
6551   data = g_new0 (LoadContentsData, 1);
6552
6553   if (cancellable)
6554     data->cancellable = g_object_ref (cancellable);
6555   data->read_more_callback = read_more_callback;
6556   data->callback = callback;
6557   data->user_data = user_data;
6558   data->content = g_byte_array_new ();
6559   data->file = g_object_ref (file);
6560
6561   g_file_read_async (file,
6562                      0,
6563                      cancellable,
6564                      load_contents_open_callback,
6565                      data);
6566 }
6567
6568 /**
6569  * g_file_load_partial_contents_finish:
6570  * @file: input #GFile.
6571  * @res: a #GAsyncResult. 
6572  * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file.
6573  * @length: (out) (allow-none): a location to place the length of the contents of the file,
6574  *     or %NULL if the length is not needed
6575  * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6576  *     or %NULL if the entity tag is not needed
6577  * @error: a #GError, or %NULL
6578  * 
6579  * Finishes an asynchronous partial load operation that was started
6580  * with g_file_load_partial_contents_async(). The data is always 
6581  * zero-terminated, but this is not included in the resultant @length.
6582  * The returned @content should be freed with g_free() when no longer
6583  * needed.
6584  *
6585  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
6586  * present, it will be set appropriately. 
6587  **/
6588 gboolean
6589 g_file_load_partial_contents_finish (GFile         *file,
6590                                      GAsyncResult  *res,
6591                                      char         **contents,
6592                                      gsize         *length,
6593                                      char         **etag_out,
6594                                      GError       **error)
6595 {
6596   GSimpleAsyncResult *simple;
6597   LoadContentsData *data;
6598
6599   g_return_val_if_fail (G_IS_FILE (file), FALSE);
6600   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6601   g_return_val_if_fail (contents != NULL, FALSE);
6602
6603   simple = G_SIMPLE_ASYNC_RESULT (res);
6604
6605   if (g_simple_async_result_propagate_error (simple, error))
6606     return FALSE;
6607   
6608   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
6609   
6610   data = g_simple_async_result_get_op_res_gpointer (simple);
6611
6612   if (data->error)
6613     {
6614       g_propagate_error (error, data->error);
6615       data->error = NULL;
6616       *contents = NULL;
6617       if (length)
6618         *length = 0;
6619       return FALSE;
6620     }
6621
6622   if (length)
6623     *length = data->pos;
6624
6625   if (etag_out)
6626     {
6627       *etag_out = data->etag;
6628       data->etag = NULL;
6629     }
6630
6631   /* Zero terminate */
6632   g_byte_array_set_size (data->content, data->pos + 1);
6633   data->content->data[data->pos] = 0;
6634   
6635   *contents = (char *)g_byte_array_free (data->content, FALSE);
6636   data->content = NULL;
6637
6638   return TRUE;
6639 }
6640
6641 /**
6642  * g_file_load_contents_async:
6643  * @file: input #GFile.
6644  * @cancellable: optional #GCancellable object, %NULL to ignore.
6645  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6646  * @user_data: the data to pass to callback function
6647  * 
6648  * Starts an asynchronous load of the @file's contents.
6649  *
6650  * For more details, see g_file_load_contents() which is
6651  * the synchronous version of this call.
6652  *
6653  * When the load operation has completed, @callback will be called 
6654  * with @user data. To finish the operation, call 
6655  * g_file_load_contents_finish() with the #GAsyncResult returned by 
6656  * the @callback.
6657  * 
6658  * If @cancellable is not %NULL, then the operation can be cancelled by
6659  * triggering the cancellable object from another thread. If the operation
6660  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
6661  **/
6662 void
6663 g_file_load_contents_async (GFile               *file,
6664                            GCancellable        *cancellable,
6665                            GAsyncReadyCallback  callback,
6666                            gpointer             user_data)
6667 {
6668   g_file_load_partial_contents_async (file,
6669                                       cancellable,
6670                                       NULL,
6671                                       callback, user_data);
6672 }
6673
6674 /**
6675  * g_file_load_contents_finish:
6676  * @file: input #GFile.
6677  * @res: a #GAsyncResult. 
6678  * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file.
6679  * @length: (out) (allow-none): a location to place the length of the contents of the file,
6680  *     or %NULL if the length is not needed
6681  * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6682  *     or %NULL if the entity tag is not needed
6683  * @error: a #GError, or %NULL
6684  * 
6685  * Finishes an asynchronous load of the @file's contents. 
6686  * The contents are placed in @contents, and @length is set to the 
6687  * size of the @contents string. The @content should be freed with
6688  * g_free() when no longer needed. If @etag_out is present, it will be 
6689  * set to the new entity tag for the @file.
6690  * 
6691  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
6692  * present, it will be set appropriately. 
6693  **/
6694 gboolean
6695 g_file_load_contents_finish (GFile         *file,
6696                              GAsyncResult  *res,
6697                              char         **contents,
6698                              gsize         *length,
6699                              char         **etag_out,
6700                              GError       **error)
6701 {
6702   return g_file_load_partial_contents_finish (file,
6703                                               res,
6704                                               contents,
6705                                               length,
6706                                               etag_out,
6707                                               error);
6708 }
6709   
6710 /**
6711  * g_file_replace_contents:
6712  * @file: input #GFile.
6713  * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file.
6714  * @length: the length of @contents in bytes.
6715  * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link> 
6716  *     for the document, or %NULL
6717  * @make_backup: %TRUE if a backup should be created.
6718  * @flags: a set of #GFileCreateFlags.
6719  * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6720  *      for the document. This should be freed with g_free() when no longer 
6721  *      needed, or %NULL
6722  * @cancellable: optional #GCancellable object, %NULL to ignore.
6723  * @error: a #GError, or %NULL
6724  *
6725  * Replaces the contents of @file with @contents of @length bytes.
6726  
6727  * If @etag is specified (not %NULL) any existing file must have that etag, or
6728  * the error %G_IO_ERROR_WRONG_ETAG will be returned.
6729  *
6730  * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
6731  * 
6732  * If @cancellable is not %NULL, then the operation can be cancelled by
6733  * triggering the cancellable object from another thread. If the operation
6734  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
6735  *
6736  * The returned @new_etag can be used to verify that the file hasn't changed the
6737  * next time it is saved over.
6738  * 
6739  * Returns: %TRUE if successful. If an error
6740  * has occurred, this function will return %FALSE and set @error
6741  * appropriately if present.
6742  **/
6743 gboolean
6744 g_file_replace_contents (GFile             *file,
6745                          const char        *contents,
6746                          gsize              length,
6747                          const char        *etag,
6748                          gboolean           make_backup,
6749                          GFileCreateFlags   flags,
6750                          char             **new_etag,
6751                          GCancellable      *cancellable,
6752                          GError           **error)
6753 {
6754   GFileOutputStream *out;
6755   gsize pos, remainder;
6756   gssize res;
6757   gboolean ret;
6758
6759   g_return_val_if_fail (G_IS_FILE (file), FALSE);
6760   g_return_val_if_fail (contents != NULL, FALSE);
6761
6762   out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6763   if (out == NULL)
6764     return FALSE;
6765
6766   pos = 0;
6767   remainder = length;
6768   while (remainder > 0 &&
6769          (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6770                                        contents + pos,
6771                                        MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6772                                        cancellable,
6773                                        error)) > 0)
6774     {
6775       pos += res;
6776       remainder -= res;
6777     }
6778   
6779   if (remainder > 0 && res < 0)
6780     {
6781       /* Ignore errors on close */
6782       g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6783       g_object_unref (out);
6784
6785       /* error is set already */
6786       return FALSE;
6787     }
6788   
6789   ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6790
6791   if (new_etag)
6792     *new_etag = g_file_output_stream_get_etag (out);
6793
6794   g_object_unref (out);
6795
6796   return ret;
6797 }
6798
6799 typedef struct {
6800   GFile *file;
6801   GError *error;
6802   GCancellable *cancellable;
6803   GAsyncReadyCallback callback;
6804   gpointer user_data;
6805   const char *content;
6806   gsize length;
6807   gsize pos;
6808   char *etag;
6809 } ReplaceContentsData;
6810
6811 static void
6812 replace_contents_data_free (ReplaceContentsData *data)
6813 {
6814   if (data->error)
6815     g_error_free (data->error);
6816   if (data->cancellable)
6817     g_object_unref (data->cancellable);
6818   g_object_unref (data->file);
6819   g_free (data->etag);
6820   g_free (data);
6821 }
6822
6823 static void
6824 replace_contents_close_callback (GObject      *obj,
6825                                  GAsyncResult *close_res,
6826                                  gpointer      user_data)
6827 {
6828   GOutputStream *stream = G_OUTPUT_STREAM (obj);
6829   ReplaceContentsData *data = user_data;
6830   GSimpleAsyncResult *res;
6831
6832   /* Ignore errors here, we're only reading anyway */
6833   g_output_stream_close_finish (stream, close_res, NULL);
6834   g_object_unref (stream);
6835
6836   data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6837   
6838   res = g_simple_async_result_new (G_OBJECT (data->file),
6839                                    data->callback,
6840                                    data->user_data,
6841                                    g_file_replace_contents_async);
6842   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
6843   g_simple_async_result_complete (res);
6844   g_object_unref (res);
6845 }
6846
6847 static void
6848 replace_contents_write_callback (GObject      *obj,
6849                                  GAsyncResult *read_res,
6850                                  gpointer      user_data)
6851 {
6852   GOutputStream *stream = G_OUTPUT_STREAM (obj);
6853   ReplaceContentsData *data = user_data;
6854   GError *error = NULL;
6855   gssize write_size;
6856   
6857   write_size = g_output_stream_write_finish (stream, read_res, &error);
6858
6859   if (write_size <= 0) 
6860     {
6861       /* Error or EOF, close the file */
6862       if (write_size < 0)
6863         data->error = error;
6864       g_output_stream_close_async (stream, 0,
6865                                    data->cancellable,
6866                                    replace_contents_close_callback, data);
6867     }
6868   else if (write_size > 0)
6869     {
6870       data->pos += write_size;
6871
6872       if (data->pos >= data->length)
6873         g_output_stream_close_async (stream, 0,
6874                                      data->cancellable,
6875                                      replace_contents_close_callback, data);
6876       else
6877         g_output_stream_write_async (stream,
6878                                      data->content + data->pos,
6879                                      data->length - data->pos,
6880                                      0,
6881                                      data->cancellable,
6882                                      replace_contents_write_callback,
6883                                      data);
6884     }
6885 }
6886
6887 static void
6888 replace_contents_open_callback (GObject      *obj,
6889                                 GAsyncResult *open_res,
6890                                 gpointer      user_data)
6891 {
6892   GFile *file = G_FILE (obj);
6893   GFileOutputStream *stream;
6894   ReplaceContentsData *data = user_data;
6895   GError *error = NULL;
6896   GSimpleAsyncResult *res;
6897
6898   stream = g_file_replace_finish (file, open_res, &error);
6899
6900   if (stream)
6901     {
6902       g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6903                                    data->content + data->pos,
6904                                    data->length - data->pos,
6905                                    0,
6906                                    data->cancellable,
6907                                    replace_contents_write_callback,
6908                                    data);
6909       
6910     }
6911   else
6912     {
6913       res = g_simple_async_result_new_take_error (G_OBJECT (data->file),
6914                                                   data->callback,
6915                                                   data->user_data,
6916                                                   error);
6917       g_simple_async_result_complete (res);
6918       replace_contents_data_free (data);
6919       g_object_unref (res);
6920     }
6921 }
6922
6923 /**
6924  * g_file_replace_contents_async:
6925  * @file: input #GFile.
6926  * @contents: (element-type guint8) (array length=length): string of contents to replace the file with.
6927  * @length: the length of @contents in bytes.
6928  * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6929  * @make_backup: %TRUE if a backup should be created.
6930  * @flags: a set of #GFileCreateFlags.
6931  * @cancellable: optional #GCancellable object, %NULL to ignore.
6932  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6933  * @user_data: the data to pass to callback function
6934  * 
6935  * Starts an asynchronous replacement of @file with the given 
6936  * @contents of @length bytes. @etag will replace the document's 
6937  * current entity tag.
6938  * 
6939  * When this operation has completed, @callback will be called with
6940  * @user_user data, and the operation can be finalized with 
6941  * g_file_replace_contents_finish().
6942  * 
6943  * If @cancellable is not %NULL, then the operation can be cancelled by
6944  * triggering the cancellable object from another thread. If the operation
6945  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
6946  * 
6947  * If @make_backup is %TRUE, this function will attempt to 
6948  * make a backup of @file.
6949  **/
6950 void
6951 g_file_replace_contents_async  (GFile               *file,
6952                                 const char          *contents,
6953                                 gsize                length,
6954                                 const char          *etag,
6955                                 gboolean             make_backup,
6956                                 GFileCreateFlags     flags,
6957                                 GCancellable        *cancellable,
6958                                 GAsyncReadyCallback  callback,
6959                                 gpointer             user_data)
6960 {
6961   ReplaceContentsData *data;
6962
6963   g_return_if_fail (G_IS_FILE (file));
6964   g_return_if_fail (contents != NULL);
6965
6966   data = g_new0 (ReplaceContentsData, 1);
6967
6968   if (cancellable)
6969     data->cancellable = g_object_ref (cancellable);
6970   data->callback = callback;
6971   data->user_data = user_data;
6972   data->content = contents;
6973   data->length = length;
6974   data->pos = 0;
6975   data->file = g_object_ref (file);
6976
6977   g_file_replace_async (file,
6978                         etag,
6979                         make_backup,
6980                         flags,
6981                         0,
6982                         cancellable,
6983                         replace_contents_open_callback,
6984                         data);
6985 }
6986   
6987 /**
6988  * g_file_replace_contents_finish:
6989  * @file: input #GFile.
6990  * @res: a #GAsyncResult. 
6991  * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link> 
6992  *     for the document. This should be freed with g_free() when it is no 
6993  *     longer needed, or %NULL
6994  * @error: a #GError, or %NULL 
6995  * 
6996  * Finishes an asynchronous replace of the given @file. See
6997  * g_file_replace_contents_async(). Sets @new_etag to the new entity 
6998  * tag for the document, if present.
6999  * 
7000  * Returns: %TRUE on success, %FALSE on failure.
7001  **/
7002 gboolean
7003 g_file_replace_contents_finish (GFile         *file,
7004                                 GAsyncResult  *res,
7005                                 char         **new_etag,
7006                                 GError       **error)
7007 {
7008   GSimpleAsyncResult *simple;
7009   ReplaceContentsData *data;
7010
7011   g_return_val_if_fail (G_IS_FILE (file), FALSE);
7012   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
7013
7014   simple = G_SIMPLE_ASYNC_RESULT (res);
7015
7016   if (g_simple_async_result_propagate_error (simple, error))
7017     return FALSE;
7018   
7019   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
7020   
7021   data = g_simple_async_result_get_op_res_gpointer (simple);
7022
7023   if (data->error)
7024     {
7025       g_propagate_error (error, data->error);
7026       data->error = NULL;
7027       return FALSE;
7028     }
7029
7030
7031   if (new_etag)
7032     {
7033       *new_etag = data->etag;
7034       data->etag = NULL; /* Take ownership */
7035     }
7036   
7037   return TRUE;
7038 }
7039
7040 /**
7041  * g_file_start_mountable:
7042  * @file: input #GFile.
7043  * @flags: flags affecting the operation
7044  * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
7045  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
7046  * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
7047  * @user_data: the data to pass to callback function
7048  *
7049  * Starts a file of type G_FILE_TYPE_MOUNTABLE.
7050  * Using @start_operation, you can request callbacks when, for instance,
7051  * passwords are needed during authentication.
7052  *
7053  * If @cancellable is not %NULL, then the operation can be cancelled by
7054  * triggering the cancellable object from another thread. If the operation
7055  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7056  *
7057  * When the operation is finished, @callback will be called. You can then call
7058  * g_file_mount_mountable_finish() to get the result of the operation.
7059  *
7060  * Since: 2.22
7061  */
7062 void
7063 g_file_start_mountable (GFile                      *file,
7064                         GDriveStartFlags            flags,
7065                         GMountOperation            *start_operation,
7066                         GCancellable               *cancellable,
7067                         GAsyncReadyCallback         callback,
7068                         gpointer                    user_data)
7069 {
7070   GFileIface *iface;
7071
7072   g_return_if_fail (G_IS_FILE (file));
7073
7074   iface = G_FILE_GET_IFACE (file);
7075
7076   if (iface->start_mountable == NULL)
7077     {
7078       g_simple_async_report_error_in_idle (G_OBJECT (file),
7079                                            callback,
7080                                            user_data,
7081                                            G_IO_ERROR,
7082                                            G_IO_ERROR_NOT_SUPPORTED,
7083                                            _("Operation not supported"));
7084       return;
7085     }
7086
7087   (* iface->start_mountable) (file,
7088                               flags,
7089                               start_operation,
7090                               cancellable,
7091                               callback,
7092                               user_data);
7093 }
7094
7095 /**
7096  * g_file_start_mountable_finish:
7097  * @file: input #GFile.
7098  * @result: a #GAsyncResult.
7099  * @error: a #GError, or %NULL
7100  *
7101  * Finishes a start operation. See g_file_start_mountable() for details.
7102  *
7103  * Finish an asynchronous start operation that was started
7104  * with g_file_start_mountable().
7105  *
7106  * Returns: %TRUE if the operation finished successfully. %FALSE
7107  * otherwise.
7108  *
7109  * Since: 2.22
7110  */
7111 gboolean
7112 g_file_start_mountable_finish (GFile                      *file,
7113                                GAsyncResult               *result,
7114                                GError                    **error)
7115 {
7116   GFileIface *iface;
7117
7118   g_return_val_if_fail (G_IS_FILE (file), FALSE);
7119   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7120
7121   if (g_async_result_legacy_propagate_error (result, error))
7122     return FALSE;
7123
7124   iface = G_FILE_GET_IFACE (file);
7125   return (* iface->start_mountable_finish) (file, result, error);
7126 }
7127
7128 /**
7129  * g_file_stop_mountable:
7130  * @file: input #GFile.
7131  * @flags: flags affecting the operation
7132  * @mount_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
7133  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
7134  * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
7135  * @user_data: the data to pass to callback function
7136  *
7137  * Stops a file of type G_FILE_TYPE_MOUNTABLE.
7138  *
7139  * If @cancellable is not %NULL, then the operation can be cancelled by
7140  * triggering the cancellable object from another thread. If the operation
7141  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7142  *
7143  * When the operation is finished, @callback will be called. You can then call
7144  * g_file_stop_mountable_finish() to get the result of the operation.
7145  *
7146  * Since: 2.22
7147  */
7148 void
7149 g_file_stop_mountable (GFile                      *file,
7150                        GMountUnmountFlags          flags,
7151                        GMountOperation            *mount_operation,
7152                        GCancellable               *cancellable,
7153                        GAsyncReadyCallback         callback,
7154                        gpointer                    user_data)
7155 {
7156   GFileIface *iface;
7157
7158   g_return_if_fail (G_IS_FILE (file));
7159
7160   iface = G_FILE_GET_IFACE (file);
7161
7162   if (iface->stop_mountable == NULL)
7163     {
7164       g_simple_async_report_error_in_idle (G_OBJECT (file),
7165                                            callback,
7166                                            user_data,
7167                                            G_IO_ERROR,
7168                                            G_IO_ERROR_NOT_SUPPORTED,
7169                                            _("Operation not supported"));
7170       return;
7171     }
7172
7173   (* iface->stop_mountable) (file,
7174                              flags,
7175                              mount_operation,
7176                              cancellable,
7177                              callback,
7178                              user_data);
7179 }
7180
7181 /**
7182  * g_file_stop_mountable_finish:
7183  * @file: input #GFile.
7184  * @result: a #GAsyncResult.
7185  * @error: a #GError, or %NULL
7186  *
7187  * Finishes an stop operation, see g_file_stop_mountable() for details.
7188  *
7189  * Finish an asynchronous stop operation that was started
7190  * with g_file_stop_mountable().
7191  *
7192  * Returns: %TRUE if the operation finished successfully. %FALSE
7193  * otherwise.
7194  *
7195  * Since: 2.22
7196  */
7197 gboolean
7198 g_file_stop_mountable_finish (GFile                      *file,
7199                               GAsyncResult               *result,
7200                               GError                    **error)
7201 {
7202   GFileIface *iface;
7203
7204   g_return_val_if_fail (G_IS_FILE (file), FALSE);
7205   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7206
7207   if (g_async_result_legacy_propagate_error (result, error))
7208     return FALSE;
7209
7210   iface = G_FILE_GET_IFACE (file);
7211   return (* iface->stop_mountable_finish) (file, result, error);
7212 }
7213
7214 /**
7215  * g_file_poll_mountable:
7216  * @file: input #GFile.
7217  * @cancellable: optional #GCancellable object, %NULL to ignore.
7218  * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
7219  * @user_data: the data to pass to callback function
7220  *
7221  * Polls a file of type G_FILE_TYPE_MOUNTABLE.
7222  *
7223  * If @cancellable is not %NULL, then the operation can be cancelled by
7224  * triggering the cancellable object from another thread. If the operation
7225  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7226  *
7227  * When the operation is finished, @callback will be called. You can then call
7228  * g_file_mount_mountable_finish() to get the result of the operation.
7229  *
7230  * Since: 2.22
7231  */
7232 void
7233 g_file_poll_mountable (GFile                      *file,
7234                        GCancellable               *cancellable,
7235                        GAsyncReadyCallback         callback,
7236                        gpointer                    user_data)
7237 {
7238   GFileIface *iface;
7239
7240   g_return_if_fail (G_IS_FILE (file));
7241
7242   iface = G_FILE_GET_IFACE (file);
7243
7244   if (iface->poll_mountable == NULL)
7245     {
7246       g_simple_async_report_error_in_idle (G_OBJECT (file),
7247                                            callback,
7248                                            user_data,
7249                                            G_IO_ERROR,
7250                                            G_IO_ERROR_NOT_SUPPORTED,
7251                                            _("Operation not supported"));
7252       return;
7253     }
7254
7255   (* iface->poll_mountable) (file,
7256                              cancellable,
7257                              callback,
7258                              user_data);
7259 }
7260
7261 /**
7262  * g_file_poll_mountable_finish:
7263  * @file: input #GFile.
7264  * @result: a #GAsyncResult.
7265  * @error: a #GError, or %NULL
7266  *
7267  * Finishes a poll operation. See g_file_poll_mountable() for details.
7268  *
7269  * Finish an asynchronous poll operation that was polled
7270  * with g_file_poll_mountable().
7271  *
7272  * Returns: %TRUE if the operation finished successfully. %FALSE
7273  * otherwise.
7274  *
7275  * Since: 2.22
7276  */
7277 gboolean
7278 g_file_poll_mountable_finish (GFile                      *file,
7279                               GAsyncResult               *result,
7280                               GError                    **error)
7281 {
7282   GFileIface *iface;
7283
7284   g_return_val_if_fail (G_IS_FILE (file), FALSE);
7285   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7286
7287   if (g_async_result_legacy_propagate_error (result, error))
7288     return FALSE;
7289
7290   iface = G_FILE_GET_IFACE (file);
7291   return (* iface->poll_mountable_finish) (file, result, error);
7292 }
7293
7294 /**
7295  * g_file_supports_thread_contexts:
7296  * @file: a #GFile.
7297  *
7298  * Checks if @file supports <link
7299  * linkend="g-main-context-push-thread-default-context">thread-default
7300  * contexts</link>. If this returns %FALSE, you cannot perform
7301  * asynchronous operations on @file in a thread that has a
7302  * thread-default context.
7303  *
7304  * Returns: Whether or not @file supports thread-default contexts.
7305  *
7306  * Since: 2.22
7307  */
7308 gboolean
7309 g_file_supports_thread_contexts (GFile *file)
7310 {
7311  GFileIface *iface;
7312
7313  g_return_val_if_fail (G_IS_FILE (file), FALSE);
7314
7315  iface = G_FILE_GET_IFACE (file);
7316  return iface->supports_thread_contexts;
7317 }