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