Add new g_file_has_prefix that does the same as g_file_contains_file.
[platform/upstream/glib.git] / gio / gfile.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #ifdef HAVE_PWD_H
27 #include <pwd.h>
28 #endif
29 #include "gfile.h"
30 #include "gvfs.h"
31 #include "gioscheduler.h"
32 #include <glocalfile.h>
33 #include "gsimpleasyncresult.h"
34 #include "gfileattribute-priv.h"
35 #include "gpollfilemonitor.h"
36 #include "glibintl.h"
37
38 #include "gioalias.h"
39
40 /**
41  * SECTION:gfile
42  * @short_description: File and Directory Handling
43  * @include: gio.h
44  * @see_also: #GFileInfo, #GFileEnumerator
45  * 
46  * #GFile is a high level abstraction for manipulating files on a 
47  * virtual file system. #GFile<!-- -->s are lightweight, immutable 
48  * objects that do no I/O upon creation. It is necessary to understand that
49  * #GFile objects do not represent files, merely a handle to a file. All
50  * file I/O is implemented as streaming operations (see #GInputStream and 
51  * #GOutputStream).
52  * 
53  * To construct a #GFile, you can use: 
54  * g_file_new_for_path() if you have a path.
55  * g_file_new_for_uri() if you have a URI.
56  * g_file_new_for_commandline_arg() for a command line argument.
57  * 
58  * You can move through the file system with #GFile handles with
59  * g_file_get_parent() to get a handle to the parent directory.
60  * g_file_get_child() to get a handle to a child within a directory.
61  * g_file_resolve_relative_path() to resolve a relative path between
62  * two #GFile<!-- -->s.
63  * 
64  * Many #GFile operations have both synchronous and asynchronous versions 
65  * to suit your application. Asynchronous versions of synchronous functions 
66  * simply have _async() appended to their function names. The asynchronous 
67  * I/O functions call a #GAsyncReadyCallback which is then used to finalize 
68  * the operation, producing a GAsyncResult which is then passed to the 
69  * function's matching _finish() 
70  * operation. 
71  *
72  * Some #GFile operations do not have synchronous analogs, as they may
73  * take a very long time to finish, and blocking may leave an application
74  * unusable. Notable cases include:
75  * g_file_mount_mountable() to mount a mountable file.
76  * g_file_unmount_mountable() to unmount a mountable file.
77  * g_file_eject_mountable() to eject a mountable file.
78  * 
79  * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
80  * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for 
81  * short. Entity tags are somewhat like a more abstract version of the 
82  * traditional mtime, and can be used to quickly determine if the file has
83  * been modified from the version on the file system. See the HTTP 1.1 
84  * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
85  * for HTTP Etag headers, which are a very similar concept.
86  * </para>
87  **/
88
89 static void g_file_base_init (gpointer g_class);
90 static void g_file_class_init (gpointer g_class,
91                                gpointer class_data);
92
93 static void               g_file_real_query_info_async            (GFile                  *file,
94                                                                    const char             *attributes,
95                                                                    GFileQueryInfoFlags     flags,
96                                                                    int                     io_priority,
97                                                                    GCancellable           *cancellable,
98                                                                    GAsyncReadyCallback     callback,
99                                                                    gpointer                user_data);
100 static GFileInfo *        g_file_real_query_info_finish           (GFile                  *file,
101                                                                    GAsyncResult           *res,
102                                                                    GError                **error);
103 static void               g_file_real_enumerate_children_async    (GFile                  *file,
104                                                                    const char             *attributes,
105                                                                    GFileQueryInfoFlags     flags,
106                                                                    int                     io_priority,
107                                                                    GCancellable           *cancellable,
108                                                                    GAsyncReadyCallback     callback,
109                                                                    gpointer                user_data);
110 static GFileEnumerator *  g_file_real_enumerate_children_finish   (GFile                  *file,
111                                                                    GAsyncResult           *res,
112                                                                    GError                **error);
113 static void               g_file_real_read_async                  (GFile                  *file,
114                                                                    int                     io_priority,
115                                                                    GCancellable           *cancellable,
116                                                                    GAsyncReadyCallback     callback,
117                                                                    gpointer                user_data);
118 static GFileInputStream * g_file_real_read_finish                 (GFile                  *file,
119                                                                    GAsyncResult           *res,
120                                                                    GError                **error);
121 static void               g_file_real_append_to_async             (GFile                  *file,
122                                                                    GFileCreateFlags        flags,
123                                                                    int                     io_priority,
124                                                                    GCancellable           *cancellable,
125                                                                    GAsyncReadyCallback     callback,
126                                                                    gpointer                user_data);
127 static GFileOutputStream *g_file_real_append_to_finish            (GFile                  *file,
128                                                                    GAsyncResult           *res,
129                                                                    GError                **error);
130 static void               g_file_real_create_async                (GFile                  *file,
131                                                                    GFileCreateFlags        flags,
132                                                                    int                     io_priority,
133                                                                    GCancellable           *cancellable,
134                                                                    GAsyncReadyCallback     callback,
135                                                                    gpointer                user_data);
136 static GFileOutputStream *g_file_real_create_finish               (GFile                  *file,
137                                                                    GAsyncResult           *res,
138                                                                    GError                **error);
139 static void               g_file_real_replace_async               (GFile                  *file,
140                                                                    const char             *etag,
141                                                                    gboolean                make_backup,
142                                                                    GFileCreateFlags        flags,
143                                                                    int                     io_priority,
144                                                                    GCancellable           *cancellable,
145                                                                    GAsyncReadyCallback     callback,
146                                                                    gpointer                user_data);
147 static GFileOutputStream *g_file_real_replace_finish              (GFile                  *file,
148                                                                    GAsyncResult           *res,
149                                                                    GError                **error);
150 static gboolean           g_file_real_set_attributes_from_info    (GFile                  *file,
151                                                                    GFileInfo              *info,
152                                                                    GFileQueryInfoFlags     flags,
153                                                                    GCancellable           *cancellable,
154                                                                    GError                **error);
155 static void               g_file_real_set_display_name_async      (GFile                  *file,
156                                                                    const char             *display_name,
157                                                                    int                     io_priority,
158                                                                    GCancellable           *cancellable,
159                                                                    GAsyncReadyCallback     callback,
160                                                                    gpointer                user_data);
161 static GFile *            g_file_real_set_display_name_finish     (GFile                  *file,
162                                                                    GAsyncResult           *res,
163                                                                    GError                **error);
164 static void               g_file_real_set_attributes_async        (GFile                  *file,
165                                                                    GFileInfo              *info,
166                                                                    GFileQueryInfoFlags     flags,
167                                                                    int                     io_priority,
168                                                                    GCancellable           *cancellable,
169                                                                    GAsyncReadyCallback     callback,
170                                                                    gpointer                user_data);
171 static gboolean           g_file_real_set_attributes_finish       (GFile                  *file,
172                                                                    GAsyncResult           *res,
173                                                                    GFileInfo             **info,
174                                                                    GError                **error);
175 static void               g_file_real_find_enclosing_mount_async  (GFile                  *file,
176                                                                    int                     io_priority,
177                                                                    GCancellable           *cancellable,
178                                                                    GAsyncReadyCallback     callback,
179                                                                    gpointer                user_data);
180 static GMount *           g_file_real_find_enclosing_mount_finish (GFile                  *file,
181                                                                    GAsyncResult           *res,
182                                                                    GError                **error);
183 static void               g_file_real_copy_async                  (GFile                  *source,
184                                                                    GFile                  *destination,
185                                                                    GFileCopyFlags          flags,
186                                                                    int                     io_priority,
187                                                                    GCancellable           *cancellable,
188                                                                    GFileProgressCallback   progress_callback,
189                                                                    gpointer                progress_callback_data,
190                                                                    GAsyncReadyCallback     callback,
191                                                                    gpointer                user_data);
192 static gboolean           g_file_real_copy_finish                 (GFile                  *file,
193                                                                    GAsyncResult           *res,
194                                                                    GError                **error);
195
196 GType
197 g_file_get_type (void)
198 {
199   static GType file_type = 0;
200
201   if (! file_type)
202     {
203       static const GTypeInfo file_info =
204       {
205         sizeof (GFileIface), /* class_size */
206         g_file_base_init,   /* base_init */
207         NULL,           /* base_finalize */
208         g_file_class_init,
209         NULL,           /* class_finalize */
210         NULL,           /* class_data */
211         0,
212         0,              /* n_preallocs */
213         NULL
214       };
215
216       file_type =
217         g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
218                                 &file_info, 0);
219
220       g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
221     }
222
223   return file_type;
224 }
225
226 static void
227 g_file_class_init (gpointer g_class,
228                    gpointer class_data)
229 {
230   GFileIface *iface = g_class;
231
232   iface->enumerate_children_async = g_file_real_enumerate_children_async;
233   iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
234   iface->set_display_name_async = g_file_real_set_display_name_async;
235   iface->set_display_name_finish = g_file_real_set_display_name_finish;
236   iface->query_info_async = g_file_real_query_info_async;
237   iface->query_info_finish = g_file_real_query_info_finish;
238   iface->set_attributes_async = g_file_real_set_attributes_async;
239   iface->set_attributes_finish = g_file_real_set_attributes_finish;
240   iface->read_async = g_file_real_read_async;
241   iface->read_finish = g_file_real_read_finish;
242   iface->append_to_async = g_file_real_append_to_async;
243   iface->append_to_finish = g_file_real_append_to_finish;
244   iface->create_async = g_file_real_create_async;
245   iface->create_finish = g_file_real_create_finish;
246   iface->replace_async = g_file_real_replace_async;
247   iface->replace_finish = g_file_real_replace_finish;
248   iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
249   iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
250   iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
251   iface->copy_async = g_file_real_copy_async;
252   iface->copy_finish = g_file_real_copy_finish;
253 }
254
255 static void
256 g_file_base_init (gpointer g_class)
257 {
258 }
259
260
261 /**
262  * g_file_is_native:
263  * @file: input #GFile.
264  *
265  * Checks to see if a file is native to the platform.
266  *
267  * A native file s one expressed in the platform-native filename format,
268  * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
269  * as it might be on a locally mounted remote filesystem.
270  *
271  * On some systems non-native files may be available using
272  * the native filesystem via a userspace filesystem (FUSE), in
273  * these cases this call will return %FALSE, but g_file_get_path()
274  * will still return a native path.
275  *
276  * This call does no blocking i/o.
277  * 
278  * Returns: %TRUE if file is native. 
279  **/
280 gboolean
281 g_file_is_native (GFile *file)
282 {
283   GFileIface *iface;
284
285   g_return_val_if_fail (G_IS_FILE (file), FALSE);
286
287   iface = G_FILE_GET_IFACE (file);
288
289   return (* iface->is_native) (file);
290 }
291
292
293 /**
294  * g_file_has_uri_scheme: 
295  * @file: input #GFile.
296  * @uri_scheme: a string containing a URI scheme.
297  *
298  * Checks to see if a #GFile has a given URI scheme.
299  *
300  * This call does no blocking i/o.
301  * 
302  * Returns: %TRUE if #GFile's backend supports the
303  *     given URI scheme, %FALSE if URI scheme is %NULL,
304  *     not supported, or #GFile is invalid.
305  **/
306 gboolean
307 g_file_has_uri_scheme (GFile      *file,
308                        const char *uri_scheme)
309 {
310   GFileIface *iface;
311   
312   g_return_val_if_fail (G_IS_FILE (file), FALSE);
313   g_return_val_if_fail (uri_scheme != NULL, FALSE);
314
315   iface = G_FILE_GET_IFACE (file);
316
317   return (* iface->has_uri_scheme) (file, uri_scheme);
318 }
319
320
321 /**
322  * g_file_get_uri_scheme:
323  * @file: input #GFile.
324  *
325  * Gets the URI scheme for a #GFile.
326  * RFC 3986 decodes the scheme as:
327  * <programlisting>
328  * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] 
329  * </programlisting>
330  * Common schemes include "file", "http", "ftp", etc. 
331  *
332  * This call does no blocking i/o.
333  * 
334  * Returns: a string containing the URI scheme for the given 
335  *     #GFile. The returned string should be freed with g_free() 
336  *     when no longer needed.
337  **/
338 char *
339 g_file_get_uri_scheme (GFile *file)
340 {
341   GFileIface *iface;
342
343   g_return_val_if_fail (G_IS_FILE (file), NULL);
344
345   iface = G_FILE_GET_IFACE (file);
346
347   return (* iface->get_uri_scheme) (file);
348 }
349
350
351 /**
352  * g_file_get_basename:
353  * @file: input #GFile.
354  *
355  * Gets the base name (the last component of the path) for a given #GFile.
356  *
357  * If called for the top level of a system (such as the filesystem root
358  * or a uri like sftp://host/ it will return a single directory separator
359  * (and on Windows, possibly a drive letter).
360  *
361  * This call does no blocking i/o.
362  * 
363  * Returns: string containing the #GFile's base name, or %NULL 
364  *     if given #GFile is invalid. The returned string should be 
365  *     freed with g_free() when no longer needed.
366  **/
367 char *
368 g_file_get_basename (GFile *file)
369 {
370   GFileIface *iface;
371   
372   g_return_val_if_fail (G_IS_FILE (file), NULL);
373
374   iface = G_FILE_GET_IFACE (file);
375
376   return (* iface->get_basename) (file);
377 }
378
379 /**
380  * g_file_get_path:
381  * @file: input #GFile.
382  *
383  * Gets the local pathname for #GFile, if one exists. 
384  *
385  * This call does no blocking i/o.
386  * 
387  * Returns: string containing the #GFile's path, or %NULL if 
388  *     no such path exists. The returned string should be 
389  *     freed with g_free() when no longer needed.
390  **/
391 char *
392 g_file_get_path (GFile *file)
393 {
394   GFileIface *iface;
395
396   g_return_val_if_fail (G_IS_FILE (file), NULL);
397
398   iface = G_FILE_GET_IFACE (file);
399
400   return (* iface->get_path) (file);
401 }
402
403 /**
404  * g_file_get_uri:
405  * @file: input #GFile.
406  *
407  * Gets the URI for the @file.
408  *
409  * This call does no blocking i/o.
410  * 
411  * Returns: a string containing the #GFile's URI.
412  *     The returned string should be freed with g_free() when no longer needed.
413  **/
414 char *
415 g_file_get_uri (GFile *file)
416 {
417   GFileIface *iface;
418   
419   g_return_val_if_fail (G_IS_FILE (file), NULL);
420
421   iface = G_FILE_GET_IFACE (file);
422
423   return (* iface->get_uri) (file);
424 }
425
426 /**
427  * g_file_get_parse_name:
428  * @file: input #GFile.
429  *
430  * Gets the parse name of the @file.
431  * A parse name is a UTF-8 string that describes the
432  * file such that one can get the #GFile back using
433  * g_file_parse_name().
434  *
435  * This is generally used to show the #GFile as a nice
436  * string in a user interface, like in a location entry.
437  *
438  * For local files with names that can safely be converted
439  * to UTF8 the pathname is used, otherwise the IRI is used
440  * (a form of URI that allows UTF8 characters unescaped).
441  *
442  * This call does no blocking i/o.
443  * 
444  * Returns: a string containing the #GFile's parse name. The returned 
445  *     string should be freed with g_free() when no longer needed.
446  **/
447 char *
448 g_file_get_parse_name (GFile *file)
449 {
450   GFileIface *iface;
451   
452   g_return_val_if_fail (G_IS_FILE (file), NULL);
453
454   iface = G_FILE_GET_IFACE (file);
455
456   return (* iface->get_parse_name) (file);
457 }
458
459 /**
460  * g_file_dup:
461  * @file: input #GFile.
462  *
463  * Duplicates a #GFile handle. This operation does not duplicate 
464  * the actual file or directory represented by the #GFile; see 
465  * g_file_copy() if attempting to copy a file. 
466  *
467  * This call does no blocking i/o.
468  * 
469  * Returns: #GFile that is a duplicate of the given #GFile. 
470  **/
471 GFile *
472 g_file_dup (GFile *file)
473 {
474   GFileIface *iface;
475   
476   g_return_val_if_fail (G_IS_FILE (file), NULL);
477
478   iface = G_FILE_GET_IFACE (file);
479
480   return (* iface->dup) (file);
481 }
482
483 /**
484  * g_file_hash:
485  * @file: #gconstpointer to a #GFile.
486  *
487  * Creates a hash value for a #GFile.
488  *
489  * This call does no blocking i/o.
490  * 
491  * Returns: 0 if @file is not a valid #GFile, otherwise an 
492  *     integer that can be used as hash value for the #GFile. 
493  *     This function is intended for easily hashing a #GFile to 
494  *     add to a #GHashTable or similar data structure.
495  **/
496 guint
497 g_file_hash (gconstpointer file)
498 {
499   GFileIface *iface;
500   
501   g_return_val_if_fail (G_IS_FILE (file), 0);
502
503   iface = G_FILE_GET_IFACE (file);
504
505   return (* iface->hash) ((GFile *)file);
506 }
507
508 /**
509  * g_file_equal:
510  * @file1: the first #GFile.
511  * @file2: the second #GFile.
512  *
513  * Checks equality of two given #GFile<!-- -->s
514  *
515  * This call does no blocking i/o.
516  * 
517  * Returns: %TRUE if @file1 and @file2 are equal.
518  *     %FALSE if either is not a #GFile.
519  **/
520 gboolean
521 g_file_equal (GFile *file1,
522               GFile *file2)
523 {
524   GFileIface *iface;
525   
526   g_return_val_if_fail (G_IS_FILE (file1), FALSE);
527   g_return_val_if_fail (G_IS_FILE (file2), FALSE);
528   
529   if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
530     return FALSE;
531
532   iface = G_FILE_GET_IFACE (file1);
533   
534   return (* iface->equal) (file1, file2);
535 }
536
537
538 /**
539  * g_file_get_parent:
540  * @file: input #GFile.
541  *
542  * Gets the parent directory for the @file. 
543  * If the @file represents the root directory of the 
544  * file system, then %NULL will be returned.
545  *
546  * This call does no blocking i/o.
547  * 
548  * Returns: a #GFile structure to the parent of the given
549  *     #GFile or %NULL if there is no parent. 
550  **/
551 GFile *
552 g_file_get_parent (GFile *file)
553 {
554   GFileIface *iface;
555   
556   g_return_val_if_fail (G_IS_FILE (file), NULL);
557
558   iface = G_FILE_GET_IFACE (file);
559
560   return (* iface->get_parent) (file);
561 }
562
563 /**
564  * g_file_get_child:
565  * @file: input #GFile.
566  * @name: string containing the child's name.
567  *
568  * Gets a specific child of @file with name equal to @name.
569  *
570  * Note that the file with that specific name might not exist, but
571  * you can still have a #GFile that points to it. You can use this
572  * for instance to create that file.
573  *
574  * This call does no blocking i/o.
575  * 
576  * Returns: a #GFile to a child specified by @name.
577  **/
578 GFile *
579 g_file_get_child (GFile      *file,
580                   const char *name)
581 {
582   g_return_val_if_fail (G_IS_FILE (file), NULL);
583   g_return_val_if_fail (name != NULL, NULL);
584
585   return g_file_resolve_relative_path (file, name);
586 }
587
588 /**
589  * g_file_get_child_for_display_name:
590  * @file: input #GFile.
591  * @display_name: string to a possible child.
592  * @error: #GError.
593  *
594  * Gets the child of @file for a given @display_name (i.e. a UTF8
595  * version of the name). If this function fails, it returns %NULL and @error will be 
596  * set. This is very useful when constructing a GFile for a new file
597  * and the user entered the filename in the user interface, for instance
598  * when you select a directory and type a filename in the file selector.
599  * 
600  * This call does no blocking i/o.
601  * 
602  * Returns: a #GFile to the specified child, or 
603  *     %NULL if the display name couldn't be converted.  
604  **/
605 GFile *
606 g_file_get_child_for_display_name (GFile      *file,
607                                    const char *display_name,
608                                    GError **error)
609 {
610   GFileIface *iface;
611   
612   g_return_val_if_fail (G_IS_FILE (file), NULL);
613   g_return_val_if_fail (display_name != NULL, NULL);
614
615   iface = G_FILE_GET_IFACE (file);
616
617   return (* iface->get_child_for_display_name) (file, display_name, error);
618 }
619
620 #undef g_file_contains_file
621
622 /**
623  * g_file_contains_file:
624  * @parent: input #GFile.
625  * @descendant: input #GFile.
626  *
627  * Deprecated version of g_file_has_prefix().
628  *
629  * Returns:  %TRUE if the @descendant's parent, grandparent, etc is @parent. %FALSE otherwise.
630  *
631  * Deprecated:2.16: The initial chosen name was unfortunate, as it
632  * may cause you to think this function did more than just
633  * filename comparisons.
634  */
635 gboolean
636 g_file_contains_file (GFile *parent,
637                       GFile *descendant)
638 {
639   /* This function is not in the header and will not be referenced by newly built code */
640   return g_file_has_prefix (descendant, parent);
641 }
642
643 /**
644  * g_file_has_prefix:
645  * @file: input #GFile.
646  * @prefix: input #GFile.
647  * 
648  * Checks whether @file has the prefix specified by @prefix. In other word, if the
649  * inital elements of @file<!-- -->s pathname match @prefix.
650  * 
651  * This call does no i/o, as it works purely on names. As such it can sometimes
652  * return %FALSE even if @file is inside a @prefix (from a filesystem point of view),
653  * because the prefix of @file is an alias of @prefix.
654  *
655  * Returns:  %TRUE if the @files's parent, grandparent, etc is @prefix. %FALSE otherwise.
656  **/
657 gboolean
658 g_file_has_prefix (GFile *file,
659                    GFile *prefix)
660 {
661   GFileIface *iface;
662   
663   g_return_val_if_fail (G_IS_FILE (file), FALSE);
664   g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
665
666   if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
667     return FALSE;
668   
669   iface = G_FILE_GET_IFACE (file);
670
671   /* The vtable function differs in arg order since we're
672      using the old contains_file call */
673   return (* iface->prefix_matches) (prefix, file);
674 }
675
676 /**
677  * g_file_get_relative_path:
678  * @parent: input #GFile.
679  * @descendant: input #GFile.
680  *
681  * Gets the path for @descendant relative to @parent. 
682  *
683  * This call does no blocking i/o.
684  * 
685  * Returns: string with the relative path from @descendant 
686  *     to @parent, or %NULL if @descendant is not a descendant of @parent. The returned string should be freed with 
687  *     g_free() when no longer needed.
688  **/
689 char *
690 g_file_get_relative_path (GFile *parent,
691                           GFile *descendant)
692 {
693   GFileIface *iface;
694   
695   g_return_val_if_fail (G_IS_FILE (parent), NULL);
696   g_return_val_if_fail (G_IS_FILE (descendant), NULL);
697
698   if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
699     return NULL;
700   
701   iface = G_FILE_GET_IFACE (parent);
702
703   return (* iface->get_relative_path) (parent, descendant);
704 }
705
706 /**
707  * g_file_resolve_relative_path:
708  * @file: input #GFile.
709  * @relative_path: a given relative path string.
710  *
711  * Resolves a relative path for @file to an absolute path.
712  *
713  * This call does no blocking i/o.
714  * 
715  * Returns: #GFile to the resolved path. %NULL if @relative_path 
716  *     is %NULL or if @file is invalid.
717  **/
718 GFile *
719 g_file_resolve_relative_path (GFile      *file,
720                               const char *relative_path)
721 {
722   GFileIface *iface;
723
724   g_return_val_if_fail (G_IS_FILE (file), NULL);
725   g_return_val_if_fail (relative_path != NULL, NULL);
726
727   iface = G_FILE_GET_IFACE (file);
728
729   return (* iface->resolve_relative_path) (file, relative_path);
730 }
731
732 /**
733  * g_file_enumerate_children:
734  * @file: input #GFile.
735  * @attributes: an attribute query string.
736  * @flags: a set of #GFileQueryInfoFlags.
737  * @cancellable: optional #GCancellable object, %NULL to ignore.
738  * @error: #GError for error reporting.
739  *
740  * Gets the requested information about the files in a directory. The result
741  * is a #GFileEnumerator object that will give out #GFileInfo objects for
742  * all the files in the directory.
743  *
744  * The @attribute value is a string that specifies the file attributes that
745  * should be gathered. It is not an error if it's not possible to read a particular
746  * requested attribute from a file - it just won't be set. @attribute should
747  * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
748  * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
749  * namespace. An example attribute query be "standard::*,owner::user".
750  * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
751  *
752  * If @cancellable is not %NULL, then the operation can be cancelled by
753  * triggering the cancellable object from another thread. If the operation
754  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
755  * 
756  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
757  * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
758  * Other errors are possible too.
759  *
760  * Returns: A #GFileEnumerator if successful, %NULL on error. 
761  **/
762 GFileEnumerator *
763 g_file_enumerate_children (GFile                *file,
764                            const char           *attributes,
765                            GFileQueryInfoFlags   flags,
766                            GCancellable         *cancellable,
767                            GError              **error)
768                            
769 {
770   GFileIface *iface;
771   
772   g_return_val_if_fail (G_IS_FILE (file), NULL);
773
774   if (g_cancellable_set_error_if_cancelled (cancellable, error))
775     return NULL;
776   
777   iface = G_FILE_GET_IFACE (file);
778
779   if (iface->enumerate_children == NULL)
780     {
781       g_set_error (error, G_IO_ERROR,
782                    G_IO_ERROR_NOT_SUPPORTED,
783                    _("Operation not supported"));
784       return NULL;
785     }
786
787   return (* iface->enumerate_children) (file, attributes, flags,
788                                         cancellable, error);
789 }
790
791 /**
792  * g_file_enumerate_children_async:
793  * @file: input #GFile.
794  * @attributes: an attribute query string.
795  * @flags: a set of #GFileQueryInfoFlags.
796  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
797  *     of the request.
798  * @cancellable: optional #GCancellable object, %NULL to ignore.
799  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
800  * @user_data: the data to pass to callback function
801  *
802  * Asynchronously gets the requested information about the files in a directory. The result
803  * is a #GFileEnumerator object that will give out #GFileInfo objects for
804  * all the files in the directory.
805  *
806  * For more details, see g_file_enumerate_children() which is
807  * the synchronous version of this call.
808  *
809  * When the operation is finished, @callback will be called. You can then call
810  * g_file_enumerate_children_finish() to get the result of the operation.
811  **/
812 void
813 g_file_enumerate_children_async (GFile               *file,
814                                  const char          *attributes,
815                                  GFileQueryInfoFlags  flags,
816                                  int                  io_priority,
817                                  GCancellable        *cancellable,
818                                  GAsyncReadyCallback  callback,
819                                  gpointer             user_data)
820 {
821   GFileIface *iface;
822
823   g_return_if_fail (G_IS_FILE (file));
824
825   iface = G_FILE_GET_IFACE (file);
826   (* iface->enumerate_children_async) (file,
827                                        attributes,
828                                        flags,
829                                        io_priority,
830                                        cancellable,
831                                        callback,
832                                        user_data);
833 }
834
835 /**
836  * g_file_enumerate_children_finish:
837  * @file: input #GFile.
838  * @res: a #GAsyncResult.
839  * @error: a #GError.
840  * 
841  * Finishes an async enumerate children operation.
842  * See g_file_enumerate_children_async().
843  *
844  * Returns: a #GFileEnumerator or %NULL if an error occurred.
845  **/
846 GFileEnumerator *
847 g_file_enumerate_children_finish (GFile         *file,
848                                   GAsyncResult  *res,
849                                   GError       **error)
850 {
851   GFileIface *iface;
852   
853   g_return_val_if_fail (G_IS_FILE (file), NULL);
854   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
855
856   if (G_IS_SIMPLE_ASYNC_RESULT (res))
857     {
858       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
859       if (g_simple_async_result_propagate_error (simple, error))
860         return NULL;
861     }
862   
863   iface = G_FILE_GET_IFACE (file);
864   return (* iface->enumerate_children_finish) (file, res, error);
865 }
866
867 /**
868  * g_file_query_exists:
869  * @file: input #GFile.
870  * @cancellable: optional #GCancellable object, %NULL to ignore.
871  *
872  * Utility function to check if a particular file exists. This is
873  * implemented using g_file_query_info() and as such does blocking I/O.
874  *
875  * Note that in many cases it is racy to first check for file existance
876  * and then execute something based on the outcome of that, because the
877  * file might have been created or removed inbetween the operations. The
878  * general approach to handling that is to not check, but just do the
879  * operation and handle the errors as they come.
880  *
881  * As an example of race-free checking, take the case of reading a file, and
882  * if it doesn't exist, creating it. There are two racy versions: read it, and
883  * on error create it; and: check if it exists, if not create it. These
884  * can both result in two processes creating the file (with perhaps a partially
885  * written file as the result). The correct approach is to always try to create
886  * the file with g_file_create() which will either atomically create the file
887  * or fail with a G_IO_ERROR_EXISTS error.
888  *
889  * However, in many cases an existance check is useful in a user
890  * interface, for instance to make a menu item sensitive/insensitive, so that
891  * you don't have to fool users that something is possible and then just show
892  * and error dialog. If you do this, you should make sure to also handle the
893  * errors that can happen due to races when you execute the operation.
894  * 
895  * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
896  */
897 gboolean
898 g_file_query_exists (GFile *file,
899                      GCancellable *cancellable)
900 {
901   GFileInfo *info;
902
903   info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
904                             G_FILE_QUERY_INFO_NONE,
905                             cancellable, NULL);
906   if (info != NULL)
907     {
908       g_object_unref (info);
909       return TRUE;
910     }
911   
912   return FALSE;
913 }
914
915 /**
916  * g_file_query_info:
917  * @file: input #GFile.
918  * @attributes: an attribute query string.
919  * @flags: a set of #GFileQueryInfoFlags.
920  * @cancellable: optional #GCancellable object, %NULL to ignore.
921  * @error: a #GError.
922  *
923  * Gets the requested information about specified @file. The result
924  * is a #GFileInfo object that contains key-value attributes (such as 
925  * the type or size of the file).
926  *
927  * The @attribute value is a string that specifies the file attributes that
928  * should be gathered. It is not an error if it's not possible to read a particular
929  * requested attribute from a file - it just won't be set. @attribute should
930  * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
931  * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
932  * namespace. An example attribute query be "standard::*,owner::user".
933  * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
934  * 
935  * If @cancellable is not %NULL, then the operation can be cancelled by
936  * triggering the cancellable object from another thread. If the operation
937  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
938  *
939  * For symlinks, normally the information about the target of the
940  * symlink is returned, rather than information about the symlink itself.
941  * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
942  * information about the symlink itself will be returned. Also, for symlinks
943  * that point to non-existing files the information about the symlink itself
944  * will be returned.
945  *
946  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
947  * Other errors are possible too, and depend on what kind of filesystem the file is on.
948  *
949  * Returns: a #GFileInfo for the given @file, or %NULL on error.
950  **/
951 GFileInfo *
952 g_file_query_info (GFile                *file,
953                    const char           *attributes,
954                    GFileQueryInfoFlags   flags,
955                    GCancellable         *cancellable,
956                    GError              **error)
957 {
958   GFileIface *iface;
959   
960   g_return_val_if_fail (G_IS_FILE (file), NULL);
961
962   if (g_cancellable_set_error_if_cancelled (cancellable, error))
963     return NULL;
964   
965   iface = G_FILE_GET_IFACE (file);
966
967   if (iface->query_info == NULL)
968     {
969       g_set_error (error, G_IO_ERROR,
970                    G_IO_ERROR_NOT_SUPPORTED,
971                    _("Operation not supported"));
972       return NULL;
973     }
974   
975   return (* iface->query_info) (file, attributes, flags, cancellable, error);
976 }
977
978 /**
979  * g_file_query_info_async:
980  * @file: input #GFile.
981  * @attributes: an attribute query string.
982  * @flags: a set of #GFileQueryInfoFlags.
983  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
984  *     of the request.
985  * @cancellable: optional #GCancellable object, %NULL to ignore. 
986  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
987  * @user_data: the data to pass to callback function
988  * 
989  * Asynchronously gets the requested information about specified @file. The result
990  * is a #GFileInfo object that contains key-value attributes (such as type or size
991  * for the file).
992  * 
993  * For more details, see g_file_query_info() which is
994  * the synchronous version of this call.
995  *
996  * When the operation is finished, @callback will be called. You can then call
997  * g_file_query_info_finish() to get the result of the operation.
998  **/
999 void
1000 g_file_query_info_async (GFile               *file,
1001                          const char          *attributes,
1002                          GFileQueryInfoFlags  flags,
1003                          int                  io_priority,
1004                          GCancellable        *cancellable,
1005                          GAsyncReadyCallback  callback,
1006                          gpointer             user_data)
1007 {
1008   GFileIface *iface;
1009   
1010   g_return_if_fail (G_IS_FILE (file));
1011
1012   iface = G_FILE_GET_IFACE (file);
1013   (* iface->query_info_async) (file,
1014                                attributes,
1015                                flags,
1016                                io_priority,
1017                                cancellable,
1018                                callback,
1019                                user_data);
1020 }
1021
1022 /**
1023  * g_file_query_info_finish:
1024  * @file: input #GFile.
1025  * @res: a #GAsyncResult. 
1026  * @error: a #GError. 
1027  * 
1028  * Finishes an asynchronous file info query. 
1029  * See g_file_query_info_async().
1030  * 
1031  * Returns: #GFileInfo for given @file or %NULL on error.
1032  **/
1033 GFileInfo *
1034 g_file_query_info_finish (GFile         *file,
1035                           GAsyncResult  *res,
1036                           GError       **error)
1037 {
1038   GFileIface *iface;
1039
1040   g_return_val_if_fail (G_IS_FILE (file), NULL);
1041   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1042
1043   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1044     {
1045       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1046       if (g_simple_async_result_propagate_error (simple, error))
1047         return NULL;
1048     }
1049   
1050   iface = G_FILE_GET_IFACE (file);
1051   return (* iface->query_info_finish) (file, res, error);
1052 }
1053
1054 /**
1055  * g_file_query_filesystem_info:
1056  * @file: input #GFile.
1057  * @attributes:  an attribute query string.
1058  * @cancellable: optional #GCancellable object, %NULL to ignore. 
1059  * @error: a #GError. 
1060  * 
1061  * Similar to g_file_query_info(), but obtains information
1062  * about the filesystem the @file is on, rather than the file itself.
1063  * For instance the amount of space available and the type of
1064  * the filesystem.
1065  *
1066  * The @attribute value is a string that specifies the file attributes that
1067  * should be gathered. It is not an error if it's not possible to read a particular
1068  * requested attribute from a file - it just won't be set. @attribute should
1069  * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1070  * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1071  * namespace. The standard namespace for filesystem attributes is "fs".
1072  * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1073  * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1074  * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1075  * 
1076  * If @cancellable is not %NULL, then the operation can be cancelled by
1077  * triggering the cancellable object from another thread. If the operation
1078  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1079  *
1080  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1081  * Other errors are possible too, and depend on what kind of filesystem the file is on.
1082  *
1083  * Returns: a #GFileInfo or %NULL if there was an error.
1084  **/
1085 GFileInfo *
1086 g_file_query_filesystem_info (GFile         *file,
1087                               const char    *attributes,
1088                               GCancellable  *cancellable,
1089                               GError       **error)
1090 {
1091   GFileIface *iface;
1092   
1093   g_return_val_if_fail (G_IS_FILE (file), NULL);
1094
1095   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1096     return NULL;
1097   
1098   iface = G_FILE_GET_IFACE (file);
1099
1100   if (iface->query_filesystem_info == NULL)
1101     {
1102       g_set_error (error, G_IO_ERROR,
1103                    G_IO_ERROR_NOT_SUPPORTED,
1104                    _("Operation not supported"));
1105       return NULL;
1106     }
1107   
1108   return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1109 }
1110
1111 /**
1112  * g_file_find_enclosing_mount:
1113  * @file: input #GFile.
1114  * @cancellable: optional #GCancellable object, %NULL to ignore. 
1115  * @error: a #GError. 
1116  *
1117  * Gets a #GMount for the #GFile. 
1118  *
1119  * If the #GFileIface for @file does not have a mount (e.g. possibly a 
1120  * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1121  * will be returned.
1122  * 
1123  * If @cancellable is not %NULL, then the operation can be cancelled by
1124  * triggering the cancellable object from another thread. If the operation
1125  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1126  * 
1127  * Returns: a #GMount where the @file is located or %NULL on error.
1128  **/
1129 GMount *
1130 g_file_find_enclosing_mount (GFile         *file,
1131                              GCancellable  *cancellable,
1132                              GError       **error)
1133 {
1134   GFileIface *iface;
1135
1136   g_return_val_if_fail (G_IS_FILE (file), NULL);
1137   
1138   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1139     return NULL;
1140
1141   iface = G_FILE_GET_IFACE (file);
1142   if (iface->find_enclosing_mount == NULL)
1143     {
1144       g_set_error (error, G_IO_ERROR,
1145                    G_IO_ERROR_NOT_FOUND,
1146                    _("Containing mount does not exist"));
1147       return NULL;
1148     }
1149   
1150   return (* iface->find_enclosing_mount) (file, cancellable, error);
1151 }
1152 /**
1153  * g_file_find_enclosing_mount_async:
1154  * @file: a #GFile
1155  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1156  *     of the request.
1157  * @cancellable: optional #GCancellable object, %NULL to ignore.
1158  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1159  * @user_data: the data to pass to callback function
1160  *
1161  * Asynchronously gets the mount for the file.
1162  *
1163  * For more details, see g_file_find_enclosing_mount() which is
1164  * the synchronous version of this call.
1165  *
1166  * When the operation is finished, @callback will be called. You can then call
1167  * g_file_find_enclosing_mount_finish() to get the result of the operation.
1168  */
1169 void
1170 g_file_find_enclosing_mount_async (GFile              *file,
1171                                    int                   io_priority,
1172                                    GCancellable         *cancellable,
1173                                    GAsyncReadyCallback   callback,
1174                                    gpointer              user_data)
1175 {
1176   GFileIface *iface;
1177
1178   g_return_if_fail (G_IS_FILE (file));
1179
1180   iface = G_FILE_GET_IFACE (file);
1181   (* iface->find_enclosing_mount_async) (file,
1182                                          io_priority,
1183                                          cancellable,
1184                                          callback,
1185                                          user_data);
1186 }
1187
1188 /**
1189  * g_file_find_enclosing_mount_finish:
1190  * @file: a #GFile
1191  * @res: a #GAsyncResult
1192  * @error: a #GError
1193  * 
1194  * Finishes an asynchronous find mount request. 
1195  * See g_file_find_enclosing_mount_async().
1196  * 
1197  * Returns: #GMount for given @file or %NULL on error.
1198  **/
1199 GMount *
1200 g_file_find_enclosing_mount_finish (GFile         *file,
1201                                     GAsyncResult  *res,
1202                                     GError       **error)
1203 {
1204   GFileIface *iface;
1205   
1206   g_return_val_if_fail (G_IS_FILE (file), NULL);
1207   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1208
1209   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1210     {
1211       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1212       if (g_simple_async_result_propagate_error (simple, error))
1213         return NULL;
1214     }
1215   
1216   iface = G_FILE_GET_IFACE (file);
1217   return (* iface->find_enclosing_mount_finish) (file, res, error);
1218 }
1219
1220
1221 /**
1222  * g_file_read:
1223  * @file: #GFile to read.
1224  * @cancellable: a #GCancellable
1225  * @error: a #GError, or %NULL
1226  *
1227  * Opens a file for reading. The result is a #GFileInputStream that
1228  * can be used to read the contents of the file.
1229  *
1230  * If @cancellable is not %NULL, then the operation can be cancelled by
1231  * triggering the cancellable object from another thread. If the operation
1232  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1233  * 
1234  * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1235  * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1236  * Other errors are possible too, and depend on what kind of filesystem the file is on.
1237  *
1238  * Returns: #GFileInputStream or %NULL on error.
1239  **/
1240 GFileInputStream *
1241 g_file_read (GFile         *file,
1242              GCancellable  *cancellable,
1243              GError       **error)
1244 {
1245   GFileIface *iface;
1246   
1247   g_return_val_if_fail (G_IS_FILE (file), NULL);
1248
1249   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1250     return NULL;
1251
1252   iface = G_FILE_GET_IFACE (file);
1253
1254   if (iface->read_fn == NULL)
1255     {
1256       g_set_error (error, G_IO_ERROR,
1257                    G_IO_ERROR_NOT_SUPPORTED,
1258                    _("Operation not supported"));
1259       return NULL;
1260     }
1261   
1262   return (* iface->read_fn) (file, cancellable, error);
1263 }
1264
1265 /**
1266  * g_file_append_to:
1267  * @file: input #GFile.
1268  * @flags: a set of #GFileCreateFlags.
1269  * @cancellable: optional #GCancellable object, %NULL to ignore.
1270  * @error: a #GError, or %NULL
1271  *
1272  * Gets an output stream for appending data to the file. If
1273  * the file doesn't already exist it is created.
1274  *
1275  * By default files created are generally readable by everyone,
1276  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1277  * will be made readable only to the current user, to the level that
1278  * is supported on the target filesystem.
1279  *
1280  * If @cancellable is not %NULL, then the operation can be cancelled by
1281  * triggering the cancellable object from another thread. If the operation
1282  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1283  *
1284  * Some file systems don't allow all file names, and may
1285  * return an G_IO_ERROR_INVALID_FILENAME error.
1286  * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1287  * returned. Other errors are possible too, and depend on what kind of
1288  * filesystem the file is on.
1289  * 
1290  * Returns: a #GFileOutputStream.
1291  **/
1292 GFileOutputStream *
1293 g_file_append_to (GFile             *file,
1294                   GFileCreateFlags   flags,
1295                   GCancellable      *cancellable,
1296                   GError           **error)
1297 {
1298   GFileIface *iface;
1299
1300   g_return_val_if_fail (G_IS_FILE (file), NULL);
1301
1302   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1303     return NULL;
1304   
1305   iface = G_FILE_GET_IFACE (file);
1306
1307   if (iface->append_to == NULL)
1308     {
1309       g_set_error (error, G_IO_ERROR,
1310                    G_IO_ERROR_NOT_SUPPORTED,
1311                    _("Operation not supported"));
1312       return NULL;
1313     }
1314   
1315   return (* iface->append_to) (file, flags, cancellable, error);
1316 }
1317
1318 /**
1319  * g_file_create:
1320  * @file: input #GFile.
1321  * @flags: a set of #GFileCreateFlags.
1322  * @cancellable: optional #GCancellable object, %NULL to ignore.
1323  * @error: a #GError, or %NULL
1324  *
1325  * Creates a new file and returns an output stream for writing to it.
1326  * The file must not already exists.
1327  *
1328  * By default files created are generally readable by everyone,
1329  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1330  * will be made readable only to the current user, to the level that
1331  * is supported on the target filesystem.
1332  *
1333  * If @cancellable is not %NULL, then the operation can be cancelled by
1334  * triggering the cancellable object from another thread. If the operation
1335  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1336  *
1337  * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1338  * error will be returned.
1339  * Some file systems don't allow all file names, and may
1340  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1341  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1342  * Other errors are possible too, and depend on what kind of
1343  * filesystem the file is on.
1344  * 
1345  * Returns: a #GFileOutputStream for the newly created file, or 
1346  * %NULL on error.
1347  **/
1348 GFileOutputStream *
1349 g_file_create (GFile             *file,
1350                GFileCreateFlags   flags,
1351                GCancellable      *cancellable,
1352                GError           **error)
1353 {
1354   GFileIface *iface;
1355   
1356   g_return_val_if_fail (G_IS_FILE (file), NULL);
1357
1358   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1359     return NULL;
1360   
1361   iface = G_FILE_GET_IFACE (file);
1362
1363   if (iface->create == NULL)
1364     {
1365       g_set_error (error, G_IO_ERROR,
1366                    G_IO_ERROR_NOT_SUPPORTED,
1367                    _("Operation not supported"));
1368       return NULL;
1369     }
1370   
1371   return (* iface->create) (file, flags, cancellable, error);
1372 }
1373
1374 /**
1375  * g_file_replace:
1376  * @file: input #GFile.
1377  * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the 
1378  *     current #GFile, or #NULL to ignore.
1379  * @make_backup: %TRUE if a backup should be created.
1380  * @flags: a set of #GFileCreateFlags.
1381  * @cancellable: optional #GCancellable object, %NULL to ignore.
1382  * @error: a #GError, or %NULL
1383  *
1384  * Returns an output stream for overwriting the file, possibly
1385  * creating a backup copy of the file first.
1386  *
1387  * This will try to replace the file in the safest way possible so
1388  * that any errors during the writing will not affect an already
1389  * existing copy of the file. For instance, for local files it
1390  * may write to a temporary file and then atomically rename over
1391  * the destination when the stream is closed.
1392  * 
1393  * By default files created are generally readable by everyone,
1394  * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1395  * will be made readable only to the current user, to the level that
1396  * is supported on the target filesystem.
1397  *
1398  * If @cancellable is not %NULL, then the operation can be cancelled by
1399  * triggering the cancellable object from another thread. If the operation
1400  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
1401  * 
1402  * If you pass in a non-#NULL @etag value, then this value is
1403  * compared to the current entity tag of the file, and if they differ
1404  * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1405  * that the file has been changed since you last read it. You can get
1406  * the new etag from g_file_output_stream_get_etag() after you've
1407  * finished writing and closed the #GFileOutputStream. When you load
1408  * a new file you can use g_file_input_stream_query_info() to get
1409  * the etag of the file.
1410  * 
1411  * If @make_backup is %TRUE, this function will attempt to make a backup
1412  * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1413  * error will be returned. If you want to replace anyway, try again with
1414  * @make_backup set to %FALSE.
1415  *
1416  * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1417  * and if the file is some other form of non-regular file then a
1418  * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1419  * Some file systems don't allow all file names, and may
1420  * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1421  * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1422  * Other errors are possible too, and depend on what kind of
1423  * filesystem the file is on.
1424  *
1425  * Returns: a #GFileOutputStream or %NULL on error. 
1426  **/
1427 GFileOutputStream *
1428 g_file_replace (GFile             *file,
1429                 const char        *etag,
1430                 gboolean           make_backup,
1431                 GFileCreateFlags   flags,
1432                 GCancellable      *cancellable,
1433                 GError           **error)
1434 {
1435   GFileIface *iface;
1436
1437   g_return_val_if_fail (G_IS_FILE (file), NULL);
1438
1439   if (g_cancellable_set_error_if_cancelled (cancellable, error))
1440     return NULL;
1441   
1442   iface = G_FILE_GET_IFACE (file);
1443
1444   if (iface->replace == NULL)
1445     {
1446       g_set_error (error, G_IO_ERROR,
1447                    G_IO_ERROR_NOT_SUPPORTED,
1448                    _("Operation not supported"));
1449       return NULL;
1450     }
1451   
1452   
1453   /* Handle empty tag string as NULL in consistent way. */
1454   if (etag && *etag == 0)
1455     etag = NULL;
1456   
1457   return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1458 }
1459
1460 /**
1461  * g_file_read_async:
1462  * @file: input #GFile.
1463  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1464  *     of the request. 
1465  * @cancellable: optional #GCancellable object, %NULL to ignore.
1466  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1467  * @user_data: the data to pass to callback function
1468  *
1469  * Asynchronously opens @file for reading.
1470  *
1471  * For more details, see g_file_read() which is
1472  * the synchronous version of this call.
1473  *
1474  * When the operation is finished, @callback will be called. You can then call
1475  * g_file_read_finish() to get the result of the operation.
1476  **/
1477 void
1478 g_file_read_async (GFile               *file,
1479                    int                  io_priority,
1480                    GCancellable        *cancellable,
1481                    GAsyncReadyCallback  callback,
1482                    gpointer             user_data)
1483 {
1484   GFileIface *iface;
1485   
1486   g_return_if_fail (G_IS_FILE (file));
1487
1488   iface = G_FILE_GET_IFACE (file);
1489   (* iface->read_async) (file,
1490                          io_priority,
1491                          cancellable,
1492                          callback,
1493                          user_data);
1494 }
1495
1496 /**
1497  * g_file_read_finish:
1498  * @file: input #GFile.
1499  * @res: a #GAsyncResult. 
1500  * @error: a #GError, or %NULL
1501  *
1502  * Finishes an asynchronous file read operation started with 
1503  * g_file_read_async(). 
1504  *  
1505  * Returns: a #GFileInputStream or %NULL on error.
1506  **/
1507 GFileInputStream *
1508 g_file_read_finish (GFile         *file,
1509                     GAsyncResult  *res,
1510                     GError       **error)
1511 {
1512   GFileIface *iface;
1513   
1514   g_return_val_if_fail (G_IS_FILE (file), NULL);
1515   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1516
1517   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1518     {
1519       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1520       if (g_simple_async_result_propagate_error (simple, error))
1521         return NULL;
1522     }
1523   
1524   iface = G_FILE_GET_IFACE (file);
1525   return (* iface->read_finish) (file, res, error);
1526 }
1527
1528 /**
1529  * g_file_append_to_async:
1530  * @file: input #GFile.
1531  * @flags: a set of #GFileCreateFlags.
1532  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1533  *     of the request. 
1534  * @cancellable: optional #GCancellable object, %NULL to ignore.
1535  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1536  * @user_data: the data to pass to callback function
1537  * 
1538  * Asynchronously opens @file for appending.
1539  *
1540  * For more details, see g_file_append_to() which is
1541  * the synchronous version of this call.
1542  *
1543  * When the operation is finished, @callback will be called. You can then call
1544  * g_file_append_to_finish() to get the result of the operation.
1545  **/
1546 void
1547 g_file_append_to_async (GFile               *file,
1548                         GFileCreateFlags     flags,
1549                         int                  io_priority,
1550                         GCancellable        *cancellable,
1551                         GAsyncReadyCallback  callback,
1552                         gpointer             user_data)
1553 {
1554   GFileIface *iface;
1555   
1556   g_return_if_fail (G_IS_FILE (file));
1557
1558   iface = G_FILE_GET_IFACE (file);
1559   (* iface->append_to_async) (file,
1560                               flags,
1561                               io_priority,
1562                               cancellable,
1563                               callback,
1564                               user_data);
1565 }
1566
1567 /**
1568  * g_file_append_to_finish:
1569  * @file: input #GFile.
1570  * @res: #GAsyncResult
1571  * @error: a #GError, or %NULL
1572  * 
1573  * Finishes an asynchronous file append operation started with 
1574  * g_file_append_to_async(). 
1575  * 
1576  * Returns: a valid #GFileOutputStream or %NULL on error.
1577  **/
1578 GFileOutputStream *
1579 g_file_append_to_finish (GFile         *file,
1580                          GAsyncResult  *res,
1581                          GError       **error)
1582 {
1583   GFileIface *iface;
1584   
1585   g_return_val_if_fail (G_IS_FILE (file), NULL);
1586   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1587
1588   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1589     {
1590       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1591       if (g_simple_async_result_propagate_error (simple, error))
1592         return NULL;
1593     }
1594   
1595   iface = G_FILE_GET_IFACE (file);
1596   return (* iface->append_to_finish) (file, res, error);
1597 }
1598
1599 /**
1600  * g_file_create_async:
1601  * @file: input #GFile.
1602  * @flags: a set of #GFileCreateFlags.
1603  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1604  *     of the request.
1605  * @cancellable: optional #GCancellable object, %NULL to ignore.
1606  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1607  * @user_data: the data to pass to callback function
1608  * 
1609  * Asynchronously creates a new file and returns an output stream for writing to it.
1610  * The file must not already exists.
1611  *
1612  * For more details, see g_file_create() which is
1613  * the synchronous version of this call.
1614  *
1615  * When the operation is finished, @callback will be called. You can then call
1616  * g_file_create_finish() to get the result of the operation.
1617  **/
1618 void
1619 g_file_create_async (GFile               *file,
1620                      GFileCreateFlags     flags,
1621                      int                  io_priority,
1622                      GCancellable        *cancellable,
1623                      GAsyncReadyCallback  callback,
1624                      gpointer             user_data)
1625 {
1626   GFileIface *iface;
1627   
1628   g_return_if_fail (G_IS_FILE (file));
1629
1630   iface = G_FILE_GET_IFACE (file);
1631   (* iface->create_async) (file,
1632                            flags,
1633                            io_priority,
1634                            cancellable,
1635                            callback,
1636                            user_data);
1637 }
1638
1639 /**
1640  * g_file_create_finish:
1641  * @file: input #GFile.
1642  * @res: a #GAsyncResult. 
1643  * @error: a #GError, or %NULL
1644  * 
1645  * Finishes an asynchronous file create operation started with 
1646  * g_file_create_async(). 
1647  * 
1648  * Returns: a #GFileOutputStream or %NULL on error.
1649  **/
1650 GFileOutputStream *
1651 g_file_create_finish (GFile         *file,
1652                       GAsyncResult  *res,
1653                       GError       **error)
1654 {
1655   GFileIface *iface;
1656   
1657   g_return_val_if_fail (G_IS_FILE (file), NULL);
1658   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1659
1660   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1661     {
1662       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1663       if (g_simple_async_result_propagate_error (simple, error))
1664         return NULL;
1665     }
1666   
1667   iface = G_FILE_GET_IFACE (file);
1668   return (* iface->create_finish) (file, res, error);
1669 }
1670
1671 /**
1672  * g_file_replace_async:
1673  * @file: input #GFile.
1674  * @etag: an <link linkend="gfile-etag">entity tag</link> for the 
1675  *     current #GFile, or NULL to ignore.
1676  * @make_backup: %TRUE if a backup should be created.
1677  * @flags: a set of #GFileCreateFlags.
1678  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
1679  *     of the request.
1680  * @cancellable: optional #GCancellable object, %NULL to ignore.
1681  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1682  * @user_data: the data to pass to callback function
1683  *
1684  * Asynchronously overwrites the file, replacing the contents, possibly
1685  * creating a backup copy of the file first.
1686  *
1687  * For more details, see g_file_replace() which is
1688  * the synchronous version of this call.
1689  *
1690  * When the operation is finished, @callback will be called. You can then call
1691  * g_file_replace_finish() to get the result of the operation.
1692  **/
1693 void
1694 g_file_replace_async (GFile               *file,
1695                       const char          *etag,
1696                       gboolean             make_backup,
1697                       GFileCreateFlags     flags,
1698                       int                  io_priority,
1699                       GCancellable        *cancellable,
1700                       GAsyncReadyCallback  callback,
1701                       gpointer             user_data)
1702 {
1703   GFileIface *iface;
1704   
1705   g_return_if_fail (G_IS_FILE (file));
1706
1707   iface = G_FILE_GET_IFACE (file);
1708   (* iface->replace_async) (file,
1709                             etag,
1710                             make_backup,
1711                             flags,
1712                             io_priority,
1713                             cancellable,
1714                             callback,
1715                             user_data);
1716 }
1717
1718 /**
1719  * g_file_replace_finish:
1720  * @file: input #GFile.
1721  * @res: a #GAsyncResult. 
1722  * @error: a #GError, or %NULL
1723  * 
1724  * Finishes an asynchronous file replace operation started with 
1725  * g_file_replace_async(). 
1726  * 
1727  * Returns: a #GFileOutputStream, or %NULL on error.
1728  **/
1729 GFileOutputStream *
1730 g_file_replace_finish (GFile         *file,
1731                        GAsyncResult  *res,
1732                        GError       **error)
1733 {
1734   GFileIface *iface;
1735   
1736   g_return_val_if_fail (G_IS_FILE (file), NULL);
1737   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1738
1739   if (G_IS_SIMPLE_ASYNC_RESULT (res))
1740     {
1741       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1742       if (g_simple_async_result_propagate_error (simple, error))
1743         return NULL;
1744     }
1745   
1746   iface = G_FILE_GET_IFACE (file);
1747   return (* iface->replace_finish) (file, res, error);
1748 }
1749
1750 static gboolean
1751 copy_symlink (GFile           *destination,
1752               GFileCopyFlags   flags,
1753               GCancellable    *cancellable,
1754               const char      *target,
1755               GError         **error)
1756 {
1757   GError *my_error;
1758   gboolean tried_delete;
1759   GFileInfo *info;
1760   GFileType file_type;
1761
1762   tried_delete = FALSE;
1763
1764  retry:
1765   my_error = NULL;
1766   if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1767     {
1768       /* Maybe it already existed, and we want to overwrite? */
1769       if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) && 
1770           my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1771         {
1772           g_error_free (my_error);
1773
1774
1775           /* Don't overwrite if the destination is a directory */
1776           info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1777                                     G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1778                                     cancellable, &my_error);
1779           if (info != NULL)
1780             {
1781               file_type = g_file_info_get_file_type (info);
1782               g_object_unref (info);
1783               
1784               if (file_type == G_FILE_TYPE_DIRECTORY)
1785                 {
1786                   g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1787                                _("Can't copy over directory"));
1788                   return FALSE;
1789                 }
1790             }
1791           
1792           if (!g_file_delete (destination, cancellable, error))
1793             return FALSE;
1794           
1795           tried_delete = TRUE;
1796           goto retry;
1797         }
1798             /* Nah, fail */
1799       g_propagate_error (error, my_error);
1800       return FALSE;
1801     }
1802
1803   return TRUE;
1804 }
1805
1806 static GInputStream *
1807 open_source_for_copy (GFile           *source,
1808                       GFile           *destination,
1809                       GFileCopyFlags   flags,
1810                       GCancellable    *cancellable,
1811                       GError         **error)
1812 {
1813   GError *my_error;
1814   GInputStream *in;
1815   GFileInfo *info;
1816   GFileType file_type;
1817   
1818   my_error = NULL;
1819   in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1820   if (in != NULL)
1821     return in;
1822
1823   /* There was an error opening the source, try to set a good error for it: */
1824
1825   if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1826     {
1827       /* The source is a directory, don't fail with WOULD_RECURSE immediately, 
1828        * as that is less useful to the app. Better check for errors on the 
1829        * target instead. 
1830        */
1831       g_error_free (my_error);
1832       my_error = NULL;
1833       
1834       info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1835                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1836                                 cancellable, &my_error);
1837       if (info != NULL)
1838         {
1839           file_type = g_file_info_get_file_type (info);
1840           g_object_unref (info);
1841           
1842           if (flags & G_FILE_COPY_OVERWRITE)
1843             {
1844               if (file_type == G_FILE_TYPE_DIRECTORY)
1845                 {
1846                   g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1847                                _("Can't copy directory over directory"));
1848                   return NULL;
1849                 }
1850               /* continue to would_recurse error */
1851             }
1852           else
1853             {
1854               g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1855                            _("Target file exists"));
1856               return NULL;
1857             }
1858         }
1859       else
1860         {
1861           /* Error getting info from target, return that error 
1862            * (except for NOT_FOUND, which is no error here) 
1863            */
1864           if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1865             {
1866               g_propagate_error (error, my_error);
1867               return NULL;
1868             }
1869           g_error_free (my_error);
1870         }
1871       
1872       g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1873                    _("Can't recursively copy directory"));
1874       return NULL;
1875     }
1876
1877   g_propagate_error (error, my_error);
1878   return NULL;
1879 }
1880
1881 static gboolean
1882 should_copy (GFileAttributeInfo *info, 
1883              gboolean            as_move)
1884 {
1885   if (as_move)
1886     return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1887   return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1888 }
1889
1890 static char *
1891 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1892                                GFileAttributeInfoList *namespaces,
1893                                gboolean                as_move)
1894 {
1895   GString *s;
1896   gboolean first;
1897   int i;
1898   
1899   first = TRUE;
1900   s = g_string_new ("");
1901
1902   if (attributes)
1903     {
1904       for (i = 0; i < attributes->n_infos; i++)
1905         {
1906           if (should_copy (&attributes->infos[i], as_move))
1907             {
1908               if (first)
1909                 first = FALSE;
1910               else
1911                 g_string_append_c (s, ',');
1912                 
1913               g_string_append (s, attributes->infos[i].name);
1914             }
1915         }
1916     }
1917
1918   if (namespaces)
1919     {
1920       for (i = 0; i < namespaces->n_infos; i++)
1921         {
1922           if (should_copy (&namespaces->infos[i], as_move))
1923             {
1924               if (first)
1925                 first = FALSE;
1926               else
1927                 g_string_append_c (s, ',');
1928                 
1929               g_string_append (s, namespaces->infos[i].name);
1930               g_string_append (s, ":*");
1931             }
1932         }
1933     }
1934
1935   return g_string_free (s, FALSE);
1936 }
1937
1938 /**
1939  * g_file_copy_attributes:
1940  * @source: a #GFile with attributes.
1941  * @destination: a #GFile to copy attributes to.
1942  * @flags: a set of #GFileCopyFlags.
1943  * @cancellable: optional #GCancellable object, %NULL to ignore.
1944  * @error: a #GError, %NULL to ignore.
1945  *
1946  * Copies the file attributes from @source to @destination. 
1947  *
1948  * Normally only a subset of the file attributes are copied,
1949  * those that are copies in a normal file copy operation
1950  * (which for instance does not include e.g. mtime). However
1951  * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
1952  * all the metadata that is possible to copy is copied.
1953  *
1954  * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1955  **/
1956 gboolean
1957 g_file_copy_attributes (GFile           *source,
1958                         GFile           *destination,
1959                         GFileCopyFlags   flags,
1960                         GCancellable    *cancellable,
1961                         GError         **error)
1962 {
1963   GFileAttributeInfoList *attributes, *namespaces;
1964   char *attrs_to_read;
1965   gboolean res;
1966   GFileInfo *info;
1967   gboolean as_move;
1968   gboolean source_nofollow_symlinks;
1969
1970   as_move = flags & G_FILE_COPY_ALL_METADATA;
1971   source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1972
1973   /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1974   attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1975   namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1976
1977   if (attributes == NULL && namespaces == NULL)
1978     return TRUE;
1979
1980   attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1981
1982   /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1983    * we just don't copy it. 
1984    */
1985   info = g_file_query_info (source, attrs_to_read,
1986                             source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1987                             cancellable,
1988                             NULL);
1989
1990   g_free (attrs_to_read);
1991   
1992   res = TRUE;
1993   if  (info)
1994     {
1995       res = g_file_set_attributes_from_info (destination,
1996                                              info, 0,
1997                                              cancellable,
1998                                              error);
1999       g_object_unref (info);
2000     }
2001   
2002   g_file_attribute_info_list_unref (attributes);
2003   g_file_attribute_info_list_unref (namespaces);
2004   
2005   return res;
2006 }
2007
2008 /* Closes the streams */
2009 static gboolean
2010 copy_stream_with_progress (GInputStream           *in,
2011                            GOutputStream          *out,
2012                            GCancellable           *cancellable,
2013                            GFileProgressCallback   progress_callback,
2014                            gpointer                progress_callback_data,
2015                            GError                **error)
2016 {
2017   gssize n_read, n_written;
2018   goffset current_size;
2019   char buffer[8192], *p;
2020   gboolean res;
2021   goffset total_size;
2022   GFileInfo *info;
2023
2024   total_size = 0;
2025   info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2026                                          G_FILE_ATTRIBUTE_STANDARD_SIZE,
2027                                          cancellable, NULL);
2028   if (info)
2029     {
2030       total_size = g_file_info_get_size (info);
2031       g_object_unref (info);
2032     }
2033   
2034   current_size = 0;
2035   res = TRUE;
2036   while (TRUE)
2037     {
2038       n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2039       if (n_read == -1)
2040         {
2041           res = FALSE;
2042           break;
2043         }
2044         
2045       if (n_read == 0)
2046         break;
2047
2048       current_size += n_read;
2049
2050       p = buffer;
2051       while (n_read > 0)
2052         {
2053           n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2054           if (n_written == -1)
2055             {
2056               res = FALSE;
2057               break;
2058             }
2059
2060           p += n_written;
2061           n_read -= n_written;
2062         }
2063
2064       if (!res)
2065         break;
2066       
2067       if (progress_callback)
2068         progress_callback (current_size, total_size, progress_callback_data);
2069     }
2070
2071   if (!res)
2072     error = NULL; /* Ignore further errors */
2073
2074   /* Make sure we send full copied size */
2075   if (progress_callback)
2076     progress_callback (current_size, total_size, progress_callback_data);
2077
2078   
2079   /* Don't care about errors in source here */
2080   g_input_stream_close (in, cancellable, NULL);
2081
2082   /* But write errors on close are bad! */
2083   if (!g_output_stream_close (out, cancellable, error))
2084     res = FALSE;
2085
2086   g_object_unref (in);
2087   g_object_unref (out);
2088       
2089   return res;
2090 }
2091
2092 static gboolean
2093 file_copy_fallback (GFile                  *source,
2094                     GFile                  *destination,
2095                     GFileCopyFlags          flags,
2096                     GCancellable           *cancellable,
2097                     GFileProgressCallback   progress_callback,
2098                     gpointer                progress_callback_data,
2099                     GError                **error)
2100 {
2101   GInputStream *in;
2102   GOutputStream *out;
2103   GFileInfo *info;
2104   const char *target;
2105
2106   /* Maybe copy the symlink? */
2107   if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2108     {
2109       info = g_file_query_info (source,
2110                                 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2111                                 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2112                                 cancellable,
2113                                 error);
2114       if (info == NULL)
2115         return FALSE;
2116
2117       if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2118           (target = g_file_info_get_symlink_target (info)) != NULL)
2119         {
2120           if (!copy_symlink (destination, flags, cancellable, target, error))
2121             {
2122               g_object_unref (info);
2123               return FALSE;
2124             }
2125           
2126           g_object_unref (info);
2127           goto copied_file;
2128         }
2129       
2130       g_object_unref (info);
2131     }
2132   
2133   in = open_source_for_copy (source, destination, flags, cancellable, error);
2134   if (in == NULL)
2135     return FALSE;
2136   
2137   if (flags & G_FILE_COPY_OVERWRITE)
2138     {
2139       out = (GOutputStream *)g_file_replace (destination,
2140                                              NULL, 0,
2141                                              flags & G_FILE_COPY_BACKUP,
2142                                              cancellable, error);
2143     }
2144   else
2145     {
2146       out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2147     }
2148
2149   if (out == NULL)
2150     {
2151       g_object_unref (in);
2152       return FALSE;
2153     }
2154
2155   if (!copy_stream_with_progress (in, out, cancellable,
2156                                   progress_callback, progress_callback_data,
2157                                   error))
2158     return FALSE;
2159
2160  copied_file:
2161
2162   /* Ignore errors here. Failure to copy metadata is not a hard error */
2163   g_file_copy_attributes (source, destination,
2164                           flags, cancellable, NULL);
2165   
2166   return TRUE;
2167 }
2168
2169 /**
2170  * g_file_copy:
2171  * @source: input #GFile.
2172  * @destination: destination #GFile
2173  * @flags: set of #GFileCopyFlags
2174  * @cancellable: optional #GCancellable object, %NULL to ignore.
2175  * @progress_callback: function to callback with progress information
2176  * @progress_callback_data: user data to pass to @progress_callback
2177  * @error: #GError to set on error, or %NULL
2178  *
2179  * Copies the file @source to the location specified by @destination.
2180  * Can not handle recursive copies of directories.
2181  *
2182  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2183  * existing @destination file is overwritten.
2184  *
2185  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2186  * will be copied as symlinks, otherwise the target of the
2187  * @source symlink will be copied.
2188  *
2189  * If @cancellable is not %NULL, then the operation can be cancelled by
2190  * triggering the cancellable object from another thread. If the operation
2191  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2192  * 
2193  * If @progress_callback is not %NULL, then the operation can be monitored by
2194  * setting this to a #GFileProgressCallback function. @progress_callback_data
2195  * will be passed to this function. It is guaranteed that this callback will
2196  * be called after all data has been transferred with the total number of bytes
2197  * copied during the operation.
2198  * 
2199  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2200  * error is returned, independent on the status of the @destination.
2201  *
2202  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2203  * error G_IO_ERROR_EXISTS is returned.
2204  *
2205  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2206  * error is returned. If trying to overwrite a directory with a directory the
2207  * G_IO_ERROR_WOULD_MERGE error is returned.
2208  *
2209  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2210  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2211  * is returned.
2212  *
2213  * If you are interested in copying the #GFile object itself (not the on-disk
2214  * file), see g_file_dup().
2215  *
2216  * Returns: %TRUE on success, %FALSE otherwise.
2217  **/
2218 gboolean
2219 g_file_copy (GFile                  *source,
2220              GFile                  *destination,
2221              GFileCopyFlags          flags,
2222              GCancellable           *cancellable,
2223              GFileProgressCallback   progress_callback,
2224              gpointer                progress_callback_data,
2225              GError                **error)
2226 {
2227   GFileIface *iface;
2228   GError *my_error;
2229   gboolean res;
2230
2231   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2232   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2233
2234   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2235     return FALSE;
2236   
2237   iface = G_FILE_GET_IFACE (destination);
2238   if (iface->copy)
2239     {
2240       my_error = NULL;
2241       res = (* iface->copy) (source, destination,
2242                              flags, cancellable,
2243                              progress_callback, progress_callback_data,
2244                              &my_error);
2245       
2246       if (res)
2247         return TRUE;
2248       
2249       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2250         {
2251           g_propagate_error (error, my_error);
2252               return FALSE;
2253         }
2254     }
2255
2256   /* If the types are different, and the destination method failed
2257      also try the source method */
2258   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2259     {
2260       iface = G_FILE_GET_IFACE (source);
2261       
2262       if (iface->copy)
2263         {
2264           my_error = NULL;
2265           res = (* iface->copy) (source, destination,
2266                                  flags, cancellable,
2267                                  progress_callback, progress_callback_data,
2268                                  &my_error);
2269           
2270           if (res)
2271             return TRUE;
2272           
2273           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2274             {
2275               g_propagate_error (error, my_error);
2276               return FALSE;
2277             }
2278         }
2279     }
2280   
2281   return file_copy_fallback (source, destination, flags, cancellable,
2282                              progress_callback, progress_callback_data,
2283                              error);
2284 }
2285
2286 /**
2287  * g_file_copy_async:
2288  * @source: input #GFile.
2289  * @destination: destination #GFile
2290  * @flags: set of #GFileCopyFlags
2291  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2292  *     of the request. 
2293  * @cancellable: optional #GCancellable object, %NULL to ignore.
2294  * @progress_callback: function to callback with progress information
2295  * @progress_callback_data: user data to pass to @progress_callback
2296  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2297  * @user_data: the data to pass to callback function
2298  *
2299  * Copies the file @source to the location specified by @destination 
2300  * asynchronously. For details of the behaviour, see g_file_copy().
2301  *
2302  * If @progress_callback is not %NULL, then that function that will be called
2303  * just like in g_file_copy(), however the callback will run in the main loop,
2304  * not in the thread that is doing the I/O operation.
2305  *
2306  * When the operation is finished, @callback will be called. You can then call
2307  * g_file_copy_finish() to get the result of the operation.
2308  **/
2309 void
2310 g_file_copy_async (GFile                  *source,
2311                    GFile                  *destination,
2312                    GFileCopyFlags          flags,
2313                    int                     io_priority,
2314                    GCancellable           *cancellable,
2315                    GFileProgressCallback   progress_callback,
2316                    gpointer                progress_callback_data,
2317                    GAsyncReadyCallback     callback,
2318                    gpointer                user_data)
2319 {
2320   GFileIface *iface;
2321
2322   g_return_if_fail (G_IS_FILE (source));
2323   g_return_if_fail (G_IS_FILE (destination));
2324
2325   iface = G_FILE_GET_IFACE (source);
2326   (* iface->copy_async) (source,
2327                          destination,
2328                          flags,
2329                          io_priority,
2330                          cancellable,
2331                          progress_callback,
2332                          progress_callback_data,
2333                          callback,
2334                          user_data);
2335 }
2336
2337 /**
2338  * g_file_copy_finish:
2339  * @file: input #GFile.
2340  * @res: a #GAsyncResult. 
2341  * @error: a #GError, or %NULL
2342  * 
2343  * Finishes copying the file started with 
2344  * g_file_copy_async().
2345  * 
2346  * Returns: a %TRUE on success, %FALSE on error.
2347  **/
2348 gboolean
2349 g_file_copy_finish (GFile        *file,
2350                     GAsyncResult *res,
2351                     GError      **error)
2352 {
2353   GFileIface *iface;
2354   
2355   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2356   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2357
2358   if (G_IS_SIMPLE_ASYNC_RESULT (res))
2359     {
2360       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2361       
2362       if (g_simple_async_result_propagate_error (simple, error))
2363         return FALSE;
2364     }
2365   
2366   iface = G_FILE_GET_IFACE (file);
2367   return (* iface->copy_finish) (file, res, error);
2368 }
2369
2370 /**
2371  * g_file_move:
2372  * @source: #GFile pointing to the source location.
2373  * @destination: #GFile pointing to the destination location.
2374  * @flags: set of #GFileCopyFlags.
2375  * @cancellable: optional #GCancellable object, %NULL to ignore.
2376  * @progress_callback: #GFileProgressCallback function for updates.
2377  * @progress_callback_data: gpointer to user data for the callback function.
2378  * @error: #GError for returning error conditions, or %NULL
2379  *
2380  *
2381  * Tries to move the file or directory @source to the location specified by @destination.
2382  * If native move operations are supported then this is used, otherwise a copy + delete
2383  * fallback is used. The native implementation may support moving directories (for instance
2384  * on moves inside the same filesystem), but the fallback code does not.
2385  * 
2386  * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2387  * existing @destination file is overwritten.
2388  *
2389  * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2390  * will be copied as symlinks, otherwise the target of the
2391  * @source symlink will be copied.
2392  *
2393  * If @cancellable is not %NULL, then the operation can be cancelled by
2394  * triggering the cancellable object from another thread. If the operation
2395  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2396  * 
2397  * If @progress_callback is not %NULL, then the operation can be monitored by
2398  * setting this to a #GFileProgressCallback function. @progress_callback_data
2399  * will be passed to this function. It is guaranteed that this callback will
2400  * be called after all data has been transferred with the total number of bytes
2401  * copied during the operation.
2402  * 
2403  * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2404  * error is returned, independent on the status of the @destination.
2405  *
2406  * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2407  * error G_IO_ERROR_EXISTS is returned.
2408  *
2409  * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2410  * error is returned. If trying to overwrite a directory with a directory the
2411  * G_IO_ERROR_WOULD_MERGE error is returned.
2412  *
2413  * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2414  * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2415  * may be returned (if the native move operation isn't available).
2416  *
2417  * Returns: %TRUE on successful move, %FALSE otherwise.
2418  **/
2419 gboolean
2420 g_file_move (GFile                  *source,
2421              GFile                  *destination,
2422              GFileCopyFlags          flags,
2423              GCancellable           *cancellable,
2424              GFileProgressCallback   progress_callback,
2425              gpointer                progress_callback_data,
2426              GError                **error)
2427 {
2428   GFileIface *iface;
2429   GError *my_error;
2430   gboolean res;
2431
2432   g_return_val_if_fail (G_IS_FILE (source), FALSE);
2433   g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2434
2435   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2436     return FALSE;
2437   
2438   iface = G_FILE_GET_IFACE (destination);
2439   if (iface->move)
2440     {
2441       my_error = NULL;
2442       res = (* iface->move) (source, destination,
2443                              flags, cancellable,
2444                              progress_callback, progress_callback_data,
2445                              &my_error);
2446       
2447       if (res)
2448         return TRUE;
2449       
2450       if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2451         {
2452           g_propagate_error (error, my_error);
2453           return FALSE;
2454         }
2455     }
2456
2457   /* If the types are different, and the destination method failed
2458      also try the source method */
2459   if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2460     {
2461       iface = G_FILE_GET_IFACE (source);
2462       
2463       if (iface->move)
2464         {
2465           my_error = NULL;
2466           res = (* iface->move) (source, destination,
2467                                  flags, cancellable,
2468                                  progress_callback, progress_callback_data,
2469                                  &my_error);
2470           
2471           if (res)
2472             return TRUE;
2473           
2474           if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2475             {
2476               g_propagate_error (error, my_error);
2477               return FALSE;
2478             }
2479         }
2480     }
2481   
2482   if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2483     {  
2484       g_set_error (error, G_IO_ERROR,
2485                    G_IO_ERROR_NOT_SUPPORTED,
2486                    _("Operation not supported"));
2487       return FALSE;
2488     }
2489   
2490   flags |= G_FILE_COPY_ALL_METADATA;
2491   if (!g_file_copy (source, destination, flags, cancellable,
2492                     progress_callback, progress_callback_data,
2493                     error))
2494     return FALSE;
2495
2496   return g_file_delete (source, cancellable, error);
2497 }
2498
2499 /**
2500  * g_file_make_directory
2501  * @file: input #GFile.
2502  * @cancellable: optional #GCancellable object, %NULL to ignore.
2503  * @error: a #GError, or %NULL 
2504  *
2505  * Creates a directory.
2506  * 
2507  * If @cancellable is not %NULL, then the operation can be cancelled by
2508  * triggering the cancellable object from another thread. If the operation
2509  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2510  * 
2511  * Returns: %TRUE on successful creation, %FALSE otherwise.
2512  **/
2513 gboolean
2514 g_file_make_directory (GFile         *file,
2515                        GCancellable  *cancellable,
2516                        GError       **error)
2517 {
2518   GFileIface *iface;
2519
2520   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2521
2522   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2523     return FALSE;
2524   
2525   iface = G_FILE_GET_IFACE (file);
2526
2527   if (iface->make_directory == NULL)
2528     {
2529       g_set_error (error, G_IO_ERROR,
2530                    G_IO_ERROR_NOT_SUPPORTED,
2531                    _("Operation not supported"));
2532       return FALSE;
2533     }
2534   
2535   return (* iface->make_directory) (file, cancellable, error);
2536 }
2537
2538 /**
2539  * g_file_make_symbolic_link:
2540  * @file: input #GFile.
2541  * @symlink_value: a string with the value of the new symlink.
2542  * @cancellable: optional #GCancellable object, %NULL to ignore.
2543  * @error: a #GError. 
2544  * 
2545  * Creates a symbolic link.
2546  *
2547  * If @cancellable is not %NULL, then the operation can be cancelled by
2548  * triggering the cancellable object from another thread. If the operation
2549  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2550  * 
2551  * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2552  **/
2553 gboolean
2554 g_file_make_symbolic_link (GFile         *file,
2555                            const char    *symlink_value,
2556                            GCancellable  *cancellable,
2557                            GError       **error)
2558 {
2559   GFileIface *iface;
2560
2561   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2562   g_return_val_if_fail (symlink_value != NULL, FALSE);
2563
2564   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2565     return FALSE;
2566
2567   if (*symlink_value == '\0')
2568     {
2569       g_set_error (error, G_IO_ERROR,
2570                    G_IO_ERROR_INVALID_ARGUMENT,
2571                    _("Invalid symlink value given"));
2572       return FALSE;
2573     }
2574   
2575   iface = G_FILE_GET_IFACE (file);
2576
2577   if (iface->make_symbolic_link == NULL)
2578     {
2579       g_set_error (error, G_IO_ERROR,
2580                    G_IO_ERROR_NOT_SUPPORTED,
2581                    _("Operation not supported"));
2582       return FALSE;
2583     }
2584   
2585   return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2586 }
2587
2588 /**
2589  * g_file_delete:
2590  * @file: input #GFile.
2591  * @cancellable: optional #GCancellable object, %NULL to ignore.
2592  * @error: a #GError, or %NULL 
2593  * 
2594  * Deletes a file.
2595  * 
2596  * If @cancellable is not %NULL, then the operation can be cancelled by
2597  * triggering the cancellable object from another thread. If the operation
2598  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2599  * 
2600  * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2601  **/
2602 gboolean
2603 g_file_delete (GFile         *file,
2604                GCancellable  *cancellable,
2605                GError       **error)
2606 {
2607   GFileIface *iface;
2608   
2609   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2610
2611   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2612     return FALSE;
2613   
2614   iface = G_FILE_GET_IFACE (file);
2615
2616   if (iface->delete_file == NULL)
2617     {
2618       g_set_error (error, G_IO_ERROR,
2619                    G_IO_ERROR_NOT_SUPPORTED,
2620                    _("Operation not supported"));
2621       return FALSE;
2622     }
2623   
2624   return (* iface->delete_file) (file, cancellable, error);
2625 }
2626
2627 /**
2628  * g_file_trash:
2629  * @file: #GFile to send to trash.
2630  * @cancellable: optional #GCancellable object, %NULL to ignore.
2631  * @error: a #GError, or %NULL
2632  *
2633  * Sends @file to the "Trashcan", if possible. This is similar to
2634  * deleting it, but the user can recover it before emptying the trashcan.
2635  * Not all file systems support trashing, so this call can return the
2636  * %G_IO_ERROR_NOT_SUPPORTED error.
2637  *
2638  *
2639  * If @cancellable is not %NULL, then the operation can be cancelled by
2640  * triggering the cancellable object from another thread. If the operation
2641  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2642  * 
2643  * Returns: %TRUE on successful trash, %FALSE otherwise.
2644  **/
2645 gboolean
2646 g_file_trash (GFile         *file,
2647               GCancellable  *cancellable,
2648               GError       **error)
2649 {
2650   GFileIface *iface;
2651   
2652   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2653
2654   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2655     return FALSE;
2656   
2657   iface = G_FILE_GET_IFACE (file);
2658
2659   if (iface->trash == NULL)
2660     {
2661       g_set_error (error,
2662                    G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2663                    _("Trash not supported"));
2664       return FALSE;
2665     }
2666   
2667   return (* iface->trash) (file, cancellable, error);
2668 }
2669
2670 /**
2671  * g_file_set_display_name:
2672  * @file: input #GFile.
2673  * @display_name: a string.
2674  * @cancellable: optional #GCancellable object, %NULL to ignore.
2675  * @error: a #GError, or %NULL
2676  * 
2677  * Renames @file to the specified display name.
2678  *
2679  * The display name is converted from UTF8 to the correct encoding for the target
2680  * filesystem if possible and the @file is renamed to this.
2681  * 
2682  * If you want to implement a rename operation in the user interface the edit name
2683  * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2684  * widget, and then the result after editing should be passed to g_file_set_display_name().
2685  *
2686  * On success the resulting converted filename is returned.
2687  * 
2688  * If @cancellable is not %NULL, then the operation can be cancelled by
2689  * triggering the cancellable object from another thread. If the operation
2690  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2691  * 
2692  * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2693  **/
2694 GFile *
2695 g_file_set_display_name (GFile         *file,
2696                          const char    *display_name,
2697                          GCancellable  *cancellable,
2698                          GError       **error)
2699 {
2700   GFileIface *iface;
2701   
2702   g_return_val_if_fail (G_IS_FILE (file), NULL);
2703   g_return_val_if_fail (display_name != NULL, NULL);
2704
2705   if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2706     {
2707       g_set_error (error,
2708                    G_IO_ERROR,
2709                    G_IO_ERROR_INVALID_ARGUMENT,
2710                    _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2711       return NULL;
2712     }
2713   
2714   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2715     return NULL;
2716   
2717   iface = G_FILE_GET_IFACE (file);
2718
2719   return (* iface->set_display_name) (file, display_name, cancellable, error);
2720 }
2721
2722 /**
2723  * g_file_set_display_name_async:
2724  * @file: input #GFile.
2725  * @display_name: a string.
2726  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
2727  *     of the request. 
2728  * @cancellable: optional #GCancellable object, %NULL to ignore.
2729  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2730  * @user_data: the data to pass to callback function
2731  * 
2732  * Asynchronously sets the display name for a given #GFile.
2733  * 
2734  * For more details, see g_set_display_name() which is
2735  * the synchronous version of this call.
2736  *
2737  * When the operation is finished, @callback will be called. You can then call
2738  * g_file_set_display_name_finish() to get the result of the operation.
2739  **/
2740 void
2741 g_file_set_display_name_async (GFile               *file,
2742                                const char          *display_name,
2743                                int                  io_priority,
2744                                GCancellable        *cancellable,
2745                                GAsyncReadyCallback  callback,
2746                                gpointer             user_data)
2747 {
2748   GFileIface *iface;
2749   
2750   g_return_if_fail (G_IS_FILE (file));
2751   g_return_if_fail (display_name != NULL);
2752
2753   iface = G_FILE_GET_IFACE (file);
2754   (* iface->set_display_name_async) (file,
2755                                      display_name,
2756                                      io_priority,
2757                                      cancellable,
2758                                      callback,
2759                                      user_data);
2760 }
2761
2762 /**
2763  * g_file_set_display_name_finish:
2764  * @file: input #GFile.
2765  * @res: a #GAsyncResult. 
2766  * @error: a #GError, or %NULL
2767  * 
2768  * Finishes setting a display name started with 
2769  * g_file_set_display_name_async().
2770  * 
2771  * Returns: a #GFile or %NULL on error.
2772  **/
2773 GFile *
2774 g_file_set_display_name_finish (GFile         *file,
2775                                 GAsyncResult  *res,
2776                                 GError       **error)
2777 {
2778   GFileIface *iface;
2779   
2780   g_return_val_if_fail (G_IS_FILE (file), NULL);
2781   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2782
2783   if (G_IS_SIMPLE_ASYNC_RESULT (res))
2784     {
2785       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2786       if (g_simple_async_result_propagate_error (simple, error))
2787         return NULL;
2788     }
2789   
2790   iface = G_FILE_GET_IFACE (file);
2791   return (* iface->set_display_name_finish) (file, res, error);
2792 }
2793
2794 /**
2795  * g_file_query_settable_attributes:
2796  * @file: input #GFile.
2797  * @cancellable: optional #GCancellable object, %NULL to ignore.
2798  * @error: a #GError, or %NULL
2799  * 
2800  * Obtain the list of settable attributes for the file.
2801  *
2802  * Returns the type and full attribute name of all the attributes 
2803  * that can be set on this file. This doesn't mean setting it will always 
2804  * succeed though, you might get an access failure, or some specific 
2805  * file may not support a specific attribute.
2806  *
2807  * If @cancellable is not %NULL, then the operation can be cancelled by
2808  * triggering the cancellable object from another thread. If the operation
2809  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2810  * 
2811  * Returns: a #GFileAttributeInfoList describing the settable attributes.
2812  * When you are done with it, release it with g_file_attribute_info_list_unref()
2813  **/
2814 GFileAttributeInfoList *
2815 g_file_query_settable_attributes (GFile         *file,
2816                                   GCancellable  *cancellable,
2817                                   GError       **error)
2818 {
2819   GFileIface *iface;
2820   GError *my_error;
2821   GFileAttributeInfoList *list;
2822
2823   g_return_val_if_fail (G_IS_FILE (file), NULL);
2824
2825   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2826     return NULL;
2827   
2828   iface = G_FILE_GET_IFACE (file);
2829
2830   if (iface->query_settable_attributes == NULL)
2831     return g_file_attribute_info_list_new ();
2832
2833   my_error = NULL;
2834   list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2835   
2836   if (list == NULL)
2837     {
2838       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2839         {
2840           list = g_file_attribute_info_list_new ();
2841           g_error_free (my_error);
2842         }
2843       else
2844         g_propagate_error (error, my_error);
2845     }
2846   
2847   return list;
2848 }
2849
2850 /**
2851  * g_file_query_writable_namespaces:
2852  * @file: input #GFile.
2853  * @cancellable: optional #GCancellable object, %NULL to ignore.
2854  * @error: a #GError, or %NULL
2855  * 
2856  * Obtain the list of attribute namespaces where new attributes 
2857  * can be created by a user. An example of this is extended
2858  * attributes (in the "xattr" namespace).
2859  *
2860  * If @cancellable is not %NULL, then the operation can be cancelled by
2861  * triggering the cancellable object from another thread. If the operation
2862  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2863  * 
2864  * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2865  * When you are done with it, release it with g_file_attribute_info_list_unref()
2866  **/
2867 GFileAttributeInfoList *
2868 g_file_query_writable_namespaces (GFile         *file,
2869                                   GCancellable  *cancellable,
2870                                   GError       **error)
2871 {
2872   GFileIface *iface;
2873   GError *my_error;
2874   GFileAttributeInfoList *list;
2875   
2876   g_return_val_if_fail (G_IS_FILE (file), NULL);
2877
2878   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2879     return NULL;
2880   
2881   iface = G_FILE_GET_IFACE (file);
2882
2883   if (iface->query_writable_namespaces == NULL)
2884     return g_file_attribute_info_list_new ();
2885
2886   my_error = NULL;
2887   list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2888   
2889   if (list == NULL)
2890     {
2891       if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2892         {
2893           list = g_file_attribute_info_list_new ();
2894           g_error_free (my_error);
2895         }
2896       else
2897         g_propagate_error (error, my_error);
2898     }
2899   
2900   return list;
2901 }
2902
2903 /**
2904  * g_file_set_attribute:
2905  * @file: input #GFile.
2906  * @attribute: a string containing the attribute's name.
2907  * @type: The type of the attribute
2908  * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2909  * @flags: a set of #GFileQueryInfoFlags.
2910  * @cancellable: optional #GCancellable object, %NULL to ignore.
2911  * @error: a #GError, or %NULL
2912  * 
2913  * Sets an attribute in the file with attribute name @attribute to @value.
2914  * 
2915  * If @cancellable is not %NULL, then the operation can be cancelled by
2916  * triggering the cancellable object from another thread. If the operation
2917  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2918  * 
2919  * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2920  **/
2921 gboolean
2922 g_file_set_attribute (GFile                      *file,
2923                       const char                 *attribute,
2924                       GFileAttributeType          type,
2925                       gpointer                    value_p,
2926                       GFileQueryInfoFlags         flags,
2927                       GCancellable               *cancellable,
2928                       GError                    **error)
2929 {
2930   GFileIface *iface;
2931   
2932   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2933   g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2934
2935   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2936     return FALSE;
2937   
2938   iface = G_FILE_GET_IFACE (file);
2939
2940   if (iface->set_attribute == NULL)
2941     {
2942       g_set_error (error, G_IO_ERROR,
2943                    G_IO_ERROR_NOT_SUPPORTED,
2944                    _("Operation not supported"));
2945       return FALSE;
2946     }
2947
2948   return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2949 }
2950
2951 /**
2952  * g_file_set_attributes_from_info:
2953  * @file: input #GFile.
2954  * @info: a #GFileInfo.
2955  * @flags: #GFileQueryInfoFlags
2956  * @cancellable: optional #GCancellable object, %NULL to ignore.
2957  * @error: a #GError, or %NULL 
2958  * 
2959  * Tries to set all attributes in the #GFileInfo on the target values, 
2960  * not stopping on the first error.
2961  * 
2962  * If there is any error during this operation then @error will be set to
2963  * the first error. Error on particular fields are flagged by setting 
2964  * the "status" field in the attribute value to 
2965  * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2966  * further errors.
2967  *
2968  * If @cancellable is not %NULL, then the operation can be cancelled by
2969  * triggering the cancellable object from another thread. If the operation
2970  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
2971  * 
2972  * Returns: %TRUE if there was any error, %FALSE otherwise.
2973  **/
2974 gboolean
2975 g_file_set_attributes_from_info (GFile                *file,
2976                                  GFileInfo            *info,
2977                                  GFileQueryInfoFlags   flags,
2978                                  GCancellable         *cancellable,
2979                                  GError              **error)
2980 {
2981   GFileIface *iface;
2982
2983   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2984   g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2985
2986   if (g_cancellable_set_error_if_cancelled (cancellable, error))
2987     return FALSE;
2988   
2989   g_file_info_clear_status (info);
2990   
2991   iface = G_FILE_GET_IFACE (file);
2992
2993   return (* iface->set_attributes_from_info) (file, 
2994                                               info, 
2995                                               flags, 
2996                                               cancellable, 
2997                                               error);
2998 }
2999
3000
3001 static gboolean
3002 g_file_real_set_attributes_from_info (GFile                *file,
3003                                       GFileInfo            *info,
3004                                       GFileQueryInfoFlags   flags,
3005                                       GCancellable         *cancellable,
3006                                       GError              **error)
3007 {
3008   char **attributes;
3009   int i;
3010   gboolean res;
3011   GFileAttributeValue *value;
3012   
3013   res = TRUE;
3014   
3015   attributes = g_file_info_list_attributes (info, NULL);
3016
3017   for (i = 0; attributes[i] != NULL; i++)
3018     {
3019       value = _g_file_info_get_attribute_value (info, attributes[i]);
3020
3021       if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3022         continue;
3023
3024       if (!g_file_set_attribute (file, attributes[i],
3025                                  value->type, _g_file_attribute_value_peek_as_pointer (value),
3026                                  flags, cancellable, error))
3027         {
3028           value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3029           res = FALSE;
3030           /* Don't set error multiple times */
3031           error = NULL;
3032         }
3033       else
3034         value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3035     }
3036   
3037   g_strfreev (attributes);
3038   
3039   return res;
3040 }
3041
3042 /**
3043  * g_file_set_attributes_async:
3044  * @file: input #GFile.
3045  * @info: a #GFileInfo.
3046  * @flags: a #GFileQueryInfoFlags.
3047  * @io_priority: the <link linkend="io-priority">I/O priority</link> 
3048  *     of the request. 
3049  * @cancellable: optional #GCancellable object, %NULL to ignore.
3050  * @callback: a #GAsyncReadyCallback. 
3051  * @user_data: a #gpointer.
3052  *
3053  * Asynchronously sets the attributes of @file with @info.
3054  * 
3055  * For more details, see g_file_set_attributes_from_info() which is
3056  * the synchronous version of this call.
3057  *
3058  * When the operation is finished, @callback will be called. You can then call
3059  * g_file_set_attributes_finish() to get the result of the operation.
3060  **/
3061 void
3062 g_file_set_attributes_async (GFile               *file,
3063                              GFileInfo           *info,
3064                              GFileQueryInfoFlags  flags,
3065                              int                  io_priority,
3066                              GCancellable        *cancellable,
3067                              GAsyncReadyCallback  callback,
3068                              gpointer             user_data)
3069 {
3070   GFileIface *iface;
3071   
3072   g_return_if_fail (G_IS_FILE (file));
3073   g_return_if_fail (G_IS_FILE_INFO (info));
3074
3075   iface = G_FILE_GET_IFACE (file);
3076   (* iface->set_attributes_async) (file, 
3077                                    info, 
3078                                    flags, 
3079                                    io_priority, 
3080                                    cancellable, 
3081                                    callback, 
3082                                    user_data);
3083 }
3084
3085 /**
3086  * g_file_set_attributes_finish:
3087  * @file: input #GFile.
3088  * @result: a #GAsyncResult.
3089  * @info: a #GFileInfo.
3090  * @error: a #GError, or %NULL
3091  * 
3092  * Finishes setting an attribute started in g_file_set_attributes_async().
3093  * 
3094  * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3095  **/
3096 gboolean
3097 g_file_set_attributes_finish (GFile         *file,
3098                               GAsyncResult  *result,
3099                               GFileInfo    **info,
3100                               GError       **error)
3101 {
3102   GFileIface *iface;
3103   
3104   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3105   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3106
3107   /* No standard handling of errors here, as we must set info even
3108    * on errors 
3109    */
3110   iface = G_FILE_GET_IFACE (file);
3111   return (* iface->set_attributes_finish) (file, result, info, error);
3112 }
3113
3114 /**
3115  * g_file_set_attribute_string:
3116  * @file: input #GFile.
3117  * @attribute: a string containing the attribute's name.
3118  * @value: a string containing the attribute's value.
3119  * @flags: #GFileQueryInfoFlags.
3120  * @cancellable: optional #GCancellable object, %NULL to ignore.
3121  * @error: a #GError, or %NULL
3122  * 
3123  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value. 
3124  * If @attribute is of a different type, this operation will fail.
3125  * 
3126  * If @cancellable is not %NULL, then the operation can be cancelled by
3127  * triggering the cancellable object from another thread. If the operation
3128  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3129  * 
3130  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3131  **/
3132 gboolean
3133 g_file_set_attribute_string (GFile                *file,
3134                              const char           *attribute,
3135                              const char           *value,
3136                              GFileQueryInfoFlags   flags,
3137                              GCancellable         *cancellable,
3138                              GError              **error)
3139 {
3140   return g_file_set_attribute (file, attribute,
3141                                G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3142                                flags, cancellable, error);
3143 }
3144
3145 /**
3146  * g_file_set_attribute_byte_string:
3147  * @file: input #GFile.
3148  * @attribute: a string containing the attribute's name.
3149  * @value: a string containing the attribute's new value.
3150  * @flags: a #GFileQueryInfoFlags.
3151  * @cancellable: optional #GCancellable object, %NULL to ignore.
3152  * @error: a #GError, or %NULL
3153  * 
3154  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value. 
3155  * If @attribute is of a different type, this operation will fail, 
3156  * returning %FALSE. 
3157  * 
3158  * If @cancellable is not %NULL, then the operation can be cancelled by
3159  * triggering the cancellable object from another thread. If the operation
3160  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3161  * 
3162  * Returns: %TRUE if the @attribute was successfully set to @value 
3163  * in the @file, %FALSE otherwise.
3164  **/
3165 gboolean
3166 g_file_set_attribute_byte_string  (GFile                *file,
3167                                    const char           *attribute,
3168                                    const char           *value,
3169                                    GFileQueryInfoFlags   flags,
3170                                    GCancellable         *cancellable,
3171                                    GError              **error)
3172 {
3173   return g_file_set_attribute (file, attribute,
3174                                G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3175                                flags, cancellable, error);
3176 }
3177
3178 /**
3179  * g_file_set_attribute_uint32:
3180  * @file: input #GFile.
3181  * @attribute: a string containing the attribute's name.
3182  * @value: a #guint32 containing the attribute's new value.
3183  * @flags: a #GFileQueryInfoFlags.
3184  * @cancellable: optional #GCancellable object, %NULL to ignore.
3185  * @error: a #GError, or %NULL
3186  * 
3187  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value. 
3188  * If @attribute is of a different type, this operation will fail.
3189  * 
3190  * If @cancellable is not %NULL, then the operation can be cancelled by
3191  * triggering the cancellable object from another thread. If the operation
3192  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3193  * 
3194  * Returns: %TRUE if the @attribute was successfully set to @value 
3195  * in the @file, %FALSE otherwise.
3196  **/
3197 gboolean
3198 g_file_set_attribute_uint32 (GFile                *file,
3199                              const char           *attribute,
3200                              guint32               value,
3201                              GFileQueryInfoFlags   flags,
3202                              GCancellable         *cancellable,
3203                              GError              **error)
3204 {
3205   return g_file_set_attribute (file, attribute,
3206                                G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3207                                flags, cancellable, error);
3208 }
3209
3210 /**
3211  * g_file_set_attribute_int32:
3212  * @file: input #GFile.
3213  * @attribute: a string containing the attribute's name.
3214  * @value: a #gint32 containing the attribute's new value.
3215  * @flags: a #GFileQueryInfoFlags.
3216  * @cancellable: optional #GCancellable object, %NULL to ignore.
3217  * @error: a #GError, or %NULL
3218  * 
3219  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value. 
3220  * If @attribute is of a different type, this operation will fail.
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 if the @attribute was successfully set to @value 
3227  * in the @file, %FALSE otherwise. 
3228  **/
3229 gboolean
3230 g_file_set_attribute_int32 (GFile                *file,
3231                             const char           *attribute,
3232                             gint32                value,
3233                             GFileQueryInfoFlags   flags,
3234                             GCancellable         *cancellable,
3235                             GError              **error)
3236 {
3237   return g_file_set_attribute (file, attribute,
3238                                G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3239                                flags, cancellable, error);
3240 }
3241
3242 /**
3243  * g_file_set_attribute_uint64:
3244  * @file: input #GFile. 
3245  * @attribute: a string containing the attribute's name.
3246  * @value: a #guint64 containing the attribute's new value.
3247  * @flags: a #GFileQueryInfoFlags.
3248  * @cancellable: optional #GCancellable object, %NULL to ignore.
3249  * @error: a #GError, or %NULL
3250  * 
3251  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value. 
3252  * If @attribute is of a different type, this operation will fail.
3253  * 
3254  * If @cancellable is not %NULL, then the operation can be cancelled by
3255  * triggering the cancellable object from another thread. If the operation
3256  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3257  * 
3258  * Returns: %TRUE if the @attribute was successfully set to @value 
3259  * in the @file, %FALSE otherwise.
3260  **/
3261 gboolean
3262 g_file_set_attribute_uint64 (GFile                *file,
3263                              const char           *attribute,
3264                              guint64               value,
3265                              GFileQueryInfoFlags   flags,
3266                              GCancellable         *cancellable,
3267                              GError              **error)
3268  {
3269   return g_file_set_attribute (file, attribute,
3270                                G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3271                                flags, cancellable, error);
3272 }
3273
3274 /**
3275  * g_file_set_attribute_int64:
3276  * @file: input #GFile.
3277  * @attribute: a string containing the attribute's name.
3278  * @value: a #guint64 containing the attribute's new value.
3279  * @flags: a #GFileQueryInfoFlags.
3280  * @cancellable: optional #GCancellable object, %NULL to ignore.
3281  * @error: a #GError, or %NULL
3282  * 
3283  * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value. 
3284  * If @attribute is of a different type, this operation will fail.
3285  * 
3286  * If @cancellable is not %NULL, then the operation can be cancelled by
3287  * triggering the cancellable object from another thread. If the operation
3288  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3289  * 
3290  * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3291  **/
3292 gboolean
3293 g_file_set_attribute_int64 (GFile                *file,
3294                             const char           *attribute,
3295                             gint64                value,
3296                             GFileQueryInfoFlags   flags,
3297                             GCancellable         *cancellable,
3298                             GError              **error)
3299 {
3300   return g_file_set_attribute (file, attribute,
3301                                G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3302                                flags, cancellable, error);
3303 }
3304
3305 /**
3306  * g_file_mount_mountable:
3307  * @file: input #GFile.
3308  * @flags: flags affecting the operation
3309  * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3310  * @cancellable: optional #GCancellable object, %NULL to ignore.
3311  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3312  * @user_data: the data to pass to callback function
3313  * 
3314  * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3315  * Using @mount_operation, you can request callbacks when, for instance, 
3316  * passwords are needed during authentication.
3317  *
3318  * If @cancellable is not %NULL, then the operation can be cancelled by
3319  * triggering the cancellable object from another thread. If the operation
3320  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3321  *
3322  * When the operation is finished, @callback will be called. You can then call
3323  * g_file_mount_mountable_finish() to get the result of the operation.
3324  **/
3325 void
3326 g_file_mount_mountable (GFile               *file,
3327                         GMountMountFlags     flags,
3328                         GMountOperation     *mount_operation,
3329                         GCancellable        *cancellable,
3330                         GAsyncReadyCallback  callback,
3331                         gpointer             user_data)
3332 {
3333   GFileIface *iface;
3334
3335   g_return_if_fail (G_IS_FILE (file));
3336
3337   iface = G_FILE_GET_IFACE (file);
3338
3339   if (iface->mount_mountable == NULL) 
3340     {
3341       g_simple_async_report_error_in_idle (G_OBJECT (file),
3342                                            callback,
3343                                            user_data,
3344                                            G_IO_ERROR,
3345                                            G_IO_ERROR_NOT_SUPPORTED,
3346                                            _("Operation not supported"));
3347       return;
3348     }
3349   
3350   (* iface->mount_mountable) (file,
3351                               flags,
3352                               mount_operation,
3353                               cancellable,
3354                               callback,
3355                               user_data);
3356 }
3357
3358 /**
3359  * g_file_mount_mountable_finish:
3360  * @file: input #GFile.
3361  * @result: a #GAsyncResult.
3362  * @error: a #GError, or %NULL
3363  *
3364  * Finishes a mount operation. See g_file_mount_mountable() for details.
3365  * 
3366  * Finish an asynchronous mount operation that was started 
3367  * with g_file_mount_mountable().
3368  *
3369  * Returns: a #GFile or %NULL on error.
3370  **/
3371 GFile *
3372 g_file_mount_mountable_finish (GFile         *file,
3373                                GAsyncResult  *result,
3374                                GError       **error)
3375 {
3376   GFileIface *iface;
3377
3378   g_return_val_if_fail (G_IS_FILE (file), NULL);
3379   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3380
3381   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3382     {
3383       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3384       if (g_simple_async_result_propagate_error (simple, error))
3385         return NULL;
3386     }
3387   
3388   iface = G_FILE_GET_IFACE (file);
3389   return (* iface->mount_mountable_finish) (file, result, error);
3390 }
3391
3392 /**
3393  * g_file_unmount_mountable:
3394  * @file: input #GFile.
3395  * @flags: flags affecting the operation
3396  * @cancellable: optional #GCancellable object, %NULL to ignore.
3397  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3398  * @user_data: the data to pass to callback function
3399  *
3400  * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3401  *
3402  * If @cancellable is not %NULL, then the operation can be cancelled by
3403  * triggering the cancellable object from another thread. If the operation
3404  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3405  *
3406  * When the operation is finished, @callback will be called. You can then call
3407  * g_file_unmount_mountable_finish() to get the result of the operation.
3408  **/
3409 void
3410 g_file_unmount_mountable (GFile               *file,
3411                           GMountUnmountFlags   flags,
3412                           GCancellable        *cancellable,
3413                           GAsyncReadyCallback  callback,
3414                           gpointer             user_data)
3415 {
3416   GFileIface *iface;
3417   
3418   g_return_if_fail (G_IS_FILE (file));
3419
3420   iface = G_FILE_GET_IFACE (file);
3421   
3422   if (iface->unmount_mountable == NULL)
3423     {
3424       g_simple_async_report_error_in_idle (G_OBJECT (file),
3425                                            callback,
3426                                            user_data,
3427                                            G_IO_ERROR,
3428                                            G_IO_ERROR_NOT_SUPPORTED,
3429                                            _("Operation not supported"));
3430       return;
3431     }
3432   
3433   (* iface->unmount_mountable) (file,
3434                                 flags,
3435                                 cancellable,
3436                                 callback,
3437                                 user_data);
3438 }
3439
3440 /**
3441  * g_file_unmount_mountable_finish:
3442  * @file: input #GFile.
3443  * @result: a #GAsyncResult.
3444  * @error: a #GError, or %NULL
3445  *
3446  * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3447  * 
3448  * Finish an asynchronous unmount operation that was started 
3449  * with g_file_unmount_mountable().
3450  *
3451  * Returns: %TRUE if the operation finished successfully. %FALSE
3452  * otherwise.
3453  **/
3454 gboolean
3455 g_file_unmount_mountable_finish (GFile         *file,
3456                                  GAsyncResult  *result,
3457                                  GError       **error)
3458 {
3459   GFileIface *iface;
3460   
3461   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3462   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3463
3464   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3465     {
3466       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3467       if (g_simple_async_result_propagate_error (simple, error))
3468         return FALSE;
3469     }
3470   
3471   iface = G_FILE_GET_IFACE (file);
3472   return (* iface->unmount_mountable_finish) (file, result, error);
3473 }
3474
3475 /**
3476  * g_file_eject_mountable:
3477  * @file: input #GFile.
3478  * @flags: flags affecting the operation
3479  * @cancellable: optional #GCancellable object, %NULL to ignore.
3480  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3481  * @user_data: the data to pass to callback function
3482  * 
3483  * Starts an asynchronous eject on a mountable.  
3484  * When this operation has completed, @callback will be called with
3485  * @user_user data, and the operation can be finalized with 
3486  * g_file_eject_mountable_finish().
3487  * 
3488  * If @cancellable is not %NULL, then the operation can be cancelled by
3489  * triggering the cancellable object from another thread. If the operation
3490  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3491  **/
3492 void
3493 g_file_eject_mountable (GFile               *file,
3494                         GMountUnmountFlags   flags,
3495                         GCancellable        *cancellable,
3496                         GAsyncReadyCallback  callback,
3497                         gpointer             user_data)
3498 {
3499   GFileIface *iface;
3500
3501   g_return_if_fail (G_IS_FILE (file));
3502
3503   iface = G_FILE_GET_IFACE (file);
3504   
3505   if (iface->eject_mountable == NULL) 
3506     {
3507       g_simple_async_report_error_in_idle (G_OBJECT (file),
3508                                            callback,
3509                                            user_data,
3510                                            G_IO_ERROR,
3511                                            G_IO_ERROR_NOT_SUPPORTED,
3512                                            _("Operation not supported"));
3513       return;
3514     }
3515   
3516   (* iface->eject_mountable) (file,
3517                               flags,
3518                               cancellable,
3519                               callback,
3520                               user_data);
3521 }
3522
3523 /**
3524  * g_file_eject_mountable_finish:
3525  * @file: input #GFile.
3526  * @result: a #GAsyncResult.
3527  * @error: a #GError, or %NULL
3528  * 
3529  * Finishes an asynchronous eject operation started by 
3530  * g_file_eject_mountable().
3531  * 
3532  * Returns: %TRUE if the @file was ejected successfully. %FALSE 
3533  * otherwise.
3534  **/
3535 gboolean
3536 g_file_eject_mountable_finish (GFile         *file,
3537                                GAsyncResult  *result,
3538                                GError       **error)
3539 {
3540   GFileIface *iface;
3541   
3542   g_return_val_if_fail (G_IS_FILE (file), FALSE);
3543   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3544
3545   if (G_IS_SIMPLE_ASYNC_RESULT (result))
3546     {
3547       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3548       if (g_simple_async_result_propagate_error (simple, error))
3549         return FALSE;
3550     }
3551   
3552   iface = G_FILE_GET_IFACE (file);
3553   return (* iface->eject_mountable_finish) (file, result, error);
3554 }
3555
3556 /**
3557  * g_file_monitor_directory:
3558  * @file: input #GFile.
3559  * @flags: a set of #GFileMonitorFlags.
3560  * @cancellable: optional #GCancellable object, %NULL to ignore.
3561  * @error: a #GError, or %NULL.
3562  * 
3563  * Obtains a directory monitor for the given file.
3564  * This may fail if directory monitoring is not supported.
3565  *
3566  * If @cancellable is not %NULL, then the operation can be cancelled by
3567  * triggering the cancellable object from another thread. If the operation
3568  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3569  * 
3570  * Returns: a #GFileMonitor for the given @file, 
3571  * or %NULL on error.
3572  **/
3573 GFileMonitor*
3574 g_file_monitor_directory (GFile             *file,
3575                           GFileMonitorFlags  flags,
3576                           GCancellable      *cancellable,
3577                           GError           **error)
3578 {
3579   GFileIface *iface;
3580
3581   g_return_val_if_fail (G_IS_FILE (file), NULL);
3582
3583   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3584     return NULL;
3585
3586   iface = G_FILE_GET_IFACE (file);
3587
3588   if (iface->monitor_dir == NULL)
3589     {
3590       g_set_error (error, G_IO_ERROR,
3591                    G_IO_ERROR_NOT_SUPPORTED,
3592                    _("Operation not supported"));
3593       return NULL;
3594     }
3595
3596   return (* iface->monitor_dir) (file, flags, cancellable, error);
3597 }
3598
3599 /**
3600  * g_file_monitor_file:
3601  * @file: input #GFile.
3602  * @flags: a set of #GFileMonitorFlags.
3603  * @cancellable: optional #GCancellable object, %NULL to ignore.
3604  * @error: a #GError, or %NULL.
3605  * 
3606  * Obtains a file monitor for the given file. If no file notification
3607  * mechanism exists, then regular polling of the file is used.
3608  *
3609  * If @cancellable is not %NULL, then the operation can be cancelled by
3610  * triggering the cancellable object from another thread. If the operation
3611  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
3612  * 
3613  * Returns: a #GFileMonitor for the given @file.
3614  **/
3615 GFileMonitor*
3616 g_file_monitor_file (GFile             *file,
3617                      GFileMonitorFlags  flags,
3618                      GCancellable      *cancellable,
3619                      GError           **error)
3620 {
3621   GFileIface *iface;
3622   GFileMonitor *monitor;
3623   
3624   g_return_val_if_fail (G_IS_FILE (file), NULL);
3625
3626   if (g_cancellable_set_error_if_cancelled (cancellable, error))
3627     return NULL;
3628
3629   iface = G_FILE_GET_IFACE (file);
3630
3631   monitor = NULL;
3632   
3633   if (iface->monitor_file)
3634     monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3635
3636 /* Fallback to polling */
3637   if (monitor == NULL)
3638     monitor = _g_poll_file_monitor_new (file);
3639
3640   return monitor;
3641 }
3642
3643 /********************************************
3644  *   Default implementation of async ops    *
3645  ********************************************/
3646
3647 typedef struct {
3648   char *attributes;
3649   GFileQueryInfoFlags flags;
3650   GFileInfo *info;
3651 } QueryInfoAsyncData;
3652
3653 static void
3654 query_info_data_free (QueryInfoAsyncData *data)
3655 {
3656   if (data->info)
3657     g_object_unref (data->info);
3658   g_free (data->attributes);
3659   g_free (data);
3660 }
3661
3662 static void
3663 query_info_async_thread (GSimpleAsyncResult *res,
3664                          GObject            *object,
3665                          GCancellable       *cancellable)
3666 {
3667   GError *error = NULL;
3668   QueryInfoAsyncData *data;
3669   GFileInfo *info;
3670   
3671   data = g_simple_async_result_get_op_res_gpointer (res);
3672   
3673   info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3674
3675   if (info == NULL)
3676     {
3677       g_simple_async_result_set_from_error (res, error);
3678       g_error_free (error);
3679     }
3680   else
3681     data->info = info;
3682 }
3683
3684 static void
3685 g_file_real_query_info_async (GFile               *file,
3686                               const char          *attributes,
3687                               GFileQueryInfoFlags  flags,
3688                               int                  io_priority,
3689                               GCancellable        *cancellable,
3690                               GAsyncReadyCallback  callback,
3691                               gpointer             user_data)
3692 {
3693   GSimpleAsyncResult *res;
3694   QueryInfoAsyncData *data;
3695
3696   data = g_new0 (QueryInfoAsyncData, 1);
3697   data->attributes = g_strdup (attributes);
3698   data->flags = flags;
3699   
3700   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3701   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3702   
3703   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3704   g_object_unref (res);
3705 }
3706
3707 static GFileInfo *
3708 g_file_real_query_info_finish (GFile         *file,
3709                                GAsyncResult  *res,
3710                                GError       **error)
3711 {
3712   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3713   QueryInfoAsyncData *data;
3714
3715   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3716
3717   data = g_simple_async_result_get_op_res_gpointer (simple);
3718   if (data->info)
3719     return g_object_ref (data->info);
3720   
3721   return NULL;
3722 }
3723
3724 typedef struct {
3725   char *attributes;
3726   GFileQueryInfoFlags flags;
3727   GFileEnumerator *enumerator;
3728 } EnumerateChildrenAsyncData;
3729
3730 static void
3731 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3732 {
3733   if (data->enumerator)
3734     g_object_unref (data->enumerator);
3735   g_free (data->attributes);
3736   g_free (data);
3737 }
3738
3739 static void
3740 enumerate_children_async_thread (GSimpleAsyncResult *res,
3741                                  GObject            *object,
3742                                  GCancellable       *cancellable)
3743 {
3744   GError *error = NULL;
3745   EnumerateChildrenAsyncData *data;
3746   GFileEnumerator *enumerator;
3747   
3748   data = g_simple_async_result_get_op_res_gpointer (res);
3749   
3750   enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3751
3752   if (enumerator == NULL)
3753     {
3754       g_simple_async_result_set_from_error (res, error);
3755       g_error_free (error);
3756     }
3757   else
3758     data->enumerator = enumerator;
3759 }
3760
3761 static void
3762 g_file_real_enumerate_children_async (GFile               *file,
3763                                       const char          *attributes,
3764                                       GFileQueryInfoFlags  flags,
3765                                       int                  io_priority,
3766                                       GCancellable        *cancellable,
3767                                       GAsyncReadyCallback  callback,
3768                                       gpointer             user_data)
3769 {
3770   GSimpleAsyncResult *res;
3771   EnumerateChildrenAsyncData *data;
3772
3773   data = g_new0 (EnumerateChildrenAsyncData, 1);
3774   data->attributes = g_strdup (attributes);
3775   data->flags = flags;
3776   
3777   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3778   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3779   
3780   g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3781   g_object_unref (res);
3782 }
3783
3784 static GFileEnumerator *
3785 g_file_real_enumerate_children_finish (GFile         *file,
3786                                        GAsyncResult  *res,
3787                                        GError       **error)
3788 {
3789   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3790   EnumerateChildrenAsyncData *data;
3791
3792   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3793
3794   data = g_simple_async_result_get_op_res_gpointer (simple);
3795   if (data->enumerator)
3796     return g_object_ref (data->enumerator);
3797   
3798   return NULL;
3799 }
3800
3801 static void
3802 open_read_async_thread (GSimpleAsyncResult *res,
3803                         GObject            *object,
3804                         GCancellable       *cancellable)
3805 {
3806   GFileIface *iface;
3807   GFileInputStream *stream;
3808   GError *error = NULL;
3809
3810   iface = G_FILE_GET_IFACE (object);
3811
3812   stream = iface->read_fn (G_FILE (object), cancellable, &error);
3813
3814   if (stream == NULL)
3815     {
3816       g_simple_async_result_set_from_error (res, error);
3817       g_error_free (error);
3818     }
3819   else
3820     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3821 }
3822
3823 static void
3824 g_file_real_read_async (GFile               *file,
3825                         int                  io_priority,
3826                         GCancellable        *cancellable,
3827                         GAsyncReadyCallback  callback,
3828                         gpointer             user_data)
3829 {
3830   GSimpleAsyncResult *res;
3831   
3832   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3833   
3834   g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3835   g_object_unref (res);
3836 }
3837
3838 static GFileInputStream *
3839 g_file_real_read_finish (GFile         *file,
3840                          GAsyncResult  *res,
3841                          GError       **error)
3842 {
3843   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3844   gpointer op;
3845
3846   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3847
3848   op = g_simple_async_result_get_op_res_gpointer (simple);
3849   if (op)
3850     return g_object_ref (op);
3851   
3852   return NULL;
3853 }
3854
3855 static void
3856 append_to_async_thread (GSimpleAsyncResult *res,
3857                         GObject            *object,
3858                         GCancellable       *cancellable)
3859 {
3860   GFileIface *iface;
3861   GFileCreateFlags *data;
3862   GFileOutputStream *stream;
3863   GError *error = NULL;
3864
3865   iface = G_FILE_GET_IFACE (object);
3866
3867   data = g_simple_async_result_get_op_res_gpointer (res);
3868
3869   stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3870
3871   if (stream == NULL)
3872     {
3873       g_simple_async_result_set_from_error (res, error);
3874       g_error_free (error);
3875     }
3876   else
3877     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3878 }
3879
3880 static void
3881 g_file_real_append_to_async (GFile               *file,
3882                              GFileCreateFlags     flags,
3883                              int                  io_priority,
3884                              GCancellable        *cancellable,
3885                              GAsyncReadyCallback  callback,
3886                              gpointer             user_data)
3887 {
3888   GFileCreateFlags *data;
3889   GSimpleAsyncResult *res;
3890
3891   data = g_new0 (GFileCreateFlags, 1);
3892   *data = flags;
3893
3894   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3895   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3896
3897   g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3898   g_object_unref (res);
3899 }
3900
3901 static GFileOutputStream *
3902 g_file_real_append_to_finish (GFile         *file,
3903                               GAsyncResult  *res,
3904                               GError       **error)
3905 {
3906   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3907   gpointer op;
3908
3909   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3910
3911   op = g_simple_async_result_get_op_res_gpointer (simple);
3912   if (op)
3913     return g_object_ref (op);
3914   
3915   return NULL;
3916 }
3917
3918 static void
3919 create_async_thread (GSimpleAsyncResult *res,
3920                      GObject            *object,
3921                      GCancellable       *cancellable)
3922 {
3923   GFileIface *iface;
3924   GFileCreateFlags *data;
3925   GFileOutputStream *stream;
3926   GError *error = NULL;
3927
3928   iface = G_FILE_GET_IFACE (object);
3929
3930   data = g_simple_async_result_get_op_res_gpointer (res);
3931
3932   stream = iface->create (G_FILE (object), *data, cancellable, &error);
3933
3934   if (stream == NULL)
3935     {
3936       g_simple_async_result_set_from_error (res, error);
3937       g_error_free (error);
3938     }
3939   else
3940     g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3941 }
3942
3943 static void
3944 g_file_real_create_async (GFile               *file,
3945                           GFileCreateFlags     flags,
3946                           int                  io_priority,
3947                           GCancellable        *cancellable,
3948                           GAsyncReadyCallback  callback,
3949                           gpointer             user_data)
3950 {
3951   GFileCreateFlags *data;
3952   GSimpleAsyncResult *res;
3953
3954   data = g_new0 (GFileCreateFlags, 1);
3955   *data = flags;
3956
3957   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3958   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3959
3960   g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3961   g_object_unref (res);
3962 }
3963
3964 static GFileOutputStream *
3965 g_file_real_create_finish (GFile         *file,
3966                            GAsyncResult  *res,
3967                            GError       **error)
3968 {
3969   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3970   gpointer op;
3971
3972   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3973
3974   op = g_simple_async_result_get_op_res_gpointer (simple);
3975   if (op)
3976     return g_object_ref (op);
3977   
3978   return NULL;
3979 }
3980
3981 typedef struct {
3982   GFileOutputStream *stream;
3983   char *etag;
3984   gboolean make_backup;
3985   GFileCreateFlags flags;
3986 } ReplaceAsyncData;
3987
3988 static void
3989 replace_async_data_free (ReplaceAsyncData *data)
3990 {
3991   if (data->stream)
3992     g_object_unref (data->stream);
3993   g_free (data->etag);
3994   g_free (data);
3995 }
3996
3997 static void
3998 replace_async_thread (GSimpleAsyncResult *res,
3999                       GObject            *object,
4000                       GCancellable       *cancellable)
4001 {
4002   GFileIface *iface;
4003   GFileOutputStream *stream;
4004   GError *error = NULL;
4005   ReplaceAsyncData *data;
4006
4007   iface = G_FILE_GET_IFACE (object);
4008   
4009   data = g_simple_async_result_get_op_res_gpointer (res);
4010
4011   stream = iface->replace (G_FILE (object),
4012                            data->etag,
4013                            data->make_backup,
4014                            data->flags,
4015                            cancellable,
4016                            &error);
4017
4018   if (stream == NULL)
4019     {
4020       g_simple_async_result_set_from_error (res, error);
4021       g_error_free (error);
4022     }
4023   else
4024     data->stream = stream;
4025 }
4026
4027 static void
4028 g_file_real_replace_async (GFile               *file,
4029                            const char          *etag,
4030                            gboolean             make_backup,
4031                            GFileCreateFlags     flags,
4032                            int                  io_priority,
4033                            GCancellable        *cancellable,
4034                            GAsyncReadyCallback  callback,
4035                            gpointer             user_data)
4036 {
4037   GSimpleAsyncResult *res;
4038   ReplaceAsyncData *data;
4039
4040   data = g_new0 (ReplaceAsyncData, 1);
4041   data->etag = g_strdup (etag);
4042   data->make_backup = make_backup;
4043   data->flags = flags;
4044
4045   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4046   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4047
4048   g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4049   g_object_unref (res);
4050 }
4051
4052 static GFileOutputStream *
4053 g_file_real_replace_finish (GFile         *file,
4054                             GAsyncResult  *res,
4055                             GError       **error)
4056 {
4057   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4058   ReplaceAsyncData *data;
4059
4060   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4061
4062   data = g_simple_async_result_get_op_res_gpointer (simple);
4063   if (data->stream)
4064     return g_object_ref (data->stream);
4065   
4066   return NULL;
4067 }
4068
4069 typedef struct {
4070   char *name;
4071   GFile *file;
4072 } SetDisplayNameAsyncData;
4073
4074 static void
4075 set_display_name_data_free (SetDisplayNameAsyncData *data)
4076 {
4077   g_free (data->name);
4078   if (data->file)
4079     g_object_unref (data->file);
4080   g_free (data);
4081 }
4082
4083 static void
4084 set_display_name_async_thread (GSimpleAsyncResult *res,
4085                                GObject            *object,
4086                                GCancellable       *cancellable)
4087 {
4088   GError *error = NULL;
4089   SetDisplayNameAsyncData *data;
4090   GFile *file;
4091   
4092   data = g_simple_async_result_get_op_res_gpointer (res);
4093   
4094   file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4095
4096   if (file == NULL)
4097     {
4098       g_simple_async_result_set_from_error (res, error);
4099       g_error_free (error);
4100     }
4101   else
4102     data->file = file;
4103 }
4104
4105 static void
4106 g_file_real_set_display_name_async (GFile               *file,  
4107                                     const char          *display_name,
4108                                     int                  io_priority,
4109                                     GCancellable        *cancellable,
4110                                     GAsyncReadyCallback  callback,
4111                                     gpointer             user_data)
4112 {
4113   GSimpleAsyncResult *res;
4114   SetDisplayNameAsyncData *data;
4115
4116   data = g_new0 (SetDisplayNameAsyncData, 1);
4117   data->name = g_strdup (display_name);
4118   
4119   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4120   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4121   
4122   g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4123   g_object_unref (res);
4124 }
4125
4126 static GFile *
4127 g_file_real_set_display_name_finish (GFile         *file,
4128                                      GAsyncResult  *res,
4129                                      GError       **error)
4130 {
4131   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4132   SetDisplayNameAsyncData *data;
4133
4134   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4135
4136   data = g_simple_async_result_get_op_res_gpointer (simple);
4137   if (data->file)
4138     return g_object_ref (data->file);
4139   
4140   return NULL;
4141 }
4142
4143 typedef struct {
4144   GFileQueryInfoFlags flags;
4145   GFileInfo *info;
4146   gboolean res;
4147   GError *error;
4148 } SetInfoAsyncData;
4149
4150 static void
4151 set_info_data_free (SetInfoAsyncData *data)
4152 {
4153   if (data->info)
4154     g_object_unref (data->info);
4155   if (data->error)
4156     g_error_free (data->error);
4157   g_free (data);
4158 }
4159
4160 static void
4161 set_info_async_thread (GSimpleAsyncResult *res,
4162                        GObject            *object,
4163                        GCancellable       *cancellable)
4164 {
4165   SetInfoAsyncData *data;
4166   
4167   data = g_simple_async_result_get_op_res_gpointer (res);
4168   
4169   data->error = NULL;
4170   data->res = g_file_set_attributes_from_info (G_FILE (object),
4171                                                data->info,
4172                                                data->flags,
4173                                                cancellable,
4174                                                &data->error);
4175 }
4176
4177 static void
4178 g_file_real_set_attributes_async (GFile               *file,
4179                                   GFileInfo           *info,
4180                                   GFileQueryInfoFlags  flags,
4181                                   int                  io_priority,
4182                                   GCancellable        *cancellable,
4183                                   GAsyncReadyCallback  callback,
4184                                   gpointer             user_data)
4185 {
4186   GSimpleAsyncResult *res;
4187   SetInfoAsyncData *data;
4188
4189   data = g_new0 (SetInfoAsyncData, 1);
4190   data->info = g_file_info_dup (info);
4191   data->flags = flags;
4192   
4193   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4194   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4195   
4196   g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4197   g_object_unref (res);
4198 }
4199
4200 static gboolean
4201 g_file_real_set_attributes_finish (GFile         *file,
4202                                    GAsyncResult  *res,
4203                                    GFileInfo    **info,
4204                                    GError       **error)
4205 {
4206   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4207   SetInfoAsyncData *data;
4208   
4209   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4210
4211   data = g_simple_async_result_get_op_res_gpointer (simple);
4212
4213   if (info) 
4214     *info = g_object_ref (data->info);
4215
4216   if (error != NULL && data->error) 
4217     *error = g_error_copy (data->error);
4218   
4219   return data->res;
4220 }
4221
4222 static void
4223 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4224                                     GObject            *object,
4225                                     GCancellable       *cancellable)
4226 {
4227   GError *error = NULL;
4228   GMount *mount;
4229   
4230   mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4231
4232   if (mount == NULL)
4233     {
4234       g_simple_async_result_set_from_error (res, error);
4235       g_error_free (error);
4236     }
4237   else
4238     g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4239 }
4240
4241 static void
4242 g_file_real_find_enclosing_mount_async (GFile               *file,
4243                                         int                  io_priority,
4244                                         GCancellable        *cancellable,
4245                                         GAsyncReadyCallback  callback,
4246                                         gpointer             user_data)
4247 {
4248   GSimpleAsyncResult *res;
4249   
4250   res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4251   
4252   g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4253   g_object_unref (res);
4254 }
4255
4256 static GMount *
4257 g_file_real_find_enclosing_mount_finish (GFile         *file,
4258                                           GAsyncResult  *res,
4259                                           GError       **error)
4260 {
4261   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4262   GMount *mount;
4263
4264   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4265
4266   mount = g_simple_async_result_get_op_res_gpointer (simple);
4267   return g_object_ref (mount);
4268 }
4269
4270
4271 typedef struct {
4272   GFile *source;
4273   GFile *destination;
4274   GFileCopyFlags flags;
4275   GFileProgressCallback progress_cb;
4276   gpointer progress_cb_data;
4277   GIOSchedulerJob *job;
4278 } CopyAsyncData;
4279
4280 static void
4281 copy_async_data_free (CopyAsyncData *data)
4282 {
4283   g_object_unref (data->source);
4284   g_object_unref (data->destination);
4285   g_free (data);
4286 }
4287
4288 typedef struct {
4289   CopyAsyncData *data;
4290   goffset current_num_bytes;
4291   goffset total_num_bytes;
4292 } ProgressData;
4293
4294 static gboolean
4295 copy_async_progress_in_main (gpointer user_data)
4296 {
4297   ProgressData *progress = user_data;
4298   CopyAsyncData *data = progress->data;
4299
4300   data->progress_cb (progress->current_num_bytes,
4301                      progress->total_num_bytes,
4302                      data->progress_cb_data);
4303
4304   return FALSE;
4305 }
4306
4307 static gboolean
4308 mainloop_barrier (gpointer user_data)
4309 {
4310   /* Does nothing, but ensures all queued idles before
4311      this are run */
4312   return FALSE;
4313 }
4314
4315
4316 static void
4317 copy_async_progress_callback (goffset  current_num_bytes,
4318                               goffset  total_num_bytes,
4319                               gpointer user_data)
4320 {
4321   CopyAsyncData *data = user_data;
4322   ProgressData *progress;
4323
4324   progress = g_new (ProgressData, 1);
4325   progress->data = data;
4326   progress->current_num_bytes = current_num_bytes;
4327   progress->total_num_bytes = total_num_bytes;
4328   
4329   g_io_scheduler_job_send_to_mainloop_async (data->job,
4330                                              copy_async_progress_in_main,
4331                                              progress,
4332                                              g_free);
4333 }
4334
4335 static gboolean
4336 copy_async_thread (GIOSchedulerJob *job,
4337                    GCancellable    *cancellable,
4338                    gpointer         user_data)
4339 {
4340   GSimpleAsyncResult *res;
4341   CopyAsyncData *data;
4342   gboolean result;
4343   GError *error;
4344
4345   res = user_data;
4346   data = g_simple_async_result_get_op_res_gpointer (res);
4347
4348   error = NULL;
4349   data->job = job;
4350   result = g_file_copy (data->source,
4351                         data->destination,
4352                         data->flags,
4353                         cancellable,
4354                         (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4355                         data,
4356                         &error);
4357
4358   /* Ensure all progress callbacks are done running in main thread */
4359   if (data->progress_cb != NULL)
4360     g_io_scheduler_job_send_to_mainloop (job,
4361                                          mainloop_barrier,
4362                                          NULL, NULL);
4363   
4364   if (!result)
4365     {
4366       g_simple_async_result_set_from_error (res, error);
4367       g_error_free (error);
4368     }
4369
4370   g_simple_async_result_complete_in_idle (res);
4371
4372   return FALSE;
4373 }
4374
4375 static void
4376 g_file_real_copy_async (GFile                  *source,
4377                         GFile                  *destination,
4378                         GFileCopyFlags          flags,
4379                         int                     io_priority,
4380                         GCancellable           *cancellable,
4381                         GFileProgressCallback   progress_callback,
4382                         gpointer                progress_callback_data,
4383                         GAsyncReadyCallback     callback,
4384                         gpointer                user_data)
4385 {
4386   GSimpleAsyncResult *res;
4387   CopyAsyncData *data;
4388
4389   data = g_new0 (CopyAsyncData, 1);
4390   data->source = g_object_ref (source);
4391   data->destination = g_object_ref (destination);
4392   data->flags = flags;
4393   data->progress_cb = progress_callback;
4394   data->progress_cb_data = progress_callback_data;
4395
4396   res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4397   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4398
4399   g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4400 }
4401
4402 static gboolean
4403 g_file_real_copy_finish (GFile        *file,
4404                          GAsyncResult *res,
4405                          GError      **error)
4406 {
4407   /* Error handled in g_file_copy_finish() */
4408   return TRUE;
4409 }
4410
4411
4412 /********************************************
4413  *   Default VFS operations                 *
4414  ********************************************/
4415
4416 /**
4417  * g_file_new_for_path:
4418  * @path: a string containing a relative or absolute path.
4419  * 
4420  * Constructs a #GFile for a given path. This operation never
4421  * fails, but the returned object might not support any I/O
4422  * operation if @path is malformed.
4423  * 
4424  * Returns: a new #GFile for the given @path. 
4425  **/
4426 GFile *
4427 g_file_new_for_path (const char *path)
4428 {
4429   g_return_val_if_fail (path != NULL, NULL);
4430
4431   return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4432 }
4433  
4434 /**
4435  * g_file_new_for_uri:
4436  * @uri: a string containing a URI.
4437  * 
4438  * Constructs a #GFile for a given URI. This operation never 
4439  * fails, but the returned object might not support any I/O 
4440  * operation if @uri is malformed or if the uri type is 
4441  * not supported.
4442  * 
4443  * Returns: a #GFile for the given @uri.
4444  **/ 
4445 GFile *
4446 g_file_new_for_uri (const char *uri)
4447 {
4448   g_return_val_if_fail (uri != NULL, NULL);
4449
4450   return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4451 }
4452   
4453 /**
4454  * g_file_parse_name:
4455  * @parse_name: a file name or path to be parsed.
4456  * 
4457  * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4458  * This operation never fails, but the returned object might not support any I/O
4459  * operation if the @parse_name cannot be parsed.
4460  * 
4461  * Returns: a new #GFile.
4462  **/
4463 GFile *
4464 g_file_parse_name (const char *parse_name)
4465 {
4466   g_return_val_if_fail (parse_name != NULL, NULL);
4467
4468   return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4469 }
4470
4471 static gboolean
4472 is_valid_scheme_character (char c)
4473 {
4474   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4475 }
4476
4477 static gboolean
4478 has_valid_scheme (const char *uri)
4479 {
4480   const char *p;
4481   
4482   p = uri;
4483   
4484   if (!is_valid_scheme_character (*p))
4485     return FALSE;
4486
4487   do {
4488     p++;
4489   } while (is_valid_scheme_character (*p));
4490
4491   return *p == ':';
4492 }
4493
4494 /**
4495  * g_file_new_for_commandline_arg:
4496  * @arg: a command line string.
4497  * 
4498  * Creates a #GFile with the given argument from the command line. The value of
4499  * @arg can be either a URI, an absolute path or a relative path resolved
4500  * relative to the current working directory.
4501  * This operation never fails, but the returned object might not support any
4502  * I/O operation if @arg points to a malformed path.
4503  *
4504  * Returns: a new #GFile. 
4505  **/
4506 GFile *
4507 g_file_new_for_commandline_arg (const char *arg)
4508 {
4509   GFile *file;
4510   char *filename;
4511   char *current_dir;
4512   
4513   g_return_val_if_fail (arg != NULL, NULL);
4514   
4515   if (g_path_is_absolute (arg))
4516     return g_file_new_for_path (arg);
4517
4518   if (has_valid_scheme (arg))
4519     return g_file_new_for_uri (arg);
4520     
4521   current_dir = g_get_current_dir ();
4522   filename = g_build_filename (current_dir, arg, NULL);
4523   g_free (current_dir);
4524   
4525   file = g_file_new_for_path (filename);
4526   g_free (filename);
4527   
4528   return file;
4529 }
4530
4531 /**
4532  * g_file_mount_enclosing_volume:
4533  * @location: input #GFile.
4534  * @flags: flags affecting the operation
4535  * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4536  * @cancellable: optional #GCancellable object, %NULL to ignore.
4537  * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4538  * @user_data: the data to pass to callback function
4539  * 
4540  * Starts a @mount_operation, mounting the volume that contains the file @location. 
4541  * 
4542  * When this operation has completed, @callback will be called with
4543  * @user_user data, and the operation can be finalized with 
4544  * g_file_mount_enclosing_volume_finish().
4545  * 
4546  * If @cancellable is not %NULL, then the operation can be cancelled by
4547  * triggering the cancellable object from another thread. If the operation
4548  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4549  **/
4550 void
4551 g_file_mount_enclosing_volume (GFile               *location,
4552                                GMountMountFlags     flags,
4553                                GMountOperation     *mount_operation,
4554                                GCancellable        *cancellable,
4555                                GAsyncReadyCallback  callback,
4556                                gpointer             user_data)
4557 {
4558   GFileIface *iface;
4559
4560   g_return_if_fail (G_IS_FILE (location));
4561
4562   iface = G_FILE_GET_IFACE (location);
4563
4564   if (iface->mount_enclosing_volume == NULL)
4565     {
4566       g_simple_async_report_error_in_idle (G_OBJECT (location),
4567                                            callback, user_data,
4568                                            G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4569                                            _("volume doesn't implement mount"));
4570       
4571       return;
4572     }
4573   
4574   (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4575
4576 }
4577
4578 /**
4579  * g_file_mount_enclosing_volume_finish:
4580  * @location: input #GFile.
4581  * @result: a #GAsyncResult.
4582  * @error: a #GError, or %NULL
4583  * 
4584  * Finishes a mount operation started by g_file_mount_enclosing_volume().
4585  * 
4586  * Returns: %TRUE if successful. If an error
4587  * has occurred, this function will return %FALSE and set @error
4588  * appropriately if present.
4589  **/
4590 gboolean
4591 g_file_mount_enclosing_volume_finish (GFile         *location,
4592                                       GAsyncResult  *result,
4593                                       GError       **error)
4594 {
4595   GFileIface *iface;
4596
4597   g_return_val_if_fail (G_IS_FILE (location), FALSE);
4598   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4599
4600   if (G_IS_SIMPLE_ASYNC_RESULT (result))
4601     {
4602       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4603       if (g_simple_async_result_propagate_error (simple, error))
4604         return FALSE;
4605     }
4606   
4607   iface = G_FILE_GET_IFACE (location);
4608
4609   return (* iface->mount_enclosing_volume_finish) (location, result, error);
4610 }
4611
4612 /********************************************
4613  *   Utility functions                      *
4614  ********************************************/
4615
4616 /**
4617  * g_file_query_default_handler:
4618  * @file: a #GFile to open.
4619  * @cancellable: optional #GCancellable object, %NULL to ignore.
4620  * @error: a #GError, or %NULL
4621  *
4622  * Returns the #GAppInfo that is registered as the default
4623  * application to handle the file specified by @file.
4624  *
4625  * If @cancellable is not %NULL, then the operation can be cancelled by
4626  * triggering the cancellable object from another thread. If the operation
4627  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4628  *
4629  * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4630  * When you are done with it, release it with g_object_unref()
4631  **/
4632 GAppInfo *
4633 g_file_query_default_handler (GFile                  *file,
4634                               GCancellable           *cancellable,
4635                               GError                **error)
4636 {
4637   char *uri_scheme;
4638   const char *content_type;
4639   GAppInfo *appinfo;
4640   GFileInfo *info;
4641   char *path;
4642   
4643   uri_scheme = g_file_get_uri_scheme (file);
4644   appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4645   g_free (uri_scheme);
4646
4647   if (appinfo != NULL)
4648     return appinfo;
4649
4650   info = g_file_query_info (file,
4651                             G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4652                             0,
4653                             cancellable,
4654                             error);
4655   if (info == NULL)
4656     return NULL;
4657
4658   appinfo = NULL;
4659
4660   content_type = g_file_info_get_content_type (info);
4661   if (content_type)
4662     {
4663       /* Don't use is_native(), as we want to support fuse paths if availible */
4664       path = g_file_get_path (file);
4665       appinfo = g_app_info_get_default_for_type (content_type,
4666                                                  path == NULL);
4667       g_free (path);
4668     }
4669   
4670   g_object_unref (info);
4671
4672   if (appinfo != NULL)
4673     return appinfo;
4674
4675   g_set_error (error, G_IO_ERROR,
4676                G_IO_ERROR_NOT_SUPPORTED,
4677                _("No application is registered as handling this file"));
4678   return NULL;
4679   
4680 }
4681
4682
4683 #define GET_CONTENT_BLOCK_SIZE 8192
4684
4685 /**
4686  * g_file_load_contents:
4687  * @file: input #GFile.
4688  * @cancellable: optional #GCancellable object, %NULL to ignore.
4689  * @contents: a location to place the contents of the file.
4690  * @length: a location to place the length of the contents of the file.
4691  * @etag_out: a location to place the current entity tag for the file.
4692  * @error: a #GError, or %NULL
4693  *
4694  * Loads the content of the file into memory, returning the size of
4695  * the data. The data is always zero terminated, but this is not
4696  * included in the resultant @length.
4697  * 
4698  * If @cancellable is not %NULL, then the operation can be cancelled by
4699  * triggering the cancellable object from another thread. If the operation
4700  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4701  * 
4702  * Returns: %TRUE if the @file's contents were successfully loaded.
4703  * %FALSE if there were errors..
4704  **/
4705 gboolean
4706 g_file_load_contents (GFile         *file,
4707                       GCancellable  *cancellable,
4708                       char         **contents,
4709                       gsize         *length,
4710                       char         **etag_out,
4711                       GError       **error)
4712 {
4713   GFileInputStream *in;
4714   GByteArray *content;
4715   gsize pos;
4716   gssize res;
4717   GFileInfo *info;
4718
4719   g_return_val_if_fail (G_IS_FILE (file), FALSE);
4720   g_return_val_if_fail (contents != NULL, FALSE);
4721
4722   in = g_file_read (file, cancellable, error);
4723   if (in == NULL)
4724     return FALSE;
4725
4726   content = g_byte_array_new ();
4727   pos = 0;
4728   
4729   g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4730   while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4731                                      content->data + pos,
4732                                      GET_CONTENT_BLOCK_SIZE,
4733                                      cancellable, error)) > 0)
4734     {
4735       pos += res;
4736       g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4737     }
4738
4739   if (etag_out)
4740     {
4741       *etag_out = NULL;
4742       
4743       info = g_file_input_stream_query_info (in,
4744                                              G_FILE_ATTRIBUTE_ETAG_VALUE,
4745                                              cancellable,
4746                                              NULL);
4747       if (info)
4748         {
4749           *etag_out = g_strdup (g_file_info_get_etag (info));
4750           g_object_unref (info);
4751         }
4752     } 
4753
4754   /* Ignore errors on close */
4755   g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4756   g_object_unref (in);
4757
4758   if (res < 0)
4759     {
4760       /* error is set already */
4761       g_byte_array_free (content, TRUE);
4762       return FALSE;
4763     }
4764
4765   if (length)
4766     *length = pos;
4767
4768   /* Zero terminate (we got an extra byte allocated for this */
4769   content->data[pos] = 0;
4770   
4771   *contents = (char *)g_byte_array_free (content, FALSE);
4772   
4773   return TRUE;
4774 }
4775
4776 typedef struct {
4777   GFile *file;
4778   GError *error;
4779   GCancellable *cancellable;
4780   GFileReadMoreCallback read_more_callback;
4781   GAsyncReadyCallback callback;
4782   gpointer user_data;
4783   GByteArray *content;
4784   gsize pos;
4785   char *etag;
4786 } LoadContentsData;
4787
4788
4789 static void
4790 load_contents_data_free (LoadContentsData *data)
4791 {
4792   if (data->error)
4793     g_error_free (data->error);
4794   if (data->cancellable)
4795     g_object_unref (data->cancellable);
4796   if (data->content)
4797     g_byte_array_free (data->content, TRUE);
4798   g_free (data->etag);
4799   g_object_unref (data->file);
4800   g_free (data);
4801 }
4802
4803 static void
4804 load_contents_close_callback (GObject      *obj,
4805                               GAsyncResult *close_res,
4806                               gpointer      user_data)
4807 {
4808   GInputStream *stream = G_INPUT_STREAM (obj);
4809   LoadContentsData *data = user_data;
4810   GSimpleAsyncResult *res;
4811
4812   /* Ignore errors here, we're only reading anyway */
4813   g_input_stream_close_finish (stream, close_res, NULL);
4814   g_object_unref (stream);
4815
4816   res = g_simple_async_result_new (G_OBJECT (data->file),
4817                                    data->callback,
4818                                    data->user_data,
4819                                    g_file_load_contents_async);
4820   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4821   g_simple_async_result_complete (res);
4822   g_object_unref (res);
4823 }
4824
4825 static void
4826 load_contents_fstat_callback (GObject      *obj,
4827                               GAsyncResult *stat_res,
4828                               gpointer      user_data)
4829 {
4830   GInputStream *stream = G_INPUT_STREAM (obj);
4831   LoadContentsData *data = user_data;
4832   GFileInfo *info;
4833
4834   info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4835                                                    stat_res, NULL);
4836   if (info)
4837     {
4838       data->etag = g_strdup (g_file_info_get_etag (info));
4839       g_object_unref (info);
4840     }
4841
4842   g_input_stream_close_async (stream, 0,
4843                               data->cancellable,
4844                               load_contents_close_callback, data);
4845 }
4846
4847 static void
4848 load_contents_read_callback (GObject      *obj,
4849                              GAsyncResult *read_res,
4850                              gpointer      user_data)
4851 {
4852   GInputStream *stream = G_INPUT_STREAM (obj);
4853   LoadContentsData *data = user_data;
4854   GError *error = NULL;
4855   gssize read_size;
4856
4857   read_size = g_input_stream_read_finish (stream, read_res, &error);
4858
4859   if (read_size < 0) 
4860     {
4861       /* Error or EOF, close the file */
4862       data->error = error;
4863       g_input_stream_close_async (stream, 0,
4864                                   data->cancellable,
4865                                   load_contents_close_callback, data);
4866     }
4867   else if (read_size == 0)
4868     {
4869       g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4870                                             G_FILE_ATTRIBUTE_ETAG_VALUE,
4871                                             0,
4872                                             data->cancellable,
4873                                             load_contents_fstat_callback,
4874                                             data);
4875     }
4876   else if (read_size > 0)
4877     {
4878       data->pos += read_size;
4879       
4880       g_byte_array_set_size (data->content,
4881                              data->pos + GET_CONTENT_BLOCK_SIZE);
4882
4883
4884       if (data->read_more_callback &&
4885           !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4886         g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4887                                               G_FILE_ATTRIBUTE_ETAG_VALUE,
4888                                               0,
4889                                               data->cancellable,
4890                                               load_contents_fstat_callback,
4891                                               data);
4892       else 
4893         g_input_stream_read_async (stream,
4894                                    data->content->data + data->pos,
4895                                    GET_CONTENT_BLOCK_SIZE,
4896                                    0,
4897                                    data->cancellable,
4898                                    load_contents_read_callback,
4899                                    data);
4900     }
4901 }
4902
4903 static void
4904 load_contents_open_callback (GObject      *obj,
4905                              GAsyncResult *open_res,
4906                              gpointer      user_data)
4907 {
4908   GFile *file = G_FILE (obj);
4909   GFileInputStream *stream;
4910   LoadContentsData *data = user_data;
4911   GError *error = NULL;
4912   GSimpleAsyncResult *res;
4913
4914   stream = g_file_read_finish (file, open_res, &error);
4915
4916   if (stream)
4917     {
4918       g_byte_array_set_size (data->content,
4919                              data->pos + GET_CONTENT_BLOCK_SIZE);
4920       g_input_stream_read_async (G_INPUT_STREAM (stream),
4921                                  data->content->data + data->pos,
4922                                  GET_CONTENT_BLOCK_SIZE,
4923                                  0,
4924                                  data->cancellable,
4925                                  load_contents_read_callback,
4926                                  data);
4927       
4928     }
4929   else
4930     {
4931       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4932                                                   data->callback,
4933                                                   data->user_data,
4934                                                   error);
4935       g_simple_async_result_complete (res);
4936       g_error_free (error);
4937       load_contents_data_free (data);
4938       g_object_unref (res);
4939     }
4940 }
4941
4942 /**
4943  * g_file_load_partial_contents_async:
4944  * @file: input #GFile.
4945  * @cancellable: optional #GCancellable object, %NULL to ignore.
4946  * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
4947  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4948  * @user_data: the data to pass to the callback functions.
4949  *
4950  * Reads the partial contents of a file. A #GFileReadMoreCallback should be 
4951  * used to stop reading from the file when appropriate, else this function
4952  * will behave exactly as g_file_load_contents_async(). This operation 
4953  * can be finished by g_file_load_partial_contents_finish().
4954  *
4955  * Users of this function should be aware that @user_data is passed to 
4956  * both the @read_more_callback and the @callback.
4957  *
4958  * If @cancellable is not %NULL, then the operation can be cancelled by
4959  * triggering the cancellable object from another thread. If the operation
4960  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
4961  **/
4962 void
4963 g_file_load_partial_contents_async (GFile                 *file,
4964                                     GCancellable          *cancellable,
4965                                     GFileReadMoreCallback  read_more_callback,
4966                                     GAsyncReadyCallback    callback,
4967                                     gpointer               user_data)
4968 {
4969   LoadContentsData *data;
4970
4971   g_return_if_fail (G_IS_FILE (file));
4972
4973   data = g_new0 (LoadContentsData, 1);
4974
4975   if (cancellable)
4976     data->cancellable = g_object_ref (cancellable);
4977   data->read_more_callback = read_more_callback;
4978   data->callback = callback;
4979   data->user_data = user_data;
4980   data->content = g_byte_array_new ();
4981   data->file = g_object_ref (file);
4982
4983   g_file_read_async (file,
4984                      0,
4985                      cancellable,
4986                      load_contents_open_callback,
4987                      data);
4988 }
4989
4990 /**
4991  * g_file_load_partial_contents_finish:
4992  * @file: input #GFile.
4993  * @res: a #GAsyncResult. 
4994  * @contents: a location to place the contents of the file.
4995  * @length: a location to place the length of the contents of the file.
4996  * @etag_out: a location to place the current entity tag for the file.
4997  * @error: a #GError, or %NULL
4998  * 
4999  * Finishes an asynchronous partial load operation that was started
5000  * with g_file_load_partial_contents_async().
5001  *
5002  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
5003  * present, it will be set appropriately. 
5004  **/
5005 gboolean
5006 g_file_load_partial_contents_finish (GFile         *file,
5007                                      GAsyncResult  *res,
5008                                      char         **contents,
5009                                      gsize         *length,
5010                                      char         **etag_out,
5011                                      GError       **error)
5012 {
5013   GSimpleAsyncResult *simple;
5014   LoadContentsData *data;
5015
5016   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5017   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5018   g_return_val_if_fail (contents != NULL, FALSE);
5019
5020   simple = G_SIMPLE_ASYNC_RESULT (res);
5021
5022   if (g_simple_async_result_propagate_error (simple, error))
5023     return FALSE;
5024   
5025   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5026   
5027   data = g_simple_async_result_get_op_res_gpointer (simple);
5028
5029   if (data->error)
5030     {
5031       g_propagate_error (error, data->error);
5032       data->error = NULL;
5033       *contents = NULL;
5034       if (length)
5035         *length = 0;
5036       return FALSE;
5037     }
5038
5039   if (length)
5040     *length = data->pos;
5041
5042   if (etag_out)
5043     {
5044       *etag_out = data->etag;
5045       data->etag = NULL;
5046     }
5047
5048   /* Zero terminate */
5049   g_byte_array_set_size (data->content, data->pos + 1);
5050   data->content->data[data->pos] = 0;
5051   
5052   *contents = (char *)g_byte_array_free (data->content, FALSE);
5053   data->content = NULL;
5054
5055   return TRUE;
5056 }
5057
5058 /**
5059  * g_file_load_contents_async:
5060  * @file: input #GFile.
5061  * @cancellable: optional #GCancellable object, %NULL to ignore.
5062  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5063  * @user_data: the data to pass to callback function
5064  * 
5065  * Starts an asynchronous load of the @file's contents.
5066  *
5067  * For more details, see g_file_load_contents() which is
5068  * the synchronous version of this call.
5069  *
5070  * When the load operation has completed, @callback will be called 
5071  * with @user data. To finish the operation, call 
5072  * g_file_load_contents_finish() with the #GAsyncResult returned by 
5073  * the @callback.
5074  * 
5075  * If @cancellable is not %NULL, then the operation can be cancelled by
5076  * triggering the cancellable object from another thread. If the operation
5077  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5078  **/
5079 void
5080 g_file_load_contents_async (GFile               *file,
5081                            GCancellable        *cancellable,
5082                            GAsyncReadyCallback  callback,
5083                            gpointer             user_data)
5084 {
5085   g_file_load_partial_contents_async (file,
5086                                       cancellable,
5087                                       NULL,
5088                                       callback, user_data);
5089 }
5090
5091 /**
5092  * g_file_load_contents_finish:
5093  * @file: input #GFile.
5094  * @res: a #GAsyncResult. 
5095  * @contents: a location to place the contents of the file.
5096  * @length: a location to place the length of the contents of the file.
5097  * @etag_out: a location to place the current entity tag for the file.
5098  * @error: a #GError, or %NULL
5099  * 
5100  * Finishes an asynchronous load of the @file's contents. 
5101  * The contents are placed in @contents, and @length is set to the 
5102  * size of the @contents string. If @etag_out is present, it will be 
5103  * set to the new entity tag for the @file.
5104  * 
5105  * Returns: %TRUE if the load was successful. If %FALSE and @error is 
5106  * present, it will be set appropriately. 
5107  **/
5108 gboolean
5109 g_file_load_contents_finish (GFile         *file,
5110                              GAsyncResult  *res,
5111                              char         **contents,
5112                              gsize         *length,
5113                              char         **etag_out,
5114                              GError       **error)
5115 {
5116   return g_file_load_partial_contents_finish (file,
5117                                               res,
5118                                               contents,
5119                                               length,
5120                                               etag_out,
5121                                               error);
5122 }
5123   
5124 /**
5125  * g_file_replace_contents:
5126  * @file: input #GFile.
5127  * @contents: a string containing the new contents for @file.
5128  * @length: the length of @contents in bytes.
5129  * @etag: the old <link linkend="gfile-etag">entity tag</link> 
5130  *     for the document.
5131  * @make_backup: %TRUE if a backup should be created.
5132  * @flags: a set of #GFileCreateFlags.
5133  * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5134  *      for the document. This should be freed with g_free() when no longer 
5135  *      needed.
5136  * @cancellable: optional #GCancellable object, %NULL to ignore.
5137  * @error: a #GError, or %NULL
5138  *
5139  * Replaces the contents of @file with @contents of @length bytes.
5140  
5141  * If @etag is specified (not %NULL) any existing file must have that etag, or
5142  * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5143  *
5144  * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5145  * 
5146  * If @cancellable is not %NULL, then the operation can be cancelled by
5147  * triggering the cancellable object from another thread. If the operation
5148  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5149  *
5150  * The returned @new_etag can be used to verify that the file hasn't changed the
5151  * next time it is saved over.
5152  * 
5153  * Returns: %TRUE if successful. If an error
5154  * has occurred, this function will return %FALSE and set @error
5155  * appropriately if present.
5156  **/
5157 gboolean
5158 g_file_replace_contents (GFile             *file,
5159                          const char        *contents,
5160                          gsize              length,
5161                          const char        *etag,
5162                          gboolean           make_backup,
5163                          GFileCreateFlags   flags,
5164                          char             **new_etag,
5165                          GCancellable      *cancellable,
5166                          GError           **error)
5167 {
5168   GFileOutputStream *out;
5169   gsize pos, remainder;
5170   gssize res;
5171
5172   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5173   g_return_val_if_fail (contents != NULL, FALSE);
5174
5175   out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5176   if (out == NULL)
5177     return FALSE;
5178
5179   pos = 0;
5180   remainder = length;
5181   while (remainder > 0 &&
5182          (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5183                                        contents + pos,
5184                                        MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5185                                        cancellable,
5186                                        error)) > 0)
5187     {
5188       pos += res;
5189       remainder -= res;
5190     }
5191   
5192   if (remainder > 0 && res < 0)
5193     {
5194       /* Ignore errors on close */
5195       g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5196       
5197       /* error is set already */
5198       return FALSE;
5199     }
5200   
5201   if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5202     return FALSE;
5203
5204   if (new_etag)
5205     *new_etag = g_file_output_stream_get_etag (out);
5206   
5207   return TRUE;
5208 }
5209
5210 typedef struct {
5211   GFile *file;
5212   GError *error;
5213   GCancellable *cancellable;
5214   GAsyncReadyCallback callback;
5215   gpointer user_data;
5216   const char *content;
5217   gsize length;
5218   gsize pos;
5219   char *etag;
5220 } ReplaceContentsData;
5221
5222 static void
5223 replace_contents_data_free (ReplaceContentsData *data)
5224 {
5225   if (data->error)
5226     g_error_free (data->error);
5227   if (data->cancellable)
5228     g_object_unref (data->cancellable);
5229   g_object_unref (data->file);
5230   g_free (data->etag);
5231   g_free (data);
5232 }
5233
5234 static void
5235 replace_contents_close_callback (GObject      *obj,
5236                                  GAsyncResult *close_res,
5237                                  gpointer      user_data)
5238 {
5239   GOutputStream *stream = G_OUTPUT_STREAM (obj);
5240   ReplaceContentsData *data = user_data;
5241   GSimpleAsyncResult *res;
5242
5243   /* Ignore errors here, we're only reading anyway */
5244   g_output_stream_close_finish (stream, close_res, NULL);
5245   g_object_unref (stream);
5246
5247   data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5248   
5249   res = g_simple_async_result_new (G_OBJECT (data->file),
5250                                    data->callback,
5251                                    data->user_data,
5252                                    g_file_replace_contents_async);
5253   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5254   g_simple_async_result_complete (res);
5255   g_object_unref (res);
5256 }
5257
5258 static void
5259 replace_contents_write_callback (GObject      *obj,
5260                                  GAsyncResult *read_res,
5261                                  gpointer      user_data)
5262 {
5263   GOutputStream *stream = G_OUTPUT_STREAM (obj);
5264   ReplaceContentsData *data = user_data;
5265   GError *error = NULL;
5266   gssize write_size;
5267   
5268   write_size = g_output_stream_write_finish (stream, read_res, &error);
5269
5270   if (write_size <= 0) 
5271     {
5272       /* Error or EOF, close the file */
5273       if (write_size < 0)
5274         data->error = error;
5275       g_output_stream_close_async (stream, 0,
5276                                    data->cancellable,
5277                                    replace_contents_close_callback, data);
5278     }
5279   else if (write_size > 0)
5280     {
5281       data->pos += write_size;
5282
5283       if (data->pos >= data->length)
5284         g_output_stream_close_async (stream, 0,
5285                                      data->cancellable,
5286                                      replace_contents_close_callback, data);
5287       else
5288         g_output_stream_write_async (stream,
5289                                      data->content + data->pos,
5290                                      data->length - data->pos,
5291                                      0,
5292                                      data->cancellable,
5293                                      replace_contents_write_callback,
5294                                      data);
5295     }
5296 }
5297
5298 static void
5299 replace_contents_open_callback (GObject      *obj,
5300                                 GAsyncResult *open_res,
5301                                 gpointer      user_data)
5302 {
5303   GFile *file = G_FILE (obj);
5304   GFileOutputStream *stream;
5305   ReplaceContentsData *data = user_data;
5306   GError *error = NULL;
5307   GSimpleAsyncResult *res;
5308
5309   stream = g_file_replace_finish (file, open_res, &error);
5310
5311   if (stream)
5312     {
5313       g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5314                                    data->content + data->pos,
5315                                    data->length - data->pos,
5316                                    0,
5317                                    data->cancellable,
5318                                    replace_contents_write_callback,
5319                                    data);
5320       
5321     }
5322   else
5323     {
5324       res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5325                                                   data->callback,
5326                                                   data->user_data,
5327                                                   error);
5328       g_simple_async_result_complete (res);
5329       g_error_free (error);
5330       replace_contents_data_free (data);
5331       g_object_unref (res);
5332     }
5333 }
5334
5335 /**
5336  * g_file_replace_contents_async:
5337  * @file: input #GFile.
5338  * @contents: string of contents to replace the file with.
5339  * @length: the length of @contents in bytes.
5340  * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5341  * @make_backup: %TRUE if a backup should be created.
5342  * @flags: a set of #GFileCreateFlags.
5343  * @cancellable: optional #GCancellable object, %NULL to ignore.
5344  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5345  * @user_data: the data to pass to callback function
5346  * 
5347  * Starts an asynchronous replacement of @file with the given 
5348  * @contents of @length bytes. @etag will replace the document's 
5349  * current entity tag.
5350  * 
5351  * When this operation has completed, @callback will be called with
5352  * @user_user data, and the operation can be finalized with 
5353  * g_file_replace_contents_finish().
5354  * 
5355  * If @cancellable is not %NULL, then the operation can be cancelled by
5356  * triggering the cancellable object from another thread. If the operation
5357  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. 
5358  * 
5359  * If @make_backup is %TRUE, this function will attempt to 
5360  * make a backup of @file.
5361  **/
5362 void
5363 g_file_replace_contents_async  (GFile               *file,
5364                                 const char          *contents,
5365                                 gsize                length,
5366                                 const char          *etag,
5367                                 gboolean             make_backup,
5368                                 GFileCreateFlags     flags,
5369                                 GCancellable        *cancellable,
5370                                 GAsyncReadyCallback  callback,
5371                                 gpointer             user_data)
5372 {
5373   ReplaceContentsData *data;
5374
5375   g_return_if_fail (G_IS_FILE (file));
5376   g_return_if_fail (contents != NULL);
5377
5378   data = g_new0 (ReplaceContentsData, 1);
5379
5380   if (cancellable)
5381     data->cancellable = g_object_ref (cancellable);
5382   data->callback = callback;
5383   data->user_data = user_data;
5384   data->content = contents;
5385   data->length = length;
5386   data->pos = 0;
5387   data->file = g_object_ref (file);
5388
5389   g_file_replace_async (file,
5390                         etag,
5391                         make_backup,
5392                         flags,
5393                         0,
5394                         cancellable,
5395                         replace_contents_open_callback,
5396                         data);
5397 }
5398   
5399 /**
5400  * g_file_replace_contents_finish:
5401  * @file: input #GFile.
5402  * @res: a #GAsyncResult. 
5403  * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link> 
5404  *     for the document. This should be freed with g_free() when it is no 
5405  *     longer needed.
5406  * @error: a #GError, or %NULL 
5407  * 
5408  * Finishes an asynchronous replace of the given @file. See
5409  * g_file_replace_contents_async(). Sets @new_etag to the new entity 
5410  * tag for the document, if present.
5411  * 
5412  * Returns: %TRUE on success, %FALSE on failure.
5413  **/
5414 gboolean
5415 g_file_replace_contents_finish (GFile         *file,
5416                                 GAsyncResult  *res,
5417                                 char         **new_etag,
5418                                 GError       **error)
5419 {
5420   GSimpleAsyncResult *simple;
5421   ReplaceContentsData *data;
5422
5423   g_return_val_if_fail (G_IS_FILE (file), FALSE);
5424   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5425
5426   simple = G_SIMPLE_ASYNC_RESULT (res);
5427
5428   if (g_simple_async_result_propagate_error (simple, error))
5429     return FALSE;
5430   
5431   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5432   
5433   data = g_simple_async_result_get_op_res_gpointer (simple);
5434
5435   if (data->error)
5436     {
5437       g_propagate_error (error, data->error);
5438       data->error = NULL;
5439       return FALSE;
5440     }
5441
5442
5443   if (new_etag)
5444     {
5445       *new_etag = data->etag;
5446       data->etag = NULL; /* Take ownership */
5447     }
5448   
5449   return TRUE;
5450 }
5451
5452 #define __G_FILE_C__
5453 #include "gioaliasdef.c"