1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
31 #include "gioscheduler.h"
32 #include <glocalfile.h>
33 #include "gsimpleasyncresult.h"
34 #include "gfileattribute-priv.h"
35 #include "gpollfilemonitor.h"
42 * @short_description: File and Directory Handling
44 * @see_also: #GFileInfo, #GFileEnumerator
46 * #GFile is a high level abstraction for manipulating files on a
47 * virtual file system. #GFile<!-- -->s are lightweight, immutable
48 * objects that do no I/O upon creation. It is necessary to understand that
49 * #GFile objects do not represent files, merely a handle to a file. All
50 * file I/O is implemented as streaming operations (see #GInputStream and
53 * To construct a #GFile, you can use:
54 * g_file_new_for_path() if you have a path.
55 * g_file_new_for_uri() if you have a URI.
56 * g_file_new_for_commandline_arg() for a command line argument.
58 * You can move through the filesystem with #GFile handles with
59 * g_file_get_parent() to get a handle to the parent directory.
60 * g_file_get_child() to get a handle to a child within a directory.
61 * g_file_resolve_relative_path() to resolve a relative path between
62 * two #GFile<!-- -->s.
64 * Many #GFile operations have both synchronous and asynchronous versions
65 * to suit your application. Asynchronous versions of synchronous functions
66 * simply have _async() appended to their function names. The asynchronous
67 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
68 * the operation, which is then passed to the function's matching _finish()
71 * Some #GFile operations do not have synchronous analogs, as they may
72 * take a very long time to finish, and blocking may leave an application
73 * unusable. Notable cases include:
74 * g_file_mount_mountable() to mount a mountable file.
75 * g_file_unmount_mountable() to unmount a mountable file.
76 * g_file_eject_mountable() to eject a mountable file.
78 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
79 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
80 * short. Entity tags are somewhat like a more abstract version of the
81 * traditional mtime, and can be used to quickly determine if the file has
82 * been modified from the version on the file system. See the HTTP 1.1
83 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
84 * for HTTP Etag headers, which are a very similar concept.
88 static void g_file_base_init (gpointer g_class);
89 static void g_file_class_init (gpointer g_class,
92 static void g_file_real_query_info_async (GFile *file,
93 const char *attributes,
94 GFileQueryInfoFlags flags,
96 GCancellable *cancellable,
97 GAsyncReadyCallback callback,
99 static GFileInfo * g_file_real_query_info_finish (GFile *file,
102 static void g_file_real_enumerate_children_async (GFile *file,
103 const char *attributes,
104 GFileQueryInfoFlags flags,
106 GCancellable *cancellable,
107 GAsyncReadyCallback callback,
109 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
112 static void g_file_real_read_async (GFile *file,
114 GCancellable *cancellable,
115 GAsyncReadyCallback callback,
117 static GFileInputStream * g_file_real_read_finish (GFile *file,
120 static void g_file_real_append_to_async (GFile *file,
121 GFileCreateFlags flags,
123 GCancellable *cancellable,
124 GAsyncReadyCallback callback,
126 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
129 static void g_file_real_create_async (GFile *file,
130 GFileCreateFlags flags,
132 GCancellable *cancellable,
133 GAsyncReadyCallback callback,
135 static GFileOutputStream *g_file_real_create_finish (GFile *file,
138 static void g_file_real_replace_async (GFile *file,
140 gboolean make_backup,
141 GFileCreateFlags flags,
143 GCancellable *cancellable,
144 GAsyncReadyCallback callback,
146 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
149 static gboolean g_file_real_set_attributes_from_info (GFile *file,
151 GFileQueryInfoFlags flags,
152 GCancellable *cancellable,
154 static void g_file_real_set_display_name_async (GFile *file,
155 const char *display_name,
157 GCancellable *cancellable,
158 GAsyncReadyCallback callback,
160 static GFile * g_file_real_set_display_name_finish (GFile *file,
163 static void g_file_real_set_attributes_async (GFile *file,
165 GFileQueryInfoFlags flags,
167 GCancellable *cancellable,
168 GAsyncReadyCallback callback,
170 static gboolean g_file_real_set_attributes_finish (GFile *file,
176 g_file_get_type (void)
178 static GType file_type = 0;
182 static const GTypeInfo file_info =
184 sizeof (GFileIface), /* class_size */
185 g_file_base_init, /* base_init */
186 NULL, /* base_finalize */
188 NULL, /* class_finalize */
189 NULL, /* class_data */
196 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
199 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
206 g_file_class_init (gpointer g_class,
209 GFileIface *iface = g_class;
211 iface->enumerate_children_async = g_file_real_enumerate_children_async;
212 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
213 iface->set_display_name_async = g_file_real_set_display_name_async;
214 iface->set_display_name_finish = g_file_real_set_display_name_finish;
215 iface->query_info_async = g_file_real_query_info_async;
216 iface->query_info_finish = g_file_real_query_info_finish;
217 iface->set_attributes_async = g_file_real_set_attributes_async;
218 iface->set_attributes_finish = g_file_real_set_attributes_finish;
219 iface->read_async = g_file_real_read_async;
220 iface->read_finish = g_file_real_read_finish;
221 iface->append_to_async = g_file_real_append_to_async;
222 iface->append_to_finish = g_file_real_append_to_finish;
223 iface->create_async = g_file_real_create_async;
224 iface->create_finish = g_file_real_create_finish;
225 iface->replace_async = g_file_real_replace_async;
226 iface->replace_finish = g_file_real_replace_finish;
227 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
231 g_file_base_init (gpointer g_class)
238 * @file: input #GFile.
240 * Checks to see if a file is native to the platform.
242 * A native file s one expressed in the platform-native filename format,
243 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
244 * as it might be on a locally mounted remote filesystem.
246 * On some systems non-native files may be availible using
247 * the native filesystem via a userspace filesystem (FUSE), in
248 * these cases this call will return %FALSE, but g_file_get_path()
249 * will still return a native path.
251 * This call does no blocking i/o.
253 * Returns: %TRUE if file is native.
256 g_file_is_native (GFile *file)
260 g_return_val_if_fail (G_IS_FILE (file), FALSE);
262 iface = G_FILE_GET_IFACE (file);
264 return (* iface->is_native) (file);
269 * g_file_has_uri_scheme:
270 * @file: input #GFile.
271 * @uri_scheme: a string containing a URI scheme.
273 * Checks to see if a #GFile has a given URI scheme.
275 * This call does no blocking i/o.
277 * Returns: %TRUE if #GFile's backend supports the
278 * given URI scheme, %FALSE if URI scheme is %NULL,
279 * not supported, or #GFile is invalid.
282 g_file_has_uri_scheme (GFile *file,
283 const char *uri_scheme)
287 g_return_val_if_fail (G_IS_FILE (file), FALSE);
288 g_return_val_if_fail (uri_scheme != NULL, FALSE);
290 iface = G_FILE_GET_IFACE (file);
292 return (* iface->has_uri_scheme) (file, uri_scheme);
297 * g_file_get_uri_scheme:
298 * @file: input #GFile.
300 * Gets the URI scheme for a #GFile.
301 * RFC 3986 decodes the scheme as:
303 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
305 * Common schemes include "file", "http", "ftp", etc.
307 * This call does no blocking i/o.
309 * Returns: a string containing the URI scheme for the given
310 * #GFile. The returned string should be freed with g_free()
311 * when no longer needed.
314 g_file_get_uri_scheme (GFile *file)
318 g_return_val_if_fail (G_IS_FILE (file), NULL);
320 iface = G_FILE_GET_IFACE (file);
322 return (* iface->get_uri_scheme) (file);
327 * g_file_get_basename:
328 * @file: input #GFile.
330 * Gets the basename (the last component of the path) for a given #GFile.
332 * If called for the toplevel of a system (such as the filesystem root
333 * or a uri like sftp://host/ it will return a single directory separator
334 * (and on Windows, possibly a drive letter).
336 * This call does no blocking i/o.
338 * Returns: string containing the #GFile's base name, or %NULL
339 * if given #GFile is invalid. The returned string should be
340 * freed with g_free() when no longer needed.
343 g_file_get_basename (GFile *file)
347 g_return_val_if_fail (G_IS_FILE (file), NULL);
349 iface = G_FILE_GET_IFACE (file);
351 return (* iface->get_basename) (file);
356 * @file: input #GFile.
358 * Gets the local pathname for #GFile, if one exists.
360 * This call does no blocking i/o.
362 * Returns: string containing the #GFile's path, or %NULL if
363 * no such path exists. The returned string should be
364 * freed with g_free() when no longer needed.
367 g_file_get_path (GFile *file)
371 g_return_val_if_fail (G_IS_FILE (file), NULL);
373 iface = G_FILE_GET_IFACE (file);
375 return (* iface->get_path) (file);
380 * @file: input #GFile.
382 * Gets the URI for the @file.
384 * This call does no blocking i/o.
386 * Returns: a string containing the #GFile's URI.
387 * The returned string should be freed with g_free() when no longer needed.
390 g_file_get_uri (GFile *file)
394 g_return_val_if_fail (G_IS_FILE (file), NULL);
396 iface = G_FILE_GET_IFACE (file);
398 return (* iface->get_uri) (file);
402 * g_file_get_parse_name:
403 * @file: input #GFile.
405 * Gets the parse name of the @file.
406 * A parse name is a UTF-8 string that describes the
407 * file such that one can get the #GFile back using
408 * g_file_parse_name().
410 * This is generally used to show the #GFile as a nice
411 * string in a user interface, like in a location entry.
413 * For local files with names that can safely be converted
414 * to UTF8 the pathname is used, otherwise the IRI is used
415 * (a form of URI that allows UTF8 characters unescaped).
417 * This call does no blocking i/o.
419 * Returns: a string containing the #GFile's parse name. The returned
420 * string should be freed with g_free() when no longer needed.
423 g_file_get_parse_name (GFile *file)
427 g_return_val_if_fail (G_IS_FILE (file), NULL);
429 iface = G_FILE_GET_IFACE (file);
431 return (* iface->get_parse_name) (file);
436 * @file: input #GFile.
438 * Duplicates a #GFile handle. This operation does not duplicate
439 * the actual file or directory represented by the #GFile; see
440 * g_file_copy() if attempting to copy a file.
442 * This call does no blocking i/o.
444 * Returns: #GFile that is a duplicate of the given #GFile.
447 g_file_dup (GFile *file)
451 g_return_val_if_fail (G_IS_FILE (file), NULL);
453 iface = G_FILE_GET_IFACE (file);
455 return (* iface->dup) (file);
460 * @file: #gconstpointer to a #GFile.
462 * Creates a hash value for a #GFile.
464 * This call does no blocking i/o.
466 * Returns: 0 if @file is not a valid #GFile, otherwise an
467 * integer that can be used as hash value for the #GFile.
468 * This function is intended for easily hashing a #GFile to
469 * add to a #GHashTable or similar data structure.
472 g_file_hash (gconstpointer file)
476 g_return_val_if_fail (G_IS_FILE (file), 0);
478 iface = G_FILE_GET_IFACE (file);
480 return (* iface->hash) ((GFile *)file);
485 * @file1: the first #GFile.
486 * @file2: the second #GFile.
488 * Checks equality of two given #GFile<!-- -->s
490 * This call does no blocking i/o.
492 * Returns: %TRUE if @file1 and @file2 are equal.
493 * %FALSE if either is not a #GFile.
496 g_file_equal (GFile *file1,
501 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
502 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
504 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
507 iface = G_FILE_GET_IFACE (file1);
509 return (* iface->equal) (file1, file2);
515 * @file: input #GFile.
517 * Gets the parent directory for the @file.
518 * If the @file represents the root directory of the
519 * file system, then %NULL will be returned.
521 * This call does no blocking i/o.
523 * Returns: a #GFile structure to the parent of the given
524 * #GFile or %NULL if there is no parent.
527 g_file_get_parent (GFile *file)
531 g_return_val_if_fail (G_IS_FILE (file), NULL);
533 iface = G_FILE_GET_IFACE (file);
535 return (* iface->get_parent) (file);
540 * @file: input #GFile.
541 * @name: string containing the child's name.
543 * Gets a specific child of @file with name equal to @name.
545 * Note that the file with that specific name might not exist, but
546 * you can still have a #GFile that points to it. You can use this
547 * for instance to create that file.
549 * This call does no blocking i/o.
551 * Returns: a #GFile to a child specified by @name.
554 g_file_get_child (GFile *file,
557 g_return_val_if_fail (G_IS_FILE (file), NULL);
558 g_return_val_if_fail (name != NULL, NULL);
560 return g_file_resolve_relative_path (file, name);
564 * g_file_get_child_for_display_name:
565 * @file: input #GFile.
566 * @display_name: string to a possible child.
569 * Gets the child of @file for a given @display_name (i.e. a UTF8
570 * version of the name). If this function fails, it returns %NULL and @error will be
571 * set. This is very useful when constructing a GFile for a new file
572 * and the user entered the filename in the user interface, for instance
573 * when you select a directory and type a filename in the file selector.
575 * This call does no blocking i/o.
577 * Returns: a #GFile to the specified child, or
578 * %NULL if the display name couldn't be converted.
581 g_file_get_child_for_display_name (GFile *file,
582 const char *display_name,
587 g_return_val_if_fail (G_IS_FILE (file), NULL);
588 g_return_val_if_fail (display_name != NULL, NULL);
590 iface = G_FILE_GET_IFACE (file);
592 return (* iface->get_child_for_display_name) (file, display_name, error);
596 * g_file_contains_file:
597 * @parent: input #GFile.
598 * @descendant: input #GFile.
600 * Checks whether @parent (recursively) contains the specified @descendent.
602 * This call does no blocking i/o.
604 * Returns: %TRUE if the @descendent's parent, grandparent, etc is @parent. %FALSE otherwise.
607 g_file_contains_file (GFile *parent,
612 g_return_val_if_fail (G_IS_FILE (parent), FALSE);
613 g_return_val_if_fail (G_IS_FILE (descendant), FALSE);
615 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
618 iface = G_FILE_GET_IFACE (parent);
620 return (* iface->contains_file) (parent, descendant);
624 * g_file_get_relative_path:
625 * @parent: input #GFile.
626 * @descendant: input #GFile.
628 * Gets the path for @descendant relative to @parent.
630 * This call does no blocking i/o.
632 * Returns: string with the relative path from @descendant
633 * to @parent, or %NULL if @descendant is not a descendant of @parent. The returned string should be freed with
634 * g_free() when no longer needed.
637 g_file_get_relative_path (GFile *parent,
642 g_return_val_if_fail (G_IS_FILE (parent), NULL);
643 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
645 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
648 iface = G_FILE_GET_IFACE (parent);
650 return (* iface->get_relative_path) (parent, descendant);
654 * g_file_resolve_relative_path:
655 * @file: input #GFile.
656 * @relative_path: a given relative path string.
658 * Resolves a relative path for @file to an absolute path.
660 * This call does no blocking i/o.
662 * Returns: #GFile to the resolved path. %NULL if @relative_path
663 * is %NULL or if @file is invalid.
666 g_file_resolve_relative_path (GFile *file,
667 const char *relative_path)
671 g_return_val_if_fail (G_IS_FILE (file), NULL);
672 g_return_val_if_fail (relative_path != NULL, NULL);
674 iface = G_FILE_GET_IFACE (file);
676 return (* iface->resolve_relative_path) (file, relative_path);
680 * g_file_enumerate_children:
681 * @file: input #GFile.
682 * @attributes: an attribute query string.
683 * @flags: a set of #GFileQueryInfoFlags.
684 * @cancellable: optional #GCancellable object, %NULL to ignore.
685 * @error: #GError for error reporting.
687 * Gets the requested information about the files in a directory. The result
688 * is a #GFileEnumerator object that will give out #GFileInfo objects for
689 * all the files in the directory.
691 * The @attribute value is a string that specifies the file attributes that
692 * should be gathered. It is not an error if its not possible to read a particular
693 * requested attribute from a file, it just won't be set. @attribute should
694 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
695 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
696 * namespace. An example attribute query be "standard::*,owner::user".
697 * The standard attributes are availible as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
699 * If @cancellable is not %NULL, then the operation can be cancelled by
700 * triggering the cancellable object from another thread. If the operation
701 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
703 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
704 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
705 * Other errors are possible too.
707 * Returns: A #GFileEnumerator if successful, %NULL on error.
710 g_file_enumerate_children (GFile *file,
711 const char *attributes,
712 GFileQueryInfoFlags flags,
713 GCancellable *cancellable,
719 g_return_val_if_fail (G_IS_FILE (file), NULL);
721 if (g_cancellable_set_error_if_cancelled (cancellable, error))
724 iface = G_FILE_GET_IFACE (file);
726 if (iface->enumerate_children == NULL)
728 g_set_error (error, G_IO_ERROR,
729 G_IO_ERROR_NOT_SUPPORTED,
730 _("Operation not supported"));
734 return (* iface->enumerate_children) (file, attributes, flags,
739 * g_file_enumerate_children_async:
740 * @file: input #GFile.
741 * @attributes: an attribute query string.
742 * @flags: a set of #GFileQueryInfoFlags.
743 * @io_priority: the <link linkend="io-priority">I/O priority</link>
745 * @cancellable: optional #GCancellable object, %NULL to ignore.
746 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
747 * @user_data: the data to pass to callback function
749 * Asynchronously gets the requested information about the files in a directory. The result
750 * is a #GFileEnumerator object that will give out #GFileInfo objects for
751 * all the files in the directory.
753 * For more details, see g_file_enumerate_children() which is
754 * the synchronous version of this call.
756 * When the operation is finished, @callback will be called. You can then call
757 * g_file_enumerate_children_finish() to get the result of the operation.
760 g_file_enumerate_children_async (GFile *file,
761 const char *attributes,
762 GFileQueryInfoFlags flags,
764 GCancellable *cancellable,
765 GAsyncReadyCallback callback,
770 g_return_if_fail (G_IS_FILE (file));
772 iface = G_FILE_GET_IFACE (file);
773 (* iface->enumerate_children_async) (file,
783 * g_file_enumerate_children_finish:
784 * @file: input #GFile.
785 * @res: a #GAsyncResult.
788 * Finishes an async enumerate children operation.
789 * See g_file_enumerate_children_async().
791 * Returns: a #GFileEnumerator or %NULL if an error occurred.
794 g_file_enumerate_children_finish (GFile *file,
800 g_return_val_if_fail (G_IS_FILE (file), NULL);
801 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
803 if (G_IS_SIMPLE_ASYNC_RESULT (res))
805 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
806 if (g_simple_async_result_propagate_error (simple, error))
810 iface = G_FILE_GET_IFACE (file);
811 return (* iface->enumerate_children_finish) (file, res, error);
817 * @file: input #GFile.
818 * @attributes: an attribute query string.
819 * @flags: a set of #GFileQueryInfoFlags.
820 * @cancellable: optional #GCancellable object, %NULL to ignore.
823 * Gets the requested information about specified @file. The result
824 * is a #GFileInfo objects that contains key-value attributes (like type or size
827 * The @attribute value is a string that specifies the file attributes that
828 * should be gathered. It is not an error if its not possible to read a particular
829 * requested attribute from a file, it just won't be set. @attribute should
830 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
831 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
832 * namespace. An example attribute query be "standard::*,owner::user".
833 * The standard attributes are availible as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
835 * If @cancellable is not %NULL, then the operation can be cancelled by
836 * triggering the cancellable object from another thread. If the operation
837 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
839 * For symlinks, normally the information about the target of the
840 * symlink is returned, rather than information about the symlink itself.
841 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
842 * information about the symlink itself will be returned. Also, for symlinks
843 * that points to non-existing files the information about the symlink itself
846 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
847 * Other errors are possible too, and depend on what kind of filesystem the file is on.
849 * Returns: a #GFileInfo for the given @file, or %NULL on error.
852 g_file_query_info (GFile *file,
853 const char *attributes,
854 GFileQueryInfoFlags flags,
855 GCancellable *cancellable,
860 g_return_val_if_fail (G_IS_FILE (file), NULL);
862 if (g_cancellable_set_error_if_cancelled (cancellable, error))
865 iface = G_FILE_GET_IFACE (file);
867 if (iface->query_info == NULL)
869 g_set_error (error, G_IO_ERROR,
870 G_IO_ERROR_NOT_SUPPORTED,
871 _("Operation not supported"));
875 return (* iface->query_info) (file, attributes, flags, cancellable, error);
879 * g_file_query_info_async:
880 * @file: input #GFile.
881 * @attributes: an attribute query string.
882 * @flags: a set of #GFileQueryInfoFlags.
883 * @io_priority: the <link linkend="io-priority">I/O priority</link>
885 * @cancellable: optional #GCancellable object, %NULL to ignore.
886 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
887 * @user_data: the data to pass to callback function
889 * Asynchronously gets the requested information about specified @file. The result
890 * is a #GFileInfo objects that contains key-value attributes (like type or size
893 * For more details, see g_file_query_info() which is
894 * the synchronous version of this call.
896 * When the operation is finished, @callback will be called. You can then call
897 * g_file_enumerate_children_finish() to get the result of the operation.
900 g_file_query_info_async (GFile *file,
901 const char *attributes,
902 GFileQueryInfoFlags flags,
904 GCancellable *cancellable,
905 GAsyncReadyCallback callback,
910 g_return_if_fail (G_IS_FILE (file));
912 iface = G_FILE_GET_IFACE (file);
913 (* iface->query_info_async) (file,
923 * g_file_query_info_finish:
924 * @file: input #GFile.
925 * @res: a #GAsyncResult.
928 * Finishes an asynchronous file info query.
929 * See g_file_query_info_async().
931 * Returns: #GFileInfo for given @file or %NULL on error.
934 g_file_query_info_finish (GFile *file,
940 g_return_val_if_fail (G_IS_FILE (file), NULL);
941 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
943 if (G_IS_SIMPLE_ASYNC_RESULT (res))
945 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
946 if (g_simple_async_result_propagate_error (simple, error))
950 iface = G_FILE_GET_IFACE (file);
951 return (* iface->query_info_finish) (file, res, error);
955 * g_file_query_filesystem_info:
956 * @file: input #GFile.
957 * @attributes: an attribute query string.
958 * @cancellable: optional #GCancellable object, %NULL to ignore.
961 * Similar to g_file_query_info(), but obtains information
962 * about the filesystem the @file is on, rather than the file itself.
963 * For instance the amount of space availible and the type of
966 * The @attribute value is a string that specifies the file attributes that
967 * should be gathered. It is not an error if its not possible to read a particular
968 * requested attribute from a file, it just won't be set. @attribute should
969 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
970 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
971 * namespace. The standard namespace for filesystem attributes is "fs".
972 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
973 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
974 * bytes availible), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
976 * If @cancellable is not %NULL, then the operation can be cancelled by
977 * triggering the cancellable object from another thread. If the operation
978 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
980 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
981 * Other errors are possible too, and depend on what kind of filesystem the file is on.
983 * Returns: a #GFileInfo or %NULL if there was an error.
986 g_file_query_filesystem_info (GFile *file,
987 const char *attributes,
988 GCancellable *cancellable,
993 g_return_val_if_fail (G_IS_FILE (file), NULL);
995 if (g_cancellable_set_error_if_cancelled (cancellable, error))
998 iface = G_FILE_GET_IFACE (file);
1000 if (iface->query_filesystem_info == NULL)
1002 g_set_error (error, G_IO_ERROR,
1003 G_IO_ERROR_NOT_SUPPORTED,
1004 _("Operation not supported"));
1008 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1012 * g_file_find_enclosing_mount:
1013 * @file: input #GFile.
1014 * @cancellable: optional #GCancellable object, %NULL to ignore.
1015 * @error: a #GError.
1017 * Gets a #GMount for the #GFile.
1019 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1020 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1023 * If @cancellable is not %NULL, then the operation can be cancelled by
1024 * triggering the cancellable object from another thread. If the operation
1025 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1027 * Returns: a #GMount where the @file is located or %NULL on error.
1030 g_file_find_enclosing_mount (GFile *file,
1031 GCancellable *cancellable,
1036 g_return_val_if_fail (G_IS_FILE (file), NULL);
1038 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1041 iface = G_FILE_GET_IFACE (file);
1042 if (iface->find_enclosing_mount == NULL)
1044 g_set_error (error, G_IO_ERROR,
1045 G_IO_ERROR_NOT_FOUND,
1046 _("Containing mount does not exist"));
1050 return (* iface->find_enclosing_mount) (file, cancellable, error);
1055 * @file: #GFile to read.
1056 * @cancellable: a #GCancellable
1057 * @error: a #GError, or %NULL
1059 * Opens a file for reading. The result is a #GFileInputStream that
1060 * can be used to read the contents of the file.
1062 * If @cancellable is not %NULL, then the operation can be cancelled by
1063 * triggering the cancellable object from another thread. If the operation
1064 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1066 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1067 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1068 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1070 * Returns: #GFileInputStream or %NULL on error.
1073 g_file_read (GFile *file,
1074 GCancellable *cancellable,
1079 g_return_val_if_fail (G_IS_FILE (file), NULL);
1081 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1084 iface = G_FILE_GET_IFACE (file);
1086 if (iface->read_fn == NULL)
1088 g_set_error (error, G_IO_ERROR,
1089 G_IO_ERROR_NOT_SUPPORTED,
1090 _("Operation not supported"));
1094 return (* iface->read_fn) (file, cancellable, error);
1099 * @file: input #GFile.
1100 * @flags: a set of #GFileCreateFlags.
1101 * @cancellable: optional #GCancellable object, %NULL to ignore.
1102 * @error: a #GError, or %NULL
1104 * Gets an output stream for appending data to the file. If
1105 * the file doesn't already exist it is created.
1107 * By default files created are generally readable by everyone,
1108 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1109 * will be made readable only to the current user, to the level that
1110 * is supported on the target filesystem.
1112 * If @cancellable is not %NULL, then the operation can be cancelled by
1113 * triggering the cancellable object from another thread. If the operation
1114 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1116 * Some filesystems don't allow all filenames, and may
1117 * return an G_IO_ERROR_INVALID_FILENAME error.
1118 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1119 * returned. Other errors are possible too, and depend on what kind of
1120 * filesystem the file is on.
1122 * Returns: a #GFileOutputStream.
1125 g_file_append_to (GFile *file,
1126 GFileCreateFlags flags,
1127 GCancellable *cancellable,
1132 g_return_val_if_fail (G_IS_FILE (file), NULL);
1134 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1137 iface = G_FILE_GET_IFACE (file);
1139 if (iface->append_to == NULL)
1141 g_set_error (error, G_IO_ERROR,
1142 G_IO_ERROR_NOT_SUPPORTED,
1143 _("Operation not supported"));
1147 return (* iface->append_to) (file, flags, cancellable, error);
1152 * @file: input #GFile.
1153 * @flags: a set of #GFileCreateFlags.
1154 * @cancellable: optional #GCancellable object, %NULL to ignore.
1155 * @error: a #GError, or %NULL
1157 * Creates a new file and returns an output stream for writing to it.
1158 * The file must not already exists.
1160 * By default files created are generally readable by everyone,
1161 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1162 * will be made readable only to the current user, to the level that
1163 * is supported on the target filesystem.
1165 * If @cancellable is not %NULL, then the operation can be cancelled by
1166 * triggering the cancellable object from another thread. If the operation
1167 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1169 * If a file with this name already exists the G_IO_ERROR_EXISTS error
1170 * will be returned. If the file is a directory the G_IO_ERROR_IS_DIRECTORY
1171 * error will be returned.
1172 * Some filesystems don't allow all filenames, and may
1173 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1174 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1175 * Other errors are possible too, and depend on what kind of
1176 * filesystem the file is on.
1178 * Returns: a #GFileOutputStream for the newly created file, or
1182 g_file_create (GFile *file,
1183 GFileCreateFlags flags,
1184 GCancellable *cancellable,
1189 g_return_val_if_fail (G_IS_FILE (file), NULL);
1191 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1194 iface = G_FILE_GET_IFACE (file);
1196 if (iface->create == NULL)
1198 g_set_error (error, G_IO_ERROR,
1199 G_IO_ERROR_NOT_SUPPORTED,
1200 _("Operation not supported"));
1204 return (* iface->create) (file, flags, cancellable, error);
1209 * @file: input #GFile.
1210 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1211 * current #GFile, or #NULL to ignore.
1212 * @make_backup: %TRUE if a backup should be created.
1213 * @flags: a set of #GFileCreateFlags.
1214 * @cancellable: optional #GCancellable object, %NULL to ignore.
1215 * @error: a #GError, or %NULL
1217 * Returns an output stream for overwriting the file, possibly
1218 * creating a backup copy of the file first.
1220 * This will try to replace the file in the safest way possible so
1221 * that any errors during the writing will not affect an already
1222 * existing copy of the file. For instance, for local files it
1223 * may write to a temporary file and then atomically rename over
1224 * the destination when the stream is closed.
1226 * By default files created are generally readable by everyone,
1227 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1228 * will be made readable only to the current user, to the level that
1229 * is supported on the target filesystem.
1231 * If @cancellable is not %NULL, then the operation can be cancelled by
1232 * triggering the cancellable object from another thread. If the operation
1233 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1235 * If you pass in a non-#NULL @etag value, then this value is
1236 * compared to the current entity tag of the file, and if they differ
1237 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1238 * that the file has been changed since you last read it. You can get
1239 * the new etag from g_file_output_stream_get_etag() after you've
1240 * finished writing and closed the #GFileOutputStream. When you load
1241 * a new file you can use g_file_input_stream_query_info() to get
1242 * the etag of the file.
1244 * If @make_backup is %TRUE, this function will attempt to make a backup
1245 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1246 * error will be returned. If you want to replace anyway, try again with
1247 * @make_backup set to %FALSE.
1249 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1250 * and if the file is some other form of non-regular file then a
1251 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1252 * Some filesystems don't allow all filenames, and may
1253 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1254 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1255 * Other errors are possible too, and depend on what kind of
1256 * filesystem the file is on.
1258 * Returns: a #GFileOutputStream or %NULL on error.
1261 g_file_replace (GFile *file,
1263 gboolean make_backup,
1264 GFileCreateFlags flags,
1265 GCancellable *cancellable,
1270 g_return_val_if_fail (G_IS_FILE (file), NULL);
1272 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1275 iface = G_FILE_GET_IFACE (file);
1277 if (iface->replace == NULL)
1279 g_set_error (error, G_IO_ERROR,
1280 G_IO_ERROR_NOT_SUPPORTED,
1281 _("Operation not supported"));
1286 /* Handle empty tag string as NULL in consistent way. */
1287 if (etag && *etag == 0)
1290 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1294 * g_file_read_async:
1295 * @file: input #GFile.
1296 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1298 * @cancellable: optional #GCancellable object, %NULL to ignore.
1299 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1300 * @user_data: the data to pass to callback function
1302 * Asynchronously opens @file for reading.
1304 * For more details, see g_file_read() which is
1305 * the synchronous version of this call.
1307 * When the operation is finished, @callback will be called. You can then call
1308 * g_file_read_finish() to get the result of the operation.
1311 g_file_read_async (GFile *file,
1313 GCancellable *cancellable,
1314 GAsyncReadyCallback callback,
1319 g_return_if_fail (G_IS_FILE (file));
1321 iface = G_FILE_GET_IFACE (file);
1322 (* iface->read_async) (file,
1330 * g_file_read_finish:
1331 * @file: input #GFile.
1332 * @res: a #GAsyncResult.
1333 * @error: a #GError, or %NULL
1335 * Finishes an asynchronous file read operation started with
1336 * g_file_read_async().
1338 * Returns: a #GFileInputStream or %NULL on error.
1341 g_file_read_finish (GFile *file,
1347 g_return_val_if_fail (G_IS_FILE (file), NULL);
1348 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1350 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1352 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1353 if (g_simple_async_result_propagate_error (simple, error))
1357 iface = G_FILE_GET_IFACE (file);
1358 return (* iface->read_finish) (file, res, error);
1362 * g_file_append_to_async:
1363 * @file: input #GFile.
1364 * @flags: a set of #GFileCreateFlags.
1365 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1367 * @cancellable: optional #GCancellable object, %NULL to ignore.
1368 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1369 * @user_data: the data to pass to callback function
1371 * Asynchronously opens @file for appending.
1373 * For more details, see g_file_append_to() which is
1374 * the synchronous version of this call.
1376 * When the operation is finished, @callback will be called. You can then call
1377 * g_file_append_to_finish() to get the result of the operation.
1380 g_file_append_to_async (GFile *file,
1381 GFileCreateFlags flags,
1383 GCancellable *cancellable,
1384 GAsyncReadyCallback callback,
1389 g_return_if_fail (G_IS_FILE (file));
1391 iface = G_FILE_GET_IFACE (file);
1392 (* iface->append_to_async) (file,
1401 * g_file_append_to_finish:
1402 * @file: input #GFile.
1403 * @res: #GAsyncResult
1404 * @error: a #GError, or %NULL
1406 * Finishes an asynchronous file append operation started with
1407 * g_file_append_to_async().
1409 * Returns: a valid #GFileOutputStream or %NULL on error.
1412 g_file_append_to_finish (GFile *file,
1418 g_return_val_if_fail (G_IS_FILE (file), NULL);
1419 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1421 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1423 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1424 if (g_simple_async_result_propagate_error (simple, error))
1428 iface = G_FILE_GET_IFACE (file);
1429 return (* iface->append_to_finish) (file, res, error);
1433 * g_file_create_async:
1434 * @file: input #GFile.
1435 * @flags: a set of #GFileCreateFlags.
1436 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1438 * @cancellable: optional #GCancellable object, %NULL to ignore.
1439 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1440 * @user_data: the data to pass to callback function
1442 * Asynchronously creates a new file and returns an output stream for writing to it.
1443 * The file must not already exists.
1445 * For more details, see g_file_creat() which is
1446 * the synchronous version of this call.
1448 * When the operation is finished, @callback will be called. You can then call
1449 * g_file_create_finish() to get the result of the operation.
1452 g_file_create_async (GFile *file,
1453 GFileCreateFlags flags,
1455 GCancellable *cancellable,
1456 GAsyncReadyCallback callback,
1461 g_return_if_fail (G_IS_FILE (file));
1463 iface = G_FILE_GET_IFACE (file);
1464 (* iface->create_async) (file,
1473 * g_file_create_finish:
1474 * @file: input #GFile.
1475 * @res: a #GAsyncResult.
1476 * @error: a #GError, or %NULL
1478 * Finishes an asynchronous file create operation started with
1479 * g_file_create_async().
1481 * Returns: a #GFileOutputStream or %NULL on error.
1484 g_file_create_finish (GFile *file,
1490 g_return_val_if_fail (G_IS_FILE (file), NULL);
1491 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1493 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1495 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1496 if (g_simple_async_result_propagate_error (simple, error))
1500 iface = G_FILE_GET_IFACE (file);
1501 return (* iface->create_finish) (file, res, error);
1505 * g_file_replace_async:
1506 * @file: input #GFile.
1507 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1509 * @make_backup: a #gboolean.
1510 * @flags: a set of #GFileCreateFlags.
1511 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1513 * @cancellable: optional #GCancellable object, %NULL to ignore.
1514 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1515 * @user_data: the data to pass to callback function
1517 * Asyncronously overwrites the file, replacing the contents, possibly
1518 * creating a backup copy of the file first.
1520 * For more details, see g_file_replace() which is
1521 * the synchronous version of this call.
1523 * When the operation is finished, @callback will be called. You can then call
1524 * g_file_replace_finish() to get the result of the operation.
1527 g_file_replace_async (GFile *file,
1529 gboolean make_backup,
1530 GFileCreateFlags flags,
1532 GCancellable *cancellable,
1533 GAsyncReadyCallback callback,
1538 g_return_if_fail (G_IS_FILE (file));
1540 iface = G_FILE_GET_IFACE (file);
1541 (* iface->replace_async) (file,
1552 * g_file_replace_finish:
1553 * @file: input #GFile.
1554 * @res: a #GAsyncResult.
1555 * @error: a #GError, or %NULL
1557 * Finishes an asynchronous file replace operation started with
1558 * g_file_replace_async().
1560 * Returns: a #GFileOutputStream, or %NULL on error.
1563 g_file_replace_finish (GFile *file,
1569 g_return_val_if_fail (G_IS_FILE (file), NULL);
1570 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1572 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1574 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1575 if (g_simple_async_result_propagate_error (simple, error))
1579 iface = G_FILE_GET_IFACE (file);
1580 return (* iface->replace_finish) (file, res, error);
1584 copy_symlink (GFile *destination,
1585 GFileCopyFlags flags,
1586 GCancellable *cancellable,
1591 gboolean tried_delete;
1593 GFileType file_type;
1595 tried_delete = FALSE;
1599 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1601 /* Maybe it already existed, and we want to overwrite? */
1602 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1603 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1605 g_error_free (my_error);
1608 /* Don't overwrite if the destination is a directory */
1609 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1610 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1611 cancellable, &my_error);
1614 file_type = g_file_info_get_file_type (info);
1615 g_object_unref (info);
1617 if (file_type == G_FILE_TYPE_DIRECTORY)
1619 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1620 _("Can't copy over directory"));
1625 if (!g_file_delete (destination, cancellable, error))
1628 tried_delete = TRUE;
1632 g_propagate_error (error, my_error);
1639 static GInputStream *
1640 open_source_for_copy (GFile *source,
1642 GFileCopyFlags flags,
1643 GCancellable *cancellable,
1649 GFileType file_type;
1652 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1656 /* There was an error opening the source, try to set a good error for it: */
1658 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1660 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1661 * as that is less useful to the app. Better check for errors on the
1664 g_error_free (my_error);
1667 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1668 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1669 cancellable, &my_error);
1672 file_type = g_file_info_get_file_type (info);
1673 g_object_unref (info);
1675 if (flags & G_FILE_COPY_OVERWRITE)
1677 if (file_type == G_FILE_TYPE_DIRECTORY)
1679 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1680 _("Can't copy directory over directory"));
1683 /* continue to would_recurse error */
1687 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1688 _("Target file exists"));
1694 /* Error getting info from target, return that error
1695 * (except for NOT_FOUND, which is no error here)
1697 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1699 g_propagate_error (error, my_error);
1702 g_error_free (my_error);
1705 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1706 _("Can't recursively copy directory"));
1710 g_propagate_error (error, my_error);
1715 should_copy (GFileAttributeInfo *info,
1719 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1720 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1724 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1725 GFileAttributeInfoList *namespaces,
1733 s = g_string_new ("");
1737 for (i = 0; i < attributes->n_infos; i++)
1739 if (should_copy (&attributes->infos[i], as_move))
1744 g_string_append_c (s, ',');
1746 g_string_append (s, attributes->infos[i].name);
1753 for (i = 0; i < namespaces->n_infos; i++)
1755 if (should_copy (&namespaces->infos[i], as_move))
1760 g_string_append_c (s, ',');
1762 g_string_append (s, namespaces->infos[i].name);
1763 g_string_append (s, ":*");
1768 return g_string_free (s, FALSE);
1772 * g_file_copy_attributes:
1773 * @source: a #GFile with attributes.
1774 * @destination: a #GFile to copy attributes to.
1775 * @flags: a set of #GFileCopyFlags.
1776 * @cancellable: optional #GCancellable object, %NULL to ignore.
1777 * @error: a #GError, %NULL to ignore.
1779 * Copies the file attributes from @source to @destination.
1781 * Normally only a subset of the file attributes are copied,
1782 * those that are copies in a normal file copy operation
1783 * (which for instance does not include e.g. mtime). However
1784 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
1785 * all the metadata that is possible to copy is copied.
1787 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1790 g_file_copy_attributes (GFile *source,
1792 GFileCopyFlags flags,
1793 GCancellable *cancellable,
1796 GFileAttributeInfoList *attributes, *namespaces;
1797 char *attrs_to_read;
1801 gboolean source_nofollow_symlinks;
1803 as_move = flags & G_FILE_COPY_ALL_METADATA;
1804 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1806 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1807 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1808 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1810 if (attributes == NULL && namespaces == NULL)
1813 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1815 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1816 * we just don't copy it.
1818 info = g_file_query_info (source, attrs_to_read,
1819 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1823 g_free (attrs_to_read);
1828 res = g_file_set_attributes_from_info (destination,
1832 g_object_unref (info);
1835 g_file_attribute_info_list_unref (attributes);
1836 g_file_attribute_info_list_unref (namespaces);
1841 /* Closes the streams */
1843 copy_stream_with_progress (GInputStream *in,
1845 GCancellable *cancellable,
1846 GFileProgressCallback progress_callback,
1847 gpointer progress_callback_data,
1850 gssize n_read, n_written;
1851 goffset current_size;
1852 char buffer[8192], *p;
1858 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1859 G_FILE_ATTRIBUTE_STANDARD_SIZE,
1863 total_size = g_file_info_get_size (info);
1864 g_object_unref (info);
1871 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
1881 current_size += n_read;
1886 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
1887 if (n_written == -1)
1894 n_read -= n_written;
1900 if (progress_callback)
1901 progress_callback (current_size, total_size, progress_callback_data);
1905 error = NULL; /* Ignore further errors */
1907 /* Make sure we send full copied size */
1908 if (progress_callback)
1909 progress_callback (current_size, total_size, progress_callback_data);
1912 /* Don't care about errors in source here */
1913 g_input_stream_close (in, cancellable, NULL);
1915 /* But write errors on close are bad! */
1916 if (!g_output_stream_close (out, cancellable, error))
1919 g_object_unref (in);
1920 g_object_unref (out);
1926 file_copy_fallback (GFile *source,
1928 GFileCopyFlags flags,
1929 GCancellable *cancellable,
1930 GFileProgressCallback progress_callback,
1931 gpointer progress_callback_data,
1939 /* Maybe copy the symlink? */
1940 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
1942 info = g_file_query_info (source,
1943 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
1944 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1950 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
1951 (target = g_file_info_get_symlink_target (info)) != NULL)
1953 if (!copy_symlink (destination, flags, cancellable, target, error))
1955 g_object_unref (info);
1959 g_object_unref (info);
1963 g_object_unref (info);
1966 in = open_source_for_copy (source, destination, flags, cancellable, error);
1970 if (flags & G_FILE_COPY_OVERWRITE)
1972 out = (GOutputStream *)g_file_replace (destination,
1974 flags & G_FILE_COPY_BACKUP,
1975 cancellable, error);
1979 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
1984 g_object_unref (in);
1988 if (!copy_stream_with_progress (in, out, cancellable,
1989 progress_callback, progress_callback_data,
1995 /* Ignore errors here. Failure to copy metadata is not a hard error */
1996 g_file_copy_attributes (source, destination,
1997 flags, cancellable, NULL);
2004 * @source: input #GFile.
2005 * @destination: destination #GFile
2006 * @flags: set of #GFileCopyFlags
2007 * @cancellable: optional #GCancellable object, %NULL to ignore.
2008 * @progress_callback: function to callback with progress information
2009 * @progress_callback_data: userdata to pass to @progress_callback
2010 * @error: #GError to set on error, or %NULL
2012 * Copies the file @source to the location specified by @destination.
2013 * Can not handle recursive copies of directories.
2015 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2016 * existing @destination file is overwritten.
2018 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2019 * will be copied as symlinks, otherwise the target of the
2020 * @source symlink will be copied.
2022 * If @cancellable is not %NULL, then the operation can be cancelled by
2023 * triggering the cancellable object from another thread. If the operation
2024 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2026 * If @progress_callback is not %NULL, then the operation can be monitored by
2027 * setting this to a #GFileProgressCallback function. @progress_callback_data
2028 * will be passed to this function. It is guaranteed that this callback will
2029 * be called after all data has been transfered with the total number of bytes
2030 * copied during the operation.
2032 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2033 * error is returned, independent on the status of the @destination.
2035 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2036 * error G_IO_ERROR_EXISTS is returned.
2038 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2039 * error is returned. If trying to overwrite a directory with a directory the
2040 * G_IO_ERROR_WOULD_MERGE error is returned.
2042 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2043 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2046 * If you are interested in copying the #GFile object itself (not the on-disk
2047 * file), see g_file_dup().
2049 * Returns: %TRUE on success, %FALSE otherwise.
2052 g_file_copy (GFile *source,
2054 GFileCopyFlags flags,
2055 GCancellable *cancellable,
2056 GFileProgressCallback progress_callback,
2057 gpointer progress_callback_data,
2064 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2065 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2067 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2070 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
2072 iface = G_FILE_GET_IFACE (source);
2077 res = (* iface->copy) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
2082 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2084 g_propagate_error (error, my_error);
2090 return file_copy_fallback (source, destination, flags, cancellable,
2091 progress_callback, progress_callback_data,
2098 * @source: #GFile pointing to the source location.
2099 * @destination: #GFile pointing to the destination location.
2100 * @flags: set of #GFileCopyFlags.
2101 * @cancellable: optional #GCancellable object, %NULL to ignore.
2102 * @progress_callback: #GFileProgressCallback function for updates.
2103 * @progress_callback_data: gpointer to user data for the callback function.
2104 * @error: #GError for returning error conditions, or %NULL
2107 * Tries to move the file or directory @source to the location specified by @destination.
2108 * If native move operations is supported then this is used, otherwise a copy + delete
2109 * fallback is used. The native implementation may support moving directories (for instance
2110 * on moves inside the same filesystem), but the fallback code does not.
2112 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2113 * existing @destination file is overwritten.
2115 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2116 * will be copied as symlinks, otherwise the target of the
2117 * @source symlink will be copied.
2119 * If @cancellable is not %NULL, then the operation can be cancelled by
2120 * triggering the cancellable object from another thread. If the operation
2121 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2123 * If @progress_callback is not %NULL, then the operation can be monitored by
2124 * setting this to a #GFileProgressCallback function. @progress_callback_data
2125 * will be passed to this function. It is guaranteed that this callback will
2126 * be called after all data has been transfered with the total number of bytes
2127 * copied during the operation.
2129 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2130 * error is returned, independent on the status of the @destination.
2132 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2133 * error G_IO_ERROR_EXISTS is returned.
2135 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2136 * error is returned. If trying to overwrite a directory with a directory the
2137 * G_IO_ERROR_WOULD_MERGE error is returned.
2139 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2140 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2141 * may be returned (if the native move operation isn't availible).
2143 * Returns: %TRUE on successful move, %FALSE otherwise.
2146 g_file_move (GFile *source,
2148 GFileCopyFlags flags,
2149 GCancellable *cancellable,
2150 GFileProgressCallback progress_callback,
2151 gpointer progress_callback_data,
2158 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2159 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2161 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2164 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
2166 iface = G_FILE_GET_IFACE (source);
2171 res = (* iface->move) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
2176 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2178 g_propagate_error (error, my_error);
2184 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2186 g_set_error (error, G_IO_ERROR,
2187 G_IO_ERROR_NOT_SUPPORTED,
2188 _("Operation not supported"));
2192 flags |= G_FILE_COPY_ALL_METADATA;
2193 if (!g_file_copy (source, destination, flags, cancellable,
2194 progress_callback, progress_callback_data,
2198 return g_file_delete (source, cancellable, error);
2202 * g_file_make_directory
2203 * @file: input #GFile.
2204 * @cancellable: optional #GCancellable object, %NULL to ignore.
2205 * @error: a #GError, or %NULL
2207 * Creates a directory.
2209 * If @cancellable is not %NULL, then the operation can be cancelled by
2210 * triggering the cancellable object from another thread. If the operation
2211 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2213 * Returns: %TRUE on successful creation, %FALSE otherwise.
2216 g_file_make_directory (GFile *file,
2217 GCancellable *cancellable,
2222 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2224 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2227 iface = G_FILE_GET_IFACE (file);
2229 if (iface->make_directory == NULL)
2231 g_set_error (error, G_IO_ERROR,
2232 G_IO_ERROR_NOT_SUPPORTED,
2233 _("Operation not supported"));
2237 return (* iface->make_directory) (file, cancellable, error);
2241 * g_file_make_symbolic_link:
2242 * @file: input #GFile.
2243 * @symlink_value: a string with the value of the new symlink.
2244 * @cancellable: optional #GCancellable object, %NULL to ignore.
2245 * @error: a #GError.
2247 * Creates a symbolic link.
2249 * If @cancellable is not %NULL, then the operation can be cancelled by
2250 * triggering the cancellable object from another thread. If the operation
2251 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2253 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2256 g_file_make_symbolic_link (GFile *file,
2257 const char *symlink_value,
2258 GCancellable *cancellable,
2263 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2264 g_return_val_if_fail (symlink_value != NULL, FALSE);
2266 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2269 if (*symlink_value == '\0')
2271 g_set_error (error, G_IO_ERROR,
2272 G_IO_ERROR_INVALID_ARGUMENT,
2273 _("Invalid symlink value given"));
2277 iface = G_FILE_GET_IFACE (file);
2279 if (iface->make_symbolic_link == NULL)
2281 g_set_error (error, G_IO_ERROR,
2282 G_IO_ERROR_NOT_SUPPORTED,
2283 _("Operation not supported"));
2287 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2292 * @file: input #GFile.
2293 * @cancellable: optional #GCancellable object, %NULL to ignore.
2294 * @error: a #GError, or %NULL
2298 * If @cancellable is not %NULL, then the operation can be cancelled by
2299 * triggering the cancellable object from another thread. If the operation
2300 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2302 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2305 g_file_delete (GFile *file,
2306 GCancellable *cancellable,
2311 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2313 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2316 iface = G_FILE_GET_IFACE (file);
2318 if (iface->delete_file == NULL)
2320 g_set_error (error, G_IO_ERROR,
2321 G_IO_ERROR_NOT_SUPPORTED,
2322 _("Operation not supported"));
2326 return (* iface->delete_file) (file, cancellable, error);
2331 * @file: #GFile to send to trash.
2332 * @cancellable: optional #GCancellable object, %NULL to ignore.
2333 * @error: a #GError, or %NULL
2335 * Sends @file to the "Trashcan", if possible. This is similar to
2336 * deleting it, but the user can recover it before emptying the trashcan.
2337 * Not all filesystems support trashing, so this call can return the
2338 * %G_IO_ERROR_NOT_SUPPORTED error.
2341 * If @cancellable is not %NULL, then the operation can be cancelled by
2342 * triggering the cancellable object from another thread. If the operation
2343 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2345 * Returns: %TRUE on successful trash, %FALSE otherwise.
2348 g_file_trash (GFile *file,
2349 GCancellable *cancellable,
2354 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2356 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2359 iface = G_FILE_GET_IFACE (file);
2361 if (iface->trash == NULL)
2364 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2365 _("Trash not supported"));
2369 return (* iface->trash) (file, cancellable, error);
2373 * g_file_set_display_name:
2374 * @file: input #GFile.
2375 * @display_name: a string.
2376 * @cancellable: optional #GCancellable object, %NULL to ignore.
2377 * @error: a #GError, or %NULL
2379 * Renames @file to the specified display name.
2381 * The display name is converted from UTF8 to the correct encoding for the target
2382 * filesystem if possible and the @file is renamed to this.
2384 * If you want to implement a rename operation in the user interface the edit name
2385 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2386 * widget, and then the result after editing should be passed to g_file_set_display_name().
2388 * On success the resulting converted filename is returned.
2390 * If @cancellable is not %NULL, then the operation can be cancelled by
2391 * triggering the cancellable object from another thread. If the operation
2392 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2394 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2397 g_file_set_display_name (GFile *file,
2398 const char *display_name,
2399 GCancellable *cancellable,
2404 g_return_val_if_fail (G_IS_FILE (file), NULL);
2405 g_return_val_if_fail (display_name != NULL, NULL);
2407 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2411 G_IO_ERROR_INVALID_ARGUMENT,
2412 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2416 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2419 iface = G_FILE_GET_IFACE (file);
2421 return (* iface->set_display_name) (file, display_name, cancellable, error);
2425 * g_file_set_display_name_async:
2426 * @file: input #GFile.
2427 * @display_name: a string.
2428 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2430 * @cancellable: optional #GCancellable object, %NULL to ignore.
2431 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2432 * @user_data: the data to pass to callback function
2434 * Asynchronously sets the display name for a given #GFile.
2435 * For the synchronous version of this function, see g_file_set_display_name().
2438 g_file_set_display_name_async (GFile *file,
2439 const char *display_name,
2441 GCancellable *cancellable,
2442 GAsyncReadyCallback callback,
2447 g_return_if_fail (G_IS_FILE (file));
2448 g_return_if_fail (display_name != NULL);
2450 iface = G_FILE_GET_IFACE (file);
2451 (* iface->set_display_name_async) (file,
2460 * g_file_set_display_name_finish:
2461 * @file: input #GFile.
2462 * @res: a #GAsyncResult.
2463 * @error: a #GError, or %NULL
2465 * Finishes setting a display name started with
2466 * g_file_set_display_name_async().
2468 * Returns: a #GFile or %NULL on error.
2471 g_file_set_display_name_finish (GFile *file,
2477 g_return_val_if_fail (G_IS_FILE (file), NULL);
2478 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2480 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2482 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2483 if (g_simple_async_result_propagate_error (simple, error))
2487 iface = G_FILE_GET_IFACE (file);
2488 return (* iface->set_display_name_finish) (file, res, error);
2492 * g_file_query_settable_attributes:
2493 * @file: input #GFile.
2494 * @cancellable: optional #GCancellable object, %NULL to ignore.
2495 * @error: a #GError, or %NULL
2497 * Obtain the list of settable attributes for the file.
2499 * Returns the type and full attribute name of all the attributes
2500 * that can be set on this file. This doesn't mean setting it will always
2501 * succeed though, you might get an access failure, or some specific
2502 * file may not support a specific attribute.
2504 * If @cancellable is not %NULL, then the operation can be cancelled by
2505 * triggering the cancellable object from another thread. If the operation
2506 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2508 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2509 * When you are done with it, release it with g_file_attribute_info_list_unref()
2511 GFileAttributeInfoList *
2512 g_file_query_settable_attributes (GFile *file,
2513 GCancellable *cancellable,
2518 GFileAttributeInfoList *list;
2520 g_return_val_if_fail (G_IS_FILE (file), NULL);
2522 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2525 iface = G_FILE_GET_IFACE (file);
2527 if (iface->query_settable_attributes == NULL)
2528 return g_file_attribute_info_list_new ();
2531 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2535 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2537 list = g_file_attribute_info_list_new ();
2538 g_error_free (my_error);
2541 g_propagate_error (error, my_error);
2548 * g_file_query_writable_namespaces:
2549 * @file: input #GFile.
2550 * @cancellable: optional #GCancellable object, %NULL to ignore.
2551 * @error: a #GError, or %NULL
2553 * Obtain the list of attribute namespaces where new attributes
2554 * can be created by a user. An example of this is extended
2555 * attributes (in the "xattr" namespace).
2557 * If @cancellable is not %NULL, then the operation can be cancelled by
2558 * triggering the cancellable object from another thread. If the operation
2559 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2561 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2562 * When you are done with it, release it with g_file_attribute_info_list_unref()
2564 GFileAttributeInfoList *
2565 g_file_query_writable_namespaces (GFile *file,
2566 GCancellable *cancellable,
2571 GFileAttributeInfoList *list;
2573 g_return_val_if_fail (G_IS_FILE (file), NULL);
2575 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2578 iface = G_FILE_GET_IFACE (file);
2580 if (iface->query_writable_namespaces == NULL)
2581 return g_file_attribute_info_list_new ();
2584 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2588 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2590 list = g_file_attribute_info_list_new ();
2591 g_error_free (my_error);
2594 g_propagate_error (error, my_error);
2601 * g_file_set_attribute:
2602 * @file: input #GFile.
2603 * @attribute: a string containing the attribute's name.
2604 * @type: The type of the attribute
2605 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2606 * @flags: a set of #GFileQueryInfoFlags.
2607 * @cancellable: optional #GCancellable object, %NULL to ignore.
2608 * @error: a #GError, or %NULL
2610 * Sets an attribute in the file with attribute name @attribute to @value.
2612 * If @cancellable is not %NULL, then the operation can be cancelled by
2613 * triggering the cancellable object from another thread. If the operation
2614 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2616 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2619 g_file_set_attribute (GFile *file,
2620 const char *attribute,
2621 GFileAttributeType type,
2623 GFileQueryInfoFlags flags,
2624 GCancellable *cancellable,
2629 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2630 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2632 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2635 iface = G_FILE_GET_IFACE (file);
2637 if (iface->set_attribute == NULL)
2639 g_set_error (error, G_IO_ERROR,
2640 G_IO_ERROR_NOT_SUPPORTED,
2641 _("Operation not supported"));
2645 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2649 * g_file_set_attributes_from_info:
2650 * @file: input #GFile.
2651 * @info: a #GFileInfo.
2652 * @flags: #GFileQueryInfoFlags
2653 * @cancellable: optional #GCancellable object, %NULL to ignore.
2654 * @error: a #GError, or %NULL
2656 * Tries to set all attributes in the #GFileInfo on the target values,
2657 * not stopping on the first error.
2659 * If there is any error during this operation then @error will be set to
2660 * the first error. Error on particular fields are flagged by setting
2661 * the "status" field in the attribute value to
2662 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2665 * If @cancellable is not %NULL, then the operation can be cancelled by
2666 * triggering the cancellable object from another thread. If the operation
2667 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2669 * Returns: %TRUE if there was any error, %FALSE otherwise.
2672 g_file_set_attributes_from_info (GFile *file,
2674 GFileQueryInfoFlags flags,
2675 GCancellable *cancellable,
2680 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2681 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2683 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2686 g_file_info_clear_status (info);
2688 iface = G_FILE_GET_IFACE (file);
2690 return (* iface->set_attributes_from_info) (file,
2699 g_file_real_set_attributes_from_info (GFile *file,
2701 GFileQueryInfoFlags flags,
2702 GCancellable *cancellable,
2708 GFileAttributeValue *value;
2712 attributes = g_file_info_list_attributes (info, NULL);
2714 for (i = 0; attributes[i] != NULL; i++)
2716 value = _g_file_info_get_attribute_value (info, attributes[i]);
2718 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2721 if (!g_file_set_attribute (file, attributes[i],
2722 value->type, _g_file_attribute_value_peek_as_pointer (value),
2723 flags, cancellable, error))
2725 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2727 /* Don't set error multiple times */
2731 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2734 g_strfreev (attributes);
2740 * g_file_set_attributes_async:
2741 * @file: input #GFile.
2742 * @info: a #GFileInfo.
2743 * @flags: a #GFileQueryInfoFlags.
2744 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2746 * @cancellable: optional #GCancellable object, %NULL to ignore.
2747 * @callback: a #GAsyncReadyCallback.
2748 * @user_data: a #gpointer.
2750 * Asynchronously sets the attributes of @file with @info.
2751 * For the synchronous version of this function, see g_file_set_attributes().
2754 g_file_set_attributes_async (GFile *file,
2756 GFileQueryInfoFlags flags,
2758 GCancellable *cancellable,
2759 GAsyncReadyCallback callback,
2764 g_return_if_fail (G_IS_FILE (file));
2765 g_return_if_fail (G_IS_FILE_INFO (info));
2767 iface = G_FILE_GET_IFACE (file);
2768 (* iface->set_attributes_async) (file,
2778 * g_file_set_attributes_finish:
2779 * @file: input #GFile.
2780 * @result: a #GAsyncResult.
2781 * @info: a #GFileInfo.
2782 * @error: a #GError, or %NULL
2784 * Finishes setting an attribute started in g_file_set_attributes_async().
2786 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2789 g_file_set_attributes_finish (GFile *file,
2790 GAsyncResult *result,
2796 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2797 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2799 /* No standard handling of errors here, as we must set info even
2802 iface = G_FILE_GET_IFACE (file);
2803 return (* iface->set_attributes_finish) (file, result, info, error);
2807 * g_file_set_attribute_string:
2808 * @file: input #GFile.
2809 * @attribute: a string containing the attribute's name.
2810 * @value: a string containing the attribute's value.
2811 * @flags: #GFileQueryInfoFlags.
2812 * @cancellable: optional #GCancellable object, %NULL to ignore.
2813 * @error: a #GError, or %NULL
2815 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
2816 * If @attribute is of a different type, this operation will fail.
2818 * If @cancellable is not %NULL, then the operation can be cancelled by
2819 * triggering the cancellable object from another thread. If the operation
2820 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2822 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2825 g_file_set_attribute_string (GFile *file,
2826 const char *attribute,
2828 GFileQueryInfoFlags flags,
2829 GCancellable *cancellable,
2832 return g_file_set_attribute (file, attribute,
2833 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
2834 flags, cancellable, error);
2838 * g_file_set_attribute_byte_string:
2839 * @file: input #GFile.
2840 * @attribute: a string containing the attribute's name.
2841 * @value: a string containing the attribute's new value.
2842 * @flags: a #GFileQueryInfoFlags.
2843 * @cancellable: optional #GCancellable object, %NULL to ignore.
2844 * @error: a #GError, or %NULL
2846 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
2847 * If @attribute is of a different type, this operation will fail,
2850 * If @cancellable is not %NULL, then the operation can be cancelled by
2851 * triggering the cancellable object from another thread. If the operation
2852 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2854 * Returns: %TRUE if the @attribute was successfully set to @value
2855 * in the @file, %FALSE otherwise.
2858 g_file_set_attribute_byte_string (GFile *file,
2859 const char *attribute,
2861 GFileQueryInfoFlags flags,
2862 GCancellable *cancellable,
2865 return g_file_set_attribute (file, attribute,
2866 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
2867 flags, cancellable, error);
2871 * g_file_set_attribute_uint32:
2872 * @file: input #GFile.
2873 * @attribute: a string containing the attribute's name.
2874 * @value: a #guint32 containing the attribute's new value.
2875 * @flags: a #GFileQueryInfoFlags.
2876 * @cancellable: optional #GCancellable object, %NULL to ignore.
2877 * @error: a #GError, or %NULL
2879 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
2880 * If @attribute is of a different type, this operation will fail.
2882 * If @cancellable is not %NULL, then the operation can be cancelled by
2883 * triggering the cancellable object from another thread. If the operation
2884 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2886 * Returns: %TRUE if the @attribute was successfully set to @value
2887 * in the @file, %FALSE otherwise.
2890 g_file_set_attribute_uint32 (GFile *file,
2891 const char *attribute,
2893 GFileQueryInfoFlags flags,
2894 GCancellable *cancellable,
2897 return g_file_set_attribute (file, attribute,
2898 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
2899 flags, cancellable, error);
2903 * g_file_set_attribute_int32:
2904 * @file: input #GFile.
2905 * @attribute: a string containing the attribute's name.
2906 * @value: a #gint32 containing the attribute's new value.
2907 * @flags: a #GFileQueryInfoFlags.
2908 * @cancellable: optional #GCancellable object, %NULL to ignore.
2909 * @error: a #GError, or %NULL
2911 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
2912 * If @attribute is of a different type, this operation will fail.
2914 * If @cancellable is not %NULL, then the operation can be cancelled by
2915 * triggering the cancellable object from another thread. If the operation
2916 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2918 * Returns: %TRUE if the @attribute was successfully set to @value
2919 * in the @file, %FALSE otherwise.
2922 g_file_set_attribute_int32 (GFile *file,
2923 const char *attribute,
2925 GFileQueryInfoFlags flags,
2926 GCancellable *cancellable,
2929 return g_file_set_attribute (file, attribute,
2930 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
2931 flags, cancellable, error);
2935 * g_file_set_attribute_uint64:
2936 * @file: input #GFile.
2937 * @attribute: a string containing the attribute's name.
2938 * @value: a #guint64 containing the attribute's new value.
2939 * @flags: a #GFileQueryInfoFlags.
2940 * @cancellable: optional #GCancellable object, %NULL to ignore.
2941 * @error: a #GError, or %NULL
2943 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
2944 * If @attribute is of a different type, this operation will fail.
2946 * If @cancellable is not %NULL, then the operation can be cancelled by
2947 * triggering the cancellable object from another thread. If the operation
2948 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2950 * Returns: %TRUE if the @attribute was successfully set to @value
2951 * in the @file, %FALSE otherwise.
2954 g_file_set_attribute_uint64 (GFile *file,
2955 const char *attribute,
2957 GFileQueryInfoFlags flags,
2958 GCancellable *cancellable,
2961 return g_file_set_attribute (file, attribute,
2962 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
2963 flags, cancellable, error);
2967 * g_file_set_attribute_int64:
2968 * @file: input #GFile.
2969 * @attribute: a string containing the attribute's name.
2970 * @value: a #guint64 containing the attribute's new value.
2971 * @flags: a #GFileQueryInfoFlags.
2972 * @cancellable: optional #GCancellable object, %NULL to ignore.
2973 * @error: a #GError, or %NULL
2975 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
2976 * If @attribute is of a different type, this operation will fail.
2978 * If @cancellable is not %NULL, then the operation can be cancelled by
2979 * triggering the cancellable object from another thread. If the operation
2980 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2982 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2985 g_file_set_attribute_int64 (GFile *file,
2986 const char *attribute,
2988 GFileQueryInfoFlags flags,
2989 GCancellable *cancellable,
2992 return g_file_set_attribute (file, attribute,
2993 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
2994 flags, cancellable, error);
2998 * g_file_mount_mountable:
2999 * @file: input #GFile.
3000 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3001 * @cancellable: optional #GCancellable object, %NULL to ignore.
3002 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3003 * @user_data: the data to pass to callback function
3005 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3006 * You can speciy using @mount_operation to get callbacks when for instance
3007 * passwords are needed during authentication.
3009 * If @cancellable is not %NULL, then the operation can be cancelled by
3010 * triggering the cancellable object from another thread. If the operation
3011 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3013 * When the operation is finished, @callback will be called. You can then call
3014 * g_file_mount_mountable_finish() to get the result of the operation.
3017 g_file_mount_mountable (GFile *file,
3018 GMountOperation *mount_operation,
3019 GCancellable *cancellable,
3020 GAsyncReadyCallback callback,
3025 g_return_if_fail (G_IS_FILE (file));
3027 iface = G_FILE_GET_IFACE (file);
3029 if (iface->mount_mountable == NULL)
3030 g_simple_async_report_error_in_idle (G_OBJECT (file),
3034 G_IO_ERROR_NOT_SUPPORTED,
3035 _("Operation not supported"));
3037 (* iface->mount_mountable) (file,
3045 * g_file_mount_mountable_finish:
3046 * @file: input #GFile.
3047 * @result: a #GAsyncResult.
3048 * @error: a #GError, or %NULL
3050 * Finishes a mount operation. See g_file_mount_mountable() for details.
3052 * Finish an asynchronous mount operation that was started
3053 * with g_file_mount_mountable().
3055 * Returns: a #GFile or %NULL on error.
3058 g_file_mount_mountable_finish (GFile *file,
3059 GAsyncResult *result,
3064 g_return_val_if_fail (G_IS_FILE (file), NULL);
3065 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3067 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3069 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3070 if (g_simple_async_result_propagate_error (simple, error))
3074 iface = G_FILE_GET_IFACE (file);
3075 return (* iface->mount_mountable_finish) (file, result, error);
3079 * g_file_unmount_mountable:
3080 * @file: input #GFile.
3081 * @flags: flags affecting the operation
3082 * @cancellable: optional #GCancellable object, %NULL to ignore.
3083 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3084 * @user_data: the data to pass to callback function
3086 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3088 * If @cancellable is not %NULL, then the operation can be cancelled by
3089 * triggering the cancellable object from another thread. If the operation
3090 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3092 * When the operation is finished, @callback will be called. You can then call
3093 * g_file_mount_mountable_finish() to get the result of the operation.
3096 g_file_unmount_mountable (GFile *file,
3097 GMountUnmountFlags flags,
3098 GCancellable *cancellable,
3099 GAsyncReadyCallback callback,
3104 g_return_if_fail (G_IS_FILE (file));
3106 iface = G_FILE_GET_IFACE (file);
3108 if (iface->unmount_mountable == NULL)
3109 g_simple_async_report_error_in_idle (G_OBJECT (file),
3113 G_IO_ERROR_NOT_SUPPORTED,
3114 _("Operation not supported"));
3116 (* iface->unmount_mountable) (file,
3124 * g_file_unmount_mountable_finish:
3125 * @file: input #GFile.
3126 * @result: a #GAsyncResult.
3127 * @error: a #GError, or %NULL
3129 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3131 * Finish an asynchronous unmount operation that was started
3132 * with g_file_unmount_mountable().
3134 * Returns: %TRUE if the operation finished successfully. %FALSE
3138 g_file_unmount_mountable_finish (GFile *file,
3139 GAsyncResult *result,
3144 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3145 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3147 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3149 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3150 if (g_simple_async_result_propagate_error (simple, error))
3154 iface = G_FILE_GET_IFACE (file);
3155 return (* iface->unmount_mountable_finish) (file, result, error);
3159 * g_file_eject_mountable:
3160 * @file: input #GFile.
3161 * @flags: flags affecting the operation
3162 * @cancellable: optional #GCancellable object, %NULL to ignore.
3163 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3164 * @user_data: the data to pass to callback function
3166 * Starts an asynchronous eject on a mountable.
3167 * When this operation has completed, @callback will be called with
3168 * @user_user data, and the operation can be finalized with
3169 * g_file_eject_mountable_finish().
3171 * If @cancellable is not %NULL, then the operation can be cancelled by
3172 * triggering the cancellable object from another thread. If the operation
3173 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3176 g_file_eject_mountable (GFile *file,
3177 GMountUnmountFlags flags,
3178 GCancellable *cancellable,
3179 GAsyncReadyCallback callback,
3184 g_return_if_fail (G_IS_FILE (file));
3186 iface = G_FILE_GET_IFACE (file);
3188 if (iface->eject_mountable == NULL)
3189 g_simple_async_report_error_in_idle (G_OBJECT (file),
3193 G_IO_ERROR_NOT_SUPPORTED,
3194 _("Operation not supported"));
3196 (* iface->eject_mountable) (file,
3204 * g_file_eject_mountable_finish:
3205 * @file: input #GFile.
3206 * @result: a #GAsyncResult.
3207 * @error: a #GError, or %NULL
3209 * Finishes an asynchronous eject operation started by
3210 * g_file_eject_mountable().
3212 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3216 g_file_eject_mountable_finish (GFile *file,
3217 GAsyncResult *result,
3222 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3223 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3225 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3227 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3228 if (g_simple_async_result_propagate_error (simple, error))
3232 iface = G_FILE_GET_IFACE (file);
3233 return (* iface->eject_mountable_finish) (file, result, error);
3237 * g_file_monitor_directory:
3238 * @file: input #GFile.
3239 * @flags: a set of #GFileMonitorFlags.
3240 * @cancellable: optional #GCancellable object, %NULL to ignore.
3242 * Obtains a directory monitor for the given file.
3243 * This may fail if directory monitoring is not supported.
3245 * If @cancellable is not %NULL, then the operation can be cancelled by
3246 * triggering the cancellable object from another thread. If the operation
3247 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3249 * Returns: a #GDirectoryMonitor for the given @file,
3250 * or %NULL on error.
3253 g_file_monitor_directory (GFile *file,
3254 GFileMonitorFlags flags,
3255 GCancellable *cancellable)
3259 g_return_val_if_fail (G_IS_FILE (file), NULL);
3261 iface = G_FILE_GET_IFACE (file);
3263 if (iface->monitor_dir == NULL)
3266 return (* iface->monitor_dir) (file, flags, cancellable);
3270 * g_file_monitor_file:
3271 * @file: input #GFile.
3272 * @flags: a set of #GFileMonitorFlags.
3273 * @cancellable: optional #GCancellable object, %NULL to ignore.
3275 * Obtains a file monitor for the given file. If no file notification
3276 * mechanism exists, then regular polling of the file is used.
3278 * If @cancellable is not %NULL, then the operation can be cancelled by
3279 * triggering the cancellable object from another thread. If the operation
3280 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3282 * Returns: a #GFileMonitor for the given @file.
3285 g_file_monitor_file (GFile *file,
3286 GFileMonitorFlags flags,
3287 GCancellable *cancellable)
3290 GFileMonitor *monitor;
3292 g_return_val_if_fail (G_IS_FILE (file), NULL);
3294 iface = G_FILE_GET_IFACE (file);
3298 if (iface->monitor_file)
3299 monitor = (* iface->monitor_file) (file, flags, cancellable);
3301 /* Fallback to polling */
3302 if (monitor == NULL)
3303 monitor = _g_poll_file_monitor_new (file);
3308 /********************************************
3309 * Default implementation of async ops *
3310 ********************************************/
3314 GFileQueryInfoFlags flags;
3316 } QueryInfoAsyncData;
3319 query_info_data_free (QueryInfoAsyncData *data)
3322 g_object_unref (data->info);
3323 g_free (data->attributes);
3328 query_info_async_thread (GSimpleAsyncResult *res,
3330 GCancellable *cancellable)
3332 GError *error = NULL;
3333 QueryInfoAsyncData *data;
3336 data = g_simple_async_result_get_op_res_gpointer (res);
3338 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3342 g_simple_async_result_set_from_error (res, error);
3343 g_error_free (error);
3350 g_file_real_query_info_async (GFile *file,
3351 const char *attributes,
3352 GFileQueryInfoFlags flags,
3354 GCancellable *cancellable,
3355 GAsyncReadyCallback callback,
3358 GSimpleAsyncResult *res;
3359 QueryInfoAsyncData *data;
3361 data = g_new0 (QueryInfoAsyncData, 1);
3362 data->attributes = g_strdup (attributes);
3363 data->flags = flags;
3365 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3366 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3368 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3369 g_object_unref (res);
3373 g_file_real_query_info_finish (GFile *file,
3377 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3378 QueryInfoAsyncData *data;
3380 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3382 data = g_simple_async_result_get_op_res_gpointer (simple);
3384 return g_object_ref (data->info);
3391 GFileQueryInfoFlags flags;
3392 GFileEnumerator *enumerator;
3393 } EnumerateChildrenAsyncData;
3396 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3398 if (data->enumerator)
3399 g_object_unref (data->enumerator);
3400 g_free (data->attributes);
3405 enumerate_children_async_thread (GSimpleAsyncResult *res,
3407 GCancellable *cancellable)
3409 GError *error = NULL;
3410 EnumerateChildrenAsyncData *data;
3411 GFileEnumerator *enumerator;
3413 data = g_simple_async_result_get_op_res_gpointer (res);
3415 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3417 if (enumerator == NULL)
3419 g_simple_async_result_set_from_error (res, error);
3420 g_error_free (error);
3423 data->enumerator = enumerator;
3427 g_file_real_enumerate_children_async (GFile *file,
3428 const char *attributes,
3429 GFileQueryInfoFlags flags,
3431 GCancellable *cancellable,
3432 GAsyncReadyCallback callback,
3435 GSimpleAsyncResult *res;
3436 EnumerateChildrenAsyncData *data;
3438 data = g_new0 (EnumerateChildrenAsyncData, 1);
3439 data->attributes = g_strdup (attributes);
3440 data->flags = flags;
3442 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3443 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3445 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3446 g_object_unref (res);
3449 static GFileEnumerator *
3450 g_file_real_enumerate_children_finish (GFile *file,
3454 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3455 EnumerateChildrenAsyncData *data;
3457 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3459 data = g_simple_async_result_get_op_res_gpointer (simple);
3460 if (data->enumerator)
3461 return g_object_ref (data->enumerator);
3467 open_read_async_thread (GSimpleAsyncResult *res,
3469 GCancellable *cancellable)
3472 GFileInputStream *stream;
3473 GError *error = NULL;
3475 iface = G_FILE_GET_IFACE (object);
3477 stream = iface->read_fn (G_FILE (object), cancellable, &error);
3481 g_simple_async_result_set_from_error (res, error);
3482 g_error_free (error);
3485 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3489 g_file_real_read_async (GFile *file,
3491 GCancellable *cancellable,
3492 GAsyncReadyCallback callback,
3495 GSimpleAsyncResult *res;
3497 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3499 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3500 g_object_unref (res);
3503 static GFileInputStream *
3504 g_file_real_read_finish (GFile *file,
3508 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3511 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3513 op = g_simple_async_result_get_op_res_gpointer (simple);
3515 return g_object_ref (op);
3521 append_to_async_thread (GSimpleAsyncResult *res,
3523 GCancellable *cancellable)
3526 GFileCreateFlags *data;
3527 GFileOutputStream *stream;
3528 GError *error = NULL;
3530 iface = G_FILE_GET_IFACE (object);
3532 data = g_simple_async_result_get_op_res_gpointer (res);
3534 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3538 g_simple_async_result_set_from_error (res, error);
3539 g_error_free (error);
3542 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3546 g_file_real_append_to_async (GFile *file,
3547 GFileCreateFlags flags,
3549 GCancellable *cancellable,
3550 GAsyncReadyCallback callback,
3553 GFileCreateFlags *data;
3554 GSimpleAsyncResult *res;
3556 data = g_new0 (GFileCreateFlags, 1);
3559 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3560 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3562 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3563 g_object_unref (res);
3566 static GFileOutputStream *
3567 g_file_real_append_to_finish (GFile *file,
3571 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3574 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3576 op = g_simple_async_result_get_op_res_gpointer (simple);
3578 return g_object_ref (op);
3584 create_async_thread (GSimpleAsyncResult *res,
3586 GCancellable *cancellable)
3589 GFileCreateFlags *data;
3590 GFileOutputStream *stream;
3591 GError *error = NULL;
3593 iface = G_FILE_GET_IFACE (object);
3595 data = g_simple_async_result_get_op_res_gpointer (res);
3597 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3601 g_simple_async_result_set_from_error (res, error);
3602 g_error_free (error);
3605 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3609 g_file_real_create_async (GFile *file,
3610 GFileCreateFlags flags,
3612 GCancellable *cancellable,
3613 GAsyncReadyCallback callback,
3616 GFileCreateFlags *data;
3617 GSimpleAsyncResult *res;
3619 data = g_new0 (GFileCreateFlags, 1);
3622 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3623 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3625 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3626 g_object_unref (res);
3629 static GFileOutputStream *
3630 g_file_real_create_finish (GFile *file,
3634 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3637 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3639 op = g_simple_async_result_get_op_res_gpointer (simple);
3641 return g_object_ref (op);
3647 GFileOutputStream *stream;
3649 gboolean make_backup;
3650 GFileCreateFlags flags;
3654 replace_async_data_free (ReplaceAsyncData *data)
3657 g_object_unref (data->stream);
3658 g_free (data->etag);
3663 replace_async_thread (GSimpleAsyncResult *res,
3665 GCancellable *cancellable)
3668 GFileOutputStream *stream;
3669 GError *error = NULL;
3670 ReplaceAsyncData *data;
3672 iface = G_FILE_GET_IFACE (object);
3674 data = g_simple_async_result_get_op_res_gpointer (res);
3676 stream = iface->replace (G_FILE (object),
3685 g_simple_async_result_set_from_error (res, error);
3686 g_error_free (error);
3689 data->stream = stream;
3693 g_file_real_replace_async (GFile *file,
3695 gboolean make_backup,
3696 GFileCreateFlags flags,
3698 GCancellable *cancellable,
3699 GAsyncReadyCallback callback,
3702 GSimpleAsyncResult *res;
3703 ReplaceAsyncData *data;
3705 data = g_new0 (ReplaceAsyncData, 1);
3706 data->etag = g_strdup (etag);
3707 data->make_backup = make_backup;
3708 data->flags = flags;
3710 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3711 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3713 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3714 g_object_unref (res);
3717 static GFileOutputStream *
3718 g_file_real_replace_finish (GFile *file,
3722 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3723 ReplaceAsyncData *data;
3725 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3727 data = g_simple_async_result_get_op_res_gpointer (simple);
3729 return g_object_ref (data->stream);
3737 } SetDisplayNameAsyncData;
3740 set_display_name_data_free (SetDisplayNameAsyncData *data)
3742 g_free (data->name);
3744 g_object_unref (data->file);
3749 set_display_name_async_thread (GSimpleAsyncResult *res,
3751 GCancellable *cancellable)
3753 GError *error = NULL;
3754 SetDisplayNameAsyncData *data;
3757 data = g_simple_async_result_get_op_res_gpointer (res);
3759 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3763 g_simple_async_result_set_from_error (res, error);
3764 g_error_free (error);
3771 g_file_real_set_display_name_async (GFile *file,
3772 const char *display_name,
3774 GCancellable *cancellable,
3775 GAsyncReadyCallback callback,
3778 GSimpleAsyncResult *res;
3779 SetDisplayNameAsyncData *data;
3781 data = g_new0 (SetDisplayNameAsyncData, 1);
3782 data->name = g_strdup (display_name);
3784 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3785 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3787 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3788 g_object_unref (res);
3792 g_file_real_set_display_name_finish (GFile *file,
3796 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3797 SetDisplayNameAsyncData *data;
3799 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3801 data = g_simple_async_result_get_op_res_gpointer (simple);
3803 return g_object_ref (data->file);
3809 GFileQueryInfoFlags flags;
3816 set_info_data_free (SetInfoAsyncData *data)
3819 g_object_unref (data->info);
3821 g_error_free (data->error);
3826 set_info_async_thread (GSimpleAsyncResult *res,
3828 GCancellable *cancellable)
3830 SetInfoAsyncData *data;
3832 data = g_simple_async_result_get_op_res_gpointer (res);
3835 data->res = g_file_set_attributes_from_info (G_FILE (object),
3843 g_file_real_set_attributes_async (GFile *file,
3845 GFileQueryInfoFlags flags,
3847 GCancellable *cancellable,
3848 GAsyncReadyCallback callback,
3851 GSimpleAsyncResult *res;
3852 SetInfoAsyncData *data;
3854 data = g_new0 (SetInfoAsyncData, 1);
3855 data->info = g_file_info_dup (info);
3856 data->flags = flags;
3858 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
3859 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
3861 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
3862 g_object_unref (res);
3866 g_file_real_set_attributes_finish (GFile *file,
3871 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3872 SetInfoAsyncData *data;
3874 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
3876 data = g_simple_async_result_get_op_res_gpointer (simple);
3879 *info = g_object_ref (data->info);
3881 if (error != NULL && data->error)
3882 *error = g_error_copy (data->error);
3887 /********************************************
3888 * Default VFS operations *
3889 ********************************************/
3892 * g_file_new_for_path:
3893 * @path: a string containing a relative or absolute path.
3895 * Constructs a #GFile for a given path. This operation never
3896 * fails, but the returned object might not support any I/O
3897 * operation if @path is malformed.
3899 * Returns: a new #GFile for the given @path.
3902 g_file_new_for_path (const char *path)
3904 g_return_val_if_fail (path != NULL, NULL);
3906 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
3910 * g_file_new_for_uri:
3911 * @uri: a string containing a URI.
3913 * Constructs a #GFile for a given URI. This operation never
3914 * fails, but the returned object might not support any I/O
3915 * operation if @uri is malformed or if the uri type is
3918 * Returns: a #GFile for the given @uri.
3921 g_file_new_for_uri (const char *uri)
3923 g_return_val_if_fail (uri != NULL, NULL);
3925 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
3929 * g_file_parse_name:
3930 * @parse_name: a file name or path to be parsed.
3932 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
3933 * This operation never fails, but the returned object might not support any I/O
3934 * operation if the @parse_name cannot be parsed.
3936 * Returns: a new #GFile.
3939 g_file_parse_name (const char *parse_name)
3941 g_return_val_if_fail (parse_name != NULL, NULL);
3943 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
3947 is_valid_scheme_character (char c)
3949 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
3953 has_valid_scheme (const char *uri)
3959 if (!is_valid_scheme_character (*p))
3964 } while (is_valid_scheme_character (*p));
3970 * g_file_new_for_commandline_arg:
3971 * @arg: a command line string.
3973 * Creates a #GFile with the given argument from
3976 * Returns: a new #GFile.
3979 g_file_new_for_commandline_arg (const char *arg)
3985 g_return_val_if_fail (arg != NULL, NULL);
3987 if (g_path_is_absolute (arg))
3988 return g_file_new_for_path (arg);
3990 if (has_valid_scheme (arg))
3991 return g_file_new_for_uri (arg);
3993 current_dir = g_get_current_dir ();
3994 filename = g_build_filename (current_dir, arg, NULL);
3995 g_free (current_dir);
3997 file = g_file_new_for_path (filename);
4004 * g_file_mount_enclosing_volume:
4005 * @location: input #GFile.
4006 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4007 * @cancellable: optional #GCancellable object, %NULL to ignore.
4008 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4009 * @user_data: the data to pass to callback function
4011 * Starts a @mount_operation, mounting the volume that contains the file @location.
4013 * When this operation has completed, @callback will be called with
4014 * @user_user data, and the operation can be finalized with
4015 * g_file_mount_enclosing_volume_finish().
4017 * If @cancellable is not %NULL, then the operation can be cancelled by
4018 * triggering the cancellable object from another thread. If the operation
4019 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4022 g_file_mount_enclosing_volume (GFile *location,
4023 GMountOperation *mount_operation,
4024 GCancellable *cancellable,
4025 GAsyncReadyCallback callback,
4030 g_return_if_fail (G_IS_FILE (location));
4032 iface = G_FILE_GET_IFACE (location);
4034 if (iface->mount_enclosing_volume == NULL)
4036 g_simple_async_report_error_in_idle (G_OBJECT (location),
4037 callback, user_data,
4038 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4039 _("volume doesn't implement mount"));
4044 (* iface->mount_enclosing_volume) (location, mount_operation, cancellable, callback, user_data);
4049 * g_file_mount_enclosing_volume_finish:
4050 * @location: input #GFile.
4051 * @result: a #GAsyncResult.
4052 * @error: a #GError, or %NULL
4054 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4056 * Returns: %TRUE if successful. If an error
4057 * has occured, this function will return %FALSE and set @error
4058 * appropriately if present.
4061 g_file_mount_enclosing_volume_finish (GFile *location,
4062 GAsyncResult *result,
4067 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4068 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4070 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4072 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4073 if (g_simple_async_result_propagate_error (simple, error))
4077 iface = G_FILE_GET_IFACE (location);
4079 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4082 /********************************************
4083 * Utility functions *
4084 ********************************************/
4086 #define GET_CONTENT_BLOCK_SIZE 8192
4089 * g_file_load_contents:
4090 * @file: input #GFile.
4091 * @cancellable: optional #GCancellable object, %NULL to ignore.
4092 * @contents: a location to place the contents of the file.
4093 * @length: a location to place the length of the contents of the file.
4094 * @etag_out: a location to place the current entity tag for the file.
4095 * @error: a #GError, or %NULL
4097 * Loads the content of the file into memory, returning the size of
4098 * the data. The data is always zero terminated, but this is not
4099 * included in the resultant @length.
4101 * If @cancellable is not %NULL, then the operation can be cancelled by
4102 * triggering the cancellable object from another thread. If the operation
4103 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4105 * Returns: %TRUE if the @file's contents were successfully loaded.
4106 * %FALSE if there were errors..
4109 g_file_load_contents (GFile *file,
4110 GCancellable *cancellable,
4116 GFileInputStream *in;
4117 GByteArray *content;
4122 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4123 g_return_val_if_fail (contents != NULL, FALSE);
4125 in = g_file_read (file, cancellable, error);
4129 content = g_byte_array_new ();
4132 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4133 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4134 content->data + pos,
4135 GET_CONTENT_BLOCK_SIZE,
4136 cancellable, error)) > 0)
4139 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4146 info = g_file_input_stream_query_info (in,
4147 G_FILE_ATTRIBUTE_ETAG_VALUE,
4152 *etag_out = g_strdup (g_file_info_get_etag (info));
4153 g_object_unref (info);
4157 /* Ignore errors on close */
4158 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4159 g_object_unref (in);
4163 /* error is set already */
4164 g_byte_array_free (content, TRUE);
4171 /* Zero terminate (we got an extra byte allocated for this */
4172 content->data[pos] = 0;
4174 *contents = (char *)g_byte_array_free (content, FALSE);
4182 GCancellable *cancellable;
4183 GFileReadMoreCallback read_more_callback;
4184 GAsyncReadyCallback callback;
4186 GByteArray *content;
4193 load_contents_data_free (LoadContentsData *data)
4196 g_error_free (data->error);
4197 if (data->cancellable)
4198 g_object_unref (data->cancellable);
4200 g_byte_array_free (data->content, TRUE);
4201 g_free (data->etag);
4202 g_object_unref (data->file);
4207 load_contents_close_callback (GObject *obj,
4208 GAsyncResult *close_res,
4211 GInputStream *stream = G_INPUT_STREAM (obj);
4212 LoadContentsData *data = user_data;
4213 GSimpleAsyncResult *res;
4215 /* Ignore errors here, we're only reading anyway */
4216 g_input_stream_close_finish (stream, close_res, NULL);
4217 g_object_unref (stream);
4219 res = g_simple_async_result_new (G_OBJECT (data->file),
4222 g_file_load_contents_async);
4223 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4224 g_simple_async_result_complete (res);
4225 g_object_unref (res);
4229 load_contents_fstat_callback (GObject *obj,
4230 GAsyncResult *stat_res,
4233 GInputStream *stream = G_INPUT_STREAM (obj);
4234 LoadContentsData *data = user_data;
4237 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4241 data->etag = g_strdup (g_file_info_get_etag (info));
4242 g_object_unref (info);
4245 g_input_stream_close_async (stream, 0,
4247 load_contents_close_callback, data);
4251 load_contents_read_callback (GObject *obj,
4252 GAsyncResult *read_res,
4255 GInputStream *stream = G_INPUT_STREAM (obj);
4256 LoadContentsData *data = user_data;
4257 GError *error = NULL;
4260 read_size = g_input_stream_read_finish (stream, read_res, &error);
4264 /* Error or EOF, close the file */
4265 data->error = error;
4266 g_input_stream_close_async (stream, 0,
4268 load_contents_close_callback, data);
4270 else if (read_size == 0)
4272 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4273 G_FILE_ATTRIBUTE_ETAG_VALUE,
4276 load_contents_fstat_callback,
4279 else if (read_size > 0)
4281 data->pos += read_size;
4283 g_byte_array_set_size (data->content,
4284 data->pos + GET_CONTENT_BLOCK_SIZE);
4287 if (data->read_more_callback &&
4288 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4289 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4290 G_FILE_ATTRIBUTE_ETAG_VALUE,
4293 load_contents_fstat_callback,
4296 g_input_stream_read_async (stream,
4297 data->content->data + data->pos,
4298 GET_CONTENT_BLOCK_SIZE,
4301 load_contents_read_callback,
4307 load_contents_open_callback (GObject *obj,
4308 GAsyncResult *open_res,
4311 GFile *file = G_FILE (obj);
4312 GFileInputStream *stream;
4313 LoadContentsData *data = user_data;
4314 GError *error = NULL;
4315 GSimpleAsyncResult *res;
4317 stream = g_file_read_finish (file, open_res, &error);
4321 g_byte_array_set_size (data->content,
4322 data->pos + GET_CONTENT_BLOCK_SIZE);
4323 g_input_stream_read_async (G_INPUT_STREAM (stream),
4324 data->content->data + data->pos,
4325 GET_CONTENT_BLOCK_SIZE,
4328 load_contents_read_callback,
4334 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4338 g_simple_async_result_complete (res);
4339 g_error_free (error);
4340 load_contents_data_free (data);
4341 g_object_unref (res);
4346 * g_file_load_partial_contents_async:
4347 * @file: input #GFile.
4348 * @cancellable: optional #GCancellable object, %NULL to ignore.
4349 * @read_more_callback: a #GFileReadMoreCallback.
4350 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4351 * @user_data: the data to pass to the callback functions.
4353 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
4354 * used to stop reading from the file when appropriate, else this function
4355 * will behave exactly as g_file_load_contents_async(). This operation
4356 * can be finished by g_file_load_partial_contents_finish().
4358 * Users of this function should be aware that @user_data is passed to
4359 * both the @read_more_callback and the @callback.
4361 * If @cancellable is not %NULL, then the operation can be cancelled by
4362 * triggering the cancellable object from another thread. If the operation
4363 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4366 g_file_load_partial_contents_async (GFile *file,
4367 GCancellable *cancellable,
4368 GFileReadMoreCallback read_more_callback,
4369 GAsyncReadyCallback callback,
4372 LoadContentsData *data;
4374 g_return_if_fail (G_IS_FILE (file));
4376 data = g_new0 (LoadContentsData, 1);
4379 data->cancellable = g_object_ref (cancellable);
4380 data->read_more_callback = read_more_callback;
4381 data->callback = callback;
4382 data->user_data = user_data;
4383 data->content = g_byte_array_new ();
4384 data->file = g_object_ref (file);
4386 g_file_read_async (file,
4389 load_contents_open_callback,
4394 * g_file_load_partial_contents_finish:
4395 * @file: input #GFile.
4396 * @res: a #GAsyncResult.
4397 * @contents: a location to place the contents of the file.
4398 * @length: a location to place the length of the contents of the file.
4399 * @etag_out: a location to place the current entity tag for the file.
4400 * @error: a #GError, or %NULL
4402 * Finishes an asynchronous partial load operation that was started
4403 * with g_file_load_partial_contents_async().
4405 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4406 * present, it will be set appropriately.
4409 g_file_load_partial_contents_finish (GFile *file,
4416 GSimpleAsyncResult *simple;
4417 LoadContentsData *data;
4419 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4420 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4421 g_return_val_if_fail (contents != NULL, FALSE);
4423 simple = G_SIMPLE_ASYNC_RESULT (res);
4425 if (g_simple_async_result_propagate_error (simple, error))
4428 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4430 data = g_simple_async_result_get_op_res_gpointer (simple);
4434 g_propagate_error (error, data->error);
4443 *length = data->pos;
4447 *etag_out = data->etag;
4451 /* Zero terminate */
4452 g_byte_array_set_size (data->content, data->pos + 1);
4453 data->content->data[data->pos] = 0;
4455 *contents = (char *)g_byte_array_free (data->content, FALSE);
4456 data->content = NULL;
4462 * g_file_load_contents_async:
4463 * @file: input #GFile.
4464 * @cancellable: optional #GCancellable object, %NULL to ignore.
4465 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4466 * @user_data: the data to pass to callback function
4468 * Starts an asynchronous load of the @file's contents.
4469 * When the load operation has completed, @callback will be called
4470 * with @userdata. To finish the operation, call
4471 * g_file_load_contents_finish() with the #GAsyncResult returned by
4474 * If @cancellable is not %NULL, then the operation can be cancelled by
4475 * triggering the cancellable object from another thread. If the operation
4476 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4479 g_file_load_contents_async (GFile *file,
4480 GCancellable *cancellable,
4481 GAsyncReadyCallback callback,
4484 g_file_load_partial_contents_async (file,
4487 callback, user_data);
4491 * g_file_load_contents_finish:
4492 * @file: input #GFile.
4493 * @res: a #GAsyncResult.
4494 * @contents: a location to place the contents of the file.
4495 * @length: a location to place the length of the contents of the file.
4496 * @etag_out: a location to place the current entity tag for the file.
4497 * @error: a #GError, or %NULL
4499 * Finishes an asynchronous load of the @file's contents.
4500 * The contents are placed in @contents, and @length is set to the
4501 * size of the @contents string. If @etag_out is present, it will be
4502 * set to the new entity tag for the @file.
4504 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4505 * present, it will be set appropriately.
4508 g_file_load_contents_finish (GFile *file,
4515 return g_file_load_partial_contents_finish (file,
4524 * g_file_replace_contents:
4525 * @file: input #GFile.
4526 * @contents: a string containing the new contents for @file.
4527 * @length: the length of @contents in bytes.
4528 * @etag: the old <link linkend="gfile-etag">entity tag</link>
4530 * @make_backup: a #gboolean.
4531 * @flags: a set of #GFileCreateFlags.
4532 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4534 * @cancellable: optional #GCancellable object, %NULL to ignore.
4535 * @error: a #GError, or %NULL
4537 * Replaces the contents of @file with @contents of @length bytes.
4539 * If @etag is specified (not %NULL) any existing file must have that etag, or
4540 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
4542 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
4544 * If @cancellable is not %NULL, then the operation can be cancelled by
4545 * triggering the cancellable object from another thread. If the operation
4546 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4548 * The returned @new_etag can be used to verify that the file hasn't changed the
4549 * next time it is saved over.
4551 * Returns: %TRUE if successful. If an error
4552 * has occured, this function will return %FALSE and set @error
4553 * appropriately if present.
4556 g_file_replace_contents (GFile *file,
4557 const char *contents,
4560 gboolean make_backup,
4561 GFileCreateFlags flags,
4563 GCancellable *cancellable,
4566 GFileOutputStream *out;
4567 gsize pos, remainder;
4570 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4571 g_return_val_if_fail (contents != NULL, FALSE);
4573 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4579 while (remainder > 0 &&
4580 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4582 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4590 if (remainder > 0 && res < 0)
4592 /* Ignore errors on close */
4593 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4595 /* error is set already */
4599 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4603 *new_etag = g_file_output_stream_get_etag (out);
4611 GCancellable *cancellable;
4612 GAsyncReadyCallback callback;
4614 const char *content;
4618 } ReplaceContentsData;
4621 replace_contents_data_free (ReplaceContentsData *data)
4624 g_error_free (data->error);
4625 if (data->cancellable)
4626 g_object_unref (data->cancellable);
4627 g_object_unref (data->file);
4628 g_free (data->etag);
4633 replace_contents_close_callback (GObject *obj,
4634 GAsyncResult *close_res,
4637 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4638 ReplaceContentsData *data = user_data;
4639 GSimpleAsyncResult *res;
4641 /* Ignore errors here, we're only reading anyway */
4642 g_output_stream_close_finish (stream, close_res, NULL);
4643 g_object_unref (stream);
4645 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4647 res = g_simple_async_result_new (G_OBJECT (data->file),
4650 g_file_replace_contents_async);
4651 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4652 g_simple_async_result_complete (res);
4653 g_object_unref (res);
4657 replace_contents_write_callback (GObject *obj,
4658 GAsyncResult *read_res,
4661 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4662 ReplaceContentsData *data = user_data;
4663 GError *error = NULL;
4666 write_size = g_output_stream_write_finish (stream, read_res, &error);
4668 if (write_size <= 0)
4670 /* Error or EOF, close the file */
4672 data->error = error;
4673 g_output_stream_close_async (stream, 0,
4675 replace_contents_close_callback, data);
4677 else if (write_size > 0)
4679 data->pos += write_size;
4681 if (data->pos >= data->length)
4682 g_output_stream_close_async (stream, 0,
4684 replace_contents_close_callback, data);
4686 g_output_stream_write_async (stream,
4687 data->content + data->pos,
4688 data->length - data->pos,
4691 replace_contents_write_callback,
4697 replace_contents_open_callback (GObject *obj,
4698 GAsyncResult *open_res,
4701 GFile *file = G_FILE (obj);
4702 GFileOutputStream *stream;
4703 ReplaceContentsData *data = user_data;
4704 GError *error = NULL;
4705 GSimpleAsyncResult *res;
4707 stream = g_file_replace_finish (file, open_res, &error);
4711 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4712 data->content + data->pos,
4713 data->length - data->pos,
4716 replace_contents_write_callback,
4722 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4726 g_simple_async_result_complete (res);
4727 g_error_free (error);
4728 replace_contents_data_free (data);
4729 g_object_unref (res);
4734 * g_file_replace_contents_async:
4735 * @file: input #GFile.
4736 * @contents: string of contents to replace the file with.
4737 * @length: the length of @contents in bytes.
4738 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4739 * @make_backup: a #gboolean.
4740 * @flags: a set of #GFileCreateFlags.
4741 * @cancellable: optional #GCancellable object, %NULL to ignore.
4742 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4743 * @user_data: the data to pass to callback function
4745 * Starts an asynchronous replacement of @file with the given
4746 * @contents of @length bytes. @etag will replace the document's
4747 * current entity tag.
4749 * When this operation has completed, @callback will be called with
4750 * @user_user data, and the operation can be finalized with
4751 * g_file_replace_contents_finish().
4753 * If @cancellable is not %NULL, then the operation can be cancelled by
4754 * triggering the cancellable object from another thread. If the operation
4755 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4757 * If @make_backup is %TRUE, this function will attempt to
4758 * make a backup of @file.
4761 g_file_replace_contents_async (GFile *file,
4762 const char *contents,
4765 gboolean make_backup,
4766 GFileCreateFlags flags,
4767 GCancellable *cancellable,
4768 GAsyncReadyCallback callback,
4771 ReplaceContentsData *data;
4773 g_return_if_fail (G_IS_FILE (file));
4774 g_return_if_fail (contents != NULL);
4776 data = g_new0 (ReplaceContentsData, 1);
4779 data->cancellable = g_object_ref (cancellable);
4780 data->callback = callback;
4781 data->user_data = user_data;
4782 data->content = contents;
4783 data->length = length;
4785 data->file = g_object_ref (file);
4787 g_file_replace_async (file,
4793 replace_contents_open_callback,
4798 * g_file_replace_contents_finish:
4799 * @file: input #GFile.
4800 * @res: a #GAsyncResult.
4801 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
4803 * @error: a #GError, or %NULL
4805 * Finishes an asynchronous replace of the given @file. See
4806 * g_file_replace_contents_async(). Sets @new_etag to the new entity
4807 * tag for the document, if present.
4809 * Returns: %TRUE on success, %FALSE on failure.
4812 g_file_replace_contents_finish (GFile *file,
4817 GSimpleAsyncResult *simple;
4818 ReplaceContentsData *data;
4820 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4821 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4823 simple = G_SIMPLE_ASYNC_RESULT (res);
4825 if (g_simple_async_result_propagate_error (simple, error))
4828 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
4830 data = g_simple_async_result_get_op_res_gpointer (simple);
4834 g_propagate_error (error, data->error);
4842 *new_etag = data->etag;
4843 data->etag = NULL; /* Take ownership */
4849 #define __G_FILE_C__
4850 #include "gioaliasdef.c"