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 "gfileattribute-priv.h"
35 #include "gpollfilemonitor.h"
42 * @short_description: File and Directory Handling
44 * @see_also: #GFileInfo, #GFileEnumerator
46 * #GFile is a high level abstraction for manipulating files on a
47 * virtual file system. #GFile<!-- -->s are lightweight, immutable
48 * objects that do no I/O upon creation. It is necessary to understand that
49 * #GFile objects do not represent files, merely a handle to a file. All
50 * file I/O is implemented as streaming operations (see #GInputStream and
53 * To construct a #GFile, you can use:
54 * g_file_new_for_path() if you have a path.
55 * g_file_new_for_uri() if you have a URI.
56 * g_file_new_for_commandline_arg() for a command line argument.
58 * You can move through the file system with #GFile handles with
59 * g_file_get_parent() to get a handle to the parent directory.
60 * g_file_get_child() to get a handle to a child within a directory.
61 * g_file_resolve_relative_path() to resolve a relative path between
62 * two #GFile<!-- -->s.
64 * Many #GFile operations have both synchronous and asynchronous versions
65 * to suit your application. Asynchronous versions of synchronous functions
66 * simply have _async() appended to their function names. The asynchronous
67 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
68 * the operation, producing a GAsyncResult which is then passed to the
69 * function's matching _finish()
72 * Some #GFile operations do not have synchronous analogs, as they may
73 * take a very long time to finish, and blocking may leave an application
74 * unusable. Notable cases include:
75 * g_file_mount_mountable() to mount a mountable file.
76 * g_file_unmount_mountable() to unmount a mountable file.
77 * g_file_eject_mountable() to eject a mountable file.
79 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
80 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
81 * short. Entity tags are somewhat like a more abstract version of the
82 * traditional mtime, and can be used to quickly determine if the file has
83 * been modified from the version on the file system. See the HTTP 1.1
84 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
85 * for HTTP Etag headers, which are a very similar concept.
89 static void g_file_base_init (gpointer g_class);
90 static void g_file_class_init (gpointer g_class,
93 static void g_file_real_query_info_async (GFile *file,
94 const char *attributes,
95 GFileQueryInfoFlags flags,
97 GCancellable *cancellable,
98 GAsyncReadyCallback callback,
100 static GFileInfo * g_file_real_query_info_finish (GFile *file,
103 static void g_file_real_enumerate_children_async (GFile *file,
104 const char *attributes,
105 GFileQueryInfoFlags flags,
107 GCancellable *cancellable,
108 GAsyncReadyCallback callback,
110 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
113 static void g_file_real_read_async (GFile *file,
115 GCancellable *cancellable,
116 GAsyncReadyCallback callback,
118 static GFileInputStream * g_file_real_read_finish (GFile *file,
121 static void g_file_real_append_to_async (GFile *file,
122 GFileCreateFlags flags,
124 GCancellable *cancellable,
125 GAsyncReadyCallback callback,
127 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
130 static void g_file_real_create_async (GFile *file,
131 GFileCreateFlags flags,
133 GCancellable *cancellable,
134 GAsyncReadyCallback callback,
136 static GFileOutputStream *g_file_real_create_finish (GFile *file,
139 static void g_file_real_replace_async (GFile *file,
141 gboolean make_backup,
142 GFileCreateFlags flags,
144 GCancellable *cancellable,
145 GAsyncReadyCallback callback,
147 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
150 static gboolean g_file_real_set_attributes_from_info (GFile *file,
152 GFileQueryInfoFlags flags,
153 GCancellable *cancellable,
155 static void g_file_real_set_display_name_async (GFile *file,
156 const char *display_name,
158 GCancellable *cancellable,
159 GAsyncReadyCallback callback,
161 static GFile * g_file_real_set_display_name_finish (GFile *file,
164 static void g_file_real_set_attributes_async (GFile *file,
166 GFileQueryInfoFlags flags,
168 GCancellable *cancellable,
169 GAsyncReadyCallback callback,
171 static gboolean g_file_real_set_attributes_finish (GFile *file,
175 static void g_file_real_find_enclosing_mount_async (GFile *file,
177 GCancellable *cancellable,
178 GAsyncReadyCallback callback,
180 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
183 static void g_file_real_copy_async (GFile *source,
185 GFileCopyFlags flags,
187 GCancellable *cancellable,
188 GFileProgressCallback progress_callback,
189 gpointer progress_callback_data,
190 GAsyncReadyCallback callback,
192 static gboolean g_file_real_copy_finish (GFile *file,
197 g_file_get_type (void)
199 static GType file_type = 0;
203 static const GTypeInfo file_info =
205 sizeof (GFileIface), /* class_size */
206 g_file_base_init, /* base_init */
207 NULL, /* base_finalize */
209 NULL, /* class_finalize */
210 NULL, /* class_data */
217 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
220 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
227 g_file_class_init (gpointer g_class,
230 GFileIface *iface = g_class;
232 iface->enumerate_children_async = g_file_real_enumerate_children_async;
233 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
234 iface->set_display_name_async = g_file_real_set_display_name_async;
235 iface->set_display_name_finish = g_file_real_set_display_name_finish;
236 iface->query_info_async = g_file_real_query_info_async;
237 iface->query_info_finish = g_file_real_query_info_finish;
238 iface->set_attributes_async = g_file_real_set_attributes_async;
239 iface->set_attributes_finish = g_file_real_set_attributes_finish;
240 iface->read_async = g_file_real_read_async;
241 iface->read_finish = g_file_real_read_finish;
242 iface->append_to_async = g_file_real_append_to_async;
243 iface->append_to_finish = g_file_real_append_to_finish;
244 iface->create_async = g_file_real_create_async;
245 iface->create_finish = g_file_real_create_finish;
246 iface->replace_async = g_file_real_replace_async;
247 iface->replace_finish = g_file_real_replace_finish;
248 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
249 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
250 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
251 iface->copy_async = g_file_real_copy_async;
252 iface->copy_finish = g_file_real_copy_finish;
256 g_file_base_init (gpointer g_class)
263 * @file: input #GFile.
265 * Checks to see if a file is native to the platform.
267 * A native file s one expressed in the platform-native filename format,
268 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
269 * as it might be on a locally mounted remote filesystem.
271 * On some systems non-native files may be available using
272 * the native filesystem via a userspace filesystem (FUSE), in
273 * these cases this call will return %FALSE, but g_file_get_path()
274 * will still return a native path.
276 * This call does no blocking i/o.
278 * Returns: %TRUE if file is native.
281 g_file_is_native (GFile *file)
285 g_return_val_if_fail (G_IS_FILE (file), FALSE);
287 iface = G_FILE_GET_IFACE (file);
289 return (* iface->is_native) (file);
294 * g_file_has_uri_scheme:
295 * @file: input #GFile.
296 * @uri_scheme: a string containing a URI scheme.
298 * Checks to see if a #GFile has a given URI scheme.
300 * This call does no blocking i/o.
302 * Returns: %TRUE if #GFile's backend supports the
303 * given URI scheme, %FALSE if URI scheme is %NULL,
304 * not supported, or #GFile is invalid.
307 g_file_has_uri_scheme (GFile *file,
308 const char *uri_scheme)
312 g_return_val_if_fail (G_IS_FILE (file), FALSE);
313 g_return_val_if_fail (uri_scheme != NULL, FALSE);
315 iface = G_FILE_GET_IFACE (file);
317 return (* iface->has_uri_scheme) (file, uri_scheme);
322 * g_file_get_uri_scheme:
323 * @file: input #GFile.
325 * Gets the URI scheme for a #GFile.
326 * RFC 3986 decodes the scheme as:
328 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
330 * Common schemes include "file", "http", "ftp", etc.
332 * This call does no blocking i/o.
334 * Returns: a string containing the URI scheme for the given
335 * #GFile. The returned string should be freed with g_free()
336 * when no longer needed.
339 g_file_get_uri_scheme (GFile *file)
343 g_return_val_if_fail (G_IS_FILE (file), NULL);
345 iface = G_FILE_GET_IFACE (file);
347 return (* iface->get_uri_scheme) (file);
352 * g_file_get_basename:
353 * @file: input #GFile.
355 * Gets the base name (the last component of the path) for a given #GFile.
357 * If called for the top level of a system (such as the filesystem root
358 * or a uri like sftp://host/ it will return a single directory separator
359 * (and on Windows, possibly a drive letter).
361 * This call does no blocking i/o.
363 * Returns: string containing the #GFile's base name, or %NULL
364 * if given #GFile is invalid. The returned string should be
365 * freed with g_free() when no longer needed.
368 g_file_get_basename (GFile *file)
372 g_return_val_if_fail (G_IS_FILE (file), NULL);
374 iface = G_FILE_GET_IFACE (file);
376 return (* iface->get_basename) (file);
381 * @file: input #GFile.
383 * Gets the local pathname for #GFile, if one exists.
385 * This call does no blocking i/o.
387 * Returns: string containing the #GFile's path, or %NULL if
388 * no such path exists. The returned string should be
389 * freed with g_free() when no longer needed.
392 g_file_get_path (GFile *file)
396 g_return_val_if_fail (G_IS_FILE (file), NULL);
398 iface = G_FILE_GET_IFACE (file);
400 return (* iface->get_path) (file);
405 * @file: input #GFile.
407 * Gets the URI for the @file.
409 * This call does no blocking i/o.
411 * Returns: a string containing the #GFile's URI.
412 * The returned string should be freed with g_free() when no longer needed.
415 g_file_get_uri (GFile *file)
419 g_return_val_if_fail (G_IS_FILE (file), NULL);
421 iface = G_FILE_GET_IFACE (file);
423 return (* iface->get_uri) (file);
427 * g_file_get_parse_name:
428 * @file: input #GFile.
430 * Gets the parse name of the @file.
431 * A parse name is a UTF-8 string that describes the
432 * file such that one can get the #GFile back using
433 * g_file_parse_name().
435 * This is generally used to show the #GFile as a nice
436 * string in a user interface, like in a location entry.
438 * For local files with names that can safely be converted
439 * to UTF8 the pathname is used, otherwise the IRI is used
440 * (a form of URI that allows UTF8 characters unescaped).
442 * This call does no blocking i/o.
444 * Returns: a string containing the #GFile's parse name. The returned
445 * string should be freed with g_free() when no longer needed.
448 g_file_get_parse_name (GFile *file)
452 g_return_val_if_fail (G_IS_FILE (file), NULL);
454 iface = G_FILE_GET_IFACE (file);
456 return (* iface->get_parse_name) (file);
461 * @file: input #GFile.
463 * Duplicates a #GFile handle. This operation does not duplicate
464 * the actual file or directory represented by the #GFile; see
465 * g_file_copy() if attempting to copy a file.
467 * This call does no blocking i/o.
469 * Returns: #GFile that is a duplicate of the given #GFile.
472 g_file_dup (GFile *file)
476 g_return_val_if_fail (G_IS_FILE (file), NULL);
478 iface = G_FILE_GET_IFACE (file);
480 return (* iface->dup) (file);
485 * @file: #gconstpointer to a #GFile.
487 * Creates a hash value for a #GFile.
489 * This call does no blocking i/o.
491 * Returns: 0 if @file is not a valid #GFile, otherwise an
492 * integer that can be used as hash value for the #GFile.
493 * This function is intended for easily hashing a #GFile to
494 * add to a #GHashTable or similar data structure.
497 g_file_hash (gconstpointer file)
501 g_return_val_if_fail (G_IS_FILE (file), 0);
503 iface = G_FILE_GET_IFACE (file);
505 return (* iface->hash) ((GFile *)file);
510 * @file1: the first #GFile.
511 * @file2: the second #GFile.
513 * Checks equality of two given #GFile<!-- -->s
515 * This call does no blocking i/o.
517 * Returns: %TRUE if @file1 and @file2 are equal.
518 * %FALSE if either is not a #GFile.
521 g_file_equal (GFile *file1,
526 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
527 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
529 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
532 iface = G_FILE_GET_IFACE (file1);
534 return (* iface->equal) (file1, file2);
540 * @file: input #GFile.
542 * Gets the parent directory for the @file.
543 * If the @file represents the root directory of the
544 * file system, then %NULL will be returned.
546 * This call does no blocking i/o.
548 * Returns: a #GFile structure to the parent of the given
549 * #GFile or %NULL if there is no parent.
552 g_file_get_parent (GFile *file)
556 g_return_val_if_fail (G_IS_FILE (file), NULL);
558 iface = G_FILE_GET_IFACE (file);
560 return (* iface->get_parent) (file);
565 * @file: input #GFile.
566 * @name: string containing the child's name.
568 * Gets a specific child of @file with name equal to @name.
570 * Note that the file with that specific name might not exist, but
571 * you can still have a #GFile that points to it. You can use this
572 * for instance to create that file.
574 * This call does no blocking i/o.
576 * Returns: a #GFile to a child specified by @name.
579 g_file_get_child (GFile *file,
582 g_return_val_if_fail (G_IS_FILE (file), NULL);
583 g_return_val_if_fail (name != NULL, NULL);
585 return g_file_resolve_relative_path (file, name);
589 * g_file_get_child_for_display_name:
590 * @file: input #GFile.
591 * @display_name: string to a possible child.
594 * Gets the child of @file for a given @display_name (i.e. a UTF8
595 * version of the name). If this function fails, it returns %NULL and @error will be
596 * set. This is very useful when constructing a GFile for a new file
597 * and the user entered the filename in the user interface, for instance
598 * when you select a directory and type a filename in the file selector.
600 * This call does no blocking i/o.
602 * Returns: a #GFile to the specified child, or
603 * %NULL if the display name couldn't be converted.
606 g_file_get_child_for_display_name (GFile *file,
607 const char *display_name,
612 g_return_val_if_fail (G_IS_FILE (file), NULL);
613 g_return_val_if_fail (display_name != NULL, NULL);
615 iface = G_FILE_GET_IFACE (file);
617 return (* iface->get_child_for_display_name) (file, display_name, error);
621 * g_file_contains_file:
622 * @parent: input #GFile.
623 * @descendant: input #GFile.
625 * Checks whether @parent (recursively) contains the specified @descendant.
627 * This call does no blocking i/o.
629 * Returns: %TRUE if the @descendant's parent, grandparent, etc is @parent. %FALSE otherwise.
632 g_file_contains_file (GFile *parent,
637 g_return_val_if_fail (G_IS_FILE (parent), FALSE);
638 g_return_val_if_fail (G_IS_FILE (descendant), FALSE);
640 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
643 iface = G_FILE_GET_IFACE (parent);
645 return (* iface->contains_file) (parent, descendant);
649 * g_file_get_relative_path:
650 * @parent: input #GFile.
651 * @descendant: input #GFile.
653 * Gets the path for @descendant relative to @parent.
655 * This call does no blocking i/o.
657 * Returns: string with the relative path from @descendant
658 * to @parent, or %NULL if @descendant is not a descendant of @parent. The returned string should be freed with
659 * g_free() when no longer needed.
662 g_file_get_relative_path (GFile *parent,
667 g_return_val_if_fail (G_IS_FILE (parent), NULL);
668 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
670 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
673 iface = G_FILE_GET_IFACE (parent);
675 return (* iface->get_relative_path) (parent, descendant);
679 * g_file_resolve_relative_path:
680 * @file: input #GFile.
681 * @relative_path: a given relative path string.
683 * Resolves a relative path for @file to an absolute path.
685 * This call does no blocking i/o.
687 * Returns: #GFile to the resolved path. %NULL if @relative_path
688 * is %NULL or if @file is invalid.
691 g_file_resolve_relative_path (GFile *file,
692 const char *relative_path)
696 g_return_val_if_fail (G_IS_FILE (file), NULL);
697 g_return_val_if_fail (relative_path != NULL, NULL);
699 iface = G_FILE_GET_IFACE (file);
701 return (* iface->resolve_relative_path) (file, relative_path);
705 * g_file_enumerate_children:
706 * @file: input #GFile.
707 * @attributes: an attribute query string.
708 * @flags: a set of #GFileQueryInfoFlags.
709 * @cancellable: optional #GCancellable object, %NULL to ignore.
710 * @error: #GError for error reporting.
712 * Gets the requested information about the files in a directory. The result
713 * is a #GFileEnumerator object that will give out #GFileInfo objects for
714 * all the files in the directory.
716 * The @attribute value is a string that specifies the file attributes that
717 * should be gathered. It is not an error if it's not possible to read a particular
718 * requested attribute from a file - it just won't be set. @attribute should
719 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
720 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
721 * namespace. An example attribute query be "standard::*,owner::user".
722 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
724 * If @cancellable is not %NULL, then the operation can be cancelled by
725 * triggering the cancellable object from another thread. If the operation
726 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
728 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
729 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
730 * Other errors are possible too.
732 * Returns: A #GFileEnumerator if successful, %NULL on error.
735 g_file_enumerate_children (GFile *file,
736 const char *attributes,
737 GFileQueryInfoFlags flags,
738 GCancellable *cancellable,
744 g_return_val_if_fail (G_IS_FILE (file), NULL);
746 if (g_cancellable_set_error_if_cancelled (cancellable, error))
749 iface = G_FILE_GET_IFACE (file);
751 if (iface->enumerate_children == NULL)
753 g_set_error (error, G_IO_ERROR,
754 G_IO_ERROR_NOT_SUPPORTED,
755 _("Operation not supported"));
759 return (* iface->enumerate_children) (file, attributes, flags,
764 * g_file_enumerate_children_async:
765 * @file: input #GFile.
766 * @attributes: an attribute query string.
767 * @flags: a set of #GFileQueryInfoFlags.
768 * @io_priority: the <link linkend="io-priority">I/O priority</link>
770 * @cancellable: optional #GCancellable object, %NULL to ignore.
771 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
772 * @user_data: the data to pass to callback function
774 * Asynchronously gets the requested information about the files in a directory. The result
775 * is a #GFileEnumerator object that will give out #GFileInfo objects for
776 * all the files in the directory.
778 * For more details, see g_file_enumerate_children() which is
779 * the synchronous version of this call.
781 * When the operation is finished, @callback will be called. You can then call
782 * g_file_enumerate_children_finish() to get the result of the operation.
785 g_file_enumerate_children_async (GFile *file,
786 const char *attributes,
787 GFileQueryInfoFlags flags,
789 GCancellable *cancellable,
790 GAsyncReadyCallback callback,
795 g_return_if_fail (G_IS_FILE (file));
797 iface = G_FILE_GET_IFACE (file);
798 (* iface->enumerate_children_async) (file,
808 * g_file_enumerate_children_finish:
809 * @file: input #GFile.
810 * @res: a #GAsyncResult.
813 * Finishes an async enumerate children operation.
814 * See g_file_enumerate_children_async().
816 * Returns: a #GFileEnumerator or %NULL if an error occurred.
819 g_file_enumerate_children_finish (GFile *file,
825 g_return_val_if_fail (G_IS_FILE (file), NULL);
826 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
828 if (G_IS_SIMPLE_ASYNC_RESULT (res))
830 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
831 if (g_simple_async_result_propagate_error (simple, error))
835 iface = G_FILE_GET_IFACE (file);
836 return (* iface->enumerate_children_finish) (file, res, error);
840 * g_file_query_exists:
841 * @file: input #GFile.
842 * @cancellable: optional #GCancellable object, %NULL to ignore.
844 * Utility function to check if a particular file exists. This is
845 * implemented using g_file_query_info() and as such does blocking I/O.
847 * Note that in many cases it is racy to first check for file existance
848 * and then execute something based on the outcome of that, because the
849 * file might have been created or removed inbetween the operations. The
850 * general approach to handling that is to not check, but just do the
851 * operation and handle the errors as they come.
853 * As an example of race-free checking, take the case of reading a file, and
854 * if it doesn't exist, creating it. There are two racy versions: read it, and
855 * on error create it; and: check if it exists, if not create it. These
856 * can both result in two processes creating the file (with perhaps a partially
857 * written file as the result). The correct approach is to always try to create
858 * the file with g_file_create() which will either atomically create the file
859 * or fail with a G_IO_ERROR_EXISTS error.
861 * However, in many cases an existance check is useful in a user
862 * interface, for instance to make a menu item sensitive/insensitive, so that
863 * you don't have to fool users that something is possible and then just show
864 * and error dialog. If you do this, you should make sure to also handle the
865 * errors that can happen due to races when you execute the operation.
867 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
870 g_file_query_exists (GFile *file,
871 GCancellable *cancellable)
875 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
876 G_FILE_QUERY_INFO_NONE,
880 g_object_unref (info);
889 * @file: input #GFile.
890 * @attributes: an attribute query string.
891 * @flags: a set of #GFileQueryInfoFlags.
892 * @cancellable: optional #GCancellable object, %NULL to ignore.
895 * Gets the requested information about specified @file. The result
896 * is a #GFileInfo object that contains key-value attributes (such as
897 * the type or size of the file).
899 * The @attribute value is a string that specifies the file attributes that
900 * should be gathered. It is not an error if it's not possible to read a particular
901 * requested attribute from a file - it just won't be set. @attribute should
902 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
903 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
904 * namespace. An example attribute query be "standard::*,owner::user".
905 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
907 * If @cancellable is not %NULL, then the operation can be cancelled by
908 * triggering the cancellable object from another thread. If the operation
909 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
911 * For symlinks, normally the information about the target of the
912 * symlink is returned, rather than information about the symlink itself.
913 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
914 * information about the symlink itself will be returned. Also, for symlinks
915 * that point to non-existing files the information about the symlink itself
918 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
919 * Other errors are possible too, and depend on what kind of filesystem the file is on.
921 * Returns: a #GFileInfo for the given @file, or %NULL on error.
924 g_file_query_info (GFile *file,
925 const char *attributes,
926 GFileQueryInfoFlags flags,
927 GCancellable *cancellable,
932 g_return_val_if_fail (G_IS_FILE (file), NULL);
934 if (g_cancellable_set_error_if_cancelled (cancellable, error))
937 iface = G_FILE_GET_IFACE (file);
939 if (iface->query_info == NULL)
941 g_set_error (error, G_IO_ERROR,
942 G_IO_ERROR_NOT_SUPPORTED,
943 _("Operation not supported"));
947 return (* iface->query_info) (file, attributes, flags, cancellable, error);
951 * g_file_query_info_async:
952 * @file: input #GFile.
953 * @attributes: an attribute query string.
954 * @flags: a set of #GFileQueryInfoFlags.
955 * @io_priority: the <link linkend="io-priority">I/O priority</link>
957 * @cancellable: optional #GCancellable object, %NULL to ignore.
958 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
959 * @user_data: the data to pass to callback function
961 * Asynchronously gets the requested information about specified @file. The result
962 * is a #GFileInfo object that contains key-value attributes (such as type or size
965 * For more details, see g_file_query_info() which is
966 * the synchronous version of this call.
968 * When the operation is finished, @callback will be called. You can then call
969 * g_file_query_info_finish() to get the result of the operation.
972 g_file_query_info_async (GFile *file,
973 const char *attributes,
974 GFileQueryInfoFlags flags,
976 GCancellable *cancellable,
977 GAsyncReadyCallback callback,
982 g_return_if_fail (G_IS_FILE (file));
984 iface = G_FILE_GET_IFACE (file);
985 (* iface->query_info_async) (file,
995 * g_file_query_info_finish:
996 * @file: input #GFile.
997 * @res: a #GAsyncResult.
1000 * Finishes an asynchronous file info query.
1001 * See g_file_query_info_async().
1003 * Returns: #GFileInfo for given @file or %NULL on error.
1006 g_file_query_info_finish (GFile *file,
1012 g_return_val_if_fail (G_IS_FILE (file), NULL);
1013 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1015 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1017 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1018 if (g_simple_async_result_propagate_error (simple, error))
1022 iface = G_FILE_GET_IFACE (file);
1023 return (* iface->query_info_finish) (file, res, error);
1027 * g_file_query_filesystem_info:
1028 * @file: input #GFile.
1029 * @attributes: an attribute query string.
1030 * @cancellable: optional #GCancellable object, %NULL to ignore.
1031 * @error: a #GError.
1033 * Similar to g_file_query_info(), but obtains information
1034 * about the filesystem the @file is on, rather than the file itself.
1035 * For instance the amount of space available and the type of
1038 * The @attribute value is a string that specifies the file attributes that
1039 * should be gathered. It is not an error if it's not possible to read a particular
1040 * requested attribute from a file - it just won't be set. @attribute should
1041 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1042 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1043 * namespace. The standard namespace for filesystem attributes is "fs".
1044 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1045 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1046 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1048 * If @cancellable is not %NULL, then the operation can be cancelled by
1049 * triggering the cancellable object from another thread. If the operation
1050 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1052 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1053 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1055 * Returns: a #GFileInfo or %NULL if there was an error.
1058 g_file_query_filesystem_info (GFile *file,
1059 const char *attributes,
1060 GCancellable *cancellable,
1065 g_return_val_if_fail (G_IS_FILE (file), NULL);
1067 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1070 iface = G_FILE_GET_IFACE (file);
1072 if (iface->query_filesystem_info == NULL)
1074 g_set_error (error, G_IO_ERROR,
1075 G_IO_ERROR_NOT_SUPPORTED,
1076 _("Operation not supported"));
1080 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1084 * g_file_find_enclosing_mount:
1085 * @file: input #GFile.
1086 * @cancellable: optional #GCancellable object, %NULL to ignore.
1087 * @error: a #GError.
1089 * Gets a #GMount for the #GFile.
1091 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1092 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1095 * If @cancellable is not %NULL, then the operation can be cancelled by
1096 * triggering the cancellable object from another thread. If the operation
1097 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1099 * Returns: a #GMount where the @file is located or %NULL on error.
1102 g_file_find_enclosing_mount (GFile *file,
1103 GCancellable *cancellable,
1108 g_return_val_if_fail (G_IS_FILE (file), NULL);
1110 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1113 iface = G_FILE_GET_IFACE (file);
1114 if (iface->find_enclosing_mount == NULL)
1116 g_set_error (error, G_IO_ERROR,
1117 G_IO_ERROR_NOT_FOUND,
1118 _("Containing mount does not exist"));
1122 return (* iface->find_enclosing_mount) (file, cancellable, error);
1125 * g_file_find_enclosing_mount_async:
1127 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1129 * @cancellable: optional #GCancellable object, %NULL to ignore.
1130 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1131 * @user_data: the data to pass to callback function
1133 * Asynchronously gets the mount for the file.
1135 * For more details, see g_file_find_enclosing_mount() which is
1136 * the synchronous version of this call.
1138 * When the operation is finished, @callback will be called. You can then call
1139 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1142 g_file_find_enclosing_mount_async (GFile *file,
1144 GCancellable *cancellable,
1145 GAsyncReadyCallback callback,
1150 g_return_if_fail (G_IS_FILE (file));
1152 iface = G_FILE_GET_IFACE (file);
1153 (* iface->find_enclosing_mount_async) (file,
1161 * g_file_find_enclosing_mount_finish:
1163 * @res: a #GAsyncResult
1166 * Finishes an asynchronous find mount request.
1167 * See g_file_find_enclosing_mount_async().
1169 * Returns: #GMount for given @file or %NULL on error.
1172 g_file_find_enclosing_mount_finish (GFile *file,
1178 g_return_val_if_fail (G_IS_FILE (file), NULL);
1179 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1181 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1183 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1184 if (g_simple_async_result_propagate_error (simple, error))
1188 iface = G_FILE_GET_IFACE (file);
1189 return (* iface->find_enclosing_mount_finish) (file, res, error);
1195 * @file: #GFile to read.
1196 * @cancellable: a #GCancellable
1197 * @error: a #GError, or %NULL
1199 * Opens a file for reading. The result is a #GFileInputStream that
1200 * can be used to read the contents of the file.
1202 * If @cancellable is not %NULL, then the operation can be cancelled by
1203 * triggering the cancellable object from another thread. If the operation
1204 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1206 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1207 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1208 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1210 * Returns: #GFileInputStream or %NULL on error.
1213 g_file_read (GFile *file,
1214 GCancellable *cancellable,
1219 g_return_val_if_fail (G_IS_FILE (file), NULL);
1221 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1224 iface = G_FILE_GET_IFACE (file);
1226 if (iface->read_fn == NULL)
1228 g_set_error (error, G_IO_ERROR,
1229 G_IO_ERROR_NOT_SUPPORTED,
1230 _("Operation not supported"));
1234 return (* iface->read_fn) (file, cancellable, error);
1239 * @file: input #GFile.
1240 * @flags: a set of #GFileCreateFlags.
1241 * @cancellable: optional #GCancellable object, %NULL to ignore.
1242 * @error: a #GError, or %NULL
1244 * Gets an output stream for appending data to the file. If
1245 * the file doesn't already exist it is created.
1247 * By default files created are generally readable by everyone,
1248 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1249 * will be made readable only to the current user, to the level that
1250 * is supported on the target filesystem.
1252 * If @cancellable is not %NULL, then the operation can be cancelled by
1253 * triggering the cancellable object from another thread. If the operation
1254 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1256 * Some file systems don't allow all file names, and may
1257 * return an G_IO_ERROR_INVALID_FILENAME error.
1258 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1259 * returned. Other errors are possible too, and depend on what kind of
1260 * filesystem the file is on.
1262 * Returns: a #GFileOutputStream.
1265 g_file_append_to (GFile *file,
1266 GFileCreateFlags flags,
1267 GCancellable *cancellable,
1272 g_return_val_if_fail (G_IS_FILE (file), NULL);
1274 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1277 iface = G_FILE_GET_IFACE (file);
1279 if (iface->append_to == NULL)
1281 g_set_error (error, G_IO_ERROR,
1282 G_IO_ERROR_NOT_SUPPORTED,
1283 _("Operation not supported"));
1287 return (* iface->append_to) (file, flags, cancellable, error);
1292 * @file: input #GFile.
1293 * @flags: a set of #GFileCreateFlags.
1294 * @cancellable: optional #GCancellable object, %NULL to ignore.
1295 * @error: a #GError, or %NULL
1297 * Creates a new file and returns an output stream for writing to it.
1298 * The file must not already exists.
1300 * By default files created are generally readable by everyone,
1301 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1302 * will be made readable only to the current user, to the level that
1303 * is supported on the target filesystem.
1305 * If @cancellable is not %NULL, then the operation can be cancelled by
1306 * triggering the cancellable object from another thread. If the operation
1307 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1309 * If a file with this name already exists the G_IO_ERROR_EXISTS error
1310 * will be returned. If the file is a directory the G_IO_ERROR_IS_DIRECTORY
1311 * error will be returned.
1312 * Some file systems don't allow all file names, and may
1313 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1314 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1315 * Other errors are possible too, and depend on what kind of
1316 * filesystem the file is on.
1318 * Returns: a #GFileOutputStream for the newly created file, or
1322 g_file_create (GFile *file,
1323 GFileCreateFlags flags,
1324 GCancellable *cancellable,
1329 g_return_val_if_fail (G_IS_FILE (file), NULL);
1331 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1334 iface = G_FILE_GET_IFACE (file);
1336 if (iface->create == NULL)
1338 g_set_error (error, G_IO_ERROR,
1339 G_IO_ERROR_NOT_SUPPORTED,
1340 _("Operation not supported"));
1344 return (* iface->create) (file, flags, cancellable, error);
1349 * @file: input #GFile.
1350 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1351 * current #GFile, or #NULL to ignore.
1352 * @make_backup: %TRUE if a backup should be created.
1353 * @flags: a set of #GFileCreateFlags.
1354 * @cancellable: optional #GCancellable object, %NULL to ignore.
1355 * @error: a #GError, or %NULL
1357 * Returns an output stream for overwriting the file, possibly
1358 * creating a backup copy of the file first.
1360 * This will try to replace the file in the safest way possible so
1361 * that any errors during the writing will not affect an already
1362 * existing copy of the file. For instance, for local files it
1363 * may write to a temporary file and then atomically rename over
1364 * the destination when the stream is closed.
1366 * By default files created are generally readable by everyone,
1367 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1368 * will be made readable only to the current user, to the level that
1369 * is supported on the target filesystem.
1371 * If @cancellable is not %NULL, then the operation can be cancelled by
1372 * triggering the cancellable object from another thread. If the operation
1373 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1375 * If you pass in a non-#NULL @etag value, then this value is
1376 * compared to the current entity tag of the file, and if they differ
1377 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1378 * that the file has been changed since you last read it. You can get
1379 * the new etag from g_file_output_stream_get_etag() after you've
1380 * finished writing and closed the #GFileOutputStream. When you load
1381 * a new file you can use g_file_input_stream_query_info() to get
1382 * the etag of the file.
1384 * If @make_backup is %TRUE, this function will attempt to make a backup
1385 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1386 * error will be returned. If you want to replace anyway, try again with
1387 * @make_backup set to %FALSE.
1389 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1390 * and if the file is some other form of non-regular file then a
1391 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1392 * Some file systems don't allow all file names, and may
1393 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1394 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1395 * Other errors are possible too, and depend on what kind of
1396 * filesystem the file is on.
1398 * Returns: a #GFileOutputStream or %NULL on error.
1401 g_file_replace (GFile *file,
1403 gboolean make_backup,
1404 GFileCreateFlags flags,
1405 GCancellable *cancellable,
1410 g_return_val_if_fail (G_IS_FILE (file), NULL);
1412 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1415 iface = G_FILE_GET_IFACE (file);
1417 if (iface->replace == NULL)
1419 g_set_error (error, G_IO_ERROR,
1420 G_IO_ERROR_NOT_SUPPORTED,
1421 _("Operation not supported"));
1426 /* Handle empty tag string as NULL in consistent way. */
1427 if (etag && *etag == 0)
1430 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1434 * g_file_read_async:
1435 * @file: input #GFile.
1436 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1438 * @cancellable: optional #GCancellable object, %NULL to ignore.
1439 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1440 * @user_data: the data to pass to callback function
1442 * Asynchronously opens @file for reading.
1444 * For more details, see g_file_read() which is
1445 * the synchronous version of this call.
1447 * When the operation is finished, @callback will be called. You can then call
1448 * g_file_read_finish() to get the result of the operation.
1451 g_file_read_async (GFile *file,
1453 GCancellable *cancellable,
1454 GAsyncReadyCallback callback,
1459 g_return_if_fail (G_IS_FILE (file));
1461 iface = G_FILE_GET_IFACE (file);
1462 (* iface->read_async) (file,
1470 * g_file_read_finish:
1471 * @file: input #GFile.
1472 * @res: a #GAsyncResult.
1473 * @error: a #GError, or %NULL
1475 * Finishes an asynchronous file read operation started with
1476 * g_file_read_async().
1478 * Returns: a #GFileInputStream or %NULL on error.
1481 g_file_read_finish (GFile *file,
1487 g_return_val_if_fail (G_IS_FILE (file), NULL);
1488 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1490 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1492 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1493 if (g_simple_async_result_propagate_error (simple, error))
1497 iface = G_FILE_GET_IFACE (file);
1498 return (* iface->read_finish) (file, res, error);
1502 * g_file_append_to_async:
1503 * @file: input #GFile.
1504 * @flags: a set of #GFileCreateFlags.
1505 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1507 * @cancellable: optional #GCancellable object, %NULL to ignore.
1508 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1509 * @user_data: the data to pass to callback function
1511 * Asynchronously opens @file for appending.
1513 * For more details, see g_file_append_to() which is
1514 * the synchronous version of this call.
1516 * When the operation is finished, @callback will be called. You can then call
1517 * g_file_append_to_finish() to get the result of the operation.
1520 g_file_append_to_async (GFile *file,
1521 GFileCreateFlags flags,
1523 GCancellable *cancellable,
1524 GAsyncReadyCallback callback,
1529 g_return_if_fail (G_IS_FILE (file));
1531 iface = G_FILE_GET_IFACE (file);
1532 (* iface->append_to_async) (file,
1541 * g_file_append_to_finish:
1542 * @file: input #GFile.
1543 * @res: #GAsyncResult
1544 * @error: a #GError, or %NULL
1546 * Finishes an asynchronous file append operation started with
1547 * g_file_append_to_async().
1549 * Returns: a valid #GFileOutputStream or %NULL on error.
1552 g_file_append_to_finish (GFile *file,
1558 g_return_val_if_fail (G_IS_FILE (file), NULL);
1559 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1561 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1563 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1564 if (g_simple_async_result_propagate_error (simple, error))
1568 iface = G_FILE_GET_IFACE (file);
1569 return (* iface->append_to_finish) (file, res, error);
1573 * g_file_create_async:
1574 * @file: input #GFile.
1575 * @flags: a set of #GFileCreateFlags.
1576 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1578 * @cancellable: optional #GCancellable object, %NULL to ignore.
1579 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1580 * @user_data: the data to pass to callback function
1582 * Asynchronously creates a new file and returns an output stream for writing to it.
1583 * The file must not already exists.
1585 * For more details, see g_file_create() which is
1586 * the synchronous version of this call.
1588 * When the operation is finished, @callback will be called. You can then call
1589 * g_file_create_finish() to get the result of the operation.
1592 g_file_create_async (GFile *file,
1593 GFileCreateFlags flags,
1595 GCancellable *cancellable,
1596 GAsyncReadyCallback callback,
1601 g_return_if_fail (G_IS_FILE (file));
1603 iface = G_FILE_GET_IFACE (file);
1604 (* iface->create_async) (file,
1613 * g_file_create_finish:
1614 * @file: input #GFile.
1615 * @res: a #GAsyncResult.
1616 * @error: a #GError, or %NULL
1618 * Finishes an asynchronous file create operation started with
1619 * g_file_create_async().
1621 * Returns: a #GFileOutputStream or %NULL on error.
1624 g_file_create_finish (GFile *file,
1630 g_return_val_if_fail (G_IS_FILE (file), NULL);
1631 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1633 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1635 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1636 if (g_simple_async_result_propagate_error (simple, error))
1640 iface = G_FILE_GET_IFACE (file);
1641 return (* iface->create_finish) (file, res, error);
1645 * g_file_replace_async:
1646 * @file: input #GFile.
1647 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1648 * current #GFile, or NULL to ignore.
1649 * @make_backup: %TRUE if a backup should be created.
1650 * @flags: a set of #GFileCreateFlags.
1651 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1653 * @cancellable: optional #GCancellable object, %NULL to ignore.
1654 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1655 * @user_data: the data to pass to callback function
1657 * Asynchronously overwrites the file, replacing the contents, possibly
1658 * creating a backup copy of the file first.
1660 * For more details, see g_file_replace() which is
1661 * the synchronous version of this call.
1663 * When the operation is finished, @callback will be called. You can then call
1664 * g_file_replace_finish() to get the result of the operation.
1667 g_file_replace_async (GFile *file,
1669 gboolean make_backup,
1670 GFileCreateFlags flags,
1672 GCancellable *cancellable,
1673 GAsyncReadyCallback callback,
1678 g_return_if_fail (G_IS_FILE (file));
1680 iface = G_FILE_GET_IFACE (file);
1681 (* iface->replace_async) (file,
1692 * g_file_replace_finish:
1693 * @file: input #GFile.
1694 * @res: a #GAsyncResult.
1695 * @error: a #GError, or %NULL
1697 * Finishes an asynchronous file replace operation started with
1698 * g_file_replace_async().
1700 * Returns: a #GFileOutputStream, or %NULL on error.
1703 g_file_replace_finish (GFile *file,
1709 g_return_val_if_fail (G_IS_FILE (file), NULL);
1710 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1712 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1714 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1715 if (g_simple_async_result_propagate_error (simple, error))
1719 iface = G_FILE_GET_IFACE (file);
1720 return (* iface->replace_finish) (file, res, error);
1724 copy_symlink (GFile *destination,
1725 GFileCopyFlags flags,
1726 GCancellable *cancellable,
1731 gboolean tried_delete;
1733 GFileType file_type;
1735 tried_delete = FALSE;
1739 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1741 /* Maybe it already existed, and we want to overwrite? */
1742 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1743 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1745 g_error_free (my_error);
1748 /* Don't overwrite if the destination is a directory */
1749 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1750 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1751 cancellable, &my_error);
1754 file_type = g_file_info_get_file_type (info);
1755 g_object_unref (info);
1757 if (file_type == G_FILE_TYPE_DIRECTORY)
1759 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1760 _("Can't copy over directory"));
1765 if (!g_file_delete (destination, cancellable, error))
1768 tried_delete = TRUE;
1772 g_propagate_error (error, my_error);
1779 static GInputStream *
1780 open_source_for_copy (GFile *source,
1782 GFileCopyFlags flags,
1783 GCancellable *cancellable,
1789 GFileType file_type;
1792 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1796 /* There was an error opening the source, try to set a good error for it: */
1798 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1800 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1801 * as that is less useful to the app. Better check for errors on the
1804 g_error_free (my_error);
1807 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1808 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1809 cancellable, &my_error);
1812 file_type = g_file_info_get_file_type (info);
1813 g_object_unref (info);
1815 if (flags & G_FILE_COPY_OVERWRITE)
1817 if (file_type == G_FILE_TYPE_DIRECTORY)
1819 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1820 _("Can't copy directory over directory"));
1823 /* continue to would_recurse error */
1827 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1828 _("Target file exists"));
1834 /* Error getting info from target, return that error
1835 * (except for NOT_FOUND, which is no error here)
1837 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1839 g_propagate_error (error, my_error);
1842 g_error_free (my_error);
1845 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1846 _("Can't recursively copy directory"));
1850 g_propagate_error (error, my_error);
1855 should_copy (GFileAttributeInfo *info,
1859 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1860 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1864 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1865 GFileAttributeInfoList *namespaces,
1873 s = g_string_new ("");
1877 for (i = 0; i < attributes->n_infos; i++)
1879 if (should_copy (&attributes->infos[i], as_move))
1884 g_string_append_c (s, ',');
1886 g_string_append (s, attributes->infos[i].name);
1893 for (i = 0; i < namespaces->n_infos; i++)
1895 if (should_copy (&namespaces->infos[i], as_move))
1900 g_string_append_c (s, ',');
1902 g_string_append (s, namespaces->infos[i].name);
1903 g_string_append (s, ":*");
1908 return g_string_free (s, FALSE);
1912 * g_file_copy_attributes:
1913 * @source: a #GFile with attributes.
1914 * @destination: a #GFile to copy attributes to.
1915 * @flags: a set of #GFileCopyFlags.
1916 * @cancellable: optional #GCancellable object, %NULL to ignore.
1917 * @error: a #GError, %NULL to ignore.
1919 * Copies the file attributes from @source to @destination.
1921 * Normally only a subset of the file attributes are copied,
1922 * those that are copies in a normal file copy operation
1923 * (which for instance does not include e.g. mtime). However
1924 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
1925 * all the metadata that is possible to copy is copied.
1927 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1930 g_file_copy_attributes (GFile *source,
1932 GFileCopyFlags flags,
1933 GCancellable *cancellable,
1936 GFileAttributeInfoList *attributes, *namespaces;
1937 char *attrs_to_read;
1941 gboolean source_nofollow_symlinks;
1943 as_move = flags & G_FILE_COPY_ALL_METADATA;
1944 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1946 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1947 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1948 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1950 if (attributes == NULL && namespaces == NULL)
1953 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1955 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1956 * we just don't copy it.
1958 info = g_file_query_info (source, attrs_to_read,
1959 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1963 g_free (attrs_to_read);
1968 res = g_file_set_attributes_from_info (destination,
1972 g_object_unref (info);
1975 g_file_attribute_info_list_unref (attributes);
1976 g_file_attribute_info_list_unref (namespaces);
1981 /* Closes the streams */
1983 copy_stream_with_progress (GInputStream *in,
1985 GCancellable *cancellable,
1986 GFileProgressCallback progress_callback,
1987 gpointer progress_callback_data,
1990 gssize n_read, n_written;
1991 goffset current_size;
1992 char buffer[8192], *p;
1998 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1999 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2003 total_size = g_file_info_get_size (info);
2004 g_object_unref (info);
2011 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2021 current_size += n_read;
2026 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2027 if (n_written == -1)
2034 n_read -= n_written;
2040 if (progress_callback)
2041 progress_callback (current_size, total_size, progress_callback_data);
2045 error = NULL; /* Ignore further errors */
2047 /* Make sure we send full copied size */
2048 if (progress_callback)
2049 progress_callback (current_size, total_size, progress_callback_data);
2052 /* Don't care about errors in source here */
2053 g_input_stream_close (in, cancellable, NULL);
2055 /* But write errors on close are bad! */
2056 if (!g_output_stream_close (out, cancellable, error))
2059 g_object_unref (in);
2060 g_object_unref (out);
2066 file_copy_fallback (GFile *source,
2068 GFileCopyFlags flags,
2069 GCancellable *cancellable,
2070 GFileProgressCallback progress_callback,
2071 gpointer progress_callback_data,
2079 /* Maybe copy the symlink? */
2080 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2082 info = g_file_query_info (source,
2083 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2084 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2090 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2091 (target = g_file_info_get_symlink_target (info)) != NULL)
2093 if (!copy_symlink (destination, flags, cancellable, target, error))
2095 g_object_unref (info);
2099 g_object_unref (info);
2103 g_object_unref (info);
2106 in = open_source_for_copy (source, destination, flags, cancellable, error);
2110 if (flags & G_FILE_COPY_OVERWRITE)
2112 out = (GOutputStream *)g_file_replace (destination,
2114 flags & G_FILE_COPY_BACKUP,
2115 cancellable, error);
2119 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2124 g_object_unref (in);
2128 if (!copy_stream_with_progress (in, out, cancellable,
2129 progress_callback, progress_callback_data,
2135 /* Ignore errors here. Failure to copy metadata is not a hard error */
2136 g_file_copy_attributes (source, destination,
2137 flags, cancellable, NULL);
2144 * @source: input #GFile.
2145 * @destination: destination #GFile
2146 * @flags: set of #GFileCopyFlags
2147 * @cancellable: optional #GCancellable object, %NULL to ignore.
2148 * @progress_callback: function to callback with progress information
2149 * @progress_callback_data: user data to pass to @progress_callback
2150 * @error: #GError to set on error, or %NULL
2152 * Copies the file @source to the location specified by @destination.
2153 * Can not handle recursive copies of directories.
2155 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2156 * existing @destination file is overwritten.
2158 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2159 * will be copied as symlinks, otherwise the target of the
2160 * @source symlink will be copied.
2162 * If @cancellable is not %NULL, then the operation can be cancelled by
2163 * triggering the cancellable object from another thread. If the operation
2164 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2166 * If @progress_callback is not %NULL, then the operation can be monitored by
2167 * setting this to a #GFileProgressCallback function. @progress_callback_data
2168 * will be passed to this function. It is guaranteed that this callback will
2169 * be called after all data has been transferred with the total number of bytes
2170 * copied during the operation.
2172 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2173 * error is returned, independent on the status of the @destination.
2175 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2176 * error G_IO_ERROR_EXISTS is returned.
2178 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2179 * error is returned. If trying to overwrite a directory with a directory the
2180 * G_IO_ERROR_WOULD_MERGE error is returned.
2182 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2183 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2186 * If you are interested in copying the #GFile object itself (not the on-disk
2187 * file), see g_file_dup().
2189 * Returns: %TRUE on success, %FALSE otherwise.
2192 g_file_copy (GFile *source,
2194 GFileCopyFlags flags,
2195 GCancellable *cancellable,
2196 GFileProgressCallback progress_callback,
2197 gpointer progress_callback_data,
2204 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2205 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2207 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2210 iface = G_FILE_GET_IFACE (destination);
2214 res = (* iface->copy) (source, destination,
2216 progress_callback, progress_callback_data,
2222 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2224 g_propagate_error (error, my_error);
2229 /* If the types are different, and the destination method failed
2230 also try the source method */
2231 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2233 iface = G_FILE_GET_IFACE (source);
2238 res = (* iface->copy) (source, destination,
2240 progress_callback, progress_callback_data,
2246 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2248 g_propagate_error (error, my_error);
2254 return file_copy_fallback (source, destination, flags, cancellable,
2255 progress_callback, progress_callback_data,
2260 * g_file_copy_async:
2261 * @source: input #GFile.
2262 * @destination: destination #GFile
2263 * @flags: set of #GFileCopyFlags
2264 * @cancellable: optional #GCancellable object, %NULL to ignore.
2265 * @progress_callback: function to callback with progress information
2266 * @progress_callback_data: user data to pass to @progress_callback
2267 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2268 * @user_data: the data to pass to callback function
2270 * Copies the file @source to the location specified by @destination asynchronously.
2271 * For details of the behaviour, see g_file_copy().
2273 * If @progress_callback is not %NULL, then that function that will be called
2274 * just like in g_file_copy(), however the callback will run in the main loop,
2275 * not in the thread that is doing the I/O operation.
2277 * When the operation is finished, @callback will be called. You can then call
2278 * g_file_copy_finish() to get the result of the operation.
2281 g_file_copy_async (GFile *source,
2283 GFileCopyFlags flags,
2285 GCancellable *cancellable,
2286 GFileProgressCallback progress_callback,
2287 gpointer progress_callback_data,
2288 GAsyncReadyCallback callback,
2293 g_return_if_fail (G_IS_FILE (source));
2294 g_return_if_fail (G_IS_FILE (destination));
2296 iface = G_FILE_GET_IFACE (source);
2297 (* iface->copy_async) (source,
2303 progress_callback_data,
2309 * g_file_copy_finish:
2310 * @file: input #GFile.
2311 * @res: a #GAsyncResult.
2312 * @error: a #GError, or %NULL
2314 * Finishes copying the file started with
2315 * g_file_copy_async().
2317 * Returns: a %TRUE on success, %FALSE on error.
2320 g_file_copy_finish (GFile *file,
2326 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2327 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2329 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2331 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2333 if (g_simple_async_result_propagate_error (simple, error))
2337 iface = G_FILE_GET_IFACE (file);
2338 return (* iface->copy_finish) (file, res, error);
2343 * @source: #GFile pointing to the source location.
2344 * @destination: #GFile pointing to the destination location.
2345 * @flags: set of #GFileCopyFlags.
2346 * @cancellable: optional #GCancellable object, %NULL to ignore.
2347 * @progress_callback: #GFileProgressCallback function for updates.
2348 * @progress_callback_data: gpointer to user data for the callback function.
2349 * @error: #GError for returning error conditions, or %NULL
2352 * Tries to move the file or directory @source to the location specified by @destination.
2353 * If native move operations are supported then this is used, otherwise a copy + delete
2354 * fallback is used. The native implementation may support moving directories (for instance
2355 * on moves inside the same filesystem), but the fallback code does not.
2357 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2358 * existing @destination file is overwritten.
2360 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2361 * will be copied as symlinks, otherwise the target of the
2362 * @source symlink will be copied.
2364 * If @cancellable is not %NULL, then the operation can be cancelled by
2365 * triggering the cancellable object from another thread. If the operation
2366 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2368 * If @progress_callback is not %NULL, then the operation can be monitored by
2369 * setting this to a #GFileProgressCallback function. @progress_callback_data
2370 * will be passed to this function. It is guaranteed that this callback will
2371 * be called after all data has been transferred with the total number of bytes
2372 * copied during the operation.
2374 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2375 * error is returned, independent on the status of the @destination.
2377 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2378 * error G_IO_ERROR_EXISTS is returned.
2380 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2381 * error is returned. If trying to overwrite a directory with a directory the
2382 * G_IO_ERROR_WOULD_MERGE error is returned.
2384 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2385 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2386 * may be returned (if the native move operation isn't available).
2388 * Returns: %TRUE on successful move, %FALSE otherwise.
2391 g_file_move (GFile *source,
2393 GFileCopyFlags flags,
2394 GCancellable *cancellable,
2395 GFileProgressCallback progress_callback,
2396 gpointer progress_callback_data,
2403 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2404 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2406 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2409 iface = G_FILE_GET_IFACE (destination);
2413 res = (* iface->move) (source, destination,
2415 progress_callback, progress_callback_data,
2421 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2423 g_propagate_error (error, my_error);
2428 /* If the types are different, and the destination method failed
2429 also try the source method */
2430 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2432 iface = G_FILE_GET_IFACE (source);
2437 res = (* iface->move) (source, destination,
2439 progress_callback, progress_callback_data,
2445 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2447 g_propagate_error (error, my_error);
2453 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2455 g_set_error (error, G_IO_ERROR,
2456 G_IO_ERROR_NOT_SUPPORTED,
2457 _("Operation not supported"));
2461 flags |= G_FILE_COPY_ALL_METADATA;
2462 if (!g_file_copy (source, destination, flags, cancellable,
2463 progress_callback, progress_callback_data,
2467 return g_file_delete (source, cancellable, error);
2471 * g_file_make_directory
2472 * @file: input #GFile.
2473 * @cancellable: optional #GCancellable object, %NULL to ignore.
2474 * @error: a #GError, or %NULL
2476 * Creates a directory.
2478 * If @cancellable is not %NULL, then the operation can be cancelled by
2479 * triggering the cancellable object from another thread. If the operation
2480 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2482 * Returns: %TRUE on successful creation, %FALSE otherwise.
2485 g_file_make_directory (GFile *file,
2486 GCancellable *cancellable,
2491 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2493 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2496 iface = G_FILE_GET_IFACE (file);
2498 if (iface->make_directory == NULL)
2500 g_set_error (error, G_IO_ERROR,
2501 G_IO_ERROR_NOT_SUPPORTED,
2502 _("Operation not supported"));
2506 return (* iface->make_directory) (file, cancellable, error);
2510 * g_file_make_symbolic_link:
2511 * @file: input #GFile.
2512 * @symlink_value: a string with the value of the new symlink.
2513 * @cancellable: optional #GCancellable object, %NULL to ignore.
2514 * @error: a #GError.
2516 * Creates a symbolic link.
2518 * If @cancellable is not %NULL, then the operation can be cancelled by
2519 * triggering the cancellable object from another thread. If the operation
2520 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2522 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2525 g_file_make_symbolic_link (GFile *file,
2526 const char *symlink_value,
2527 GCancellable *cancellable,
2532 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2533 g_return_val_if_fail (symlink_value != NULL, FALSE);
2535 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2538 if (*symlink_value == '\0')
2540 g_set_error (error, G_IO_ERROR,
2541 G_IO_ERROR_INVALID_ARGUMENT,
2542 _("Invalid symlink value given"));
2546 iface = G_FILE_GET_IFACE (file);
2548 if (iface->make_symbolic_link == NULL)
2550 g_set_error (error, G_IO_ERROR,
2551 G_IO_ERROR_NOT_SUPPORTED,
2552 _("Operation not supported"));
2556 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2561 * @file: input #GFile.
2562 * @cancellable: optional #GCancellable object, %NULL to ignore.
2563 * @error: a #GError, or %NULL
2567 * If @cancellable is not %NULL, then the operation can be cancelled by
2568 * triggering the cancellable object from another thread. If the operation
2569 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2571 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2574 g_file_delete (GFile *file,
2575 GCancellable *cancellable,
2580 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2582 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2585 iface = G_FILE_GET_IFACE (file);
2587 if (iface->delete_file == NULL)
2589 g_set_error (error, G_IO_ERROR,
2590 G_IO_ERROR_NOT_SUPPORTED,
2591 _("Operation not supported"));
2595 return (* iface->delete_file) (file, cancellable, error);
2600 * @file: #GFile to send to trash.
2601 * @cancellable: optional #GCancellable object, %NULL to ignore.
2602 * @error: a #GError, or %NULL
2604 * Sends @file to the "Trashcan", if possible. This is similar to
2605 * deleting it, but the user can recover it before emptying the trashcan.
2606 * Not all file systems support trashing, so this call can return the
2607 * %G_IO_ERROR_NOT_SUPPORTED error.
2610 * If @cancellable is not %NULL, then the operation can be cancelled by
2611 * triggering the cancellable object from another thread. If the operation
2612 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2614 * Returns: %TRUE on successful trash, %FALSE otherwise.
2617 g_file_trash (GFile *file,
2618 GCancellable *cancellable,
2623 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2625 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2628 iface = G_FILE_GET_IFACE (file);
2630 if (iface->trash == NULL)
2633 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2634 _("Trash not supported"));
2638 return (* iface->trash) (file, cancellable, error);
2642 * g_file_set_display_name:
2643 * @file: input #GFile.
2644 * @display_name: a string.
2645 * @cancellable: optional #GCancellable object, %NULL to ignore.
2646 * @error: a #GError, or %NULL
2648 * Renames @file to the specified display name.
2650 * The display name is converted from UTF8 to the correct encoding for the target
2651 * filesystem if possible and the @file is renamed to this.
2653 * If you want to implement a rename operation in the user interface the edit name
2654 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2655 * widget, and then the result after editing should be passed to g_file_set_display_name().
2657 * On success the resulting converted filename is returned.
2659 * If @cancellable is not %NULL, then the operation can be cancelled by
2660 * triggering the cancellable object from another thread. If the operation
2661 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2663 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2666 g_file_set_display_name (GFile *file,
2667 const char *display_name,
2668 GCancellable *cancellable,
2673 g_return_val_if_fail (G_IS_FILE (file), NULL);
2674 g_return_val_if_fail (display_name != NULL, NULL);
2676 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2680 G_IO_ERROR_INVALID_ARGUMENT,
2681 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2685 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2688 iface = G_FILE_GET_IFACE (file);
2690 return (* iface->set_display_name) (file, display_name, cancellable, error);
2694 * g_file_set_display_name_async:
2695 * @file: input #GFile.
2696 * @display_name: a string.
2697 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2699 * @cancellable: optional #GCancellable object, %NULL to ignore.
2700 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2701 * @user_data: the data to pass to callback function
2703 * Asynchronously sets the display name for a given #GFile.
2705 * For more details, see g_set_display_name() which is
2706 * the synchronous version of this call.
2708 * When the operation is finished, @callback will be called. You can then call
2709 * g_file_set_display_name_finish() to get the result of the operation.
2712 g_file_set_display_name_async (GFile *file,
2713 const char *display_name,
2715 GCancellable *cancellable,
2716 GAsyncReadyCallback callback,
2721 g_return_if_fail (G_IS_FILE (file));
2722 g_return_if_fail (display_name != NULL);
2724 iface = G_FILE_GET_IFACE (file);
2725 (* iface->set_display_name_async) (file,
2734 * g_file_set_display_name_finish:
2735 * @file: input #GFile.
2736 * @res: a #GAsyncResult.
2737 * @error: a #GError, or %NULL
2739 * Finishes setting a display name started with
2740 * g_file_set_display_name_async().
2742 * Returns: a #GFile or %NULL on error.
2745 g_file_set_display_name_finish (GFile *file,
2751 g_return_val_if_fail (G_IS_FILE (file), NULL);
2752 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2754 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2756 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2757 if (g_simple_async_result_propagate_error (simple, error))
2761 iface = G_FILE_GET_IFACE (file);
2762 return (* iface->set_display_name_finish) (file, res, error);
2766 * g_file_query_settable_attributes:
2767 * @file: input #GFile.
2768 * @cancellable: optional #GCancellable object, %NULL to ignore.
2769 * @error: a #GError, or %NULL
2771 * Obtain the list of settable attributes for the file.
2773 * Returns the type and full attribute name of all the attributes
2774 * that can be set on this file. This doesn't mean setting it will always
2775 * succeed though, you might get an access failure, or some specific
2776 * file may not support a specific attribute.
2778 * If @cancellable is not %NULL, then the operation can be cancelled by
2779 * triggering the cancellable object from another thread. If the operation
2780 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2782 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2783 * When you are done with it, release it with g_file_attribute_info_list_unref()
2785 GFileAttributeInfoList *
2786 g_file_query_settable_attributes (GFile *file,
2787 GCancellable *cancellable,
2792 GFileAttributeInfoList *list;
2794 g_return_val_if_fail (G_IS_FILE (file), NULL);
2796 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2799 iface = G_FILE_GET_IFACE (file);
2801 if (iface->query_settable_attributes == NULL)
2802 return g_file_attribute_info_list_new ();
2805 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2809 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2811 list = g_file_attribute_info_list_new ();
2812 g_error_free (my_error);
2815 g_propagate_error (error, my_error);
2822 * g_file_query_writable_namespaces:
2823 * @file: input #GFile.
2824 * @cancellable: optional #GCancellable object, %NULL to ignore.
2825 * @error: a #GError, or %NULL
2827 * Obtain the list of attribute namespaces where new attributes
2828 * can be created by a user. An example of this is extended
2829 * attributes (in the "xattr" namespace).
2831 * If @cancellable is not %NULL, then the operation can be cancelled by
2832 * triggering the cancellable object from another thread. If the operation
2833 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2835 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2836 * When you are done with it, release it with g_file_attribute_info_list_unref()
2838 GFileAttributeInfoList *
2839 g_file_query_writable_namespaces (GFile *file,
2840 GCancellable *cancellable,
2845 GFileAttributeInfoList *list;
2847 g_return_val_if_fail (G_IS_FILE (file), NULL);
2849 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2852 iface = G_FILE_GET_IFACE (file);
2854 if (iface->query_writable_namespaces == NULL)
2855 return g_file_attribute_info_list_new ();
2858 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2862 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2864 list = g_file_attribute_info_list_new ();
2865 g_error_free (my_error);
2868 g_propagate_error (error, my_error);
2875 * g_file_set_attribute:
2876 * @file: input #GFile.
2877 * @attribute: a string containing the attribute's name.
2878 * @type: The type of the attribute
2879 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2880 * @flags: a set of #GFileQueryInfoFlags.
2881 * @cancellable: optional #GCancellable object, %NULL to ignore.
2882 * @error: a #GError, or %NULL
2884 * Sets an attribute in the file with attribute name @attribute to @value.
2886 * If @cancellable is not %NULL, then the operation can be cancelled by
2887 * triggering the cancellable object from another thread. If the operation
2888 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2890 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2893 g_file_set_attribute (GFile *file,
2894 const char *attribute,
2895 GFileAttributeType type,
2897 GFileQueryInfoFlags flags,
2898 GCancellable *cancellable,
2903 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2904 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2906 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2909 iface = G_FILE_GET_IFACE (file);
2911 if (iface->set_attribute == NULL)
2913 g_set_error (error, G_IO_ERROR,
2914 G_IO_ERROR_NOT_SUPPORTED,
2915 _("Operation not supported"));
2919 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2923 * g_file_set_attributes_from_info:
2924 * @file: input #GFile.
2925 * @info: a #GFileInfo.
2926 * @flags: #GFileQueryInfoFlags
2927 * @cancellable: optional #GCancellable object, %NULL to ignore.
2928 * @error: a #GError, or %NULL
2930 * Tries to set all attributes in the #GFileInfo on the target values,
2931 * not stopping on the first error.
2933 * If there is any error during this operation then @error will be set to
2934 * the first error. Error on particular fields are flagged by setting
2935 * the "status" field in the attribute value to
2936 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2939 * If @cancellable is not %NULL, then the operation can be cancelled by
2940 * triggering the cancellable object from another thread. If the operation
2941 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2943 * Returns: %TRUE if there was any error, %FALSE otherwise.
2946 g_file_set_attributes_from_info (GFile *file,
2948 GFileQueryInfoFlags flags,
2949 GCancellable *cancellable,
2954 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2955 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2957 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2960 g_file_info_clear_status (info);
2962 iface = G_FILE_GET_IFACE (file);
2964 return (* iface->set_attributes_from_info) (file,
2973 g_file_real_set_attributes_from_info (GFile *file,
2975 GFileQueryInfoFlags flags,
2976 GCancellable *cancellable,
2982 GFileAttributeValue *value;
2986 attributes = g_file_info_list_attributes (info, NULL);
2988 for (i = 0; attributes[i] != NULL; i++)
2990 value = _g_file_info_get_attribute_value (info, attributes[i]);
2992 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2995 if (!g_file_set_attribute (file, attributes[i],
2996 value->type, _g_file_attribute_value_peek_as_pointer (value),
2997 flags, cancellable, error))
2999 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3001 /* Don't set error multiple times */
3005 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3008 g_strfreev (attributes);
3014 * g_file_set_attributes_async:
3015 * @file: input #GFile.
3016 * @info: a #GFileInfo.
3017 * @flags: a #GFileQueryInfoFlags.
3018 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3020 * @cancellable: optional #GCancellable object, %NULL to ignore.
3021 * @callback: a #GAsyncReadyCallback.
3022 * @user_data: a #gpointer.
3024 * Asynchronously sets the attributes of @file with @info.
3026 * For more details, see g_file_set_attributes_from_info() which is
3027 * the synchronous version of this call.
3029 * When the operation is finished, @callback will be called. You can then call
3030 * g_file_set_attributes_finish() to get the result of the operation.
3033 g_file_set_attributes_async (GFile *file,
3035 GFileQueryInfoFlags flags,
3037 GCancellable *cancellable,
3038 GAsyncReadyCallback callback,
3043 g_return_if_fail (G_IS_FILE (file));
3044 g_return_if_fail (G_IS_FILE_INFO (info));
3046 iface = G_FILE_GET_IFACE (file);
3047 (* iface->set_attributes_async) (file,
3057 * g_file_set_attributes_finish:
3058 * @file: input #GFile.
3059 * @result: a #GAsyncResult.
3060 * @info: a #GFileInfo.
3061 * @error: a #GError, or %NULL
3063 * Finishes setting an attribute started in g_file_set_attributes_async().
3065 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3068 g_file_set_attributes_finish (GFile *file,
3069 GAsyncResult *result,
3075 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3076 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3078 /* No standard handling of errors here, as we must set info even
3081 iface = G_FILE_GET_IFACE (file);
3082 return (* iface->set_attributes_finish) (file, result, info, error);
3086 * g_file_set_attribute_string:
3087 * @file: input #GFile.
3088 * @attribute: a string containing the attribute's name.
3089 * @value: a string containing the attribute's value.
3090 * @flags: #GFileQueryInfoFlags.
3091 * @cancellable: optional #GCancellable object, %NULL to ignore.
3092 * @error: a #GError, or %NULL
3094 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3095 * If @attribute is of a different type, this operation will fail.
3097 * If @cancellable is not %NULL, then the operation can be cancelled by
3098 * triggering the cancellable object from another thread. If the operation
3099 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3101 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3104 g_file_set_attribute_string (GFile *file,
3105 const char *attribute,
3107 GFileQueryInfoFlags flags,
3108 GCancellable *cancellable,
3111 return g_file_set_attribute (file, attribute,
3112 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3113 flags, cancellable, error);
3117 * g_file_set_attribute_byte_string:
3118 * @file: input #GFile.
3119 * @attribute: a string containing the attribute's name.
3120 * @value: a string containing the attribute's new value.
3121 * @flags: a #GFileQueryInfoFlags.
3122 * @cancellable: optional #GCancellable object, %NULL to ignore.
3123 * @error: a #GError, or %NULL
3125 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3126 * If @attribute is of a different type, this operation will fail,
3129 * If @cancellable is not %NULL, then the operation can be cancelled by
3130 * triggering the cancellable object from another thread. If the operation
3131 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3133 * Returns: %TRUE if the @attribute was successfully set to @value
3134 * in the @file, %FALSE otherwise.
3137 g_file_set_attribute_byte_string (GFile *file,
3138 const char *attribute,
3140 GFileQueryInfoFlags flags,
3141 GCancellable *cancellable,
3144 return g_file_set_attribute (file, attribute,
3145 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3146 flags, cancellable, error);
3150 * g_file_set_attribute_uint32:
3151 * @file: input #GFile.
3152 * @attribute: a string containing the attribute's name.
3153 * @value: a #guint32 containing the attribute's new value.
3154 * @flags: a #GFileQueryInfoFlags.
3155 * @cancellable: optional #GCancellable object, %NULL to ignore.
3156 * @error: a #GError, or %NULL
3158 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3159 * If @attribute is of a different type, this operation will fail.
3161 * If @cancellable is not %NULL, then the operation can be cancelled by
3162 * triggering the cancellable object from another thread. If the operation
3163 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3165 * Returns: %TRUE if the @attribute was successfully set to @value
3166 * in the @file, %FALSE otherwise.
3169 g_file_set_attribute_uint32 (GFile *file,
3170 const char *attribute,
3172 GFileQueryInfoFlags flags,
3173 GCancellable *cancellable,
3176 return g_file_set_attribute (file, attribute,
3177 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3178 flags, cancellable, error);
3182 * g_file_set_attribute_int32:
3183 * @file: input #GFile.
3184 * @attribute: a string containing the attribute's name.
3185 * @value: a #gint32 containing the attribute's new value.
3186 * @flags: a #GFileQueryInfoFlags.
3187 * @cancellable: optional #GCancellable object, %NULL to ignore.
3188 * @error: a #GError, or %NULL
3190 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3191 * If @attribute is of a different type, this operation will fail.
3193 * If @cancellable is not %NULL, then the operation can be cancelled by
3194 * triggering the cancellable object from another thread. If the operation
3195 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3197 * Returns: %TRUE if the @attribute was successfully set to @value
3198 * in the @file, %FALSE otherwise.
3201 g_file_set_attribute_int32 (GFile *file,
3202 const char *attribute,
3204 GFileQueryInfoFlags flags,
3205 GCancellable *cancellable,
3208 return g_file_set_attribute (file, attribute,
3209 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3210 flags, cancellable, error);
3214 * g_file_set_attribute_uint64:
3215 * @file: input #GFile.
3216 * @attribute: a string containing the attribute's name.
3217 * @value: a #guint64 containing the attribute's new value.
3218 * @flags: a #GFileQueryInfoFlags.
3219 * @cancellable: optional #GCancellable object, %NULL to ignore.
3220 * @error: a #GError, or %NULL
3222 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3223 * If @attribute is of a different type, this operation will fail.
3225 * If @cancellable is not %NULL, then the operation can be cancelled by
3226 * triggering the cancellable object from another thread. If the operation
3227 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3229 * Returns: %TRUE if the @attribute was successfully set to @value
3230 * in the @file, %FALSE otherwise.
3233 g_file_set_attribute_uint64 (GFile *file,
3234 const char *attribute,
3236 GFileQueryInfoFlags flags,
3237 GCancellable *cancellable,
3240 return g_file_set_attribute (file, attribute,
3241 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3242 flags, cancellable, error);
3246 * g_file_set_attribute_int64:
3247 * @file: input #GFile.
3248 * @attribute: a string containing the attribute's name.
3249 * @value: a #guint64 containing the attribute's new value.
3250 * @flags: a #GFileQueryInfoFlags.
3251 * @cancellable: optional #GCancellable object, %NULL to ignore.
3252 * @error: a #GError, or %NULL
3254 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3255 * If @attribute is of a different type, this operation will fail.
3257 * If @cancellable is not %NULL, then the operation can be cancelled by
3258 * triggering the cancellable object from another thread. If the operation
3259 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3261 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3264 g_file_set_attribute_int64 (GFile *file,
3265 const char *attribute,
3267 GFileQueryInfoFlags flags,
3268 GCancellable *cancellable,
3271 return g_file_set_attribute (file, attribute,
3272 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3273 flags, cancellable, error);
3277 * g_file_mount_mountable:
3278 * @file: input #GFile.
3279 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3280 * @cancellable: optional #GCancellable object, %NULL to ignore.
3281 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3282 * @user_data: the data to pass to callback function
3284 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3285 * Using @mount_operation, you can request callbacks when, for instance,
3286 * passwords are needed during authentication.
3288 * If @cancellable is not %NULL, then the operation can be cancelled by
3289 * triggering the cancellable object from another thread. If the operation
3290 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3292 * When the operation is finished, @callback will be called. You can then call
3293 * g_file_mount_mountable_finish() to get the result of the operation.
3296 g_file_mount_mountable (GFile *file,
3297 GMountOperation *mount_operation,
3298 GCancellable *cancellable,
3299 GAsyncReadyCallback callback,
3304 g_return_if_fail (G_IS_FILE (file));
3306 iface = G_FILE_GET_IFACE (file);
3308 if (iface->mount_mountable == NULL)
3309 g_simple_async_report_error_in_idle (G_OBJECT (file),
3313 G_IO_ERROR_NOT_SUPPORTED,
3314 _("Operation not supported"));
3316 (* iface->mount_mountable) (file,
3324 * g_file_mount_mountable_finish:
3325 * @file: input #GFile.
3326 * @result: a #GAsyncResult.
3327 * @error: a #GError, or %NULL
3329 * Finishes a mount operation. See g_file_mount_mountable() for details.
3331 * Finish an asynchronous mount operation that was started
3332 * with g_file_mount_mountable().
3334 * Returns: a #GFile or %NULL on error.
3337 g_file_mount_mountable_finish (GFile *file,
3338 GAsyncResult *result,
3343 g_return_val_if_fail (G_IS_FILE (file), NULL);
3344 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3346 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3348 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3349 if (g_simple_async_result_propagate_error (simple, error))
3353 iface = G_FILE_GET_IFACE (file);
3354 return (* iface->mount_mountable_finish) (file, result, error);
3358 * g_file_unmount_mountable:
3359 * @file: input #GFile.
3360 * @flags: flags affecting the operation
3361 * @cancellable: optional #GCancellable object, %NULL to ignore.
3362 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3363 * @user_data: the data to pass to callback function
3365 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3367 * If @cancellable is not %NULL, then the operation can be cancelled by
3368 * triggering the cancellable object from another thread. If the operation
3369 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3371 * When the operation is finished, @callback will be called. You can then call
3372 * g_file_unmount_mountable_finish() to get the result of the operation.
3375 g_file_unmount_mountable (GFile *file,
3376 GMountUnmountFlags flags,
3377 GCancellable *cancellable,
3378 GAsyncReadyCallback callback,
3383 g_return_if_fail (G_IS_FILE (file));
3385 iface = G_FILE_GET_IFACE (file);
3387 if (iface->unmount_mountable == NULL)
3388 g_simple_async_report_error_in_idle (G_OBJECT (file),
3392 G_IO_ERROR_NOT_SUPPORTED,
3393 _("Operation not supported"));
3395 (* iface->unmount_mountable) (file,
3403 * g_file_unmount_mountable_finish:
3404 * @file: input #GFile.
3405 * @result: a #GAsyncResult.
3406 * @error: a #GError, or %NULL
3408 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3410 * Finish an asynchronous unmount operation that was started
3411 * with g_file_unmount_mountable().
3413 * Returns: %TRUE if the operation finished successfully. %FALSE
3417 g_file_unmount_mountable_finish (GFile *file,
3418 GAsyncResult *result,
3423 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3424 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3426 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3428 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3429 if (g_simple_async_result_propagate_error (simple, error))
3433 iface = G_FILE_GET_IFACE (file);
3434 return (* iface->unmount_mountable_finish) (file, result, error);
3438 * g_file_eject_mountable:
3439 * @file: input #GFile.
3440 * @flags: flags affecting the operation
3441 * @cancellable: optional #GCancellable object, %NULL to ignore.
3442 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3443 * @user_data: the data to pass to callback function
3445 * Starts an asynchronous eject on a mountable.
3446 * When this operation has completed, @callback will be called with
3447 * @user_user data, and the operation can be finalized with
3448 * g_file_eject_mountable_finish().
3450 * If @cancellable is not %NULL, then the operation can be cancelled by
3451 * triggering the cancellable object from another thread. If the operation
3452 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3455 g_file_eject_mountable (GFile *file,
3456 GMountUnmountFlags flags,
3457 GCancellable *cancellable,
3458 GAsyncReadyCallback callback,
3463 g_return_if_fail (G_IS_FILE (file));
3465 iface = G_FILE_GET_IFACE (file);
3467 if (iface->eject_mountable == NULL)
3468 g_simple_async_report_error_in_idle (G_OBJECT (file),
3472 G_IO_ERROR_NOT_SUPPORTED,
3473 _("Operation not supported"));
3475 (* iface->eject_mountable) (file,
3483 * g_file_eject_mountable_finish:
3484 * @file: input #GFile.
3485 * @result: a #GAsyncResult.
3486 * @error: a #GError, or %NULL
3488 * Finishes an asynchronous eject operation started by
3489 * g_file_eject_mountable().
3491 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3495 g_file_eject_mountable_finish (GFile *file,
3496 GAsyncResult *result,
3501 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3502 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3504 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3506 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3507 if (g_simple_async_result_propagate_error (simple, error))
3511 iface = G_FILE_GET_IFACE (file);
3512 return (* iface->eject_mountable_finish) (file, result, error);
3516 * g_file_monitor_directory:
3517 * @file: input #GFile.
3518 * @flags: a set of #GFileMonitorFlags.
3519 * @cancellable: optional #GCancellable object, %NULL to ignore.
3520 * @error: a #GError, or %NULL.
3522 * Obtains a directory monitor for the given file.
3523 * This may fail if directory monitoring is not supported.
3525 * If @cancellable is not %NULL, then the operation can be cancelled by
3526 * triggering the cancellable object from another thread. If the operation
3527 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3529 * Returns: a #GFileMonitor for the given @file,
3530 * or %NULL on error.
3533 g_file_monitor_directory (GFile *file,
3534 GFileMonitorFlags flags,
3535 GCancellable *cancellable,
3540 g_return_val_if_fail (G_IS_FILE (file), NULL);
3542 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3545 iface = G_FILE_GET_IFACE (file);
3547 if (iface->monitor_dir == NULL)
3549 g_set_error (error, G_IO_ERROR,
3550 G_IO_ERROR_NOT_SUPPORTED,
3551 _("Operation not supported"));
3555 return (* iface->monitor_dir) (file, flags, cancellable, error);
3559 * g_file_monitor_file:
3560 * @file: input #GFile.
3561 * @flags: a set of #GFileMonitorFlags.
3562 * @cancellable: optional #GCancellable object, %NULL to ignore.
3563 * @error: a #GError, or %NULL.
3565 * Obtains a file monitor for the given file. If no file notification
3566 * mechanism exists, then regular polling of the file is used.
3568 * If @cancellable is not %NULL, then the operation can be cancelled by
3569 * triggering the cancellable object from another thread. If the operation
3570 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3572 * Returns: a #GFileMonitor for the given @file.
3575 g_file_monitor_file (GFile *file,
3576 GFileMonitorFlags flags,
3577 GCancellable *cancellable,
3581 GFileMonitor *monitor;
3583 g_return_val_if_fail (G_IS_FILE (file), NULL);
3585 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3588 iface = G_FILE_GET_IFACE (file);
3592 if (iface->monitor_file)
3593 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3595 /* Fallback to polling */
3596 if (monitor == NULL)
3597 monitor = _g_poll_file_monitor_new (file);
3602 /********************************************
3603 * Default implementation of async ops *
3604 ********************************************/
3608 GFileQueryInfoFlags flags;
3610 } QueryInfoAsyncData;
3613 query_info_data_free (QueryInfoAsyncData *data)
3616 g_object_unref (data->info);
3617 g_free (data->attributes);
3622 query_info_async_thread (GSimpleAsyncResult *res,
3624 GCancellable *cancellable)
3626 GError *error = NULL;
3627 QueryInfoAsyncData *data;
3630 data = g_simple_async_result_get_op_res_gpointer (res);
3632 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3636 g_simple_async_result_set_from_error (res, error);
3637 g_error_free (error);
3644 g_file_real_query_info_async (GFile *file,
3645 const char *attributes,
3646 GFileQueryInfoFlags flags,
3648 GCancellable *cancellable,
3649 GAsyncReadyCallback callback,
3652 GSimpleAsyncResult *res;
3653 QueryInfoAsyncData *data;
3655 data = g_new0 (QueryInfoAsyncData, 1);
3656 data->attributes = g_strdup (attributes);
3657 data->flags = flags;
3659 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3660 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3662 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3663 g_object_unref (res);
3667 g_file_real_query_info_finish (GFile *file,
3671 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3672 QueryInfoAsyncData *data;
3674 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3676 data = g_simple_async_result_get_op_res_gpointer (simple);
3678 return g_object_ref (data->info);
3685 GFileQueryInfoFlags flags;
3686 GFileEnumerator *enumerator;
3687 } EnumerateChildrenAsyncData;
3690 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3692 if (data->enumerator)
3693 g_object_unref (data->enumerator);
3694 g_free (data->attributes);
3699 enumerate_children_async_thread (GSimpleAsyncResult *res,
3701 GCancellable *cancellable)
3703 GError *error = NULL;
3704 EnumerateChildrenAsyncData *data;
3705 GFileEnumerator *enumerator;
3707 data = g_simple_async_result_get_op_res_gpointer (res);
3709 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3711 if (enumerator == NULL)
3713 g_simple_async_result_set_from_error (res, error);
3714 g_error_free (error);
3717 data->enumerator = enumerator;
3721 g_file_real_enumerate_children_async (GFile *file,
3722 const char *attributes,
3723 GFileQueryInfoFlags flags,
3725 GCancellable *cancellable,
3726 GAsyncReadyCallback callback,
3729 GSimpleAsyncResult *res;
3730 EnumerateChildrenAsyncData *data;
3732 data = g_new0 (EnumerateChildrenAsyncData, 1);
3733 data->attributes = g_strdup (attributes);
3734 data->flags = flags;
3736 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3737 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3739 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3740 g_object_unref (res);
3743 static GFileEnumerator *
3744 g_file_real_enumerate_children_finish (GFile *file,
3748 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3749 EnumerateChildrenAsyncData *data;
3751 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3753 data = g_simple_async_result_get_op_res_gpointer (simple);
3754 if (data->enumerator)
3755 return g_object_ref (data->enumerator);
3761 open_read_async_thread (GSimpleAsyncResult *res,
3763 GCancellable *cancellable)
3766 GFileInputStream *stream;
3767 GError *error = NULL;
3769 iface = G_FILE_GET_IFACE (object);
3771 stream = iface->read_fn (G_FILE (object), cancellable, &error);
3775 g_simple_async_result_set_from_error (res, error);
3776 g_error_free (error);
3779 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3783 g_file_real_read_async (GFile *file,
3785 GCancellable *cancellable,
3786 GAsyncReadyCallback callback,
3789 GSimpleAsyncResult *res;
3791 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3793 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3794 g_object_unref (res);
3797 static GFileInputStream *
3798 g_file_real_read_finish (GFile *file,
3802 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3805 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3807 op = g_simple_async_result_get_op_res_gpointer (simple);
3809 return g_object_ref (op);
3815 append_to_async_thread (GSimpleAsyncResult *res,
3817 GCancellable *cancellable)
3820 GFileCreateFlags *data;
3821 GFileOutputStream *stream;
3822 GError *error = NULL;
3824 iface = G_FILE_GET_IFACE (object);
3826 data = g_simple_async_result_get_op_res_gpointer (res);
3828 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3832 g_simple_async_result_set_from_error (res, error);
3833 g_error_free (error);
3836 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3840 g_file_real_append_to_async (GFile *file,
3841 GFileCreateFlags flags,
3843 GCancellable *cancellable,
3844 GAsyncReadyCallback callback,
3847 GFileCreateFlags *data;
3848 GSimpleAsyncResult *res;
3850 data = g_new0 (GFileCreateFlags, 1);
3853 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3854 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3856 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3857 g_object_unref (res);
3860 static GFileOutputStream *
3861 g_file_real_append_to_finish (GFile *file,
3865 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3868 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3870 op = g_simple_async_result_get_op_res_gpointer (simple);
3872 return g_object_ref (op);
3878 create_async_thread (GSimpleAsyncResult *res,
3880 GCancellable *cancellable)
3883 GFileCreateFlags *data;
3884 GFileOutputStream *stream;
3885 GError *error = NULL;
3887 iface = G_FILE_GET_IFACE (object);
3889 data = g_simple_async_result_get_op_res_gpointer (res);
3891 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3895 g_simple_async_result_set_from_error (res, error);
3896 g_error_free (error);
3899 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3903 g_file_real_create_async (GFile *file,
3904 GFileCreateFlags flags,
3906 GCancellable *cancellable,
3907 GAsyncReadyCallback callback,
3910 GFileCreateFlags *data;
3911 GSimpleAsyncResult *res;
3913 data = g_new0 (GFileCreateFlags, 1);
3916 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3917 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3919 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3920 g_object_unref (res);
3923 static GFileOutputStream *
3924 g_file_real_create_finish (GFile *file,
3928 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3931 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3933 op = g_simple_async_result_get_op_res_gpointer (simple);
3935 return g_object_ref (op);
3941 GFileOutputStream *stream;
3943 gboolean make_backup;
3944 GFileCreateFlags flags;
3948 replace_async_data_free (ReplaceAsyncData *data)
3951 g_object_unref (data->stream);
3952 g_free (data->etag);
3957 replace_async_thread (GSimpleAsyncResult *res,
3959 GCancellable *cancellable)
3962 GFileOutputStream *stream;
3963 GError *error = NULL;
3964 ReplaceAsyncData *data;
3966 iface = G_FILE_GET_IFACE (object);
3968 data = g_simple_async_result_get_op_res_gpointer (res);
3970 stream = iface->replace (G_FILE (object),
3979 g_simple_async_result_set_from_error (res, error);
3980 g_error_free (error);
3983 data->stream = stream;
3987 g_file_real_replace_async (GFile *file,
3989 gboolean make_backup,
3990 GFileCreateFlags flags,
3992 GCancellable *cancellable,
3993 GAsyncReadyCallback callback,
3996 GSimpleAsyncResult *res;
3997 ReplaceAsyncData *data;
3999 data = g_new0 (ReplaceAsyncData, 1);
4000 data->etag = g_strdup (etag);
4001 data->make_backup = make_backup;
4002 data->flags = flags;
4004 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4005 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4007 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4008 g_object_unref (res);
4011 static GFileOutputStream *
4012 g_file_real_replace_finish (GFile *file,
4016 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4017 ReplaceAsyncData *data;
4019 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4021 data = g_simple_async_result_get_op_res_gpointer (simple);
4023 return g_object_ref (data->stream);
4031 } SetDisplayNameAsyncData;
4034 set_display_name_data_free (SetDisplayNameAsyncData *data)
4036 g_free (data->name);
4038 g_object_unref (data->file);
4043 set_display_name_async_thread (GSimpleAsyncResult *res,
4045 GCancellable *cancellable)
4047 GError *error = NULL;
4048 SetDisplayNameAsyncData *data;
4051 data = g_simple_async_result_get_op_res_gpointer (res);
4053 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4057 g_simple_async_result_set_from_error (res, error);
4058 g_error_free (error);
4065 g_file_real_set_display_name_async (GFile *file,
4066 const char *display_name,
4068 GCancellable *cancellable,
4069 GAsyncReadyCallback callback,
4072 GSimpleAsyncResult *res;
4073 SetDisplayNameAsyncData *data;
4075 data = g_new0 (SetDisplayNameAsyncData, 1);
4076 data->name = g_strdup (display_name);
4078 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4079 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4081 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4082 g_object_unref (res);
4086 g_file_real_set_display_name_finish (GFile *file,
4090 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4091 SetDisplayNameAsyncData *data;
4093 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4095 data = g_simple_async_result_get_op_res_gpointer (simple);
4097 return g_object_ref (data->file);
4103 GFileQueryInfoFlags flags;
4110 set_info_data_free (SetInfoAsyncData *data)
4113 g_object_unref (data->info);
4115 g_error_free (data->error);
4120 set_info_async_thread (GSimpleAsyncResult *res,
4122 GCancellable *cancellable)
4124 SetInfoAsyncData *data;
4126 data = g_simple_async_result_get_op_res_gpointer (res);
4129 data->res = g_file_set_attributes_from_info (G_FILE (object),
4137 g_file_real_set_attributes_async (GFile *file,
4139 GFileQueryInfoFlags flags,
4141 GCancellable *cancellable,
4142 GAsyncReadyCallback callback,
4145 GSimpleAsyncResult *res;
4146 SetInfoAsyncData *data;
4148 data = g_new0 (SetInfoAsyncData, 1);
4149 data->info = g_file_info_dup (info);
4150 data->flags = flags;
4152 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4153 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4155 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4156 g_object_unref (res);
4160 g_file_real_set_attributes_finish (GFile *file,
4165 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4166 SetInfoAsyncData *data;
4168 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4170 data = g_simple_async_result_get_op_res_gpointer (simple);
4173 *info = g_object_ref (data->info);
4175 if (error != NULL && data->error)
4176 *error = g_error_copy (data->error);
4182 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4184 GCancellable *cancellable)
4186 GError *error = NULL;
4189 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4193 g_simple_async_result_set_from_error (res, error);
4194 g_error_free (error);
4197 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4201 g_file_real_find_enclosing_mount_async (GFile *file,
4203 GCancellable *cancellable,
4204 GAsyncReadyCallback callback,
4207 GSimpleAsyncResult *res;
4209 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4211 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4212 g_object_unref (res);
4216 g_file_real_find_enclosing_mount_finish (GFile *file,
4220 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4223 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4225 mount = g_simple_async_result_get_op_res_gpointer (simple);
4226 return g_object_ref (mount);
4233 GFileCopyFlags flags;
4234 GFileProgressCallback progress_cb;
4235 gpointer progress_cb_data;
4236 GIOSchedulerJob *job;
4240 copy_async_data_free (CopyAsyncData *data)
4242 g_object_unref (data->source);
4243 g_object_unref (data->destination);
4248 CopyAsyncData *data;
4249 goffset current_num_bytes;
4250 goffset total_num_bytes;
4254 copy_async_progress_in_main (gpointer user_data)
4256 ProgressData *progress = user_data;
4257 CopyAsyncData *data = progress->data;
4259 data->progress_cb (progress->current_num_bytes,
4260 progress->total_num_bytes,
4261 data->progress_cb_data);
4267 mainloop_barrier (gpointer user_data)
4269 /* Does nothing, but ensures all queued idles before
4276 copy_async_progress_callback (goffset current_num_bytes,
4277 goffset total_num_bytes,
4280 CopyAsyncData *data = user_data;
4281 ProgressData *progress;
4283 progress = g_new (ProgressData, 1);
4284 progress->data = data;
4285 progress->current_num_bytes = current_num_bytes;
4286 progress->total_num_bytes = total_num_bytes;
4288 g_io_scheduler_job_send_to_mainloop_async (data->job,
4289 copy_async_progress_in_main,
4295 copy_async_thread (GIOSchedulerJob *job,
4296 GCancellable *cancellable,
4299 GSimpleAsyncResult *res;
4300 CopyAsyncData *data;
4305 data = g_simple_async_result_get_op_res_gpointer (res);
4309 result = g_file_copy (data->source,
4313 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4317 /* Ensure all progress callbacks are done running in main thread */
4318 if (data->progress_cb != NULL)
4319 g_io_scheduler_job_send_to_mainloop (job,
4325 g_simple_async_result_set_from_error (res, error);
4326 g_error_free (error);
4329 g_simple_async_result_complete_in_idle (res);
4335 g_file_real_copy_async (GFile *source,
4337 GFileCopyFlags flags,
4339 GCancellable *cancellable,
4340 GFileProgressCallback progress_callback,
4341 gpointer progress_callback_data,
4342 GAsyncReadyCallback callback,
4345 GSimpleAsyncResult *res;
4346 CopyAsyncData *data;
4348 data = g_new0 (CopyAsyncData, 1);
4349 data->source = g_object_ref (source);
4350 data->destination = g_object_ref (destination);
4351 data->flags = flags;
4352 data->progress_cb = progress_callback;
4353 data->progress_cb_data = progress_callback_data;
4355 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4356 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4358 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4362 g_file_real_copy_finish (GFile *file,
4366 /* Error handled in g_file_copy_finish() */
4371 /********************************************
4372 * Default VFS operations *
4373 ********************************************/
4376 * g_file_new_for_path:
4377 * @path: a string containing a relative or absolute path.
4379 * Constructs a #GFile for a given path. This operation never
4380 * fails, but the returned object might not support any I/O
4381 * operation if @path is malformed.
4383 * Returns: a new #GFile for the given @path.
4386 g_file_new_for_path (const char *path)
4388 g_return_val_if_fail (path != NULL, NULL);
4390 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4394 * g_file_new_for_uri:
4395 * @uri: a string containing a URI.
4397 * Constructs a #GFile for a given URI. This operation never
4398 * fails, but the returned object might not support any I/O
4399 * operation if @uri is malformed or if the uri type is
4402 * Returns: a #GFile for the given @uri.
4405 g_file_new_for_uri (const char *uri)
4407 g_return_val_if_fail (uri != NULL, NULL);
4409 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4413 * g_file_parse_name:
4414 * @parse_name: a file name or path to be parsed.
4416 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4417 * This operation never fails, but the returned object might not support any I/O
4418 * operation if the @parse_name cannot be parsed.
4420 * Returns: a new #GFile.
4423 g_file_parse_name (const char *parse_name)
4425 g_return_val_if_fail (parse_name != NULL, NULL);
4427 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4431 is_valid_scheme_character (char c)
4433 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4437 has_valid_scheme (const char *uri)
4443 if (!is_valid_scheme_character (*p))
4448 } while (is_valid_scheme_character (*p));
4454 * g_file_new_for_commandline_arg:
4455 * @arg: a command line string.
4457 * Creates a #GFile with the given argument from the command line. The value of
4458 * @arg can be either a URI, an absolute path or a relative path resolved
4459 * relative to the current working directory.
4460 * This operation never fails, but the returned object might not support any
4461 * I/O operation if @arg points to a malformed path.
4463 * Returns: a new #GFile.
4466 g_file_new_for_commandline_arg (const char *arg)
4472 g_return_val_if_fail (arg != NULL, NULL);
4474 if (g_path_is_absolute (arg))
4475 return g_file_new_for_path (arg);
4477 if (has_valid_scheme (arg))
4478 return g_file_new_for_uri (arg);
4480 current_dir = g_get_current_dir ();
4481 filename = g_build_filename (current_dir, arg, NULL);
4482 g_free (current_dir);
4484 file = g_file_new_for_path (filename);
4491 * g_file_mount_enclosing_volume:
4492 * @location: input #GFile.
4493 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4494 * @cancellable: optional #GCancellable object, %NULL to ignore.
4495 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4496 * @user_data: the data to pass to callback function
4498 * Starts a @mount_operation, mounting the volume that contains the file @location.
4500 * When this operation has completed, @callback will be called with
4501 * @user_user data, and the operation can be finalized with
4502 * g_file_mount_enclosing_volume_finish().
4504 * If @cancellable is not %NULL, then the operation can be cancelled by
4505 * triggering the cancellable object from another thread. If the operation
4506 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4509 g_file_mount_enclosing_volume (GFile *location,
4510 GMountOperation *mount_operation,
4511 GCancellable *cancellable,
4512 GAsyncReadyCallback callback,
4517 g_return_if_fail (G_IS_FILE (location));
4519 iface = G_FILE_GET_IFACE (location);
4521 if (iface->mount_enclosing_volume == NULL)
4523 g_simple_async_report_error_in_idle (G_OBJECT (location),
4524 callback, user_data,
4525 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4526 _("volume doesn't implement mount"));
4531 (* iface->mount_enclosing_volume) (location, mount_operation, cancellable, callback, user_data);
4536 * g_file_mount_enclosing_volume_finish:
4537 * @location: input #GFile.
4538 * @result: a #GAsyncResult.
4539 * @error: a #GError, or %NULL
4541 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4543 * Returns: %TRUE if successful. If an error
4544 * has occurred, this function will return %FALSE and set @error
4545 * appropriately if present.
4548 g_file_mount_enclosing_volume_finish (GFile *location,
4549 GAsyncResult *result,
4554 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4555 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4557 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4559 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4560 if (g_simple_async_result_propagate_error (simple, error))
4564 iface = G_FILE_GET_IFACE (location);
4566 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4569 /********************************************
4570 * Utility functions *
4571 ********************************************/
4573 #define GET_CONTENT_BLOCK_SIZE 8192
4576 * g_file_load_contents:
4577 * @file: input #GFile.
4578 * @cancellable: optional #GCancellable object, %NULL to ignore.
4579 * @contents: a location to place the contents of the file.
4580 * @length: a location to place the length of the contents of the file.
4581 * @etag_out: a location to place the current entity tag for the file.
4582 * @error: a #GError, or %NULL
4584 * Loads the content of the file into memory, returning the size of
4585 * the data. The data is always zero terminated, but this is not
4586 * included in the resultant @length.
4588 * If @cancellable is not %NULL, then the operation can be cancelled by
4589 * triggering the cancellable object from another thread. If the operation
4590 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4592 * Returns: %TRUE if the @file's contents were successfully loaded.
4593 * %FALSE if there were errors..
4596 g_file_load_contents (GFile *file,
4597 GCancellable *cancellable,
4603 GFileInputStream *in;
4604 GByteArray *content;
4609 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4610 g_return_val_if_fail (contents != NULL, FALSE);
4612 in = g_file_read (file, cancellable, error);
4616 content = g_byte_array_new ();
4619 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4620 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4621 content->data + pos,
4622 GET_CONTENT_BLOCK_SIZE,
4623 cancellable, error)) > 0)
4626 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4633 info = g_file_input_stream_query_info (in,
4634 G_FILE_ATTRIBUTE_ETAG_VALUE,
4639 *etag_out = g_strdup (g_file_info_get_etag (info));
4640 g_object_unref (info);
4644 /* Ignore errors on close */
4645 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4646 g_object_unref (in);
4650 /* error is set already */
4651 g_byte_array_free (content, TRUE);
4658 /* Zero terminate (we got an extra byte allocated for this */
4659 content->data[pos] = 0;
4661 *contents = (char *)g_byte_array_free (content, FALSE);
4669 GCancellable *cancellable;
4670 GFileReadMoreCallback read_more_callback;
4671 GAsyncReadyCallback callback;
4673 GByteArray *content;
4680 load_contents_data_free (LoadContentsData *data)
4683 g_error_free (data->error);
4684 if (data->cancellable)
4685 g_object_unref (data->cancellable);
4687 g_byte_array_free (data->content, TRUE);
4688 g_free (data->etag);
4689 g_object_unref (data->file);
4694 load_contents_close_callback (GObject *obj,
4695 GAsyncResult *close_res,
4698 GInputStream *stream = G_INPUT_STREAM (obj);
4699 LoadContentsData *data = user_data;
4700 GSimpleAsyncResult *res;
4702 /* Ignore errors here, we're only reading anyway */
4703 g_input_stream_close_finish (stream, close_res, NULL);
4704 g_object_unref (stream);
4706 res = g_simple_async_result_new (G_OBJECT (data->file),
4709 g_file_load_contents_async);
4710 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4711 g_simple_async_result_complete (res);
4712 g_object_unref (res);
4716 load_contents_fstat_callback (GObject *obj,
4717 GAsyncResult *stat_res,
4720 GInputStream *stream = G_INPUT_STREAM (obj);
4721 LoadContentsData *data = user_data;
4724 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4728 data->etag = g_strdup (g_file_info_get_etag (info));
4729 g_object_unref (info);
4732 g_input_stream_close_async (stream, 0,
4734 load_contents_close_callback, data);
4738 load_contents_read_callback (GObject *obj,
4739 GAsyncResult *read_res,
4742 GInputStream *stream = G_INPUT_STREAM (obj);
4743 LoadContentsData *data = user_data;
4744 GError *error = NULL;
4747 read_size = g_input_stream_read_finish (stream, read_res, &error);
4751 /* Error or EOF, close the file */
4752 data->error = error;
4753 g_input_stream_close_async (stream, 0,
4755 load_contents_close_callback, data);
4757 else if (read_size == 0)
4759 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4760 G_FILE_ATTRIBUTE_ETAG_VALUE,
4763 load_contents_fstat_callback,
4766 else if (read_size > 0)
4768 data->pos += read_size;
4770 g_byte_array_set_size (data->content,
4771 data->pos + GET_CONTENT_BLOCK_SIZE);
4774 if (data->read_more_callback &&
4775 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4776 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4777 G_FILE_ATTRIBUTE_ETAG_VALUE,
4780 load_contents_fstat_callback,
4783 g_input_stream_read_async (stream,
4784 data->content->data + data->pos,
4785 GET_CONTENT_BLOCK_SIZE,
4788 load_contents_read_callback,
4794 load_contents_open_callback (GObject *obj,
4795 GAsyncResult *open_res,
4798 GFile *file = G_FILE (obj);
4799 GFileInputStream *stream;
4800 LoadContentsData *data = user_data;
4801 GError *error = NULL;
4802 GSimpleAsyncResult *res;
4804 stream = g_file_read_finish (file, open_res, &error);
4808 g_byte_array_set_size (data->content,
4809 data->pos + GET_CONTENT_BLOCK_SIZE);
4810 g_input_stream_read_async (G_INPUT_STREAM (stream),
4811 data->content->data + data->pos,
4812 GET_CONTENT_BLOCK_SIZE,
4815 load_contents_read_callback,
4821 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4825 g_simple_async_result_complete (res);
4826 g_error_free (error);
4827 load_contents_data_free (data);
4828 g_object_unref (res);
4833 * g_file_load_partial_contents_async:
4834 * @file: input #GFile.
4835 * @cancellable: optional #GCancellable object, %NULL to ignore.
4836 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
4837 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4838 * @user_data: the data to pass to the callback functions.
4840 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
4841 * used to stop reading from the file when appropriate, else this function
4842 * will behave exactly as g_file_load_contents_async(). This operation
4843 * can be finished by g_file_load_partial_contents_finish().
4845 * Users of this function should be aware that @user_data is passed to
4846 * both the @read_more_callback and the @callback.
4848 * If @cancellable is not %NULL, then the operation can be cancelled by
4849 * triggering the cancellable object from another thread. If the operation
4850 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4853 g_file_load_partial_contents_async (GFile *file,
4854 GCancellable *cancellable,
4855 GFileReadMoreCallback read_more_callback,
4856 GAsyncReadyCallback callback,
4859 LoadContentsData *data;
4861 g_return_if_fail (G_IS_FILE (file));
4863 data = g_new0 (LoadContentsData, 1);
4866 data->cancellable = g_object_ref (cancellable);
4867 data->read_more_callback = read_more_callback;
4868 data->callback = callback;
4869 data->user_data = user_data;
4870 data->content = g_byte_array_new ();
4871 data->file = g_object_ref (file);
4873 g_file_read_async (file,
4876 load_contents_open_callback,
4881 * g_file_load_partial_contents_finish:
4882 * @file: input #GFile.
4883 * @res: a #GAsyncResult.
4884 * @contents: a location to place the contents of the file.
4885 * @length: a location to place the length of the contents of the file.
4886 * @etag_out: a location to place the current entity tag for the file.
4887 * @error: a #GError, or %NULL
4889 * Finishes an asynchronous partial load operation that was started
4890 * with g_file_load_partial_contents_async().
4892 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4893 * present, it will be set appropriately.
4896 g_file_load_partial_contents_finish (GFile *file,
4903 GSimpleAsyncResult *simple;
4904 LoadContentsData *data;
4906 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4907 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4908 g_return_val_if_fail (contents != NULL, FALSE);
4910 simple = G_SIMPLE_ASYNC_RESULT (res);
4912 if (g_simple_async_result_propagate_error (simple, error))
4915 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4917 data = g_simple_async_result_get_op_res_gpointer (simple);
4921 g_propagate_error (error, data->error);
4930 *length = data->pos;
4934 *etag_out = data->etag;
4938 /* Zero terminate */
4939 g_byte_array_set_size (data->content, data->pos + 1);
4940 data->content->data[data->pos] = 0;
4942 *contents = (char *)g_byte_array_free (data->content, FALSE);
4943 data->content = NULL;
4949 * g_file_load_contents_async:
4950 * @file: input #GFile.
4951 * @cancellable: optional #GCancellable object, %NULL to ignore.
4952 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4953 * @user_data: the data to pass to callback function
4955 * Starts an asynchronous load of the @file's contents.
4957 * For more details, see g_file_load_contents() which is
4958 * the synchronous version of this call.
4960 * When the load operation has completed, @callback will be called
4961 * with @user data. To finish the operation, call
4962 * g_file_load_contents_finish() with the #GAsyncResult returned by
4965 * If @cancellable is not %NULL, then the operation can be cancelled by
4966 * triggering the cancellable object from another thread. If the operation
4967 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4970 g_file_load_contents_async (GFile *file,
4971 GCancellable *cancellable,
4972 GAsyncReadyCallback callback,
4975 g_file_load_partial_contents_async (file,
4978 callback, user_data);
4982 * g_file_load_contents_finish:
4983 * @file: input #GFile.
4984 * @res: a #GAsyncResult.
4985 * @contents: a location to place the contents of the file.
4986 * @length: a location to place the length of the contents of the file.
4987 * @etag_out: a location to place the current entity tag for the file.
4988 * @error: a #GError, or %NULL
4990 * Finishes an asynchronous load of the @file's contents.
4991 * The contents are placed in @contents, and @length is set to the
4992 * size of the @contents string. If @etag_out is present, it will be
4993 * set to the new entity tag for the @file.
4995 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4996 * present, it will be set appropriately.
4999 g_file_load_contents_finish (GFile *file,
5006 return g_file_load_partial_contents_finish (file,
5015 * g_file_replace_contents:
5016 * @file: input #GFile.
5017 * @contents: a string containing the new contents for @file.
5018 * @length: the length of @contents in bytes.
5019 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5021 * @make_backup: %TRUE if a backup should be created.
5022 * @flags: a set of #GFileCreateFlags.
5023 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5024 * for the document. This should be freed with g_free() when no longer
5026 * @cancellable: optional #GCancellable object, %NULL to ignore.
5027 * @error: a #GError, or %NULL
5029 * Replaces the contents of @file with @contents of @length bytes.
5031 * If @etag is specified (not %NULL) any existing file must have that etag, or
5032 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5034 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5036 * If @cancellable is not %NULL, then the operation can be cancelled by
5037 * triggering the cancellable object from another thread. If the operation
5038 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5040 * The returned @new_etag can be used to verify that the file hasn't changed the
5041 * next time it is saved over.
5043 * Returns: %TRUE if successful. If an error
5044 * has occurred, this function will return %FALSE and set @error
5045 * appropriately if present.
5048 g_file_replace_contents (GFile *file,
5049 const char *contents,
5052 gboolean make_backup,
5053 GFileCreateFlags flags,
5055 GCancellable *cancellable,
5058 GFileOutputStream *out;
5059 gsize pos, remainder;
5062 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5063 g_return_val_if_fail (contents != NULL, FALSE);
5065 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5071 while (remainder > 0 &&
5072 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5074 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5082 if (remainder > 0 && res < 0)
5084 /* Ignore errors on close */
5085 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5087 /* error is set already */
5091 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5095 *new_etag = g_file_output_stream_get_etag (out);
5103 GCancellable *cancellable;
5104 GAsyncReadyCallback callback;
5106 const char *content;
5110 } ReplaceContentsData;
5113 replace_contents_data_free (ReplaceContentsData *data)
5116 g_error_free (data->error);
5117 if (data->cancellable)
5118 g_object_unref (data->cancellable);
5119 g_object_unref (data->file);
5120 g_free (data->etag);
5125 replace_contents_close_callback (GObject *obj,
5126 GAsyncResult *close_res,
5129 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5130 ReplaceContentsData *data = user_data;
5131 GSimpleAsyncResult *res;
5133 /* Ignore errors here, we're only reading anyway */
5134 g_output_stream_close_finish (stream, close_res, NULL);
5135 g_object_unref (stream);
5137 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5139 res = g_simple_async_result_new (G_OBJECT (data->file),
5142 g_file_replace_contents_async);
5143 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5144 g_simple_async_result_complete (res);
5145 g_object_unref (res);
5149 replace_contents_write_callback (GObject *obj,
5150 GAsyncResult *read_res,
5153 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5154 ReplaceContentsData *data = user_data;
5155 GError *error = NULL;
5158 write_size = g_output_stream_write_finish (stream, read_res, &error);
5160 if (write_size <= 0)
5162 /* Error or EOF, close the file */
5164 data->error = error;
5165 g_output_stream_close_async (stream, 0,
5167 replace_contents_close_callback, data);
5169 else if (write_size > 0)
5171 data->pos += write_size;
5173 if (data->pos >= data->length)
5174 g_output_stream_close_async (stream, 0,
5176 replace_contents_close_callback, data);
5178 g_output_stream_write_async (stream,
5179 data->content + data->pos,
5180 data->length - data->pos,
5183 replace_contents_write_callback,
5189 replace_contents_open_callback (GObject *obj,
5190 GAsyncResult *open_res,
5193 GFile *file = G_FILE (obj);
5194 GFileOutputStream *stream;
5195 ReplaceContentsData *data = user_data;
5196 GError *error = NULL;
5197 GSimpleAsyncResult *res;
5199 stream = g_file_replace_finish (file, open_res, &error);
5203 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5204 data->content + data->pos,
5205 data->length - data->pos,
5208 replace_contents_write_callback,
5214 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5218 g_simple_async_result_complete (res);
5219 g_error_free (error);
5220 replace_contents_data_free (data);
5221 g_object_unref (res);
5226 * g_file_replace_contents_async:
5227 * @file: input #GFile.
5228 * @contents: string of contents to replace the file with.
5229 * @length: the length of @contents in bytes.
5230 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5231 * @make_backup: %TRUE if a backup should be created.
5232 * @flags: a set of #GFileCreateFlags.
5233 * @cancellable: optional #GCancellable object, %NULL to ignore.
5234 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5235 * @user_data: the data to pass to callback function
5237 * Starts an asynchronous replacement of @file with the given
5238 * @contents of @length bytes. @etag will replace the document's
5239 * current entity tag.
5241 * When this operation has completed, @callback will be called with
5242 * @user_user data, and the operation can be finalized with
5243 * g_file_replace_contents_finish().
5245 * If @cancellable is not %NULL, then the operation can be cancelled by
5246 * triggering the cancellable object from another thread. If the operation
5247 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5249 * If @make_backup is %TRUE, this function will attempt to
5250 * make a backup of @file.
5253 g_file_replace_contents_async (GFile *file,
5254 const char *contents,
5257 gboolean make_backup,
5258 GFileCreateFlags flags,
5259 GCancellable *cancellable,
5260 GAsyncReadyCallback callback,
5263 ReplaceContentsData *data;
5265 g_return_if_fail (G_IS_FILE (file));
5266 g_return_if_fail (contents != NULL);
5268 data = g_new0 (ReplaceContentsData, 1);
5271 data->cancellable = g_object_ref (cancellable);
5272 data->callback = callback;
5273 data->user_data = user_data;
5274 data->content = contents;
5275 data->length = length;
5277 data->file = g_object_ref (file);
5279 g_file_replace_async (file,
5285 replace_contents_open_callback,
5290 * g_file_replace_contents_finish:
5291 * @file: input #GFile.
5292 * @res: a #GAsyncResult.
5293 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5294 * for the document. This should be freed with g_free() when it is no
5296 * @error: a #GError, or %NULL
5298 * Finishes an asynchronous replace of the given @file. See
5299 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5300 * tag for the document, if present.
5302 * Returns: %TRUE on success, %FALSE on failure.
5305 g_file_replace_contents_finish (GFile *file,
5310 GSimpleAsyncResult *simple;
5311 ReplaceContentsData *data;
5313 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5314 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5316 simple = G_SIMPLE_ASYNC_RESULT (res);
5318 if (g_simple_async_result_propagate_error (simple, error))
5321 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5323 data = g_simple_async_result_get_op_res_gpointer (simple);
5327 g_propagate_error (error, data->error);
5335 *new_etag = data->etag;
5336 data->etag = NULL; /* Take ownership */
5342 #define __G_FILE_C__
5343 #include "gioaliasdef.c"