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
43 * @see_also: #GFileInfo, #GFileEnumerator
44 * @include: gio/gfile.h
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 "std:*" means all attributes in the std
696 * namespace. An example attribute query be "std:*,owner:user".
697 * The standard attributes are availible as defines, like #G_FILE_ATTRIBUTE_STD_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 "std:*" means all attributes in the std
832 * namespace. An example attribute query be "std:*,owner:user".
833 * The standard attributes are availible as defines, like #G_FILE_ATTRIBUTE_STD_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_FS_SIZE
973 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FS_FREE (number of
974 * bytes availible), and #G_FILE_ATTRIBUTE_FS_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_STD_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_STD_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_STD_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_STD_TYPE "," G_FILE_ATTRIBUTE_STD_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_STD_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.
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));
3026 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
3028 iface = G_FILE_GET_IFACE (file);
3030 if (iface->mount_mountable == NULL)
3031 g_simple_async_report_error_in_idle (G_OBJECT (file),
3035 G_IO_ERROR_NOT_SUPPORTED,
3036 _("Operation not supported"));
3038 (* iface->mount_mountable) (file,
3046 * g_file_mount_mountable_finish:
3047 * @file: input #GFile.
3048 * @result: a #GAsyncResult.
3049 * @error: a #GError, or %NULL
3051 * Finishes a mount operation. See g_file_mount_mountable() for details.
3053 * Finish an asynchronous mount operation that was started
3054 * with g_file_mount_mountable().
3056 * Returns: a #GFile or %NULL on error.
3059 g_file_mount_mountable_finish (GFile *file,
3060 GAsyncResult *result,
3065 g_return_val_if_fail (G_IS_FILE (file), NULL);
3066 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3068 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3070 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3071 if (g_simple_async_result_propagate_error (simple, error))
3075 iface = G_FILE_GET_IFACE (file);
3076 return (* iface->mount_mountable_finish) (file, result, error);
3080 * g_file_unmount_mountable:
3081 * @file: input #GFile.
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 GCancellable *cancellable,
3098 GAsyncReadyCallback callback,
3103 g_return_if_fail (G_IS_FILE (file));
3105 iface = G_FILE_GET_IFACE (file);
3107 if (iface->unmount_mountable == NULL)
3108 g_simple_async_report_error_in_idle (G_OBJECT (file),
3112 G_IO_ERROR_NOT_SUPPORTED,
3113 _("Operation not supported"));
3115 (* iface->unmount_mountable) (file,
3122 * g_file_unmount_mountable_finish:
3123 * @file: input #GFile.
3124 * @result: a #GAsyncResult.
3125 * @error: a #GError, or %NULL
3127 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3129 * Finish an asynchronous unmount operation that was started
3130 * with g_file_unmount_mountable().
3132 * Returns: %TRUE if the operation finished successfully. %FALSE
3136 g_file_unmount_mountable_finish (GFile *file,
3137 GAsyncResult *result,
3142 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3143 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3145 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3147 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3148 if (g_simple_async_result_propagate_error (simple, error))
3152 iface = G_FILE_GET_IFACE (file);
3153 return (* iface->unmount_mountable_finish) (file, result, error);
3157 * g_file_eject_mountable:
3158 * @file: input #GFile.
3159 * @cancellable: optional #GCancellable object, %NULL to ignore.
3160 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3161 * @user_data: the data to pass to callback function
3163 * Starts an asynchronous eject on a mountable.
3164 * When this operation has completed, @callback will be called with
3165 * @user_user data, and the operation can be finalized with
3166 * g_file_eject_mountable_finish().
3168 * If @cancellable is not %NULL, then the operation can be cancelled by
3169 * triggering the cancellable object from another thread. If the operation
3170 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3173 g_file_eject_mountable (GFile *file,
3174 GCancellable *cancellable,
3175 GAsyncReadyCallback callback,
3180 g_return_if_fail (G_IS_FILE (file));
3182 iface = G_FILE_GET_IFACE (file);
3184 if (iface->eject_mountable == NULL)
3185 g_simple_async_report_error_in_idle (G_OBJECT (file),
3189 G_IO_ERROR_NOT_SUPPORTED,
3190 _("Operation not supported"));
3192 (* iface->eject_mountable) (file,
3199 * g_file_eject_mountable_finish:
3200 * @file: input #GFile.
3201 * @result: a #GAsyncResult.
3202 * @error: a #GError, or %NULL
3204 * Finishes an asynchronous eject operation started by
3205 * g_file_eject_mountable().
3207 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3211 g_file_eject_mountable_finish (GFile *file,
3212 GAsyncResult *result,
3217 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3218 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3220 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3222 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3223 if (g_simple_async_result_propagate_error (simple, error))
3227 iface = G_FILE_GET_IFACE (file);
3228 return (* iface->eject_mountable_finish) (file, result, error);
3232 * g_file_monitor_directory:
3233 * @file: input #GFile.
3234 * @flags: a set of #GFileMonitorFlags.
3235 * @cancellable: optional #GCancellable object, %NULL to ignore.
3237 * Obtains a directory monitor for the given file.
3238 * This may fail if directory monitoring is not supported.
3240 * If @cancellable is not %NULL, then the operation can be cancelled by
3241 * triggering the cancellable object from another thread. If the operation
3242 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3244 * Returns: a #GDirectoryMonitor for the given @file,
3245 * or %NULL on error.
3248 g_file_monitor_directory (GFile *file,
3249 GFileMonitorFlags flags,
3250 GCancellable *cancellable)
3254 g_return_val_if_fail (G_IS_FILE (file), NULL);
3256 iface = G_FILE_GET_IFACE (file);
3258 if (iface->monitor_dir == NULL)
3261 return (* iface->monitor_dir) (file, flags, cancellable);
3265 * g_file_monitor_file:
3266 * @file: input #GFile.
3267 * @flags: a set of #GFileMonitorFlags.
3268 * @cancellable: optional #GCancellable object, %NULL to ignore.
3270 * Obtains a file monitor for the given file. If no file notification
3271 * mechanism exists, then regular polling of the file is used.
3273 * If @cancellable is not %NULL, then the operation can be cancelled by
3274 * triggering the cancellable object from another thread. If the operation
3275 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3277 * Returns: a #GFileMonitor for the given @file.
3280 g_file_monitor_file (GFile *file,
3281 GFileMonitorFlags flags,
3282 GCancellable *cancellable)
3285 GFileMonitor *monitor;
3287 g_return_val_if_fail (G_IS_FILE (file), NULL);
3289 iface = G_FILE_GET_IFACE (file);
3293 if (iface->monitor_file)
3294 monitor = (* iface->monitor_file) (file, flags, cancellable);
3296 /* Fallback to polling */
3297 if (monitor == NULL)
3298 monitor = _g_poll_file_monitor_new (file);
3303 /********************************************
3304 * Default implementation of async ops *
3305 ********************************************/
3309 GFileQueryInfoFlags flags;
3311 } QueryInfoAsyncData;
3314 query_info_data_free (QueryInfoAsyncData *data)
3317 g_object_unref (data->info);
3318 g_free (data->attributes);
3323 query_info_async_thread (GSimpleAsyncResult *res,
3325 GCancellable *cancellable)
3327 GError *error = NULL;
3328 QueryInfoAsyncData *data;
3331 data = g_simple_async_result_get_op_res_gpointer (res);
3333 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3337 g_simple_async_result_set_from_error (res, error);
3338 g_error_free (error);
3345 g_file_real_query_info_async (GFile *file,
3346 const char *attributes,
3347 GFileQueryInfoFlags flags,
3349 GCancellable *cancellable,
3350 GAsyncReadyCallback callback,
3353 GSimpleAsyncResult *res;
3354 QueryInfoAsyncData *data;
3356 data = g_new0 (QueryInfoAsyncData, 1);
3357 data->attributes = g_strdup (attributes);
3358 data->flags = flags;
3360 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3361 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3363 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3364 g_object_unref (res);
3368 g_file_real_query_info_finish (GFile *file,
3372 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3373 QueryInfoAsyncData *data;
3375 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3377 data = g_simple_async_result_get_op_res_gpointer (simple);
3379 return g_object_ref (data->info);
3386 GFileQueryInfoFlags flags;
3387 GFileEnumerator *enumerator;
3388 } EnumerateChildrenAsyncData;
3391 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3393 if (data->enumerator)
3394 g_object_unref (data->enumerator);
3395 g_free (data->attributes);
3400 enumerate_children_async_thread (GSimpleAsyncResult *res,
3402 GCancellable *cancellable)
3404 GError *error = NULL;
3405 EnumerateChildrenAsyncData *data;
3406 GFileEnumerator *enumerator;
3408 data = g_simple_async_result_get_op_res_gpointer (res);
3410 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3412 if (enumerator == NULL)
3414 g_simple_async_result_set_from_error (res, error);
3415 g_error_free (error);
3418 data->enumerator = enumerator;
3422 g_file_real_enumerate_children_async (GFile *file,
3423 const char *attributes,
3424 GFileQueryInfoFlags flags,
3426 GCancellable *cancellable,
3427 GAsyncReadyCallback callback,
3430 GSimpleAsyncResult *res;
3431 EnumerateChildrenAsyncData *data;
3433 data = g_new0 (EnumerateChildrenAsyncData, 1);
3434 data->attributes = g_strdup (attributes);
3435 data->flags = flags;
3437 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3438 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3440 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3441 g_object_unref (res);
3444 static GFileEnumerator *
3445 g_file_real_enumerate_children_finish (GFile *file,
3449 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3450 EnumerateChildrenAsyncData *data;
3452 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3454 data = g_simple_async_result_get_op_res_gpointer (simple);
3455 if (data->enumerator)
3456 return g_object_ref (data->enumerator);
3462 open_read_async_thread (GSimpleAsyncResult *res,
3464 GCancellable *cancellable)
3467 GFileInputStream *stream;
3468 GError *error = NULL;
3470 iface = G_FILE_GET_IFACE (object);
3472 stream = iface->read_fn (G_FILE (object), cancellable, &error);
3476 g_simple_async_result_set_from_error (res, error);
3477 g_error_free (error);
3480 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3484 g_file_real_read_async (GFile *file,
3486 GCancellable *cancellable,
3487 GAsyncReadyCallback callback,
3490 GSimpleAsyncResult *res;
3492 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3494 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3495 g_object_unref (res);
3498 static GFileInputStream *
3499 g_file_real_read_finish (GFile *file,
3503 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3506 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3508 op = g_simple_async_result_get_op_res_gpointer (simple);
3510 return g_object_ref (op);
3516 append_to_async_thread (GSimpleAsyncResult *res,
3518 GCancellable *cancellable)
3521 GFileCreateFlags *data;
3522 GFileOutputStream *stream;
3523 GError *error = NULL;
3525 iface = G_FILE_GET_IFACE (object);
3527 data = g_simple_async_result_get_op_res_gpointer (res);
3529 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3533 g_simple_async_result_set_from_error (res, error);
3534 g_error_free (error);
3537 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3541 g_file_real_append_to_async (GFile *file,
3542 GFileCreateFlags flags,
3544 GCancellable *cancellable,
3545 GAsyncReadyCallback callback,
3548 GFileCreateFlags *data;
3549 GSimpleAsyncResult *res;
3551 data = g_new0 (GFileCreateFlags, 1);
3554 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3555 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3557 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3558 g_object_unref (res);
3561 static GFileOutputStream *
3562 g_file_real_append_to_finish (GFile *file,
3566 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3569 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3571 op = g_simple_async_result_get_op_res_gpointer (simple);
3573 return g_object_ref (op);
3579 create_async_thread (GSimpleAsyncResult *res,
3581 GCancellable *cancellable)
3584 GFileCreateFlags *data;
3585 GFileOutputStream *stream;
3586 GError *error = NULL;
3588 iface = G_FILE_GET_IFACE (object);
3590 data = g_simple_async_result_get_op_res_gpointer (res);
3592 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3596 g_simple_async_result_set_from_error (res, error);
3597 g_error_free (error);
3600 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3604 g_file_real_create_async (GFile *file,
3605 GFileCreateFlags flags,
3607 GCancellable *cancellable,
3608 GAsyncReadyCallback callback,
3611 GFileCreateFlags *data;
3612 GSimpleAsyncResult *res;
3614 data = g_new0 (GFileCreateFlags, 1);
3617 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3618 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3620 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3621 g_object_unref (res);
3624 static GFileOutputStream *
3625 g_file_real_create_finish (GFile *file,
3629 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3632 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3634 op = g_simple_async_result_get_op_res_gpointer (simple);
3636 return g_object_ref (op);
3642 GFileOutputStream *stream;
3644 gboolean make_backup;
3645 GFileCreateFlags flags;
3649 replace_async_data_free (ReplaceAsyncData *data)
3652 g_object_unref (data->stream);
3653 g_free (data->etag);
3658 replace_async_thread (GSimpleAsyncResult *res,
3660 GCancellable *cancellable)
3663 GFileOutputStream *stream;
3664 GError *error = NULL;
3665 ReplaceAsyncData *data;
3667 iface = G_FILE_GET_IFACE (object);
3669 data = g_simple_async_result_get_op_res_gpointer (res);
3671 stream = iface->replace (G_FILE (object),
3680 g_simple_async_result_set_from_error (res, error);
3681 g_error_free (error);
3684 data->stream = stream;
3688 g_file_real_replace_async (GFile *file,
3690 gboolean make_backup,
3691 GFileCreateFlags flags,
3693 GCancellable *cancellable,
3694 GAsyncReadyCallback callback,
3697 GSimpleAsyncResult *res;
3698 ReplaceAsyncData *data;
3700 data = g_new0 (ReplaceAsyncData, 1);
3701 data->etag = g_strdup (etag);
3702 data->make_backup = make_backup;
3703 data->flags = flags;
3705 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3706 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3708 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3709 g_object_unref (res);
3712 static GFileOutputStream *
3713 g_file_real_replace_finish (GFile *file,
3717 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3718 ReplaceAsyncData *data;
3720 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3722 data = g_simple_async_result_get_op_res_gpointer (simple);
3724 return g_object_ref (data->stream);
3732 } SetDisplayNameAsyncData;
3735 set_display_name_data_free (SetDisplayNameAsyncData *data)
3737 g_free (data->name);
3739 g_object_unref (data->file);
3744 set_display_name_async_thread (GSimpleAsyncResult *res,
3746 GCancellable *cancellable)
3748 GError *error = NULL;
3749 SetDisplayNameAsyncData *data;
3752 data = g_simple_async_result_get_op_res_gpointer (res);
3754 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3758 g_simple_async_result_set_from_error (res, error);
3759 g_error_free (error);
3766 g_file_real_set_display_name_async (GFile *file,
3767 const char *display_name,
3769 GCancellable *cancellable,
3770 GAsyncReadyCallback callback,
3773 GSimpleAsyncResult *res;
3774 SetDisplayNameAsyncData *data;
3776 data = g_new0 (SetDisplayNameAsyncData, 1);
3777 data->name = g_strdup (display_name);
3779 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3780 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3782 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3783 g_object_unref (res);
3787 g_file_real_set_display_name_finish (GFile *file,
3791 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3792 SetDisplayNameAsyncData *data;
3794 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3796 data = g_simple_async_result_get_op_res_gpointer (simple);
3798 return g_object_ref (data->file);
3804 GFileQueryInfoFlags flags;
3811 set_info_data_free (SetInfoAsyncData *data)
3814 g_object_unref (data->info);
3816 g_error_free (data->error);
3821 set_info_async_thread (GSimpleAsyncResult *res,
3823 GCancellable *cancellable)
3825 SetInfoAsyncData *data;
3827 data = g_simple_async_result_get_op_res_gpointer (res);
3830 data->res = g_file_set_attributes_from_info (G_FILE (object),
3838 g_file_real_set_attributes_async (GFile *file,
3840 GFileQueryInfoFlags flags,
3842 GCancellable *cancellable,
3843 GAsyncReadyCallback callback,
3846 GSimpleAsyncResult *res;
3847 SetInfoAsyncData *data;
3849 data = g_new0 (SetInfoAsyncData, 1);
3850 data->info = g_file_info_dup (info);
3851 data->flags = flags;
3853 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
3854 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
3856 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
3857 g_object_unref (res);
3861 g_file_real_set_attributes_finish (GFile *file,
3866 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3867 SetInfoAsyncData *data;
3869 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
3871 data = g_simple_async_result_get_op_res_gpointer (simple);
3874 *info = g_object_ref (data->info);
3876 if (error != NULL && data->error)
3877 *error = g_error_copy (data->error);
3882 /********************************************
3883 * Default VFS operations *
3884 ********************************************/
3887 * g_file_new_for_path:
3888 * @path: a string containing a relative or absolute path.
3890 * Constructs a #GFile for a given path. This operation never
3891 * fails, but the returned object might not support any I/O
3892 * operation if @path is malformed.
3894 * Returns: a new #GFile for the given @path.
3897 g_file_new_for_path (const char *path)
3899 g_return_val_if_fail (path != NULL, NULL);
3901 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
3905 * g_file_new_for_uri:
3906 * @uri: a string containing a URI.
3908 * Constructs a #GFile for a given URI. This operation never
3909 * fails, but the returned object might not support any I/O
3910 * operation if @uri is malformed or if the uri type is
3913 * Returns: a #GFile for the given @uri.
3916 g_file_new_for_uri (const char *uri)
3918 g_return_val_if_fail (uri != NULL, NULL);
3920 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
3924 * g_file_parse_name:
3925 * @parse_name: a file name or path to be parsed.
3927 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
3928 * This operation never fails, but the returned object might not support any I/O
3929 * operation if the @parse_name cannot be parsed.
3931 * Returns: a new #GFile.
3934 g_file_parse_name (const char *parse_name)
3936 g_return_val_if_fail (parse_name != NULL, NULL);
3938 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
3942 is_valid_scheme_character (char c)
3944 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
3948 has_valid_scheme (const char *uri)
3954 if (!is_valid_scheme_character (*p))
3959 } while (is_valid_scheme_character (*p));
3965 * g_file_new_for_commandline_arg:
3966 * @arg: a command line string.
3968 * Creates a #GFile with the given argument from
3971 * Returns: a new #GFile.
3974 g_file_new_for_commandline_arg (const char *arg)
3980 g_return_val_if_fail (arg != NULL, NULL);
3982 if (g_path_is_absolute (arg))
3983 return g_file_new_for_path (arg);
3985 if (has_valid_scheme (arg))
3986 return g_file_new_for_uri (arg);
3988 current_dir = g_get_current_dir ();
3989 filename = g_build_filename (current_dir, arg, NULL);
3990 g_free (current_dir);
3992 file = g_file_new_for_path (filename);
3999 * g_file_mount_enclosing_volume:
4000 * @location: input #GFile.
4001 * @mount_operation: a #GMountOperation.
4002 * @cancellable: optional #GCancellable object, %NULL to ignore.
4003 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4004 * @user_data: the data to pass to callback function
4006 * Starts a @mount_operation, mounting the volume that contains the file @location.
4008 * When this operation has completed, @callback will be called with
4009 * @user_user data, and the operation can be finalized with
4010 * g_file_mount_enclosing_volume_finish().
4012 * If @cancellable is not %NULL, then the operation can be cancelled by
4013 * triggering the cancellable object from another thread. If the operation
4014 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4017 g_file_mount_enclosing_volume (GFile *location,
4018 GMountOperation *mount_operation,
4019 GCancellable *cancellable,
4020 GAsyncReadyCallback callback,
4025 g_return_if_fail (G_IS_FILE (location));
4026 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
4028 iface = G_FILE_GET_IFACE (location);
4030 if (iface->mount_enclosing_volume == NULL)
4032 g_simple_async_report_error_in_idle (G_OBJECT (location),
4033 callback, user_data,
4034 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4035 _("volume doesn't implement mount"));
4040 (* iface->mount_enclosing_volume) (location, mount_operation, cancellable, callback, user_data);
4045 * g_file_mount_enclosing_volume_finish:
4046 * @location: input #GFile.
4047 * @result: a #GAsyncResult.
4048 * @error: a #GError, or %NULL
4050 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4052 * Returns: %TRUE if successful. If an error
4053 * has occured, this function will return %FALSE and set @error
4054 * appropriately if present.
4057 g_file_mount_enclosing_volume_finish (GFile *location,
4058 GAsyncResult *result,
4063 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4064 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4066 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4068 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4069 if (g_simple_async_result_propagate_error (simple, error))
4073 iface = G_FILE_GET_IFACE (location);
4075 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4078 /********************************************
4079 * Utility functions *
4080 ********************************************/
4082 #define GET_CONTENT_BLOCK_SIZE 8192
4085 * g_file_load_contents:
4086 * @file: input #GFile.
4087 * @cancellable: optional #GCancellable object, %NULL to ignore.
4088 * @contents: a location to place the contents of the file.
4089 * @length: a location to place the length of the contents of the file.
4090 * @etag_out: a location to place the current entity tag for the file.
4091 * @error: a #GError, or %NULL
4093 * Loads the content of the file into memory, returning the size of
4094 * the data. The data is always zero terminated, but this is not
4095 * included in the resultant @length.
4097 * If @cancellable is not %NULL, then the operation can be cancelled by
4098 * triggering the cancellable object from another thread. If the operation
4099 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4101 * Returns: %TRUE if the @file's contents were successfully loaded.
4102 * %FALSE if there were errors..
4105 g_file_load_contents (GFile *file,
4106 GCancellable *cancellable,
4112 GFileInputStream *in;
4113 GByteArray *content;
4118 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4119 g_return_val_if_fail (contents != NULL, FALSE);
4121 in = g_file_read (file, cancellable, error);
4125 content = g_byte_array_new ();
4128 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4129 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4130 content->data + pos,
4131 GET_CONTENT_BLOCK_SIZE,
4132 cancellable, error)) > 0)
4135 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4142 info = g_file_input_stream_query_info (in,
4143 G_FILE_ATTRIBUTE_ETAG_VALUE,
4148 *etag_out = g_strdup (g_file_info_get_etag (info));
4149 g_object_unref (info);
4153 /* Ignore errors on close */
4154 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4155 g_object_unref (in);
4159 /* error is set already */
4160 g_byte_array_free (content, TRUE);
4167 /* Zero terminate (we got an extra byte allocated for this */
4168 content->data[pos] = 0;
4170 *contents = (char *)g_byte_array_free (content, FALSE);
4178 GCancellable *cancellable;
4179 GFileReadMoreCallback read_more_callback;
4180 GAsyncReadyCallback callback;
4182 GByteArray *content;
4189 load_contents_data_free (LoadContentsData *data)
4192 g_error_free (data->error);
4193 if (data->cancellable)
4194 g_object_unref (data->cancellable);
4196 g_byte_array_free (data->content, TRUE);
4197 g_free (data->etag);
4198 g_object_unref (data->file);
4203 load_contents_close_callback (GObject *obj,
4204 GAsyncResult *close_res,
4207 GInputStream *stream = G_INPUT_STREAM (obj);
4208 LoadContentsData *data = user_data;
4209 GSimpleAsyncResult *res;
4211 /* Ignore errors here, we're only reading anyway */
4212 g_input_stream_close_finish (stream, close_res, NULL);
4213 g_object_unref (stream);
4215 res = g_simple_async_result_new (G_OBJECT (data->file),
4218 g_file_load_contents_async);
4219 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4220 g_simple_async_result_complete (res);
4221 g_object_unref (res);
4225 load_contents_fstat_callback (GObject *obj,
4226 GAsyncResult *stat_res,
4229 GInputStream *stream = G_INPUT_STREAM (obj);
4230 LoadContentsData *data = user_data;
4233 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4237 data->etag = g_strdup (g_file_info_get_etag (info));
4238 g_object_unref (info);
4241 g_input_stream_close_async (stream, 0,
4243 load_contents_close_callback, data);
4247 load_contents_read_callback (GObject *obj,
4248 GAsyncResult *read_res,
4251 GInputStream *stream = G_INPUT_STREAM (obj);
4252 LoadContentsData *data = user_data;
4253 GError *error = NULL;
4256 read_size = g_input_stream_read_finish (stream, read_res, &error);
4260 /* Error or EOF, close the file */
4261 data->error = error;
4262 g_input_stream_close_async (stream, 0,
4264 load_contents_close_callback, data);
4266 else if (read_size == 0)
4268 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4269 G_FILE_ATTRIBUTE_ETAG_VALUE,
4272 load_contents_fstat_callback,
4275 else if (read_size > 0)
4277 data->pos += read_size;
4279 g_byte_array_set_size (data->content,
4280 data->pos + GET_CONTENT_BLOCK_SIZE);
4283 if (data->read_more_callback &&
4284 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4285 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4286 G_FILE_ATTRIBUTE_ETAG_VALUE,
4289 load_contents_fstat_callback,
4292 g_input_stream_read_async (stream,
4293 data->content->data + data->pos,
4294 GET_CONTENT_BLOCK_SIZE,
4297 load_contents_read_callback,
4303 load_contents_open_callback (GObject *obj,
4304 GAsyncResult *open_res,
4307 GFile *file = G_FILE (obj);
4308 GFileInputStream *stream;
4309 LoadContentsData *data = user_data;
4310 GError *error = NULL;
4311 GSimpleAsyncResult *res;
4313 stream = g_file_read_finish (file, open_res, &error);
4317 g_byte_array_set_size (data->content,
4318 data->pos + GET_CONTENT_BLOCK_SIZE);
4319 g_input_stream_read_async (G_INPUT_STREAM (stream),
4320 data->content->data + data->pos,
4321 GET_CONTENT_BLOCK_SIZE,
4324 load_contents_read_callback,
4330 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4334 g_simple_async_result_complete (res);
4335 g_error_free (error);
4336 load_contents_data_free (data);
4337 g_object_unref (res);
4342 * g_file_load_partial_contents_async:
4343 * @file: input #GFile.
4344 * @cancellable: optional #GCancellable object, %NULL to ignore.
4345 * @read_more_callback: a #GFileReadMoreCallback.
4346 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4347 * @user_data: the data to pass to the callback functions.
4349 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
4350 * used to stop reading from the file when appropriate, else this function
4351 * will behave exactly as g_file_load_contents_async(). This operation
4352 * can be finished by g_file_load_partial_contents_finish().
4354 * Users of this function should be aware that @user_data is passed to
4355 * both the @read_more_callback and the @callback.
4357 * If @cancellable is not %NULL, then the operation can be cancelled by
4358 * triggering the cancellable object from another thread. If the operation
4359 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4362 g_file_load_partial_contents_async (GFile *file,
4363 GCancellable *cancellable,
4364 GFileReadMoreCallback read_more_callback,
4365 GAsyncReadyCallback callback,
4368 LoadContentsData *data;
4370 g_return_if_fail (G_IS_FILE (file));
4372 data = g_new0 (LoadContentsData, 1);
4375 data->cancellable = g_object_ref (cancellable);
4376 data->read_more_callback = read_more_callback;
4377 data->callback = callback;
4378 data->user_data = user_data;
4379 data->content = g_byte_array_new ();
4380 data->file = g_object_ref (file);
4382 g_file_read_async (file,
4385 load_contents_open_callback,
4390 * g_file_load_partial_contents_finish:
4391 * @file: input #GFile.
4392 * @res: a #GAsyncResult.
4393 * @contents: a location to place the contents of the file.
4394 * @length: a location to place the length of the contents of the file.
4395 * @etag_out: a location to place the current entity tag for the file.
4396 * @error: a #GError, or %NULL
4398 * Finishes an asynchronous partial load operation that was started
4399 * with g_file_load_partial_contents_async().
4401 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4402 * present, it will be set appropriately.
4405 g_file_load_partial_contents_finish (GFile *file,
4412 GSimpleAsyncResult *simple;
4413 LoadContentsData *data;
4415 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4416 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4417 g_return_val_if_fail (contents != NULL, FALSE);
4419 simple = G_SIMPLE_ASYNC_RESULT (res);
4421 if (g_simple_async_result_propagate_error (simple, error))
4424 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4426 data = g_simple_async_result_get_op_res_gpointer (simple);
4430 g_propagate_error (error, data->error);
4439 *length = data->pos;
4443 *etag_out = data->etag;
4447 /* Zero terminate */
4448 g_byte_array_set_size (data->content, data->pos + 1);
4449 data->content->data[data->pos] = 0;
4451 *contents = (char *)g_byte_array_free (data->content, FALSE);
4452 data->content = NULL;
4458 * g_file_load_contents_async:
4459 * @file: input #GFile.
4460 * @cancellable: optional #GCancellable object, %NULL to ignore.
4461 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4462 * @user_data: the data to pass to callback function
4464 * Starts an asynchronous load of the @file's contents.
4465 * When the load operation has completed, @callback will be called
4466 * with @userdata. To finish the operation, call
4467 * g_file_load_contents_finish() with the #GAsyncResult returned by
4470 * If @cancellable is not %NULL, then the operation can be cancelled by
4471 * triggering the cancellable object from another thread. If the operation
4472 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4475 g_file_load_contents_async (GFile *file,
4476 GCancellable *cancellable,
4477 GAsyncReadyCallback callback,
4480 g_file_load_partial_contents_async (file,
4483 callback, user_data);
4487 * g_file_load_contents_finish:
4488 * @file: input #GFile.
4489 * @res: a #GAsyncResult.
4490 * @contents: a location to place the contents of the file.
4491 * @length: a location to place the length of the contents of the file.
4492 * @etag_out: a location to place the current entity tag for the file.
4493 * @error: a #GError, or %NULL
4495 * Finishes an asynchronous load of the @file's contents.
4496 * The contents are placed in @contents, and @length is set to the
4497 * size of the @contents string. If @etag_out is present, it will be
4498 * set to the new entity tag for the @file.
4500 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4501 * present, it will be set appropriately.
4504 g_file_load_contents_finish (GFile *file,
4511 return g_file_load_partial_contents_finish (file,
4520 * g_file_replace_contents:
4521 * @file: input #GFile.
4522 * @contents: a string containing the new contents for @file.
4523 * @length: the length of @contents in bytes.
4524 * @etag: the old <link linkend="gfile-etag">entity tag</link>
4526 * @make_backup: a #gboolean.
4527 * @flags: a set of #GFileCreateFlags.
4528 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4530 * @cancellable: optional #GCancellable object, %NULL to ignore.
4531 * @error: a #GError, or %NULL
4533 * Replaces the contents of @file with @contents of @length bytes.
4535 * If @etag is specified (not %NULL) any existing file must have that etag, or
4536 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
4538 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
4540 * If @cancellable is not %NULL, then the operation can be cancelled by
4541 * triggering the cancellable object from another thread. If the operation
4542 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4544 * The returned @new_etag can be used to verify that the file hasn't changed the
4545 * next time it is saved over.
4547 * Returns: %TRUE if successful. If an error
4548 * has occured, this function will return %FALSE and set @error
4549 * appropriately if present.
4552 g_file_replace_contents (GFile *file,
4553 const char *contents,
4556 gboolean make_backup,
4557 GFileCreateFlags flags,
4559 GCancellable *cancellable,
4562 GFileOutputStream *out;
4563 gsize pos, remainder;
4566 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4567 g_return_val_if_fail (contents != NULL, FALSE);
4569 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4575 while (remainder > 0 &&
4576 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4578 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4586 if (remainder > 0 && res < 0)
4588 /* Ignore errors on close */
4589 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4591 /* error is set already */
4595 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4599 *new_etag = g_file_output_stream_get_etag (out);
4607 GCancellable *cancellable;
4608 GAsyncReadyCallback callback;
4610 const char *content;
4614 } ReplaceContentsData;
4617 replace_contents_data_free (ReplaceContentsData *data)
4620 g_error_free (data->error);
4621 if (data->cancellable)
4622 g_object_unref (data->cancellable);
4623 g_object_unref (data->file);
4624 g_free (data->etag);
4629 replace_contents_close_callback (GObject *obj,
4630 GAsyncResult *close_res,
4633 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4634 ReplaceContentsData *data = user_data;
4635 GSimpleAsyncResult *res;
4637 /* Ignore errors here, we're only reading anyway */
4638 g_output_stream_close_finish (stream, close_res, NULL);
4639 g_object_unref (stream);
4641 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4643 res = g_simple_async_result_new (G_OBJECT (data->file),
4646 g_file_replace_contents_async);
4647 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4648 g_simple_async_result_complete (res);
4649 g_object_unref (res);
4653 replace_contents_write_callback (GObject *obj,
4654 GAsyncResult *read_res,
4657 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4658 ReplaceContentsData *data = user_data;
4659 GError *error = NULL;
4662 write_size = g_output_stream_write_finish (stream, read_res, &error);
4664 if (write_size <= 0)
4666 /* Error or EOF, close the file */
4668 data->error = error;
4669 g_output_stream_close_async (stream, 0,
4671 replace_contents_close_callback, data);
4673 else if (write_size > 0)
4675 data->pos += write_size;
4677 if (data->pos >= data->length)
4678 g_output_stream_close_async (stream, 0,
4680 replace_contents_close_callback, data);
4682 g_output_stream_write_async (stream,
4683 data->content + data->pos,
4684 data->length - data->pos,
4687 replace_contents_write_callback,
4693 replace_contents_open_callback (GObject *obj,
4694 GAsyncResult *open_res,
4697 GFile *file = G_FILE (obj);
4698 GFileOutputStream *stream;
4699 ReplaceContentsData *data = user_data;
4700 GError *error = NULL;
4701 GSimpleAsyncResult *res;
4703 stream = g_file_replace_finish (file, open_res, &error);
4707 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4708 data->content + data->pos,
4709 data->length - data->pos,
4712 replace_contents_write_callback,
4718 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4722 g_simple_async_result_complete (res);
4723 g_error_free (error);
4724 replace_contents_data_free (data);
4725 g_object_unref (res);
4730 * g_file_replace_contents_async:
4731 * @file: input #GFile.
4732 * @contents: string of contents to replace the file with.
4733 * @length: the length of @contents in bytes.
4734 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4735 * @make_backup: a #gboolean.
4736 * @flags: a set of #GFileCreateFlags.
4737 * @cancellable: optional #GCancellable object, %NULL to ignore.
4738 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4739 * @user_data: the data to pass to callback function
4741 * Starts an asynchronous replacement of @file with the given
4742 * @contents of @length bytes. @etag will replace the document's
4743 * current entity tag.
4745 * When this operation has completed, @callback will be called with
4746 * @user_user data, and the operation can be finalized with
4747 * g_file_replace_contents_finish().
4749 * If @cancellable is not %NULL, then the operation can be cancelled by
4750 * triggering the cancellable object from another thread. If the operation
4751 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4753 * If @make_backup is %TRUE, this function will attempt to
4754 * make a backup of @file.
4757 g_file_replace_contents_async (GFile *file,
4758 const char *contents,
4761 gboolean make_backup,
4762 GFileCreateFlags flags,
4763 GCancellable *cancellable,
4764 GAsyncReadyCallback callback,
4767 ReplaceContentsData *data;
4769 g_return_if_fail (G_IS_FILE (file));
4770 g_return_if_fail (contents != NULL);
4772 data = g_new0 (ReplaceContentsData, 1);
4775 data->cancellable = g_object_ref (cancellable);
4776 data->callback = callback;
4777 data->user_data = user_data;
4778 data->content = contents;
4779 data->length = length;
4781 data->file = g_object_ref (file);
4783 g_file_replace_async (file,
4789 replace_contents_open_callback,
4794 * g_file_replace_contents_finish:
4795 * @file: input #GFile.
4796 * @res: a #GAsyncResult.
4797 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
4799 * @error: a #GError, or %NULL
4801 * Finishes an asynchronous replace of the given @file. See
4802 * g_file_replace_contents_async(). Sets @new_etag to the new entity
4803 * tag for the document, if present.
4805 * Returns: %TRUE on success, %FALSE on failure.
4808 g_file_replace_contents_finish (GFile *file,
4813 GSimpleAsyncResult *simple;
4814 ReplaceContentsData *data;
4816 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4817 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4819 simple = G_SIMPLE_ASYNC_RESULT (res);
4821 if (g_simple_async_result_propagate_error (simple, error))
4824 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
4826 data = g_simple_async_result_get_op_res_gpointer (simple);
4830 g_propagate_error (error, data->error);
4838 *new_etag = data->etag;
4839 data->etag = NULL; /* Take ownership */
4845 #define __G_FILE_C__
4846 #include "gioaliasdef.c"