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.
56 * You can move through the filesystem with #GFile handles with
57 * g_file_get_parent() to get a handle to the parent directory.
58 * g_file_get_child() to get a handle to a child within a directory.
59 * g_file_resolve_relative_path() to resolve a relative path between
60 * two #GFile<!-- -->s.
62 * Many #GFile operations have both synchronous and asynchronous versions
63 * to suit your application. Asynchronous versions of synchronous functions
64 * simply have _async() appended to their function names. The asynchronous
65 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
66 * the operation, which is then passed to the function's matching _finish()
69 * Some #GFile operations do not have synchronous analogs, as they may
70 * take a very long time to finish, and blocking may leave an application
71 * unusable. Notable cases include:
72 * g_file_mount_mountable() to mount a mountable file.
73 * g_file_unmount_mountable() to unmount a mountable file.
74 * g_file_eject_mountable() to eject a mountable file.
76 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
77 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
78 * short. Entity tags are somewhat like a more abstract version of the
79 * traditional mtime, and can be used to quickly determine if the file has
80 * been modified from the version on the file system. See the HTTP 1.1
81 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
82 * for HTTP Etag headers, which are a very similar concept.
86 static void g_file_base_init (gpointer g_class);
87 static void g_file_class_init (gpointer g_class,
90 static void g_file_real_query_info_async (GFile *file,
91 const char *attributes,
92 GFileQueryInfoFlags flags,
94 GCancellable *cancellable,
95 GAsyncReadyCallback callback,
97 static GFileInfo * g_file_real_query_info_finish (GFile *file,
100 static void g_file_real_enumerate_children_async (GFile *file,
101 const char *attributes,
102 GFileQueryInfoFlags flags,
104 GCancellable *cancellable,
105 GAsyncReadyCallback callback,
107 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
110 static void g_file_real_read_async (GFile *file,
112 GCancellable *cancellable,
113 GAsyncReadyCallback callback,
115 static GFileInputStream * g_file_real_read_finish (GFile *file,
118 static void g_file_real_append_to_async (GFile *file,
119 GFileCreateFlags flags,
121 GCancellable *cancellable,
122 GAsyncReadyCallback callback,
124 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
127 static void g_file_real_create_async (GFile *file,
128 GFileCreateFlags flags,
130 GCancellable *cancellable,
131 GAsyncReadyCallback callback,
133 static GFileOutputStream *g_file_real_create_finish (GFile *file,
136 static void g_file_real_replace_async (GFile *file,
138 gboolean make_backup,
139 GFileCreateFlags flags,
141 GCancellable *cancellable,
142 GAsyncReadyCallback callback,
144 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
147 static gboolean g_file_real_set_attributes_from_info (GFile *file,
149 GFileQueryInfoFlags flags,
150 GCancellable *cancellable,
152 static void g_file_real_set_display_name_async (GFile *file,
153 const char *display_name,
155 GCancellable *cancellable,
156 GAsyncReadyCallback callback,
158 static GFile * g_file_real_set_display_name_finish (GFile *file,
161 static void g_file_real_set_attributes_async (GFile *file,
163 GFileQueryInfoFlags flags,
165 GCancellable *cancellable,
166 GAsyncReadyCallback callback,
168 static gboolean g_file_real_set_attributes_finish (GFile *file,
174 g_file_get_type (void)
176 static GType file_type = 0;
180 static const GTypeInfo file_info =
182 sizeof (GFileIface), /* class_size */
183 g_file_base_init, /* base_init */
184 NULL, /* base_finalize */
186 NULL, /* class_finalize */
187 NULL, /* class_data */
194 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
197 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
204 g_file_class_init (gpointer g_class,
207 GFileIface *iface = g_class;
209 iface->enumerate_children_async = g_file_real_enumerate_children_async;
210 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
211 iface->set_display_name_async = g_file_real_set_display_name_async;
212 iface->set_display_name_finish = g_file_real_set_display_name_finish;
213 iface->query_info_async = g_file_real_query_info_async;
214 iface->query_info_finish = g_file_real_query_info_finish;
215 iface->set_attributes_async = g_file_real_set_attributes_async;
216 iface->set_attributes_finish = g_file_real_set_attributes_finish;
217 iface->read_async = g_file_real_read_async;
218 iface->read_finish = g_file_real_read_finish;
219 iface->append_to_async = g_file_real_append_to_async;
220 iface->append_to_finish = g_file_real_append_to_finish;
221 iface->create_async = g_file_real_create_async;
222 iface->create_finish = g_file_real_create_finish;
223 iface->replace_async = g_file_real_replace_async;
224 iface->replace_finish = g_file_real_replace_finish;
225 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
229 g_file_base_init (gpointer g_class)
236 * @file: input #GFile.
238 * Checks to see if a file is native to the platform.
240 * Returns: %TRUE if file is native. (If the #GFile<!---->'s expressed in
241 * the platform-native filename format, e.g. "C:\Windows", "/usr/bin/").
244 g_file_is_native (GFile *file)
248 g_return_val_if_fail (G_IS_FILE (file), FALSE);
250 iface = G_FILE_GET_IFACE (file);
252 return (* iface->is_native) (file);
257 * g_file_has_uri_scheme:
258 * @file: input #GFile.
259 * @uri_scheme: a string containing a URI scheme.
261 * Checks to see if a #GFile has a given URI scheme.
263 * Returns: %TRUE if #GFile's backend supports the
264 * given URI scheme, %FALSE if URI scheme is %NULL,
265 * not supported, or #GFile is invalid.
268 g_file_has_uri_scheme (GFile *file,
269 const char *uri_scheme)
273 g_return_val_if_fail (G_IS_FILE (file), FALSE);
274 g_return_val_if_fail (uri_scheme != NULL, FALSE);
276 iface = G_FILE_GET_IFACE (file);
278 return (* iface->has_uri_scheme) (file, uri_scheme);
283 * g_file_get_uri_scheme:
284 * @file: input #GFile.
286 * Gets the URI scheme for a #GFile.
287 * RFC 3986 decodes the scheme as:
289 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
291 * Common schemes include "file", "http", "svn", etc.
293 * Returns: a string containing the URI scheme for the given
294 * #GFile. The returned string should be freed with g_free()
295 * when no longer needed.
298 g_file_get_uri_scheme (GFile *file)
302 g_return_val_if_fail (G_IS_FILE (file), NULL);
304 iface = G_FILE_GET_IFACE (file);
306 return (* iface->get_uri_scheme) (file);
311 * g_file_get_basename:
312 * @file: input #GFile.
314 * Gets the basename for a given #GFile.
316 * Returns: string containing the #GFile's base name, or %NULL
317 * if given #GFile is invalid. The returned string should be
318 * freed with g_free() when no longer needed.
321 g_file_get_basename (GFile *file)
325 g_return_val_if_fail (G_IS_FILE (file), NULL);
327 iface = G_FILE_GET_IFACE (file);
329 return (* iface->get_basename) (file);
334 * @file: input #GFile.
336 * Gets the current path within a #GFile.
338 * Returns: string containing the #GFile's path, or %NULL if
339 * given #GFile is invalid. The returned string should be
340 * freed with g_free() when no longer needed.
343 g_file_get_path (GFile *file)
347 g_return_val_if_fail (G_IS_FILE (file), NULL);
349 iface = G_FILE_GET_IFACE (file);
351 return (* iface->get_path) (file);
356 * @file: input #GFile.
358 * Gets a URI for the path within a #GFile.
360 * Returns: a string containing the #GFile's URI or %NULL
361 * if given #GFile is invalid. The returned string should
362 * be freed with g_free() when no longer needed.
365 g_file_get_uri (GFile *file)
369 g_return_val_if_fail (G_IS_FILE (file), NULL);
371 iface = G_FILE_GET_IFACE (file);
373 return (* iface->get_uri) (file);
377 * g_file_get_parse_name:
378 * @file: input #GFile.
380 * Gets the UTF-8 parsed name for the #GFile.
382 * Returns: a string containing the #GFile's parsed name,
383 * or %NULL if given #GFile is invalid. The returned
384 * string should be freed with g_free() when no longer needed.
387 g_file_get_parse_name (GFile *file)
391 g_return_val_if_fail (G_IS_FILE (file), NULL);
393 iface = G_FILE_GET_IFACE (file);
395 return (* iface->get_parse_name) (file);
400 * @file: input #GFile.
402 * Duplicates a #GFile handle. This operation does not duplicate
403 * the actual file or directory represented by the #GFile; see
404 * g_file_copy() if attempting to copy a file.
406 * Returns: #GFile that is a duplicate of the given #GFile,
407 * or %NULL if given #GFile is invalid.
410 g_file_dup (GFile *file)
414 g_return_val_if_fail (G_IS_FILE (file), NULL);
416 iface = G_FILE_GET_IFACE (file);
418 return (* iface->dup) (file);
423 * @file: #gconstpointer to a #GFile.
425 * Creates a hash value for a #GFile.
427 * Returns: 0 if @file is not a valid #GFile, otherwise an
428 * integer that can be used as hash value for the #GFile.
429 * This function is intended for easily hashing a #GFile to
430 * add to a #GHashTable or similar data structure.
433 g_file_hash (gconstpointer file)
437 g_return_val_if_fail (G_IS_FILE (file), 0);
439 iface = G_FILE_GET_IFACE (file);
441 return (* iface->hash) ((GFile *)file);
446 * @file1: the first #GFile.
447 * @file2: the second #GFile.
449 * Checks equality of two given #GFile<!-- -->s
451 * Returns: %TRUE if @file1 and @file2 are equal.
452 * %FALSE if either is not a #GFile.
455 g_file_equal (GFile *file1,
460 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
461 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
463 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
466 iface = G_FILE_GET_IFACE (file1);
468 return (* iface->equal) (file1, file2);
474 * @file: input #GFile.
476 * Gets the parent directory for the @file.
477 * If the @file represents the root directory of the
478 * file system, then %NULL will be returned.
480 * Returns: a #GFile structure to the parent of the given
484 g_file_get_parent (GFile *file)
488 g_return_val_if_fail (G_IS_FILE (file), NULL);
490 iface = G_FILE_GET_IFACE (file);
492 return (* iface->get_parent) (file);
497 * @file: input #GFile.
498 * @name: string containing the child's name.
500 * Gets a specific child of @file with name equal to @name if
503 * Returns: a #GFile to a child specified by
504 * @name or %NULL if @name is %NULL, or the specified
505 * child doesn't exist.
508 g_file_get_child (GFile *file,
511 g_return_val_if_fail (G_IS_FILE (file), NULL);
512 g_return_val_if_fail (name != NULL, NULL);
514 return g_file_resolve_relative_path (file, name);
518 * g_file_get_child_for_display_name:
519 * @file: input #GFile.
520 * @display_name: string to a possible child.
523 * Gets the child of @file for a given @display_name. If
524 * this function fails, it returns %NULL and @error will be
525 * set with %G_IO_ERROR_INVALID_FILENAME.
527 * Returns: a #GFile to the specified child, or
528 * %NULL if @display_name is %NULL.
531 g_file_get_child_for_display_name (GFile *file,
532 const char *display_name,
537 g_return_val_if_fail (G_IS_FILE (file), NULL);
538 g_return_val_if_fail (display_name != NULL, NULL);
540 iface = G_FILE_GET_IFACE (file);
542 return (* iface->get_child_for_display_name) (file, display_name, error);
546 * g_file_contains_file:
547 * @parent: input #GFile.
548 * @descendant: input #GFile.
550 * Checks whether @parent contains the specified @descendent.
552 * Returns: %TRUE if the @descendent's parent is @parent. %FALSE otherwise.
555 g_file_contains_file (GFile *parent,
560 g_return_val_if_fail (G_IS_FILE (parent), FALSE);
561 g_return_val_if_fail (G_IS_FILE (descendant), FALSE);
563 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
566 iface = G_FILE_GET_IFACE (parent);
568 return (* iface->contains_file) (parent, descendant);
572 * g_file_get_relative_path:
573 * @parent: input #GFile.
574 * @descendant: input #GFile.
576 * Gets the path for @descendant relative to @parent.
578 * Returns: string with the relative path from @descendant
579 * to @parent. The returned string should be freed with
580 * g_free() when no longer needed.
583 g_file_get_relative_path (GFile *parent,
588 g_return_val_if_fail (G_IS_FILE (parent), NULL);
589 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
591 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
594 iface = G_FILE_GET_IFACE (parent);
596 return (* iface->get_relative_path) (parent, descendant);
600 * g_file_resolve_relative_path:
601 * @file: input #GFile.
602 * @relative_path: a given relative path string.
604 * Resolves a relative path for @file to an absolute path.
606 * Returns: #GFile to the resolved path. %NULL if @relative_path
607 * is %NULL or if @file is invalid.
610 g_file_resolve_relative_path (GFile *file,
611 const char *relative_path)
615 g_return_val_if_fail (G_IS_FILE (file), NULL);
616 g_return_val_if_fail (relative_path != NULL, NULL);
618 iface = G_FILE_GET_IFACE (file);
620 return (* iface->resolve_relative_path) (file, relative_path);
624 * g_file_enumerate_children:
625 * @file: input #GFile.
626 * @attributes: a string containing a #GFileAttributeInfo query.
627 * @flags: a set of #GFileQueryInfoFlags.
628 * @cancellable: optional #GCancellable object, %NULL to ignore.
629 * @error: #GError for error reporting.
631 * Gets a #GFileEnumerator for the children of @file that match @attributes,
632 * where attributes is a #GFileAttributeInfo query string (e.g. "std:type",
635 * If @cancellable is not %NULL, then the operation can be cancelled by
636 * triggering the cancellable object from another thread. If the operation
637 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
639 * If the #GFileIface for the given @file does not support #GFileEnumerator,
640 * then %NULL will be returned and the error %G_IO_ERROR_NOT_SUPPORTED will
643 * Returns: A #GFileEnumerator if successful. %NULL if cancelled or if #GFile's
644 * backend doesn't support #GFileEnumerator.
647 g_file_enumerate_children (GFile *file,
648 const char *attributes,
649 GFileQueryInfoFlags flags,
650 GCancellable *cancellable,
656 g_return_val_if_fail (G_IS_FILE (file), NULL);
658 if (g_cancellable_set_error_if_cancelled (cancellable, error))
661 iface = G_FILE_GET_IFACE (file);
663 if (iface->enumerate_children == NULL)
665 g_set_error (error, G_IO_ERROR,
666 G_IO_ERROR_NOT_SUPPORTED,
667 _("Operation not supported"));
671 return (* iface->enumerate_children) (file, attributes, flags,
676 * g_file_enumerate_children_async:
677 * @file: input #GFile.
678 * @attributes: a string containing a #GFileAttributeInfo query.
679 * @flags: a set of #GFileQueryInfoFlags.
680 * @io_priority: the <link linkend="io-priority">I/O priority</link>
682 * @cancellable: optional #GCancellable object, %NULL to ignore.
683 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
684 * @user_data: the data to pass to callback function
686 * Asynchronously gets a #GFileEnumerator for the children of @file that
687 * match @attributes, where attributes is a #GFileAttributeInfo query
688 * string (e.g. "std:type", "std:*"). For the synchronous version of this
689 * function, see g_file_enumerate_children().
691 * To finish this asynchronous operation, see g_file_enumerate_children_finish().
694 g_file_enumerate_children_async (GFile *file,
695 const char *attributes,
696 GFileQueryInfoFlags flags,
698 GCancellable *cancellable,
699 GAsyncReadyCallback callback,
704 g_return_if_fail (G_IS_FILE (file));
706 iface = G_FILE_GET_IFACE (file);
707 (* iface->enumerate_children_async) (file,
717 * g_file_enumerate_children_finish:
718 * @file: input #GFile.
719 * @res: a #GAsyncResult.
722 * Finishes an async enumerate children operation.
724 * If @cancellable was not %NULL when g_file_enumerate_children_async()
725 * was called, then the operation could have been cancelled by triggering
726 * the cancellable object from another thread. If the operation was cancelled,
727 * the @error will be set to %G_IO_ERROR_CANCELLED and this function will
730 * If the #GFileIface for the given @file does not support enumerating files,
731 * then %NULL will be returned and the error %G_IO_ERROR_NOT_SUPPORTED will
734 * Returns: a #GFileEnumerator or %NULL if an error occurred.
737 g_file_enumerate_children_finish (GFile *file,
743 g_return_val_if_fail (G_IS_FILE (file), NULL);
744 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
746 if (G_IS_SIMPLE_ASYNC_RESULT (res))
748 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
749 if (g_simple_async_result_propagate_error (simple, error))
753 iface = G_FILE_GET_IFACE (file);
754 return (* iface->enumerate_children_finish) (file, res, error);
760 * @file: input #GFile.
761 * @attributes: a string containing a #GFileAttributeInfo query.
762 * @flags: a set of #GFileQueryInfoFlags.
763 * @cancellable: optional #GCancellable object, %NULL to ignore.
766 * If @cancellable is not %NULL, then the operation can be cancelled by
767 * triggering the cancellable object from another thread. If the operation
768 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
770 * If the #GFileIface for the given @file does not support querying file
771 * information, then %NULL will be returned and the error
772 * %G_IO_ERROR_NOT_SUPPORTED will be set in @error.
774 * Returns: a #GFileInfo for the given @file, or %NULL on error.
777 g_file_query_info (GFile *file,
778 const char *attributes,
779 GFileQueryInfoFlags flags,
780 GCancellable *cancellable,
785 g_return_val_if_fail (G_IS_FILE (file), NULL);
787 if (g_cancellable_set_error_if_cancelled (cancellable, error))
790 iface = G_FILE_GET_IFACE (file);
792 if (iface->query_info == NULL)
794 g_set_error (error, G_IO_ERROR,
795 G_IO_ERROR_NOT_SUPPORTED,
796 _("Operation not supported"));
800 return (* iface->query_info) (file, attributes, flags, cancellable, error);
804 * g_file_query_info_async:
805 * @file: input #GFile.
806 * @attributes: a string containing a #GFileAttributeInfo query.
807 * @flags: a set of #GFileQueryInfoFlags.
808 * @io_priority: the <link linkend="io-priority">I/O priority</link>
810 * @cancellable: optional #GCancellable object, %NULL to ignore.
811 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
812 * @user_data: the data to pass to callback function
814 * If @cancellable is not %NULL, then the operation can be cancelled by
815 * triggering the cancellable object from another thread. If the operation
816 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
818 * To finish this asynchronous operation, see g_file_query_info_finish().
821 g_file_query_info_async (GFile *file,
822 const char *attributes,
823 GFileQueryInfoFlags flags,
825 GCancellable *cancellable,
826 GAsyncReadyCallback callback,
831 g_return_if_fail (G_IS_FILE (file));
833 iface = G_FILE_GET_IFACE (file);
834 (* iface->query_info_async) (file,
844 * g_file_query_info_finish:
845 * @file: input #GFile.
846 * @res: a #GAsyncResult.
849 * Finishes an asynchronous file info query.
851 * If @cancellable was not %NULL when g_file_query_info_async() was called,
852 * then the operation could have been cancelled by triggering the cancellable
853 * object from another thread. If the operation was cancelled, the @error will
854 * be set to %G_IO_ERROR_CANCELLED and this function will return %NULL.
856 * If the #GFileIface for the given @file does not support querying file
857 * information, then %NULL will be returned and the error
858 * %G_IO_ERROR_NOT_SUPPORTED will be set in @error.
860 * Returns: #GFileInfo for given @file or %NULL on error.
863 g_file_query_info_finish (GFile *file,
869 g_return_val_if_fail (G_IS_FILE (file), NULL);
870 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
872 if (G_IS_SIMPLE_ASYNC_RESULT (res))
874 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
875 if (g_simple_async_result_propagate_error (simple, error))
879 iface = G_FILE_GET_IFACE (file);
880 return (* iface->query_info_finish) (file, res, error);
884 * g_file_query_filesystem_info:
885 * @file: input #GFile.
886 * @attributes: a string containing a #GFileAttributeInfo query.
887 * @cancellable: optional #GCancellable object, %NULL to ignore.
890 * Obtains attributes of a #GFile.
892 * If @cancellable is not %NULL, then the operation can be cancelled by
893 * triggering the cancellable object from another thread. If the operation
894 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
896 * If the #GFileIface for the given @file does not support querying file system
897 * information, then %NULL will be returned and the error
898 * %G_IO_ERROR_NOT_SUPPORTED will be set in @error.
900 * Returns: a #GFileInfo or %NULL if there was an error.
903 g_file_query_filesystem_info (GFile *file,
904 const char *attributes,
905 GCancellable *cancellable,
910 g_return_val_if_fail (G_IS_FILE (file), NULL);
912 if (g_cancellable_set_error_if_cancelled (cancellable, error))
915 iface = G_FILE_GET_IFACE (file);
917 if (iface->query_filesystem_info == NULL)
919 g_set_error (error, G_IO_ERROR,
920 G_IO_ERROR_NOT_SUPPORTED,
921 _("Operation not supported"));
925 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
929 * g_file_find_enclosing_volume:
930 * @file: input #GFile.
931 * @cancellable: optional #GCancellable object, %NULL to ignore.
934 * Gets a #GVolume for the #GFile.
936 * If the #GFileIface for @file does not have a volume (e.g. possibly a
937 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
940 * If @cancellable is not %NULL, then the operation can be cancelled by
941 * triggering the cancellable object from another thread. If the operation
942 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
944 * Returns: a #GVolume where the @file is located or %NULL on error.
947 g_file_find_enclosing_volume (GFile *file,
948 GCancellable *cancellable,
953 g_return_val_if_fail (G_IS_FILE (file), NULL);
955 if (g_cancellable_set_error_if_cancelled (cancellable, error))
958 iface = G_FILE_GET_IFACE (file);
959 if (iface->find_enclosing_volume == NULL)
961 g_set_error (error, G_IO_ERROR,
962 G_IO_ERROR_NOT_FOUND,
963 _("Containing volume does not exist"));
967 return (* iface->find_enclosing_volume) (file, cancellable, error);
972 * @file: #GFile to read.
973 * @cancellable: a #GCancellable
974 * @error: a #GError, or %NULL
976 * Reads a whole file into a #GFileInputStream. Fails returning %NULL if
977 * given #GFile points to a directory.
979 * If the #GFileIface for @file does not support reading files, then
980 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
982 * If @cancellable is not %NULL, then the operation can be cancelled by
983 * triggering the cancellable object from another thread. If the operation
984 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
986 * Returns: #GFileInputStream or %NULL on error.
989 g_file_read (GFile *file,
990 GCancellable *cancellable,
995 g_return_val_if_fail (G_IS_FILE (file), NULL);
997 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1000 iface = G_FILE_GET_IFACE (file);
1002 if (iface->read == NULL)
1004 g_set_error (error, G_IO_ERROR,
1005 G_IO_ERROR_NOT_SUPPORTED,
1006 _("Operation not supported"));
1010 return (* iface->read) (file, cancellable, error);
1015 * @file: input #GFile.
1016 * @flags: a set of #GFileCreateFlags.
1017 * @cancellable: optional #GCancellable object, %NULL to ignore.
1018 * @error: a #GError, or %NULL
1020 * Gets an output stream for appending to the file.
1022 * If the #GFileIface for @file does not support appending to files,
1023 * then @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will
1026 * If @cancellable is not %NULL, then the operation can be cancelled by
1027 * triggering the cancellable object from another thread. If the operation
1028 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1030 * Returns: a #GFileOutputStream.
1033 g_file_append_to (GFile *file,
1034 GFileCreateFlags flags,
1035 GCancellable *cancellable,
1040 g_return_val_if_fail (G_IS_FILE (file), NULL);
1042 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1045 iface = G_FILE_GET_IFACE (file);
1047 if (iface->append_to == NULL)
1049 g_set_error (error, G_IO_ERROR,
1050 G_IO_ERROR_NOT_SUPPORTED,
1051 _("Operation not supported"));
1055 return (* iface->append_to) (file, flags, cancellable, error);
1060 * @file: input #GFile.
1061 * @flags: a set of #GFileCreateFlags.
1062 * @cancellable: optional #GCancellable object, %NULL to ignore.
1063 * @error: a #GError, or %NULL
1065 * Creates the file and returns an output stream for writing to it.
1067 * If the #GFileIface for @file does not support creating files, then
1068 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
1070 * If @cancellable is not %NULL, then the operation can be cancelled by
1071 * triggering the cancellable object from another thread. If the operation
1072 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1074 * Returns: a #GFileOutputStream for the newly created file, or
1078 g_file_create (GFile *file,
1079 GFileCreateFlags flags,
1080 GCancellable *cancellable,
1085 g_return_val_if_fail (G_IS_FILE (file), NULL);
1087 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1090 iface = G_FILE_GET_IFACE (file);
1092 if (iface->create == NULL)
1094 g_set_error (error, G_IO_ERROR,
1095 G_IO_ERROR_NOT_SUPPORTED,
1096 _("Operation not supported"));
1100 return (* iface->create) (file, flags, cancellable, error);
1105 * @file: input #GFile.
1106 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1108 * @make_backup: %TRUE if a backup should be created`.
1109 * @flags: a set of #GFileCreateFlags.
1110 * @cancellable: optional #GCancellable object, %NULL to ignore.
1111 * @error: a #GError, or %NULL
1113 * Returns an output stream for overwriting the file, possibly
1114 * creating a backup copy of the file first.
1116 * If the #GFileIface for @file does not support streaming operations,
1117 * then @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will
1120 * If @cancellable is not %NULL, then the operation can be cancelled by
1121 * triggering the cancellable object from another thread. If the operation
1122 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1124 * @etag will replace the entity tag for the current file.
1126 * Returns: a #GFileOutputStream or %NULL on error. If @make_backup is
1127 * %TRUE, this function will attempt to make a backup of the current
1131 g_file_replace (GFile *file,
1133 gboolean make_backup,
1134 GFileCreateFlags flags,
1135 GCancellable *cancellable,
1140 g_return_val_if_fail (G_IS_FILE (file), NULL);
1142 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1145 iface = G_FILE_GET_IFACE (file);
1147 if (iface->replace == NULL)
1149 g_set_error (error, G_IO_ERROR,
1150 G_IO_ERROR_NOT_SUPPORTED,
1151 _("Operation not supported"));
1156 /* Handle empty tag string as NULL in consistent way. */
1157 if (etag && *etag == 0)
1160 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1164 * g_file_read_async:
1165 * @file: input #GFile.
1166 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1168 * @cancellable: optional #GCancellable object, %NULL to ignore.
1169 * @callback: a #GAsyncReadyCallback.
1170 * @user_data: a #gpointer.
1172 * Asynchronously reads @file.
1174 * For the synchronous version of this function, see g_file_read().
1177 g_file_read_async (GFile *file,
1179 GCancellable *cancellable,
1180 GAsyncReadyCallback callback,
1185 g_return_if_fail (G_IS_FILE (file));
1187 iface = G_FILE_GET_IFACE (file);
1188 (* iface->read_async) (file,
1196 * g_file_read_finish:
1197 * @file: input #GFile.
1198 * @res: a #GAsyncResult.
1199 * @error: a #GError, or %NULL
1201 * Finishes an asynchronous file read operation started with
1202 * g_file_read_async().
1204 * If the #GFileIface for @file does not support streaming operations,
1205 * then @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will
1208 * If @cancellable is not %NULL, then the operation can be cancelled by
1209 * triggering the cancellable object from another thread. If the operation
1210 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1212 * Returns: a #GFileInputStream or %NULL on error.
1215 g_file_read_finish (GFile *file,
1221 g_return_val_if_fail (G_IS_FILE (file), NULL);
1222 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1224 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1226 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1227 if (g_simple_async_result_propagate_error (simple, error))
1231 iface = G_FILE_GET_IFACE (file);
1232 return (* iface->read_finish) (file, res, error);
1236 * g_file_append_to_async:
1237 * @file: input #GFile.
1238 * @flags: a set of #GFileCreateFlags.
1239 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1241 * @cancellable: optional #GCancellable object, %NULL to ignore.
1242 * @callback: a #GAsyncReadyCallback.
1243 * @user_data: a #gpointer.
1245 * Readies a file for appending data asynchronously.
1247 * For the synchronous version of this function, see g_file_append_to().
1248 * To finish this operation, see g_file_append_to_finish().
1250 * If @cancellable is not %NULL, then the operation can be cancelled by
1251 * triggering the cancellable object from another thread. If the operation
1252 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set when
1253 * g_file_append_to_finish() is called, and %NULL will be returned.
1256 g_file_append_to_async (GFile *file,
1257 GFileCreateFlags flags,
1259 GCancellable *cancellable,
1260 GAsyncReadyCallback callback,
1265 g_return_if_fail (G_IS_FILE (file));
1267 iface = G_FILE_GET_IFACE (file);
1268 (* iface->append_to_async) (file,
1277 * g_file_append_to_finish:
1278 * @file: input #GFile.
1279 * @res: #GAsyncResult
1280 * @error: a #GError, or %NULL
1282 * Finishes appending to a file. See g_file_append_to_async().
1284 * If @cancellable was not %NULL when g_file_append_to_async() was called,
1285 * then the operation could have been be cancelled by triggering the cancellable
1286 * object from another thread. If the operation was cancelled, the error
1287 * %G_IO_ERROR_CANCELLED will be set in @error, and %NULL will be returned.
1289 * Returns: a valid #GFileOutputStream or %NULL on error.
1292 g_file_append_to_finish (GFile *file,
1298 g_return_val_if_fail (G_IS_FILE (file), NULL);
1299 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1301 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1303 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1304 if (g_simple_async_result_propagate_error (simple, error))
1308 iface = G_FILE_GET_IFACE (file);
1309 return (* iface->append_to_finish) (file, res, error);
1313 * g_file_create_async:
1314 * @file: input #GFile.
1315 * @flags: a set of #GFileCreateFlags.
1316 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1318 * @cancellable: optional #GCancellable object, %NULL to ignore.
1319 * @callback: a #GAsyncReadyCallback.
1320 * @user_data: a #gpointer.
1322 * Creates a new file asynchronously.
1324 * For the synchronous version of this function, see g_file_create().
1325 * To finish this operation, see g_file_create_finish().
1327 * If @cancellable is not %NULL, then the operation can be cancelled by
1328 * triggering the cancellable object from another thread. If the operation
1329 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL
1330 * will be returned by g_file_create_finish().
1333 g_file_create_async (GFile *file,
1334 GFileCreateFlags flags,
1336 GCancellable *cancellable,
1337 GAsyncReadyCallback callback,
1342 g_return_if_fail (G_IS_FILE (file));
1344 iface = G_FILE_GET_IFACE (file);
1345 (* iface->create_async) (file,
1354 * g_file_create_finish:
1355 * @file: input #GFile.
1356 * @res: a #GAsyncResult.
1357 * @error: a #GError, or %NULL
1359 * Finishes creating a file. See g_file_create_async().
1361 * If @cancellable was not %NULL when g_file_create_async() was called,
1362 * then the operation could have been be cancelled by triggering the cancellable
1363 * object from another thread. If the operation was cancelled, the error
1364 * %G_IO_ERROR_CANCELLED will be set in @error, and %NULL will be returned.
1366 * Returns: a #GFileOutputStream or %NULL on error.
1369 g_file_create_finish (GFile *file,
1375 g_return_val_if_fail (G_IS_FILE (file), NULL);
1376 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1378 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1380 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1381 if (g_simple_async_result_propagate_error (simple, error))
1385 iface = G_FILE_GET_IFACE (file);
1386 return (* iface->create_finish) (file, res, error);
1390 * g_file_replace_async:
1391 * @file: input #GFile.
1392 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1394 * @make_backup: a #gboolean.
1395 * @flags: a set of #GFileCreateFlags.
1396 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1398 * @cancellable: optional #GCancellable object, %NULL to ignore.
1399 * @callback: a #GAsyncReadyCallback.
1400 * @user_data: a #gpointer.
1402 * Replaces a file's contents asynchronously. If @make_backup is
1403 * %TRUE, this function will attempt to make a backup of the current file.
1405 * For the synchronous version of this function, see g_file_replace().
1406 * To finish this operation, see g_file_replace_finish().
1408 * If @cancellable is not %NULL, then the operation can be cancelled by
1409 * triggering the cancellable object from another thread. If the operation
1410 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and
1411 * %NULL will be returned in g_file_replace_finish().
1414 g_file_replace_async (GFile *file,
1416 gboolean make_backup,
1417 GFileCreateFlags flags,
1419 GCancellable *cancellable,
1420 GAsyncReadyCallback callback,
1425 g_return_if_fail (G_IS_FILE (file));
1427 iface = G_FILE_GET_IFACE (file);
1428 (* iface->replace_async) (file,
1439 * g_file_replace_finish:
1440 * @file: input #GFile.
1441 * @res: a #GAsyncResult.
1442 * @error: a #GError, or %NULL
1444 * Finishes replacing the contents of the file. See g_file_replace_async().
1446 * If @cancellable was not %NULL when g_file_replace_async() was called,
1447 * then the operation could have been be cancelled by triggering the cancellable
1448 * object from another thread. If the operation was cancelled, the error
1449 * %G_IO_ERROR_CANCELLED will be set in @error, and %NULL will be returned.
1451 * Returns: a #GFileOutputStream, or %NULL if an error has occured.
1454 g_file_replace_finish (GFile *file,
1460 g_return_val_if_fail (G_IS_FILE (file), NULL);
1461 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1463 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1465 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1466 if (g_simple_async_result_propagate_error (simple, error))
1470 iface = G_FILE_GET_IFACE (file);
1471 return (* iface->replace_finish) (file, res, error);
1475 copy_symlink (GFile *destination,
1476 GFileCopyFlags flags,
1477 GCancellable *cancellable,
1482 gboolean tried_delete;
1484 GFileType file_type;
1486 tried_delete = FALSE;
1490 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1492 /* Maybe it already existed, and we want to overwrite? */
1493 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1494 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1496 g_error_free (my_error);
1499 /* Don't overwrite if the destination is a directory */
1500 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STD_TYPE,
1501 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1502 cancellable, &my_error);
1505 file_type = g_file_info_get_file_type (info);
1506 g_object_unref (info);
1508 if (file_type == G_FILE_TYPE_DIRECTORY)
1510 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1511 _("Can't copy over directory"));
1516 if (!g_file_delete (destination, cancellable, error))
1519 tried_delete = TRUE;
1523 g_propagate_error (error, my_error);
1530 static GInputStream *
1531 open_source_for_copy (GFile *source,
1533 GFileCopyFlags flags,
1534 GCancellable *cancellable,
1540 GFileType file_type;
1543 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1547 /* There was an error opening the source, try to set a good error for it: */
1549 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1551 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1552 * as that is less useful to the app. Better check for errors on the
1555 g_error_free (my_error);
1558 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STD_TYPE,
1559 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1560 cancellable, &my_error);
1563 file_type = g_file_info_get_file_type (info);
1564 g_object_unref (info);
1566 if (flags & G_FILE_COPY_OVERWRITE)
1568 if (file_type == G_FILE_TYPE_DIRECTORY)
1570 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1571 _("Can't copy directory over directory"));
1574 /* continue to would_recurse error */
1578 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1579 _("Target file exists"));
1585 /* Error getting info from target, return that error
1586 * (except for NOT_FOUND, which is no error here)
1588 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1590 g_propagate_error (error, my_error);
1593 g_error_free (my_error);
1596 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1597 _("Can't recursively copy directory"));
1601 g_propagate_error (error, my_error);
1606 should_copy (GFileAttributeInfo *info,
1610 return info->flags & G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED;
1611 return info->flags & G_FILE_ATTRIBUTE_FLAGS_COPY_WITH_FILE;
1615 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1616 GFileAttributeInfoList *namespaces,
1624 s = g_string_new ("");
1628 for (i = 0; i < attributes->n_infos; i++)
1630 if (should_copy (&attributes->infos[i], as_move))
1635 g_string_append_c (s, ',');
1637 g_string_append (s, attributes->infos[i].name);
1644 for (i = 0; i < namespaces->n_infos; i++)
1646 if (should_copy (&namespaces->infos[i], as_move))
1651 g_string_append_c (s, ',');
1653 g_string_append (s, namespaces->infos[i].name);
1654 g_string_append (s, ":*");
1659 return g_string_free (s, FALSE);
1663 g_file_copy_attributes (GFile *source,
1665 GFileCopyFlags flags,
1666 GCancellable *cancellable,
1669 GFileAttributeInfoList *attributes, *namespaces;
1670 char *attrs_to_read;
1674 gboolean source_nofollow_symlinks;
1676 as_move = flags & G_FILE_COPY_ALL_METADATA;
1677 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1679 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1680 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1681 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1683 if (attributes == NULL && namespaces == NULL)
1686 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1688 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1689 * we just don't copy it.
1691 info = g_file_query_info (source, attrs_to_read,
1692 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1696 g_free (attrs_to_read);
1701 res = g_file_set_attributes_from_info (destination,
1705 g_object_unref (info);
1708 g_file_attribute_info_list_unref (attributes);
1709 g_file_attribute_info_list_unref (namespaces);
1714 /* Closes the streams */
1716 copy_stream_with_progress (GInputStream *in,
1718 GCancellable *cancellable,
1719 GFileProgressCallback progress_callback,
1720 gpointer progress_callback_data,
1723 gssize n_read, n_written;
1724 goffset current_size;
1725 char buffer[8192], *p;
1731 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1732 G_FILE_ATTRIBUTE_STD_SIZE,
1736 total_size = g_file_info_get_size (info);
1737 g_object_unref (info);
1744 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
1754 current_size += n_read;
1759 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
1760 if (n_written == -1)
1767 n_read -= n_written;
1773 if (progress_callback)
1774 progress_callback (current_size, total_size, progress_callback_data);
1778 error = NULL; /* Ignore further errors */
1780 /* Make sure we send full copied size */
1781 if (progress_callback)
1782 progress_callback (current_size, total_size, progress_callback_data);
1785 /* Don't care about errors in source here */
1786 g_input_stream_close (in, cancellable, NULL);
1788 /* But write errors on close are bad! */
1789 if (!g_output_stream_close (out, cancellable, error))
1792 g_object_unref (in);
1793 g_object_unref (out);
1799 file_copy_fallback (GFile *source,
1801 GFileCopyFlags flags,
1802 GCancellable *cancellable,
1803 GFileProgressCallback progress_callback,
1804 gpointer progress_callback_data,
1812 /* Maybe copy the symlink? */
1813 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
1815 info = g_file_query_info (source,
1816 G_FILE_ATTRIBUTE_STD_TYPE "," G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET,
1817 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1823 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
1824 (target = g_file_info_get_symlink_target (info)) != NULL)
1826 if (!copy_symlink (destination, flags, cancellable, target, error))
1828 g_object_unref (info);
1832 g_object_unref (info);
1836 g_object_unref (info);
1839 in = open_source_for_copy (source, destination, flags, cancellable, error);
1843 if (flags & G_FILE_COPY_OVERWRITE)
1845 out = (GOutputStream *)g_file_replace (destination,
1847 flags & G_FILE_COPY_BACKUP,
1848 cancellable, error);
1852 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
1857 g_object_unref (in);
1861 if (!copy_stream_with_progress (in, out, cancellable,
1862 progress_callback, progress_callback_data,
1868 /* Ignore errors here. Failure to copy metadata is not a hard error */
1869 g_file_copy_attributes (source, destination,
1870 flags, cancellable, NULL);
1877 * @source: input #GFile.
1878 * @destination: destination #GFile
1879 * @flags: set of #GFileCopyFlags
1880 * @cancellable: optional #GCancellable object, %NULL to ignore.
1881 * @progress_callback: function to callback with progress information
1882 * @progress_callback_data: userdata to pass to @progress_callback
1883 * @error: #GError to set on error, or %NULL
1885 * <!-- Source Friendly Version
1886 * List of possible errors resulting from g_file_copy():
1887 * source dest flags res
1888 * - * * G_IO_ERROR_NOT_FOUND
1890 * file * 0 G_IO_ERROR_EXISTS
1891 * file file overwr ok
1892 * file dir overwr G_IO_ERROR_IS_DIRECTORY
1894 * dir - * G_IO_ERROR_WOULD_RECURSE
1895 * dir * 0 G_IO_ERROR_EXISTS
1896 * dir dir overwr G_IO_ERROR_WOULD_MERGE
1897 * dir file overwr G_IO_ERROR_WOULD_RECURSE
1898 * Docbook version below -->
1900 * Copies a file or directory from @source to @destination, with the given
1901 * @flags. This operation may fail, and @error will be set appropriately with
1902 * the given error result (see the following table).
1903 * File copies are always asynchronous.
1905 * If @cancellable is not %NULL, then the operation can be cancelled by
1906 * triggering the cancellable object from another thread. If the operation
1907 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1909 * If @progress_callback is not %NULL, then the operation can be monitored by
1910 * setting this to a #GFileProgressCallback function. @progress_callback_data
1911 * will be passed to this function.
1914 * <title>g_file_copy() Error Conditions</title>
1915 * <tgroup cols='4' align='left'><thead>
1916 * <row><entry>Source</entry><entry>Destination</entry><entry>Flags</entry><entry>Results in</entry></row>
1918 * <row><entry>%NULL</entry><entry>Anything</entry><entry>Anything</entry><entry>%G_IO_ERROR_NOT_FOUND</entry></row>
1919 * <row><entry>File</entry><entry>%NULL</entry><entry>Anything</entry><entry>No Error</entry></row>
1920 * <row><entry>File</entry><entry>Anything</entry><entry>0</entry><entry>%G_IO_ERROR_EXISTS</entry></row>
1921 * <row><entry>File</entry><entry>File</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>No Error</entry></row>
1922 * <row><entry>File</entry><entry>Directory</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>%G_IO_ERROR_IS_DIRECTORY</entry></row>
1923 * <row><entry>Directory</entry><entry>%NULL</entry><entry>Anything</entry><entry>%G_IO_ERROR_WOULD_RECURSE</entry></row>
1924 * <row><entry>Directory</entry><entry>Anything</entry><entry>0</entry><entry>%G_IO_ERROR_EXISTS</entry></row>
1925 * <row><entry>Directory</entry><entry>Directory</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>%G_IO_ERROR_IS_DIRECTORY</entry></row>
1926 * <row><entry>Directory</entry><entry>File</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>%G_IO_ERROR_WOULD_RECURSE</entry></row>
1931 * Returns: %TRUE on success, %FALSE otherwise.
1934 g_file_copy (GFile *source,
1936 GFileCopyFlags flags,
1937 GCancellable *cancellable,
1938 GFileProgressCallback progress_callback,
1939 gpointer progress_callback_data,
1946 g_return_val_if_fail (G_IS_FILE (source), FALSE);
1947 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
1949 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1952 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
1954 iface = G_FILE_GET_IFACE (source);
1959 res = (* iface->copy) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
1964 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
1966 g_propagate_error (error, my_error);
1972 return file_copy_fallback (source, destination, flags, cancellable,
1973 progress_callback, progress_callback_data,
1980 * @source: #GFile pointing to the source location.
1981 * @destination: #GFile pointing to the destination location.
1982 * @flags: set of #GFileCopyFlags.
1983 * @cancellable: optional #GCancellable object, %NULL to ignore.
1984 * @progress_callback: #GFileProgressCallback function for updates.
1985 * @progress_callback_data: gpointer to user data for the callback function.
1986 * @error: #GError for returning error conditions, or %NULL
1988 * <!-- Source version
1989 * source dest flags results in
1990 * - * * G_IO_ERROR_NOT_FOUND
1992 * file * 0 G_IO_ERROR_EXISTS
1993 * file file overwr ok
1994 * file dir overwr G_IO_ERROR_IS_DIRECTORY
1996 * dir - * ok || G_IO_ERROR_WOULD_RECURSE
1997 * dir * 0 G_IO_ERROR_EXISTS
1998 * dir dir overwr G_IO_ERROR_WOULD_MERGE
1999 * dir file overwr ok || G_IO_ERROR_WOULD_RECURSE
2000 * Docbook version below -->
2002 * Moves a file or directory from @source to @destination, with the given
2003 * @flags. This operation may fail, and @error will be set appropriately with
2004 * the given error result (see the following table).
2005 * File moves are always asynchronous.
2007 * If @cancellable is not %NULL, then the operation can be cancelled by
2008 * triggering the cancellable object from another thread. If the operation
2009 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2011 * If @progress_callback is not %NULL, then the operation can be monitored by
2012 * setting this to a #GFileProgressCallback function. @progress_callback_data
2013 * will be passed to this function.
2016 * <title>g_file_move() Error Conditions</title>
2017 * <tgroup cols='4' align='left'><thead>
2018 * <row><entry>Source</entry><entry>Destination</entry>
2019 * <entry>Flags</entry><entry>Results in</entry></row>
2021 * <row><entry> %NULL </entry><entry> Anything </entry>
2022 * <entry> Anything </entry><entry> %G_IO_ERROR_NOT_FOUND </entry></row>
2023 * <row><entry> File </entry><entry> %NULL </entry>
2024 * <entry> Anything </entry><entry> No Error </entry></row>
2025 * <row><entry> File </entry><entry> Anything </entry>
2026 * <entry> 0 </entry><entry> %G_IO_ERROR_EXISTS </entry></row>
2027 * <row><entry> File </entry><entry> File </entry>
2028 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> No Error </entry></row>
2029 * <row><entry> File </entry><entry> Directory </entry>
2030 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> %G_IO_ERROR_IS_DIRECTORY </entry></row>
2031 * <row><entry> Directory </entry><entry> %NULL </entry>
2032 * <entry> Anything </entry><entry> No Error or %G_IO_ERROR_WOULD_RECURSE </entry></row>
2033 * <row><entry> Directory </entry><entry> Anything </entry>
2034 * <entry> 0 </entry><entry> %G_IO_ERROR_EXISTS </entry></row>
2035 * <row><entry> Directory </entry><entry> Directory </entry>
2036 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> %G_IO_ERROR_IS_DIRECTORY </entry></row>
2037 * <row><entry> Directory </entry><entry> File </entry>
2038 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> No Error or %G_IO_ERROR_WOULD_RECURSE </entry></row>
2043 * Returns: %TRUE on successful move, %FALSE otherwise.
2046 g_file_move (GFile *source,
2048 GFileCopyFlags flags,
2049 GCancellable *cancellable,
2050 GFileProgressCallback progress_callback,
2051 gpointer progress_callback_data,
2058 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2059 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2061 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2064 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
2066 iface = G_FILE_GET_IFACE (source);
2071 res = (* iface->move) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
2076 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2078 g_propagate_error (error, my_error);
2084 flags |= G_FILE_COPY_ALL_METADATA;
2085 if (!g_file_copy (source, destination, flags, cancellable,
2086 progress_callback, progress_callback_data,
2090 return g_file_delete (source, cancellable, error);
2094 * g_file_make_directory
2095 * @file: input #GFile.
2096 * @cancellable: optional #GCancellable object, %NULL to ignore.
2097 * @error: a #GError, or %NULL
2099 * Creates a directory.
2101 * If @cancellable is not %NULL, then the operation can be cancelled by
2102 * triggering the cancellable object from another thread. If the operation
2103 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2105 * Returns: %TRUE on successful creation, %FALSE otherwise.
2108 g_file_make_directory (GFile *file,
2109 GCancellable *cancellable,
2114 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2116 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2119 iface = G_FILE_GET_IFACE (file);
2121 if (iface->make_directory == NULL)
2123 g_set_error (error, G_IO_ERROR,
2124 G_IO_ERROR_NOT_SUPPORTED,
2125 _("Operation not supported"));
2129 return (* iface->make_directory) (file, cancellable, error);
2133 * g_file_make_symbolic_link:
2134 * @file: input #GFile.
2135 * @symlink_value: a string with the name of the new symlink.
2136 * @cancellable: optional #GCancellable object, %NULL to ignore.
2137 * @error: a #GError.
2139 * Creates a symbolic link.
2141 * If @cancellable is not %NULL, then the operation can be cancelled by
2142 * triggering the cancellable object from another thread. If the operation
2143 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2145 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2148 g_file_make_symbolic_link (GFile *file,
2149 const char *symlink_value,
2150 GCancellable *cancellable,
2155 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2156 g_return_val_if_fail (symlink_value != NULL, FALSE);
2158 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2161 if (*symlink_value == '\0')
2163 g_set_error (error, G_IO_ERROR,
2164 G_IO_ERROR_INVALID_ARGUMENT,
2165 _("Invalid symlink value given"));
2169 iface = G_FILE_GET_IFACE (file);
2171 if (iface->make_symbolic_link == NULL)
2173 g_set_error (error, G_IO_ERROR,
2174 G_IO_ERROR_NOT_SUPPORTED,
2175 _("Operation not supported"));
2179 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2184 * @file: input #GFile.
2185 * @cancellable: optional #GCancellable object, %NULL to ignore.
2186 * @error: a #GError, or %NULL
2190 * If @cancellable is not %NULL, then the operation can be cancelled by
2191 * triggering the cancellable object from another thread. If the operation
2192 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2194 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2197 g_file_delete (GFile *file,
2198 GCancellable *cancellable,
2203 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2205 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2208 iface = G_FILE_GET_IFACE (file);
2210 if (iface->delete_file == NULL)
2212 g_set_error (error, G_IO_ERROR,
2213 G_IO_ERROR_NOT_SUPPORTED,
2214 _("Operation not supported"));
2218 return (* iface->delete_file) (file, cancellable, error);
2223 * @file: #GFile to send to trash.
2224 * @cancellable: optional #GCancellable object, %NULL to ignore.
2225 * @error: a #GError, or %NULL
2227 * Sends @file to the virtual file system "Trash" location. If the
2228 * virtual file system does not have support having a "Trash" location,
2229 * %FALSE will be returned, and @error will be set to
2230 * %G_IO_ERROR_NOT_SUPPORTED.
2232 * If @cancellable is not %NULL, then the operation can be cancelled by
2233 * triggering the cancellable object from another thread. If the operation
2234 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2236 * Returns: %TRUE on successful trash, %FALSE otherwise.
2239 g_file_trash (GFile *file,
2240 GCancellable *cancellable,
2245 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2247 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2250 iface = G_FILE_GET_IFACE (file);
2252 if (iface->trash == NULL)
2255 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2256 _("Trash not supported"));
2260 return (* iface->trash) (file, cancellable, error);
2264 * g_file_set_display_name:
2265 * @file: input #GFile.
2266 * @display_name: a string.
2267 * @cancellable: optional #GCancellable object, %NULL to ignore.
2268 * @error: a #GError, or %NULL
2270 * Sets the display name for @file. If the display name contains invalid
2271 * characters, @error will be set to %G_IO_ERROR_INVALID_ARGUMENT. For the
2272 * asynchronous version of this function, see g_file_set_display_name_async().
2274 * If @cancellable is not %NULL, then the operation can be cancelled by
2275 * triggering the cancellable object from another thread. If the operation
2276 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2278 * Returns: a #GFile, or %NULL if there was an error.
2281 g_file_set_display_name (GFile *file,
2282 const char *display_name,
2283 GCancellable *cancellable,
2288 g_return_val_if_fail (G_IS_FILE (file), NULL);
2289 g_return_val_if_fail (display_name != NULL, NULL);
2291 if (strchr (display_name, '/') != NULL)
2295 G_IO_ERROR_INVALID_ARGUMENT,
2296 _("File names cannot contain '/'"));
2300 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2303 iface = G_FILE_GET_IFACE (file);
2305 return (* iface->set_display_name) (file, display_name, cancellable, error);
2309 * g_file_set_display_name_async:
2310 * @file: input #GFile.
2311 * @display_name: a string.
2312 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2314 * @cancellable: optional #GCancellable object, %NULL to ignore.
2315 * @callback: a #GAsyncReadyCallback.
2316 * @user_data: a #gpointer.
2318 * Asynchronously sets the display name for a given #GFile.
2319 * For the synchronous version of this function, see g_file_set_display_name().
2321 * If @cancellable is not %NULL, then the operation can be cancelled by
2322 * triggering the cancellable object from another thread. If the operation
2323 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2326 g_file_set_display_name_async (GFile *file,
2327 const char *display_name,
2329 GCancellable *cancellable,
2330 GAsyncReadyCallback callback,
2335 g_return_if_fail (G_IS_FILE (file));
2336 g_return_if_fail (display_name != NULL);
2338 iface = G_FILE_GET_IFACE (file);
2339 (* iface->set_display_name_async) (file,
2348 * g_file_set_display_name_finish:
2349 * @file: input #GFile.
2350 * @res: a #GAsyncResult.
2351 * @error: a #GError, or %NULL
2353 * Finishes setting a display name started with
2354 * g_file_set_display_name_async().
2356 * Returns: a #GFile or %NULL on error.
2359 g_file_set_display_name_finish (GFile *file,
2365 g_return_val_if_fail (G_IS_FILE (file), NULL);
2366 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2368 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2370 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2371 if (g_simple_async_result_propagate_error (simple, error))
2375 iface = G_FILE_GET_IFACE (file);
2376 return (* iface->set_display_name_finish) (file, res, error);
2380 * g_file_query_settable_attributes:
2381 * @file: input #GFile.
2382 * @cancellable: optional #GCancellable object, %NULL to ignore.
2383 * @error: a #GError, or %NULL
2385 * Obtain the list of settable attributes for the file.
2387 * If @cancellable is not %NULL, then the operation can be cancelled by
2388 * triggering the cancellable object from another thread. If the operation
2389 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2391 * Returns: the type and full attribute name of all the attributes
2392 * that the file can set. This doesn't mean setting it will always
2393 * succeed though, you might get an access failure, or some specific
2394 * file may not support a specific attribute.
2396 GFileAttributeInfoList *
2397 g_file_query_settable_attributes (GFile *file,
2398 GCancellable *cancellable,
2403 GFileAttributeInfoList *list;
2405 g_return_val_if_fail (G_IS_FILE (file), NULL);
2407 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2410 iface = G_FILE_GET_IFACE (file);
2412 if (iface->query_settable_attributes == NULL)
2413 return g_file_attribute_info_list_new ();
2416 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2420 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2422 list = g_file_attribute_info_list_new ();
2423 g_error_free (my_error);
2426 g_propagate_error (error, my_error);
2433 * g_file_query_writable_namespaces:
2434 * @file: input #GFile.
2435 * @cancellable: optional #GCancellable object, %NULL to ignore.
2436 * @error: a #GError, or %NULL
2438 * Obtain the list of attribute namespaces where new attributes
2441 * If @cancellable is not %NULL, then the operation can be cancelled by
2442 * triggering the cancellable object from another thread. If the operation
2443 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2445 * Returns: a #GFileAttributeInfoList of attribute namespaces
2446 * where the user can create their own attribute names, such
2447 * as extended attributes.
2449 GFileAttributeInfoList *
2450 g_file_query_writable_namespaces (GFile *file,
2451 GCancellable *cancellable,
2456 GFileAttributeInfoList *list;
2458 g_return_val_if_fail (G_IS_FILE (file), NULL);
2460 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2463 iface = G_FILE_GET_IFACE (file);
2465 if (iface->query_writable_namespaces == NULL)
2466 return g_file_attribute_info_list_new ();
2469 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2473 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2475 list = g_file_attribute_info_list_new ();
2476 g_error_free (my_error);
2479 g_propagate_error (error, my_error);
2486 * g_file_set_attribute:
2487 * @file: input #GFile.
2488 * @attribute: a string containing the attribute's name.
2489 * @value: a set of #GFileAttributeValue.
2490 * @flags: a set of #GFileQueryInfoFlags.
2491 * @cancellable: optional #GCancellable object, %NULL to ignore.
2492 * @error: a #GError, or %NULL
2494 * Sets an attribute in the file with attribute name @attribute to @value.
2495 * If setting attributes is not suppored by the #GFileIface for @file,
2496 * then @error will be set to %G_IO_ERROR_NOT_SUPPORTED.
2498 * If @cancellable is not %NULL, then the operation can be cancelled by
2499 * triggering the cancellable object from another thread. If the operation
2500 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2502 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2505 g_file_set_attribute (GFile *file,
2506 const char *attribute,
2507 const GFileAttributeValue *value,
2508 GFileQueryInfoFlags flags,
2509 GCancellable *cancellable,
2514 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2515 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2517 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2520 iface = G_FILE_GET_IFACE (file);
2522 if (iface->set_attribute == NULL)
2524 g_set_error (error, G_IO_ERROR,
2525 G_IO_ERROR_NOT_SUPPORTED,
2526 _("Operation not supported"));
2530 return (* iface->set_attribute) (file, attribute, value, flags, cancellable, error);
2534 * g_file_set_attributes_from_info:
2535 * @file: input #GFile.
2536 * @info: a #GFileInfo.
2537 * @flags: #GFileQueryInfoFlags
2538 * @cancellable: optional #GCancellable object, %NULL to ignore.
2539 * @error: a #GError, or %NULL
2541 * Tries to set all attributes in the #GFileInfo on the target values,
2542 * not stopping on the first error.
2544 * If @cancellable is not %NULL, then the operation can be cancelled by
2545 * triggering the cancellable object from another thread. If the operation
2546 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2548 * Returns: %TRUE if there was any error, and @error will be set to
2549 * the first error. Error on particular fields are flagged by setting
2550 * the "status" field in the attribute value to
2551 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING.
2554 g_file_set_attributes_from_info (GFile *file,
2556 GFileQueryInfoFlags flags,
2557 GCancellable *cancellable,
2562 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2563 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2565 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2568 g_file_info_clear_status (info);
2570 iface = G_FILE_GET_IFACE (file);
2572 return (* iface->set_attributes_from_info) (file,
2581 g_file_real_set_attributes_from_info (GFile *file,
2583 GFileQueryInfoFlags flags,
2584 GCancellable *cancellable,
2590 GFileAttributeValue *value;
2594 attributes = g_file_info_list_attributes (info, NULL);
2596 for (i = 0; attributes[i] != NULL; i++)
2598 value = (GFileAttributeValue *)g_file_info_get_attribute (info, attributes[i]);
2600 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2603 if (!g_file_set_attribute (file, attributes[i], value, flags, cancellable, error))
2605 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2607 /* Don't set error multiple times */
2611 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2614 g_strfreev (attributes);
2620 * g_file_set_attributes_async:
2621 * @file: input #GFile.
2622 * @info: a #GFileInfo.
2623 * @flags: a #GFileQueryInfoFlags.
2624 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2626 * @cancellable: optional #GCancellable object, %NULL to ignore.
2627 * @callback: a #GAsyncReadyCallback.
2628 * @user_data: a #gpointer.
2630 * Asynchronously sets the attributes of @file with @info.
2631 * For the synchronous version of this function, see g_file_set_attributes().
2633 * If @cancellable is not %NULL, then the operation can be cancelled by
2634 * triggering the cancellable object from another thread. If the operation
2635 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2638 g_file_set_attributes_async (GFile *file,
2640 GFileQueryInfoFlags flags,
2642 GCancellable *cancellable,
2643 GAsyncReadyCallback callback,
2648 g_return_if_fail (G_IS_FILE (file));
2649 g_return_if_fail (G_IS_FILE_INFO (info));
2651 iface = G_FILE_GET_IFACE (file);
2652 (* iface->set_attributes_async) (file,
2662 * g_file_set_attributes_finish:
2663 * @file: input #GFile.
2664 * @result: a #GAsyncResult.
2665 * @info: a #GFileInfo.
2666 * @error: a #GError, or %NULL
2668 * Finishes setting an attribute started in g_file_set_attributes_async().
2670 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2673 g_file_set_attributes_finish (GFile *file,
2674 GAsyncResult *result,
2680 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2681 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2683 /* No standard handling of errors here, as we must set info even
2686 iface = G_FILE_GET_IFACE (file);
2687 return (* iface->set_attributes_finish) (file, result, info, error);
2691 * g_file_set_attribute_string:
2692 * @file: input #GFile.
2693 * @attribute: a string containing the attribute's name.
2694 * @value: a string containing the attribute's value.
2695 * @flags: #GFileQueryInfoFlags.
2696 * @cancellable: optional #GCancellable object, %NULL to ignore.
2697 * @error: a #GError, or %NULL
2699 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
2700 * If @attribute is of a different type, this operation will fail.
2702 * If @cancellable is not %NULL, then the operation can be cancelled by
2703 * triggering the cancellable object from another thread. If the operation
2704 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2706 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2709 g_file_set_attribute_string (GFile *file,
2710 const char *attribute,
2712 GFileQueryInfoFlags flags,
2713 GCancellable *cancellable,
2716 GFileAttributeValue v;
2718 v.type = G_FILE_ATTRIBUTE_TYPE_STRING;
2719 v.u.string = (char *)value;
2720 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2724 * g_file_set_attribute_byte_string:
2725 * @file: input #GFile.
2726 * @attribute: a string containing the attribute's name.
2727 * @value: a string containing the attribute's new value.
2728 * @flags: a #GFileQueryInfoFlags.
2729 * @cancellable: optional #GCancellable object, %NULL to ignore.
2730 * @error: a #GError, or %NULL
2732 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
2733 * If @attribute is of a different type, this operation will fail,
2736 * If @cancellable is not %NULL, then the operation can be cancelled by
2737 * triggering the cancellable object from another thread. If the operation
2738 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2740 * Returns: %TRUE if the @attribute was successfully set to @value
2741 * in the @file, %FALSE otherwise.
2744 g_file_set_attribute_byte_string (GFile *file,
2745 const char *attribute,
2747 GFileQueryInfoFlags flags,
2748 GCancellable *cancellable,
2751 GFileAttributeValue v;
2753 v.type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
2754 v.u.string = (char *)value;
2755 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2759 * g_file_set_attribute_uint32:
2760 * @file: input #GFile.
2761 * @attribute: a string containing the attribute's name.
2762 * @value: a #guint32 containing the attribute's new value.
2763 * @flags: a #GFileQueryInfoFlags.
2764 * @cancellable: optional #GCancellable object, %NULL to ignore.
2765 * @error: a #GError, or %NULL
2767 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
2768 * If @attribute is of a different type, this operation will fail.
2770 * If @cancellable is not %NULL, then the operation can be cancelled by
2771 * triggering the cancellable object from another thread. If the operation
2772 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2774 * Returns: %TRUE if the @attribute was successfully set to @value
2775 * in the @file, %FALSE otherwise.
2778 g_file_set_attribute_uint32 (GFile *file,
2779 const char *attribute,
2781 GFileQueryInfoFlags flags,
2782 GCancellable *cancellable,
2785 GFileAttributeValue v;
2787 v.type = G_FILE_ATTRIBUTE_TYPE_UINT32;
2789 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2793 * g_file_set_attribute_int32:
2794 * @file: input #GFile.
2795 * @attribute: a string containing the attribute's name.
2796 * @value: a #gint32 containing the attribute's new value.
2797 * @flags: a #GFileQueryInfoFlags.
2798 * @cancellable: optional #GCancellable object, %NULL to ignore.
2799 * @error: a #GError, or %NULL
2801 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
2802 * If @attribute is of a different type, this operation will fail.
2804 * If @cancellable is not %NULL, then the operation can be cancelled by
2805 * triggering the cancellable object from another thread. If the operation
2806 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2808 * Returns: %TRUE if the @attribute was successfully set to @value
2809 * in the @file, %FALSE otherwise.
2812 g_file_set_attribute_int32 (GFile *file,
2813 const char *attribute,
2815 GFileQueryInfoFlags flags,
2816 GCancellable *cancellable,
2819 GFileAttributeValue v;
2821 v.type = G_FILE_ATTRIBUTE_TYPE_INT32;
2823 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2827 * g_file_set_attribute_uint64:
2828 * @file: input #GFile.
2829 * @attribute: a string containing the attribute's name.
2830 * @value: a #guint64 containing the attribute's new value.
2831 * @flags: a #GFileQueryInfoFlags.
2832 * @cancellable: optional #GCancellable object, %NULL to ignore.
2833 * @error: a #GError, or %NULL
2835 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
2836 * If @attribute is of a different type, this operation will fail.
2838 * If @cancellable is not %NULL, then the operation can be cancelled by
2839 * triggering the cancellable object from another thread. If the operation
2840 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2842 * Returns: %TRUE if the @attribute was successfully set to @value
2843 * in the @file, %FALSE otherwise.
2846 g_file_set_attribute_uint64 (GFile *file,
2847 const char *attribute,
2849 GFileQueryInfoFlags flags,
2850 GCancellable *cancellable,
2853 GFileAttributeValue v;
2855 v.type = G_FILE_ATTRIBUTE_TYPE_UINT64;
2857 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2861 * g_file_set_attribute_int64:
2862 * @file: input #GFile.
2863 * @attribute: a string containing the attribute's name.
2864 * @value: a #guint64 containing the attribute's new value.
2865 * @flags: a #GFileQueryInfoFlags.
2866 * @cancellable: optional #GCancellable object, %NULL to ignore.
2867 * @error: a #GError, or %NULL
2869 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
2870 * If @attribute is of a different type, this operation will fail.
2872 * If @cancellable is not %NULL, then the operation can be cancelled by
2873 * triggering the cancellable object from another thread. If the operation
2874 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2876 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2879 g_file_set_attribute_int64 (GFile *file,
2880 const char *attribute,
2882 GFileQueryInfoFlags flags,
2883 GCancellable *cancellable,
2886 GFileAttributeValue v;
2888 v.type = G_FILE_ATTRIBUTE_TYPE_INT64;
2890 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2894 * g_file_mount_mountable:
2895 * @file: input #GFile.
2896 * @mount_operation: a #GMountOperation.
2897 * @cancellable: optional #GCancellable object, %NULL to ignore.
2898 * @callback: a #GAsyncReadyCallback.
2899 * @user_data: a #gpointer.
2901 * Mounts a mountable file using @mount_operation, if possible.
2903 * If @cancellable is not %NULL, then the operation can be cancelled by
2904 * triggering the cancellable object from another thread. If the operation
2905 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2908 g_file_mount_mountable (GFile *file,
2909 GMountOperation *mount_operation,
2910 GCancellable *cancellable,
2911 GAsyncReadyCallback callback,
2916 g_return_if_fail (G_IS_FILE (file));
2917 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
2919 iface = G_FILE_GET_IFACE (file);
2921 if (iface->mount_mountable == NULL)
2922 g_simple_async_report_error_in_idle (G_OBJECT (file),
2926 G_IO_ERROR_NOT_SUPPORTED,
2927 _("Operation not supported"));
2929 (* iface->mount_mountable) (file,
2937 * g_file_mount_mountable_finish:
2938 * @file: input #GFile.
2939 * @result: a #GAsyncResult.
2940 * @error: a #GError, or %NULL
2942 * Finishes a mount operation. See g_file_mount_mountable() for details.
2944 * Finish an asynchronous mount operation that was started
2945 * with g_file_mount_mountable().
2947 * Returns: a #GFile or %NULL on error.
2950 g_file_mount_mountable_finish (GFile *file,
2951 GAsyncResult *result,
2956 g_return_val_if_fail (G_IS_FILE (file), NULL);
2957 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
2959 if (G_IS_SIMPLE_ASYNC_RESULT (result))
2961 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
2962 if (g_simple_async_result_propagate_error (simple, error))
2966 iface = G_FILE_GET_IFACE (file);
2967 return (* iface->mount_mountable_finish) (file, result, error);
2971 * g_file_unmount_mountable:
2972 * @file: input #GFile.
2973 * @cancellable: optional #GCancellable object, %NULL to ignore.
2974 * @callback: a #GAsyncReadyCallback.
2975 * @user_data: a #gpointer.
2977 * Starts an asynchronous unmount operation.
2979 * Unmounts a mounted file.
2981 * If @cancellable is not %NULL, then the operation can be cancelled by
2982 * triggering the cancellable object from another thread. If the operation
2983 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2986 g_file_unmount_mountable (GFile *file,
2987 GCancellable *cancellable,
2988 GAsyncReadyCallback callback,
2993 g_return_if_fail (G_IS_FILE (file));
2995 iface = G_FILE_GET_IFACE (file);
2997 if (iface->unmount_mountable == NULL)
2998 g_simple_async_report_error_in_idle (G_OBJECT (file),
3002 G_IO_ERROR_NOT_SUPPORTED,
3003 _("Operation not supported"));
3005 (* iface->unmount_mountable) (file,
3012 * g_file_unmount_mountable_finish:
3013 * @file: input #GFile.
3014 * @result: a #GAsyncResult.
3015 * @error: a #GError, or %NULL
3017 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3019 * Finish an asynchronous unmount operation that was started
3020 * with g_file_unmount_mountable().
3022 * Returns: %TRUE if the operation finished successfully. %FALSE
3026 g_file_unmount_mountable_finish (GFile *file,
3027 GAsyncResult *result,
3032 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3033 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3035 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3037 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3038 if (g_simple_async_result_propagate_error (simple, error))
3042 iface = G_FILE_GET_IFACE (file);
3043 return (* iface->unmount_mountable_finish) (file, result, error);
3047 * g_file_eject_mountable:
3048 * @file: input #GFile.
3049 * @cancellable: optional #GCancellable object, %NULL to ignore.
3050 * @callback: a #GAsyncReadyCallback.
3051 * @user_data: a #gpointer.
3053 * Starts an asynchronous eject on a mountable.
3054 * When this operation has completed, @callback will be called with
3055 * @user_user data, and the operation can be finalized with
3056 * g_file_eject_mountable_finish().
3058 * If @cancellable is not %NULL, then the operation can be cancelled by
3059 * triggering the cancellable object from another thread. If the operation
3060 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3063 g_file_eject_mountable (GFile *file,
3064 GCancellable *cancellable,
3065 GAsyncReadyCallback callback,
3070 g_return_if_fail (G_IS_FILE (file));
3072 iface = G_FILE_GET_IFACE (file);
3074 if (iface->eject_mountable == NULL)
3075 g_simple_async_report_error_in_idle (G_OBJECT (file),
3079 G_IO_ERROR_NOT_SUPPORTED,
3080 _("Operation not supported"));
3082 (* iface->eject_mountable) (file,
3089 * g_file_eject_mountable_finish:
3090 * @file: input #GFile.
3091 * @result: a #GAsyncResult.
3092 * @error: a #GError, or %NULL
3094 * Finishes an asynchronous eject operation started by
3095 * g_file_eject_mountable().
3097 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3101 g_file_eject_mountable_finish (GFile *file,
3102 GAsyncResult *result,
3107 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3108 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3110 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3112 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3113 if (g_simple_async_result_propagate_error (simple, error))
3117 iface = G_FILE_GET_IFACE (file);
3118 return (* iface->eject_mountable_finish) (file, result, error);
3122 * g_file_monitor_directory:
3123 * @file: input #GFile.
3124 * @flags: a set of #GFileMonitorFlags.
3125 * @cancellable: optional #GCancellable object, %NULL to ignore.
3127 * Obtains a directory monitor for the given file.
3129 * If @cancellable is not %NULL, then the operation can be cancelled by
3130 * triggering the cancellable object from another thread. If the operation
3131 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3133 * Returns: a #GDirectoryMonitor for the given @file,
3134 * or %NULL on error.
3137 g_file_monitor_directory (GFile *file,
3138 GFileMonitorFlags flags,
3139 GCancellable *cancellable)
3143 g_return_val_if_fail (G_IS_FILE (file), NULL);
3145 iface = G_FILE_GET_IFACE (file);
3147 if (iface->monitor_dir == NULL)
3150 return (* iface->monitor_dir) (file, flags, cancellable);
3154 * g_file_monitor_file:
3155 * @file: input #GFile.
3156 * @flags: a set of #GFileMonitorFlags.
3157 * @cancellable: optional #GCancellable object, %NULL to ignore.
3159 * Obtains a file monitor for the given file.
3161 * If @cancellable is not %NULL, then the operation can be cancelled by
3162 * triggering the cancellable object from another thread. If the operation
3163 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3165 * Returns: a #GFileMonitor for the given @file,
3166 * or %NULL on error.
3169 g_file_monitor_file (GFile *file,
3170 GFileMonitorFlags flags,
3171 GCancellable *cancellable)
3174 GFileMonitor *monitor;
3176 g_return_val_if_fail (G_IS_FILE (file), NULL);
3178 iface = G_FILE_GET_IFACE (file);
3182 if (iface->monitor_file)
3183 monitor = (* iface->monitor_file) (file, flags, cancellable);
3185 /* Fallback to polling */
3186 if (monitor == NULL)
3187 monitor = _g_poll_file_monitor_new (file);
3192 /********************************************
3193 * Default implementation of async ops *
3194 ********************************************/
3198 GFileQueryInfoFlags flags;
3200 } QueryInfoAsyncData;
3203 query_info_data_free (QueryInfoAsyncData *data)
3206 g_object_unref (data->info);
3207 g_free (data->attributes);
3212 query_info_async_thread (GSimpleAsyncResult *res,
3214 GCancellable *cancellable)
3216 GError *error = NULL;
3217 QueryInfoAsyncData *data;
3220 data = g_simple_async_result_get_op_res_gpointer (res);
3222 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3226 g_simple_async_result_set_from_error (res, error);
3227 g_error_free (error);
3234 g_file_real_query_info_async (GFile *file,
3235 const char *attributes,
3236 GFileQueryInfoFlags flags,
3238 GCancellable *cancellable,
3239 GAsyncReadyCallback callback,
3242 GSimpleAsyncResult *res;
3243 QueryInfoAsyncData *data;
3245 data = g_new0 (QueryInfoAsyncData, 1);
3246 data->attributes = g_strdup (attributes);
3247 data->flags = flags;
3249 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3250 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3252 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3253 g_object_unref (res);
3257 g_file_real_query_info_finish (GFile *file,
3261 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3262 QueryInfoAsyncData *data;
3264 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3266 data = g_simple_async_result_get_op_res_gpointer (simple);
3268 return g_object_ref (data->info);
3275 GFileQueryInfoFlags flags;
3276 GFileEnumerator *enumerator;
3277 } EnumerateChildrenAsyncData;
3280 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3282 if (data->enumerator)
3283 g_object_unref (data->enumerator);
3284 g_free (data->attributes);
3289 enumerate_children_async_thread (GSimpleAsyncResult *res,
3291 GCancellable *cancellable)
3293 GError *error = NULL;
3294 EnumerateChildrenAsyncData *data;
3295 GFileEnumerator *enumerator;
3297 data = g_simple_async_result_get_op_res_gpointer (res);
3299 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3301 if (enumerator == NULL)
3303 g_simple_async_result_set_from_error (res, error);
3304 g_error_free (error);
3307 data->enumerator = enumerator;
3311 g_file_real_enumerate_children_async (GFile *file,
3312 const char *attributes,
3313 GFileQueryInfoFlags flags,
3315 GCancellable *cancellable,
3316 GAsyncReadyCallback callback,
3319 GSimpleAsyncResult *res;
3320 EnumerateChildrenAsyncData *data;
3322 data = g_new0 (EnumerateChildrenAsyncData, 1);
3323 data->attributes = g_strdup (attributes);
3324 data->flags = flags;
3326 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3327 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3329 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3330 g_object_unref (res);
3333 static GFileEnumerator *
3334 g_file_real_enumerate_children_finish (GFile *file,
3338 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3339 EnumerateChildrenAsyncData *data;
3341 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3343 data = g_simple_async_result_get_op_res_gpointer (simple);
3344 if (data->enumerator)
3345 return g_object_ref (data->enumerator);
3351 open_read_async_thread (GSimpleAsyncResult *res,
3353 GCancellable *cancellable)
3356 GFileInputStream *stream;
3357 GError *error = NULL;
3359 iface = G_FILE_GET_IFACE (object);
3361 stream = iface->read (G_FILE (object), cancellable, &error);
3365 g_simple_async_result_set_from_error (res, error);
3366 g_error_free (error);
3369 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3373 g_file_real_read_async (GFile *file,
3375 GCancellable *cancellable,
3376 GAsyncReadyCallback callback,
3379 GSimpleAsyncResult *res;
3381 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3383 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3384 g_object_unref (res);
3387 static GFileInputStream *
3388 g_file_real_read_finish (GFile *file,
3392 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3395 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3397 op = g_simple_async_result_get_op_res_gpointer (simple);
3399 return g_object_ref (op);
3405 append_to_async_thread (GSimpleAsyncResult *res,
3407 GCancellable *cancellable)
3410 GFileCreateFlags *data;
3411 GFileOutputStream *stream;
3412 GError *error = NULL;
3414 iface = G_FILE_GET_IFACE (object);
3416 data = g_simple_async_result_get_op_res_gpointer (res);
3418 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3422 g_simple_async_result_set_from_error (res, error);
3423 g_error_free (error);
3426 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3430 g_file_real_append_to_async (GFile *file,
3431 GFileCreateFlags flags,
3433 GCancellable *cancellable,
3434 GAsyncReadyCallback callback,
3437 GFileCreateFlags *data;
3438 GSimpleAsyncResult *res;
3440 data = g_new0 (GFileCreateFlags, 1);
3443 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3444 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3446 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3447 g_object_unref (res);
3450 static GFileOutputStream *
3451 g_file_real_append_to_finish (GFile *file,
3455 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3458 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3460 op = g_simple_async_result_get_op_res_gpointer (simple);
3462 return g_object_ref (op);
3468 create_async_thread (GSimpleAsyncResult *res,
3470 GCancellable *cancellable)
3473 GFileCreateFlags *data;
3474 GFileOutputStream *stream;
3475 GError *error = NULL;
3477 iface = G_FILE_GET_IFACE (object);
3479 data = g_simple_async_result_get_op_res_gpointer (res);
3481 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3485 g_simple_async_result_set_from_error (res, error);
3486 g_error_free (error);
3489 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3493 g_file_real_create_async (GFile *file,
3494 GFileCreateFlags flags,
3496 GCancellable *cancellable,
3497 GAsyncReadyCallback callback,
3500 GFileCreateFlags *data;
3501 GSimpleAsyncResult *res;
3503 data = g_new0 (GFileCreateFlags, 1);
3506 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3507 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3509 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3510 g_object_unref (res);
3513 static GFileOutputStream *
3514 g_file_real_create_finish (GFile *file,
3518 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3521 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3523 op = g_simple_async_result_get_op_res_gpointer (simple);
3525 return g_object_ref (op);
3531 GFileOutputStream *stream;
3533 gboolean make_backup;
3534 GFileCreateFlags flags;
3538 replace_async_data_free (ReplaceAsyncData *data)
3541 g_object_unref (data->stream);
3542 g_free (data->etag);
3547 replace_async_thread (GSimpleAsyncResult *res,
3549 GCancellable *cancellable)
3552 GFileOutputStream *stream;
3553 GError *error = NULL;
3554 ReplaceAsyncData *data;
3556 iface = G_FILE_GET_IFACE (object);
3558 data = g_simple_async_result_get_op_res_gpointer (res);
3560 stream = iface->replace (G_FILE (object),
3569 g_simple_async_result_set_from_error (res, error);
3570 g_error_free (error);
3573 data->stream = stream;
3577 g_file_real_replace_async (GFile *file,
3579 gboolean make_backup,
3580 GFileCreateFlags flags,
3582 GCancellable *cancellable,
3583 GAsyncReadyCallback callback,
3586 GSimpleAsyncResult *res;
3587 ReplaceAsyncData *data;
3589 data = g_new0 (ReplaceAsyncData, 1);
3590 data->etag = g_strdup (etag);
3591 data->make_backup = make_backup;
3592 data->flags = flags;
3594 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3595 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3597 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3598 g_object_unref (res);
3601 static GFileOutputStream *
3602 g_file_real_replace_finish (GFile *file,
3606 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3607 ReplaceAsyncData *data;
3609 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3611 data = g_simple_async_result_get_op_res_gpointer (simple);
3613 return g_object_ref (data->stream);
3621 } SetDisplayNameAsyncData;
3624 set_display_name_data_free (SetDisplayNameAsyncData *data)
3626 g_free (data->name);
3628 g_object_unref (data->file);
3633 set_display_name_async_thread (GSimpleAsyncResult *res,
3635 GCancellable *cancellable)
3637 GError *error = NULL;
3638 SetDisplayNameAsyncData *data;
3641 data = g_simple_async_result_get_op_res_gpointer (res);
3643 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3647 g_simple_async_result_set_from_error (res, error);
3648 g_error_free (error);
3655 g_file_real_set_display_name_async (GFile *file,
3656 const char *display_name,
3658 GCancellable *cancellable,
3659 GAsyncReadyCallback callback,
3662 GSimpleAsyncResult *res;
3663 SetDisplayNameAsyncData *data;
3665 data = g_new0 (SetDisplayNameAsyncData, 1);
3666 data->name = g_strdup (display_name);
3668 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3669 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3671 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3672 g_object_unref (res);
3676 g_file_real_set_display_name_finish (GFile *file,
3680 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3681 SetDisplayNameAsyncData *data;
3683 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3685 data = g_simple_async_result_get_op_res_gpointer (simple);
3687 return g_object_ref (data->file);
3693 GFileQueryInfoFlags flags;
3700 set_info_data_free (SetInfoAsyncData *data)
3703 g_object_unref (data->info);
3705 g_error_free (data->error);
3710 set_info_async_thread (GSimpleAsyncResult *res,
3712 GCancellable *cancellable)
3714 SetInfoAsyncData *data;
3716 data = g_simple_async_result_get_op_res_gpointer (res);
3719 data->res = g_file_set_attributes_from_info (G_FILE (object),
3727 g_file_real_set_attributes_async (GFile *file,
3729 GFileQueryInfoFlags flags,
3731 GCancellable *cancellable,
3732 GAsyncReadyCallback callback,
3735 GSimpleAsyncResult *res;
3736 SetInfoAsyncData *data;
3738 data = g_new0 (SetInfoAsyncData, 1);
3739 data->info = g_file_info_dup (info);
3740 data->flags = flags;
3742 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
3743 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
3745 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
3746 g_object_unref (res);
3750 g_file_real_set_attributes_finish (GFile *file,
3755 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3756 SetInfoAsyncData *data;
3758 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
3760 data = g_simple_async_result_get_op_res_gpointer (simple);
3763 *info = g_object_ref (data->info);
3765 if (error != NULL && data->error)
3766 *error = g_error_copy (data->error);
3771 /********************************************
3772 * Default VFS operations *
3773 ********************************************/
3776 * g_file_new_for_path:
3777 * @path: a string containing a relative or absolute path.
3779 * Constructs a #GFile for a given path. This operation never
3780 * fails, but the returned object might not support any I/O
3781 * operation if @path is malformed.
3783 * Returns: a new #GFile for the given @path.
3786 g_file_new_for_path (const char *path)
3788 g_return_val_if_fail (path != NULL, NULL);
3790 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
3794 * g_file_new_for_uri:
3795 * @uri: a string containing a URI.
3797 * Constructs a #GFile for a given URI. This operation never
3798 * fails, but the returned object might not support any I/O
3799 * operation if @uri is malformed or if the uri type is
3802 * Returns: a #GFile for the given @uri.
3805 g_file_new_for_uri (const char *uri)
3807 g_return_val_if_fail (uri != NULL, NULL);
3809 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
3813 * g_file_parse_name:
3814 * @parse_name: a file name or path to be parsed.
3816 * Constructs a #GFile with the given @parse_name,
3817 * looked up by #GVfs. This operation never fails,
3818 * but the returned object might not support any I/O
3819 * operation if the @parse_name cannot be parsed by #GVfs.
3821 * Returns: a new #GFile.
3824 g_file_parse_name (const char *parse_name)
3826 g_return_val_if_fail (parse_name != NULL, NULL);
3828 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
3832 is_valid_scheme_character (char c)
3834 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
3838 has_valid_scheme (const char *uri)
3844 if (!is_valid_scheme_character (*p))
3849 } while (is_valid_scheme_character (*p));
3855 * g_file_new_for_commandline_arg:
3856 * @arg: a command line string.
3858 * Attempts to generate a #GFile with the given line from
3859 * the command line argument.
3861 * Returns: a new #GFile.
3864 g_file_new_for_commandline_arg (const char *arg)
3870 g_return_val_if_fail (arg != NULL, NULL);
3872 if (g_path_is_absolute (arg))
3873 return g_file_new_for_path (arg);
3875 if (has_valid_scheme (arg))
3876 return g_file_new_for_uri (arg);
3878 current_dir = g_get_current_dir ();
3879 filename = g_build_filename (current_dir, arg, NULL);
3880 g_free (current_dir);
3882 file = g_file_new_for_path (filename);
3889 * g_mount_for_location:
3890 * @location: input #GFile.
3891 * @mount_operation: a #GMountOperation.
3892 * @cancellable: optional #GCancellable object, %NULL to ignore.
3893 * @callback: a #GAsyncReadyCallback.
3894 * @user_data: a #gpointer.
3896 * Starts the @mount_operation, mounting the volume at @location.
3898 * When this operation has completed, @callback will be called with
3899 * @user_user data, and the operation can be finalized with
3900 * g_mount_for_location_finish().
3902 * If @cancellable is not %NULL, then the operation can be cancelled by
3903 * triggering the cancellable object from another thread. If the operation
3904 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3907 g_mount_for_location (GFile *location,
3908 GMountOperation *mount_operation,
3909 GCancellable *cancellable,
3910 GAsyncReadyCallback callback,
3915 g_return_if_fail (G_IS_FILE (location));
3916 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
3918 iface = G_FILE_GET_IFACE (location);
3920 if (iface->mount_for_location == NULL)
3922 g_simple_async_report_error_in_idle (G_OBJECT (location),
3923 callback, user_data,
3924 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3925 _("volume doesn't implement mount"));
3930 (* iface->mount_for_location) (location, mount_operation, cancellable, callback, user_data);
3935 * g_mount_for_location_finish:
3936 * @location: input #GFile.
3937 * @result: a #GAsyncResult.
3938 * @error: a #GError, or %NULL
3940 * Finishes a mount operation started by g_mount_for_location().
3942 * Returns: %TRUE if successful. If an error
3943 * has occured, this function will return %FALSE and set @error
3944 * appropriately if present.
3947 g_mount_for_location_finish (GFile *location,
3948 GAsyncResult *result,
3953 g_return_val_if_fail (G_IS_FILE (location), FALSE);
3954 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3956 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3958 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3959 if (g_simple_async_result_propagate_error (simple, error))
3963 iface = G_FILE_GET_IFACE (location);
3965 return (* iface->mount_for_location_finish) (location, result, error);
3968 /********************************************
3969 * Utility functions *
3970 ********************************************/
3972 #define GET_CONTENT_BLOCK_SIZE 8192
3975 * g_file_load_contents:
3976 * @file: input #GFile.
3977 * @cancellable: optional #GCancellable object, %NULL to ignore.
3978 * @contents: a location to place the contents of the file.
3979 * @length: a location to place the length of the contents of the file.
3980 * @etag_out: a location to place the current entity tag for the file.
3981 * @error: a #GError, or %NULL
3983 * If @cancellable is not %NULL, then the operation can be cancelled by
3984 * triggering the cancellable object from another thread. If the operation
3985 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3987 * Returns: %TRUE if the @file's contents were successfully loaded.
3988 * %FALSE if there were errors. The length of the loaded data will be
3989 * put into @length, the contents in @contents.
3992 g_file_load_contents (GFile *file,
3993 GCancellable *cancellable,
3999 GFileInputStream *in;
4000 GByteArray *content;
4005 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4006 g_return_val_if_fail (contents != NULL, FALSE);
4008 in = g_file_read (file, cancellable, error);
4012 content = g_byte_array_new ();
4015 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4016 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4017 content->data + pos,
4018 GET_CONTENT_BLOCK_SIZE,
4019 cancellable, error)) > 0)
4022 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4029 info = g_file_input_stream_query_info (in,
4030 G_FILE_ATTRIBUTE_ETAG_VALUE,
4035 *etag_out = g_strdup (g_file_info_get_etag (info));
4036 g_object_unref (info);
4040 /* Ignore errors on close */
4041 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4042 g_object_unref (in);
4046 /* error is set already */
4047 g_byte_array_free (content, TRUE);
4054 /* Zero terminate (we got an extra byte allocated for this */
4055 content->data[pos] = 0;
4057 *contents = (char *)g_byte_array_free (content, FALSE);
4065 GCancellable *cancellable;
4066 GFileReadMoreCallback read_more_callback;
4067 GAsyncReadyCallback callback;
4069 GByteArray *content;
4076 load_contents_data_free (LoadContentsData *data)
4079 g_error_free (data->error);
4080 if (data->cancellable)
4081 g_object_unref (data->cancellable);
4083 g_byte_array_free (data->content, TRUE);
4084 g_free (data->etag);
4085 g_object_unref (data->file);
4090 load_contents_close_callback (GObject *obj,
4091 GAsyncResult *close_res,
4094 GInputStream *stream = G_INPUT_STREAM (obj);
4095 LoadContentsData *data = user_data;
4096 GSimpleAsyncResult *res;
4098 /* Ignore errors here, we're only reading anyway */
4099 g_input_stream_close_finish (stream, close_res, NULL);
4100 g_object_unref (stream);
4102 res = g_simple_async_result_new (G_OBJECT (data->file),
4105 g_file_load_contents_async);
4106 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4107 g_simple_async_result_complete (res);
4108 g_object_unref (res);
4112 load_contents_fstat_callback (GObject *obj,
4113 GAsyncResult *stat_res,
4116 GInputStream *stream = G_INPUT_STREAM (obj);
4117 LoadContentsData *data = user_data;
4120 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4124 data->etag = g_strdup (g_file_info_get_etag (info));
4125 g_object_unref (info);
4128 g_input_stream_close_async (stream, 0,
4130 load_contents_close_callback, data);
4134 load_contents_read_callback (GObject *obj,
4135 GAsyncResult *read_res,
4138 GInputStream *stream = G_INPUT_STREAM (obj);
4139 LoadContentsData *data = user_data;
4140 GError *error = NULL;
4143 read_size = g_input_stream_read_finish (stream, read_res, &error);
4147 /* Error or EOF, close the file */
4148 data->error = error;
4149 g_input_stream_close_async (stream, 0,
4151 load_contents_close_callback, data);
4153 else if (read_size == 0)
4155 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4156 G_FILE_ATTRIBUTE_ETAG_VALUE,
4159 load_contents_fstat_callback,
4162 else if (read_size > 0)
4164 data->pos += read_size;
4166 g_byte_array_set_size (data->content,
4167 data->pos + GET_CONTENT_BLOCK_SIZE);
4170 if (data->read_more_callback &&
4171 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4172 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4173 G_FILE_ATTRIBUTE_ETAG_VALUE,
4176 load_contents_fstat_callback,
4179 g_input_stream_read_async (stream,
4180 data->content->data + data->pos,
4181 GET_CONTENT_BLOCK_SIZE,
4184 load_contents_read_callback,
4190 load_contents_open_callback (GObject *obj,
4191 GAsyncResult *open_res,
4194 GFile *file = G_FILE (obj);
4195 GFileInputStream *stream;
4196 LoadContentsData *data = user_data;
4197 GError *error = NULL;
4198 GSimpleAsyncResult *res;
4200 stream = g_file_read_finish (file, open_res, &error);
4204 g_byte_array_set_size (data->content,
4205 data->pos + GET_CONTENT_BLOCK_SIZE);
4206 g_input_stream_read_async (G_INPUT_STREAM (stream),
4207 data->content->data + data->pos,
4208 GET_CONTENT_BLOCK_SIZE,
4211 load_contents_read_callback,
4217 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4221 g_simple_async_result_complete (res);
4222 g_error_free (error);
4223 load_contents_data_free (data);
4224 g_object_unref (res);
4229 * g_file_load_partial_contents_async:
4230 * @file: input #GFile.
4231 * @cancellable: optional #GCancellable object, %NULL to ignore.
4232 * @read_more_callback: a #GFileReadMoreCallback.
4233 * @callback: a #GAsyncReadyCallback.
4234 * @user_data: a #gpointer.
4236 * If @cancellable is not %NULL, then the operation can be cancelled by
4237 * triggering the cancellable object from another thread. If the operation
4238 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4241 g_file_load_partial_contents_async (GFile *file,
4242 GCancellable *cancellable,
4243 GFileReadMoreCallback read_more_callback,
4244 GAsyncReadyCallback callback,
4247 LoadContentsData *data;
4249 g_return_if_fail (G_IS_FILE (file));
4251 data = g_new0 (LoadContentsData, 1);
4254 data->cancellable = g_object_ref (cancellable);
4255 data->read_more_callback = read_more_callback;
4256 data->callback = callback;
4257 data->user_data = user_data;
4258 data->content = g_byte_array_new ();
4259 data->file = g_object_ref (file);
4261 g_file_read_async (file,
4264 load_contents_open_callback,
4269 * g_file_load_partial_contents_finish:
4270 * @file: input #GFile.
4271 * @res: a #GAsyncResult.
4272 * @contents: a location to place the contents of the file.
4273 * @length: a location to place the length of the contents of the file.
4274 * @etag_out: a location to place the current entity tag for the file.
4275 * @error: a #GError, or %NULL
4277 * Finishes an asynchronous partial load operation that was started
4278 * with g_file_load_partial_contents_async().
4280 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4281 * present, it will be set appropriately.
4284 g_file_load_partial_contents_finish (GFile *file,
4291 GSimpleAsyncResult *simple;
4292 LoadContentsData *data;
4294 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4295 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4296 g_return_val_if_fail (contents != NULL, FALSE);
4298 simple = G_SIMPLE_ASYNC_RESULT (res);
4300 if (g_simple_async_result_propagate_error (simple, error))
4303 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4305 data = g_simple_async_result_get_op_res_gpointer (simple);
4309 g_propagate_error (error, data->error);
4318 *length = data->pos;
4322 *etag_out = data->etag;
4326 /* Zero terminate */
4327 g_byte_array_set_size (data->content, data->pos + 1);
4328 data->content->data[data->pos] = 0;
4330 *contents = (char *)g_byte_array_free (data->content, FALSE);
4331 data->content = NULL;
4337 * g_file_load_contents_async:
4338 * @file: input #GFile.
4339 * @cancellable: optional #GCancellable object, %NULL to ignore.
4340 * @callback: a #GAsyncReadyCallback.
4341 * @user_data: a #gpointer.
4343 * Starts an asynchronous load of the @file's contents.
4344 * When the load operation has completed, @callback will be called
4345 * with @userdata. To finish the operation, call
4346 * g_file_load_contents_finish() with the #GAsyncResult returned by
4349 * If @cancellable is not %NULL, then the operation can be cancelled by
4350 * triggering the cancellable object from another thread. If the operation
4351 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4354 g_file_load_contents_async (GFile *file,
4355 GCancellable *cancellable,
4356 GAsyncReadyCallback callback,
4359 g_file_load_partial_contents_async (file,
4362 callback, user_data);
4366 * g_file_load_contents_finish:
4367 * @file: input #GFile.
4368 * @res: a #GAsyncResult.
4369 * @contents: a location to place the contents of the file.
4370 * @length: a location to place the length of the contents of the file.
4371 * @etag_out: a location to place the current entity tag for the file.
4372 * @error: a #GError, or %NULL
4374 * Finishes an asynchronous load of the @file's contents.
4375 * The contents are placed in @contents, and @length is set to the
4376 * size of the @contents string. If @etag_out is present, it will be
4377 * set to the new entity tag for the @file.
4379 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4380 * present, it will be set appropriately.
4383 g_file_load_contents_finish (GFile *file,
4390 return g_file_load_partial_contents_finish (file,
4399 * g_file_replace_contents:
4400 * @file: input #GFile.
4401 * @contents: a string containing the new contents for @file.
4402 * @length: the length of @contents in bytes.
4403 * @etag: the old <link linkend="gfile-etag">entity tag</link>
4405 * @make_backup: a #gboolean.
4406 * @flags: a set of #GFileCreateFlags.
4407 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4409 * @cancellable: optional #GCancellable object, %NULL to ignore.
4410 * @error: a #GError, or %NULL
4412 * Replaces the contents of @file with @contents of @length bytes.
4413 * The old @etag will be replaced with the @new_etag. If @make_backup
4414 * is %TRUE, this function will attempt to make a backup of @file.
4416 * If @cancellable is not %NULL, then the operation can be cancelled by
4417 * triggering the cancellable object from another thread. If the operation
4418 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4420 * Returns: %TRUE if successful. If an error
4421 * has occured, this function will return %FALSE and set @error
4422 * appropriately if present.
4425 g_file_replace_contents (GFile *file,
4426 const char *contents,
4429 gboolean make_backup,
4430 GFileCreateFlags flags,
4432 GCancellable *cancellable,
4435 GFileOutputStream *out;
4436 gsize pos, remainder;
4439 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4440 g_return_val_if_fail (contents != NULL, FALSE);
4442 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4448 while (remainder > 0 &&
4449 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4451 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4459 if (remainder > 0 && res < 0)
4461 /* Ignore errors on close */
4462 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4464 /* error is set already */
4468 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4472 *new_etag = g_file_output_stream_get_etag (out);
4480 GCancellable *cancellable;
4481 GAsyncReadyCallback callback;
4483 const char *content;
4487 } ReplaceContentsData;
4490 replace_contents_data_free (ReplaceContentsData *data)
4493 g_error_free (data->error);
4494 if (data->cancellable)
4495 g_object_unref (data->cancellable);
4496 g_object_unref (data->file);
4497 g_free (data->etag);
4502 replace_contents_close_callback (GObject *obj,
4503 GAsyncResult *close_res,
4506 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4507 ReplaceContentsData *data = user_data;
4508 GSimpleAsyncResult *res;
4510 /* Ignore errors here, we're only reading anyway */
4511 g_output_stream_close_finish (stream, close_res, NULL);
4512 g_object_unref (stream);
4514 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4516 res = g_simple_async_result_new (G_OBJECT (data->file),
4519 g_file_replace_contents_async);
4520 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4521 g_simple_async_result_complete (res);
4522 g_object_unref (res);
4526 replace_contents_write_callback (GObject *obj,
4527 GAsyncResult *read_res,
4530 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4531 ReplaceContentsData *data = user_data;
4532 GError *error = NULL;
4535 write_size = g_output_stream_write_finish (stream, read_res, &error);
4537 if (write_size <= 0)
4539 /* Error or EOF, close the file */
4541 data->error = error;
4542 g_output_stream_close_async (stream, 0,
4544 replace_contents_close_callback, data);
4546 else if (write_size > 0)
4548 data->pos += write_size;
4550 if (data->pos >= data->length)
4551 g_output_stream_close_async (stream, 0,
4553 replace_contents_close_callback, data);
4555 g_output_stream_write_async (stream,
4556 data->content + data->pos,
4557 data->length - data->pos,
4560 replace_contents_write_callback,
4566 replace_contents_open_callback (GObject *obj,
4567 GAsyncResult *open_res,
4570 GFile *file = G_FILE (obj);
4571 GFileOutputStream *stream;
4572 ReplaceContentsData *data = user_data;
4573 GError *error = NULL;
4574 GSimpleAsyncResult *res;
4576 stream = g_file_replace_finish (file, open_res, &error);
4580 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4581 data->content + data->pos,
4582 data->length - data->pos,
4585 replace_contents_write_callback,
4591 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4595 g_simple_async_result_complete (res);
4596 g_error_free (error);
4597 replace_contents_data_free (data);
4598 g_object_unref (res);
4603 * g_file_replace_contents_async:
4604 * @file: input #GFile.
4605 * @contents: string of contents to replace the file with.
4606 * @length: the length of @contents in bytes.
4607 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4608 * @make_backup: a #gboolean.
4609 * @flags: a set of #GFileCreateFlags.
4610 * @cancellable: optional #GCancellable object, %NULL to ignore.
4611 * @callback: a #GAsyncReadyCallback.
4612 * @user_data: a #gpointer.
4614 * Starts an asynchronous replacement of @file with the given
4615 * @contents of @length bytes. @etag will replace the document's
4616 * current entity tag.
4618 * When this operation has completed, @callback will be called with
4619 * @user_user data, and the operation can be finalized with
4620 * g_file_replace_contents_finish().
4622 * If @cancellable is not %NULL, then the operation can be cancelled by
4623 * triggering the cancellable object from another thread. If the operation
4624 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4626 * If @make_backup is %TRUE, this function will attempt to
4627 * make a backup of @file.
4630 g_file_replace_contents_async (GFile *file,
4631 const char *contents,
4634 gboolean make_backup,
4635 GFileCreateFlags flags,
4636 GCancellable *cancellable,
4637 GAsyncReadyCallback callback,
4640 ReplaceContentsData *data;
4642 g_return_if_fail (G_IS_FILE (file));
4643 g_return_if_fail (contents != NULL);
4645 data = g_new0 (ReplaceContentsData, 1);
4648 data->cancellable = g_object_ref (cancellable);
4649 data->callback = callback;
4650 data->user_data = user_data;
4651 data->content = contents;
4652 data->length = length;
4654 data->file = g_object_ref (file);
4656 g_file_replace_async (file,
4662 replace_contents_open_callback,
4667 * g_file_replace_contents_finish:
4668 * @file: input #GFile.
4669 * @res: a #GAsyncResult.
4670 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
4672 * @error: a #GError, or %NULL
4674 * Finishes an asynchronous replace of the given @file. See
4675 * g_file_replace_contents_async(). Sets @new_etag to the new entity
4676 * tag for the document, if present.
4678 * Returns: %TRUE on success, %FALSE on failure.
4681 g_file_replace_contents_finish (GFile *file,
4686 GSimpleAsyncResult *simple;
4687 ReplaceContentsData *data;
4689 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4690 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4692 simple = G_SIMPLE_ASYNC_RESULT (res);
4694 if (g_simple_async_result_propagate_error (simple, error))
4697 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
4699 data = g_simple_async_result_get_op_res_gpointer (simple);
4703 g_propagate_error (error, data->error);
4711 *new_etag = data->etag;
4712 data->etag = NULL; /* Take ownership */
4718 #define __G_FILE_C__
4719 #include "gioaliasdef.c"