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 or directory with this name already exists the G_IO_ERROR_EXISTS
1310 * error will be returned.
1311 * Some file systems don't allow all file names, and may
1312 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1313 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1314 * Other errors are possible too, and depend on what kind of
1315 * filesystem the file is on.
1317 * Returns: a #GFileOutputStream for the newly created file, or
1321 g_file_create (GFile *file,
1322 GFileCreateFlags flags,
1323 GCancellable *cancellable,
1328 g_return_val_if_fail (G_IS_FILE (file), NULL);
1330 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1333 iface = G_FILE_GET_IFACE (file);
1335 if (iface->create == NULL)
1337 g_set_error (error, G_IO_ERROR,
1338 G_IO_ERROR_NOT_SUPPORTED,
1339 _("Operation not supported"));
1343 return (* iface->create) (file, flags, cancellable, error);
1348 * @file: input #GFile.
1349 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1350 * current #GFile, or #NULL to ignore.
1351 * @make_backup: %TRUE if a backup should be created.
1352 * @flags: a set of #GFileCreateFlags.
1353 * @cancellable: optional #GCancellable object, %NULL to ignore.
1354 * @error: a #GError, or %NULL
1356 * Returns an output stream for overwriting the file, possibly
1357 * creating a backup copy of the file first.
1359 * This will try to replace the file in the safest way possible so
1360 * that any errors during the writing will not affect an already
1361 * existing copy of the file. For instance, for local files it
1362 * may write to a temporary file and then atomically rename over
1363 * the destination when the stream is closed.
1365 * By default files created are generally readable by everyone,
1366 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1367 * will be made readable only to the current user, to the level that
1368 * is supported on the target filesystem.
1370 * If @cancellable is not %NULL, then the operation can be cancelled by
1371 * triggering the cancellable object from another thread. If the operation
1372 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1374 * If you pass in a non-#NULL @etag value, then this value is
1375 * compared to the current entity tag of the file, and if they differ
1376 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1377 * that the file has been changed since you last read it. You can get
1378 * the new etag from g_file_output_stream_get_etag() after you've
1379 * finished writing and closed the #GFileOutputStream. When you load
1380 * a new file you can use g_file_input_stream_query_info() to get
1381 * the etag of the file.
1383 * If @make_backup is %TRUE, this function will attempt to make a backup
1384 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1385 * error will be returned. If you want to replace anyway, try again with
1386 * @make_backup set to %FALSE.
1388 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1389 * and if the file is some other form of non-regular file then a
1390 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1391 * Some file systems don't allow all file names, and may
1392 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1393 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1394 * Other errors are possible too, and depend on what kind of
1395 * filesystem the file is on.
1397 * Returns: a #GFileOutputStream or %NULL on error.
1400 g_file_replace (GFile *file,
1402 gboolean make_backup,
1403 GFileCreateFlags flags,
1404 GCancellable *cancellable,
1409 g_return_val_if_fail (G_IS_FILE (file), NULL);
1411 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1414 iface = G_FILE_GET_IFACE (file);
1416 if (iface->replace == NULL)
1418 g_set_error (error, G_IO_ERROR,
1419 G_IO_ERROR_NOT_SUPPORTED,
1420 _("Operation not supported"));
1425 /* Handle empty tag string as NULL in consistent way. */
1426 if (etag && *etag == 0)
1429 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1433 * g_file_read_async:
1434 * @file: input #GFile.
1435 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1437 * @cancellable: optional #GCancellable object, %NULL to ignore.
1438 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1439 * @user_data: the data to pass to callback function
1441 * Asynchronously opens @file for reading.
1443 * For more details, see g_file_read() which is
1444 * the synchronous version of this call.
1446 * When the operation is finished, @callback will be called. You can then call
1447 * g_file_read_finish() to get the result of the operation.
1450 g_file_read_async (GFile *file,
1452 GCancellable *cancellable,
1453 GAsyncReadyCallback callback,
1458 g_return_if_fail (G_IS_FILE (file));
1460 iface = G_FILE_GET_IFACE (file);
1461 (* iface->read_async) (file,
1469 * g_file_read_finish:
1470 * @file: input #GFile.
1471 * @res: a #GAsyncResult.
1472 * @error: a #GError, or %NULL
1474 * Finishes an asynchronous file read operation started with
1475 * g_file_read_async().
1477 * Returns: a #GFileInputStream or %NULL on error.
1480 g_file_read_finish (GFile *file,
1486 g_return_val_if_fail (G_IS_FILE (file), NULL);
1487 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1489 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1491 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1492 if (g_simple_async_result_propagate_error (simple, error))
1496 iface = G_FILE_GET_IFACE (file);
1497 return (* iface->read_finish) (file, res, error);
1501 * g_file_append_to_async:
1502 * @file: input #GFile.
1503 * @flags: a set of #GFileCreateFlags.
1504 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1506 * @cancellable: optional #GCancellable object, %NULL to ignore.
1507 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1508 * @user_data: the data to pass to callback function
1510 * Asynchronously opens @file for appending.
1512 * For more details, see g_file_append_to() which is
1513 * the synchronous version of this call.
1515 * When the operation is finished, @callback will be called. You can then call
1516 * g_file_append_to_finish() to get the result of the operation.
1519 g_file_append_to_async (GFile *file,
1520 GFileCreateFlags flags,
1522 GCancellable *cancellable,
1523 GAsyncReadyCallback callback,
1528 g_return_if_fail (G_IS_FILE (file));
1530 iface = G_FILE_GET_IFACE (file);
1531 (* iface->append_to_async) (file,
1540 * g_file_append_to_finish:
1541 * @file: input #GFile.
1542 * @res: #GAsyncResult
1543 * @error: a #GError, or %NULL
1545 * Finishes an asynchronous file append operation started with
1546 * g_file_append_to_async().
1548 * Returns: a valid #GFileOutputStream or %NULL on error.
1551 g_file_append_to_finish (GFile *file,
1557 g_return_val_if_fail (G_IS_FILE (file), NULL);
1558 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1560 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1562 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1563 if (g_simple_async_result_propagate_error (simple, error))
1567 iface = G_FILE_GET_IFACE (file);
1568 return (* iface->append_to_finish) (file, res, error);
1572 * g_file_create_async:
1573 * @file: input #GFile.
1574 * @flags: a set of #GFileCreateFlags.
1575 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1577 * @cancellable: optional #GCancellable object, %NULL to ignore.
1578 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1579 * @user_data: the data to pass to callback function
1581 * Asynchronously creates a new file and returns an output stream for writing to it.
1582 * The file must not already exists.
1584 * For more details, see g_file_create() which is
1585 * the synchronous version of this call.
1587 * When the operation is finished, @callback will be called. You can then call
1588 * g_file_create_finish() to get the result of the operation.
1591 g_file_create_async (GFile *file,
1592 GFileCreateFlags flags,
1594 GCancellable *cancellable,
1595 GAsyncReadyCallback callback,
1600 g_return_if_fail (G_IS_FILE (file));
1602 iface = G_FILE_GET_IFACE (file);
1603 (* iface->create_async) (file,
1612 * g_file_create_finish:
1613 * @file: input #GFile.
1614 * @res: a #GAsyncResult.
1615 * @error: a #GError, or %NULL
1617 * Finishes an asynchronous file create operation started with
1618 * g_file_create_async().
1620 * Returns: a #GFileOutputStream or %NULL on error.
1623 g_file_create_finish (GFile *file,
1629 g_return_val_if_fail (G_IS_FILE (file), NULL);
1630 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1632 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1634 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1635 if (g_simple_async_result_propagate_error (simple, error))
1639 iface = G_FILE_GET_IFACE (file);
1640 return (* iface->create_finish) (file, res, error);
1644 * g_file_replace_async:
1645 * @file: input #GFile.
1646 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1647 * current #GFile, or NULL to ignore.
1648 * @make_backup: %TRUE if a backup should be created.
1649 * @flags: a set of #GFileCreateFlags.
1650 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1652 * @cancellable: optional #GCancellable object, %NULL to ignore.
1653 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1654 * @user_data: the data to pass to callback function
1656 * Asynchronously overwrites the file, replacing the contents, possibly
1657 * creating a backup copy of the file first.
1659 * For more details, see g_file_replace() which is
1660 * the synchronous version of this call.
1662 * When the operation is finished, @callback will be called. You can then call
1663 * g_file_replace_finish() to get the result of the operation.
1666 g_file_replace_async (GFile *file,
1668 gboolean make_backup,
1669 GFileCreateFlags flags,
1671 GCancellable *cancellable,
1672 GAsyncReadyCallback callback,
1677 g_return_if_fail (G_IS_FILE (file));
1679 iface = G_FILE_GET_IFACE (file);
1680 (* iface->replace_async) (file,
1691 * g_file_replace_finish:
1692 * @file: input #GFile.
1693 * @res: a #GAsyncResult.
1694 * @error: a #GError, or %NULL
1696 * Finishes an asynchronous file replace operation started with
1697 * g_file_replace_async().
1699 * Returns: a #GFileOutputStream, or %NULL on error.
1702 g_file_replace_finish (GFile *file,
1708 g_return_val_if_fail (G_IS_FILE (file), NULL);
1709 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1711 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1713 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1714 if (g_simple_async_result_propagate_error (simple, error))
1718 iface = G_FILE_GET_IFACE (file);
1719 return (* iface->replace_finish) (file, res, error);
1723 copy_symlink (GFile *destination,
1724 GFileCopyFlags flags,
1725 GCancellable *cancellable,
1730 gboolean tried_delete;
1732 GFileType file_type;
1734 tried_delete = FALSE;
1738 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1740 /* Maybe it already existed, and we want to overwrite? */
1741 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1742 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1744 g_error_free (my_error);
1747 /* Don't overwrite if the destination is a directory */
1748 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1749 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1750 cancellable, &my_error);
1753 file_type = g_file_info_get_file_type (info);
1754 g_object_unref (info);
1756 if (file_type == G_FILE_TYPE_DIRECTORY)
1758 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1759 _("Can't copy over directory"));
1764 if (!g_file_delete (destination, cancellable, error))
1767 tried_delete = TRUE;
1771 g_propagate_error (error, my_error);
1778 static GInputStream *
1779 open_source_for_copy (GFile *source,
1781 GFileCopyFlags flags,
1782 GCancellable *cancellable,
1788 GFileType file_type;
1791 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1795 /* There was an error opening the source, try to set a good error for it: */
1797 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1799 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1800 * as that is less useful to the app. Better check for errors on the
1803 g_error_free (my_error);
1806 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1807 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1808 cancellable, &my_error);
1811 file_type = g_file_info_get_file_type (info);
1812 g_object_unref (info);
1814 if (flags & G_FILE_COPY_OVERWRITE)
1816 if (file_type == G_FILE_TYPE_DIRECTORY)
1818 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1819 _("Can't copy directory over directory"));
1822 /* continue to would_recurse error */
1826 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1827 _("Target file exists"));
1833 /* Error getting info from target, return that error
1834 * (except for NOT_FOUND, which is no error here)
1836 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1838 g_propagate_error (error, my_error);
1841 g_error_free (my_error);
1844 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1845 _("Can't recursively copy directory"));
1849 g_propagate_error (error, my_error);
1854 should_copy (GFileAttributeInfo *info,
1858 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1859 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1863 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1864 GFileAttributeInfoList *namespaces,
1872 s = g_string_new ("");
1876 for (i = 0; i < attributes->n_infos; i++)
1878 if (should_copy (&attributes->infos[i], as_move))
1883 g_string_append_c (s, ',');
1885 g_string_append (s, attributes->infos[i].name);
1892 for (i = 0; i < namespaces->n_infos; i++)
1894 if (should_copy (&namespaces->infos[i], as_move))
1899 g_string_append_c (s, ',');
1901 g_string_append (s, namespaces->infos[i].name);
1902 g_string_append (s, ":*");
1907 return g_string_free (s, FALSE);
1911 * g_file_copy_attributes:
1912 * @source: a #GFile with attributes.
1913 * @destination: a #GFile to copy attributes to.
1914 * @flags: a set of #GFileCopyFlags.
1915 * @cancellable: optional #GCancellable object, %NULL to ignore.
1916 * @error: a #GError, %NULL to ignore.
1918 * Copies the file attributes from @source to @destination.
1920 * Normally only a subset of the file attributes are copied,
1921 * those that are copies in a normal file copy operation
1922 * (which for instance does not include e.g. mtime). However
1923 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
1924 * all the metadata that is possible to copy is copied.
1926 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1929 g_file_copy_attributes (GFile *source,
1931 GFileCopyFlags flags,
1932 GCancellable *cancellable,
1935 GFileAttributeInfoList *attributes, *namespaces;
1936 char *attrs_to_read;
1940 gboolean source_nofollow_symlinks;
1942 as_move = flags & G_FILE_COPY_ALL_METADATA;
1943 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1945 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1946 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1947 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1949 if (attributes == NULL && namespaces == NULL)
1952 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1954 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1955 * we just don't copy it.
1957 info = g_file_query_info (source, attrs_to_read,
1958 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1962 g_free (attrs_to_read);
1967 res = g_file_set_attributes_from_info (destination,
1971 g_object_unref (info);
1974 g_file_attribute_info_list_unref (attributes);
1975 g_file_attribute_info_list_unref (namespaces);
1980 /* Closes the streams */
1982 copy_stream_with_progress (GInputStream *in,
1984 GCancellable *cancellable,
1985 GFileProgressCallback progress_callback,
1986 gpointer progress_callback_data,
1989 gssize n_read, n_written;
1990 goffset current_size;
1991 char buffer[8192], *p;
1997 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1998 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2002 total_size = g_file_info_get_size (info);
2003 g_object_unref (info);
2010 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2020 current_size += n_read;
2025 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2026 if (n_written == -1)
2033 n_read -= n_written;
2039 if (progress_callback)
2040 progress_callback (current_size, total_size, progress_callback_data);
2044 error = NULL; /* Ignore further errors */
2046 /* Make sure we send full copied size */
2047 if (progress_callback)
2048 progress_callback (current_size, total_size, progress_callback_data);
2051 /* Don't care about errors in source here */
2052 g_input_stream_close (in, cancellable, NULL);
2054 /* But write errors on close are bad! */
2055 if (!g_output_stream_close (out, cancellable, error))
2058 g_object_unref (in);
2059 g_object_unref (out);
2065 file_copy_fallback (GFile *source,
2067 GFileCopyFlags flags,
2068 GCancellable *cancellable,
2069 GFileProgressCallback progress_callback,
2070 gpointer progress_callback_data,
2078 /* Maybe copy the symlink? */
2079 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2081 info = g_file_query_info (source,
2082 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2083 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2089 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2090 (target = g_file_info_get_symlink_target (info)) != NULL)
2092 if (!copy_symlink (destination, flags, cancellable, target, error))
2094 g_object_unref (info);
2098 g_object_unref (info);
2102 g_object_unref (info);
2105 in = open_source_for_copy (source, destination, flags, cancellable, error);
2109 if (flags & G_FILE_COPY_OVERWRITE)
2111 out = (GOutputStream *)g_file_replace (destination,
2113 flags & G_FILE_COPY_BACKUP,
2114 cancellable, error);
2118 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2123 g_object_unref (in);
2127 if (!copy_stream_with_progress (in, out, cancellable,
2128 progress_callback, progress_callback_data,
2134 /* Ignore errors here. Failure to copy metadata is not a hard error */
2135 g_file_copy_attributes (source, destination,
2136 flags, cancellable, NULL);
2143 * @source: input #GFile.
2144 * @destination: destination #GFile
2145 * @flags: set of #GFileCopyFlags
2146 * @cancellable: optional #GCancellable object, %NULL to ignore.
2147 * @progress_callback: function to callback with progress information
2148 * @progress_callback_data: user data to pass to @progress_callback
2149 * @error: #GError to set on error, or %NULL
2151 * Copies the file @source to the location specified by @destination.
2152 * Can not handle recursive copies of directories.
2154 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2155 * existing @destination file is overwritten.
2157 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2158 * will be copied as symlinks, otherwise the target of the
2159 * @source symlink will be copied.
2161 * If @cancellable is not %NULL, then the operation can be cancelled by
2162 * triggering the cancellable object from another thread. If the operation
2163 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2165 * If @progress_callback is not %NULL, then the operation can be monitored by
2166 * setting this to a #GFileProgressCallback function. @progress_callback_data
2167 * will be passed to this function. It is guaranteed that this callback will
2168 * be called after all data has been transferred with the total number of bytes
2169 * copied during the operation.
2171 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2172 * error is returned, independent on the status of the @destination.
2174 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2175 * error G_IO_ERROR_EXISTS is returned.
2177 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2178 * error is returned. If trying to overwrite a directory with a directory the
2179 * G_IO_ERROR_WOULD_MERGE error is returned.
2181 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2182 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2185 * If you are interested in copying the #GFile object itself (not the on-disk
2186 * file), see g_file_dup().
2188 * Returns: %TRUE on success, %FALSE otherwise.
2191 g_file_copy (GFile *source,
2193 GFileCopyFlags flags,
2194 GCancellable *cancellable,
2195 GFileProgressCallback progress_callback,
2196 gpointer progress_callback_data,
2203 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2204 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2206 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2209 iface = G_FILE_GET_IFACE (destination);
2213 res = (* iface->copy) (source, destination,
2215 progress_callback, progress_callback_data,
2221 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2223 g_propagate_error (error, my_error);
2228 /* If the types are different, and the destination method failed
2229 also try the source method */
2230 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2232 iface = G_FILE_GET_IFACE (source);
2237 res = (* iface->copy) (source, destination,
2239 progress_callback, progress_callback_data,
2245 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2247 g_propagate_error (error, my_error);
2253 return file_copy_fallback (source, destination, flags, cancellable,
2254 progress_callback, progress_callback_data,
2259 * g_file_copy_async:
2260 * @source: input #GFile.
2261 * @destination: destination #GFile
2262 * @flags: set of #GFileCopyFlags
2263 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2265 * @cancellable: optional #GCancellable object, %NULL to ignore.
2266 * @progress_callback: function to callback with progress information
2267 * @progress_callback_data: user data to pass to @progress_callback
2268 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2269 * @user_data: the data to pass to callback function
2271 * Copies the file @source to the location specified by @destination
2272 * asynchronously. For details of the behaviour, see g_file_copy().
2274 * If @progress_callback is not %NULL, then that function that will be called
2275 * just like in g_file_copy(), however the callback will run in the main loop,
2276 * not in the thread that is doing the I/O operation.
2278 * When the operation is finished, @callback will be called. You can then call
2279 * g_file_copy_finish() to get the result of the operation.
2282 g_file_copy_async (GFile *source,
2284 GFileCopyFlags flags,
2286 GCancellable *cancellable,
2287 GFileProgressCallback progress_callback,
2288 gpointer progress_callback_data,
2289 GAsyncReadyCallback callback,
2294 g_return_if_fail (G_IS_FILE (source));
2295 g_return_if_fail (G_IS_FILE (destination));
2297 iface = G_FILE_GET_IFACE (source);
2298 (* iface->copy_async) (source,
2304 progress_callback_data,
2310 * g_file_copy_finish:
2311 * @file: input #GFile.
2312 * @res: a #GAsyncResult.
2313 * @error: a #GError, or %NULL
2315 * Finishes copying the file started with
2316 * g_file_copy_async().
2318 * Returns: a %TRUE on success, %FALSE on error.
2321 g_file_copy_finish (GFile *file,
2327 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2328 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2330 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2332 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2334 if (g_simple_async_result_propagate_error (simple, error))
2338 iface = G_FILE_GET_IFACE (file);
2339 return (* iface->copy_finish) (file, res, error);
2344 * @source: #GFile pointing to the source location.
2345 * @destination: #GFile pointing to the destination location.
2346 * @flags: set of #GFileCopyFlags.
2347 * @cancellable: optional #GCancellable object, %NULL to ignore.
2348 * @progress_callback: #GFileProgressCallback function for updates.
2349 * @progress_callback_data: gpointer to user data for the callback function.
2350 * @error: #GError for returning error conditions, or %NULL
2353 * Tries to move the file or directory @source to the location specified by @destination.
2354 * If native move operations are supported then this is used, otherwise a copy + delete
2355 * fallback is used. The native implementation may support moving directories (for instance
2356 * on moves inside the same filesystem), but the fallback code does not.
2358 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2359 * existing @destination file is overwritten.
2361 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2362 * will be copied as symlinks, otherwise the target of the
2363 * @source symlink will be copied.
2365 * If @cancellable is not %NULL, then the operation can be cancelled by
2366 * triggering the cancellable object from another thread. If the operation
2367 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2369 * If @progress_callback is not %NULL, then the operation can be monitored by
2370 * setting this to a #GFileProgressCallback function. @progress_callback_data
2371 * will be passed to this function. It is guaranteed that this callback will
2372 * be called after all data has been transferred with the total number of bytes
2373 * copied during the operation.
2375 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2376 * error is returned, independent on the status of the @destination.
2378 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2379 * error G_IO_ERROR_EXISTS is returned.
2381 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2382 * error is returned. If trying to overwrite a directory with a directory the
2383 * G_IO_ERROR_WOULD_MERGE error is returned.
2385 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2386 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2387 * may be returned (if the native move operation isn't available).
2389 * Returns: %TRUE on successful move, %FALSE otherwise.
2392 g_file_move (GFile *source,
2394 GFileCopyFlags flags,
2395 GCancellable *cancellable,
2396 GFileProgressCallback progress_callback,
2397 gpointer progress_callback_data,
2404 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2405 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2407 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2410 iface = G_FILE_GET_IFACE (destination);
2414 res = (* iface->move) (source, destination,
2416 progress_callback, progress_callback_data,
2422 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2424 g_propagate_error (error, my_error);
2429 /* If the types are different, and the destination method failed
2430 also try the source method */
2431 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2433 iface = G_FILE_GET_IFACE (source);
2438 res = (* iface->move) (source, destination,
2440 progress_callback, progress_callback_data,
2446 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2448 g_propagate_error (error, my_error);
2454 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2456 g_set_error (error, G_IO_ERROR,
2457 G_IO_ERROR_NOT_SUPPORTED,
2458 _("Operation not supported"));
2462 flags |= G_FILE_COPY_ALL_METADATA;
2463 if (!g_file_copy (source, destination, flags, cancellable,
2464 progress_callback, progress_callback_data,
2468 return g_file_delete (source, cancellable, error);
2472 * g_file_make_directory
2473 * @file: input #GFile.
2474 * @cancellable: optional #GCancellable object, %NULL to ignore.
2475 * @error: a #GError, or %NULL
2477 * Creates a directory.
2479 * If @cancellable is not %NULL, then the operation can be cancelled by
2480 * triggering the cancellable object from another thread. If the operation
2481 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2483 * Returns: %TRUE on successful creation, %FALSE otherwise.
2486 g_file_make_directory (GFile *file,
2487 GCancellable *cancellable,
2492 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2494 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2497 iface = G_FILE_GET_IFACE (file);
2499 if (iface->make_directory == NULL)
2501 g_set_error (error, G_IO_ERROR,
2502 G_IO_ERROR_NOT_SUPPORTED,
2503 _("Operation not supported"));
2507 return (* iface->make_directory) (file, cancellable, error);
2511 * g_file_make_symbolic_link:
2512 * @file: input #GFile.
2513 * @symlink_value: a string with the value of the new symlink.
2514 * @cancellable: optional #GCancellable object, %NULL to ignore.
2515 * @error: a #GError.
2517 * Creates a symbolic link.
2519 * If @cancellable is not %NULL, then the operation can be cancelled by
2520 * triggering the cancellable object from another thread. If the operation
2521 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2523 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2526 g_file_make_symbolic_link (GFile *file,
2527 const char *symlink_value,
2528 GCancellable *cancellable,
2533 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2534 g_return_val_if_fail (symlink_value != NULL, FALSE);
2536 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2539 if (*symlink_value == '\0')
2541 g_set_error (error, G_IO_ERROR,
2542 G_IO_ERROR_INVALID_ARGUMENT,
2543 _("Invalid symlink value given"));
2547 iface = G_FILE_GET_IFACE (file);
2549 if (iface->make_symbolic_link == NULL)
2551 g_set_error (error, G_IO_ERROR,
2552 G_IO_ERROR_NOT_SUPPORTED,
2553 _("Operation not supported"));
2557 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2562 * @file: input #GFile.
2563 * @cancellable: optional #GCancellable object, %NULL to ignore.
2564 * @error: a #GError, or %NULL
2568 * If @cancellable is not %NULL, then the operation can be cancelled by
2569 * triggering the cancellable object from another thread. If the operation
2570 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2572 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2575 g_file_delete (GFile *file,
2576 GCancellable *cancellable,
2581 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2583 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2586 iface = G_FILE_GET_IFACE (file);
2588 if (iface->delete_file == NULL)
2590 g_set_error (error, G_IO_ERROR,
2591 G_IO_ERROR_NOT_SUPPORTED,
2592 _("Operation not supported"));
2596 return (* iface->delete_file) (file, cancellable, error);
2601 * @file: #GFile to send to trash.
2602 * @cancellable: optional #GCancellable object, %NULL to ignore.
2603 * @error: a #GError, or %NULL
2605 * Sends @file to the "Trashcan", if possible. This is similar to
2606 * deleting it, but the user can recover it before emptying the trashcan.
2607 * Not all file systems support trashing, so this call can return the
2608 * %G_IO_ERROR_NOT_SUPPORTED error.
2611 * If @cancellable is not %NULL, then the operation can be cancelled by
2612 * triggering the cancellable object from another thread. If the operation
2613 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2615 * Returns: %TRUE on successful trash, %FALSE otherwise.
2618 g_file_trash (GFile *file,
2619 GCancellable *cancellable,
2624 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2626 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2629 iface = G_FILE_GET_IFACE (file);
2631 if (iface->trash == NULL)
2634 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2635 _("Trash not supported"));
2639 return (* iface->trash) (file, cancellable, error);
2643 * g_file_set_display_name:
2644 * @file: input #GFile.
2645 * @display_name: a string.
2646 * @cancellable: optional #GCancellable object, %NULL to ignore.
2647 * @error: a #GError, or %NULL
2649 * Renames @file to the specified display name.
2651 * The display name is converted from UTF8 to the correct encoding for the target
2652 * filesystem if possible and the @file is renamed to this.
2654 * If you want to implement a rename operation in the user interface the edit name
2655 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2656 * widget, and then the result after editing should be passed to g_file_set_display_name().
2658 * On success the resulting converted filename is returned.
2660 * If @cancellable is not %NULL, then the operation can be cancelled by
2661 * triggering the cancellable object from another thread. If the operation
2662 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2664 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2667 g_file_set_display_name (GFile *file,
2668 const char *display_name,
2669 GCancellable *cancellable,
2674 g_return_val_if_fail (G_IS_FILE (file), NULL);
2675 g_return_val_if_fail (display_name != NULL, NULL);
2677 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2681 G_IO_ERROR_INVALID_ARGUMENT,
2682 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2686 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2689 iface = G_FILE_GET_IFACE (file);
2691 return (* iface->set_display_name) (file, display_name, cancellable, error);
2695 * g_file_set_display_name_async:
2696 * @file: input #GFile.
2697 * @display_name: a string.
2698 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2700 * @cancellable: optional #GCancellable object, %NULL to ignore.
2701 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2702 * @user_data: the data to pass to callback function
2704 * Asynchronously sets the display name for a given #GFile.
2706 * For more details, see g_set_display_name() which is
2707 * the synchronous version of this call.
2709 * When the operation is finished, @callback will be called. You can then call
2710 * g_file_set_display_name_finish() to get the result of the operation.
2713 g_file_set_display_name_async (GFile *file,
2714 const char *display_name,
2716 GCancellable *cancellable,
2717 GAsyncReadyCallback callback,
2722 g_return_if_fail (G_IS_FILE (file));
2723 g_return_if_fail (display_name != NULL);
2725 iface = G_FILE_GET_IFACE (file);
2726 (* iface->set_display_name_async) (file,
2735 * g_file_set_display_name_finish:
2736 * @file: input #GFile.
2737 * @res: a #GAsyncResult.
2738 * @error: a #GError, or %NULL
2740 * Finishes setting a display name started with
2741 * g_file_set_display_name_async().
2743 * Returns: a #GFile or %NULL on error.
2746 g_file_set_display_name_finish (GFile *file,
2752 g_return_val_if_fail (G_IS_FILE (file), NULL);
2753 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2755 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2757 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2758 if (g_simple_async_result_propagate_error (simple, error))
2762 iface = G_FILE_GET_IFACE (file);
2763 return (* iface->set_display_name_finish) (file, res, error);
2767 * g_file_query_settable_attributes:
2768 * @file: input #GFile.
2769 * @cancellable: optional #GCancellable object, %NULL to ignore.
2770 * @error: a #GError, or %NULL
2772 * Obtain the list of settable attributes for the file.
2774 * Returns the type and full attribute name of all the attributes
2775 * that can be set on this file. This doesn't mean setting it will always
2776 * succeed though, you might get an access failure, or some specific
2777 * file may not support a specific attribute.
2779 * If @cancellable is not %NULL, then the operation can be cancelled by
2780 * triggering the cancellable object from another thread. If the operation
2781 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2783 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2784 * When you are done with it, release it with g_file_attribute_info_list_unref()
2786 GFileAttributeInfoList *
2787 g_file_query_settable_attributes (GFile *file,
2788 GCancellable *cancellable,
2793 GFileAttributeInfoList *list;
2795 g_return_val_if_fail (G_IS_FILE (file), NULL);
2797 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2800 iface = G_FILE_GET_IFACE (file);
2802 if (iface->query_settable_attributes == NULL)
2803 return g_file_attribute_info_list_new ();
2806 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2810 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2812 list = g_file_attribute_info_list_new ();
2813 g_error_free (my_error);
2816 g_propagate_error (error, my_error);
2823 * g_file_query_writable_namespaces:
2824 * @file: input #GFile.
2825 * @cancellable: optional #GCancellable object, %NULL to ignore.
2826 * @error: a #GError, or %NULL
2828 * Obtain the list of attribute namespaces where new attributes
2829 * can be created by a user. An example of this is extended
2830 * attributes (in the "xattr" namespace).
2832 * If @cancellable is not %NULL, then the operation can be cancelled by
2833 * triggering the cancellable object from another thread. If the operation
2834 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2836 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2837 * When you are done with it, release it with g_file_attribute_info_list_unref()
2839 GFileAttributeInfoList *
2840 g_file_query_writable_namespaces (GFile *file,
2841 GCancellable *cancellable,
2846 GFileAttributeInfoList *list;
2848 g_return_val_if_fail (G_IS_FILE (file), NULL);
2850 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2853 iface = G_FILE_GET_IFACE (file);
2855 if (iface->query_writable_namespaces == NULL)
2856 return g_file_attribute_info_list_new ();
2859 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2863 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2865 list = g_file_attribute_info_list_new ();
2866 g_error_free (my_error);
2869 g_propagate_error (error, my_error);
2876 * g_file_set_attribute:
2877 * @file: input #GFile.
2878 * @attribute: a string containing the attribute's name.
2879 * @type: The type of the attribute
2880 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2881 * @flags: a set of #GFileQueryInfoFlags.
2882 * @cancellable: optional #GCancellable object, %NULL to ignore.
2883 * @error: a #GError, or %NULL
2885 * Sets an attribute in the file with attribute name @attribute to @value.
2887 * If @cancellable is not %NULL, then the operation can be cancelled by
2888 * triggering the cancellable object from another thread. If the operation
2889 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2891 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2894 g_file_set_attribute (GFile *file,
2895 const char *attribute,
2896 GFileAttributeType type,
2898 GFileQueryInfoFlags flags,
2899 GCancellable *cancellable,
2904 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2905 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2907 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2910 iface = G_FILE_GET_IFACE (file);
2912 if (iface->set_attribute == NULL)
2914 g_set_error (error, G_IO_ERROR,
2915 G_IO_ERROR_NOT_SUPPORTED,
2916 _("Operation not supported"));
2920 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2924 * g_file_set_attributes_from_info:
2925 * @file: input #GFile.
2926 * @info: a #GFileInfo.
2927 * @flags: #GFileQueryInfoFlags
2928 * @cancellable: optional #GCancellable object, %NULL to ignore.
2929 * @error: a #GError, or %NULL
2931 * Tries to set all attributes in the #GFileInfo on the target values,
2932 * not stopping on the first error.
2934 * If there is any error during this operation then @error will be set to
2935 * the first error. Error on particular fields are flagged by setting
2936 * the "status" field in the attribute value to
2937 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2940 * If @cancellable is not %NULL, then the operation can be cancelled by
2941 * triggering the cancellable object from another thread. If the operation
2942 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2944 * Returns: %TRUE if there was any error, %FALSE otherwise.
2947 g_file_set_attributes_from_info (GFile *file,
2949 GFileQueryInfoFlags flags,
2950 GCancellable *cancellable,
2955 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2956 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2958 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2961 g_file_info_clear_status (info);
2963 iface = G_FILE_GET_IFACE (file);
2965 return (* iface->set_attributes_from_info) (file,
2974 g_file_real_set_attributes_from_info (GFile *file,
2976 GFileQueryInfoFlags flags,
2977 GCancellable *cancellable,
2983 GFileAttributeValue *value;
2987 attributes = g_file_info_list_attributes (info, NULL);
2989 for (i = 0; attributes[i] != NULL; i++)
2991 value = _g_file_info_get_attribute_value (info, attributes[i]);
2993 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2996 if (!g_file_set_attribute (file, attributes[i],
2997 value->type, _g_file_attribute_value_peek_as_pointer (value),
2998 flags, cancellable, error))
3000 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3002 /* Don't set error multiple times */
3006 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3009 g_strfreev (attributes);
3015 * g_file_set_attributes_async:
3016 * @file: input #GFile.
3017 * @info: a #GFileInfo.
3018 * @flags: a #GFileQueryInfoFlags.
3019 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3021 * @cancellable: optional #GCancellable object, %NULL to ignore.
3022 * @callback: a #GAsyncReadyCallback.
3023 * @user_data: a #gpointer.
3025 * Asynchronously sets the attributes of @file with @info.
3027 * For more details, see g_file_set_attributes_from_info() which is
3028 * the synchronous version of this call.
3030 * When the operation is finished, @callback will be called. You can then call
3031 * g_file_set_attributes_finish() to get the result of the operation.
3034 g_file_set_attributes_async (GFile *file,
3036 GFileQueryInfoFlags flags,
3038 GCancellable *cancellable,
3039 GAsyncReadyCallback callback,
3044 g_return_if_fail (G_IS_FILE (file));
3045 g_return_if_fail (G_IS_FILE_INFO (info));
3047 iface = G_FILE_GET_IFACE (file);
3048 (* iface->set_attributes_async) (file,
3058 * g_file_set_attributes_finish:
3059 * @file: input #GFile.
3060 * @result: a #GAsyncResult.
3061 * @info: a #GFileInfo.
3062 * @error: a #GError, or %NULL
3064 * Finishes setting an attribute started in g_file_set_attributes_async().
3066 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3069 g_file_set_attributes_finish (GFile *file,
3070 GAsyncResult *result,
3076 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3077 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3079 /* No standard handling of errors here, as we must set info even
3082 iface = G_FILE_GET_IFACE (file);
3083 return (* iface->set_attributes_finish) (file, result, info, error);
3087 * g_file_set_attribute_string:
3088 * @file: input #GFile.
3089 * @attribute: a string containing the attribute's name.
3090 * @value: a string containing the attribute's value.
3091 * @flags: #GFileQueryInfoFlags.
3092 * @cancellable: optional #GCancellable object, %NULL to ignore.
3093 * @error: a #GError, or %NULL
3095 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3096 * If @attribute is of a different type, this operation will fail.
3098 * If @cancellable is not %NULL, then the operation can be cancelled by
3099 * triggering the cancellable object from another thread. If the operation
3100 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3102 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3105 g_file_set_attribute_string (GFile *file,
3106 const char *attribute,
3108 GFileQueryInfoFlags flags,
3109 GCancellable *cancellable,
3112 return g_file_set_attribute (file, attribute,
3113 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3114 flags, cancellable, error);
3118 * g_file_set_attribute_byte_string:
3119 * @file: input #GFile.
3120 * @attribute: a string containing the attribute's name.
3121 * @value: a string containing the attribute's new value.
3122 * @flags: a #GFileQueryInfoFlags.
3123 * @cancellable: optional #GCancellable object, %NULL to ignore.
3124 * @error: a #GError, or %NULL
3126 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3127 * If @attribute is of a different type, this operation will fail,
3130 * If @cancellable is not %NULL, then the operation can be cancelled by
3131 * triggering the cancellable object from another thread. If the operation
3132 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3134 * Returns: %TRUE if the @attribute was successfully set to @value
3135 * in the @file, %FALSE otherwise.
3138 g_file_set_attribute_byte_string (GFile *file,
3139 const char *attribute,
3141 GFileQueryInfoFlags flags,
3142 GCancellable *cancellable,
3145 return g_file_set_attribute (file, attribute,
3146 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3147 flags, cancellable, error);
3151 * g_file_set_attribute_uint32:
3152 * @file: input #GFile.
3153 * @attribute: a string containing the attribute's name.
3154 * @value: a #guint32 containing the attribute's new value.
3155 * @flags: a #GFileQueryInfoFlags.
3156 * @cancellable: optional #GCancellable object, %NULL to ignore.
3157 * @error: a #GError, or %NULL
3159 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3160 * If @attribute is of a different type, this operation will fail.
3162 * If @cancellable is not %NULL, then the operation can be cancelled by
3163 * triggering the cancellable object from another thread. If the operation
3164 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3166 * Returns: %TRUE if the @attribute was successfully set to @value
3167 * in the @file, %FALSE otherwise.
3170 g_file_set_attribute_uint32 (GFile *file,
3171 const char *attribute,
3173 GFileQueryInfoFlags flags,
3174 GCancellable *cancellable,
3177 return g_file_set_attribute (file, attribute,
3178 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3179 flags, cancellable, error);
3183 * g_file_set_attribute_int32:
3184 * @file: input #GFile.
3185 * @attribute: a string containing the attribute's name.
3186 * @value: a #gint32 containing the attribute's new value.
3187 * @flags: a #GFileQueryInfoFlags.
3188 * @cancellable: optional #GCancellable object, %NULL to ignore.
3189 * @error: a #GError, or %NULL
3191 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3192 * If @attribute is of a different type, this operation will fail.
3194 * If @cancellable is not %NULL, then the operation can be cancelled by
3195 * triggering the cancellable object from another thread. If the operation
3196 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3198 * Returns: %TRUE if the @attribute was successfully set to @value
3199 * in the @file, %FALSE otherwise.
3202 g_file_set_attribute_int32 (GFile *file,
3203 const char *attribute,
3205 GFileQueryInfoFlags flags,
3206 GCancellable *cancellable,
3209 return g_file_set_attribute (file, attribute,
3210 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3211 flags, cancellable, error);
3215 * g_file_set_attribute_uint64:
3216 * @file: input #GFile.
3217 * @attribute: a string containing the attribute's name.
3218 * @value: a #guint64 containing the attribute's new value.
3219 * @flags: a #GFileQueryInfoFlags.
3220 * @cancellable: optional #GCancellable object, %NULL to ignore.
3221 * @error: a #GError, or %NULL
3223 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3224 * If @attribute is of a different type, this operation will fail.
3226 * If @cancellable is not %NULL, then the operation can be cancelled by
3227 * triggering the cancellable object from another thread. If the operation
3228 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3230 * Returns: %TRUE if the @attribute was successfully set to @value
3231 * in the @file, %FALSE otherwise.
3234 g_file_set_attribute_uint64 (GFile *file,
3235 const char *attribute,
3237 GFileQueryInfoFlags flags,
3238 GCancellable *cancellable,
3241 return g_file_set_attribute (file, attribute,
3242 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3243 flags, cancellable, error);
3247 * g_file_set_attribute_int64:
3248 * @file: input #GFile.
3249 * @attribute: a string containing the attribute's name.
3250 * @value: a #guint64 containing the attribute's new value.
3251 * @flags: a #GFileQueryInfoFlags.
3252 * @cancellable: optional #GCancellable object, %NULL to ignore.
3253 * @error: a #GError, or %NULL
3255 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3256 * If @attribute is of a different type, this operation will fail.
3258 * If @cancellable is not %NULL, then the operation can be cancelled by
3259 * triggering the cancellable object from another thread. If the operation
3260 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3262 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3265 g_file_set_attribute_int64 (GFile *file,
3266 const char *attribute,
3268 GFileQueryInfoFlags flags,
3269 GCancellable *cancellable,
3272 return g_file_set_attribute (file, attribute,
3273 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3274 flags, cancellable, error);
3278 * g_file_mount_mountable:
3279 * @file: input #GFile.
3280 * @flags: flags affecting the operation
3281 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3282 * @cancellable: optional #GCancellable object, %NULL to ignore.
3283 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3284 * @user_data: the data to pass to callback function
3286 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3287 * Using @mount_operation, you can request callbacks when, for instance,
3288 * passwords are needed during authentication.
3290 * If @cancellable is not %NULL, then the operation can be cancelled by
3291 * triggering the cancellable object from another thread. If the operation
3292 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3294 * When the operation is finished, @callback will be called. You can then call
3295 * g_file_mount_mountable_finish() to get the result of the operation.
3298 g_file_mount_mountable (GFile *file,
3299 GMountMountFlags flags,
3300 GMountOperation *mount_operation,
3301 GCancellable *cancellable,
3302 GAsyncReadyCallback callback,
3307 g_return_if_fail (G_IS_FILE (file));
3309 iface = G_FILE_GET_IFACE (file);
3311 if (iface->mount_mountable == NULL)
3312 g_simple_async_report_error_in_idle (G_OBJECT (file),
3316 G_IO_ERROR_NOT_SUPPORTED,
3317 _("Operation not supported"));
3319 (* iface->mount_mountable) (file,
3328 * g_file_mount_mountable_finish:
3329 * @file: input #GFile.
3330 * @result: a #GAsyncResult.
3331 * @error: a #GError, or %NULL
3333 * Finishes a mount operation. See g_file_mount_mountable() for details.
3335 * Finish an asynchronous mount operation that was started
3336 * with g_file_mount_mountable().
3338 * Returns: a #GFile or %NULL on error.
3341 g_file_mount_mountable_finish (GFile *file,
3342 GAsyncResult *result,
3347 g_return_val_if_fail (G_IS_FILE (file), NULL);
3348 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3350 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3352 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3353 if (g_simple_async_result_propagate_error (simple, error))
3357 iface = G_FILE_GET_IFACE (file);
3358 return (* iface->mount_mountable_finish) (file, result, error);
3362 * g_file_unmount_mountable:
3363 * @file: input #GFile.
3364 * @flags: flags affecting the operation
3365 * @cancellable: optional #GCancellable object, %NULL to ignore.
3366 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3367 * @user_data: the data to pass to callback function
3369 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3371 * If @cancellable is not %NULL, then the operation can be cancelled by
3372 * triggering the cancellable object from another thread. If the operation
3373 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3375 * When the operation is finished, @callback will be called. You can then call
3376 * g_file_unmount_mountable_finish() to get the result of the operation.
3379 g_file_unmount_mountable (GFile *file,
3380 GMountUnmountFlags flags,
3381 GCancellable *cancellable,
3382 GAsyncReadyCallback callback,
3387 g_return_if_fail (G_IS_FILE (file));
3389 iface = G_FILE_GET_IFACE (file);
3391 if (iface->unmount_mountable == NULL)
3392 g_simple_async_report_error_in_idle (G_OBJECT (file),
3396 G_IO_ERROR_NOT_SUPPORTED,
3397 _("Operation not supported"));
3399 (* iface->unmount_mountable) (file,
3407 * g_file_unmount_mountable_finish:
3408 * @file: input #GFile.
3409 * @result: a #GAsyncResult.
3410 * @error: a #GError, or %NULL
3412 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3414 * Finish an asynchronous unmount operation that was started
3415 * with g_file_unmount_mountable().
3417 * Returns: %TRUE if the operation finished successfully. %FALSE
3421 g_file_unmount_mountable_finish (GFile *file,
3422 GAsyncResult *result,
3427 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3428 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3430 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3432 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3433 if (g_simple_async_result_propagate_error (simple, error))
3437 iface = G_FILE_GET_IFACE (file);
3438 return (* iface->unmount_mountable_finish) (file, result, error);
3442 * g_file_eject_mountable:
3443 * @file: input #GFile.
3444 * @flags: flags affecting the operation
3445 * @cancellable: optional #GCancellable object, %NULL to ignore.
3446 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3447 * @user_data: the data to pass to callback function
3449 * Starts an asynchronous eject on a mountable.
3450 * When this operation has completed, @callback will be called with
3451 * @user_user data, and the operation can be finalized with
3452 * g_file_eject_mountable_finish().
3454 * If @cancellable is not %NULL, then the operation can be cancelled by
3455 * triggering the cancellable object from another thread. If the operation
3456 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3459 g_file_eject_mountable (GFile *file,
3460 GMountUnmountFlags flags,
3461 GCancellable *cancellable,
3462 GAsyncReadyCallback callback,
3467 g_return_if_fail (G_IS_FILE (file));
3469 iface = G_FILE_GET_IFACE (file);
3471 if (iface->eject_mountable == NULL)
3472 g_simple_async_report_error_in_idle (G_OBJECT (file),
3476 G_IO_ERROR_NOT_SUPPORTED,
3477 _("Operation not supported"));
3479 (* iface->eject_mountable) (file,
3487 * g_file_eject_mountable_finish:
3488 * @file: input #GFile.
3489 * @result: a #GAsyncResult.
3490 * @error: a #GError, or %NULL
3492 * Finishes an asynchronous eject operation started by
3493 * g_file_eject_mountable().
3495 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3499 g_file_eject_mountable_finish (GFile *file,
3500 GAsyncResult *result,
3505 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3506 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3508 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3510 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3511 if (g_simple_async_result_propagate_error (simple, error))
3515 iface = G_FILE_GET_IFACE (file);
3516 return (* iface->eject_mountable_finish) (file, result, error);
3520 * g_file_monitor_directory:
3521 * @file: input #GFile.
3522 * @flags: a set of #GFileMonitorFlags.
3523 * @cancellable: optional #GCancellable object, %NULL to ignore.
3524 * @error: a #GError, or %NULL.
3526 * Obtains a directory monitor for the given file.
3527 * This may fail if directory monitoring is not supported.
3529 * If @cancellable is not %NULL, then the operation can be cancelled by
3530 * triggering the cancellable object from another thread. If the operation
3531 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3533 * Returns: a #GFileMonitor for the given @file,
3534 * or %NULL on error.
3537 g_file_monitor_directory (GFile *file,
3538 GFileMonitorFlags flags,
3539 GCancellable *cancellable,
3544 g_return_val_if_fail (G_IS_FILE (file), NULL);
3546 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3549 iface = G_FILE_GET_IFACE (file);
3551 if (iface->monitor_dir == NULL)
3553 g_set_error (error, G_IO_ERROR,
3554 G_IO_ERROR_NOT_SUPPORTED,
3555 _("Operation not supported"));
3559 return (* iface->monitor_dir) (file, flags, cancellable, error);
3563 * g_file_monitor_file:
3564 * @file: input #GFile.
3565 * @flags: a set of #GFileMonitorFlags.
3566 * @cancellable: optional #GCancellable object, %NULL to ignore.
3567 * @error: a #GError, or %NULL.
3569 * Obtains a file monitor for the given file. If no file notification
3570 * mechanism exists, then regular polling of the file is used.
3572 * If @cancellable is not %NULL, then the operation can be cancelled by
3573 * triggering the cancellable object from another thread. If the operation
3574 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3576 * Returns: a #GFileMonitor for the given @file.
3579 g_file_monitor_file (GFile *file,
3580 GFileMonitorFlags flags,
3581 GCancellable *cancellable,
3585 GFileMonitor *monitor;
3587 g_return_val_if_fail (G_IS_FILE (file), NULL);
3589 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3592 iface = G_FILE_GET_IFACE (file);
3596 if (iface->monitor_file)
3597 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3599 /* Fallback to polling */
3600 if (monitor == NULL)
3601 monitor = _g_poll_file_monitor_new (file);
3606 /********************************************
3607 * Default implementation of async ops *
3608 ********************************************/
3612 GFileQueryInfoFlags flags;
3614 } QueryInfoAsyncData;
3617 query_info_data_free (QueryInfoAsyncData *data)
3620 g_object_unref (data->info);
3621 g_free (data->attributes);
3626 query_info_async_thread (GSimpleAsyncResult *res,
3628 GCancellable *cancellable)
3630 GError *error = NULL;
3631 QueryInfoAsyncData *data;
3634 data = g_simple_async_result_get_op_res_gpointer (res);
3636 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3640 g_simple_async_result_set_from_error (res, error);
3641 g_error_free (error);
3648 g_file_real_query_info_async (GFile *file,
3649 const char *attributes,
3650 GFileQueryInfoFlags flags,
3652 GCancellable *cancellable,
3653 GAsyncReadyCallback callback,
3656 GSimpleAsyncResult *res;
3657 QueryInfoAsyncData *data;
3659 data = g_new0 (QueryInfoAsyncData, 1);
3660 data->attributes = g_strdup (attributes);
3661 data->flags = flags;
3663 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3664 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3666 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3667 g_object_unref (res);
3671 g_file_real_query_info_finish (GFile *file,
3675 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3676 QueryInfoAsyncData *data;
3678 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3680 data = g_simple_async_result_get_op_res_gpointer (simple);
3682 return g_object_ref (data->info);
3689 GFileQueryInfoFlags flags;
3690 GFileEnumerator *enumerator;
3691 } EnumerateChildrenAsyncData;
3694 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3696 if (data->enumerator)
3697 g_object_unref (data->enumerator);
3698 g_free (data->attributes);
3703 enumerate_children_async_thread (GSimpleAsyncResult *res,
3705 GCancellable *cancellable)
3707 GError *error = NULL;
3708 EnumerateChildrenAsyncData *data;
3709 GFileEnumerator *enumerator;
3711 data = g_simple_async_result_get_op_res_gpointer (res);
3713 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3715 if (enumerator == NULL)
3717 g_simple_async_result_set_from_error (res, error);
3718 g_error_free (error);
3721 data->enumerator = enumerator;
3725 g_file_real_enumerate_children_async (GFile *file,
3726 const char *attributes,
3727 GFileQueryInfoFlags flags,
3729 GCancellable *cancellable,
3730 GAsyncReadyCallback callback,
3733 GSimpleAsyncResult *res;
3734 EnumerateChildrenAsyncData *data;
3736 data = g_new0 (EnumerateChildrenAsyncData, 1);
3737 data->attributes = g_strdup (attributes);
3738 data->flags = flags;
3740 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3741 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3743 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3744 g_object_unref (res);
3747 static GFileEnumerator *
3748 g_file_real_enumerate_children_finish (GFile *file,
3752 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3753 EnumerateChildrenAsyncData *data;
3755 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3757 data = g_simple_async_result_get_op_res_gpointer (simple);
3758 if (data->enumerator)
3759 return g_object_ref (data->enumerator);
3765 open_read_async_thread (GSimpleAsyncResult *res,
3767 GCancellable *cancellable)
3770 GFileInputStream *stream;
3771 GError *error = NULL;
3773 iface = G_FILE_GET_IFACE (object);
3775 stream = iface->read_fn (G_FILE (object), cancellable, &error);
3779 g_simple_async_result_set_from_error (res, error);
3780 g_error_free (error);
3783 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3787 g_file_real_read_async (GFile *file,
3789 GCancellable *cancellable,
3790 GAsyncReadyCallback callback,
3793 GSimpleAsyncResult *res;
3795 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3797 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3798 g_object_unref (res);
3801 static GFileInputStream *
3802 g_file_real_read_finish (GFile *file,
3806 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3809 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3811 op = g_simple_async_result_get_op_res_gpointer (simple);
3813 return g_object_ref (op);
3819 append_to_async_thread (GSimpleAsyncResult *res,
3821 GCancellable *cancellable)
3824 GFileCreateFlags *data;
3825 GFileOutputStream *stream;
3826 GError *error = NULL;
3828 iface = G_FILE_GET_IFACE (object);
3830 data = g_simple_async_result_get_op_res_gpointer (res);
3832 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3836 g_simple_async_result_set_from_error (res, error);
3837 g_error_free (error);
3840 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3844 g_file_real_append_to_async (GFile *file,
3845 GFileCreateFlags flags,
3847 GCancellable *cancellable,
3848 GAsyncReadyCallback callback,
3851 GFileCreateFlags *data;
3852 GSimpleAsyncResult *res;
3854 data = g_new0 (GFileCreateFlags, 1);
3857 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3858 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3860 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3861 g_object_unref (res);
3864 static GFileOutputStream *
3865 g_file_real_append_to_finish (GFile *file,
3869 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3872 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3874 op = g_simple_async_result_get_op_res_gpointer (simple);
3876 return g_object_ref (op);
3882 create_async_thread (GSimpleAsyncResult *res,
3884 GCancellable *cancellable)
3887 GFileCreateFlags *data;
3888 GFileOutputStream *stream;
3889 GError *error = NULL;
3891 iface = G_FILE_GET_IFACE (object);
3893 data = g_simple_async_result_get_op_res_gpointer (res);
3895 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3899 g_simple_async_result_set_from_error (res, error);
3900 g_error_free (error);
3903 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3907 g_file_real_create_async (GFile *file,
3908 GFileCreateFlags flags,
3910 GCancellable *cancellable,
3911 GAsyncReadyCallback callback,
3914 GFileCreateFlags *data;
3915 GSimpleAsyncResult *res;
3917 data = g_new0 (GFileCreateFlags, 1);
3920 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3921 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3923 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3924 g_object_unref (res);
3927 static GFileOutputStream *
3928 g_file_real_create_finish (GFile *file,
3932 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3935 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3937 op = g_simple_async_result_get_op_res_gpointer (simple);
3939 return g_object_ref (op);
3945 GFileOutputStream *stream;
3947 gboolean make_backup;
3948 GFileCreateFlags flags;
3952 replace_async_data_free (ReplaceAsyncData *data)
3955 g_object_unref (data->stream);
3956 g_free (data->etag);
3961 replace_async_thread (GSimpleAsyncResult *res,
3963 GCancellable *cancellable)
3966 GFileOutputStream *stream;
3967 GError *error = NULL;
3968 ReplaceAsyncData *data;
3970 iface = G_FILE_GET_IFACE (object);
3972 data = g_simple_async_result_get_op_res_gpointer (res);
3974 stream = iface->replace (G_FILE (object),
3983 g_simple_async_result_set_from_error (res, error);
3984 g_error_free (error);
3987 data->stream = stream;
3991 g_file_real_replace_async (GFile *file,
3993 gboolean make_backup,
3994 GFileCreateFlags flags,
3996 GCancellable *cancellable,
3997 GAsyncReadyCallback callback,
4000 GSimpleAsyncResult *res;
4001 ReplaceAsyncData *data;
4003 data = g_new0 (ReplaceAsyncData, 1);
4004 data->etag = g_strdup (etag);
4005 data->make_backup = make_backup;
4006 data->flags = flags;
4008 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4009 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4011 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4012 g_object_unref (res);
4015 static GFileOutputStream *
4016 g_file_real_replace_finish (GFile *file,
4020 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4021 ReplaceAsyncData *data;
4023 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4025 data = g_simple_async_result_get_op_res_gpointer (simple);
4027 return g_object_ref (data->stream);
4035 } SetDisplayNameAsyncData;
4038 set_display_name_data_free (SetDisplayNameAsyncData *data)
4040 g_free (data->name);
4042 g_object_unref (data->file);
4047 set_display_name_async_thread (GSimpleAsyncResult *res,
4049 GCancellable *cancellable)
4051 GError *error = NULL;
4052 SetDisplayNameAsyncData *data;
4055 data = g_simple_async_result_get_op_res_gpointer (res);
4057 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4061 g_simple_async_result_set_from_error (res, error);
4062 g_error_free (error);
4069 g_file_real_set_display_name_async (GFile *file,
4070 const char *display_name,
4072 GCancellable *cancellable,
4073 GAsyncReadyCallback callback,
4076 GSimpleAsyncResult *res;
4077 SetDisplayNameAsyncData *data;
4079 data = g_new0 (SetDisplayNameAsyncData, 1);
4080 data->name = g_strdup (display_name);
4082 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4083 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4085 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4086 g_object_unref (res);
4090 g_file_real_set_display_name_finish (GFile *file,
4094 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4095 SetDisplayNameAsyncData *data;
4097 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4099 data = g_simple_async_result_get_op_res_gpointer (simple);
4101 return g_object_ref (data->file);
4107 GFileQueryInfoFlags flags;
4114 set_info_data_free (SetInfoAsyncData *data)
4117 g_object_unref (data->info);
4119 g_error_free (data->error);
4124 set_info_async_thread (GSimpleAsyncResult *res,
4126 GCancellable *cancellable)
4128 SetInfoAsyncData *data;
4130 data = g_simple_async_result_get_op_res_gpointer (res);
4133 data->res = g_file_set_attributes_from_info (G_FILE (object),
4141 g_file_real_set_attributes_async (GFile *file,
4143 GFileQueryInfoFlags flags,
4145 GCancellable *cancellable,
4146 GAsyncReadyCallback callback,
4149 GSimpleAsyncResult *res;
4150 SetInfoAsyncData *data;
4152 data = g_new0 (SetInfoAsyncData, 1);
4153 data->info = g_file_info_dup (info);
4154 data->flags = flags;
4156 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4157 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4159 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4160 g_object_unref (res);
4164 g_file_real_set_attributes_finish (GFile *file,
4169 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4170 SetInfoAsyncData *data;
4172 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4174 data = g_simple_async_result_get_op_res_gpointer (simple);
4177 *info = g_object_ref (data->info);
4179 if (error != NULL && data->error)
4180 *error = g_error_copy (data->error);
4186 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4188 GCancellable *cancellable)
4190 GError *error = NULL;
4193 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4197 g_simple_async_result_set_from_error (res, error);
4198 g_error_free (error);
4201 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4205 g_file_real_find_enclosing_mount_async (GFile *file,
4207 GCancellable *cancellable,
4208 GAsyncReadyCallback callback,
4211 GSimpleAsyncResult *res;
4213 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4215 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4216 g_object_unref (res);
4220 g_file_real_find_enclosing_mount_finish (GFile *file,
4224 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4227 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4229 mount = g_simple_async_result_get_op_res_gpointer (simple);
4230 return g_object_ref (mount);
4237 GFileCopyFlags flags;
4238 GFileProgressCallback progress_cb;
4239 gpointer progress_cb_data;
4240 GIOSchedulerJob *job;
4244 copy_async_data_free (CopyAsyncData *data)
4246 g_object_unref (data->source);
4247 g_object_unref (data->destination);
4252 CopyAsyncData *data;
4253 goffset current_num_bytes;
4254 goffset total_num_bytes;
4258 copy_async_progress_in_main (gpointer user_data)
4260 ProgressData *progress = user_data;
4261 CopyAsyncData *data = progress->data;
4263 data->progress_cb (progress->current_num_bytes,
4264 progress->total_num_bytes,
4265 data->progress_cb_data);
4271 mainloop_barrier (gpointer user_data)
4273 /* Does nothing, but ensures all queued idles before
4280 copy_async_progress_callback (goffset current_num_bytes,
4281 goffset total_num_bytes,
4284 CopyAsyncData *data = user_data;
4285 ProgressData *progress;
4287 progress = g_new (ProgressData, 1);
4288 progress->data = data;
4289 progress->current_num_bytes = current_num_bytes;
4290 progress->total_num_bytes = total_num_bytes;
4292 g_io_scheduler_job_send_to_mainloop_async (data->job,
4293 copy_async_progress_in_main,
4299 copy_async_thread (GIOSchedulerJob *job,
4300 GCancellable *cancellable,
4303 GSimpleAsyncResult *res;
4304 CopyAsyncData *data;
4309 data = g_simple_async_result_get_op_res_gpointer (res);
4313 result = g_file_copy (data->source,
4317 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4321 /* Ensure all progress callbacks are done running in main thread */
4322 if (data->progress_cb != NULL)
4323 g_io_scheduler_job_send_to_mainloop (job,
4329 g_simple_async_result_set_from_error (res, error);
4330 g_error_free (error);
4333 g_simple_async_result_complete_in_idle (res);
4339 g_file_real_copy_async (GFile *source,
4341 GFileCopyFlags flags,
4343 GCancellable *cancellable,
4344 GFileProgressCallback progress_callback,
4345 gpointer progress_callback_data,
4346 GAsyncReadyCallback callback,
4349 GSimpleAsyncResult *res;
4350 CopyAsyncData *data;
4352 data = g_new0 (CopyAsyncData, 1);
4353 data->source = g_object_ref (source);
4354 data->destination = g_object_ref (destination);
4355 data->flags = flags;
4356 data->progress_cb = progress_callback;
4357 data->progress_cb_data = progress_callback_data;
4359 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4360 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4362 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4366 g_file_real_copy_finish (GFile *file,
4370 /* Error handled in g_file_copy_finish() */
4375 /********************************************
4376 * Default VFS operations *
4377 ********************************************/
4380 * g_file_new_for_path:
4381 * @path: a string containing a relative or absolute path.
4383 * Constructs a #GFile for a given path. This operation never
4384 * fails, but the returned object might not support any I/O
4385 * operation if @path is malformed.
4387 * Returns: a new #GFile for the given @path.
4390 g_file_new_for_path (const char *path)
4392 g_return_val_if_fail (path != NULL, NULL);
4394 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4398 * g_file_new_for_uri:
4399 * @uri: a string containing a URI.
4401 * Constructs a #GFile for a given URI. This operation never
4402 * fails, but the returned object might not support any I/O
4403 * operation if @uri is malformed or if the uri type is
4406 * Returns: a #GFile for the given @uri.
4409 g_file_new_for_uri (const char *uri)
4411 g_return_val_if_fail (uri != NULL, NULL);
4413 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4417 * g_file_parse_name:
4418 * @parse_name: a file name or path to be parsed.
4420 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4421 * This operation never fails, but the returned object might not support any I/O
4422 * operation if the @parse_name cannot be parsed.
4424 * Returns: a new #GFile.
4427 g_file_parse_name (const char *parse_name)
4429 g_return_val_if_fail (parse_name != NULL, NULL);
4431 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4435 is_valid_scheme_character (char c)
4437 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4441 has_valid_scheme (const char *uri)
4447 if (!is_valid_scheme_character (*p))
4452 } while (is_valid_scheme_character (*p));
4458 * g_file_new_for_commandline_arg:
4459 * @arg: a command line string.
4461 * Creates a #GFile with the given argument from the command line. The value of
4462 * @arg can be either a URI, an absolute path or a relative path resolved
4463 * relative to the current working directory.
4464 * This operation never fails, but the returned object might not support any
4465 * I/O operation if @arg points to a malformed path.
4467 * Returns: a new #GFile.
4470 g_file_new_for_commandline_arg (const char *arg)
4476 g_return_val_if_fail (arg != NULL, NULL);
4478 if (g_path_is_absolute (arg))
4479 return g_file_new_for_path (arg);
4481 if (has_valid_scheme (arg))
4482 return g_file_new_for_uri (arg);
4484 current_dir = g_get_current_dir ();
4485 filename = g_build_filename (current_dir, arg, NULL);
4486 g_free (current_dir);
4488 file = g_file_new_for_path (filename);
4495 * g_file_mount_enclosing_volume:
4496 * @location: input #GFile.
4497 * @flags: flags affecting the operation
4498 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4499 * @cancellable: optional #GCancellable object, %NULL to ignore.
4500 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4501 * @user_data: the data to pass to callback function
4503 * Starts a @mount_operation, mounting the volume that contains the file @location.
4505 * When this operation has completed, @callback will be called with
4506 * @user_user data, and the operation can be finalized with
4507 * g_file_mount_enclosing_volume_finish().
4509 * If @cancellable is not %NULL, then the operation can be cancelled by
4510 * triggering the cancellable object from another thread. If the operation
4511 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4514 g_file_mount_enclosing_volume (GFile *location,
4515 GMountMountFlags flags,
4516 GMountOperation *mount_operation,
4517 GCancellable *cancellable,
4518 GAsyncReadyCallback callback,
4523 g_return_if_fail (G_IS_FILE (location));
4525 iface = G_FILE_GET_IFACE (location);
4527 if (iface->mount_enclosing_volume == NULL)
4529 g_simple_async_report_error_in_idle (G_OBJECT (location),
4530 callback, user_data,
4531 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4532 _("volume doesn't implement mount"));
4537 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4542 * g_file_mount_enclosing_volume_finish:
4543 * @location: input #GFile.
4544 * @result: a #GAsyncResult.
4545 * @error: a #GError, or %NULL
4547 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4549 * Returns: %TRUE if successful. If an error
4550 * has occurred, this function will return %FALSE and set @error
4551 * appropriately if present.
4554 g_file_mount_enclosing_volume_finish (GFile *location,
4555 GAsyncResult *result,
4560 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4561 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4563 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4565 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4566 if (g_simple_async_result_propagate_error (simple, error))
4570 iface = G_FILE_GET_IFACE (location);
4572 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4575 /********************************************
4576 * Utility functions *
4577 ********************************************/
4580 * g_file_query_default_handler:
4581 * @file: a #GFile to open.
4582 * @cancellable: optional #GCancellable object, %NULL to ignore.
4583 * @error: a #GError, or %NULL
4585 * Returns the #GAppInfo that is registered as the default
4586 * application to handle the file specified by @file.
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: a #GAppInfo if the handle was found, %NULL if there were errors.
4593 * When you are done with it, release it with g_object_unref()
4596 g_file_query_default_handler (GFile *file,
4597 GCancellable *cancellable,
4601 const char *content_type;
4606 uri_scheme = g_file_get_uri_scheme (file);
4607 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4608 g_free (uri_scheme);
4610 if (appinfo != NULL)
4613 info = g_file_query_info (file,
4614 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4623 content_type = g_file_info_get_content_type (info);
4626 /* Don't use is_native(), as we want to support fuse paths if availible */
4627 path = g_file_get_path (file);
4628 appinfo = g_app_info_get_default_for_type (content_type,
4633 g_object_unref (info);
4635 if (appinfo != NULL)
4638 g_set_error (error, G_IO_ERROR,
4639 G_IO_ERROR_NOT_SUPPORTED,
4640 _("No application is registered as handling this file"));
4646 #define GET_CONTENT_BLOCK_SIZE 8192
4649 * g_file_load_contents:
4650 * @file: input #GFile.
4651 * @cancellable: optional #GCancellable object, %NULL to ignore.
4652 * @contents: a location to place the contents of the file.
4653 * @length: a location to place the length of the contents of the file.
4654 * @etag_out: a location to place the current entity tag for the file.
4655 * @error: a #GError, or %NULL
4657 * Loads the content of the file into memory, returning the size of
4658 * the data. The data is always zero terminated, but this is not
4659 * included in the resultant @length.
4661 * If @cancellable is not %NULL, then the operation can be cancelled by
4662 * triggering the cancellable object from another thread. If the operation
4663 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4665 * Returns: %TRUE if the @file's contents were successfully loaded.
4666 * %FALSE if there were errors..
4669 g_file_load_contents (GFile *file,
4670 GCancellable *cancellable,
4676 GFileInputStream *in;
4677 GByteArray *content;
4682 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4683 g_return_val_if_fail (contents != NULL, FALSE);
4685 in = g_file_read (file, cancellable, error);
4689 content = g_byte_array_new ();
4692 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4693 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4694 content->data + pos,
4695 GET_CONTENT_BLOCK_SIZE,
4696 cancellable, error)) > 0)
4699 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4706 info = g_file_input_stream_query_info (in,
4707 G_FILE_ATTRIBUTE_ETAG_VALUE,
4712 *etag_out = g_strdup (g_file_info_get_etag (info));
4713 g_object_unref (info);
4717 /* Ignore errors on close */
4718 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4719 g_object_unref (in);
4723 /* error is set already */
4724 g_byte_array_free (content, TRUE);
4731 /* Zero terminate (we got an extra byte allocated for this */
4732 content->data[pos] = 0;
4734 *contents = (char *)g_byte_array_free (content, FALSE);
4742 GCancellable *cancellable;
4743 GFileReadMoreCallback read_more_callback;
4744 GAsyncReadyCallback callback;
4746 GByteArray *content;
4753 load_contents_data_free (LoadContentsData *data)
4756 g_error_free (data->error);
4757 if (data->cancellable)
4758 g_object_unref (data->cancellable);
4760 g_byte_array_free (data->content, TRUE);
4761 g_free (data->etag);
4762 g_object_unref (data->file);
4767 load_contents_close_callback (GObject *obj,
4768 GAsyncResult *close_res,
4771 GInputStream *stream = G_INPUT_STREAM (obj);
4772 LoadContentsData *data = user_data;
4773 GSimpleAsyncResult *res;
4775 /* Ignore errors here, we're only reading anyway */
4776 g_input_stream_close_finish (stream, close_res, NULL);
4777 g_object_unref (stream);
4779 res = g_simple_async_result_new (G_OBJECT (data->file),
4782 g_file_load_contents_async);
4783 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4784 g_simple_async_result_complete (res);
4785 g_object_unref (res);
4789 load_contents_fstat_callback (GObject *obj,
4790 GAsyncResult *stat_res,
4793 GInputStream *stream = G_INPUT_STREAM (obj);
4794 LoadContentsData *data = user_data;
4797 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4801 data->etag = g_strdup (g_file_info_get_etag (info));
4802 g_object_unref (info);
4805 g_input_stream_close_async (stream, 0,
4807 load_contents_close_callback, data);
4811 load_contents_read_callback (GObject *obj,
4812 GAsyncResult *read_res,
4815 GInputStream *stream = G_INPUT_STREAM (obj);
4816 LoadContentsData *data = user_data;
4817 GError *error = NULL;
4820 read_size = g_input_stream_read_finish (stream, read_res, &error);
4824 /* Error or EOF, close the file */
4825 data->error = error;
4826 g_input_stream_close_async (stream, 0,
4828 load_contents_close_callback, data);
4830 else if (read_size == 0)
4832 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4833 G_FILE_ATTRIBUTE_ETAG_VALUE,
4836 load_contents_fstat_callback,
4839 else if (read_size > 0)
4841 data->pos += read_size;
4843 g_byte_array_set_size (data->content,
4844 data->pos + GET_CONTENT_BLOCK_SIZE);
4847 if (data->read_more_callback &&
4848 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4849 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4850 G_FILE_ATTRIBUTE_ETAG_VALUE,
4853 load_contents_fstat_callback,
4856 g_input_stream_read_async (stream,
4857 data->content->data + data->pos,
4858 GET_CONTENT_BLOCK_SIZE,
4861 load_contents_read_callback,
4867 load_contents_open_callback (GObject *obj,
4868 GAsyncResult *open_res,
4871 GFile *file = G_FILE (obj);
4872 GFileInputStream *stream;
4873 LoadContentsData *data = user_data;
4874 GError *error = NULL;
4875 GSimpleAsyncResult *res;
4877 stream = g_file_read_finish (file, open_res, &error);
4881 g_byte_array_set_size (data->content,
4882 data->pos + GET_CONTENT_BLOCK_SIZE);
4883 g_input_stream_read_async (G_INPUT_STREAM (stream),
4884 data->content->data + data->pos,
4885 GET_CONTENT_BLOCK_SIZE,
4888 load_contents_read_callback,
4894 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4898 g_simple_async_result_complete (res);
4899 g_error_free (error);
4900 load_contents_data_free (data);
4901 g_object_unref (res);
4906 * g_file_load_partial_contents_async:
4907 * @file: input #GFile.
4908 * @cancellable: optional #GCancellable object, %NULL to ignore.
4909 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
4910 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4911 * @user_data: the data to pass to the callback functions.
4913 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
4914 * used to stop reading from the file when appropriate, else this function
4915 * will behave exactly as g_file_load_contents_async(). This operation
4916 * can be finished by g_file_load_partial_contents_finish().
4918 * Users of this function should be aware that @user_data is passed to
4919 * both the @read_more_callback and the @callback.
4921 * If @cancellable is not %NULL, then the operation can be cancelled by
4922 * triggering the cancellable object from another thread. If the operation
4923 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4926 g_file_load_partial_contents_async (GFile *file,
4927 GCancellable *cancellable,
4928 GFileReadMoreCallback read_more_callback,
4929 GAsyncReadyCallback callback,
4932 LoadContentsData *data;
4934 g_return_if_fail (G_IS_FILE (file));
4936 data = g_new0 (LoadContentsData, 1);
4939 data->cancellable = g_object_ref (cancellable);
4940 data->read_more_callback = read_more_callback;
4941 data->callback = callback;
4942 data->user_data = user_data;
4943 data->content = g_byte_array_new ();
4944 data->file = g_object_ref (file);
4946 g_file_read_async (file,
4949 load_contents_open_callback,
4954 * g_file_load_partial_contents_finish:
4955 * @file: input #GFile.
4956 * @res: a #GAsyncResult.
4957 * @contents: a location to place the contents of the file.
4958 * @length: a location to place the length of the contents of the file.
4959 * @etag_out: a location to place the current entity tag for the file.
4960 * @error: a #GError, or %NULL
4962 * Finishes an asynchronous partial load operation that was started
4963 * with g_file_load_partial_contents_async().
4965 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4966 * present, it will be set appropriately.
4969 g_file_load_partial_contents_finish (GFile *file,
4976 GSimpleAsyncResult *simple;
4977 LoadContentsData *data;
4979 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4980 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4981 g_return_val_if_fail (contents != NULL, FALSE);
4983 simple = G_SIMPLE_ASYNC_RESULT (res);
4985 if (g_simple_async_result_propagate_error (simple, error))
4988 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4990 data = g_simple_async_result_get_op_res_gpointer (simple);
4994 g_propagate_error (error, data->error);
5003 *length = data->pos;
5007 *etag_out = data->etag;
5011 /* Zero terminate */
5012 g_byte_array_set_size (data->content, data->pos + 1);
5013 data->content->data[data->pos] = 0;
5015 *contents = (char *)g_byte_array_free (data->content, FALSE);
5016 data->content = NULL;
5022 * g_file_load_contents_async:
5023 * @file: input #GFile.
5024 * @cancellable: optional #GCancellable object, %NULL to ignore.
5025 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5026 * @user_data: the data to pass to callback function
5028 * Starts an asynchronous load of the @file's contents.
5030 * For more details, see g_file_load_contents() which is
5031 * the synchronous version of this call.
5033 * When the load operation has completed, @callback will be called
5034 * with @user data. To finish the operation, call
5035 * g_file_load_contents_finish() with the #GAsyncResult returned by
5038 * If @cancellable is not %NULL, then the operation can be cancelled by
5039 * triggering the cancellable object from another thread. If the operation
5040 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5043 g_file_load_contents_async (GFile *file,
5044 GCancellable *cancellable,
5045 GAsyncReadyCallback callback,
5048 g_file_load_partial_contents_async (file,
5051 callback, user_data);
5055 * g_file_load_contents_finish:
5056 * @file: input #GFile.
5057 * @res: a #GAsyncResult.
5058 * @contents: a location to place the contents of the file.
5059 * @length: a location to place the length of the contents of the file.
5060 * @etag_out: a location to place the current entity tag for the file.
5061 * @error: a #GError, or %NULL
5063 * Finishes an asynchronous load of the @file's contents.
5064 * The contents are placed in @contents, and @length is set to the
5065 * size of the @contents string. If @etag_out is present, it will be
5066 * set to the new entity tag for the @file.
5068 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5069 * present, it will be set appropriately.
5072 g_file_load_contents_finish (GFile *file,
5079 return g_file_load_partial_contents_finish (file,
5088 * g_file_replace_contents:
5089 * @file: input #GFile.
5090 * @contents: a string containing the new contents for @file.
5091 * @length: the length of @contents in bytes.
5092 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5094 * @make_backup: %TRUE if a backup should be created.
5095 * @flags: a set of #GFileCreateFlags.
5096 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5097 * for the document. This should be freed with g_free() when no longer
5099 * @cancellable: optional #GCancellable object, %NULL to ignore.
5100 * @error: a #GError, or %NULL
5102 * Replaces the contents of @file with @contents of @length bytes.
5104 * If @etag is specified (not %NULL) any existing file must have that etag, or
5105 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5107 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5109 * If @cancellable is not %NULL, then the operation can be cancelled by
5110 * triggering the cancellable object from another thread. If the operation
5111 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5113 * The returned @new_etag can be used to verify that the file hasn't changed the
5114 * next time it is saved over.
5116 * Returns: %TRUE if successful. If an error
5117 * has occurred, this function will return %FALSE and set @error
5118 * appropriately if present.
5121 g_file_replace_contents (GFile *file,
5122 const char *contents,
5125 gboolean make_backup,
5126 GFileCreateFlags flags,
5128 GCancellable *cancellable,
5131 GFileOutputStream *out;
5132 gsize pos, remainder;
5135 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5136 g_return_val_if_fail (contents != NULL, FALSE);
5138 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5144 while (remainder > 0 &&
5145 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5147 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5155 if (remainder > 0 && res < 0)
5157 /* Ignore errors on close */
5158 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5160 /* error is set already */
5164 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5168 *new_etag = g_file_output_stream_get_etag (out);
5176 GCancellable *cancellable;
5177 GAsyncReadyCallback callback;
5179 const char *content;
5183 } ReplaceContentsData;
5186 replace_contents_data_free (ReplaceContentsData *data)
5189 g_error_free (data->error);
5190 if (data->cancellable)
5191 g_object_unref (data->cancellable);
5192 g_object_unref (data->file);
5193 g_free (data->etag);
5198 replace_contents_close_callback (GObject *obj,
5199 GAsyncResult *close_res,
5202 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5203 ReplaceContentsData *data = user_data;
5204 GSimpleAsyncResult *res;
5206 /* Ignore errors here, we're only reading anyway */
5207 g_output_stream_close_finish (stream, close_res, NULL);
5208 g_object_unref (stream);
5210 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5212 res = g_simple_async_result_new (G_OBJECT (data->file),
5215 g_file_replace_contents_async);
5216 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5217 g_simple_async_result_complete (res);
5218 g_object_unref (res);
5222 replace_contents_write_callback (GObject *obj,
5223 GAsyncResult *read_res,
5226 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5227 ReplaceContentsData *data = user_data;
5228 GError *error = NULL;
5231 write_size = g_output_stream_write_finish (stream, read_res, &error);
5233 if (write_size <= 0)
5235 /* Error or EOF, close the file */
5237 data->error = error;
5238 g_output_stream_close_async (stream, 0,
5240 replace_contents_close_callback, data);
5242 else if (write_size > 0)
5244 data->pos += write_size;
5246 if (data->pos >= data->length)
5247 g_output_stream_close_async (stream, 0,
5249 replace_contents_close_callback, data);
5251 g_output_stream_write_async (stream,
5252 data->content + data->pos,
5253 data->length - data->pos,
5256 replace_contents_write_callback,
5262 replace_contents_open_callback (GObject *obj,
5263 GAsyncResult *open_res,
5266 GFile *file = G_FILE (obj);
5267 GFileOutputStream *stream;
5268 ReplaceContentsData *data = user_data;
5269 GError *error = NULL;
5270 GSimpleAsyncResult *res;
5272 stream = g_file_replace_finish (file, open_res, &error);
5276 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5277 data->content + data->pos,
5278 data->length - data->pos,
5281 replace_contents_write_callback,
5287 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5291 g_simple_async_result_complete (res);
5292 g_error_free (error);
5293 replace_contents_data_free (data);
5294 g_object_unref (res);
5299 * g_file_replace_contents_async:
5300 * @file: input #GFile.
5301 * @contents: string of contents to replace the file with.
5302 * @length: the length of @contents in bytes.
5303 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5304 * @make_backup: %TRUE if a backup should be created.
5305 * @flags: a set of #GFileCreateFlags.
5306 * @cancellable: optional #GCancellable object, %NULL to ignore.
5307 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5308 * @user_data: the data to pass to callback function
5310 * Starts an asynchronous replacement of @file with the given
5311 * @contents of @length bytes. @etag will replace the document's
5312 * current entity tag.
5314 * When this operation has completed, @callback will be called with
5315 * @user_user data, and the operation can be finalized with
5316 * g_file_replace_contents_finish().
5318 * If @cancellable is not %NULL, then the operation can be cancelled by
5319 * triggering the cancellable object from another thread. If the operation
5320 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5322 * If @make_backup is %TRUE, this function will attempt to
5323 * make a backup of @file.
5326 g_file_replace_contents_async (GFile *file,
5327 const char *contents,
5330 gboolean make_backup,
5331 GFileCreateFlags flags,
5332 GCancellable *cancellable,
5333 GAsyncReadyCallback callback,
5336 ReplaceContentsData *data;
5338 g_return_if_fail (G_IS_FILE (file));
5339 g_return_if_fail (contents != NULL);
5341 data = g_new0 (ReplaceContentsData, 1);
5344 data->cancellable = g_object_ref (cancellable);
5345 data->callback = callback;
5346 data->user_data = user_data;
5347 data->content = contents;
5348 data->length = length;
5350 data->file = g_object_ref (file);
5352 g_file_replace_async (file,
5358 replace_contents_open_callback,
5363 * g_file_replace_contents_finish:
5364 * @file: input #GFile.
5365 * @res: a #GAsyncResult.
5366 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5367 * for the document. This should be freed with g_free() when it is no
5369 * @error: a #GError, or %NULL
5371 * Finishes an asynchronous replace of the given @file. See
5372 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5373 * tag for the document, if present.
5375 * Returns: %TRUE on success, %FALSE on failure.
5378 g_file_replace_contents_finish (GFile *file,
5383 GSimpleAsyncResult *simple;
5384 ReplaceContentsData *data;
5386 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5387 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5389 simple = G_SIMPLE_ASYNC_RESULT (res);
5391 if (g_simple_async_result_propagate_error (simple, error))
5394 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5396 data = g_simple_async_result_get_op_res_gpointer (simple);
5400 g_propagate_error (error, data->error);
5408 *new_etag = data->etag;
5409 data->etag = NULL; /* Take ownership */
5415 #define __G_FILE_C__
5416 #include "gioaliasdef.c"