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,
186 g_file_get_type (void)
188 static GType file_type = 0;
192 static const GTypeInfo file_info =
194 sizeof (GFileIface), /* class_size */
195 g_file_base_init, /* base_init */
196 NULL, /* base_finalize */
198 NULL, /* class_finalize */
199 NULL, /* class_data */
206 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
209 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
216 g_file_class_init (gpointer g_class,
219 GFileIface *iface = g_class;
221 iface->enumerate_children_async = g_file_real_enumerate_children_async;
222 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
223 iface->set_display_name_async = g_file_real_set_display_name_async;
224 iface->set_display_name_finish = g_file_real_set_display_name_finish;
225 iface->query_info_async = g_file_real_query_info_async;
226 iface->query_info_finish = g_file_real_query_info_finish;
227 iface->set_attributes_async = g_file_real_set_attributes_async;
228 iface->set_attributes_finish = g_file_real_set_attributes_finish;
229 iface->read_async = g_file_real_read_async;
230 iface->read_finish = g_file_real_read_finish;
231 iface->append_to_async = g_file_real_append_to_async;
232 iface->append_to_finish = g_file_real_append_to_finish;
233 iface->create_async = g_file_real_create_async;
234 iface->create_finish = g_file_real_create_finish;
235 iface->replace_async = g_file_real_replace_async;
236 iface->replace_finish = g_file_real_replace_finish;
237 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
238 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
239 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
243 g_file_base_init (gpointer g_class)
250 * @file: input #GFile.
252 * Checks to see if a file is native to the platform.
254 * A native file s one expressed in the platform-native filename format,
255 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
256 * as it might be on a locally mounted remote filesystem.
258 * On some systems non-native files may be available using
259 * the native filesystem via a userspace filesystem (FUSE), in
260 * these cases this call will return %FALSE, but g_file_get_path()
261 * will still return a native path.
263 * This call does no blocking i/o.
265 * Returns: %TRUE if file is native.
268 g_file_is_native (GFile *file)
272 g_return_val_if_fail (G_IS_FILE (file), FALSE);
274 iface = G_FILE_GET_IFACE (file);
276 return (* iface->is_native) (file);
281 * g_file_has_uri_scheme:
282 * @file: input #GFile.
283 * @uri_scheme: a string containing a URI scheme.
285 * Checks to see if a #GFile has a given URI scheme.
287 * This call does no blocking i/o.
289 * Returns: %TRUE if #GFile's backend supports the
290 * given URI scheme, %FALSE if URI scheme is %NULL,
291 * not supported, or #GFile is invalid.
294 g_file_has_uri_scheme (GFile *file,
295 const char *uri_scheme)
299 g_return_val_if_fail (G_IS_FILE (file), FALSE);
300 g_return_val_if_fail (uri_scheme != NULL, FALSE);
302 iface = G_FILE_GET_IFACE (file);
304 return (* iface->has_uri_scheme) (file, uri_scheme);
309 * g_file_get_uri_scheme:
310 * @file: input #GFile.
312 * Gets the URI scheme for a #GFile.
313 * RFC 3986 decodes the scheme as:
315 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
317 * Common schemes include "file", "http", "ftp", etc.
319 * This call does no blocking i/o.
321 * Returns: a string containing the URI scheme for the given
322 * #GFile. The returned string should be freed with g_free()
323 * when no longer needed.
326 g_file_get_uri_scheme (GFile *file)
330 g_return_val_if_fail (G_IS_FILE (file), NULL);
332 iface = G_FILE_GET_IFACE (file);
334 return (* iface->get_uri_scheme) (file);
339 * g_file_get_basename:
340 * @file: input #GFile.
342 * Gets the base name (the last component of the path) for a given #GFile.
344 * If called for the top level of a system (such as the filesystem root
345 * or a uri like sftp://host/ it will return a single directory separator
346 * (and on Windows, possibly a drive letter).
348 * This call does no blocking i/o.
350 * Returns: string containing the #GFile's base name, or %NULL
351 * if given #GFile is invalid. The returned string should be
352 * freed with g_free() when no longer needed.
355 g_file_get_basename (GFile *file)
359 g_return_val_if_fail (G_IS_FILE (file), NULL);
361 iface = G_FILE_GET_IFACE (file);
363 return (* iface->get_basename) (file);
368 * @file: input #GFile.
370 * Gets the local pathname for #GFile, if one exists.
372 * This call does no blocking i/o.
374 * Returns: string containing the #GFile's path, or %NULL if
375 * no such path exists. The returned string should be
376 * freed with g_free() when no longer needed.
379 g_file_get_path (GFile *file)
383 g_return_val_if_fail (G_IS_FILE (file), NULL);
385 iface = G_FILE_GET_IFACE (file);
387 return (* iface->get_path) (file);
392 * @file: input #GFile.
394 * Gets the URI for the @file.
396 * This call does no blocking i/o.
398 * Returns: a string containing the #GFile's URI.
399 * The returned string should be freed with g_free() when no longer needed.
402 g_file_get_uri (GFile *file)
406 g_return_val_if_fail (G_IS_FILE (file), NULL);
408 iface = G_FILE_GET_IFACE (file);
410 return (* iface->get_uri) (file);
414 * g_file_get_parse_name:
415 * @file: input #GFile.
417 * Gets the parse name of the @file.
418 * A parse name is a UTF-8 string that describes the
419 * file such that one can get the #GFile back using
420 * g_file_parse_name().
422 * This is generally used to show the #GFile as a nice
423 * string in a user interface, like in a location entry.
425 * For local files with names that can safely be converted
426 * to UTF8 the pathname is used, otherwise the IRI is used
427 * (a form of URI that allows UTF8 characters unescaped).
429 * This call does no blocking i/o.
431 * Returns: a string containing the #GFile's parse name. The returned
432 * string should be freed with g_free() when no longer needed.
435 g_file_get_parse_name (GFile *file)
439 g_return_val_if_fail (G_IS_FILE (file), NULL);
441 iface = G_FILE_GET_IFACE (file);
443 return (* iface->get_parse_name) (file);
448 * @file: input #GFile.
450 * Duplicates a #GFile handle. This operation does not duplicate
451 * the actual file or directory represented by the #GFile; see
452 * g_file_copy() if attempting to copy a file.
454 * This call does no blocking i/o.
456 * Returns: #GFile that is a duplicate of the given #GFile.
459 g_file_dup (GFile *file)
463 g_return_val_if_fail (G_IS_FILE (file), NULL);
465 iface = G_FILE_GET_IFACE (file);
467 return (* iface->dup) (file);
472 * @file: #gconstpointer to a #GFile.
474 * Creates a hash value for a #GFile.
476 * This call does no blocking i/o.
478 * Returns: 0 if @file is not a valid #GFile, otherwise an
479 * integer that can be used as hash value for the #GFile.
480 * This function is intended for easily hashing a #GFile to
481 * add to a #GHashTable or similar data structure.
484 g_file_hash (gconstpointer file)
488 g_return_val_if_fail (G_IS_FILE (file), 0);
490 iface = G_FILE_GET_IFACE (file);
492 return (* iface->hash) ((GFile *)file);
497 * @file1: the first #GFile.
498 * @file2: the second #GFile.
500 * Checks equality of two given #GFile<!-- -->s
502 * This call does no blocking i/o.
504 * Returns: %TRUE if @file1 and @file2 are equal.
505 * %FALSE if either is not a #GFile.
508 g_file_equal (GFile *file1,
513 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
514 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
516 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
519 iface = G_FILE_GET_IFACE (file1);
521 return (* iface->equal) (file1, file2);
527 * @file: input #GFile.
529 * Gets the parent directory for the @file.
530 * If the @file represents the root directory of the
531 * file system, then %NULL will be returned.
533 * This call does no blocking i/o.
535 * Returns: a #GFile structure to the parent of the given
536 * #GFile or %NULL if there is no parent.
539 g_file_get_parent (GFile *file)
543 g_return_val_if_fail (G_IS_FILE (file), NULL);
545 iface = G_FILE_GET_IFACE (file);
547 return (* iface->get_parent) (file);
552 * @file: input #GFile.
553 * @name: string containing the child's name.
555 * Gets a specific child of @file with name equal to @name.
557 * Note that the file with that specific name might not exist, but
558 * you can still have a #GFile that points to it. You can use this
559 * for instance to create that file.
561 * This call does no blocking i/o.
563 * Returns: a #GFile to a child specified by @name.
566 g_file_get_child (GFile *file,
569 g_return_val_if_fail (G_IS_FILE (file), NULL);
570 g_return_val_if_fail (name != NULL, NULL);
572 return g_file_resolve_relative_path (file, name);
576 * g_file_get_child_for_display_name:
577 * @file: input #GFile.
578 * @display_name: string to a possible child.
581 * Gets the child of @file for a given @display_name (i.e. a UTF8
582 * version of the name). If this function fails, it returns %NULL and @error will be
583 * set. This is very useful when constructing a GFile for a new file
584 * and the user entered the filename in the user interface, for instance
585 * when you select a directory and type a filename in the file selector.
587 * This call does no blocking i/o.
589 * Returns: a #GFile to the specified child, or
590 * %NULL if the display name couldn't be converted.
593 g_file_get_child_for_display_name (GFile *file,
594 const char *display_name,
599 g_return_val_if_fail (G_IS_FILE (file), NULL);
600 g_return_val_if_fail (display_name != NULL, NULL);
602 iface = G_FILE_GET_IFACE (file);
604 return (* iface->get_child_for_display_name) (file, display_name, error);
608 * g_file_contains_file:
609 * @parent: input #GFile.
610 * @descendant: input #GFile.
612 * Checks whether @parent (recursively) contains the specified @descendant.
614 * This call does no blocking i/o.
616 * Returns: %TRUE if the @descendant's parent, grandparent, etc is @parent. %FALSE otherwise.
619 g_file_contains_file (GFile *parent,
624 g_return_val_if_fail (G_IS_FILE (parent), FALSE);
625 g_return_val_if_fail (G_IS_FILE (descendant), FALSE);
627 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
630 iface = G_FILE_GET_IFACE (parent);
632 return (* iface->contains_file) (parent, descendant);
636 * g_file_get_relative_path:
637 * @parent: input #GFile.
638 * @descendant: input #GFile.
640 * Gets the path for @descendant relative to @parent.
642 * This call does no blocking i/o.
644 * Returns: string with the relative path from @descendant
645 * to @parent, or %NULL if @descendant is not a descendant of @parent. The returned string should be freed with
646 * g_free() when no longer needed.
649 g_file_get_relative_path (GFile *parent,
654 g_return_val_if_fail (G_IS_FILE (parent), NULL);
655 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
657 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
660 iface = G_FILE_GET_IFACE (parent);
662 return (* iface->get_relative_path) (parent, descendant);
666 * g_file_resolve_relative_path:
667 * @file: input #GFile.
668 * @relative_path: a given relative path string.
670 * Resolves a relative path for @file to an absolute path.
672 * This call does no blocking i/o.
674 * Returns: #GFile to the resolved path. %NULL if @relative_path
675 * is %NULL or if @file is invalid.
678 g_file_resolve_relative_path (GFile *file,
679 const char *relative_path)
683 g_return_val_if_fail (G_IS_FILE (file), NULL);
684 g_return_val_if_fail (relative_path != NULL, NULL);
686 iface = G_FILE_GET_IFACE (file);
688 return (* iface->resolve_relative_path) (file, relative_path);
692 * g_file_enumerate_children:
693 * @file: input #GFile.
694 * @attributes: an attribute query string.
695 * @flags: a set of #GFileQueryInfoFlags.
696 * @cancellable: optional #GCancellable object, %NULL to ignore.
697 * @error: #GError for error reporting.
699 * Gets the requested information about the files in a directory. The result
700 * is a #GFileEnumerator object that will give out #GFileInfo objects for
701 * all the files in the directory.
703 * The @attribute value is a string that specifies the file attributes that
704 * should be gathered. It is not an error if it's not possible to read a particular
705 * requested attribute from a file - it just won't be set. @attribute should
706 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
707 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
708 * namespace. An example attribute query be "standard::*,owner::user".
709 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
711 * If @cancellable is not %NULL, then the operation can be cancelled by
712 * triggering the cancellable object from another thread. If the operation
713 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
715 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
716 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
717 * Other errors are possible too.
719 * Returns: A #GFileEnumerator if successful, %NULL on error.
722 g_file_enumerate_children (GFile *file,
723 const char *attributes,
724 GFileQueryInfoFlags flags,
725 GCancellable *cancellable,
731 g_return_val_if_fail (G_IS_FILE (file), NULL);
733 if (g_cancellable_set_error_if_cancelled (cancellable, error))
736 iface = G_FILE_GET_IFACE (file);
738 if (iface->enumerate_children == NULL)
740 g_set_error (error, G_IO_ERROR,
741 G_IO_ERROR_NOT_SUPPORTED,
742 _("Operation not supported"));
746 return (* iface->enumerate_children) (file, attributes, flags,
751 * g_file_enumerate_children_async:
752 * @file: input #GFile.
753 * @attributes: an attribute query string.
754 * @flags: a set of #GFileQueryInfoFlags.
755 * @io_priority: the <link linkend="io-priority">I/O priority</link>
757 * @cancellable: optional #GCancellable object, %NULL to ignore.
758 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
759 * @user_data: the data to pass to callback function
761 * Asynchronously gets the requested information about the files in a directory. The result
762 * is a #GFileEnumerator object that will give out #GFileInfo objects for
763 * all the files in the directory.
765 * For more details, see g_file_enumerate_children() which is
766 * the synchronous version of this call.
768 * When the operation is finished, @callback will be called. You can then call
769 * g_file_enumerate_children_finish() to get the result of the operation.
772 g_file_enumerate_children_async (GFile *file,
773 const char *attributes,
774 GFileQueryInfoFlags flags,
776 GCancellable *cancellable,
777 GAsyncReadyCallback callback,
782 g_return_if_fail (G_IS_FILE (file));
784 iface = G_FILE_GET_IFACE (file);
785 (* iface->enumerate_children_async) (file,
795 * g_file_enumerate_children_finish:
796 * @file: input #GFile.
797 * @res: a #GAsyncResult.
800 * Finishes an async enumerate children operation.
801 * See g_file_enumerate_children_async().
803 * Returns: a #GFileEnumerator or %NULL if an error occurred.
806 g_file_enumerate_children_finish (GFile *file,
812 g_return_val_if_fail (G_IS_FILE (file), NULL);
813 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
815 if (G_IS_SIMPLE_ASYNC_RESULT (res))
817 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
818 if (g_simple_async_result_propagate_error (simple, error))
822 iface = G_FILE_GET_IFACE (file);
823 return (* iface->enumerate_children_finish) (file, res, error);
827 * g_file_query_exists:
828 * @file: input #GFile.
829 * @cancellable: optional #GCancellable object, %NULL to ignore.
831 * Utility function to check if a particular file exists. This is
832 * implemented using g_file_query_info() and as such does blocking I/O.
834 * Note that in many cases it is racy to first check for file existance
835 * and then execute something based on the outcome of that, because the
836 * file might have been created or removed inbetween the operations. The
837 * general approach to handling that is to not check, but just do the
838 * operation and handle the errors as they come.
840 * As an example of race-free checking, take the case of reading a file, and
841 * if it doesn't exist, creating it. There are two racy versions: read it, and
842 * on error create it; and: check if it exists, if not create it. These
843 * can both result in two processes creating the file (with perhaps a partially
844 * written file as the result). The correct approach is to always try to create
845 * the file with g_file_create() which will either atomically create the file
846 * or fail with a G_IO_ERROR_EXISTS error.
848 * However, in many cases an existance check is useful in a user
849 * interface, for instance to make a menu item sensitive/insensitive, so that
850 * you don't have to fool users that something is possible and then just show
851 * and error dialog. If you do this, you should make sure to also handle the
852 * errors that can happen due to races when you execute the operation.
854 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
857 g_file_query_exists (GFile *file,
858 GCancellable *cancellable)
862 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
863 G_FILE_QUERY_INFO_NONE,
867 g_object_unref (info);
876 * @file: input #GFile.
877 * @attributes: an attribute query string.
878 * @flags: a set of #GFileQueryInfoFlags.
879 * @cancellable: optional #GCancellable object, %NULL to ignore.
882 * Gets the requested information about specified @file. The result
883 * is a #GFileInfo object that contains key-value attributes (such as
884 * the type or size of the file).
886 * The @attribute value is a string that specifies the file attributes that
887 * should be gathered. It is not an error if it's not possible to read a particular
888 * requested attribute from a file - it just won't be set. @attribute should
889 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
890 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
891 * namespace. An example attribute query be "standard::*,owner::user".
892 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
894 * If @cancellable is not %NULL, then the operation can be cancelled by
895 * triggering the cancellable object from another thread. If the operation
896 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
898 * For symlinks, normally the information about the target of the
899 * symlink is returned, rather than information about the symlink itself.
900 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
901 * information about the symlink itself will be returned. Also, for symlinks
902 * that point to non-existing files the information about the symlink itself
905 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
906 * Other errors are possible too, and depend on what kind of filesystem the file is on.
908 * Returns: a #GFileInfo for the given @file, or %NULL on error.
911 g_file_query_info (GFile *file,
912 const char *attributes,
913 GFileQueryInfoFlags flags,
914 GCancellable *cancellable,
919 g_return_val_if_fail (G_IS_FILE (file), NULL);
921 if (g_cancellable_set_error_if_cancelled (cancellable, error))
924 iface = G_FILE_GET_IFACE (file);
926 if (iface->query_info == NULL)
928 g_set_error (error, G_IO_ERROR,
929 G_IO_ERROR_NOT_SUPPORTED,
930 _("Operation not supported"));
934 return (* iface->query_info) (file, attributes, flags, cancellable, error);
938 * g_file_query_info_async:
939 * @file: input #GFile.
940 * @attributes: an attribute query string.
941 * @flags: a set of #GFileQueryInfoFlags.
942 * @io_priority: the <link linkend="io-priority">I/O priority</link>
944 * @cancellable: optional #GCancellable object, %NULL to ignore.
945 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
946 * @user_data: the data to pass to callback function
948 * Asynchronously gets the requested information about specified @file. The result
949 * is a #GFileInfo object that contains key-value attributes (such as type or size
952 * For more details, see g_file_query_info() which is
953 * the synchronous version of this call.
955 * When the operation is finished, @callback will be called. You can then call
956 * g_file_query_info_finish() to get the result of the operation.
959 g_file_query_info_async (GFile *file,
960 const char *attributes,
961 GFileQueryInfoFlags flags,
963 GCancellable *cancellable,
964 GAsyncReadyCallback callback,
969 g_return_if_fail (G_IS_FILE (file));
971 iface = G_FILE_GET_IFACE (file);
972 (* iface->query_info_async) (file,
982 * g_file_query_info_finish:
983 * @file: input #GFile.
984 * @res: a #GAsyncResult.
987 * Finishes an asynchronous file info query.
988 * See g_file_query_info_async().
990 * Returns: #GFileInfo for given @file or %NULL on error.
993 g_file_query_info_finish (GFile *file,
999 g_return_val_if_fail (G_IS_FILE (file), NULL);
1000 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1002 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1004 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1005 if (g_simple_async_result_propagate_error (simple, error))
1009 iface = G_FILE_GET_IFACE (file);
1010 return (* iface->query_info_finish) (file, res, error);
1014 * g_file_query_filesystem_info:
1015 * @file: input #GFile.
1016 * @attributes: an attribute query string.
1017 * @cancellable: optional #GCancellable object, %NULL to ignore.
1018 * @error: a #GError.
1020 * Similar to g_file_query_info(), but obtains information
1021 * about the filesystem the @file is on, rather than the file itself.
1022 * For instance the amount of space available and the type of
1025 * The @attribute value is a string that specifies the file attributes that
1026 * should be gathered. It is not an error if it's not possible to read a particular
1027 * requested attribute from a file - it just won't be set. @attribute should
1028 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1029 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1030 * namespace. The standard namespace for filesystem attributes is "fs".
1031 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1032 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1033 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1035 * If @cancellable is not %NULL, then the operation can be cancelled by
1036 * triggering the cancellable object from another thread. If the operation
1037 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1039 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1040 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1042 * Returns: a #GFileInfo or %NULL if there was an error.
1045 g_file_query_filesystem_info (GFile *file,
1046 const char *attributes,
1047 GCancellable *cancellable,
1052 g_return_val_if_fail (G_IS_FILE (file), NULL);
1054 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1057 iface = G_FILE_GET_IFACE (file);
1059 if (iface->query_filesystem_info == NULL)
1061 g_set_error (error, G_IO_ERROR,
1062 G_IO_ERROR_NOT_SUPPORTED,
1063 _("Operation not supported"));
1067 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1071 * g_file_find_enclosing_mount:
1072 * @file: input #GFile.
1073 * @cancellable: optional #GCancellable object, %NULL to ignore.
1074 * @error: a #GError.
1076 * Gets a #GMount for the #GFile.
1078 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1079 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1082 * If @cancellable is not %NULL, then the operation can be cancelled by
1083 * triggering the cancellable object from another thread. If the operation
1084 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1086 * Returns: a #GMount where the @file is located or %NULL on error.
1089 g_file_find_enclosing_mount (GFile *file,
1090 GCancellable *cancellable,
1095 g_return_val_if_fail (G_IS_FILE (file), NULL);
1097 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1100 iface = G_FILE_GET_IFACE (file);
1101 if (iface->find_enclosing_mount == NULL)
1103 g_set_error (error, G_IO_ERROR,
1104 G_IO_ERROR_NOT_FOUND,
1105 _("Containing mount does not exist"));
1109 return (* iface->find_enclosing_mount) (file, cancellable, error);
1112 * g_file_find_enclosing_mount_async:
1114 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1116 * @cancellable: optional #GCancellable object, %NULL to ignore.
1117 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1118 * @user_data: the data to pass to callback function
1120 * Asynchronously gets the mount for the file.
1122 * For more details, see g_file_find_enclosing_mount() which is
1123 * the synchronous version of this call.
1125 * When the operation is finished, @callback will be called. You can then call
1126 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1129 g_file_find_enclosing_mount_async (GFile *file,
1131 GCancellable *cancellable,
1132 GAsyncReadyCallback callback,
1137 g_return_if_fail (G_IS_FILE (file));
1139 iface = G_FILE_GET_IFACE (file);
1140 (* iface->find_enclosing_mount_async) (file,
1148 * g_file_find_enclosing_mount_finish:
1150 * @res: a #GAsyncResult
1153 * Finishes an asynchronous find mount request.
1154 * See g_file_find_enclosing_mount_async().
1156 * Returns: #GMount for given @file or %NULL on error.
1159 g_file_find_enclosing_mount_finish (GFile *file,
1165 g_return_val_if_fail (G_IS_FILE (file), NULL);
1166 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1168 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1170 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1171 if (g_simple_async_result_propagate_error (simple, error))
1175 iface = G_FILE_GET_IFACE (file);
1176 return (* iface->find_enclosing_mount_finish) (file, res, error);
1182 * @file: #GFile to read.
1183 * @cancellable: a #GCancellable
1184 * @error: a #GError, or %NULL
1186 * Opens a file for reading. The result is a #GFileInputStream that
1187 * can be used to read the contents of the file.
1189 * If @cancellable is not %NULL, then the operation can be cancelled by
1190 * triggering the cancellable object from another thread. If the operation
1191 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1193 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1194 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1195 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1197 * Returns: #GFileInputStream or %NULL on error.
1200 g_file_read (GFile *file,
1201 GCancellable *cancellable,
1206 g_return_val_if_fail (G_IS_FILE (file), NULL);
1208 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1211 iface = G_FILE_GET_IFACE (file);
1213 if (iface->read_fn == NULL)
1215 g_set_error (error, G_IO_ERROR,
1216 G_IO_ERROR_NOT_SUPPORTED,
1217 _("Operation not supported"));
1221 return (* iface->read_fn) (file, cancellable, error);
1226 * @file: input #GFile.
1227 * @flags: a set of #GFileCreateFlags.
1228 * @cancellable: optional #GCancellable object, %NULL to ignore.
1229 * @error: a #GError, or %NULL
1231 * Gets an output stream for appending data to the file. If
1232 * the file doesn't already exist it is created.
1234 * By default files created are generally readable by everyone,
1235 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1236 * will be made readable only to the current user, to the level that
1237 * is supported on the target filesystem.
1239 * If @cancellable is not %NULL, then the operation can be cancelled by
1240 * triggering the cancellable object from another thread. If the operation
1241 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1243 * Some file systems don't allow all file names, and may
1244 * return an G_IO_ERROR_INVALID_FILENAME error.
1245 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1246 * returned. Other errors are possible too, and depend on what kind of
1247 * filesystem the file is on.
1249 * Returns: a #GFileOutputStream.
1252 g_file_append_to (GFile *file,
1253 GFileCreateFlags flags,
1254 GCancellable *cancellable,
1259 g_return_val_if_fail (G_IS_FILE (file), NULL);
1261 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1264 iface = G_FILE_GET_IFACE (file);
1266 if (iface->append_to == NULL)
1268 g_set_error (error, G_IO_ERROR,
1269 G_IO_ERROR_NOT_SUPPORTED,
1270 _("Operation not supported"));
1274 return (* iface->append_to) (file, flags, cancellable, error);
1279 * @file: input #GFile.
1280 * @flags: a set of #GFileCreateFlags.
1281 * @cancellable: optional #GCancellable object, %NULL to ignore.
1282 * @error: a #GError, or %NULL
1284 * Creates a new file and returns an output stream for writing to it.
1285 * The file must not already exists.
1287 * By default files created are generally readable by everyone,
1288 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1289 * will be made readable only to the current user, to the level that
1290 * is supported on the target filesystem.
1292 * If @cancellable is not %NULL, then the operation can be cancelled by
1293 * triggering the cancellable object from another thread. If the operation
1294 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1296 * If a file with this name already exists the G_IO_ERROR_EXISTS error
1297 * will be returned. If the file is a directory the G_IO_ERROR_IS_DIRECTORY
1298 * error will be returned.
1299 * Some file systems don't allow all file names, and may
1300 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1301 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1302 * Other errors are possible too, and depend on what kind of
1303 * filesystem the file is on.
1305 * Returns: a #GFileOutputStream for the newly created file, or
1309 g_file_create (GFile *file,
1310 GFileCreateFlags flags,
1311 GCancellable *cancellable,
1316 g_return_val_if_fail (G_IS_FILE (file), NULL);
1318 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1321 iface = G_FILE_GET_IFACE (file);
1323 if (iface->create == NULL)
1325 g_set_error (error, G_IO_ERROR,
1326 G_IO_ERROR_NOT_SUPPORTED,
1327 _("Operation not supported"));
1331 return (* iface->create) (file, flags, cancellable, error);
1336 * @file: input #GFile.
1337 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1338 * current #GFile, or #NULL to ignore.
1339 * @make_backup: %TRUE if a backup should be created.
1340 * @flags: a set of #GFileCreateFlags.
1341 * @cancellable: optional #GCancellable object, %NULL to ignore.
1342 * @error: a #GError, or %NULL
1344 * Returns an output stream for overwriting the file, possibly
1345 * creating a backup copy of the file first.
1347 * This will try to replace the file in the safest way possible so
1348 * that any errors during the writing will not affect an already
1349 * existing copy of the file. For instance, for local files it
1350 * may write to a temporary file and then atomically rename over
1351 * the destination when the stream is closed.
1353 * By default files created are generally readable by everyone,
1354 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1355 * will be made readable only to the current user, to the level that
1356 * is supported on the target filesystem.
1358 * If @cancellable is not %NULL, then the operation can be cancelled by
1359 * triggering the cancellable object from another thread. If the operation
1360 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1362 * If you pass in a non-#NULL @etag value, then this value is
1363 * compared to the current entity tag of the file, and if they differ
1364 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1365 * that the file has been changed since you last read it. You can get
1366 * the new etag from g_file_output_stream_get_etag() after you've
1367 * finished writing and closed the #GFileOutputStream. When you load
1368 * a new file you can use g_file_input_stream_query_info() to get
1369 * the etag of the file.
1371 * If @make_backup is %TRUE, this function will attempt to make a backup
1372 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1373 * error will be returned. If you want to replace anyway, try again with
1374 * @make_backup set to %FALSE.
1376 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1377 * and if the file is some other form of non-regular file then a
1378 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1379 * Some file systems don't allow all file names, and may
1380 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1381 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1382 * Other errors are possible too, and depend on what kind of
1383 * filesystem the file is on.
1385 * Returns: a #GFileOutputStream or %NULL on error.
1388 g_file_replace (GFile *file,
1390 gboolean make_backup,
1391 GFileCreateFlags flags,
1392 GCancellable *cancellable,
1397 g_return_val_if_fail (G_IS_FILE (file), NULL);
1399 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1402 iface = G_FILE_GET_IFACE (file);
1404 if (iface->replace == NULL)
1406 g_set_error (error, G_IO_ERROR,
1407 G_IO_ERROR_NOT_SUPPORTED,
1408 _("Operation not supported"));
1413 /* Handle empty tag string as NULL in consistent way. */
1414 if (etag && *etag == 0)
1417 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1421 * g_file_read_async:
1422 * @file: input #GFile.
1423 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1425 * @cancellable: optional #GCancellable object, %NULL to ignore.
1426 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1427 * @user_data: the data to pass to callback function
1429 * Asynchronously opens @file for reading.
1431 * For more details, see g_file_read() which is
1432 * the synchronous version of this call.
1434 * When the operation is finished, @callback will be called. You can then call
1435 * g_file_read_finish() to get the result of the operation.
1438 g_file_read_async (GFile *file,
1440 GCancellable *cancellable,
1441 GAsyncReadyCallback callback,
1446 g_return_if_fail (G_IS_FILE (file));
1448 iface = G_FILE_GET_IFACE (file);
1449 (* iface->read_async) (file,
1457 * g_file_read_finish:
1458 * @file: input #GFile.
1459 * @res: a #GAsyncResult.
1460 * @error: a #GError, or %NULL
1462 * Finishes an asynchronous file read operation started with
1463 * g_file_read_async().
1465 * Returns: a #GFileInputStream or %NULL on error.
1468 g_file_read_finish (GFile *file,
1474 g_return_val_if_fail (G_IS_FILE (file), NULL);
1475 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1477 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1479 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1480 if (g_simple_async_result_propagate_error (simple, error))
1484 iface = G_FILE_GET_IFACE (file);
1485 return (* iface->read_finish) (file, res, error);
1489 * g_file_append_to_async:
1490 * @file: input #GFile.
1491 * @flags: a set of #GFileCreateFlags.
1492 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1494 * @cancellable: optional #GCancellable object, %NULL to ignore.
1495 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1496 * @user_data: the data to pass to callback function
1498 * Asynchronously opens @file for appending.
1500 * For more details, see g_file_append_to() which is
1501 * the synchronous version of this call.
1503 * When the operation is finished, @callback will be called. You can then call
1504 * g_file_append_to_finish() to get the result of the operation.
1507 g_file_append_to_async (GFile *file,
1508 GFileCreateFlags flags,
1510 GCancellable *cancellable,
1511 GAsyncReadyCallback callback,
1516 g_return_if_fail (G_IS_FILE (file));
1518 iface = G_FILE_GET_IFACE (file);
1519 (* iface->append_to_async) (file,
1528 * g_file_append_to_finish:
1529 * @file: input #GFile.
1530 * @res: #GAsyncResult
1531 * @error: a #GError, or %NULL
1533 * Finishes an asynchronous file append operation started with
1534 * g_file_append_to_async().
1536 * Returns: a valid #GFileOutputStream or %NULL on error.
1539 g_file_append_to_finish (GFile *file,
1545 g_return_val_if_fail (G_IS_FILE (file), NULL);
1546 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1548 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1550 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1551 if (g_simple_async_result_propagate_error (simple, error))
1555 iface = G_FILE_GET_IFACE (file);
1556 return (* iface->append_to_finish) (file, res, error);
1560 * g_file_create_async:
1561 * @file: input #GFile.
1562 * @flags: a set of #GFileCreateFlags.
1563 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1565 * @cancellable: optional #GCancellable object, %NULL to ignore.
1566 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1567 * @user_data: the data to pass to callback function
1569 * Asynchronously creates a new file and returns an output stream for writing to it.
1570 * The file must not already exists.
1572 * For more details, see g_file_create() which is
1573 * the synchronous version of this call.
1575 * When the operation is finished, @callback will be called. You can then call
1576 * g_file_create_finish() to get the result of the operation.
1579 g_file_create_async (GFile *file,
1580 GFileCreateFlags flags,
1582 GCancellable *cancellable,
1583 GAsyncReadyCallback callback,
1588 g_return_if_fail (G_IS_FILE (file));
1590 iface = G_FILE_GET_IFACE (file);
1591 (* iface->create_async) (file,
1600 * g_file_create_finish:
1601 * @file: input #GFile.
1602 * @res: a #GAsyncResult.
1603 * @error: a #GError, or %NULL
1605 * Finishes an asynchronous file create operation started with
1606 * g_file_create_async().
1608 * Returns: a #GFileOutputStream or %NULL on error.
1611 g_file_create_finish (GFile *file,
1617 g_return_val_if_fail (G_IS_FILE (file), NULL);
1618 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1620 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1622 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1623 if (g_simple_async_result_propagate_error (simple, error))
1627 iface = G_FILE_GET_IFACE (file);
1628 return (* iface->create_finish) (file, res, error);
1632 * g_file_replace_async:
1633 * @file: input #GFile.
1634 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1635 * current #GFile, or NULL to ignore.
1636 * @make_backup: %TRUE if a backup should be created.
1637 * @flags: a set of #GFileCreateFlags.
1638 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1640 * @cancellable: optional #GCancellable object, %NULL to ignore.
1641 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1642 * @user_data: the data to pass to callback function
1644 * Asynchronously overwrites the file, replacing the contents, possibly
1645 * creating a backup copy of the file first.
1647 * For more details, see g_file_replace() which is
1648 * the synchronous version of this call.
1650 * When the operation is finished, @callback will be called. You can then call
1651 * g_file_replace_finish() to get the result of the operation.
1654 g_file_replace_async (GFile *file,
1656 gboolean make_backup,
1657 GFileCreateFlags flags,
1659 GCancellable *cancellable,
1660 GAsyncReadyCallback callback,
1665 g_return_if_fail (G_IS_FILE (file));
1667 iface = G_FILE_GET_IFACE (file);
1668 (* iface->replace_async) (file,
1679 * g_file_replace_finish:
1680 * @file: input #GFile.
1681 * @res: a #GAsyncResult.
1682 * @error: a #GError, or %NULL
1684 * Finishes an asynchronous file replace operation started with
1685 * g_file_replace_async().
1687 * Returns: a #GFileOutputStream, or %NULL on error.
1690 g_file_replace_finish (GFile *file,
1696 g_return_val_if_fail (G_IS_FILE (file), NULL);
1697 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1699 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1701 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1702 if (g_simple_async_result_propagate_error (simple, error))
1706 iface = G_FILE_GET_IFACE (file);
1707 return (* iface->replace_finish) (file, res, error);
1711 copy_symlink (GFile *destination,
1712 GFileCopyFlags flags,
1713 GCancellable *cancellable,
1718 gboolean tried_delete;
1720 GFileType file_type;
1722 tried_delete = FALSE;
1726 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1728 /* Maybe it already existed, and we want to overwrite? */
1729 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1730 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1732 g_error_free (my_error);
1735 /* Don't overwrite if the destination is a directory */
1736 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1737 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1738 cancellable, &my_error);
1741 file_type = g_file_info_get_file_type (info);
1742 g_object_unref (info);
1744 if (file_type == G_FILE_TYPE_DIRECTORY)
1746 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1747 _("Can't copy over directory"));
1752 if (!g_file_delete (destination, cancellable, error))
1755 tried_delete = TRUE;
1759 g_propagate_error (error, my_error);
1766 static GInputStream *
1767 open_source_for_copy (GFile *source,
1769 GFileCopyFlags flags,
1770 GCancellable *cancellable,
1776 GFileType file_type;
1779 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1783 /* There was an error opening the source, try to set a good error for it: */
1785 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1787 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1788 * as that is less useful to the app. Better check for errors on the
1791 g_error_free (my_error);
1794 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1795 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1796 cancellable, &my_error);
1799 file_type = g_file_info_get_file_type (info);
1800 g_object_unref (info);
1802 if (flags & G_FILE_COPY_OVERWRITE)
1804 if (file_type == G_FILE_TYPE_DIRECTORY)
1806 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1807 _("Can't copy directory over directory"));
1810 /* continue to would_recurse error */
1814 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1815 _("Target file exists"));
1821 /* Error getting info from target, return that error
1822 * (except for NOT_FOUND, which is no error here)
1824 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1826 g_propagate_error (error, my_error);
1829 g_error_free (my_error);
1832 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1833 _("Can't recursively copy directory"));
1837 g_propagate_error (error, my_error);
1842 should_copy (GFileAttributeInfo *info,
1846 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1847 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1851 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1852 GFileAttributeInfoList *namespaces,
1860 s = g_string_new ("");
1864 for (i = 0; i < attributes->n_infos; i++)
1866 if (should_copy (&attributes->infos[i], as_move))
1871 g_string_append_c (s, ',');
1873 g_string_append (s, attributes->infos[i].name);
1880 for (i = 0; i < namespaces->n_infos; i++)
1882 if (should_copy (&namespaces->infos[i], as_move))
1887 g_string_append_c (s, ',');
1889 g_string_append (s, namespaces->infos[i].name);
1890 g_string_append (s, ":*");
1895 return g_string_free (s, FALSE);
1899 * g_file_copy_attributes:
1900 * @source: a #GFile with attributes.
1901 * @destination: a #GFile to copy attributes to.
1902 * @flags: a set of #GFileCopyFlags.
1903 * @cancellable: optional #GCancellable object, %NULL to ignore.
1904 * @error: a #GError, %NULL to ignore.
1906 * Copies the file attributes from @source to @destination.
1908 * Normally only a subset of the file attributes are copied,
1909 * those that are copies in a normal file copy operation
1910 * (which for instance does not include e.g. mtime). However
1911 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
1912 * all the metadata that is possible to copy is copied.
1914 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1917 g_file_copy_attributes (GFile *source,
1919 GFileCopyFlags flags,
1920 GCancellable *cancellable,
1923 GFileAttributeInfoList *attributes, *namespaces;
1924 char *attrs_to_read;
1928 gboolean source_nofollow_symlinks;
1930 as_move = flags & G_FILE_COPY_ALL_METADATA;
1931 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1933 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1934 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1935 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1937 if (attributes == NULL && namespaces == NULL)
1940 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1942 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1943 * we just don't copy it.
1945 info = g_file_query_info (source, attrs_to_read,
1946 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1950 g_free (attrs_to_read);
1955 res = g_file_set_attributes_from_info (destination,
1959 g_object_unref (info);
1962 g_file_attribute_info_list_unref (attributes);
1963 g_file_attribute_info_list_unref (namespaces);
1968 /* Closes the streams */
1970 copy_stream_with_progress (GInputStream *in,
1972 GCancellable *cancellable,
1973 GFileProgressCallback progress_callback,
1974 gpointer progress_callback_data,
1977 gssize n_read, n_written;
1978 goffset current_size;
1979 char buffer[8192], *p;
1985 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1986 G_FILE_ATTRIBUTE_STANDARD_SIZE,
1990 total_size = g_file_info_get_size (info);
1991 g_object_unref (info);
1998 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2008 current_size += n_read;
2013 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2014 if (n_written == -1)
2021 n_read -= n_written;
2027 if (progress_callback)
2028 progress_callback (current_size, total_size, progress_callback_data);
2032 error = NULL; /* Ignore further errors */
2034 /* Make sure we send full copied size */
2035 if (progress_callback)
2036 progress_callback (current_size, total_size, progress_callback_data);
2039 /* Don't care about errors in source here */
2040 g_input_stream_close (in, cancellable, NULL);
2042 /* But write errors on close are bad! */
2043 if (!g_output_stream_close (out, cancellable, error))
2046 g_object_unref (in);
2047 g_object_unref (out);
2053 file_copy_fallback (GFile *source,
2055 GFileCopyFlags flags,
2056 GCancellable *cancellable,
2057 GFileProgressCallback progress_callback,
2058 gpointer progress_callback_data,
2066 /* Maybe copy the symlink? */
2067 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2069 info = g_file_query_info (source,
2070 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2071 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2077 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2078 (target = g_file_info_get_symlink_target (info)) != NULL)
2080 if (!copy_symlink (destination, flags, cancellable, target, error))
2082 g_object_unref (info);
2086 g_object_unref (info);
2090 g_object_unref (info);
2093 in = open_source_for_copy (source, destination, flags, cancellable, error);
2097 if (flags & G_FILE_COPY_OVERWRITE)
2099 out = (GOutputStream *)g_file_replace (destination,
2101 flags & G_FILE_COPY_BACKUP,
2102 cancellable, error);
2106 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2111 g_object_unref (in);
2115 if (!copy_stream_with_progress (in, out, cancellable,
2116 progress_callback, progress_callback_data,
2122 /* Ignore errors here. Failure to copy metadata is not a hard error */
2123 g_file_copy_attributes (source, destination,
2124 flags, cancellable, NULL);
2131 * @source: input #GFile.
2132 * @destination: destination #GFile
2133 * @flags: set of #GFileCopyFlags
2134 * @cancellable: optional #GCancellable object, %NULL to ignore.
2135 * @progress_callback: function to callback with progress information
2136 * @progress_callback_data: user data to pass to @progress_callback
2137 * @error: #GError to set on error, or %NULL
2139 * Copies the file @source to the location specified by @destination.
2140 * Can not handle recursive copies of directories.
2142 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2143 * existing @destination file is overwritten.
2145 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2146 * will be copied as symlinks, otherwise the target of the
2147 * @source symlink will be copied.
2149 * If @cancellable is not %NULL, then the operation can be cancelled by
2150 * triggering the cancellable object from another thread. If the operation
2151 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2153 * If @progress_callback is not %NULL, then the operation can be monitored by
2154 * setting this to a #GFileProgressCallback function. @progress_callback_data
2155 * will be passed to this function. It is guaranteed that this callback will
2156 * be called after all data has been transferred with the total number of bytes
2157 * copied during the operation.
2159 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2160 * error is returned, independent on the status of the @destination.
2162 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2163 * error G_IO_ERROR_EXISTS is returned.
2165 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2166 * error is returned. If trying to overwrite a directory with a directory the
2167 * G_IO_ERROR_WOULD_MERGE error is returned.
2169 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2170 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2173 * If you are interested in copying the #GFile object itself (not the on-disk
2174 * file), see g_file_dup().
2176 * Returns: %TRUE on success, %FALSE otherwise.
2179 g_file_copy (GFile *source,
2181 GFileCopyFlags flags,
2182 GCancellable *cancellable,
2183 GFileProgressCallback progress_callback,
2184 gpointer progress_callback_data,
2191 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2192 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2194 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2197 iface = G_FILE_GET_IFACE (destination);
2201 res = (* iface->copy) (source, destination,
2203 progress_callback, progress_callback_data,
2209 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2211 g_propagate_error (error, my_error);
2216 /* If the types are different, and the destination method failed
2217 also try the source method */
2218 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2220 iface = G_FILE_GET_IFACE (source);
2225 res = (* iface->copy) (source, destination,
2227 progress_callback, progress_callback_data,
2233 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2235 g_propagate_error (error, my_error);
2241 return file_copy_fallback (source, destination, flags, cancellable,
2242 progress_callback, progress_callback_data,
2249 * @source: #GFile pointing to the source location.
2250 * @destination: #GFile pointing to the destination location.
2251 * @flags: set of #GFileCopyFlags.
2252 * @cancellable: optional #GCancellable object, %NULL to ignore.
2253 * @progress_callback: #GFileProgressCallback function for updates.
2254 * @progress_callback_data: gpointer to user data for the callback function.
2255 * @error: #GError for returning error conditions, or %NULL
2258 * Tries to move the file or directory @source to the location specified by @destination.
2259 * If native move operations are supported then this is used, otherwise a copy + delete
2260 * fallback is used. The native implementation may support moving directories (for instance
2261 * on moves inside the same filesystem), but the fallback code does not.
2263 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2264 * existing @destination file is overwritten.
2266 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2267 * will be copied as symlinks, otherwise the target of the
2268 * @source symlink will be copied.
2270 * If @cancellable is not %NULL, then the operation can be cancelled by
2271 * triggering the cancellable object from another thread. If the operation
2272 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2274 * If @progress_callback is not %NULL, then the operation can be monitored by
2275 * setting this to a #GFileProgressCallback function. @progress_callback_data
2276 * will be passed to this function. It is guaranteed that this callback will
2277 * be called after all data has been transferred with the total number of bytes
2278 * copied during the operation.
2280 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2281 * error is returned, independent on the status of the @destination.
2283 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2284 * error G_IO_ERROR_EXISTS is returned.
2286 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2287 * error is returned. If trying to overwrite a directory with a directory the
2288 * G_IO_ERROR_WOULD_MERGE error is returned.
2290 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2291 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2292 * may be returned (if the native move operation isn't available).
2294 * Returns: %TRUE on successful move, %FALSE otherwise.
2297 g_file_move (GFile *source,
2299 GFileCopyFlags flags,
2300 GCancellable *cancellable,
2301 GFileProgressCallback progress_callback,
2302 gpointer progress_callback_data,
2309 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2310 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2312 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2315 iface = G_FILE_GET_IFACE (destination);
2319 res = (* iface->move) (source, destination,
2321 progress_callback, progress_callback_data,
2327 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2329 g_propagate_error (error, my_error);
2334 /* If the types are different, and the destination method failed
2335 also try the source method */
2336 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2338 iface = G_FILE_GET_IFACE (source);
2343 res = (* iface->move) (source, destination,
2345 progress_callback, progress_callback_data,
2351 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2353 g_propagate_error (error, my_error);
2359 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2361 g_set_error (error, G_IO_ERROR,
2362 G_IO_ERROR_NOT_SUPPORTED,
2363 _("Operation not supported"));
2367 flags |= G_FILE_COPY_ALL_METADATA;
2368 if (!g_file_copy (source, destination, flags, cancellable,
2369 progress_callback, progress_callback_data,
2373 return g_file_delete (source, cancellable, error);
2377 * g_file_make_directory
2378 * @file: input #GFile.
2379 * @cancellable: optional #GCancellable object, %NULL to ignore.
2380 * @error: a #GError, or %NULL
2382 * Creates a directory.
2384 * If @cancellable is not %NULL, then the operation can be cancelled by
2385 * triggering the cancellable object from another thread. If the operation
2386 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2388 * Returns: %TRUE on successful creation, %FALSE otherwise.
2391 g_file_make_directory (GFile *file,
2392 GCancellable *cancellable,
2397 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2399 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2402 iface = G_FILE_GET_IFACE (file);
2404 if (iface->make_directory == NULL)
2406 g_set_error (error, G_IO_ERROR,
2407 G_IO_ERROR_NOT_SUPPORTED,
2408 _("Operation not supported"));
2412 return (* iface->make_directory) (file, cancellable, error);
2416 * g_file_make_symbolic_link:
2417 * @file: input #GFile.
2418 * @symlink_value: a string with the value of the new symlink.
2419 * @cancellable: optional #GCancellable object, %NULL to ignore.
2420 * @error: a #GError.
2422 * Creates a symbolic link.
2424 * If @cancellable is not %NULL, then the operation can be cancelled by
2425 * triggering the cancellable object from another thread. If the operation
2426 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2428 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2431 g_file_make_symbolic_link (GFile *file,
2432 const char *symlink_value,
2433 GCancellable *cancellable,
2438 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2439 g_return_val_if_fail (symlink_value != NULL, FALSE);
2441 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2444 if (*symlink_value == '\0')
2446 g_set_error (error, G_IO_ERROR,
2447 G_IO_ERROR_INVALID_ARGUMENT,
2448 _("Invalid symlink value given"));
2452 iface = G_FILE_GET_IFACE (file);
2454 if (iface->make_symbolic_link == NULL)
2456 g_set_error (error, G_IO_ERROR,
2457 G_IO_ERROR_NOT_SUPPORTED,
2458 _("Operation not supported"));
2462 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2467 * @file: input #GFile.
2468 * @cancellable: optional #GCancellable object, %NULL to ignore.
2469 * @error: a #GError, or %NULL
2473 * If @cancellable is not %NULL, then the operation can be cancelled by
2474 * triggering the cancellable object from another thread. If the operation
2475 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2477 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2480 g_file_delete (GFile *file,
2481 GCancellable *cancellable,
2486 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2488 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2491 iface = G_FILE_GET_IFACE (file);
2493 if (iface->delete_file == NULL)
2495 g_set_error (error, G_IO_ERROR,
2496 G_IO_ERROR_NOT_SUPPORTED,
2497 _("Operation not supported"));
2501 return (* iface->delete_file) (file, cancellable, error);
2506 * @file: #GFile to send to trash.
2507 * @cancellable: optional #GCancellable object, %NULL to ignore.
2508 * @error: a #GError, or %NULL
2510 * Sends @file to the "Trashcan", if possible. This is similar to
2511 * deleting it, but the user can recover it before emptying the trashcan.
2512 * Not all file systems support trashing, so this call can return the
2513 * %G_IO_ERROR_NOT_SUPPORTED error.
2516 * If @cancellable is not %NULL, then the operation can be cancelled by
2517 * triggering the cancellable object from another thread. If the operation
2518 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2520 * Returns: %TRUE on successful trash, %FALSE otherwise.
2523 g_file_trash (GFile *file,
2524 GCancellable *cancellable,
2529 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2531 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2534 iface = G_FILE_GET_IFACE (file);
2536 if (iface->trash == NULL)
2539 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2540 _("Trash not supported"));
2544 return (* iface->trash) (file, cancellable, error);
2548 * g_file_set_display_name:
2549 * @file: input #GFile.
2550 * @display_name: a string.
2551 * @cancellable: optional #GCancellable object, %NULL to ignore.
2552 * @error: a #GError, or %NULL
2554 * Renames @file to the specified display name.
2556 * The display name is converted from UTF8 to the correct encoding for the target
2557 * filesystem if possible and the @file is renamed to this.
2559 * If you want to implement a rename operation in the user interface the edit name
2560 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2561 * widget, and then the result after editing should be passed to g_file_set_display_name().
2563 * On success the resulting converted filename is returned.
2565 * If @cancellable is not %NULL, then the operation can be cancelled by
2566 * triggering the cancellable object from another thread. If the operation
2567 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2569 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2572 g_file_set_display_name (GFile *file,
2573 const char *display_name,
2574 GCancellable *cancellable,
2579 g_return_val_if_fail (G_IS_FILE (file), NULL);
2580 g_return_val_if_fail (display_name != NULL, NULL);
2582 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2586 G_IO_ERROR_INVALID_ARGUMENT,
2587 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2591 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2594 iface = G_FILE_GET_IFACE (file);
2596 return (* iface->set_display_name) (file, display_name, cancellable, error);
2600 * g_file_set_display_name_async:
2601 * @file: input #GFile.
2602 * @display_name: a string.
2603 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2605 * @cancellable: optional #GCancellable object, %NULL to ignore.
2606 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2607 * @user_data: the data to pass to callback function
2609 * Asynchronously sets the display name for a given #GFile.
2611 * For more details, see g_set_display_name() which is
2612 * the synchronous version of this call.
2614 * When the operation is finished, @callback will be called. You can then call
2615 * g_file_set_display_name_finish() to get the result of the operation.
2618 g_file_set_display_name_async (GFile *file,
2619 const char *display_name,
2621 GCancellable *cancellable,
2622 GAsyncReadyCallback callback,
2627 g_return_if_fail (G_IS_FILE (file));
2628 g_return_if_fail (display_name != NULL);
2630 iface = G_FILE_GET_IFACE (file);
2631 (* iface->set_display_name_async) (file,
2640 * g_file_set_display_name_finish:
2641 * @file: input #GFile.
2642 * @res: a #GAsyncResult.
2643 * @error: a #GError, or %NULL
2645 * Finishes setting a display name started with
2646 * g_file_set_display_name_async().
2648 * Returns: a #GFile or %NULL on error.
2651 g_file_set_display_name_finish (GFile *file,
2657 g_return_val_if_fail (G_IS_FILE (file), NULL);
2658 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2660 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2662 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2663 if (g_simple_async_result_propagate_error (simple, error))
2667 iface = G_FILE_GET_IFACE (file);
2668 return (* iface->set_display_name_finish) (file, res, error);
2672 * g_file_query_settable_attributes:
2673 * @file: input #GFile.
2674 * @cancellable: optional #GCancellable object, %NULL to ignore.
2675 * @error: a #GError, or %NULL
2677 * Obtain the list of settable attributes for the file.
2679 * Returns the type and full attribute name of all the attributes
2680 * that can be set on this file. This doesn't mean setting it will always
2681 * succeed though, you might get an access failure, or some specific
2682 * file may not support a specific attribute.
2684 * If @cancellable is not %NULL, then the operation can be cancelled by
2685 * triggering the cancellable object from another thread. If the operation
2686 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2688 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2689 * When you are done with it, release it with g_file_attribute_info_list_unref()
2691 GFileAttributeInfoList *
2692 g_file_query_settable_attributes (GFile *file,
2693 GCancellable *cancellable,
2698 GFileAttributeInfoList *list;
2700 g_return_val_if_fail (G_IS_FILE (file), NULL);
2702 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2705 iface = G_FILE_GET_IFACE (file);
2707 if (iface->query_settable_attributes == NULL)
2708 return g_file_attribute_info_list_new ();
2711 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2715 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2717 list = g_file_attribute_info_list_new ();
2718 g_error_free (my_error);
2721 g_propagate_error (error, my_error);
2728 * g_file_query_writable_namespaces:
2729 * @file: input #GFile.
2730 * @cancellable: optional #GCancellable object, %NULL to ignore.
2731 * @error: a #GError, or %NULL
2733 * Obtain the list of attribute namespaces where new attributes
2734 * can be created by a user. An example of this is extended
2735 * attributes (in the "xattr" namespace).
2737 * If @cancellable is not %NULL, then the operation can be cancelled by
2738 * triggering the cancellable object from another thread. If the operation
2739 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2741 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2742 * When you are done with it, release it with g_file_attribute_info_list_unref()
2744 GFileAttributeInfoList *
2745 g_file_query_writable_namespaces (GFile *file,
2746 GCancellable *cancellable,
2751 GFileAttributeInfoList *list;
2753 g_return_val_if_fail (G_IS_FILE (file), NULL);
2755 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2758 iface = G_FILE_GET_IFACE (file);
2760 if (iface->query_writable_namespaces == NULL)
2761 return g_file_attribute_info_list_new ();
2764 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2768 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2770 list = g_file_attribute_info_list_new ();
2771 g_error_free (my_error);
2774 g_propagate_error (error, my_error);
2781 * g_file_set_attribute:
2782 * @file: input #GFile.
2783 * @attribute: a string containing the attribute's name.
2784 * @type: The type of the attribute
2785 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2786 * @flags: a set of #GFileQueryInfoFlags.
2787 * @cancellable: optional #GCancellable object, %NULL to ignore.
2788 * @error: a #GError, or %NULL
2790 * Sets an attribute in the file with attribute name @attribute to @value.
2792 * If @cancellable is not %NULL, then the operation can be cancelled by
2793 * triggering the cancellable object from another thread. If the operation
2794 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2796 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2799 g_file_set_attribute (GFile *file,
2800 const char *attribute,
2801 GFileAttributeType type,
2803 GFileQueryInfoFlags flags,
2804 GCancellable *cancellable,
2809 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2810 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2812 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2815 iface = G_FILE_GET_IFACE (file);
2817 if (iface->set_attribute == NULL)
2819 g_set_error (error, G_IO_ERROR,
2820 G_IO_ERROR_NOT_SUPPORTED,
2821 _("Operation not supported"));
2825 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2829 * g_file_set_attributes_from_info:
2830 * @file: input #GFile.
2831 * @info: a #GFileInfo.
2832 * @flags: #GFileQueryInfoFlags
2833 * @cancellable: optional #GCancellable object, %NULL to ignore.
2834 * @error: a #GError, or %NULL
2836 * Tries to set all attributes in the #GFileInfo on the target values,
2837 * not stopping on the first error.
2839 * If there is any error during this operation then @error will be set to
2840 * the first error. Error on particular fields are flagged by setting
2841 * the "status" field in the attribute value to
2842 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2845 * If @cancellable is not %NULL, then the operation can be cancelled by
2846 * triggering the cancellable object from another thread. If the operation
2847 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2849 * Returns: %TRUE if there was any error, %FALSE otherwise.
2852 g_file_set_attributes_from_info (GFile *file,
2854 GFileQueryInfoFlags flags,
2855 GCancellable *cancellable,
2860 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2861 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2863 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2866 g_file_info_clear_status (info);
2868 iface = G_FILE_GET_IFACE (file);
2870 return (* iface->set_attributes_from_info) (file,
2879 g_file_real_set_attributes_from_info (GFile *file,
2881 GFileQueryInfoFlags flags,
2882 GCancellable *cancellable,
2888 GFileAttributeValue *value;
2892 attributes = g_file_info_list_attributes (info, NULL);
2894 for (i = 0; attributes[i] != NULL; i++)
2896 value = _g_file_info_get_attribute_value (info, attributes[i]);
2898 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2901 if (!g_file_set_attribute (file, attributes[i],
2902 value->type, _g_file_attribute_value_peek_as_pointer (value),
2903 flags, cancellable, error))
2905 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2907 /* Don't set error multiple times */
2911 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2914 g_strfreev (attributes);
2920 * g_file_set_attributes_async:
2921 * @file: input #GFile.
2922 * @info: a #GFileInfo.
2923 * @flags: a #GFileQueryInfoFlags.
2924 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2926 * @cancellable: optional #GCancellable object, %NULL to ignore.
2927 * @callback: a #GAsyncReadyCallback.
2928 * @user_data: a #gpointer.
2930 * Asynchronously sets the attributes of @file with @info.
2932 * For more details, see g_file_set_attributes_from_info() which is
2933 * the synchronous version of this call.
2935 * When the operation is finished, @callback will be called. You can then call
2936 * g_file_set_attributes_finish() to get the result of the operation.
2939 g_file_set_attributes_async (GFile *file,
2941 GFileQueryInfoFlags flags,
2943 GCancellable *cancellable,
2944 GAsyncReadyCallback callback,
2949 g_return_if_fail (G_IS_FILE (file));
2950 g_return_if_fail (G_IS_FILE_INFO (info));
2952 iface = G_FILE_GET_IFACE (file);
2953 (* iface->set_attributes_async) (file,
2963 * g_file_set_attributes_finish:
2964 * @file: input #GFile.
2965 * @result: a #GAsyncResult.
2966 * @info: a #GFileInfo.
2967 * @error: a #GError, or %NULL
2969 * Finishes setting an attribute started in g_file_set_attributes_async().
2971 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2974 g_file_set_attributes_finish (GFile *file,
2975 GAsyncResult *result,
2981 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2982 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2984 /* No standard handling of errors here, as we must set info even
2987 iface = G_FILE_GET_IFACE (file);
2988 return (* iface->set_attributes_finish) (file, result, info, error);
2992 * g_file_set_attribute_string:
2993 * @file: input #GFile.
2994 * @attribute: a string containing the attribute's name.
2995 * @value: a string containing the attribute's value.
2996 * @flags: #GFileQueryInfoFlags.
2997 * @cancellable: optional #GCancellable object, %NULL to ignore.
2998 * @error: a #GError, or %NULL
3000 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3001 * If @attribute is of a different type, this operation will fail.
3003 * If @cancellable is not %NULL, then the operation can be cancelled by
3004 * triggering the cancellable object from another thread. If the operation
3005 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3007 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3010 g_file_set_attribute_string (GFile *file,
3011 const char *attribute,
3013 GFileQueryInfoFlags flags,
3014 GCancellable *cancellable,
3017 return g_file_set_attribute (file, attribute,
3018 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3019 flags, cancellable, error);
3023 * g_file_set_attribute_byte_string:
3024 * @file: input #GFile.
3025 * @attribute: a string containing the attribute's name.
3026 * @value: a string containing the attribute's new value.
3027 * @flags: a #GFileQueryInfoFlags.
3028 * @cancellable: optional #GCancellable object, %NULL to ignore.
3029 * @error: a #GError, or %NULL
3031 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3032 * If @attribute is of a different type, this operation will fail,
3035 * If @cancellable is not %NULL, then the operation can be cancelled by
3036 * triggering the cancellable object from another thread. If the operation
3037 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3039 * Returns: %TRUE if the @attribute was successfully set to @value
3040 * in the @file, %FALSE otherwise.
3043 g_file_set_attribute_byte_string (GFile *file,
3044 const char *attribute,
3046 GFileQueryInfoFlags flags,
3047 GCancellable *cancellable,
3050 return g_file_set_attribute (file, attribute,
3051 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3052 flags, cancellable, error);
3056 * g_file_set_attribute_uint32:
3057 * @file: input #GFile.
3058 * @attribute: a string containing the attribute's name.
3059 * @value: a #guint32 containing the attribute's new value.
3060 * @flags: a #GFileQueryInfoFlags.
3061 * @cancellable: optional #GCancellable object, %NULL to ignore.
3062 * @error: a #GError, or %NULL
3064 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3065 * If @attribute is of a different type, this operation will fail.
3067 * If @cancellable is not %NULL, then the operation can be cancelled by
3068 * triggering the cancellable object from another thread. If the operation
3069 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3071 * Returns: %TRUE if the @attribute was successfully set to @value
3072 * in the @file, %FALSE otherwise.
3075 g_file_set_attribute_uint32 (GFile *file,
3076 const char *attribute,
3078 GFileQueryInfoFlags flags,
3079 GCancellable *cancellable,
3082 return g_file_set_attribute (file, attribute,
3083 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3084 flags, cancellable, error);
3088 * g_file_set_attribute_int32:
3089 * @file: input #GFile.
3090 * @attribute: a string containing the attribute's name.
3091 * @value: a #gint32 containing the attribute's new value.
3092 * @flags: a #GFileQueryInfoFlags.
3093 * @cancellable: optional #GCancellable object, %NULL to ignore.
3094 * @error: a #GError, or %NULL
3096 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3097 * If @attribute is of a different type, this operation will fail.
3099 * If @cancellable is not %NULL, then the operation can be cancelled by
3100 * triggering the cancellable object from another thread. If the operation
3101 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3103 * Returns: %TRUE if the @attribute was successfully set to @value
3104 * in the @file, %FALSE otherwise.
3107 g_file_set_attribute_int32 (GFile *file,
3108 const char *attribute,
3110 GFileQueryInfoFlags flags,
3111 GCancellable *cancellable,
3114 return g_file_set_attribute (file, attribute,
3115 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3116 flags, cancellable, error);
3120 * g_file_set_attribute_uint64:
3121 * @file: input #GFile.
3122 * @attribute: a string containing the attribute's name.
3123 * @value: a #guint64 containing the attribute's new value.
3124 * @flags: a #GFileQueryInfoFlags.
3125 * @cancellable: optional #GCancellable object, %NULL to ignore.
3126 * @error: a #GError, or %NULL
3128 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3129 * If @attribute is of a different type, this operation will fail.
3131 * If @cancellable is not %NULL, then the operation can be cancelled by
3132 * triggering the cancellable object from another thread. If the operation
3133 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3135 * Returns: %TRUE if the @attribute was successfully set to @value
3136 * in the @file, %FALSE otherwise.
3139 g_file_set_attribute_uint64 (GFile *file,
3140 const char *attribute,
3142 GFileQueryInfoFlags flags,
3143 GCancellable *cancellable,
3146 return g_file_set_attribute (file, attribute,
3147 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3148 flags, cancellable, error);
3152 * g_file_set_attribute_int64:
3153 * @file: input #GFile.
3154 * @attribute: a string containing the attribute's name.
3155 * @value: a #guint64 containing the attribute's new value.
3156 * @flags: a #GFileQueryInfoFlags.
3157 * @cancellable: optional #GCancellable object, %NULL to ignore.
3158 * @error: a #GError, or %NULL
3160 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3161 * If @attribute is of a different type, this operation will fail.
3163 * If @cancellable is not %NULL, then the operation can be cancelled by
3164 * triggering the cancellable object from another thread. If the operation
3165 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3167 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3170 g_file_set_attribute_int64 (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_INT64, &value,
3179 flags, cancellable, error);
3183 * g_file_mount_mountable:
3184 * @file: input #GFile.
3185 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3186 * @cancellable: optional #GCancellable object, %NULL to ignore.
3187 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3188 * @user_data: the data to pass to callback function
3190 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3191 * Using @mount_operation, you can request callbacks when, for instance,
3192 * passwords are needed during authentication.
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 * When the operation is finished, @callback will be called. You can then call
3199 * g_file_mount_mountable_finish() to get the result of the operation.
3202 g_file_mount_mountable (GFile *file,
3203 GMountOperation *mount_operation,
3204 GCancellable *cancellable,
3205 GAsyncReadyCallback callback,
3210 g_return_if_fail (G_IS_FILE (file));
3212 iface = G_FILE_GET_IFACE (file);
3214 if (iface->mount_mountable == NULL)
3215 g_simple_async_report_error_in_idle (G_OBJECT (file),
3219 G_IO_ERROR_NOT_SUPPORTED,
3220 _("Operation not supported"));
3222 (* iface->mount_mountable) (file,
3230 * g_file_mount_mountable_finish:
3231 * @file: input #GFile.
3232 * @result: a #GAsyncResult.
3233 * @error: a #GError, or %NULL
3235 * Finishes a mount operation. See g_file_mount_mountable() for details.
3237 * Finish an asynchronous mount operation that was started
3238 * with g_file_mount_mountable().
3240 * Returns: a #GFile or %NULL on error.
3243 g_file_mount_mountable_finish (GFile *file,
3244 GAsyncResult *result,
3249 g_return_val_if_fail (G_IS_FILE (file), NULL);
3250 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3252 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3254 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3255 if (g_simple_async_result_propagate_error (simple, error))
3259 iface = G_FILE_GET_IFACE (file);
3260 return (* iface->mount_mountable_finish) (file, result, error);
3264 * g_file_unmount_mountable:
3265 * @file: input #GFile.
3266 * @flags: flags affecting the operation
3267 * @cancellable: optional #GCancellable object, %NULL to ignore.
3268 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3269 * @user_data: the data to pass to callback function
3271 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3273 * If @cancellable is not %NULL, then the operation can be cancelled by
3274 * triggering the cancellable object from another thread. If the operation
3275 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3277 * When the operation is finished, @callback will be called. You can then call
3278 * g_file_unmount_mountable_finish() to get the result of the operation.
3281 g_file_unmount_mountable (GFile *file,
3282 GMountUnmountFlags flags,
3283 GCancellable *cancellable,
3284 GAsyncReadyCallback callback,
3289 g_return_if_fail (G_IS_FILE (file));
3291 iface = G_FILE_GET_IFACE (file);
3293 if (iface->unmount_mountable == NULL)
3294 g_simple_async_report_error_in_idle (G_OBJECT (file),
3298 G_IO_ERROR_NOT_SUPPORTED,
3299 _("Operation not supported"));
3301 (* iface->unmount_mountable) (file,
3309 * g_file_unmount_mountable_finish:
3310 * @file: input #GFile.
3311 * @result: a #GAsyncResult.
3312 * @error: a #GError, or %NULL
3314 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3316 * Finish an asynchronous unmount operation that was started
3317 * with g_file_unmount_mountable().
3319 * Returns: %TRUE if the operation finished successfully. %FALSE
3323 g_file_unmount_mountable_finish (GFile *file,
3324 GAsyncResult *result,
3329 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3330 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3332 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3334 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3335 if (g_simple_async_result_propagate_error (simple, error))
3339 iface = G_FILE_GET_IFACE (file);
3340 return (* iface->unmount_mountable_finish) (file, result, error);
3344 * g_file_eject_mountable:
3345 * @file: input #GFile.
3346 * @flags: flags affecting the operation
3347 * @cancellable: optional #GCancellable object, %NULL to ignore.
3348 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3349 * @user_data: the data to pass to callback function
3351 * Starts an asynchronous eject on a mountable.
3352 * When this operation has completed, @callback will be called with
3353 * @user_user data, and the operation can be finalized with
3354 * g_file_eject_mountable_finish().
3356 * If @cancellable is not %NULL, then the operation can be cancelled by
3357 * triggering the cancellable object from another thread. If the operation
3358 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3361 g_file_eject_mountable (GFile *file,
3362 GMountUnmountFlags flags,
3363 GCancellable *cancellable,
3364 GAsyncReadyCallback callback,
3369 g_return_if_fail (G_IS_FILE (file));
3371 iface = G_FILE_GET_IFACE (file);
3373 if (iface->eject_mountable == NULL)
3374 g_simple_async_report_error_in_idle (G_OBJECT (file),
3378 G_IO_ERROR_NOT_SUPPORTED,
3379 _("Operation not supported"));
3381 (* iface->eject_mountable) (file,
3389 * g_file_eject_mountable_finish:
3390 * @file: input #GFile.
3391 * @result: a #GAsyncResult.
3392 * @error: a #GError, or %NULL
3394 * Finishes an asynchronous eject operation started by
3395 * g_file_eject_mountable().
3397 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3401 g_file_eject_mountable_finish (GFile *file,
3402 GAsyncResult *result,
3407 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3408 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3410 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3412 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3413 if (g_simple_async_result_propagate_error (simple, error))
3417 iface = G_FILE_GET_IFACE (file);
3418 return (* iface->eject_mountable_finish) (file, result, error);
3422 * g_file_monitor_directory:
3423 * @file: input #GFile.
3424 * @flags: a set of #GFileMonitorFlags.
3425 * @cancellable: optional #GCancellable object, %NULL to ignore.
3426 * @error: a #GError, or %NULL.
3428 * Obtains a directory monitor for the given file.
3429 * This may fail if directory monitoring is not supported.
3431 * If @cancellable is not %NULL, then the operation can be cancelled by
3432 * triggering the cancellable object from another thread. If the operation
3433 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3435 * Returns: a #GFileMonitor for the given @file,
3436 * or %NULL on error.
3439 g_file_monitor_directory (GFile *file,
3440 GFileMonitorFlags flags,
3441 GCancellable *cancellable,
3446 g_return_val_if_fail (G_IS_FILE (file), NULL);
3448 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3451 iface = G_FILE_GET_IFACE (file);
3453 if (iface->monitor_dir == NULL)
3455 g_set_error (error, G_IO_ERROR,
3456 G_IO_ERROR_NOT_SUPPORTED,
3457 _("Operation not supported"));
3461 return (* iface->monitor_dir) (file, flags, cancellable, error);
3465 * g_file_monitor_file:
3466 * @file: input #GFile.
3467 * @flags: a set of #GFileMonitorFlags.
3468 * @cancellable: optional #GCancellable object, %NULL to ignore.
3469 * @error: a #GError, or %NULL.
3471 * Obtains a file monitor for the given file. If no file notification
3472 * mechanism exists, then regular polling of the file is used.
3474 * If @cancellable is not %NULL, then the operation can be cancelled by
3475 * triggering the cancellable object from another thread. If the operation
3476 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3478 * Returns: a #GFileMonitor for the given @file.
3481 g_file_monitor_file (GFile *file,
3482 GFileMonitorFlags flags,
3483 GCancellable *cancellable,
3487 GFileMonitor *monitor;
3489 g_return_val_if_fail (G_IS_FILE (file), NULL);
3491 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3494 iface = G_FILE_GET_IFACE (file);
3498 if (iface->monitor_file)
3499 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3501 /* Fallback to polling */
3502 if (monitor == NULL)
3503 monitor = _g_poll_file_monitor_new (file);
3508 /********************************************
3509 * Default implementation of async ops *
3510 ********************************************/
3514 GFileQueryInfoFlags flags;
3516 } QueryInfoAsyncData;
3519 query_info_data_free (QueryInfoAsyncData *data)
3522 g_object_unref (data->info);
3523 g_free (data->attributes);
3528 query_info_async_thread (GSimpleAsyncResult *res,
3530 GCancellable *cancellable)
3532 GError *error = NULL;
3533 QueryInfoAsyncData *data;
3536 data = g_simple_async_result_get_op_res_gpointer (res);
3538 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3542 g_simple_async_result_set_from_error (res, error);
3543 g_error_free (error);
3550 g_file_real_query_info_async (GFile *file,
3551 const char *attributes,
3552 GFileQueryInfoFlags flags,
3554 GCancellable *cancellable,
3555 GAsyncReadyCallback callback,
3558 GSimpleAsyncResult *res;
3559 QueryInfoAsyncData *data;
3561 data = g_new0 (QueryInfoAsyncData, 1);
3562 data->attributes = g_strdup (attributes);
3563 data->flags = flags;
3565 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3566 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3568 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3569 g_object_unref (res);
3573 g_file_real_query_info_finish (GFile *file,
3577 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3578 QueryInfoAsyncData *data;
3580 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3582 data = g_simple_async_result_get_op_res_gpointer (simple);
3584 return g_object_ref (data->info);
3591 GFileQueryInfoFlags flags;
3592 GFileEnumerator *enumerator;
3593 } EnumerateChildrenAsyncData;
3596 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3598 if (data->enumerator)
3599 g_object_unref (data->enumerator);
3600 g_free (data->attributes);
3605 enumerate_children_async_thread (GSimpleAsyncResult *res,
3607 GCancellable *cancellable)
3609 GError *error = NULL;
3610 EnumerateChildrenAsyncData *data;
3611 GFileEnumerator *enumerator;
3613 data = g_simple_async_result_get_op_res_gpointer (res);
3615 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3617 if (enumerator == NULL)
3619 g_simple_async_result_set_from_error (res, error);
3620 g_error_free (error);
3623 data->enumerator = enumerator;
3627 g_file_real_enumerate_children_async (GFile *file,
3628 const char *attributes,
3629 GFileQueryInfoFlags flags,
3631 GCancellable *cancellable,
3632 GAsyncReadyCallback callback,
3635 GSimpleAsyncResult *res;
3636 EnumerateChildrenAsyncData *data;
3638 data = g_new0 (EnumerateChildrenAsyncData, 1);
3639 data->attributes = g_strdup (attributes);
3640 data->flags = flags;
3642 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3643 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3645 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3646 g_object_unref (res);
3649 static GFileEnumerator *
3650 g_file_real_enumerate_children_finish (GFile *file,
3654 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3655 EnumerateChildrenAsyncData *data;
3657 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3659 data = g_simple_async_result_get_op_res_gpointer (simple);
3660 if (data->enumerator)
3661 return g_object_ref (data->enumerator);
3667 open_read_async_thread (GSimpleAsyncResult *res,
3669 GCancellable *cancellable)
3672 GFileInputStream *stream;
3673 GError *error = NULL;
3675 iface = G_FILE_GET_IFACE (object);
3677 stream = iface->read_fn (G_FILE (object), cancellable, &error);
3681 g_simple_async_result_set_from_error (res, error);
3682 g_error_free (error);
3685 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3689 g_file_real_read_async (GFile *file,
3691 GCancellable *cancellable,
3692 GAsyncReadyCallback callback,
3695 GSimpleAsyncResult *res;
3697 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3699 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3700 g_object_unref (res);
3703 static GFileInputStream *
3704 g_file_real_read_finish (GFile *file,
3708 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3711 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3713 op = g_simple_async_result_get_op_res_gpointer (simple);
3715 return g_object_ref (op);
3721 append_to_async_thread (GSimpleAsyncResult *res,
3723 GCancellable *cancellable)
3726 GFileCreateFlags *data;
3727 GFileOutputStream *stream;
3728 GError *error = NULL;
3730 iface = G_FILE_GET_IFACE (object);
3732 data = g_simple_async_result_get_op_res_gpointer (res);
3734 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3738 g_simple_async_result_set_from_error (res, error);
3739 g_error_free (error);
3742 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3746 g_file_real_append_to_async (GFile *file,
3747 GFileCreateFlags flags,
3749 GCancellable *cancellable,
3750 GAsyncReadyCallback callback,
3753 GFileCreateFlags *data;
3754 GSimpleAsyncResult *res;
3756 data = g_new0 (GFileCreateFlags, 1);
3759 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3760 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3762 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3763 g_object_unref (res);
3766 static GFileOutputStream *
3767 g_file_real_append_to_finish (GFile *file,
3771 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3774 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3776 op = g_simple_async_result_get_op_res_gpointer (simple);
3778 return g_object_ref (op);
3784 create_async_thread (GSimpleAsyncResult *res,
3786 GCancellable *cancellable)
3789 GFileCreateFlags *data;
3790 GFileOutputStream *stream;
3791 GError *error = NULL;
3793 iface = G_FILE_GET_IFACE (object);
3795 data = g_simple_async_result_get_op_res_gpointer (res);
3797 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3801 g_simple_async_result_set_from_error (res, error);
3802 g_error_free (error);
3805 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3809 g_file_real_create_async (GFile *file,
3810 GFileCreateFlags flags,
3812 GCancellable *cancellable,
3813 GAsyncReadyCallback callback,
3816 GFileCreateFlags *data;
3817 GSimpleAsyncResult *res;
3819 data = g_new0 (GFileCreateFlags, 1);
3822 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3823 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3825 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3826 g_object_unref (res);
3829 static GFileOutputStream *
3830 g_file_real_create_finish (GFile *file,
3834 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3837 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3839 op = g_simple_async_result_get_op_res_gpointer (simple);
3841 return g_object_ref (op);
3847 GFileOutputStream *stream;
3849 gboolean make_backup;
3850 GFileCreateFlags flags;
3854 replace_async_data_free (ReplaceAsyncData *data)
3857 g_object_unref (data->stream);
3858 g_free (data->etag);
3863 replace_async_thread (GSimpleAsyncResult *res,
3865 GCancellable *cancellable)
3868 GFileOutputStream *stream;
3869 GError *error = NULL;
3870 ReplaceAsyncData *data;
3872 iface = G_FILE_GET_IFACE (object);
3874 data = g_simple_async_result_get_op_res_gpointer (res);
3876 stream = iface->replace (G_FILE (object),
3885 g_simple_async_result_set_from_error (res, error);
3886 g_error_free (error);
3889 data->stream = stream;
3893 g_file_real_replace_async (GFile *file,
3895 gboolean make_backup,
3896 GFileCreateFlags flags,
3898 GCancellable *cancellable,
3899 GAsyncReadyCallback callback,
3902 GSimpleAsyncResult *res;
3903 ReplaceAsyncData *data;
3905 data = g_new0 (ReplaceAsyncData, 1);
3906 data->etag = g_strdup (etag);
3907 data->make_backup = make_backup;
3908 data->flags = flags;
3910 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3911 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3913 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3914 g_object_unref (res);
3917 static GFileOutputStream *
3918 g_file_real_replace_finish (GFile *file,
3922 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3923 ReplaceAsyncData *data;
3925 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3927 data = g_simple_async_result_get_op_res_gpointer (simple);
3929 return g_object_ref (data->stream);
3937 } SetDisplayNameAsyncData;
3940 set_display_name_data_free (SetDisplayNameAsyncData *data)
3942 g_free (data->name);
3944 g_object_unref (data->file);
3949 set_display_name_async_thread (GSimpleAsyncResult *res,
3951 GCancellable *cancellable)
3953 GError *error = NULL;
3954 SetDisplayNameAsyncData *data;
3957 data = g_simple_async_result_get_op_res_gpointer (res);
3959 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3963 g_simple_async_result_set_from_error (res, error);
3964 g_error_free (error);
3971 g_file_real_set_display_name_async (GFile *file,
3972 const char *display_name,
3974 GCancellable *cancellable,
3975 GAsyncReadyCallback callback,
3978 GSimpleAsyncResult *res;
3979 SetDisplayNameAsyncData *data;
3981 data = g_new0 (SetDisplayNameAsyncData, 1);
3982 data->name = g_strdup (display_name);
3984 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3985 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3987 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3988 g_object_unref (res);
3992 g_file_real_set_display_name_finish (GFile *file,
3996 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3997 SetDisplayNameAsyncData *data;
3999 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4001 data = g_simple_async_result_get_op_res_gpointer (simple);
4003 return g_object_ref (data->file);
4009 GFileQueryInfoFlags flags;
4016 set_info_data_free (SetInfoAsyncData *data)
4019 g_object_unref (data->info);
4021 g_error_free (data->error);
4026 set_info_async_thread (GSimpleAsyncResult *res,
4028 GCancellable *cancellable)
4030 SetInfoAsyncData *data;
4032 data = g_simple_async_result_get_op_res_gpointer (res);
4035 data->res = g_file_set_attributes_from_info (G_FILE (object),
4043 g_file_real_set_attributes_async (GFile *file,
4045 GFileQueryInfoFlags flags,
4047 GCancellable *cancellable,
4048 GAsyncReadyCallback callback,
4051 GSimpleAsyncResult *res;
4052 SetInfoAsyncData *data;
4054 data = g_new0 (SetInfoAsyncData, 1);
4055 data->info = g_file_info_dup (info);
4056 data->flags = flags;
4058 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4059 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4061 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4062 g_object_unref (res);
4066 g_file_real_set_attributes_finish (GFile *file,
4071 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4072 SetInfoAsyncData *data;
4074 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4076 data = g_simple_async_result_get_op_res_gpointer (simple);
4079 *info = g_object_ref (data->info);
4081 if (error != NULL && data->error)
4082 *error = g_error_copy (data->error);
4088 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4090 GCancellable *cancellable)
4092 GError *error = NULL;
4095 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4099 g_simple_async_result_set_from_error (res, error);
4100 g_error_free (error);
4103 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4107 g_file_real_find_enclosing_mount_async (GFile *file,
4109 GCancellable *cancellable,
4110 GAsyncReadyCallback callback,
4113 GSimpleAsyncResult *res;
4115 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4117 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4118 g_object_unref (res);
4122 g_file_real_find_enclosing_mount_finish (GFile *file,
4126 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4129 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4131 mount = g_simple_async_result_get_op_res_gpointer (simple);
4132 return g_object_ref (mount);
4136 /********************************************
4137 * Default VFS operations *
4138 ********************************************/
4141 * g_file_new_for_path:
4142 * @path: a string containing a relative or absolute path.
4144 * Constructs a #GFile for a given path. This operation never
4145 * fails, but the returned object might not support any I/O
4146 * operation if @path is malformed.
4148 * Returns: a new #GFile for the given @path.
4151 g_file_new_for_path (const char *path)
4153 g_return_val_if_fail (path != NULL, NULL);
4155 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4159 * g_file_new_for_uri:
4160 * @uri: a string containing a URI.
4162 * Constructs a #GFile for a given URI. This operation never
4163 * fails, but the returned object might not support any I/O
4164 * operation if @uri is malformed or if the uri type is
4167 * Returns: a #GFile for the given @uri.
4170 g_file_new_for_uri (const char *uri)
4172 g_return_val_if_fail (uri != NULL, NULL);
4174 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4178 * g_file_parse_name:
4179 * @parse_name: a file name or path to be parsed.
4181 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4182 * This operation never fails, but the returned object might not support any I/O
4183 * operation if the @parse_name cannot be parsed.
4185 * Returns: a new #GFile.
4188 g_file_parse_name (const char *parse_name)
4190 g_return_val_if_fail (parse_name != NULL, NULL);
4192 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4196 is_valid_scheme_character (char c)
4198 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4202 has_valid_scheme (const char *uri)
4208 if (!is_valid_scheme_character (*p))
4213 } while (is_valid_scheme_character (*p));
4219 * g_file_new_for_commandline_arg:
4220 * @arg: a command line string.
4222 * Creates a #GFile with the given argument from the command line. The value of
4223 * @arg can be either a URI, an absolute path or a relative path resolved
4224 * relative to the current working directory.
4225 * This operation never fails, but the returned object might not support any
4226 * I/O operation if @arg points to a malformed path.
4228 * Returns: a new #GFile.
4231 g_file_new_for_commandline_arg (const char *arg)
4237 g_return_val_if_fail (arg != NULL, NULL);
4239 if (g_path_is_absolute (arg))
4240 return g_file_new_for_path (arg);
4242 if (has_valid_scheme (arg))
4243 return g_file_new_for_uri (arg);
4245 current_dir = g_get_current_dir ();
4246 filename = g_build_filename (current_dir, arg, NULL);
4247 g_free (current_dir);
4249 file = g_file_new_for_path (filename);
4256 * g_file_mount_enclosing_volume:
4257 * @location: input #GFile.
4258 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4259 * @cancellable: optional #GCancellable object, %NULL to ignore.
4260 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4261 * @user_data: the data to pass to callback function
4263 * Starts a @mount_operation, mounting the volume that contains the file @location.
4265 * When this operation has completed, @callback will be called with
4266 * @user_user data, and the operation can be finalized with
4267 * g_file_mount_enclosing_volume_finish().
4269 * If @cancellable is not %NULL, then the operation can be cancelled by
4270 * triggering the cancellable object from another thread. If the operation
4271 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4274 g_file_mount_enclosing_volume (GFile *location,
4275 GMountOperation *mount_operation,
4276 GCancellable *cancellable,
4277 GAsyncReadyCallback callback,
4282 g_return_if_fail (G_IS_FILE (location));
4284 iface = G_FILE_GET_IFACE (location);
4286 if (iface->mount_enclosing_volume == NULL)
4288 g_simple_async_report_error_in_idle (G_OBJECT (location),
4289 callback, user_data,
4290 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4291 _("volume doesn't implement mount"));
4296 (* iface->mount_enclosing_volume) (location, mount_operation, cancellable, callback, user_data);
4301 * g_file_mount_enclosing_volume_finish:
4302 * @location: input #GFile.
4303 * @result: a #GAsyncResult.
4304 * @error: a #GError, or %NULL
4306 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4308 * Returns: %TRUE if successful. If an error
4309 * has occurred, this function will return %FALSE and set @error
4310 * appropriately if present.
4313 g_file_mount_enclosing_volume_finish (GFile *location,
4314 GAsyncResult *result,
4319 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4320 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4322 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4324 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4325 if (g_simple_async_result_propagate_error (simple, error))
4329 iface = G_FILE_GET_IFACE (location);
4331 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4334 /********************************************
4335 * Utility functions *
4336 ********************************************/
4338 #define GET_CONTENT_BLOCK_SIZE 8192
4341 * g_file_load_contents:
4342 * @file: input #GFile.
4343 * @cancellable: optional #GCancellable object, %NULL to ignore.
4344 * @contents: a location to place the contents of the file.
4345 * @length: a location to place the length of the contents of the file.
4346 * @etag_out: a location to place the current entity tag for the file.
4347 * @error: a #GError, or %NULL
4349 * Loads the content of the file into memory, returning the size of
4350 * the data. The data is always zero terminated, but this is not
4351 * included in the resultant @length.
4353 * If @cancellable is not %NULL, then the operation can be cancelled by
4354 * triggering the cancellable object from another thread. If the operation
4355 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4357 * Returns: %TRUE if the @file's contents were successfully loaded.
4358 * %FALSE if there were errors..
4361 g_file_load_contents (GFile *file,
4362 GCancellable *cancellable,
4368 GFileInputStream *in;
4369 GByteArray *content;
4374 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4375 g_return_val_if_fail (contents != NULL, FALSE);
4377 in = g_file_read (file, cancellable, error);
4381 content = g_byte_array_new ();
4384 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4385 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4386 content->data + pos,
4387 GET_CONTENT_BLOCK_SIZE,
4388 cancellable, error)) > 0)
4391 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4398 info = g_file_input_stream_query_info (in,
4399 G_FILE_ATTRIBUTE_ETAG_VALUE,
4404 *etag_out = g_strdup (g_file_info_get_etag (info));
4405 g_object_unref (info);
4409 /* Ignore errors on close */
4410 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4411 g_object_unref (in);
4415 /* error is set already */
4416 g_byte_array_free (content, TRUE);
4423 /* Zero terminate (we got an extra byte allocated for this */
4424 content->data[pos] = 0;
4426 *contents = (char *)g_byte_array_free (content, FALSE);
4434 GCancellable *cancellable;
4435 GFileReadMoreCallback read_more_callback;
4436 GAsyncReadyCallback callback;
4438 GByteArray *content;
4445 load_contents_data_free (LoadContentsData *data)
4448 g_error_free (data->error);
4449 if (data->cancellable)
4450 g_object_unref (data->cancellable);
4452 g_byte_array_free (data->content, TRUE);
4453 g_free (data->etag);
4454 g_object_unref (data->file);
4459 load_contents_close_callback (GObject *obj,
4460 GAsyncResult *close_res,
4463 GInputStream *stream = G_INPUT_STREAM (obj);
4464 LoadContentsData *data = user_data;
4465 GSimpleAsyncResult *res;
4467 /* Ignore errors here, we're only reading anyway */
4468 g_input_stream_close_finish (stream, close_res, NULL);
4469 g_object_unref (stream);
4471 res = g_simple_async_result_new (G_OBJECT (data->file),
4474 g_file_load_contents_async);
4475 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4476 g_simple_async_result_complete (res);
4477 g_object_unref (res);
4481 load_contents_fstat_callback (GObject *obj,
4482 GAsyncResult *stat_res,
4485 GInputStream *stream = G_INPUT_STREAM (obj);
4486 LoadContentsData *data = user_data;
4489 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4493 data->etag = g_strdup (g_file_info_get_etag (info));
4494 g_object_unref (info);
4497 g_input_stream_close_async (stream, 0,
4499 load_contents_close_callback, data);
4503 load_contents_read_callback (GObject *obj,
4504 GAsyncResult *read_res,
4507 GInputStream *stream = G_INPUT_STREAM (obj);
4508 LoadContentsData *data = user_data;
4509 GError *error = NULL;
4512 read_size = g_input_stream_read_finish (stream, read_res, &error);
4516 /* Error or EOF, close the file */
4517 data->error = error;
4518 g_input_stream_close_async (stream, 0,
4520 load_contents_close_callback, data);
4522 else if (read_size == 0)
4524 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4525 G_FILE_ATTRIBUTE_ETAG_VALUE,
4528 load_contents_fstat_callback,
4531 else if (read_size > 0)
4533 data->pos += read_size;
4535 g_byte_array_set_size (data->content,
4536 data->pos + GET_CONTENT_BLOCK_SIZE);
4539 if (data->read_more_callback &&
4540 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4541 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4542 G_FILE_ATTRIBUTE_ETAG_VALUE,
4545 load_contents_fstat_callback,
4548 g_input_stream_read_async (stream,
4549 data->content->data + data->pos,
4550 GET_CONTENT_BLOCK_SIZE,
4553 load_contents_read_callback,
4559 load_contents_open_callback (GObject *obj,
4560 GAsyncResult *open_res,
4563 GFile *file = G_FILE (obj);
4564 GFileInputStream *stream;
4565 LoadContentsData *data = user_data;
4566 GError *error = NULL;
4567 GSimpleAsyncResult *res;
4569 stream = g_file_read_finish (file, open_res, &error);
4573 g_byte_array_set_size (data->content,
4574 data->pos + GET_CONTENT_BLOCK_SIZE);
4575 g_input_stream_read_async (G_INPUT_STREAM (stream),
4576 data->content->data + data->pos,
4577 GET_CONTENT_BLOCK_SIZE,
4580 load_contents_read_callback,
4586 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4590 g_simple_async_result_complete (res);
4591 g_error_free (error);
4592 load_contents_data_free (data);
4593 g_object_unref (res);
4598 * g_file_load_partial_contents_async:
4599 * @file: input #GFile.
4600 * @cancellable: optional #GCancellable object, %NULL to ignore.
4601 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
4602 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4603 * @user_data: the data to pass to the callback functions.
4605 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
4606 * used to stop reading from the file when appropriate, else this function
4607 * will behave exactly as g_file_load_contents_async(). This operation
4608 * can be finished by g_file_load_partial_contents_finish().
4610 * Users of this function should be aware that @user_data is passed to
4611 * both the @read_more_callback and the @callback.
4613 * If @cancellable is not %NULL, then the operation can be cancelled by
4614 * triggering the cancellable object from another thread. If the operation
4615 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4618 g_file_load_partial_contents_async (GFile *file,
4619 GCancellable *cancellable,
4620 GFileReadMoreCallback read_more_callback,
4621 GAsyncReadyCallback callback,
4624 LoadContentsData *data;
4626 g_return_if_fail (G_IS_FILE (file));
4628 data = g_new0 (LoadContentsData, 1);
4631 data->cancellable = g_object_ref (cancellable);
4632 data->read_more_callback = read_more_callback;
4633 data->callback = callback;
4634 data->user_data = user_data;
4635 data->content = g_byte_array_new ();
4636 data->file = g_object_ref (file);
4638 g_file_read_async (file,
4641 load_contents_open_callback,
4646 * g_file_load_partial_contents_finish:
4647 * @file: input #GFile.
4648 * @res: a #GAsyncResult.
4649 * @contents: a location to place the contents of the file.
4650 * @length: a location to place the length of the contents of the file.
4651 * @etag_out: a location to place the current entity tag for the file.
4652 * @error: a #GError, or %NULL
4654 * Finishes an asynchronous partial load operation that was started
4655 * with g_file_load_partial_contents_async().
4657 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4658 * present, it will be set appropriately.
4661 g_file_load_partial_contents_finish (GFile *file,
4668 GSimpleAsyncResult *simple;
4669 LoadContentsData *data;
4671 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4672 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4673 g_return_val_if_fail (contents != NULL, FALSE);
4675 simple = G_SIMPLE_ASYNC_RESULT (res);
4677 if (g_simple_async_result_propagate_error (simple, error))
4680 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4682 data = g_simple_async_result_get_op_res_gpointer (simple);
4686 g_propagate_error (error, data->error);
4695 *length = data->pos;
4699 *etag_out = data->etag;
4703 /* Zero terminate */
4704 g_byte_array_set_size (data->content, data->pos + 1);
4705 data->content->data[data->pos] = 0;
4707 *contents = (char *)g_byte_array_free (data->content, FALSE);
4708 data->content = NULL;
4714 * g_file_load_contents_async:
4715 * @file: input #GFile.
4716 * @cancellable: optional #GCancellable object, %NULL to ignore.
4717 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4718 * @user_data: the data to pass to callback function
4720 * Starts an asynchronous load of the @file's contents.
4722 * For more details, see g_file_load_contents() which is
4723 * the synchronous version of this call.
4725 * When the load operation has completed, @callback will be called
4726 * with @user data. To finish the operation, call
4727 * g_file_load_contents_finish() with the #GAsyncResult returned by
4730 * If @cancellable is not %NULL, then the operation can be cancelled by
4731 * triggering the cancellable object from another thread. If the operation
4732 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4735 g_file_load_contents_async (GFile *file,
4736 GCancellable *cancellable,
4737 GAsyncReadyCallback callback,
4740 g_file_load_partial_contents_async (file,
4743 callback, user_data);
4747 * g_file_load_contents_finish:
4748 * @file: input #GFile.
4749 * @res: a #GAsyncResult.
4750 * @contents: a location to place the contents of the file.
4751 * @length: a location to place the length of the contents of the file.
4752 * @etag_out: a location to place the current entity tag for the file.
4753 * @error: a #GError, or %NULL
4755 * Finishes an asynchronous load of the @file's contents.
4756 * The contents are placed in @contents, and @length is set to the
4757 * size of the @contents string. If @etag_out is present, it will be
4758 * set to the new entity tag for the @file.
4760 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4761 * present, it will be set appropriately.
4764 g_file_load_contents_finish (GFile *file,
4771 return g_file_load_partial_contents_finish (file,
4780 * g_file_replace_contents:
4781 * @file: input #GFile.
4782 * @contents: a string containing the new contents for @file.
4783 * @length: the length of @contents in bytes.
4784 * @etag: the old <link linkend="gfile-etag">entity tag</link>
4786 * @make_backup: %TRUE if a backup should be created.
4787 * @flags: a set of #GFileCreateFlags.
4788 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4790 * @cancellable: optional #GCancellable object, %NULL to ignore.
4791 * @error: a #GError, or %NULL
4793 * Replaces the contents of @file with @contents of @length bytes.
4795 * If @etag is specified (not %NULL) any existing file must have that etag, or
4796 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
4798 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
4800 * If @cancellable is not %NULL, then the operation can be cancelled by
4801 * triggering the cancellable object from another thread. If the operation
4802 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4804 * The returned @new_etag can be used to verify that the file hasn't changed the
4805 * next time it is saved over.
4807 * Returns: %TRUE if successful. If an error
4808 * has occurred, this function will return %FALSE and set @error
4809 * appropriately if present.
4812 g_file_replace_contents (GFile *file,
4813 const char *contents,
4816 gboolean make_backup,
4817 GFileCreateFlags flags,
4819 GCancellable *cancellable,
4822 GFileOutputStream *out;
4823 gsize pos, remainder;
4826 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4827 g_return_val_if_fail (contents != NULL, FALSE);
4829 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4835 while (remainder > 0 &&
4836 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4838 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4846 if (remainder > 0 && res < 0)
4848 /* Ignore errors on close */
4849 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4851 /* error is set already */
4855 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4859 *new_etag = g_file_output_stream_get_etag (out);
4867 GCancellable *cancellable;
4868 GAsyncReadyCallback callback;
4870 const char *content;
4874 } ReplaceContentsData;
4877 replace_contents_data_free (ReplaceContentsData *data)
4880 g_error_free (data->error);
4881 if (data->cancellable)
4882 g_object_unref (data->cancellable);
4883 g_object_unref (data->file);
4884 g_free (data->etag);
4889 replace_contents_close_callback (GObject *obj,
4890 GAsyncResult *close_res,
4893 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4894 ReplaceContentsData *data = user_data;
4895 GSimpleAsyncResult *res;
4897 /* Ignore errors here, we're only reading anyway */
4898 g_output_stream_close_finish (stream, close_res, NULL);
4899 g_object_unref (stream);
4901 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4903 res = g_simple_async_result_new (G_OBJECT (data->file),
4906 g_file_replace_contents_async);
4907 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4908 g_simple_async_result_complete (res);
4909 g_object_unref (res);
4913 replace_contents_write_callback (GObject *obj,
4914 GAsyncResult *read_res,
4917 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4918 ReplaceContentsData *data = user_data;
4919 GError *error = NULL;
4922 write_size = g_output_stream_write_finish (stream, read_res, &error);
4924 if (write_size <= 0)
4926 /* Error or EOF, close the file */
4928 data->error = error;
4929 g_output_stream_close_async (stream, 0,
4931 replace_contents_close_callback, data);
4933 else if (write_size > 0)
4935 data->pos += write_size;
4937 if (data->pos >= data->length)
4938 g_output_stream_close_async (stream, 0,
4940 replace_contents_close_callback, data);
4942 g_output_stream_write_async (stream,
4943 data->content + data->pos,
4944 data->length - data->pos,
4947 replace_contents_write_callback,
4953 replace_contents_open_callback (GObject *obj,
4954 GAsyncResult *open_res,
4957 GFile *file = G_FILE (obj);
4958 GFileOutputStream *stream;
4959 ReplaceContentsData *data = user_data;
4960 GError *error = NULL;
4961 GSimpleAsyncResult *res;
4963 stream = g_file_replace_finish (file, open_res, &error);
4967 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4968 data->content + data->pos,
4969 data->length - data->pos,
4972 replace_contents_write_callback,
4978 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4982 g_simple_async_result_complete (res);
4983 g_error_free (error);
4984 replace_contents_data_free (data);
4985 g_object_unref (res);
4990 * g_file_replace_contents_async:
4991 * @file: input #GFile.
4992 * @contents: string of contents to replace the file with.
4993 * @length: the length of @contents in bytes.
4994 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4995 * @make_backup: %TRUE if a backup should be created.
4996 * @flags: a set of #GFileCreateFlags.
4997 * @cancellable: optional #GCancellable object, %NULL to ignore.
4998 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4999 * @user_data: the data to pass to callback function
5001 * Starts an asynchronous replacement of @file with the given
5002 * @contents of @length bytes. @etag will replace the document's
5003 * current entity tag.
5005 * When this operation has completed, @callback will be called with
5006 * @user_user data, and the operation can be finalized with
5007 * g_file_replace_contents_finish().
5009 * If @cancellable is not %NULL, then the operation can be cancelled by
5010 * triggering the cancellable object from another thread. If the operation
5011 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5013 * If @make_backup is %TRUE, this function will attempt to
5014 * make a backup of @file.
5017 g_file_replace_contents_async (GFile *file,
5018 const char *contents,
5021 gboolean make_backup,
5022 GFileCreateFlags flags,
5023 GCancellable *cancellable,
5024 GAsyncReadyCallback callback,
5027 ReplaceContentsData *data;
5029 g_return_if_fail (G_IS_FILE (file));
5030 g_return_if_fail (contents != NULL);
5032 data = g_new0 (ReplaceContentsData, 1);
5035 data->cancellable = g_object_ref (cancellable);
5036 data->callback = callback;
5037 data->user_data = user_data;
5038 data->content = contents;
5039 data->length = length;
5041 data->file = g_object_ref (file);
5043 g_file_replace_async (file,
5049 replace_contents_open_callback,
5054 * g_file_replace_contents_finish:
5055 * @file: input #GFile.
5056 * @res: a #GAsyncResult.
5057 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5059 * @error: a #GError, or %NULL
5061 * Finishes an asynchronous replace of the given @file. See
5062 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5063 * tag for the document, if present.
5065 * Returns: %TRUE on success, %FALSE on failure.
5068 g_file_replace_contents_finish (GFile *file,
5073 GSimpleAsyncResult *simple;
5074 ReplaceContentsData *data;
5076 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5077 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5079 simple = G_SIMPLE_ASYNC_RESULT (res);
5081 if (g_simple_async_result_propagate_error (simple, error))
5084 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5086 data = g_simple_async_result_get_op_res_gpointer (simple);
5090 g_propagate_error (error, data->error);
5098 *new_etag = data->etag;
5099 data->etag = NULL; /* Take ownership */
5105 #define __G_FILE_C__
5106 #include "gioaliasdef.c"