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