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 * Returns: %TRUE if file is native.
254 g_file_is_native (GFile *file)
258 g_return_val_if_fail (G_IS_FILE (file), FALSE);
260 iface = G_FILE_GET_IFACE (file);
262 return (* iface->is_native) (file);
267 * g_file_has_uri_scheme:
268 * @file: input #GFile.
269 * @uri_scheme: a string containing a URI scheme.
271 * Checks to see if a #GFile has a given URI scheme.
273 * Returns: %TRUE if #GFile's backend supports the
274 * given URI scheme, %FALSE if URI scheme is %NULL,
275 * not supported, or #GFile is invalid.
278 g_file_has_uri_scheme (GFile *file,
279 const char *uri_scheme)
283 g_return_val_if_fail (G_IS_FILE (file), FALSE);
284 g_return_val_if_fail (uri_scheme != NULL, FALSE);
286 iface = G_FILE_GET_IFACE (file);
288 return (* iface->has_uri_scheme) (file, uri_scheme);
293 * g_file_get_uri_scheme:
294 * @file: input #GFile.
296 * Gets the URI scheme for a #GFile.
297 * RFC 3986 decodes the scheme as:
299 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
301 * Common schemes include "file", "http", "ftp", etc.
303 * Returns: a string containing the URI scheme for the given
304 * #GFile. The returned string should be freed with g_free()
305 * when no longer needed.
308 g_file_get_uri_scheme (GFile *file)
312 g_return_val_if_fail (G_IS_FILE (file), NULL);
314 iface = G_FILE_GET_IFACE (file);
316 return (* iface->get_uri_scheme) (file);
321 * g_file_get_basename:
322 * @file: input #GFile.
324 * Gets the basename (the last component of the path) for a given #GFile.
326 * If called for the toplevel of a system (such as the filesystem root
327 * or a uri like sftp://host/ it will return a single directory separator
328 * (and on Windows, possibly a drive letter).
330 * Returns: string containing the #GFile's base name, or %NULL
331 * if given #GFile is invalid. The returned string should be
332 * freed with g_free() when no longer needed.
335 g_file_get_basename (GFile *file)
339 g_return_val_if_fail (G_IS_FILE (file), NULL);
341 iface = G_FILE_GET_IFACE (file);
343 return (* iface->get_basename) (file);
348 * @file: input #GFile.
350 * Gets the local pathname for #GFile, if one exists.
352 * Returns: string containing the #GFile's path, or %NULL if
353 * no such path exists. The returned string should be
354 * freed with g_free() when no longer needed.
357 g_file_get_path (GFile *file)
361 g_return_val_if_fail (G_IS_FILE (file), NULL);
363 iface = G_FILE_GET_IFACE (file);
365 return (* iface->get_path) (file);
370 * @file: input #GFile.
372 * Gets the URI for the @file.
374 * Returns: a string containing the #GFile's URI.
375 * The returned string should be freed with g_free() when no longer needed.
378 g_file_get_uri (GFile *file)
382 g_return_val_if_fail (G_IS_FILE (file), NULL);
384 iface = G_FILE_GET_IFACE (file);
386 return (* iface->get_uri) (file);
390 * g_file_get_parse_name:
391 * @file: input #GFile.
393 * Gets the parse name of the @file.
394 * A parse name is a UTF-8 string that describes the
395 * file such that one can get the #GFile back using
396 * g_file_parse_name().
398 * This is generally used to show the #GFile as a nice
399 * string in a user interface, like in a location entry.
401 * For local files with names that can safely be converted
402 * to UTF8 the pathname is used, otherwise the IRI is used
403 * (a form of URI that allows UTF8 characters unescaped).
405 * Returns: a string containing the #GFile's parse name. The returned
406 * string should be freed with g_free() when no longer needed.
409 g_file_get_parse_name (GFile *file)
413 g_return_val_if_fail (G_IS_FILE (file), NULL);
415 iface = G_FILE_GET_IFACE (file);
417 return (* iface->get_parse_name) (file);
422 * @file: input #GFile.
424 * Duplicates a #GFile handle. This operation does not duplicate
425 * the actual file or directory represented by the #GFile; see
426 * g_file_copy() if attempting to copy a file.
428 * Returns: #GFile that is a duplicate of the given #GFile.
431 g_file_dup (GFile *file)
435 g_return_val_if_fail (G_IS_FILE (file), NULL);
437 iface = G_FILE_GET_IFACE (file);
439 return (* iface->dup) (file);
444 * @file: #gconstpointer to a #GFile.
446 * Creates a hash value for a #GFile.
448 * Returns: 0 if @file is not a valid #GFile, otherwise an
449 * integer that can be used as hash value for the #GFile.
450 * This function is intended for easily hashing a #GFile to
451 * add to a #GHashTable or similar data structure.
454 g_file_hash (gconstpointer file)
458 g_return_val_if_fail (G_IS_FILE (file), 0);
460 iface = G_FILE_GET_IFACE (file);
462 return (* iface->hash) ((GFile *)file);
467 * @file1: the first #GFile.
468 * @file2: the second #GFile.
470 * Checks equality of two given #GFile<!-- -->s
472 * Returns: %TRUE if @file1 and @file2 are equal.
473 * %FALSE if either is not a #GFile.
476 g_file_equal (GFile *file1,
481 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
482 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
484 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
487 iface = G_FILE_GET_IFACE (file1);
489 return (* iface->equal) (file1, file2);
495 * @file: input #GFile.
497 * Gets the parent directory for the @file.
498 * If the @file represents the root directory of the
499 * file system, then %NULL will be returned.
501 * Returns: a #GFile structure to the parent of the given
502 * #GFile or %NULL if there is no parent.
505 g_file_get_parent (GFile *file)
509 g_return_val_if_fail (G_IS_FILE (file), NULL);
511 iface = G_FILE_GET_IFACE (file);
513 return (* iface->get_parent) (file);
518 * @file: input #GFile.
519 * @name: string containing the child's name.
521 * Gets a specific child of @file with name equal to @name.
523 * Note that the file with that specific name might not exist, but
524 * you can still have a #GFile that points to it. You can use this
525 * for instance to create that file.
527 * Returns: a #GFile to a child specified by @name.
530 g_file_get_child (GFile *file,
533 g_return_val_if_fail (G_IS_FILE (file), NULL);
534 g_return_val_if_fail (name != NULL, NULL);
536 return g_file_resolve_relative_path (file, name);
540 * g_file_get_child_for_display_name:
541 * @file: input #GFile.
542 * @display_name: string to a possible child.
545 * Gets the child of @file for a given @display_name (i.e. a UTF8
546 * version of the name). If this function fails, it returns %NULL and @error will be
549 * Returns: a #GFile to the specified child, or
550 * %NULL if the display name couldn't be converted.
553 g_file_get_child_for_display_name (GFile *file,
554 const char *display_name,
559 g_return_val_if_fail (G_IS_FILE (file), NULL);
560 g_return_val_if_fail (display_name != NULL, NULL);
562 iface = G_FILE_GET_IFACE (file);
564 return (* iface->get_child_for_display_name) (file, display_name, error);
568 * g_file_contains_file:
569 * @parent: input #GFile.
570 * @descendant: input #GFile.
572 * Checks whether @parent (recursively) contains the specified @descendent.
574 * Returns: %TRUE if the @descendent's parent, grandparent, etc is @parent. %FALSE otherwise.
577 g_file_contains_file (GFile *parent,
582 g_return_val_if_fail (G_IS_FILE (parent), FALSE);
583 g_return_val_if_fail (G_IS_FILE (descendant), FALSE);
585 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
588 iface = G_FILE_GET_IFACE (parent);
590 return (* iface->contains_file) (parent, descendant);
594 * g_file_get_relative_path:
595 * @parent: input #GFile.
596 * @descendant: input #GFile.
598 * Gets the path for @descendant relative to @parent.
600 * Returns: string with the relative path from @descendant
601 * to @parent, or %NULL if @descendant is not a descendant of @parent. The returned string should be freed with
602 * g_free() when no longer needed.
605 g_file_get_relative_path (GFile *parent,
610 g_return_val_if_fail (G_IS_FILE (parent), NULL);
611 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
613 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
616 iface = G_FILE_GET_IFACE (parent);
618 return (* iface->get_relative_path) (parent, descendant);
622 * g_file_resolve_relative_path:
623 * @file: input #GFile.
624 * @relative_path: a given relative path string.
626 * Resolves a relative path for @file to an absolute path.
628 * Returns: #GFile to the resolved path. %NULL if @relative_path
629 * is %NULL or if @file is invalid.
632 g_file_resolve_relative_path (GFile *file,
633 const char *relative_path)
637 g_return_val_if_fail (G_IS_FILE (file), NULL);
638 g_return_val_if_fail (relative_path != NULL, NULL);
640 iface = G_FILE_GET_IFACE (file);
642 return (* iface->resolve_relative_path) (file, relative_path);
646 * g_file_enumerate_children:
647 * @file: input #GFile.
648 * @attributes: an attribute query string.
649 * @flags: a set of #GFileQueryInfoFlags.
650 * @cancellable: optional #GCancellable object, %NULL to ignore.
651 * @error: #GError for error reporting.
653 * Gets the requested information about the files in a directory. The result
654 * is a #GFileEnumerator object that will give out #GFileInfo objects for
655 * all the files in the directory.
657 * The @attribute value is a string that specifies the file attributes that
658 * should be gathered. It is not an error if its not possible to read a particular
659 * requested attribute from a file, it just won't be set. @attribute should
660 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
661 * means all attributes, and a wildcard like "std:*" means all attributes in the std
662 * namespace. An example attribute query be "std:*,owner:user".
663 * The standard attributes are availible as defines, like #G_FILE_ATTRIBUTE_STD_NAME.
665 * If @cancellable is not %NULL, then the operation can be cancelled by
666 * triggering the cancellable object from another thread. If the operation
667 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
669 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
670 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
671 * Other errors are possible too.
673 * Returns: A #GFileEnumerator if successful, %NULL on error.
676 g_file_enumerate_children (GFile *file,
677 const char *attributes,
678 GFileQueryInfoFlags flags,
679 GCancellable *cancellable,
685 g_return_val_if_fail (G_IS_FILE (file), NULL);
687 if (g_cancellable_set_error_if_cancelled (cancellable, error))
690 iface = G_FILE_GET_IFACE (file);
692 if (iface->enumerate_children == NULL)
694 g_set_error (error, G_IO_ERROR,
695 G_IO_ERROR_NOT_SUPPORTED,
696 _("Operation not supported"));
700 return (* iface->enumerate_children) (file, attributes, flags,
705 * g_file_enumerate_children_async:
706 * @file: input #GFile.
707 * @attributes: an attribute query string.
708 * @flags: a set of #GFileQueryInfoFlags.
709 * @io_priority: the <link linkend="io-priority">I/O priority</link>
711 * @cancellable: optional #GCancellable object, %NULL to ignore.
712 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
713 * @user_data: the data to pass to callback function
715 * Asynchronously gets the requested information about the files in a directory. The result
716 * is a #GFileEnumerator object that will give out #GFileInfo objects for
717 * all the files in the directory.
719 * For more details, see g_file_enumerate_children() which is
720 * the synchronous version of this call.
722 * When the operation is finished, @callback will be called. You can then call
723 * g_file_enumerate_children_finish() to get the result of the operation.
726 g_file_enumerate_children_async (GFile *file,
727 const char *attributes,
728 GFileQueryInfoFlags flags,
730 GCancellable *cancellable,
731 GAsyncReadyCallback callback,
736 g_return_if_fail (G_IS_FILE (file));
738 iface = G_FILE_GET_IFACE (file);
739 (* iface->enumerate_children_async) (file,
749 * g_file_enumerate_children_finish:
750 * @file: input #GFile.
751 * @res: a #GAsyncResult.
754 * Finishes an async enumerate children operation.
755 * See g_file_enumerate_children_async().
757 * Returns: a #GFileEnumerator or %NULL if an error occurred.
760 g_file_enumerate_children_finish (GFile *file,
766 g_return_val_if_fail (G_IS_FILE (file), NULL);
767 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
769 if (G_IS_SIMPLE_ASYNC_RESULT (res))
771 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
772 if (g_simple_async_result_propagate_error (simple, error))
776 iface = G_FILE_GET_IFACE (file);
777 return (* iface->enumerate_children_finish) (file, res, error);
783 * @file: input #GFile.
784 * @attributes: an attribute query string.
785 * @flags: a set of #GFileQueryInfoFlags.
786 * @cancellable: optional #GCancellable object, %NULL to ignore.
789 * Gets the requested information about specified @file. The result
790 * is a #GFileInfo objects that contains key-value attributes (like type or size
793 * The @attribute value is a string that specifies the file attributes that
794 * should be gathered. It is not an error if its not possible to read a particular
795 * requested attribute from a file, it just won't be set. @attribute should
796 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
797 * means all attributes, and a wildcard like "std:*" means all attributes in the std
798 * namespace. An example attribute query be "std:*,owner:user".
799 * The standard attributes are availible as defines, like #G_FILE_ATTRIBUTE_STD_NAME.
801 * If @cancellable is not %NULL, then the operation can be cancelled by
802 * triggering the cancellable object from another thread. If the operation
803 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
805 * For symlinks, normally the information about the target of the
806 * symlink is returned, rather than information about the symlink itself.
807 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
808 * information about the symlink itself will be returned. Also, for symlinks
809 * that points to non-existing files the information about the symlink itself
812 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
813 * Other errors are possible too, and depend on what kind of filesystem the file is on.
815 * Returns: a #GFileInfo for the given @file, or %NULL on error.
818 g_file_query_info (GFile *file,
819 const char *attributes,
820 GFileQueryInfoFlags flags,
821 GCancellable *cancellable,
826 g_return_val_if_fail (G_IS_FILE (file), NULL);
828 if (g_cancellable_set_error_if_cancelled (cancellable, error))
831 iface = G_FILE_GET_IFACE (file);
833 if (iface->query_info == NULL)
835 g_set_error (error, G_IO_ERROR,
836 G_IO_ERROR_NOT_SUPPORTED,
837 _("Operation not supported"));
841 return (* iface->query_info) (file, attributes, flags, cancellable, error);
845 * g_file_query_info_async:
846 * @file: input #GFile.
847 * @attributes: an attribute query string.
848 * @flags: a set of #GFileQueryInfoFlags.
849 * @io_priority: the <link linkend="io-priority">I/O priority</link>
851 * @cancellable: optional #GCancellable object, %NULL to ignore.
852 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
853 * @user_data: the data to pass to callback function
855 * Asynchronously gets the requested information about specified @file. The result
856 * is a #GFileInfo objects that contains key-value attributes (like type or size
859 * For more details, see g_file_query_info() which is
860 * the synchronous version of this call.
862 * When the operation is finished, @callback will be called. You can then call
863 * g_file_enumerate_children_finish() to get the result of the operation.
866 g_file_query_info_async (GFile *file,
867 const char *attributes,
868 GFileQueryInfoFlags flags,
870 GCancellable *cancellable,
871 GAsyncReadyCallback callback,
876 g_return_if_fail (G_IS_FILE (file));
878 iface = G_FILE_GET_IFACE (file);
879 (* iface->query_info_async) (file,
889 * g_file_query_info_finish:
890 * @file: input #GFile.
891 * @res: a #GAsyncResult.
894 * Finishes an asynchronous file info query.
895 * See g_file_query_info_async().
897 * Returns: #GFileInfo for given @file or %NULL on error.
900 g_file_query_info_finish (GFile *file,
906 g_return_val_if_fail (G_IS_FILE (file), NULL);
907 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
909 if (G_IS_SIMPLE_ASYNC_RESULT (res))
911 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
912 if (g_simple_async_result_propagate_error (simple, error))
916 iface = G_FILE_GET_IFACE (file);
917 return (* iface->query_info_finish) (file, res, error);
921 * g_file_query_filesystem_info:
922 * @file: input #GFile.
923 * @attributes: an attribute query string.
924 * @cancellable: optional #GCancellable object, %NULL to ignore.
927 * Similar to g_file_query_info(), but obtains information
928 * about the filesystem the @file is on, rather than the file itself.
929 * For instance the amount of space availible and the type of
932 * The @attribute value is a string that specifies the file attributes that
933 * should be gathered. It is not an error if its not possible to read a particular
934 * requested attribute from a file, it just won't be set. @attribute should
935 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
936 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
937 * namespace. The standard namespace for filesystem attributes is "fs".
938 * Common attributes of interest are #G_FILE_ATTRIBUTE_FS_SIZE
939 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FS_FREE (number of
940 * bytes availible), and #G_FILE_ATTRIBUTE_FS_TYPE (type of the filesystem).
942 * If @cancellable is not %NULL, then the operation can be cancelled by
943 * triggering the cancellable object from another thread. If the operation
944 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
946 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
947 * Other errors are possible too, and depend on what kind of filesystem the file is on.
949 * Returns: a #GFileInfo or %NULL if there was an error.
952 g_file_query_filesystem_info (GFile *file,
953 const char *attributes,
954 GCancellable *cancellable,
959 g_return_val_if_fail (G_IS_FILE (file), NULL);
961 if (g_cancellable_set_error_if_cancelled (cancellable, error))
964 iface = G_FILE_GET_IFACE (file);
966 if (iface->query_filesystem_info == NULL)
968 g_set_error (error, G_IO_ERROR,
969 G_IO_ERROR_NOT_SUPPORTED,
970 _("Operation not supported"));
974 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
978 * g_file_find_enclosing_mount:
979 * @file: input #GFile.
980 * @cancellable: optional #GCancellable object, %NULL to ignore.
983 * Gets a #GMount for the #GFile.
985 * If the #GFileIface for @file does not have a mount (e.g. possibly a
986 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
989 * If @cancellable is not %NULL, then the operation can be cancelled by
990 * triggering the cancellable object from another thread. If the operation
991 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
993 * Returns: a #GMount where the @file is located or %NULL on error.
996 g_file_find_enclosing_mount (GFile *file,
997 GCancellable *cancellable,
1002 g_return_val_if_fail (G_IS_FILE (file), NULL);
1004 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1007 iface = G_FILE_GET_IFACE (file);
1008 if (iface->find_enclosing_mount == NULL)
1010 g_set_error (error, G_IO_ERROR,
1011 G_IO_ERROR_NOT_FOUND,
1012 _("Containing mount does not exist"));
1016 return (* iface->find_enclosing_mount) (file, cancellable, error);
1021 * @file: #GFile to read.
1022 * @cancellable: a #GCancellable
1023 * @error: a #GError, or %NULL
1025 * Opens a file for reading. The result is a #GFileInputStream that
1026 * can be used to read the contents of the file.
1028 * If @cancellable is not %NULL, then the operation can be cancelled by
1029 * triggering the cancellable object from another thread. If the operation
1030 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1032 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1033 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1034 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1036 * Returns: #GFileInputStream or %NULL on error.
1039 g_file_read (GFile *file,
1040 GCancellable *cancellable,
1045 g_return_val_if_fail (G_IS_FILE (file), NULL);
1047 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1050 iface = G_FILE_GET_IFACE (file);
1052 if (iface->read_fn == NULL)
1054 g_set_error (error, G_IO_ERROR,
1055 G_IO_ERROR_NOT_SUPPORTED,
1056 _("Operation not supported"));
1060 return (* iface->read_fn) (file, cancellable, error);
1065 * @file: input #GFile.
1066 * @flags: a set of #GFileCreateFlags.
1067 * @cancellable: optional #GCancellable object, %NULL to ignore.
1068 * @error: a #GError, or %NULL
1070 * Gets an output stream for appending data to the file. If
1071 * the file doesn't already exist it is created.
1073 * By default files created are generally readable by everyone,
1074 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1075 * will be made readable only to the current user, to the level that
1076 * is supported on the target filesystem.
1078 * If @cancellable is not %NULL, then the operation can be cancelled by
1079 * triggering the cancellable object from another thread. If the operation
1080 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1082 * Some filesystems don't allow all filenames, and may
1083 * return an G_IO_ERROR_INVALID_FILENAME error.
1084 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1085 * returned. Other errors are possible too, and depend on what kind of
1086 * filesystem the file is on.
1088 * Returns: a #GFileOutputStream.
1091 g_file_append_to (GFile *file,
1092 GFileCreateFlags flags,
1093 GCancellable *cancellable,
1098 g_return_val_if_fail (G_IS_FILE (file), NULL);
1100 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1103 iface = G_FILE_GET_IFACE (file);
1105 if (iface->append_to == NULL)
1107 g_set_error (error, G_IO_ERROR,
1108 G_IO_ERROR_NOT_SUPPORTED,
1109 _("Operation not supported"));
1113 return (* iface->append_to) (file, flags, cancellable, error);
1118 * @file: input #GFile.
1119 * @flags: a set of #GFileCreateFlags.
1120 * @cancellable: optional #GCancellable object, %NULL to ignore.
1121 * @error: a #GError, or %NULL
1123 * Creates a new file and returns an output stream for writing to it.
1124 * The file must not already exists.
1126 * By default files created are generally readable by everyone,
1127 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1128 * will be made readable only to the current user, to the level that
1129 * is supported on the target filesystem.
1131 * If @cancellable is not %NULL, then the operation can be cancelled by
1132 * triggering the cancellable object from another thread. If the operation
1133 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1135 * If a file with this name already exists the G_IO_ERROR_EXISTS error
1136 * will be returned. If the file is a directory the G_IO_ERROR_IS_DIRECTORY
1137 * error will be returned.
1138 * Some filesystems don't allow all filenames, and may
1139 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1140 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1141 * Other errors are possible too, and depend on what kind of
1142 * filesystem the file is on.
1144 * Returns: a #GFileOutputStream for the newly created file, or
1148 g_file_create (GFile *file,
1149 GFileCreateFlags flags,
1150 GCancellable *cancellable,
1155 g_return_val_if_fail (G_IS_FILE (file), NULL);
1157 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1160 iface = G_FILE_GET_IFACE (file);
1162 if (iface->create == NULL)
1164 g_set_error (error, G_IO_ERROR,
1165 G_IO_ERROR_NOT_SUPPORTED,
1166 _("Operation not supported"));
1170 return (* iface->create) (file, flags, cancellable, error);
1175 * @file: input #GFile.
1176 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1177 * current #GFile, or #NULL to ignore.
1178 * @make_backup: %TRUE if a backup should be created.
1179 * @flags: a set of #GFileCreateFlags.
1180 * @cancellable: optional #GCancellable object, %NULL to ignore.
1181 * @error: a #GError, or %NULL
1183 * Returns an output stream for overwriting the file, possibly
1184 * creating a backup copy of the file first.
1186 * This will try to replace the file in the safest way possible so
1187 * that any errors during the writing will not affect an already
1188 * existing copy of the file. For instance, for local files it
1189 * may write to a temporary file and then atomically rename over
1190 * the destination when the stream is closed.
1192 * By default files created are generally readable by everyone,
1193 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1194 * will be made readable only to the current user, to the level that
1195 * is supported on the target filesystem.
1197 * If @cancellable is not %NULL, then the operation can be cancelled by
1198 * triggering the cancellable object from another thread. If the operation
1199 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1201 * If you pass in a non-#NULL @etag value, then this value is
1202 * compared to the current entity tag of the file, and if they differ
1203 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1204 * that the file has been changed since you last read it. You can get
1205 * the new etag from g_file_output_stream_get_etag() after you've
1206 * finished writing and closed the #GFileOutputStream. When you load
1207 * a new file you can use g_file_input_stream_query_info() to get
1208 * the etag of the file.
1210 * If @make_backup is %TRUE, this function will attempt to make a backup
1211 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1212 * error will be returned. If you want to replace anyway, try again with
1213 * @make_backup set to %FALSE.
1215 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1216 * and if the file is some other form of non-regular file then a
1217 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1218 * Some filesystems don't allow all filenames, and may
1219 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1220 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1221 * Other errors are possible too, and depend on what kind of
1222 * filesystem the file is on.
1224 * Returns: a #GFileOutputStream or %NULL on error.
1227 g_file_replace (GFile *file,
1229 gboolean make_backup,
1230 GFileCreateFlags flags,
1231 GCancellable *cancellable,
1236 g_return_val_if_fail (G_IS_FILE (file), NULL);
1238 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1241 iface = G_FILE_GET_IFACE (file);
1243 if (iface->replace == NULL)
1245 g_set_error (error, G_IO_ERROR,
1246 G_IO_ERROR_NOT_SUPPORTED,
1247 _("Operation not supported"));
1252 /* Handle empty tag string as NULL in consistent way. */
1253 if (etag && *etag == 0)
1256 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1260 * g_file_read_async:
1261 * @file: input #GFile.
1262 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1264 * @cancellable: optional #GCancellable object, %NULL to ignore.
1265 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1266 * @user_data: the data to pass to callback function
1268 * Asynchronously opens @file for reading.
1270 * For more details, see g_file_read() which is
1271 * the synchronous version of this call.
1273 * When the operation is finished, @callback will be called. You can then call
1274 * g_file_read_finish() to get the result of the operation.
1277 g_file_read_async (GFile *file,
1279 GCancellable *cancellable,
1280 GAsyncReadyCallback callback,
1285 g_return_if_fail (G_IS_FILE (file));
1287 iface = G_FILE_GET_IFACE (file);
1288 (* iface->read_async) (file,
1296 * g_file_read_finish:
1297 * @file: input #GFile.
1298 * @res: a #GAsyncResult.
1299 * @error: a #GError, or %NULL
1301 * Finishes an asynchronous file read operation started with
1302 * g_file_read_async().
1304 * Returns: a #GFileInputStream or %NULL on error.
1307 g_file_read_finish (GFile *file,
1313 g_return_val_if_fail (G_IS_FILE (file), NULL);
1314 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1316 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1318 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1319 if (g_simple_async_result_propagate_error (simple, error))
1323 iface = G_FILE_GET_IFACE (file);
1324 return (* iface->read_finish) (file, res, error);
1328 * g_file_append_to_async:
1329 * @file: input #GFile.
1330 * @flags: a set of #GFileCreateFlags.
1331 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1333 * @cancellable: optional #GCancellable object, %NULL to ignore.
1334 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1335 * @user_data: the data to pass to callback function
1337 * Asynchronously opens @file for appending.
1339 * For more details, see g_file_append_to() which is
1340 * the synchronous version of this call.
1342 * When the operation is finished, @callback will be called. You can then call
1343 * g_file_append_to_finish() to get the result of the operation.
1346 g_file_append_to_async (GFile *file,
1347 GFileCreateFlags flags,
1349 GCancellable *cancellable,
1350 GAsyncReadyCallback callback,
1355 g_return_if_fail (G_IS_FILE (file));
1357 iface = G_FILE_GET_IFACE (file);
1358 (* iface->append_to_async) (file,
1367 * g_file_append_to_finish:
1368 * @file: input #GFile.
1369 * @res: #GAsyncResult
1370 * @error: a #GError, or %NULL
1372 * Finishes an asynchronous file append operation started with
1373 * g_file_append_to_async().
1375 * Returns: a valid #GFileOutputStream or %NULL on error.
1378 g_file_append_to_finish (GFile *file,
1384 g_return_val_if_fail (G_IS_FILE (file), NULL);
1385 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1387 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1389 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1390 if (g_simple_async_result_propagate_error (simple, error))
1394 iface = G_FILE_GET_IFACE (file);
1395 return (* iface->append_to_finish) (file, res, error);
1399 * g_file_create_async:
1400 * @file: input #GFile.
1401 * @flags: a set of #GFileCreateFlags.
1402 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1404 * @cancellable: optional #GCancellable object, %NULL to ignore.
1405 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1406 * @user_data: the data to pass to callback function
1408 * Asynchronously creates a new file and returns an output stream for writing to it.
1409 * The file must not already exists.
1411 * For more details, see g_file_creat() which is
1412 * the synchronous version of this call.
1414 * When the operation is finished, @callback will be called. You can then call
1415 * g_file_create_finish() to get the result of the operation.
1418 g_file_create_async (GFile *file,
1419 GFileCreateFlags flags,
1421 GCancellable *cancellable,
1422 GAsyncReadyCallback callback,
1427 g_return_if_fail (G_IS_FILE (file));
1429 iface = G_FILE_GET_IFACE (file);
1430 (* iface->create_async) (file,
1439 * g_file_create_finish:
1440 * @file: input #GFile.
1441 * @res: a #GAsyncResult.
1442 * @error: a #GError, or %NULL
1444 * Finishes an asynchronous file create operation started with
1445 * g_file_create_async().
1447 * Returns: a #GFileOutputStream or %NULL on error.
1450 g_file_create_finish (GFile *file,
1456 g_return_val_if_fail (G_IS_FILE (file), NULL);
1457 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1459 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1461 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1462 if (g_simple_async_result_propagate_error (simple, error))
1466 iface = G_FILE_GET_IFACE (file);
1467 return (* iface->create_finish) (file, res, error);
1471 * g_file_replace_async:
1472 * @file: input #GFile.
1473 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1475 * @make_backup: a #gboolean.
1476 * @flags: a set of #GFileCreateFlags.
1477 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1479 * @cancellable: optional #GCancellable object, %NULL to ignore.
1480 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1481 * @user_data: the data to pass to callback function
1483 * Asyncronously overwrites the file, replacing the contents, possibly
1484 * creating a backup copy of the file first.
1486 * For more details, see g_file_replace() which is
1487 * the synchronous version of this call.
1489 * When the operation is finished, @callback will be called. You can then call
1490 * g_file_replace_finish() to get the result of the operation.
1493 g_file_replace_async (GFile *file,
1495 gboolean make_backup,
1496 GFileCreateFlags flags,
1498 GCancellable *cancellable,
1499 GAsyncReadyCallback callback,
1504 g_return_if_fail (G_IS_FILE (file));
1506 iface = G_FILE_GET_IFACE (file);
1507 (* iface->replace_async) (file,
1518 * g_file_replace_finish:
1519 * @file: input #GFile.
1520 * @res: a #GAsyncResult.
1521 * @error: a #GError, or %NULL
1523 * Finishes an asynchronous file replace operation started with
1524 * g_file_replace_async().
1526 * Returns: a #GFileOutputStream, or %NULL on error.
1529 g_file_replace_finish (GFile *file,
1535 g_return_val_if_fail (G_IS_FILE (file), NULL);
1536 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1538 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1540 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1541 if (g_simple_async_result_propagate_error (simple, error))
1545 iface = G_FILE_GET_IFACE (file);
1546 return (* iface->replace_finish) (file, res, error);
1550 copy_symlink (GFile *destination,
1551 GFileCopyFlags flags,
1552 GCancellable *cancellable,
1557 gboolean tried_delete;
1559 GFileType file_type;
1561 tried_delete = FALSE;
1565 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1567 /* Maybe it already existed, and we want to overwrite? */
1568 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1569 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1571 g_error_free (my_error);
1574 /* Don't overwrite if the destination is a directory */
1575 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STD_TYPE,
1576 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1577 cancellable, &my_error);
1580 file_type = g_file_info_get_file_type (info);
1581 g_object_unref (info);
1583 if (file_type == G_FILE_TYPE_DIRECTORY)
1585 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1586 _("Can't copy over directory"));
1591 if (!g_file_delete (destination, cancellable, error))
1594 tried_delete = TRUE;
1598 g_propagate_error (error, my_error);
1605 static GInputStream *
1606 open_source_for_copy (GFile *source,
1608 GFileCopyFlags flags,
1609 GCancellable *cancellable,
1615 GFileType file_type;
1618 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1622 /* There was an error opening the source, try to set a good error for it: */
1624 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1626 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1627 * as that is less useful to the app. Better check for errors on the
1630 g_error_free (my_error);
1633 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STD_TYPE,
1634 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1635 cancellable, &my_error);
1638 file_type = g_file_info_get_file_type (info);
1639 g_object_unref (info);
1641 if (flags & G_FILE_COPY_OVERWRITE)
1643 if (file_type == G_FILE_TYPE_DIRECTORY)
1645 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1646 _("Can't copy directory over directory"));
1649 /* continue to would_recurse error */
1653 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1654 _("Target file exists"));
1660 /* Error getting info from target, return that error
1661 * (except for NOT_FOUND, which is no error here)
1663 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1665 g_propagate_error (error, my_error);
1668 g_error_free (my_error);
1671 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1672 _("Can't recursively copy directory"));
1676 g_propagate_error (error, my_error);
1681 should_copy (GFileAttributeInfo *info,
1685 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1686 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1690 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1691 GFileAttributeInfoList *namespaces,
1699 s = g_string_new ("");
1703 for (i = 0; i < attributes->n_infos; i++)
1705 if (should_copy (&attributes->infos[i], as_move))
1710 g_string_append_c (s, ',');
1712 g_string_append (s, attributes->infos[i].name);
1719 for (i = 0; i < namespaces->n_infos; i++)
1721 if (should_copy (&namespaces->infos[i], as_move))
1726 g_string_append_c (s, ',');
1728 g_string_append (s, namespaces->infos[i].name);
1729 g_string_append (s, ":*");
1734 return g_string_free (s, FALSE);
1738 * g_file_copy_attributes:
1739 * @source: a #GFile with attributes.
1740 * @destination: a #GFile to copy attributes to.
1741 * @flags: a set of #GFileCopyFlags.
1742 * @cancellable: optional #GCancellable object, %NULL to ignore.
1743 * @error: a #GError, %NULL to ignore.
1745 * Copies the file attributes from @source to @destination.
1747 * Normally only a subset of the file attributes are copied,
1748 * those that are copies in a normal file copy operation
1749 * (which for instance does not include e.g. mtime). However
1750 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
1751 * all the metadata that is possible to copy is copied.
1753 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1756 g_file_copy_attributes (GFile *source,
1758 GFileCopyFlags flags,
1759 GCancellable *cancellable,
1762 GFileAttributeInfoList *attributes, *namespaces;
1763 char *attrs_to_read;
1767 gboolean source_nofollow_symlinks;
1769 as_move = flags & G_FILE_COPY_ALL_METADATA;
1770 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1772 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1773 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1774 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1776 if (attributes == NULL && namespaces == NULL)
1779 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1781 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1782 * we just don't copy it.
1784 info = g_file_query_info (source, attrs_to_read,
1785 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1789 g_free (attrs_to_read);
1794 res = g_file_set_attributes_from_info (destination,
1798 g_object_unref (info);
1801 g_file_attribute_info_list_unref (attributes);
1802 g_file_attribute_info_list_unref (namespaces);
1807 /* Closes the streams */
1809 copy_stream_with_progress (GInputStream *in,
1811 GCancellable *cancellable,
1812 GFileProgressCallback progress_callback,
1813 gpointer progress_callback_data,
1816 gssize n_read, n_written;
1817 goffset current_size;
1818 char buffer[8192], *p;
1824 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1825 G_FILE_ATTRIBUTE_STD_SIZE,
1829 total_size = g_file_info_get_size (info);
1830 g_object_unref (info);
1837 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
1847 current_size += n_read;
1852 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
1853 if (n_written == -1)
1860 n_read -= n_written;
1866 if (progress_callback)
1867 progress_callback (current_size, total_size, progress_callback_data);
1871 error = NULL; /* Ignore further errors */
1873 /* Make sure we send full copied size */
1874 if (progress_callback)
1875 progress_callback (current_size, total_size, progress_callback_data);
1878 /* Don't care about errors in source here */
1879 g_input_stream_close (in, cancellable, NULL);
1881 /* But write errors on close are bad! */
1882 if (!g_output_stream_close (out, cancellable, error))
1885 g_object_unref (in);
1886 g_object_unref (out);
1892 file_copy_fallback (GFile *source,
1894 GFileCopyFlags flags,
1895 GCancellable *cancellable,
1896 GFileProgressCallback progress_callback,
1897 gpointer progress_callback_data,
1905 /* Maybe copy the symlink? */
1906 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
1908 info = g_file_query_info (source,
1909 G_FILE_ATTRIBUTE_STD_TYPE "," G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET,
1910 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1916 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
1917 (target = g_file_info_get_symlink_target (info)) != NULL)
1919 if (!copy_symlink (destination, flags, cancellable, target, error))
1921 g_object_unref (info);
1925 g_object_unref (info);
1929 g_object_unref (info);
1932 in = open_source_for_copy (source, destination, flags, cancellable, error);
1936 if (flags & G_FILE_COPY_OVERWRITE)
1938 out = (GOutputStream *)g_file_replace (destination,
1940 flags & G_FILE_COPY_BACKUP,
1941 cancellable, error);
1945 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
1950 g_object_unref (in);
1954 if (!copy_stream_with_progress (in, out, cancellable,
1955 progress_callback, progress_callback_data,
1961 /* Ignore errors here. Failure to copy metadata is not a hard error */
1962 g_file_copy_attributes (source, destination,
1963 flags, cancellable, NULL);
1970 * @source: input #GFile.
1971 * @destination: destination #GFile
1972 * @flags: set of #GFileCopyFlags
1973 * @cancellable: optional #GCancellable object, %NULL to ignore.
1974 * @progress_callback: function to callback with progress information
1975 * @progress_callback_data: userdata to pass to @progress_callback
1976 * @error: #GError to set on error, or %NULL
1978 * Copies the file @source to the location specified by @destination.
1979 * Can not handle recursive copies of directories.
1981 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
1982 * existing @destination file is overwritten.
1984 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
1985 * will be copied as symlinks, otherwise the target of the
1986 * @source symlink will be copied.
1988 * If @cancellable is not %NULL, then the operation can be cancelled by
1989 * triggering the cancellable object from another thread. If the operation
1990 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1992 * If @progress_callback is not %NULL, then the operation can be monitored by
1993 * setting this to a #GFileProgressCallback function. @progress_callback_data
1994 * will be passed to this function. It is guaranteed that this callback will
1995 * be called after all data has been transfered with the total number of bytes
1996 * copied during the operation.
1998 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
1999 * error is returned, independent on the status of the @destination.
2001 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2002 * error G_IO_ERROR_EXISTS is returned.
2004 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2005 * error is returned. If trying to overwrite a directory with a directory the
2006 * G_IO_ERROR_WOULD_MERGE error is returned.
2008 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2009 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2012 * If you are interested in copying the #GFile object itself (not the on-disk
2013 * file), see g_file_dup().
2015 * Returns: %TRUE on success, %FALSE otherwise.
2018 g_file_copy (GFile *source,
2020 GFileCopyFlags flags,
2021 GCancellable *cancellable,
2022 GFileProgressCallback progress_callback,
2023 gpointer progress_callback_data,
2030 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2031 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2033 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2036 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
2038 iface = G_FILE_GET_IFACE (source);
2043 res = (* iface->copy) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
2048 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2050 g_propagate_error (error, my_error);
2056 return file_copy_fallback (source, destination, flags, cancellable,
2057 progress_callback, progress_callback_data,
2064 * @source: #GFile pointing to the source location.
2065 * @destination: #GFile pointing to the destination location.
2066 * @flags: set of #GFileCopyFlags.
2067 * @cancellable: optional #GCancellable object, %NULL to ignore.
2068 * @progress_callback: #GFileProgressCallback function for updates.
2069 * @progress_callback_data: gpointer to user data for the callback function.
2070 * @error: #GError for returning error conditions, or %NULL
2073 * Tries to move the file or directory @source to the location specified by @destination.
2074 * If native move operations is supported then this is used, otherwise a copy + delete
2075 * fallback is used. The native implementation may support moving directories (for instance
2076 * on moves inside the same filesystem), but the fallback code does not.
2078 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2079 * existing @destination file is overwritten.
2081 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2082 * will be copied as symlinks, otherwise the target of the
2083 * @source symlink will be copied.
2085 * If @cancellable is not %NULL, then the operation can be cancelled by
2086 * triggering the cancellable object from another thread. If the operation
2087 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2089 * If @progress_callback is not %NULL, then the operation can be monitored by
2090 * setting this to a #GFileProgressCallback function. @progress_callback_data
2091 * will be passed to this function. It is guaranteed that this callback will
2092 * be called after all data has been transfered with the total number of bytes
2093 * copied during the operation.
2095 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2096 * error is returned, independent on the status of the @destination.
2098 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2099 * error G_IO_ERROR_EXISTS is returned.
2101 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2102 * error is returned. If trying to overwrite a directory with a directory the
2103 * G_IO_ERROR_WOULD_MERGE error is returned.
2105 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2106 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2107 * may be returned (if the native move operation isn't availible).
2109 * Returns: %TRUE on successful move, %FALSE otherwise.
2112 g_file_move (GFile *source,
2114 GFileCopyFlags flags,
2115 GCancellable *cancellable,
2116 GFileProgressCallback progress_callback,
2117 gpointer progress_callback_data,
2124 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2125 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2127 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2130 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
2132 iface = G_FILE_GET_IFACE (source);
2137 res = (* iface->move) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
2142 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2144 g_propagate_error (error, my_error);
2150 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2152 g_set_error (error, G_IO_ERROR,
2153 G_IO_ERROR_NOT_SUPPORTED,
2154 _("Operation not supported"));
2158 flags |= G_FILE_COPY_ALL_METADATA;
2159 if (!g_file_copy (source, destination, flags, cancellable,
2160 progress_callback, progress_callback_data,
2164 return g_file_delete (source, cancellable, error);
2168 * g_file_make_directory
2169 * @file: input #GFile.
2170 * @cancellable: optional #GCancellable object, %NULL to ignore.
2171 * @error: a #GError, or %NULL
2173 * Creates a directory.
2175 * If @cancellable is not %NULL, then the operation can be cancelled by
2176 * triggering the cancellable object from another thread. If the operation
2177 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2179 * Returns: %TRUE on successful creation, %FALSE otherwise.
2182 g_file_make_directory (GFile *file,
2183 GCancellable *cancellable,
2188 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2190 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2193 iface = G_FILE_GET_IFACE (file);
2195 if (iface->make_directory == NULL)
2197 g_set_error (error, G_IO_ERROR,
2198 G_IO_ERROR_NOT_SUPPORTED,
2199 _("Operation not supported"));
2203 return (* iface->make_directory) (file, cancellable, error);
2207 * g_file_make_symbolic_link:
2208 * @file: input #GFile.
2209 * @symlink_value: a string with the value of the new symlink.
2210 * @cancellable: optional #GCancellable object, %NULL to ignore.
2211 * @error: a #GError.
2213 * Creates a symbolic link.
2215 * If @cancellable is not %NULL, then the operation can be cancelled by
2216 * triggering the cancellable object from another thread. If the operation
2217 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2219 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2222 g_file_make_symbolic_link (GFile *file,
2223 const char *symlink_value,
2224 GCancellable *cancellable,
2229 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2230 g_return_val_if_fail (symlink_value != NULL, FALSE);
2232 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2235 if (*symlink_value == '\0')
2237 g_set_error (error, G_IO_ERROR,
2238 G_IO_ERROR_INVALID_ARGUMENT,
2239 _("Invalid symlink value given"));
2243 iface = G_FILE_GET_IFACE (file);
2245 if (iface->make_symbolic_link == NULL)
2247 g_set_error (error, G_IO_ERROR,
2248 G_IO_ERROR_NOT_SUPPORTED,
2249 _("Operation not supported"));
2253 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2258 * @file: input #GFile.
2259 * @cancellable: optional #GCancellable object, %NULL to ignore.
2260 * @error: a #GError, or %NULL
2264 * If @cancellable is not %NULL, then the operation can be cancelled by
2265 * triggering the cancellable object from another thread. If the operation
2266 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2268 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2271 g_file_delete (GFile *file,
2272 GCancellable *cancellable,
2277 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2279 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2282 iface = G_FILE_GET_IFACE (file);
2284 if (iface->delete_file == NULL)
2286 g_set_error (error, G_IO_ERROR,
2287 G_IO_ERROR_NOT_SUPPORTED,
2288 _("Operation not supported"));
2292 return (* iface->delete_file) (file, cancellable, error);
2297 * @file: #GFile to send to trash.
2298 * @cancellable: optional #GCancellable object, %NULL to ignore.
2299 * @error: a #GError, or %NULL
2301 * Sends @file to the "Trashcan", if possible. This is similar to
2302 * deleting it, but the user can recover it before emptying the trashcan.
2303 * Not all filesystems support trashing, so this call can return the
2304 * %G_IO_ERROR_NOT_SUPPORTED error.
2307 * If @cancellable is not %NULL, then the operation can be cancelled by
2308 * triggering the cancellable object from another thread. If the operation
2309 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2311 * Returns: %TRUE on successful trash, %FALSE otherwise.
2314 g_file_trash (GFile *file,
2315 GCancellable *cancellable,
2320 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2322 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2325 iface = G_FILE_GET_IFACE (file);
2327 if (iface->trash == NULL)
2330 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2331 _("Trash not supported"));
2335 return (* iface->trash) (file, cancellable, error);
2339 * g_file_set_display_name:
2340 * @file: input #GFile.
2341 * @display_name: a string.
2342 * @cancellable: optional #GCancellable object, %NULL to ignore.
2343 * @error: a #GError, or %NULL
2345 * Renames @file to the specified display name.
2347 * The display name is converted from UTF8 to the correct encoding for the target
2348 * filesystem if possible and the @file is renamed to this.
2350 * If you want to implement a rename operation in the user interface the edit name
2351 * (#G_FILE_ATTRIBUTE_STD_EDIT_NAME) should be used as the initial value in the rename
2352 * widget, and then the result after editing should be passed to g_file_set_display_name().
2354 * On success the resulting converted filename is returned.
2356 * If @cancellable is not %NULL, then the operation can be cancelled by
2357 * triggering the cancellable object from another thread. If the operation
2358 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2360 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2363 g_file_set_display_name (GFile *file,
2364 const char *display_name,
2365 GCancellable *cancellable,
2370 g_return_val_if_fail (G_IS_FILE (file), NULL);
2371 g_return_val_if_fail (display_name != NULL, NULL);
2373 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2377 G_IO_ERROR_INVALID_ARGUMENT,
2378 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2382 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2385 iface = G_FILE_GET_IFACE (file);
2387 return (* iface->set_display_name) (file, display_name, cancellable, error);
2391 * g_file_set_display_name_async:
2392 * @file: input #GFile.
2393 * @display_name: a string.
2394 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2396 * @cancellable: optional #GCancellable object, %NULL to ignore.
2397 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2398 * @user_data: the data to pass to callback function
2400 * Asynchronously sets the display name for a given #GFile.
2401 * For the synchronous version of this function, see g_file_set_display_name().
2404 g_file_set_display_name_async (GFile *file,
2405 const char *display_name,
2407 GCancellable *cancellable,
2408 GAsyncReadyCallback callback,
2413 g_return_if_fail (G_IS_FILE (file));
2414 g_return_if_fail (display_name != NULL);
2416 iface = G_FILE_GET_IFACE (file);
2417 (* iface->set_display_name_async) (file,
2426 * g_file_set_display_name_finish:
2427 * @file: input #GFile.
2428 * @res: a #GAsyncResult.
2429 * @error: a #GError, or %NULL
2431 * Finishes setting a display name started with
2432 * g_file_set_display_name_async().
2434 * Returns: a #GFile or %NULL on error.
2437 g_file_set_display_name_finish (GFile *file,
2443 g_return_val_if_fail (G_IS_FILE (file), NULL);
2444 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2446 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2448 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2449 if (g_simple_async_result_propagate_error (simple, error))
2453 iface = G_FILE_GET_IFACE (file);
2454 return (* iface->set_display_name_finish) (file, res, error);
2458 * g_file_query_settable_attributes:
2459 * @file: input #GFile.
2460 * @cancellable: optional #GCancellable object, %NULL to ignore.
2461 * @error: a #GError, or %NULL
2463 * Obtain the list of settable attributes for the file.
2465 * Returns the type and full attribute name of all the attributes
2466 * that can be set on this file. This doesn't mean setting it will always
2467 * succeed though, you might get an access failure, or some specific
2468 * file may not support a specific attribute.
2470 * If @cancellable is not %NULL, then the operation can be cancelled by
2471 * triggering the cancellable object from another thread. If the operation
2472 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2474 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2475 * When you are done with it, release it with g_file_attribute_info_list_unref()
2477 GFileAttributeInfoList *
2478 g_file_query_settable_attributes (GFile *file,
2479 GCancellable *cancellable,
2484 GFileAttributeInfoList *list;
2486 g_return_val_if_fail (G_IS_FILE (file), NULL);
2488 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2491 iface = G_FILE_GET_IFACE (file);
2493 if (iface->query_settable_attributes == NULL)
2494 return g_file_attribute_info_list_new ();
2497 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2501 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2503 list = g_file_attribute_info_list_new ();
2504 g_error_free (my_error);
2507 g_propagate_error (error, my_error);
2514 * g_file_query_writable_namespaces:
2515 * @file: input #GFile.
2516 * @cancellable: optional #GCancellable object, %NULL to ignore.
2517 * @error: a #GError, or %NULL
2519 * Obtain the list of attribute namespaces where new attributes
2520 * can be created by a user. An example of this is extended
2521 * attributes (in the "xattr" namespace).
2523 * If @cancellable is not %NULL, then the operation can be cancelled by
2524 * triggering the cancellable object from another thread. If the operation
2525 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2527 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2528 * When you are done with it, release it with g_file_attribute_info_list_unref()
2530 GFileAttributeInfoList *
2531 g_file_query_writable_namespaces (GFile *file,
2532 GCancellable *cancellable,
2537 GFileAttributeInfoList *list;
2539 g_return_val_if_fail (G_IS_FILE (file), NULL);
2541 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2544 iface = G_FILE_GET_IFACE (file);
2546 if (iface->query_writable_namespaces == NULL)
2547 return g_file_attribute_info_list_new ();
2550 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2554 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2556 list = g_file_attribute_info_list_new ();
2557 g_error_free (my_error);
2560 g_propagate_error (error, my_error);
2567 * g_file_set_attribute:
2568 * @file: input #GFile.
2569 * @attribute: a string containing the attribute's name.
2570 * @type: The type of the attribute
2571 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2572 * @flags: a set of #GFileQueryInfoFlags.
2573 * @cancellable: optional #GCancellable object, %NULL to ignore.
2574 * @error: a #GError, or %NULL
2576 * Sets an attribute in the file with attribute name @attribute to @value.
2578 * If @cancellable is not %NULL, then the operation can be cancelled by
2579 * triggering the cancellable object from another thread. If the operation
2580 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2582 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2585 g_file_set_attribute (GFile *file,
2586 const char *attribute,
2587 GFileAttributeType type,
2589 GFileQueryInfoFlags flags,
2590 GCancellable *cancellable,
2595 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2596 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2598 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2601 iface = G_FILE_GET_IFACE (file);
2603 if (iface->set_attribute == NULL)
2605 g_set_error (error, G_IO_ERROR,
2606 G_IO_ERROR_NOT_SUPPORTED,
2607 _("Operation not supported"));
2611 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2615 * g_file_set_attributes_from_info:
2616 * @file: input #GFile.
2617 * @info: a #GFileInfo.
2618 * @flags: #GFileQueryInfoFlags
2619 * @cancellable: optional #GCancellable object, %NULL to ignore.
2620 * @error: a #GError, or %NULL
2622 * Tries to set all attributes in the #GFileInfo on the target values,
2623 * not stopping on the first error.
2625 * If there is any error during this operation then @error will be set to
2626 * the first error. Error on particular fields are flagged by setting
2627 * the "status" field in the attribute value to
2628 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2631 * If @cancellable is not %NULL, then the operation can be cancelled by
2632 * triggering the cancellable object from another thread. If the operation
2633 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2635 * Returns: %TRUE if there was any error, %FALSE otherwise.
2638 g_file_set_attributes_from_info (GFile *file,
2640 GFileQueryInfoFlags flags,
2641 GCancellable *cancellable,
2646 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2647 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2649 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2652 g_file_info_clear_status (info);
2654 iface = G_FILE_GET_IFACE (file);
2656 return (* iface->set_attributes_from_info) (file,
2665 g_file_real_set_attributes_from_info (GFile *file,
2667 GFileQueryInfoFlags flags,
2668 GCancellable *cancellable,
2674 GFileAttributeValue *value;
2678 attributes = g_file_info_list_attributes (info, NULL);
2680 for (i = 0; attributes[i] != NULL; i++)
2682 value = _g_file_info_get_attribute_value (info, attributes[i]);
2684 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2687 if (!g_file_set_attribute (file, attributes[i],
2688 value->type, _g_file_attribute_value_peek_as_pointer (value),
2689 flags, cancellable, error))
2691 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2693 /* Don't set error multiple times */
2697 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2700 g_strfreev (attributes);
2706 * g_file_set_attributes_async:
2707 * @file: input #GFile.
2708 * @info: a #GFileInfo.
2709 * @flags: a #GFileQueryInfoFlags.
2710 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2712 * @cancellable: optional #GCancellable object, %NULL to ignore.
2713 * @callback: a #GAsyncReadyCallback.
2714 * @user_data: a #gpointer.
2716 * Asynchronously sets the attributes of @file with @info.
2717 * For the synchronous version of this function, see g_file_set_attributes().
2720 g_file_set_attributes_async (GFile *file,
2722 GFileQueryInfoFlags flags,
2724 GCancellable *cancellable,
2725 GAsyncReadyCallback callback,
2730 g_return_if_fail (G_IS_FILE (file));
2731 g_return_if_fail (G_IS_FILE_INFO (info));
2733 iface = G_FILE_GET_IFACE (file);
2734 (* iface->set_attributes_async) (file,
2744 * g_file_set_attributes_finish:
2745 * @file: input #GFile.
2746 * @result: a #GAsyncResult.
2747 * @info: a #GFileInfo.
2748 * @error: a #GError, or %NULL
2750 * Finishes setting an attribute started in g_file_set_attributes_async().
2752 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2755 g_file_set_attributes_finish (GFile *file,
2756 GAsyncResult *result,
2762 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2763 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2765 /* No standard handling of errors here, as we must set info even
2768 iface = G_FILE_GET_IFACE (file);
2769 return (* iface->set_attributes_finish) (file, result, info, error);
2773 * g_file_set_attribute_string:
2774 * @file: input #GFile.
2775 * @attribute: a string containing the attribute's name.
2776 * @value: a string containing the attribute's value.
2777 * @flags: #GFileQueryInfoFlags.
2778 * @cancellable: optional #GCancellable object, %NULL to ignore.
2779 * @error: a #GError, or %NULL
2781 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
2782 * If @attribute is of a different type, this operation will fail.
2784 * If @cancellable is not %NULL, then the operation can be cancelled by
2785 * triggering the cancellable object from another thread. If the operation
2786 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2788 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2791 g_file_set_attribute_string (GFile *file,
2792 const char *attribute,
2794 GFileQueryInfoFlags flags,
2795 GCancellable *cancellable,
2798 return g_file_set_attribute (file, attribute,
2799 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
2800 flags, cancellable, error);
2804 * g_file_set_attribute_byte_string:
2805 * @file: input #GFile.
2806 * @attribute: a string containing the attribute's name.
2807 * @value: a string containing the attribute's new value.
2808 * @flags: a #GFileQueryInfoFlags.
2809 * @cancellable: optional #GCancellable object, %NULL to ignore.
2810 * @error: a #GError, or %NULL
2812 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
2813 * If @attribute is of a different type, this operation will fail,
2816 * If @cancellable is not %NULL, then the operation can be cancelled by
2817 * triggering the cancellable object from another thread. If the operation
2818 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2820 * Returns: %TRUE if the @attribute was successfully set to @value
2821 * in the @file, %FALSE otherwise.
2824 g_file_set_attribute_byte_string (GFile *file,
2825 const char *attribute,
2827 GFileQueryInfoFlags flags,
2828 GCancellable *cancellable,
2831 return g_file_set_attribute (file, attribute,
2832 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
2833 flags, cancellable, error);
2837 * g_file_set_attribute_uint32:
2838 * @file: input #GFile.
2839 * @attribute: a string containing the attribute's name.
2840 * @value: a #guint32 containing the attribute's new value.
2841 * @flags: a #GFileQueryInfoFlags.
2842 * @cancellable: optional #GCancellable object, %NULL to ignore.
2843 * @error: a #GError, or %NULL
2845 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
2846 * If @attribute is of a different type, this operation will fail.
2848 * If @cancellable is not %NULL, then the operation can be cancelled by
2849 * triggering the cancellable object from another thread. If the operation
2850 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2852 * Returns: %TRUE if the @attribute was successfully set to @value
2853 * in the @file, %FALSE otherwise.
2856 g_file_set_attribute_uint32 (GFile *file,
2857 const char *attribute,
2859 GFileQueryInfoFlags flags,
2860 GCancellable *cancellable,
2863 return g_file_set_attribute (file, attribute,
2864 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
2865 flags, cancellable, error);
2869 * g_file_set_attribute_int32:
2870 * @file: input #GFile.
2871 * @attribute: a string containing the attribute's name.
2872 * @value: a #gint32 containing the attribute's new value.
2873 * @flags: a #GFileQueryInfoFlags.
2874 * @cancellable: optional #GCancellable object, %NULL to ignore.
2875 * @error: a #GError, or %NULL
2877 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
2878 * If @attribute is of a different type, this operation will fail.
2880 * If @cancellable is not %NULL, then the operation can be cancelled by
2881 * triggering the cancellable object from another thread. If the operation
2882 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2884 * Returns: %TRUE if the @attribute was successfully set to @value
2885 * in the @file, %FALSE otherwise.
2888 g_file_set_attribute_int32 (GFile *file,
2889 const char *attribute,
2891 GFileQueryInfoFlags flags,
2892 GCancellable *cancellable,
2895 return g_file_set_attribute (file, attribute,
2896 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
2897 flags, cancellable, error);
2901 * g_file_set_attribute_uint64:
2902 * @file: input #GFile.
2903 * @attribute: a string containing the attribute's name.
2904 * @value: a #guint64 containing the attribute's new value.
2905 * @flags: a #GFileQueryInfoFlags.
2906 * @cancellable: optional #GCancellable object, %NULL to ignore.
2907 * @error: a #GError, or %NULL
2909 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
2910 * If @attribute is of a different type, this operation will fail.
2912 * If @cancellable is not %NULL, then the operation can be cancelled by
2913 * triggering the cancellable object from another thread. If the operation
2914 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2916 * Returns: %TRUE if the @attribute was successfully set to @value
2917 * in the @file, %FALSE otherwise.
2920 g_file_set_attribute_uint64 (GFile *file,
2921 const char *attribute,
2923 GFileQueryInfoFlags flags,
2924 GCancellable *cancellable,
2927 return g_file_set_attribute (file, attribute,
2928 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
2929 flags, cancellable, error);
2933 * g_file_set_attribute_int64:
2934 * @file: input #GFile.
2935 * @attribute: a string containing the attribute's name.
2936 * @value: a #guint64 containing the attribute's new value.
2937 * @flags: a #GFileQueryInfoFlags.
2938 * @cancellable: optional #GCancellable object, %NULL to ignore.
2939 * @error: a #GError, or %NULL
2941 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
2942 * If @attribute is of a different type, this operation will fail.
2944 * If @cancellable is not %NULL, then the operation can be cancelled by
2945 * triggering the cancellable object from another thread. If the operation
2946 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2948 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2951 g_file_set_attribute_int64 (GFile *file,
2952 const char *attribute,
2954 GFileQueryInfoFlags flags,
2955 GCancellable *cancellable,
2958 return g_file_set_attribute (file, attribute,
2959 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
2960 flags, cancellable, error);
2964 * g_file_mount_mountable:
2965 * @file: input #GFile.
2966 * @mount_operation: a #GMountOperation, or %NULL.
2967 * @cancellable: optional #GCancellable object, %NULL to ignore.
2968 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2969 * @user_data: the data to pass to callback function
2971 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
2972 * You can speciy using @mount_operation to get callbacks when for instance
2973 * passwords are needed during authentication.
2975 * If @cancellable is not %NULL, then the operation can be cancelled by
2976 * triggering the cancellable object from another thread. If the operation
2977 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2979 * When the operation is finished, @callback will be called. You can then call
2980 * g_file_mount_mountable_finish() to get the result of the operation.
2983 g_file_mount_mountable (GFile *file,
2984 GMountOperation *mount_operation,
2985 GCancellable *cancellable,
2986 GAsyncReadyCallback callback,
2991 g_return_if_fail (G_IS_FILE (file));
2992 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
2994 iface = G_FILE_GET_IFACE (file);
2996 if (iface->mount_mountable == NULL)
2997 g_simple_async_report_error_in_idle (G_OBJECT (file),
3001 G_IO_ERROR_NOT_SUPPORTED,
3002 _("Operation not supported"));
3004 (* iface->mount_mountable) (file,
3012 * g_file_mount_mountable_finish:
3013 * @file: input #GFile.
3014 * @result: a #GAsyncResult.
3015 * @error: a #GError, or %NULL
3017 * Finishes a mount operation. See g_file_mount_mountable() for details.
3019 * Finish an asynchronous mount operation that was started
3020 * with g_file_mount_mountable().
3022 * Returns: a #GFile or %NULL on error.
3025 g_file_mount_mountable_finish (GFile *file,
3026 GAsyncResult *result,
3031 g_return_val_if_fail (G_IS_FILE (file), NULL);
3032 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3034 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3036 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3037 if (g_simple_async_result_propagate_error (simple, error))
3041 iface = G_FILE_GET_IFACE (file);
3042 return (* iface->mount_mountable_finish) (file, result, error);
3046 * g_file_unmount_mountable:
3047 * @file: input #GFile.
3048 * @cancellable: optional #GCancellable object, %NULL to ignore.
3049 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3050 * @user_data: the data to pass to callback function
3052 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3054 * If @cancellable is not %NULL, then the operation can be cancelled by
3055 * triggering the cancellable object from another thread. If the operation
3056 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3058 * When the operation is finished, @callback will be called. You can then call
3059 * g_file_mount_mountable_finish() to get the result of the operation.
3062 g_file_unmount_mountable (GFile *file,
3063 GCancellable *cancellable,
3064 GAsyncReadyCallback callback,
3069 g_return_if_fail (G_IS_FILE (file));
3071 iface = G_FILE_GET_IFACE (file);
3073 if (iface->unmount_mountable == NULL)
3074 g_simple_async_report_error_in_idle (G_OBJECT (file),
3078 G_IO_ERROR_NOT_SUPPORTED,
3079 _("Operation not supported"));
3081 (* iface->unmount_mountable) (file,
3088 * g_file_unmount_mountable_finish:
3089 * @file: input #GFile.
3090 * @result: a #GAsyncResult.
3091 * @error: a #GError, or %NULL
3093 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3095 * Finish an asynchronous unmount operation that was started
3096 * with g_file_unmount_mountable().
3098 * Returns: %TRUE if the operation finished successfully. %FALSE
3102 g_file_unmount_mountable_finish (GFile *file,
3103 GAsyncResult *result,
3108 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3109 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3111 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3113 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3114 if (g_simple_async_result_propagate_error (simple, error))
3118 iface = G_FILE_GET_IFACE (file);
3119 return (* iface->unmount_mountable_finish) (file, result, error);
3123 * g_file_eject_mountable:
3124 * @file: input #GFile.
3125 * @cancellable: optional #GCancellable object, %NULL to ignore.
3126 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3127 * @user_data: the data to pass to callback function
3129 * Starts an asynchronous eject on a mountable.
3130 * When this operation has completed, @callback will be called with
3131 * @user_user data, and the operation can be finalized with
3132 * g_file_eject_mountable_finish().
3134 * If @cancellable is not %NULL, then the operation can be cancelled by
3135 * triggering the cancellable object from another thread. If the operation
3136 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3139 g_file_eject_mountable (GFile *file,
3140 GCancellable *cancellable,
3141 GAsyncReadyCallback callback,
3146 g_return_if_fail (G_IS_FILE (file));
3148 iface = G_FILE_GET_IFACE (file);
3150 if (iface->eject_mountable == NULL)
3151 g_simple_async_report_error_in_idle (G_OBJECT (file),
3155 G_IO_ERROR_NOT_SUPPORTED,
3156 _("Operation not supported"));
3158 (* iface->eject_mountable) (file,
3165 * g_file_eject_mountable_finish:
3166 * @file: input #GFile.
3167 * @result: a #GAsyncResult.
3168 * @error: a #GError, or %NULL
3170 * Finishes an asynchronous eject operation started by
3171 * g_file_eject_mountable().
3173 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3177 g_file_eject_mountable_finish (GFile *file,
3178 GAsyncResult *result,
3183 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3184 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3186 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3188 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3189 if (g_simple_async_result_propagate_error (simple, error))
3193 iface = G_FILE_GET_IFACE (file);
3194 return (* iface->eject_mountable_finish) (file, result, error);
3198 * g_file_monitor_directory:
3199 * @file: input #GFile.
3200 * @flags: a set of #GFileMonitorFlags.
3201 * @cancellable: optional #GCancellable object, %NULL to ignore.
3203 * Obtains a directory monitor for the given file.
3204 * This may fail if directory monitoring is not supported.
3206 * If @cancellable is not %NULL, then the operation can be cancelled by
3207 * triggering the cancellable object from another thread. If the operation
3208 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3210 * Returns: a #GDirectoryMonitor for the given @file,
3211 * or %NULL on error.
3214 g_file_monitor_directory (GFile *file,
3215 GFileMonitorFlags flags,
3216 GCancellable *cancellable)
3220 g_return_val_if_fail (G_IS_FILE (file), NULL);
3222 iface = G_FILE_GET_IFACE (file);
3224 if (iface->monitor_dir == NULL)
3227 return (* iface->monitor_dir) (file, flags, cancellable);
3231 * g_file_monitor_file:
3232 * @file: input #GFile.
3233 * @flags: a set of #GFileMonitorFlags.
3234 * @cancellable: optional #GCancellable object, %NULL to ignore.
3236 * Obtains a file monitor for the given file. If no file notification
3237 * mechanism exists, then regular polling of the file is used.
3239 * If @cancellable is not %NULL, then the operation can be cancelled by
3240 * triggering the cancellable object from another thread. If the operation
3241 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3243 * Returns: a #GFileMonitor for the given @file.
3246 g_file_monitor_file (GFile *file,
3247 GFileMonitorFlags flags,
3248 GCancellable *cancellable)
3251 GFileMonitor *monitor;
3253 g_return_val_if_fail (G_IS_FILE (file), NULL);
3255 iface = G_FILE_GET_IFACE (file);
3259 if (iface->monitor_file)
3260 monitor = (* iface->monitor_file) (file, flags, cancellable);
3262 /* Fallback to polling */
3263 if (monitor == NULL)
3264 monitor = _g_poll_file_monitor_new (file);
3269 /********************************************
3270 * Default implementation of async ops *
3271 ********************************************/
3275 GFileQueryInfoFlags flags;
3277 } QueryInfoAsyncData;
3280 query_info_data_free (QueryInfoAsyncData *data)
3283 g_object_unref (data->info);
3284 g_free (data->attributes);
3289 query_info_async_thread (GSimpleAsyncResult *res,
3291 GCancellable *cancellable)
3293 GError *error = NULL;
3294 QueryInfoAsyncData *data;
3297 data = g_simple_async_result_get_op_res_gpointer (res);
3299 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3303 g_simple_async_result_set_from_error (res, error);
3304 g_error_free (error);
3311 g_file_real_query_info_async (GFile *file,
3312 const char *attributes,
3313 GFileQueryInfoFlags flags,
3315 GCancellable *cancellable,
3316 GAsyncReadyCallback callback,
3319 GSimpleAsyncResult *res;
3320 QueryInfoAsyncData *data;
3322 data = g_new0 (QueryInfoAsyncData, 1);
3323 data->attributes = g_strdup (attributes);
3324 data->flags = flags;
3326 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3327 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3329 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3330 g_object_unref (res);
3334 g_file_real_query_info_finish (GFile *file,
3338 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3339 QueryInfoAsyncData *data;
3341 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3343 data = g_simple_async_result_get_op_res_gpointer (simple);
3345 return g_object_ref (data->info);
3352 GFileQueryInfoFlags flags;
3353 GFileEnumerator *enumerator;
3354 } EnumerateChildrenAsyncData;
3357 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3359 if (data->enumerator)
3360 g_object_unref (data->enumerator);
3361 g_free (data->attributes);
3366 enumerate_children_async_thread (GSimpleAsyncResult *res,
3368 GCancellable *cancellable)
3370 GError *error = NULL;
3371 EnumerateChildrenAsyncData *data;
3372 GFileEnumerator *enumerator;
3374 data = g_simple_async_result_get_op_res_gpointer (res);
3376 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3378 if (enumerator == NULL)
3380 g_simple_async_result_set_from_error (res, error);
3381 g_error_free (error);
3384 data->enumerator = enumerator;
3388 g_file_real_enumerate_children_async (GFile *file,
3389 const char *attributes,
3390 GFileQueryInfoFlags flags,
3392 GCancellable *cancellable,
3393 GAsyncReadyCallback callback,
3396 GSimpleAsyncResult *res;
3397 EnumerateChildrenAsyncData *data;
3399 data = g_new0 (EnumerateChildrenAsyncData, 1);
3400 data->attributes = g_strdup (attributes);
3401 data->flags = flags;
3403 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3404 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3406 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3407 g_object_unref (res);
3410 static GFileEnumerator *
3411 g_file_real_enumerate_children_finish (GFile *file,
3415 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3416 EnumerateChildrenAsyncData *data;
3418 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3420 data = g_simple_async_result_get_op_res_gpointer (simple);
3421 if (data->enumerator)
3422 return g_object_ref (data->enumerator);
3428 open_read_async_thread (GSimpleAsyncResult *res,
3430 GCancellable *cancellable)
3433 GFileInputStream *stream;
3434 GError *error = NULL;
3436 iface = G_FILE_GET_IFACE (object);
3438 stream = iface->read_fn (G_FILE (object), cancellable, &error);
3442 g_simple_async_result_set_from_error (res, error);
3443 g_error_free (error);
3446 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3450 g_file_real_read_async (GFile *file,
3452 GCancellable *cancellable,
3453 GAsyncReadyCallback callback,
3456 GSimpleAsyncResult *res;
3458 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3460 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3461 g_object_unref (res);
3464 static GFileInputStream *
3465 g_file_real_read_finish (GFile *file,
3469 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3472 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3474 op = g_simple_async_result_get_op_res_gpointer (simple);
3476 return g_object_ref (op);
3482 append_to_async_thread (GSimpleAsyncResult *res,
3484 GCancellable *cancellable)
3487 GFileCreateFlags *data;
3488 GFileOutputStream *stream;
3489 GError *error = NULL;
3491 iface = G_FILE_GET_IFACE (object);
3493 data = g_simple_async_result_get_op_res_gpointer (res);
3495 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3499 g_simple_async_result_set_from_error (res, error);
3500 g_error_free (error);
3503 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3507 g_file_real_append_to_async (GFile *file,
3508 GFileCreateFlags flags,
3510 GCancellable *cancellable,
3511 GAsyncReadyCallback callback,
3514 GFileCreateFlags *data;
3515 GSimpleAsyncResult *res;
3517 data = g_new0 (GFileCreateFlags, 1);
3520 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3521 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3523 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3524 g_object_unref (res);
3527 static GFileOutputStream *
3528 g_file_real_append_to_finish (GFile *file,
3532 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3535 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3537 op = g_simple_async_result_get_op_res_gpointer (simple);
3539 return g_object_ref (op);
3545 create_async_thread (GSimpleAsyncResult *res,
3547 GCancellable *cancellable)
3550 GFileCreateFlags *data;
3551 GFileOutputStream *stream;
3552 GError *error = NULL;
3554 iface = G_FILE_GET_IFACE (object);
3556 data = g_simple_async_result_get_op_res_gpointer (res);
3558 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3562 g_simple_async_result_set_from_error (res, error);
3563 g_error_free (error);
3566 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3570 g_file_real_create_async (GFile *file,
3571 GFileCreateFlags flags,
3573 GCancellable *cancellable,
3574 GAsyncReadyCallback callback,
3577 GFileCreateFlags *data;
3578 GSimpleAsyncResult *res;
3580 data = g_new0 (GFileCreateFlags, 1);
3583 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3584 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3586 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3587 g_object_unref (res);
3590 static GFileOutputStream *
3591 g_file_real_create_finish (GFile *file,
3595 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3598 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3600 op = g_simple_async_result_get_op_res_gpointer (simple);
3602 return g_object_ref (op);
3608 GFileOutputStream *stream;
3610 gboolean make_backup;
3611 GFileCreateFlags flags;
3615 replace_async_data_free (ReplaceAsyncData *data)
3618 g_object_unref (data->stream);
3619 g_free (data->etag);
3624 replace_async_thread (GSimpleAsyncResult *res,
3626 GCancellable *cancellable)
3629 GFileOutputStream *stream;
3630 GError *error = NULL;
3631 ReplaceAsyncData *data;
3633 iface = G_FILE_GET_IFACE (object);
3635 data = g_simple_async_result_get_op_res_gpointer (res);
3637 stream = iface->replace (G_FILE (object),
3646 g_simple_async_result_set_from_error (res, error);
3647 g_error_free (error);
3650 data->stream = stream;
3654 g_file_real_replace_async (GFile *file,
3656 gboolean make_backup,
3657 GFileCreateFlags flags,
3659 GCancellable *cancellable,
3660 GAsyncReadyCallback callback,
3663 GSimpleAsyncResult *res;
3664 ReplaceAsyncData *data;
3666 data = g_new0 (ReplaceAsyncData, 1);
3667 data->etag = g_strdup (etag);
3668 data->make_backup = make_backup;
3669 data->flags = flags;
3671 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3672 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3674 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3675 g_object_unref (res);
3678 static GFileOutputStream *
3679 g_file_real_replace_finish (GFile *file,
3683 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3684 ReplaceAsyncData *data;
3686 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3688 data = g_simple_async_result_get_op_res_gpointer (simple);
3690 return g_object_ref (data->stream);
3698 } SetDisplayNameAsyncData;
3701 set_display_name_data_free (SetDisplayNameAsyncData *data)
3703 g_free (data->name);
3705 g_object_unref (data->file);
3710 set_display_name_async_thread (GSimpleAsyncResult *res,
3712 GCancellable *cancellable)
3714 GError *error = NULL;
3715 SetDisplayNameAsyncData *data;
3718 data = g_simple_async_result_get_op_res_gpointer (res);
3720 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3724 g_simple_async_result_set_from_error (res, error);
3725 g_error_free (error);
3732 g_file_real_set_display_name_async (GFile *file,
3733 const char *display_name,
3735 GCancellable *cancellable,
3736 GAsyncReadyCallback callback,
3739 GSimpleAsyncResult *res;
3740 SetDisplayNameAsyncData *data;
3742 data = g_new0 (SetDisplayNameAsyncData, 1);
3743 data->name = g_strdup (display_name);
3745 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3746 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3748 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3749 g_object_unref (res);
3753 g_file_real_set_display_name_finish (GFile *file,
3757 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3758 SetDisplayNameAsyncData *data;
3760 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3762 data = g_simple_async_result_get_op_res_gpointer (simple);
3764 return g_object_ref (data->file);
3770 GFileQueryInfoFlags flags;
3777 set_info_data_free (SetInfoAsyncData *data)
3780 g_object_unref (data->info);
3782 g_error_free (data->error);
3787 set_info_async_thread (GSimpleAsyncResult *res,
3789 GCancellable *cancellable)
3791 SetInfoAsyncData *data;
3793 data = g_simple_async_result_get_op_res_gpointer (res);
3796 data->res = g_file_set_attributes_from_info (G_FILE (object),
3804 g_file_real_set_attributes_async (GFile *file,
3806 GFileQueryInfoFlags flags,
3808 GCancellable *cancellable,
3809 GAsyncReadyCallback callback,
3812 GSimpleAsyncResult *res;
3813 SetInfoAsyncData *data;
3815 data = g_new0 (SetInfoAsyncData, 1);
3816 data->info = g_file_info_dup (info);
3817 data->flags = flags;
3819 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
3820 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
3822 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
3823 g_object_unref (res);
3827 g_file_real_set_attributes_finish (GFile *file,
3832 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3833 SetInfoAsyncData *data;
3835 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
3837 data = g_simple_async_result_get_op_res_gpointer (simple);
3840 *info = g_object_ref (data->info);
3842 if (error != NULL && data->error)
3843 *error = g_error_copy (data->error);
3848 /********************************************
3849 * Default VFS operations *
3850 ********************************************/
3853 * g_file_new_for_path:
3854 * @path: a string containing a relative or absolute path.
3856 * Constructs a #GFile for a given path. This operation never
3857 * fails, but the returned object might not support any I/O
3858 * operation if @path is malformed.
3860 * Returns: a new #GFile for the given @path.
3863 g_file_new_for_path (const char *path)
3865 g_return_val_if_fail (path != NULL, NULL);
3867 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
3871 * g_file_new_for_uri:
3872 * @uri: a string containing a URI.
3874 * Constructs a #GFile for a given URI. This operation never
3875 * fails, but the returned object might not support any I/O
3876 * operation if @uri is malformed or if the uri type is
3879 * Returns: a #GFile for the given @uri.
3882 g_file_new_for_uri (const char *uri)
3884 g_return_val_if_fail (uri != NULL, NULL);
3886 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
3890 * g_file_parse_name:
3891 * @parse_name: a file name or path to be parsed.
3893 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
3894 * This operation never fails, but the returned object might not support any I/O
3895 * operation if the @parse_name cannot be parsed.
3897 * Returns: a new #GFile.
3900 g_file_parse_name (const char *parse_name)
3902 g_return_val_if_fail (parse_name != NULL, NULL);
3904 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
3908 is_valid_scheme_character (char c)
3910 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
3914 has_valid_scheme (const char *uri)
3920 if (!is_valid_scheme_character (*p))
3925 } while (is_valid_scheme_character (*p));
3931 * g_file_new_for_commandline_arg:
3932 * @arg: a command line string.
3934 * Creates a #GFile with the given argument from
3937 * Returns: a new #GFile.
3940 g_file_new_for_commandline_arg (const char *arg)
3946 g_return_val_if_fail (arg != NULL, NULL);
3948 if (g_path_is_absolute (arg))
3949 return g_file_new_for_path (arg);
3951 if (has_valid_scheme (arg))
3952 return g_file_new_for_uri (arg);
3954 current_dir = g_get_current_dir ();
3955 filename = g_build_filename (current_dir, arg, NULL);
3956 g_free (current_dir);
3958 file = g_file_new_for_path (filename);
3965 * g_file_mount_enclosing_volume:
3966 * @location: input #GFile.
3967 * @mount_operation: a #GMountOperation.
3968 * @cancellable: optional #GCancellable object, %NULL to ignore.
3969 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3970 * @user_data: the data to pass to callback function
3972 * Starts a @mount_operation, mounting the volume that contains the file @location.
3974 * When this operation has completed, @callback will be called with
3975 * @user_user data, and the operation can be finalized with
3976 * g_file_mount_enclosing_volume_finish().
3978 * If @cancellable is not %NULL, then the operation can be cancelled by
3979 * triggering the cancellable object from another thread. If the operation
3980 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3983 g_file_mount_enclosing_volume (GFile *location,
3984 GMountOperation *mount_operation,
3985 GCancellable *cancellable,
3986 GAsyncReadyCallback callback,
3991 g_return_if_fail (G_IS_FILE (location));
3992 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
3994 iface = G_FILE_GET_IFACE (location);
3996 if (iface->mount_enclosing_volume == NULL)
3998 g_simple_async_report_error_in_idle (G_OBJECT (location),
3999 callback, user_data,
4000 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4001 _("volume doesn't implement mount"));
4006 (* iface->mount_enclosing_volume) (location, mount_operation, cancellable, callback, user_data);
4011 * g_file_mount_enclosing_volume_finish:
4012 * @location: input #GFile.
4013 * @result: a #GAsyncResult.
4014 * @error: a #GError, or %NULL
4016 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4018 * Returns: %TRUE if successful. If an error
4019 * has occured, this function will return %FALSE and set @error
4020 * appropriately if present.
4023 g_file_mount_enclosing_volume_finish (GFile *location,
4024 GAsyncResult *result,
4029 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4030 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4032 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4034 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4035 if (g_simple_async_result_propagate_error (simple, error))
4039 iface = G_FILE_GET_IFACE (location);
4041 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4044 /********************************************
4045 * Utility functions *
4046 ********************************************/
4048 #define GET_CONTENT_BLOCK_SIZE 8192
4051 * g_file_load_contents:
4052 * @file: input #GFile.
4053 * @cancellable: optional #GCancellable object, %NULL to ignore.
4054 * @contents: a location to place the contents of the file.
4055 * @length: a location to place the length of the contents of the file.
4056 * @etag_out: a location to place the current entity tag for the file.
4057 * @error: a #GError, or %NULL
4059 * Loads the content of the file into memory, returning the size of
4060 * the data. The data is always zero terminated, but this is not
4061 * included in the resultant @length.
4063 * If @cancellable is not %NULL, then the operation can be cancelled by
4064 * triggering the cancellable object from another thread. If the operation
4065 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4067 * Returns: %TRUE if the @file's contents were successfully loaded.
4068 * %FALSE if there were errors..
4071 g_file_load_contents (GFile *file,
4072 GCancellable *cancellable,
4078 GFileInputStream *in;
4079 GByteArray *content;
4084 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4085 g_return_val_if_fail (contents != NULL, FALSE);
4087 in = g_file_read (file, cancellable, error);
4091 content = g_byte_array_new ();
4094 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4095 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4096 content->data + pos,
4097 GET_CONTENT_BLOCK_SIZE,
4098 cancellable, error)) > 0)
4101 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4108 info = g_file_input_stream_query_info (in,
4109 G_FILE_ATTRIBUTE_ETAG_VALUE,
4114 *etag_out = g_strdup (g_file_info_get_etag (info));
4115 g_object_unref (info);
4119 /* Ignore errors on close */
4120 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4121 g_object_unref (in);
4125 /* error is set already */
4126 g_byte_array_free (content, TRUE);
4133 /* Zero terminate (we got an extra byte allocated for this */
4134 content->data[pos] = 0;
4136 *contents = (char *)g_byte_array_free (content, FALSE);
4144 GCancellable *cancellable;
4145 GFileReadMoreCallback read_more_callback;
4146 GAsyncReadyCallback callback;
4148 GByteArray *content;
4155 load_contents_data_free (LoadContentsData *data)
4158 g_error_free (data->error);
4159 if (data->cancellable)
4160 g_object_unref (data->cancellable);
4162 g_byte_array_free (data->content, TRUE);
4163 g_free (data->etag);
4164 g_object_unref (data->file);
4169 load_contents_close_callback (GObject *obj,
4170 GAsyncResult *close_res,
4173 GInputStream *stream = G_INPUT_STREAM (obj);
4174 LoadContentsData *data = user_data;
4175 GSimpleAsyncResult *res;
4177 /* Ignore errors here, we're only reading anyway */
4178 g_input_stream_close_finish (stream, close_res, NULL);
4179 g_object_unref (stream);
4181 res = g_simple_async_result_new (G_OBJECT (data->file),
4184 g_file_load_contents_async);
4185 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4186 g_simple_async_result_complete (res);
4187 g_object_unref (res);
4191 load_contents_fstat_callback (GObject *obj,
4192 GAsyncResult *stat_res,
4195 GInputStream *stream = G_INPUT_STREAM (obj);
4196 LoadContentsData *data = user_data;
4199 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4203 data->etag = g_strdup (g_file_info_get_etag (info));
4204 g_object_unref (info);
4207 g_input_stream_close_async (stream, 0,
4209 load_contents_close_callback, data);
4213 load_contents_read_callback (GObject *obj,
4214 GAsyncResult *read_res,
4217 GInputStream *stream = G_INPUT_STREAM (obj);
4218 LoadContentsData *data = user_data;
4219 GError *error = NULL;
4222 read_size = g_input_stream_read_finish (stream, read_res, &error);
4226 /* Error or EOF, close the file */
4227 data->error = error;
4228 g_input_stream_close_async (stream, 0,
4230 load_contents_close_callback, data);
4232 else if (read_size == 0)
4234 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4235 G_FILE_ATTRIBUTE_ETAG_VALUE,
4238 load_contents_fstat_callback,
4241 else if (read_size > 0)
4243 data->pos += read_size;
4245 g_byte_array_set_size (data->content,
4246 data->pos + GET_CONTENT_BLOCK_SIZE);
4249 if (data->read_more_callback &&
4250 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4251 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4252 G_FILE_ATTRIBUTE_ETAG_VALUE,
4255 load_contents_fstat_callback,
4258 g_input_stream_read_async (stream,
4259 data->content->data + data->pos,
4260 GET_CONTENT_BLOCK_SIZE,
4263 load_contents_read_callback,
4269 load_contents_open_callback (GObject *obj,
4270 GAsyncResult *open_res,
4273 GFile *file = G_FILE (obj);
4274 GFileInputStream *stream;
4275 LoadContentsData *data = user_data;
4276 GError *error = NULL;
4277 GSimpleAsyncResult *res;
4279 stream = g_file_read_finish (file, open_res, &error);
4283 g_byte_array_set_size (data->content,
4284 data->pos + GET_CONTENT_BLOCK_SIZE);
4285 g_input_stream_read_async (G_INPUT_STREAM (stream),
4286 data->content->data + data->pos,
4287 GET_CONTENT_BLOCK_SIZE,
4290 load_contents_read_callback,
4296 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4300 g_simple_async_result_complete (res);
4301 g_error_free (error);
4302 load_contents_data_free (data);
4303 g_object_unref (res);
4308 * g_file_load_partial_contents_async:
4309 * @file: input #GFile.
4310 * @cancellable: optional #GCancellable object, %NULL to ignore.
4311 * @read_more_callback: a #GFileReadMoreCallback.
4312 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4313 * @user_data: the data to pass to the callback functions.
4315 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
4316 * used to stop reading from the file when appropriate, else this function
4317 * will behave exactly as g_file_load_contents_async(). This operation
4318 * can be finished by g_file_load_partial_contents_finish().
4320 * Users of this function should be aware that @user_data is passed to
4321 * both the @read_more_callback and the @callback.
4323 * If @cancellable is not %NULL, then the operation can be cancelled by
4324 * triggering the cancellable object from another thread. If the operation
4325 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4328 g_file_load_partial_contents_async (GFile *file,
4329 GCancellable *cancellable,
4330 GFileReadMoreCallback read_more_callback,
4331 GAsyncReadyCallback callback,
4334 LoadContentsData *data;
4336 g_return_if_fail (G_IS_FILE (file));
4338 data = g_new0 (LoadContentsData, 1);
4341 data->cancellable = g_object_ref (cancellable);
4342 data->read_more_callback = read_more_callback;
4343 data->callback = callback;
4344 data->user_data = user_data;
4345 data->content = g_byte_array_new ();
4346 data->file = g_object_ref (file);
4348 g_file_read_async (file,
4351 load_contents_open_callback,
4356 * g_file_load_partial_contents_finish:
4357 * @file: input #GFile.
4358 * @res: a #GAsyncResult.
4359 * @contents: a location to place the contents of the file.
4360 * @length: a location to place the length of the contents of the file.
4361 * @etag_out: a location to place the current entity tag for the file.
4362 * @error: a #GError, or %NULL
4364 * Finishes an asynchronous partial load operation that was started
4365 * with g_file_load_partial_contents_async().
4367 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4368 * present, it will be set appropriately.
4371 g_file_load_partial_contents_finish (GFile *file,
4378 GSimpleAsyncResult *simple;
4379 LoadContentsData *data;
4381 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4382 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4383 g_return_val_if_fail (contents != NULL, FALSE);
4385 simple = G_SIMPLE_ASYNC_RESULT (res);
4387 if (g_simple_async_result_propagate_error (simple, error))
4390 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4392 data = g_simple_async_result_get_op_res_gpointer (simple);
4396 g_propagate_error (error, data->error);
4405 *length = data->pos;
4409 *etag_out = data->etag;
4413 /* Zero terminate */
4414 g_byte_array_set_size (data->content, data->pos + 1);
4415 data->content->data[data->pos] = 0;
4417 *contents = (char *)g_byte_array_free (data->content, FALSE);
4418 data->content = NULL;
4424 * g_file_load_contents_async:
4425 * @file: input #GFile.
4426 * @cancellable: optional #GCancellable object, %NULL to ignore.
4427 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4428 * @user_data: the data to pass to callback function
4430 * Starts an asynchronous load of the @file's contents.
4431 * When the load operation has completed, @callback will be called
4432 * with @userdata. To finish the operation, call
4433 * g_file_load_contents_finish() with the #GAsyncResult returned by
4436 * If @cancellable is not %NULL, then the operation can be cancelled by
4437 * triggering the cancellable object from another thread. If the operation
4438 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4441 g_file_load_contents_async (GFile *file,
4442 GCancellable *cancellable,
4443 GAsyncReadyCallback callback,
4446 g_file_load_partial_contents_async (file,
4449 callback, user_data);
4453 * g_file_load_contents_finish:
4454 * @file: input #GFile.
4455 * @res: a #GAsyncResult.
4456 * @contents: a location to place the contents of the file.
4457 * @length: a location to place the length of the contents of the file.
4458 * @etag_out: a location to place the current entity tag for the file.
4459 * @error: a #GError, or %NULL
4461 * Finishes an asynchronous load of the @file's contents.
4462 * The contents are placed in @contents, and @length is set to the
4463 * size of the @contents string. If @etag_out is present, it will be
4464 * set to the new entity tag for the @file.
4466 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4467 * present, it will be set appropriately.
4470 g_file_load_contents_finish (GFile *file,
4477 return g_file_load_partial_contents_finish (file,
4486 * g_file_replace_contents:
4487 * @file: input #GFile.
4488 * @contents: a string containing the new contents for @file.
4489 * @length: the length of @contents in bytes.
4490 * @etag: the old <link linkend="gfile-etag">entity tag</link>
4492 * @make_backup: a #gboolean.
4493 * @flags: a set of #GFileCreateFlags.
4494 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4496 * @cancellable: optional #GCancellable object, %NULL to ignore.
4497 * @error: a #GError, or %NULL
4499 * Replaces the contents of @file with @contents of @length bytes.
4501 * If @etag is specified (not %NULL) any existing file must have that etag, or
4502 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
4504 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
4506 * If @cancellable is not %NULL, then the operation can be cancelled by
4507 * triggering the cancellable object from another thread. If the operation
4508 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4510 * The returned @new_etag can be used to verify that the file hasn't changed the
4511 * next time it is saved over.
4513 * Returns: %TRUE if successful. If an error
4514 * has occured, this function will return %FALSE and set @error
4515 * appropriately if present.
4518 g_file_replace_contents (GFile *file,
4519 const char *contents,
4522 gboolean make_backup,
4523 GFileCreateFlags flags,
4525 GCancellable *cancellable,
4528 GFileOutputStream *out;
4529 gsize pos, remainder;
4532 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4533 g_return_val_if_fail (contents != NULL, FALSE);
4535 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4541 while (remainder > 0 &&
4542 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4544 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4552 if (remainder > 0 && res < 0)
4554 /* Ignore errors on close */
4555 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4557 /* error is set already */
4561 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4565 *new_etag = g_file_output_stream_get_etag (out);
4573 GCancellable *cancellable;
4574 GAsyncReadyCallback callback;
4576 const char *content;
4580 } ReplaceContentsData;
4583 replace_contents_data_free (ReplaceContentsData *data)
4586 g_error_free (data->error);
4587 if (data->cancellable)
4588 g_object_unref (data->cancellable);
4589 g_object_unref (data->file);
4590 g_free (data->etag);
4595 replace_contents_close_callback (GObject *obj,
4596 GAsyncResult *close_res,
4599 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4600 ReplaceContentsData *data = user_data;
4601 GSimpleAsyncResult *res;
4603 /* Ignore errors here, we're only reading anyway */
4604 g_output_stream_close_finish (stream, close_res, NULL);
4605 g_object_unref (stream);
4607 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4609 res = g_simple_async_result_new (G_OBJECT (data->file),
4612 g_file_replace_contents_async);
4613 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4614 g_simple_async_result_complete (res);
4615 g_object_unref (res);
4619 replace_contents_write_callback (GObject *obj,
4620 GAsyncResult *read_res,
4623 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4624 ReplaceContentsData *data = user_data;
4625 GError *error = NULL;
4628 write_size = g_output_stream_write_finish (stream, read_res, &error);
4630 if (write_size <= 0)
4632 /* Error or EOF, close the file */
4634 data->error = error;
4635 g_output_stream_close_async (stream, 0,
4637 replace_contents_close_callback, data);
4639 else if (write_size > 0)
4641 data->pos += write_size;
4643 if (data->pos >= data->length)
4644 g_output_stream_close_async (stream, 0,
4646 replace_contents_close_callback, data);
4648 g_output_stream_write_async (stream,
4649 data->content + data->pos,
4650 data->length - data->pos,
4653 replace_contents_write_callback,
4659 replace_contents_open_callback (GObject *obj,
4660 GAsyncResult *open_res,
4663 GFile *file = G_FILE (obj);
4664 GFileOutputStream *stream;
4665 ReplaceContentsData *data = user_data;
4666 GError *error = NULL;
4667 GSimpleAsyncResult *res;
4669 stream = g_file_replace_finish (file, open_res, &error);
4673 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4674 data->content + data->pos,
4675 data->length - data->pos,
4678 replace_contents_write_callback,
4684 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4688 g_simple_async_result_complete (res);
4689 g_error_free (error);
4690 replace_contents_data_free (data);
4691 g_object_unref (res);
4696 * g_file_replace_contents_async:
4697 * @file: input #GFile.
4698 * @contents: string of contents to replace the file with.
4699 * @length: the length of @contents in bytes.
4700 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4701 * @make_backup: a #gboolean.
4702 * @flags: a set of #GFileCreateFlags.
4703 * @cancellable: optional #GCancellable object, %NULL to ignore.
4704 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4705 * @user_data: the data to pass to callback function
4707 * Starts an asynchronous replacement of @file with the given
4708 * @contents of @length bytes. @etag will replace the document's
4709 * current entity tag.
4711 * When this operation has completed, @callback will be called with
4712 * @user_user data, and the operation can be finalized with
4713 * g_file_replace_contents_finish().
4715 * If @cancellable is not %NULL, then the operation can be cancelled by
4716 * triggering the cancellable object from another thread. If the operation
4717 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4719 * If @make_backup is %TRUE, this function will attempt to
4720 * make a backup of @file.
4723 g_file_replace_contents_async (GFile *file,
4724 const char *contents,
4727 gboolean make_backup,
4728 GFileCreateFlags flags,
4729 GCancellable *cancellable,
4730 GAsyncReadyCallback callback,
4733 ReplaceContentsData *data;
4735 g_return_if_fail (G_IS_FILE (file));
4736 g_return_if_fail (contents != NULL);
4738 data = g_new0 (ReplaceContentsData, 1);
4741 data->cancellable = g_object_ref (cancellable);
4742 data->callback = callback;
4743 data->user_data = user_data;
4744 data->content = contents;
4745 data->length = length;
4747 data->file = g_object_ref (file);
4749 g_file_replace_async (file,
4755 replace_contents_open_callback,
4760 * g_file_replace_contents_finish:
4761 * @file: input #GFile.
4762 * @res: a #GAsyncResult.
4763 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
4765 * @error: a #GError, or %NULL
4767 * Finishes an asynchronous replace of the given @file. See
4768 * g_file_replace_contents_async(). Sets @new_etag to the new entity
4769 * tag for the document, if present.
4771 * Returns: %TRUE on success, %FALSE on failure.
4774 g_file_replace_contents_finish (GFile *file,
4779 GSimpleAsyncResult *simple;
4780 ReplaceContentsData *data;
4782 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4783 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4785 simple = G_SIMPLE_ASYNC_RESULT (res);
4787 if (g_simple_async_result_propagate_error (simple, error))
4790 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
4792 data = g_simple_async_result_get_op_res_gpointer (simple);
4796 g_propagate_error (error, data->error);
4804 *new_etag = data->etag;
4805 data->etag = NULL; /* Take ownership */
4811 #define __G_FILE_C__
4812 #include "gioaliasdef.c"