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