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