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 "gpollfilemonitor.h"
41 * @short_description: File and Directory Handling
42 * @see_also: #GFileInfo, #GFileEnumerator
43 * @include: gio/gfile.h
45 * #GFile is a high level abstraction for manipulating files on a
46 * virtual file system. #GFile<!-- -->s are lightweight, immutable
47 * objects that do no I/O upon creation. It is necessary to understand that
48 * #GFile objects do not represent files, merely a handle to a file. All
49 * file I/O is implemented as streaming operations (see #GInputStream and
52 * To construct a #GFile, you can use:
53 * g_file_new_for_path() if you have a path.
54 * g_file_new_for_uri() if you have a URI.
55 * g_file_new_for_commandline_arg() for a command line argument.
57 * You can move through the filesystem with #GFile handles with
58 * g_file_get_parent() to get a handle to the parent directory.
59 * g_file_get_child() to get a handle to a child within a directory.
60 * g_file_resolve_relative_path() to resolve a relative path between
61 * two #GFile<!-- -->s.
63 * Many #GFile operations have both synchronous and asynchronous versions
64 * to suit your application. Asynchronous versions of synchronous functions
65 * simply have _async() appended to their function names. The asynchronous
66 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
67 * the operation, which is then passed to the function's matching _finish()
70 * Some #GFile operations do not have synchronous analogs, as they may
71 * take a very long time to finish, and blocking may leave an application
72 * unusable. Notable cases include:
73 * g_file_mount_mountable() to mount a mountable file.
74 * g_file_unmount_mountable() to unmount a mountable file.
75 * g_file_eject_mountable() to eject a mountable file.
77 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
78 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
79 * short. Entity tags are somewhat like a more abstract version of the
80 * traditional mtime, and can be used to quickly determine if the file has
81 * been modified from the version on the file system. See the HTTP 1.1
82 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
83 * for HTTP Etag headers, which are a very similar concept.
87 static void g_file_base_init (gpointer g_class);
88 static void g_file_class_init (gpointer g_class,
91 static void g_file_real_query_info_async (GFile *file,
92 const char *attributes,
93 GFileQueryInfoFlags flags,
95 GCancellable *cancellable,
96 GAsyncReadyCallback callback,
98 static GFileInfo * g_file_real_query_info_finish (GFile *file,
101 static void g_file_real_enumerate_children_async (GFile *file,
102 const char *attributes,
103 GFileQueryInfoFlags flags,
105 GCancellable *cancellable,
106 GAsyncReadyCallback callback,
108 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
111 static void g_file_real_read_async (GFile *file,
113 GCancellable *cancellable,
114 GAsyncReadyCallback callback,
116 static GFileInputStream * g_file_real_read_finish (GFile *file,
119 static void g_file_real_append_to_async (GFile *file,
120 GFileCreateFlags flags,
122 GCancellable *cancellable,
123 GAsyncReadyCallback callback,
125 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
128 static void g_file_real_create_async (GFile *file,
129 GFileCreateFlags flags,
131 GCancellable *cancellable,
132 GAsyncReadyCallback callback,
134 static GFileOutputStream *g_file_real_create_finish (GFile *file,
137 static void g_file_real_replace_async (GFile *file,
139 gboolean make_backup,
140 GFileCreateFlags flags,
142 GCancellable *cancellable,
143 GAsyncReadyCallback callback,
145 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
148 static gboolean g_file_real_set_attributes_from_info (GFile *file,
150 GFileQueryInfoFlags flags,
151 GCancellable *cancellable,
153 static void g_file_real_set_display_name_async (GFile *file,
154 const char *display_name,
156 GCancellable *cancellable,
157 GAsyncReadyCallback callback,
159 static GFile * g_file_real_set_display_name_finish (GFile *file,
162 static void g_file_real_set_attributes_async (GFile *file,
164 GFileQueryInfoFlags flags,
166 GCancellable *cancellable,
167 GAsyncReadyCallback callback,
169 static gboolean g_file_real_set_attributes_finish (GFile *file,
175 g_file_get_type (void)
177 static GType file_type = 0;
181 static const GTypeInfo file_info =
183 sizeof (GFileIface), /* class_size */
184 g_file_base_init, /* base_init */
185 NULL, /* base_finalize */
187 NULL, /* class_finalize */
188 NULL, /* class_data */
195 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
198 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
205 g_file_class_init (gpointer g_class,
208 GFileIface *iface = g_class;
210 iface->enumerate_children_async = g_file_real_enumerate_children_async;
211 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
212 iface->set_display_name_async = g_file_real_set_display_name_async;
213 iface->set_display_name_finish = g_file_real_set_display_name_finish;
214 iface->query_info_async = g_file_real_query_info_async;
215 iface->query_info_finish = g_file_real_query_info_finish;
216 iface->set_attributes_async = g_file_real_set_attributes_async;
217 iface->set_attributes_finish = g_file_real_set_attributes_finish;
218 iface->read_async = g_file_real_read_async;
219 iface->read_finish = g_file_real_read_finish;
220 iface->append_to_async = g_file_real_append_to_async;
221 iface->append_to_finish = g_file_real_append_to_finish;
222 iface->create_async = g_file_real_create_async;
223 iface->create_finish = g_file_real_create_finish;
224 iface->replace_async = g_file_real_replace_async;
225 iface->replace_finish = g_file_real_replace_finish;
226 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
230 g_file_base_init (gpointer g_class)
237 * @file: input #GFile.
239 * Checks to see if a file is native to the platform.
241 * Returns: %TRUE if file is native. (If the #GFile<!---->'s expressed in
242 * the platform-native filename format, e.g. "C:\Windows", "/usr/bin/").
245 g_file_is_native (GFile *file)
249 g_return_val_if_fail (G_IS_FILE (file), FALSE);
251 iface = G_FILE_GET_IFACE (file);
253 return (* iface->is_native) (file);
258 * g_file_has_uri_scheme:
259 * @file: input #GFile.
260 * @uri_scheme: a string containing a URI scheme.
262 * Checks to see if a #GFile has a given URI scheme.
264 * Returns: %TRUE if #GFile's backend supports the
265 * given URI scheme, %FALSE if URI scheme is %NULL,
266 * not supported, or #GFile is invalid.
269 g_file_has_uri_scheme (GFile *file,
270 const char *uri_scheme)
274 g_return_val_if_fail (G_IS_FILE (file), FALSE);
275 g_return_val_if_fail (uri_scheme != NULL, FALSE);
277 iface = G_FILE_GET_IFACE (file);
279 return (* iface->has_uri_scheme) (file, uri_scheme);
284 * g_file_get_uri_scheme:
285 * @file: input #GFile.
287 * Gets the URI scheme for a #GFile.
288 * RFC 3986 decodes the scheme as:
290 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
292 * Common schemes include "file", "http", "svn", etc.
294 * Returns: a string containing the URI scheme for the given
295 * #GFile. The returned string should be freed with g_free()
296 * when no longer needed.
299 g_file_get_uri_scheme (GFile *file)
303 g_return_val_if_fail (G_IS_FILE (file), NULL);
305 iface = G_FILE_GET_IFACE (file);
307 return (* iface->get_uri_scheme) (file);
312 * g_file_get_basename:
313 * @file: input #GFile.
315 * Gets the basename for a given #GFile.
317 * Returns: string containing the #GFile's base name, or %NULL
318 * if given #GFile is invalid. The returned string should be
319 * freed with g_free() when no longer needed.
322 g_file_get_basename (GFile *file)
326 g_return_val_if_fail (G_IS_FILE (file), NULL);
328 iface = G_FILE_GET_IFACE (file);
330 return (* iface->get_basename) (file);
335 * @file: input #GFile.
337 * Gets the current path within a #GFile.
339 * Returns: string containing the #GFile's path, or %NULL if
340 * given #GFile is invalid. The returned string should be
341 * freed with g_free() when no longer needed.
344 g_file_get_path (GFile *file)
348 g_return_val_if_fail (G_IS_FILE (file), NULL);
350 iface = G_FILE_GET_IFACE (file);
352 return (* iface->get_path) (file);
357 * @file: input #GFile.
359 * Gets a URI for the path within a #GFile.
361 * Returns: a string containing the #GFile's URI or %NULL
362 * if given #GFile is invalid. The returned string should
363 * be freed with g_free() when no longer needed.
366 g_file_get_uri (GFile *file)
370 g_return_val_if_fail (G_IS_FILE (file), NULL);
372 iface = G_FILE_GET_IFACE (file);
374 return (* iface->get_uri) (file);
378 * g_file_get_parse_name:
379 * @file: input #GFile.
381 * Gets the UTF-8 parsed name for the #GFile.
383 * Returns: a string containing the #GFile's parsed name,
384 * or %NULL if given #GFile is invalid. The returned
385 * string should be freed with g_free() when no longer needed.
388 g_file_get_parse_name (GFile *file)
392 g_return_val_if_fail (G_IS_FILE (file), NULL);
394 iface = G_FILE_GET_IFACE (file);
396 return (* iface->get_parse_name) (file);
401 * @file: input #GFile.
403 * Duplicates a #GFile handle. This operation does not duplicate
404 * the actual file or directory represented by the #GFile; see
405 * g_file_copy() if attempting to copy a file.
407 * Returns: #GFile that is a duplicate of the given #GFile,
408 * or %NULL if given #GFile is invalid.
411 g_file_dup (GFile *file)
415 g_return_val_if_fail (G_IS_FILE (file), NULL);
417 iface = G_FILE_GET_IFACE (file);
419 return (* iface->dup) (file);
424 * @file: #gconstpointer to a #GFile.
426 * Creates a hash value for a #GFile.
428 * Returns: 0 if @file is not a valid #GFile, otherwise an
429 * integer that can be used as hash value for the #GFile.
430 * This function is intended for easily hashing a #GFile to
431 * add to a #GHashTable or similar data structure.
434 g_file_hash (gconstpointer file)
438 g_return_val_if_fail (G_IS_FILE (file), 0);
440 iface = G_FILE_GET_IFACE (file);
442 return (* iface->hash) ((GFile *)file);
447 * @file1: the first #GFile.
448 * @file2: the second #GFile.
450 * Checks equality of two given #GFile<!-- -->s
452 * Returns: %TRUE if @file1 and @file2 are equal.
453 * %FALSE if either is not a #GFile.
456 g_file_equal (GFile *file1,
461 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
462 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
464 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
467 iface = G_FILE_GET_IFACE (file1);
469 return (* iface->equal) (file1, file2);
475 * @file: input #GFile.
477 * Gets the parent directory for the @file.
478 * If the @file represents the root directory of the
479 * file system, then %NULL will be returned.
481 * Returns: a #GFile structure to the parent of the given
485 g_file_get_parent (GFile *file)
489 g_return_val_if_fail (G_IS_FILE (file), NULL);
491 iface = G_FILE_GET_IFACE (file);
493 return (* iface->get_parent) (file);
498 * @file: input #GFile.
499 * @name: string containing the child's name.
501 * Gets a specific child of @file with name equal to @name if
504 * Returns: a #GFile to a child specified by
505 * @name or %NULL if @name is %NULL, or the specified
506 * child doesn't exist.
509 g_file_get_child (GFile *file,
512 g_return_val_if_fail (G_IS_FILE (file), NULL);
513 g_return_val_if_fail (name != NULL, NULL);
515 return g_file_resolve_relative_path (file, name);
519 * g_file_get_child_for_display_name:
520 * @file: input #GFile.
521 * @display_name: string to a possible child.
524 * Gets the child of @file for a given @display_name. If
525 * this function fails, it returns %NULL and @error will be
526 * set with %G_IO_ERROR_INVALID_FILENAME.
528 * Returns: a #GFile to the specified child, or
529 * %NULL if @display_name is %NULL.
532 g_file_get_child_for_display_name (GFile *file,
533 const char *display_name,
538 g_return_val_if_fail (G_IS_FILE (file), NULL);
539 g_return_val_if_fail (display_name != NULL, NULL);
541 iface = G_FILE_GET_IFACE (file);
543 return (* iface->get_child_for_display_name) (file, display_name, error);
547 * g_file_contains_file:
548 * @parent: input #GFile.
549 * @descendant: input #GFile.
551 * Checks whether @parent contains the specified @descendent.
553 * Returns: %TRUE if the @descendent's parent is @parent. %FALSE otherwise.
556 g_file_contains_file (GFile *parent,
561 g_return_val_if_fail (G_IS_FILE (parent), FALSE);
562 g_return_val_if_fail (G_IS_FILE (descendant), FALSE);
564 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
567 iface = G_FILE_GET_IFACE (parent);
569 return (* iface->contains_file) (parent, descendant);
573 * g_file_get_relative_path:
574 * @parent: input #GFile.
575 * @descendant: input #GFile.
577 * Gets the path for @descendant relative to @parent.
579 * Returns: string with the relative path from @descendant
580 * to @parent. The returned string should be freed with
581 * g_free() when no longer needed.
584 g_file_get_relative_path (GFile *parent,
589 g_return_val_if_fail (G_IS_FILE (parent), NULL);
590 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
592 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
595 iface = G_FILE_GET_IFACE (parent);
597 return (* iface->get_relative_path) (parent, descendant);
601 * g_file_resolve_relative_path:
602 * @file: input #GFile.
603 * @relative_path: a given relative path string.
605 * Resolves a relative path for @file to an absolute path.
607 * Returns: #GFile to the resolved path. %NULL if @relative_path
608 * is %NULL or if @file is invalid.
611 g_file_resolve_relative_path (GFile *file,
612 const char *relative_path)
616 g_return_val_if_fail (G_IS_FILE (file), NULL);
617 g_return_val_if_fail (relative_path != NULL, NULL);
619 iface = G_FILE_GET_IFACE (file);
621 return (* iface->resolve_relative_path) (file, relative_path);
625 * g_file_enumerate_children:
626 * @file: input #GFile.
627 * @attributes: a string containing a #GFileAttributeInfo query.
628 * @flags: a set of #GFileQueryInfoFlags.
629 * @cancellable: optional #GCancellable object, %NULL to ignore.
630 * @error: #GError for error reporting.
632 * Gets a #GFileEnumerator for the children of @file. The returned enumerator will
633 * automatically generate a #GFileAttributeMatcher internally that match @attributes,
634 * where attributes is a #GFileAttributeInfo query string (e.g. "std:type",
635 * "std:*"). See g_file_enumerator_next_file() for details.
637 * If @cancellable is not %NULL, then the operation can be cancelled by
638 * triggering the cancellable object from another thread. If the operation
639 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
641 * If the #GFileIface for the given @file does not support #GFileEnumerator,
642 * then %NULL will be returned and the error %G_IO_ERROR_NOT_SUPPORTED will
645 * Returns: A #GFileEnumerator if successful. %NULL if cancelled or if #GFile's
646 * backend doesn't support #GFileEnumerator.
649 g_file_enumerate_children (GFile *file,
650 const char *attributes,
651 GFileQueryInfoFlags flags,
652 GCancellable *cancellable,
658 g_return_val_if_fail (G_IS_FILE (file), NULL);
660 if (g_cancellable_set_error_if_cancelled (cancellable, error))
663 iface = G_FILE_GET_IFACE (file);
665 if (iface->enumerate_children == NULL)
667 g_set_error (error, G_IO_ERROR,
668 G_IO_ERROR_NOT_SUPPORTED,
669 _("Operation not supported"));
673 return (* iface->enumerate_children) (file, attributes, flags,
678 * g_file_enumerate_children_async:
679 * @file: input #GFile.
680 * @attributes: a string containing a #GFileAttributeInfo query.
681 * @flags: a set of #GFileQueryInfoFlags.
682 * @io_priority: the <link linkend="io-priority">I/O priority</link>
684 * @cancellable: optional #GCancellable object, %NULL to ignore.
685 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
686 * @user_data: the data to pass to callback function
688 * Asynchronously gets a #GFileEnumerator for the children of @file. The returned
689 * file enumerator will automatically generate a #GFileAttributeMatcher
690 * that matches @attributes, where attributes is a #GFileAttributeInfo query
691 * string (e.g. "std:type", "std:*"). See g_file_enumerator_next_file() for details.
692 * For the synchronous version of this function, see g_file_enumerate_children().
694 * To finish this asynchronous operation, see g_file_enumerate_children_finish().
697 g_file_enumerate_children_async (GFile *file,
698 const char *attributes,
699 GFileQueryInfoFlags flags,
701 GCancellable *cancellable,
702 GAsyncReadyCallback callback,
707 g_return_if_fail (G_IS_FILE (file));
709 iface = G_FILE_GET_IFACE (file);
710 (* iface->enumerate_children_async) (file,
720 * g_file_enumerate_children_finish:
721 * @file: input #GFile.
722 * @res: a #GAsyncResult.
725 * Finishes an async enumerate children operation.
727 * If @cancellable was not %NULL when g_file_enumerate_children_async()
728 * was called, then the operation could have been cancelled by triggering
729 * the cancellable object from another thread. If the operation was cancelled,
730 * the @error will be set to %G_IO_ERROR_CANCELLED and this function will
733 * If the #GFileIface for the given @file does not support enumerating files,
734 * then %NULL will be returned and the error %G_IO_ERROR_NOT_SUPPORTED will
737 * Returns: a #GFileEnumerator or %NULL if an error occurred.
740 g_file_enumerate_children_finish (GFile *file,
746 g_return_val_if_fail (G_IS_FILE (file), NULL);
747 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
749 if (G_IS_SIMPLE_ASYNC_RESULT (res))
751 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
752 if (g_simple_async_result_propagate_error (simple, error))
756 iface = G_FILE_GET_IFACE (file);
757 return (* iface->enumerate_children_finish) (file, res, error);
763 * @file: input #GFile.
764 * @attributes: a string containing a #GFileAttributeInfo query.
765 * @flags: a set of #GFileQueryInfoFlags.
766 * @cancellable: optional #GCancellable object, %NULL to ignore.
769 * If @cancellable is not %NULL, then the operation can be cancelled by
770 * triggering the cancellable object from another thread. If the operation
771 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
773 * If the #GFileIface for the given @file does not support querying file
774 * information, then %NULL will be returned and the error
775 * %G_IO_ERROR_NOT_SUPPORTED will be set in @error.
777 * Returns: a #GFileInfo for the given @file, or %NULL on error.
780 g_file_query_info (GFile *file,
781 const char *attributes,
782 GFileQueryInfoFlags flags,
783 GCancellable *cancellable,
788 g_return_val_if_fail (G_IS_FILE (file), NULL);
790 if (g_cancellable_set_error_if_cancelled (cancellable, error))
793 iface = G_FILE_GET_IFACE (file);
795 if (iface->query_info == NULL)
797 g_set_error (error, G_IO_ERROR,
798 G_IO_ERROR_NOT_SUPPORTED,
799 _("Operation not supported"));
803 return (* iface->query_info) (file, attributes, flags, cancellable, error);
807 * g_file_query_info_async:
808 * @file: input #GFile.
809 * @attributes: a string containing a #GFileAttributeInfo query.
810 * @flags: a set of #GFileQueryInfoFlags.
811 * @io_priority: the <link linkend="io-priority">I/O priority</link>
813 * @cancellable: optional #GCancellable object, %NULL to ignore.
814 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
815 * @user_data: the data to pass to callback function
817 * If @cancellable is not %NULL, then the operation can be cancelled by
818 * triggering the cancellable object from another thread. If the operation
819 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
821 * To finish this asynchronous operation, see g_file_query_info_finish().
824 g_file_query_info_async (GFile *file,
825 const char *attributes,
826 GFileQueryInfoFlags flags,
828 GCancellable *cancellable,
829 GAsyncReadyCallback callback,
834 g_return_if_fail (G_IS_FILE (file));
836 iface = G_FILE_GET_IFACE (file);
837 (* iface->query_info_async) (file,
847 * g_file_query_info_finish:
848 * @file: input #GFile.
849 * @res: a #GAsyncResult.
852 * Finishes an asynchronous file info query.
854 * If @cancellable was not %NULL when g_file_query_info_async() was called,
855 * then the operation could have been cancelled by triggering the cancellable
856 * object from another thread. If the operation was cancelled, the @error will
857 * be set to %G_IO_ERROR_CANCELLED and this function will return %NULL.
859 * If the #GFileIface for the given @file does not support querying file
860 * information, then %NULL will be returned and the error
861 * %G_IO_ERROR_NOT_SUPPORTED will be set in @error.
863 * Returns: #GFileInfo for given @file or %NULL on error.
866 g_file_query_info_finish (GFile *file,
872 g_return_val_if_fail (G_IS_FILE (file), NULL);
873 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
875 if (G_IS_SIMPLE_ASYNC_RESULT (res))
877 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
878 if (g_simple_async_result_propagate_error (simple, error))
882 iface = G_FILE_GET_IFACE (file);
883 return (* iface->query_info_finish) (file, res, error);
887 * g_file_query_filesystem_info:
888 * @file: input #GFile.
889 * @attributes: a string containing a #GFileAttributeInfo query.
890 * @cancellable: optional #GCancellable object, %NULL to ignore.
893 * Obtains attributes of a #GFile.
895 * If @cancellable is not %NULL, then the operation can be cancelled by
896 * triggering the cancellable object from another thread. If the operation
897 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
899 * If the #GFileIface for the given @file does not support querying file system
900 * information, then %NULL will be returned and the error
901 * %G_IO_ERROR_NOT_SUPPORTED will be set in @error.
903 * Returns: a #GFileInfo or %NULL if there was an error.
906 g_file_query_filesystem_info (GFile *file,
907 const char *attributes,
908 GCancellable *cancellable,
913 g_return_val_if_fail (G_IS_FILE (file), NULL);
915 if (g_cancellable_set_error_if_cancelled (cancellable, error))
918 iface = G_FILE_GET_IFACE (file);
920 if (iface->query_filesystem_info == NULL)
922 g_set_error (error, G_IO_ERROR,
923 G_IO_ERROR_NOT_SUPPORTED,
924 _("Operation not supported"));
928 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
932 * g_file_find_enclosing_mount:
933 * @file: input #GFile.
934 * @cancellable: optional #GCancellable object, %NULL to ignore.
937 * Gets a #GMount for the #GFile.
939 * If the #GFileIface for @file does not have a mount (e.g. possibly a
940 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
943 * If @cancellable is not %NULL, then the operation can be cancelled by
944 * triggering the cancellable object from another thread. If the operation
945 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
947 * Returns: a #GMount where the @file is located or %NULL on error.
950 g_file_find_enclosing_mount (GFile *file,
951 GCancellable *cancellable,
956 g_return_val_if_fail (G_IS_FILE (file), NULL);
958 if (g_cancellable_set_error_if_cancelled (cancellable, error))
961 iface = G_FILE_GET_IFACE (file);
962 if (iface->find_enclosing_mount == NULL)
964 g_set_error (error, G_IO_ERROR,
965 G_IO_ERROR_NOT_FOUND,
966 _("Containing mount does not exist"));
970 return (* iface->find_enclosing_mount) (file, cancellable, error);
975 * @file: #GFile to read.
976 * @cancellable: a #GCancellable
977 * @error: a #GError, or %NULL
979 * Reads a whole file into a #GFileInputStream. Fails returning %NULL if
980 * given #GFile points to a directory.
982 * If the #GFileIface for @file does not support reading files, then
983 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
985 * If @cancellable is not %NULL, then the operation can be cancelled by
986 * triggering the cancellable object from another thread. If the operation
987 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
989 * Returns: #GFileInputStream or %NULL on error.
992 g_file_read (GFile *file,
993 GCancellable *cancellable,
998 g_return_val_if_fail (G_IS_FILE (file), NULL);
1000 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1003 iface = G_FILE_GET_IFACE (file);
1005 if (iface->read_fn == NULL)
1007 g_set_error (error, G_IO_ERROR,
1008 G_IO_ERROR_NOT_SUPPORTED,
1009 _("Operation not supported"));
1013 return (* iface->read_fn) (file, cancellable, error);
1018 * @file: input #GFile.
1019 * @flags: a set of #GFileCreateFlags.
1020 * @cancellable: optional #GCancellable object, %NULL to ignore.
1021 * @error: a #GError, or %NULL
1023 * Gets an output stream for appending to the file.
1025 * If the #GFileIface for @file does not support appending to files,
1026 * then @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will
1029 * If @cancellable is not %NULL, then the operation can be cancelled by
1030 * triggering the cancellable object from another thread. If the operation
1031 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1033 * Returns: a #GFileOutputStream.
1036 g_file_append_to (GFile *file,
1037 GFileCreateFlags flags,
1038 GCancellable *cancellable,
1043 g_return_val_if_fail (G_IS_FILE (file), NULL);
1045 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1048 iface = G_FILE_GET_IFACE (file);
1050 if (iface->append_to == NULL)
1052 g_set_error (error, G_IO_ERROR,
1053 G_IO_ERROR_NOT_SUPPORTED,
1054 _("Operation not supported"));
1058 return (* iface->append_to) (file, flags, cancellable, error);
1063 * @file: input #GFile.
1064 * @flags: a set of #GFileCreateFlags.
1065 * @cancellable: optional #GCancellable object, %NULL to ignore.
1066 * @error: a #GError, or %NULL
1068 * Creates the file and returns an output stream for writing to it.
1070 * If the #GFileIface for @file does not support creating files, then
1071 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
1073 * If @cancellable is not %NULL, then the operation can be cancelled by
1074 * triggering the cancellable object from another thread. If the operation
1075 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1077 * Returns: a #GFileOutputStream for the newly created file, or
1081 g_file_create (GFile *file,
1082 GFileCreateFlags flags,
1083 GCancellable *cancellable,
1088 g_return_val_if_fail (G_IS_FILE (file), NULL);
1090 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1093 iface = G_FILE_GET_IFACE (file);
1095 if (iface->create == NULL)
1097 g_set_error (error, G_IO_ERROR,
1098 G_IO_ERROR_NOT_SUPPORTED,
1099 _("Operation not supported"));
1103 return (* iface->create) (file, flags, cancellable, error);
1108 * @file: input #GFile.
1109 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1111 * @make_backup: %TRUE if a backup should be created`.
1112 * @flags: a set of #GFileCreateFlags.
1113 * @cancellable: optional #GCancellable object, %NULL to ignore.
1114 * @error: a #GError, or %NULL
1116 * Returns an output stream for overwriting the file, possibly
1117 * creating a backup copy of the file first.
1119 * If the #GFileIface for @file does not support streaming operations,
1120 * then @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will
1123 * If @cancellable is not %NULL, then the operation can be cancelled by
1124 * triggering the cancellable object from another thread. If the operation
1125 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1127 * @etag will replace the entity tag for the current file.
1129 * Returns: a #GFileOutputStream or %NULL on error. If @make_backup is
1130 * %TRUE, this function will attempt to make a backup of the current
1134 g_file_replace (GFile *file,
1136 gboolean make_backup,
1137 GFileCreateFlags flags,
1138 GCancellable *cancellable,
1143 g_return_val_if_fail (G_IS_FILE (file), NULL);
1145 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1148 iface = G_FILE_GET_IFACE (file);
1150 if (iface->replace == NULL)
1152 g_set_error (error, G_IO_ERROR,
1153 G_IO_ERROR_NOT_SUPPORTED,
1154 _("Operation not supported"));
1159 /* Handle empty tag string as NULL in consistent way. */
1160 if (etag && *etag == 0)
1163 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1167 * g_file_read_async:
1168 * @file: input #GFile.
1169 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1171 * @cancellable: optional #GCancellable object, %NULL to ignore.
1172 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1173 * @user_data: the data to pass to callback function
1175 * Asynchronously reads @file.
1177 * For the synchronous version of this function, see g_file_read().
1180 g_file_read_async (GFile *file,
1182 GCancellable *cancellable,
1183 GAsyncReadyCallback callback,
1188 g_return_if_fail (G_IS_FILE (file));
1190 iface = G_FILE_GET_IFACE (file);
1191 (* iface->read_async) (file,
1199 * g_file_read_finish:
1200 * @file: input #GFile.
1201 * @res: a #GAsyncResult.
1202 * @error: a #GError, or %NULL
1204 * Finishes an asynchronous file read operation started with
1205 * g_file_read_async().
1207 * If the #GFileIface for @file does not support streaming operations,
1208 * then @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will
1211 * If @cancellable is not %NULL, then the operation can be cancelled by
1212 * triggering the cancellable object from another thread. If the operation
1213 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1215 * Returns: a #GFileInputStream or %NULL on error.
1218 g_file_read_finish (GFile *file,
1224 g_return_val_if_fail (G_IS_FILE (file), NULL);
1225 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1227 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1229 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1230 if (g_simple_async_result_propagate_error (simple, error))
1234 iface = G_FILE_GET_IFACE (file);
1235 return (* iface->read_finish) (file, res, error);
1239 * g_file_append_to_async:
1240 * @file: input #GFile.
1241 * @flags: a set of #GFileCreateFlags.
1242 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1244 * @cancellable: optional #GCancellable object, %NULL to ignore.
1245 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1246 * @user_data: the data to pass to callback function
1248 * Readies a file for appending data asynchronously.
1250 * For the synchronous version of this function, see g_file_append_to().
1251 * To finish this operation, see g_file_append_to_finish().
1253 * If @cancellable is not %NULL, then the operation can be cancelled by
1254 * triggering the cancellable object from another thread. If the operation
1255 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set when
1256 * g_file_append_to_finish() is called, and %NULL will be returned.
1259 g_file_append_to_async (GFile *file,
1260 GFileCreateFlags flags,
1262 GCancellable *cancellable,
1263 GAsyncReadyCallback callback,
1268 g_return_if_fail (G_IS_FILE (file));
1270 iface = G_FILE_GET_IFACE (file);
1271 (* iface->append_to_async) (file,
1280 * g_file_append_to_finish:
1281 * @file: input #GFile.
1282 * @res: #GAsyncResult
1283 * @error: a #GError, or %NULL
1285 * Finishes appending to a file. See g_file_append_to_async().
1287 * If @cancellable was not %NULL when g_file_append_to_async() was called,
1288 * then the operation could have been be cancelled by triggering the cancellable
1289 * object from another thread. If the operation was cancelled, the error
1290 * %G_IO_ERROR_CANCELLED will be set in @error, and %NULL will be returned.
1292 * Returns: a valid #GFileOutputStream or %NULL on error.
1295 g_file_append_to_finish (GFile *file,
1301 g_return_val_if_fail (G_IS_FILE (file), NULL);
1302 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1304 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1306 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1307 if (g_simple_async_result_propagate_error (simple, error))
1311 iface = G_FILE_GET_IFACE (file);
1312 return (* iface->append_to_finish) (file, res, error);
1316 * g_file_create_async:
1317 * @file: input #GFile.
1318 * @flags: a set of #GFileCreateFlags.
1319 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1321 * @cancellable: optional #GCancellable object, %NULL to ignore.
1322 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1323 * @user_data: the data to pass to callback function
1325 * Creates a new file asynchronously.
1327 * For the synchronous version of this function, see g_file_create().
1328 * To finish this operation, see g_file_create_finish().
1330 * If @cancellable is not %NULL, then the operation can be cancelled by
1331 * triggering the cancellable object from another thread. If the operation
1332 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL
1333 * will be returned by g_file_create_finish().
1336 g_file_create_async (GFile *file,
1337 GFileCreateFlags flags,
1339 GCancellable *cancellable,
1340 GAsyncReadyCallback callback,
1345 g_return_if_fail (G_IS_FILE (file));
1347 iface = G_FILE_GET_IFACE (file);
1348 (* iface->create_async) (file,
1357 * g_file_create_finish:
1358 * @file: input #GFile.
1359 * @res: a #GAsyncResult.
1360 * @error: a #GError, or %NULL
1362 * Finishes creating a file. See g_file_create_async().
1364 * If @cancellable was not %NULL when g_file_create_async() was called,
1365 * then the operation could have been be cancelled by triggering the cancellable
1366 * object from another thread. If the operation was cancelled, the error
1367 * %G_IO_ERROR_CANCELLED will be set in @error, and %NULL will be returned.
1369 * Returns: a #GFileOutputStream or %NULL on error.
1372 g_file_create_finish (GFile *file,
1378 g_return_val_if_fail (G_IS_FILE (file), NULL);
1379 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1381 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1383 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1384 if (g_simple_async_result_propagate_error (simple, error))
1388 iface = G_FILE_GET_IFACE (file);
1389 return (* iface->create_finish) (file, res, error);
1393 * g_file_replace_async:
1394 * @file: input #GFile.
1395 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1397 * @make_backup: a #gboolean.
1398 * @flags: a set of #GFileCreateFlags.
1399 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1401 * @cancellable: optional #GCancellable object, %NULL to ignore.
1402 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1403 * @user_data: the data to pass to callback function
1405 * Replaces a file's contents asynchronously. If @make_backup is
1406 * %TRUE, this function will attempt to make a backup of the current file.
1408 * For the synchronous version of this function, see g_file_replace().
1409 * To finish this operation, see g_file_replace_finish().
1411 * If @cancellable is not %NULL, then the operation can be cancelled by
1412 * triggering the cancellable object from another thread. If the operation
1413 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and
1414 * %NULL will be returned in g_file_replace_finish().
1417 g_file_replace_async (GFile *file,
1419 gboolean make_backup,
1420 GFileCreateFlags flags,
1422 GCancellable *cancellable,
1423 GAsyncReadyCallback callback,
1428 g_return_if_fail (G_IS_FILE (file));
1430 iface = G_FILE_GET_IFACE (file);
1431 (* iface->replace_async) (file,
1442 * g_file_replace_finish:
1443 * @file: input #GFile.
1444 * @res: a #GAsyncResult.
1445 * @error: a #GError, or %NULL
1447 * Finishes replacing the contents of the file. See g_file_replace_async().
1449 * If @cancellable was not %NULL when g_file_replace_async() was called,
1450 * then the operation could have been be cancelled by triggering the cancellable
1451 * object from another thread. If the operation was cancelled, the error
1452 * %G_IO_ERROR_CANCELLED will be set in @error, and %NULL will be returned.
1454 * Returns: a #GFileOutputStream, or %NULL if an error has occured.
1457 g_file_replace_finish (GFile *file,
1463 g_return_val_if_fail (G_IS_FILE (file), NULL);
1464 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1466 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1468 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1469 if (g_simple_async_result_propagate_error (simple, error))
1473 iface = G_FILE_GET_IFACE (file);
1474 return (* iface->replace_finish) (file, res, error);
1478 copy_symlink (GFile *destination,
1479 GFileCopyFlags flags,
1480 GCancellable *cancellable,
1485 gboolean tried_delete;
1487 GFileType file_type;
1489 tried_delete = FALSE;
1493 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1495 /* Maybe it already existed, and we want to overwrite? */
1496 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1497 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1499 g_error_free (my_error);
1502 /* Don't overwrite if the destination is a directory */
1503 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STD_TYPE,
1504 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1505 cancellable, &my_error);
1508 file_type = g_file_info_get_file_type (info);
1509 g_object_unref (info);
1511 if (file_type == G_FILE_TYPE_DIRECTORY)
1513 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1514 _("Can't copy over directory"));
1519 if (!g_file_delete (destination, cancellable, error))
1522 tried_delete = TRUE;
1526 g_propagate_error (error, my_error);
1533 static GInputStream *
1534 open_source_for_copy (GFile *source,
1536 GFileCopyFlags flags,
1537 GCancellable *cancellable,
1543 GFileType file_type;
1546 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1550 /* There was an error opening the source, try to set a good error for it: */
1552 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1554 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1555 * as that is less useful to the app. Better check for errors on the
1558 g_error_free (my_error);
1561 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STD_TYPE,
1562 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1563 cancellable, &my_error);
1566 file_type = g_file_info_get_file_type (info);
1567 g_object_unref (info);
1569 if (flags & G_FILE_COPY_OVERWRITE)
1571 if (file_type == G_FILE_TYPE_DIRECTORY)
1573 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1574 _("Can't copy directory over directory"));
1577 /* continue to would_recurse error */
1581 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1582 _("Target file exists"));
1588 /* Error getting info from target, return that error
1589 * (except for NOT_FOUND, which is no error here)
1591 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1593 g_propagate_error (error, my_error);
1596 g_error_free (my_error);
1599 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1600 _("Can't recursively copy directory"));
1604 g_propagate_error (error, my_error);
1609 should_copy (GFileAttributeInfo *info,
1613 return info->flags & G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED;
1614 return info->flags & G_FILE_ATTRIBUTE_FLAGS_COPY_WITH_FILE;
1618 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1619 GFileAttributeInfoList *namespaces,
1627 s = g_string_new ("");
1631 for (i = 0; i < attributes->n_infos; i++)
1633 if (should_copy (&attributes->infos[i], as_move))
1638 g_string_append_c (s, ',');
1640 g_string_append (s, attributes->infos[i].name);
1647 for (i = 0; i < namespaces->n_infos; i++)
1649 if (should_copy (&namespaces->infos[i], as_move))
1654 g_string_append_c (s, ',');
1656 g_string_append (s, namespaces->infos[i].name);
1657 g_string_append (s, ":*");
1662 return g_string_free (s, FALSE);
1666 * g_file_copy_attributes:
1667 * @source: a #GFile with attributes.
1668 * @destination: a #GFile to copy attributes to.
1669 * @flags: a set of #GFileCopyFlags.
1670 * @cancellable: optional #GCancellable object, %NULL to ignore.
1671 * @error: a #GError, %NULL to ignore.
1673 * Copies the file attributes from @source to @destination.
1675 * If @cancellable is not %NULL, then the operation can be cancelled by
1676 * triggering the cancellable object from another thread. If the operation
1677 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1679 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1682 g_file_copy_attributes (GFile *source,
1684 GFileCopyFlags flags,
1685 GCancellable *cancellable,
1688 GFileAttributeInfoList *attributes, *namespaces;
1689 char *attrs_to_read;
1693 gboolean source_nofollow_symlinks;
1695 as_move = flags & G_FILE_COPY_ALL_METADATA;
1696 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1698 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1699 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1700 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1702 if (attributes == NULL && namespaces == NULL)
1705 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1707 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1708 * we just don't copy it.
1710 info = g_file_query_info (source, attrs_to_read,
1711 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1715 g_free (attrs_to_read);
1720 res = g_file_set_attributes_from_info (destination,
1724 g_object_unref (info);
1727 g_file_attribute_info_list_unref (attributes);
1728 g_file_attribute_info_list_unref (namespaces);
1733 /* Closes the streams */
1735 copy_stream_with_progress (GInputStream *in,
1737 GCancellable *cancellable,
1738 GFileProgressCallback progress_callback,
1739 gpointer progress_callback_data,
1742 gssize n_read, n_written;
1743 goffset current_size;
1744 char buffer[8192], *p;
1750 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1751 G_FILE_ATTRIBUTE_STD_SIZE,
1755 total_size = g_file_info_get_size (info);
1756 g_object_unref (info);
1763 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
1773 current_size += n_read;
1778 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
1779 if (n_written == -1)
1786 n_read -= n_written;
1792 if (progress_callback)
1793 progress_callback (current_size, total_size, progress_callback_data);
1797 error = NULL; /* Ignore further errors */
1799 /* Make sure we send full copied size */
1800 if (progress_callback)
1801 progress_callback (current_size, total_size, progress_callback_data);
1804 /* Don't care about errors in source here */
1805 g_input_stream_close (in, cancellable, NULL);
1807 /* But write errors on close are bad! */
1808 if (!g_output_stream_close (out, cancellable, error))
1811 g_object_unref (in);
1812 g_object_unref (out);
1818 file_copy_fallback (GFile *source,
1820 GFileCopyFlags flags,
1821 GCancellable *cancellable,
1822 GFileProgressCallback progress_callback,
1823 gpointer progress_callback_data,
1831 /* Maybe copy the symlink? */
1832 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
1834 info = g_file_query_info (source,
1835 G_FILE_ATTRIBUTE_STD_TYPE "," G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET,
1836 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1842 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
1843 (target = g_file_info_get_symlink_target (info)) != NULL)
1845 if (!copy_symlink (destination, flags, cancellable, target, error))
1847 g_object_unref (info);
1851 g_object_unref (info);
1855 g_object_unref (info);
1858 in = open_source_for_copy (source, destination, flags, cancellable, error);
1862 if (flags & G_FILE_COPY_OVERWRITE)
1864 out = (GOutputStream *)g_file_replace (destination,
1866 flags & G_FILE_COPY_BACKUP,
1867 cancellable, error);
1871 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
1876 g_object_unref (in);
1880 if (!copy_stream_with_progress (in, out, cancellable,
1881 progress_callback, progress_callback_data,
1887 /* Ignore errors here. Failure to copy metadata is not a hard error */
1888 g_file_copy_attributes (source, destination,
1889 flags, cancellable, NULL);
1896 * @source: input #GFile.
1897 * @destination: destination #GFile
1898 * @flags: set of #GFileCopyFlags
1899 * @cancellable: optional #GCancellable object, %NULL to ignore.
1900 * @progress_callback: function to callback with progress information
1901 * @progress_callback_data: userdata to pass to @progress_callback
1902 * @error: #GError to set on error, or %NULL
1904 * <!-- Source Friendly Version
1905 * List of possible errors resulting from g_file_copy():
1906 * source dest flags res
1907 * - * * G_IO_ERROR_NOT_FOUND
1909 * file * 0 G_IO_ERROR_EXISTS
1910 * file file overwr ok
1911 * file dir overwr G_IO_ERROR_IS_DIRECTORY
1913 * dir - * G_IO_ERROR_WOULD_RECURSE
1914 * dir * 0 G_IO_ERROR_EXISTS
1915 * dir dir overwr G_IO_ERROR_WOULD_MERGE
1916 * dir file overwr G_IO_ERROR_WOULD_RECURSE
1917 * Docbook version below -->
1919 * Copies a file or directory from @source to @destination, with the given
1920 * @flags. This operation may fail, and @error will be set appropriately with
1921 * the given error result (see the following table).
1922 * File copies are always asynchronous.
1924 * If @cancellable is not %NULL, then the operation can be cancelled by
1925 * triggering the cancellable object from another thread. If the operation
1926 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1928 * If @progress_callback is not %NULL, then the operation can be monitored by
1929 * setting this to a #GFileProgressCallback function. @progress_callback_data
1930 * will be passed to this function.
1933 * <title>g_file_copy() Error Conditions</title>
1934 * <tgroup cols='4' align='left'><thead>
1935 * <row><entry>Source</entry><entry>Destination</entry><entry>Flags</entry><entry>Results in</entry></row>
1937 * <row><entry>%NULL</entry><entry>Anything</entry><entry>Anything</entry><entry>%G_IO_ERROR_NOT_FOUND</entry></row>
1938 * <row><entry>File</entry><entry>%NULL</entry><entry>Anything</entry><entry>No Error</entry></row>
1939 * <row><entry>File</entry><entry>Anything</entry><entry>0</entry><entry>%G_IO_ERROR_EXISTS</entry></row>
1940 * <row><entry>File</entry><entry>File</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>No Error</entry></row>
1941 * <row><entry>File</entry><entry>Directory</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>%G_IO_ERROR_IS_DIRECTORY</entry></row>
1942 * <row><entry>Directory</entry><entry>%NULL</entry><entry>Anything</entry><entry>%G_IO_ERROR_WOULD_RECURSE</entry></row>
1943 * <row><entry>Directory</entry><entry>Anything</entry><entry>0</entry><entry>%G_IO_ERROR_EXISTS</entry></row>
1944 * <row><entry>Directory</entry><entry>Directory</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>%G_IO_ERROR_IS_DIRECTORY</entry></row>
1945 * <row><entry>Directory</entry><entry>File</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>%G_IO_ERROR_WOULD_RECURSE</entry></row>
1950 * If you are interested in copying the #GFile object itself (not the on-disk
1951 * file), see g_file_dup().
1953 * Returns: %TRUE on success, %FALSE otherwise.
1956 g_file_copy (GFile *source,
1958 GFileCopyFlags flags,
1959 GCancellable *cancellable,
1960 GFileProgressCallback progress_callback,
1961 gpointer progress_callback_data,
1968 g_return_val_if_fail (G_IS_FILE (source), FALSE);
1969 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
1971 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1974 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
1976 iface = G_FILE_GET_IFACE (source);
1981 res = (* iface->copy) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
1986 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
1988 g_propagate_error (error, my_error);
1994 return file_copy_fallback (source, destination, flags, cancellable,
1995 progress_callback, progress_callback_data,
2002 * @source: #GFile pointing to the source location.
2003 * @destination: #GFile pointing to the destination location.
2004 * @flags: set of #GFileCopyFlags.
2005 * @cancellable: optional #GCancellable object, %NULL to ignore.
2006 * @progress_callback: #GFileProgressCallback function for updates.
2007 * @progress_callback_data: gpointer to user data for the callback function.
2008 * @error: #GError for returning error conditions, or %NULL
2010 * <!-- Source version
2011 * source dest flags results in
2012 * - * * G_IO_ERROR_NOT_FOUND
2014 * file * 0 G_IO_ERROR_EXISTS
2015 * file file overwr ok
2016 * file dir overwr G_IO_ERROR_IS_DIRECTORY
2018 * dir - * ok || G_IO_ERROR_WOULD_RECURSE
2019 * dir * 0 G_IO_ERROR_EXISTS
2020 * dir dir overwr G_IO_ERROR_WOULD_MERGE
2021 * dir file overwr ok || G_IO_ERROR_WOULD_RECURSE
2022 * Docbook version below -->
2024 * Moves a file or directory from @source to @destination, with the given
2025 * @flags. This operation may fail, and @error will be set appropriately with
2026 * the given error result (see the following table).
2027 * File moves are always asynchronous.
2029 * If @cancellable is not %NULL, then the operation can be cancelled by
2030 * triggering the cancellable object from another thread. If the operation
2031 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2033 * If @progress_callback is not %NULL, then the operation can be monitored by
2034 * setting this to a #GFileProgressCallback function. @progress_callback_data
2035 * will be passed to this function.
2038 * <title>g_file_move() Error Conditions</title>
2039 * <tgroup cols='4' align='left'><thead>
2040 * <row><entry>Source</entry><entry>Destination</entry>
2041 * <entry>Flags</entry><entry>Results in</entry></row>
2043 * <row><entry> %NULL </entry><entry> Anything </entry>
2044 * <entry> Anything </entry><entry> %G_IO_ERROR_NOT_FOUND </entry></row>
2045 * <row><entry> File </entry><entry> %NULL </entry>
2046 * <entry> Anything </entry><entry> No Error </entry></row>
2047 * <row><entry> File </entry><entry> Anything </entry>
2048 * <entry> 0 </entry><entry> %G_IO_ERROR_EXISTS </entry></row>
2049 * <row><entry> File </entry><entry> File </entry>
2050 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> No Error </entry></row>
2051 * <row><entry> File </entry><entry> Directory </entry>
2052 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> %G_IO_ERROR_IS_DIRECTORY </entry></row>
2053 * <row><entry> Directory </entry><entry> %NULL </entry>
2054 * <entry> Anything </entry><entry> No Error or %G_IO_ERROR_WOULD_RECURSE </entry></row>
2055 * <row><entry> Directory </entry><entry> Anything </entry>
2056 * <entry> 0 </entry><entry> %G_IO_ERROR_EXISTS </entry></row>
2057 * <row><entry> Directory </entry><entry> Directory </entry>
2058 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> %G_IO_ERROR_IS_DIRECTORY </entry></row>
2059 * <row><entry> Directory </entry><entry> File </entry>
2060 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> No Error or %G_IO_ERROR_WOULD_RECURSE </entry></row>
2065 * Returns: %TRUE on successful move, %FALSE otherwise.
2068 g_file_move (GFile *source,
2070 GFileCopyFlags flags,
2071 GCancellable *cancellable,
2072 GFileProgressCallback progress_callback,
2073 gpointer progress_callback_data,
2080 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2081 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2083 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2086 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
2088 iface = G_FILE_GET_IFACE (source);
2093 res = (* iface->move) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
2098 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2100 g_propagate_error (error, my_error);
2106 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2108 g_set_error (error, G_IO_ERROR,
2109 G_IO_ERROR_NOT_SUPPORTED,
2110 _("Operation not supported"));
2114 flags |= G_FILE_COPY_ALL_METADATA;
2115 if (!g_file_copy (source, destination, flags, cancellable,
2116 progress_callback, progress_callback_data,
2120 return g_file_delete (source, cancellable, error);
2124 * g_file_make_directory
2125 * @file: input #GFile.
2126 * @cancellable: optional #GCancellable object, %NULL to ignore.
2127 * @error: a #GError, or %NULL
2129 * Creates a directory.
2131 * If @cancellable is not %NULL, then the operation can be cancelled by
2132 * triggering the cancellable object from another thread. If the operation
2133 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2135 * Returns: %TRUE on successful creation, %FALSE otherwise.
2138 g_file_make_directory (GFile *file,
2139 GCancellable *cancellable,
2144 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2146 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2149 iface = G_FILE_GET_IFACE (file);
2151 if (iface->make_directory == NULL)
2153 g_set_error (error, G_IO_ERROR,
2154 G_IO_ERROR_NOT_SUPPORTED,
2155 _("Operation not supported"));
2159 return (* iface->make_directory) (file, cancellable, error);
2163 * g_file_make_symbolic_link:
2164 * @file: input #GFile.
2165 * @symlink_value: a string with the name of the new symlink.
2166 * @cancellable: optional #GCancellable object, %NULL to ignore.
2167 * @error: a #GError.
2169 * Creates a symbolic link.
2171 * If @cancellable is not %NULL, then the operation can be cancelled by
2172 * triggering the cancellable object from another thread. If the operation
2173 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2175 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2178 g_file_make_symbolic_link (GFile *file,
2179 const char *symlink_value,
2180 GCancellable *cancellable,
2185 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2186 g_return_val_if_fail (symlink_value != NULL, FALSE);
2188 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2191 if (*symlink_value == '\0')
2193 g_set_error (error, G_IO_ERROR,
2194 G_IO_ERROR_INVALID_ARGUMENT,
2195 _("Invalid symlink value given"));
2199 iface = G_FILE_GET_IFACE (file);
2201 if (iface->make_symbolic_link == NULL)
2203 g_set_error (error, G_IO_ERROR,
2204 G_IO_ERROR_NOT_SUPPORTED,
2205 _("Operation not supported"));
2209 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2214 * @file: input #GFile.
2215 * @cancellable: optional #GCancellable object, %NULL to ignore.
2216 * @error: a #GError, or %NULL
2220 * If @cancellable is not %NULL, then the operation can be cancelled by
2221 * triggering the cancellable object from another thread. If the operation
2222 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2224 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2227 g_file_delete (GFile *file,
2228 GCancellable *cancellable,
2233 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2235 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2238 iface = G_FILE_GET_IFACE (file);
2240 if (iface->delete_file == NULL)
2242 g_set_error (error, G_IO_ERROR,
2243 G_IO_ERROR_NOT_SUPPORTED,
2244 _("Operation not supported"));
2248 return (* iface->delete_file) (file, cancellable, error);
2253 * @file: #GFile to send to trash.
2254 * @cancellable: optional #GCancellable object, %NULL to ignore.
2255 * @error: a #GError, or %NULL
2257 * Sends @file to the virtual file system "Trash" location. If the
2258 * virtual file system does not have support having a "Trash" location,
2259 * %FALSE will be returned, and @error will be set to
2260 * %G_IO_ERROR_NOT_SUPPORTED.
2262 * If @cancellable is not %NULL, then the operation can be cancelled by
2263 * triggering the cancellable object from another thread. If the operation
2264 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2266 * Returns: %TRUE on successful trash, %FALSE otherwise.
2269 g_file_trash (GFile *file,
2270 GCancellable *cancellable,
2275 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2277 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2280 iface = G_FILE_GET_IFACE (file);
2282 if (iface->trash == NULL)
2285 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2286 _("Trash not supported"));
2290 return (* iface->trash) (file, cancellable, error);
2294 * g_file_set_display_name:
2295 * @file: input #GFile.
2296 * @display_name: a string.
2297 * @cancellable: optional #GCancellable object, %NULL to ignore.
2298 * @error: a #GError, or %NULL
2300 * Sets the display name for @file. If the display name contains invalid
2301 * characters, @error will be set to %G_IO_ERROR_INVALID_ARGUMENT. For the
2302 * asynchronous version of this function, see g_file_set_display_name_async().
2304 * If @cancellable is not %NULL, then the operation can be cancelled by
2305 * triggering the cancellable object from another thread. If the operation
2306 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2308 * Returns: a #GFile, or %NULL if there was an error.
2311 g_file_set_display_name (GFile *file,
2312 const char *display_name,
2313 GCancellable *cancellable,
2318 g_return_val_if_fail (G_IS_FILE (file), NULL);
2319 g_return_val_if_fail (display_name != NULL, NULL);
2321 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2325 G_IO_ERROR_INVALID_ARGUMENT,
2326 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2330 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2333 iface = G_FILE_GET_IFACE (file);
2335 return (* iface->set_display_name) (file, display_name, cancellable, error);
2339 * g_file_set_display_name_async:
2340 * @file: input #GFile.
2341 * @display_name: a string.
2342 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2344 * @cancellable: optional #GCancellable object, %NULL to ignore.
2345 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2346 * @user_data: the data to pass to callback function
2348 * Asynchronously sets the display name for a given #GFile.
2349 * For the synchronous version of this function, see g_file_set_display_name().
2351 * If @cancellable is not %NULL, then the operation can be cancelled by
2352 * triggering the cancellable object from another thread. If the operation
2353 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2356 g_file_set_display_name_async (GFile *file,
2357 const char *display_name,
2359 GCancellable *cancellable,
2360 GAsyncReadyCallback callback,
2365 g_return_if_fail (G_IS_FILE (file));
2366 g_return_if_fail (display_name != NULL);
2368 iface = G_FILE_GET_IFACE (file);
2369 (* iface->set_display_name_async) (file,
2378 * g_file_set_display_name_finish:
2379 * @file: input #GFile.
2380 * @res: a #GAsyncResult.
2381 * @error: a #GError, or %NULL
2383 * Finishes setting a display name started with
2384 * g_file_set_display_name_async().
2386 * Returns: a #GFile or %NULL on error.
2389 g_file_set_display_name_finish (GFile *file,
2395 g_return_val_if_fail (G_IS_FILE (file), NULL);
2396 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2398 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2400 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2401 if (g_simple_async_result_propagate_error (simple, error))
2405 iface = G_FILE_GET_IFACE (file);
2406 return (* iface->set_display_name_finish) (file, res, error);
2410 * g_file_query_settable_attributes:
2411 * @file: input #GFile.
2412 * @cancellable: optional #GCancellable object, %NULL to ignore.
2413 * @error: a #GError, or %NULL
2415 * Obtain the list of settable attributes for the file.
2417 * If @cancellable is not %NULL, then the operation can be cancelled by
2418 * triggering the cancellable object from another thread. If the operation
2419 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2421 * Returns: the type and full attribute name of all the attributes
2422 * that the file can set. This doesn't mean setting it will always
2423 * succeed though, you might get an access failure, or some specific
2424 * file may not support a specific attribute.
2426 GFileAttributeInfoList *
2427 g_file_query_settable_attributes (GFile *file,
2428 GCancellable *cancellable,
2433 GFileAttributeInfoList *list;
2435 g_return_val_if_fail (G_IS_FILE (file), NULL);
2437 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2440 iface = G_FILE_GET_IFACE (file);
2442 if (iface->query_settable_attributes == NULL)
2443 return g_file_attribute_info_list_new ();
2446 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2450 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2452 list = g_file_attribute_info_list_new ();
2453 g_error_free (my_error);
2456 g_propagate_error (error, my_error);
2463 * g_file_query_writable_namespaces:
2464 * @file: input #GFile.
2465 * @cancellable: optional #GCancellable object, %NULL to ignore.
2466 * @error: a #GError, or %NULL
2468 * Obtain the list of attribute namespaces where new attributes
2471 * If @cancellable is not %NULL, then the operation can be cancelled by
2472 * triggering the cancellable object from another thread. If the operation
2473 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2475 * Returns: a #GFileAttributeInfoList of attribute namespaces
2476 * where the user can create their own attribute names, such
2477 * as extended attributes.
2479 GFileAttributeInfoList *
2480 g_file_query_writable_namespaces (GFile *file,
2481 GCancellable *cancellable,
2486 GFileAttributeInfoList *list;
2488 g_return_val_if_fail (G_IS_FILE (file), NULL);
2490 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2493 iface = G_FILE_GET_IFACE (file);
2495 if (iface->query_writable_namespaces == NULL)
2496 return g_file_attribute_info_list_new ();
2499 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2503 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2505 list = g_file_attribute_info_list_new ();
2506 g_error_free (my_error);
2509 g_propagate_error (error, my_error);
2516 * g_file_set_attribute:
2517 * @file: input #GFile.
2518 * @attribute: a string containing the attribute's name.
2519 * @value: a set of #GFileAttributeValue.
2520 * @flags: a set of #GFileQueryInfoFlags.
2521 * @cancellable: optional #GCancellable object, %NULL to ignore.
2522 * @error: a #GError, or %NULL
2524 * Sets an attribute in the file with attribute name @attribute to @value.
2525 * If setting attributes is not suppored by the #GFileIface for @file,
2526 * then @error will be set to %G_IO_ERROR_NOT_SUPPORTED.
2528 * If @cancellable is not %NULL, then the operation can be cancelled by
2529 * triggering the cancellable object from another thread. If the operation
2530 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2532 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2535 g_file_set_attribute (GFile *file,
2536 const char *attribute,
2537 const GFileAttributeValue *value,
2538 GFileQueryInfoFlags flags,
2539 GCancellable *cancellable,
2544 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2545 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2547 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2550 iface = G_FILE_GET_IFACE (file);
2552 if (iface->set_attribute == NULL)
2554 g_set_error (error, G_IO_ERROR,
2555 G_IO_ERROR_NOT_SUPPORTED,
2556 _("Operation not supported"));
2560 return (* iface->set_attribute) (file, attribute, value, flags, cancellable, error);
2564 * g_file_set_attributes_from_info:
2565 * @file: input #GFile.
2566 * @info: a #GFileInfo.
2567 * @flags: #GFileQueryInfoFlags
2568 * @cancellable: optional #GCancellable object, %NULL to ignore.
2569 * @error: a #GError, or %NULL
2571 * Tries to set all attributes in the #GFileInfo on the target values,
2572 * not stopping on the first error.
2574 * If @cancellable is not %NULL, then the operation can be cancelled by
2575 * triggering the cancellable object from another thread. If the operation
2576 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2578 * Returns: %TRUE if there was any error, and @error will be set to
2579 * the first error. Error on particular fields are flagged by setting
2580 * the "status" field in the attribute value to
2581 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING.
2584 g_file_set_attributes_from_info (GFile *file,
2586 GFileQueryInfoFlags flags,
2587 GCancellable *cancellable,
2592 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2593 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2595 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2598 g_file_info_clear_status (info);
2600 iface = G_FILE_GET_IFACE (file);
2602 return (* iface->set_attributes_from_info) (file,
2611 g_file_real_set_attributes_from_info (GFile *file,
2613 GFileQueryInfoFlags flags,
2614 GCancellable *cancellable,
2620 GFileAttributeValue *value;
2624 attributes = g_file_info_list_attributes (info, NULL);
2626 for (i = 0; attributes[i] != NULL; i++)
2628 value = (GFileAttributeValue *)g_file_info_get_attribute (info, attributes[i]);
2630 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2633 if (!g_file_set_attribute (file, attributes[i], value, flags, cancellable, error))
2635 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2637 /* Don't set error multiple times */
2641 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2644 g_strfreev (attributes);
2650 * g_file_set_attributes_async:
2651 * @file: input #GFile.
2652 * @info: a #GFileInfo.
2653 * @flags: a #GFileQueryInfoFlags.
2654 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2656 * @cancellable: optional #GCancellable object, %NULL to ignore.
2657 * @callback: a #GAsyncReadyCallback.
2658 * @user_data: a #gpointer.
2660 * Asynchronously sets the attributes of @file with @info.
2661 * For the synchronous version of this function, see g_file_set_attributes().
2663 * If @cancellable is not %NULL, then the operation can be cancelled by
2664 * triggering the cancellable object from another thread. If the operation
2665 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2668 g_file_set_attributes_async (GFile *file,
2670 GFileQueryInfoFlags flags,
2672 GCancellable *cancellable,
2673 GAsyncReadyCallback callback,
2678 g_return_if_fail (G_IS_FILE (file));
2679 g_return_if_fail (G_IS_FILE_INFO (info));
2681 iface = G_FILE_GET_IFACE (file);
2682 (* iface->set_attributes_async) (file,
2692 * g_file_set_attributes_finish:
2693 * @file: input #GFile.
2694 * @result: a #GAsyncResult.
2695 * @info: a #GFileInfo.
2696 * @error: a #GError, or %NULL
2698 * Finishes setting an attribute started in g_file_set_attributes_async().
2700 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2703 g_file_set_attributes_finish (GFile *file,
2704 GAsyncResult *result,
2710 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2711 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2713 /* No standard handling of errors here, as we must set info even
2716 iface = G_FILE_GET_IFACE (file);
2717 return (* iface->set_attributes_finish) (file, result, info, error);
2721 * g_file_set_attribute_string:
2722 * @file: input #GFile.
2723 * @attribute: a string containing the attribute's name.
2724 * @value: a string containing the attribute's value.
2725 * @flags: #GFileQueryInfoFlags.
2726 * @cancellable: optional #GCancellable object, %NULL to ignore.
2727 * @error: a #GError, or %NULL
2729 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
2730 * If @attribute is of a different type, this operation will fail.
2732 * If @cancellable is not %NULL, then the operation can be cancelled by
2733 * triggering the cancellable object from another thread. If the operation
2734 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2736 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2739 g_file_set_attribute_string (GFile *file,
2740 const char *attribute,
2742 GFileQueryInfoFlags flags,
2743 GCancellable *cancellable,
2746 GFileAttributeValue v;
2748 v.type = G_FILE_ATTRIBUTE_TYPE_STRING;
2749 v.u.string = (char *)value;
2750 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2754 * g_file_set_attribute_byte_string:
2755 * @file: input #GFile.
2756 * @attribute: a string containing the attribute's name.
2757 * @value: a string containing the attribute's new value.
2758 * @flags: a #GFileQueryInfoFlags.
2759 * @cancellable: optional #GCancellable object, %NULL to ignore.
2760 * @error: a #GError, or %NULL
2762 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
2763 * If @attribute is of a different type, this operation will fail,
2766 * If @cancellable is not %NULL, then the operation can be cancelled by
2767 * triggering the cancellable object from another thread. If the operation
2768 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2770 * Returns: %TRUE if the @attribute was successfully set to @value
2771 * in the @file, %FALSE otherwise.
2774 g_file_set_attribute_byte_string (GFile *file,
2775 const char *attribute,
2777 GFileQueryInfoFlags flags,
2778 GCancellable *cancellable,
2781 GFileAttributeValue v;
2783 v.type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
2784 v.u.string = (char *)value;
2785 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2789 * g_file_set_attribute_uint32:
2790 * @file: input #GFile.
2791 * @attribute: a string containing the attribute's name.
2792 * @value: a #guint32 containing the attribute's new value.
2793 * @flags: a #GFileQueryInfoFlags.
2794 * @cancellable: optional #GCancellable object, %NULL to ignore.
2795 * @error: a #GError, or %NULL
2797 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
2798 * If @attribute is of a different type, this operation will fail.
2800 * If @cancellable is not %NULL, then the operation can be cancelled by
2801 * triggering the cancellable object from another thread. If the operation
2802 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2804 * Returns: %TRUE if the @attribute was successfully set to @value
2805 * in the @file, %FALSE otherwise.
2808 g_file_set_attribute_uint32 (GFile *file,
2809 const char *attribute,
2811 GFileQueryInfoFlags flags,
2812 GCancellable *cancellable,
2815 GFileAttributeValue v;
2817 v.type = G_FILE_ATTRIBUTE_TYPE_UINT32;
2819 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2823 * g_file_set_attribute_int32:
2824 * @file: input #GFile.
2825 * @attribute: a string containing the attribute's name.
2826 * @value: a #gint32 containing the attribute's new value.
2827 * @flags: a #GFileQueryInfoFlags.
2828 * @cancellable: optional #GCancellable object, %NULL to ignore.
2829 * @error: a #GError, or %NULL
2831 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
2832 * If @attribute is of a different type, this operation will fail.
2834 * If @cancellable is not %NULL, then the operation can be cancelled by
2835 * triggering the cancellable object from another thread. If the operation
2836 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2838 * Returns: %TRUE if the @attribute was successfully set to @value
2839 * in the @file, %FALSE otherwise.
2842 g_file_set_attribute_int32 (GFile *file,
2843 const char *attribute,
2845 GFileQueryInfoFlags flags,
2846 GCancellable *cancellable,
2849 GFileAttributeValue v;
2851 v.type = G_FILE_ATTRIBUTE_TYPE_INT32;
2853 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2857 * g_file_set_attribute_uint64:
2858 * @file: input #GFile.
2859 * @attribute: a string containing the attribute's name.
2860 * @value: a #guint64 containing the attribute's new value.
2861 * @flags: a #GFileQueryInfoFlags.
2862 * @cancellable: optional #GCancellable object, %NULL to ignore.
2863 * @error: a #GError, or %NULL
2865 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
2866 * If @attribute is of a different type, this operation will fail.
2868 * If @cancellable is not %NULL, then the operation can be cancelled by
2869 * triggering the cancellable object from another thread. If the operation
2870 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2872 * Returns: %TRUE if the @attribute was successfully set to @value
2873 * in the @file, %FALSE otherwise.
2876 g_file_set_attribute_uint64 (GFile *file,
2877 const char *attribute,
2879 GFileQueryInfoFlags flags,
2880 GCancellable *cancellable,
2883 GFileAttributeValue v;
2885 v.type = G_FILE_ATTRIBUTE_TYPE_UINT64;
2887 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2891 * g_file_set_attribute_int64:
2892 * @file: input #GFile.
2893 * @attribute: a string containing the attribute's name.
2894 * @value: a #guint64 containing the attribute's new value.
2895 * @flags: a #GFileQueryInfoFlags.
2896 * @cancellable: optional #GCancellable object, %NULL to ignore.
2897 * @error: a #GError, or %NULL
2899 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
2900 * If @attribute is of a different type, this operation will fail.
2902 * If @cancellable is not %NULL, then the operation can be cancelled by
2903 * triggering the cancellable object from another thread. If the operation
2904 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2906 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2909 g_file_set_attribute_int64 (GFile *file,
2910 const char *attribute,
2912 GFileQueryInfoFlags flags,
2913 GCancellable *cancellable,
2916 GFileAttributeValue v;
2918 v.type = G_FILE_ATTRIBUTE_TYPE_INT64;
2920 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2924 * g_file_mount_mountable:
2925 * @file: input #GFile.
2926 * @mount_operation: a #GMountOperation.
2927 * @cancellable: optional #GCancellable object, %NULL to ignore.
2928 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2929 * @user_data: the data to pass to callback function
2931 * Mounts a mountable file using @mount_operation, if possible.
2933 * If @cancellable is not %NULL, then the operation can be cancelled by
2934 * triggering the cancellable object from another thread. If the operation
2935 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2938 g_file_mount_mountable (GFile *file,
2939 GMountOperation *mount_operation,
2940 GCancellable *cancellable,
2941 GAsyncReadyCallback callback,
2946 g_return_if_fail (G_IS_FILE (file));
2947 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
2949 iface = G_FILE_GET_IFACE (file);
2951 if (iface->mount_mountable == NULL)
2952 g_simple_async_report_error_in_idle (G_OBJECT (file),
2956 G_IO_ERROR_NOT_SUPPORTED,
2957 _("Operation not supported"));
2959 (* iface->mount_mountable) (file,
2967 * g_file_mount_mountable_finish:
2968 * @file: input #GFile.
2969 * @result: a #GAsyncResult.
2970 * @error: a #GError, or %NULL
2972 * Finishes a mount operation. See g_file_mount_mountable() for details.
2974 * Finish an asynchronous mount operation that was started
2975 * with g_file_mount_mountable().
2977 * Returns: a #GFile or %NULL on error.
2980 g_file_mount_mountable_finish (GFile *file,
2981 GAsyncResult *result,
2986 g_return_val_if_fail (G_IS_FILE (file), NULL);
2987 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
2989 if (G_IS_SIMPLE_ASYNC_RESULT (result))
2991 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
2992 if (g_simple_async_result_propagate_error (simple, error))
2996 iface = G_FILE_GET_IFACE (file);
2997 return (* iface->mount_mountable_finish) (file, result, error);
3001 * g_file_unmount_mountable:
3002 * @file: input #GFile.
3003 * @cancellable: optional #GCancellable object, %NULL to ignore.
3004 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3005 * @user_data: the data to pass to callback function
3007 * Starts an asynchronous unmount operation.
3009 * Unmounts a mounted file.
3011 * If @cancellable is not %NULL, then the operation can be cancelled by
3012 * triggering the cancellable object from another thread. If the operation
3013 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3016 g_file_unmount_mountable (GFile *file,
3017 GCancellable *cancellable,
3018 GAsyncReadyCallback callback,
3023 g_return_if_fail (G_IS_FILE (file));
3025 iface = G_FILE_GET_IFACE (file);
3027 if (iface->unmount_mountable == NULL)
3028 g_simple_async_report_error_in_idle (G_OBJECT (file),
3032 G_IO_ERROR_NOT_SUPPORTED,
3033 _("Operation not supported"));
3035 (* iface->unmount_mountable) (file,
3042 * g_file_unmount_mountable_finish:
3043 * @file: input #GFile.
3044 * @result: a #GAsyncResult.
3045 * @error: a #GError, or %NULL
3047 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3049 * Finish an asynchronous unmount operation that was started
3050 * with g_file_unmount_mountable().
3052 * Returns: %TRUE if the operation finished successfully. %FALSE
3056 g_file_unmount_mountable_finish (GFile *file,
3057 GAsyncResult *result,
3062 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3063 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3065 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3067 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3068 if (g_simple_async_result_propagate_error (simple, error))
3072 iface = G_FILE_GET_IFACE (file);
3073 return (* iface->unmount_mountable_finish) (file, result, error);
3077 * g_file_eject_mountable:
3078 * @file: input #GFile.
3079 * @cancellable: optional #GCancellable object, %NULL to ignore.
3080 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3081 * @user_data: the data to pass to callback function
3083 * Starts an asynchronous eject on a mountable.
3084 * When this operation has completed, @callback will be called with
3085 * @user_user data, and the operation can be finalized with
3086 * g_file_eject_mountable_finish().
3088 * If @cancellable is not %NULL, then the operation can be cancelled by
3089 * triggering the cancellable object from another thread. If the operation
3090 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3093 g_file_eject_mountable (GFile *file,
3094 GCancellable *cancellable,
3095 GAsyncReadyCallback callback,
3100 g_return_if_fail (G_IS_FILE (file));
3102 iface = G_FILE_GET_IFACE (file);
3104 if (iface->eject_mountable == NULL)
3105 g_simple_async_report_error_in_idle (G_OBJECT (file),
3109 G_IO_ERROR_NOT_SUPPORTED,
3110 _("Operation not supported"));
3112 (* iface->eject_mountable) (file,
3119 * g_file_eject_mountable_finish:
3120 * @file: input #GFile.
3121 * @result: a #GAsyncResult.
3122 * @error: a #GError, or %NULL
3124 * Finishes an asynchronous eject operation started by
3125 * g_file_eject_mountable().
3127 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3131 g_file_eject_mountable_finish (GFile *file,
3132 GAsyncResult *result,
3137 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3138 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3140 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3142 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3143 if (g_simple_async_result_propagate_error (simple, error))
3147 iface = G_FILE_GET_IFACE (file);
3148 return (* iface->eject_mountable_finish) (file, result, error);
3152 * g_file_monitor_directory:
3153 * @file: input #GFile.
3154 * @flags: a set of #GFileMonitorFlags.
3155 * @cancellable: optional #GCancellable object, %NULL to ignore.
3157 * Obtains a directory monitor for the given file.
3159 * If @cancellable is not %NULL, then the operation can be cancelled by
3160 * triggering the cancellable object from another thread. If the operation
3161 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3163 * Returns: a #GDirectoryMonitor for the given @file,
3164 * or %NULL on error.
3167 g_file_monitor_directory (GFile *file,
3168 GFileMonitorFlags flags,
3169 GCancellable *cancellable)
3173 g_return_val_if_fail (G_IS_FILE (file), NULL);
3175 iface = G_FILE_GET_IFACE (file);
3177 if (iface->monitor_dir == NULL)
3180 return (* iface->monitor_dir) (file, flags, cancellable);
3184 * g_file_monitor_file:
3185 * @file: input #GFile.
3186 * @flags: a set of #GFileMonitorFlags.
3187 * @cancellable: optional #GCancellable object, %NULL to ignore.
3189 * Obtains a file monitor for the given file.
3191 * If @cancellable is not %NULL, then the operation can be cancelled by
3192 * triggering the cancellable object from another thread. If the operation
3193 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3195 * Returns: a #GFileMonitor for the given @file,
3196 * or %NULL on error.
3199 g_file_monitor_file (GFile *file,
3200 GFileMonitorFlags flags,
3201 GCancellable *cancellable)
3204 GFileMonitor *monitor;
3206 g_return_val_if_fail (G_IS_FILE (file), NULL);
3208 iface = G_FILE_GET_IFACE (file);
3212 if (iface->monitor_file)
3213 monitor = (* iface->monitor_file) (file, flags, cancellable);
3215 /* Fallback to polling */
3216 if (monitor == NULL)
3217 monitor = _g_poll_file_monitor_new (file);
3222 /********************************************
3223 * Default implementation of async ops *
3224 ********************************************/
3228 GFileQueryInfoFlags flags;
3230 } QueryInfoAsyncData;
3233 query_info_data_free (QueryInfoAsyncData *data)
3236 g_object_unref (data->info);
3237 g_free (data->attributes);
3242 query_info_async_thread (GSimpleAsyncResult *res,
3244 GCancellable *cancellable)
3246 GError *error = NULL;
3247 QueryInfoAsyncData *data;
3250 data = g_simple_async_result_get_op_res_gpointer (res);
3252 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3256 g_simple_async_result_set_from_error (res, error);
3257 g_error_free (error);
3264 g_file_real_query_info_async (GFile *file,
3265 const char *attributes,
3266 GFileQueryInfoFlags flags,
3268 GCancellable *cancellable,
3269 GAsyncReadyCallback callback,
3272 GSimpleAsyncResult *res;
3273 QueryInfoAsyncData *data;
3275 data = g_new0 (QueryInfoAsyncData, 1);
3276 data->attributes = g_strdup (attributes);
3277 data->flags = flags;
3279 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3280 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3282 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3283 g_object_unref (res);
3287 g_file_real_query_info_finish (GFile *file,
3291 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3292 QueryInfoAsyncData *data;
3294 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3296 data = g_simple_async_result_get_op_res_gpointer (simple);
3298 return g_object_ref (data->info);
3305 GFileQueryInfoFlags flags;
3306 GFileEnumerator *enumerator;
3307 } EnumerateChildrenAsyncData;
3310 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3312 if (data->enumerator)
3313 g_object_unref (data->enumerator);
3314 g_free (data->attributes);
3319 enumerate_children_async_thread (GSimpleAsyncResult *res,
3321 GCancellable *cancellable)
3323 GError *error = NULL;
3324 EnumerateChildrenAsyncData *data;
3325 GFileEnumerator *enumerator;
3327 data = g_simple_async_result_get_op_res_gpointer (res);
3329 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3331 if (enumerator == NULL)
3333 g_simple_async_result_set_from_error (res, error);
3334 g_error_free (error);
3337 data->enumerator = enumerator;
3341 g_file_real_enumerate_children_async (GFile *file,
3342 const char *attributes,
3343 GFileQueryInfoFlags flags,
3345 GCancellable *cancellable,
3346 GAsyncReadyCallback callback,
3349 GSimpleAsyncResult *res;
3350 EnumerateChildrenAsyncData *data;
3352 data = g_new0 (EnumerateChildrenAsyncData, 1);
3353 data->attributes = g_strdup (attributes);
3354 data->flags = flags;
3356 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3357 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3359 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3360 g_object_unref (res);
3363 static GFileEnumerator *
3364 g_file_real_enumerate_children_finish (GFile *file,
3368 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3369 EnumerateChildrenAsyncData *data;
3371 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3373 data = g_simple_async_result_get_op_res_gpointer (simple);
3374 if (data->enumerator)
3375 return g_object_ref (data->enumerator);
3381 open_read_async_thread (GSimpleAsyncResult *res,
3383 GCancellable *cancellable)
3386 GFileInputStream *stream;
3387 GError *error = NULL;
3389 iface = G_FILE_GET_IFACE (object);
3391 stream = iface->read_fn (G_FILE (object), cancellable, &error);
3395 g_simple_async_result_set_from_error (res, error);
3396 g_error_free (error);
3399 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3403 g_file_real_read_async (GFile *file,
3405 GCancellable *cancellable,
3406 GAsyncReadyCallback callback,
3409 GSimpleAsyncResult *res;
3411 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3413 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3414 g_object_unref (res);
3417 static GFileInputStream *
3418 g_file_real_read_finish (GFile *file,
3422 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3425 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3427 op = g_simple_async_result_get_op_res_gpointer (simple);
3429 return g_object_ref (op);
3435 append_to_async_thread (GSimpleAsyncResult *res,
3437 GCancellable *cancellable)
3440 GFileCreateFlags *data;
3441 GFileOutputStream *stream;
3442 GError *error = NULL;
3444 iface = G_FILE_GET_IFACE (object);
3446 data = g_simple_async_result_get_op_res_gpointer (res);
3448 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3452 g_simple_async_result_set_from_error (res, error);
3453 g_error_free (error);
3456 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3460 g_file_real_append_to_async (GFile *file,
3461 GFileCreateFlags flags,
3463 GCancellable *cancellable,
3464 GAsyncReadyCallback callback,
3467 GFileCreateFlags *data;
3468 GSimpleAsyncResult *res;
3470 data = g_new0 (GFileCreateFlags, 1);
3473 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3474 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3476 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3477 g_object_unref (res);
3480 static GFileOutputStream *
3481 g_file_real_append_to_finish (GFile *file,
3485 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3488 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3490 op = g_simple_async_result_get_op_res_gpointer (simple);
3492 return g_object_ref (op);
3498 create_async_thread (GSimpleAsyncResult *res,
3500 GCancellable *cancellable)
3503 GFileCreateFlags *data;
3504 GFileOutputStream *stream;
3505 GError *error = NULL;
3507 iface = G_FILE_GET_IFACE (object);
3509 data = g_simple_async_result_get_op_res_gpointer (res);
3511 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3515 g_simple_async_result_set_from_error (res, error);
3516 g_error_free (error);
3519 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3523 g_file_real_create_async (GFile *file,
3524 GFileCreateFlags flags,
3526 GCancellable *cancellable,
3527 GAsyncReadyCallback callback,
3530 GFileCreateFlags *data;
3531 GSimpleAsyncResult *res;
3533 data = g_new0 (GFileCreateFlags, 1);
3536 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3537 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3539 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3540 g_object_unref (res);
3543 static GFileOutputStream *
3544 g_file_real_create_finish (GFile *file,
3548 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3551 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3553 op = g_simple_async_result_get_op_res_gpointer (simple);
3555 return g_object_ref (op);
3561 GFileOutputStream *stream;
3563 gboolean make_backup;
3564 GFileCreateFlags flags;
3568 replace_async_data_free (ReplaceAsyncData *data)
3571 g_object_unref (data->stream);
3572 g_free (data->etag);
3577 replace_async_thread (GSimpleAsyncResult *res,
3579 GCancellable *cancellable)
3582 GFileOutputStream *stream;
3583 GError *error = NULL;
3584 ReplaceAsyncData *data;
3586 iface = G_FILE_GET_IFACE (object);
3588 data = g_simple_async_result_get_op_res_gpointer (res);
3590 stream = iface->replace (G_FILE (object),
3599 g_simple_async_result_set_from_error (res, error);
3600 g_error_free (error);
3603 data->stream = stream;
3607 g_file_real_replace_async (GFile *file,
3609 gboolean make_backup,
3610 GFileCreateFlags flags,
3612 GCancellable *cancellable,
3613 GAsyncReadyCallback callback,
3616 GSimpleAsyncResult *res;
3617 ReplaceAsyncData *data;
3619 data = g_new0 (ReplaceAsyncData, 1);
3620 data->etag = g_strdup (etag);
3621 data->make_backup = make_backup;
3622 data->flags = flags;
3624 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3625 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3627 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3628 g_object_unref (res);
3631 static GFileOutputStream *
3632 g_file_real_replace_finish (GFile *file,
3636 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3637 ReplaceAsyncData *data;
3639 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3641 data = g_simple_async_result_get_op_res_gpointer (simple);
3643 return g_object_ref (data->stream);
3651 } SetDisplayNameAsyncData;
3654 set_display_name_data_free (SetDisplayNameAsyncData *data)
3656 g_free (data->name);
3658 g_object_unref (data->file);
3663 set_display_name_async_thread (GSimpleAsyncResult *res,
3665 GCancellable *cancellable)
3667 GError *error = NULL;
3668 SetDisplayNameAsyncData *data;
3671 data = g_simple_async_result_get_op_res_gpointer (res);
3673 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3677 g_simple_async_result_set_from_error (res, error);
3678 g_error_free (error);
3685 g_file_real_set_display_name_async (GFile *file,
3686 const char *display_name,
3688 GCancellable *cancellable,
3689 GAsyncReadyCallback callback,
3692 GSimpleAsyncResult *res;
3693 SetDisplayNameAsyncData *data;
3695 data = g_new0 (SetDisplayNameAsyncData, 1);
3696 data->name = g_strdup (display_name);
3698 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3699 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3701 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3702 g_object_unref (res);
3706 g_file_real_set_display_name_finish (GFile *file,
3710 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3711 SetDisplayNameAsyncData *data;
3713 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3715 data = g_simple_async_result_get_op_res_gpointer (simple);
3717 return g_object_ref (data->file);
3723 GFileQueryInfoFlags flags;
3730 set_info_data_free (SetInfoAsyncData *data)
3733 g_object_unref (data->info);
3735 g_error_free (data->error);
3740 set_info_async_thread (GSimpleAsyncResult *res,
3742 GCancellable *cancellable)
3744 SetInfoAsyncData *data;
3746 data = g_simple_async_result_get_op_res_gpointer (res);
3749 data->res = g_file_set_attributes_from_info (G_FILE (object),
3757 g_file_real_set_attributes_async (GFile *file,
3759 GFileQueryInfoFlags flags,
3761 GCancellable *cancellable,
3762 GAsyncReadyCallback callback,
3765 GSimpleAsyncResult *res;
3766 SetInfoAsyncData *data;
3768 data = g_new0 (SetInfoAsyncData, 1);
3769 data->info = g_file_info_dup (info);
3770 data->flags = flags;
3772 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
3773 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
3775 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
3776 g_object_unref (res);
3780 g_file_real_set_attributes_finish (GFile *file,
3785 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3786 SetInfoAsyncData *data;
3788 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
3790 data = g_simple_async_result_get_op_res_gpointer (simple);
3793 *info = g_object_ref (data->info);
3795 if (error != NULL && data->error)
3796 *error = g_error_copy (data->error);
3801 /********************************************
3802 * Default VFS operations *
3803 ********************************************/
3806 * g_file_new_for_path:
3807 * @path: a string containing a relative or absolute path.
3809 * Constructs a #GFile for a given path. This operation never
3810 * fails, but the returned object might not support any I/O
3811 * operation if @path is malformed.
3813 * Returns: a new #GFile for the given @path.
3816 g_file_new_for_path (const char *path)
3818 g_return_val_if_fail (path != NULL, NULL);
3820 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
3824 * g_file_new_for_uri:
3825 * @uri: a string containing a URI.
3827 * Constructs a #GFile for a given URI. This operation never
3828 * fails, but the returned object might not support any I/O
3829 * operation if @uri is malformed or if the uri type is
3832 * Returns: a #GFile for the given @uri.
3835 g_file_new_for_uri (const char *uri)
3837 g_return_val_if_fail (uri != NULL, NULL);
3839 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
3843 * g_file_parse_name:
3844 * @parse_name: a file name or path to be parsed.
3846 * Constructs a #GFile with the given @parse_name,
3847 * looked up by #GVfs. This operation never fails,
3848 * but the returned object might not support any I/O
3849 * operation if the @parse_name cannot be parsed by #GVfs.
3851 * Returns: a new #GFile.
3854 g_file_parse_name (const char *parse_name)
3856 g_return_val_if_fail (parse_name != NULL, NULL);
3858 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
3862 is_valid_scheme_character (char c)
3864 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
3868 has_valid_scheme (const char *uri)
3874 if (!is_valid_scheme_character (*p))
3879 } while (is_valid_scheme_character (*p));
3885 * g_file_new_for_commandline_arg:
3886 * @arg: a command line string.
3888 * Attempts to generate a #GFile with the given line from
3889 * the command line argument.
3891 * Returns: a new #GFile.
3894 g_file_new_for_commandline_arg (const char *arg)
3900 g_return_val_if_fail (arg != NULL, NULL);
3902 if (g_path_is_absolute (arg))
3903 return g_file_new_for_path (arg);
3905 if (has_valid_scheme (arg))
3906 return g_file_new_for_uri (arg);
3908 current_dir = g_get_current_dir ();
3909 filename = g_build_filename (current_dir, arg, NULL);
3910 g_free (current_dir);
3912 file = g_file_new_for_path (filename);
3919 * g_mount_for_location:
3920 * @location: input #GFile.
3921 * @mount_operation: a #GMountOperation.
3922 * @cancellable: optional #GCancellable object, %NULL to ignore.
3923 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3924 * @user_data: the data to pass to callback function
3926 * Starts the @mount_operation, mounting the volume at @location.
3928 * When this operation has completed, @callback will be called with
3929 * @user_user data, and the operation can be finalized with
3930 * g_mount_for_location_finish().
3932 * If @cancellable is not %NULL, then the operation can be cancelled by
3933 * triggering the cancellable object from another thread. If the operation
3934 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3937 g_mount_for_location (GFile *location,
3938 GMountOperation *mount_operation,
3939 GCancellable *cancellable,
3940 GAsyncReadyCallback callback,
3945 g_return_if_fail (G_IS_FILE (location));
3946 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
3948 iface = G_FILE_GET_IFACE (location);
3950 if (iface->mount_for_location == NULL)
3952 g_simple_async_report_error_in_idle (G_OBJECT (location),
3953 callback, user_data,
3954 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3955 _("volume doesn't implement mount"));
3960 (* iface->mount_for_location) (location, mount_operation, cancellable, callback, user_data);
3965 * g_mount_for_location_finish:
3966 * @location: input #GFile.
3967 * @result: a #GAsyncResult.
3968 * @error: a #GError, or %NULL
3970 * Finishes a mount operation started by g_mount_for_location().
3972 * Returns: %TRUE if successful. If an error
3973 * has occured, this function will return %FALSE and set @error
3974 * appropriately if present.
3977 g_mount_for_location_finish (GFile *location,
3978 GAsyncResult *result,
3983 g_return_val_if_fail (G_IS_FILE (location), FALSE);
3984 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3986 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3988 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3989 if (g_simple_async_result_propagate_error (simple, error))
3993 iface = G_FILE_GET_IFACE (location);
3995 return (* iface->mount_for_location_finish) (location, result, error);
3998 /********************************************
3999 * Utility functions *
4000 ********************************************/
4002 #define GET_CONTENT_BLOCK_SIZE 8192
4005 * g_file_load_contents:
4006 * @file: input #GFile.
4007 * @cancellable: optional #GCancellable object, %NULL to ignore.
4008 * @contents: a location to place the contents of the file.
4009 * @length: a location to place the length of the contents of the file.
4010 * @etag_out: a location to place the current entity tag for the file.
4011 * @error: a #GError, or %NULL
4013 * If @cancellable is not %NULL, then the operation can be cancelled by
4014 * triggering the cancellable object from another thread. If the operation
4015 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4017 * Returns: %TRUE if the @file's contents were successfully loaded.
4018 * %FALSE if there were errors. The length of the loaded data will be
4019 * put into @length, the contents in @contents.
4022 g_file_load_contents (GFile *file,
4023 GCancellable *cancellable,
4029 GFileInputStream *in;
4030 GByteArray *content;
4035 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4036 g_return_val_if_fail (contents != NULL, FALSE);
4038 in = g_file_read (file, cancellable, error);
4042 content = g_byte_array_new ();
4045 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4046 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4047 content->data + pos,
4048 GET_CONTENT_BLOCK_SIZE,
4049 cancellable, error)) > 0)
4052 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4059 info = g_file_input_stream_query_info (in,
4060 G_FILE_ATTRIBUTE_ETAG_VALUE,
4065 *etag_out = g_strdup (g_file_info_get_etag (info));
4066 g_object_unref (info);
4070 /* Ignore errors on close */
4071 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4072 g_object_unref (in);
4076 /* error is set already */
4077 g_byte_array_free (content, TRUE);
4084 /* Zero terminate (we got an extra byte allocated for this */
4085 content->data[pos] = 0;
4087 *contents = (char *)g_byte_array_free (content, FALSE);
4095 GCancellable *cancellable;
4096 GFileReadMoreCallback read_more_callback;
4097 GAsyncReadyCallback callback;
4099 GByteArray *content;
4106 load_contents_data_free (LoadContentsData *data)
4109 g_error_free (data->error);
4110 if (data->cancellable)
4111 g_object_unref (data->cancellable);
4113 g_byte_array_free (data->content, TRUE);
4114 g_free (data->etag);
4115 g_object_unref (data->file);
4120 load_contents_close_callback (GObject *obj,
4121 GAsyncResult *close_res,
4124 GInputStream *stream = G_INPUT_STREAM (obj);
4125 LoadContentsData *data = user_data;
4126 GSimpleAsyncResult *res;
4128 /* Ignore errors here, we're only reading anyway */
4129 g_input_stream_close_finish (stream, close_res, NULL);
4130 g_object_unref (stream);
4132 res = g_simple_async_result_new (G_OBJECT (data->file),
4135 g_file_load_contents_async);
4136 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4137 g_simple_async_result_complete (res);
4138 g_object_unref (res);
4142 load_contents_fstat_callback (GObject *obj,
4143 GAsyncResult *stat_res,
4146 GInputStream *stream = G_INPUT_STREAM (obj);
4147 LoadContentsData *data = user_data;
4150 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4154 data->etag = g_strdup (g_file_info_get_etag (info));
4155 g_object_unref (info);
4158 g_input_stream_close_async (stream, 0,
4160 load_contents_close_callback, data);
4164 load_contents_read_callback (GObject *obj,
4165 GAsyncResult *read_res,
4168 GInputStream *stream = G_INPUT_STREAM (obj);
4169 LoadContentsData *data = user_data;
4170 GError *error = NULL;
4173 read_size = g_input_stream_read_finish (stream, read_res, &error);
4177 /* Error or EOF, close the file */
4178 data->error = error;
4179 g_input_stream_close_async (stream, 0,
4181 load_contents_close_callback, data);
4183 else if (read_size == 0)
4185 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4186 G_FILE_ATTRIBUTE_ETAG_VALUE,
4189 load_contents_fstat_callback,
4192 else if (read_size > 0)
4194 data->pos += read_size;
4196 g_byte_array_set_size (data->content,
4197 data->pos + GET_CONTENT_BLOCK_SIZE);
4200 if (data->read_more_callback &&
4201 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4202 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4203 G_FILE_ATTRIBUTE_ETAG_VALUE,
4206 load_contents_fstat_callback,
4209 g_input_stream_read_async (stream,
4210 data->content->data + data->pos,
4211 GET_CONTENT_BLOCK_SIZE,
4214 load_contents_read_callback,
4220 load_contents_open_callback (GObject *obj,
4221 GAsyncResult *open_res,
4224 GFile *file = G_FILE (obj);
4225 GFileInputStream *stream;
4226 LoadContentsData *data = user_data;
4227 GError *error = NULL;
4228 GSimpleAsyncResult *res;
4230 stream = g_file_read_finish (file, open_res, &error);
4234 g_byte_array_set_size (data->content,
4235 data->pos + GET_CONTENT_BLOCK_SIZE);
4236 g_input_stream_read_async (G_INPUT_STREAM (stream),
4237 data->content->data + data->pos,
4238 GET_CONTENT_BLOCK_SIZE,
4241 load_contents_read_callback,
4247 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4251 g_simple_async_result_complete (res);
4252 g_error_free (error);
4253 load_contents_data_free (data);
4254 g_object_unref (res);
4259 * g_file_load_partial_contents_async:
4260 * @file: input #GFile.
4261 * @cancellable: optional #GCancellable object, %NULL to ignore.
4262 * @read_more_callback: a #GFileReadMoreCallback.
4263 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4264 * @user_data: the data to pass to the callback functions.
4266 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
4267 * used to stop reading from the file when appropriate, else this function
4268 * will behave exactly as g_file_load_contents_async(). This operation
4269 * can be finished by g_file_load_partial_contents_finish().
4271 * Users of this function should be aware that @user_data is passed to
4272 * both the @read_more_callback and the @callback.
4274 * If @cancellable is not %NULL, then the operation can be cancelled by
4275 * triggering the cancellable object from another thread. If the operation
4276 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4279 g_file_load_partial_contents_async (GFile *file,
4280 GCancellable *cancellable,
4281 GFileReadMoreCallback read_more_callback,
4282 GAsyncReadyCallback callback,
4285 LoadContentsData *data;
4287 g_return_if_fail (G_IS_FILE (file));
4289 data = g_new0 (LoadContentsData, 1);
4292 data->cancellable = g_object_ref (cancellable);
4293 data->read_more_callback = read_more_callback;
4294 data->callback = callback;
4295 data->user_data = user_data;
4296 data->content = g_byte_array_new ();
4297 data->file = g_object_ref (file);
4299 g_file_read_async (file,
4302 load_contents_open_callback,
4307 * g_file_load_partial_contents_finish:
4308 * @file: input #GFile.
4309 * @res: a #GAsyncResult.
4310 * @contents: a location to place the contents of the file.
4311 * @length: a location to place the length of the contents of the file.
4312 * @etag_out: a location to place the current entity tag for the file.
4313 * @error: a #GError, or %NULL
4315 * Finishes an asynchronous partial load operation that was started
4316 * with g_file_load_partial_contents_async().
4318 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4319 * present, it will be set appropriately.
4322 g_file_load_partial_contents_finish (GFile *file,
4329 GSimpleAsyncResult *simple;
4330 LoadContentsData *data;
4332 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4333 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4334 g_return_val_if_fail (contents != NULL, FALSE);
4336 simple = G_SIMPLE_ASYNC_RESULT (res);
4338 if (g_simple_async_result_propagate_error (simple, error))
4341 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4343 data = g_simple_async_result_get_op_res_gpointer (simple);
4347 g_propagate_error (error, data->error);
4356 *length = data->pos;
4360 *etag_out = data->etag;
4364 /* Zero terminate */
4365 g_byte_array_set_size (data->content, data->pos + 1);
4366 data->content->data[data->pos] = 0;
4368 *contents = (char *)g_byte_array_free (data->content, FALSE);
4369 data->content = NULL;
4375 * g_file_load_contents_async:
4376 * @file: input #GFile.
4377 * @cancellable: optional #GCancellable object, %NULL to ignore.
4378 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4379 * @user_data: the data to pass to callback function
4381 * Starts an asynchronous load of the @file's contents.
4382 * When the load operation has completed, @callback will be called
4383 * with @userdata. To finish the operation, call
4384 * g_file_load_contents_finish() with the #GAsyncResult returned by
4387 * If @cancellable is not %NULL, then the operation can be cancelled by
4388 * triggering the cancellable object from another thread. If the operation
4389 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4392 g_file_load_contents_async (GFile *file,
4393 GCancellable *cancellable,
4394 GAsyncReadyCallback callback,
4397 g_file_load_partial_contents_async (file,
4400 callback, user_data);
4404 * g_file_load_contents_finish:
4405 * @file: input #GFile.
4406 * @res: a #GAsyncResult.
4407 * @contents: a location to place the contents of the file.
4408 * @length: a location to place the length of the contents of the file.
4409 * @etag_out: a location to place the current entity tag for the file.
4410 * @error: a #GError, or %NULL
4412 * Finishes an asynchronous load of the @file's contents.
4413 * The contents are placed in @contents, and @length is set to the
4414 * size of the @contents string. If @etag_out is present, it will be
4415 * set to the new entity tag for the @file.
4417 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4418 * present, it will be set appropriately.
4421 g_file_load_contents_finish (GFile *file,
4428 return g_file_load_partial_contents_finish (file,
4437 * g_file_replace_contents:
4438 * @file: input #GFile.
4439 * @contents: a string containing the new contents for @file.
4440 * @length: the length of @contents in bytes.
4441 * @etag: the old <link linkend="gfile-etag">entity tag</link>
4443 * @make_backup: a #gboolean.
4444 * @flags: a set of #GFileCreateFlags.
4445 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4447 * @cancellable: optional #GCancellable object, %NULL to ignore.
4448 * @error: a #GError, or %NULL
4450 * Replaces the contents of @file with @contents of @length bytes.
4452 * If @etag is specified (not %NULL) any existing file must have that etag, or
4453 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
4455 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
4457 * If @cancellable is not %NULL, then the operation can be cancelled by
4458 * triggering the cancellable object from another thread. If the operation
4459 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4461 * The returned @new_etag can be used to verify that the file hasn't changed the
4462 * next time it is saved over.
4464 * Returns: %TRUE if successful. If an error
4465 * has occured, this function will return %FALSE and set @error
4466 * appropriately if present.
4469 g_file_replace_contents (GFile *file,
4470 const char *contents,
4473 gboolean make_backup,
4474 GFileCreateFlags flags,
4476 GCancellable *cancellable,
4479 GFileOutputStream *out;
4480 gsize pos, remainder;
4483 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4484 g_return_val_if_fail (contents != NULL, FALSE);
4486 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4492 while (remainder > 0 &&
4493 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4495 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4503 if (remainder > 0 && res < 0)
4505 /* Ignore errors on close */
4506 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4508 /* error is set already */
4512 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4516 *new_etag = g_file_output_stream_get_etag (out);
4524 GCancellable *cancellable;
4525 GAsyncReadyCallback callback;
4527 const char *content;
4531 } ReplaceContentsData;
4534 replace_contents_data_free (ReplaceContentsData *data)
4537 g_error_free (data->error);
4538 if (data->cancellable)
4539 g_object_unref (data->cancellable);
4540 g_object_unref (data->file);
4541 g_free (data->etag);
4546 replace_contents_close_callback (GObject *obj,
4547 GAsyncResult *close_res,
4550 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4551 ReplaceContentsData *data = user_data;
4552 GSimpleAsyncResult *res;
4554 /* Ignore errors here, we're only reading anyway */
4555 g_output_stream_close_finish (stream, close_res, NULL);
4556 g_object_unref (stream);
4558 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4560 res = g_simple_async_result_new (G_OBJECT (data->file),
4563 g_file_replace_contents_async);
4564 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4565 g_simple_async_result_complete (res);
4566 g_object_unref (res);
4570 replace_contents_write_callback (GObject *obj,
4571 GAsyncResult *read_res,
4574 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4575 ReplaceContentsData *data = user_data;
4576 GError *error = NULL;
4579 write_size = g_output_stream_write_finish (stream, read_res, &error);
4581 if (write_size <= 0)
4583 /* Error or EOF, close the file */
4585 data->error = error;
4586 g_output_stream_close_async (stream, 0,
4588 replace_contents_close_callback, data);
4590 else if (write_size > 0)
4592 data->pos += write_size;
4594 if (data->pos >= data->length)
4595 g_output_stream_close_async (stream, 0,
4597 replace_contents_close_callback, data);
4599 g_output_stream_write_async (stream,
4600 data->content + data->pos,
4601 data->length - data->pos,
4604 replace_contents_write_callback,
4610 replace_contents_open_callback (GObject *obj,
4611 GAsyncResult *open_res,
4614 GFile *file = G_FILE (obj);
4615 GFileOutputStream *stream;
4616 ReplaceContentsData *data = user_data;
4617 GError *error = NULL;
4618 GSimpleAsyncResult *res;
4620 stream = g_file_replace_finish (file, open_res, &error);
4624 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4625 data->content + data->pos,
4626 data->length - data->pos,
4629 replace_contents_write_callback,
4635 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4639 g_simple_async_result_complete (res);
4640 g_error_free (error);
4641 replace_contents_data_free (data);
4642 g_object_unref (res);
4647 * g_file_replace_contents_async:
4648 * @file: input #GFile.
4649 * @contents: string of contents to replace the file with.
4650 * @length: the length of @contents in bytes.
4651 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4652 * @make_backup: a #gboolean.
4653 * @flags: a set of #GFileCreateFlags.
4654 * @cancellable: optional #GCancellable object, %NULL to ignore.
4655 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4656 * @user_data: the data to pass to callback function
4658 * Starts an asynchronous replacement of @file with the given
4659 * @contents of @length bytes. @etag will replace the document's
4660 * current entity tag.
4662 * When this operation has completed, @callback will be called with
4663 * @user_user data, and the operation can be finalized with
4664 * g_file_replace_contents_finish().
4666 * If @cancellable is not %NULL, then the operation can be cancelled by
4667 * triggering the cancellable object from another thread. If the operation
4668 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4670 * If @make_backup is %TRUE, this function will attempt to
4671 * make a backup of @file.
4674 g_file_replace_contents_async (GFile *file,
4675 const char *contents,
4678 gboolean make_backup,
4679 GFileCreateFlags flags,
4680 GCancellable *cancellable,
4681 GAsyncReadyCallback callback,
4684 ReplaceContentsData *data;
4686 g_return_if_fail (G_IS_FILE (file));
4687 g_return_if_fail (contents != NULL);
4689 data = g_new0 (ReplaceContentsData, 1);
4692 data->cancellable = g_object_ref (cancellable);
4693 data->callback = callback;
4694 data->user_data = user_data;
4695 data->content = contents;
4696 data->length = length;
4698 data->file = g_object_ref (file);
4700 g_file_replace_async (file,
4706 replace_contents_open_callback,
4711 * g_file_replace_contents_finish:
4712 * @file: input #GFile.
4713 * @res: a #GAsyncResult.
4714 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
4716 * @error: a #GError, or %NULL
4718 * Finishes an asynchronous replace of the given @file. See
4719 * g_file_replace_contents_async(). Sets @new_etag to the new entity
4720 * tag for the document, if present.
4722 * Returns: %TRUE on success, %FALSE on failure.
4725 g_file_replace_contents_finish (GFile *file,
4730 GSimpleAsyncResult *simple;
4731 ReplaceContentsData *data;
4733 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4734 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4736 simple = G_SIMPLE_ASYNC_RESULT (res);
4738 if (g_simple_async_result_propagate_error (simple, error))
4741 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
4743 data = g_simple_async_result_get_op_res_gpointer (simple);
4747 g_propagate_error (error, data->error);
4755 *new_etag = data->etag;
4756 data->etag = NULL; /* Take ownership */
4762 #define __G_FILE_C__
4763 #include "gioaliasdef.c"