Fix the build
[glib.git] / gio / gfile.c
blob941fa81f5d4e44c1e98f10ba5d5517597b34480e
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <config.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #ifdef HAVE_PWD_H
29 #include <pwd.h>
30 #endif
31 #include "gfile.h"
32 #include "gvfs.h"
33 #include "gioscheduler.h"
34 #include <glocalfile.h>
35 #include "gsimpleasyncresult.h"
36 #include "gfileattribute-priv.h"
37 #include "gpollfilemonitor.h"
38 #include "glibintl.h"
40 #include "gioalias.h"
42 /**
43 * SECTION:gfile
44 * @short_description: File and Directory Handling
45 * @include: gio/gio.h
46 * @see_also: #GFileInfo, #GFileEnumerator
48 * #GFile is a high level abstraction for manipulating files on a
49 * virtual file system. #GFile<!-- -->s are lightweight, immutable
50 * objects that do no I/O upon creation. It is necessary to understand that
51 * #GFile objects do not represent files, merely an identifier for a file. All
52 * file content I/O is implemented as streaming operations (see #GInputStream and
53 * #GOutputStream).
55 * To construct a #GFile, you can use:
56 * g_file_new_for_path() if you have a path.
57 * g_file_new_for_uri() if you have a URI.
58 * g_file_new_for_commandline_arg() for a command line argument.
59 * g_file_parse_name() from a utf8 string gotten from g_file_get_parse_name().
61 * One way to think of a #GFile is as an abstraction of a pathname. For normal
62 * files the system pathname is what is stored internally, but as #GFile<!-- -->s
63 * are extensible it could also be something else that corresponds to a pathname
64 * in a userspace implementation of a filesystem.
66 * #GFile<!-- -->s make up hierarchies of directories and files that correspond to the
67 * files on a filesystem. You can move through the file system with #GFile using
68 * g_file_get_parent() to get an identifier for the parent directory, g_file_get_child()
69 * to get a child within a directory, g_file_resolve_relative_path() to resolve a relative
70 * path between two #GFile<!-- -->s. There can be multiple hierarchies, so you may not
71 * end up at the same root if you repeatedly call g_file_get_parent() on two different
72 * files.
74 * All #GFile<!-- -->s have a basename (get with g_file_get_basename()). These names
75 * are byte strings that are used to identify the file on the filesystem (relative to
76 * its parent directory) and there is no guarantees that they have any particular charset
77 * encoding or even make any sense at all. If you want to use filenames in a user
78 * interface you should use the display name that you can get by requesting the
79 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
80 * This is guaranteed to be in utf8 and can be used in a user interface. But always
81 * store the real basename or the #GFile to use to actually access the file, because
82 * there is no way to go from a display name to the actual name.
84 * Using #GFile as an identifier has the same weaknesses as using a path in that
85 * there may be multiple aliases for the same file. For instance, hard or
86 * soft links may cause two different #GFile<!-- -->s to refer to the same file.
87 * Other possible causes for aliases are: case insensitive filesystems, short
88 * and long names on Fat/NTFS, or bind mounts in Linux. If you want to check if
89 * two #GFile<!-- -->s point to the same file you can query for the
90 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
91 * canonicalization of pathnames passed in, so that trivial differences in the
92 * path string used at creation (duplicated slashes, slash at end of path, "."
93 * or ".." path segments, etc) does not create different #GFile<!-- -->s.
95 * Many #GFile operations have both synchronous and asynchronous versions
96 * to suit your application. Asynchronous versions of synchronous functions
97 * simply have _async() appended to their function names. The asynchronous
98 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
99 * the operation, producing a GAsyncResult which is then passed to the
100 * function's matching _finish() operation.
102 * Some #GFile operations do not have synchronous analogs, as they may
103 * take a very long time to finish, and blocking may leave an application
104 * unusable. Notable cases include:
105 * g_file_mount_mountable() to mount a mountable file.
106 * g_file_unmount_mountable() to unmount a mountable file.
107 * g_file_eject_mountable() to eject a mountable file.
109 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
110 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
111 * short. Entity tags are somewhat like a more abstract version of the
112 * traditional mtime, and can be used to quickly determine if the file has
113 * been modified from the version on the file system. See the HTTP 1.1
114 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
115 * for HTTP Etag headers, which are a very similar concept.
116 * </para>
119 static void g_file_base_init (gpointer g_class);
120 static void g_file_class_init (gpointer g_class,
121 gpointer class_data);
123 static void g_file_real_query_info_async (GFile *file,
124 const char *attributes,
125 GFileQueryInfoFlags flags,
126 int io_priority,
127 GCancellable *cancellable,
128 GAsyncReadyCallback callback,
129 gpointer user_data);
130 static GFileInfo * g_file_real_query_info_finish (GFile *file,
131 GAsyncResult *res,
132 GError **error);
133 static void g_file_real_query_filesystem_info_async (GFile *file,
134 const char *attributes,
135 int io_priority,
136 GCancellable *cancellable,
137 GAsyncReadyCallback callback,
138 gpointer user_data);
139 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
140 GAsyncResult *res,
141 GError **error);
142 static void g_file_real_enumerate_children_async (GFile *file,
143 const char *attributes,
144 GFileQueryInfoFlags flags,
145 int io_priority,
146 GCancellable *cancellable,
147 GAsyncReadyCallback callback,
148 gpointer user_data);
149 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
150 GAsyncResult *res,
151 GError **error);
152 static void g_file_real_read_async (GFile *file,
153 int io_priority,
154 GCancellable *cancellable,
155 GAsyncReadyCallback callback,
156 gpointer user_data);
157 static GFileInputStream * g_file_real_read_finish (GFile *file,
158 GAsyncResult *res,
159 GError **error);
160 static void g_file_real_append_to_async (GFile *file,
161 GFileCreateFlags flags,
162 int io_priority,
163 GCancellable *cancellable,
164 GAsyncReadyCallback callback,
165 gpointer user_data);
166 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
167 GAsyncResult *res,
168 GError **error);
169 static void g_file_real_create_async (GFile *file,
170 GFileCreateFlags flags,
171 int io_priority,
172 GCancellable *cancellable,
173 GAsyncReadyCallback callback,
174 gpointer user_data);
175 static GFileOutputStream *g_file_real_create_finish (GFile *file,
176 GAsyncResult *res,
177 GError **error);
178 static void g_file_real_replace_async (GFile *file,
179 const char *etag,
180 gboolean make_backup,
181 GFileCreateFlags flags,
182 int io_priority,
183 GCancellable *cancellable,
184 GAsyncReadyCallback callback,
185 gpointer user_data);
186 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
187 GAsyncResult *res,
188 GError **error);
189 static gboolean g_file_real_set_attributes_from_info (GFile *file,
190 GFileInfo *info,
191 GFileQueryInfoFlags flags,
192 GCancellable *cancellable,
193 GError **error);
194 static void g_file_real_set_display_name_async (GFile *file,
195 const char *display_name,
196 int io_priority,
197 GCancellable *cancellable,
198 GAsyncReadyCallback callback,
199 gpointer user_data);
200 static GFile * g_file_real_set_display_name_finish (GFile *file,
201 GAsyncResult *res,
202 GError **error);
203 static void g_file_real_set_attributes_async (GFile *file,
204 GFileInfo *info,
205 GFileQueryInfoFlags flags,
206 int io_priority,
207 GCancellable *cancellable,
208 GAsyncReadyCallback callback,
209 gpointer user_data);
210 static gboolean g_file_real_set_attributes_finish (GFile *file,
211 GAsyncResult *res,
212 GFileInfo **info,
213 GError **error);
214 static void g_file_real_find_enclosing_mount_async (GFile *file,
215 int io_priority,
216 GCancellable *cancellable,
217 GAsyncReadyCallback callback,
218 gpointer user_data);
219 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
220 GAsyncResult *res,
221 GError **error);
222 static void g_file_real_copy_async (GFile *source,
223 GFile *destination,
224 GFileCopyFlags flags,
225 int io_priority,
226 GCancellable *cancellable,
227 GFileProgressCallback progress_callback,
228 gpointer progress_callback_data,
229 GAsyncReadyCallback callback,
230 gpointer user_data);
231 static gboolean g_file_real_copy_finish (GFile *file,
232 GAsyncResult *res,
233 GError **error);
235 GType
236 g_file_get_type (void)
238 static GType file_type = 0;
240 if (! file_type)
242 static const GTypeInfo file_info =
244 sizeof (GFileIface), /* class_size */
245 g_file_base_init, /* base_init */
246 NULL, /* base_finalize */
247 g_file_class_init,
248 NULL, /* class_finalize */
249 NULL, /* class_data */
251 0, /* n_preallocs */
252 NULL
255 file_type =
256 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
257 &file_info, 0);
259 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
262 return file_type;
265 static void
266 g_file_class_init (gpointer g_class,
267 gpointer class_data)
269 GFileIface *iface = g_class;
271 iface->enumerate_children_async = g_file_real_enumerate_children_async;
272 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
273 iface->set_display_name_async = g_file_real_set_display_name_async;
274 iface->set_display_name_finish = g_file_real_set_display_name_finish;
275 iface->query_info_async = g_file_real_query_info_async;
276 iface->query_info_finish = g_file_real_query_info_finish;
277 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
278 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
279 iface->set_attributes_async = g_file_real_set_attributes_async;
280 iface->set_attributes_finish = g_file_real_set_attributes_finish;
281 iface->read_async = g_file_real_read_async;
282 iface->read_finish = g_file_real_read_finish;
283 iface->append_to_async = g_file_real_append_to_async;
284 iface->append_to_finish = g_file_real_append_to_finish;
285 iface->create_async = g_file_real_create_async;
286 iface->create_finish = g_file_real_create_finish;
287 iface->replace_async = g_file_real_replace_async;
288 iface->replace_finish = g_file_real_replace_finish;
289 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
290 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
291 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
292 iface->copy_async = g_file_real_copy_async;
293 iface->copy_finish = g_file_real_copy_finish;
296 static void
297 g_file_base_init (gpointer g_class)
303 * g_file_is_native:
304 * @file: input #GFile.
306 * Checks to see if a file is native to the platform.
308 * A native file s one expressed in the platform-native filename format,
309 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
310 * as it might be on a locally mounted remote filesystem.
312 * On some systems non-native files may be available using
313 * the native filesystem via a userspace filesystem (FUSE), in
314 * these cases this call will return %FALSE, but g_file_get_path()
315 * will still return a native path.
317 * This call does no blocking i/o.
319 * Returns: %TRUE if file is native.
321 gboolean
322 g_file_is_native (GFile *file)
324 GFileIface *iface;
326 g_return_val_if_fail (G_IS_FILE (file), FALSE);
328 iface = G_FILE_GET_IFACE (file);
330 return (* iface->is_native) (file);
335 * g_file_has_uri_scheme:
336 * @file: input #GFile.
337 * @uri_scheme: a string containing a URI scheme.
339 * Checks to see if a #GFile has a given URI scheme.
341 * This call does no blocking i/o.
343 * Returns: %TRUE if #GFile's backend supports the
344 * given URI scheme, %FALSE if URI scheme is %NULL,
345 * not supported, or #GFile is invalid.
347 gboolean
348 g_file_has_uri_scheme (GFile *file,
349 const char *uri_scheme)
351 GFileIface *iface;
353 g_return_val_if_fail (G_IS_FILE (file), FALSE);
354 g_return_val_if_fail (uri_scheme != NULL, FALSE);
356 iface = G_FILE_GET_IFACE (file);
358 return (* iface->has_uri_scheme) (file, uri_scheme);
363 * g_file_get_uri_scheme:
364 * @file: input #GFile.
366 * Gets the URI scheme for a #GFile.
367 * RFC 3986 decodes the scheme as:
368 * <programlisting>
369 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
370 * </programlisting>
371 * Common schemes include "file", "http", "ftp", etc.
373 * This call does no blocking i/o.
375 * Returns: a string containing the URI scheme for the given
376 * #GFile. The returned string should be freed with g_free()
377 * when no longer needed.
379 char *
380 g_file_get_uri_scheme (GFile *file)
382 GFileIface *iface;
384 g_return_val_if_fail (G_IS_FILE (file), NULL);
386 iface = G_FILE_GET_IFACE (file);
388 return (* iface->get_uri_scheme) (file);
393 * g_file_get_basename:
394 * @file: input #GFile.
396 * Gets the base name (the last component of the path) for a given #GFile.
398 * If called for the top level of a system (such as the filesystem root
399 * or a uri like sftp://host/) it will return a single directory separator
400 * (and on Windows, possibly a drive letter).
402 * The base name is a byte string (*not* UTF-8). It has no defined encoding
403 * or rules other than it may not contain zero bytes. If you want to use
404 * filenames in a user interface you should use the display name that you
405 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
406 * attribute with g_file_query_info().
408 * This call does no blocking i/o.
410 * Returns: string containing the #GFile's base name, or %NULL
411 * if given #GFile is invalid. The returned string should be
412 * freed with g_free() when no longer needed.
414 char *
415 g_file_get_basename (GFile *file)
417 GFileIface *iface;
419 g_return_val_if_fail (G_IS_FILE (file), NULL);
421 iface = G_FILE_GET_IFACE (file);
423 return (* iface->get_basename) (file);
427 * g_file_get_path:
428 * @file: input #GFile.
430 * Gets the local pathname for #GFile, if one exists.
432 * This call does no blocking i/o.
434 * Returns: string containing the #GFile's path, or %NULL if
435 * no such path exists. The returned string should be
436 * freed with g_free() when no longer needed.
438 char *
439 g_file_get_path (GFile *file)
441 GFileIface *iface;
443 g_return_val_if_fail (G_IS_FILE (file), NULL);
445 iface = G_FILE_GET_IFACE (file);
447 return (* iface->get_path) (file);
451 * g_file_get_uri:
452 * @file: input #GFile.
454 * Gets the URI for the @file.
456 * This call does no blocking i/o.
458 * Returns: a string containing the #GFile's URI.
459 * The returned string should be freed with g_free() when no longer needed.
461 char *
462 g_file_get_uri (GFile *file)
464 GFileIface *iface;
466 g_return_val_if_fail (G_IS_FILE (file), NULL);
468 iface = G_FILE_GET_IFACE (file);
470 return (* iface->get_uri) (file);
474 * g_file_get_parse_name:
475 * @file: input #GFile.
477 * Gets the parse name of the @file.
478 * A parse name is a UTF-8 string that describes the
479 * file such that one can get the #GFile back using
480 * g_file_parse_name().
482 * This is generally used to show the #GFile as a nice
483 * full-pathname kind of string in a user interface,
484 * like in a location entry.
486 * For local files with names that can safely be converted
487 * to UTF8 the pathname is used, otherwise the IRI is used
488 * (a form of URI that allows UTF8 characters unescaped).
490 * This call does no blocking i/o.
492 * Returns: a string containing the #GFile's parse name. The returned
493 * string should be freed with g_free() when no longer needed.
495 char *
496 g_file_get_parse_name (GFile *file)
498 GFileIface *iface;
500 g_return_val_if_fail (G_IS_FILE (file), NULL);
502 iface = G_FILE_GET_IFACE (file);
504 return (* iface->get_parse_name) (file);
508 * g_file_dup:
509 * @file: input #GFile.
511 * Duplicates a #GFile handle. This operation does not duplicate
512 * the actual file or directory represented by the #GFile; see
513 * g_file_copy() if attempting to copy a file.
515 * This call does no blocking i/o.
517 * Returns: #GFile that is a duplicate of the given #GFile.
519 GFile *
520 g_file_dup (GFile *file)
522 GFileIface *iface;
524 g_return_val_if_fail (G_IS_FILE (file), NULL);
526 iface = G_FILE_GET_IFACE (file);
528 return (* iface->dup) (file);
532 * g_file_hash:
533 * @file: #gconstpointer to a #GFile.
535 * Creates a hash value for a #GFile.
537 * This call does no blocking i/o.
539 * Returns: 0 if @file is not a valid #GFile, otherwise an
540 * integer that can be used as hash value for the #GFile.
541 * This function is intended for easily hashing a #GFile to
542 * add to a #GHashTable or similar data structure.
544 guint
545 g_file_hash (gconstpointer file)
547 GFileIface *iface;
549 g_return_val_if_fail (G_IS_FILE (file), 0);
551 iface = G_FILE_GET_IFACE (file);
553 return (* iface->hash) ((GFile *)file);
557 * g_file_equal:
558 * @file1: the first #GFile.
559 * @file2: the second #GFile.
561 * Checks equality of two given #GFile<!-- -->s. Note that two
562 * #GFile<!-- -->s that differ can still refer to the same
563 * file on the filesystem due to various forms of filename
564 * aliasing.
566 * This call does no blocking i/o.
568 * Returns: %TRUE if @file1 and @file2 are equal.
569 * %FALSE if either is not a #GFile.
571 gboolean
572 g_file_equal (GFile *file1,
573 GFile *file2)
575 GFileIface *iface;
577 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
578 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
580 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
581 return FALSE;
583 iface = G_FILE_GET_IFACE (file1);
585 return (* iface->equal) (file1, file2);
590 * g_file_get_parent:
591 * @file: input #GFile.
593 * Gets the parent directory for the @file.
594 * If the @file represents the root directory of the
595 * file system, then %NULL will be returned.
597 * This call does no blocking i/o.
599 * Returns: a #GFile structure to the parent of the given
600 * #GFile or %NULL if there is no parent.
602 GFile *
603 g_file_get_parent (GFile *file)
605 GFileIface *iface;
607 g_return_val_if_fail (G_IS_FILE (file), NULL);
609 iface = G_FILE_GET_IFACE (file);
611 return (* iface->get_parent) (file);
615 * g_file_get_child:
616 * @file: input #GFile.
617 * @name: string containing the child's basename.
619 * Gets a child of @file with basename equal to @name.
621 * Note that the file with that specific name might not exist, but
622 * you can still have a #GFile that points to it. You can use this
623 * for instance to create that file.
625 * This call does no blocking i/o.
627 * Returns: a #GFile to a child specified by @name.
629 GFile *
630 g_file_get_child (GFile *file,
631 const char *name)
633 g_return_val_if_fail (G_IS_FILE (file), NULL);
634 g_return_val_if_fail (name != NULL, NULL);
636 return g_file_resolve_relative_path (file, name);
640 * g_file_get_child_for_display_name:
641 * @file: input #GFile.
642 * @display_name: string to a possible child.
643 * @error: #GError.
645 * Gets the child of @file for a given @display_name (i.e. a UTF8
646 * version of the name). If this function fails, it returns %NULL and @error will be
647 * set. This is very useful when constructing a GFile for a new file
648 * and the user entered the filename in the user interface, for instance
649 * when you select a directory and type a filename in the file selector.
651 * This call does no blocking i/o.
653 * Returns: a #GFile to the specified child, or
654 * %NULL if the display name couldn't be converted.
656 GFile *
657 g_file_get_child_for_display_name (GFile *file,
658 const char *display_name,
659 GError **error)
661 GFileIface *iface;
663 g_return_val_if_fail (G_IS_FILE (file), NULL);
664 g_return_val_if_fail (display_name != NULL, NULL);
666 iface = G_FILE_GET_IFACE (file);
668 return (* iface->get_child_for_display_name) (file, display_name, error);
672 * g_file_has_prefix:
673 * @file: input #GFile.
674 * @prefix: input #GFile.
676 * Checks whether @file has the prefix specified by @prefix. In other word, if the
677 * names of inital elements of @file<!-- -->s pathname match @prefix.
679 * This call does no i/o, as it works purely on names. As such it can sometimes
680 * return %FALSE even if @file is inside a @prefix (from a filesystem point of view),
681 * because the prefix of @file is an alias of @prefix.
683 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix. %FALSE otherwise.
685 gboolean
686 g_file_has_prefix (GFile *file,
687 GFile *prefix)
689 GFileIface *iface;
691 g_return_val_if_fail (G_IS_FILE (file), FALSE);
692 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
694 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
695 return FALSE;
697 iface = G_FILE_GET_IFACE (file);
699 /* The vtable function differs in arg order since we're
700 using the old contains_file call */
701 return (* iface->prefix_matches) (prefix, file);
705 * g_file_get_relative_path:
706 * @parent: input #GFile.
707 * @descendant: input #GFile.
709 * Gets the path for @descendant relative to @parent.
711 * This call does no blocking i/o.
713 * Returns: string with the relative path from @descendant
714 * to @parent, or %NULL if @descendant doesn't have @parent as prefix. The returned string should be freed with
715 * g_free() when no longer needed.
717 char *
718 g_file_get_relative_path (GFile *parent,
719 GFile *descendant)
721 GFileIface *iface;
723 g_return_val_if_fail (G_IS_FILE (parent), NULL);
724 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
726 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
727 return NULL;
729 iface = G_FILE_GET_IFACE (parent);
731 return (* iface->get_relative_path) (parent, descendant);
735 * g_file_resolve_relative_path:
736 * @file: input #GFile.
737 * @relative_path: a given relative path string.
739 * Resolves a relative path for @file to an absolute path.
741 * This call does no blocking i/o.
743 * Returns: #GFile to the resolved path. %NULL if @relative_path
744 * is %NULL or if @file is invalid.
746 GFile *
747 g_file_resolve_relative_path (GFile *file,
748 const char *relative_path)
750 GFileIface *iface;
752 g_return_val_if_fail (G_IS_FILE (file), NULL);
753 g_return_val_if_fail (relative_path != NULL, NULL);
755 iface = G_FILE_GET_IFACE (file);
757 return (* iface->resolve_relative_path) (file, relative_path);
761 * g_file_enumerate_children:
762 * @file: input #GFile.
763 * @attributes: an attribute query string.
764 * @flags: a set of #GFileQueryInfoFlags.
765 * @cancellable: optional #GCancellable object, %NULL to ignore.
766 * @error: #GError for error reporting.
768 * Gets the requested information about the files in a directory. The result
769 * is a #GFileEnumerator object that will give out #GFileInfo objects for
770 * all the files in the directory.
772 * The @attribute value is a string that specifies the file attributes that
773 * should be gathered. It is not an error if it's not possible to read a particular
774 * requested attribute from a file - it just won't be set. @attribute should
775 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
776 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
777 * namespace. An example attribute query be "standard::*,owner::user".
778 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
780 * If @cancellable is not %NULL, then the operation can be cancelled by
781 * triggering the cancellable object from another thread. If the operation
782 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
784 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
785 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
786 * Other errors are possible too.
788 * Returns: A #GFileEnumerator if successful, %NULL on error.
790 GFileEnumerator *
791 g_file_enumerate_children (GFile *file,
792 const char *attributes,
793 GFileQueryInfoFlags flags,
794 GCancellable *cancellable,
795 GError **error)
798 GFileIface *iface;
800 g_return_val_if_fail (G_IS_FILE (file), NULL);
802 if (g_cancellable_set_error_if_cancelled (cancellable, error))
803 return NULL;
805 iface = G_FILE_GET_IFACE (file);
807 if (iface->enumerate_children == NULL)
809 g_set_error (error, G_IO_ERROR,
810 G_IO_ERROR_NOT_SUPPORTED,
811 _("Operation not supported"));
812 return NULL;
815 return (* iface->enumerate_children) (file, attributes, flags,
816 cancellable, error);
820 * g_file_enumerate_children_async:
821 * @file: input #GFile.
822 * @attributes: an attribute query string.
823 * @flags: a set of #GFileQueryInfoFlags.
824 * @io_priority: the <link linkend="io-priority">I/O priority</link>
825 * of the request.
826 * @cancellable: optional #GCancellable object, %NULL to ignore.
827 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
828 * @user_data: the data to pass to callback function
830 * Asynchronously gets the requested information about the files in a directory. The result
831 * is a #GFileEnumerator object that will give out #GFileInfo objects for
832 * all the files in the directory.
834 * For more details, see g_file_enumerate_children() which is
835 * the synchronous version of this call.
837 * When the operation is finished, @callback will be called. You can then call
838 * g_file_enumerate_children_finish() to get the result of the operation.
840 void
841 g_file_enumerate_children_async (GFile *file,
842 const char *attributes,
843 GFileQueryInfoFlags flags,
844 int io_priority,
845 GCancellable *cancellable,
846 GAsyncReadyCallback callback,
847 gpointer user_data)
849 GFileIface *iface;
851 g_return_if_fail (G_IS_FILE (file));
853 iface = G_FILE_GET_IFACE (file);
854 (* iface->enumerate_children_async) (file,
855 attributes,
856 flags,
857 io_priority,
858 cancellable,
859 callback,
860 user_data);
864 * g_file_enumerate_children_finish:
865 * @file: input #GFile.
866 * @res: a #GAsyncResult.
867 * @error: a #GError.
869 * Finishes an async enumerate children operation.
870 * See g_file_enumerate_children_async().
872 * Returns: a #GFileEnumerator or %NULL if an error occurred.
874 GFileEnumerator *
875 g_file_enumerate_children_finish (GFile *file,
876 GAsyncResult *res,
877 GError **error)
879 GFileIface *iface;
881 g_return_val_if_fail (G_IS_FILE (file), NULL);
882 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
884 if (G_IS_SIMPLE_ASYNC_RESULT (res))
886 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
887 if (g_simple_async_result_propagate_error (simple, error))
888 return NULL;
891 iface = G_FILE_GET_IFACE (file);
892 return (* iface->enumerate_children_finish) (file, res, error);
896 * g_file_query_exists:
897 * @file: input #GFile.
898 * @cancellable: optional #GCancellable object, %NULL to ignore.
900 * Utility function to check if a particular file exists. This is
901 * implemented using g_file_query_info() and as such does blocking I/O.
903 * Note that in many cases it is racy to first check for file existence
904 * and then execute something based on the outcome of that, because the
905 * file might have been created or removed in between the operations. The
906 * general approach to handling that is to not check, but just do the
907 * operation and handle the errors as they come.
909 * As an example of race-free checking, take the case of reading a file, and
910 * if it doesn't exist, creating it. There are two racy versions: read it, and
911 * on error create it; and: check if it exists, if not create it. These
912 * can both result in two processes creating the file (with perhaps a partially
913 * written file as the result). The correct approach is to always try to create
914 * the file with g_file_create() which will either atomically create the file
915 * or fail with a G_IO_ERROR_EXISTS error.
917 * However, in many cases an existence check is useful in a user
918 * interface, for instance to make a menu item sensitive/insensitive, so that
919 * you don't have to fool users that something is possible and then just show
920 * and error dialog. If you do this, you should make sure to also handle the
921 * errors that can happen due to races when you execute the operation.
923 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
925 gboolean
926 g_file_query_exists (GFile *file,
927 GCancellable *cancellable)
929 GFileInfo *info;
931 g_return_val_if_fail (G_IS_FILE(file), FALSE);
933 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
934 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
935 if (info != NULL)
937 g_object_unref (info);
938 return TRUE;
941 return FALSE;
945 * g_file_query_file_type:
946 * @file: input #GFile.
947 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
948 * @cancellable: optional #GCancellable object, %NULL to ignore.
950 * Utility function to inspect the #GFileType of a file. This is
951 * implemented using g_file_query_info() and as such does blocking I/O.
953 * The primary use case of this method is to check if a file is a regular file,
954 * directory, or symlink.
956 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
957 * does not exist
959 * Since: 2.18
961 GFileType
962 g_file_query_file_type (GFile *file,
963 GFileQueryInfoFlags flags,
964 GCancellable *cancellable)
966 GFileInfo *info;
967 GFileType file_type;
969 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
970 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
971 cancellable, NULL);
972 if (info != NULL)
974 file_type = g_file_info_get_file_type (info);
975 g_object_unref (info);
977 else
978 file_type = G_FILE_TYPE_UNKNOWN;
980 return file_type;
984 * g_file_query_info:
985 * @file: input #GFile.
986 * @attributes: an attribute query string.
987 * @flags: a set of #GFileQueryInfoFlags.
988 * @cancellable: optional #GCancellable object, %NULL to ignore.
989 * @error: a #GError.
991 * Gets the requested information about specified @file. The result
992 * is a #GFileInfo object that contains key-value attributes (such as
993 * the type or size of the file).
995 * The @attribute value is a string that specifies the file attributes that
996 * should be gathered. It is not an error if it's not possible to read a particular
997 * requested attribute from a file - it just won't be set. @attribute should
998 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
999 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1000 * namespace. An example attribute query be "standard::*,owner::user".
1001 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1003 * If @cancellable is not %NULL, then the operation can be cancelled by
1004 * triggering the cancellable object from another thread. If the operation
1005 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1007 * For symlinks, normally the information about the target of the
1008 * symlink is returned, rather than information about the symlink itself.
1009 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1010 * information about the symlink itself will be returned. Also, for symlinks
1011 * that point to non-existing files the information about the symlink itself
1012 * will be returned.
1014 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1015 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1017 * Returns: a #GFileInfo for the given @file, or %NULL on error.
1019 GFileInfo *
1020 g_file_query_info (GFile *file,
1021 const char *attributes,
1022 GFileQueryInfoFlags flags,
1023 GCancellable *cancellable,
1024 GError **error)
1026 GFileIface *iface;
1028 g_return_val_if_fail (G_IS_FILE (file), NULL);
1030 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1031 return NULL;
1033 iface = G_FILE_GET_IFACE (file);
1035 if (iface->query_info == NULL)
1037 g_set_error (error, G_IO_ERROR,
1038 G_IO_ERROR_NOT_SUPPORTED,
1039 _("Operation not supported"));
1040 return NULL;
1043 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1047 * g_file_query_info_async:
1048 * @file: input #GFile.
1049 * @attributes: an attribute query string.
1050 * @flags: a set of #GFileQueryInfoFlags.
1051 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1052 * of the request.
1053 * @cancellable: optional #GCancellable object, %NULL to ignore.
1054 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1055 * @user_data: the data to pass to callback function
1057 * Asynchronously gets the requested information about specified @file. The result
1058 * is a #GFileInfo object that contains key-value attributes (such as type or size
1059 * for the file).
1061 * For more details, see g_file_query_info() which is
1062 * the synchronous version of this call.
1064 * When the operation is finished, @callback will be called. You can then call
1065 * g_file_query_info_finish() to get the result of the operation.
1067 void
1068 g_file_query_info_async (GFile *file,
1069 const char *attributes,
1070 GFileQueryInfoFlags flags,
1071 int io_priority,
1072 GCancellable *cancellable,
1073 GAsyncReadyCallback callback,
1074 gpointer user_data)
1076 GFileIface *iface;
1078 g_return_if_fail (G_IS_FILE (file));
1080 iface = G_FILE_GET_IFACE (file);
1081 (* iface->query_info_async) (file,
1082 attributes,
1083 flags,
1084 io_priority,
1085 cancellable,
1086 callback,
1087 user_data);
1091 * g_file_query_info_finish:
1092 * @file: input #GFile.
1093 * @res: a #GAsyncResult.
1094 * @error: a #GError.
1096 * Finishes an asynchronous file info query.
1097 * See g_file_query_info_async().
1099 * Returns: #GFileInfo for given @file or %NULL on error.
1101 GFileInfo *
1102 g_file_query_info_finish (GFile *file,
1103 GAsyncResult *res,
1104 GError **error)
1106 GFileIface *iface;
1108 g_return_val_if_fail (G_IS_FILE (file), NULL);
1109 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1111 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1113 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1114 if (g_simple_async_result_propagate_error (simple, error))
1115 return NULL;
1118 iface = G_FILE_GET_IFACE (file);
1119 return (* iface->query_info_finish) (file, res, error);
1123 * g_file_query_filesystem_info:
1124 * @file: input #GFile.
1125 * @attributes: an attribute query string.
1126 * @cancellable: optional #GCancellable object, %NULL to ignore.
1127 * @error: a #GError.
1129 * Similar to g_file_query_info(), but obtains information
1130 * about the filesystem the @file is on, rather than the file itself.
1131 * For instance the amount of space available and the type of
1132 * the filesystem.
1134 * The @attribute value is a string that specifies the file attributes that
1135 * should be gathered. It is not an error if it's not possible to read a particular
1136 * requested attribute from a file - it just won't be set. @attribute should
1137 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1138 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1139 * namespace. The standard namespace for filesystem attributes is "fs".
1140 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1141 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1142 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1144 * If @cancellable is not %NULL, then the operation can be cancelled by
1145 * triggering the cancellable object from another thread. If the operation
1146 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1148 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1149 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1151 * Returns: a #GFileInfo or %NULL if there was an error.
1153 GFileInfo *
1154 g_file_query_filesystem_info (GFile *file,
1155 const char *attributes,
1156 GCancellable *cancellable,
1157 GError **error)
1159 GFileIface *iface;
1161 g_return_val_if_fail (G_IS_FILE (file), NULL);
1163 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1164 return NULL;
1166 iface = G_FILE_GET_IFACE (file);
1168 if (iface->query_filesystem_info == NULL)
1170 g_set_error (error, G_IO_ERROR,
1171 G_IO_ERROR_NOT_SUPPORTED,
1172 _("Operation not supported"));
1173 return NULL;
1176 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1180 * g_file_query_filesystem_info_async:
1181 * @file: input #GFile.
1182 * @attributes: an attribute query string.
1183 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1184 * of the request.
1185 * @cancellable: optional #GCancellable object, %NULL to ignore.
1186 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1187 * @user_data: the data to pass to callback function
1189 * Asynchronously gets the requested information about the filesystem
1190 * that the specified @file is on. The result is a #GFileInfo object
1191 * that contains key-value attributes (such as type or size for the
1192 * file).
1194 * For more details, see g_file_query_filesystem_info() which is the
1195 * synchronous version of this call.
1197 * When the operation is finished, @callback will be called. You can
1198 * then call g_file_query_info_finish() to get the result of the
1199 * operation.
1201 void
1202 g_file_query_filesystem_info_async (GFile *file,
1203 const char *attributes,
1204 int io_priority,
1205 GCancellable *cancellable,
1206 GAsyncReadyCallback callback,
1207 gpointer user_data)
1209 GFileIface *iface;
1211 g_return_if_fail (G_IS_FILE (file));
1213 iface = G_FILE_GET_IFACE (file);
1214 (* iface->query_filesystem_info_async) (file,
1215 attributes,
1216 io_priority,
1217 cancellable,
1218 callback,
1219 user_data);
1223 * g_file_query_filesystem_info_finish:
1224 * @file: input #GFile.
1225 * @res: a #GAsyncResult.
1226 * @error: a #GError.
1228 * Finishes an asynchronous filesystem info query. See
1229 * g_file_query_filesystem_info_async().
1231 * Returns: #GFileInfo for given @file or %NULL on error.
1233 GFileInfo *
1234 g_file_query_filesystem_info_finish (GFile *file,
1235 GAsyncResult *res,
1236 GError **error)
1238 GFileIface *iface;
1240 g_return_val_if_fail (G_IS_FILE (file), NULL);
1241 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1243 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1245 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1246 if (g_simple_async_result_propagate_error (simple, error))
1247 return NULL;
1250 iface = G_FILE_GET_IFACE (file);
1251 return (* iface->query_filesystem_info_finish) (file, res, error);
1255 * g_file_find_enclosing_mount:
1256 * @file: input #GFile.
1257 * @cancellable: optional #GCancellable object, %NULL to ignore.
1258 * @error: a #GError.
1260 * Gets a #GMount for the #GFile.
1262 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1263 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1264 * will be returned.
1266 * If @cancellable is not %NULL, then the operation can be cancelled by
1267 * triggering the cancellable object from another thread. If the operation
1268 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1270 * Returns: a #GMount where the @file is located or %NULL on error.
1272 GMount *
1273 g_file_find_enclosing_mount (GFile *file,
1274 GCancellable *cancellable,
1275 GError **error)
1277 GFileIface *iface;
1279 g_return_val_if_fail (G_IS_FILE (file), NULL);
1281 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1282 return NULL;
1284 iface = G_FILE_GET_IFACE (file);
1285 if (iface->find_enclosing_mount == NULL)
1288 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1289 /* Translators: This is an error message when trying to find the
1290 * enclosing (user visible) mount of a file, but none exists. */
1291 _("Containing mount does not exist"));
1292 return NULL;
1295 return (* iface->find_enclosing_mount) (file, cancellable, error);
1298 * g_file_find_enclosing_mount_async:
1299 * @file: a #GFile
1300 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1301 * of the request.
1302 * @cancellable: optional #GCancellable object, %NULL to ignore.
1303 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1304 * @user_data: the data to pass to callback function
1306 * Asynchronously gets the mount for the file.
1308 * For more details, see g_file_find_enclosing_mount() which is
1309 * the synchronous version of this call.
1311 * When the operation is finished, @callback will be called. You can then call
1312 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1314 void
1315 g_file_find_enclosing_mount_async (GFile *file,
1316 int io_priority,
1317 GCancellable *cancellable,
1318 GAsyncReadyCallback callback,
1319 gpointer user_data)
1321 GFileIface *iface;
1323 g_return_if_fail (G_IS_FILE (file));
1325 iface = G_FILE_GET_IFACE (file);
1326 (* iface->find_enclosing_mount_async) (file,
1327 io_priority,
1328 cancellable,
1329 callback,
1330 user_data);
1334 * g_file_find_enclosing_mount_finish:
1335 * @file: a #GFile
1336 * @res: a #GAsyncResult
1337 * @error: a #GError
1339 * Finishes an asynchronous find mount request.
1340 * See g_file_find_enclosing_mount_async().
1342 * Returns: #GMount for given @file or %NULL on error.
1344 GMount *
1345 g_file_find_enclosing_mount_finish (GFile *file,
1346 GAsyncResult *res,
1347 GError **error)
1349 GFileIface *iface;
1351 g_return_val_if_fail (G_IS_FILE (file), NULL);
1352 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1354 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1356 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1357 if (g_simple_async_result_propagate_error (simple, error))
1358 return NULL;
1361 iface = G_FILE_GET_IFACE (file);
1362 return (* iface->find_enclosing_mount_finish) (file, res, error);
1367 * g_file_read:
1368 * @file: #GFile to read.
1369 * @cancellable: a #GCancellable
1370 * @error: a #GError, or %NULL
1372 * Opens a file for reading. The result is a #GFileInputStream that
1373 * can be used to read the contents of the file.
1375 * If @cancellable is not %NULL, then the operation can be cancelled by
1376 * triggering the cancellable object from another thread. If the operation
1377 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1379 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1380 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1381 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1383 * Returns: #GFileInputStream or %NULL on error.
1385 GFileInputStream *
1386 g_file_read (GFile *file,
1387 GCancellable *cancellable,
1388 GError **error)
1390 GFileIface *iface;
1392 g_return_val_if_fail (G_IS_FILE (file), NULL);
1394 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1395 return NULL;
1397 iface = G_FILE_GET_IFACE (file);
1399 if (iface->read_fn == NULL)
1401 g_set_error (error, G_IO_ERROR,
1402 G_IO_ERROR_NOT_SUPPORTED,
1403 _("Operation not supported"));
1404 return NULL;
1407 return (* iface->read_fn) (file, cancellable, error);
1411 * g_file_append_to:
1412 * @file: input #GFile.
1413 * @flags: a set of #GFileCreateFlags.
1414 * @cancellable: optional #GCancellable object, %NULL to ignore.
1415 * @error: a #GError, or %NULL
1417 * Gets an output stream for appending data to the file. If
1418 * the file doesn't already exist it is created.
1420 * By default files created are generally readable by everyone,
1421 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1422 * will be made readable only to the current user, to the level that
1423 * is supported on the target filesystem.
1425 * If @cancellable is not %NULL, then the operation can be cancelled by
1426 * triggering the cancellable object from another thread. If the operation
1427 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1429 * Some file systems don't allow all file names, and may
1430 * return an G_IO_ERROR_INVALID_FILENAME error.
1431 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1432 * returned. Other errors are possible too, and depend on what kind of
1433 * filesystem the file is on.
1435 * Returns: a #GFileOutputStream.
1437 GFileOutputStream *
1438 g_file_append_to (GFile *file,
1439 GFileCreateFlags flags,
1440 GCancellable *cancellable,
1441 GError **error)
1443 GFileIface *iface;
1445 g_return_val_if_fail (G_IS_FILE (file), NULL);
1447 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1448 return NULL;
1450 iface = G_FILE_GET_IFACE (file);
1452 if (iface->append_to == NULL)
1454 g_set_error (error, G_IO_ERROR,
1455 G_IO_ERROR_NOT_SUPPORTED,
1456 _("Operation not supported"));
1457 return NULL;
1460 return (* iface->append_to) (file, flags, cancellable, error);
1464 * g_file_create:
1465 * @file: input #GFile.
1466 * @flags: a set of #GFileCreateFlags.
1467 * @cancellable: optional #GCancellable object, %NULL to ignore.
1468 * @error: a #GError, or %NULL
1470 * Creates a new file and returns an output stream for writing to it.
1471 * The file must not already exist.
1473 * By default files created are generally readable by everyone,
1474 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1475 * will be made readable only to the current user, to the level that
1476 * is supported on the target filesystem.
1478 * If @cancellable is not %NULL, then the operation can be cancelled by
1479 * triggering the cancellable object from another thread. If the operation
1480 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1482 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1483 * error will be returned.
1484 * Some file systems don't allow all file names, and may
1485 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1486 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1487 * Other errors are possible too, and depend on what kind of
1488 * filesystem the file is on.
1490 * Returns: a #GFileOutputStream for the newly created file, or
1491 * %NULL on error.
1493 GFileOutputStream *
1494 g_file_create (GFile *file,
1495 GFileCreateFlags flags,
1496 GCancellable *cancellable,
1497 GError **error)
1499 GFileIface *iface;
1501 g_return_val_if_fail (G_IS_FILE (file), NULL);
1503 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1504 return NULL;
1506 iface = G_FILE_GET_IFACE (file);
1508 if (iface->create == NULL)
1510 g_set_error (error, G_IO_ERROR,
1511 G_IO_ERROR_NOT_SUPPORTED,
1512 _("Operation not supported"));
1513 return NULL;
1516 return (* iface->create) (file, flags, cancellable, error);
1520 * g_file_replace:
1521 * @file: input #GFile.
1522 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1523 * current #GFile, or #NULL to ignore.
1524 * @make_backup: %TRUE if a backup should be created.
1525 * @flags: a set of #GFileCreateFlags.
1526 * @cancellable: optional #GCancellable object, %NULL to ignore.
1527 * @error: a #GError, or %NULL
1529 * Returns an output stream for overwriting the file, possibly
1530 * creating a backup copy of the file first. If the file doesn't exist,
1531 * it will be created.
1533 * This will try to replace the file in the safest way possible so
1534 * that any errors during the writing will not affect an already
1535 * existing copy of the file. For instance, for local files it
1536 * may write to a temporary file and then atomically rename over
1537 * the destination when the stream is closed.
1539 * By default files created are generally readable by everyone,
1540 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1541 * will be made readable only to the current user, to the level that
1542 * is supported on the target filesystem.
1544 * If @cancellable is not %NULL, then the operation can be cancelled by
1545 * triggering the cancellable object from another thread. If the operation
1546 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1548 * If you pass in a non-#NULL @etag value, then this value is
1549 * compared to the current entity tag of the file, and if they differ
1550 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1551 * that the file has been changed since you last read it. You can get
1552 * the new etag from g_file_output_stream_get_etag() after you've
1553 * finished writing and closed the #GFileOutputStream. When you load
1554 * a new file you can use g_file_input_stream_query_info() to get
1555 * the etag of the file.
1557 * If @make_backup is %TRUE, this function will attempt to make a backup
1558 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1559 * error will be returned. If you want to replace anyway, try again with
1560 * @make_backup set to %FALSE.
1562 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1563 * and if the file is some other form of non-regular file then a
1564 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1565 * Some file systems don't allow all file names, and may
1566 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1567 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1568 * Other errors are possible too, and depend on what kind of
1569 * filesystem the file is on.
1571 * Returns: a #GFileOutputStream or %NULL on error.
1573 GFileOutputStream *
1574 g_file_replace (GFile *file,
1575 const char *etag,
1576 gboolean make_backup,
1577 GFileCreateFlags flags,
1578 GCancellable *cancellable,
1579 GError **error)
1581 GFileIface *iface;
1583 g_return_val_if_fail (G_IS_FILE (file), NULL);
1585 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1586 return NULL;
1588 iface = G_FILE_GET_IFACE (file);
1590 if (iface->replace == NULL)
1592 g_set_error (error, G_IO_ERROR,
1593 G_IO_ERROR_NOT_SUPPORTED,
1594 _("Operation not supported"));
1595 return NULL;
1599 /* Handle empty tag string as NULL in consistent way. */
1600 if (etag && *etag == 0)
1601 etag = NULL;
1603 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1607 * g_file_read_async:
1608 * @file: input #GFile.
1609 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1610 * of the request.
1611 * @cancellable: optional #GCancellable object, %NULL to ignore.
1612 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1613 * @user_data: the data to pass to callback function
1615 * Asynchronously opens @file for reading.
1617 * For more details, see g_file_read() which is
1618 * the synchronous version of this call.
1620 * When the operation is finished, @callback will be called. You can then call
1621 * g_file_read_finish() to get the result of the operation.
1623 void
1624 g_file_read_async (GFile *file,
1625 int io_priority,
1626 GCancellable *cancellable,
1627 GAsyncReadyCallback callback,
1628 gpointer user_data)
1630 GFileIface *iface;
1632 g_return_if_fail (G_IS_FILE (file));
1634 iface = G_FILE_GET_IFACE (file);
1635 (* iface->read_async) (file,
1636 io_priority,
1637 cancellable,
1638 callback,
1639 user_data);
1643 * g_file_read_finish:
1644 * @file: input #GFile.
1645 * @res: a #GAsyncResult.
1646 * @error: a #GError, or %NULL
1648 * Finishes an asynchronous file read operation started with
1649 * g_file_read_async().
1651 * Returns: a #GFileInputStream or %NULL on error.
1653 GFileInputStream *
1654 g_file_read_finish (GFile *file,
1655 GAsyncResult *res,
1656 GError **error)
1658 GFileIface *iface;
1660 g_return_val_if_fail (G_IS_FILE (file), NULL);
1661 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1663 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1665 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1666 if (g_simple_async_result_propagate_error (simple, error))
1667 return NULL;
1670 iface = G_FILE_GET_IFACE (file);
1671 return (* iface->read_finish) (file, res, error);
1675 * g_file_append_to_async:
1676 * @file: input #GFile.
1677 * @flags: a set of #GFileCreateFlags.
1678 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1679 * of the request.
1680 * @cancellable: optional #GCancellable object, %NULL to ignore.
1681 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1682 * @user_data: the data to pass to callback function
1684 * Asynchronously opens @file for appending.
1686 * For more details, see g_file_append_to() which is
1687 * the synchronous version of this call.
1689 * When the operation is finished, @callback will be called. You can then call
1690 * g_file_append_to_finish() to get the result of the operation.
1692 void
1693 g_file_append_to_async (GFile *file,
1694 GFileCreateFlags flags,
1695 int io_priority,
1696 GCancellable *cancellable,
1697 GAsyncReadyCallback callback,
1698 gpointer user_data)
1700 GFileIface *iface;
1702 g_return_if_fail (G_IS_FILE (file));
1704 iface = G_FILE_GET_IFACE (file);
1705 (* iface->append_to_async) (file,
1706 flags,
1707 io_priority,
1708 cancellable,
1709 callback,
1710 user_data);
1714 * g_file_append_to_finish:
1715 * @file: input #GFile.
1716 * @res: #GAsyncResult
1717 * @error: a #GError, or %NULL
1719 * Finishes an asynchronous file append operation started with
1720 * g_file_append_to_async().
1722 * Returns: a valid #GFileOutputStream or %NULL on error.
1724 GFileOutputStream *
1725 g_file_append_to_finish (GFile *file,
1726 GAsyncResult *res,
1727 GError **error)
1729 GFileIface *iface;
1731 g_return_val_if_fail (G_IS_FILE (file), NULL);
1732 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1734 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1736 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1737 if (g_simple_async_result_propagate_error (simple, error))
1738 return NULL;
1741 iface = G_FILE_GET_IFACE (file);
1742 return (* iface->append_to_finish) (file, res, error);
1746 * g_file_create_async:
1747 * @file: input #GFile.
1748 * @flags: a set of #GFileCreateFlags.
1749 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1750 * of the request.
1751 * @cancellable: optional #GCancellable object, %NULL to ignore.
1752 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1753 * @user_data: the data to pass to callback function
1755 * Asynchronously creates a new file and returns an output stream for writing to it.
1756 * The file must not already exist.
1758 * For more details, see g_file_create() which is
1759 * the synchronous version of this call.
1761 * When the operation is finished, @callback will be called. You can then call
1762 * g_file_create_finish() to get the result of the operation.
1764 void
1765 g_file_create_async (GFile *file,
1766 GFileCreateFlags flags,
1767 int io_priority,
1768 GCancellable *cancellable,
1769 GAsyncReadyCallback callback,
1770 gpointer user_data)
1772 GFileIface *iface;
1774 g_return_if_fail (G_IS_FILE (file));
1776 iface = G_FILE_GET_IFACE (file);
1777 (* iface->create_async) (file,
1778 flags,
1779 io_priority,
1780 cancellable,
1781 callback,
1782 user_data);
1786 * g_file_create_finish:
1787 * @file: input #GFile.
1788 * @res: a #GAsyncResult.
1789 * @error: a #GError, or %NULL
1791 * Finishes an asynchronous file create operation started with
1792 * g_file_create_async().
1794 * Returns: a #GFileOutputStream or %NULL on error.
1796 GFileOutputStream *
1797 g_file_create_finish (GFile *file,
1798 GAsyncResult *res,
1799 GError **error)
1801 GFileIface *iface;
1803 g_return_val_if_fail (G_IS_FILE (file), NULL);
1804 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1806 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1808 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1809 if (g_simple_async_result_propagate_error (simple, error))
1810 return NULL;
1813 iface = G_FILE_GET_IFACE (file);
1814 return (* iface->create_finish) (file, res, error);
1818 * g_file_replace_async:
1819 * @file: input #GFile.
1820 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1821 * current #GFile, or NULL to ignore.
1822 * @make_backup: %TRUE if a backup should be created.
1823 * @flags: a set of #GFileCreateFlags.
1824 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1825 * of the request.
1826 * @cancellable: optional #GCancellable object, %NULL to ignore.
1827 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1828 * @user_data: the data to pass to callback function
1830 * Asynchronously overwrites the file, replacing the contents, possibly
1831 * creating a backup copy of the file first.
1833 * For more details, see g_file_replace() which is
1834 * the synchronous version of this call.
1836 * When the operation is finished, @callback will be called. You can then call
1837 * g_file_replace_finish() to get the result of the operation.
1839 void
1840 g_file_replace_async (GFile *file,
1841 const char *etag,
1842 gboolean make_backup,
1843 GFileCreateFlags flags,
1844 int io_priority,
1845 GCancellable *cancellable,
1846 GAsyncReadyCallback callback,
1847 gpointer user_data)
1849 GFileIface *iface;
1851 g_return_if_fail (G_IS_FILE (file));
1853 iface = G_FILE_GET_IFACE (file);
1854 (* iface->replace_async) (file,
1855 etag,
1856 make_backup,
1857 flags,
1858 io_priority,
1859 cancellable,
1860 callback,
1861 user_data);
1865 * g_file_replace_finish:
1866 * @file: input #GFile.
1867 * @res: a #GAsyncResult.
1868 * @error: a #GError, or %NULL
1870 * Finishes an asynchronous file replace operation started with
1871 * g_file_replace_async().
1873 * Returns: a #GFileOutputStream, or %NULL on error.
1875 GFileOutputStream *
1876 g_file_replace_finish (GFile *file,
1877 GAsyncResult *res,
1878 GError **error)
1880 GFileIface *iface;
1882 g_return_val_if_fail (G_IS_FILE (file), NULL);
1883 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1885 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1887 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1888 if (g_simple_async_result_propagate_error (simple, error))
1889 return NULL;
1892 iface = G_FILE_GET_IFACE (file);
1893 return (* iface->replace_finish) (file, res, error);
1896 static gboolean
1897 copy_symlink (GFile *destination,
1898 GFileCopyFlags flags,
1899 GCancellable *cancellable,
1900 const char *target,
1901 GError **error)
1903 GError *my_error;
1904 gboolean tried_delete;
1905 GFileInfo *info;
1906 GFileType file_type;
1908 tried_delete = FALSE;
1910 retry:
1911 my_error = NULL;
1912 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1914 /* Maybe it already existed, and we want to overwrite? */
1915 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1916 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1918 g_error_free (my_error);
1921 /* Don't overwrite if the destination is a directory */
1922 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1923 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1924 cancellable, &my_error);
1925 if (info != NULL)
1927 file_type = g_file_info_get_file_type (info);
1928 g_object_unref (info);
1930 if (file_type == G_FILE_TYPE_DIRECTORY)
1932 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1933 _("Can't copy over directory"));
1934 return FALSE;
1938 if (!g_file_delete (destination, cancellable, error))
1939 return FALSE;
1941 tried_delete = TRUE;
1942 goto retry;
1944 /* Nah, fail */
1945 g_propagate_error (error, my_error);
1946 return FALSE;
1949 return TRUE;
1952 static GInputStream *
1953 open_source_for_copy (GFile *source,
1954 GFile *destination,
1955 GFileCopyFlags flags,
1956 GCancellable *cancellable,
1957 GError **error)
1959 GError *my_error;
1960 GInputStream *in;
1961 GFileInfo *info;
1962 GFileType file_type;
1964 my_error = NULL;
1965 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1966 if (in != NULL)
1967 return in;
1969 /* There was an error opening the source, try to set a good error for it: */
1971 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1973 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1974 * as that is less useful to the app. Better check for errors on the
1975 * target instead.
1977 g_error_free (my_error);
1978 my_error = NULL;
1980 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1981 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1982 cancellable, &my_error);
1983 if (info != NULL)
1985 file_type = g_file_info_get_file_type (info);
1986 g_object_unref (info);
1988 if (flags & G_FILE_COPY_OVERWRITE)
1990 if (file_type == G_FILE_TYPE_DIRECTORY)
1992 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1993 _("Can't copy directory over directory"));
1994 return NULL;
1996 /* continue to would_recurse error */
1998 else
2000 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2001 _("Target file exists"));
2002 return NULL;
2005 else
2007 /* Error getting info from target, return that error
2008 * (except for NOT_FOUND, which is no error here)
2010 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
2012 g_propagate_error (error, my_error);
2013 return NULL;
2015 g_error_free (my_error);
2018 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2019 _("Can't recursively copy directory"));
2020 return NULL;
2023 g_propagate_error (error, my_error);
2024 return NULL;
2027 static gboolean
2028 should_copy (GFileAttributeInfo *info,
2029 gboolean as_move)
2031 if (as_move)
2032 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2033 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2036 static char *
2037 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2038 GFileAttributeInfoList *namespaces,
2039 gboolean as_move)
2041 GString *s;
2042 gboolean first;
2043 int i;
2045 first = TRUE;
2046 s = g_string_new ("");
2048 if (attributes)
2050 for (i = 0; i < attributes->n_infos; i++)
2052 if (should_copy (&attributes->infos[i], as_move))
2054 if (first)
2055 first = FALSE;
2056 else
2057 g_string_append_c (s, ',');
2059 g_string_append (s, attributes->infos[i].name);
2064 if (namespaces)
2066 for (i = 0; i < namespaces->n_infos; i++)
2068 if (should_copy (&namespaces->infos[i], as_move))
2070 if (first)
2071 first = FALSE;
2072 else
2073 g_string_append_c (s, ',');
2075 g_string_append (s, namespaces->infos[i].name);
2076 g_string_append (s, ":*");
2081 return g_string_free (s, FALSE);
2085 * g_file_copy_attributes:
2086 * @source: a #GFile with attributes.
2087 * @destination: a #GFile to copy attributes to.
2088 * @flags: a set of #GFileCopyFlags.
2089 * @cancellable: optional #GCancellable object, %NULL to ignore.
2090 * @error: a #GError, %NULL to ignore.
2092 * Copies the file attributes from @source to @destination.
2094 * Normally only a subset of the file attributes are copied,
2095 * those that are copies in a normal file copy operation
2096 * (which for instance does not include e.g. mtime). However
2097 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2098 * all the metadata that is possible to copy is copied.
2100 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2102 gboolean
2103 g_file_copy_attributes (GFile *source,
2104 GFile *destination,
2105 GFileCopyFlags flags,
2106 GCancellable *cancellable,
2107 GError **error)
2109 GFileAttributeInfoList *attributes, *namespaces;
2110 char *attrs_to_read;
2111 gboolean res;
2112 GFileInfo *info;
2113 gboolean as_move;
2114 gboolean source_nofollow_symlinks;
2116 as_move = flags & G_FILE_COPY_ALL_METADATA;
2117 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2119 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2120 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2121 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2123 if (attributes == NULL && namespaces == NULL)
2124 return TRUE;
2126 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2128 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2129 * we just don't copy it.
2131 info = g_file_query_info (source, attrs_to_read,
2132 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2133 cancellable,
2134 NULL);
2136 g_free (attrs_to_read);
2138 res = TRUE;
2139 if (info)
2141 res = g_file_set_attributes_from_info (destination,
2142 info, 0,
2143 cancellable,
2144 error);
2145 g_object_unref (info);
2148 g_file_attribute_info_list_unref (attributes);
2149 g_file_attribute_info_list_unref (namespaces);
2151 return res;
2154 /* Closes the streams */
2155 static gboolean
2156 copy_stream_with_progress (GInputStream *in,
2157 GOutputStream *out,
2158 GFile *source,
2159 GCancellable *cancellable,
2160 GFileProgressCallback progress_callback,
2161 gpointer progress_callback_data,
2162 GError **error)
2164 gssize n_read, n_written;
2165 goffset current_size;
2166 char buffer[1024*64], *p;
2167 gboolean res;
2168 goffset total_size;
2169 GFileInfo *info;
2171 total_size = -1;
2172 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2173 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2174 cancellable, NULL);
2175 if (info)
2177 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2178 total_size = g_file_info_get_size (info);
2179 g_object_unref (info);
2182 if (total_size == -1)
2184 info = g_file_query_info (source,
2185 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2186 G_FILE_QUERY_INFO_NONE,
2187 cancellable, NULL);
2188 if (info)
2190 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2191 total_size = g_file_info_get_size (info);
2192 g_object_unref (info);
2196 if (total_size == -1)
2197 total_size = 0;
2199 current_size = 0;
2200 res = TRUE;
2201 while (TRUE)
2203 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2204 if (n_read == -1)
2206 res = FALSE;
2207 break;
2210 if (n_read == 0)
2211 break;
2213 current_size += n_read;
2215 p = buffer;
2216 while (n_read > 0)
2218 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2219 if (n_written == -1)
2221 res = FALSE;
2222 break;
2225 p += n_written;
2226 n_read -= n_written;
2229 if (!res)
2230 break;
2232 if (progress_callback)
2233 progress_callback (current_size, total_size, progress_callback_data);
2236 if (!res)
2237 error = NULL; /* Ignore further errors */
2239 /* Make sure we send full copied size */
2240 if (progress_callback)
2241 progress_callback (current_size, total_size, progress_callback_data);
2243 /* Don't care about errors in source here */
2244 g_input_stream_close (in, cancellable, NULL);
2246 /* But write errors on close are bad! */
2247 if (!g_output_stream_close (out, cancellable, error))
2248 res = FALSE;
2250 g_object_unref (in);
2251 g_object_unref (out);
2253 return res;
2256 static gboolean
2257 file_copy_fallback (GFile *source,
2258 GFile *destination,
2259 GFileCopyFlags flags,
2260 GCancellable *cancellable,
2261 GFileProgressCallback progress_callback,
2262 gpointer progress_callback_data,
2263 GError **error)
2265 GInputStream *in;
2266 GOutputStream *out;
2267 GFileInfo *info;
2268 const char *target;
2270 /* Maybe copy the symlink? */
2271 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2273 info = g_file_query_info (source,
2274 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2275 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2276 cancellable,
2277 error);
2278 if (info == NULL)
2279 return FALSE;
2281 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2282 (target = g_file_info_get_symlink_target (info)) != NULL)
2284 if (!copy_symlink (destination, flags, cancellable, target, error))
2286 g_object_unref (info);
2287 return FALSE;
2290 g_object_unref (info);
2291 goto copied_file;
2294 g_object_unref (info);
2297 in = open_source_for_copy (source, destination, flags, cancellable, error);
2298 if (in == NULL)
2299 return FALSE;
2301 if (flags & G_FILE_COPY_OVERWRITE)
2303 out = (GOutputStream *)g_file_replace (destination,
2304 NULL,
2305 flags & G_FILE_COPY_BACKUP,
2307 cancellable, error);
2309 else
2311 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2314 if (out == NULL)
2316 g_object_unref (in);
2317 return FALSE;
2320 if (!copy_stream_with_progress (in, out, source, cancellable,
2321 progress_callback, progress_callback_data,
2322 error))
2323 return FALSE;
2325 copied_file:
2327 /* Ignore errors here. Failure to copy metadata is not a hard error */
2328 g_file_copy_attributes (source, destination,
2329 flags, cancellable, NULL);
2331 return TRUE;
2335 * g_file_copy:
2336 * @source: input #GFile.
2337 * @destination: destination #GFile
2338 * @flags: set of #GFileCopyFlags
2339 * @cancellable: optional #GCancellable object, %NULL to ignore.
2340 * @progress_callback: function to callback with progress information
2341 * @progress_callback_data: user data to pass to @progress_callback
2342 * @error: #GError to set on error, or %NULL
2344 * Copies the file @source to the location specified by @destination.
2345 * Can not handle recursive copies of directories.
2347 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2348 * existing @destination file is overwritten.
2350 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2351 * will be copied as symlinks, otherwise the target of the
2352 * @source symlink will be copied.
2354 * If @cancellable is not %NULL, then the operation can be cancelled by
2355 * triggering the cancellable object from another thread. If the operation
2356 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2358 * If @progress_callback is not %NULL, then the operation can be monitored by
2359 * setting this to a #GFileProgressCallback function. @progress_callback_data
2360 * will be passed to this function. It is guaranteed that this callback will
2361 * be called after all data has been transferred with the total number of bytes
2362 * copied during the operation.
2364 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2365 * error is returned, independent on the status of the @destination.
2367 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2368 * error G_IO_ERROR_EXISTS is returned.
2370 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2371 * error is returned. If trying to overwrite a directory with a directory the
2372 * G_IO_ERROR_WOULD_MERGE error is returned.
2374 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2375 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2376 * is returned.
2378 * If you are interested in copying the #GFile object itself (not the on-disk
2379 * file), see g_file_dup().
2381 * Returns: %TRUE on success, %FALSE otherwise.
2383 gboolean
2384 g_file_copy (GFile *source,
2385 GFile *destination,
2386 GFileCopyFlags flags,
2387 GCancellable *cancellable,
2388 GFileProgressCallback progress_callback,
2389 gpointer progress_callback_data,
2390 GError **error)
2392 GFileIface *iface;
2393 GError *my_error;
2394 gboolean res;
2396 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2397 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2399 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2400 return FALSE;
2402 iface = G_FILE_GET_IFACE (destination);
2403 if (iface->copy)
2405 my_error = NULL;
2406 res = (* iface->copy) (source, destination,
2407 flags, cancellable,
2408 progress_callback, progress_callback_data,
2409 &my_error);
2411 if (res)
2412 return TRUE;
2414 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2416 g_propagate_error (error, my_error);
2417 return FALSE;
2421 /* If the types are different, and the destination method failed
2422 also try the source method */
2423 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2425 iface = G_FILE_GET_IFACE (source);
2427 if (iface->copy)
2429 my_error = NULL;
2430 res = (* iface->copy) (source, destination,
2431 flags, cancellable,
2432 progress_callback, progress_callback_data,
2433 &my_error);
2435 if (res)
2436 return TRUE;
2438 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2440 g_propagate_error (error, my_error);
2441 return FALSE;
2446 return file_copy_fallback (source, destination, flags, cancellable,
2447 progress_callback, progress_callback_data,
2448 error);
2452 * g_file_copy_async:
2453 * @source: input #GFile.
2454 * @destination: destination #GFile
2455 * @flags: set of #GFileCopyFlags
2456 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2457 * of the request.
2458 * @cancellable: optional #GCancellable object, %NULL to ignore.
2459 * @progress_callback: function to callback with progress information
2460 * @progress_callback_data: user data to pass to @progress_callback
2461 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2462 * @user_data: the data to pass to callback function
2464 * Copies the file @source to the location specified by @destination
2465 * asynchronously. For details of the behaviour, see g_file_copy().
2467 * If @progress_callback is not %NULL, then that function that will be called
2468 * just like in g_file_copy(), however the callback will run in the main loop,
2469 * not in the thread that is doing the I/O operation.
2471 * When the operation is finished, @callback will be called. You can then call
2472 * g_file_copy_finish() to get the result of the operation.
2474 void
2475 g_file_copy_async (GFile *source,
2476 GFile *destination,
2477 GFileCopyFlags flags,
2478 int io_priority,
2479 GCancellable *cancellable,
2480 GFileProgressCallback progress_callback,
2481 gpointer progress_callback_data,
2482 GAsyncReadyCallback callback,
2483 gpointer user_data)
2485 GFileIface *iface;
2487 g_return_if_fail (G_IS_FILE (source));
2488 g_return_if_fail (G_IS_FILE (destination));
2490 iface = G_FILE_GET_IFACE (source);
2491 (* iface->copy_async) (source,
2492 destination,
2493 flags,
2494 io_priority,
2495 cancellable,
2496 progress_callback,
2497 progress_callback_data,
2498 callback,
2499 user_data);
2503 * g_file_copy_finish:
2504 * @file: input #GFile.
2505 * @res: a #GAsyncResult.
2506 * @error: a #GError, or %NULL
2508 * Finishes copying the file started with
2509 * g_file_copy_async().
2511 * Returns: a %TRUE on success, %FALSE on error.
2513 gboolean
2514 g_file_copy_finish (GFile *file,
2515 GAsyncResult *res,
2516 GError **error)
2518 GFileIface *iface;
2520 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2521 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2523 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2525 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2527 if (g_simple_async_result_propagate_error (simple, error))
2528 return FALSE;
2531 iface = G_FILE_GET_IFACE (file);
2532 return (* iface->copy_finish) (file, res, error);
2536 * g_file_move:
2537 * @source: #GFile pointing to the source location.
2538 * @destination: #GFile pointing to the destination location.
2539 * @flags: set of #GFileCopyFlags.
2540 * @cancellable: optional #GCancellable object, %NULL to ignore.
2541 * @progress_callback: #GFileProgressCallback function for updates.
2542 * @progress_callback_data: gpointer to user data for the callback function.
2543 * @error: #GError for returning error conditions, or %NULL
2546 * Tries to move the file or directory @source to the location specified by @destination.
2547 * If native move operations are supported then this is used, otherwise a copy + delete
2548 * fallback is used. The native implementation may support moving directories (for instance
2549 * on moves inside the same filesystem), but the fallback code does not.
2551 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2552 * existing @destination file is overwritten.
2554 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2555 * will be copied as symlinks, otherwise the target of the
2556 * @source symlink will be copied.
2558 * If @cancellable is not %NULL, then the operation can be cancelled by
2559 * triggering the cancellable object from another thread. If the operation
2560 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2562 * If @progress_callback is not %NULL, then the operation can be monitored by
2563 * setting this to a #GFileProgressCallback function. @progress_callback_data
2564 * will be passed to this function. It is guaranteed that this callback will
2565 * be called after all data has been transferred with the total number of bytes
2566 * copied during the operation.
2568 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2569 * error is returned, independent on the status of the @destination.
2571 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2572 * error G_IO_ERROR_EXISTS is returned.
2574 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2575 * error is returned. If trying to overwrite a directory with a directory the
2576 * G_IO_ERROR_WOULD_MERGE error is returned.
2578 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2579 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2580 * may be returned (if the native move operation isn't available).
2582 * Returns: %TRUE on successful move, %FALSE otherwise.
2584 gboolean
2585 g_file_move (GFile *source,
2586 GFile *destination,
2587 GFileCopyFlags flags,
2588 GCancellable *cancellable,
2589 GFileProgressCallback progress_callback,
2590 gpointer progress_callback_data,
2591 GError **error)
2593 GFileIface *iface;
2594 GError *my_error;
2595 gboolean res;
2597 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2598 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2600 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2601 return FALSE;
2603 iface = G_FILE_GET_IFACE (destination);
2604 if (iface->move)
2606 my_error = NULL;
2607 res = (* iface->move) (source, destination,
2608 flags, cancellable,
2609 progress_callback, progress_callback_data,
2610 &my_error);
2612 if (res)
2613 return TRUE;
2615 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2617 g_propagate_error (error, my_error);
2618 return FALSE;
2622 /* If the types are different, and the destination method failed
2623 also try the source method */
2624 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2626 iface = G_FILE_GET_IFACE (source);
2628 if (iface->move)
2630 my_error = NULL;
2631 res = (* iface->move) (source, destination,
2632 flags, cancellable,
2633 progress_callback, progress_callback_data,
2634 &my_error);
2636 if (res)
2637 return TRUE;
2639 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2641 g_propagate_error (error, my_error);
2642 return FALSE;
2647 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2649 g_set_error (error, G_IO_ERROR,
2650 G_IO_ERROR_NOT_SUPPORTED,
2651 _("Operation not supported"));
2652 return FALSE;
2655 flags |= G_FILE_COPY_ALL_METADATA;
2656 if (!g_file_copy (source, destination, flags, cancellable,
2657 progress_callback, progress_callback_data,
2658 error))
2659 return FALSE;
2661 return g_file_delete (source, cancellable, error);
2665 * g_file_make_directory
2666 * @file: input #GFile.
2667 * @cancellable: optional #GCancellable object, %NULL to ignore.
2668 * @error: a #GError, or %NULL
2670 * Creates a directory.
2672 * If @cancellable is not %NULL, then the operation can be cancelled by
2673 * triggering the cancellable object from another thread. If the operation
2674 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2676 * Returns: %TRUE on successful creation, %FALSE otherwise.
2678 gboolean
2679 g_file_make_directory (GFile *file,
2680 GCancellable *cancellable,
2681 GError **error)
2683 GFileIface *iface;
2685 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2687 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2688 return FALSE;
2690 iface = G_FILE_GET_IFACE (file);
2692 if (iface->make_directory == NULL)
2694 g_set_error (error, G_IO_ERROR,
2695 G_IO_ERROR_NOT_SUPPORTED,
2696 _("Operation not supported"));
2697 return FALSE;
2700 return (* iface->make_directory) (file, cancellable, error);
2704 * g_file_make_symbolic_link:
2705 * @file: input #GFile.
2706 * @symlink_value: a string with the value of the new symlink.
2707 * @cancellable: optional #GCancellable object, %NULL to ignore.
2708 * @error: a #GError.
2710 * Creates a symbolic link.
2712 * If @cancellable is not %NULL, then the operation can be cancelled by
2713 * triggering the cancellable object from another thread. If the operation
2714 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2716 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2718 gboolean
2719 g_file_make_symbolic_link (GFile *file,
2720 const char *symlink_value,
2721 GCancellable *cancellable,
2722 GError **error)
2724 GFileIface *iface;
2726 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2727 g_return_val_if_fail (symlink_value != NULL, FALSE);
2729 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2730 return FALSE;
2732 if (*symlink_value == '\0')
2734 g_set_error (error, G_IO_ERROR,
2735 G_IO_ERROR_INVALID_ARGUMENT,
2736 _("Invalid symlink value given"));
2737 return FALSE;
2740 iface = G_FILE_GET_IFACE (file);
2742 if (iface->make_symbolic_link == NULL)
2744 g_set_error (error, G_IO_ERROR,
2745 G_IO_ERROR_NOT_SUPPORTED,
2746 _("Operation not supported"));
2747 return FALSE;
2750 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2754 * g_file_delete:
2755 * @file: input #GFile.
2756 * @cancellable: optional #GCancellable object, %NULL to ignore.
2757 * @error: a #GError, or %NULL
2759 * Deletes a file. If the @file is a directory, it will only be deleted if it
2760 * is empty.
2762 * If @cancellable is not %NULL, then the operation can be cancelled by
2763 * triggering the cancellable object from another thread. If the operation
2764 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2766 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2768 gboolean
2769 g_file_delete (GFile *file,
2770 GCancellable *cancellable,
2771 GError **error)
2773 GFileIface *iface;
2775 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2777 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2778 return FALSE;
2780 iface = G_FILE_GET_IFACE (file);
2782 if (iface->delete_file == NULL)
2784 g_set_error (error, G_IO_ERROR,
2785 G_IO_ERROR_NOT_SUPPORTED,
2786 _("Operation not supported"));
2787 return FALSE;
2790 return (* iface->delete_file) (file, cancellable, error);
2794 * g_file_trash:
2795 * @file: #GFile to send to trash.
2796 * @cancellable: optional #GCancellable object, %NULL to ignore.
2797 * @error: a #GError, or %NULL
2799 * Sends @file to the "Trashcan", if possible. This is similar to
2800 * deleting it, but the user can recover it before emptying the trashcan.
2801 * Not all file systems support trashing, so this call can return the
2802 * %G_IO_ERROR_NOT_SUPPORTED error.
2805 * If @cancellable is not %NULL, then the operation can be cancelled by
2806 * triggering the cancellable object from another thread. If the operation
2807 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2809 * Returns: %TRUE on successful trash, %FALSE otherwise.
2811 gboolean
2812 g_file_trash (GFile *file,
2813 GCancellable *cancellable,
2814 GError **error)
2816 GFileIface *iface;
2818 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2820 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2821 return FALSE;
2823 iface = G_FILE_GET_IFACE (file);
2825 if (iface->trash == NULL)
2827 g_set_error (error,
2828 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2829 _("Trash not supported"));
2830 return FALSE;
2833 return (* iface->trash) (file, cancellable, error);
2837 * g_file_set_display_name:
2838 * @file: input #GFile.
2839 * @display_name: a string.
2840 * @cancellable: optional #GCancellable object, %NULL to ignore.
2841 * @error: a #GError, or %NULL
2843 * Renames @file to the specified display name.
2845 * The display name is converted from UTF8 to the correct encoding for the target
2846 * filesystem if possible and the @file is renamed to this.
2848 * If you want to implement a rename operation in the user interface the edit name
2849 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2850 * widget, and then the result after editing should be passed to g_file_set_display_name().
2852 * On success the resulting converted filename is returned.
2854 * If @cancellable is not %NULL, then the operation can be cancelled by
2855 * triggering the cancellable object from another thread. If the operation
2856 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2858 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2860 GFile *
2861 g_file_set_display_name (GFile *file,
2862 const char *display_name,
2863 GCancellable *cancellable,
2864 GError **error)
2866 GFileIface *iface;
2868 g_return_val_if_fail (G_IS_FILE (file), NULL);
2869 g_return_val_if_fail (display_name != NULL, NULL);
2871 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2873 g_set_error (error,
2874 G_IO_ERROR,
2875 G_IO_ERROR_INVALID_ARGUMENT,
2876 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2877 return NULL;
2880 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2881 return NULL;
2883 iface = G_FILE_GET_IFACE (file);
2885 return (* iface->set_display_name) (file, display_name, cancellable, error);
2889 * g_file_set_display_name_async:
2890 * @file: input #GFile.
2891 * @display_name: a string.
2892 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2893 * of the request.
2894 * @cancellable: optional #GCancellable object, %NULL to ignore.
2895 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2896 * @user_data: the data to pass to callback function
2898 * Asynchronously sets the display name for a given #GFile.
2900 * For more details, see g_set_display_name() which is
2901 * the synchronous version of this call.
2903 * When the operation is finished, @callback will be called. You can then call
2904 * g_file_set_display_name_finish() to get the result of the operation.
2906 void
2907 g_file_set_display_name_async (GFile *file,
2908 const char *display_name,
2909 int io_priority,
2910 GCancellable *cancellable,
2911 GAsyncReadyCallback callback,
2912 gpointer user_data)
2914 GFileIface *iface;
2916 g_return_if_fail (G_IS_FILE (file));
2917 g_return_if_fail (display_name != NULL);
2919 iface = G_FILE_GET_IFACE (file);
2920 (* iface->set_display_name_async) (file,
2921 display_name,
2922 io_priority,
2923 cancellable,
2924 callback,
2925 user_data);
2929 * g_file_set_display_name_finish:
2930 * @file: input #GFile.
2931 * @res: a #GAsyncResult.
2932 * @error: a #GError, or %NULL
2934 * Finishes setting a display name started with
2935 * g_file_set_display_name_async().
2937 * Returns: a #GFile or %NULL on error.
2939 GFile *
2940 g_file_set_display_name_finish (GFile *file,
2941 GAsyncResult *res,
2942 GError **error)
2944 GFileIface *iface;
2946 g_return_val_if_fail (G_IS_FILE (file), NULL);
2947 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2949 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2951 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2952 if (g_simple_async_result_propagate_error (simple, error))
2953 return NULL;
2956 iface = G_FILE_GET_IFACE (file);
2957 return (* iface->set_display_name_finish) (file, res, error);
2961 * g_file_query_settable_attributes:
2962 * @file: input #GFile.
2963 * @cancellable: optional #GCancellable object, %NULL to ignore.
2964 * @error: a #GError, or %NULL
2966 * Obtain the list of settable attributes for the file.
2968 * Returns the type and full attribute name of all the attributes
2969 * that can be set on this file. This doesn't mean setting it will always
2970 * succeed though, you might get an access failure, or some specific
2971 * file may not support a specific attribute.
2973 * If @cancellable is not %NULL, then the operation can be cancelled by
2974 * triggering the cancellable object from another thread. If the operation
2975 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2977 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2978 * When you are done with it, release it with g_file_attribute_info_list_unref()
2980 GFileAttributeInfoList *
2981 g_file_query_settable_attributes (GFile *file,
2982 GCancellable *cancellable,
2983 GError **error)
2985 GFileIface *iface;
2986 GError *my_error;
2987 GFileAttributeInfoList *list;
2989 g_return_val_if_fail (G_IS_FILE (file), NULL);
2991 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2992 return NULL;
2994 iface = G_FILE_GET_IFACE (file);
2996 if (iface->query_settable_attributes == NULL)
2997 return g_file_attribute_info_list_new ();
2999 my_error = NULL;
3000 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3002 if (list == NULL)
3004 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3006 list = g_file_attribute_info_list_new ();
3007 g_error_free (my_error);
3009 else
3010 g_propagate_error (error, my_error);
3013 return list;
3017 * g_file_query_writable_namespaces:
3018 * @file: input #GFile.
3019 * @cancellable: optional #GCancellable object, %NULL to ignore.
3020 * @error: a #GError, or %NULL
3022 * Obtain the list of attribute namespaces where new attributes
3023 * can be created by a user. An example of this is extended
3024 * attributes (in the "xattr" namespace).
3026 * If @cancellable is not %NULL, then the operation can be cancelled by
3027 * triggering the cancellable object from another thread. If the operation
3028 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3030 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3031 * When you are done with it, release it with g_file_attribute_info_list_unref()
3033 GFileAttributeInfoList *
3034 g_file_query_writable_namespaces (GFile *file,
3035 GCancellable *cancellable,
3036 GError **error)
3038 GFileIface *iface;
3039 GError *my_error;
3040 GFileAttributeInfoList *list;
3042 g_return_val_if_fail (G_IS_FILE (file), NULL);
3044 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3045 return NULL;
3047 iface = G_FILE_GET_IFACE (file);
3049 if (iface->query_writable_namespaces == NULL)
3050 return g_file_attribute_info_list_new ();
3052 my_error = NULL;
3053 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3055 if (list == NULL)
3057 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3059 list = g_file_attribute_info_list_new ();
3060 g_error_free (my_error);
3062 else
3063 g_propagate_error (error, my_error);
3066 return list;
3070 * g_file_set_attribute:
3071 * @file: input #GFile.
3072 * @attribute: a string containing the attribute's name.
3073 * @type: The type of the attribute
3074 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3075 * @flags: a set of #GFileQueryInfoFlags.
3076 * @cancellable: optional #GCancellable object, %NULL to ignore.
3077 * @error: a #GError, or %NULL
3079 * Sets an attribute in the file with attribute name @attribute to @value.
3081 * If @cancellable is not %NULL, then the operation can be cancelled by
3082 * triggering the cancellable object from another thread. If the operation
3083 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3085 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3087 gboolean
3088 g_file_set_attribute (GFile *file,
3089 const char *attribute,
3090 GFileAttributeType type,
3091 gpointer value_p,
3092 GFileQueryInfoFlags flags,
3093 GCancellable *cancellable,
3094 GError **error)
3096 GFileIface *iface;
3098 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3099 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3101 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3102 return FALSE;
3104 iface = G_FILE_GET_IFACE (file);
3106 if (iface->set_attribute == NULL)
3108 g_set_error (error, G_IO_ERROR,
3109 G_IO_ERROR_NOT_SUPPORTED,
3110 _("Operation not supported"));
3111 return FALSE;
3114 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3118 * g_file_set_attributes_from_info:
3119 * @file: input #GFile.
3120 * @info: a #GFileInfo.
3121 * @flags: #GFileQueryInfoFlags
3122 * @cancellable: optional #GCancellable object, %NULL to ignore.
3123 * @error: a #GError, or %NULL
3125 * Tries to set all attributes in the #GFileInfo on the target values,
3126 * not stopping on the first error.
3128 * If there is any error during this operation then @error will be set to
3129 * the first error. Error on particular fields are flagged by setting
3130 * the "status" field in the attribute value to
3131 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3132 * further errors.
3134 * If @cancellable is not %NULL, then the operation can be cancelled by
3135 * triggering the cancellable object from another thread. If the operation
3136 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3138 * Returns: %TRUE if there was any error, %FALSE otherwise.
3140 gboolean
3141 g_file_set_attributes_from_info (GFile *file,
3142 GFileInfo *info,
3143 GFileQueryInfoFlags flags,
3144 GCancellable *cancellable,
3145 GError **error)
3147 GFileIface *iface;
3149 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3150 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3152 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3153 return FALSE;
3155 g_file_info_clear_status (info);
3157 iface = G_FILE_GET_IFACE (file);
3159 return (* iface->set_attributes_from_info) (file,
3160 info,
3161 flags,
3162 cancellable,
3163 error);
3167 static gboolean
3168 g_file_real_set_attributes_from_info (GFile *file,
3169 GFileInfo *info,
3170 GFileQueryInfoFlags flags,
3171 GCancellable *cancellable,
3172 GError **error)
3174 char **attributes;
3175 int i;
3176 gboolean res;
3177 GFileAttributeValue *value;
3179 res = TRUE;
3181 attributes = g_file_info_list_attributes (info, NULL);
3183 for (i = 0; attributes[i] != NULL; i++)
3185 value = _g_file_info_get_attribute_value (info, attributes[i]);
3187 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3188 continue;
3190 if (!g_file_set_attribute (file, attributes[i],
3191 value->type, _g_file_attribute_value_peek_as_pointer (value),
3192 flags, cancellable, error))
3194 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3195 res = FALSE;
3196 /* Don't set error multiple times */
3197 error = NULL;
3199 else
3200 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3203 g_strfreev (attributes);
3205 return res;
3209 * g_file_set_attributes_async:
3210 * @file: input #GFile.
3211 * @info: a #GFileInfo.
3212 * @flags: a #GFileQueryInfoFlags.
3213 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3214 * of the request.
3215 * @cancellable: optional #GCancellable object, %NULL to ignore.
3216 * @callback: a #GAsyncReadyCallback.
3217 * @user_data: a #gpointer.
3219 * Asynchronously sets the attributes of @file with @info.
3221 * For more details, see g_file_set_attributes_from_info() which is
3222 * the synchronous version of this call.
3224 * When the operation is finished, @callback will be called. You can then call
3225 * g_file_set_attributes_finish() to get the result of the operation.
3227 void
3228 g_file_set_attributes_async (GFile *file,
3229 GFileInfo *info,
3230 GFileQueryInfoFlags flags,
3231 int io_priority,
3232 GCancellable *cancellable,
3233 GAsyncReadyCallback callback,
3234 gpointer user_data)
3236 GFileIface *iface;
3238 g_return_if_fail (G_IS_FILE (file));
3239 g_return_if_fail (G_IS_FILE_INFO (info));
3241 iface = G_FILE_GET_IFACE (file);
3242 (* iface->set_attributes_async) (file,
3243 info,
3244 flags,
3245 io_priority,
3246 cancellable,
3247 callback,
3248 user_data);
3252 * g_file_set_attributes_finish:
3253 * @file: input #GFile.
3254 * @result: a #GAsyncResult.
3255 * @info: a #GFileInfo.
3256 * @error: a #GError, or %NULL
3258 * Finishes setting an attribute started in g_file_set_attributes_async().
3260 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3262 gboolean
3263 g_file_set_attributes_finish (GFile *file,
3264 GAsyncResult *result,
3265 GFileInfo **info,
3266 GError **error)
3268 GFileIface *iface;
3270 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3271 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3273 /* No standard handling of errors here, as we must set info even
3274 * on errors
3276 iface = G_FILE_GET_IFACE (file);
3277 return (* iface->set_attributes_finish) (file, result, info, error);
3281 * g_file_set_attribute_string:
3282 * @file: input #GFile.
3283 * @attribute: a string containing the attribute's name.
3284 * @value: a string containing the attribute's value.
3285 * @flags: #GFileQueryInfoFlags.
3286 * @cancellable: optional #GCancellable object, %NULL to ignore.
3287 * @error: a #GError, or %NULL
3289 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3290 * If @attribute is of a different type, this operation will fail.
3292 * If @cancellable is not %NULL, then the operation can be cancelled by
3293 * triggering the cancellable object from another thread. If the operation
3294 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3296 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3298 gboolean
3299 g_file_set_attribute_string (GFile *file,
3300 const char *attribute,
3301 const char *value,
3302 GFileQueryInfoFlags flags,
3303 GCancellable *cancellable,
3304 GError **error)
3306 return g_file_set_attribute (file, attribute,
3307 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3308 flags, cancellable, error);
3312 * g_file_set_attribute_byte_string:
3313 * @file: input #GFile.
3314 * @attribute: a string containing the attribute's name.
3315 * @value: a string containing the attribute's new value.
3316 * @flags: a #GFileQueryInfoFlags.
3317 * @cancellable: optional #GCancellable object, %NULL to ignore.
3318 * @error: a #GError, or %NULL
3320 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3321 * If @attribute is of a different type, this operation will fail,
3322 * returning %FALSE.
3324 * If @cancellable is not %NULL, then the operation can be cancelled by
3325 * triggering the cancellable object from another thread. If the operation
3326 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3328 * Returns: %TRUE if the @attribute was successfully set to @value
3329 * in the @file, %FALSE otherwise.
3331 gboolean
3332 g_file_set_attribute_byte_string (GFile *file,
3333 const char *attribute,
3334 const char *value,
3335 GFileQueryInfoFlags flags,
3336 GCancellable *cancellable,
3337 GError **error)
3339 return g_file_set_attribute (file, attribute,
3340 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3341 flags, cancellable, error);
3345 * g_file_set_attribute_uint32:
3346 * @file: input #GFile.
3347 * @attribute: a string containing the attribute's name.
3348 * @value: a #guint32 containing the attribute's new value.
3349 * @flags: a #GFileQueryInfoFlags.
3350 * @cancellable: optional #GCancellable object, %NULL to ignore.
3351 * @error: a #GError, or %NULL
3353 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3354 * If @attribute is of a different type, this operation will fail.
3356 * If @cancellable is not %NULL, then the operation can be cancelled by
3357 * triggering the cancellable object from another thread. If the operation
3358 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3360 * Returns: %TRUE if the @attribute was successfully set to @value
3361 * in the @file, %FALSE otherwise.
3363 gboolean
3364 g_file_set_attribute_uint32 (GFile *file,
3365 const char *attribute,
3366 guint32 value,
3367 GFileQueryInfoFlags flags,
3368 GCancellable *cancellable,
3369 GError **error)
3371 return g_file_set_attribute (file, attribute,
3372 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3373 flags, cancellable, error);
3377 * g_file_set_attribute_int32:
3378 * @file: input #GFile.
3379 * @attribute: a string containing the attribute's name.
3380 * @value: a #gint32 containing the attribute's new value.
3381 * @flags: a #GFileQueryInfoFlags.
3382 * @cancellable: optional #GCancellable object, %NULL to ignore.
3383 * @error: a #GError, or %NULL
3385 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3386 * If @attribute is of a different type, this operation will fail.
3388 * If @cancellable is not %NULL, then the operation can be cancelled by
3389 * triggering the cancellable object from another thread. If the operation
3390 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3392 * Returns: %TRUE if the @attribute was successfully set to @value
3393 * in the @file, %FALSE otherwise.
3395 gboolean
3396 g_file_set_attribute_int32 (GFile *file,
3397 const char *attribute,
3398 gint32 value,
3399 GFileQueryInfoFlags flags,
3400 GCancellable *cancellable,
3401 GError **error)
3403 return g_file_set_attribute (file, attribute,
3404 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3405 flags, cancellable, error);
3409 * g_file_set_attribute_uint64:
3410 * @file: input #GFile.
3411 * @attribute: a string containing the attribute's name.
3412 * @value: a #guint64 containing the attribute's new value.
3413 * @flags: a #GFileQueryInfoFlags.
3414 * @cancellable: optional #GCancellable object, %NULL to ignore.
3415 * @error: a #GError, or %NULL
3417 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3418 * If @attribute is of a different type, this operation will fail.
3420 * If @cancellable is not %NULL, then the operation can be cancelled by
3421 * triggering the cancellable object from another thread. If the operation
3422 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3424 * Returns: %TRUE if the @attribute was successfully set to @value
3425 * in the @file, %FALSE otherwise.
3427 gboolean
3428 g_file_set_attribute_uint64 (GFile *file,
3429 const char *attribute,
3430 guint64 value,
3431 GFileQueryInfoFlags flags,
3432 GCancellable *cancellable,
3433 GError **error)
3435 return g_file_set_attribute (file, attribute,
3436 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3437 flags, cancellable, error);
3441 * g_file_set_attribute_int64:
3442 * @file: input #GFile.
3443 * @attribute: a string containing the attribute's name.
3444 * @value: a #guint64 containing the attribute's new value.
3445 * @flags: a #GFileQueryInfoFlags.
3446 * @cancellable: optional #GCancellable object, %NULL to ignore.
3447 * @error: a #GError, or %NULL
3449 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3450 * If @attribute is of a different type, this operation will fail.
3452 * If @cancellable is not %NULL, then the operation can be cancelled by
3453 * triggering the cancellable object from another thread. If the operation
3454 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3456 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3458 gboolean
3459 g_file_set_attribute_int64 (GFile *file,
3460 const char *attribute,
3461 gint64 value,
3462 GFileQueryInfoFlags flags,
3463 GCancellable *cancellable,
3464 GError **error)
3466 return g_file_set_attribute (file, attribute,
3467 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3468 flags, cancellable, error);
3472 * g_file_mount_mountable:
3473 * @file: input #GFile.
3474 * @flags: flags affecting the operation
3475 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3476 * @cancellable: optional #GCancellable object, %NULL to ignore.
3477 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3478 * @user_data: the data to pass to callback function
3480 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3481 * Using @mount_operation, you can request callbacks when, for instance,
3482 * passwords are needed during authentication.
3484 * If @cancellable is not %NULL, then the operation can be cancelled by
3485 * triggering the cancellable object from another thread. If the operation
3486 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3488 * When the operation is finished, @callback will be called. You can then call
3489 * g_file_mount_mountable_finish() to get the result of the operation.
3491 void
3492 g_file_mount_mountable (GFile *file,
3493 GMountMountFlags flags,
3494 GMountOperation *mount_operation,
3495 GCancellable *cancellable,
3496 GAsyncReadyCallback callback,
3497 gpointer user_data)
3499 GFileIface *iface;
3501 g_return_if_fail (G_IS_FILE (file));
3503 iface = G_FILE_GET_IFACE (file);
3505 if (iface->mount_mountable == NULL)
3507 g_simple_async_report_error_in_idle (G_OBJECT (file),
3508 callback,
3509 user_data,
3510 G_IO_ERROR,
3511 G_IO_ERROR_NOT_SUPPORTED,
3512 _("Operation not supported"));
3513 return;
3516 (* iface->mount_mountable) (file,
3517 flags,
3518 mount_operation,
3519 cancellable,
3520 callback,
3521 user_data);
3525 * g_file_mount_mountable_finish:
3526 * @file: input #GFile.
3527 * @result: a #GAsyncResult.
3528 * @error: a #GError, or %NULL
3530 * Finishes a mount operation. See g_file_mount_mountable() for details.
3532 * Finish an asynchronous mount operation that was started
3533 * with g_file_mount_mountable().
3535 * Returns: a #GFile or %NULL on error.
3537 GFile *
3538 g_file_mount_mountable_finish (GFile *file,
3539 GAsyncResult *result,
3540 GError **error)
3542 GFileIface *iface;
3544 g_return_val_if_fail (G_IS_FILE (file), NULL);
3545 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3547 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3549 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3550 if (g_simple_async_result_propagate_error (simple, error))
3551 return NULL;
3554 iface = G_FILE_GET_IFACE (file);
3555 return (* iface->mount_mountable_finish) (file, result, error);
3559 * g_file_unmount_mountable:
3560 * @file: input #GFile.
3561 * @flags: flags affecting the operation
3562 * @cancellable: optional #GCancellable object, %NULL to ignore.
3563 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3564 * @user_data: the data to pass to callback function
3566 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3568 * If @cancellable is not %NULL, then the operation can be cancelled by
3569 * triggering the cancellable object from another thread. If the operation
3570 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3572 * When the operation is finished, @callback will be called. You can then call
3573 * g_file_unmount_mountable_finish() to get the result of the operation.
3575 void
3576 g_file_unmount_mountable (GFile *file,
3577 GMountUnmountFlags flags,
3578 GCancellable *cancellable,
3579 GAsyncReadyCallback callback,
3580 gpointer user_data)
3582 GFileIface *iface;
3584 g_return_if_fail (G_IS_FILE (file));
3586 iface = G_FILE_GET_IFACE (file);
3588 if (iface->unmount_mountable == NULL)
3590 g_simple_async_report_error_in_idle (G_OBJECT (file),
3591 callback,
3592 user_data,
3593 G_IO_ERROR,
3594 G_IO_ERROR_NOT_SUPPORTED,
3595 _("Operation not supported"));
3596 return;
3599 (* iface->unmount_mountable) (file,
3600 flags,
3601 cancellable,
3602 callback,
3603 user_data);
3607 * g_file_unmount_mountable_finish:
3608 * @file: input #GFile.
3609 * @result: a #GAsyncResult.
3610 * @error: a #GError, or %NULL
3612 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3614 * Finish an asynchronous unmount operation that was started
3615 * with g_file_unmount_mountable().
3617 * Returns: %TRUE if the operation finished successfully. %FALSE
3618 * otherwise.
3620 gboolean
3621 g_file_unmount_mountable_finish (GFile *file,
3622 GAsyncResult *result,
3623 GError **error)
3625 GFileIface *iface;
3627 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3628 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3630 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3632 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3633 if (g_simple_async_result_propagate_error (simple, error))
3634 return FALSE;
3637 iface = G_FILE_GET_IFACE (file);
3638 return (* iface->unmount_mountable_finish) (file, result, error);
3642 * g_file_eject_mountable:
3643 * @file: input #GFile.
3644 * @flags: flags affecting the operation
3645 * @cancellable: optional #GCancellable object, %NULL to ignore.
3646 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3647 * @user_data: the data to pass to callback function
3649 * Starts an asynchronous eject on a mountable.
3650 * When this operation has completed, @callback will be called with
3651 * @user_user data, and the operation can be finalized with
3652 * g_file_eject_mountable_finish().
3654 * If @cancellable is not %NULL, then the operation can be cancelled by
3655 * triggering the cancellable object from another thread. If the operation
3656 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3658 void
3659 g_file_eject_mountable (GFile *file,
3660 GMountUnmountFlags flags,
3661 GCancellable *cancellable,
3662 GAsyncReadyCallback callback,
3663 gpointer user_data)
3665 GFileIface *iface;
3667 g_return_if_fail (G_IS_FILE (file));
3669 iface = G_FILE_GET_IFACE (file);
3671 if (iface->eject_mountable == NULL)
3673 g_simple_async_report_error_in_idle (G_OBJECT (file),
3674 callback,
3675 user_data,
3676 G_IO_ERROR,
3677 G_IO_ERROR_NOT_SUPPORTED,
3678 _("Operation not supported"));
3679 return;
3682 (* iface->eject_mountable) (file,
3683 flags,
3684 cancellable,
3685 callback,
3686 user_data);
3690 * g_file_eject_mountable_finish:
3691 * @file: input #GFile.
3692 * @result: a #GAsyncResult.
3693 * @error: a #GError, or %NULL
3695 * Finishes an asynchronous eject operation started by
3696 * g_file_eject_mountable().
3698 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3699 * otherwise.
3701 gboolean
3702 g_file_eject_mountable_finish (GFile *file,
3703 GAsyncResult *result,
3704 GError **error)
3706 GFileIface *iface;
3708 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3709 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3711 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3713 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3714 if (g_simple_async_result_propagate_error (simple, error))
3715 return FALSE;
3718 iface = G_FILE_GET_IFACE (file);
3719 return (* iface->eject_mountable_finish) (file, result, error);
3723 * g_file_monitor_directory:
3724 * @file: input #GFile.
3725 * @flags: a set of #GFileMonitorFlags.
3726 * @cancellable: optional #GCancellable object, %NULL to ignore.
3727 * @error: a #GError, or %NULL.
3729 * Obtains a directory monitor for the given file.
3730 * This may fail if directory monitoring is not supported.
3732 * If @cancellable is not %NULL, then the operation can be cancelled by
3733 * triggering the cancellable object from another thread. If the operation
3734 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3736 * Returns: a #GFileMonitor for the given @file,
3737 * or %NULL on error.
3739 GFileMonitor*
3740 g_file_monitor_directory (GFile *file,
3741 GFileMonitorFlags flags,
3742 GCancellable *cancellable,
3743 GError **error)
3745 GFileIface *iface;
3747 g_return_val_if_fail (G_IS_FILE (file), NULL);
3749 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3750 return NULL;
3752 iface = G_FILE_GET_IFACE (file);
3754 if (iface->monitor_dir == NULL)
3756 g_set_error (error, G_IO_ERROR,
3757 G_IO_ERROR_NOT_SUPPORTED,
3758 _("Operation not supported"));
3759 return NULL;
3762 return (* iface->monitor_dir) (file, flags, cancellable, error);
3766 * g_file_monitor_file:
3767 * @file: input #GFile.
3768 * @flags: a set of #GFileMonitorFlags.
3769 * @cancellable: optional #GCancellable object, %NULL to ignore.
3770 * @error: a #GError, or %NULL.
3772 * Obtains a file monitor for the given file. If no file notification
3773 * mechanism exists, then regular polling of the file is used.
3775 * If @cancellable is not %NULL, then the operation can be cancelled by
3776 * triggering the cancellable object from another thread. If the operation
3777 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3779 * Returns: a #GFileMonitor for the given @file.
3781 GFileMonitor*
3782 g_file_monitor_file (GFile *file,
3783 GFileMonitorFlags flags,
3784 GCancellable *cancellable,
3785 GError **error)
3787 GFileIface *iface;
3788 GFileMonitor *monitor;
3790 g_return_val_if_fail (G_IS_FILE (file), NULL);
3792 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3793 return NULL;
3795 iface = G_FILE_GET_IFACE (file);
3797 monitor = NULL;
3799 if (iface->monitor_file)
3800 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3802 /* Fallback to polling */
3803 if (monitor == NULL)
3804 monitor = _g_poll_file_monitor_new (file);
3806 return monitor;
3809 /********************************************
3810 * Default implementation of async ops *
3811 ********************************************/
3813 typedef struct {
3814 char *attributes;
3815 GFileQueryInfoFlags flags;
3816 GFileInfo *info;
3817 } QueryInfoAsyncData;
3819 static void
3820 query_info_data_free (QueryInfoAsyncData *data)
3822 if (data->info)
3823 g_object_unref (data->info);
3824 g_free (data->attributes);
3825 g_free (data);
3828 static void
3829 query_info_async_thread (GSimpleAsyncResult *res,
3830 GObject *object,
3831 GCancellable *cancellable)
3833 GError *error = NULL;
3834 QueryInfoAsyncData *data;
3835 GFileInfo *info;
3837 data = g_simple_async_result_get_op_res_gpointer (res);
3839 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3841 if (info == NULL)
3843 g_simple_async_result_set_from_error (res, error);
3844 g_error_free (error);
3846 else
3847 data->info = info;
3850 static void
3851 g_file_real_query_info_async (GFile *file,
3852 const char *attributes,
3853 GFileQueryInfoFlags flags,
3854 int io_priority,
3855 GCancellable *cancellable,
3856 GAsyncReadyCallback callback,
3857 gpointer user_data)
3859 GSimpleAsyncResult *res;
3860 QueryInfoAsyncData *data;
3862 data = g_new0 (QueryInfoAsyncData, 1);
3863 data->attributes = g_strdup (attributes);
3864 data->flags = flags;
3866 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3867 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3869 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3870 g_object_unref (res);
3873 static GFileInfo *
3874 g_file_real_query_info_finish (GFile *file,
3875 GAsyncResult *res,
3876 GError **error)
3878 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3879 QueryInfoAsyncData *data;
3881 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3883 data = g_simple_async_result_get_op_res_gpointer (simple);
3884 if (data->info)
3885 return g_object_ref (data->info);
3887 return NULL;
3890 typedef struct {
3891 char *attributes;
3892 GFileInfo *info;
3893 } QueryFilesystemInfoAsyncData;
3895 static void
3896 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
3898 if (data->info)
3899 g_object_unref (data->info);
3900 g_free (data->attributes);
3901 g_free (data);
3904 static void
3905 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
3906 GObject *object,
3907 GCancellable *cancellable)
3909 GError *error = NULL;
3910 QueryFilesystemInfoAsyncData *data;
3911 GFileInfo *info;
3913 data = g_simple_async_result_get_op_res_gpointer (res);
3915 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
3917 if (info == NULL)
3919 g_simple_async_result_set_from_error (res, error);
3920 g_error_free (error);
3922 else
3923 data->info = info;
3926 static void
3927 g_file_real_query_filesystem_info_async (GFile *file,
3928 const char *attributes,
3929 int io_priority,
3930 GCancellable *cancellable,
3931 GAsyncReadyCallback callback,
3932 gpointer user_data)
3934 GSimpleAsyncResult *res;
3935 QueryFilesystemInfoAsyncData *data;
3937 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
3938 data->attributes = g_strdup (attributes);
3940 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
3941 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
3943 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
3944 g_object_unref (res);
3947 static GFileInfo *
3948 g_file_real_query_filesystem_info_finish (GFile *file,
3949 GAsyncResult *res,
3950 GError **error)
3952 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3953 QueryFilesystemInfoAsyncData *data;
3955 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
3957 data = g_simple_async_result_get_op_res_gpointer (simple);
3958 if (data->info)
3959 return g_object_ref (data->info);
3961 return NULL;
3964 typedef struct {
3965 char *attributes;
3966 GFileQueryInfoFlags flags;
3967 GFileEnumerator *enumerator;
3968 } EnumerateChildrenAsyncData;
3970 static void
3971 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3973 if (data->enumerator)
3974 g_object_unref (data->enumerator);
3975 g_free (data->attributes);
3976 g_free (data);
3979 static void
3980 enumerate_children_async_thread (GSimpleAsyncResult *res,
3981 GObject *object,
3982 GCancellable *cancellable)
3984 GError *error = NULL;
3985 EnumerateChildrenAsyncData *data;
3986 GFileEnumerator *enumerator;
3988 data = g_simple_async_result_get_op_res_gpointer (res);
3990 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3992 if (enumerator == NULL)
3994 g_simple_async_result_set_from_error (res, error);
3995 g_error_free (error);
3997 else
3998 data->enumerator = enumerator;
4001 static void
4002 g_file_real_enumerate_children_async (GFile *file,
4003 const char *attributes,
4004 GFileQueryInfoFlags flags,
4005 int io_priority,
4006 GCancellable *cancellable,
4007 GAsyncReadyCallback callback,
4008 gpointer user_data)
4010 GSimpleAsyncResult *res;
4011 EnumerateChildrenAsyncData *data;
4013 data = g_new0 (EnumerateChildrenAsyncData, 1);
4014 data->attributes = g_strdup (attributes);
4015 data->flags = flags;
4017 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4018 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4020 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4021 g_object_unref (res);
4024 static GFileEnumerator *
4025 g_file_real_enumerate_children_finish (GFile *file,
4026 GAsyncResult *res,
4027 GError **error)
4029 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4030 EnumerateChildrenAsyncData *data;
4032 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4034 data = g_simple_async_result_get_op_res_gpointer (simple);
4035 if (data->enumerator)
4036 return g_object_ref (data->enumerator);
4038 return NULL;
4041 static void
4042 open_read_async_thread (GSimpleAsyncResult *res,
4043 GObject *object,
4044 GCancellable *cancellable)
4046 GFileIface *iface;
4047 GFileInputStream *stream;
4048 GError *error = NULL;
4050 iface = G_FILE_GET_IFACE (object);
4052 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4054 if (stream == NULL)
4056 g_simple_async_result_set_from_error (res, error);
4057 g_error_free (error);
4059 else
4060 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4063 static void
4064 g_file_real_read_async (GFile *file,
4065 int io_priority,
4066 GCancellable *cancellable,
4067 GAsyncReadyCallback callback,
4068 gpointer user_data)
4070 GSimpleAsyncResult *res;
4072 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4074 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4075 g_object_unref (res);
4078 static GFileInputStream *
4079 g_file_real_read_finish (GFile *file,
4080 GAsyncResult *res,
4081 GError **error)
4083 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4084 gpointer op;
4086 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4088 op = g_simple_async_result_get_op_res_gpointer (simple);
4089 if (op)
4090 return g_object_ref (op);
4092 return NULL;
4095 static void
4096 append_to_async_thread (GSimpleAsyncResult *res,
4097 GObject *object,
4098 GCancellable *cancellable)
4100 GFileIface *iface;
4101 GFileCreateFlags *data;
4102 GFileOutputStream *stream;
4103 GError *error = NULL;
4105 iface = G_FILE_GET_IFACE (object);
4107 data = g_simple_async_result_get_op_res_gpointer (res);
4109 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4111 if (stream == NULL)
4113 g_simple_async_result_set_from_error (res, error);
4114 g_error_free (error);
4116 else
4117 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4120 static void
4121 g_file_real_append_to_async (GFile *file,
4122 GFileCreateFlags flags,
4123 int io_priority,
4124 GCancellable *cancellable,
4125 GAsyncReadyCallback callback,
4126 gpointer user_data)
4128 GFileCreateFlags *data;
4129 GSimpleAsyncResult *res;
4131 data = g_new0 (GFileCreateFlags, 1);
4132 *data = flags;
4134 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4135 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4137 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4138 g_object_unref (res);
4141 static GFileOutputStream *
4142 g_file_real_append_to_finish (GFile *file,
4143 GAsyncResult *res,
4144 GError **error)
4146 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4147 gpointer op;
4149 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4151 op = g_simple_async_result_get_op_res_gpointer (simple);
4152 if (op)
4153 return g_object_ref (op);
4155 return NULL;
4158 static void
4159 create_async_thread (GSimpleAsyncResult *res,
4160 GObject *object,
4161 GCancellable *cancellable)
4163 GFileIface *iface;
4164 GFileCreateFlags *data;
4165 GFileOutputStream *stream;
4166 GError *error = NULL;
4168 iface = G_FILE_GET_IFACE (object);
4170 data = g_simple_async_result_get_op_res_gpointer (res);
4172 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4174 if (stream == NULL)
4176 g_simple_async_result_set_from_error (res, error);
4177 g_error_free (error);
4179 else
4180 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4183 static void
4184 g_file_real_create_async (GFile *file,
4185 GFileCreateFlags flags,
4186 int io_priority,
4187 GCancellable *cancellable,
4188 GAsyncReadyCallback callback,
4189 gpointer user_data)
4191 GFileCreateFlags *data;
4192 GSimpleAsyncResult *res;
4194 data = g_new0 (GFileCreateFlags, 1);
4195 *data = flags;
4197 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4198 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4200 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4201 g_object_unref (res);
4204 static GFileOutputStream *
4205 g_file_real_create_finish (GFile *file,
4206 GAsyncResult *res,
4207 GError **error)
4209 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4210 gpointer op;
4212 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4214 op = g_simple_async_result_get_op_res_gpointer (simple);
4215 if (op)
4216 return g_object_ref (op);
4218 return NULL;
4221 typedef struct {
4222 GFileOutputStream *stream;
4223 char *etag;
4224 gboolean make_backup;
4225 GFileCreateFlags flags;
4226 } ReplaceAsyncData;
4228 static void
4229 replace_async_data_free (ReplaceAsyncData *data)
4231 if (data->stream)
4232 g_object_unref (data->stream);
4233 g_free (data->etag);
4234 g_free (data);
4237 static void
4238 replace_async_thread (GSimpleAsyncResult *res,
4239 GObject *object,
4240 GCancellable *cancellable)
4242 GFileIface *iface;
4243 GFileOutputStream *stream;
4244 GError *error = NULL;
4245 ReplaceAsyncData *data;
4247 iface = G_FILE_GET_IFACE (object);
4249 data = g_simple_async_result_get_op_res_gpointer (res);
4251 stream = iface->replace (G_FILE (object),
4252 data->etag,
4253 data->make_backup,
4254 data->flags,
4255 cancellable,
4256 &error);
4258 if (stream == NULL)
4260 g_simple_async_result_set_from_error (res, error);
4261 g_error_free (error);
4263 else
4264 data->stream = stream;
4267 static void
4268 g_file_real_replace_async (GFile *file,
4269 const char *etag,
4270 gboolean make_backup,
4271 GFileCreateFlags flags,
4272 int io_priority,
4273 GCancellable *cancellable,
4274 GAsyncReadyCallback callback,
4275 gpointer user_data)
4277 GSimpleAsyncResult *res;
4278 ReplaceAsyncData *data;
4280 data = g_new0 (ReplaceAsyncData, 1);
4281 data->etag = g_strdup (etag);
4282 data->make_backup = make_backup;
4283 data->flags = flags;
4285 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4286 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4288 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4289 g_object_unref (res);
4292 static GFileOutputStream *
4293 g_file_real_replace_finish (GFile *file,
4294 GAsyncResult *res,
4295 GError **error)
4297 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4298 ReplaceAsyncData *data;
4300 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4302 data = g_simple_async_result_get_op_res_gpointer (simple);
4303 if (data->stream)
4304 return g_object_ref (data->stream);
4306 return NULL;
4309 typedef struct {
4310 char *name;
4311 GFile *file;
4312 } SetDisplayNameAsyncData;
4314 static void
4315 set_display_name_data_free (SetDisplayNameAsyncData *data)
4317 g_free (data->name);
4318 if (data->file)
4319 g_object_unref (data->file);
4320 g_free (data);
4323 static void
4324 set_display_name_async_thread (GSimpleAsyncResult *res,
4325 GObject *object,
4326 GCancellable *cancellable)
4328 GError *error = NULL;
4329 SetDisplayNameAsyncData *data;
4330 GFile *file;
4332 data = g_simple_async_result_get_op_res_gpointer (res);
4334 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4336 if (file == NULL)
4338 g_simple_async_result_set_from_error (res, error);
4339 g_error_free (error);
4341 else
4342 data->file = file;
4345 static void
4346 g_file_real_set_display_name_async (GFile *file,
4347 const char *display_name,
4348 int io_priority,
4349 GCancellable *cancellable,
4350 GAsyncReadyCallback callback,
4351 gpointer user_data)
4353 GSimpleAsyncResult *res;
4354 SetDisplayNameAsyncData *data;
4356 data = g_new0 (SetDisplayNameAsyncData, 1);
4357 data->name = g_strdup (display_name);
4359 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4360 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4362 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4363 g_object_unref (res);
4366 static GFile *
4367 g_file_real_set_display_name_finish (GFile *file,
4368 GAsyncResult *res,
4369 GError **error)
4371 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4372 SetDisplayNameAsyncData *data;
4374 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4376 data = g_simple_async_result_get_op_res_gpointer (simple);
4377 if (data->file)
4378 return g_object_ref (data->file);
4380 return NULL;
4383 typedef struct {
4384 GFileQueryInfoFlags flags;
4385 GFileInfo *info;
4386 gboolean res;
4387 GError *error;
4388 } SetInfoAsyncData;
4390 static void
4391 set_info_data_free (SetInfoAsyncData *data)
4393 if (data->info)
4394 g_object_unref (data->info);
4395 if (data->error)
4396 g_error_free (data->error);
4397 g_free (data);
4400 static void
4401 set_info_async_thread (GSimpleAsyncResult *res,
4402 GObject *object,
4403 GCancellable *cancellable)
4405 SetInfoAsyncData *data;
4407 data = g_simple_async_result_get_op_res_gpointer (res);
4409 data->error = NULL;
4410 data->res = g_file_set_attributes_from_info (G_FILE (object),
4411 data->info,
4412 data->flags,
4413 cancellable,
4414 &data->error);
4417 static void
4418 g_file_real_set_attributes_async (GFile *file,
4419 GFileInfo *info,
4420 GFileQueryInfoFlags flags,
4421 int io_priority,
4422 GCancellable *cancellable,
4423 GAsyncReadyCallback callback,
4424 gpointer user_data)
4426 GSimpleAsyncResult *res;
4427 SetInfoAsyncData *data;
4429 data = g_new0 (SetInfoAsyncData, 1);
4430 data->info = g_file_info_dup (info);
4431 data->flags = flags;
4433 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4434 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4436 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4437 g_object_unref (res);
4440 static gboolean
4441 g_file_real_set_attributes_finish (GFile *file,
4442 GAsyncResult *res,
4443 GFileInfo **info,
4444 GError **error)
4446 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4447 SetInfoAsyncData *data;
4449 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4451 data = g_simple_async_result_get_op_res_gpointer (simple);
4453 if (info)
4454 *info = g_object_ref (data->info);
4456 if (error != NULL && data->error)
4457 *error = g_error_copy (data->error);
4459 return data->res;
4462 static void
4463 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4464 GObject *object,
4465 GCancellable *cancellable)
4467 GError *error = NULL;
4468 GMount *mount;
4470 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4472 if (mount == NULL)
4474 g_simple_async_result_set_from_error (res, error);
4475 g_error_free (error);
4477 else
4478 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4481 static void
4482 g_file_real_find_enclosing_mount_async (GFile *file,
4483 int io_priority,
4484 GCancellable *cancellable,
4485 GAsyncReadyCallback callback,
4486 gpointer user_data)
4488 GSimpleAsyncResult *res;
4490 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4492 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4493 g_object_unref (res);
4496 static GMount *
4497 g_file_real_find_enclosing_mount_finish (GFile *file,
4498 GAsyncResult *res,
4499 GError **error)
4501 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4502 GMount *mount;
4504 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4506 mount = g_simple_async_result_get_op_res_gpointer (simple);
4507 return g_object_ref (mount);
4511 typedef struct {
4512 GFile *source;
4513 GFile *destination;
4514 GFileCopyFlags flags;
4515 GFileProgressCallback progress_cb;
4516 gpointer progress_cb_data;
4517 GIOSchedulerJob *job;
4518 } CopyAsyncData;
4520 static void
4521 copy_async_data_free (CopyAsyncData *data)
4523 g_object_unref (data->source);
4524 g_object_unref (data->destination);
4525 g_free (data);
4528 typedef struct {
4529 CopyAsyncData *data;
4530 goffset current_num_bytes;
4531 goffset total_num_bytes;
4532 } ProgressData;
4534 static gboolean
4535 copy_async_progress_in_main (gpointer user_data)
4537 ProgressData *progress = user_data;
4538 CopyAsyncData *data = progress->data;
4540 data->progress_cb (progress->current_num_bytes,
4541 progress->total_num_bytes,
4542 data->progress_cb_data);
4544 return FALSE;
4547 static gboolean
4548 mainloop_barrier (gpointer user_data)
4550 /* Does nothing, but ensures all queued idles before
4551 this are run */
4552 return FALSE;
4556 static void
4557 copy_async_progress_callback (goffset current_num_bytes,
4558 goffset total_num_bytes,
4559 gpointer user_data)
4561 CopyAsyncData *data = user_data;
4562 ProgressData *progress;
4564 progress = g_new (ProgressData, 1);
4565 progress->data = data;
4566 progress->current_num_bytes = current_num_bytes;
4567 progress->total_num_bytes = total_num_bytes;
4569 g_io_scheduler_job_send_to_mainloop_async (data->job,
4570 copy_async_progress_in_main,
4571 progress,
4572 g_free);
4575 static gboolean
4576 copy_async_thread (GIOSchedulerJob *job,
4577 GCancellable *cancellable,
4578 gpointer user_data)
4580 GSimpleAsyncResult *res;
4581 CopyAsyncData *data;
4582 gboolean result;
4583 GError *error;
4585 res = user_data;
4586 data = g_simple_async_result_get_op_res_gpointer (res);
4588 error = NULL;
4589 data->job = job;
4590 result = g_file_copy (data->source,
4591 data->destination,
4592 data->flags,
4593 cancellable,
4594 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4595 data,
4596 &error);
4598 /* Ensure all progress callbacks are done running in main thread */
4599 if (data->progress_cb != NULL)
4600 g_io_scheduler_job_send_to_mainloop (job,
4601 mainloop_barrier,
4602 NULL, NULL);
4604 if (!result)
4606 g_simple_async_result_set_from_error (res, error);
4607 g_error_free (error);
4610 g_simple_async_result_complete_in_idle (res);
4612 return FALSE;
4615 static void
4616 g_file_real_copy_async (GFile *source,
4617 GFile *destination,
4618 GFileCopyFlags flags,
4619 int io_priority,
4620 GCancellable *cancellable,
4621 GFileProgressCallback progress_callback,
4622 gpointer progress_callback_data,
4623 GAsyncReadyCallback callback,
4624 gpointer user_data)
4626 GSimpleAsyncResult *res;
4627 CopyAsyncData *data;
4629 data = g_new0 (CopyAsyncData, 1);
4630 data->source = g_object_ref (source);
4631 data->destination = g_object_ref (destination);
4632 data->flags = flags;
4633 data->progress_cb = progress_callback;
4634 data->progress_cb_data = progress_callback_data;
4636 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4637 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4639 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4642 static gboolean
4643 g_file_real_copy_finish (GFile *file,
4644 GAsyncResult *res,
4645 GError **error)
4647 /* Error handled in g_file_copy_finish() */
4648 return TRUE;
4652 /********************************************
4653 * Default VFS operations *
4654 ********************************************/
4657 * g_file_new_for_path:
4658 * @path: a string containing a relative or absolute path.
4660 * Constructs a #GFile for a given path. This operation never
4661 * fails, but the returned object might not support any I/O
4662 * operation if @path is malformed.
4664 * Returns: a new #GFile for the given @path.
4666 GFile *
4667 g_file_new_for_path (const char *path)
4669 g_return_val_if_fail (path != NULL, NULL);
4671 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4675 * g_file_new_for_uri:
4676 * @uri: a string containing a URI.
4678 * Constructs a #GFile for a given URI. This operation never
4679 * fails, but the returned object might not support any I/O
4680 * operation if @uri is malformed or if the uri type is
4681 * not supported.
4683 * Returns: a #GFile for the given @uri.
4684 **/
4685 GFile *
4686 g_file_new_for_uri (const char *uri)
4688 g_return_val_if_fail (uri != NULL, NULL);
4690 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4694 * g_file_parse_name:
4695 * @parse_name: a file name or path to be parsed.
4697 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4698 * This operation never fails, but the returned object might not support any I/O
4699 * operation if the @parse_name cannot be parsed.
4701 * Returns: a new #GFile.
4703 GFile *
4704 g_file_parse_name (const char *parse_name)
4706 g_return_val_if_fail (parse_name != NULL, NULL);
4708 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4711 static gboolean
4712 is_valid_scheme_character (char c)
4714 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4717 /* Following RFC 2396, valid schemes are built like:
4718 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
4720 static gboolean
4721 has_valid_scheme (const char *uri)
4723 const char *p;
4725 p = uri;
4727 if (!g_ascii_isalpha (*p))
4728 return FALSE;
4730 do {
4731 p++;
4732 } while (is_valid_scheme_character (*p));
4734 return *p == ':';
4738 * g_file_new_for_commandline_arg:
4739 * @arg: a command line string.
4741 * Creates a #GFile with the given argument from the command line. The value of
4742 * @arg can be either a URI, an absolute path or a relative path resolved
4743 * relative to the current working directory.
4744 * This operation never fails, but the returned object might not support any
4745 * I/O operation if @arg points to a malformed path.
4747 * Returns: a new #GFile.
4749 GFile *
4750 g_file_new_for_commandline_arg (const char *arg)
4752 GFile *file;
4753 char *filename;
4754 char *current_dir;
4756 g_return_val_if_fail (arg != NULL, NULL);
4758 if (g_path_is_absolute (arg))
4759 return g_file_new_for_path (arg);
4761 if (has_valid_scheme (arg))
4762 return g_file_new_for_uri (arg);
4764 current_dir = g_get_current_dir ();
4765 filename = g_build_filename (current_dir, arg, NULL);
4766 g_free (current_dir);
4768 file = g_file_new_for_path (filename);
4769 g_free (filename);
4771 return file;
4775 * g_file_mount_enclosing_volume:
4776 * @location: input #GFile.
4777 * @flags: flags affecting the operation
4778 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4779 * @cancellable: optional #GCancellable object, %NULL to ignore.
4780 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4781 * @user_data: the data to pass to callback function
4783 * Starts a @mount_operation, mounting the volume that contains the file @location.
4785 * When this operation has completed, @callback will be called with
4786 * @user_user data, and the operation can be finalized with
4787 * g_file_mount_enclosing_volume_finish().
4789 * If @cancellable is not %NULL, then the operation can be cancelled by
4790 * triggering the cancellable object from another thread. If the operation
4791 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4793 void
4794 g_file_mount_enclosing_volume (GFile *location,
4795 GMountMountFlags flags,
4796 GMountOperation *mount_operation,
4797 GCancellable *cancellable,
4798 GAsyncReadyCallback callback,
4799 gpointer user_data)
4801 GFileIface *iface;
4803 g_return_if_fail (G_IS_FILE (location));
4805 iface = G_FILE_GET_IFACE (location);
4807 if (iface->mount_enclosing_volume == NULL)
4809 g_simple_async_report_error_in_idle (G_OBJECT (location),
4810 callback, user_data,
4811 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4812 _("volume doesn't implement mount"));
4814 return;
4817 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4822 * g_file_mount_enclosing_volume_finish:
4823 * @location: input #GFile.
4824 * @result: a #GAsyncResult.
4825 * @error: a #GError, or %NULL
4827 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4829 * Returns: %TRUE if successful. If an error
4830 * has occurred, this function will return %FALSE and set @error
4831 * appropriately if present.
4833 gboolean
4834 g_file_mount_enclosing_volume_finish (GFile *location,
4835 GAsyncResult *result,
4836 GError **error)
4838 GFileIface *iface;
4840 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4841 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4843 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4845 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4846 if (g_simple_async_result_propagate_error (simple, error))
4847 return FALSE;
4850 iface = G_FILE_GET_IFACE (location);
4852 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4855 /********************************************
4856 * Utility functions *
4857 ********************************************/
4860 * g_file_query_default_handler:
4861 * @file: a #GFile to open.
4862 * @cancellable: optional #GCancellable object, %NULL to ignore.
4863 * @error: a #GError, or %NULL
4865 * Returns the #GAppInfo that is registered as the default
4866 * application to handle the file specified by @file.
4868 * If @cancellable is not %NULL, then the operation can be cancelled by
4869 * triggering the cancellable object from another thread. If the operation
4870 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4872 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4873 * When you are done with it, release it with g_object_unref()
4875 GAppInfo *
4876 g_file_query_default_handler (GFile *file,
4877 GCancellable *cancellable,
4878 GError **error)
4880 char *uri_scheme;
4881 const char *content_type;
4882 GAppInfo *appinfo;
4883 GFileInfo *info;
4884 char *path;
4886 uri_scheme = g_file_get_uri_scheme (file);
4887 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4888 g_free (uri_scheme);
4890 if (appinfo != NULL)
4891 return appinfo;
4893 info = g_file_query_info (file,
4894 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4896 cancellable,
4897 error);
4898 if (info == NULL)
4899 return NULL;
4901 appinfo = NULL;
4903 content_type = g_file_info_get_content_type (info);
4904 if (content_type)
4906 /* Don't use is_native(), as we want to support fuse paths if availible */
4907 path = g_file_get_path (file);
4908 appinfo = g_app_info_get_default_for_type (content_type,
4909 path == NULL);
4910 g_free (path);
4913 g_object_unref (info);
4915 if (appinfo != NULL)
4916 return appinfo;
4918 g_set_error (error, G_IO_ERROR,
4919 G_IO_ERROR_NOT_SUPPORTED,
4920 _("No application is registered as handling this file"));
4921 return NULL;
4926 #define GET_CONTENT_BLOCK_SIZE 8192
4929 * g_file_load_contents:
4930 * @file: input #GFile.
4931 * @cancellable: optional #GCancellable object, %NULL to ignore.
4932 * @contents: a location to place the contents of the file.
4933 * @length: a location to place the length of the contents of the file.
4934 * @etag_out: a location to place the current entity tag for the file.
4935 * @error: a #GError, or %NULL
4937 * Loads the content of the file into memory, returning the size of
4938 * the data. The data is always zero terminated, but this is not
4939 * included in the resultant @length.
4941 * If @cancellable is not %NULL, then the operation can be cancelled by
4942 * triggering the cancellable object from another thread. If the operation
4943 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4945 * Returns: %TRUE if the @file's contents were successfully loaded.
4946 * %FALSE if there were errors..
4948 gboolean
4949 g_file_load_contents (GFile *file,
4950 GCancellable *cancellable,
4951 char **contents,
4952 gsize *length,
4953 char **etag_out,
4954 GError **error)
4956 GFileInputStream *in;
4957 GByteArray *content;
4958 gsize pos;
4959 gssize res;
4960 GFileInfo *info;
4962 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4963 g_return_val_if_fail (contents != NULL, FALSE);
4965 in = g_file_read (file, cancellable, error);
4966 if (in == NULL)
4967 return FALSE;
4969 content = g_byte_array_new ();
4970 pos = 0;
4972 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4973 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4974 content->data + pos,
4975 GET_CONTENT_BLOCK_SIZE,
4976 cancellable, error)) > 0)
4978 pos += res;
4979 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4982 if (etag_out)
4984 *etag_out = NULL;
4986 info = g_file_input_stream_query_info (in,
4987 G_FILE_ATTRIBUTE_ETAG_VALUE,
4988 cancellable,
4989 NULL);
4990 if (info)
4992 *etag_out = g_strdup (g_file_info_get_etag (info));
4993 g_object_unref (info);
4997 /* Ignore errors on close */
4998 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4999 g_object_unref (in);
5001 if (res < 0)
5003 /* error is set already */
5004 g_byte_array_free (content, TRUE);
5005 return FALSE;
5008 if (length)
5009 *length = pos;
5011 /* Zero terminate (we got an extra byte allocated for this */
5012 content->data[pos] = 0;
5014 *contents = (char *)g_byte_array_free (content, FALSE);
5016 return TRUE;
5019 typedef struct {
5020 GFile *file;
5021 GError *error;
5022 GCancellable *cancellable;
5023 GFileReadMoreCallback read_more_callback;
5024 GAsyncReadyCallback callback;
5025 gpointer user_data;
5026 GByteArray *content;
5027 gsize pos;
5028 char *etag;
5029 } LoadContentsData;
5032 static void
5033 load_contents_data_free (LoadContentsData *data)
5035 if (data->error)
5036 g_error_free (data->error);
5037 if (data->cancellable)
5038 g_object_unref (data->cancellable);
5039 if (data->content)
5040 g_byte_array_free (data->content, TRUE);
5041 g_free (data->etag);
5042 g_object_unref (data->file);
5043 g_free (data);
5046 static void
5047 load_contents_close_callback (GObject *obj,
5048 GAsyncResult *close_res,
5049 gpointer user_data)
5051 GInputStream *stream = G_INPUT_STREAM (obj);
5052 LoadContentsData *data = user_data;
5053 GSimpleAsyncResult *res;
5055 /* Ignore errors here, we're only reading anyway */
5056 g_input_stream_close_finish (stream, close_res, NULL);
5057 g_object_unref (stream);
5059 res = g_simple_async_result_new (G_OBJECT (data->file),
5060 data->callback,
5061 data->user_data,
5062 g_file_load_contents_async);
5063 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5064 g_simple_async_result_complete (res);
5065 g_object_unref (res);
5068 static void
5069 load_contents_fstat_callback (GObject *obj,
5070 GAsyncResult *stat_res,
5071 gpointer user_data)
5073 GInputStream *stream = G_INPUT_STREAM (obj);
5074 LoadContentsData *data = user_data;
5075 GFileInfo *info;
5077 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5078 stat_res, NULL);
5079 if (info)
5081 data->etag = g_strdup (g_file_info_get_etag (info));
5082 g_object_unref (info);
5085 g_input_stream_close_async (stream, 0,
5086 data->cancellable,
5087 load_contents_close_callback, data);
5090 static void
5091 load_contents_read_callback (GObject *obj,
5092 GAsyncResult *read_res,
5093 gpointer user_data)
5095 GInputStream *stream = G_INPUT_STREAM (obj);
5096 LoadContentsData *data = user_data;
5097 GError *error = NULL;
5098 gssize read_size;
5100 read_size = g_input_stream_read_finish (stream, read_res, &error);
5102 if (read_size < 0)
5104 /* Error or EOF, close the file */
5105 data->error = error;
5106 g_input_stream_close_async (stream, 0,
5107 data->cancellable,
5108 load_contents_close_callback, data);
5110 else if (read_size == 0)
5112 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5113 G_FILE_ATTRIBUTE_ETAG_VALUE,
5115 data->cancellable,
5116 load_contents_fstat_callback,
5117 data);
5119 else if (read_size > 0)
5121 data->pos += read_size;
5123 g_byte_array_set_size (data->content,
5124 data->pos + GET_CONTENT_BLOCK_SIZE);
5127 if (data->read_more_callback &&
5128 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5129 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5130 G_FILE_ATTRIBUTE_ETAG_VALUE,
5132 data->cancellable,
5133 load_contents_fstat_callback,
5134 data);
5135 else
5136 g_input_stream_read_async (stream,
5137 data->content->data + data->pos,
5138 GET_CONTENT_BLOCK_SIZE,
5140 data->cancellable,
5141 load_contents_read_callback,
5142 data);
5146 static void
5147 load_contents_open_callback (GObject *obj,
5148 GAsyncResult *open_res,
5149 gpointer user_data)
5151 GFile *file = G_FILE (obj);
5152 GFileInputStream *stream;
5153 LoadContentsData *data = user_data;
5154 GError *error = NULL;
5155 GSimpleAsyncResult *res;
5157 stream = g_file_read_finish (file, open_res, &error);
5159 if (stream)
5161 g_byte_array_set_size (data->content,
5162 data->pos + GET_CONTENT_BLOCK_SIZE);
5163 g_input_stream_read_async (G_INPUT_STREAM (stream),
5164 data->content->data + data->pos,
5165 GET_CONTENT_BLOCK_SIZE,
5167 data->cancellable,
5168 load_contents_read_callback,
5169 data);
5172 else
5174 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5175 data->callback,
5176 data->user_data,
5177 error);
5178 g_simple_async_result_complete (res);
5179 g_error_free (error);
5180 load_contents_data_free (data);
5181 g_object_unref (res);
5186 * g_file_load_partial_contents_async:
5187 * @file: input #GFile.
5188 * @cancellable: optional #GCancellable object, %NULL to ignore.
5189 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5190 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5191 * @user_data: the data to pass to the callback functions.
5193 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5194 * used to stop reading from the file when appropriate, else this function
5195 * will behave exactly as g_file_load_contents_async(). This operation
5196 * can be finished by g_file_load_partial_contents_finish().
5198 * Users of this function should be aware that @user_data is passed to
5199 * both the @read_more_callback and the @callback.
5201 * If @cancellable is not %NULL, then the operation can be cancelled by
5202 * triggering the cancellable object from another thread. If the operation
5203 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5205 void
5206 g_file_load_partial_contents_async (GFile *file,
5207 GCancellable *cancellable,
5208 GFileReadMoreCallback read_more_callback,
5209 GAsyncReadyCallback callback,
5210 gpointer user_data)
5212 LoadContentsData *data;
5214 g_return_if_fail (G_IS_FILE (file));
5216 data = g_new0 (LoadContentsData, 1);
5218 if (cancellable)
5219 data->cancellable = g_object_ref (cancellable);
5220 data->read_more_callback = read_more_callback;
5221 data->callback = callback;
5222 data->user_data = user_data;
5223 data->content = g_byte_array_new ();
5224 data->file = g_object_ref (file);
5226 g_file_read_async (file,
5228 cancellable,
5229 load_contents_open_callback,
5230 data);
5234 * g_file_load_partial_contents_finish:
5235 * @file: input #GFile.
5236 * @res: a #GAsyncResult.
5237 * @contents: a location to place the contents of the file.
5238 * @length: a location to place the length of the contents of the file.
5239 * @etag_out: a location to place the current entity tag for the file.
5240 * @error: a #GError, or %NULL
5242 * Finishes an asynchronous partial load operation that was started
5243 * with g_file_load_partial_contents_async().
5245 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5246 * present, it will be set appropriately.
5248 gboolean
5249 g_file_load_partial_contents_finish (GFile *file,
5250 GAsyncResult *res,
5251 char **contents,
5252 gsize *length,
5253 char **etag_out,
5254 GError **error)
5256 GSimpleAsyncResult *simple;
5257 LoadContentsData *data;
5259 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5260 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5261 g_return_val_if_fail (contents != NULL, FALSE);
5263 simple = G_SIMPLE_ASYNC_RESULT (res);
5265 if (g_simple_async_result_propagate_error (simple, error))
5266 return FALSE;
5268 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5270 data = g_simple_async_result_get_op_res_gpointer (simple);
5272 if (data->error)
5274 g_propagate_error (error, data->error);
5275 data->error = NULL;
5276 *contents = NULL;
5277 if (length)
5278 *length = 0;
5279 return FALSE;
5282 if (length)
5283 *length = data->pos;
5285 if (etag_out)
5287 *etag_out = data->etag;
5288 data->etag = NULL;
5291 /* Zero terminate */
5292 g_byte_array_set_size (data->content, data->pos + 1);
5293 data->content->data[data->pos] = 0;
5295 *contents = (char *)g_byte_array_free (data->content, FALSE);
5296 data->content = NULL;
5298 return TRUE;
5302 * g_file_load_contents_async:
5303 * @file: input #GFile.
5304 * @cancellable: optional #GCancellable object, %NULL to ignore.
5305 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5306 * @user_data: the data to pass to callback function
5308 * Starts an asynchronous load of the @file's contents.
5310 * For more details, see g_file_load_contents() which is
5311 * the synchronous version of this call.
5313 * When the load operation has completed, @callback will be called
5314 * with @user data. To finish the operation, call
5315 * g_file_load_contents_finish() with the #GAsyncResult returned by
5316 * the @callback.
5318 * If @cancellable is not %NULL, then the operation can be cancelled by
5319 * triggering the cancellable object from another thread. If the operation
5320 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5322 void
5323 g_file_load_contents_async (GFile *file,
5324 GCancellable *cancellable,
5325 GAsyncReadyCallback callback,
5326 gpointer user_data)
5328 g_file_load_partial_contents_async (file,
5329 cancellable,
5330 NULL,
5331 callback, user_data);
5335 * g_file_load_contents_finish:
5336 * @file: input #GFile.
5337 * @res: a #GAsyncResult.
5338 * @contents: a location to place the contents of the file.
5339 * @length: a location to place the length of the contents of the file.
5340 * @etag_out: a location to place the current entity tag for the file.
5341 * @error: a #GError, or %NULL
5343 * Finishes an asynchronous load of the @file's contents.
5344 * The contents are placed in @contents, and @length is set to the
5345 * size of the @contents string. If @etag_out is present, it will be
5346 * set to the new entity tag for the @file.
5348 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5349 * present, it will be set appropriately.
5351 gboolean
5352 g_file_load_contents_finish (GFile *file,
5353 GAsyncResult *res,
5354 char **contents,
5355 gsize *length,
5356 char **etag_out,
5357 GError **error)
5359 return g_file_load_partial_contents_finish (file,
5360 res,
5361 contents,
5362 length,
5363 etag_out,
5364 error);
5368 * g_file_replace_contents:
5369 * @file: input #GFile.
5370 * @contents: a string containing the new contents for @file.
5371 * @length: the length of @contents in bytes.
5372 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5373 * for the document.
5374 * @make_backup: %TRUE if a backup should be created.
5375 * @flags: a set of #GFileCreateFlags.
5376 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5377 * for the document. This should be freed with g_free() when no longer
5378 * needed.
5379 * @cancellable: optional #GCancellable object, %NULL to ignore.
5380 * @error: a #GError, or %NULL
5382 * Replaces the contents of @file with @contents of @length bytes.
5384 * If @etag is specified (not %NULL) any existing file must have that etag, or
5385 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5387 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5389 * If @cancellable is not %NULL, then the operation can be cancelled by
5390 * triggering the cancellable object from another thread. If the operation
5391 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5393 * The returned @new_etag can be used to verify that the file hasn't changed the
5394 * next time it is saved over.
5396 * Returns: %TRUE if successful. If an error
5397 * has occurred, this function will return %FALSE and set @error
5398 * appropriately if present.
5400 gboolean
5401 g_file_replace_contents (GFile *file,
5402 const char *contents,
5403 gsize length,
5404 const char *etag,
5405 gboolean make_backup,
5406 GFileCreateFlags flags,
5407 char **new_etag,
5408 GCancellable *cancellable,
5409 GError **error)
5411 GFileOutputStream *out;
5412 gsize pos, remainder;
5413 gssize res;
5415 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5416 g_return_val_if_fail (contents != NULL, FALSE);
5418 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5419 if (out == NULL)
5420 return FALSE;
5422 pos = 0;
5423 remainder = length;
5424 while (remainder > 0 &&
5425 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5426 contents + pos,
5427 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5428 cancellable,
5429 error)) > 0)
5431 pos += res;
5432 remainder -= res;
5435 if (remainder > 0 && res < 0)
5437 /* Ignore errors on close */
5438 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5440 /* error is set already */
5441 return FALSE;
5444 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5445 return FALSE;
5447 if (new_etag)
5448 *new_etag = g_file_output_stream_get_etag (out);
5450 return TRUE;
5453 typedef struct {
5454 GFile *file;
5455 GError *error;
5456 GCancellable *cancellable;
5457 GAsyncReadyCallback callback;
5458 gpointer user_data;
5459 const char *content;
5460 gsize length;
5461 gsize pos;
5462 char *etag;
5463 } ReplaceContentsData;
5465 static void
5466 replace_contents_data_free (ReplaceContentsData *data)
5468 if (data->error)
5469 g_error_free (data->error);
5470 if (data->cancellable)
5471 g_object_unref (data->cancellable);
5472 g_object_unref (data->file);
5473 g_free (data->etag);
5474 g_free (data);
5477 static void
5478 replace_contents_close_callback (GObject *obj,
5479 GAsyncResult *close_res,
5480 gpointer user_data)
5482 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5483 ReplaceContentsData *data = user_data;
5484 GSimpleAsyncResult *res;
5486 /* Ignore errors here, we're only reading anyway */
5487 g_output_stream_close_finish (stream, close_res, NULL);
5488 g_object_unref (stream);
5490 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5492 res = g_simple_async_result_new (G_OBJECT (data->file),
5493 data->callback,
5494 data->user_data,
5495 g_file_replace_contents_async);
5496 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5497 g_simple_async_result_complete (res);
5498 g_object_unref (res);
5501 static void
5502 replace_contents_write_callback (GObject *obj,
5503 GAsyncResult *read_res,
5504 gpointer user_data)
5506 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5507 ReplaceContentsData *data = user_data;
5508 GError *error = NULL;
5509 gssize write_size;
5511 write_size = g_output_stream_write_finish (stream, read_res, &error);
5513 if (write_size <= 0)
5515 /* Error or EOF, close the file */
5516 if (write_size < 0)
5517 data->error = error;
5518 g_output_stream_close_async (stream, 0,
5519 data->cancellable,
5520 replace_contents_close_callback, data);
5522 else if (write_size > 0)
5524 data->pos += write_size;
5526 if (data->pos >= data->length)
5527 g_output_stream_close_async (stream, 0,
5528 data->cancellable,
5529 replace_contents_close_callback, data);
5530 else
5531 g_output_stream_write_async (stream,
5532 data->content + data->pos,
5533 data->length - data->pos,
5535 data->cancellable,
5536 replace_contents_write_callback,
5537 data);
5541 static void
5542 replace_contents_open_callback (GObject *obj,
5543 GAsyncResult *open_res,
5544 gpointer user_data)
5546 GFile *file = G_FILE (obj);
5547 GFileOutputStream *stream;
5548 ReplaceContentsData *data = user_data;
5549 GError *error = NULL;
5550 GSimpleAsyncResult *res;
5552 stream = g_file_replace_finish (file, open_res, &error);
5554 if (stream)
5556 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5557 data->content + data->pos,
5558 data->length - data->pos,
5560 data->cancellable,
5561 replace_contents_write_callback,
5562 data);
5565 else
5567 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5568 data->callback,
5569 data->user_data,
5570 error);
5571 g_simple_async_result_complete (res);
5572 g_error_free (error);
5573 replace_contents_data_free (data);
5574 g_object_unref (res);
5579 * g_file_replace_contents_async:
5580 * @file: input #GFile.
5581 * @contents: string of contents to replace the file with.
5582 * @length: the length of @contents in bytes.
5583 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5584 * @make_backup: %TRUE if a backup should be created.
5585 * @flags: a set of #GFileCreateFlags.
5586 * @cancellable: optional #GCancellable object, %NULL to ignore.
5587 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5588 * @user_data: the data to pass to callback function
5590 * Starts an asynchronous replacement of @file with the given
5591 * @contents of @length bytes. @etag will replace the document's
5592 * current entity tag.
5594 * When this operation has completed, @callback will be called with
5595 * @user_user data, and the operation can be finalized with
5596 * g_file_replace_contents_finish().
5598 * If @cancellable is not %NULL, then the operation can be cancelled by
5599 * triggering the cancellable object from another thread. If the operation
5600 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5602 * If @make_backup is %TRUE, this function will attempt to
5603 * make a backup of @file.
5605 void
5606 g_file_replace_contents_async (GFile *file,
5607 const char *contents,
5608 gsize length,
5609 const char *etag,
5610 gboolean make_backup,
5611 GFileCreateFlags flags,
5612 GCancellable *cancellable,
5613 GAsyncReadyCallback callback,
5614 gpointer user_data)
5616 ReplaceContentsData *data;
5618 g_return_if_fail (G_IS_FILE (file));
5619 g_return_if_fail (contents != NULL);
5621 data = g_new0 (ReplaceContentsData, 1);
5623 if (cancellable)
5624 data->cancellable = g_object_ref (cancellable);
5625 data->callback = callback;
5626 data->user_data = user_data;
5627 data->content = contents;
5628 data->length = length;
5629 data->pos = 0;
5630 data->file = g_object_ref (file);
5632 g_file_replace_async (file,
5633 etag,
5634 make_backup,
5635 flags,
5637 cancellable,
5638 replace_contents_open_callback,
5639 data);
5643 * g_file_replace_contents_finish:
5644 * @file: input #GFile.
5645 * @res: a #GAsyncResult.
5646 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5647 * for the document. This should be freed with g_free() when it is no
5648 * longer needed.
5649 * @error: a #GError, or %NULL
5651 * Finishes an asynchronous replace of the given @file. See
5652 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5653 * tag for the document, if present.
5655 * Returns: %TRUE on success, %FALSE on failure.
5657 gboolean
5658 g_file_replace_contents_finish (GFile *file,
5659 GAsyncResult *res,
5660 char **new_etag,
5661 GError **error)
5663 GSimpleAsyncResult *simple;
5664 ReplaceContentsData *data;
5666 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5667 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5669 simple = G_SIMPLE_ASYNC_RESULT (res);
5671 if (g_simple_async_result_propagate_error (simple, error))
5672 return FALSE;
5674 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5676 data = g_simple_async_result_get_op_res_gpointer (simple);
5678 if (data->error)
5680 g_propagate_error (error, data->error);
5681 data->error = NULL;
5682 return FALSE;
5686 if (new_etag)
5688 *new_etag = data->etag;
5689 data->etag = NULL; /* Take ownership */
5692 return TRUE;
5695 #define __G_FILE_C__
5696 #include "gioaliasdef.c"