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 streams (see #GInputStream and #GOutputStream).
51 * To construct a #GFile, you can use:
52 * g_file_new_for_path() if you have a path.
53 * g_file_new_for_uri() if you have a URI.
55 * You can move through the filesystem with #GFile handles with
56 * g_file_get_parent() to get a handle to the parent directory.
57 * g_file_get_child() to get a handle to a child within a directory.
58 * g_file_resolve_relative_path() to resolve a relative path between
59 * two #GFile<!-- -->s.
61 * Many #GFile operations have both synchronous (or blocking) and
62 * asynchronous (non-blocking) versions to suit your application.
63 * Asynchronous versions of synchronous functions simply have _async()
64 * appended to their function names. The asynchronous I/O functions
65 * call a #GAsyncReadyCallback which is then used to finalize the operation,
66 * which is then passed to the function's matching _finish() operation.
72 static void g_file_base_init (gpointer g_class);
73 static void g_file_class_init (gpointer g_class,
76 static void g_file_real_query_info_async (GFile *file,
77 const char *attributes,
78 GFileQueryInfoFlags flags,
80 GCancellable *cancellable,
81 GAsyncReadyCallback callback,
83 static GFileInfo * g_file_real_query_info_finish (GFile *file,
86 static void g_file_real_enumerate_children_async (GFile *file,
87 const char *attributes,
88 GFileQueryInfoFlags flags,
90 GCancellable *cancellable,
91 GAsyncReadyCallback callback,
93 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
96 static void g_file_real_read_async (GFile *file,
98 GCancellable *cancellable,
99 GAsyncReadyCallback callback,
101 static GFileInputStream * g_file_real_read_finish (GFile *file,
104 static void g_file_real_append_to_async (GFile *file,
105 GFileCreateFlags flags,
107 GCancellable *cancellable,
108 GAsyncReadyCallback callback,
110 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
113 static void g_file_real_create_async (GFile *file,
114 GFileCreateFlags flags,
116 GCancellable *cancellable,
117 GAsyncReadyCallback callback,
119 static GFileOutputStream *g_file_real_create_finish (GFile *file,
122 static void g_file_real_replace_async (GFile *file,
124 gboolean make_backup,
125 GFileCreateFlags flags,
127 GCancellable *cancellable,
128 GAsyncReadyCallback callback,
130 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
133 static gboolean g_file_real_set_attributes_from_info (GFile *file,
135 GFileQueryInfoFlags flags,
136 GCancellable *cancellable,
138 static void g_file_real_set_display_name_async (GFile *file,
139 const char *display_name,
141 GCancellable *cancellable,
142 GAsyncReadyCallback callback,
144 static GFile * g_file_real_set_display_name_finish (GFile *file,
147 static void g_file_real_set_attributes_async (GFile *file,
149 GFileQueryInfoFlags flags,
151 GCancellable *cancellable,
152 GAsyncReadyCallback callback,
154 static gboolean g_file_real_set_attributes_finish (GFile *file,
160 g_file_get_type (void)
162 static GType file_type = 0;
166 static const GTypeInfo file_info =
168 sizeof (GFileIface), /* class_size */
169 g_file_base_init, /* base_init */
170 NULL, /* base_finalize */
172 NULL, /* class_finalize */
173 NULL, /* class_data */
180 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
183 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
190 g_file_class_init (gpointer g_class,
193 GFileIface *iface = g_class;
195 iface->enumerate_children_async = g_file_real_enumerate_children_async;
196 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
197 iface->set_display_name_async = g_file_real_set_display_name_async;
198 iface->set_display_name_finish = g_file_real_set_display_name_finish;
199 iface->query_info_async = g_file_real_query_info_async;
200 iface->query_info_finish = g_file_real_query_info_finish;
201 iface->set_attributes_async = g_file_real_set_attributes_async;
202 iface->set_attributes_finish = g_file_real_set_attributes_finish;
203 iface->read_async = g_file_real_read_async;
204 iface->read_finish = g_file_real_read_finish;
205 iface->append_to_async = g_file_real_append_to_async;
206 iface->append_to_finish = g_file_real_append_to_finish;
207 iface->create_async = g_file_real_create_async;
208 iface->create_finish = g_file_real_create_finish;
209 iface->replace_async = g_file_real_replace_async;
210 iface->replace_finish = g_file_real_replace_finish;
211 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
215 g_file_base_init (gpointer g_class)
222 * @file: input #GFile.
224 * Checks to see if a file is native to the system.
226 * Returns: %TRUE if file is native. (If the file is native
227 * to the system, e.g. located in file:///).
230 g_file_is_native (GFile *file)
234 g_return_val_if_fail (G_IS_FILE (file), FALSE);
236 iface = G_FILE_GET_IFACE (file);
238 return (* iface->is_native) (file);
243 * g_file_has_uri_scheme:
244 * @file: input #GFile.
245 * @uri_scheme: a string containing a URI scheme.
247 * Checks to see if a #GFile has a given URI scheme.
249 * Returns: %TRUE if #GFile's backend supports the
250 * given URI scheme, FALSE if URI scheme is %NULL,
251 * not supported, or #GFile is invalid.
254 g_file_has_uri_scheme (GFile *file,
255 const char *uri_scheme)
259 g_return_val_if_fail (G_IS_FILE (file), FALSE);
260 g_return_val_if_fail (uri_scheme != NULL, FALSE);
262 iface = G_FILE_GET_IFACE (file);
264 return (* iface->has_uri_scheme) (file, uri_scheme);
269 * g_file_get_uri_scheme:
270 * @file: input #GFile.
272 * Gets the URI scheme for a #GFile.
273 * RFC 3986 decodes the scheme as:
275 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
277 * Common schemes include "file", "http", "svn", etc.
279 * Returns: a string containing the URI scheme for the given
280 * #GFile. The returned string should be freed with g_free()
281 * when no longer needed.
284 g_file_get_uri_scheme (GFile *file)
288 g_return_val_if_fail (G_IS_FILE (file), NULL);
290 iface = G_FILE_GET_IFACE (file);
292 return (* iface->get_uri_scheme) (file);
297 * g_file_get_basename:
298 * @file: input #GFile.
300 * Gets the basename for a given #GFile.
302 * Returns: string containing the #GFile's base name, or %NULL
303 * if given #GFile is invalid. The returned string should be
304 * freed with g_free() when no longer needed.
307 g_file_get_basename (GFile *file)
311 g_return_val_if_fail (G_IS_FILE (file), NULL);
313 iface = G_FILE_GET_IFACE (file);
315 return (* iface->get_basename) (file);
320 * @file: input #GFile.
322 * Gets the current path within a #GFile.
324 * Returns: string containing the #GFile's path, or %NULL if
325 * given #GFile is invalid. The returned string should be
326 * freed with g_free() when no longer needed.
329 g_file_get_path (GFile *file)
333 g_return_val_if_fail (G_IS_FILE (file), NULL);
335 iface = G_FILE_GET_IFACE (file);
337 return (* iface->get_path) (file);
342 * @file: input #GFile.
344 * Gets a URI for the path within a #GFile.
346 * Returns: a string containing the #GFile's URI or %NULL
347 * if given #GFile is invalid. The returned string should
348 * be freed with g_free() when no longer needed.
351 g_file_get_uri (GFile *file)
355 g_return_val_if_fail (G_IS_FILE (file), NULL);
357 iface = G_FILE_GET_IFACE (file);
359 return (* iface->get_uri) (file);
363 * g_file_get_parse_name:
364 * @file: input #GFile.
366 * Gets the parsed name for the #GFile.
368 * Returns: a string containing the #GFile's parsed name,
369 * or %NULL if given #GFile is invalid. The returned
370 * string should be freed with g_free() when no longer needed.
373 g_file_get_parse_name (GFile *file)
377 g_return_val_if_fail (G_IS_FILE (file), NULL);
379 iface = G_FILE_GET_IFACE (file);
381 return (* iface->get_parse_name) (file);
386 * @file: input #GFile.
388 * Duplicates a #GFile handle. This operation does not duplicate
389 * the actual file or directory represented by the #GFile; see
390 * g_file_copy() if attempting to copy a file.
392 * Returns: #GFile that is a duplicate of the given #GFile,
393 * or %NULL if given #GFile is invalid.
396 g_file_dup (GFile *file)
400 g_return_val_if_fail (G_IS_FILE (file), NULL);
402 iface = G_FILE_GET_IFACE (file);
404 return (* iface->dup) (file);
409 * @file: #gconstpointer to a #GFile.
411 * Creates a hash value for a #GFile.
413 * Returns: 0 if @file is not a valid #GFile, otherwise an
414 * integer that can be used as hash value for the #GFile.
415 * This function is intended for easily hashing a #GFile to
416 * add to a #GHashTable or similar data structure.
419 g_file_hash (gconstpointer file)
423 g_return_val_if_fail (G_IS_FILE (file), 0);
425 iface = G_FILE_GET_IFACE (file);
427 return (* iface->hash) ((GFile *)file);
432 * @file1: the first #GFile.
433 * @file2: the second #GFile.
435 * Checks equality of two given #GFile<!-- -->s
437 * Returns: %TRUE if @file1 and @file2 are equal.
438 * %FALSE if either is not a #GFile.
441 g_file_equal (GFile *file1,
446 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
447 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
449 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
452 iface = G_FILE_GET_IFACE (file1);
454 return (* iface->equal) (file1, file2);
460 * @file: input #GFile.
462 * Gets the parent directory for the @file.
463 * If the @file represents the root directory of the
464 * file system, then %NULL will be returned.
466 * Returns: a #GFile structure to the parent of the given
470 g_file_get_parent (GFile *file)
474 g_return_val_if_fail (G_IS_FILE (file), NULL);
476 iface = G_FILE_GET_IFACE (file);
478 return (* iface->get_parent) (file);
483 * @file: input #GFile.
484 * @name: string containing the child's name.
486 * Gets a specific child of @file with name equal to @name if
489 * Returns: a #GFile to a child specified by
490 * @name or %NULL if @name is %NULL, or the specified
491 * child doesn't exist.
494 g_file_get_child (GFile *file,
497 g_return_val_if_fail (G_IS_FILE (file), NULL);
498 g_return_val_if_fail (name != NULL, NULL);
500 return g_file_resolve_relative_path (file, name);
504 * g_file_get_child_for_display_name:
505 * @file: input #GFile.
506 * @display_name: string to a possible child.
509 * Gets the child of @file for a given @display_name. If
510 * this function fails, it returns %NULL and @error will be
511 * set with %G_IO_ERROR_INVALID_FILENAME.
513 * Returns: a #GFile to the specified child, or
514 * %NULL if @display_name is %NULL.
517 g_file_get_child_for_display_name (GFile *file,
518 const char *display_name,
523 g_return_val_if_fail (G_IS_FILE (file), NULL);
524 g_return_val_if_fail (display_name != NULL, NULL);
526 iface = G_FILE_GET_IFACE (file);
528 return (* iface->get_child_for_display_name) (file, display_name, error);
532 * g_file_contains_file:
533 * @parent: input #GFile.
534 * @descendant: input #GFile.
536 * Checks whether @parent contains the specified @descendent.
538 * Returns: %TRUE if the @descendent's parent is @parent. %FALSE otherwise.
541 g_file_contains_file (GFile *parent,
546 g_return_val_if_fail (G_IS_FILE (parent), FALSE);
547 g_return_val_if_fail (G_IS_FILE (descendant), FALSE);
549 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
552 iface = G_FILE_GET_IFACE (parent);
554 return (* iface->contains_file) (parent, descendant);
558 * g_file_get_relative_path:
559 * @parent: input #GFile.
560 * @descendant: input #GFile.
562 * Gets the path for @descendant relative to @parent.
564 * Returns: string with the relative path from @descendant
565 * to @parent. The returned string should be freed with
566 * g_free() when no longer needed.
569 g_file_get_relative_path (GFile *parent,
574 g_return_val_if_fail (G_IS_FILE (parent), NULL);
575 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
577 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
580 iface = G_FILE_GET_IFACE (parent);
582 return (* iface->get_relative_path) (parent, descendant);
586 * g_file_resolve_relative_path:
587 * @file: input #GFile.
588 * @relative_path: a given relative path string.
590 * Resolves a relative path for @file to an absolute path.
592 * Returns: #GFile to the resolved path. %NULL if @relative_path
593 * is %NULL or if @file is invalid.
596 g_file_resolve_relative_path (GFile *file,
597 const char *relative_path)
601 g_return_val_if_fail (G_IS_FILE (file), NULL);
602 g_return_val_if_fail (relative_path != NULL, NULL);
604 iface = G_FILE_GET_IFACE (file);
606 return (* iface->resolve_relative_path) (file, relative_path);
610 * g_file_enumerate_children:
611 * @file: input #GFile.
612 * @attributes: a string containing a #GFileAttributeInfo query.
613 * @flags: a set of #GFileQueryInfoFlags.
614 * @cancellable: optional #GCancellable object, %NULL to ignore.
615 * @error: #GError for error reporting.
617 * Gets a #GFileEnumerator for the children of @file that match @attributes,
618 * where attributes is a #GFileAttributeInfo query string (e.g. "std:type",
621 * If @cancellable is not %NULL, then the operation can be cancelled by
622 * triggering the cancellable object from another thread. If the operation
623 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
625 * If the #GFileIface for the given @file does not support #GFileEnumerator,
626 * then %NULL will be returned and the error %G_IO_ERROR_NOT_SUPPORTED will
629 * Returns: A #GFileEnumerator if successful. %NULL if cancelled or if #GFile's
630 * backend doesn't support #GFileEnumerator.
633 g_file_enumerate_children (GFile *file,
634 const char *attributes,
635 GFileQueryInfoFlags flags,
636 GCancellable *cancellable,
642 g_return_val_if_fail (G_IS_FILE (file), NULL);
644 if (g_cancellable_set_error_if_cancelled (cancellable, error))
647 iface = G_FILE_GET_IFACE (file);
649 if (iface->enumerate_children == NULL)
651 g_set_error (error, G_IO_ERROR,
652 G_IO_ERROR_NOT_SUPPORTED,
653 _("Operation not supported"));
657 return (* iface->enumerate_children) (file, attributes, flags,
662 * g_file_enumerate_children_async:
663 * @file: input #GFile.
664 * @attributes: a string containing a #GFileAttributeInfo query.
665 * @flags: a set of #GFileQueryInfoFlags.
666 * @io_priority: the io priority of the request.
667 * @cancellable: optional #GCancellable object, %NULL to ignore.
668 * @callback: a #GAsyncReadyCallback.
669 * @user_data: a #gpointer.
671 * Asynchronously gets a #GFileEnumerator for the children of @file that
672 * match @attributes, where attributes is a #GFileAttributeInfo query
673 * string (e.g. "std:type", "std:*"). For the synchronous version of this
674 * function, see g_file_enumerate_children().
676 * To finish this asynchronous operation, see g_file_enumerate_children_finish().
679 g_file_enumerate_children_async (GFile *file,
680 const char *attributes,
681 GFileQueryInfoFlags flags,
683 GCancellable *cancellable,
684 GAsyncReadyCallback callback,
689 g_return_if_fail (G_IS_FILE (file));
691 iface = G_FILE_GET_IFACE (file);
692 (* iface->enumerate_children_async) (file,
702 * g_file_enumerate_children_finish:
703 * @file: input #GFile.
704 * @res: a #GAsyncResult.
707 * If @cancellable was not %NULL when g_file_enumerate_children_async()
708 * was called, then the operation could have been cancelled by triggering
709 * the cancellable object from another thread. If the operation was cancelled,
710 * the @error will be set to %G_IO_ERROR_CANCELLED and this function will
713 * If the #GFileIface for the given @file does not support enumerating files,
714 * then %NULL will be returned and the error %G_IO_ERROR_NOT_SUPPORTED will
717 * Returns: a #GFileEnumerator or %NULL if an error occurred.
720 g_file_enumerate_children_finish (GFile *file,
726 g_return_val_if_fail (G_IS_FILE (file), NULL);
727 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
729 if (G_IS_SIMPLE_ASYNC_RESULT (res))
731 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
732 if (g_simple_async_result_propagate_error (simple, error))
736 iface = G_FILE_GET_IFACE (file);
737 return (* iface->enumerate_children_finish) (file, res, error);
743 * @file: input #GFile.
744 * @attributes: a string containing a #GFileAttributeInfo query.
745 * @flags: a set of #GFileQueryInfoFlags.
746 * @cancellable: optional #GCancellable object, %NULL to ignore.
749 * If @cancellable is not %NULL, then the operation can be cancelled by
750 * triggering the cancellable object from another thread. If the operation
751 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
753 * If the #GFileIface for the given @file does not support querying file
754 * information, then %NULL will be returned and the error
755 * %G_IO_ERROR_NOT_SUPPORTED will be set in @error.
757 * Returns: a #GFileInfo for the given @file, or %NULL on error.
760 g_file_query_info (GFile *file,
761 const char *attributes,
762 GFileQueryInfoFlags flags,
763 GCancellable *cancellable,
768 g_return_val_if_fail (G_IS_FILE (file), NULL);
770 if (g_cancellable_set_error_if_cancelled (cancellable, error))
773 iface = G_FILE_GET_IFACE (file);
775 if (iface->query_info == NULL)
777 g_set_error (error, G_IO_ERROR,
778 G_IO_ERROR_NOT_SUPPORTED,
779 _("Operation not supported"));
783 return (* iface->query_info) (file, attributes, flags, cancellable, error);
787 * g_file_query_info_async:
788 * @file: input #GFile.
789 * @attributes: a string containing a #GFileAttributeInfo query.
790 * @flags: a set of #GFileQueryInfoFlags.
791 * @io_priority: the io priority of the request.
792 * @cancellable: optional #GCancellable object, %NULL to ignore.
793 * @callback: a #GAsyncReadyCallback.
794 * @user_data: a #gpointer.
796 * If @cancellable is not %NULL, then the operation can be cancelled by
797 * triggering the cancellable object from another thread. If the operation
798 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
800 * To finish this asynchronous operation, see g_file_query_info_finish().
803 g_file_query_info_async (GFile *file,
804 const char *attributes,
805 GFileQueryInfoFlags flags,
807 GCancellable *cancellable,
808 GAsyncReadyCallback callback,
813 g_return_if_fail (G_IS_FILE (file));
815 iface = G_FILE_GET_IFACE (file);
816 (* iface->query_info_async) (file,
826 * g_file_query_info_finish:
827 * @file: input #GFile.
828 * @res: a #GAsyncResult.
831 * Finishes an asynchronous file info query.
833 * If @cancellable was not %NULL when g_file_query_info_async() was called,
834 * then the operation could have been cancelled by triggering the cancellable
835 * object from another thread. If the operation was cancelled, the @error will
836 * be set to %G_IO_ERROR_CANCELLED and this function will return %NULL.
838 * If the #GFileIface for the given @file does not support querying file
839 * information, then %NULL will be returned and the error
840 * %G_IO_ERROR_NOT_SUPPORTED will be set in @error.
842 * Returns: #GFileInfo for given @file or %NULL on error.
845 g_file_query_info_finish (GFile *file,
851 g_return_val_if_fail (G_IS_FILE (file), NULL);
852 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
854 if (G_IS_SIMPLE_ASYNC_RESULT (res))
856 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
857 if (g_simple_async_result_propagate_error (simple, error))
861 iface = G_FILE_GET_IFACE (file);
862 return (* iface->query_info_finish) (file, res, error);
866 * g_file_query_filesystem_info:
867 * @file: input #GFile.
868 * @attributes: a string containing a #GFileAttributeInfo query.
869 * @cancellable: optional #GCancellable object, %NULL to ignore.
872 * If @cancellable is not %NULL, then the operation can be cancelled by
873 * triggering the cancellable object from another thread. If the operation
874 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
876 * If the #GFileIface for the given @file does not support querying file system
877 * information, then %NULL will be returned and the error
878 * %G_IO_ERROR_NOT_SUPPORTED will be set in @error.
880 * Returns: a #GFileInfo or %NULL if there was an error.
883 g_file_query_filesystem_info (GFile *file,
884 const char *attributes,
885 GCancellable *cancellable,
890 g_return_val_if_fail (G_IS_FILE (file), NULL);
892 if (g_cancellable_set_error_if_cancelled (cancellable, error))
895 iface = G_FILE_GET_IFACE (file);
897 if (iface->query_filesystem_info == NULL)
899 g_set_error (error, G_IO_ERROR,
900 G_IO_ERROR_NOT_SUPPORTED,
901 _("Operation not supported"));
905 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
909 * g_file_find_enclosing_volume:
910 * @file: input #GFile.
911 * @cancellable: optional #GCancellable object, %NULL to ignore.
914 * Gets a #GVolume for the #GFile.
916 * If the #GFileIface for @file does not have a volume (e.g. possibly a
917 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
920 * If @cancellable is not %NULL, then the operation can be cancelled by
921 * triggering the cancellable object from another thread. If the operation
922 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
924 * Returns: a #GVolume where the @file is located or %NULL on error.
927 g_file_find_enclosing_volume (GFile *file,
928 GCancellable *cancellable,
933 g_return_val_if_fail (G_IS_FILE (file), NULL);
935 if (g_cancellable_set_error_if_cancelled (cancellable, error))
938 iface = G_FILE_GET_IFACE (file);
939 if (iface->find_enclosing_volume == NULL)
941 g_set_error (error, G_IO_ERROR,
942 G_IO_ERROR_NOT_FOUND,
943 _("Containing volume does not exist"));
947 return (* iface->find_enclosing_volume) (file, cancellable, error);
952 * @file: #GFile to read.
953 * @cancellable: a #GCancellable
954 * @error: a #GError, or %NULL
956 * Reads a whole file into a #GFileInputStream. Fails returning %NULL if
957 * given #GFile points to a directory.
959 * If the #GFileIface for @file does not support reading files, then
960 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
962 * If @cancellable is not %NULL, then the operation can be cancelled by
963 * triggering the cancellable object from another thread. If the operation
964 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
966 * Returns: #GFileInputStream or %NULL on error.
969 g_file_read (GFile *file,
970 GCancellable *cancellable,
975 g_return_val_if_fail (G_IS_FILE (file), NULL);
977 if (g_cancellable_set_error_if_cancelled (cancellable, error))
980 iface = G_FILE_GET_IFACE (file);
982 if (iface->read == NULL)
984 g_set_error (error, G_IO_ERROR,
985 G_IO_ERROR_NOT_SUPPORTED,
986 _("Operation not supported"));
990 return (* iface->read) (file, cancellable, error);
995 * @file: input #GFile.
996 * @flags: a set of #GFileCreateFlags.
997 * @cancellable: optional #GCancellable object, %NULL to ignore.
998 * @error: a #GError, or %NULL
1000 * If the #GFileIface for @file does not support appending to files, then
1001 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
1003 * If @cancellable is not %NULL, then the operation can be cancelled by
1004 * triggering the cancellable object from another thread. If the operation
1005 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1007 * Returns: a #GFileOutputStream.
1010 g_file_append_to (GFile *file,
1011 GFileCreateFlags flags,
1012 GCancellable *cancellable,
1017 g_return_val_if_fail (G_IS_FILE (file), NULL);
1019 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1022 iface = G_FILE_GET_IFACE (file);
1024 if (iface->append_to == NULL)
1026 g_set_error (error, G_IO_ERROR,
1027 G_IO_ERROR_NOT_SUPPORTED,
1028 _("Operation not supported"));
1032 return (* iface->append_to) (file, flags, cancellable, error);
1037 * @file: input #GFile.
1038 * @flags: a set of #GFileCreateFlags.
1039 * @cancellable: optional #GCancellable object, %NULL to ignore.
1040 * @error: a #GError, or %NULL
1042 * If the #GFileIface for @file does not support creating files, then
1043 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
1045 * If @cancellable is not %NULL, then the operation can be cancelled by
1046 * triggering the cancellable object from another thread. If the operation
1047 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1049 * Returns: a #GFileOutputStream for the newly created file, or
1053 g_file_create (GFile *file,
1054 GFileCreateFlags flags,
1055 GCancellable *cancellable,
1060 g_return_val_if_fail (G_IS_FILE (file), NULL);
1062 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1065 iface = G_FILE_GET_IFACE (file);
1067 if (iface->create == NULL)
1069 g_set_error (error, G_IO_ERROR,
1070 G_IO_ERROR_NOT_SUPPORTED,
1071 _("Operation not supported"));
1075 return (* iface->create) (file, flags, cancellable, error);
1080 * @file: input #GFile.
1081 * @etag: an Entity Tag for the current #GFile.
1082 * @make_backup: a #gboolean.
1083 * @flags: a set of #GFileCreateFlags.
1084 * @cancellable: optional #GCancellable object, %NULL to ignore.
1085 * @error: a #GError, or %NULL
1087 * If the #GFileIface for @file does not support streaming operations, then
1088 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
1090 * If @cancellable is not %NULL, then the operation can be cancelled by
1091 * triggering the cancellable object from another thread. If the operation
1092 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1094 * @etag will replace the entity tag for the current file.
1096 * Returns: a #GFileOutputStream or %NULL on error. If @make_backup is %TRUE,
1097 * this function will attempt to make a backup of the current file.
1100 g_file_replace (GFile *file,
1102 gboolean make_backup,
1103 GFileCreateFlags flags,
1104 GCancellable *cancellable,
1109 g_return_val_if_fail (G_IS_FILE (file), NULL);
1111 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1114 iface = G_FILE_GET_IFACE (file);
1116 if (iface->replace == NULL)
1118 g_set_error (error, G_IO_ERROR,
1119 G_IO_ERROR_NOT_SUPPORTED,
1120 _("Operation not supported"));
1125 /* Handle empty tag string as NULL in consistent way. */
1126 if (etag && *etag == 0)
1129 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1133 * g_file_read_async:
1134 * @file: input #GFile.
1135 * @io_priority: the io priority of the request.
1136 * @cancellable: optional #GCancellable object, %NULL to ignore.
1137 * @callback: a #GAsyncReadyCallback.
1138 * @user_data: a #gpointer.
1140 * Asynchronously reads @file. For the synchronous version of this function,
1141 * see g_file_read().
1144 g_file_read_async (GFile *file,
1146 GCancellable *cancellable,
1147 GAsyncReadyCallback callback,
1152 g_return_if_fail (G_IS_FILE (file));
1154 iface = G_FILE_GET_IFACE (file);
1155 (* iface->read_async) (file,
1163 * g_file_read_finish:
1164 * @file: input #GFile.
1165 * @res: a #GAsyncResult.
1166 * @error: a #GError, or %NULL
1168 * Finishes an asynchronous file read operation started with
1169 * g_file_read_async().
1171 * If the #GFileIface for @file does not support streaming operations, then
1172 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
1174 * If @cancellable is not %NULL, then the operation can be cancelled by
1175 * triggering the cancellable object from another thread. If the operation
1176 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1178 * Returns: a #GFileInputStream or %NULL on error.
1181 g_file_read_finish (GFile *file,
1187 g_return_val_if_fail (G_IS_FILE (file), NULL);
1188 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1190 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1192 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1193 if (g_simple_async_result_propagate_error (simple, error))
1197 iface = G_FILE_GET_IFACE (file);
1198 return (* iface->read_finish) (file, res, error);
1202 * g_file_append_to_async:
1203 * @file: input #GFile.
1204 * @flags: a set of #GFileCreateFlags.
1205 * @io_priority: the io priority of the request. the io priority of the request.
1206 * @cancellable: optional #GCancellable object, %NULL to ignore.
1207 * @callback: a #GAsyncReadyCallback.
1208 * @user_data: a #gpointer.
1210 * Readies a file for appending data asynchronously.
1211 * For the synchronous version of this function, see
1212 * g_file_append_to().
1214 * If @cancellable is not %NULL, then the operation can be cancelled by
1215 * triggering the cancellable object from another thread. If the operation
1216 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1219 g_file_append_to_async (GFile *file,
1220 GFileCreateFlags flags,
1222 GCancellable *cancellable,
1223 GAsyncReadyCallback callback,
1228 g_return_if_fail (G_IS_FILE (file));
1230 iface = G_FILE_GET_IFACE (file);
1231 (* iface->append_to_async) (file,
1240 * g_file_append_to_finish:
1241 * @file: input #GFile.
1242 * @res: #GAsyncResult
1243 * @error: a #GError, or #NULL
1245 * Finishes appending to a file started with g_file_append_to_async().
1247 * Returns: a valid #GFileOutputStream or %NULL on error.
1250 g_file_append_to_finish (GFile *file,
1256 g_return_val_if_fail (G_IS_FILE (file), NULL);
1257 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1259 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1261 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1262 if (g_simple_async_result_propagate_error (simple, error))
1266 iface = G_FILE_GET_IFACE (file);
1267 return (* iface->append_to_finish) (file, res, error);
1271 * g_file_create_async:
1272 * @file: input #GFile.
1273 * @flags: a set of #GFileCreateFlags.
1274 * @io_priority: the io priority of the request. the io priority of the request.
1275 * @cancellable: optional #GCancellable object, %NULL to ignore.
1276 * @callback: a #GAsyncReadyCallback.
1277 * @user_data: a #gpointer.
1279 * Creates a new file asynchronously. For the synchronous version of
1280 * this function, see g_file_create().
1282 * If @cancellable is not %NULL, then the operation can be cancelled by
1283 * triggering the cancellable object from another thread. If the operation
1284 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1287 g_file_create_async (GFile *file,
1288 GFileCreateFlags flags,
1290 GCancellable *cancellable,
1291 GAsyncReadyCallback callback,
1296 g_return_if_fail (G_IS_FILE (file));
1298 iface = G_FILE_GET_IFACE (file);
1299 (* iface->create_async) (file,
1308 * g_file_create_finish:
1309 * @file: input #GFile.
1310 * @res: a #GAsyncResult.
1311 * @error: a #GError, or %NULL
1313 * Finishes creating a file started with g_file_create_async().
1315 * Returns: a #GFileOutputStream.
1318 g_file_create_finish (GFile *file,
1324 g_return_val_if_fail (G_IS_FILE (file), NULL);
1325 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1327 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1329 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1330 if (g_simple_async_result_propagate_error (simple, error))
1334 iface = G_FILE_GET_IFACE (file);
1335 return (* iface->create_finish) (file, res, error);
1339 * g_file_replace_async:
1340 * @file: input #GFile.
1341 * @etag: an Entity Tag for the current #GFile.
1342 * @make_backup: a #gboolean.
1343 * @flags: a set of #GFileCreateFlags.
1344 * @io_priority: the io priority of the request. the io priority of the request.
1345 * @cancellable: optional #GCancellable object, %NULL to ignore.
1346 * @callback: a #GAsyncReadyCallback.
1347 * @user_data: a #gpointer.
1349 * Replaces a file's contents. For the synchronous version of this
1350 * function, see g_file_replace(). If @make_backup is %TRUE, this function
1351 * will attempt to make a backup of the current file.
1353 * If @cancellable is not %NULL, then the operation can be cancelled by
1354 * triggering the cancellable object from another thread. If the operation
1355 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1358 g_file_replace_async (GFile *file,
1360 gboolean make_backup,
1361 GFileCreateFlags flags,
1363 GCancellable *cancellable,
1364 GAsyncReadyCallback callback,
1369 g_return_if_fail (G_IS_FILE (file));
1371 iface = G_FILE_GET_IFACE (file);
1372 (* iface->replace_async) (file,
1383 * g_file_replace_finish:
1384 * @file: input #GFile.
1385 * @res: a #GAsyncResult.
1386 * @error: a #GError, or %NULL
1388 * Finishes replacing the contents of the file started by
1389 * g_file_replace_async(). This is typically called within
1390 * a #GAsyncReadyCallback.
1392 * Returns: a #GFileOutputStream, or %NULL if an error has occured.
1395 g_file_replace_finish (GFile *file,
1401 g_return_val_if_fail (G_IS_FILE (file), NULL);
1402 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1404 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1406 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1407 if (g_simple_async_result_propagate_error (simple, error))
1411 iface = G_FILE_GET_IFACE (file);
1412 return (* iface->replace_finish) (file, res, error);
1416 copy_symlink (GFile *destination,
1417 GFileCopyFlags flags,
1418 GCancellable *cancellable,
1423 gboolean tried_delete;
1425 GFileType file_type;
1427 tried_delete = FALSE;
1431 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1433 /* Maybe it already existed, and we want to overwrite? */
1434 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1435 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1437 g_error_free (my_error);
1440 /* Don't overwrite if the destination is a directory */
1441 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STD_TYPE,
1442 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1443 cancellable, &my_error);
1446 file_type = g_file_info_get_file_type (info);
1447 g_object_unref (info);
1449 if (file_type == G_FILE_TYPE_DIRECTORY)
1451 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1452 _("Can't copy over directory"));
1457 if (!g_file_delete (destination, cancellable, error))
1460 tried_delete = TRUE;
1464 g_propagate_error (error, my_error);
1471 static GInputStream *
1472 open_source_for_copy (GFile *source,
1474 GFileCopyFlags flags,
1475 GCancellable *cancellable,
1481 GFileType file_type;
1484 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1488 /* There was an error opening the source, try to set a good error for it: */
1490 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1492 /* The source is a directory, don't fail with WOULD_RECURSE immediately, as
1493 that is less useful to the app. Better check for errors on the target instead. */
1495 g_error_free (my_error);
1498 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STD_TYPE,
1499 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1500 cancellable, &my_error);
1503 file_type = g_file_info_get_file_type (info);
1504 g_object_unref (info);
1506 if (flags & G_FILE_COPY_OVERWRITE)
1508 if (file_type == G_FILE_TYPE_DIRECTORY)
1510 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1511 _("Can't copy directory over directory"));
1514 /* continue to would_recurse error */
1518 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1519 _("Target file exists"));
1525 /* Error getting info from target, return that error (except for NOT_FOUND, which is no error here) */
1526 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1528 g_propagate_error (error, my_error);
1531 g_error_free (my_error);
1534 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1535 _("Can't recursively copy directory"));
1539 g_propagate_error (error, my_error);
1544 should_copy (GFileAttributeInfo *info, gboolean as_move)
1547 return info->flags & G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED;
1548 return info->flags & G_FILE_ATTRIBUTE_FLAGS_COPY_WITH_FILE;
1552 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1553 GFileAttributeInfoList *namespaces,
1561 s = g_string_new ("");
1565 for (i = 0; i < attributes->n_infos; i++)
1567 if (should_copy (&attributes->infos[i], as_move))
1572 g_string_append_c (s, ',');
1574 g_string_append (s, attributes->infos[i].name);
1581 for (i = 0; i < namespaces->n_infos; i++)
1583 if (should_copy (&namespaces->infos[i], as_move))
1588 g_string_append_c (s, ',');
1590 g_string_append (s, namespaces->infos[i].name);
1591 g_string_append (s, ":*");
1596 return g_string_free (s, FALSE);
1601 g_file_copy_attributes (GFile *source,
1603 GFileCopyFlags flags,
1604 GCancellable *cancellable,
1607 GFileAttributeInfoList *attributes, *namespaces;
1608 char *attrs_to_read;
1612 gboolean source_nofollow_symlinks;
1614 as_move = flags & G_FILE_COPY_ALL_METADATA;
1615 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1617 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1618 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1619 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1621 if (attributes == NULL && namespaces == NULL)
1624 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1626 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1627 we just don't copy it. */
1628 info = g_file_query_info (source, attrs_to_read,
1629 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1633 g_free (attrs_to_read);
1638 res = g_file_set_attributes_from_info (destination,
1642 g_object_unref (info);
1645 g_file_attribute_info_list_unref (attributes);
1646 g_file_attribute_info_list_unref (namespaces);
1651 /* Closes the streams */
1653 copy_stream_with_progress (GInputStream *in,
1655 GCancellable *cancellable,
1656 GFileProgressCallback progress_callback,
1657 gpointer progress_callback_data,
1660 gssize n_read, n_written;
1661 goffset current_size;
1662 char buffer[8192], *p;
1668 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1669 G_FILE_ATTRIBUTE_STD_SIZE,
1673 total_size = g_file_info_get_size (info);
1674 g_object_unref (info);
1681 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
1691 current_size += n_read;
1696 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
1697 if (n_written == -1)
1704 n_read -= n_written;
1710 if (progress_callback)
1711 progress_callback (current_size, total_size, progress_callback_data);
1715 error = NULL; /* Ignore further errors */
1717 /* Make sure we send full copied size */
1718 if (progress_callback)
1719 progress_callback (current_size, total_size, progress_callback_data);
1722 /* Don't care about errors in source here */
1723 g_input_stream_close (in, cancellable, NULL);
1725 /* But write errors on close are bad! */
1726 if (!g_output_stream_close (out, cancellable, error))
1729 g_object_unref (in);
1730 g_object_unref (out);
1736 file_copy_fallback (GFile *source,
1738 GFileCopyFlags flags,
1739 GCancellable *cancellable,
1740 GFileProgressCallback progress_callback,
1741 gpointer progress_callback_data,
1749 /* Maybe copy the symlink? */
1750 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
1752 info = g_file_query_info (source,
1753 G_FILE_ATTRIBUTE_STD_TYPE "," G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET,
1754 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1760 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
1761 (target = g_file_info_get_symlink_target (info)) != NULL)
1763 if (!copy_symlink (destination, flags, cancellable, target, error))
1765 g_object_unref (info);
1769 g_object_unref (info);
1773 g_object_unref (info);
1776 in = open_source_for_copy (source, destination, flags, cancellable, error);
1780 if (flags & G_FILE_COPY_OVERWRITE)
1782 out = (GOutputStream *)g_file_replace (destination,
1784 flags & G_FILE_COPY_BACKUP,
1785 cancellable, error);
1789 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
1794 g_object_unref (in);
1798 if (!copy_stream_with_progress (in, out, cancellable,
1799 progress_callback, progress_callback_data,
1805 /* Ignore errors here. Failure to copy metadata is not a hard error */
1806 g_file_copy_attributes (source, destination,
1807 flags, cancellable, NULL);
1814 * @source: input #GFile.
1815 * @destination: destination #GFile
1816 * @flags: set of #GFileCopyFlags
1817 * @cancellable: optional #GCancellable object, %NULL to ignore.
1818 * @progress_callback: function to callback with progress information
1819 * @progress_callback_data: userdata to pass to @progress_callback
1820 * @error: #GError to set on error, or %NULL
1822 * <!-- Source Friendly Version
1823 * List of possible errors resulting from g_file_copy():
1824 * source dest flags res
1825 * - * * G_IO_ERROR_NOT_FOUND
1827 * file * 0 G_IO_ERROR_EXISTS
1828 * file file overwr ok
1829 * file dir overwr G_IO_ERROR_IS_DIRECTORY
1831 * dir - * G_IO_ERROR_WOULD_RECURSE
1832 * dir * 0 G_IO_ERROR_EXISTS
1833 * dir dir overwr G_IO_ERROR_WOULD_MERGE
1834 * dir file overwr G_IO_ERROR_WOULD_RECURSE
1835 * Docbook version below -->
1837 * Copies a file or directory from @source to @destination, with the given @flags.
1838 * @flags should be set to %0 if not used. This operation may fail, and @error will
1839 * be set appropriately with the given error result (see the following table).
1840 * File copies are always asynchronous.
1842 * If @cancellable is not %NULL, then the operation can be cancelled by
1843 * triggering the cancellable object from another thread. If the operation
1844 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1846 * If @progress_callback is not %NULL, then the operation can be monitored by
1847 * setting this to a #GFileProgressCallback function. @progress_callback_data
1848 * will be passed to this function.
1851 * <title>g_file_copy() Error Conditions</title>
1852 * <tgroup cols='4' align='left'><thead>
1853 * <row><entry>Source</entry><entry>Destination</entry><entry>Flags</entry><entry>Results in</entry></row>
1855 * <row><entry>%NULL</entry><entry>Anything</entry><entry>Anything</entry><entry>%G_IO_ERROR_NOT_FOUND</entry></row>
1856 * <row><entry>File</entry><entry>%NULL</entry><entry>Anything</entry><entry>No Error</entry></row>
1857 * <row><entry>File</entry><entry>Anything</entry><entry>0</entry><entry>%G_IO_ERROR_EXISTS</entry></row>
1858 * <row><entry>File</entry><entry>File</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>No Error</entry></row>
1859 * <row><entry>File</entry><entry>Directory</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>%G_IO_ERROR_IS_DIRECTORY</entry></row>
1860 * <row><entry>Directory</entry><entry>%NULL</entry><entry>Anything</entry><entry>%G_IO_ERROR_WOULD_RECURSE</entry></row>
1861 * <row><entry>Directory</entry><entry>Anything</entry><entry>0</entry><entry>%G_IO_ERROR_EXISTS</entry></row>
1862 * <row><entry>Directory</entry><entry>Directory</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>%G_IO_ERROR_IS_DIRECTORY</entry></row>
1863 * <row><entry>Directory</entry><entry>File</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>%G_IO_ERROR_WOULD_RECURSE</entry></row>
1868 * Returns: %TRUE on success, %FALSE otherwise.
1871 g_file_copy (GFile *source,
1873 GFileCopyFlags flags,
1874 GCancellable *cancellable,
1875 GFileProgressCallback progress_callback,
1876 gpointer progress_callback_data,
1883 g_return_val_if_fail (G_IS_FILE (source), FALSE);
1884 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
1886 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1889 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
1891 iface = G_FILE_GET_IFACE (source);
1896 res = (* iface->copy) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
1901 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
1903 g_propagate_error (error, my_error);
1909 return file_copy_fallback (source, destination, flags, cancellable,
1910 progress_callback, progress_callback_data,
1917 * @source: #GFile pointing to the source location.
1918 * @destination: #GFile pointing to the destination location.
1919 * @flags: #GFileCopyFlags enum.
1920 * @cancellable: optional #GCancellable object, %NULL to ignore.
1921 * @progress_callback: GFileProgressCallback function for updates.
1922 * @progress_callback_data: gpointer to user data for the callback function.
1923 * @error: #GError for returning error conditions, or %NULL
1925 * List of possible returns from g_file_move() with given source,
1926 * destination, and flags:
1927 * <!-- Source version
1928 * source dest flags results in
1929 * - * * G_IO_ERROR_NOT_FOUND
1931 * file * 0 G_IO_ERROR_EXISTS
1932 * file file overwr ok
1933 * file dir overwr G_IO_ERROR_IS_DIRECTORY
1935 * dir - * ok || G_IO_ERROR_WOULD_RECURSE
1936 * dir * 0 G_IO_ERROR_EXISTS
1937 * dir dir overwr G_IO_ERROR_WOULD_MERGE
1938 * dir file overwr ok || G_IO_ERROR_WOULD_RECURSE
1939 * Pretty Docbook version -->
1941 * <title>g_file_move() Error Conditions</title>
1942 * <tgroup cols='4' align='left'><thead>
1943 * <row><entry>Source</entry><entry>Destination</entry>
1944 * <entry>Flags</entry><entry>Results in</entry></row>
1946 * <row><entry> %NULL </entry><entry> Anything </entry>
1947 * <entry> Anything </entry><entry> %G_IO_ERROR_NOT_FOUND </entry></row>
1948 * <row><entry> File </entry><entry> %NULL </entry>
1949 * <entry> Anything </entry><entry> No Error </entry></row>
1950 * <row><entry> File </entry><entry> Anything </entry>
1951 * <entry> 0 </entry><entry> %G_IO_ERROR_EXISTS </entry></row>
1952 * <row><entry> File </entry><entry> File </entry>
1953 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> No Error </entry></row>
1954 * <row><entry> File </entry><entry> Directory </entry>
1955 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> %G_IO_ERROR_IS_DIRECTORY </entry></row>
1956 * <row><entry> Directory </entry><entry> %NULL </entry>
1957 * <entry> Anything </entry><entry> No Error or %G_IO_ERROR_WOULD_RECURSE </entry></row>
1958 * <row><entry> Directory </entry><entry> Anything </entry>
1959 * <entry> 0 </entry><entry> %G_IO_ERROR_EXISTS </entry></row>
1960 * <row><entry> Directory </entry><entry> Directory </entry>
1961 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> %G_IO_ERROR_IS_DIRECTORY </entry></row>
1962 * <row><entry> Directory </entry><entry> File </entry>
1963 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> No Error or %G_IO_ERROR_WOULD_RECURSE </entry></row>
1968 * Returns: %TRUE on successful move, %FALSE otherwise.
1971 g_file_move (GFile *source,
1973 GFileCopyFlags flags,
1974 GCancellable *cancellable,
1975 GFileProgressCallback progress_callback,
1976 gpointer progress_callback_data,
1983 g_return_val_if_fail (G_IS_FILE (source), FALSE);
1984 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
1986 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1989 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
1991 iface = G_FILE_GET_IFACE (source);
1996 res = (* iface->move) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
2001 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2003 g_propagate_error (error, my_error);
2009 flags |= G_FILE_COPY_ALL_METADATA;
2010 if (!g_file_copy (source, destination, flags, cancellable,
2011 progress_callback, progress_callback_data,
2015 return g_file_delete (source, cancellable, error);
2019 * g_file_make_directory
2020 * @file: input #GFile.
2021 * @cancellable: optional #GCancellable object, %NULL to ignore.
2022 * @error: a #GError, or %NULL
2024 * If @cancellable is not %NULL, then the operation can be cancelled by
2025 * triggering the cancellable object from another thread. If the operation
2026 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2028 * Returns: %TRUE on successful creation, %FALSE otherwise.
2031 g_file_make_directory (GFile *file,
2032 GCancellable *cancellable,
2037 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2039 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2042 iface = G_FILE_GET_IFACE (file);
2044 if (iface->make_directory == NULL)
2046 g_set_error (error, G_IO_ERROR,
2047 G_IO_ERROR_NOT_SUPPORTED,
2048 _("Operation not supported"));
2052 return (* iface->make_directory) (file, cancellable, error);
2056 * g_file_make_symbolic_link:
2057 * @file: input #GFile.
2058 * @symlink_value: a string with the name of the new symlink.
2059 * @cancellable: optional #GCancellable object, %NULL to ignore.
2060 * @error: a #GError.
2062 * If @cancellable is not %NULL, then the operation can be cancelled by
2063 * triggering the cancellable object from another thread. If the operation
2064 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2066 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2069 g_file_make_symbolic_link (GFile *file,
2070 const char *symlink_value,
2071 GCancellable *cancellable,
2076 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2077 g_return_val_if_fail (symlink_value != NULL, FALSE);
2079 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2082 if (*symlink_value == '\0')
2084 g_set_error (error, G_IO_ERROR,
2085 G_IO_ERROR_INVALID_ARGUMENT,
2086 _("Invalid symlink value given"));
2090 iface = G_FILE_GET_IFACE (file);
2092 if (iface->make_symbolic_link == NULL)
2094 g_set_error (error, G_IO_ERROR,
2095 G_IO_ERROR_NOT_SUPPORTED,
2096 _("Operation not supported"));
2100 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2105 * @file: input #GFile.
2106 * @cancellable: optional #GCancellable object, %NULL to ignore.
2107 * @error: a #GError, or %NULL
2111 * If @cancellable is not %NULL, then the operation can be cancelled by
2112 * triggering the cancellable object from another thread. If the operation
2113 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2115 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2118 g_file_delete (GFile *file,
2119 GCancellable *cancellable,
2124 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2126 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2129 iface = G_FILE_GET_IFACE (file);
2131 if (iface->delete_file == NULL)
2133 g_set_error (error, G_IO_ERROR,
2134 G_IO_ERROR_NOT_SUPPORTED,
2135 _("Operation not supported"));
2139 return (* iface->delete_file) (file, cancellable, error);
2144 * @file: #GFile to send to trash.
2145 * @cancellable: optional #GCancellable object, %NULL to ignore.
2146 * @error: a #GError, or %NULL
2148 * Sends @file to the virtual file system "Trash" location. If the
2149 * virtual file system does not have support having a "Trash" location,
2150 * %FALSE will be returned, and @error will be set to %G_IO_ERROR_NOT_SUPPORTED.
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 successful trash, %FALSE otherwise.
2159 g_file_trash (GFile *file,
2160 GCancellable *cancellable,
2165 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2167 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2170 iface = G_FILE_GET_IFACE (file);
2172 if (iface->trash == NULL)
2175 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2176 _("Trash not supported"));
2180 return (* iface->trash) (file, cancellable, error);
2184 * g_file_set_display_name:
2185 * @file: input #GFile.
2186 * @display_name: a string.
2187 * @cancellable: optional #GCancellable object, %NULL to ignore.
2188 * @error: a #GError, or %NULL
2190 * Sets the display name for @file. If the display name contains invalid
2191 * characters, @error will be set to %G_IO_ERROR_INVALID_ARGUMENT. For the
2192 * asynchronous version of this function, see g_file_set_display_name_async().
2194 * If @cancellable is not %NULL, then the operation can be cancelled by
2195 * triggering the cancellable object from another thread. If the operation
2196 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2198 * Returns: a #GFile, or %NULL if there was an error.
2201 g_file_set_display_name (GFile *file,
2202 const char *display_name,
2203 GCancellable *cancellable,
2208 g_return_val_if_fail (G_IS_FILE (file), NULL);
2209 g_return_val_if_fail (display_name != NULL, NULL);
2211 if (strchr (display_name, '/') != NULL)
2215 G_IO_ERROR_INVALID_ARGUMENT,
2216 _("File names cannot contain '/'"));
2220 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2223 iface = G_FILE_GET_IFACE (file);
2225 return (* iface->set_display_name) (file, display_name, cancellable, error);
2229 * g_file_set_display_name_async:
2230 * @file: input #GFile.
2231 * @display_name: a string.
2232 * @io_priority: the io priority of the request. the io priority of the request.
2233 * @cancellable: optional #GCancellable object, %NULL to ignore.
2234 * @callback: a #GAsyncReadyCallback.
2235 * @user_data: a #gpointer.
2237 * Asynchronously sets the display name for a given #GFile.
2238 * For the synchronous version of this function, see
2239 * g_file_set_display_name().
2241 * If @cancellable is not %NULL, then the operation can be cancelled by
2242 * triggering the cancellable object from another thread. If the operation
2243 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2246 g_file_set_display_name_async (GFile *file,
2247 const char *display_name,
2249 GCancellable *cancellable,
2250 GAsyncReadyCallback callback,
2255 g_return_if_fail (G_IS_FILE (file));
2256 g_return_if_fail (display_name != NULL);
2258 iface = G_FILE_GET_IFACE (file);
2259 (* iface->set_display_name_async) (file,
2268 * g_file_set_display_name_finish:
2269 * @file: input #GFile.
2270 * @res: a #GAsyncResult.
2271 * @error: a #GError, or %NULL
2273 * Finishes setting a display name started with g_file_set_display_name_async().
2275 * Returns: a #GFile or %NULL on error.
2278 g_file_set_display_name_finish (GFile *file,
2284 g_return_val_if_fail (G_IS_FILE (file), NULL);
2285 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2287 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2289 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2290 if (g_simple_async_result_propagate_error (simple, error))
2294 iface = G_FILE_GET_IFACE (file);
2295 return (* iface->set_display_name_finish) (file, res, error);
2299 * g_file_query_settable_attributes:
2300 * @file: input #GFile.
2301 * @cancellable: optional #GCancellable object, %NULL to ignore.
2302 * @error: a #GError, or %NULL
2304 * If @cancellable is not %NULL, then the operation can be cancelled by
2305 * triggering the cancellable object from another thread. If the operation
2306 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2308 * Returns: the type and full attribute name of all the attributes that
2309 * the file can set. This doesn't mean setting it will always succeed though,
2310 * you might get an access failure, or some specific file may not support a
2311 * specific attribute.
2313 GFileAttributeInfoList *
2314 g_file_query_settable_attributes (GFile *file,
2315 GCancellable *cancellable,
2320 GFileAttributeInfoList *list;
2322 g_return_val_if_fail (G_IS_FILE (file), NULL);
2324 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2327 iface = G_FILE_GET_IFACE (file);
2329 if (iface->query_settable_attributes == NULL)
2330 return g_file_attribute_info_list_new ();
2333 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2337 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2339 list = g_file_attribute_info_list_new ();
2340 g_error_free (my_error);
2343 g_propagate_error (error, my_error);
2350 * g_file_query_writable_namespaces:
2351 * @file: input #GFile.
2352 * @cancellable: optional #GCancellable object, %NULL to ignore.
2353 * @error: a #GError, or %NULL
2355 * If @cancellable is not %NULL, then the operation can be cancelled by
2356 * triggering the cancellable object from another thread. If the operation
2357 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2359 * Returns: a #GFileAttributeInfoList of attribute namespaces
2360 * where the user can create their own attribute names, such
2361 * as extended attributes.
2363 GFileAttributeInfoList *
2364 g_file_query_writable_namespaces (GFile *file,
2365 GCancellable *cancellable,
2370 GFileAttributeInfoList *list;
2372 g_return_val_if_fail (G_IS_FILE (file), NULL);
2374 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2377 iface = G_FILE_GET_IFACE (file);
2379 if (iface->query_writable_namespaces == NULL)
2380 return g_file_attribute_info_list_new ();
2383 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2387 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2389 list = g_file_attribute_info_list_new ();
2390 g_error_free (my_error);
2393 g_propagate_error (error, my_error);
2400 * g_file_set_attribute:
2401 * @file: input #GFile.
2402 * @attribute: a string containing the attribute's name.
2403 * @value: a set of #GFileAttributeValue.
2404 * @flags: a set of #GFileQueryInfoFlags.
2405 * @cancellable: optional #GCancellable object, %NULL to ignore.
2406 * @error: a #GError, or %NULL
2408 * Sets an attribute in the file with attribute name @attribute to @value.
2409 * If setting attributes is not suppored by the #GFileIface for @file,
2410 * then @error will be set to %G_IO_ERROR_NOT_SUPPORTED.
2412 * If @cancellable is not %NULL, then the operation can be cancelled by
2413 * triggering the cancellable object from another thread. If the operation
2414 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2416 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2419 g_file_set_attribute (GFile *file,
2420 const char *attribute,
2421 const GFileAttributeValue *value,
2422 GFileQueryInfoFlags flags,
2423 GCancellable *cancellable,
2428 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2429 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2431 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2434 iface = G_FILE_GET_IFACE (file);
2436 if (iface->set_attribute == NULL)
2438 g_set_error (error, G_IO_ERROR,
2439 G_IO_ERROR_NOT_SUPPORTED,
2440 _("Operation not supported"));
2444 return (* iface->set_attribute) (file, attribute, value, flags, cancellable, error);
2448 * g_file_set_attributes_from_info:
2449 * @file: input #GFile.
2450 * @info: a #GFileInfo.
2451 * @flags: #GFileQueryInfoFlags
2452 * @cancellable: optional #GCancellable object, %NULL to ignore.
2453 * @error: a #GError, or %NULL
2455 * Tries to set all attributes in the #GFileInfo on the target values,
2456 * not stopping on the first error.
2458 * If @cancellable is not %NULL, then the operation can be cancelled by
2459 * triggering the cancellable object from another thread. If the operation
2460 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2462 * Returns: %TRUE if there was any error, and @error will be set to
2463 * the first error. Error on particular fields are flagged by setting
2464 * the "status" field in the attribute value to
2465 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING.
2468 g_file_set_attributes_from_info (GFile *file,
2470 GFileQueryInfoFlags flags,
2471 GCancellable *cancellable,
2476 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2477 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2479 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2482 g_file_info_clear_status (info);
2484 iface = G_FILE_GET_IFACE (file);
2486 return (* iface->set_attributes_from_info) (file, info, flags, cancellable, error);
2491 g_file_real_set_attributes_from_info (GFile *file,
2493 GFileQueryInfoFlags flags,
2494 GCancellable *cancellable,
2500 GFileAttributeValue *value;
2504 attributes = g_file_info_list_attributes (info, NULL);
2506 for (i = 0; attributes[i] != NULL; i++)
2508 value = (GFileAttributeValue *)g_file_info_get_attribute (info, attributes[i]);
2510 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2513 if (!g_file_set_attribute (file, attributes[i], value, flags, cancellable, error))
2515 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2517 /* Don't set error multiple times */
2521 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2524 g_strfreev (attributes);
2530 * g_file_set_attributes_async:
2531 * @file: input #GFile.
2532 * @info: a #GFileInfo.
2533 * @flags: a #GFileQueryInfoFlags.
2534 * @io_priority: the io priority of the request. the io priority of the request.
2535 * @cancellable: optional #GCancellable object, %NULL to ignore.
2536 * @callback: a #GAsyncReadyCallback.
2537 * @user_data: a #gpointer.
2539 * Asynchronously sets the attributes of @file with @info.
2540 * For the synchronous version of this function, see g_file_set_attributes().
2542 * If @cancellable is not %NULL, then the operation can be cancelled by
2543 * triggering the cancellable object from another thread. If the operation
2544 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2547 g_file_set_attributes_async (GFile *file,
2549 GFileQueryInfoFlags flags,
2551 GCancellable *cancellable,
2552 GAsyncReadyCallback callback,
2557 g_return_if_fail (G_IS_FILE (file));
2558 g_return_if_fail (G_IS_FILE_INFO (info));
2560 iface = G_FILE_GET_IFACE (file);
2561 (* iface->set_attributes_async) (file, info, flags, io_priority, cancellable, callback, user_data);
2566 * g_file_set_attributes_finish:
2567 * @file: input #GFile.
2568 * @result: a #GAsyncResult.
2569 * @info: a #GFileInfo.
2570 * @error: a #GError, or %NULL
2572 * Finishes setting an attribute started in g_file_set_attributes_async().
2574 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2577 g_file_set_attributes_finish (GFile *file,
2578 GAsyncResult *result,
2584 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2585 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2587 /* No standard handling of errors here, as we must set info even
2590 iface = G_FILE_GET_IFACE (file);
2591 return (* iface->set_attributes_finish) (file, result, info, error);
2595 * g_file_set_attribute_string:
2596 * @file: input #GFile.
2597 * @attribute: a string containing the attribute's name.
2598 * @value: a string containing the attribute's value.
2599 * @flags: #GFileQueryInfoFlags.
2600 * @cancellable: optional #GCancellable object, %NULL to ignore.
2601 * @error: a #GError, or %NULL
2603 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value. If
2604 * @attribute is of a different type, this operation will fail.
2606 * If @cancellable is not %NULL, then the operation can be cancelled by
2607 * triggering the cancellable object from another thread. If the operation
2608 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2610 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2613 g_file_set_attribute_string (GFile *file,
2614 const char *attribute,
2616 GFileQueryInfoFlags flags,
2617 GCancellable *cancellable,
2620 GFileAttributeValue v;
2622 v.type = G_FILE_ATTRIBUTE_TYPE_STRING;
2623 v.u.string = (char *)value;
2624 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2628 * g_file_set_attribute_byte_string:
2629 * @file: input #GFile.
2630 * @attribute: a string containing the attribute's name.
2631 * @value: a string containing the attribute's new value.
2632 * @flags: a #GFileQueryInfoFlags.
2633 * @cancellable: optional #GCancellable object, %NULL to ignore.
2634 * @error: a #GError, or %NULL
2636 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value. If
2637 * @attribute is of a different type, this operation will fail, returning %FALSE.
2639 * If @cancellable is not %NULL, then the operation can be cancelled by
2640 * triggering the cancellable object from another thread. If the operation
2641 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2643 * Returns: %TRUE if the @attribute was successfully set to @value
2644 * in the @file, %FALSE otherwise.
2647 g_file_set_attribute_byte_string (GFile *file,
2648 const char *attribute,
2650 GFileQueryInfoFlags flags,
2651 GCancellable *cancellable,
2654 GFileAttributeValue v;
2656 v.type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
2657 v.u.string = (char *)value;
2658 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2662 * g_file_set_attribute_uint32:
2663 * @file: input #GFile.
2664 * @attribute: a string containing the attribute's name.
2665 * @value: a #guint32 containing the attribute's new value.
2666 * @flags: a #GFileQueryInfoFlags.
2667 * @cancellable: optional #GCancellable object, %NULL to ignore.
2668 * @error: a #GError, or %NULL
2670 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value. If
2671 * @attribute is of a different type, this operation will fail.
2673 * If @cancellable is not %NULL, then the operation can be cancelled by
2674 * triggering the cancellable object from another thread. If the operation
2675 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2677 * Returns: %TRUE if the @attribute was successfully set to @value
2678 * in the @file, %FALSE otherwise.
2681 g_file_set_attribute_uint32 (GFile *file,
2682 const char *attribute,
2684 GFileQueryInfoFlags flags,
2685 GCancellable *cancellable,
2688 GFileAttributeValue v;
2690 v.type = G_FILE_ATTRIBUTE_TYPE_UINT32;
2692 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2696 * g_file_set_attribute_int32:
2697 * @file: input #GFile.
2698 * @attribute: a string containing the attribute's name.
2699 * @value: a #gint32 containing the attribute's new value.
2700 * @flags: a #GFileQueryInfoFlags.
2701 * @cancellable: optional #GCancellable object, %NULL to ignore.
2702 * @error: a #GError, or %NULL
2704 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value. If
2705 * @attribute is of a different type, this operation will fail.
2707 * If @cancellable is not %NULL, then the operation can be cancelled by
2708 * triggering the cancellable object from another thread. If the operation
2709 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2711 * Returns: %TRUE if the @attribute was successfully set to @value
2712 * in the @file, %FALSE otherwise.
2715 g_file_set_attribute_int32 (GFile *file,
2716 const char *attribute,
2718 GFileQueryInfoFlags flags,
2719 GCancellable *cancellable,
2722 GFileAttributeValue v;
2724 v.type = G_FILE_ATTRIBUTE_TYPE_INT32;
2726 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2730 * g_file_set_attribute_uint64:
2731 * @file: input #GFile.
2732 * @attribute: a string containing the attribute's name.
2733 * @value: a #guint64 containing the attribute's new value.
2734 * @flags: a #GFileQueryInfoFlags.
2735 * @cancellable: optional #GCancellable object, %NULL to ignore.
2736 * @error: a #GError, or %NULL
2738 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value. If
2739 * @attribute is of a different type, this operation will fail.
2741 * If @cancellable is not %NULL, then the operation can be cancelled by
2742 * triggering the cancellable object from another thread. If the operation
2743 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2745 * Returns: %TRUE if the @attribute was successfully set to @value
2746 * in the @file, %FALSE otherwise.
2749 g_file_set_attribute_uint64 (GFile *file,
2750 const char *attribute,
2752 GFileQueryInfoFlags flags,
2753 GCancellable *cancellable,
2756 GFileAttributeValue v;
2758 v.type = G_FILE_ATTRIBUTE_TYPE_UINT64;
2760 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2764 * g_file_set_attribute_int64:
2765 * @file: input #GFile.
2766 * @attribute: a string containing the attribute's name.
2767 * @value: a #guint64 containing the attribute's new value.
2768 * @flags: a #GFileQueryInfoFlags.
2769 * @cancellable: optional #GCancellable object, %NULL to ignore.
2770 * @error: a #GError, or %NULL
2772 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value. If
2773 * @attribute is of a different type, this operation will fail.
2775 * If @cancellable is not %NULL, then the operation can be cancelled by
2776 * triggering the cancellable object from another thread. If the operation
2777 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2779 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2782 g_file_set_attribute_int64 (GFile *file,
2783 const char *attribute,
2785 GFileQueryInfoFlags flags,
2786 GCancellable *cancellable,
2789 GFileAttributeValue v;
2791 v.type = G_FILE_ATTRIBUTE_TYPE_INT64;
2793 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2797 * g_file_mount_mountable:
2798 * @file: input #GFile.
2799 * @mount_operation: a #GMountOperation.
2800 * @cancellable: optional #GCancellable object, %NULL to ignore.
2801 * @callback: a #GAsyncReadyCallback.
2802 * @user_data: a #gpointer.
2804 * If @cancellable is not %NULL, then the operation can be cancelled by
2805 * triggering the cancellable object from another thread. If the operation
2806 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2808 * Mounts a mountable file using @mount_operation, if possible.
2811 g_file_mount_mountable (GFile *file,
2812 GMountOperation *mount_operation,
2813 GCancellable *cancellable,
2814 GAsyncReadyCallback callback,
2819 g_return_if_fail (G_IS_FILE (file));
2820 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
2822 iface = G_FILE_GET_IFACE (file);
2824 if (iface->mount_mountable == NULL)
2826 g_simple_async_report_error_in_idle (G_OBJECT (file),
2830 G_IO_ERROR_NOT_SUPPORTED,
2831 _("Operation not supported"));
2834 (* iface->mount_mountable) (file,
2843 * g_file_mount_mountable_finish:
2844 * @file: input #GFile.
2845 * @result: a #GAsyncResult.
2846 * @error: a #GError, or %NULL
2848 * Returns: a #GFile or %NULL on error.
2851 g_file_mount_mountable_finish (GFile *file,
2852 GAsyncResult *result,
2857 g_return_val_if_fail (G_IS_FILE (file), NULL);
2858 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
2860 if (G_IS_SIMPLE_ASYNC_RESULT (result))
2862 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
2863 if (g_simple_async_result_propagate_error (simple, error))
2867 iface = G_FILE_GET_IFACE (file);
2868 return (* iface->mount_mountable_finish) (file, result, error);
2872 * g_file_unmount_mountable:
2873 * @file: input #GFile.
2874 * @cancellable: optional #GCancellable object, %NULL to ignore.
2875 * @callback: a #GAsyncReadyCallback.
2876 * @user_data: a #gpointer.
2878 * If @cancellable is not %NULL, then the operation can be cancelled by
2879 * triggering the cancellable object from another thread. If the operation
2880 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2883 g_file_unmount_mountable (GFile *file,
2884 GCancellable *cancellable,
2885 GAsyncReadyCallback callback,
2890 g_return_if_fail (G_IS_FILE (file));
2892 iface = G_FILE_GET_IFACE (file);
2894 if (iface->unmount_mountable == NULL)
2896 g_simple_async_report_error_in_idle (G_OBJECT (file),
2900 G_IO_ERROR_NOT_SUPPORTED,
2901 _("Operation not supported"));
2904 (* iface->unmount_mountable) (file,
2911 * g_file_unmount_mountable_finish:
2912 * @file: input #GFile.
2913 * @result: a #GAsyncResult.
2914 * @error: a #GError, or %NULL
2916 * Returns: %TRUE if the operation finished successfully. %FALSE
2920 g_file_unmount_mountable_finish (GFile *file,
2921 GAsyncResult *result,
2926 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2927 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2929 if (G_IS_SIMPLE_ASYNC_RESULT (result))
2931 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
2932 if (g_simple_async_result_propagate_error (simple, error))
2936 iface = G_FILE_GET_IFACE (file);
2937 return (* iface->unmount_mountable_finish) (file, result, error);
2941 * g_file_eject_mountable:
2942 * @file: input #GFile.
2943 * @cancellable: optional #GCancellable object, %NULL to ignore.
2944 * @callback: a #GAsyncReadyCallback.
2945 * @user_data: a #gpointer.
2947 * Starts an asynchronous eject on a mountable.
2948 * When this operation has completed, @callback will be called with
2949 * @user_user data, and the operation can be finalized with
2950 * g_file_eject_mountable_finish().
2952 * If @cancellable is not %NULL, then the operation can be cancelled by
2953 * triggering the cancellable object from another thread. If the operation
2954 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2957 g_file_eject_mountable (GFile *file,
2958 GCancellable *cancellable,
2959 GAsyncReadyCallback callback,
2964 g_return_if_fail (G_IS_FILE (file));
2966 iface = G_FILE_GET_IFACE (file);
2968 if (iface->eject_mountable == NULL)
2970 g_simple_async_report_error_in_idle (G_OBJECT (file),
2974 G_IO_ERROR_NOT_SUPPORTED,
2975 _("Operation not supported"));
2978 (* iface->eject_mountable) (file,
2985 * g_file_eject_mountable_finish:
2986 * @file: input #GFile.
2987 * @result: a #GAsyncResult.
2988 * @error: a #GError, or %NULL
2990 * Finishes an asynchronous eject operation started by
2991 * g_file_eject_mountable().
2993 * Returns: %TRUE if the @file was ejected successfully. %FALSE
2997 g_file_eject_mountable_finish (GFile *file,
2998 GAsyncResult *result,
3003 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3004 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3006 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3008 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3009 if (g_simple_async_result_propagate_error (simple, error))
3013 iface = G_FILE_GET_IFACE (file);
3014 return (* iface->eject_mountable_finish) (file, result, error);
3018 * g_file_monitor_directory:
3019 * @file: input #GFile.
3020 * @flags: a set of #GFileMonitorFlags.
3021 * @cancellable: optional #GCancellable object, %NULL to ignore.
3023 * If @cancellable is not %NULL, then the operation can be cancelled by
3024 * triggering the cancellable object from another thread. If the operation
3025 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3027 * Returns: a #GDirectoryMonitor for the given @file,
3028 * or %NULL on error.
3031 g_file_monitor_directory (GFile *file,
3032 GFileMonitorFlags flags,
3033 GCancellable *cancellable)
3037 g_return_val_if_fail (G_IS_FILE (file), NULL);
3039 iface = G_FILE_GET_IFACE (file);
3041 if (iface->monitor_dir == NULL)
3044 return (* iface->monitor_dir) (file, flags, cancellable);
3048 * g_file_monitor_file:
3049 * @file: input #GFile.
3050 * @flags: a set of #GFileMonitorFlags.
3051 * @cancellable: optional #GCancellable object, %NULL to ignore.
3053 * If @cancellable is not %NULL, then the operation can be cancelled by
3054 * triggering the cancellable object from another thread. If the operation
3055 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3057 * Returns: a #GFileMonitor for the given @file,
3058 * or %NULL on error.
3061 g_file_monitor_file (GFile *file,
3062 GFileMonitorFlags flags,
3063 GCancellable *cancellable)
3066 GFileMonitor *monitor;
3068 g_return_val_if_fail (G_IS_FILE (file), NULL);
3070 iface = G_FILE_GET_IFACE (file);
3074 if (iface->monitor_file)
3075 monitor = (* iface->monitor_file) (file, flags, cancellable);
3077 /* Fallback to polling */
3078 if (monitor == NULL)
3079 monitor = _g_poll_file_monitor_new (file);
3084 /********************************************
3085 * Default implementation of async ops *
3086 ********************************************/
3090 GFileQueryInfoFlags flags;
3092 } QueryInfoAsyncData;
3095 query_info_data_free (QueryInfoAsyncData *data)
3098 g_object_unref (data->info);
3099 g_free (data->attributes);
3104 query_info_async_thread (GSimpleAsyncResult *res,
3106 GCancellable *cancellable)
3108 GError *error = NULL;
3109 QueryInfoAsyncData *data;
3112 data = g_simple_async_result_get_op_res_gpointer (res);
3114 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3118 g_simple_async_result_set_from_error (res, error);
3119 g_error_free (error);
3126 g_file_real_query_info_async (GFile *file,
3127 const char *attributes,
3128 GFileQueryInfoFlags flags,
3130 GCancellable *cancellable,
3131 GAsyncReadyCallback callback,
3134 GSimpleAsyncResult *res;
3135 QueryInfoAsyncData *data;
3137 data = g_new0 (QueryInfoAsyncData, 1);
3138 data->attributes = g_strdup (attributes);
3139 data->flags = flags;
3141 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3142 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3144 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3145 g_object_unref (res);
3149 g_file_real_query_info_finish (GFile *file,
3153 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3154 QueryInfoAsyncData *data;
3156 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3158 data = g_simple_async_result_get_op_res_gpointer (simple);
3160 return g_object_ref (data->info);
3167 GFileQueryInfoFlags flags;
3168 GFileEnumerator *enumerator;
3169 } EnumerateChildrenAsyncData;
3172 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3174 if (data->enumerator)
3175 g_object_unref (data->enumerator);
3176 g_free (data->attributes);
3181 enumerate_children_async_thread (GSimpleAsyncResult *res,
3183 GCancellable *cancellable)
3185 GError *error = NULL;
3186 EnumerateChildrenAsyncData *data;
3187 GFileEnumerator *enumerator;
3189 data = g_simple_async_result_get_op_res_gpointer (res);
3191 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3193 if (enumerator == NULL)
3195 g_simple_async_result_set_from_error (res, error);
3196 g_error_free (error);
3199 data->enumerator = enumerator;
3203 g_file_real_enumerate_children_async (GFile *file,
3204 const char *attributes,
3205 GFileQueryInfoFlags flags,
3207 GCancellable *cancellable,
3208 GAsyncReadyCallback callback,
3211 GSimpleAsyncResult *res;
3212 EnumerateChildrenAsyncData *data;
3214 data = g_new0 (EnumerateChildrenAsyncData, 1);
3215 data->attributes = g_strdup (attributes);
3216 data->flags = flags;
3218 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3219 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3221 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3222 g_object_unref (res);
3225 static GFileEnumerator *
3226 g_file_real_enumerate_children_finish (GFile *file,
3230 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3231 EnumerateChildrenAsyncData *data;
3233 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3235 data = g_simple_async_result_get_op_res_gpointer (simple);
3236 if (data->enumerator)
3237 return g_object_ref (data->enumerator);
3243 open_read_async_thread (GSimpleAsyncResult *res,
3245 GCancellable *cancellable)
3248 GFileInputStream *stream;
3249 GError *error = NULL;
3251 iface = G_FILE_GET_IFACE (object);
3253 stream = iface->read (G_FILE (object), cancellable, &error);
3257 g_simple_async_result_set_from_error (res, error);
3258 g_error_free (error);
3261 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3265 g_file_real_read_async (GFile *file,
3267 GCancellable *cancellable,
3268 GAsyncReadyCallback callback,
3271 GSimpleAsyncResult *res;
3273 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3275 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3276 g_object_unref (res);
3279 static GFileInputStream *
3280 g_file_real_read_finish (GFile *file,
3284 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3287 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3289 op = g_simple_async_result_get_op_res_gpointer (simple);
3291 return g_object_ref (op);
3297 append_to_async_thread (GSimpleAsyncResult *res,
3299 GCancellable *cancellable)
3302 GFileCreateFlags *data;
3303 GFileOutputStream *stream;
3304 GError *error = NULL;
3306 iface = G_FILE_GET_IFACE (object);
3308 data = g_simple_async_result_get_op_res_gpointer (res);
3310 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3314 g_simple_async_result_set_from_error (res, error);
3315 g_error_free (error);
3318 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3322 g_file_real_append_to_async (GFile *file,
3323 GFileCreateFlags flags,
3325 GCancellable *cancellable,
3326 GAsyncReadyCallback callback,
3329 GFileCreateFlags *data;
3330 GSimpleAsyncResult *res;
3332 data = g_new0 (GFileCreateFlags, 1);
3335 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3336 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3338 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3339 g_object_unref (res);
3342 static GFileOutputStream *
3343 g_file_real_append_to_finish (GFile *file,
3347 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3350 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3352 op = g_simple_async_result_get_op_res_gpointer (simple);
3354 return g_object_ref (op);
3360 create_async_thread (GSimpleAsyncResult *res,
3362 GCancellable *cancellable)
3365 GFileCreateFlags *data;
3366 GFileOutputStream *stream;
3367 GError *error = NULL;
3369 iface = G_FILE_GET_IFACE (object);
3371 data = g_simple_async_result_get_op_res_gpointer (res);
3373 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3377 g_simple_async_result_set_from_error (res, error);
3378 g_error_free (error);
3381 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3385 g_file_real_create_async (GFile *file,
3386 GFileCreateFlags flags,
3388 GCancellable *cancellable,
3389 GAsyncReadyCallback callback,
3392 GFileCreateFlags *data;
3393 GSimpleAsyncResult *res;
3395 data = g_new0 (GFileCreateFlags, 1);
3398 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3399 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3401 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3402 g_object_unref (res);
3405 static GFileOutputStream *
3406 g_file_real_create_finish (GFile *file,
3410 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3413 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3415 op = g_simple_async_result_get_op_res_gpointer (simple);
3417 return g_object_ref (op);
3423 GFileOutputStream *stream;
3425 gboolean make_backup;
3426 GFileCreateFlags flags;
3430 replace_async_data_free (ReplaceAsyncData *data)
3433 g_object_unref (data->stream);
3434 g_free (data->etag);
3439 replace_async_thread (GSimpleAsyncResult *res,
3441 GCancellable *cancellable)
3444 GFileOutputStream *stream;
3445 GError *error = NULL;
3446 ReplaceAsyncData *data;
3448 iface = G_FILE_GET_IFACE (object);
3450 data = g_simple_async_result_get_op_res_gpointer (res);
3452 stream = iface->replace (G_FILE (object),
3461 g_simple_async_result_set_from_error (res, error);
3462 g_error_free (error);
3465 data->stream = stream;
3469 g_file_real_replace_async (GFile *file,
3471 gboolean make_backup,
3472 GFileCreateFlags flags,
3474 GCancellable *cancellable,
3475 GAsyncReadyCallback callback,
3478 GSimpleAsyncResult *res;
3479 ReplaceAsyncData *data;
3481 data = g_new0 (ReplaceAsyncData, 1);
3482 data->etag = g_strdup (etag);
3483 data->make_backup = make_backup;
3484 data->flags = flags;
3486 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3487 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3489 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3490 g_object_unref (res);
3493 static GFileOutputStream *
3494 g_file_real_replace_finish (GFile *file,
3498 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3499 ReplaceAsyncData *data;
3501 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3503 data = g_simple_async_result_get_op_res_gpointer (simple);
3505 return g_object_ref (data->stream);
3513 } SetDisplayNameAsyncData;
3516 set_display_name_data_free (SetDisplayNameAsyncData *data)
3518 g_free (data->name);
3520 g_object_unref (data->file);
3525 set_display_name_async_thread (GSimpleAsyncResult *res,
3527 GCancellable *cancellable)
3529 GError *error = NULL;
3530 SetDisplayNameAsyncData *data;
3533 data = g_simple_async_result_get_op_res_gpointer (res);
3535 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3539 g_simple_async_result_set_from_error (res, error);
3540 g_error_free (error);
3547 g_file_real_set_display_name_async (GFile *file,
3548 const char *display_name,
3550 GCancellable *cancellable,
3551 GAsyncReadyCallback callback,
3554 GSimpleAsyncResult *res;
3555 SetDisplayNameAsyncData *data;
3557 data = g_new0 (SetDisplayNameAsyncData, 1);
3558 data->name = g_strdup (display_name);
3560 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3561 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3563 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3564 g_object_unref (res);
3568 g_file_real_set_display_name_finish (GFile *file,
3572 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3573 SetDisplayNameAsyncData *data;
3575 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3577 data = g_simple_async_result_get_op_res_gpointer (simple);
3579 return g_object_ref (data->file);
3585 GFileQueryInfoFlags flags;
3592 set_info_data_free (SetInfoAsyncData *data)
3595 g_object_unref (data->info);
3597 g_error_free (data->error);
3602 set_info_async_thread (GSimpleAsyncResult *res,
3604 GCancellable *cancellable)
3606 SetInfoAsyncData *data;
3608 data = g_simple_async_result_get_op_res_gpointer (res);
3611 data->res = g_file_set_attributes_from_info (G_FILE (object),
3619 g_file_real_set_attributes_async (GFile *file,
3621 GFileQueryInfoFlags flags,
3623 GCancellable *cancellable,
3624 GAsyncReadyCallback callback,
3627 GSimpleAsyncResult *res;
3628 SetInfoAsyncData *data;
3630 data = g_new0 (SetInfoAsyncData, 1);
3631 data->info = g_file_info_dup (info);
3632 data->flags = flags;
3634 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
3635 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
3637 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
3638 g_object_unref (res);
3642 g_file_real_set_attributes_finish (GFile *file,
3647 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3648 SetInfoAsyncData *data;
3650 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
3652 data = g_simple_async_result_get_op_res_gpointer (simple);
3655 *info = g_object_ref (data->info);
3657 if (error != NULL && data->error) {
3658 *error = g_error_copy (data->error);
3664 /********************************************
3665 * Default VFS operations *
3666 ********************************************/
3669 * g_file_new_for_path:
3670 * @path: a string containing a relative or absolute path.
3672 * Constructs a #GFile for a given path. This operation never
3673 * fails, but the returned object might not support any I/O
3674 * operation if @path is malformed.
3676 * Returns: a new #GFile for the given @path.
3679 g_file_new_for_path (const char *path)
3681 g_return_val_if_fail (path != NULL, NULL);
3683 return g_vfs_get_file_for_path (g_vfs_get_default (),
3688 * g_file_new_for_uri:
3689 * @uri: a string containing a URI.
3691 * Constructs a #GFile for a given URI. This operation never
3692 * fails, but the returned object might not support any I/O
3693 * operation if @uri is malformed or if the uri type is
3696 * Returns: a #GFile for the given @uri.
3699 g_file_new_for_uri (const char *uri)
3701 g_return_val_if_fail (uri != NULL, NULL);
3703 return g_vfs_get_file_for_uri (g_vfs_get_default (),
3708 * g_file_parse_name:
3709 * @parse_name: a file name or path to be parsed.
3711 * Constructs a #GFile with the given @parse_name,
3712 * looked up by #GVfs. This operation never fails,
3713 * but the returned object might not support any I/O
3714 * operation if the @parse_name cannot be parsed by #GVfs.
3716 * Returns: a new #GFile.
3719 g_file_parse_name (const char *parse_name)
3721 g_return_val_if_fail (parse_name != NULL, NULL);
3723 return g_vfs_parse_name (g_vfs_get_default (),
3728 is_valid_scheme_character (char c)
3730 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
3734 has_valid_scheme (const char *uri)
3740 if (!is_valid_scheme_character (*p))
3745 } while (is_valid_scheme_character (*p));
3751 * g_file_new_for_commandline_arg:
3752 * @arg: a command line string.
3754 * Attempts to generate a #GFile with the given line from
3755 * the command line argument.
3757 * Returns: a new #GFile.
3760 g_file_new_for_commandline_arg (const char *arg)
3766 g_return_val_if_fail (arg != NULL, NULL);
3768 if (g_path_is_absolute (arg))
3769 return g_file_new_for_path (arg);
3771 if (has_valid_scheme (arg))
3772 return g_file_new_for_uri (arg);
3774 current_dir = g_get_current_dir ();
3775 filename = g_build_filename (current_dir, arg, NULL);
3776 g_free (current_dir);
3778 file = g_file_new_for_path (filename);
3785 * g_mount_for_location:
3786 * @location: input #GFile.
3787 * @mount_operation: a #GMountOperation.
3788 * @cancellable: optional #GCancellable object, %NULL to ignore.
3789 * @callback: a #GAsyncReadyCallback.
3790 * @user_data: a #gpointer.
3792 * Starts the @mount_operation, mounting the volume at @location.
3794 * When this operation has completed, @callback will be called with
3795 * @user_user data, and the operation can be finalized with
3796 * g_mount_for_location_finish().
3798 * If @cancellable is not %NULL, then the operation can be cancelled by
3799 * triggering the cancellable object from another thread. If the operation
3800 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3803 g_mount_for_location (GFile *location,
3804 GMountOperation *mount_operation,
3805 GCancellable *cancellable,
3806 GAsyncReadyCallback callback,
3811 g_return_if_fail (G_IS_FILE (location));
3812 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
3814 iface = G_FILE_GET_IFACE (location);
3816 if (iface->mount_for_location == NULL)
3818 g_simple_async_report_error_in_idle (G_OBJECT (location),
3819 callback, user_data,
3820 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3821 _("volume doesn't implement mount"));
3826 (* iface->mount_for_location) (location, mount_operation, cancellable, callback, user_data);
3831 * g_mount_for_location_finish:
3832 * @location: input #GFile.
3833 * @result: a #GAsyncResult.
3834 * @error: a #GError, or %NULL
3836 * Finishes a mount operation started by g_mount_for_location().
3838 * Returns: %TRUE if successful. If an error
3839 * has occured, this function will return %FALSE and set @error
3840 * appropriately if present.
3843 g_mount_for_location_finish (GFile *location,
3844 GAsyncResult *result,
3849 g_return_val_if_fail (G_IS_FILE (location), FALSE);
3850 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3852 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3854 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3855 if (g_simple_async_result_propagate_error (simple, error))
3859 iface = G_FILE_GET_IFACE (location);
3861 return (* iface->mount_for_location_finish) (location, result, error);
3864 /********************************************
3865 * Utility functions *
3866 ********************************************/
3868 #define GET_CONTENT_BLOCK_SIZE 8192
3871 * g_file_load_contents:
3872 * @file: input #GFile.
3873 * @cancellable: optional #GCancellable object, %NULL to ignore.
3874 * @contents: a location to place the contents of the file.
3875 * @length: a location to place the length of the contents of the file.
3876 * @etag_out: a location to place the current entity tag for the file.
3877 * @error: a #GError, or %NULL
3879 * If @cancellable is not %NULL, then the operation can be cancelled by
3880 * triggering the cancellable object from another thread. If the operation
3881 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3883 * Returns: %TRUE if the @file's contents were successfully loaded.
3884 * %FALSE if there were errors. The length of the loaded data will be
3885 * put into @length, the contents in @contents.
3888 g_file_load_contents (GFile *file,
3889 GCancellable *cancellable,
3895 GFileInputStream *in;
3896 GByteArray *content;
3901 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3902 g_return_val_if_fail (contents != NULL, FALSE);
3904 in = g_file_read (file,
3910 content = g_byte_array_new ();
3913 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
3914 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
3915 content->data + pos,
3916 GET_CONTENT_BLOCK_SIZE,
3917 cancellable, error)) > 0)
3920 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
3927 info = g_file_input_stream_query_info (in,
3928 G_FILE_ATTRIBUTE_ETAG_VALUE,
3933 *etag_out = g_strdup (g_file_info_get_etag (info));
3934 g_object_unref (info);
3938 /* Ignore errors on close */
3939 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
3940 g_object_unref (in);
3944 /* error is set already */
3945 g_byte_array_free (content, TRUE);
3952 /* Zero terminate (we got an extra byte allocated for this */
3953 content->data[pos] = 0;
3955 *contents = (char *)g_byte_array_free (content, FALSE);
3963 GCancellable *cancellable;
3964 GFileReadMoreCallback read_more_callback;
3965 GAsyncReadyCallback callback;
3967 GByteArray *content;
3974 load_contents_data_free (LoadContentsData *data)
3977 g_error_free (data->error);
3978 if (data->cancellable)
3979 g_object_unref (data->cancellable);
3981 g_byte_array_free (data->content, TRUE);
3982 g_free (data->etag);
3983 g_object_unref (data->file);
3988 load_contents_close_callback (GObject *obj,
3989 GAsyncResult *close_res,
3992 GInputStream *stream = G_INPUT_STREAM (obj);
3993 LoadContentsData *data = user_data;
3994 GSimpleAsyncResult *res;
3996 /* Ignore errors here, we're only reading anyway */
3997 g_input_stream_close_finish (stream, close_res, NULL);
3998 g_object_unref (stream);
4000 res = g_simple_async_result_new (G_OBJECT (data->file),
4003 g_file_load_contents_async);
4004 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4005 g_simple_async_result_complete (res);
4006 g_object_unref (res);
4010 load_contents_fstat_callback (GObject *obj,
4011 GAsyncResult *stat_res,
4014 GInputStream *stream = G_INPUT_STREAM (obj);
4015 LoadContentsData *data = user_data;
4018 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4022 data->etag = g_strdup (g_file_info_get_etag (info));
4023 g_object_unref (info);
4026 g_input_stream_close_async (stream, 0,
4028 load_contents_close_callback, data);
4032 load_contents_read_callback (GObject *obj,
4033 GAsyncResult *read_res,
4036 GInputStream *stream = G_INPUT_STREAM (obj);
4037 LoadContentsData *data = user_data;
4038 GError *error = NULL;
4041 read_size = g_input_stream_read_finish (stream, read_res, &error);
4045 /* Error or EOF, close the file */
4046 data->error = error;
4047 g_input_stream_close_async (stream, 0,
4049 load_contents_close_callback, data);
4051 else if (read_size == 0)
4053 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4054 G_FILE_ATTRIBUTE_ETAG_VALUE,
4057 load_contents_fstat_callback,
4060 else if (read_size > 0)
4062 data->pos += read_size;
4064 g_byte_array_set_size (data->content,
4065 data->pos + GET_CONTENT_BLOCK_SIZE);
4068 if (data->read_more_callback &&
4069 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4070 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4071 G_FILE_ATTRIBUTE_ETAG_VALUE,
4074 load_contents_fstat_callback,
4077 g_input_stream_read_async (stream,
4078 data->content->data + data->pos,
4079 GET_CONTENT_BLOCK_SIZE,
4082 load_contents_read_callback,
4088 load_contents_open_callback (GObject *obj,
4089 GAsyncResult *open_res,
4092 GFile *file = G_FILE (obj);
4093 GFileInputStream *stream;
4094 LoadContentsData *data = user_data;
4095 GError *error = NULL;
4096 GSimpleAsyncResult *res;
4098 stream = g_file_read_finish (file, open_res, &error);
4102 g_byte_array_set_size (data->content,
4103 data->pos + GET_CONTENT_BLOCK_SIZE);
4104 g_input_stream_read_async (G_INPUT_STREAM (stream),
4105 data->content->data + data->pos,
4106 GET_CONTENT_BLOCK_SIZE,
4109 load_contents_read_callback,
4115 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4119 g_simple_async_result_complete (res);
4120 g_error_free (error);
4121 load_contents_data_free (data);
4122 g_object_unref (res);
4127 * g_file_load_partial_contents_async:
4128 * @file: input #GFile.
4129 * @cancellable: optional #GCancellable object, %NULL to ignore.
4130 * @read_more_callback: a #GFileReadMoreCallback.
4131 * @callback: a #GAsyncReadyCallback.
4132 * @user_data: a #gpointer.
4134 * If @cancellable is not %NULL, then the operation can be cancelled by
4135 * triggering the cancellable object from another thread. If the operation
4136 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4139 g_file_load_partial_contents_async (GFile *file,
4140 GCancellable *cancellable,
4141 GFileReadMoreCallback read_more_callback,
4142 GAsyncReadyCallback callback,
4145 LoadContentsData *data;
4147 g_return_if_fail (G_IS_FILE (file));
4149 data = g_new0 (LoadContentsData, 1);
4152 data->cancellable = g_object_ref (cancellable);
4153 data->read_more_callback = read_more_callback;
4154 data->callback = callback;
4155 data->user_data = user_data;
4156 data->content = g_byte_array_new ();
4157 data->file = g_object_ref (file);
4159 g_file_read_async (file,
4162 load_contents_open_callback,
4167 * g_file_load_partial_contents_finish:
4168 * @file: input #GFile.
4169 * @res: a #GAsyncResult.
4170 * @contents: a location to place the contents of the file.
4171 * @length: a location to place the length of the contents of the file.
4172 * @etag_out: a location to place the current entity tag for the file.
4173 * @error: a #GError, or %NULL
4175 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4176 * present, it will be set appropriately.
4179 g_file_load_partial_contents_finish (GFile *file,
4186 GSimpleAsyncResult *simple;
4187 LoadContentsData *data;
4189 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4190 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4191 g_return_val_if_fail (contents != NULL, FALSE);
4193 simple = G_SIMPLE_ASYNC_RESULT (res);
4195 if (g_simple_async_result_propagate_error (simple, error))
4198 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4200 data = g_simple_async_result_get_op_res_gpointer (simple);
4204 g_propagate_error (error, data->error);
4213 *length = data->pos;
4217 *etag_out = data->etag;
4221 /* Zero terminate */
4222 g_byte_array_set_size (data->content,
4224 data->content->data[data->pos] = 0;
4226 *contents = (char *)g_byte_array_free (data->content, FALSE);
4227 data->content = NULL;
4233 * g_file_load_contents_async:
4234 * @file: input #GFile.
4235 * @cancellable: optional #GCancellable object, %NULL to ignore.
4236 * @callback: a #GAsyncReadyCallback.
4237 * @user_data: a #gpointer.
4239 * Starts an asynchronous load of the @file's contents. When the load operation
4240 * has completed, @callback will be called with @userdata. To finish
4241 * the operation, call g_file_load_contents_finish() with the
4242 * #GAsyncResult returned by the @callback.
4244 * If @cancellable is not %NULL, then the operation can be cancelled by
4245 * triggering the cancellable object from another thread. If the operation
4246 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4249 g_file_load_contents_async (GFile *file,
4250 GCancellable *cancellable,
4251 GAsyncReadyCallback callback,
4254 g_file_load_partial_contents_async (file,
4257 callback, user_data);
4262 * g_file_load_contents_finish:
4263 * @file: input #GFile.
4264 * @res: a #GAsyncResult.
4265 * @contents: a location to place the contents of the file.
4266 * @length: a location to place the length of the contents of the file.
4267 * @etag_out: a location to place the current entity tag for the file.
4268 * @error: a #GError, or %NULL
4270 * Finishes an asynchronous load of the @file's contents. The contents
4271 * are placed in @contents, and @length is set to the size of the @contents
4272 * string. If @etag_out is present, it will be set to the new entity
4273 * tag for the @file.
4275 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4276 * present, it will be set appropriately.
4279 g_file_load_contents_finish (GFile *file,
4286 return g_file_load_partial_contents_finish (file,
4296 * g_file_replace_contents:
4297 * @file: input #GFile.
4298 * @contents: a string containing the new contents for @file.
4299 * @length: the length of @contents in bytes.
4300 * @etag: the old entity tag for the document.
4301 * @make_backup: a #gboolean.
4302 * @flags: a set of #GFileCreateFlags.
4303 * @new_etag: a location to a new entity tag for the document.
4304 * @cancellable: optional #GCancellable object, %NULL to ignore.
4305 * @error: a #GError, or %NULL
4307 * Replaces the contents of @file with @contents of @length bytes. The old
4308 * @etag will be replaced with the @new_etag. If @make_backup is %TRUE,
4309 * this function will attempt to make a backup of @file.
4311 * If @cancellable is not %NULL, then the operation can be cancelled by
4312 * triggering the cancellable object from another thread. If the operation
4313 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4315 * Returns: %TRUE if successful. If an error
4316 * has occured, this function will return %FALSE and set @error
4317 * appropriately if present.
4320 g_file_replace_contents (GFile *file,
4321 const char *contents,
4324 gboolean make_backup,
4325 GFileCreateFlags flags,
4327 GCancellable *cancellable,
4330 GFileOutputStream *out;
4331 gsize pos, remainder;
4334 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4335 g_return_val_if_fail (contents != NULL, FALSE);
4337 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4343 while (remainder > 0 &&
4344 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4346 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4354 if (remainder > 0 && res < 0)
4356 /* Ignore errors on close */
4357 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4359 /* error is set already */
4363 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4367 *new_etag = g_file_output_stream_get_etag (out);
4375 GCancellable *cancellable;
4376 GAsyncReadyCallback callback;
4378 const char *content;
4382 } ReplaceContentsData;
4385 replace_contents_data_free (ReplaceContentsData *data)
4388 g_error_free (data->error);
4389 if (data->cancellable)
4390 g_object_unref (data->cancellable);
4391 g_object_unref (data->file);
4392 g_free (data->etag);
4397 replace_contents_close_callback (GObject *obj,
4398 GAsyncResult *close_res,
4401 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4402 ReplaceContentsData *data = user_data;
4403 GSimpleAsyncResult *res;
4405 /* Ignore errors here, we're only reading anyway */
4406 g_output_stream_close_finish (stream, close_res, NULL);
4407 g_object_unref (stream);
4409 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4411 res = g_simple_async_result_new (G_OBJECT (data->file),
4414 g_file_replace_contents_async);
4415 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4416 g_simple_async_result_complete (res);
4417 g_object_unref (res);
4421 replace_contents_write_callback (GObject *obj,
4422 GAsyncResult *read_res,
4425 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4426 ReplaceContentsData *data = user_data;
4427 GError *error = NULL;
4430 write_size = g_output_stream_write_finish (stream, read_res, &error);
4432 if (write_size <= 0)
4434 /* Error or EOF, close the file */
4436 data->error = error;
4437 g_output_stream_close_async (stream, 0,
4439 replace_contents_close_callback, data);
4441 else if (write_size > 0)
4443 data->pos += write_size;
4445 if (data->pos >= data->length)
4446 g_output_stream_close_async (stream, 0,
4448 replace_contents_close_callback, data);
4450 g_output_stream_write_async (stream,
4451 data->content + data->pos,
4452 data->length - data->pos,
4455 replace_contents_write_callback,
4461 replace_contents_open_callback (GObject *obj,
4462 GAsyncResult *open_res,
4465 GFile *file = G_FILE (obj);
4466 GFileOutputStream *stream;
4467 ReplaceContentsData *data = user_data;
4468 GError *error = NULL;
4469 GSimpleAsyncResult *res;
4471 stream = g_file_replace_finish (file, open_res, &error);
4475 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4476 data->content + data->pos,
4477 data->length - data->pos,
4480 replace_contents_write_callback,
4486 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4490 g_simple_async_result_complete (res);
4491 g_error_free (error);
4492 replace_contents_data_free (data);
4493 g_object_unref (res);
4498 * g_file_replace_contents_async:
4499 * @file: input #GFile.
4500 * @contents: string of contents to replace the file with.
4501 * @length: the length of @contents in bytes.
4502 * @etag: a new entity tag for the @file.
4503 * @make_backup: a #gboolean.
4504 * @flags: a set of #GFileCreateFlags.
4505 * @cancellable: optional #GCancellable object, %NULL to ignore.
4506 * @callback: a #GAsyncReadyCallback.
4507 * @user_data: a #gpointer.
4509 * Starts an asynchronous replacement of @file with the given
4510 * @contents of @length bytes. @etag will replace the document's
4511 * current entity tag.
4513 * When this operation has completed, @callback will be called with
4514 * @user_user data, and the operation can be finalized with
4515 * g_file_replace_contents_finish().
4517 * If @cancellable is not %NULL, then the operation can be cancelled by
4518 * triggering the cancellable object from another thread. If the operation
4519 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4521 * If @make_backup is %TRUE, this function will attempt to
4522 * make a backup of @file.
4525 g_file_replace_contents_async (GFile *file,
4526 const char *contents,
4529 gboolean make_backup,
4530 GFileCreateFlags flags,
4531 GCancellable *cancellable,
4532 GAsyncReadyCallback callback,
4535 ReplaceContentsData *data;
4537 g_return_if_fail (G_IS_FILE (file));
4538 g_return_if_fail (contents != NULL);
4540 data = g_new0 (ReplaceContentsData, 1);
4543 data->cancellable = g_object_ref (cancellable);
4544 data->callback = callback;
4545 data->user_data = user_data;
4546 data->content = contents;
4547 data->length = length;
4549 data->file = g_object_ref (file);
4551 g_file_replace_async (file,
4557 replace_contents_open_callback,
4562 * g_file_replace_contents_finish:
4563 * @file: input #GFile.
4564 * @res: a #GAsyncResult.
4565 * @new_etag: a location of a new entity tag for the document.
4566 * @error: a #GError, or %NULL
4568 * Finishes an asynchronous replace of the given @file. Sets
4569 * @new_etag to the new entity tag for the document, if present.
4571 * Returns: %TRUE on success, %FALSE on failure.
4574 g_file_replace_contents_finish (GFile *file,
4579 GSimpleAsyncResult *simple;
4580 ReplaceContentsData *data;
4582 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4583 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4585 simple = G_SIMPLE_ASYNC_RESULT (res);
4587 if (g_simple_async_result_propagate_error (simple, error))
4590 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
4592 data = g_simple_async_result_get_op_res_gpointer (simple);
4596 g_propagate_error (error, data->error);
4604 *new_etag = data->etag;
4605 data->etag = NULL; /* Take ownership */
4611 #define __G_FILE_C__
4612 #include "gioaliasdef.c"