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 * If you are interested in copying the #GFile object itself (not the on-disk
1932 * file), see g_file_dup().
1934 * Returns: %TRUE on success, %FALSE otherwise.
1937 g_file_copy (GFile *source,
1939 GFileCopyFlags flags,
1940 GCancellable *cancellable,
1941 GFileProgressCallback progress_callback,
1942 gpointer progress_callback_data,
1949 g_return_val_if_fail (G_IS_FILE (source), FALSE);
1950 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
1952 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1955 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
1957 iface = G_FILE_GET_IFACE (source);
1962 res = (* iface->copy) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
1967 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
1969 g_propagate_error (error, my_error);
1975 return file_copy_fallback (source, destination, flags, cancellable,
1976 progress_callback, progress_callback_data,
1983 * @source: #GFile pointing to the source location.
1984 * @destination: #GFile pointing to the destination location.
1985 * @flags: set of #GFileCopyFlags.
1986 * @cancellable: optional #GCancellable object, %NULL to ignore.
1987 * @progress_callback: #GFileProgressCallback function for updates.
1988 * @progress_callback_data: gpointer to user data for the callback function.
1989 * @error: #GError for returning error conditions, or %NULL
1991 * <!-- Source version
1992 * source dest flags results in
1993 * - * * G_IO_ERROR_NOT_FOUND
1995 * file * 0 G_IO_ERROR_EXISTS
1996 * file file overwr ok
1997 * file dir overwr G_IO_ERROR_IS_DIRECTORY
1999 * dir - * ok || G_IO_ERROR_WOULD_RECURSE
2000 * dir * 0 G_IO_ERROR_EXISTS
2001 * dir dir overwr G_IO_ERROR_WOULD_MERGE
2002 * dir file overwr ok || G_IO_ERROR_WOULD_RECURSE
2003 * Docbook version below -->
2005 * Moves a file or directory from @source to @destination, with the given
2006 * @flags. This operation may fail, and @error will be set appropriately with
2007 * the given error result (see the following table).
2008 * File moves are always asynchronous.
2010 * If @cancellable is not %NULL, then the operation can be cancelled by
2011 * triggering the cancellable object from another thread. If the operation
2012 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2014 * If @progress_callback is not %NULL, then the operation can be monitored by
2015 * setting this to a #GFileProgressCallback function. @progress_callback_data
2016 * will be passed to this function.
2019 * <title>g_file_move() Error Conditions</title>
2020 * <tgroup cols='4' align='left'><thead>
2021 * <row><entry>Source</entry><entry>Destination</entry>
2022 * <entry>Flags</entry><entry>Results in</entry></row>
2024 * <row><entry> %NULL </entry><entry> Anything </entry>
2025 * <entry> Anything </entry><entry> %G_IO_ERROR_NOT_FOUND </entry></row>
2026 * <row><entry> File </entry><entry> %NULL </entry>
2027 * <entry> Anything </entry><entry> No Error </entry></row>
2028 * <row><entry> File </entry><entry> Anything </entry>
2029 * <entry> 0 </entry><entry> %G_IO_ERROR_EXISTS </entry></row>
2030 * <row><entry> File </entry><entry> File </entry>
2031 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> No Error </entry></row>
2032 * <row><entry> File </entry><entry> Directory </entry>
2033 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> %G_IO_ERROR_IS_DIRECTORY </entry></row>
2034 * <row><entry> Directory </entry><entry> %NULL </entry>
2035 * <entry> Anything </entry><entry> No Error or %G_IO_ERROR_WOULD_RECURSE </entry></row>
2036 * <row><entry> Directory </entry><entry> Anything </entry>
2037 * <entry> 0 </entry><entry> %G_IO_ERROR_EXISTS </entry></row>
2038 * <row><entry> Directory </entry><entry> Directory </entry>
2039 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> %G_IO_ERROR_IS_DIRECTORY </entry></row>
2040 * <row><entry> Directory </entry><entry> File </entry>
2041 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> No Error or %G_IO_ERROR_WOULD_RECURSE </entry></row>
2046 * Returns: %TRUE on successful move, %FALSE otherwise.
2049 g_file_move (GFile *source,
2051 GFileCopyFlags flags,
2052 GCancellable *cancellable,
2053 GFileProgressCallback progress_callback,
2054 gpointer progress_callback_data,
2061 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2062 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2064 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2067 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
2069 iface = G_FILE_GET_IFACE (source);
2074 res = (* iface->move) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
2079 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2081 g_propagate_error (error, my_error);
2087 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2089 g_set_error (error, G_IO_ERROR,
2090 G_IO_ERROR_NOT_SUPPORTED,
2091 _("Operation not supported"));
2095 flags |= G_FILE_COPY_ALL_METADATA;
2096 if (!g_file_copy (source, destination, flags, cancellable,
2097 progress_callback, progress_callback_data,
2101 return g_file_delete (source, cancellable, error);
2105 * g_file_make_directory
2106 * @file: input #GFile.
2107 * @cancellable: optional #GCancellable object, %NULL to ignore.
2108 * @error: a #GError, or %NULL
2110 * Creates a directory.
2112 * If @cancellable is not %NULL, then the operation can be cancelled by
2113 * triggering the cancellable object from another thread. If the operation
2114 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2116 * Returns: %TRUE on successful creation, %FALSE otherwise.
2119 g_file_make_directory (GFile *file,
2120 GCancellable *cancellable,
2125 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2127 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2130 iface = G_FILE_GET_IFACE (file);
2132 if (iface->make_directory == NULL)
2134 g_set_error (error, G_IO_ERROR,
2135 G_IO_ERROR_NOT_SUPPORTED,
2136 _("Operation not supported"));
2140 return (* iface->make_directory) (file, cancellable, error);
2144 * g_file_make_symbolic_link:
2145 * @file: input #GFile.
2146 * @symlink_value: a string with the name of the new symlink.
2147 * @cancellable: optional #GCancellable object, %NULL to ignore.
2148 * @error: a #GError.
2150 * Creates a symbolic link.
2152 * If @cancellable is not %NULL, then the operation can be cancelled by
2153 * triggering the cancellable object from another thread. If the operation
2154 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2156 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2159 g_file_make_symbolic_link (GFile *file,
2160 const char *symlink_value,
2161 GCancellable *cancellable,
2166 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2167 g_return_val_if_fail (symlink_value != NULL, FALSE);
2169 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2172 if (*symlink_value == '\0')
2174 g_set_error (error, G_IO_ERROR,
2175 G_IO_ERROR_INVALID_ARGUMENT,
2176 _("Invalid symlink value given"));
2180 iface = G_FILE_GET_IFACE (file);
2182 if (iface->make_symbolic_link == NULL)
2184 g_set_error (error, G_IO_ERROR,
2185 G_IO_ERROR_NOT_SUPPORTED,
2186 _("Operation not supported"));
2190 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2195 * @file: input #GFile.
2196 * @cancellable: optional #GCancellable object, %NULL to ignore.
2197 * @error: a #GError, or %NULL
2201 * If @cancellable is not %NULL, then the operation can be cancelled by
2202 * triggering the cancellable object from another thread. If the operation
2203 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2205 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2208 g_file_delete (GFile *file,
2209 GCancellable *cancellable,
2214 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2216 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2219 iface = G_FILE_GET_IFACE (file);
2221 if (iface->delete_file == NULL)
2223 g_set_error (error, G_IO_ERROR,
2224 G_IO_ERROR_NOT_SUPPORTED,
2225 _("Operation not supported"));
2229 return (* iface->delete_file) (file, cancellable, error);
2234 * @file: #GFile to send to trash.
2235 * @cancellable: optional #GCancellable object, %NULL to ignore.
2236 * @error: a #GError, or %NULL
2238 * Sends @file to the virtual file system "Trash" location. If the
2239 * virtual file system does not have support having a "Trash" location,
2240 * %FALSE will be returned, and @error will be set to
2241 * %G_IO_ERROR_NOT_SUPPORTED.
2243 * If @cancellable is not %NULL, then the operation can be cancelled by
2244 * triggering the cancellable object from another thread. If the operation
2245 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2247 * Returns: %TRUE on successful trash, %FALSE otherwise.
2250 g_file_trash (GFile *file,
2251 GCancellable *cancellable,
2256 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2258 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2261 iface = G_FILE_GET_IFACE (file);
2263 if (iface->trash == NULL)
2266 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2267 _("Trash not supported"));
2271 return (* iface->trash) (file, cancellable, error);
2275 * g_file_set_display_name:
2276 * @file: input #GFile.
2277 * @display_name: a string.
2278 * @cancellable: optional #GCancellable object, %NULL to ignore.
2279 * @error: a #GError, or %NULL
2281 * Sets the display name for @file. If the display name contains invalid
2282 * characters, @error will be set to %G_IO_ERROR_INVALID_ARGUMENT. For the
2283 * asynchronous version of this function, see g_file_set_display_name_async().
2285 * If @cancellable is not %NULL, then the operation can be cancelled by
2286 * triggering the cancellable object from another thread. If the operation
2287 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2289 * Returns: a #GFile, or %NULL if there was an error.
2292 g_file_set_display_name (GFile *file,
2293 const char *display_name,
2294 GCancellable *cancellable,
2299 g_return_val_if_fail (G_IS_FILE (file), NULL);
2300 g_return_val_if_fail (display_name != NULL, NULL);
2302 if (strchr (display_name, '/') != NULL)
2306 G_IO_ERROR_INVALID_ARGUMENT,
2307 _("File names cannot contain '/'"));
2311 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2314 iface = G_FILE_GET_IFACE (file);
2316 return (* iface->set_display_name) (file, display_name, cancellable, error);
2320 * g_file_set_display_name_async:
2321 * @file: input #GFile.
2322 * @display_name: a string.
2323 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2325 * @cancellable: optional #GCancellable object, %NULL to ignore.
2326 * @callback: a #GAsyncReadyCallback.
2327 * @user_data: a #gpointer.
2329 * Asynchronously sets the display name for a given #GFile.
2330 * For the synchronous version of this function, see g_file_set_display_name().
2332 * If @cancellable is not %NULL, then the operation can be cancelled by
2333 * triggering the cancellable object from another thread. If the operation
2334 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2337 g_file_set_display_name_async (GFile *file,
2338 const char *display_name,
2340 GCancellable *cancellable,
2341 GAsyncReadyCallback callback,
2346 g_return_if_fail (G_IS_FILE (file));
2347 g_return_if_fail (display_name != NULL);
2349 iface = G_FILE_GET_IFACE (file);
2350 (* iface->set_display_name_async) (file,
2359 * g_file_set_display_name_finish:
2360 * @file: input #GFile.
2361 * @res: a #GAsyncResult.
2362 * @error: a #GError, or %NULL
2364 * Finishes setting a display name started with
2365 * g_file_set_display_name_async().
2367 * Returns: a #GFile or %NULL on error.
2370 g_file_set_display_name_finish (GFile *file,
2376 g_return_val_if_fail (G_IS_FILE (file), NULL);
2377 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2379 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2381 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2382 if (g_simple_async_result_propagate_error (simple, error))
2386 iface = G_FILE_GET_IFACE (file);
2387 return (* iface->set_display_name_finish) (file, res, error);
2391 * g_file_query_settable_attributes:
2392 * @file: input #GFile.
2393 * @cancellable: optional #GCancellable object, %NULL to ignore.
2394 * @error: a #GError, or %NULL
2396 * Obtain the list of settable attributes for the file.
2398 * If @cancellable is not %NULL, then the operation can be cancelled by
2399 * triggering the cancellable object from another thread. If the operation
2400 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2402 * Returns: the type and full attribute name of all the attributes
2403 * that the file can set. This doesn't mean setting it will always
2404 * succeed though, you might get an access failure, or some specific
2405 * file may not support a specific attribute.
2407 GFileAttributeInfoList *
2408 g_file_query_settable_attributes (GFile *file,
2409 GCancellable *cancellable,
2414 GFileAttributeInfoList *list;
2416 g_return_val_if_fail (G_IS_FILE (file), NULL);
2418 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2421 iface = G_FILE_GET_IFACE (file);
2423 if (iface->query_settable_attributes == NULL)
2424 return g_file_attribute_info_list_new ();
2427 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2431 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2433 list = g_file_attribute_info_list_new ();
2434 g_error_free (my_error);
2437 g_propagate_error (error, my_error);
2444 * g_file_query_writable_namespaces:
2445 * @file: input #GFile.
2446 * @cancellable: optional #GCancellable object, %NULL to ignore.
2447 * @error: a #GError, or %NULL
2449 * Obtain the list of attribute namespaces where new attributes
2452 * If @cancellable is not %NULL, then the operation can be cancelled by
2453 * triggering the cancellable object from another thread. If the operation
2454 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2456 * Returns: a #GFileAttributeInfoList of attribute namespaces
2457 * where the user can create their own attribute names, such
2458 * as extended attributes.
2460 GFileAttributeInfoList *
2461 g_file_query_writable_namespaces (GFile *file,
2462 GCancellable *cancellable,
2467 GFileAttributeInfoList *list;
2469 g_return_val_if_fail (G_IS_FILE (file), NULL);
2471 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2474 iface = G_FILE_GET_IFACE (file);
2476 if (iface->query_writable_namespaces == NULL)
2477 return g_file_attribute_info_list_new ();
2480 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2484 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2486 list = g_file_attribute_info_list_new ();
2487 g_error_free (my_error);
2490 g_propagate_error (error, my_error);
2497 * g_file_set_attribute:
2498 * @file: input #GFile.
2499 * @attribute: a string containing the attribute's name.
2500 * @value: a set of #GFileAttributeValue.
2501 * @flags: a set of #GFileQueryInfoFlags.
2502 * @cancellable: optional #GCancellable object, %NULL to ignore.
2503 * @error: a #GError, or %NULL
2505 * Sets an attribute in the file with attribute name @attribute to @value.
2506 * If setting attributes is not suppored by the #GFileIface for @file,
2507 * then @error will be set to %G_IO_ERROR_NOT_SUPPORTED.
2509 * If @cancellable is not %NULL, then the operation can be cancelled by
2510 * triggering the cancellable object from another thread. If the operation
2511 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2513 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2516 g_file_set_attribute (GFile *file,
2517 const char *attribute,
2518 const GFileAttributeValue *value,
2519 GFileQueryInfoFlags flags,
2520 GCancellable *cancellable,
2525 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2526 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2528 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2531 iface = G_FILE_GET_IFACE (file);
2533 if (iface->set_attribute == NULL)
2535 g_set_error (error, G_IO_ERROR,
2536 G_IO_ERROR_NOT_SUPPORTED,
2537 _("Operation not supported"));
2541 return (* iface->set_attribute) (file, attribute, value, flags, cancellable, error);
2545 * g_file_set_attributes_from_info:
2546 * @file: input #GFile.
2547 * @info: a #GFileInfo.
2548 * @flags: #GFileQueryInfoFlags
2549 * @cancellable: optional #GCancellable object, %NULL to ignore.
2550 * @error: a #GError, or %NULL
2552 * Tries to set all attributes in the #GFileInfo on the target values,
2553 * not stopping on the first error.
2555 * If @cancellable is not %NULL, then the operation can be cancelled by
2556 * triggering the cancellable object from another thread. If the operation
2557 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2559 * Returns: %TRUE if there was any error, and @error will be set to
2560 * the first error. Error on particular fields are flagged by setting
2561 * the "status" field in the attribute value to
2562 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING.
2565 g_file_set_attributes_from_info (GFile *file,
2567 GFileQueryInfoFlags flags,
2568 GCancellable *cancellable,
2573 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2574 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2576 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2579 g_file_info_clear_status (info);
2581 iface = G_FILE_GET_IFACE (file);
2583 return (* iface->set_attributes_from_info) (file,
2592 g_file_real_set_attributes_from_info (GFile *file,
2594 GFileQueryInfoFlags flags,
2595 GCancellable *cancellable,
2601 GFileAttributeValue *value;
2605 attributes = g_file_info_list_attributes (info, NULL);
2607 for (i = 0; attributes[i] != NULL; i++)
2609 value = (GFileAttributeValue *)g_file_info_get_attribute (info, attributes[i]);
2611 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2614 if (!g_file_set_attribute (file, attributes[i], value, flags, cancellable, error))
2616 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2618 /* Don't set error multiple times */
2622 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2625 g_strfreev (attributes);
2631 * g_file_set_attributes_async:
2632 * @file: input #GFile.
2633 * @info: a #GFileInfo.
2634 * @flags: a #GFileQueryInfoFlags.
2635 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2637 * @cancellable: optional #GCancellable object, %NULL to ignore.
2638 * @callback: a #GAsyncReadyCallback.
2639 * @user_data: a #gpointer.
2641 * Asynchronously sets the attributes of @file with @info.
2642 * For the synchronous version of this function, see g_file_set_attributes().
2644 * If @cancellable is not %NULL, then the operation can be cancelled by
2645 * triggering the cancellable object from another thread. If the operation
2646 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2649 g_file_set_attributes_async (GFile *file,
2651 GFileQueryInfoFlags flags,
2653 GCancellable *cancellable,
2654 GAsyncReadyCallback callback,
2659 g_return_if_fail (G_IS_FILE (file));
2660 g_return_if_fail (G_IS_FILE_INFO (info));
2662 iface = G_FILE_GET_IFACE (file);
2663 (* iface->set_attributes_async) (file,
2673 * g_file_set_attributes_finish:
2674 * @file: input #GFile.
2675 * @result: a #GAsyncResult.
2676 * @info: a #GFileInfo.
2677 * @error: a #GError, or %NULL
2679 * Finishes setting an attribute started in g_file_set_attributes_async().
2681 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2684 g_file_set_attributes_finish (GFile *file,
2685 GAsyncResult *result,
2691 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2692 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2694 /* No standard handling of errors here, as we must set info even
2697 iface = G_FILE_GET_IFACE (file);
2698 return (* iface->set_attributes_finish) (file, result, info, error);
2702 * g_file_set_attribute_string:
2703 * @file: input #GFile.
2704 * @attribute: a string containing the attribute's name.
2705 * @value: a string containing the attribute's value.
2706 * @flags: #GFileQueryInfoFlags.
2707 * @cancellable: optional #GCancellable object, %NULL to ignore.
2708 * @error: a #GError, or %NULL
2710 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
2711 * If @attribute is of a different type, this operation will fail.
2713 * If @cancellable is not %NULL, then the operation can be cancelled by
2714 * triggering the cancellable object from another thread. If the operation
2715 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2717 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2720 g_file_set_attribute_string (GFile *file,
2721 const char *attribute,
2723 GFileQueryInfoFlags flags,
2724 GCancellable *cancellable,
2727 GFileAttributeValue v;
2729 v.type = G_FILE_ATTRIBUTE_TYPE_STRING;
2730 v.u.string = (char *)value;
2731 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2735 * g_file_set_attribute_byte_string:
2736 * @file: input #GFile.
2737 * @attribute: a string containing the attribute's name.
2738 * @value: a string containing the attribute's new value.
2739 * @flags: a #GFileQueryInfoFlags.
2740 * @cancellable: optional #GCancellable object, %NULL to ignore.
2741 * @error: a #GError, or %NULL
2743 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
2744 * If @attribute is of a different type, this operation will fail,
2747 * If @cancellable is not %NULL, then the operation can be cancelled by
2748 * triggering the cancellable object from another thread. If the operation
2749 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2751 * Returns: %TRUE if the @attribute was successfully set to @value
2752 * in the @file, %FALSE otherwise.
2755 g_file_set_attribute_byte_string (GFile *file,
2756 const char *attribute,
2758 GFileQueryInfoFlags flags,
2759 GCancellable *cancellable,
2762 GFileAttributeValue v;
2764 v.type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
2765 v.u.string = (char *)value;
2766 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2770 * g_file_set_attribute_uint32:
2771 * @file: input #GFile.
2772 * @attribute: a string containing the attribute's name.
2773 * @value: a #guint32 containing the attribute's new value.
2774 * @flags: a #GFileQueryInfoFlags.
2775 * @cancellable: optional #GCancellable object, %NULL to ignore.
2776 * @error: a #GError, or %NULL
2778 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
2779 * If @attribute is of a different type, this operation will fail.
2781 * If @cancellable is not %NULL, then the operation can be cancelled by
2782 * triggering the cancellable object from another thread. If the operation
2783 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2785 * Returns: %TRUE if the @attribute was successfully set to @value
2786 * in the @file, %FALSE otherwise.
2789 g_file_set_attribute_uint32 (GFile *file,
2790 const char *attribute,
2792 GFileQueryInfoFlags flags,
2793 GCancellable *cancellable,
2796 GFileAttributeValue v;
2798 v.type = G_FILE_ATTRIBUTE_TYPE_UINT32;
2800 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2804 * g_file_set_attribute_int32:
2805 * @file: input #GFile.
2806 * @attribute: a string containing the attribute's name.
2807 * @value: a #gint32 containing the attribute's new value.
2808 * @flags: a #GFileQueryInfoFlags.
2809 * @cancellable: optional #GCancellable object, %NULL to ignore.
2810 * @error: a #GError, or %NULL
2812 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
2813 * If @attribute is of a different type, this operation will fail.
2815 * If @cancellable is not %NULL, then the operation can be cancelled by
2816 * triggering the cancellable object from another thread. If the operation
2817 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2819 * Returns: %TRUE if the @attribute was successfully set to @value
2820 * in the @file, %FALSE otherwise.
2823 g_file_set_attribute_int32 (GFile *file,
2824 const char *attribute,
2826 GFileQueryInfoFlags flags,
2827 GCancellable *cancellable,
2830 GFileAttributeValue v;
2832 v.type = G_FILE_ATTRIBUTE_TYPE_INT32;
2834 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2838 * g_file_set_attribute_uint64:
2839 * @file: input #GFile.
2840 * @attribute: a string containing the attribute's name.
2841 * @value: a #guint64 containing the attribute's new value.
2842 * @flags: a #GFileQueryInfoFlags.
2843 * @cancellable: optional #GCancellable object, %NULL to ignore.
2844 * @error: a #GError, or %NULL
2846 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
2847 * If @attribute is of a different type, this operation will fail.
2849 * If @cancellable is not %NULL, then the operation can be cancelled by
2850 * triggering the cancellable object from another thread. If the operation
2851 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2853 * Returns: %TRUE if the @attribute was successfully set to @value
2854 * in the @file, %FALSE otherwise.
2857 g_file_set_attribute_uint64 (GFile *file,
2858 const char *attribute,
2860 GFileQueryInfoFlags flags,
2861 GCancellable *cancellable,
2864 GFileAttributeValue v;
2866 v.type = G_FILE_ATTRIBUTE_TYPE_UINT64;
2868 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2872 * g_file_set_attribute_int64:
2873 * @file: input #GFile.
2874 * @attribute: a string containing the attribute's name.
2875 * @value: a #guint64 containing the attribute's new value.
2876 * @flags: a #GFileQueryInfoFlags.
2877 * @cancellable: optional #GCancellable object, %NULL to ignore.
2878 * @error: a #GError, or %NULL
2880 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
2881 * If @attribute is of a different type, this operation will fail.
2883 * If @cancellable is not %NULL, then the operation can be cancelled by
2884 * triggering the cancellable object from another thread. If the operation
2885 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2887 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2890 g_file_set_attribute_int64 (GFile *file,
2891 const char *attribute,
2893 GFileQueryInfoFlags flags,
2894 GCancellable *cancellable,
2897 GFileAttributeValue v;
2899 v.type = G_FILE_ATTRIBUTE_TYPE_INT64;
2901 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2905 * g_file_mount_mountable:
2906 * @file: input #GFile.
2907 * @mount_operation: a #GMountOperation.
2908 * @cancellable: optional #GCancellable object, %NULL to ignore.
2909 * @callback: a #GAsyncReadyCallback.
2910 * @user_data: a #gpointer.
2912 * Mounts a mountable file using @mount_operation, if possible.
2914 * If @cancellable is not %NULL, then the operation can be cancelled by
2915 * triggering the cancellable object from another thread. If the operation
2916 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2919 g_file_mount_mountable (GFile *file,
2920 GMountOperation *mount_operation,
2921 GCancellable *cancellable,
2922 GAsyncReadyCallback callback,
2927 g_return_if_fail (G_IS_FILE (file));
2928 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
2930 iface = G_FILE_GET_IFACE (file);
2932 if (iface->mount_mountable == NULL)
2933 g_simple_async_report_error_in_idle (G_OBJECT (file),
2937 G_IO_ERROR_NOT_SUPPORTED,
2938 _("Operation not supported"));
2940 (* iface->mount_mountable) (file,
2948 * g_file_mount_mountable_finish:
2949 * @file: input #GFile.
2950 * @result: a #GAsyncResult.
2951 * @error: a #GError, or %NULL
2953 * Finishes a mount operation. See g_file_mount_mountable() for details.
2955 * Finish an asynchronous mount operation that was started
2956 * with g_file_mount_mountable().
2958 * Returns: a #GFile or %NULL on error.
2961 g_file_mount_mountable_finish (GFile *file,
2962 GAsyncResult *result,
2967 g_return_val_if_fail (G_IS_FILE (file), NULL);
2968 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
2970 if (G_IS_SIMPLE_ASYNC_RESULT (result))
2972 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
2973 if (g_simple_async_result_propagate_error (simple, error))
2977 iface = G_FILE_GET_IFACE (file);
2978 return (* iface->mount_mountable_finish) (file, result, error);
2982 * g_file_unmount_mountable:
2983 * @file: input #GFile.
2984 * @cancellable: optional #GCancellable object, %NULL to ignore.
2985 * @callback: a #GAsyncReadyCallback.
2986 * @user_data: a #gpointer.
2988 * Starts an asynchronous unmount operation.
2990 * Unmounts a mounted file.
2992 * If @cancellable is not %NULL, then the operation can be cancelled by
2993 * triggering the cancellable object from another thread. If the operation
2994 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2997 g_file_unmount_mountable (GFile *file,
2998 GCancellable *cancellable,
2999 GAsyncReadyCallback callback,
3004 g_return_if_fail (G_IS_FILE (file));
3006 iface = G_FILE_GET_IFACE (file);
3008 if (iface->unmount_mountable == NULL)
3009 g_simple_async_report_error_in_idle (G_OBJECT (file),
3013 G_IO_ERROR_NOT_SUPPORTED,
3014 _("Operation not supported"));
3016 (* iface->unmount_mountable) (file,
3023 * g_file_unmount_mountable_finish:
3024 * @file: input #GFile.
3025 * @result: a #GAsyncResult.
3026 * @error: a #GError, or %NULL
3028 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3030 * Finish an asynchronous unmount operation that was started
3031 * with g_file_unmount_mountable().
3033 * Returns: %TRUE if the operation finished successfully. %FALSE
3037 g_file_unmount_mountable_finish (GFile *file,
3038 GAsyncResult *result,
3043 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3044 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3046 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3048 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3049 if (g_simple_async_result_propagate_error (simple, error))
3053 iface = G_FILE_GET_IFACE (file);
3054 return (* iface->unmount_mountable_finish) (file, result, error);
3058 * g_file_eject_mountable:
3059 * @file: input #GFile.
3060 * @cancellable: optional #GCancellable object, %NULL to ignore.
3061 * @callback: a #GAsyncReadyCallback.
3062 * @user_data: a #gpointer.
3064 * Starts an asynchronous eject on a mountable.
3065 * When this operation has completed, @callback will be called with
3066 * @user_user data, and the operation can be finalized with
3067 * g_file_eject_mountable_finish().
3069 * If @cancellable is not %NULL, then the operation can be cancelled by
3070 * triggering the cancellable object from another thread. If the operation
3071 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3074 g_file_eject_mountable (GFile *file,
3075 GCancellable *cancellable,
3076 GAsyncReadyCallback callback,
3081 g_return_if_fail (G_IS_FILE (file));
3083 iface = G_FILE_GET_IFACE (file);
3085 if (iface->eject_mountable == NULL)
3086 g_simple_async_report_error_in_idle (G_OBJECT (file),
3090 G_IO_ERROR_NOT_SUPPORTED,
3091 _("Operation not supported"));
3093 (* iface->eject_mountable) (file,
3100 * g_file_eject_mountable_finish:
3101 * @file: input #GFile.
3102 * @result: a #GAsyncResult.
3103 * @error: a #GError, or %NULL
3105 * Finishes an asynchronous eject operation started by
3106 * g_file_eject_mountable().
3108 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3112 g_file_eject_mountable_finish (GFile *file,
3113 GAsyncResult *result,
3118 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3119 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3121 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3123 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3124 if (g_simple_async_result_propagate_error (simple, error))
3128 iface = G_FILE_GET_IFACE (file);
3129 return (* iface->eject_mountable_finish) (file, result, error);
3133 * g_file_monitor_directory:
3134 * @file: input #GFile.
3135 * @flags: a set of #GFileMonitorFlags.
3136 * @cancellable: optional #GCancellable object, %NULL to ignore.
3138 * Obtains a directory monitor for the given file.
3140 * If @cancellable is not %NULL, then the operation can be cancelled by
3141 * triggering the cancellable object from another thread. If the operation
3142 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3144 * Returns: a #GDirectoryMonitor for the given @file,
3145 * or %NULL on error.
3148 g_file_monitor_directory (GFile *file,
3149 GFileMonitorFlags flags,
3150 GCancellable *cancellable)
3154 g_return_val_if_fail (G_IS_FILE (file), NULL);
3156 iface = G_FILE_GET_IFACE (file);
3158 if (iface->monitor_dir == NULL)
3161 return (* iface->monitor_dir) (file, flags, cancellable);
3165 * g_file_monitor_file:
3166 * @file: input #GFile.
3167 * @flags: a set of #GFileMonitorFlags.
3168 * @cancellable: optional #GCancellable object, %NULL to ignore.
3170 * Obtains a file monitor for the given file.
3172 * If @cancellable is not %NULL, then the operation can be cancelled by
3173 * triggering the cancellable object from another thread. If the operation
3174 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3176 * Returns: a #GFileMonitor for the given @file,
3177 * or %NULL on error.
3180 g_file_monitor_file (GFile *file,
3181 GFileMonitorFlags flags,
3182 GCancellable *cancellable)
3185 GFileMonitor *monitor;
3187 g_return_val_if_fail (G_IS_FILE (file), NULL);
3189 iface = G_FILE_GET_IFACE (file);
3193 if (iface->monitor_file)
3194 monitor = (* iface->monitor_file) (file, flags, cancellable);
3196 /* Fallback to polling */
3197 if (monitor == NULL)
3198 monitor = _g_poll_file_monitor_new (file);
3203 /********************************************
3204 * Default implementation of async ops *
3205 ********************************************/
3209 GFileQueryInfoFlags flags;
3211 } QueryInfoAsyncData;
3214 query_info_data_free (QueryInfoAsyncData *data)
3217 g_object_unref (data->info);
3218 g_free (data->attributes);
3223 query_info_async_thread (GSimpleAsyncResult *res,
3225 GCancellable *cancellable)
3227 GError *error = NULL;
3228 QueryInfoAsyncData *data;
3231 data = g_simple_async_result_get_op_res_gpointer (res);
3233 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3237 g_simple_async_result_set_from_error (res, error);
3238 g_error_free (error);
3245 g_file_real_query_info_async (GFile *file,
3246 const char *attributes,
3247 GFileQueryInfoFlags flags,
3249 GCancellable *cancellable,
3250 GAsyncReadyCallback callback,
3253 GSimpleAsyncResult *res;
3254 QueryInfoAsyncData *data;
3256 data = g_new0 (QueryInfoAsyncData, 1);
3257 data->attributes = g_strdup (attributes);
3258 data->flags = flags;
3260 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3261 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3263 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3264 g_object_unref (res);
3268 g_file_real_query_info_finish (GFile *file,
3272 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3273 QueryInfoAsyncData *data;
3275 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3277 data = g_simple_async_result_get_op_res_gpointer (simple);
3279 return g_object_ref (data->info);
3286 GFileQueryInfoFlags flags;
3287 GFileEnumerator *enumerator;
3288 } EnumerateChildrenAsyncData;
3291 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3293 if (data->enumerator)
3294 g_object_unref (data->enumerator);
3295 g_free (data->attributes);
3300 enumerate_children_async_thread (GSimpleAsyncResult *res,
3302 GCancellable *cancellable)
3304 GError *error = NULL;
3305 EnumerateChildrenAsyncData *data;
3306 GFileEnumerator *enumerator;
3308 data = g_simple_async_result_get_op_res_gpointer (res);
3310 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3312 if (enumerator == NULL)
3314 g_simple_async_result_set_from_error (res, error);
3315 g_error_free (error);
3318 data->enumerator = enumerator;
3322 g_file_real_enumerate_children_async (GFile *file,
3323 const char *attributes,
3324 GFileQueryInfoFlags flags,
3326 GCancellable *cancellable,
3327 GAsyncReadyCallback callback,
3330 GSimpleAsyncResult *res;
3331 EnumerateChildrenAsyncData *data;
3333 data = g_new0 (EnumerateChildrenAsyncData, 1);
3334 data->attributes = g_strdup (attributes);
3335 data->flags = flags;
3337 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3338 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3340 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3341 g_object_unref (res);
3344 static GFileEnumerator *
3345 g_file_real_enumerate_children_finish (GFile *file,
3349 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3350 EnumerateChildrenAsyncData *data;
3352 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3354 data = g_simple_async_result_get_op_res_gpointer (simple);
3355 if (data->enumerator)
3356 return g_object_ref (data->enumerator);
3362 open_read_async_thread (GSimpleAsyncResult *res,
3364 GCancellable *cancellable)
3367 GFileInputStream *stream;
3368 GError *error = NULL;
3370 iface = G_FILE_GET_IFACE (object);
3372 stream = iface->read (G_FILE (object), cancellable, &error);
3376 g_simple_async_result_set_from_error (res, error);
3377 g_error_free (error);
3380 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3384 g_file_real_read_async (GFile *file,
3386 GCancellable *cancellable,
3387 GAsyncReadyCallback callback,
3390 GSimpleAsyncResult *res;
3392 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3394 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3395 g_object_unref (res);
3398 static GFileInputStream *
3399 g_file_real_read_finish (GFile *file,
3403 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3406 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3408 op = g_simple_async_result_get_op_res_gpointer (simple);
3410 return g_object_ref (op);
3416 append_to_async_thread (GSimpleAsyncResult *res,
3418 GCancellable *cancellable)
3421 GFileCreateFlags *data;
3422 GFileOutputStream *stream;
3423 GError *error = NULL;
3425 iface = G_FILE_GET_IFACE (object);
3427 data = g_simple_async_result_get_op_res_gpointer (res);
3429 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3433 g_simple_async_result_set_from_error (res, error);
3434 g_error_free (error);
3437 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3441 g_file_real_append_to_async (GFile *file,
3442 GFileCreateFlags flags,
3444 GCancellable *cancellable,
3445 GAsyncReadyCallback callback,
3448 GFileCreateFlags *data;
3449 GSimpleAsyncResult *res;
3451 data = g_new0 (GFileCreateFlags, 1);
3454 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3455 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3457 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3458 g_object_unref (res);
3461 static GFileOutputStream *
3462 g_file_real_append_to_finish (GFile *file,
3466 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3469 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3471 op = g_simple_async_result_get_op_res_gpointer (simple);
3473 return g_object_ref (op);
3479 create_async_thread (GSimpleAsyncResult *res,
3481 GCancellable *cancellable)
3484 GFileCreateFlags *data;
3485 GFileOutputStream *stream;
3486 GError *error = NULL;
3488 iface = G_FILE_GET_IFACE (object);
3490 data = g_simple_async_result_get_op_res_gpointer (res);
3492 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3496 g_simple_async_result_set_from_error (res, error);
3497 g_error_free (error);
3500 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3504 g_file_real_create_async (GFile *file,
3505 GFileCreateFlags flags,
3507 GCancellable *cancellable,
3508 GAsyncReadyCallback callback,
3511 GFileCreateFlags *data;
3512 GSimpleAsyncResult *res;
3514 data = g_new0 (GFileCreateFlags, 1);
3517 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3518 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3520 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3521 g_object_unref (res);
3524 static GFileOutputStream *
3525 g_file_real_create_finish (GFile *file,
3529 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3532 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3534 op = g_simple_async_result_get_op_res_gpointer (simple);
3536 return g_object_ref (op);
3542 GFileOutputStream *stream;
3544 gboolean make_backup;
3545 GFileCreateFlags flags;
3549 replace_async_data_free (ReplaceAsyncData *data)
3552 g_object_unref (data->stream);
3553 g_free (data->etag);
3558 replace_async_thread (GSimpleAsyncResult *res,
3560 GCancellable *cancellable)
3563 GFileOutputStream *stream;
3564 GError *error = NULL;
3565 ReplaceAsyncData *data;
3567 iface = G_FILE_GET_IFACE (object);
3569 data = g_simple_async_result_get_op_res_gpointer (res);
3571 stream = iface->replace (G_FILE (object),
3580 g_simple_async_result_set_from_error (res, error);
3581 g_error_free (error);
3584 data->stream = stream;
3588 g_file_real_replace_async (GFile *file,
3590 gboolean make_backup,
3591 GFileCreateFlags flags,
3593 GCancellable *cancellable,
3594 GAsyncReadyCallback callback,
3597 GSimpleAsyncResult *res;
3598 ReplaceAsyncData *data;
3600 data = g_new0 (ReplaceAsyncData, 1);
3601 data->etag = g_strdup (etag);
3602 data->make_backup = make_backup;
3603 data->flags = flags;
3605 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3606 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3608 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3609 g_object_unref (res);
3612 static GFileOutputStream *
3613 g_file_real_replace_finish (GFile *file,
3617 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3618 ReplaceAsyncData *data;
3620 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3622 data = g_simple_async_result_get_op_res_gpointer (simple);
3624 return g_object_ref (data->stream);
3632 } SetDisplayNameAsyncData;
3635 set_display_name_data_free (SetDisplayNameAsyncData *data)
3637 g_free (data->name);
3639 g_object_unref (data->file);
3644 set_display_name_async_thread (GSimpleAsyncResult *res,
3646 GCancellable *cancellable)
3648 GError *error = NULL;
3649 SetDisplayNameAsyncData *data;
3652 data = g_simple_async_result_get_op_res_gpointer (res);
3654 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3658 g_simple_async_result_set_from_error (res, error);
3659 g_error_free (error);
3666 g_file_real_set_display_name_async (GFile *file,
3667 const char *display_name,
3669 GCancellable *cancellable,
3670 GAsyncReadyCallback callback,
3673 GSimpleAsyncResult *res;
3674 SetDisplayNameAsyncData *data;
3676 data = g_new0 (SetDisplayNameAsyncData, 1);
3677 data->name = g_strdup (display_name);
3679 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3680 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3682 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3683 g_object_unref (res);
3687 g_file_real_set_display_name_finish (GFile *file,
3691 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3692 SetDisplayNameAsyncData *data;
3694 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3696 data = g_simple_async_result_get_op_res_gpointer (simple);
3698 return g_object_ref (data->file);
3704 GFileQueryInfoFlags flags;
3711 set_info_data_free (SetInfoAsyncData *data)
3714 g_object_unref (data->info);
3716 g_error_free (data->error);
3721 set_info_async_thread (GSimpleAsyncResult *res,
3723 GCancellable *cancellable)
3725 SetInfoAsyncData *data;
3727 data = g_simple_async_result_get_op_res_gpointer (res);
3730 data->res = g_file_set_attributes_from_info (G_FILE (object),
3738 g_file_real_set_attributes_async (GFile *file,
3740 GFileQueryInfoFlags flags,
3742 GCancellable *cancellable,
3743 GAsyncReadyCallback callback,
3746 GSimpleAsyncResult *res;
3747 SetInfoAsyncData *data;
3749 data = g_new0 (SetInfoAsyncData, 1);
3750 data->info = g_file_info_dup (info);
3751 data->flags = flags;
3753 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
3754 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
3756 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
3757 g_object_unref (res);
3761 g_file_real_set_attributes_finish (GFile *file,
3766 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3767 SetInfoAsyncData *data;
3769 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
3771 data = g_simple_async_result_get_op_res_gpointer (simple);
3774 *info = g_object_ref (data->info);
3776 if (error != NULL && data->error)
3777 *error = g_error_copy (data->error);
3782 /********************************************
3783 * Default VFS operations *
3784 ********************************************/
3787 * g_file_new_for_path:
3788 * @path: a string containing a relative or absolute path.
3790 * Constructs a #GFile for a given path. This operation never
3791 * fails, but the returned object might not support any I/O
3792 * operation if @path is malformed.
3794 * Returns: a new #GFile for the given @path.
3797 g_file_new_for_path (const char *path)
3799 g_return_val_if_fail (path != NULL, NULL);
3801 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
3805 * g_file_new_for_uri:
3806 * @uri: a string containing a URI.
3808 * Constructs a #GFile for a given URI. This operation never
3809 * fails, but the returned object might not support any I/O
3810 * operation if @uri is malformed or if the uri type is
3813 * Returns: a #GFile for the given @uri.
3816 g_file_new_for_uri (const char *uri)
3818 g_return_val_if_fail (uri != NULL, NULL);
3820 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
3824 * g_file_parse_name:
3825 * @parse_name: a file name or path to be parsed.
3827 * Constructs a #GFile with the given @parse_name,
3828 * looked up by #GVfs. This operation never fails,
3829 * but the returned object might not support any I/O
3830 * operation if the @parse_name cannot be parsed by #GVfs.
3832 * Returns: a new #GFile.
3835 g_file_parse_name (const char *parse_name)
3837 g_return_val_if_fail (parse_name != NULL, NULL);
3839 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
3843 is_valid_scheme_character (char c)
3845 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
3849 has_valid_scheme (const char *uri)
3855 if (!is_valid_scheme_character (*p))
3860 } while (is_valid_scheme_character (*p));
3866 * g_file_new_for_commandline_arg:
3867 * @arg: a command line string.
3869 * Attempts to generate a #GFile with the given line from
3870 * the command line argument.
3872 * Returns: a new #GFile.
3875 g_file_new_for_commandline_arg (const char *arg)
3881 g_return_val_if_fail (arg != NULL, NULL);
3883 if (g_path_is_absolute (arg))
3884 return g_file_new_for_path (arg);
3886 if (has_valid_scheme (arg))
3887 return g_file_new_for_uri (arg);
3889 current_dir = g_get_current_dir ();
3890 filename = g_build_filename (current_dir, arg, NULL);
3891 g_free (current_dir);
3893 file = g_file_new_for_path (filename);
3900 * g_mount_for_location:
3901 * @location: input #GFile.
3902 * @mount_operation: a #GMountOperation.
3903 * @cancellable: optional #GCancellable object, %NULL to ignore.
3904 * @callback: a #GAsyncReadyCallback.
3905 * @user_data: a #gpointer.
3907 * Starts the @mount_operation, mounting the volume at @location.
3909 * When this operation has completed, @callback will be called with
3910 * @user_user data, and the operation can be finalized with
3911 * g_mount_for_location_finish().
3913 * If @cancellable is not %NULL, then the operation can be cancelled by
3914 * triggering the cancellable object from another thread. If the operation
3915 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3918 g_mount_for_location (GFile *location,
3919 GMountOperation *mount_operation,
3920 GCancellable *cancellable,
3921 GAsyncReadyCallback callback,
3926 g_return_if_fail (G_IS_FILE (location));
3927 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
3929 iface = G_FILE_GET_IFACE (location);
3931 if (iface->mount_for_location == NULL)
3933 g_simple_async_report_error_in_idle (G_OBJECT (location),
3934 callback, user_data,
3935 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3936 _("volume doesn't implement mount"));
3941 (* iface->mount_for_location) (location, mount_operation, cancellable, callback, user_data);
3946 * g_mount_for_location_finish:
3947 * @location: input #GFile.
3948 * @result: a #GAsyncResult.
3949 * @error: a #GError, or %NULL
3951 * Finishes a mount operation started by g_mount_for_location().
3953 * Returns: %TRUE if successful. If an error
3954 * has occured, this function will return %FALSE and set @error
3955 * appropriately if present.
3958 g_mount_for_location_finish (GFile *location,
3959 GAsyncResult *result,
3964 g_return_val_if_fail (G_IS_FILE (location), FALSE);
3965 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3967 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3969 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3970 if (g_simple_async_result_propagate_error (simple, error))
3974 iface = G_FILE_GET_IFACE (location);
3976 return (* iface->mount_for_location_finish) (location, result, error);
3979 /********************************************
3980 * Utility functions *
3981 ********************************************/
3983 #define GET_CONTENT_BLOCK_SIZE 8192
3986 * g_file_load_contents:
3987 * @file: input #GFile.
3988 * @cancellable: optional #GCancellable object, %NULL to ignore.
3989 * @contents: a location to place the contents of the file.
3990 * @length: a location to place the length of the contents of the file.
3991 * @etag_out: a location to place the current entity tag for the file.
3992 * @error: a #GError, or %NULL
3994 * If @cancellable is not %NULL, then the operation can be cancelled by
3995 * triggering the cancellable object from another thread. If the operation
3996 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3998 * Returns: %TRUE if the @file's contents were successfully loaded.
3999 * %FALSE if there were errors. The length of the loaded data will be
4000 * put into @length, the contents in @contents.
4003 g_file_load_contents (GFile *file,
4004 GCancellable *cancellable,
4010 GFileInputStream *in;
4011 GByteArray *content;
4016 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4017 g_return_val_if_fail (contents != NULL, FALSE);
4019 in = g_file_read (file, cancellable, error);
4023 content = g_byte_array_new ();
4026 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4027 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4028 content->data + pos,
4029 GET_CONTENT_BLOCK_SIZE,
4030 cancellable, error)) > 0)
4033 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4040 info = g_file_input_stream_query_info (in,
4041 G_FILE_ATTRIBUTE_ETAG_VALUE,
4046 *etag_out = g_strdup (g_file_info_get_etag (info));
4047 g_object_unref (info);
4051 /* Ignore errors on close */
4052 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4053 g_object_unref (in);
4057 /* error is set already */
4058 g_byte_array_free (content, TRUE);
4065 /* Zero terminate (we got an extra byte allocated for this */
4066 content->data[pos] = 0;
4068 *contents = (char *)g_byte_array_free (content, FALSE);
4076 GCancellable *cancellable;
4077 GFileReadMoreCallback read_more_callback;
4078 GAsyncReadyCallback callback;
4080 GByteArray *content;
4087 load_contents_data_free (LoadContentsData *data)
4090 g_error_free (data->error);
4091 if (data->cancellable)
4092 g_object_unref (data->cancellable);
4094 g_byte_array_free (data->content, TRUE);
4095 g_free (data->etag);
4096 g_object_unref (data->file);
4101 load_contents_close_callback (GObject *obj,
4102 GAsyncResult *close_res,
4105 GInputStream *stream = G_INPUT_STREAM (obj);
4106 LoadContentsData *data = user_data;
4107 GSimpleAsyncResult *res;
4109 /* Ignore errors here, we're only reading anyway */
4110 g_input_stream_close_finish (stream, close_res, NULL);
4111 g_object_unref (stream);
4113 res = g_simple_async_result_new (G_OBJECT (data->file),
4116 g_file_load_contents_async);
4117 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4118 g_simple_async_result_complete (res);
4119 g_object_unref (res);
4123 load_contents_fstat_callback (GObject *obj,
4124 GAsyncResult *stat_res,
4127 GInputStream *stream = G_INPUT_STREAM (obj);
4128 LoadContentsData *data = user_data;
4131 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4135 data->etag = g_strdup (g_file_info_get_etag (info));
4136 g_object_unref (info);
4139 g_input_stream_close_async (stream, 0,
4141 load_contents_close_callback, data);
4145 load_contents_read_callback (GObject *obj,
4146 GAsyncResult *read_res,
4149 GInputStream *stream = G_INPUT_STREAM (obj);
4150 LoadContentsData *data = user_data;
4151 GError *error = NULL;
4154 read_size = g_input_stream_read_finish (stream, read_res, &error);
4158 /* Error or EOF, close the file */
4159 data->error = error;
4160 g_input_stream_close_async (stream, 0,
4162 load_contents_close_callback, data);
4164 else if (read_size == 0)
4166 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4167 G_FILE_ATTRIBUTE_ETAG_VALUE,
4170 load_contents_fstat_callback,
4173 else if (read_size > 0)
4175 data->pos += read_size;
4177 g_byte_array_set_size (data->content,
4178 data->pos + GET_CONTENT_BLOCK_SIZE);
4181 if (data->read_more_callback &&
4182 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4183 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4184 G_FILE_ATTRIBUTE_ETAG_VALUE,
4187 load_contents_fstat_callback,
4190 g_input_stream_read_async (stream,
4191 data->content->data + data->pos,
4192 GET_CONTENT_BLOCK_SIZE,
4195 load_contents_read_callback,
4201 load_contents_open_callback (GObject *obj,
4202 GAsyncResult *open_res,
4205 GFile *file = G_FILE (obj);
4206 GFileInputStream *stream;
4207 LoadContentsData *data = user_data;
4208 GError *error = NULL;
4209 GSimpleAsyncResult *res;
4211 stream = g_file_read_finish (file, open_res, &error);
4215 g_byte_array_set_size (data->content,
4216 data->pos + GET_CONTENT_BLOCK_SIZE);
4217 g_input_stream_read_async (G_INPUT_STREAM (stream),
4218 data->content->data + data->pos,
4219 GET_CONTENT_BLOCK_SIZE,
4222 load_contents_read_callback,
4228 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4232 g_simple_async_result_complete (res);
4233 g_error_free (error);
4234 load_contents_data_free (data);
4235 g_object_unref (res);
4240 * g_file_load_partial_contents_async:
4241 * @file: input #GFile.
4242 * @cancellable: optional #GCancellable object, %NULL to ignore.
4243 * @read_more_callback: a #GFileReadMoreCallback.
4244 * @callback: a #GAsyncReadyCallback.
4245 * @user_data: a #gpointer.
4247 * If @cancellable is not %NULL, then the operation can be cancelled by
4248 * triggering the cancellable object from another thread. If the operation
4249 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4252 g_file_load_partial_contents_async (GFile *file,
4253 GCancellable *cancellable,
4254 GFileReadMoreCallback read_more_callback,
4255 GAsyncReadyCallback callback,
4258 LoadContentsData *data;
4260 g_return_if_fail (G_IS_FILE (file));
4262 data = g_new0 (LoadContentsData, 1);
4265 data->cancellable = g_object_ref (cancellable);
4266 data->read_more_callback = read_more_callback;
4267 data->callback = callback;
4268 data->user_data = user_data;
4269 data->content = g_byte_array_new ();
4270 data->file = g_object_ref (file);
4272 g_file_read_async (file,
4275 load_contents_open_callback,
4280 * g_file_load_partial_contents_finish:
4281 * @file: input #GFile.
4282 * @res: a #GAsyncResult.
4283 * @contents: a location to place the contents of the file.
4284 * @length: a location to place the length of the contents of the file.
4285 * @etag_out: a location to place the current entity tag for the file.
4286 * @error: a #GError, or %NULL
4288 * Finishes an asynchronous partial load operation that was started
4289 * with g_file_load_partial_contents_async().
4291 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4292 * present, it will be set appropriately.
4295 g_file_load_partial_contents_finish (GFile *file,
4302 GSimpleAsyncResult *simple;
4303 LoadContentsData *data;
4305 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4306 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4307 g_return_val_if_fail (contents != NULL, FALSE);
4309 simple = G_SIMPLE_ASYNC_RESULT (res);
4311 if (g_simple_async_result_propagate_error (simple, error))
4314 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4316 data = g_simple_async_result_get_op_res_gpointer (simple);
4320 g_propagate_error (error, data->error);
4329 *length = data->pos;
4333 *etag_out = data->etag;
4337 /* Zero terminate */
4338 g_byte_array_set_size (data->content, data->pos + 1);
4339 data->content->data[data->pos] = 0;
4341 *contents = (char *)g_byte_array_free (data->content, FALSE);
4342 data->content = NULL;
4348 * g_file_load_contents_async:
4349 * @file: input #GFile.
4350 * @cancellable: optional #GCancellable object, %NULL to ignore.
4351 * @callback: a #GAsyncReadyCallback.
4352 * @user_data: a #gpointer.
4354 * Starts an asynchronous load of the @file's contents.
4355 * When the load operation has completed, @callback will be called
4356 * with @userdata. To finish the operation, call
4357 * g_file_load_contents_finish() with the #GAsyncResult returned by
4360 * If @cancellable is not %NULL, then the operation can be cancelled by
4361 * triggering the cancellable object from another thread. If the operation
4362 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4365 g_file_load_contents_async (GFile *file,
4366 GCancellable *cancellable,
4367 GAsyncReadyCallback callback,
4370 g_file_load_partial_contents_async (file,
4373 callback, user_data);
4377 * g_file_load_contents_finish:
4378 * @file: input #GFile.
4379 * @res: a #GAsyncResult.
4380 * @contents: a location to place the contents of the file.
4381 * @length: a location to place the length of the contents of the file.
4382 * @etag_out: a location to place the current entity tag for the file.
4383 * @error: a #GError, or %NULL
4385 * Finishes an asynchronous load of the @file's contents.
4386 * The contents are placed in @contents, and @length is set to the
4387 * size of the @contents string. If @etag_out is present, it will be
4388 * set to the new entity tag for the @file.
4390 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4391 * present, it will be set appropriately.
4394 g_file_load_contents_finish (GFile *file,
4401 return g_file_load_partial_contents_finish (file,
4410 * g_file_replace_contents:
4411 * @file: input #GFile.
4412 * @contents: a string containing the new contents for @file.
4413 * @length: the length of @contents in bytes.
4414 * @etag: the old <link linkend="gfile-etag">entity tag</link>
4416 * @make_backup: a #gboolean.
4417 * @flags: a set of #GFileCreateFlags.
4418 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4420 * @cancellable: optional #GCancellable object, %NULL to ignore.
4421 * @error: a #GError, or %NULL
4423 * Replaces the contents of @file with @contents of @length bytes.
4424 * The old @etag will be replaced with the @new_etag. If @make_backup
4425 * is %TRUE, this function will attempt to make a backup of @file.
4427 * If @cancellable is not %NULL, then the operation can be cancelled by
4428 * triggering the cancellable object from another thread. If the operation
4429 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4431 * Returns: %TRUE if successful. If an error
4432 * has occured, this function will return %FALSE and set @error
4433 * appropriately if present.
4436 g_file_replace_contents (GFile *file,
4437 const char *contents,
4440 gboolean make_backup,
4441 GFileCreateFlags flags,
4443 GCancellable *cancellable,
4446 GFileOutputStream *out;
4447 gsize pos, remainder;
4450 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4451 g_return_val_if_fail (contents != NULL, FALSE);
4453 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4459 while (remainder > 0 &&
4460 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4462 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4470 if (remainder > 0 && res < 0)
4472 /* Ignore errors on close */
4473 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4475 /* error is set already */
4479 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4483 *new_etag = g_file_output_stream_get_etag (out);
4491 GCancellable *cancellable;
4492 GAsyncReadyCallback callback;
4494 const char *content;
4498 } ReplaceContentsData;
4501 replace_contents_data_free (ReplaceContentsData *data)
4504 g_error_free (data->error);
4505 if (data->cancellable)
4506 g_object_unref (data->cancellable);
4507 g_object_unref (data->file);
4508 g_free (data->etag);
4513 replace_contents_close_callback (GObject *obj,
4514 GAsyncResult *close_res,
4517 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4518 ReplaceContentsData *data = user_data;
4519 GSimpleAsyncResult *res;
4521 /* Ignore errors here, we're only reading anyway */
4522 g_output_stream_close_finish (stream, close_res, NULL);
4523 g_object_unref (stream);
4525 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4527 res = g_simple_async_result_new (G_OBJECT (data->file),
4530 g_file_replace_contents_async);
4531 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4532 g_simple_async_result_complete (res);
4533 g_object_unref (res);
4537 replace_contents_write_callback (GObject *obj,
4538 GAsyncResult *read_res,
4541 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4542 ReplaceContentsData *data = user_data;
4543 GError *error = NULL;
4546 write_size = g_output_stream_write_finish (stream, read_res, &error);
4548 if (write_size <= 0)
4550 /* Error or EOF, close the file */
4552 data->error = error;
4553 g_output_stream_close_async (stream, 0,
4555 replace_contents_close_callback, data);
4557 else if (write_size > 0)
4559 data->pos += write_size;
4561 if (data->pos >= data->length)
4562 g_output_stream_close_async (stream, 0,
4564 replace_contents_close_callback, data);
4566 g_output_stream_write_async (stream,
4567 data->content + data->pos,
4568 data->length - data->pos,
4571 replace_contents_write_callback,
4577 replace_contents_open_callback (GObject *obj,
4578 GAsyncResult *open_res,
4581 GFile *file = G_FILE (obj);
4582 GFileOutputStream *stream;
4583 ReplaceContentsData *data = user_data;
4584 GError *error = NULL;
4585 GSimpleAsyncResult *res;
4587 stream = g_file_replace_finish (file, open_res, &error);
4591 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4592 data->content + data->pos,
4593 data->length - data->pos,
4596 replace_contents_write_callback,
4602 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4606 g_simple_async_result_complete (res);
4607 g_error_free (error);
4608 replace_contents_data_free (data);
4609 g_object_unref (res);
4614 * g_file_replace_contents_async:
4615 * @file: input #GFile.
4616 * @contents: string of contents to replace the file with.
4617 * @length: the length of @contents in bytes.
4618 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4619 * @make_backup: a #gboolean.
4620 * @flags: a set of #GFileCreateFlags.
4621 * @cancellable: optional #GCancellable object, %NULL to ignore.
4622 * @callback: a #GAsyncReadyCallback.
4623 * @user_data: a #gpointer.
4625 * Starts an asynchronous replacement of @file with the given
4626 * @contents of @length bytes. @etag will replace the document's
4627 * current entity tag.
4629 * When this operation has completed, @callback will be called with
4630 * @user_user data, and the operation can be finalized with
4631 * g_file_replace_contents_finish().
4633 * If @cancellable is not %NULL, then the operation can be cancelled by
4634 * triggering the cancellable object from another thread. If the operation
4635 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4637 * If @make_backup is %TRUE, this function will attempt to
4638 * make a backup of @file.
4641 g_file_replace_contents_async (GFile *file,
4642 const char *contents,
4645 gboolean make_backup,
4646 GFileCreateFlags flags,
4647 GCancellable *cancellable,
4648 GAsyncReadyCallback callback,
4651 ReplaceContentsData *data;
4653 g_return_if_fail (G_IS_FILE (file));
4654 g_return_if_fail (contents != NULL);
4656 data = g_new0 (ReplaceContentsData, 1);
4659 data->cancellable = g_object_ref (cancellable);
4660 data->callback = callback;
4661 data->user_data = user_data;
4662 data->content = contents;
4663 data->length = length;
4665 data->file = g_object_ref (file);
4667 g_file_replace_async (file,
4673 replace_contents_open_callback,
4678 * g_file_replace_contents_finish:
4679 * @file: input #GFile.
4680 * @res: a #GAsyncResult.
4681 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
4683 * @error: a #GError, or %NULL
4685 * Finishes an asynchronous replace of the given @file. See
4686 * g_file_replace_contents_async(). Sets @new_etag to the new entity
4687 * tag for the document, if present.
4689 * Returns: %TRUE on success, %FALSE on failure.
4692 g_file_replace_contents_finish (GFile *file,
4697 GSimpleAsyncResult *simple;
4698 ReplaceContentsData *data;
4700 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4701 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4703 simple = G_SIMPLE_ASYNC_RESULT (res);
4705 if (g_simple_async_result_propagate_error (simple, error))
4708 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
4710 data = g_simple_async_result_get_op_res_gpointer (simple);
4714 g_propagate_error (error, data->error);
4722 *new_etag = data->etag;
4723 data->etag = NULL; /* Take ownership */
4729 #define __G_FILE_C__
4730 #include "gioaliasdef.c"