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