gio + inotify support for hard links
[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 "gtask.h"
40 #include "gfileattribute-priv.h"
41 #include "gfiledescriptorbased.h"
42 #include "gpollfilemonitor.h"
43 #include "gappinfo.h"
44 #include "gfileinputstream.h"
45 #include "gfileoutputstream.h"
46 #include "glocalfileoutputstream.h"
47 #include "glocalfileiostream.h"
48 #include "gcancellable.h"
49 #include "gasyncresult.h"
50 #include "gioerror.h"
51 #include "glibintl.h"
52
53
54 /**
55  * SECTION:gfile
56  * @short_description: File and Directory Handling
57  * @include: gio/gio.h
58  * @see_also: #GFileInfo, #GFileEnumerator
59  *
60  * #GFile is a high level abstraction for manipulating files on a
61  * virtual file system. #GFiles are lightweight, immutable objects
62  * that do no I/O upon creation. It is necessary to understand that
63  * #GFile objects do not represent files, merely an identifier for a
64  * file. All file content I/O is implemented as streaming operations
65  * (see #GInputStream and #GOutputStream).
66  *
67  * To construct a #GFile, you can use:
68  * <simplelist>
69  * <member>g_file_new_for_path() if you have a path.</member>
70  * <member>g_file_new_for_uri() if you have a URI.</member>
71  * <member>g_file_new_for_commandline_arg() for a command line argument.</member>
72  * <member>g_file_new_tmp() to create a temporary file from a template.</member>
73  * <member>g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().</member>
74  * </simplelist>
75  *
76  * One way to think of a #GFile is as an abstraction of a pathname. For
77  * normal files the system pathname is what is stored internally, but as
78  * #GFiles are extensible it could also be something else that corresponds
79  * to a pathname in a userspace implementation of a filesystem.
80  *
81  * #GFiles make up hierarchies of directories and files that correspond to
82  * the files on a filesystem. You can move through the file system with
83  * #GFile using g_file_get_parent() to get an identifier for the parent
84  * directory, g_file_get_child() to get a child within a directory,
85  * g_file_resolve_relative_path() to resolve a relative path between two
86  * #GFiles. There can be multiple hierarchies, so you may not end up at
87  * the same root if you repeatedly call g_file_get_parent() on two different
88  * files.
89  *
90  * All #GFiles have a basename (get with g_file_get_basename()). These names
91  * are byte strings that are used to identify the file on the filesystem
92  * (relative to its parent directory) and there is no guarantees that they
93  * have any particular charset encoding or even make any sense at all. If
94  * you want to use filenames in a user interface you should use the display
95  * name that you can get by requesting the
96  * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
97  * This is guaranteed to be in UTF-8 and can be used in a user interface.
98  * But always store the real basename or the #GFile to use to actually
99  * access the file, because there is no way to go from a display name to
100  * the actual name.
101  *
102  * Using #GFile as an identifier has the same weaknesses as using a path
103  * in that there may be multiple aliases for the same file. For instance,
104  * hard or soft links may cause two different #GFiles to refer to the same
105  * file. Other possible causes for aliases are: case insensitive filesystems,
106  * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
107  * check if two #GFiles point to the same file you can query for the
108  * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
109  * canonicalization of pathnames passed in, so that trivial differences in
110  * the path string used at creation (duplicated slashes, slash at end of
111  * path, "." or ".." path segments, etc) does not create different #GFiles.
112  *
113  * Many #GFile operations have both synchronous and asynchronous versions
114  * to suit your application. Asynchronous versions of synchronous functions
115  * simply have _async() appended to their function names. The asynchronous
116  * I/O functions call a #GAsyncReadyCallback which is then used to finalize
117  * the operation, producing a GAsyncResult which is then passed to the
118  * function's matching _finish() operation.
119  *
120  * Some #GFile operations do not have synchronous analogs, as they may
121  * take a very long time to finish, and blocking may leave an application
122  * unusable. Notable cases include:
123  * <simplelist>
124  * <member>g_file_mount_mountable() to mount a mountable file.</member>
125  * <member>g_file_unmount_mountable_with_operation() to unmount a mountable file.</member>
126  * <member>g_file_eject_mountable_with_operation() to eject a mountable file.</member>
127  * </simplelist>
128  *
129  * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
130  * One notable feature of #GFiles are entity tags, or "etags" for
131  * short. Entity tags are somewhat like a more abstract version of the
132  * traditional mtime, and can be used to quickly determine if the file has
133  * been modified from the version on the file system. See the HTTP 1.1
134  * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
135  * for HTTP Etag headers, which are a very similar concept.
136  * </para>
137  **/
138
139 static void               g_file_real_query_info_async            (GFile                  *file,
140                                                                    const char             *attributes,
141                                                                    GFileQueryInfoFlags     flags,
142                                                                    int                     io_priority,
143                                                                    GCancellable           *cancellable,
144                                                                    GAsyncReadyCallback     callback,
145                                                                    gpointer                user_data);
146 static GFileInfo *        g_file_real_query_info_finish           (GFile                  *file,
147                                                                    GAsyncResult           *res,
148                                                                    GError                **error);
149 static void               g_file_real_query_filesystem_info_async (GFile                  *file,
150                                                                    const char             *attributes,
151                                                                    int                     io_priority,
152                                                                    GCancellable           *cancellable,
153                                                                    GAsyncReadyCallback     callback,
154                                                                    gpointer                user_data);
155 static GFileInfo *        g_file_real_query_filesystem_info_finish (GFile                  *file,
156                                                                    GAsyncResult           *res,
157                                                                    GError                **error);
158 static void               g_file_real_enumerate_children_async    (GFile                  *file,
159                                                                    const char             *attributes,
160                                                                    GFileQueryInfoFlags     flags,
161                                                                    int                     io_priority,
162                                                                    GCancellable           *cancellable,
163                                                                    GAsyncReadyCallback     callback,
164                                                                    gpointer                user_data);
165 static GFileEnumerator *  g_file_real_enumerate_children_finish   (GFile                  *file,
166                                                                    GAsyncResult           *res,
167                                                                    GError                **error);
168 static void               g_file_real_read_async                  (GFile                  *file,
169                                                                    int                     io_priority,
170                                                                    GCancellable           *cancellable,
171                                                                    GAsyncReadyCallback     callback,
172                                                                    gpointer                user_data);
173 static GFileInputStream * g_file_real_read_finish                 (GFile                  *file,
174                                                                    GAsyncResult           *res,
175                                                                    GError                **error);
176 static void               g_file_real_append_to_async             (GFile                  *file,
177                                                                    GFileCreateFlags        flags,
178                                                                    int                     io_priority,
179                                                                    GCancellable           *cancellable,
180                                                                    GAsyncReadyCallback     callback,
181                                                                    gpointer                user_data);
182 static GFileOutputStream *g_file_real_append_to_finish            (GFile                  *file,
183                                                                    GAsyncResult           *res,
184                                                                    GError                **error);
185 static void               g_file_real_create_async                (GFile                  *file,
186                                                                    GFileCreateFlags        flags,
187                                                                    int                     io_priority,
188                                                                    GCancellable           *cancellable,
189                                                                    GAsyncReadyCallback     callback,
190                                                                    gpointer                user_data);
191 static GFileOutputStream *g_file_real_create_finish               (GFile                  *file,
192                                                                    GAsyncResult           *res,
193                                                                    GError                **error);
194 static void               g_file_real_replace_async               (GFile                  *file,
195                                                                    const char             *etag,
196                                                                    gboolean                make_backup,
197                                                                    GFileCreateFlags        flags,
198                                                                    int                     io_priority,
199                                                                    GCancellable           *cancellable,
200                                                                    GAsyncReadyCallback     callback,
201                                                                    gpointer                user_data);
202 static GFileOutputStream *g_file_real_replace_finish              (GFile                  *file,
203                                                                    GAsyncResult           *res,
204                                                                    GError                **error);
205 static void               g_file_real_delete_async                (GFile                  *file,
206                                                                    int                     io_priority,
207                                                                    GCancellable           *cancellable,
208                                                                    GAsyncReadyCallback     callback,
209                                                                    gpointer                user_data);
210 static gboolean           g_file_real_delete_finish               (GFile                  *file,
211                                                                    GAsyncResult           *res,
212                                                                    GError                **error);
213 static void               g_file_real_open_readwrite_async        (GFile                  *file,
214                                                                    int                  io_priority,
215                                                                    GCancellable           *cancellable,
216                                                                    GAsyncReadyCallback     callback,
217                                                                    gpointer                user_data);
218 static GFileIOStream *    g_file_real_open_readwrite_finish       (GFile                  *file,
219                                                                    GAsyncResult           *res,
220                                                                    GError                **error);
221 static void               g_file_real_create_readwrite_async      (GFile                  *file,
222                                                                    GFileCreateFlags        flags,
223                                                                    int                     io_priority,
224                                                                    GCancellable           *cancellable,
225                                                                    GAsyncReadyCallback     callback,
226                                                                    gpointer                user_data);
227 static GFileIOStream *    g_file_real_create_readwrite_finish     (GFile                  *file,
228                                                                    GAsyncResult           *res,
229                                                                    GError                **error);
230 static void               g_file_real_replace_readwrite_async     (GFile                  *file,
231                                                                    const char             *etag,
232                                                                    gboolean                make_backup,
233                                                                    GFileCreateFlags        flags,
234                                                                    int                     io_priority,
235                                                                    GCancellable           *cancellable,
236                                                                    GAsyncReadyCallback     callback,
237                                                                    gpointer                user_data);
238 static GFileIOStream *    g_file_real_replace_readwrite_finish    (GFile                  *file,
239                                                                   GAsyncResult            *res,
240                                                                   GError                 **error);
241 static gboolean           g_file_real_set_attributes_from_info    (GFile                  *file,
242                                                                    GFileInfo              *info,
243                                                                    GFileQueryInfoFlags     flags,
244                                                                    GCancellable           *cancellable,
245                                                                    GError                **error);
246 static void               g_file_real_set_display_name_async      (GFile                  *file,
247                                                                    const char             *display_name,
248                                                                    int                     io_priority,
249                                                                    GCancellable           *cancellable,
250                                                                    GAsyncReadyCallback     callback,
251                                                                    gpointer                user_data);
252 static GFile *            g_file_real_set_display_name_finish     (GFile                  *file,
253                                                                    GAsyncResult           *res,
254                                                                    GError                **error);
255 static void               g_file_real_set_attributes_async        (GFile                  *file,
256                                                                    GFileInfo              *info,
257                                                                    GFileQueryInfoFlags     flags,
258                                                                    int                     io_priority,
259                                                                    GCancellable           *cancellable,
260                                                                    GAsyncReadyCallback     callback,
261                                                                    gpointer                user_data);
262 static gboolean           g_file_real_set_attributes_finish       (GFile                  *file,
263                                                                    GAsyncResult           *res,
264                                                                    GFileInfo             **info,
265                                                                    GError                **error);
266 static void               g_file_real_find_enclosing_mount_async  (GFile                  *file,
267                                                                    int                     io_priority,
268                                                                    GCancellable           *cancellable,
269                                                                    GAsyncReadyCallback     callback,
270                                                                    gpointer                user_data);
271 static GMount *           g_file_real_find_enclosing_mount_finish (GFile                  *file,
272                                                                    GAsyncResult           *res,
273                                                                    GError                **error);
274 static void               g_file_real_copy_async                  (GFile                  *source,
275                                                                    GFile                  *destination,
276                                                                    GFileCopyFlags          flags,
277                                                                    int                     io_priority,
278                                                                    GCancellable           *cancellable,
279                                                                    GFileProgressCallback   progress_callback,
280                                                                    gpointer                progress_callback_data,
281                                                                    GAsyncReadyCallback     callback,
282                                                                    gpointer                user_data);
283 static gboolean           g_file_real_copy_finish                 (GFile                  *file,
284                                                                    GAsyncResult           *res,
285                                                                    GError                **error);
286
287 typedef GFileIface GFileInterface;
288 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
289
290 static void
291 g_file_default_init (GFileIface *iface)
292 {
293   iface->enumerate_children_async = g_file_real_enumerate_children_async;
294   iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
295   iface->set_display_name_async = g_file_real_set_display_name_async;
296   iface->set_display_name_finish = g_file_real_set_display_name_finish;
297   iface->query_info_async = g_file_real_query_info_async;
298   iface->query_info_finish = g_file_real_query_info_finish;
299   iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
300   iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
301   iface->set_attributes_async = g_file_real_set_attributes_async;
302   iface->set_attributes_finish = g_file_real_set_attributes_finish;
303   iface->read_async = g_file_real_read_async;
304   iface->read_finish = g_file_real_read_finish;
305   iface->append_to_async = g_file_real_append_to_async;
306   iface->append_to_finish = g_file_real_append_to_finish;
307   iface->create_async = g_file_real_create_async;
308   iface->create_finish = g_file_real_create_finish;
309   iface->replace_async = g_file_real_replace_async;
310   iface->replace_finish = g_file_real_replace_finish;
311   iface->delete_file_async = g_file_real_delete_async;
312   iface->delete_file_finish = g_file_real_delete_finish;
313   iface->open_readwrite_async = g_file_real_open_readwrite_async;
314   iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
315   iface->create_readwrite_async = g_file_real_create_readwrite_async;
316   iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
317   iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
318   iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
319   iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
320   iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
321   iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
322   iface->copy_async = g_file_real_copy_async;
323   iface->copy_finish = g_file_real_copy_finish;
324 }
325
326
327 /**
328  * g_file_is_native:
329  * @file: input #GFile
330  *
331  * Checks to see if a file is native to the platform.
332  *
333  * A native file s one expressed in the platform-native filename format,
334  * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
335  * as it might be on a locally mounted remote filesystem.
336  *
337  * On some systems non-native files may be available using the native
338  * filesystem via a userspace filesystem (FUSE), in these cases this call
339  * will return %FALSE, but g_file_get_path() will still return a native path.
340  *
341  * This call does no blocking I/O.
342  *
343  * Returns: %TRUE if @file is native
344  */
345 gboolean
346 g_file_is_native (GFile *file)
347 {
348   GFileIface *iface;
349
350   g_return_val_if_fail (G_IS_FILE (file), FALSE);
351
352   iface = G_FILE_GET_IFACE (file);
353
354   return (* iface->is_native) (file);
355 }
356
357
358 /**
359  * g_file_has_uri_scheme:
360  * @file: input #GFile
361  * @uri_scheme: a string containing a URI scheme
362  *
363  * Checks to see if a #GFile has a given URI scheme.
364  *
365  * This call does no blocking I/O.
366  *
367  * Returns: %TRUE if #GFile's backend supports the
368  *     given URI scheme, %FALSE if URI scheme is %NULL,
369  *     not supported, or #GFile is invalid.
370  */
371 gboolean
372 g_file_has_uri_scheme (GFile      *file,
373                        const char *uri_scheme)
374 {
375   GFileIface *iface;
376
377   g_return_val_if_fail (G_IS_FILE (file), FALSE);
378   g_return_val_if_fail (uri_scheme != NULL, FALSE);
379
380   iface = G_FILE_GET_IFACE (file);
381
382   return (* iface->has_uri_scheme) (file, uri_scheme);
383 }
384
385
386 /**
387  * g_file_get_uri_scheme:
388  * @file: input #GFile
389  *
390  * Gets the URI scheme for a #GFile.
391  * RFC 3986 decodes the scheme as:
392  * <programlisting>
393  * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
394  * </programlisting>
395  * Common schemes include "file", "http", "ftp", etc.
396  *
397  * This call does no blocking I/O.
398  *
399  * Returns: a string containing the URI scheme for the given
400  *     #GFile. The returned string should be freed with g_free()
401  *     when no longer needed.
402  */
403 char *
404 g_file_get_uri_scheme (GFile *file)
405 {
406   GFileIface *iface;
407
408   g_return_val_if_fail (G_IS_FILE (file), NULL);
409
410   iface = G_FILE_GET_IFACE (file);
411
412   return (* iface->get_uri_scheme) (file);
413 }
414
415
416 /**
417  * g_file_get_basename:
418  * @file: input #GFile
419  *
420  * Gets the base name (the last component of the path) for a given #GFile.
421  *
422  * If called for the top level of a system (such as the filesystem root
423  * or a uri like sftp://host/) it will return a single directory separator
424  * (and on Windows, possibly a drive letter).
425  *
426  * The base name is a byte string (not UTF-8). It has no defined encoding
427  * or rules other than it may not contain zero bytes.  If you want to use
428  * filenames in a user interface you should use the display name that you
429  * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
430  * attribute with g_file_query_info().
431  *
432  * This call does no blocking I/O.
433  *
434  * Returns: string containing the #GFile's base name, or %NULL
435  *     if given #GFile is invalid. The returned string should be
436  *     freed with g_free() when no longer needed.
437  */
438 char *
439 g_file_get_basename (GFile *file)
440 {
441   GFileIface *iface;
442
443   g_return_val_if_fail (G_IS_FILE (file), NULL);
444
445   iface = G_FILE_GET_IFACE (file);
446
447   return (* iface->get_basename) (file);
448 }
449
450 /**
451  * g_file_get_path:
452  * @file: input #GFile
453  *
454  * Gets the local pathname for #GFile, if one exists.
455  *
456  * This call does no blocking I/O.
457  *
458  * Returns: string containing the #GFile's path, or %NULL if
459  *     no such path exists. The returned string should be
460  *     freed with g_free() when no longer needed.
461  */
462 char *
463 g_file_get_path (GFile *file)
464 {
465   GFileIface *iface;
466
467   g_return_val_if_fail (G_IS_FILE (file), NULL);
468
469   iface = G_FILE_GET_IFACE (file);
470
471   return (* iface->get_path) (file);
472 }
473
474 /**
475  * g_file_get_uri:
476  * @file: input #GFile
477  *
478  * Gets the URI for the @file.
479  *
480  * This call does no blocking I/O.
481  *
482  * Returns: a string containing the #GFile's URI.
483  *     The returned string should be freed with g_free()
484  *     when no longer needed.
485  */
486 char *
487 g_file_get_uri (GFile *file)
488 {
489   GFileIface *iface;
490
491   g_return_val_if_fail (G_IS_FILE (file), NULL);
492
493   iface = G_FILE_GET_IFACE (file);
494
495   return (* iface->get_uri) (file);
496 }
497
498 /**
499  * g_file_get_parse_name:
500  * @file: input #GFile
501  *
502  * Gets the parse name of the @file.
503  * A parse name is a UTF-8 string that describes the
504  * file such that one can get the #GFile back using
505  * g_file_parse_name().
506  *
507  * This is generally used to show the #GFile as a nice
508  * full-pathname kind of string in a user interface,
509  * like in a location entry.
510  *
511  * For local files with names that can safely be converted
512  * to UTF-8 the pathname is used, otherwise the IRI is used
513  * (a form of URI that allows UTF-8 characters unescaped).
514  *
515  * This call does no blocking I/O.
516  *
517  * Returns: a string containing the #GFile's parse name.
518  *     The returned string should be freed with g_free()
519  *     when no longer needed.
520  */
521 char *
522 g_file_get_parse_name (GFile *file)
523 {
524   GFileIface *iface;
525
526   g_return_val_if_fail (G_IS_FILE (file), NULL);
527
528   iface = G_FILE_GET_IFACE (file);
529
530   return (* iface->get_parse_name) (file);
531 }
532
533 /**
534  * g_file_dup:
535  * @file: input #GFile
536  *
537  * Duplicates a #GFile handle. This operation does not duplicate
538  * the actual file or directory represented by the #GFile; see
539  * g_file_copy() if attempting to copy a file.
540  *
541  * This call does no blocking I/O.
542  *
543  * Returns: (transfer full): a new #GFile that is a duplicate
544  *     of the given #GFile.
545  */
546 GFile *
547 g_file_dup (GFile *file)
548 {
549   GFileIface *iface;
550
551   g_return_val_if_fail (G_IS_FILE (file), NULL);
552
553   iface = G_FILE_GET_IFACE (file);
554
555   return (* iface->dup) (file);
556 }
557
558 /**
559  * g_file_hash:
560  * @file: (type GFile): #gconstpointer to a #GFile
561  *
562  * Creates a hash value for a #GFile.
563  *
564  * This call does no blocking I/O.
565  *
566  * Virtual: hash
567  * Returns: 0 if @file is not a valid #GFile, otherwise an
568  *     integer that can be used as hash value for the #GFile.
569  *     This function is intended for easily hashing a #GFile to
570  *     add to a #GHashTable or similar data structure.
571  */
572 guint
573 g_file_hash (gconstpointer file)
574 {
575   GFileIface *iface;
576
577   g_return_val_if_fail (G_IS_FILE (file), 0);
578
579   iface = G_FILE_GET_IFACE (file);
580
581   return (* iface->hash) ((GFile *)file);
582 }
583
584 /**
585  * g_file_equal:
586  * @file1: the first #GFile
587  * @file2: the second #GFile
588  *
589  * Checks equality of two given #GFiles.
590  *
591  * Note that two #GFiles that differ can still refer to the same
592  * file on the filesystem due to various forms of filename
593  * aliasing.
594  *
595  * This call does no blocking I/O.
596  *
597  * Returns: %TRUE if @file1 and @file2 are equal.
598  *     %FALSE if either is not a #GFile.
599  */
600 gboolean
601 g_file_equal (GFile *file1,
602               GFile *file2)
603 {
604   GFileIface *iface;
605
606   g_return_val_if_fail (G_IS_FILE (file1), FALSE);
607   g_return_val_if_fail (G_IS_FILE (file2), FALSE);
608
609   if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
610     return FALSE;
611
612   iface = G_FILE_GET_IFACE (file1);
613
614   return (* iface->equal) (file1, file2);
615 }
616
617
618 /**
619  * g_file_get_parent:
620  * @file: input #GFile
621  *
622  * Gets the parent directory for the @file.
623  * If the @file represents the root directory of the
624  * file system, then %NULL will be returned.
625  *
626  * This call does no blocking I/O.
627  *
628  * Returns: (transfer full): a #GFile structure to the
629  *     parent of the given #GFile or %NULL if there is
630  *     no parent. Free the returned object with g_object_unref().
631  */
632 GFile *
633 g_file_get_parent (GFile *file)
634 {
635   GFileIface *iface;
636
637   g_return_val_if_fail (G_IS_FILE (file), NULL);
638
639   iface = G_FILE_GET_IFACE (file);
640
641   return (* iface->get_parent) (file);
642 }
643
644 /**
645  * g_file_has_parent:
646  * @file: input #GFile
647  * @parent: (allow-none): the parent to check for, or %NULL
648  *
649  * Checks if @file has a parent, and optionally, if it is @parent.
650  *
651  * If @parent is %NULL then this function returns %TRUE if @file has any
652  * parent at all.  If @parent is non-%NULL then %TRUE is only returned
653  * if @file is a child of @parent.
654  *
655  * Returns: %TRUE if @file is a child of @parent (or any parent in the
656  *          case that @parent is %NULL).
657  *
658  * Since: 2.24
659  */
660 gboolean
661 g_file_has_parent (GFile *file,
662                    GFile *parent)
663 {
664   GFile *actual_parent;
665   gboolean result;
666
667   g_return_val_if_fail (G_IS_FILE (file), FALSE);
668   g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
669
670   actual_parent = g_file_get_parent (file);
671
672   if (actual_parent != NULL)
673     {
674       if (parent != NULL)
675         result = g_file_equal (parent, actual_parent);
676       else
677         result = TRUE;
678
679       g_object_unref (actual_parent);
680     }
681   else
682     result = FALSE;
683
684   return result;
685 }
686
687 /**
688  * g_file_get_child:
689  * @file: input #GFile
690  * @name: string containing the child's basename
691  *
692  * Gets a child of @file with basename equal to @name.
693  *
694  * Note that the file with that specific name might not exist, but
695  * you can still have a #GFile that points to it. You can use this
696  * for instance to create that file.
697  *
698  * This call does no blocking I/O.
699  *
700  * Returns: (transfer full): a #GFile to a child specified by @name.
701  *     Free the returned object with g_object_unref().
702  */
703 GFile *
704 g_file_get_child (GFile      *file,
705                   const char *name)
706 {
707   g_return_val_if_fail (G_IS_FILE (file), NULL);
708   g_return_val_if_fail (name != NULL, NULL);
709
710   return g_file_resolve_relative_path (file, name);
711 }
712
713 /**
714  * g_file_get_child_for_display_name:
715  * @file: input #GFile
716  * @display_name: string to a possible child
717  * @error: return location for an error
718  *
719  * Gets the child of @file for a given @display_name (i.e. a UTF-8
720  * version of the name). If this function fails, it returns %NULL
721  * and @error will be set. This is very useful when constructing a
722  * #GFile for a new file and the user entered the filename in the
723  * user interface, for instance when you select a directory and
724  * type a filename in the file selector.
725  *
726  * This call does no blocking I/O.
727  *
728  * Returns: (transfer full): a #GFile to the specified child, or
729  *     %NULL if the display name couldn't be converted.
730  *     Free the returned object with g_object_unref().
731  */
732 GFile *
733 g_file_get_child_for_display_name (GFile      *file,
734                                    const char *display_name,
735                                    GError **error)
736 {
737   GFileIface *iface;
738
739   g_return_val_if_fail (G_IS_FILE (file), NULL);
740   g_return_val_if_fail (display_name != NULL, NULL);
741
742   iface = G_FILE_GET_IFACE (file);
743
744   return (* iface->get_child_for_display_name) (file, display_name, error);
745 }
746
747 /**
748  * g_file_has_prefix:
749  * @file: input #GFile
750  * @prefix: input #GFile
751  *
752  * Checks whether @file has the prefix specified by @prefix.
753  *
754  * In other words, if the names of initial elements of @file's
755  * pathname match @prefix. Only full pathname elements are matched,
756  * so a path like /foo is not considered a prefix of /foobar, only
757  * of /foo/bar.
758  *
759  * This call does no I/O, as it works purely on names. As such it can
760  * sometimes return %FALSE even if @file is inside a @prefix (from a
761  * filesystem point of view), because the prefix of @file is an alias
762  * of @prefix.
763  *
764  * Virtual: prefix_matches
765  * Returns:  %TRUE if the @files's parent, grandparent, etc is @prefix,
766  *     %FALSE otherwise.
767  */
768 gboolean
769 g_file_has_prefix (GFile *file,
770                    GFile *prefix)
771 {
772   GFileIface *iface;
773
774   g_return_val_if_fail (G_IS_FILE (file), FALSE);
775   g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
776
777   if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
778     return FALSE;
779
780   iface = G_FILE_GET_IFACE (file);
781
782   /* The vtable function differs in arg order since
783    * we're using the old contains_file call
784    */
785   return (* iface->prefix_matches) (prefix, file);
786 }
787
788 /**
789  * g_file_get_relative_path:
790  * @parent: input #GFile
791  * @descendant: input #GFile
792  *
793  * Gets the path for @descendant relative to @parent.
794  *
795  * This call does no blocking I/O.
796  *
797  * Returns: string with the relative path from @descendant
798  *     to @parent, or %NULL if @descendant doesn't have @parent
799  *     as prefix. The returned string should be freed with g_free()
800  *     when no longer needed.
801  */
802 char *
803 g_file_get_relative_path (GFile *parent,
804                           GFile *descendant)
805 {
806   GFileIface *iface;
807
808   g_return_val_if_fail (G_IS_FILE (parent), NULL);
809   g_return_val_if_fail (G_IS_FILE (descendant), NULL);
810
811   if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
812     return NULL;
813
814   iface = G_FILE_GET_IFACE (parent);
815
816   return (* iface->get_relative_path) (parent, descendant);
817 }
818
819 /**
820  * g_file_resolve_relative_path:
821  * @file: input #GFile
822  * @relative_path: a given relative path string
823  *
824  * Resolves a relative path for @file to an absolute path.
825  *
826  * This call does no blocking I/O.
827  *
828  * Returns: (transfer full): #GFile to the resolved path.
829  *     %NULL if @relative_path is %NULL or if @file is invalid.
830  *     Free the returned object with g_object_unref().
831  */
832 GFile *
833 g_file_resolve_relative_path (GFile      *file,
834                               const char *relative_path)
835 {
836   GFileIface *iface;
837
838   g_return_val_if_fail (G_IS_FILE (file), NULL);
839   g_return_val_if_fail (relative_path != NULL, NULL);
840
841   iface = G_FILE_GET_IFACE (file);
842
843   return (* iface->resolve_relative_path) (file, relative_path);
844 }
845
846 /**
847  * g_file_enumerate_children:
848  * @file: input #GFile
849  * @attributes: an attribute query string
850  * @flags: a set of #GFileQueryInfoFlags
851  * @cancellable: (allow-none): optional #GCancellable object,
852  *     %NULL to ignore
853  * @error: #GError for error reporting
854  *
855  * Gets the requested information about the files in a directory.
856  * The result is a #GFileEnumerator object that will give out
857  * #GFileInfo objects for all the files in the directory.
858  *
859  * The @attributes value is a string that specifies the file
860  * attributes that should be gathered. It is not an error if
861  * it's not possible to read a particular requested attribute
862  * from a file - it just won't be set. @attributes should
863  * be a comma-separated list of attributes or attribute wildcards.
864  * The wildcard "*" means all attributes, and a wildcard like
865  * "standard::*" means all attributes in the standard namespace.
866  * An example attribute query be "standard::*,owner::user".
867  * The standard attributes are available as defines, like
868  * #G_FILE_ATTRIBUTE_STANDARD_NAME.
869  *
870  * If @cancellable is not %NULL, then the operation can be cancelled
871  * by triggering the cancellable object from another thread. If the
872  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
873  * returned.
874  *
875  * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
876  * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
877  * error will be returned. Other errors are possible too.
878  *
879  * Returns: (transfer full): A #GFileEnumerator if successful,
880  *     %NULL on error. Free the returned object with g_object_unref().
881  */
882 GFileEnumerator *
883 g_file_enumerate_children (GFile                *file,
884                            const char           *attributes,
885                            GFileQueryInfoFlags   flags,
886                            GCancellable         *cancellable,
887                            GError              **error)
888 {
889   GFileIface *iface;
890
891   g_return_val_if_fail (G_IS_FILE (file), NULL);
892
893   if (g_cancellable_set_error_if_cancelled (cancellable, error))
894     return NULL;
895
896   iface = G_FILE_GET_IFACE (file);
897
898   if (iface->enumerate_children == NULL)
899     {
900       g_set_error_literal (error, G_IO_ERROR,
901                            G_IO_ERROR_NOT_SUPPORTED,
902                            _("Operation not supported"));
903       return NULL;
904     }
905
906   return (* iface->enumerate_children) (file, attributes, flags,
907                                         cancellable, error);
908 }
909
910 /**
911  * g_file_enumerate_children_async:
912  * @file: input #GFile
913  * @attributes: an attribute query string
914  * @flags: a set of #GFileQueryInfoFlags
915  * @io_priority: the <link linkend="io-priority">I/O priority</link>
916  *     of the request
917  * @cancellable: (allow-none): optional #GCancellable object,
918  *     %NULL to ignore
919  * @callback: (scope async): a #GAsyncReadyCallback to call when the
920  *     request is satisfied
921  * @user_data: (closure): the data to pass to callback function
922  *
923  * Asynchronously gets the requested information about the files
924  * in a directory. The result is a #GFileEnumerator object that will
925  * give out #GFileInfo objects for all the files in the directory.
926  *
927  * For more details, see g_file_enumerate_children() which is
928  * the synchronous version of this call.
929  *
930  * When the operation is finished, @callback will be called. You can
931  * then call g_file_enumerate_children_finish() to get the result of
932  * the operation.
933  */
934 void
935 g_file_enumerate_children_async (GFile               *file,
936                                  const char          *attributes,
937                                  GFileQueryInfoFlags  flags,
938                                  int                  io_priority,
939                                  GCancellable        *cancellable,
940                                  GAsyncReadyCallback  callback,
941                                  gpointer             user_data)
942 {
943   GFileIface *iface;
944
945   g_return_if_fail (G_IS_FILE (file));
946
947   iface = G_FILE_GET_IFACE (file);
948   (* iface->enumerate_children_async) (file,
949                                        attributes,
950                                        flags,
951                                        io_priority,
952                                        cancellable,
953                                        callback,
954                                        user_data);
955 }
956
957 /**
958  * g_file_enumerate_children_finish:
959  * @file: input #GFile
960  * @res: a #GAsyncResult
961  * @error: a #GError
962  *
963  * Finishes an async enumerate children operation.
964  * See g_file_enumerate_children_async().
965  *
966  * Returns: (transfer full): a #GFileEnumerator or %NULL
967  *     if an error occurred.
968  *     Free the returned object with g_object_unref().
969  */
970 GFileEnumerator *
971 g_file_enumerate_children_finish (GFile         *file,
972                                   GAsyncResult  *res,
973                                   GError       **error)
974 {
975   GFileIface *iface;
976
977   g_return_val_if_fail (G_IS_FILE (file), NULL);
978   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
979
980   if (g_async_result_legacy_propagate_error (res, error))
981     return NULL;
982
983   iface = G_FILE_GET_IFACE (file);
984   return (* iface->enumerate_children_finish) (file, res, error);
985 }
986
987 /**
988  * g_file_query_exists:
989  * @file: input #GFile
990  * @cancellable: (allow-none): optional #GCancellable object,
991  *     %NULL to ignore
992  *
993  * Utility function to check if a particular file exists. This is
994  * implemented using g_file_query_info() and as such does blocking I/O.
995  *
996  * Note that in many cases it is racy to first check for file existence
997  * and then execute something based on the outcome of that, because the
998  * file might have been created or removed in between the operations. The
999  * general approach to handling that is to not check, but just do the
1000  * operation and handle the errors as they come.
1001  *
1002  * As an example of race-free checking, take the case of reading a file,
1003  * and if it doesn't exist, creating it. There are two racy versions: read
1004  * it, and on error create it; and: check if it exists, if not create it.
1005  * These can both result in two processes creating the file (with perhaps
1006  * a partially written file as the result). The correct approach is to
1007  * always try to create the file with g_file_create() which will either
1008  * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1009  *
1010  * However, in many cases an existence check is useful in a user interface,
1011  * for instance to make a menu item sensitive/insensitive, so that you don't
1012  * have to fool users that something is possible and then just show an error
1013  * dialog. If you do this, you should make sure to also handle the errors
1014  * that can happen due to races when you execute the operation.
1015  *
1016  * Returns: %TRUE if the file exists (and can be detected without error),
1017  *     %FALSE otherwise (or if cancelled).
1018  */
1019 gboolean
1020 g_file_query_exists (GFile        *file,
1021                      GCancellable *cancellable)
1022 {
1023   GFileInfo *info;
1024
1025   g_return_val_if_fail (G_IS_FILE(file), FALSE);
1026
1027   info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1028                             G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1029   if (info != NULL)
1030     {
1031       g_object_unref (info);
1032       return TRUE;
1033     }
1034
1035   return FALSE;
1036 }
1037
1038 /**
1039  * g_file_query_file_type:
1040  * @file: input #GFile
1041  * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1042  * @cancellable: (allow-none): optional #GCancellable object,
1043  *     %NULL to ignore
1044  *
1045  * Utility function to inspect the #GFileType of a file. This is
1046  * implemented using g_file_query_info() and as such does blocking I/O.
1047  *
1048  * The primary use case of this method is to check if a file is
1049  * a regular file, directory, or symlink.
1050  *
1051  * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1052  *     if the file does not exist
1053  *
1054  * Since: 2.18
1055  */
1056 GFileType
1057 g_file_query_file_type (GFile               *file,
1058                         GFileQueryInfoFlags  flags,
1059                         GCancellable        *cancellable)
1060 {
1061   GFileInfo *info;
1062   GFileType file_type;
1063
1064   g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1065   info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1066                             cancellable, NULL);
1067   if (info != NULL)
1068     {
1069       file_type = g_file_info_get_file_type (info);
1070       g_object_unref (info);
1071     }
1072   else
1073     file_type = G_FILE_TYPE_UNKNOWN;
1074
1075   return file_type;
1076 }
1077
1078 /**
1079  * g_file_query_info:
1080  * @file: input #GFile
1081  * @attributes: an attribute query string
1082  * @flags: a set of #GFileQueryInfoFlags
1083  * @cancellable: (allow-none): optional #GCancellable object,
1084  *     %NULL to ignore
1085  * @error: a #GError
1086  *
1087  * Gets the requested information about specified @file.
1088  * The result is a #GFileInfo object that contains key-value
1089  * attributes (such as the type or size of the file).
1090  *
1091  * The @attributes value is a string that specifies the file
1092  * attributes that should be gathered. It is not an error if
1093  * it's not possible to read a particular requested attribute
1094  * from a file - it just won't be set. @attributes should be a
1095  * comma-separated list of attributes or attribute wildcards.
1096  * The wildcard "*" means all attributes, and a wildcard like
1097  * "standard::*" means all attributes in the standard namespace.
1098  * An example attribute query be "standard::*,owner::user".
1099  * The standard attributes are available as defines, like
1100  * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1101  *
1102  * If @cancellable is not %NULL, then the operation can be cancelled
1103  * by triggering the cancellable object from another thread. If the
1104  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1105  * returned.
1106  *
1107  * For symlinks, normally the information about the target of the
1108  * symlink is returned, rather than information about the symlink
1109  * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1110  * in @flags the information about the symlink itself will be returned.
1111  * Also, for symlinks that point to non-existing files the information
1112  * about the symlink itself will be returned.
1113  *
1114  * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1115  * returned. Other errors are possible too, and depend on what kind of
1116  * filesystem the file is on.
1117  *
1118  * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1119  *     on error. Free the returned object with g_object_unref().
1120  */
1121 GFileInfo *
1122 g_file_query_info (GFile                *file,
1123                    const char           *attributes,
1124                    GFileQueryInfoFlags   flags,
1125                    GCancellable         *cancellable,
1126                    GError              **error)
1127 {
1128   GFileIface *iface;
1129
1130   g_return_val_if_fail (G_IS_FILE (file), NULL);
1131
1132   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1133     return NULL;
1134
1135   iface = G_FILE_GET_IFACE (file);
1136
1137   if (iface->query_info == NULL)
1138     {
1139       g_set_error_literal (error, G_IO_ERROR,
1140                            G_IO_ERROR_NOT_SUPPORTED,
1141                            _("Operation not supported"));
1142       return NULL;
1143     }
1144
1145   return (* iface->query_info) (file, attributes, flags, cancellable, error);
1146 }
1147
1148 /**
1149  * g_file_query_info_async:
1150  * @file: input #GFile
1151  * @attributes: an attribute query string
1152  * @flags: a set of #GFileQueryInfoFlags
1153  * @io_priority: the <link linkend="io-priority">I/O priority</link>
1154  *     of the request
1155  * @cancellable: (allow-none): optional #GCancellable object,
1156  *     %NULL to ignore
1157  * @callback: (scope async): a #GAsyncReadyCallback to call when the
1158  *     request is satisfied
1159  * @user_data: (closure): the data to pass to callback function
1160  *
1161  * Asynchronously gets the requested information about specified @file.
1162  * The result is a #GFileInfo object that contains key-value attributes
1163  * (such as type or size for the file).
1164  *
1165  * For more details, see g_file_query_info() which is the synchronous
1166  * version of this call.
1167  *
1168  * When the operation is finished, @callback will be called. You can
1169  * then call g_file_query_info_finish() to get the result of the operation.
1170  */
1171 void
1172 g_file_query_info_async (GFile               *file,
1173                          const char          *attributes,
1174                          GFileQueryInfoFlags  flags,
1175                          int                  io_priority,
1176                          GCancellable        *cancellable,
1177                          GAsyncReadyCallback  callback,
1178                          gpointer             user_data)
1179 {
1180   GFileIface *iface;
1181
1182   g_return_if_fail (G_IS_FILE (file));
1183
1184   iface = G_FILE_GET_IFACE (file);
1185   (* iface->query_info_async) (file,
1186                                attributes,
1187                                flags,
1188                                io_priority,
1189                                cancellable,
1190                                callback,
1191                                user_data);
1192 }
1193
1194 /**
1195  * g_file_query_info_finish:
1196  * @file: input #GFile
1197  * @res: a #GAsyncResult
1198  * @error: a #GError
1199  *
1200  * Finishes an asynchronous file info query.
1201  * See g_file_query_info_async().
1202  *
1203  * Returns: (transfer full): #GFileInfo for given @file
1204  *     or %NULL on error. Free the returned object with
1205  *     g_object_unref().
1206  */
1207 GFileInfo *
1208 g_file_query_info_finish (GFile         *file,
1209                           GAsyncResult  *res,
1210                           GError       **error)
1211 {
1212   GFileIface *iface;
1213
1214   g_return_val_if_fail (G_IS_FILE (file), NULL);
1215   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1216
1217   if (g_async_result_legacy_propagate_error (res, error))
1218     return NULL;
1219
1220   iface = G_FILE_GET_IFACE (file);
1221   return (* iface->query_info_finish) (file, res, error);
1222 }
1223
1224 /**
1225  * g_file_query_filesystem_info:
1226  * @file: input #GFile
1227  * @attributes:  an attribute query string
1228  * @cancellable: (allow-none): optional #GCancellable object,
1229  *     %NULL to ignore
1230  * @error: a #GError
1231  *
1232  * Similar to g_file_query_info(), but obtains information
1233  * about the filesystem the @file is on, rather than the file itself.
1234  * For instance the amount of space available and the type of
1235  * the filesystem.
1236  *
1237  * The @attributes value is a string that specifies the attributes
1238  * that should be gathered. It is not an error if it's not possible
1239  * to read a particular requested attribute from a file - it just
1240  * won't be set. @attributes should be a comma-separated list of
1241  * attributes or attribute wildcards. The wildcard "*" means all
1242  * attributes, and a wildcard like "filesystem::*" means all attributes
1243  * in the filesystem namespace. The standard namespace for filesystem
1244  * attributes is "filesystem". Common attributes of interest are
1245  * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1246  * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1247  * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1248  *
1249  * If @cancellable is not %NULL, then the operation can be cancelled
1250  * by triggering the cancellable object from another thread. If the
1251  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1252  * returned.
1253  *
1254  * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1255  * be returned. Other errors are possible too, and depend on what
1256  * kind of filesystem the file is on.
1257  *
1258  * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1259  *     Free the returned object with g_object_unref().
1260  */
1261 GFileInfo *
1262 g_file_query_filesystem_info (GFile         *file,
1263                               const char    *attributes,
1264                               GCancellable  *cancellable,
1265                               GError       **error)
1266 {
1267   GFileIface *iface;
1268
1269   g_return_val_if_fail (G_IS_FILE (file), NULL);
1270
1271   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1272     return NULL;
1273
1274   iface = G_FILE_GET_IFACE (file);
1275
1276   if (iface->query_filesystem_info == NULL)
1277     {
1278       g_set_error_literal (error, G_IO_ERROR,
1279                            G_IO_ERROR_NOT_SUPPORTED,
1280                            _("Operation not supported"));
1281       return NULL;
1282     }
1283
1284   return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1285 }
1286
1287 /**
1288  * g_file_query_filesystem_info_async:
1289  * @file: input #GFile
1290  * @attributes: an attribute query string
1291  * @io_priority: the <link linkend="io-priority">I/O priority</link>
1292  *     of the request
1293  * @cancellable: (allow-none): optional #GCancellable object,
1294  *     %NULL to ignore
1295  * @callback: (scope async): a #GAsyncReadyCallback to call
1296  *     when the request is satisfied
1297  * @user_data: (closure): the data to pass to callback function
1298  *
1299  * Asynchronously gets the requested information about the filesystem
1300  * that the specified @file is on. The result is a #GFileInfo object
1301  * that contains key-value attributes (such as type or size for the
1302  * file).
1303  *
1304  * For more details, see g_file_query_filesystem_info() which is the
1305  * synchronous version of this call.
1306  *
1307  * When the operation is finished, @callback will be called. You can
1308  * then call g_file_query_info_finish() to get the result of the
1309  * operation.
1310  */
1311 void
1312 g_file_query_filesystem_info_async (GFile               *file,
1313                                     const char          *attributes,
1314                                     int                  io_priority,
1315                                     GCancellable        *cancellable,
1316                                     GAsyncReadyCallback  callback,
1317                                     gpointer             user_data)
1318 {
1319   GFileIface *iface;
1320
1321   g_return_if_fail (G_IS_FILE (file));
1322
1323   iface = G_FILE_GET_IFACE (file);
1324   (* iface->query_filesystem_info_async) (file,
1325                                           attributes,
1326                                           io_priority,
1327                                           cancellable,
1328                                           callback,
1329                                           user_data);
1330 }
1331
1332 /**
1333  * g_file_query_filesystem_info_finish:
1334  * @file: input #GFile
1335  * @res: a #GAsyncResult
1336  * @error: a #GError
1337  *
1338  * Finishes an asynchronous filesystem info query.
1339  * See g_file_query_filesystem_info_async().
1340  *
1341  * Returns: (transfer full): #GFileInfo for given @file
1342  *     or %NULL on error.
1343  *     Free the returned object with g_object_unref().
1344  */
1345 GFileInfo *
1346 g_file_query_filesystem_info_finish (GFile         *file,
1347                                      GAsyncResult  *res,
1348                                      GError       **error)
1349 {
1350   GFileIface *iface;
1351
1352   g_return_val_if_fail (G_IS_FILE (file), NULL);
1353   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1354
1355   if (g_async_result_legacy_propagate_error (res, error))
1356     return NULL;
1357
1358   iface = G_FILE_GET_IFACE (file);
1359   return (* iface->query_filesystem_info_finish) (file, res, error);
1360 }
1361
1362 /**
1363  * g_file_find_enclosing_mount:
1364  * @file: input #GFile
1365  * @cancellable: (allow-none): optional #GCancellable object,
1366  *     %NULL to ignore
1367  * @error: a #GError
1368  *
1369  * Gets a #GMount for the #GFile.
1370  *
1371  * If the #GFileIface for @file does not have a mount (e.g.
1372  * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1373  * and %NULL will be returned.
1374  *
1375  * If @cancellable is not %NULL, then the operation can be cancelled by
1376  * triggering the cancellable object from another thread. If the operation
1377  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1378  *
1379  * Returns: (transfer full): a #GMount where the @file is located
1380  *     or %NULL on error.
1381  *     Free the returned object with g_object_unref().
1382  */
1383 GMount *
1384 g_file_find_enclosing_mount (GFile         *file,
1385                              GCancellable  *cancellable,
1386                              GError       **error)
1387 {
1388   GFileIface *iface;
1389
1390   g_return_val_if_fail (G_IS_FILE (file), NULL);
1391
1392   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1393     return NULL;
1394
1395   iface = G_FILE_GET_IFACE (file);
1396   if (iface->find_enclosing_mount == NULL)
1397     {
1398
1399       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1400                            /* Translators: This is an error message when
1401                             * trying to find the enclosing (user visible)
1402                             * mount of a file, but none exists.
1403                             */
1404                            _("Containing mount does not exist"));
1405       return NULL;
1406     }
1407
1408   return (* iface->find_enclosing_mount) (file, cancellable, error);
1409 }
1410
1411 /**
1412  * g_file_find_enclosing_mount_async:
1413  * @file: a #GFile
1414  * @io_priority: the <link linkend="io-priority">I/O priority</link>
1415  *     of the request
1416  * @cancellable: (allow-none): optional #GCancellable object,
1417  *     %NULL to ignore
1418  * @callback: (scope async): a #GAsyncReadyCallback to call
1419  *     when the request is satisfied
1420  * @user_data: (closure): the data to pass to callback function
1421  *
1422  * Asynchronously gets the mount for the file.
1423  *
1424  * For more details, see g_file_find_enclosing_mount() which is
1425  * the synchronous version of this call.
1426  *
1427  * When the operation is finished, @callback will be called.
1428  * You can then call g_file_find_enclosing_mount_finish() to
1429  * get the result of the operation.
1430  */
1431 void
1432 g_file_find_enclosing_mount_async (GFile              *file,
1433                                    int                   io_priority,
1434                                    GCancellable         *cancellable,
1435                                    GAsyncReadyCallback   callback,
1436                                    gpointer              user_data)
1437 {
1438   GFileIface *iface;
1439
1440   g_return_if_fail (G_IS_FILE (file));
1441
1442   iface = G_FILE_GET_IFACE (file);
1443   (* iface->find_enclosing_mount_async) (file,
1444                                          io_priority,
1445                                          cancellable,
1446                                          callback,
1447                                          user_data);
1448 }
1449
1450 /**
1451  * g_file_find_enclosing_mount_finish:
1452  * @file: a #GFile
1453  * @res: a #GAsyncResult
1454  * @error: a #GError
1455  *
1456  * Finishes an asynchronous find mount request.
1457  * See g_file_find_enclosing_mount_async().
1458  *
1459  * Returns: (transfer full): #GMount for given @file or %NULL on error.
1460  *     Free the returned object with g_object_unref().
1461  */
1462 GMount *
1463 g_file_find_enclosing_mount_finish (GFile         *file,
1464                                     GAsyncResult  *res,
1465                                     GError       **error)
1466 {
1467   GFileIface *iface;
1468
1469   g_return_val_if_fail (G_IS_FILE (file), NULL);
1470   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1471
1472   if (g_async_result_legacy_propagate_error (res, error))
1473     return NULL;
1474
1475   iface = G_FILE_GET_IFACE (file);
1476   return (* iface->find_enclosing_mount_finish) (file, res, error);
1477 }
1478
1479
1480 /**
1481  * g_file_read:
1482  * @file: #GFile to read
1483  * @cancellable: (allow-none): a #GCancellable
1484  * @error: a #GError, or %NULL
1485  *
1486  * Opens a file for reading. The result is a #GFileInputStream that
1487  * can be used to read the contents of the file.
1488  *
1489  * If @cancellable is not %NULL, then the operation can be cancelled by
1490  * triggering the cancellable object from another thread. If the operation
1491  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1492  *
1493  * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1494  * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1495  * error will be returned. Other errors are possible too, and depend
1496  * on what kind of filesystem the file is on.
1497  *
1498  * Virtual: read_fn
1499  * Returns: (transfer full): #GFileInputStream or %NULL on error.
1500  *     Free the returned object with g_object_unref().
1501  */
1502 GFileInputStream *
1503 g_file_read (GFile         *file,
1504              GCancellable  *cancellable,
1505              GError       **error)
1506 {
1507   GFileIface *iface;
1508
1509   g_return_val_if_fail (G_IS_FILE (file), NULL);
1510
1511   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1512     return NULL;
1513
1514   iface = G_FILE_GET_IFACE (file);
1515
1516   if (iface->read_fn == NULL)
1517     {
1518       g_set_error_literal (error, G_IO_ERROR,
1519                            G_IO_ERROR_NOT_SUPPORTED,
1520                            _("Operation not supported"));
1521       return NULL;
1522     }
1523
1524   return (* iface->read_fn) (file, cancellable, error);
1525 }
1526
1527 /**
1528  * g_file_append_to:
1529  * @file: input #GFile
1530  * @flags: a set of #GFileCreateFlags
1531  * @cancellable: (allow-none): optional #GCancellable object,
1532  *     %NULL to ignore
1533  * @error: a #GError, or %NULL
1534  *
1535  * Gets an output stream for appending data to the file.
1536  * If the file doesn't already exist it is created.
1537  *
1538  * By default files created are generally readable by everyone,
1539  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1540  * will be made readable only to the current user, to the level that
1541  * is supported on the target filesystem.
1542  *
1543  * If @cancellable is not %NULL, then the operation can be cancelled
1544  * by triggering the cancellable object from another thread. If the
1545  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1546  * returned.
1547  *
1548  * Some file systems don't allow all file names, and may return an
1549  * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1550  * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1551  * possible too, and depend on what kind of filesystem the file is on.
1552  *
1553  * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1554  *     Free the returned object with g_object_unref().
1555  */
1556 GFileOutputStream *
1557 g_file_append_to (GFile             *file,
1558                   GFileCreateFlags   flags,
1559                   GCancellable      *cancellable,
1560                   GError           **error)
1561 {
1562   GFileIface *iface;
1563
1564   g_return_val_if_fail (G_IS_FILE (file), NULL);
1565
1566   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1567     return NULL;
1568
1569   iface = G_FILE_GET_IFACE (file);
1570
1571   if (iface->append_to == NULL)
1572     {
1573       g_set_error_literal (error, G_IO_ERROR,
1574                            G_IO_ERROR_NOT_SUPPORTED,
1575                            _("Operation not supported"));
1576       return NULL;
1577     }
1578
1579   return (* iface->append_to) (file, flags, cancellable, error);
1580 }
1581
1582 /**
1583  * g_file_create:
1584  * @file: input #GFile
1585  * @flags: a set of #GFileCreateFlags
1586  * @cancellable: (allow-none): optional #GCancellable object,
1587  *     %NULL to ignore
1588  * @error: a #GError, or %NULL
1589  *
1590  * Creates a new file and returns an output stream for writing to it.
1591  * The file must not already exist.
1592  *
1593  * By default files created are generally readable by everyone,
1594  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1595  * will be made readable only to the current user, to the level
1596  * that is supported on the target filesystem.
1597  *
1598  * If @cancellable is not %NULL, then the operation can be cancelled
1599  * by triggering the cancellable object from another thread. If the
1600  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1601  * returned.
1602  *
1603  * If a file or directory with this name already exists the
1604  * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1605  * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1606  * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1607  * be returned. Other errors are possible too, and depend on what kind
1608  * of filesystem the file is on.
1609  *
1610  * Returns: (transfer full): a #GFileOutputStream for the newly created
1611  *     file, or %NULL on error.
1612  *     Free the returned object with g_object_unref().
1613  */
1614 GFileOutputStream *
1615 g_file_create (GFile             *file,
1616                GFileCreateFlags   flags,
1617                GCancellable      *cancellable,
1618                GError           **error)
1619 {
1620   GFileIface *iface;
1621
1622   g_return_val_if_fail (G_IS_FILE (file), NULL);
1623
1624   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1625     return NULL;
1626
1627   iface = G_FILE_GET_IFACE (file);
1628
1629   if (iface->create == NULL)
1630     {
1631       g_set_error_literal (error, G_IO_ERROR,
1632                            G_IO_ERROR_NOT_SUPPORTED,
1633                            _("Operation not supported"));
1634       return NULL;
1635     }
1636
1637   return (* iface->create) (file, flags, cancellable, error);
1638 }
1639
1640 /**
1641  * g_file_replace:
1642  * @file: input #GFile
1643  * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1644  *     for the current #GFile, or #NULL to ignore
1645  * @make_backup: %TRUE if a backup should be created
1646  * @flags: a set of #GFileCreateFlags
1647  * @cancellable: (allow-none): optional #GCancellable object,
1648  *     %NULL to ignore
1649  * @error: a #GError, or %NULL
1650  *
1651  * Returns an output stream for overwriting the file, possibly
1652  * creating a backup copy of the file first. If the file doesn't exist,
1653  * it will be created.
1654  *
1655  * This will try to replace the file in the safest way possible so
1656  * that any errors during the writing will not affect an already
1657  * existing copy of the file. For instance, for local files it
1658  * may write to a temporary file and then atomically rename over
1659  * the destination when the stream is closed.
1660  *
1661  * By default files created are generally readable by everyone,
1662  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1663  * will be made readable only to the current user, to the level that
1664  * is supported on the target filesystem.
1665  *
1666  * If @cancellable is not %NULL, then the operation can be cancelled
1667  * by triggering the cancellable object from another thread. If the
1668  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1669  * returned.
1670  *
1671  * If you pass in a non-%NULL @etag value, then this value is
1672  * compared to the current entity tag of the file, and if they differ
1673  * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1674  * that the file has been changed since you last read it. You can get
1675  * the new etag from g_file_output_stream_get_etag() after you've
1676  * finished writing and closed the #GFileOutputStream. When you load
1677  * a new file you can use g_file_input_stream_query_info() to get
1678  * the etag of the file.
1679  *
1680  * If @make_backup is %TRUE, this function will attempt to make a
1681  * backup of the current file before overwriting it. If this fails
1682  * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1683  * want to replace anyway, try again with @make_backup set to %FALSE.
1684  *
1685  * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1686  * be returned, and if the file is some other form of non-regular file
1687  * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1688  * file systems don't allow all file names, and may return an
1689  * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1690  * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1691  * possible too, and depend on what kind of filesystem the file is on.
1692  *
1693  * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1694  *     Free the returned object with g_object_unref().
1695  */
1696 GFileOutputStream *
1697 g_file_replace (GFile             *file,
1698                 const char        *etag,
1699                 gboolean           make_backup,
1700                 GFileCreateFlags   flags,
1701                 GCancellable      *cancellable,
1702                 GError           **error)
1703 {
1704   GFileIface *iface;
1705
1706   g_return_val_if_fail (G_IS_FILE (file), NULL);
1707
1708   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1709     return NULL;
1710
1711   iface = G_FILE_GET_IFACE (file);
1712
1713   if (iface->replace == NULL)
1714     {
1715       g_set_error_literal (error, G_IO_ERROR,
1716                            G_IO_ERROR_NOT_SUPPORTED,
1717                            _("Operation not supported"));
1718       return NULL;
1719     }
1720
1721   /* Handle empty tag string as NULL in consistent way. */
1722   if (etag && *etag == 0)
1723     etag = NULL;
1724
1725   return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1726 }
1727
1728 /**
1729  * g_file_open_readwrite:
1730  * @file: #GFile to open
1731  * @cancellable: (allow-none): a #GCancellable
1732  * @error: a #GError, or %NULL
1733  *
1734  * Opens an existing file for reading and writing. The result is
1735  * a #GFileIOStream that can be used to read and write the contents
1736  * of the file.
1737  *
1738  * If @cancellable is not %NULL, then the operation can be cancelled
1739  * by triggering the cancellable object from another thread. If the
1740  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1741  * returned.
1742  *
1743  * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1744  * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1745  * error will be returned. Other errors are possible too, and depend on
1746  * what kind of filesystem the file is on. Note that in many non-local
1747  * file cases read and write streams are not supported, so make sure you
1748  * really need to do read and write streaming, rather than just opening
1749  * for reading or writing.
1750  *
1751  * Returns: (transfer full): #GFileIOStream or %NULL on error.
1752  *     Free the returned object with g_object_unref().
1753  *
1754  * Since: 2.22
1755  */
1756 GFileIOStream *
1757 g_file_open_readwrite (GFile         *file,
1758                        GCancellable  *cancellable,
1759                        GError       **error)
1760 {
1761   GFileIface *iface;
1762
1763   g_return_val_if_fail (G_IS_FILE (file), NULL);
1764
1765   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1766     return NULL;
1767
1768   iface = G_FILE_GET_IFACE (file);
1769
1770   if (iface->open_readwrite == NULL)
1771     {
1772       g_set_error_literal (error, G_IO_ERROR,
1773                            G_IO_ERROR_NOT_SUPPORTED,
1774                            _("Operation not supported"));
1775       return NULL;
1776     }
1777
1778   return (* iface->open_readwrite) (file, cancellable, error);
1779 }
1780
1781 /**
1782  * g_file_create_readwrite:
1783  * @file: a #GFile
1784  * @flags: a set of #GFileCreateFlags
1785  * @cancellable: (allow-none): optional #GCancellable object,
1786  *     %NULL to ignore
1787  * @error: return location for a #GError, or %NULL
1788  *
1789  * Creates a new file and returns a stream for reading and
1790  * writing to it. The file must not already exist.
1791  *
1792  * By default files created are generally readable by everyone,
1793  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1794  * will be made readable only to the current user, to the level
1795  * that is supported on the target filesystem.
1796  *
1797  * If @cancellable is not %NULL, then the operation can be cancelled
1798  * by triggering the cancellable object from another thread. If the
1799  * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1800  * returned.
1801  *
1802  * If a file or directory with this name already exists, the
1803  * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1804  * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1805  * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1806  * will be returned. Other errors are possible too, and depend on what
1807  * kind of filesystem the file is on.
1808  *
1809  * Note that in many non-local file cases read and write streams are
1810  * not supported, so make sure you really need to do read and write
1811  * streaming, rather than just opening for reading or writing.
1812  *
1813  * Returns: (transfer full): a #GFileIOStream for the newly created
1814  *     file, or %NULL on error.
1815  *     Free the returned object with g_object_unref().
1816  *
1817  * Since: 2.22
1818  */
1819 GFileIOStream *
1820 g_file_create_readwrite (GFile             *file,
1821                          GFileCreateFlags   flags,
1822                          GCancellable      *cancellable,
1823                          GError           **error)
1824 {
1825   GFileIface *iface;
1826
1827   g_return_val_if_fail (G_IS_FILE (file), NULL);
1828
1829   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1830     return NULL;
1831
1832   iface = G_FILE_GET_IFACE (file);
1833
1834   if (iface->create_readwrite == NULL)
1835     {
1836       g_set_error_literal (error, G_IO_ERROR,
1837                            G_IO_ERROR_NOT_SUPPORTED,
1838                            _("Operation not supported"));
1839       return NULL;
1840     }
1841
1842   return (* iface->create_readwrite) (file, flags, cancellable, error);
1843 }
1844
1845 /**
1846  * g_file_replace_readwrite:
1847  * @file: a #GFile
1848  * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1849  *     for the current #GFile, or #NULL to ignore
1850  * @make_backup: %TRUE if a backup should be created
1851  * @flags: a set of #GFileCreateFlags
1852  * @cancellable: (allow-none): optional #GCancellable object,
1853  *     %NULL to ignore
1854  * @error: return location for a #GError, or %NULL
1855  *
1856  * Returns an output stream for overwriting the file in readwrite mode,
1857  * possibly creating a backup copy of the file first. If the file doesn't
1858  * exist, it will be created.
1859  *
1860  * For details about the behaviour, see g_file_replace() which does the
1861  * same thing but returns an output stream only.
1862  *
1863  * Note that in many non-local file cases read and write streams are not
1864  * supported, so make sure you really need to do read and write streaming,
1865  * rather than just opening for reading or writing.
1866  *
1867  * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1868  *     Free the returned object with g_object_unref().
1869  *
1870  * Since: 2.22
1871  */
1872 GFileIOStream *
1873 g_file_replace_readwrite (GFile             *file,
1874                           const char        *etag,
1875                           gboolean           make_backup,
1876                           GFileCreateFlags   flags,
1877                           GCancellable      *cancellable,
1878                           GError           **error)
1879 {
1880   GFileIface *iface;
1881
1882   g_return_val_if_fail (G_IS_FILE (file), NULL);
1883
1884   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1885     return NULL;
1886
1887   iface = G_FILE_GET_IFACE (file);
1888
1889   if (iface->replace_readwrite == NULL)
1890     {
1891       g_set_error_literal (error, G_IO_ERROR,
1892                            G_IO_ERROR_NOT_SUPPORTED,
1893                            _("Operation not supported"));
1894       return NULL;
1895     }
1896
1897   return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1898 }
1899
1900 /**
1901  * g_file_read_async:
1902  * @file: input #GFile
1903  * @io_priority: the <link linkend="io-priority">I/O priority</link>
1904  *     of the request
1905  * @cancellable: (allow-none): optional #GCancellable object,
1906  *     %NULL to ignore
1907  * @callback: (scope async): a #GAsyncReadyCallback to call
1908  *     when the request is satisfied
1909  * @user_data: (closure): the data to pass to callback function
1910  *
1911  * Asynchronously opens @file for reading.
1912  *
1913  * For more details, see g_file_read() which is
1914  * the synchronous version of this call.
1915  *
1916  * When the operation is finished, @callback will be called.
1917  * You can then call g_file_read_finish() to get the result
1918  * of the operation.
1919  */
1920 void
1921 g_file_read_async (GFile               *file,
1922                    int                  io_priority,
1923                    GCancellable        *cancellable,
1924                    GAsyncReadyCallback  callback,
1925                    gpointer             user_data)
1926 {
1927   GFileIface *iface;
1928
1929   g_return_if_fail (G_IS_FILE (file));
1930
1931   iface = G_FILE_GET_IFACE (file);
1932   (* iface->read_async) (file,
1933                          io_priority,
1934                          cancellable,
1935                          callback,
1936                          user_data);
1937 }
1938
1939 /**
1940  * g_file_read_finish:
1941  * @file: input #GFile
1942  * @res: a #GAsyncResult
1943  * @error: a #GError, or %NULL
1944  *
1945  * Finishes an asynchronous file read operation started with
1946  * g_file_read_async().
1947  *
1948  * Returns: (transfer full): a #GFileInputStream or %NULL on error.
1949  *     Free the returned object with g_object_unref().
1950  */
1951 GFileInputStream *
1952 g_file_read_finish (GFile         *file,
1953                     GAsyncResult  *res,
1954                     GError       **error)
1955 {
1956   GFileIface *iface;
1957
1958   g_return_val_if_fail (G_IS_FILE (file), NULL);
1959   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1960
1961   if (g_async_result_legacy_propagate_error (res, error))
1962     return NULL;
1963
1964   iface = G_FILE_GET_IFACE (file);
1965   return (* iface->read_finish) (file, res, error);
1966 }
1967
1968 /**
1969  * g_file_append_to_async:
1970  * @file: input #GFile
1971  * @flags: a set of #GFileCreateFlags
1972  * @io_priority: the <link linkend="io-priority">I/O priority</link>
1973  *     of the request
1974  * @cancellable: (allow-none): optional #GCancellable object,
1975  *     %NULL to ignore
1976  * @callback: (scope async): a #GAsyncReadyCallback to call
1977  *     when the request is satisfied
1978  * @user_data: (closure): the data to pass to callback function
1979  *
1980  * Asynchronously opens @file for appending.
1981  *
1982  * For more details, see g_file_append_to() which is
1983  * the synchronous version of this call.
1984  *
1985  * When the operation is finished, @callback will be called.
1986  * You can then call g_file_append_to_finish() to get the result
1987  * of the operation.
1988  */
1989 void
1990 g_file_append_to_async (GFile               *file,
1991                         GFileCreateFlags     flags,
1992                         int                  io_priority,
1993                         GCancellable        *cancellable,
1994                         GAsyncReadyCallback  callback,
1995                         gpointer             user_data)
1996 {
1997   GFileIface *iface;
1998
1999   g_return_if_fail (G_IS_FILE (file));
2000
2001   iface = G_FILE_GET_IFACE (file);
2002   (* iface->append_to_async) (file,
2003                               flags,
2004                               io_priority,
2005                               cancellable,
2006                               callback,
2007                               user_data);
2008 }
2009
2010 /**
2011  * g_file_append_to_finish:
2012  * @file: input #GFile
2013  * @res: #GAsyncResult
2014  * @error: a #GError, or %NULL
2015  *
2016  * Finishes an asynchronous file append operation started with
2017  * g_file_append_to_async().
2018  *
2019  * Returns: (transfer full): a valid #GFileOutputStream
2020  *     or %NULL on error.
2021  *     Free the returned object with g_object_unref().
2022  */
2023 GFileOutputStream *
2024 g_file_append_to_finish (GFile         *file,
2025                          GAsyncResult  *res,
2026                          GError       **error)
2027 {
2028   GFileIface *iface;
2029
2030   g_return_val_if_fail (G_IS_FILE (file), NULL);
2031   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2032
2033   if (g_async_result_legacy_propagate_error (res, error))
2034     return NULL;
2035
2036   iface = G_FILE_GET_IFACE (file);
2037   return (* iface->append_to_finish) (file, res, error);
2038 }
2039
2040 /**
2041  * g_file_create_async:
2042  * @file: input #GFile
2043  * @flags: a set of #GFileCreateFlags
2044  * @io_priority: the <link linkend="io-priority">I/O priority</link>
2045  *     of the request
2046  * @cancellable: (allow-none): optional #GCancellable object,
2047  *     %NULL to ignore
2048  * @callback: (scope async): a #GAsyncReadyCallback to call
2049  *     when the request is satisfied
2050  * @user_data: (closure): the data to pass to callback function
2051  *
2052  * Asynchronously creates a new file and returns an output stream
2053  * for writing to it. The file must not already exist.
2054  *
2055  * For more details, see g_file_create() which is
2056  * the synchronous version of this call.
2057  *
2058  * When the operation is finished, @callback will be called.
2059  * You can then call g_file_create_finish() to get the result
2060  * of the operation.
2061  */
2062 void
2063 g_file_create_async (GFile               *file,
2064                      GFileCreateFlags     flags,
2065                      int                  io_priority,
2066                      GCancellable        *cancellable,
2067                      GAsyncReadyCallback  callback,
2068                      gpointer             user_data)
2069 {
2070   GFileIface *iface;
2071
2072   g_return_if_fail (G_IS_FILE (file));
2073
2074   iface = G_FILE_GET_IFACE (file);
2075   (* iface->create_async) (file,
2076                            flags,
2077                            io_priority,
2078                            cancellable,
2079                            callback,
2080                            user_data);
2081 }
2082
2083 /**
2084  * g_file_create_finish:
2085  * @file: input #GFile
2086  * @res: a #GAsyncResult
2087  * @error: a #GError, or %NULL
2088  *
2089  * Finishes an asynchronous file create operation started with
2090  * g_file_create_async().
2091  *
2092  * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2093  *     Free the returned object with g_object_unref().
2094  */
2095 GFileOutputStream *
2096 g_file_create_finish (GFile         *file,
2097                       GAsyncResult  *res,
2098                       GError       **error)
2099 {
2100   GFileIface *iface;
2101
2102   g_return_val_if_fail (G_IS_FILE (file), NULL);
2103   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2104
2105   if (g_async_result_legacy_propagate_error (res, error))
2106     return NULL;
2107
2108   iface = G_FILE_GET_IFACE (file);
2109   return (* iface->create_finish) (file, res, error);
2110 }
2111
2112 /**
2113  * g_file_replace_async:
2114  * @file: input #GFile
2115  * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2116  *     for the current #GFile, or NULL to ignore
2117  * @make_backup: %TRUE if a backup should be created
2118  * @flags: a set of #GFileCreateFlags
2119  * @io_priority: the <link linkend="io-priority">I/O priority</link>
2120  *     of the request
2121  * @cancellable: (allow-none): optional #GCancellable object,
2122  *     %NULL to ignore
2123  * @callback: (scope async): a #GAsyncReadyCallback to call
2124  *     when the request is satisfied
2125  * @user_data: (closure): the data to pass to callback function
2126  *
2127  * Asynchronously overwrites the file, replacing the contents,
2128  * possibly creating a backup copy of the file first.
2129  *
2130  * For more details, see g_file_replace() which is
2131  * the synchronous version of this call.
2132  *
2133  * When the operation is finished, @callback will be called.
2134  * You can then call g_file_replace_finish() to get the result
2135  * of the operation.
2136  */
2137 void
2138 g_file_replace_async (GFile               *file,
2139                       const char          *etag,
2140                       gboolean             make_backup,
2141                       GFileCreateFlags     flags,
2142                       int                  io_priority,
2143                       GCancellable        *cancellable,
2144                       GAsyncReadyCallback  callback,
2145                       gpointer             user_data)
2146 {
2147   GFileIface *iface;
2148
2149   g_return_if_fail (G_IS_FILE (file));
2150
2151   iface = G_FILE_GET_IFACE (file);
2152   (* iface->replace_async) (file,
2153                             etag,
2154                             make_backup,
2155                             flags,
2156                             io_priority,
2157                             cancellable,
2158                             callback,
2159                             user_data);
2160 }
2161
2162 /**
2163  * g_file_replace_finish:
2164  * @file: input #GFile
2165  * @res: a #GAsyncResult
2166  * @error: a #GError, or %NULL
2167  *
2168  * Finishes an asynchronous file replace operation started with
2169  * g_file_replace_async().
2170  *
2171  * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2172  *     Free the returned object with g_object_unref().
2173  */
2174 GFileOutputStream *
2175 g_file_replace_finish (GFile         *file,
2176                        GAsyncResult  *res,
2177                        GError       **error)
2178 {
2179   GFileIface *iface;
2180
2181   g_return_val_if_fail (G_IS_FILE (file), NULL);
2182   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2183
2184   if (g_async_result_legacy_propagate_error (res, error))
2185     return NULL;
2186
2187   iface = G_FILE_GET_IFACE (file);
2188   return (* iface->replace_finish) (file, res, error);
2189 }
2190
2191 /**
2192  * g_file_open_readwrite_async
2193  * @file: input #GFile
2194  * @io_priority: the <link linkend="io-priority">I/O priority</link>
2195  *     of the request
2196  * @cancellable: (allow-none): optional #GCancellable object,
2197  *     %NULL to ignore
2198  * @callback: (scope async): a #GAsyncReadyCallback to call
2199  *     when the request is satisfied
2200  * @user_data: (closure): the data to pass to callback function
2201  *
2202  * Asynchronously opens @file for reading and writing.
2203  *
2204  * For more details, see g_file_open_readwrite() which is
2205  * the synchronous version of this call.
2206  *
2207  * When the operation is finished, @callback will be called.
2208  * You can then call g_file_open_readwrite_finish() to get
2209  * the result of the operation.
2210  *
2211  * Since: 2.22
2212  */
2213 void
2214 g_file_open_readwrite_async (GFile               *file,
2215                              int                  io_priority,
2216                              GCancellable        *cancellable,
2217                              GAsyncReadyCallback  callback,
2218                              gpointer             user_data)
2219 {
2220   GFileIface *iface;
2221
2222   g_return_if_fail (G_IS_FILE (file));
2223
2224   iface = G_FILE_GET_IFACE (file);
2225   (* iface->open_readwrite_async) (file,
2226                                    io_priority,
2227                                    cancellable,
2228                                    callback,
2229                                    user_data);
2230 }
2231
2232 /**
2233  * g_file_open_readwrite_finish:
2234  * @file: input #GFile
2235  * @res: a #GAsyncResult
2236  * @error: a #GError, or %NULL
2237  *
2238  * Finishes an asynchronous file read operation started with
2239  * g_file_open_readwrite_async().
2240  *
2241  * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2242  *     Free the returned object with g_object_unref().
2243  *
2244  * Since: 2.22
2245  */
2246 GFileIOStream *
2247 g_file_open_readwrite_finish (GFile         *file,
2248                               GAsyncResult  *res,
2249                               GError       **error)
2250 {
2251   GFileIface *iface;
2252
2253   g_return_val_if_fail (G_IS_FILE (file), NULL);
2254   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2255
2256   if (g_async_result_legacy_propagate_error (res, error))
2257     return NULL;
2258
2259   iface = G_FILE_GET_IFACE (file);
2260   return (* iface->open_readwrite_finish) (file, res, error);
2261 }
2262
2263 /**
2264  * g_file_create_readwrite_async:
2265  * @file: input #GFile
2266  * @flags: a set of #GFileCreateFlags
2267  * @io_priority: the <link linkend="io-priority">I/O priority</link>
2268  *     of the request
2269  * @cancellable: (allow-none): optional #GCancellable object,
2270  *     %NULL to ignore
2271  * @callback: (scope async): a #GAsyncReadyCallback to call
2272  *     when the request is satisfied
2273  * @user_data: (closure): the data to pass to callback function
2274  *
2275  * Asynchronously creates a new file and returns a stream
2276  * for reading and writing to it. The file must not already exist.
2277  *
2278  * For more details, see g_file_create_readwrite() which is
2279  * the synchronous version of this call.
2280  *
2281  * When the operation is finished, @callback will be called.
2282  * You can then call g_file_create_readwrite_finish() to get
2283  * the result of the operation.
2284  *
2285  * Since: 2.22
2286  */
2287 void
2288 g_file_create_readwrite_async (GFile               *file,
2289                                GFileCreateFlags     flags,
2290                                int                  io_priority,
2291                                GCancellable        *cancellable,
2292                                GAsyncReadyCallback  callback,
2293                                gpointer             user_data)
2294 {
2295   GFileIface *iface;
2296
2297   g_return_if_fail (G_IS_FILE (file));
2298
2299   iface = G_FILE_GET_IFACE (file);
2300   (* iface->create_readwrite_async) (file,
2301                                      flags,
2302                                      io_priority,
2303                                      cancellable,
2304                                      callback,
2305                                      user_data);
2306 }
2307
2308 /**
2309  * g_file_create_readwrite_finish:
2310  * @file: input #GFile
2311  * @res: a #GAsyncResult
2312  * @error: a #GError, or %NULL
2313  *
2314  * Finishes an asynchronous file create operation started with
2315  * g_file_create_readwrite_async().
2316  *
2317  * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2318  *     Free the returned object with g_object_unref().
2319  *
2320  * Since: 2.22
2321  */
2322 GFileIOStream *
2323 g_file_create_readwrite_finish (GFile         *file,
2324                                 GAsyncResult  *res,
2325                                 GError       **error)
2326 {
2327   GFileIface *iface;
2328
2329   g_return_val_if_fail (G_IS_FILE (file), NULL);
2330   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2331
2332   if (g_async_result_legacy_propagate_error (res, error))
2333     return NULL;
2334
2335   iface = G_FILE_GET_IFACE (file);
2336   return (* iface->create_readwrite_finish) (file, res, error);
2337 }
2338
2339 /**
2340  * g_file_replace_readwrite_async:
2341  * @file: input #GFile
2342  * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2343  *     for the current #GFile, or NULL to ignore
2344  * @make_backup: %TRUE if a backup should be created
2345  * @flags: a set of #GFileCreateFlags
2346  * @io_priority: the <link linkend="io-priority">I/O priority</link>
2347  *     of the request
2348  * @cancellable: (allow-none): optional #GCancellable object,
2349  *     %NULL to ignore
2350  * @callback: (scope async): a #GAsyncReadyCallback to call
2351  *     when the request is satisfied
2352  * @user_data: (closure): the data to pass to callback function
2353  *
2354  * Asynchronously overwrites the file in read-write mode,
2355  * replacing the contents, possibly creating a backup copy
2356  * of the file first.
2357  *
2358  * For more details, see g_file_replace_readwrite() which is
2359  * the synchronous version of this call.
2360  *
2361  * When the operation is finished, @callback will be called.
2362  * You can then call g_file_replace_readwrite_finish() to get
2363  * the result of the operation.
2364  *
2365  * Since: 2.22
2366  */
2367 void
2368 g_file_replace_readwrite_async (GFile               *file,
2369                                 const char          *etag,
2370                                 gboolean             make_backup,
2371                                 GFileCreateFlags     flags,
2372                                 int                  io_priority,
2373                                 GCancellable        *cancellable,
2374                                 GAsyncReadyCallback  callback,
2375                                 gpointer             user_data)
2376 {
2377   GFileIface *iface;
2378
2379   g_return_if_fail (G_IS_FILE (file));
2380
2381   iface = G_FILE_GET_IFACE (file);
2382   (* iface->replace_readwrite_async) (file,
2383                                       etag,
2384                                       make_backup,
2385                                       flags,
2386                                       io_priority,
2387                                       cancellable,
2388                                       callback,
2389                                       user_data);
2390 }
2391
2392 /**
2393  * g_file_replace_readwrite_finish:
2394  * @file: input #GFile
2395  * @res: a #GAsyncResult
2396  * @error: a #GError, or %NULL
2397  *
2398  * Finishes an asynchronous file replace operation started with
2399  * g_file_replace_readwrite_async().
2400  *
2401  * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2402  *     Free the returned object with g_object_unref().
2403  *
2404  * Since: 2.22
2405  */
2406 GFileIOStream *
2407 g_file_replace_readwrite_finish (GFile         *file,
2408                                  GAsyncResult  *res,
2409                                  GError       **error)
2410 {
2411   GFileIface *iface;
2412
2413   g_return_val_if_fail (G_IS_FILE (file), NULL);
2414   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2415
2416   if (g_async_result_legacy_propagate_error (res, error))
2417     return NULL;
2418
2419   iface = G_FILE_GET_IFACE (file);
2420   return (* iface->replace_readwrite_finish) (file, res, error);
2421 }
2422
2423 static gboolean
2424 copy_symlink (GFile           *destination,
2425               GFileCopyFlags   flags,
2426               GCancellable    *cancellable,
2427               const char      *target,
2428               GError         **error)
2429 {
2430   GError *my_error;
2431   gboolean tried_delete;
2432   GFileInfo *info;
2433   GFileType file_type;
2434
2435   tried_delete = FALSE;
2436
2437  retry:
2438   my_error = NULL;
2439   if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2440     {
2441       /* Maybe it already existed, and we want to overwrite? */
2442       if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2443           my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2444         {
2445           g_error_free (my_error);
2446
2447           /* Don't overwrite if the destination is a directory */
2448           info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2449                                     G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2450                                     cancellable, &my_error);
2451           if (info != NULL)
2452             {
2453               file_type = g_file_info_get_file_type (info);
2454               g_object_unref (info);
2455
2456               if (file_type == G_FILE_TYPE_DIRECTORY)
2457                 {
2458                   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2459                                        _("Can't copy over directory"));
2460                   return FALSE;
2461                 }
2462             }
2463
2464           if (!g_file_delete (destination, cancellable, error))
2465             return FALSE;
2466
2467           tried_delete = TRUE;
2468           goto retry;
2469         }
2470             /* Nah, fail */
2471       g_propagate_error (error, my_error);
2472       return FALSE;
2473     }
2474
2475   return TRUE;
2476 }
2477
2478 static GInputStream *
2479 open_source_for_copy (GFile           *source,
2480                       GFile           *destination,
2481                       GFileCopyFlags   flags,
2482                       GCancellable    *cancellable,
2483                       GError         **error)
2484 {
2485   GError *my_error;
2486   GInputStream *in;
2487   GFileInfo *info;
2488   GFileType file_type;
2489
2490   my_error = NULL;
2491   in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2492   if (in != NULL)
2493     return in;
2494
2495   /* There was an error opening the source, try to set a good error for it: */
2496   if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2497     {
2498       /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2499        * as that is less useful to the app. Better check for errors on the
2500        * target instead.
2501        */
2502       g_error_free (my_error);
2503       my_error = NULL;
2504
2505       info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2506                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2507                                 cancellable, &my_error);
2508       if (info != NULL &&
2509           g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2510         {
2511           file_type = g_file_info_get_file_type (info);
2512           g_object_unref (info);
2513
2514           if (flags & G_FILE_COPY_OVERWRITE)
2515             {
2516               if (file_type == G_FILE_TYPE_DIRECTORY)
2517                 {
2518                   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2519                                        _("Can't copy directory over directory"));
2520                   return NULL;
2521                 }
2522               /* continue to would_recurse error */
2523             }
2524           else
2525             {
2526               g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2527                                    _("Target file exists"));
2528               return NULL;
2529             }
2530         }
2531       else
2532         {
2533           /* Error getting info from target, return that error
2534            * (except for NOT_FOUND, which is no error here)
2535            */
2536           g_clear_object (&info);
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  * @result: 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: %FALSE if there was any error, %TRUE 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_task_report_new_error (file, callback, user_data,
4417                                g_file_mount_mountable,
4418                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4419                                _("Operation not supported"));
4420       return;
4421     }
4422
4423   (* iface->mount_mountable) (file,
4424                               flags,
4425                               mount_operation,
4426                               cancellable,
4427                               callback,
4428                               user_data);
4429 }
4430
4431 /**
4432  * g_file_mount_mountable_finish:
4433  * @file: input #GFile
4434  * @result: a #GAsyncResult
4435  * @error: a #GError, or %NULL
4436  *
4437  * Finishes a mount operation. See g_file_mount_mountable() for details.
4438  *
4439  * Finish an asynchronous mount operation that was started
4440  * with g_file_mount_mountable().
4441  *
4442  * Returns: (transfer full): a #GFile or %NULL on error.
4443  *     Free the returned object with g_object_unref().
4444  */
4445 GFile *
4446 g_file_mount_mountable_finish (GFile         *file,
4447                                GAsyncResult  *result,
4448                                GError       **error)
4449 {
4450   GFileIface *iface;
4451
4452   g_return_val_if_fail (G_IS_FILE (file), NULL);
4453   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4454
4455   if (g_async_result_legacy_propagate_error (result, error))
4456     return NULL;
4457   else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4458     return g_task_propagate_pointer (G_TASK (result), error);
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_task_report_new_error (file, callback, user_data,
4502                                g_file_unmount_mountable_with_operation,
4503                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4504                                _("Operation not supported"));
4505       return;
4506     }
4507
4508   (* iface->unmount_mountable) (file,
4509                                 flags,
4510                                 cancellable,
4511                                 callback,
4512                                 user_data);
4513 }
4514
4515 /**
4516  * g_file_unmount_mountable_finish:
4517  * @file: input #GFile
4518  * @result: a #GAsyncResult
4519  * @error: a #GError, or %NULL
4520  *
4521  * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4522  *
4523  * Finish an asynchronous unmount operation that was started
4524  * with g_file_unmount_mountable().
4525  *
4526  * Returns: %TRUE if the operation finished successfully.
4527  *     %FALSE otherwise.
4528  *
4529  * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4530  *     instead.
4531  */
4532 gboolean
4533 g_file_unmount_mountable_finish (GFile         *file,
4534                                  GAsyncResult  *result,
4535                                  GError       **error)
4536 {
4537   GFileIface *iface;
4538
4539   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4540   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4541
4542   if (g_async_result_legacy_propagate_error (result, error))
4543     return FALSE;
4544   else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4545     return g_task_propagate_boolean (G_TASK (result), error);
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_task_report_new_error (file, callback, user_data,
4592                                g_file_unmount_mountable_with_operation,
4593                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4594                                _("Operation not supported"));
4595       return;
4596     }
4597
4598   if (iface->unmount_mountable_with_operation != NULL)
4599     (* iface->unmount_mountable_with_operation) (file,
4600                                                  flags,
4601                                                  mount_operation,
4602                                                  cancellable,
4603                                                  callback,
4604                                                  user_data);
4605   else
4606     (* iface->unmount_mountable) (file,
4607                                   flags,
4608                                   cancellable,
4609                                   callback,
4610                                   user_data);
4611 }
4612
4613 /**
4614  * g_file_unmount_mountable_with_operation_finish:
4615  * @file: input #GFile
4616  * @result: a #GAsyncResult
4617  * @error: a #GError, or %NULL
4618  *
4619  * Finishes an unmount operation,
4620  * see g_file_unmount_mountable_with_operation() for details.
4621  *
4622  * Finish an asynchronous unmount operation that was started
4623  * with g_file_unmount_mountable_with_operation().
4624  *
4625  * Returns: %TRUE if the operation finished successfully.
4626  *     %FALSE otherwise.
4627  *
4628  * Since: 2.22
4629  */
4630 gboolean
4631 g_file_unmount_mountable_with_operation_finish (GFile         *file,
4632                                                 GAsyncResult  *result,
4633                                                 GError       **error)
4634 {
4635   GFileIface *iface;
4636
4637   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4638   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4639
4640   if (g_async_result_legacy_propagate_error (result, error))
4641     return FALSE;
4642   else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4643     return g_task_propagate_boolean (G_TASK (result), error);
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_task_report_new_error (file, callback, user_data,
4689                                g_file_eject_mountable_with_operation,
4690                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4691                                _("Operation not supported"));
4692       return;
4693     }
4694
4695   (* iface->eject_mountable) (file,
4696                               flags,
4697                               cancellable,
4698                               callback,
4699                               user_data);
4700 }
4701
4702 /**
4703  * g_file_eject_mountable_finish:
4704  * @file: input #GFile
4705  * @result: a #GAsyncResult
4706  * @error: a #GError, or %NULL
4707  *
4708  * Finishes an asynchronous eject operation started by
4709  * g_file_eject_mountable().
4710  *
4711  * Returns: %TRUE if the @file was ejected successfully.
4712  *     %FALSE otherwise.
4713  *
4714  * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
4715  *     instead.
4716  */
4717 gboolean
4718 g_file_eject_mountable_finish (GFile         *file,
4719                                GAsyncResult  *result,
4720                                GError       **error)
4721 {
4722   GFileIface *iface;
4723
4724   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4725   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4726
4727   if (g_async_result_legacy_propagate_error (result, error))
4728     return FALSE;
4729   else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
4730     return g_task_propagate_boolean (G_TASK (result), error);
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_task_report_new_error (file, callback, user_data,
4776                                g_file_eject_mountable_with_operation,
4777                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4778                                _("Operation not supported"));
4779       return;
4780     }
4781
4782   if (iface->eject_mountable_with_operation != NULL)
4783     (* iface->eject_mountable_with_operation) (file,
4784                                                flags,
4785                                                mount_operation,
4786                                                cancellable,
4787                                                callback,
4788                                                user_data);
4789   else
4790     (* iface->eject_mountable) (file,
4791                                 flags,
4792                                 cancellable,
4793                                 callback,
4794                                 user_data);
4795 }
4796
4797 /**
4798  * g_file_eject_mountable_with_operation_finish:
4799  * @file: input #GFile
4800  * @result: a #GAsyncResult
4801  * @error: a #GError, or %NULL
4802  *
4803  * Finishes an asynchronous eject operation started by
4804  * g_file_eject_mountable_with_operation().
4805  *
4806  * Returns: %TRUE if the @file was ejected successfully.
4807  *     %FALSE otherwise.
4808  *
4809  * Since: 2.22
4810  */
4811 gboolean
4812 g_file_eject_mountable_with_operation_finish (GFile         *file,
4813                                               GAsyncResult  *result,
4814                                               GError       **error)
4815 {
4816   GFileIface *iface;
4817
4818   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4819   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4820
4821   if (g_async_result_legacy_propagate_error (result, error))
4822     return FALSE;
4823   else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
4824     return g_task_propagate_boolean (G_TASK (result), error);
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  * It does not make sense for @flags to contain
4849  * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
4850  * directories.  It is not possible to monitor all the files in a
4851  * directory for changes made via hard links; if you want to do this then
4852  * you must register individual watches with g_file_monitor().
4853  *
4854  * Virtual: monitor_dir
4855  * Returns: (transfer full): a #GFileMonitor for the given @file,
4856  *     or %NULL on error.
4857  *     Free the returned object with g_object_unref().
4858  */
4859 GFileMonitor *
4860 g_file_monitor_directory (GFile              *file,
4861                           GFileMonitorFlags   flags,
4862                           GCancellable       *cancellable,
4863                           GError            **error)
4864 {
4865   GFileIface *iface;
4866
4867   g_return_val_if_fail (G_IS_FILE (file), NULL);
4868   g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
4869
4870   if (g_cancellable_set_error_if_cancelled (cancellable, error))
4871     return NULL;
4872
4873   iface = G_FILE_GET_IFACE (file);
4874
4875   if (iface->monitor_dir == NULL)
4876     {
4877       g_set_error_literal (error, G_IO_ERROR,
4878                            G_IO_ERROR_NOT_SUPPORTED,
4879                            _("Operation not supported"));
4880       return NULL;
4881     }
4882
4883   return (* iface->monitor_dir) (file, flags, cancellable, error);
4884 }
4885
4886 /**
4887  * g_file_monitor_file:
4888  * @file: input #GFile
4889  * @flags: a set of #GFileMonitorFlags
4890  * @cancellable: (allow-none): optional #GCancellable object,
4891  *     %NULL to ignore
4892  * @error: a #GError, or %NULL
4893  *
4894  * Obtains a file monitor for the given file. If no file notification
4895  * mechanism exists, then regular polling of the file is used.
4896  *
4897  * If @cancellable is not %NULL, then the operation can be cancelled by
4898  * triggering the cancellable object from another thread. If the operation
4899  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4900  *
4901  * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
4902  * will also attempt to report changes made to the file via another
4903  * filename (ie, a hard link). Without this flag, you can only rely on
4904  * changes made through the filename contained in @file to be
4905  * reported. Using this flag may result in an increase in resource
4906  * usage, and may not have any effect depending on the #GFileMonitor
4907  * backend and/or filesystem type.
4908  * 
4909  * Returns: (transfer full): a #GFileMonitor for the given @file,
4910  *     or %NULL on error.
4911  *     Free the returned object with g_object_unref().
4912  */
4913 GFileMonitor *
4914 g_file_monitor_file (GFile              *file,
4915                      GFileMonitorFlags   flags,
4916                      GCancellable       *cancellable,
4917                      GError            **error)
4918 {
4919   GFileIface *iface;
4920   GFileMonitor *monitor;
4921
4922   g_return_val_if_fail (G_IS_FILE (file), NULL);
4923
4924   if (g_cancellable_set_error_if_cancelled (cancellable, error))
4925     return NULL;
4926
4927   iface = G_FILE_GET_IFACE (file);
4928
4929   monitor = NULL;
4930
4931   if (iface->monitor_file)
4932     monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4933
4934   /* Fallback to polling */
4935   if (monitor == NULL)
4936     monitor = _g_poll_file_monitor_new (file);
4937
4938   return monitor;
4939 }
4940
4941 /**
4942  * g_file_monitor:
4943  * @file: input #GFile
4944  * @flags: a set of #GFileMonitorFlags
4945  * @cancellable: (allow-none): optional #GCancellable object,
4946  *     %NULL to ignore
4947  * @error: a #GError, or %NULL
4948  *
4949  * Obtains a file or directory monitor for the given file,
4950  * depending on the type of the file.
4951  *
4952  * If @cancellable is not %NULL, then the operation can be cancelled by
4953  * triggering the cancellable object from another thread. If the operation
4954  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4955  *
4956  * Returns: (transfer full): a #GFileMonitor for the given @file,
4957  *     or %NULL on error.
4958  *     Free the returned object with g_object_unref().
4959  *
4960  * Since: 2.18
4961  */
4962 GFileMonitor *
4963 g_file_monitor (GFile              *file,
4964                 GFileMonitorFlags   flags,
4965                 GCancellable       *cancellable,
4966                 GError            **error)
4967 {
4968   if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4969     return g_file_monitor_directory (file,
4970                                      flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
4971                                      cancellable, error);
4972   else
4973     return g_file_monitor_file (file, flags, cancellable, error);
4974 }
4975
4976 /********************************************
4977  *   Default implementation of async ops    *
4978  ********************************************/
4979
4980 typedef struct {
4981   char *attributes;
4982   GFileQueryInfoFlags flags;
4983 } QueryInfoAsyncData;
4984
4985 static void
4986 query_info_data_free (QueryInfoAsyncData *data)
4987 {
4988   g_free (data->attributes);
4989   g_free (data);
4990 }
4991
4992 static void
4993 query_info_async_thread (GTask         *task,
4994                          gpointer       object,
4995                          gpointer       task_data,
4996                          GCancellable  *cancellable)
4997 {
4998   QueryInfoAsyncData *data = task_data;
4999   GFileInfo *info;
5000   GError *error = NULL;
5001
5002   info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5003   if (info)
5004     g_task_return_pointer (task, info, g_object_unref);
5005   else
5006     g_task_return_error (task, error);
5007 }
5008
5009 static void
5010 g_file_real_query_info_async (GFile               *file,
5011                               const char          *attributes,
5012                               GFileQueryInfoFlags  flags,
5013                               int                  io_priority,
5014                               GCancellable        *cancellable,
5015                               GAsyncReadyCallback  callback,
5016                               gpointer             user_data)
5017 {
5018   GTask *task;
5019   QueryInfoAsyncData *data;
5020
5021   data = g_new0 (QueryInfoAsyncData, 1);
5022   data->attributes = g_strdup (attributes);
5023   data->flags = flags;
5024
5025   task = g_task_new (file, cancellable, callback, user_data);
5026   g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5027   g_task_set_priority (task, io_priority);
5028   g_task_run_in_thread (task, query_info_async_thread);
5029   g_object_unref (task);
5030 }
5031
5032 static GFileInfo *
5033 g_file_real_query_info_finish (GFile         *file,
5034                                GAsyncResult  *res,
5035                                GError       **error)
5036 {
5037   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5038
5039   return g_task_propagate_pointer (G_TASK (res), error);
5040 }
5041
5042 static void
5043 query_filesystem_info_async_thread (GTask         *task,
5044                                     gpointer       object,
5045                                     gpointer       task_data,
5046                                     GCancellable  *cancellable)
5047 {
5048   const char *attributes = task_data;
5049   GFileInfo *info;
5050   GError *error = NULL;
5051
5052   info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5053   if (info)
5054     g_task_return_pointer (task, info, g_object_unref);
5055   else
5056     g_task_return_error (task, error);
5057 }
5058
5059 static void
5060 g_file_real_query_filesystem_info_async (GFile               *file,
5061                                          const char          *attributes,
5062                                          int                  io_priority,
5063                                          GCancellable        *cancellable,
5064                                          GAsyncReadyCallback  callback,
5065                                          gpointer             user_data)
5066 {
5067   GTask *task;
5068
5069   task = g_task_new (file, cancellable, callback, user_data);
5070   g_task_set_task_data (task, g_strdup (attributes), g_free);
5071   g_task_set_priority (task, io_priority);
5072   g_task_run_in_thread (task, query_filesystem_info_async_thread);
5073   g_object_unref (task);
5074 }
5075
5076 static GFileInfo *
5077 g_file_real_query_filesystem_info_finish (GFile         *file,
5078                                           GAsyncResult  *res,
5079                                           GError       **error)
5080 {
5081   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5082
5083   return g_task_propagate_pointer (G_TASK (res), error);
5084 }
5085
5086 static void
5087 enumerate_children_async_thread (GTask         *task,
5088                                  gpointer       object,
5089                                  gpointer       task_data,
5090                                  GCancellable  *cancellable)
5091 {
5092   QueryInfoAsyncData *data = task_data;
5093   GFileEnumerator *enumerator;
5094   GError *error = NULL;
5095
5096   enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5097   if (error)
5098     g_task_return_error (task, error);
5099   else
5100     g_task_return_pointer (task, enumerator, g_object_unref);
5101 }
5102
5103 static void
5104 g_file_real_enumerate_children_async (GFile               *file,
5105                                       const char          *attributes,
5106                                       GFileQueryInfoFlags  flags,
5107                                       int                  io_priority,
5108                                       GCancellable        *cancellable,
5109                                       GAsyncReadyCallback  callback,
5110                                       gpointer             user_data)
5111 {
5112   GTask *task;
5113   QueryInfoAsyncData *data;
5114
5115   data = g_new0 (QueryInfoAsyncData, 1);
5116   data->attributes = g_strdup (attributes);
5117   data->flags = flags;
5118
5119   task = g_task_new (file, cancellable, callback, user_data);
5120   g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5121   g_task_set_priority (task, io_priority);
5122   g_task_run_in_thread (task, enumerate_children_async_thread);
5123   g_object_unref (task);
5124 }
5125
5126 static GFileEnumerator *
5127 g_file_real_enumerate_children_finish (GFile         *file,
5128                                        GAsyncResult  *res,
5129                                        GError       **error)
5130 {
5131   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5132
5133   return g_task_propagate_pointer (G_TASK (res), error);
5134 }
5135
5136 static void
5137 open_read_async_thread (GTask         *task,
5138                         gpointer       object,
5139                         gpointer       task_data,
5140                         GCancellable  *cancellable)
5141 {
5142   GFileIface *iface;
5143   GFileInputStream *stream;
5144   GError *error = NULL;
5145
5146   iface = G_FILE_GET_IFACE (object);
5147
5148   if (iface->read_fn == NULL)
5149     {
5150       g_task_return_new_error (task, G_IO_ERROR,
5151                                G_IO_ERROR_NOT_SUPPORTED,
5152                                _("Operation not supported"));
5153       return;
5154     }
5155
5156   stream = iface->read_fn (G_FILE (object), cancellable, &error);
5157   if (stream)
5158     g_task_return_pointer (task, stream, g_object_unref);
5159   else
5160     g_task_return_error (task, error);
5161 }
5162
5163 static void
5164 g_file_real_read_async (GFile               *file,
5165                         int                  io_priority,
5166                         GCancellable        *cancellable,
5167                         GAsyncReadyCallback  callback,
5168                         gpointer             user_data)
5169 {
5170   GTask *task;
5171
5172   task = g_task_new (file, cancellable, callback, user_data);
5173   g_task_set_priority (task, io_priority);
5174   g_task_run_in_thread (task, open_read_async_thread);
5175   g_object_unref (task);
5176 }
5177
5178 static GFileInputStream *
5179 g_file_real_read_finish (GFile         *file,
5180                          GAsyncResult  *res,
5181                          GError       **error)
5182 {
5183   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5184
5185   return g_task_propagate_pointer (G_TASK (res), error);
5186 }
5187
5188 static void
5189 append_to_async_thread (GTask         *task,
5190                         gpointer       source_object,
5191                         gpointer       task_data,
5192                         GCancellable  *cancellable)
5193 {
5194   GFileIface *iface;
5195   GFileCreateFlags *data = task_data;
5196   GFileOutputStream *stream;
5197   GError *error = NULL;
5198
5199   iface = G_FILE_GET_IFACE (source_object);
5200
5201   stream = iface->append_to (G_FILE (source_object), *data, cancellable, &error);
5202   if (stream)
5203     g_task_return_pointer (task, stream, g_object_unref);
5204   else
5205     g_task_return_error (task, error);
5206 }
5207
5208 static void
5209 g_file_real_append_to_async (GFile               *file,
5210                              GFileCreateFlags     flags,
5211                              int                  io_priority,
5212                              GCancellable        *cancellable,
5213                              GAsyncReadyCallback  callback,
5214                              gpointer             user_data)
5215 {
5216   GFileCreateFlags *data;
5217   GTask *task;
5218
5219   data = g_new0 (GFileCreateFlags, 1);
5220   *data = flags;
5221
5222   task = g_task_new (file, cancellable, callback, user_data);
5223   g_task_set_task_data (task, data, g_free);
5224   g_task_set_priority (task, io_priority);
5225
5226   g_task_run_in_thread (task, append_to_async_thread);
5227   g_object_unref (task);
5228 }
5229
5230 static GFileOutputStream *
5231 g_file_real_append_to_finish (GFile         *file,
5232                               GAsyncResult  *res,
5233                               GError       **error)
5234 {
5235   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5236
5237   return g_task_propagate_pointer (G_TASK (res), error);
5238 }
5239
5240 static void
5241 create_async_thread (GTask         *task,
5242                      gpointer       source_object,
5243                      gpointer       task_data,
5244                      GCancellable  *cancellable)
5245 {
5246   GFileIface *iface;
5247   GFileCreateFlags *data = task_data;
5248   GFileOutputStream *stream;
5249   GError *error = NULL;
5250
5251   iface = G_FILE_GET_IFACE (source_object);
5252
5253   stream = iface->create (G_FILE (source_object), *data, cancellable, &error);
5254   if (stream)
5255     g_task_return_pointer (task, stream, g_object_unref);
5256   else
5257     g_task_return_error (task, error);
5258 }
5259
5260 static void
5261 g_file_real_create_async (GFile               *file,
5262                           GFileCreateFlags     flags,
5263                           int                  io_priority,
5264                           GCancellable        *cancellable,
5265                           GAsyncReadyCallback  callback,
5266                           gpointer             user_data)
5267 {
5268   GFileCreateFlags *data;
5269   GTask *task;
5270
5271   data = g_new0 (GFileCreateFlags, 1);
5272   *data = flags;
5273
5274   task = g_task_new (file, cancellable, callback, user_data);
5275   g_task_set_task_data (task, data, g_free);
5276   g_task_set_priority (task, io_priority);
5277
5278   g_task_run_in_thread (task, create_async_thread);
5279   g_object_unref (task);
5280 }
5281
5282 static GFileOutputStream *
5283 g_file_real_create_finish (GFile         *file,
5284                            GAsyncResult  *res,
5285                            GError       **error)
5286 {
5287   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5288
5289   return g_task_propagate_pointer (G_TASK (res), error);
5290 }
5291
5292 typedef struct {
5293   GFileOutputStream *stream;
5294   char *etag;
5295   gboolean make_backup;
5296   GFileCreateFlags flags;
5297 } ReplaceAsyncData;
5298
5299 static void
5300 replace_async_data_free (ReplaceAsyncData *data)
5301 {
5302   if (data->stream)
5303     g_object_unref (data->stream);
5304   g_free (data->etag);
5305   g_free (data);
5306 }
5307
5308 static void
5309 replace_async_thread (GTask         *task,
5310                       gpointer       source_object,
5311                       gpointer       task_data,
5312                       GCancellable  *cancellable)
5313 {
5314   GFileIface *iface;
5315   GFileOutputStream *stream;
5316   ReplaceAsyncData *data = task_data;
5317   GError *error = NULL;
5318
5319   iface = G_FILE_GET_IFACE (source_object);
5320
5321   stream = iface->replace (G_FILE (source_object),
5322                            data->etag,
5323                            data->make_backup,
5324                            data->flags,
5325                            cancellable,
5326                            &error);
5327
5328   if (stream)
5329     g_task_return_pointer (task, stream, g_object_unref);
5330   else
5331     g_task_return_error (task, error);
5332 }
5333
5334 static void
5335 g_file_real_replace_async (GFile               *file,
5336                            const char          *etag,
5337                            gboolean             make_backup,
5338                            GFileCreateFlags     flags,
5339                            int                  io_priority,
5340                            GCancellable        *cancellable,
5341                            GAsyncReadyCallback  callback,
5342                            gpointer             user_data)
5343 {
5344   GTask *task;
5345   ReplaceAsyncData *data;
5346
5347   data = g_new0 (ReplaceAsyncData, 1);
5348   data->etag = g_strdup (etag);
5349   data->make_backup = make_backup;
5350   data->flags = flags;
5351
5352   task = g_task_new (file, cancellable, callback, user_data);
5353   g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5354   g_task_set_priority (task, io_priority);
5355
5356   g_task_run_in_thread (task, replace_async_thread);
5357   g_object_unref (task);
5358 }
5359
5360 static GFileOutputStream *
5361 g_file_real_replace_finish (GFile         *file,
5362                             GAsyncResult  *res,
5363                             GError       **error)
5364 {
5365   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5366
5367   return g_task_propagate_pointer (G_TASK (res), error);
5368 }
5369
5370 static void
5371 delete_async_thread (GTask        *task,
5372                      gpointer      object,
5373                      gpointer      task_data,
5374                      GCancellable *cancellable)
5375 {
5376   GFile *file = object;
5377   GFileIface *iface;
5378   GError *error = NULL;
5379
5380   iface = G_FILE_GET_IFACE (object);
5381
5382   if (iface->delete_file (file,
5383                           cancellable,
5384                           &error))
5385     g_task_return_boolean (task, TRUE);
5386   else
5387     g_task_return_error (task, error);
5388 }
5389
5390 static void
5391 g_file_real_delete_async (GFile               *file,
5392                           int                  io_priority,
5393                           GCancellable        *cancellable,
5394                           GAsyncReadyCallback  callback,
5395                           gpointer             user_data)
5396 {
5397   GTask *task;
5398
5399   task = g_task_new (file, cancellable, callback, user_data);
5400   g_task_set_priority (task, io_priority);
5401   g_task_run_in_thread (task, delete_async_thread);
5402   g_object_unref (task);
5403 }
5404
5405 static gboolean
5406 g_file_real_delete_finish (GFile         *file,
5407                            GAsyncResult  *res,
5408                            GError       **error)
5409 {
5410   g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5411
5412   return g_task_propagate_boolean (G_TASK (res), error);
5413 }
5414
5415 static void
5416 open_readwrite_async_thread (GTask        *task,
5417                              gpointer      object,
5418                              gpointer      task_data,
5419                              GCancellable *cancellable)
5420 {
5421   GFileIface *iface;
5422   GFileIOStream *stream;
5423   GError *error = NULL;
5424
5425   iface = G_FILE_GET_IFACE (object);
5426
5427   if (iface->open_readwrite == NULL)
5428     {
5429       g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5430                                _("Operation not supported"));
5431       return;
5432     }
5433
5434   stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5435
5436   if (stream == NULL)
5437     g_task_return_error (task, error);
5438   else
5439     g_task_return_pointer (task, stream, g_object_unref);
5440 }
5441
5442 static void
5443 g_file_real_open_readwrite_async (GFile               *file,
5444                                   int                  io_priority,
5445                                   GCancellable        *cancellable,
5446                                   GAsyncReadyCallback  callback,
5447                                   gpointer             user_data)
5448 {
5449   GTask *task;
5450
5451   task = g_task_new (file, cancellable, callback, user_data);
5452   g_task_set_priority (task, io_priority);
5453
5454   g_task_run_in_thread (task, open_readwrite_async_thread);
5455   g_object_unref (task);
5456 }
5457
5458 static GFileIOStream *
5459 g_file_real_open_readwrite_finish (GFile         *file,
5460                                    GAsyncResult  *res,
5461                                    GError       **error)
5462 {
5463   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5464
5465   return g_task_propagate_pointer (G_TASK (res), error);
5466 }
5467
5468 static void
5469 create_readwrite_async_thread (GTask        *task,
5470                                gpointer      object,
5471                                gpointer      task_data,
5472                                GCancellable *cancellable)
5473 {
5474   GFileIface *iface;
5475   GFileCreateFlags *data = task_data;
5476   GFileIOStream *stream;
5477   GError *error = NULL;
5478
5479   iface = G_FILE_GET_IFACE (object);
5480
5481   if (iface->create_readwrite == NULL)
5482     {
5483       g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5484                                _("Operation not supported"));
5485       return;
5486     }
5487
5488   stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5489
5490   if (stream == NULL)
5491     g_task_return_error (task, error);
5492   else
5493     g_task_return_pointer (task, stream, g_object_unref);
5494 }
5495
5496 static void
5497 g_file_real_create_readwrite_async (GFile               *file,
5498                                     GFileCreateFlags     flags,
5499                                     int                  io_priority,
5500                                     GCancellable        *cancellable,
5501                                     GAsyncReadyCallback  callback,
5502                                     gpointer             user_data)
5503 {
5504   GFileCreateFlags *data;
5505   GTask *task;
5506
5507   data = g_new0 (GFileCreateFlags, 1);
5508   *data = flags;
5509
5510   task = g_task_new (file, cancellable, callback, user_data);
5511   g_task_set_task_data (task, data, g_free);
5512   g_task_set_priority (task, io_priority);
5513
5514   g_task_run_in_thread (task, create_readwrite_async_thread);
5515   g_object_unref (task);
5516 }
5517
5518 static GFileIOStream *
5519 g_file_real_create_readwrite_finish (GFile         *file,
5520                                      GAsyncResult  *res,
5521                                      GError       **error)
5522 {
5523   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5524
5525   return g_task_propagate_pointer (G_TASK (res), error);
5526 }
5527
5528 typedef struct {
5529   char *etag;
5530   gboolean make_backup;
5531   GFileCreateFlags flags;
5532 } ReplaceRWAsyncData;
5533
5534 static void
5535 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5536 {
5537   g_free (data->etag);
5538   g_free (data);
5539 }
5540
5541 static void
5542 replace_readwrite_async_thread (GTask        *task,
5543                                 gpointer      object,
5544                                 gpointer      task_data,
5545                                 GCancellable *cancellable)
5546 {
5547   GFileIface *iface;
5548   GFileIOStream *stream;
5549   GError *error = NULL;
5550   ReplaceRWAsyncData *data = task_data;
5551
5552   iface = G_FILE_GET_IFACE (object);
5553
5554   stream = iface->replace_readwrite (G_FILE (object),
5555                                      data->etag,
5556                                      data->make_backup,
5557                                      data->flags,
5558                                      cancellable,
5559                                      &error);
5560
5561   if (stream == NULL)
5562     g_task_return_error (task, error);
5563   else
5564     g_task_return_pointer (task, stream, g_object_unref);
5565 }
5566
5567 static void
5568 g_file_real_replace_readwrite_async (GFile               *file,
5569                                      const char          *etag,
5570                                      gboolean             make_backup,
5571                                      GFileCreateFlags     flags,
5572                                      int                  io_priority,
5573                                      GCancellable        *cancellable,
5574                                      GAsyncReadyCallback  callback,
5575                                      gpointer             user_data)
5576 {
5577   GTask *task;
5578   ReplaceRWAsyncData *data;
5579
5580   data = g_new0 (ReplaceRWAsyncData, 1);
5581   data->etag = g_strdup (etag);
5582   data->make_backup = make_backup;
5583   data->flags = flags;
5584
5585   task = g_task_new (file, cancellable, callback, user_data);
5586   g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5587   g_task_set_priority (task, io_priority);
5588
5589   g_task_run_in_thread (task, replace_readwrite_async_thread);
5590   g_object_unref (task);
5591 }
5592
5593 static GFileIOStream *
5594 g_file_real_replace_readwrite_finish (GFile         *file,
5595                                       GAsyncResult  *res,
5596                                       GError       **error)
5597 {
5598   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5599
5600   return g_task_propagate_pointer (G_TASK (res), error);
5601 }
5602
5603 static void
5604 set_display_name_async_thread (GTask        *task,
5605                                gpointer      object,
5606                                gpointer      task_data,
5607                                GCancellable *cancellable)
5608 {
5609   GError *error = NULL;
5610   char *name = task_data;
5611   GFile *file;
5612
5613   file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
5614
5615   if (file == NULL)
5616     g_task_return_error (task, error);
5617   else
5618     g_task_return_pointer (task, file, g_object_unref);
5619 }
5620
5621 static void
5622 g_file_real_set_display_name_async (GFile               *file,
5623                                     const char          *display_name,
5624                                     int                  io_priority,
5625                                     GCancellable        *cancellable,
5626                                     GAsyncReadyCallback  callback,
5627                                     gpointer             user_data)
5628 {
5629   GTask *task;
5630
5631   task = g_task_new (file, cancellable, callback, user_data);
5632   g_task_set_task_data (task, g_strdup (display_name), g_free);
5633   g_task_set_priority (task, io_priority);
5634
5635   g_task_run_in_thread (task, set_display_name_async_thread);
5636   g_object_unref (task);
5637 }
5638
5639 static GFile *
5640 g_file_real_set_display_name_finish (GFile         *file,
5641                                      GAsyncResult  *res,
5642                                      GError       **error)
5643 {
5644   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5645
5646   return g_task_propagate_pointer (G_TASK (res), error);
5647 }
5648
5649 typedef struct {
5650   GFileQueryInfoFlags flags;
5651   GFileInfo *info;
5652   gboolean res;
5653   GError *error;
5654 } SetInfoAsyncData;
5655
5656 static void
5657 set_info_data_free (SetInfoAsyncData *data)
5658 {
5659   if (data->info)
5660     g_object_unref (data->info);
5661   if (data->error)
5662     g_error_free (data->error);
5663   g_free (data);
5664 }
5665
5666 static void
5667 set_info_async_thread (GTask        *task,
5668                        gpointer      object,
5669                        gpointer      task_data,
5670                        GCancellable *cancellable)
5671 {
5672   SetInfoAsyncData *data = task_data;
5673
5674   data->error = NULL;
5675   data->res = g_file_set_attributes_from_info (G_FILE (object),
5676                                                data->info,
5677                                                data->flags,
5678                                                cancellable,
5679                                                &data->error);
5680 }
5681
5682 static void
5683 g_file_real_set_attributes_async (GFile               *file,
5684                                   GFileInfo           *info,
5685                                   GFileQueryInfoFlags  flags,
5686                                   int                  io_priority,
5687                                   GCancellable        *cancellable,
5688                                   GAsyncReadyCallback  callback,
5689                                   gpointer             user_data)
5690 {
5691   GTask *task;
5692   SetInfoAsyncData *data;
5693
5694   data = g_new0 (SetInfoAsyncData, 1);
5695   data->info = g_file_info_dup (info);
5696   data->flags = flags;
5697
5698   task = g_task_new (file, cancellable, callback, user_data);
5699   g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
5700   g_task_set_priority (task, io_priority);
5701
5702   g_task_run_in_thread (task, set_info_async_thread);
5703   g_object_unref (task);
5704 }
5705
5706 static gboolean
5707 g_file_real_set_attributes_finish (GFile         *file,
5708                                    GAsyncResult  *res,
5709                                    GFileInfo    **info,
5710                                    GError       **error)
5711 {
5712   SetInfoAsyncData *data;
5713
5714   g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5715
5716   data = g_task_get_task_data (G_TASK (res));
5717
5718   if (info)
5719     *info = g_object_ref (data->info);
5720
5721   if (error != NULL && data->error)
5722     *error = g_error_copy (data->error);
5723
5724   return data->res;
5725 }
5726
5727 static void
5728 find_enclosing_mount_async_thread (GTask        *task,
5729                                    gpointer      object,
5730                                    gpointer      task_data,
5731                                    GCancellable *cancellable)
5732 {
5733   GError *error = NULL;
5734   GMount *mount;
5735
5736   mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5737
5738   if (mount == NULL)
5739     g_task_return_error (task, error);
5740   else
5741     g_task_return_pointer (task, mount, g_object_unref);
5742 }
5743
5744 static void
5745 g_file_real_find_enclosing_mount_async (GFile               *file,
5746                                         int                  io_priority,
5747                                         GCancellable        *cancellable,
5748                                         GAsyncReadyCallback  callback,
5749                                         gpointer             user_data)
5750 {
5751   GTask *task;
5752
5753   task = g_task_new (file, cancellable, callback, user_data);
5754   g_task_set_priority (task, io_priority);
5755
5756   g_task_run_in_thread (task, find_enclosing_mount_async_thread);
5757   g_object_unref (task);
5758 }
5759
5760 static GMount *
5761 g_file_real_find_enclosing_mount_finish (GFile         *file,
5762                                          GAsyncResult  *res,
5763                                          GError       **error)
5764 {
5765   g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5766
5767   return g_task_propagate_pointer (G_TASK (res), error);
5768 }
5769
5770
5771 typedef struct {
5772   GFile *source;
5773   GFile *destination;
5774   GFileCopyFlags flags;
5775   GFileProgressCallback progress_cb;
5776   gpointer progress_cb_data;
5777 } CopyAsyncData;
5778
5779 static void
5780 copy_async_data_free (CopyAsyncData *data)
5781 {
5782   g_object_unref (data->source);
5783   g_object_unref (data->destination);
5784   g_slice_free (CopyAsyncData, data);
5785 }
5786
5787 typedef struct {
5788   CopyAsyncData *data;
5789   goffset current_num_bytes;
5790   goffset total_num_bytes;
5791 } ProgressData;
5792
5793 static gboolean
5794 copy_async_progress_in_main (gpointer user_data)
5795 {
5796   ProgressData *progress = user_data;
5797   CopyAsyncData *data = progress->data;
5798
5799   data->progress_cb (progress->current_num_bytes,
5800                      progress->total_num_bytes,
5801                      data->progress_cb_data);
5802
5803   return FALSE;
5804 }
5805
5806 static void
5807 copy_async_progress_callback (goffset  current_num_bytes,
5808                               goffset  total_num_bytes,
5809                               gpointer user_data)
5810 {
5811   GTask *task = user_data;
5812   CopyAsyncData *data = g_task_get_task_data (task);
5813   ProgressData *progress;
5814
5815   progress = g_new (ProgressData, 1);
5816   progress->data = data;
5817   progress->current_num_bytes = current_num_bytes;
5818   progress->total_num_bytes = total_num_bytes;
5819
5820   g_main_context_invoke_full (g_task_get_context (task),
5821                               g_task_get_priority (task),
5822                               copy_async_progress_in_main,
5823                               progress,
5824                               g_free);
5825 }
5826
5827 static void
5828 copy_async_thread (GTask        *task,
5829                    gpointer      source,
5830                    gpointer      task_data,
5831                    GCancellable *cancellable)
5832 {
5833   CopyAsyncData *data = task_data;
5834   gboolean result;
5835   GError *error = NULL;
5836
5837   result = g_file_copy (data->source,
5838                         data->destination,
5839                         data->flags,
5840                         cancellable,
5841                         (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5842                         task,
5843                         &error);
5844   if (result)
5845     g_task_return_boolean (task, TRUE);
5846   else
5847     g_task_return_error (task, error);
5848 }
5849
5850 static void
5851 g_file_real_copy_async (GFile                  *source,
5852                         GFile                  *destination,
5853                         GFileCopyFlags          flags,
5854                         int                     io_priority,
5855                         GCancellable           *cancellable,
5856                         GFileProgressCallback   progress_callback,
5857                         gpointer                progress_callback_data,
5858                         GAsyncReadyCallback     callback,
5859                         gpointer                user_data)
5860 {
5861   GTask *task;
5862   CopyAsyncData *data;
5863
5864   data = g_slice_new (CopyAsyncData);
5865   data->source = g_object_ref (source);
5866   data->destination = g_object_ref (destination);
5867   data->flags = flags;
5868   data->progress_cb = progress_callback;
5869   data->progress_cb_data = progress_callback_data;
5870
5871   task = g_task_new (source, cancellable, callback, user_data);
5872   g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
5873   g_task_set_priority (task, io_priority);
5874   g_task_run_in_thread (task, copy_async_thread);
5875   g_object_unref (task);
5876 }
5877
5878 static gboolean
5879 g_file_real_copy_finish (GFile        *file,
5880                          GAsyncResult *res,
5881                          GError      **error)
5882 {
5883   g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5884
5885   return g_task_propagate_boolean (G_TASK (res), error);
5886 }
5887
5888
5889 /********************************************
5890  *   Default VFS operations                 *
5891  ********************************************/
5892
5893 /**
5894  * g_file_new_for_path:
5895  * @path: a string containing a relative or absolute path.
5896  *     The string must be encoded in the glib filename encoding.
5897  *
5898  * Constructs a #GFile for a given path. This operation never
5899  * fails, but the returned object might not support any I/O
5900  * operation if @path is malformed.
5901  *
5902  * Returns: (transfer full): a new #GFile for the given @path.
5903  *   Free the returned object with g_object_unref().
5904  */
5905 GFile *
5906 g_file_new_for_path (const char *path)
5907 {
5908   g_return_val_if_fail (path != NULL, NULL);
5909
5910   return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5911 }
5912
5913 /**
5914  * g_file_new_for_uri:
5915  * @uri: a UTF-8 string containing a URI
5916  *
5917  * Constructs a #GFile for a given URI. This operation never
5918  * fails, but the returned object might not support any I/O
5919  * operation if @uri is malformed or if the uri type is
5920  * not supported.
5921  *
5922  * Returns: (transfer full): a new #GFile for the given @uri.
5923  *     Free the returned object with g_object_unref().
5924  */
5925 GFile *
5926 g_file_new_for_uri (const char *uri)
5927 {
5928   g_return_val_if_fail (uri != NULL, NULL);
5929
5930   return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
5931 }
5932
5933 /**
5934  * g_file_new_tmp:
5935  * @tmpl: (type filename) (allow-none): Template for the file
5936  *   name, as in g_file_open_tmp(), or %NULL for a default template
5937  * @iostream: (out): on return, a #GFileIOStream for the created file
5938  * @error: a #GError, or %NULL
5939  *
5940  * Opens a file in the preferred directory for temporary files (as
5941  * returned by g_get_tmp_dir()) and returns a #GFile and
5942  * #GFileIOStream pointing to it.
5943  *
5944  * @tmpl should be a string in the GLib file name encoding
5945  * containing a sequence of six 'X' characters, and containing no
5946  * directory components. If it is %NULL, a default template is used.
5947  *
5948  * Unlike the other #GFile constructors, this will return %NULL if
5949  * a temporary file could not be created.
5950  *
5951  * Returns: (transfer full): a new #GFile.
5952  *     Free the returned object with g_object_unref().
5953  *
5954  * Since: 2.32
5955  */
5956 GFile *
5957 g_file_new_tmp (const char     *tmpl,
5958                 GFileIOStream **iostream,
5959                 GError        **error)
5960 {
5961   gint fd;
5962   gchar *path;
5963   GFile *file;
5964   GFileOutputStream *output;
5965
5966   g_return_val_if_fail (iostream != NULL, NULL);
5967
5968   fd = g_file_open_tmp (tmpl, &path, error);
5969   if (fd == -1)
5970     return NULL;
5971
5972   file = g_file_new_for_path (path);
5973
5974   output = _g_local_file_output_stream_new (fd);
5975   *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
5976
5977   g_object_unref (output);
5978   g_free (path);
5979
5980   return file;
5981 }
5982
5983 /**
5984  * g_file_parse_name:
5985  * @parse_name: a file name or path to be parsed
5986  *
5987  * Constructs a #GFile with the given @parse_name (i.e. something
5988  * given by g_file_get_parse_name()). This operation never fails,
5989  * but the returned object might not support any I/O operation if
5990  * the @parse_name cannot be parsed.
5991  *
5992  * Returns: (transfer full): a new #GFile.
5993  */
5994 GFile *
5995 g_file_parse_name (const char *parse_name)
5996 {
5997   g_return_val_if_fail (parse_name != NULL, NULL);
5998
5999   return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6000 }
6001
6002 static gboolean
6003 is_valid_scheme_character (char c)
6004 {
6005   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6006 }
6007
6008 /* Following RFC 2396, valid schemes are built like:
6009  *       scheme        = alpha *( alpha | digit | "+" | "-" | "." )
6010  */
6011 static gboolean
6012 has_valid_scheme (const char *uri)
6013 {
6014   const char *p;
6015
6016   p = uri;
6017
6018   if (!g_ascii_isalpha (*p))
6019     return FALSE;
6020
6021   do {
6022     p++;
6023   } while (is_valid_scheme_character (*p));
6024
6025   return *p == ':';
6026 }
6027
6028 static GFile *
6029 new_for_cmdline_arg (const gchar *arg,
6030                      const gchar *cwd)
6031 {
6032   GFile *file;
6033   char *filename;
6034
6035   if (g_path_is_absolute (arg))
6036     return g_file_new_for_path (arg);
6037
6038   if (has_valid_scheme (arg))
6039     return g_file_new_for_uri (arg);
6040
6041   if (cwd == NULL)
6042     {
6043       char *current_dir;
6044
6045       current_dir = g_get_current_dir ();
6046       filename = g_build_filename (current_dir, arg, NULL);
6047       g_free (current_dir);
6048     }
6049   else
6050     filename = g_build_filename (cwd, arg, NULL);
6051
6052   file = g_file_new_for_path (filename);
6053   g_free (filename);
6054
6055   return file;
6056 }
6057
6058 /**
6059  * g_file_new_for_commandline_arg:
6060  * @arg: a command line string
6061  *
6062  * Creates a #GFile with the given argument from the command line.
6063  * The value of @arg can be either a URI, an absolute path or a
6064  * relative path resolved relative to the current working directory.
6065  * This operation never fails, but the returned object might not
6066  * support any I/O operation if @arg points to a malformed path.
6067  *
6068  * Returns: (transfer full): a new #GFile.
6069  *    Free the returned object with g_object_unref().
6070  */
6071 GFile *
6072 g_file_new_for_commandline_arg (const char *arg)
6073 {
6074   g_return_val_if_fail (arg != NULL, NULL);
6075
6076   return new_for_cmdline_arg (arg, NULL);
6077 }
6078
6079 /**
6080  * g_file_new_for_commandline_arg_and_cwd:
6081  * @arg: a command line string
6082  * @cwd: the current working directory of the commandline
6083  *
6084  * Creates a #GFile with the given argument from the command line.
6085  *
6086  * This function is similar to g_file_new_for_commandline_arg() except
6087  * that it allows for passing the current working directory as an
6088  * argument instead of using the current working directory of the
6089  * process.
6090  *
6091  * This is useful if the commandline argument was given in a context
6092  * other than the invocation of the current process.
6093  *
6094  * See also g_application_command_line_create_file_for_arg().
6095  *
6096  * Returns: (transfer full): a new #GFile
6097  *
6098  * Since: 2.36
6099  **/
6100 GFile *
6101 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6102                                         const gchar *cwd)
6103 {
6104   g_return_val_if_fail (arg != NULL, NULL);
6105   g_return_val_if_fail (cwd != NULL, NULL);
6106
6107   return new_for_cmdline_arg (arg, cwd);
6108 }
6109
6110 /**
6111  * g_file_mount_enclosing_volume:
6112  * @location: input #GFile
6113  * @flags: flags affecting the operation
6114  * @mount_operation: (allow-none): a #GMountOperation
6115  *     or %NULL to avoid user interaction
6116  * @cancellable: (allow-none): optional #GCancellable object,
6117  *     %NULL to ignore
6118  * @callback: (allow-none): a #GAsyncReadyCallback to call
6119  *     when the request is satisfied, or %NULL
6120  * @user_data: the data to pass to callback function
6121  *
6122  * Starts a @mount_operation, mounting the volume that contains
6123  * the file @location.
6124  *
6125  * When this operation has completed, @callback will be called with
6126  * @user_user data, and the operation can be finalized with
6127  * g_file_mount_enclosing_volume_finish().
6128  *
6129  * If @cancellable is not %NULL, then the operation can be cancelled by
6130  * triggering the cancellable object from another thread. If the operation
6131  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6132  */
6133 void
6134 g_file_mount_enclosing_volume (GFile               *location,
6135                                GMountMountFlags     flags,
6136                                GMountOperation     *mount_operation,
6137                                GCancellable        *cancellable,
6138                                GAsyncReadyCallback  callback,
6139                                gpointer             user_data)
6140 {
6141   GFileIface *iface;
6142
6143   g_return_if_fail (G_IS_FILE (location));
6144
6145   iface = G_FILE_GET_IFACE (location);
6146
6147   if (iface->mount_enclosing_volume == NULL)
6148     {
6149       g_task_report_new_error (location, callback, user_data,
6150                                g_file_mount_enclosing_volume,
6151                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6152                                _("volume doesn't implement mount"));
6153       return;
6154     }
6155
6156   (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6157
6158 }
6159
6160 /**
6161  * g_file_mount_enclosing_volume_finish:
6162  * @location: input #GFile
6163  * @result: a #GAsyncResult
6164  * @error: a #GError, or %NULL
6165  *
6166  * Finishes a mount operation started by g_file_mount_enclosing_volume().
6167  *
6168  * Returns: %TRUE if successful. If an error has occurred,
6169  *     this function will return %FALSE and set @error
6170  *     appropriately if present.
6171  */
6172 gboolean
6173 g_file_mount_enclosing_volume_finish (GFile         *location,
6174                                       GAsyncResult  *result,
6175                                       GError       **error)
6176 {
6177   GFileIface *iface;
6178
6179   g_return_val_if_fail (G_IS_FILE (location), FALSE);
6180   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6181
6182   if (g_async_result_legacy_propagate_error (result, error))
6183     return FALSE;
6184   else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6185     return g_task_propagate_boolean (G_TASK (result), error);
6186
6187   iface = G_FILE_GET_IFACE (location);
6188
6189   return (* iface->mount_enclosing_volume_finish) (location, result, error);
6190 }
6191
6192 /********************************************
6193  *   Utility functions                      *
6194  ********************************************/
6195
6196 /**
6197  * g_file_query_default_handler:
6198  * @file: a #GFile to open
6199  * @cancellable: optional #GCancellable object, %NULL to ignore
6200  * @error: a #GError, or %NULL
6201  *
6202  * Returns the #GAppInfo that is registered as the default
6203  * application to handle the file specified by @file.
6204  *
6205  * If @cancellable is not %NULL, then the operation can be cancelled by
6206  * triggering the cancellable object from another thread. If the operation
6207  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6208  *
6209  * Returns: (transfer full): a #GAppInfo if the handle was found,
6210  *     %NULL if there were errors.
6211  *     When you are done with it, release it with g_object_unref()
6212  */
6213 GAppInfo *
6214 g_file_query_default_handler (GFile         *file,
6215                               GCancellable  *cancellable,
6216                               GError       **error)
6217 {
6218   char *uri_scheme;
6219   const char *content_type;
6220   GAppInfo *appinfo;
6221   GFileInfo *info;
6222   char *path;
6223
6224   uri_scheme = g_file_get_uri_scheme (file);
6225   if (uri_scheme && uri_scheme[0] != '\0')
6226     {
6227       appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6228       g_free (uri_scheme);
6229
6230       if (appinfo != NULL)
6231         return appinfo;
6232     }
6233
6234   info = g_file_query_info (file,
6235                             G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6236                             0,
6237                             cancellable,
6238                             error);
6239   if (info == NULL)
6240     return NULL;
6241
6242   appinfo = NULL;
6243
6244   content_type = g_file_info_get_content_type (info);
6245   if (content_type)
6246     {
6247       /* Don't use is_native(), as we want to support fuse paths if available */
6248       path = g_file_get_path (file);
6249       appinfo = g_app_info_get_default_for_type (content_type,
6250                                                  path == NULL);
6251       g_free (path);
6252     }
6253
6254   g_object_unref (info);
6255
6256   if (appinfo != NULL)
6257     return appinfo;
6258
6259   g_set_error_literal (error, G_IO_ERROR,
6260                        G_IO_ERROR_NOT_SUPPORTED,
6261                        _("No application is registered as handling this file"));
6262   return NULL;
6263 }
6264
6265 #define GET_CONTENT_BLOCK_SIZE 8192
6266
6267 /**
6268  * g_file_load_contents:
6269  * @file: input #GFile
6270  * @cancellable: optional #GCancellable object, %NULL to ignore
6271  * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6272  * @length: (out) (allow-none): a location to place the length of the contents of the file,
6273  *    or %NULL if the length is not needed
6274  * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6275  *    or %NULL if the entity tag is not needed
6276  * @error: a #GError, or %NULL
6277  *
6278  * Loads the content of the file into memory. The data is always
6279  * zero-terminated, but this is not included in the resultant @length.
6280  * The returned @content should be freed with g_free() when no longer
6281  * needed.
6282  *
6283  * If @cancellable is not %NULL, then the operation can be cancelled by
6284  * triggering the cancellable object from another thread. If the operation
6285  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6286  *
6287  * Returns: %TRUE if the @file's contents were successfully loaded.
6288  *     %FALSE if there were errors.
6289  */
6290 gboolean
6291 g_file_load_contents (GFile         *file,
6292                       GCancellable  *cancellable,
6293                       char         **contents,
6294                       gsize         *length,
6295                       char         **etag_out,
6296                       GError       **error)
6297 {
6298   GFileInputStream *in;
6299   GByteArray *content;
6300   gsize pos;
6301   gssize res;
6302   GFileInfo *info;
6303
6304   g_return_val_if_fail (G_IS_FILE (file), FALSE);
6305   g_return_val_if_fail (contents != NULL, FALSE);
6306
6307   in = g_file_read (file, cancellable, error);
6308   if (in == NULL)
6309     return FALSE;
6310
6311   content = g_byte_array_new ();
6312   pos = 0;
6313
6314   g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6315   while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6316                                      content->data + pos,
6317                                      GET_CONTENT_BLOCK_SIZE,
6318                                      cancellable, error)) > 0)
6319     {
6320       pos += res;
6321       g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6322     }
6323
6324   if (etag_out)
6325     {
6326       *etag_out = NULL;
6327
6328       info = g_file_input_stream_query_info (in,
6329                                              G_FILE_ATTRIBUTE_ETAG_VALUE,
6330                                              cancellable,
6331                                              NULL);
6332       if (info)
6333         {
6334           *etag_out = g_strdup (g_file_info_get_etag (info));
6335           g_object_unref (info);
6336         }
6337     }
6338
6339   /* Ignore errors on close */
6340   g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6341   g_object_unref (in);
6342
6343   if (res < 0)
6344     {
6345       /* error is set already */
6346       g_byte_array_free (content, TRUE);
6347       return FALSE;
6348     }
6349
6350   if (length)
6351     *length = pos;
6352
6353   /* Zero terminate (we got an extra byte allocated for this */
6354   content->data[pos] = 0;
6355
6356   *contents = (char *)g_byte_array_free (content, FALSE);
6357
6358   return TRUE;
6359 }
6360
6361 typedef struct {
6362   GTask *task;
6363   GFileReadMoreCallback read_more_callback;
6364   GByteArray *content;
6365   gsize pos;
6366   char *etag;
6367 } LoadContentsData;
6368
6369
6370 static void
6371 load_contents_data_free (LoadContentsData *data)
6372 {
6373   if (data->content)
6374     g_byte_array_free (data->content, TRUE);
6375   g_free (data->etag);
6376   g_free (data);
6377 }
6378
6379 static void
6380 load_contents_close_callback (GObject      *obj,
6381                               GAsyncResult *close_res,
6382                               gpointer      user_data)
6383 {
6384   GInputStream *stream = G_INPUT_STREAM (obj);
6385   LoadContentsData *data = user_data;
6386
6387   /* Ignore errors here, we're only reading anyway */
6388   g_input_stream_close_finish (stream, close_res, NULL);
6389   g_object_unref (stream);
6390
6391   g_task_return_boolean (data->task, TRUE);
6392   g_object_unref (data->task);
6393 }
6394
6395 static void
6396 load_contents_fstat_callback (GObject      *obj,
6397                               GAsyncResult *stat_res,
6398                               gpointer      user_data)
6399 {
6400   GInputStream *stream = G_INPUT_STREAM (obj);
6401   LoadContentsData *data = user_data;
6402   GFileInfo *info;
6403
6404   info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6405                                                 stat_res, NULL);
6406   if (info)
6407     {
6408       data->etag = g_strdup (g_file_info_get_etag (info));
6409       g_object_unref (info);
6410     }
6411
6412   g_input_stream_close_async (stream, 0,
6413                               g_task_get_cancellable (data->task),
6414                               load_contents_close_callback, data);
6415 }
6416
6417 static void
6418 load_contents_read_callback (GObject      *obj,
6419                              GAsyncResult *read_res,
6420                              gpointer      user_data)
6421 {
6422   GInputStream *stream = G_INPUT_STREAM (obj);
6423   LoadContentsData *data = user_data;
6424   GError *error = NULL;
6425   gssize read_size;
6426
6427   read_size = g_input_stream_read_finish (stream, read_res, &error);
6428
6429   if (read_size < 0)
6430     {
6431       /* EOF, close the file */
6432       g_task_return_error (data->task, error);
6433       g_input_stream_close_async (stream, 0,
6434                                   g_task_get_cancellable (data->task),
6435                                   load_contents_close_callback, data);
6436     }
6437   else if (read_size == 0)
6438     {
6439       g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6440                                             G_FILE_ATTRIBUTE_ETAG_VALUE,
6441                                             0,
6442                                             g_task_get_cancellable (data->task),
6443                                             load_contents_fstat_callback,
6444                                             data);
6445     }
6446   else if (read_size > 0)
6447     {
6448       data->pos += read_size;
6449
6450       g_byte_array_set_size (data->content,
6451                              data->pos + GET_CONTENT_BLOCK_SIZE);
6452
6453
6454       if (data->read_more_callback &&
6455           !data->read_more_callback ((char *)data->content->data, data->pos,
6456                                      g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6457         g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6458                                               G_FILE_ATTRIBUTE_ETAG_VALUE,
6459                                               0,
6460                                               g_task_get_cancellable (data->task),
6461                                               load_contents_fstat_callback,
6462                                               data);
6463       else
6464         g_input_stream_read_async (stream,
6465                                    data->content->data + data->pos,
6466                                    GET_CONTENT_BLOCK_SIZE,
6467                                    0,
6468                                    g_task_get_cancellable (data->task),
6469                                    load_contents_read_callback,
6470                                    data);
6471     }
6472 }
6473
6474 static void
6475 load_contents_open_callback (GObject      *obj,
6476                              GAsyncResult *open_res,
6477                              gpointer      user_data)
6478 {
6479   GFile *file = G_FILE (obj);
6480   GFileInputStream *stream;
6481   LoadContentsData *data = user_data;
6482   GError *error = NULL;
6483
6484   stream = g_file_read_finish (file, open_res, &error);
6485
6486   if (stream)
6487     {
6488       g_byte_array_set_size (data->content,
6489                              data->pos + GET_CONTENT_BLOCK_SIZE);
6490       g_input_stream_read_async (G_INPUT_STREAM (stream),
6491                                  data->content->data + data->pos,
6492                                  GET_CONTENT_BLOCK_SIZE,
6493                                  0,
6494                                  g_task_get_cancellable (data->task),
6495                                  load_contents_read_callback,
6496                                  data);
6497     }
6498   else
6499     {
6500       g_task_return_error (data->task, error);
6501       g_object_unref (data->task);
6502     }
6503 }
6504
6505 /**
6506  * g_file_load_partial_contents_async: (skip)
6507  * @file: input #GFile
6508  * @cancellable: optional #GCancellable object, %NULL to ignore
6509  * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6510  *     and to specify whether further data should be read
6511  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6512  * @user_data: the data to pass to the callback functions
6513  *
6514  * Reads the partial contents of a file. A #GFileReadMoreCallback should
6515  * be used to stop reading from the file when appropriate, else this
6516  * function will behave exactly as g_file_load_contents_async(). This
6517  * operation can be finished by g_file_load_partial_contents_finish().
6518  *
6519  * Users of this function should be aware that @user_data is passed to
6520  * both the @read_more_callback and the @callback.
6521  *
6522  * If @cancellable is not %NULL, then the operation can be cancelled by
6523  * triggering the cancellable object from another thread. If the operation
6524  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6525  */
6526 void
6527 g_file_load_partial_contents_async (GFile                 *file,
6528                                     GCancellable          *cancellable,
6529                                     GFileReadMoreCallback  read_more_callback,
6530                                     GAsyncReadyCallback    callback,
6531                                     gpointer               user_data)
6532 {
6533   LoadContentsData *data;
6534
6535   g_return_if_fail (G_IS_FILE (file));
6536
6537   data = g_new0 (LoadContentsData, 1);
6538   data->read_more_callback = read_more_callback;
6539   data->content = g_byte_array_new ();
6540
6541   data->task = g_task_new (file, cancellable, callback, user_data);
6542   g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6543
6544   g_file_read_async (file,
6545                      0,
6546                      g_task_get_cancellable (data->task),
6547                      load_contents_open_callback,
6548                      data);
6549 }
6550
6551 /**
6552  * g_file_load_partial_contents_finish:
6553  * @file: input #GFile
6554  * @res: a #GAsyncResult
6555  * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6556  * @length: (out) (allow-none): a location to place the length of the contents of the file,
6557  *     or %NULL if the length is not needed
6558  * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6559  *     or %NULL if the entity tag is not needed
6560  * @error: a #GError, or %NULL
6561  *
6562  * Finishes an asynchronous partial load operation that was started
6563  * with g_file_load_partial_contents_async(). The data is always
6564  * zero-terminated, but this is not included in the resultant @length.
6565  * The returned @content should be freed with g_free() when no longer
6566  * needed.
6567  *
6568  * Returns: %TRUE if the load was successful. If %FALSE and @error is
6569  *     present, it will be set appropriately.
6570  */
6571 gboolean
6572 g_file_load_partial_contents_finish (GFile         *file,
6573                                      GAsyncResult  *res,
6574                                      char         **contents,
6575                                      gsize         *length,
6576                                      char         **etag_out,
6577                                      GError       **error)
6578 {
6579   GTask *task;
6580   LoadContentsData *data;
6581
6582   g_return_val_if_fail (G_IS_FILE (file), FALSE);
6583   g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6584   g_return_val_if_fail (contents != NULL, FALSE);
6585
6586   task = G_TASK (res);
6587
6588   if (!g_task_propagate_boolean (task, error))
6589     {
6590       if (length)
6591         *length = 0;
6592       return FALSE;
6593     }
6594
6595   data = g_task_get_task_data (task);
6596
6597   if (length)
6598     *length = data->pos;
6599
6600   if (etag_out)
6601     {
6602       *etag_out = data->etag;
6603       data->etag = NULL;
6604     }
6605
6606   /* Zero terminate */
6607   g_byte_array_set_size (data->content, data->pos + 1);
6608   data->content->data[data->pos] = 0;
6609
6610   *contents = (char *)g_byte_array_free (data->content, FALSE);
6611   data->content = NULL;
6612
6613   return TRUE;
6614 }
6615
6616 /**
6617  * g_file_load_contents_async:
6618  * @file: input #GFile
6619  * @cancellable: optional #GCancellable object, %NULL to ignore
6620  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6621  * @user_data: the data to pass to callback function
6622  *
6623  * Starts an asynchronous load of the @file's contents.
6624  *
6625  * For more details, see g_file_load_contents() which is
6626  * the synchronous version of this call.
6627  *
6628  * When the load operation has completed, @callback will be called
6629  * with @user data. To finish the operation, call
6630  * g_file_load_contents_finish() with the #GAsyncResult returned by
6631  * the @callback.
6632  *
6633  * If @cancellable is not %NULL, then the operation can be cancelled by
6634  * triggering the cancellable object from another thread. If the operation
6635  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6636  */
6637 void
6638 g_file_load_contents_async (GFile               *file,
6639                            GCancellable        *cancellable,
6640                            GAsyncReadyCallback  callback,
6641                            gpointer             user_data)
6642 {
6643   g_file_load_partial_contents_async (file,
6644                                       cancellable,
6645                                       NULL,
6646                                       callback, user_data);
6647 }
6648
6649 /**
6650  * g_file_load_contents_finish:
6651  * @file: input #GFile
6652  * @res: a #GAsyncResult
6653  * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6654  * @length: (out) (allow-none): a location to place the length of the contents of the file,
6655  *     or %NULL if the length is not needed
6656  * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6657  *     or %NULL if the entity tag is not needed
6658  * @error: a #GError, or %NULL
6659  *
6660  * Finishes an asynchronous load of the @file's contents.
6661  * The contents are placed in @contents, and @length is set to the
6662  * size of the @contents string. The @content should be freed with
6663  * g_free() when no longer needed. If @etag_out is present, it will be
6664  * set to the new entity tag for the @file.
6665  *
6666  * Returns: %TRUE if the load was successful. If %FALSE and @error is
6667  *     present, it will be set appropriately.
6668  */
6669 gboolean
6670 g_file_load_contents_finish (GFile         *file,
6671                              GAsyncResult  *res,
6672                              char         **contents,
6673                              gsize         *length,
6674                              char         **etag_out,
6675                              GError       **error)
6676 {
6677   return g_file_load_partial_contents_finish (file,
6678                                               res,
6679                                               contents,
6680                                               length,
6681                                               etag_out,
6682                                               error);
6683 }
6684
6685 /**
6686  * g_file_replace_contents:
6687  * @file: input #GFile
6688  * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
6689  * @length: the length of @contents in bytes
6690  * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6691  *     for the document, or %NULL
6692  * @make_backup: %TRUE if a backup should be created
6693  * @flags: a set of #GFileCreateFlags
6694  * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6695  *      for the document. This should be freed with g_free() when no longer
6696  *      needed, or %NULL
6697  * @cancellable: optional #GCancellable object, %NULL to ignore
6698  * @error: a #GError, or %NULL
6699  *
6700  * Replaces the contents of @file with @contents of @length bytes.
6701  *
6702  * If @etag is specified (not %NULL), any existing file must have that etag,
6703  * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
6704  *
6705  * If @make_backup is %TRUE, this function will attempt to make a backup
6706  * of @file.
6707  *
6708  * If @cancellable is not %NULL, then the operation can be cancelled by
6709  * triggering the cancellable object from another thread. If the operation
6710  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6711  *
6712  * The returned @new_etag can be used to verify that the file hasn't
6713  * changed the next time it is saved over.
6714  *
6715  * Returns: %TRUE if successful. If an error has occurred, this function
6716  *     will return %FALSE and set @error appropriately if present.
6717  */
6718 gboolean
6719 g_file_replace_contents (GFile             *file,
6720                          const char        *contents,
6721                          gsize              length,
6722                          const char        *etag,
6723                          gboolean           make_backup,
6724                          GFileCreateFlags   flags,
6725                          char             **new_etag,
6726                          GCancellable      *cancellable,
6727                          GError           **error)
6728 {
6729   GFileOutputStream *out;
6730   gsize pos, remainder;
6731   gssize res;
6732   gboolean ret;
6733
6734   g_return_val_if_fail (G_IS_FILE (file), FALSE);
6735   g_return_val_if_fail (contents != NULL, FALSE);
6736
6737   out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6738   if (out == NULL)
6739     return FALSE;
6740
6741   pos = 0;
6742   remainder = length;
6743   while (remainder > 0 &&
6744          (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6745                                        contents + pos,
6746                                        MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6747                                        cancellable,
6748                                        error)) > 0)
6749     {
6750       pos += res;
6751       remainder -= res;
6752     }
6753
6754   if (remainder > 0 && res < 0)
6755     {
6756       /* Ignore errors on close */
6757       g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6758       g_object_unref (out);
6759
6760       /* error is set already */
6761       return FALSE;
6762     }
6763
6764   ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6765
6766   if (new_etag)
6767     *new_etag = g_file_output_stream_get_etag (out);
6768
6769   g_object_unref (out);
6770
6771   return ret;
6772 }
6773
6774 typedef struct {
6775   GTask *task;
6776   const char *content;
6777   gsize length;
6778   gsize pos;
6779   char *etag;
6780   gboolean failed;
6781 } ReplaceContentsData;
6782
6783 static void
6784 replace_contents_data_free (ReplaceContentsData *data)
6785 {
6786   g_free (data->etag);
6787   g_free (data);
6788 }
6789
6790 static void
6791 replace_contents_close_callback (GObject      *obj,
6792                                  GAsyncResult *close_res,
6793                                  gpointer      user_data)
6794 {
6795   GOutputStream *stream = G_OUTPUT_STREAM (obj);
6796   ReplaceContentsData *data = user_data;
6797
6798   /* Ignore errors here, we're only reading anyway */
6799   g_output_stream_close_finish (stream, close_res, NULL);
6800   g_object_unref (stream);
6801
6802   if (!data->failed)
6803     {
6804       data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6805       g_task_return_boolean (data->task, TRUE);
6806     }
6807   g_object_unref (data->task);
6808 }
6809
6810 static void
6811 replace_contents_write_callback (GObject      *obj,
6812                                  GAsyncResult *read_res,
6813                                  gpointer      user_data)
6814 {
6815   GOutputStream *stream = G_OUTPUT_STREAM (obj);
6816   ReplaceContentsData *data = user_data;
6817   GError *error = NULL;
6818   gssize write_size;
6819
6820   write_size = g_output_stream_write_finish (stream, read_res, &error);
6821
6822   if (write_size <= 0)
6823     {
6824       /* Error or EOF, close the file */
6825       if (write_size < 0)
6826         {
6827           data->failed = TRUE;
6828           g_task_return_error (data->task, error);
6829         }
6830       g_output_stream_close_async (stream, 0,
6831                                    g_task_get_cancellable (data->task),
6832                                    replace_contents_close_callback, data);
6833     }
6834   else if (write_size > 0)
6835     {
6836       data->pos += write_size;
6837
6838       if (data->pos >= data->length)
6839         g_output_stream_close_async (stream, 0,
6840                                      g_task_get_cancellable (data->task),
6841                                      replace_contents_close_callback, data);
6842       else
6843         g_output_stream_write_async (stream,
6844                                      data->content + data->pos,
6845                                      data->length - data->pos,
6846                                      0,
6847                                      g_task_get_cancellable (data->task),
6848                                      replace_contents_write_callback,
6849                                      data);
6850     }
6851 }
6852
6853 static void
6854 replace_contents_open_callback (GObject      *obj,
6855                                 GAsyncResult *open_res,
6856                                 gpointer      user_data)
6857 {
6858   GFile *file = G_FILE (obj);
6859   GFileOutputStream *stream;
6860   ReplaceContentsData *data = user_data;
6861   GError *error = NULL;
6862
6863   stream = g_file_replace_finish (file, open_res, &error);
6864
6865   if (stream)
6866     {
6867       g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6868                                    data->content + data->pos,
6869                                    data->length - data->pos,
6870                                    0,
6871                                    g_task_get_cancellable (data->task),
6872                                    replace_contents_write_callback,
6873                                    data);
6874     }
6875   else
6876     {
6877       g_task_return_error (data->task, error);
6878       g_object_unref (data->task);
6879     }
6880 }
6881
6882 /**
6883  * g_file_replace_contents_async:
6884  * @file: input #GFile
6885  * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
6886  * @length: the length of @contents in bytes
6887  * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6888  * @make_backup: %TRUE if a backup should be created
6889  * @flags: a set of #GFileCreateFlags
6890  * @cancellable: optional #GCancellable object, %NULL to ignore
6891  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6892  * @user_data: the data to pass to callback function
6893  *
6894  * Starts an asynchronous replacement of @file with the given
6895  * @contents of @length bytes. @etag will replace the document's
6896  * current entity tag.
6897  *
6898  * When this operation has completed, @callback will be called with
6899  * @user_user data, and the operation can be finalized with
6900  * g_file_replace_contents_finish().
6901  *
6902  * If @cancellable is not %NULL, then the operation can be cancelled by
6903  * triggering the cancellable object from another thread. If the operation
6904  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6905  *
6906  * If @make_backup is %TRUE, this function will attempt to
6907  * make a backup of @file.
6908  */
6909 void
6910 g_file_replace_contents_async  (GFile               *file,
6911                                 const char          *contents,
6912                                 gsize                length,
6913                                 const char          *etag,
6914                                 gboolean             make_backup,
6915                                 GFileCreateFlags     flags,
6916                                 GCancellable        *cancellable,
6917                                 GAsyncReadyCallback  callback,
6918                                 gpointer             user_data)
6919 {
6920   ReplaceContentsData *data;
6921
6922   g_return_if_fail (G_IS_FILE (file));
6923   g_return_if_fail (contents != NULL);
6924
6925   data = g_new0 (ReplaceContentsData, 1);
6926
6927   data->content = contents;
6928   data->length = length;
6929
6930   data->task = g_task_new (file, cancellable, callback, user_data);
6931   g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
6932
6933   g_file_replace_async (file,
6934                         etag,
6935                         make_backup,
6936                         flags,
6937                         0,
6938                         g_task_get_cancellable (data->task),
6939                         replace_contents_open_callback,
6940                         data);
6941 }
6942
6943 /**
6944  * g_file_replace_contents_finish:
6945  * @file: input #GFile
6946  * @res: a #GAsyncResult
6947  * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
6948  *     for the document. This should be freed with g_free() when it is no
6949  *     longer needed, or %NULL
6950  * @error: a #GError, or %NULL
6951  *
6952  * Finishes an asynchronous replace of the given @file. See
6953  * g_file_replace_contents_async(). Sets @new_etag to the new entity
6954  * tag for the document, if present.
6955  *
6956  * Returns: %TRUE on success, %FALSE on failure.
6957  */
6958 gboolean
6959 g_file_replace_contents_finish (GFile         *file,
6960                                 GAsyncResult  *res,
6961                                 char         **new_etag,
6962                                 GError       **error)
6963 {
6964   GTask *task;
6965   ReplaceContentsData *data;
6966
6967   g_return_val_if_fail (G_IS_FILE (file), FALSE);
6968   g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6969
6970   task = G_TASK (res);
6971
6972   if (!g_task_propagate_boolean (task, error))
6973     return FALSE;
6974
6975   data = g_task_get_task_data (task);
6976
6977   if (new_etag)
6978     {
6979       *new_etag = data->etag;
6980       data->etag = NULL; /* Take ownership */
6981     }
6982
6983   return TRUE;
6984 }
6985
6986 /**
6987  * g_file_start_mountable:
6988  * @file: input #GFile
6989  * @flags: flags affecting the operation
6990  * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
6991  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
6992  * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
6993  * @user_data: the data to pass to callback function
6994  *
6995  * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
6996  * Using @start_operation, you can request callbacks when, for instance,
6997  * passwords are needed during authentication.
6998  *
6999  * If @cancellable is not %NULL, then the operation can be cancelled by
7000  * triggering the cancellable object from another thread. If the operation
7001  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7002  *
7003  * When the operation is finished, @callback will be called.
7004  * You can then call g_file_mount_mountable_finish() to get
7005  * the result of the operation.
7006  *
7007  * Since: 2.22
7008  */
7009 void
7010 g_file_start_mountable (GFile               *file,
7011                         GDriveStartFlags     flags,
7012                         GMountOperation     *start_operation,
7013                         GCancellable        *cancellable,
7014                         GAsyncReadyCallback  callback,
7015                         gpointer             user_data)
7016 {
7017   GFileIface *iface;
7018
7019   g_return_if_fail (G_IS_FILE (file));
7020
7021   iface = G_FILE_GET_IFACE (file);
7022
7023   if (iface->start_mountable == NULL)
7024     {
7025       g_task_report_new_error (file, callback, user_data,
7026                                g_file_start_mountable,
7027                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7028                                _("Operation not supported"));
7029       return;
7030     }
7031
7032   (* iface->start_mountable) (file,
7033                               flags,
7034                               start_operation,
7035                               cancellable,
7036                               callback,
7037                               user_data);
7038 }
7039
7040 /**
7041  * g_file_start_mountable_finish:
7042  * @file: input #GFile
7043  * @result: a #GAsyncResult
7044  * @error: a #GError, or %NULL
7045  *
7046  * Finishes a start operation. See g_file_start_mountable() for details.
7047  *
7048  * Finish an asynchronous start operation that was started
7049  * with g_file_start_mountable().
7050  *
7051  * Returns: %TRUE if the operation finished successfully. %FALSE
7052  * otherwise.
7053  *
7054  * Since: 2.22
7055  */
7056 gboolean
7057 g_file_start_mountable_finish (GFile         *file,
7058                                GAsyncResult  *result,
7059                                GError       **error)
7060 {
7061   GFileIface *iface;
7062
7063   g_return_val_if_fail (G_IS_FILE (file), FALSE);
7064   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7065
7066   if (g_async_result_legacy_propagate_error (result, error))
7067     return FALSE;
7068   else if (g_async_result_is_tagged (result, g_file_start_mountable))
7069     return g_task_propagate_boolean (G_TASK (result), error);
7070
7071   iface = G_FILE_GET_IFACE (file);
7072   return (* iface->start_mountable_finish) (file, result, error);
7073 }
7074
7075 /**
7076  * g_file_stop_mountable:
7077  * @file: input #GFile
7078  * @flags: flags affecting the operation
7079  * @mount_operation: (allow-none): a #GMountOperation,
7080  *     or %NULL to avoid user interaction.
7081  * @cancellable: (allow-none): optional #GCancellable object,
7082  *     %NULL to ignore
7083  * @callback: (allow-none): a #GAsyncReadyCallback to call
7084  *     when the request is satisfied, or %NULL
7085  * @user_data: the data to pass to callback function
7086  *
7087  * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7088  *
7089  * If @cancellable is not %NULL, then the operation can be cancelled by
7090  * triggering the cancellable object from another thread. If the operation
7091  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7092  *
7093  * When the operation is finished, @callback will be called.
7094  * You can then call g_file_stop_mountable_finish() to get
7095  * the result of the operation.
7096  *
7097  * Since: 2.22
7098  */
7099 void
7100 g_file_stop_mountable (GFile               *file,
7101                        GMountUnmountFlags   flags,
7102                        GMountOperation     *mount_operation,
7103                        GCancellable        *cancellable,
7104                        GAsyncReadyCallback  callback,
7105                        gpointer             user_data)
7106 {
7107   GFileIface *iface;
7108
7109   g_return_if_fail (G_IS_FILE (file));
7110
7111   iface = G_FILE_GET_IFACE (file);
7112
7113   if (iface->stop_mountable == NULL)
7114     {
7115       g_task_report_new_error (file, callback, user_data,
7116                                g_file_stop_mountable,
7117                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7118                                _("Operation not supported"));
7119       return;
7120     }
7121
7122   (* iface->stop_mountable) (file,
7123                              flags,
7124                              mount_operation,
7125                              cancellable,
7126                              callback,
7127                              user_data);
7128 }
7129
7130 /**
7131  * g_file_stop_mountable_finish:
7132  * @file: input #GFile
7133  * @result: a #GAsyncResult
7134  * @error: a #GError, or %NULL
7135  *
7136  * Finishes an stop operation, see g_file_stop_mountable() for details.
7137  *
7138  * Finish an asynchronous stop operation that was started
7139  * with g_file_stop_mountable().
7140  *
7141  * Returns: %TRUE if the operation finished successfully.
7142  *     %FALSE otherwise.
7143  *
7144  * Since: 2.22
7145  */
7146 gboolean
7147 g_file_stop_mountable_finish (GFile         *file,
7148                               GAsyncResult  *result,
7149                               GError       **error)
7150 {
7151   GFileIface *iface;
7152
7153   g_return_val_if_fail (G_IS_FILE (file), FALSE);
7154   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7155
7156   if (g_async_result_legacy_propagate_error (result, error))
7157     return FALSE;
7158   else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7159     return g_task_propagate_boolean (G_TASK (result), error);
7160
7161   iface = G_FILE_GET_IFACE (file);
7162   return (* iface->stop_mountable_finish) (file, result, error);
7163 }
7164
7165 /**
7166  * g_file_poll_mountable:
7167  * @file: input #GFile
7168  * @cancellable: optional #GCancellable object, %NULL to ignore
7169  * @callback: (allow-none): a #GAsyncReadyCallback to call
7170  *     when the request is satisfied, or %NULL
7171  * @user_data: the data to pass to callback function
7172  *
7173  * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7174  *
7175  * If @cancellable is not %NULL, then the operation can be cancelled by
7176  * triggering the cancellable object from another thread. If the operation
7177  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7178  *
7179  * When the operation is finished, @callback will be called.
7180  * You can then call g_file_mount_mountable_finish() to get
7181  * the result of the operation.
7182  *
7183  * Since: 2.22
7184  */
7185 void
7186 g_file_poll_mountable (GFile               *file,
7187                        GCancellable        *cancellable,
7188                        GAsyncReadyCallback  callback,
7189                        gpointer             user_data)
7190 {
7191   GFileIface *iface;
7192
7193   g_return_if_fail (G_IS_FILE (file));
7194
7195   iface = G_FILE_GET_IFACE (file);
7196
7197   if (iface->poll_mountable == NULL)
7198     {
7199       g_task_report_new_error (file, callback, user_data,
7200                                g_file_poll_mountable,
7201                                G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7202                                _("Operation not supported"));
7203       return;
7204     }
7205
7206   (* iface->poll_mountable) (file,
7207                              cancellable,
7208                              callback,
7209                              user_data);
7210 }
7211
7212 /**
7213  * g_file_poll_mountable_finish:
7214  * @file: input #GFile
7215  * @result: a #GAsyncResult
7216  * @error: a #GError, or %NULL
7217  *
7218  * Finishes a poll operation. See g_file_poll_mountable() for details.
7219  *
7220  * Finish an asynchronous poll operation that was polled
7221  * with g_file_poll_mountable().
7222  *
7223  * Returns: %TRUE if the operation finished successfully. %FALSE
7224  * otherwise.
7225  *
7226  * Since: 2.22
7227  */
7228 gboolean
7229 g_file_poll_mountable_finish (GFile         *file,
7230                               GAsyncResult  *result,
7231                               GError       **error)
7232 {
7233   GFileIface *iface;
7234
7235   g_return_val_if_fail (G_IS_FILE (file), FALSE);
7236   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7237
7238   if (g_async_result_legacy_propagate_error (result, error))
7239     return FALSE;
7240   else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7241     return g_task_propagate_boolean (G_TASK (result), error);
7242
7243   iface = G_FILE_GET_IFACE (file);
7244   return (* iface->poll_mountable_finish) (file, result, error);
7245 }
7246
7247 /**
7248  * g_file_supports_thread_contexts:
7249  * @file: a #GFile
7250  *
7251  * Checks if @file supports <link
7252  * linkend="g-main-context-push-thread-default-context">thread-default
7253  * contexts</link>. If this returns %FALSE, you cannot perform
7254  * asynchronous operations on @file in a thread that has a
7255  * thread-default context.
7256  *
7257  * Returns: Whether or not @file supports thread-default contexts.
7258  *
7259  * Since: 2.22
7260  */
7261 gboolean
7262 g_file_supports_thread_contexts (GFile *file)
7263 {
7264  GFileIface *iface;
7265
7266  g_return_val_if_fail (G_IS_FILE (file), FALSE);
7267
7268  iface = G_FILE_GET_IFACE (file);
7269  return iface->supports_thread_contexts;
7270 }