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