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"
39 * @short_description: File and Directory Handling
40 * @see_also: #GFileInfo, #GFileEnumerator
41 * @include: gio/gfile.h
43 * #GFile is a high level abstraction for manipulating files on a
44 * virtual file system. #GFile<!-- -->s are lightweight, immutable
45 * objects that do no I/O upon creation. It is necessary to understand that
46 * #GFile objects do not represent files, merely a handle to a file. All
47 * file I/O is implemented as streams (see #GInputStream and #GOutputStream).
49 * To construct a #GFile, you can use:
50 * g_file_new_for_path() if you have a path.
51 * g_file_new_for_uri() if you have a URI.
53 * You can move through the filesystem with #GFile handles with
54 * g_file_get_parent() to get a handle to the parent directory.
55 * g_file_get_child() to get a handle to a child within a directory.
56 * g_file_resolve_relative_path() to resolve a relative path between
57 * two #GFile<!-- -->s.
59 * Many #GFile operations have both synchronous (or blocking) and
60 * asynchronous (non-blocking) versions to suit your application.
61 * Asynchronous versions of synchronous functions simply have _async()
62 * appended to their function names. The asynchronous I/O functions
63 * call a #GAsyncReadyCallback which is then used to finalize the operation,
64 * which is then passed to the function's matching _finish() operation.
70 static void g_file_base_init (gpointer g_class);
71 static void g_file_class_init (gpointer g_class,
74 static void g_file_real_query_info_async (GFile *file,
75 const char *attributes,
76 GFileQueryInfoFlags flags,
78 GCancellable *cancellable,
79 GAsyncReadyCallback callback,
81 static GFileInfo * g_file_real_query_info_finish (GFile *file,
84 static void g_file_real_enumerate_children_async (GFile *file,
85 const char *attributes,
86 GFileQueryInfoFlags flags,
88 GCancellable *cancellable,
89 GAsyncReadyCallback callback,
91 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
94 static void g_file_real_read_async (GFile *file,
96 GCancellable *cancellable,
97 GAsyncReadyCallback callback,
99 static GFileInputStream * g_file_real_read_finish (GFile *file,
102 static void g_file_real_append_to_async (GFile *file,
103 GFileCreateFlags flags,
105 GCancellable *cancellable,
106 GAsyncReadyCallback callback,
108 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
111 static void g_file_real_create_async (GFile *file,
112 GFileCreateFlags flags,
114 GCancellable *cancellable,
115 GAsyncReadyCallback callback,
117 static GFileOutputStream *g_file_real_create_finish (GFile *file,
120 static void g_file_real_replace_async (GFile *file,
122 gboolean make_backup,
123 GFileCreateFlags flags,
125 GCancellable *cancellable,
126 GAsyncReadyCallback callback,
128 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
131 static gboolean g_file_real_set_attributes_from_info (GFile *file,
133 GFileQueryInfoFlags flags,
134 GCancellable *cancellable,
136 static void g_file_real_set_display_name_async (GFile *file,
137 const char *display_name,
139 GCancellable *cancellable,
140 GAsyncReadyCallback callback,
142 static GFile * g_file_real_set_display_name_finish (GFile *file,
145 static void g_file_real_set_attributes_async (GFile *file,
147 GFileQueryInfoFlags flags,
149 GCancellable *cancellable,
150 GAsyncReadyCallback callback,
152 static gboolean g_file_real_set_attributes_finish (GFile *file,
158 g_file_get_type (void)
160 static GType file_type = 0;
164 static const GTypeInfo file_info =
166 sizeof (GFileIface), /* class_size */
167 g_file_base_init, /* base_init */
168 NULL, /* base_finalize */
170 NULL, /* class_finalize */
171 NULL, /* class_data */
178 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
181 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
188 g_file_class_init (gpointer g_class,
191 GFileIface *iface = g_class;
193 iface->enumerate_children_async = g_file_real_enumerate_children_async;
194 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
195 iface->set_display_name_async = g_file_real_set_display_name_async;
196 iface->set_display_name_finish = g_file_real_set_display_name_finish;
197 iface->query_info_async = g_file_real_query_info_async;
198 iface->query_info_finish = g_file_real_query_info_finish;
199 iface->set_attributes_async = g_file_real_set_attributes_async;
200 iface->set_attributes_finish = g_file_real_set_attributes_finish;
201 iface->read_async = g_file_real_read_async;
202 iface->read_finish = g_file_real_read_finish;
203 iface->append_to_async = g_file_real_append_to_async;
204 iface->append_to_finish = g_file_real_append_to_finish;
205 iface->create_async = g_file_real_create_async;
206 iface->create_finish = g_file_real_create_finish;
207 iface->replace_async = g_file_real_replace_async;
208 iface->replace_finish = g_file_real_replace_finish;
209 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
213 g_file_base_init (gpointer g_class)
220 * @file: input #GFile.
222 * Checks to see if a file is native to the system.
224 * Returns: %TRUE if file is native. (If the file is native
225 * to the system, e.g. located in file:///).
228 g_file_is_native (GFile *file)
232 g_return_val_if_fail (G_IS_FILE (file), FALSE);
234 iface = G_FILE_GET_IFACE (file);
236 return (* iface->is_native) (file);
241 * g_file_has_uri_scheme:
242 * @file: input #GFile.
243 * @uri_scheme: a string containing a URI scheme.
245 * Checks to see if a #GFile has a given URI scheme.
247 * Returns: %TRUE if #GFile's backend supports the
248 * given URI scheme, FALSE if URI scheme is %NULL,
249 * not supported, or #GFile is invalid.
252 g_file_has_uri_scheme (GFile *file,
253 const char *uri_scheme)
257 g_return_val_if_fail (G_IS_FILE (file), FALSE);
258 g_return_val_if_fail (uri_scheme != NULL, FALSE);
260 iface = G_FILE_GET_IFACE (file);
262 return (* iface->has_uri_scheme) (file, uri_scheme);
267 * g_file_get_uri_scheme:
268 * @file: input #GFile.
270 * Gets the URI scheme for a #GFile.
272 * Returns: string to the URI scheme for the given #GFile.
273 * RFC 3986 decodes the scheme as:
274 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
275 * Common schemes include "file", "http", "svn", etc. The returned
276 * string should be freed when no longer needed.
279 g_file_get_uri_scheme (GFile *file)
283 g_return_val_if_fail (G_IS_FILE (file), NULL);
285 iface = G_FILE_GET_IFACE (file);
287 return (* iface->get_uri_scheme) (file);
292 * g_file_get_basename:
293 * @file: input #GFile.
295 * Gets the basename for a given #GFile.
297 * Returns: string containing the #GFile's base name, or %NULL if given
298 * #GFile is invalid. The returned string should be freed when no longer
302 g_file_get_basename (GFile *file)
306 g_return_val_if_fail (G_IS_FILE (file), NULL);
308 iface = G_FILE_GET_IFACE (file);
310 return (* iface->get_basename) (file);
315 * @file: input #GFile.
317 * Gets the current path within a #GFile.
319 * Returns: string containing the #GFile's path, or %NULL if given #GFile is
320 * invalid. The returned string should be freed when no longer needed.
323 g_file_get_path (GFile *file)
327 g_return_val_if_fail (G_IS_FILE (file), NULL);
329 iface = G_FILE_GET_IFACE (file);
331 return (* iface->get_path) (file);
336 * @file: input #GFile.
338 * Gets a URI for the path within a #GFile.
340 * Returns: string to the #GFile's Universal Resource Identifier (URI),
341 * or %NULL if given #GFile is invalid. The returned string should be freed
342 * when no longer needed.
345 g_file_get_uri (GFile *file)
349 g_return_val_if_fail (G_IS_FILE (file), NULL);
351 iface = G_FILE_GET_IFACE (file);
353 return (* iface->get_uri) (file);
357 * g_file_get_parse_name:
358 * @file: input #GFile.
360 * Gets the parsed name for the #GFile.
362 * Returns: string to the #GFile's parsed name, or %NULL if given
363 * #GFile is invalid. The returned string should be freed when no longer needed.
366 g_file_get_parse_name (GFile *file)
370 g_return_val_if_fail (G_IS_FILE (file), NULL);
372 iface = G_FILE_GET_IFACE (file);
374 return (* iface->get_parse_name) (file);
379 * @file: input #GFile.
381 * Duplicates a #GFile handle. This operation does not duplicate
382 * the actual file or directory represented by the #GFile; see g_file_copy()
383 * if attempting to copy a file.
385 * Returns: #GFile that is a duplicate of the given #GFile,
386 * or %NULL if given #GFile is invalid.
389 g_file_dup (GFile *file)
393 g_return_val_if_fail (G_IS_FILE (file), NULL);
395 iface = G_FILE_GET_IFACE (file);
397 return (* iface->dup) (file);
402 * @file: #gconstpointer to a #GFile.
404 * Creates a hash of a #GFile.
406 * Returns: 0 if @file is not a valid #GFile, otherwise a
407 * guint containing a hash of the #GFile. This function
408 * is intended for easily hashing a #GFile to add to a
409 * #GHashTable or similar data structure.
413 g_file_hash (gconstpointer file)
417 g_return_val_if_fail (G_IS_FILE (file), 0);
419 iface = G_FILE_GET_IFACE (file);
421 return (* iface->hash) ((GFile *)file);
426 * @file1: the first #GFile.
427 * @file2: the second #GFile.
429 * Checks equality of two given #GFile<!-- -->s
431 * Returns: %TRUE if @file1 and @file2 are equal.
432 * %FALSE if either is not a #GFile.
435 g_file_equal (GFile *file1,
440 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
441 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
443 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
446 iface = G_FILE_GET_IFACE (file1);
448 return (* iface->equal) (file1, file2);
454 * @file: input #GFile.
456 * Gets the parent directory for the @file.
457 * If the @file represents the root directory of the
458 * file system, then %NULL will be returned.
460 * Returns: a #GFile structure to the parent of the given
464 g_file_get_parent (GFile *file)
468 g_return_val_if_fail (G_IS_FILE (file), NULL);
470 iface = G_FILE_GET_IFACE (file);
472 return (* iface->get_parent) (file);
477 * @file: input #GFile.
478 * @name: string containing the child's name.
480 * Gets a specific child of @file with name equal to @name if
483 * Returns: a #GFile to a child specified by
484 * @name or %NULL if @name is %NULL, or the specified
485 * child doesn't exist.
488 g_file_get_child (GFile *file,
491 g_return_val_if_fail (G_IS_FILE (file), NULL);
492 g_return_val_if_fail (name != NULL, NULL);
494 return g_file_resolve_relative_path (file, name);
498 * g_file_get_child_for_display_name:
499 * @file: input #GFile.
500 * @display_name: string to a possible child.
503 * Gets the child of @file for a given @display_name. If
504 * this function fails, it returns %NULL and @error will be
505 * set with %G_IO_ERROR_INVALID_FILENAME.
507 * Returns: a #GFile to the specified child, or
508 * %NULL if @display_name is %NULL.
511 g_file_get_child_for_display_name (GFile *file,
512 const char *display_name,
517 g_return_val_if_fail (G_IS_FILE (file), NULL);
518 g_return_val_if_fail (display_name != NULL, NULL);
520 iface = G_FILE_GET_IFACE (file);
522 return (* iface->get_child_for_display_name) (file, display_name, error);
526 * g_file_contains_file:
527 * @parent: input #GFile.
528 * @descendant: input #GFile.
530 * Checks whether @parent contains the specified @descendent.
532 * Returns: %TRUE if the @descendent's parent is @parent. %FALSE otherwise.
535 g_file_contains_file (GFile *parent,
540 g_return_val_if_fail (G_IS_FILE (parent), FALSE);
541 g_return_val_if_fail (G_IS_FILE (descendant), FALSE);
543 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
546 iface = G_FILE_GET_IFACE (parent);
548 return (* iface->contains_file) (parent, descendant);
552 * g_file_get_relative_path:
553 * @parent: input #GFile.
554 * @descendant: input #GFile.
556 * Gets the path for @descendant relative to @parent.
558 * Returns: string with the relative path from @descendant to @parent.
559 * The returned string should be freed when no longer needed.
562 g_file_get_relative_path (GFile *parent,
567 g_return_val_if_fail (G_IS_FILE (parent), NULL);
568 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
570 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
573 iface = G_FILE_GET_IFACE (parent);
575 return (* iface->get_relative_path) (parent, descendant);
579 * g_file_resolve_relative_path:
580 * @file: input #GFile.
581 * @relative_path: a given relative path string.
583 * Resolves a relative path for @file to an absolute path.
585 * Returns: #GFile to the resolved path. %NULL if @relative_path is NULL.
586 * or if @file is invalid.
589 g_file_resolve_relative_path (GFile *file,
590 const char *relative_path)
594 g_return_val_if_fail (G_IS_FILE (file), NULL);
595 g_return_val_if_fail (relative_path != NULL, NULL);
597 iface = G_FILE_GET_IFACE (file);
599 return (* iface->resolve_relative_path) (file, relative_path);
603 * g_file_enumerate_children:
604 * @file: input #GFile.
605 * @attributes: a string containing a #GFileAttributeInfo query.
606 * @flags: a set of #GFileQueryInfoFlags.
607 * @cancellable: optional #GCancellable object, %NULL to ignore.
608 * @error: #GError for error reporting.
610 * Gets a #GFileEnumerator for the children of @file that match @attributes,
611 * where attributes is a #GFileAttributeInfo query string (e.g. "std:type", "std:*").
613 * If @cancellable is not %NULL, then the operation can be cancelled by
614 * triggering the cancellable object from another thread. If the operation
615 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
617 * If the #GFileIface for the given @file does not support #GFileEnumerator,
618 * then %NULL will be returned and the error %G_IO_ERROR_NOT_SUPPORTED will
621 * Returns: A #GFileEnumerator if successful. %NULL if cancelled or if #GFile's
622 * backend doesn't support #GFileEnumerator.
625 g_file_enumerate_children (GFile *file,
626 const char *attributes,
627 GFileQueryInfoFlags flags,
628 GCancellable *cancellable,
634 g_return_val_if_fail (G_IS_FILE (file), NULL);
636 if (g_cancellable_set_error_if_cancelled (cancellable, error))
639 iface = G_FILE_GET_IFACE (file);
641 if (iface->enumerate_children == NULL)
643 g_set_error (error, G_IO_ERROR,
644 G_IO_ERROR_NOT_SUPPORTED,
645 _("Operation not supported"));
649 return (* iface->enumerate_children) (file, attributes, flags,
654 * g_file_enumerate_children_async:
655 * @file: input #GFile.
656 * @attributes: a string containing a #GFileAttributeInfo query.
657 * @flags: a set of #GFileQueryInfoFlags.
658 * @io_priority: the io priority of the request.
659 * @cancellable: optional #GCancellable object, %NULL to ignore.
660 * @callback: a #GAsyncReadyCallback.
661 * @user_data: a #gpointer.
663 * Asynchronously gets a #GFileEnumerator for the children of @file that match @attributes,
664 * where attributes is a #GFileAttributeInfo query string (e.g. "std:type", "std:*"). For the
665 * synchronous version of this function, see g_file_enumerate_children().
667 * To finish this asynchronous operation, see g_file_enumerate_children_finish().
671 g_file_enumerate_children_async (GFile *file,
672 const char *attributes,
673 GFileQueryInfoFlags flags,
675 GCancellable *cancellable,
676 GAsyncReadyCallback callback,
681 g_return_if_fail (G_IS_FILE (file));
683 iface = G_FILE_GET_IFACE (file);
684 (* iface->enumerate_children_async) (file,
694 * g_file_enumerate_children_finish:
695 * @file: input #GFile.
696 * @res: a #GAsyncResult.
699 * If @cancellable was not %NULL when g_file_enumerate_children_async() was called,
700 * then the operation could have been cancelled by triggering the cancellable object
701 * from another thread. If the operation was cancelled, the @error will be set to
702 * %G_IO_ERROR_CANCELLED and this function will return %NULL.
704 * If the #GFileIface for the given @file does not support enumerating files,
705 * then %NULL will be returned and the error %G_IO_ERROR_NOT_SUPPORTED will
708 * Returns: a #GFileEnumerator or %NULL if an error occurred.
711 g_file_enumerate_children_finish (GFile *file,
717 g_return_val_if_fail (G_IS_FILE (file), NULL);
718 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
720 if (G_IS_SIMPLE_ASYNC_RESULT (res))
722 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
723 if (g_simple_async_result_propagate_error (simple, error))
727 iface = G_FILE_GET_IFACE (file);
728 return (* iface->enumerate_children_finish) (file, res, error);
734 * @file: input #GFile.
735 * @attributes: a string containing a #GFileAttributeInfo query.
736 * @flags: a set of #GFileQueryInfoFlags.
737 * @cancellable: optional #GCancellable object, %NULL to ignore.
740 * If @cancellable is not %NULL, then the operation can be cancelled by
741 * triggering the cancellable object from another thread. If the operation
742 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
744 * If the #GFileIface for the given @file does not support querying file information,
745 * then %NULL will be returned and the error %G_IO_ERROR_NOT_SUPPORTED will
748 * Returns: a #GFileInfo for the given @file, or %NULL on error.
751 g_file_query_info (GFile *file,
752 const char *attributes,
753 GFileQueryInfoFlags flags,
754 GCancellable *cancellable,
759 g_return_val_if_fail (G_IS_FILE (file), NULL);
761 if (g_cancellable_set_error_if_cancelled (cancellable, error))
764 iface = G_FILE_GET_IFACE (file);
766 if (iface->query_info == NULL)
768 g_set_error (error, G_IO_ERROR,
769 G_IO_ERROR_NOT_SUPPORTED,
770 _("Operation not supported"));
774 return (* iface->query_info) (file, attributes, flags, cancellable, error);
778 * g_file_query_info_async:
779 * @file: input #GFile.
780 * @attributes: a string containing a #GFileAttributeInfo query.
781 * @flags: a set of #GFileQueryInfoFlags.
782 * @io_priority: the io priority of the request. the io priority of the request.
783 * @cancellable: optional #GCancellable object, %NULL to ignore.
784 * @callback: a #GAsyncReadyCallback.
785 * @user_data: a #gpointer.
787 * If @cancellable is not %NULL, then the operation can be cancelled by
788 * triggering the cancellable object from another thread. If the operation
789 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
791 * To finish this asynchronous operation, see g_file_query_info_finish().
795 g_file_query_info_async (GFile *file,
796 const char *attributes,
797 GFileQueryInfoFlags flags,
799 GCancellable *cancellable,
800 GAsyncReadyCallback callback,
805 g_return_if_fail (G_IS_FILE (file));
807 iface = G_FILE_GET_IFACE (file);
808 (* iface->query_info_async) (file,
818 * g_file_query_info_finish:
819 * @file: input #GFile.
820 * @res: a #GAsyncResult.
823 * Finishes an asynchronous file info query.
825 * If @cancellable was not %NULL when g_file_query_info_async() was called,
826 * then the operation could have been cancelled by triggering the cancellable object
827 * from another thread. If the operation was cancelled, the @error will be set to
828 * %G_IO_ERROR_CANCELLED and this function will return %NULL.
830 * If the #GFileIface for the given @file does not support querying file information,
831 * then %NULL will be returned and the error %G_IO_ERROR_NOT_SUPPORTED will
834 * Returns: #GFileInfo for given @file or %NULL on error.
837 g_file_query_info_finish (GFile *file,
843 g_return_val_if_fail (G_IS_FILE (file), NULL);
844 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
846 if (G_IS_SIMPLE_ASYNC_RESULT (res))
848 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
849 if (g_simple_async_result_propagate_error (simple, error))
853 iface = G_FILE_GET_IFACE (file);
854 return (* iface->query_info_finish) (file, res, error);
858 * g_file_query_filesystem_info:
859 * @file: input #GFile.
860 * @attributes: a string containing a #GFileAttributeInfo query.
861 * @cancellable: optional #GCancellable object, %NULL to ignore.
864 * If @cancellable is not %NULL, then the operation can be cancelled by
865 * triggering the cancellable object from another thread. If the operation
866 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
868 * If the #GFileIface for the given @file does not support querying file system
869 * information, then %NULL will be returned and the error
870 * %G_IO_ERROR_NOT_SUPPORTED will be set in @error.
872 * Returns: a #GFileInfo or %NULL if there was an error.
875 g_file_query_filesystem_info (GFile *file,
876 const char *attributes,
877 GCancellable *cancellable,
882 g_return_val_if_fail (G_IS_FILE (file), NULL);
884 if (g_cancellable_set_error_if_cancelled (cancellable, error))
887 iface = G_FILE_GET_IFACE (file);
889 if (iface->query_filesystem_info == NULL)
891 g_set_error (error, G_IO_ERROR,
892 G_IO_ERROR_NOT_SUPPORTED,
893 _("Operation not supported"));
897 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
901 * g_file_find_enclosing_volume:
902 * @file: input #GFile.
903 * @cancellable: optional #GCancellable object, %NULL to ignore.
906 * Gets a #GVolume for the #GFile.
908 * If the #GFileIface for @file does not have a volume (e.g. possibly a
909 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
912 * If @cancellable is not %NULL, then the operation can be cancelled by
913 * triggering the cancellable object from another thread. If the operation
914 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
916 * Returns: a #GVolume where the @file is located or %NULL on error.
919 g_file_find_enclosing_volume (GFile *file,
920 GCancellable *cancellable,
925 g_return_val_if_fail (G_IS_FILE (file), NULL);
927 if (g_cancellable_set_error_if_cancelled (cancellable, error))
930 iface = G_FILE_GET_IFACE (file);
931 if (iface->find_enclosing_volume == NULL)
933 g_set_error (error, G_IO_ERROR,
934 G_IO_ERROR_NOT_FOUND,
935 _("Containing volume does not exist"));
939 return (* iface->find_enclosing_volume) (file, cancellable, error);
944 * @file: #GFile to read.
945 * @cancellable: a #GCancellable
946 * @error: a #GError, or %NULL
948 * Reads a whole file into a #GFileInputStream. Fails returning %NULL if
949 * given #GFile points to a directory.
951 * If the #GFileIface for @file does not support reading files, then
952 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
954 * If @cancellable is not %NULL, then the operation can be cancelled by
955 * triggering the cancellable object from another thread. If the operation
956 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
958 * Returns: #GFileInputStream or %NULL on error.
961 g_file_read (GFile *file,
962 GCancellable *cancellable,
967 g_return_val_if_fail (G_IS_FILE (file), NULL);
969 if (g_cancellable_set_error_if_cancelled (cancellable, error))
972 iface = G_FILE_GET_IFACE (file);
974 if (iface->read == NULL)
976 g_set_error (error, G_IO_ERROR,
977 G_IO_ERROR_NOT_SUPPORTED,
978 _("Operation not supported"));
982 return (* iface->read) (file, cancellable, error);
987 * @file: input #GFile.
988 * @flags: a set of #GFileCreateFlags.
989 * @cancellable: optional #GCancellable object, %NULL to ignore.
990 * @error: a #GError, or %NULL
992 * If the #GFileIface for @file does not support appending to files, then
993 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
995 * If @cancellable is not %NULL, then the operation can be cancelled by
996 * triggering the cancellable object from another thread. If the operation
997 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
999 * Returns: a #GFileOutputStream.
1002 g_file_append_to (GFile *file,
1003 GFileCreateFlags flags,
1004 GCancellable *cancellable,
1009 g_return_val_if_fail (G_IS_FILE (file), NULL);
1011 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1014 iface = G_FILE_GET_IFACE (file);
1016 if (iface->append_to == NULL)
1018 g_set_error (error, G_IO_ERROR,
1019 G_IO_ERROR_NOT_SUPPORTED,
1020 _("Operation not supported"));
1024 return (* iface->append_to) (file, flags, cancellable, error);
1029 * @file: input #GFile.
1030 * @flags: a set of #GFileCreateFlags.
1031 * @cancellable: optional #GCancellable object, %NULL to ignore.
1032 * @error: a #GError, or %NULL
1034 * If the #GFileIface for @file does not support creating files, then
1035 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
1037 * If @cancellable is not %NULL, then the operation can be cancelled by
1038 * triggering the cancellable object from another thread. If the operation
1039 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1041 * Returns: a #GFileOutputStream for the newly created file, or
1045 g_file_create (GFile *file,
1046 GFileCreateFlags flags,
1047 GCancellable *cancellable,
1052 g_return_val_if_fail (G_IS_FILE (file), NULL);
1054 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1057 iface = G_FILE_GET_IFACE (file);
1059 if (iface->create == NULL)
1061 g_set_error (error, G_IO_ERROR,
1062 G_IO_ERROR_NOT_SUPPORTED,
1063 _("Operation not supported"));
1067 return (* iface->create) (file, flags, cancellable, error);
1072 * @file: input #GFile.
1073 * @etag: an Entity Tag for the current #GFile.
1074 * @make_backup: a #gboolean.
1075 * @flags: a set of #GFileCreateFlags.
1076 * @cancellable: optional #GCancellable object, %NULL to ignore.
1077 * @error: a #GError, or %NULL
1079 * If the #GFileIface for @file does not support streaming operations, then
1080 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
1082 * If @cancellable is not %NULL, then the operation can be cancelled by
1083 * triggering the cancellable object from another thread. If the operation
1084 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1086 * @etag will replace the entity tag for the current file.
1088 * Returns: a #GFileOutputStream or %NULL on error. If @make_backup is %TRUE,
1089 * this function will attempt to make a backup of the current file.
1092 g_file_replace (GFile *file,
1094 gboolean make_backup,
1095 GFileCreateFlags flags,
1096 GCancellable *cancellable,
1101 g_return_val_if_fail (G_IS_FILE (file), NULL);
1103 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1106 iface = G_FILE_GET_IFACE (file);
1108 if (iface->replace == NULL)
1110 g_set_error (error, G_IO_ERROR,
1111 G_IO_ERROR_NOT_SUPPORTED,
1112 _("Operation not supported"));
1117 /* Handle empty tag string as NULL in consistent way. */
1118 if (etag && *etag == 0)
1121 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1125 * g_file_read_async:
1126 * @file: input #GFile.
1127 * @io_priority: the io priority of the request.
1128 * @cancellable: optional #GCancellable object, %NULL to ignore.
1129 * @callback: a #GAsyncReadyCallback.
1130 * @user_data: a #gpointer.
1132 * Asynchronously reads @file. For the synchronous version of this function,
1133 * see g_file_read().
1137 g_file_read_async (GFile *file,
1139 GCancellable *cancellable,
1140 GAsyncReadyCallback callback,
1145 g_return_if_fail (G_IS_FILE (file));
1147 iface = G_FILE_GET_IFACE (file);
1148 (* iface->read_async) (file,
1156 * g_file_read_finish:
1157 * @file: input #GFile.
1158 * @res: a #GAsyncResult.
1159 * @error: a #GError, or %NULL
1161 * Finishes an asynchronous file read operation started with
1162 * g_file_read_async().
1164 * If the #GFileIface for @file does not support streaming operations, then
1165 * @error will be set to %G_IO_ERROR_NOT_SUPPORTED and %NULL will be returned.
1167 * If @cancellable is not %NULL, then the operation can be cancelled by
1168 * triggering the cancellable object from another thread. If the operation
1169 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1171 * Returns: a #GFileInputStream or %NULL on error.
1174 g_file_read_finish (GFile *file,
1180 g_return_val_if_fail (G_IS_FILE (file), NULL);
1181 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1183 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1185 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1186 if (g_simple_async_result_propagate_error (simple, error))
1190 iface = G_FILE_GET_IFACE (file);
1191 return (* iface->read_finish) (file, res, error);
1195 * g_file_append_to_async:
1196 * @file: input #GFile.
1197 * @flags: a set of #GFileCreateFlags.
1198 * @io_priority: the io priority of the request. the io priority of the request.
1199 * @cancellable: optional #GCancellable object, %NULL to ignore.
1200 * @callback: a #GAsyncReadyCallback.
1201 * @user_data: a #gpointer.
1203 * Readies a file for appending data asynchronously.
1204 * For the synchronous version of this function, see
1205 * g_file_append_to().
1207 * If @cancellable is not %NULL, then the operation can be cancelled by
1208 * triggering the cancellable object from another thread. If the operation
1209 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1213 g_file_append_to_async (GFile *file,
1214 GFileCreateFlags flags,
1216 GCancellable *cancellable,
1217 GAsyncReadyCallback callback,
1222 g_return_if_fail (G_IS_FILE (file));
1224 iface = G_FILE_GET_IFACE (file);
1225 (* iface->append_to_async) (file,
1234 * g_file_append_to_finish:
1235 * @file: input #GFile.
1236 * @res: #GAsyncResult
1237 * @error: a #GError, or #NULL
1239 * Finishes appending to a file started with g_file_append_to_async().
1241 * Returns: a valid #GFileOutputStream or %NULL on error.
1244 g_file_append_to_finish (GFile *file,
1250 g_return_val_if_fail (G_IS_FILE (file), NULL);
1251 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1253 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1255 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1256 if (g_simple_async_result_propagate_error (simple, error))
1260 iface = G_FILE_GET_IFACE (file);
1261 return (* iface->append_to_finish) (file, res, error);
1265 * g_file_create_async:
1266 * @file: input #GFile.
1267 * @flags: a set of #GFileCreateFlags.
1268 * @io_priority: the io priority of the request. the io priority of the request.
1269 * @cancellable: optional #GCancellable object, %NULL to ignore.
1270 * @callback: a #GAsyncReadyCallback.
1271 * @user_data: a #gpointer.
1273 * Creates a new file asynchronously. For the synchronous version of
1274 * this function, see g_file_create().
1276 * If @cancellable is not %NULL, then the operation can be cancelled by
1277 * triggering the cancellable object from another thread. If the operation
1278 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1282 g_file_create_async (GFile *file,
1283 GFileCreateFlags flags,
1285 GCancellable *cancellable,
1286 GAsyncReadyCallback callback,
1291 g_return_if_fail (G_IS_FILE (file));
1293 iface = G_FILE_GET_IFACE (file);
1294 (* iface->create_async) (file,
1303 * g_file_create_finish:
1304 * @file: input #GFile.
1305 * @res: a #GAsyncResult.
1306 * @error: a #GError, or %NULL
1308 * Finishes creating a file started with g_file_create_async().
1310 * Returns: a #GFileOutputStream.
1313 g_file_create_finish (GFile *file,
1319 g_return_val_if_fail (G_IS_FILE (file), NULL);
1320 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1322 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1324 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1325 if (g_simple_async_result_propagate_error (simple, error))
1329 iface = G_FILE_GET_IFACE (file);
1330 return (* iface->create_finish) (file, res, error);
1334 * g_file_replace_async:
1335 * @file: input #GFile.
1336 * @etag: an Entity Tag for the current #GFile.
1337 * @make_backup: a #gboolean.
1338 * @flags: a set of #GFileCreateFlags.
1339 * @io_priority: the io priority of the request. the io priority of the request.
1340 * @cancellable: optional #GCancellable object, %NULL to ignore.
1341 * @callback: a #GAsyncReadyCallback.
1342 * @user_data: a #gpointer.
1344 * Replaces a file's contents. For the synchronous version of this
1345 * function, see g_file_replace(). If @make_backup is %TRUE, this function
1346 * will attempt to make a backup of the current file.
1348 * If @cancellable is not %NULL, then the operation can be cancelled by
1349 * triggering the cancellable object from another thread. If the operation
1350 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1354 g_file_replace_async (GFile *file,
1356 gboolean make_backup,
1357 GFileCreateFlags flags,
1359 GCancellable *cancellable,
1360 GAsyncReadyCallback callback,
1365 g_return_if_fail (G_IS_FILE (file));
1367 iface = G_FILE_GET_IFACE (file);
1368 (* iface->replace_async) (file,
1379 * g_file_replace_finish:
1380 * @file: input #GFile.
1381 * @res: a #GAsyncResult.
1382 * @error: a #GError, or %NULL
1384 * Finishes replacing the contents of the file started by
1385 * g_file_replace_async(). This is typically called within
1386 * a #GAsyncReadyCallback.
1388 * Returns: a #GFileOutputStream, or %NULL if an error has occured.
1391 g_file_replace_finish (GFile *file,
1397 g_return_val_if_fail (G_IS_FILE (file), NULL);
1398 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1400 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1402 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1403 if (g_simple_async_result_propagate_error (simple, error))
1407 iface = G_FILE_GET_IFACE (file);
1408 return (* iface->replace_finish) (file, res, error);
1412 copy_symlink (GFile *destination,
1413 GFileCopyFlags flags,
1414 GCancellable *cancellable,
1419 gboolean tried_delete;
1421 GFileType file_type;
1423 tried_delete = FALSE;
1427 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1429 /* Maybe it already existed, and we want to overwrite? */
1430 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1431 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1433 g_error_free (my_error);
1436 /* Don't overwrite if the destination is a directory */
1437 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STD_TYPE,
1438 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1439 cancellable, &my_error);
1442 file_type = g_file_info_get_file_type (info);
1443 g_object_unref (info);
1445 if (file_type == G_FILE_TYPE_DIRECTORY)
1447 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1448 _("Can't copy over directory"));
1453 if (!g_file_delete (destination, cancellable, error))
1456 tried_delete = TRUE;
1460 g_propagate_error (error, my_error);
1467 static GInputStream *
1468 open_source_for_copy (GFile *source,
1470 GFileCopyFlags flags,
1471 GCancellable *cancellable,
1477 GFileType file_type;
1480 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1484 /* There was an error opening the source, try to set a good error for it: */
1486 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1488 /* The source is a directory, don't fail with WOULD_RECURSE immediately, as
1489 that is less useful to the app. Better check for errors on the target instead. */
1491 g_error_free (my_error);
1494 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STD_TYPE,
1495 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1496 cancellable, &my_error);
1499 file_type = g_file_info_get_file_type (info);
1500 g_object_unref (info);
1502 if (flags & G_FILE_COPY_OVERWRITE)
1504 if (file_type == G_FILE_TYPE_DIRECTORY)
1506 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1507 _("Can't copy directory over directory"));
1510 /* continue to would_recurse error */
1514 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1515 _("Target file exists"));
1521 /* Error getting info from target, return that error (except for NOT_FOUND, which is no error here) */
1522 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1524 g_propagate_error (error, my_error);
1527 g_error_free (my_error);
1530 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1531 _("Can't recursively copy directory"));
1535 g_propagate_error (error, my_error);
1540 should_copy (GFileAttributeInfo *info, gboolean as_move)
1543 return info->flags & G_FILE_ATTRIBUTE_FLAGS_COPY_WHEN_MOVED;
1544 return info->flags & G_FILE_ATTRIBUTE_FLAGS_COPY_WITH_FILE;
1548 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1549 GFileAttributeInfoList *namespaces,
1557 s = g_string_new ("");
1561 for (i = 0; i < attributes->n_infos; i++)
1563 if (should_copy (&attributes->infos[i], as_move))
1568 g_string_append_c (s, ',');
1570 g_string_append (s, attributes->infos[i].name);
1577 for (i = 0; i < namespaces->n_infos; i++)
1579 if (should_copy (&namespaces->infos[i], as_move))
1584 g_string_append_c (s, ',');
1586 g_string_append (s, namespaces->infos[i].name);
1587 g_string_append (s, ":*");
1592 return g_string_free (s, FALSE);
1597 g_file_copy_attributes (GFile *source,
1599 GFileCopyFlags flags,
1600 GCancellable *cancellable,
1603 GFileAttributeInfoList *attributes, *namespaces;
1604 char *attrs_to_read;
1608 gboolean source_nofollow_symlinks;
1610 as_move = flags & G_FILE_COPY_ALL_METADATA;
1611 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1613 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1614 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1615 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1617 if (attributes == NULL && namespaces == NULL)
1620 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1622 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1623 we just don't copy it. */
1624 info = g_file_query_info (source, attrs_to_read,
1625 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1629 g_free (attrs_to_read);
1634 res = g_file_set_attributes_from_info (destination,
1638 g_object_unref (info);
1641 g_file_attribute_info_list_unref (attributes);
1642 g_file_attribute_info_list_unref (namespaces);
1647 /* Closes the streams */
1649 copy_stream_with_progress (GInputStream *in,
1651 GCancellable *cancellable,
1652 GFileProgressCallback progress_callback,
1653 gpointer progress_callback_data,
1656 gssize n_read, n_written;
1657 goffset current_size;
1658 char buffer[8192], *p;
1664 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1665 G_FILE_ATTRIBUTE_STD_SIZE,
1669 total_size = g_file_info_get_size (info);
1670 g_object_unref (info);
1677 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
1687 current_size += n_read;
1692 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
1693 if (n_written == -1)
1700 n_read -= n_written;
1706 if (progress_callback)
1707 progress_callback (current_size, total_size, progress_callback_data);
1711 error = NULL; /* Ignore further errors */
1713 /* Make sure we send full copied size */
1714 if (progress_callback)
1715 progress_callback (current_size, total_size, progress_callback_data);
1718 /* Don't care about errors in source here */
1719 g_input_stream_close (in, cancellable, NULL);
1721 /* But write errors on close are bad! */
1722 if (!g_output_stream_close (out, cancellable, error))
1725 g_object_unref (in);
1726 g_object_unref (out);
1732 file_copy_fallback (GFile *source,
1734 GFileCopyFlags flags,
1735 GCancellable *cancellable,
1736 GFileProgressCallback progress_callback,
1737 gpointer progress_callback_data,
1745 /* Maybe copy the symlink? */
1746 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
1748 info = g_file_query_info (source,
1749 G_FILE_ATTRIBUTE_STD_TYPE "," G_FILE_ATTRIBUTE_STD_SYMLINK_TARGET,
1750 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1756 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
1757 (target = g_file_info_get_symlink_target (info)) != NULL)
1759 if (!copy_symlink (destination, flags, cancellable, target, error))
1761 g_object_unref (info);
1765 g_object_unref (info);
1769 g_object_unref (info);
1772 in = open_source_for_copy (source, destination, flags, cancellable, error);
1776 if (flags & G_FILE_COPY_OVERWRITE)
1778 out = (GOutputStream *)g_file_replace (destination,
1780 flags & G_FILE_COPY_BACKUP,
1781 cancellable, error);
1785 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
1790 g_object_unref (in);
1794 if (!copy_stream_with_progress (in, out, cancellable,
1795 progress_callback, progress_callback_data,
1801 /* Ignore errors here. Failure to copy metadata is not a hard error */
1802 g_file_copy_attributes (source, destination,
1803 flags, cancellable, NULL);
1810 * @source: input #GFile.
1811 * @destination: destination #GFile
1812 * @flags: set of #GFileCopyFlags
1813 * @cancellable: optional #GCancellable object, %NULL to ignore.
1814 * @progress_callback: function to callback with progress information
1815 * @progress_callback_data: userdata to pass to @progress_callback
1816 * @error: #GError to set on error, or %NULL
1818 * <!-- Source Friendly Version
1819 * List of possible errors resulting from g_file_copy():
1820 * source dest flags res
1821 * - * * G_IO_ERROR_NOT_FOUND
1823 * file * 0 G_IO_ERROR_EXISTS
1824 * file file overwr ok
1825 * file dir overwr G_IO_ERROR_IS_DIRECTORY
1827 * dir - * G_IO_ERROR_WOULD_RECURSE
1828 * dir * 0 G_IO_ERROR_EXISTS
1829 * dir dir overwr G_IO_ERROR_WOULD_MERGE
1830 * dir file overwr G_IO_ERROR_WOULD_RECURSE
1831 * Docbook version below -->
1833 * Copies a file or directory from @source to @destination, with the given @flags.
1834 * @flags should be set to %0 if not used. This operation may fail, and @error will
1835 * be set appropriately with the given error result (see the following table).
1836 * File copies are always asynchronous.
1838 * If @cancellable is not %NULL, then the operation can be cancelled by
1839 * triggering the cancellable object from another thread. If the operation
1840 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1842 * If @progress_callback is not %NULL, then the operation can be monitored by
1843 * setting this to a #GFileProgressCallback function. @progress_callback_data
1844 * will be passed to this function.
1847 * <title>g_file_copy() Error Conditions</title>
1848 * <tgroup cols='4' align='left'><thead>
1849 * <row><entry>Source</entry><entry>Destination</entry><entry>Flags</entry><entry>Results in</entry></row>
1851 * <row><entry>%NULL</entry><entry>Anything</entry><entry>Anything</entry><entry>%G_IO_ERROR_NOT_FOUND</entry></row>
1852 * <row><entry>File</entry><entry>%NULL</entry><entry>Anything</entry><entry>No Error</entry></row>
1853 * <row><entry>File</entry><entry>Anything</entry><entry>0</entry><entry>%G_IO_ERROR_EXISTS</entry></row>
1854 * <row><entry>File</entry><entry>File</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>No Error</entry></row>
1855 * <row><entry>File</entry><entry>Directory</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>%G_IO_ERROR_IS_DIRECTORY</entry></row>
1856 * <row><entry>Directory</entry><entry>%NULL</entry><entry>Anything</entry><entry>%G_IO_ERROR_WOULD_RECURSE</entry></row>
1857 * <row><entry>Directory</entry><entry>Anything</entry><entry>0</entry><entry>%G_IO_ERROR_EXISTS</entry></row>
1858 * <row><entry>Directory</entry><entry>Directory</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>%G_IO_ERROR_IS_DIRECTORY</entry></row>
1859 * <row><entry>Directory</entry><entry>File</entry><entry>%G_FILE_COPY_OVERWRITE</entry><entry>%G_IO_ERROR_WOULD_RECURSE</entry></row>
1864 * Returns: %TRUE on success, %FALSE otherwise.
1867 g_file_copy (GFile *source,
1869 GFileCopyFlags flags,
1870 GCancellable *cancellable,
1871 GFileProgressCallback progress_callback,
1872 gpointer progress_callback_data,
1879 g_return_val_if_fail (G_IS_FILE (source), FALSE);
1880 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
1882 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1885 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
1887 iface = G_FILE_GET_IFACE (source);
1892 res = (* iface->copy) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
1897 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
1899 g_propagate_error (error, my_error);
1905 return file_copy_fallback (source, destination, flags, cancellable,
1906 progress_callback, progress_callback_data,
1913 * @source: #GFile pointing to the source location.
1914 * @destination: #GFile pointing to the destination location.
1915 * @flags: #GFileCopyFlags enum.
1916 * @cancellable: optional #GCancellable object, %NULL to ignore.
1917 * @progress_callback: GFileProgressCallback function for updates.
1918 * @progress_callback_data: gpointer to user data for the callback function.
1919 * @error: #GError for returning error conditions, or %NULL
1921 * List of possible returns from g_file_move() with given source,
1922 * destination, and flags:
1923 * <!-- Source version
1924 * source dest flags results in
1925 * - * * G_IO_ERROR_NOT_FOUND
1927 * file * 0 G_IO_ERROR_EXISTS
1928 * file file overwr ok
1929 * file dir overwr G_IO_ERROR_IS_DIRECTORY
1931 * dir - * ok || G_IO_ERROR_WOULD_RECURSE
1932 * dir * 0 G_IO_ERROR_EXISTS
1933 * dir dir overwr G_IO_ERROR_WOULD_MERGE
1934 * dir file overwr ok || G_IO_ERROR_WOULD_RECURSE
1935 * Pretty Docbook version -->
1937 * <title>g_file_move() Error Conditions</title>
1938 * <tgroup cols='4' align='left'><thead>
1939 * <row><entry>Source</entry><entry>Destination</entry>
1940 * <entry>Flags</entry><entry>Results in</entry></row>
1942 * <row><entry> %NULL </entry><entry> Anything </entry>
1943 * <entry> Anything </entry><entry> %G_IO_ERROR_NOT_FOUND </entry></row>
1944 * <row><entry> File </entry><entry> %NULL </entry>
1945 * <entry> Anything </entry><entry> No Error </entry></row>
1946 * <row><entry> File </entry><entry> Anything </entry>
1947 * <entry> 0 </entry><entry> %G_IO_ERROR_EXISTS </entry></row>
1948 * <row><entry> File </entry><entry> File </entry>
1949 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> No Error </entry></row>
1950 * <row><entry> File </entry><entry> Directory </entry>
1951 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> %G_IO_ERROR_IS_DIRECTORY </entry></row>
1952 * <row><entry> Directory </entry><entry> %NULL </entry>
1953 * <entry> Anything </entry><entry> No Error or %G_IO_ERROR_WOULD_RECURSE </entry></row>
1954 * <row><entry> Directory </entry><entry> Anything </entry>
1955 * <entry> 0 </entry><entry> %G_IO_ERROR_EXISTS </entry></row>
1956 * <row><entry> Directory </entry><entry> Directory </entry>
1957 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> %G_IO_ERROR_IS_DIRECTORY </entry></row>
1958 * <row><entry> Directory </entry><entry> File </entry>
1959 * <entry> %G_FILE_COPY_OVERWRITE </entry><entry> No Error or %G_IO_ERROR_WOULD_RECURSE </entry></row>
1963 * Returns: %TRUE on successful move, %FALSE otherwise.
1966 g_file_move (GFile *source,
1968 GFileCopyFlags flags,
1969 GCancellable *cancellable,
1970 GFileProgressCallback progress_callback,
1971 gpointer progress_callback_data,
1978 g_return_val_if_fail (G_IS_FILE (source), FALSE);
1979 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
1981 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1984 if (G_OBJECT_TYPE (source) == G_OBJECT_TYPE (destination))
1986 iface = G_FILE_GET_IFACE (source);
1991 res = (* iface->move) (source, destination, flags, cancellable, progress_callback, progress_callback_data, &my_error);
1996 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
1998 g_propagate_error (error, my_error);
2004 flags |= G_FILE_COPY_ALL_METADATA;
2005 if (!g_file_copy (source, destination, flags, cancellable,
2006 progress_callback, progress_callback_data,
2010 return g_file_delete (source, cancellable, error);
2014 * g_file_make_directory
2015 * @file: input #GFile.
2016 * @cancellable: optional #GCancellable object, %NULL to ignore.
2017 * @error: a #GError, or %NULL
2019 * If @cancellable is not %NULL, then the operation can be cancelled by
2020 * triggering the cancellable object from another thread. If the operation
2021 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2023 * Returns: %TRUE on successful creation, %FALSE otherwise.
2026 g_file_make_directory (GFile *file,
2027 GCancellable *cancellable,
2032 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2034 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2037 iface = G_FILE_GET_IFACE (file);
2039 if (iface->make_directory == NULL)
2041 g_set_error (error, G_IO_ERROR,
2042 G_IO_ERROR_NOT_SUPPORTED,
2043 _("Operation not supported"));
2047 return (* iface->make_directory) (file, cancellable, error);
2051 * g_file_make_symbolic_link:
2052 * @file: input #GFile.
2053 * @symlink_value: a string with the name of the new symlink.
2054 * @cancellable: optional #GCancellable object, %NULL to ignore.
2055 * @error: a #GError.
2057 * If @cancellable is not %NULL, then the operation can be cancelled by
2058 * triggering the cancellable object from another thread. If the operation
2059 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2061 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2064 g_file_make_symbolic_link (GFile *file,
2065 const char *symlink_value,
2066 GCancellable *cancellable,
2071 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2072 g_return_val_if_fail (symlink_value != NULL, FALSE);
2074 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2077 if (*symlink_value == '\0')
2079 g_set_error (error, G_IO_ERROR,
2080 G_IO_ERROR_INVALID_ARGUMENT,
2081 _("Invalid symlink value given"));
2085 iface = G_FILE_GET_IFACE (file);
2087 if (iface->make_symbolic_link == NULL)
2089 g_set_error (error, G_IO_ERROR,
2090 G_IO_ERROR_NOT_SUPPORTED,
2091 _("Operation not supported"));
2095 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2100 * @file: input #GFile.
2101 * @cancellable: optional #GCancellable object, %NULL to ignore.
2102 * @error: a #GError, or %NULL
2106 * If @cancellable is not %NULL, then the operation can be cancelled by
2107 * triggering the cancellable object from another thread. If the operation
2108 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2110 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2113 g_file_delete (GFile *file,
2114 GCancellable *cancellable,
2119 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2121 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2124 iface = G_FILE_GET_IFACE (file);
2126 if (iface->delete_file == NULL)
2128 g_set_error (error, G_IO_ERROR,
2129 G_IO_ERROR_NOT_SUPPORTED,
2130 _("Operation not supported"));
2134 return (* iface->delete_file) (file, cancellable, error);
2139 * @file: #GFile to send to trash.
2140 * @cancellable: optional #GCancellable object, %NULL to ignore.
2141 * @error: a #GError, or %NULL
2143 * Sends @file to the virtual file system "Trash" location. If the
2144 * virtual file system does not have support having a "Trash" location,
2145 * %FALSE will be returned, and @error will be set to %G_IO_ERROR_NOT_SUPPORTED.
2147 * If @cancellable is not %NULL, then the operation can be cancelled by
2148 * triggering the cancellable object from another thread. If the operation
2149 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2151 * Returns: %TRUE on successful trash, %FALSE otherwise.
2154 g_file_trash (GFile *file,
2155 GCancellable *cancellable,
2160 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2162 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2165 iface = G_FILE_GET_IFACE (file);
2167 if (iface->trash == NULL)
2170 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2171 _("Trash not supported"));
2175 return (* iface->trash) (file, cancellable, error);
2179 * g_file_set_display_name:
2180 * @file: input #GFile.
2181 * @display_name: a string.
2182 * @cancellable: optional #GCancellable object, %NULL to ignore.
2183 * @error: a #GError, or %NULL
2185 * Sets the display name for @file. If the display name contains invalid
2186 * characters, @error will be set to %G_IO_ERROR_INVALID_ARGUMENT. For the
2187 * asynchronous version of this function, see g_file_set_display_name_async().
2189 * If @cancellable is not %NULL, then the operation can be cancelled by
2190 * triggering the cancellable object from another thread. If the operation
2191 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2193 * Returns: a #GFile, or %NULL if there was an error.
2196 g_file_set_display_name (GFile *file,
2197 const char *display_name,
2198 GCancellable *cancellable,
2203 g_return_val_if_fail (G_IS_FILE (file), NULL);
2204 g_return_val_if_fail (display_name != NULL, NULL);
2206 if (strchr (display_name, '/') != NULL)
2210 G_IO_ERROR_INVALID_ARGUMENT,
2211 _("File names cannot contain '/'"));
2215 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2218 iface = G_FILE_GET_IFACE (file);
2220 return (* iface->set_display_name) (file, display_name, cancellable, error);
2224 * g_file_set_display_name_async:
2225 * @file: input #GFile.
2226 * @display_name: a string.
2227 * @io_priority: the io priority of the request. the io priority of the request.
2228 * @cancellable: optional #GCancellable object, %NULL to ignore.
2229 * @callback: a #GAsyncReadyCallback.
2230 * @user_data: a #gpointer.
2232 * Asynchronously sets the display name for a given #GFile.
2233 * For the synchronous version of this function, see
2234 * g_file_set_display_name().
2236 * If @cancellable is not %NULL, then the operation can be cancelled by
2237 * triggering the cancellable object from another thread. If the operation
2238 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2242 g_file_set_display_name_async (GFile *file,
2243 const char *display_name,
2245 GCancellable *cancellable,
2246 GAsyncReadyCallback callback,
2251 g_return_if_fail (G_IS_FILE (file));
2252 g_return_if_fail (display_name != NULL);
2254 iface = G_FILE_GET_IFACE (file);
2255 (* iface->set_display_name_async) (file,
2264 * g_file_set_display_name_finish:
2265 * @file: input #GFile.
2266 * @res: a #GAsyncResult.
2267 * @error: a #GError, or %NULL
2269 * Finishes setting a display name started with g_file_set_display_name_async().
2271 * Returns: a #GFile or %NULL on error.
2274 g_file_set_display_name_finish (GFile *file,
2280 g_return_val_if_fail (G_IS_FILE (file), NULL);
2281 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2283 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2285 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2286 if (g_simple_async_result_propagate_error (simple, error))
2290 iface = G_FILE_GET_IFACE (file);
2291 return (* iface->set_display_name_finish) (file, res, error);
2295 * g_file_query_settable_attributes:
2296 * @file: input #GFile.
2297 * @cancellable: optional #GCancellable object, %NULL to ignore.
2298 * @error: a #GError, or %NULL
2300 * If @cancellable is not %NULL, then the operation can be cancelled by
2301 * triggering the cancellable object from another thread. If the operation
2302 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2304 * Returns: the type and full attribute name of all the attributes that
2305 * the file can set. This doesn't mean setting it will always succeed though,
2306 * you might get an access failure, or some specific file may not support a
2307 * specific attribute.
2309 GFileAttributeInfoList *
2310 g_file_query_settable_attributes (GFile *file,
2311 GCancellable *cancellable,
2316 GFileAttributeInfoList *list;
2318 g_return_val_if_fail (G_IS_FILE (file), NULL);
2320 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2323 iface = G_FILE_GET_IFACE (file);
2325 if (iface->query_settable_attributes == NULL)
2326 return g_file_attribute_info_list_new ();
2329 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2333 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2335 list = g_file_attribute_info_list_new ();
2336 g_error_free (my_error);
2339 g_propagate_error (error, my_error);
2346 * g_file_query_writable_namespaces:
2347 * @file: input #GFile.
2348 * @cancellable: optional #GCancellable object, %NULL to ignore.
2349 * @error: a #GError, or %NULL
2351 * If @cancellable is not %NULL, then the operation can be cancelled by
2352 * triggering the cancellable object from another thread. If the operation
2353 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2355 * Returns: a #GFileAttributeInfoList of attribute namespaces
2356 * where the user can create their own attribute names, such
2357 * as extended attributes.
2359 GFileAttributeInfoList *
2360 g_file_query_writable_namespaces (GFile *file,
2361 GCancellable *cancellable,
2366 GFileAttributeInfoList *list;
2368 g_return_val_if_fail (G_IS_FILE (file), NULL);
2370 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2373 iface = G_FILE_GET_IFACE (file);
2375 if (iface->query_writable_namespaces == NULL)
2376 return g_file_attribute_info_list_new ();
2379 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2383 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2385 list = g_file_attribute_info_list_new ();
2386 g_error_free (my_error);
2389 g_propagate_error (error, my_error);
2396 * g_file_set_attribute:
2397 * @file: input #GFile.
2398 * @attribute: a string containing the attribute's name.
2399 * @value: a set of #GFileAttributeValue.
2400 * @flags: a set of #GFileQueryInfoFlags.
2401 * @cancellable: optional #GCancellable object, %NULL to ignore.
2402 * @error: a #GError, or %NULL
2404 * Sets an attribute in the file with attribute name @attribute to @value.
2405 * If setting attributes is not suppored by the #GFileIface for @file,
2406 * then @error will be set to %G_IO_ERROR_NOT_SUPPORTED.
2408 * If @cancellable is not %NULL, then the operation can be cancelled by
2409 * triggering the cancellable object from another thread. If the operation
2410 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2412 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2415 g_file_set_attribute (GFile *file,
2416 const char *attribute,
2417 const GFileAttributeValue *value,
2418 GFileQueryInfoFlags flags,
2419 GCancellable *cancellable,
2424 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2425 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2427 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2430 iface = G_FILE_GET_IFACE (file);
2432 if (iface->set_attribute == NULL)
2434 g_set_error (error, G_IO_ERROR,
2435 G_IO_ERROR_NOT_SUPPORTED,
2436 _("Operation not supported"));
2440 return (* iface->set_attribute) (file, attribute, value, flags, cancellable, error);
2444 * g_file_set_attributes_from_info:
2445 * @file: input #GFile.
2446 * @info: a #GFileInfo.
2447 * @flags: #GFileQueryInfoFlags
2448 * @cancellable: optional #GCancellable object, %NULL to ignore.
2449 * @error: a #GError, or %NULL
2451 * Tries to set all attributes in the #GFileInfo on the target values, not stopping
2452 * on the first error.
2454 * If @cancellable is not %NULL, then the operation can be cancelled by
2455 * triggering the cancellable object from another thread. If the operation
2456 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2458 * Returns: %TRUE if there was any error, and @error will be set to
2459 * the first error. Error on particular fields are flagged by setting the
2460 * "status" field in the attribute value to %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING.
2463 g_file_set_attributes_from_info (GFile *file,
2465 GFileQueryInfoFlags flags,
2466 GCancellable *cancellable,
2471 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2472 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2474 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2477 g_file_info_clear_status (info);
2479 iface = G_FILE_GET_IFACE (file);
2481 return (* iface->set_attributes_from_info) (file, info, flags, cancellable, error);
2486 g_file_real_set_attributes_from_info (GFile *file,
2488 GFileQueryInfoFlags flags,
2489 GCancellable *cancellable,
2495 GFileAttributeValue *value;
2499 attributes = g_file_info_list_attributes (info, NULL);
2501 for (i = 0; attributes[i] != NULL; i++)
2503 value = (GFileAttributeValue *)g_file_info_get_attribute (info, attributes[i]);
2505 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2508 if (!g_file_set_attribute (file, attributes[i], value, flags, cancellable, error))
2510 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2512 /* Don't set error multiple times */
2516 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2519 g_strfreev (attributes);
2525 * g_file_set_attributes_async:
2526 * @file: input #GFile.
2527 * @info: a #GFileInfo.
2528 * @flags: a #GFileQueryInfoFlags.
2529 * @io_priority: the io priority of the request. the io priority of the request.
2530 * @cancellable: optional #GCancellable object, %NULL to ignore.
2531 * @callback: a #GAsyncReadyCallback.
2532 * @user_data: a #gpointer.
2534 * Asynchronously sets the attributes of @file with @info.
2535 * For the synchronous version of this function, see g_file_set_attributes().
2537 * If @cancellable is not %NULL, then the operation can be cancelled by
2538 * triggering the cancellable object from another thread. If the operation
2539 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2543 g_file_set_attributes_async (GFile *file,
2545 GFileQueryInfoFlags flags,
2547 GCancellable *cancellable,
2548 GAsyncReadyCallback callback,
2553 g_return_if_fail (G_IS_FILE (file));
2554 g_return_if_fail (G_IS_FILE_INFO (info));
2556 iface = G_FILE_GET_IFACE (file);
2557 (* iface->set_attributes_async) (file, info, flags, io_priority, cancellable, callback, user_data);
2562 * g_file_set_attributes_finish:
2563 * @file: input #GFile.
2564 * @result: a #GAsyncResult.
2565 * @info: a #GFileInfo.
2566 * @error: a #GError, or %NULL
2568 * Finishes setting an attribute started in g_file_set_attributes_async().
2570 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2573 g_file_set_attributes_finish (GFile *file,
2574 GAsyncResult *result,
2580 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2581 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2583 /* No standard handling of errors here, as we must set info even
2586 iface = G_FILE_GET_IFACE (file);
2587 return (* iface->set_attributes_finish) (file, result, info, error);
2591 * g_file_set_attribute_string:
2592 * @file: input #GFile.
2593 * @attribute: a string containing the attribute's name.
2594 * @value: a string containing the attribute's value.
2595 * @flags: #GFileQueryInfoFlags.
2596 * @cancellable: optional #GCancellable object, %NULL to ignore.
2597 * @error: a #GError, or %NULL
2599 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value. If
2600 * @attribute is of a different type, this operation will fail.
2602 * If @cancellable is not %NULL, then the operation can be cancelled by
2603 * triggering the cancellable object from another thread. If the operation
2604 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2606 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2609 g_file_set_attribute_string (GFile *file,
2610 const char *attribute,
2612 GFileQueryInfoFlags flags,
2613 GCancellable *cancellable,
2616 GFileAttributeValue v;
2618 v.type = G_FILE_ATTRIBUTE_TYPE_STRING;
2619 v.u.string = (char *)value;
2620 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2624 * g_file_set_attribute_byte_string:
2625 * @file: input #GFile.
2626 * @attribute: a string containing the attribute's name.
2627 * @value: a string containing the attribute's new value.
2628 * @flags: a #GFileQueryInfoFlags.
2629 * @cancellable: optional #GCancellable object, %NULL to ignore.
2630 * @error: a #GError, or %NULL
2632 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value. If
2633 * @attribute is of a different type, this operation will fail, returning %FALSE.
2635 * If @cancellable is not %NULL, then the operation can be cancelled by
2636 * triggering the cancellable object from another thread. If the operation
2637 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2639 * Returns: %TRUE if the @attribute was successfully set to @value
2640 * in the @file, %FALSE otherwise.
2643 g_file_set_attribute_byte_string (GFile *file,
2644 const char *attribute,
2646 GFileQueryInfoFlags flags,
2647 GCancellable *cancellable,
2650 GFileAttributeValue v;
2652 v.type = G_FILE_ATTRIBUTE_TYPE_BYTE_STRING;
2653 v.u.string = (char *)value;
2654 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2658 * g_file_set_attribute_uint32:
2659 * @file: input #GFile.
2660 * @attribute: a string containing the attribute's name.
2661 * @value: a #guint32 containing the attribute's new value.
2662 * @flags: a #GFileQueryInfoFlags.
2663 * @cancellable: optional #GCancellable object, %NULL to ignore.
2664 * @error: a #GError, or %NULL
2666 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value. If
2667 * @attribute is of a different type, this operation will fail.
2669 * If @cancellable is not %NULL, then the operation can be cancelled by
2670 * triggering the cancellable object from another thread. If the operation
2671 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2673 * Returns: %TRUE if the @attribute was successfully set to @value
2674 * in the @file, %FALSE otherwise.
2677 g_file_set_attribute_uint32 (GFile *file,
2678 const char *attribute,
2680 GFileQueryInfoFlags flags,
2681 GCancellable *cancellable,
2684 GFileAttributeValue v;
2686 v.type = G_FILE_ATTRIBUTE_TYPE_UINT32;
2688 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2692 * g_file_set_attribute_int32:
2693 * @file: input #GFile.
2694 * @attribute: a string containing the attribute's name.
2695 * @value: a #gint32 containing the attribute's new value.
2696 * @flags: a #GFileQueryInfoFlags.
2697 * @cancellable: optional #GCancellable object, %NULL to ignore.
2698 * @error: a #GError, or %NULL
2700 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value. If
2701 * @attribute is of a different type, this operation will fail.
2703 * If @cancellable is not %NULL, then the operation can be cancelled by
2704 * triggering the cancellable object from another thread. If the operation
2705 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2707 * Returns: %TRUE if the @attribute was successfully set to @value
2708 * in the @file, %FALSE otherwise.
2711 g_file_set_attribute_int32 (GFile *file,
2712 const char *attribute,
2714 GFileQueryInfoFlags flags,
2715 GCancellable *cancellable,
2718 GFileAttributeValue v;
2720 v.type = G_FILE_ATTRIBUTE_TYPE_INT32;
2722 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2726 * g_file_set_attribute_uint64:
2727 * @file: input #GFile.
2728 * @attribute: a string containing the attribute's name.
2729 * @value: a #guint64 containing the attribute's new value.
2730 * @flags: a #GFileQueryInfoFlags.
2731 * @cancellable: optional #GCancellable object, %NULL to ignore.
2732 * @error: a #GError, or %NULL
2734 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value. If
2735 * @attribute is of a different type, this operation will fail.
2737 * If @cancellable is not %NULL, then the operation can be cancelled by
2738 * triggering the cancellable object from another thread. If the operation
2739 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2741 * Returns: %TRUE if the @attribute was successfully set to @value
2742 * in the @file, %FALSE otherwise.
2745 g_file_set_attribute_uint64 (GFile *file,
2746 const char *attribute,
2748 GFileQueryInfoFlags flags,
2749 GCancellable *cancellable,
2752 GFileAttributeValue v;
2754 v.type = G_FILE_ATTRIBUTE_TYPE_UINT64;
2756 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2760 * g_file_set_attribute_int64:
2761 * @file: input #GFile.
2762 * @attribute: a string containing the attribute's name.
2763 * @value: a #guint64 containing the attribute's new value.
2764 * @flags: a #GFileQueryInfoFlags.
2765 * @cancellable: optional #GCancellable object, %NULL to ignore.
2766 * @error: a #GError, or %NULL
2768 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value. If
2769 * @attribute is of a different type, this operation will fail.
2771 * If @cancellable is not %NULL, then the operation can be cancelled by
2772 * triggering the cancellable object from another thread. If the operation
2773 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2775 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2778 g_file_set_attribute_int64 (GFile *file,
2779 const char *attribute,
2781 GFileQueryInfoFlags flags,
2782 GCancellable *cancellable,
2785 GFileAttributeValue v;
2787 v.type = G_FILE_ATTRIBUTE_TYPE_INT64;
2789 return g_file_set_attribute (file, attribute, &v, flags, cancellable, error);
2793 * g_file_mount_mountable:
2794 * @file: input #GFile.
2795 * @mount_operation: a #GMountOperation.
2796 * @cancellable: optional #GCancellable object, %NULL to ignore.
2797 * @callback: a #GAsyncReadyCallback.
2798 * @user_data: a #gpointer.
2800 * If @cancellable is not %NULL, then the operation can be cancelled by
2801 * triggering the cancellable object from another thread. If the operation
2802 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2804 * Mounts a mountable file using @mount_operation, if possible.
2808 g_file_mount_mountable (GFile *file,
2809 GMountOperation *mount_operation,
2810 GCancellable *cancellable,
2811 GAsyncReadyCallback callback,
2816 g_return_if_fail (G_IS_FILE (file));
2817 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
2819 iface = G_FILE_GET_IFACE (file);
2821 if (iface->mount_mountable == NULL)
2823 g_simple_async_report_error_in_idle (G_OBJECT (file),
2827 G_IO_ERROR_NOT_SUPPORTED,
2828 _("Operation not supported"));
2831 (* iface->mount_mountable) (file,
2840 * g_file_mount_mountable_finish:
2841 * @file: input #GFile.
2842 * @result: a #GAsyncResult.
2843 * @error: a #GError, or %NULL
2845 * Returns: a #GFile or %NULL on error.
2848 g_file_mount_mountable_finish (GFile *file,
2849 GAsyncResult *result,
2854 g_return_val_if_fail (G_IS_FILE (file), NULL);
2855 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
2857 if (G_IS_SIMPLE_ASYNC_RESULT (result))
2859 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
2860 if (g_simple_async_result_propagate_error (simple, error))
2864 iface = G_FILE_GET_IFACE (file);
2865 return (* iface->mount_mountable_finish) (file, result, error);
2869 * g_file_unmount_mountable:
2870 * @file: input #GFile.
2871 * @cancellable: optional #GCancellable object, %NULL to ignore.
2872 * @callback: a #GAsyncReadyCallback.
2873 * @user_data: a #gpointer.
2875 * If @cancellable is not %NULL, then the operation can be cancelled by
2876 * triggering the cancellable object from another thread. If the operation
2877 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2881 g_file_unmount_mountable (GFile *file,
2882 GCancellable *cancellable,
2883 GAsyncReadyCallback callback,
2888 g_return_if_fail (G_IS_FILE (file));
2890 iface = G_FILE_GET_IFACE (file);
2892 if (iface->unmount_mountable == NULL)
2894 g_simple_async_report_error_in_idle (G_OBJECT (file),
2898 G_IO_ERROR_NOT_SUPPORTED,
2899 _("Operation not supported"));
2902 (* iface->unmount_mountable) (file,
2909 * g_file_unmount_mountable_finish:
2910 * @file: input #GFile.
2911 * @result: a #GAsyncResult.
2912 * @error: a #GError, or %NULL
2914 * Returns: %TRUE if the operation finished successfully. %FALSE
2918 g_file_unmount_mountable_finish (GFile *file,
2919 GAsyncResult *result,
2924 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2925 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2927 if (G_IS_SIMPLE_ASYNC_RESULT (result))
2929 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
2930 if (g_simple_async_result_propagate_error (simple, error))
2934 iface = G_FILE_GET_IFACE (file);
2935 return (* iface->unmount_mountable_finish) (file, result, error);
2939 * g_file_eject_mountable:
2940 * @file: input #GFile.
2941 * @cancellable: optional #GCancellable object, %NULL to ignore.
2942 * @callback: a #GAsyncReadyCallback.
2943 * @user_data: a #gpointer.
2945 * Starts an asynchronous eject on a mountable.
2946 * When this operation has completed, @callback will be called with
2947 * @user_user data, and the operation can be finalized with
2948 * g_file_eject_mountable_finish().
2950 * If @cancellable is not %NULL, then the operation can be cancelled by
2951 * triggering the cancellable object from another thread. If the operation
2952 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2956 g_file_eject_mountable (GFile *file,
2957 GCancellable *cancellable,
2958 GAsyncReadyCallback callback,
2963 g_return_if_fail (G_IS_FILE (file));
2965 iface = G_FILE_GET_IFACE (file);
2967 if (iface->eject_mountable == NULL)
2969 g_simple_async_report_error_in_idle (G_OBJECT (file),
2973 G_IO_ERROR_NOT_SUPPORTED,
2974 _("Operation not supported"));
2977 (* iface->eject_mountable) (file,
2984 * g_file_eject_mountable_finish:
2985 * @file: input #GFile.
2986 * @result: a #GAsyncResult.
2987 * @error: a #GError, or %NULL
2989 * Finishes an asynchronous eject operation started by
2990 * g_file_eject_mountable().
2992 * Returns: %TRUE if the @file was ejected successfully. %FALSE
2996 g_file_eject_mountable_finish (GFile *file,
2997 GAsyncResult *result,
3002 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3003 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3005 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3007 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3008 if (g_simple_async_result_propagate_error (simple, error))
3012 iface = G_FILE_GET_IFACE (file);
3013 return (* iface->eject_mountable_finish) (file, result, error);
3017 * g_file_monitor_directory:
3018 * @file: input #GFile.
3019 * @flags: a set of #GFileMonitorFlags.
3020 * @cancellable: optional #GCancellable object, %NULL to ignore.
3022 * If @cancellable is not %NULL, then the operation can be cancelled by
3023 * triggering the cancellable object from another thread. If the operation
3024 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3026 * Returns: a #GDirectoryMonitor for the given @file,
3027 * or %NULL on error.
3030 g_file_monitor_directory (GFile *file,
3031 GFileMonitorFlags flags,
3032 GCancellable *cancellable)
3036 g_return_val_if_fail (G_IS_FILE (file), NULL);
3038 iface = G_FILE_GET_IFACE (file);
3040 if (iface->monitor_dir == NULL)
3043 return (* iface->monitor_dir) (file, flags, cancellable);
3047 * g_file_monitor_file:
3048 * @file: input #GFile.
3049 * @flags: a set of #GFileMonitorFlags.
3050 * @cancellable: optional #GCancellable object, %NULL to ignore.
3052 * If @cancellable is not %NULL, then the operation can be cancelled by
3053 * triggering the cancellable object from another thread. If the operation
3054 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3056 * Returns: a #GFileMonitor for the given @file,
3057 * or %NULL on error.
3060 g_file_monitor_file (GFile *file,
3061 GFileMonitorFlags flags,
3062 GCancellable *cancellable)
3065 GFileMonitor *monitor;
3067 g_return_val_if_fail (G_IS_FILE (file), NULL);
3069 iface = G_FILE_GET_IFACE (file);
3073 if (iface->monitor_file)
3074 monitor = (* iface->monitor_file) (file, flags, cancellable);
3076 /* Fallback to polling */
3077 if (monitor == NULL)
3078 monitor = g_poll_file_monitor_new (file);
3083 /********************************************
3084 * Default implementation of async ops *
3085 ********************************************/
3089 GFileQueryInfoFlags flags;
3091 } QueryInfoAsyncData;
3094 query_info_data_free (QueryInfoAsyncData *data)
3097 g_object_unref (data->info);
3098 g_free (data->attributes);
3103 query_info_async_thread (GSimpleAsyncResult *res,
3105 GCancellable *cancellable)
3107 GError *error = NULL;
3108 QueryInfoAsyncData *data;
3111 data = g_simple_async_result_get_op_res_gpointer (res);
3113 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3117 g_simple_async_result_set_from_error (res, error);
3118 g_error_free (error);
3125 g_file_real_query_info_async (GFile *file,
3126 const char *attributes,
3127 GFileQueryInfoFlags flags,
3129 GCancellable *cancellable,
3130 GAsyncReadyCallback callback,
3133 GSimpleAsyncResult *res;
3134 QueryInfoAsyncData *data;
3136 data = g_new0 (QueryInfoAsyncData, 1);
3137 data->attributes = g_strdup (attributes);
3138 data->flags = flags;
3140 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3141 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3143 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3144 g_object_unref (res);
3148 g_file_real_query_info_finish (GFile *file,
3152 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3153 QueryInfoAsyncData *data;
3155 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3157 data = g_simple_async_result_get_op_res_gpointer (simple);
3159 return g_object_ref (data->info);
3166 GFileQueryInfoFlags flags;
3167 GFileEnumerator *enumerator;
3168 } EnumerateChildrenAsyncData;
3171 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3173 if (data->enumerator)
3174 g_object_unref (data->enumerator);
3175 g_free (data->attributes);
3180 enumerate_children_async_thread (GSimpleAsyncResult *res,
3182 GCancellable *cancellable)
3184 GError *error = NULL;
3185 EnumerateChildrenAsyncData *data;
3186 GFileEnumerator *enumerator;
3188 data = g_simple_async_result_get_op_res_gpointer (res);
3190 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3192 if (enumerator == NULL)
3194 g_simple_async_result_set_from_error (res, error);
3195 g_error_free (error);
3198 data->enumerator = enumerator;
3202 g_file_real_enumerate_children_async (GFile *file,
3203 const char *attributes,
3204 GFileQueryInfoFlags flags,
3206 GCancellable *cancellable,
3207 GAsyncReadyCallback callback,
3210 GSimpleAsyncResult *res;
3211 EnumerateChildrenAsyncData *data;
3213 data = g_new0 (EnumerateChildrenAsyncData, 1);
3214 data->attributes = g_strdup (attributes);
3215 data->flags = flags;
3217 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3218 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3220 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3221 g_object_unref (res);
3224 static GFileEnumerator *
3225 g_file_real_enumerate_children_finish (GFile *file,
3229 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3230 EnumerateChildrenAsyncData *data;
3232 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3234 data = g_simple_async_result_get_op_res_gpointer (simple);
3235 if (data->enumerator)
3236 return g_object_ref (data->enumerator);
3242 open_read_async_thread (GSimpleAsyncResult *res,
3244 GCancellable *cancellable)
3247 GFileInputStream *stream;
3248 GError *error = NULL;
3250 iface = G_FILE_GET_IFACE (object);
3252 stream = iface->read (G_FILE (object), cancellable, &error);
3256 g_simple_async_result_set_from_error (res, error);
3257 g_error_free (error);
3260 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3264 g_file_real_read_async (GFile *file,
3266 GCancellable *cancellable,
3267 GAsyncReadyCallback callback,
3270 GSimpleAsyncResult *res;
3272 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3274 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3275 g_object_unref (res);
3278 static GFileInputStream *
3279 g_file_real_read_finish (GFile *file,
3283 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3286 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3288 op = g_simple_async_result_get_op_res_gpointer (simple);
3290 return g_object_ref (op);
3296 append_to_async_thread (GSimpleAsyncResult *res,
3298 GCancellable *cancellable)
3301 GFileCreateFlags *data;
3302 GFileOutputStream *stream;
3303 GError *error = NULL;
3305 iface = G_FILE_GET_IFACE (object);
3307 data = g_simple_async_result_get_op_res_gpointer (res);
3309 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3313 g_simple_async_result_set_from_error (res, error);
3314 g_error_free (error);
3317 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3321 g_file_real_append_to_async (GFile *file,
3322 GFileCreateFlags flags,
3324 GCancellable *cancellable,
3325 GAsyncReadyCallback callback,
3328 GFileCreateFlags *data;
3329 GSimpleAsyncResult *res;
3331 data = g_new0 (GFileCreateFlags, 1);
3334 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3335 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3337 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3338 g_object_unref (res);
3341 static GFileOutputStream *
3342 g_file_real_append_to_finish (GFile *file,
3346 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3349 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3351 op = g_simple_async_result_get_op_res_gpointer (simple);
3353 return g_object_ref (op);
3359 create_async_thread (GSimpleAsyncResult *res,
3361 GCancellable *cancellable)
3364 GFileCreateFlags *data;
3365 GFileOutputStream *stream;
3366 GError *error = NULL;
3368 iface = G_FILE_GET_IFACE (object);
3370 data = g_simple_async_result_get_op_res_gpointer (res);
3372 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3376 g_simple_async_result_set_from_error (res, error);
3377 g_error_free (error);
3380 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3384 g_file_real_create_async (GFile *file,
3385 GFileCreateFlags flags,
3387 GCancellable *cancellable,
3388 GAsyncReadyCallback callback,
3391 GFileCreateFlags *data;
3392 GSimpleAsyncResult *res;
3394 data = g_new0 (GFileCreateFlags, 1);
3397 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3398 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3400 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3401 g_object_unref (res);
3404 static GFileOutputStream *
3405 g_file_real_create_finish (GFile *file,
3409 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3412 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3414 op = g_simple_async_result_get_op_res_gpointer (simple);
3416 return g_object_ref (op);
3422 GFileOutputStream *stream;
3424 gboolean make_backup;
3425 GFileCreateFlags flags;
3429 replace_async_data_free (ReplaceAsyncData *data)
3432 g_object_unref (data->stream);
3433 g_free (data->etag);
3438 replace_async_thread (GSimpleAsyncResult *res,
3440 GCancellable *cancellable)
3443 GFileOutputStream *stream;
3444 GError *error = NULL;
3445 ReplaceAsyncData *data;
3447 iface = G_FILE_GET_IFACE (object);
3449 data = g_simple_async_result_get_op_res_gpointer (res);
3451 stream = iface->replace (G_FILE (object),
3460 g_simple_async_result_set_from_error (res, error);
3461 g_error_free (error);
3464 data->stream = stream;
3468 g_file_real_replace_async (GFile *file,
3470 gboolean make_backup,
3471 GFileCreateFlags flags,
3473 GCancellable *cancellable,
3474 GAsyncReadyCallback callback,
3477 GSimpleAsyncResult *res;
3478 ReplaceAsyncData *data;
3480 data = g_new0 (ReplaceAsyncData, 1);
3481 data->etag = g_strdup (etag);
3482 data->make_backup = make_backup;
3483 data->flags = flags;
3485 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3486 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3488 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3489 g_object_unref (res);
3492 static GFileOutputStream *
3493 g_file_real_replace_finish (GFile *file,
3497 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3498 ReplaceAsyncData *data;
3500 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3502 data = g_simple_async_result_get_op_res_gpointer (simple);
3504 return g_object_ref (data->stream);
3512 } SetDisplayNameAsyncData;
3515 set_display_name_data_free (SetDisplayNameAsyncData *data)
3517 g_free (data->name);
3519 g_object_unref (data->file);
3524 set_display_name_async_thread (GSimpleAsyncResult *res,
3526 GCancellable *cancellable)
3528 GError *error = NULL;
3529 SetDisplayNameAsyncData *data;
3532 data = g_simple_async_result_get_op_res_gpointer (res);
3534 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3538 g_simple_async_result_set_from_error (res, error);
3539 g_error_free (error);
3546 g_file_real_set_display_name_async (GFile *file,
3547 const char *display_name,
3549 GCancellable *cancellable,
3550 GAsyncReadyCallback callback,
3553 GSimpleAsyncResult *res;
3554 SetDisplayNameAsyncData *data;
3556 data = g_new0 (SetDisplayNameAsyncData, 1);
3557 data->name = g_strdup (display_name);
3559 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3560 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3562 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3563 g_object_unref (res);
3567 g_file_real_set_display_name_finish (GFile *file,
3571 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3572 SetDisplayNameAsyncData *data;
3574 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3576 data = g_simple_async_result_get_op_res_gpointer (simple);
3578 return g_object_ref (data->file);
3584 GFileQueryInfoFlags flags;
3591 set_info_data_free (SetInfoAsyncData *data)
3594 g_object_unref (data->info);
3596 g_error_free (data->error);
3601 set_info_async_thread (GSimpleAsyncResult *res,
3603 GCancellable *cancellable)
3605 SetInfoAsyncData *data;
3607 data = g_simple_async_result_get_op_res_gpointer (res);
3610 data->res = g_file_set_attributes_from_info (G_FILE (object),
3618 g_file_real_set_attributes_async (GFile *file,
3620 GFileQueryInfoFlags flags,
3622 GCancellable *cancellable,
3623 GAsyncReadyCallback callback,
3626 GSimpleAsyncResult *res;
3627 SetInfoAsyncData *data;
3629 data = g_new0 (SetInfoAsyncData, 1);
3630 data->info = g_file_info_dup (info);
3631 data->flags = flags;
3633 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
3634 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
3636 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
3637 g_object_unref (res);
3641 g_file_real_set_attributes_finish (GFile *file,
3646 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3647 SetInfoAsyncData *data;
3649 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
3651 data = g_simple_async_result_get_op_res_gpointer (simple);
3654 *info = g_object_ref (data->info);
3656 if (error != NULL && data->error) {
3657 *error = g_error_copy (data->error);
3663 /********************************************
3664 * Default VFS operations *
3665 ********************************************/
3668 * g_file_new_for_path:
3669 * @path: a string containing a relative or absolute path.
3671 * Constructs a #GFile for given @path. This operation never
3672 * fails, but the returned object might not support any I/O
3673 * operation if the @path is malformed.
3675 * Returns: a new #GFile for the given @path.
3678 g_file_new_for_path (const char *path)
3680 g_return_val_if_fail (path != NULL, NULL);
3682 return g_vfs_get_file_for_path (g_vfs_get_default (),
3687 * g_file_new_for_uri:
3688 * @uri: a string containing a URI.
3690 * This operation never fails, but the returned object
3691 * might not support any I/O operation if the @uri
3692 * is malformed or if the uri type is not supported.
3694 * Returns: a #GFile for the given @uri.
3697 g_file_new_for_uri (const char *uri)
3699 g_return_val_if_fail (uri != NULL, NULL);
3701 return g_vfs_get_file_for_uri (g_vfs_get_default (),
3706 * g_file_parse_name:
3707 * @parse_name: a file name or path to be parsed.
3709 * Constructs a #GFile with the given @parse_name,
3710 * looked up by #GVfs. This operation never fails,
3711 * but the returned object might not support any I/O
3712 * operation if the @parse_name cannot be parsed by #GVfs.
3714 * Returns: a new #GFile.
3717 g_file_parse_name (const char *parse_name)
3719 g_return_val_if_fail (parse_name != NULL, NULL);
3721 return g_vfs_parse_name (g_vfs_get_default (),
3726 is_valid_scheme_character (char c)
3728 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
3732 has_valid_scheme (const char *uri)
3738 if (!is_valid_scheme_character (*p))
3743 } while (is_valid_scheme_character (*p));
3749 * g_file_new_for_commandline_arg:
3750 * @arg: a command line string.
3752 * Attempts to generate a #GFile with the given line from
3753 * the command line argument.
3755 * Returns: a new #GFile.
3758 g_file_new_for_commandline_arg (const char *arg)
3764 g_return_val_if_fail (arg != NULL, NULL);
3766 if (g_path_is_absolute (arg))
3767 return g_file_new_for_path (arg);
3769 if (has_valid_scheme (arg))
3770 return g_file_new_for_uri (arg);
3772 current_dir = g_get_current_dir ();
3773 filename = g_build_filename (current_dir, arg, NULL);
3774 g_free (current_dir);
3776 file = g_file_new_for_path (filename);
3783 * g_mount_for_location:
3784 * @location: input #GFile.
3785 * @mount_operation: a #GMountOperation.
3786 * @cancellable: optional #GCancellable object, %NULL to ignore.
3787 * @callback: a #GAsyncReadyCallback.
3788 * @user_data: a #gpointer.
3790 * Starts the @mount_operation, mounting the volume at @location.
3792 * When this operation has completed, @callback will be called with
3793 * @user_user data, and the operation can be finalized with
3794 * g_mount_for_location_finish().
3796 * If @cancellable is not %NULL, then the operation can be cancelled by
3797 * triggering the cancellable object from another thread. If the operation
3798 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3802 g_mount_for_location (GFile *location,
3803 GMountOperation *mount_operation,
3804 GCancellable *cancellable,
3805 GAsyncReadyCallback callback,
3810 g_return_if_fail (G_IS_FILE (location));
3811 g_return_if_fail (G_IS_MOUNT_OPERATION (mount_operation));
3813 iface = G_FILE_GET_IFACE (location);
3815 if (iface->mount_for_location == NULL)
3817 g_simple_async_report_error_in_idle (G_OBJECT (location),
3818 callback, user_data,
3819 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3820 _("volume doesn't implement mount"));
3825 (* iface->mount_for_location) (location, mount_operation, cancellable, callback, user_data);
3830 * g_mount_for_location_finish:
3831 * @location: input #GFile.
3832 * @result: a #GAsyncResult.
3833 * @error: a #GError, or %NULL
3835 * Finishes a mount operation started by g_mount_for_location().
3837 * Returns: %TRUE if successful. If an error
3838 * has occured, this function will return %FALSE and set @error
3839 * appropriately if present.
3842 g_mount_for_location_finish (GFile *location,
3843 GAsyncResult *result,
3848 g_return_val_if_fail (G_IS_FILE (location), FALSE);
3849 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3851 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3853 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3854 if (g_simple_async_result_propagate_error (simple, error))
3858 iface = G_FILE_GET_IFACE (location);
3860 return (* iface->mount_for_location_finish) (location, result, error);
3863 /********************************************
3864 * Utility functions *
3865 ********************************************/
3867 #define GET_CONTENT_BLOCK_SIZE 8192
3870 * g_file_load_contents:
3871 * @file: input #GFile.
3872 * @cancellable: optional #GCancellable object, %NULL to ignore.
3873 * @contents: a location to place the contents of the file.
3874 * @length: a location to place the length of the contents of the file.
3875 * @etag_out: a location to place the current entity tag for the file.
3876 * @error: a #GError, or %NULL
3878 * If @cancellable is not %NULL, then the operation can be cancelled by
3879 * triggering the cancellable object from another thread. If the operation
3880 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3882 * Returns: %TRUE if the @file's contents were successfully loaded.
3883 * %FALSE if there were errors. The length of the loaded data will be
3884 * put into @length, the contents in @contents.
3887 g_file_load_contents (GFile *file,
3888 GCancellable *cancellable,
3894 GFileInputStream *in;
3895 GByteArray *content;
3900 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3901 g_return_val_if_fail (contents != NULL, FALSE);
3903 in = g_file_read (file,
3909 content = g_byte_array_new ();
3912 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
3913 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
3914 content->data + pos,
3915 GET_CONTENT_BLOCK_SIZE,
3916 cancellable, error)) > 0)
3919 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
3926 info = g_file_input_stream_query_info (in,
3927 G_FILE_ATTRIBUTE_ETAG_VALUE,
3932 *etag_out = g_strdup (g_file_info_get_etag (info));
3933 g_object_unref (info);
3937 /* Ignore errors on close */
3938 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
3939 g_object_unref (in);
3943 /* error is set already */
3944 g_byte_array_free (content, TRUE);
3951 /* Zero terminate (we got an extra byte allocated for this */
3952 content->data[pos] = 0;
3954 *contents = (char *)g_byte_array_free (content, FALSE);
3962 GCancellable *cancellable;
3963 GFileReadMoreCallback read_more_callback;
3964 GAsyncReadyCallback callback;
3966 GByteArray *content;
3973 load_contents_data_free (LoadContentsData *data)
3976 g_error_free (data->error);
3977 if (data->cancellable)
3978 g_object_unref (data->cancellable);
3980 g_byte_array_free (data->content, TRUE);
3981 g_free (data->etag);
3982 g_object_unref (data->file);
3987 load_contents_close_callback (GObject *obj,
3988 GAsyncResult *close_res,
3991 GInputStream *stream = G_INPUT_STREAM (obj);
3992 LoadContentsData *data = user_data;
3993 GSimpleAsyncResult *res;
3995 /* Ignore errors here, we're only reading anyway */
3996 g_input_stream_close_finish (stream, close_res, NULL);
3997 g_object_unref (stream);
3999 res = g_simple_async_result_new (G_OBJECT (data->file),
4002 g_file_load_contents_async);
4003 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4004 g_simple_async_result_complete (res);
4005 g_object_unref (res);
4009 load_contents_fstat_callback (GObject *obj,
4010 GAsyncResult *stat_res,
4013 GInputStream *stream = G_INPUT_STREAM (obj);
4014 LoadContentsData *data = user_data;
4017 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4021 data->etag = g_strdup (g_file_info_get_etag (info));
4022 g_object_unref (info);
4025 g_input_stream_close_async (stream, 0,
4027 load_contents_close_callback, data);
4031 load_contents_read_callback (GObject *obj,
4032 GAsyncResult *read_res,
4035 GInputStream *stream = G_INPUT_STREAM (obj);
4036 LoadContentsData *data = user_data;
4037 GError *error = NULL;
4040 read_size = g_input_stream_read_finish (stream, read_res, &error);
4044 /* Error or EOF, close the file */
4045 data->error = error;
4046 g_input_stream_close_async (stream, 0,
4048 load_contents_close_callback, data);
4050 else if (read_size == 0)
4052 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4053 G_FILE_ATTRIBUTE_ETAG_VALUE,
4056 load_contents_fstat_callback,
4059 else if (read_size > 0)
4061 data->pos += read_size;
4063 g_byte_array_set_size (data->content,
4064 data->pos + GET_CONTENT_BLOCK_SIZE);
4067 if (data->read_more_callback &&
4068 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4069 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4070 G_FILE_ATTRIBUTE_ETAG_VALUE,
4073 load_contents_fstat_callback,
4076 g_input_stream_read_async (stream,
4077 data->content->data + data->pos,
4078 GET_CONTENT_BLOCK_SIZE,
4081 load_contents_read_callback,
4087 load_contents_open_callback (GObject *obj,
4088 GAsyncResult *open_res,
4091 GFile *file = G_FILE (obj);
4092 GFileInputStream *stream;
4093 LoadContentsData *data = user_data;
4094 GError *error = NULL;
4095 GSimpleAsyncResult *res;
4097 stream = g_file_read_finish (file, open_res, &error);
4101 g_byte_array_set_size (data->content,
4102 data->pos + GET_CONTENT_BLOCK_SIZE);
4103 g_input_stream_read_async (G_INPUT_STREAM (stream),
4104 data->content->data + data->pos,
4105 GET_CONTENT_BLOCK_SIZE,
4108 load_contents_read_callback,
4114 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4118 g_simple_async_result_complete (res);
4119 g_error_free (error);
4120 load_contents_data_free (data);
4121 g_object_unref (res);
4126 * g_file_load_partial_contents_async:
4127 * @file: input #GFile.
4128 * @cancellable: optional #GCancellable object, %NULL to ignore.
4129 * @read_more_callback: a #GFileReadMoreCallback.
4130 * @callback: a #GAsyncReadyCallback.
4131 * @user_data: a #gpointer.
4133 * If @cancellable is not %NULL, then the operation can be cancelled by
4134 * triggering the cancellable object from another thread. If the operation
4135 * 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.
4250 g_file_load_contents_async (GFile *file,
4251 GCancellable *cancellable,
4252 GAsyncReadyCallback callback,
4255 g_file_load_partial_contents_async (file,
4258 callback, user_data);
4263 * g_file_load_contents_finish:
4264 * @file: input #GFile.
4265 * @res: a #GAsyncResult.
4266 * @contents: a location to place the contents of the file.
4267 * @length: a location to place the length of the contents of the file.
4268 * @etag_out: a location to place the current entity tag for the file.
4269 * @error: a #GError, or %NULL
4271 * Finishes an asynchronous load of the @file's contents. The contents
4272 * are placed in @contents, and @length is set to the size of the @contents
4273 * string. If @etag_out is present, it will be set to the new entity
4274 * tag for the @file.
4276 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4277 * present, it will be set appropriately.
4280 g_file_load_contents_finish (GFile *file,
4287 return g_file_load_partial_contents_finish (file,
4297 * g_file_replace_contents:
4298 * @file: input #GFile.
4299 * @contents: a string containing the new contents for @file.
4300 * @length: the length of @contents in bytes.
4301 * @etag: the old entity tag for the document.
4302 * @make_backup: a #gboolean.
4303 * @flags: a set of #GFileCreateFlags.
4304 * @new_etag: a location to a new entity tag for the document.
4305 * @cancellable: optional #GCancellable object, %NULL to ignore.
4306 * @error: a #GError, or %NULL
4308 * Replaces the contents of @file with @contents of @length bytes. The old
4309 * @etag will be replaced with the @new_etag. If @make_backup is %TRUE,
4310 * this function will attempt to make a backup of @file.
4312 * If @cancellable is not %NULL, then the operation can be cancelled by
4313 * triggering the cancellable object from another thread. If the operation
4314 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4316 * Returns: %TRUE if successful. If an error
4317 * has occured, this function will return %FALSE and set @error
4318 * appropriately if present.
4321 g_file_replace_contents (GFile *file,
4322 const char *contents,
4325 gboolean make_backup,
4326 GFileCreateFlags flags,
4328 GCancellable *cancellable,
4331 GFileOutputStream *out;
4332 gsize pos, remainder;
4335 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4336 g_return_val_if_fail (contents != NULL, FALSE);
4338 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4344 while (remainder > 0 &&
4345 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4347 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4355 if (remainder > 0 && res < 0)
4357 /* Ignore errors on close */
4358 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4360 /* error is set already */
4364 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4368 *new_etag = g_file_output_stream_get_etag (out);
4376 GCancellable *cancellable;
4377 GAsyncReadyCallback callback;
4379 const char *content;
4383 } ReplaceContentsData;
4386 replace_contents_data_free (ReplaceContentsData *data)
4389 g_error_free (data->error);
4390 if (data->cancellable)
4391 g_object_unref (data->cancellable);
4392 g_object_unref (data->file);
4393 g_free (data->etag);
4398 replace_contents_close_callback (GObject *obj,
4399 GAsyncResult *close_res,
4402 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4403 ReplaceContentsData *data = user_data;
4404 GSimpleAsyncResult *res;
4406 /* Ignore errors here, we're only reading anyway */
4407 g_output_stream_close_finish (stream, close_res, NULL);
4408 g_object_unref (stream);
4410 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4412 res = g_simple_async_result_new (G_OBJECT (data->file),
4415 g_file_replace_contents_async);
4416 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4417 g_simple_async_result_complete (res);
4418 g_object_unref (res);
4422 replace_contents_write_callback (GObject *obj,
4423 GAsyncResult *read_res,
4426 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4427 ReplaceContentsData *data = user_data;
4428 GError *error = NULL;
4431 write_size = g_output_stream_write_finish (stream, read_res, &error);
4433 if (write_size <= 0)
4435 /* Error or EOF, close the file */
4437 data->error = error;
4438 g_output_stream_close_async (stream, 0,
4440 replace_contents_close_callback, data);
4442 else if (write_size > 0)
4444 data->pos += write_size;
4446 if (data->pos >= data->length)
4447 g_output_stream_close_async (stream, 0,
4449 replace_contents_close_callback, data);
4451 g_output_stream_write_async (stream,
4452 data->content + data->pos,
4453 data->length - data->pos,
4456 replace_contents_write_callback,
4462 replace_contents_open_callback (GObject *obj,
4463 GAsyncResult *open_res,
4466 GFile *file = G_FILE (obj);
4467 GFileOutputStream *stream;
4468 ReplaceContentsData *data = user_data;
4469 GError *error = NULL;
4470 GSimpleAsyncResult *res;
4472 stream = g_file_replace_finish (file, open_res, &error);
4476 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4477 data->content + data->pos,
4478 data->length - data->pos,
4481 replace_contents_write_callback,
4487 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4491 g_simple_async_result_complete (res);
4492 g_error_free (error);
4493 replace_contents_data_free (data);
4494 g_object_unref (res);
4499 * g_file_replace_contents_async:
4500 * @file: input #GFile.
4501 * @contents: string of contents to replace the file with.
4502 * @length: the length of @contents in bytes.
4503 * @etag: a new entity tag for the @file.
4504 * @make_backup: a #gboolean.
4505 * @flags: a set of #GFileCreateFlags.
4506 * @cancellable: optional #GCancellable object, %NULL to ignore.
4507 * @callback: a #GAsyncReadyCallback.
4508 * @user_data: a #gpointer.
4510 * Starts an asynchronous replacement of @file with the given
4511 * @contents of @length bytes. @etag will replace the document's
4512 * current entity tag.
4514 * When this operation has completed, @callback will be called with
4515 * @user_user data, and the operation can be finalized with
4516 * g_file_replace_contents_finish().
4518 * If @cancellable is not %NULL, then the operation can be cancelled by
4519 * triggering the cancellable object from another thread. If the operation
4520 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4522 * If @make_backup is %TRUE, this function will attempt to
4523 * make a backup of @file.
4527 g_file_replace_contents_async (GFile *file,
4528 const char *contents,
4531 gboolean make_backup,
4532 GFileCreateFlags flags,
4533 GCancellable *cancellable,
4534 GAsyncReadyCallback callback,
4537 ReplaceContentsData *data;
4539 g_return_if_fail (G_IS_FILE (file));
4540 g_return_if_fail (contents != NULL);
4542 data = g_new0 (ReplaceContentsData, 1);
4545 data->cancellable = g_object_ref (cancellable);
4546 data->callback = callback;
4547 data->user_data = user_data;
4548 data->content = contents;
4549 data->length = length;
4551 data->file = g_object_ref (file);
4553 g_file_replace_async (file,
4559 replace_contents_open_callback,
4564 * g_file_replace_contents_finish:
4565 * @file: input #GFile.
4566 * @res: a #GAsyncResult.
4567 * @new_etag: a location of a new entity tag for the document.
4568 * @error: a #GError, or %NULL
4570 * Finishes an asynchronous replace of the given @file. Sets
4571 * @new_etag to the new entity tag for the document, if present.
4573 * Returns: %TRUE on success, %FALSE on failure.
4576 g_file_replace_contents_finish (GFile *file,
4581 GSimpleAsyncResult *simple;
4582 ReplaceContentsData *data;
4584 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4585 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4587 simple = G_SIMPLE_ASYNC_RESULT (res);
4589 if (g_simple_async_result_propagate_error (simple, error))
4592 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
4594 data = g_simple_async_result_get_op_res_gpointer (simple);
4598 g_propagate_error (error, data->error);
4606 *new_etag = data->etag;
4607 data->etag = NULL; /* Take ownership */