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, create 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 an 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 objects that contains key-value attributes (like type or size
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 points 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 objects 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);
1113 g_file_find_enclosing_mount_async (GFile *file,
1115 GCancellable *cancellable,
1116 GAsyncReadyCallback callback,
1121 g_return_if_fail (G_IS_FILE (file));
1123 iface = G_FILE_GET_IFACE (file);
1124 (* iface->find_enclosing_mount_async) (file,
1132 g_file_find_enclosing_mount_finish (GFile *file,
1138 g_return_val_if_fail (G_IS_FILE (file), NULL);
1139 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1141 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1143 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1144 if (g_simple_async_result_propagate_error (simple, error))
1148 iface = G_FILE_GET_IFACE (file);
1149 return (* iface->find_enclosing_mount_finish) (file, res, error);
1155 * @file: #GFile to read.
1156 * @cancellable: a #GCancellable
1157 * @error: a #GError, or %NULL
1159 * Opens a file for reading. The result is a #GFileInputStream that
1160 * can be used to read the contents of the file.
1162 * If @cancellable is not %NULL, then the operation can be cancelled by
1163 * triggering the cancellable object from another thread. If the operation
1164 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1166 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1167 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1168 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1170 * Returns: #GFileInputStream or %NULL on error.
1173 g_file_read (GFile *file,
1174 GCancellable *cancellable,
1179 g_return_val_if_fail (G_IS_FILE (file), NULL);
1181 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1184 iface = G_FILE_GET_IFACE (file);
1186 if (iface->read_fn == NULL)
1188 g_set_error (error, G_IO_ERROR,
1189 G_IO_ERROR_NOT_SUPPORTED,
1190 _("Operation not supported"));
1194 return (* iface->read_fn) (file, cancellable, error);
1199 * @file: input #GFile.
1200 * @flags: a set of #GFileCreateFlags.
1201 * @cancellable: optional #GCancellable object, %NULL to ignore.
1202 * @error: a #GError, or %NULL
1204 * Gets an output stream for appending data to the file. If
1205 * the file doesn't already exist it is created.
1207 * By default files created are generally readable by everyone,
1208 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1209 * will be made readable only to the current user, to the level that
1210 * is supported on the target filesystem.
1212 * If @cancellable is not %NULL, then the operation can be cancelled by
1213 * triggering the cancellable object from another thread. If the operation
1214 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1216 * Some file systems don't allow all file names, and may
1217 * return an G_IO_ERROR_INVALID_FILENAME error.
1218 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1219 * returned. Other errors are possible too, and depend on what kind of
1220 * filesystem the file is on.
1222 * Returns: a #GFileOutputStream.
1225 g_file_append_to (GFile *file,
1226 GFileCreateFlags flags,
1227 GCancellable *cancellable,
1232 g_return_val_if_fail (G_IS_FILE (file), NULL);
1234 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1237 iface = G_FILE_GET_IFACE (file);
1239 if (iface->append_to == NULL)
1241 g_set_error (error, G_IO_ERROR,
1242 G_IO_ERROR_NOT_SUPPORTED,
1243 _("Operation not supported"));
1247 return (* iface->append_to) (file, flags, cancellable, error);
1252 * @file: input #GFile.
1253 * @flags: a set of #GFileCreateFlags.
1254 * @cancellable: optional #GCancellable object, %NULL to ignore.
1255 * @error: a #GError, or %NULL
1257 * Creates a new file and returns an output stream for writing to it.
1258 * The file must not already exists.
1260 * By default files created are generally readable by everyone,
1261 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1262 * will be made readable only to the current user, to the level that
1263 * is supported on the target filesystem.
1265 * If @cancellable is not %NULL, then the operation can be cancelled by
1266 * triggering the cancellable object from another thread. If the operation
1267 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1269 * If a file with this name already exists the G_IO_ERROR_EXISTS error
1270 * will be returned. If the file is a directory the G_IO_ERROR_IS_DIRECTORY
1271 * error will be returned.
1272 * Some file systems don't allow all file names, and may
1273 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1274 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1275 * Other errors are possible too, and depend on what kind of
1276 * filesystem the file is on.
1278 * Returns: a #GFileOutputStream for the newly created file, or
1282 g_file_create (GFile *file,
1283 GFileCreateFlags flags,
1284 GCancellable *cancellable,
1289 g_return_val_if_fail (G_IS_FILE (file), NULL);
1291 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1294 iface = G_FILE_GET_IFACE (file);
1296 if (iface->create == NULL)
1298 g_set_error (error, G_IO_ERROR,
1299 G_IO_ERROR_NOT_SUPPORTED,
1300 _("Operation not supported"));
1304 return (* iface->create) (file, flags, cancellable, error);
1309 * @file: input #GFile.
1310 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1311 * current #GFile, or #NULL to ignore.
1312 * @make_backup: %TRUE if a backup should be created.
1313 * @flags: a set of #GFileCreateFlags.
1314 * @cancellable: optional #GCancellable object, %NULL to ignore.
1315 * @error: a #GError, or %NULL
1317 * Returns an output stream for overwriting the file, possibly
1318 * creating a backup copy of the file first.
1320 * This will try to replace the file in the safest way possible so
1321 * that any errors during the writing will not affect an already
1322 * existing copy of the file. For instance, for local files it
1323 * may write to a temporary file and then atomically rename over
1324 * the destination when the stream is closed.
1326 * By default files created are generally readable by everyone,
1327 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1328 * will be made readable only to the current user, to the level that
1329 * is supported on the target filesystem.
1331 * If @cancellable is not %NULL, then the operation can be cancelled by
1332 * triggering the cancellable object from another thread. If the operation
1333 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1335 * If you pass in a non-#NULL @etag value, then this value is
1336 * compared to the current entity tag of the file, and if they differ
1337 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1338 * that the file has been changed since you last read it. You can get
1339 * the new etag from g_file_output_stream_get_etag() after you've
1340 * finished writing and closed the #GFileOutputStream. When you load
1341 * a new file you can use g_file_input_stream_query_info() to get
1342 * the etag of the file.
1344 * If @make_backup is %TRUE, this function will attempt to make a backup
1345 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1346 * error will be returned. If you want to replace anyway, try again with
1347 * @make_backup set to %FALSE.
1349 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1350 * and if the file is some other form of non-regular file then a
1351 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1352 * Some file systems don't allow all file names, and may
1353 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1354 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1355 * Other errors are possible too, and depend on what kind of
1356 * filesystem the file is on.
1358 * Returns: a #GFileOutputStream or %NULL on error.
1361 g_file_replace (GFile *file,
1363 gboolean make_backup,
1364 GFileCreateFlags flags,
1365 GCancellable *cancellable,
1370 g_return_val_if_fail (G_IS_FILE (file), NULL);
1372 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1375 iface = G_FILE_GET_IFACE (file);
1377 if (iface->replace == NULL)
1379 g_set_error (error, G_IO_ERROR,
1380 G_IO_ERROR_NOT_SUPPORTED,
1381 _("Operation not supported"));
1386 /* Handle empty tag string as NULL in consistent way. */
1387 if (etag && *etag == 0)
1390 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1394 * g_file_read_async:
1395 * @file: input #GFile.
1396 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1398 * @cancellable: optional #GCancellable object, %NULL to ignore.
1399 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1400 * @user_data: the data to pass to callback function
1402 * Asynchronously opens @file for reading.
1404 * For more details, see g_file_read() which is
1405 * the synchronous version of this call.
1407 * When the operation is finished, @callback will be called. You can then call
1408 * g_file_read_finish() to get the result of the operation.
1411 g_file_read_async (GFile *file,
1413 GCancellable *cancellable,
1414 GAsyncReadyCallback callback,
1419 g_return_if_fail (G_IS_FILE (file));
1421 iface = G_FILE_GET_IFACE (file);
1422 (* iface->read_async) (file,
1430 * g_file_read_finish:
1431 * @file: input #GFile.
1432 * @res: a #GAsyncResult.
1433 * @error: a #GError, or %NULL
1435 * Finishes an asynchronous file read operation started with
1436 * g_file_read_async().
1438 * Returns: a #GFileInputStream or %NULL on error.
1441 g_file_read_finish (GFile *file,
1447 g_return_val_if_fail (G_IS_FILE (file), NULL);
1448 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1450 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1452 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1453 if (g_simple_async_result_propagate_error (simple, error))
1457 iface = G_FILE_GET_IFACE (file);
1458 return (* iface->read_finish) (file, res, error);
1462 * g_file_append_to_async:
1463 * @file: input #GFile.
1464 * @flags: a set of #GFileCreateFlags.
1465 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1467 * @cancellable: optional #GCancellable object, %NULL to ignore.
1468 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1469 * @user_data: the data to pass to callback function
1471 * Asynchronously opens @file for appending.
1473 * For more details, see g_file_append_to() which is
1474 * the synchronous version of this call.
1476 * When the operation is finished, @callback will be called. You can then call
1477 * g_file_append_to_finish() to get the result of the operation.
1480 g_file_append_to_async (GFile *file,
1481 GFileCreateFlags flags,
1483 GCancellable *cancellable,
1484 GAsyncReadyCallback callback,
1489 g_return_if_fail (G_IS_FILE (file));
1491 iface = G_FILE_GET_IFACE (file);
1492 (* iface->append_to_async) (file,
1501 * g_file_append_to_finish:
1502 * @file: input #GFile.
1503 * @res: #GAsyncResult
1504 * @error: a #GError, or %NULL
1506 * Finishes an asynchronous file append operation started with
1507 * g_file_append_to_async().
1509 * Returns: a valid #GFileOutputStream or %NULL on error.
1512 g_file_append_to_finish (GFile *file,
1518 g_return_val_if_fail (G_IS_FILE (file), NULL);
1519 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1521 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1523 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1524 if (g_simple_async_result_propagate_error (simple, error))
1528 iface = G_FILE_GET_IFACE (file);
1529 return (* iface->append_to_finish) (file, res, error);
1533 * g_file_create_async:
1534 * @file: input #GFile.
1535 * @flags: a set of #GFileCreateFlags.
1536 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1538 * @cancellable: optional #GCancellable object, %NULL to ignore.
1539 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1540 * @user_data: the data to pass to callback function
1542 * Asynchronously creates a new file and returns an output stream for writing to it.
1543 * The file must not already exists.
1545 * For more details, see g_file_create() which is
1546 * the synchronous version of this call.
1548 * When the operation is finished, @callback will be called. You can then call
1549 * g_file_create_finish() to get the result of the operation.
1552 g_file_create_async (GFile *file,
1553 GFileCreateFlags flags,
1555 GCancellable *cancellable,
1556 GAsyncReadyCallback callback,
1561 g_return_if_fail (G_IS_FILE (file));
1563 iface = G_FILE_GET_IFACE (file);
1564 (* iface->create_async) (file,
1573 * g_file_create_finish:
1574 * @file: input #GFile.
1575 * @res: a #GAsyncResult.
1576 * @error: a #GError, or %NULL
1578 * Finishes an asynchronous file create operation started with
1579 * g_file_create_async().
1581 * Returns: a #GFileOutputStream or %NULL on error.
1584 g_file_create_finish (GFile *file,
1590 g_return_val_if_fail (G_IS_FILE (file), NULL);
1591 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1593 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1595 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1596 if (g_simple_async_result_propagate_error (simple, error))
1600 iface = G_FILE_GET_IFACE (file);
1601 return (* iface->create_finish) (file, res, error);
1605 * g_file_replace_async:
1606 * @file: input #GFile.
1607 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1608 * current #GFile, or NULL to ignore.
1609 * @make_backup: %TRUE if a backup should be created.
1610 * @flags: a set of #GFileCreateFlags.
1611 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1613 * @cancellable: optional #GCancellable object, %NULL to ignore.
1614 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1615 * @user_data: the data to pass to callback function
1617 * Asynchronously overwrites the file, replacing the contents, possibly
1618 * creating a backup copy of the file first.
1620 * For more details, see g_file_replace() which is
1621 * the synchronous version of this call.
1623 * When the operation is finished, @callback will be called. You can then call
1624 * g_file_replace_finish() to get the result of the operation.
1627 g_file_replace_async (GFile *file,
1629 gboolean make_backup,
1630 GFileCreateFlags flags,
1632 GCancellable *cancellable,
1633 GAsyncReadyCallback callback,
1638 g_return_if_fail (G_IS_FILE (file));
1640 iface = G_FILE_GET_IFACE (file);
1641 (* iface->replace_async) (file,
1652 * g_file_replace_finish:
1653 * @file: input #GFile.
1654 * @res: a #GAsyncResult.
1655 * @error: a #GError, or %NULL
1657 * Finishes an asynchronous file replace operation started with
1658 * g_file_replace_async().
1660 * Returns: a #GFileOutputStream, or %NULL on error.
1663 g_file_replace_finish (GFile *file,
1669 g_return_val_if_fail (G_IS_FILE (file), NULL);
1670 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1672 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1674 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1675 if (g_simple_async_result_propagate_error (simple, error))
1679 iface = G_FILE_GET_IFACE (file);
1680 return (* iface->replace_finish) (file, res, error);
1684 copy_symlink (GFile *destination,
1685 GFileCopyFlags flags,
1686 GCancellable *cancellable,
1691 gboolean tried_delete;
1693 GFileType file_type;
1695 tried_delete = FALSE;
1699 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1701 /* Maybe it already existed, and we want to overwrite? */
1702 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1703 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1705 g_error_free (my_error);
1708 /* Don't overwrite if the destination is a directory */
1709 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1710 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1711 cancellable, &my_error);
1714 file_type = g_file_info_get_file_type (info);
1715 g_object_unref (info);
1717 if (file_type == G_FILE_TYPE_DIRECTORY)
1719 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1720 _("Can't copy over directory"));
1725 if (!g_file_delete (destination, cancellable, error))
1728 tried_delete = TRUE;
1732 g_propagate_error (error, my_error);
1739 static GInputStream *
1740 open_source_for_copy (GFile *source,
1742 GFileCopyFlags flags,
1743 GCancellable *cancellable,
1749 GFileType file_type;
1752 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1756 /* There was an error opening the source, try to set a good error for it: */
1758 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1760 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1761 * as that is less useful to the app. Better check for errors on the
1764 g_error_free (my_error);
1767 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1768 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1769 cancellable, &my_error);
1772 file_type = g_file_info_get_file_type (info);
1773 g_object_unref (info);
1775 if (flags & G_FILE_COPY_OVERWRITE)
1777 if (file_type == G_FILE_TYPE_DIRECTORY)
1779 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1780 _("Can't copy directory over directory"));
1783 /* continue to would_recurse error */
1787 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1788 _("Target file exists"));
1794 /* Error getting info from target, return that error
1795 * (except for NOT_FOUND, which is no error here)
1797 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1799 g_propagate_error (error, my_error);
1802 g_error_free (my_error);
1805 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1806 _("Can't recursively copy directory"));
1810 g_propagate_error (error, my_error);
1815 should_copy (GFileAttributeInfo *info,
1819 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1820 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1824 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1825 GFileAttributeInfoList *namespaces,
1833 s = g_string_new ("");
1837 for (i = 0; i < attributes->n_infos; i++)
1839 if (should_copy (&attributes->infos[i], as_move))
1844 g_string_append_c (s, ',');
1846 g_string_append (s, attributes->infos[i].name);
1853 for (i = 0; i < namespaces->n_infos; i++)
1855 if (should_copy (&namespaces->infos[i], as_move))
1860 g_string_append_c (s, ',');
1862 g_string_append (s, namespaces->infos[i].name);
1863 g_string_append (s, ":*");
1868 return g_string_free (s, FALSE);
1872 * g_file_copy_attributes:
1873 * @source: a #GFile with attributes.
1874 * @destination: a #GFile to copy attributes to.
1875 * @flags: a set of #GFileCopyFlags.
1876 * @cancellable: optional #GCancellable object, %NULL to ignore.
1877 * @error: a #GError, %NULL to ignore.
1879 * Copies the file attributes from @source to @destination.
1881 * Normally only a subset of the file attributes are copied,
1882 * those that are copies in a normal file copy operation
1883 * (which for instance does not include e.g. mtime). However
1884 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
1885 * all the metadata that is possible to copy is copied.
1887 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
1890 g_file_copy_attributes (GFile *source,
1892 GFileCopyFlags flags,
1893 GCancellable *cancellable,
1896 GFileAttributeInfoList *attributes, *namespaces;
1897 char *attrs_to_read;
1901 gboolean source_nofollow_symlinks;
1903 as_move = flags & G_FILE_COPY_ALL_METADATA;
1904 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
1906 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
1907 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
1908 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
1910 if (attributes == NULL && namespaces == NULL)
1913 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
1915 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
1916 * we just don't copy it.
1918 info = g_file_query_info (source, attrs_to_read,
1919 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
1923 g_free (attrs_to_read);
1928 res = g_file_set_attributes_from_info (destination,
1932 g_object_unref (info);
1935 g_file_attribute_info_list_unref (attributes);
1936 g_file_attribute_info_list_unref (namespaces);
1941 /* Closes the streams */
1943 copy_stream_with_progress (GInputStream *in,
1945 GCancellable *cancellable,
1946 GFileProgressCallback progress_callback,
1947 gpointer progress_callback_data,
1950 gssize n_read, n_written;
1951 goffset current_size;
1952 char buffer[8192], *p;
1958 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
1959 G_FILE_ATTRIBUTE_STANDARD_SIZE,
1963 total_size = g_file_info_get_size (info);
1964 g_object_unref (info);
1971 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
1981 current_size += n_read;
1986 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
1987 if (n_written == -1)
1994 n_read -= n_written;
2000 if (progress_callback)
2001 progress_callback (current_size, total_size, progress_callback_data);
2005 error = NULL; /* Ignore further errors */
2007 /* Make sure we send full copied size */
2008 if (progress_callback)
2009 progress_callback (current_size, total_size, progress_callback_data);
2012 /* Don't care about errors in source here */
2013 g_input_stream_close (in, cancellable, NULL);
2015 /* But write errors on close are bad! */
2016 if (!g_output_stream_close (out, cancellable, error))
2019 g_object_unref (in);
2020 g_object_unref (out);
2026 file_copy_fallback (GFile *source,
2028 GFileCopyFlags flags,
2029 GCancellable *cancellable,
2030 GFileProgressCallback progress_callback,
2031 gpointer progress_callback_data,
2039 /* Maybe copy the symlink? */
2040 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2042 info = g_file_query_info (source,
2043 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2044 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2050 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2051 (target = g_file_info_get_symlink_target (info)) != NULL)
2053 if (!copy_symlink (destination, flags, cancellable, target, error))
2055 g_object_unref (info);
2059 g_object_unref (info);
2063 g_object_unref (info);
2066 in = open_source_for_copy (source, destination, flags, cancellable, error);
2070 if (flags & G_FILE_COPY_OVERWRITE)
2072 out = (GOutputStream *)g_file_replace (destination,
2074 flags & G_FILE_COPY_BACKUP,
2075 cancellable, error);
2079 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2084 g_object_unref (in);
2088 if (!copy_stream_with_progress (in, out, cancellable,
2089 progress_callback, progress_callback_data,
2095 /* Ignore errors here. Failure to copy metadata is not a hard error */
2096 g_file_copy_attributes (source, destination,
2097 flags, cancellable, NULL);
2104 * @source: input #GFile.
2105 * @destination: destination #GFile
2106 * @flags: set of #GFileCopyFlags
2107 * @cancellable: optional #GCancellable object, %NULL to ignore.
2108 * @progress_callback: function to callback with progress information
2109 * @progress_callback_data: user data to pass to @progress_callback
2110 * @error: #GError to set on error, or %NULL
2112 * Copies the file @source to the location specified by @destination.
2113 * Can not handle recursive copies of directories.
2115 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2116 * existing @destination file is overwritten.
2118 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2119 * will be copied as symlinks, otherwise the target of the
2120 * @source symlink will be copied.
2122 * If @cancellable is not %NULL, then the operation can be cancelled by
2123 * triggering the cancellable object from another thread. If the operation
2124 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2126 * If @progress_callback is not %NULL, then the operation can be monitored by
2127 * setting this to a #GFileProgressCallback function. @progress_callback_data
2128 * will be passed to this function. It is guaranteed that this callback will
2129 * be called after all data has been transferred with the total number of bytes
2130 * copied during the operation.
2132 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2133 * error is returned, independent on the status of the @destination.
2135 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2136 * error G_IO_ERROR_EXISTS is returned.
2138 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2139 * error is returned. If trying to overwrite a directory with a directory the
2140 * G_IO_ERROR_WOULD_MERGE error is returned.
2142 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2143 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2146 * If you are interested in copying the #GFile object itself (not the on-disk
2147 * file), see g_file_dup().
2149 * Returns: %TRUE on success, %FALSE otherwise.
2152 g_file_copy (GFile *source,
2154 GFileCopyFlags flags,
2155 GCancellable *cancellable,
2156 GFileProgressCallback progress_callback,
2157 gpointer progress_callback_data,
2164 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2165 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2167 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2170 iface = G_FILE_GET_IFACE (destination);
2174 res = (* iface->copy) (source, destination,
2176 progress_callback, progress_callback_data,
2182 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2184 g_propagate_error (error, my_error);
2189 /* If the types are different, and the destination method failed
2190 also try the source method */
2191 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2193 iface = G_FILE_GET_IFACE (source);
2198 res = (* iface->copy) (source, destination,
2200 progress_callback, progress_callback_data,
2206 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2208 g_propagate_error (error, my_error);
2214 return file_copy_fallback (source, destination, flags, cancellable,
2215 progress_callback, progress_callback_data,
2222 * @source: #GFile pointing to the source location.
2223 * @destination: #GFile pointing to the destination location.
2224 * @flags: set of #GFileCopyFlags.
2225 * @cancellable: optional #GCancellable object, %NULL to ignore.
2226 * @progress_callback: #GFileProgressCallback function for updates.
2227 * @progress_callback_data: gpointer to user data for the callback function.
2228 * @error: #GError for returning error conditions, or %NULL
2231 * Tries to move the file or directory @source to the location specified by @destination.
2232 * If native move operations are supported then this is used, otherwise a copy + delete
2233 * fallback is used. The native implementation may support moving directories (for instance
2234 * on moves inside the same filesystem), but the fallback code does not.
2236 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2237 * existing @destination file is overwritten.
2239 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2240 * will be copied as symlinks, otherwise the target of the
2241 * @source symlink will be copied.
2243 * If @cancellable is not %NULL, then the operation can be cancelled by
2244 * triggering the cancellable object from another thread. If the operation
2245 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2247 * If @progress_callback is not %NULL, then the operation can be monitored by
2248 * setting this to a #GFileProgressCallback function. @progress_callback_data
2249 * will be passed to this function. It is guaranteed that this callback will
2250 * be called after all data has been transferred with the total number of bytes
2251 * copied during the operation.
2253 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2254 * error is returned, independent on the status of the @destination.
2256 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2257 * error G_IO_ERROR_EXISTS is returned.
2259 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2260 * error is returned. If trying to overwrite a directory with a directory the
2261 * G_IO_ERROR_WOULD_MERGE error is returned.
2263 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2264 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2265 * may be returned (if the native move operation isn't available).
2267 * Returns: %TRUE on successful move, %FALSE otherwise.
2270 g_file_move (GFile *source,
2272 GFileCopyFlags flags,
2273 GCancellable *cancellable,
2274 GFileProgressCallback progress_callback,
2275 gpointer progress_callback_data,
2282 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2283 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2285 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2288 iface = G_FILE_GET_IFACE (destination);
2292 res = (* iface->move) (source, destination,
2294 progress_callback, progress_callback_data,
2300 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2302 g_propagate_error (error, my_error);
2307 /* If the types are different, and the destination method failed
2308 also try the source method */
2309 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2311 iface = G_FILE_GET_IFACE (source);
2316 res = (* iface->move) (source, destination,
2318 progress_callback, progress_callback_data,
2324 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2326 g_propagate_error (error, my_error);
2332 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2334 g_set_error (error, G_IO_ERROR,
2335 G_IO_ERROR_NOT_SUPPORTED,
2336 _("Operation not supported"));
2340 flags |= G_FILE_COPY_ALL_METADATA;
2341 if (!g_file_copy (source, destination, flags, cancellable,
2342 progress_callback, progress_callback_data,
2346 return g_file_delete (source, cancellable, error);
2350 * g_file_make_directory
2351 * @file: input #GFile.
2352 * @cancellable: optional #GCancellable object, %NULL to ignore.
2353 * @error: a #GError, or %NULL
2355 * Creates a directory.
2357 * If @cancellable is not %NULL, then the operation can be cancelled by
2358 * triggering the cancellable object from another thread. If the operation
2359 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2361 * Returns: %TRUE on successful creation, %FALSE otherwise.
2364 g_file_make_directory (GFile *file,
2365 GCancellable *cancellable,
2370 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2372 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2375 iface = G_FILE_GET_IFACE (file);
2377 if (iface->make_directory == NULL)
2379 g_set_error (error, G_IO_ERROR,
2380 G_IO_ERROR_NOT_SUPPORTED,
2381 _("Operation not supported"));
2385 return (* iface->make_directory) (file, cancellable, error);
2389 * g_file_make_symbolic_link:
2390 * @file: input #GFile.
2391 * @symlink_value: a string with the value of the new symlink.
2392 * @cancellable: optional #GCancellable object, %NULL to ignore.
2393 * @error: a #GError.
2395 * Creates a symbolic link.
2397 * If @cancellable is not %NULL, then the operation can be cancelled by
2398 * triggering the cancellable object from another thread. If the operation
2399 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2401 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2404 g_file_make_symbolic_link (GFile *file,
2405 const char *symlink_value,
2406 GCancellable *cancellable,
2411 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2412 g_return_val_if_fail (symlink_value != NULL, FALSE);
2414 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2417 if (*symlink_value == '\0')
2419 g_set_error (error, G_IO_ERROR,
2420 G_IO_ERROR_INVALID_ARGUMENT,
2421 _("Invalid symlink value given"));
2425 iface = G_FILE_GET_IFACE (file);
2427 if (iface->make_symbolic_link == NULL)
2429 g_set_error (error, G_IO_ERROR,
2430 G_IO_ERROR_NOT_SUPPORTED,
2431 _("Operation not supported"));
2435 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2440 * @file: input #GFile.
2441 * @cancellable: optional #GCancellable object, %NULL to ignore.
2442 * @error: a #GError, or %NULL
2446 * If @cancellable is not %NULL, then the operation can be cancelled by
2447 * triggering the cancellable object from another thread. If the operation
2448 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2450 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2453 g_file_delete (GFile *file,
2454 GCancellable *cancellable,
2459 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2461 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2464 iface = G_FILE_GET_IFACE (file);
2466 if (iface->delete_file == NULL)
2468 g_set_error (error, G_IO_ERROR,
2469 G_IO_ERROR_NOT_SUPPORTED,
2470 _("Operation not supported"));
2474 return (* iface->delete_file) (file, cancellable, error);
2479 * @file: #GFile to send to trash.
2480 * @cancellable: optional #GCancellable object, %NULL to ignore.
2481 * @error: a #GError, or %NULL
2483 * Sends @file to the "Trashcan", if possible. This is similar to
2484 * deleting it, but the user can recover it before emptying the trashcan.
2485 * Not all file systems support trashing, so this call can return the
2486 * %G_IO_ERROR_NOT_SUPPORTED error.
2489 * If @cancellable is not %NULL, then the operation can be cancelled by
2490 * triggering the cancellable object from another thread. If the operation
2491 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2493 * Returns: %TRUE on successful trash, %FALSE otherwise.
2496 g_file_trash (GFile *file,
2497 GCancellable *cancellable,
2502 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2504 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2507 iface = G_FILE_GET_IFACE (file);
2509 if (iface->trash == NULL)
2512 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2513 _("Trash not supported"));
2517 return (* iface->trash) (file, cancellable, error);
2521 * g_file_set_display_name:
2522 * @file: input #GFile.
2523 * @display_name: a string.
2524 * @cancellable: optional #GCancellable object, %NULL to ignore.
2525 * @error: a #GError, or %NULL
2527 * Renames @file to the specified display name.
2529 * The display name is converted from UTF8 to the correct encoding for the target
2530 * filesystem if possible and the @file is renamed to this.
2532 * If you want to implement a rename operation in the user interface the edit name
2533 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2534 * widget, and then the result after editing should be passed to g_file_set_display_name().
2536 * On success the resulting converted filename is returned.
2538 * If @cancellable is not %NULL, then the operation can be cancelled by
2539 * triggering the cancellable object from another thread. If the operation
2540 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2542 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2545 g_file_set_display_name (GFile *file,
2546 const char *display_name,
2547 GCancellable *cancellable,
2552 g_return_val_if_fail (G_IS_FILE (file), NULL);
2553 g_return_val_if_fail (display_name != NULL, NULL);
2555 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2559 G_IO_ERROR_INVALID_ARGUMENT,
2560 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2564 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2567 iface = G_FILE_GET_IFACE (file);
2569 return (* iface->set_display_name) (file, display_name, cancellable, error);
2573 * g_file_set_display_name_async:
2574 * @file: input #GFile.
2575 * @display_name: a string.
2576 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2578 * @cancellable: optional #GCancellable object, %NULL to ignore.
2579 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2580 * @user_data: the data to pass to callback function
2582 * Asynchronously sets the display name for a given #GFile.
2584 * For more details, see g_set_display_name() which is
2585 * the synchronous version of this call.
2587 * When the operation is finished, @callback will be called. You can then call
2588 * g_file_set_display_name_finish() to get the result of the operation.
2591 g_file_set_display_name_async (GFile *file,
2592 const char *display_name,
2594 GCancellable *cancellable,
2595 GAsyncReadyCallback callback,
2600 g_return_if_fail (G_IS_FILE (file));
2601 g_return_if_fail (display_name != NULL);
2603 iface = G_FILE_GET_IFACE (file);
2604 (* iface->set_display_name_async) (file,
2613 * g_file_set_display_name_finish:
2614 * @file: input #GFile.
2615 * @res: a #GAsyncResult.
2616 * @error: a #GError, or %NULL
2618 * Finishes setting a display name started with
2619 * g_file_set_display_name_async().
2621 * Returns: a #GFile or %NULL on error.
2624 g_file_set_display_name_finish (GFile *file,
2630 g_return_val_if_fail (G_IS_FILE (file), NULL);
2631 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2633 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2635 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2636 if (g_simple_async_result_propagate_error (simple, error))
2640 iface = G_FILE_GET_IFACE (file);
2641 return (* iface->set_display_name_finish) (file, res, error);
2645 * g_file_query_settable_attributes:
2646 * @file: input #GFile.
2647 * @cancellable: optional #GCancellable object, %NULL to ignore.
2648 * @error: a #GError, or %NULL
2650 * Obtain the list of settable attributes for the file.
2652 * Returns the type and full attribute name of all the attributes
2653 * that can be set on this file. This doesn't mean setting it will always
2654 * succeed though, you might get an access failure, or some specific
2655 * file may not support a specific attribute.
2657 * If @cancellable is not %NULL, then the operation can be cancelled by
2658 * triggering the cancellable object from another thread. If the operation
2659 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2661 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2662 * When you are done with it, release it with g_file_attribute_info_list_unref()
2664 GFileAttributeInfoList *
2665 g_file_query_settable_attributes (GFile *file,
2666 GCancellable *cancellable,
2671 GFileAttributeInfoList *list;
2673 g_return_val_if_fail (G_IS_FILE (file), NULL);
2675 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2678 iface = G_FILE_GET_IFACE (file);
2680 if (iface->query_settable_attributes == NULL)
2681 return g_file_attribute_info_list_new ();
2684 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2688 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2690 list = g_file_attribute_info_list_new ();
2691 g_error_free (my_error);
2694 g_propagate_error (error, my_error);
2701 * g_file_query_writable_namespaces:
2702 * @file: input #GFile.
2703 * @cancellable: optional #GCancellable object, %NULL to ignore.
2704 * @error: a #GError, or %NULL
2706 * Obtain the list of attribute namespaces where new attributes
2707 * can be created by a user. An example of this is extended
2708 * attributes (in the "xattr" namespace).
2710 * If @cancellable is not %NULL, then the operation can be cancelled by
2711 * triggering the cancellable object from another thread. If the operation
2712 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2714 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2715 * When you are done with it, release it with g_file_attribute_info_list_unref()
2717 GFileAttributeInfoList *
2718 g_file_query_writable_namespaces (GFile *file,
2719 GCancellable *cancellable,
2724 GFileAttributeInfoList *list;
2726 g_return_val_if_fail (G_IS_FILE (file), NULL);
2728 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2731 iface = G_FILE_GET_IFACE (file);
2733 if (iface->query_writable_namespaces == NULL)
2734 return g_file_attribute_info_list_new ();
2737 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
2741 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2743 list = g_file_attribute_info_list_new ();
2744 g_error_free (my_error);
2747 g_propagate_error (error, my_error);
2754 * g_file_set_attribute:
2755 * @file: input #GFile.
2756 * @attribute: a string containing the attribute's name.
2757 * @type: The type of the attribute
2758 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
2759 * @flags: a set of #GFileQueryInfoFlags.
2760 * @cancellable: optional #GCancellable object, %NULL to ignore.
2761 * @error: a #GError, or %NULL
2763 * Sets an attribute in the file with attribute name @attribute to @value.
2765 * If @cancellable is not %NULL, then the operation can be cancelled by
2766 * triggering the cancellable object from another thread. If the operation
2767 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2769 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
2772 g_file_set_attribute (GFile *file,
2773 const char *attribute,
2774 GFileAttributeType type,
2776 GFileQueryInfoFlags flags,
2777 GCancellable *cancellable,
2782 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2783 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
2785 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2788 iface = G_FILE_GET_IFACE (file);
2790 if (iface->set_attribute == NULL)
2792 g_set_error (error, G_IO_ERROR,
2793 G_IO_ERROR_NOT_SUPPORTED,
2794 _("Operation not supported"));
2798 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
2802 * g_file_set_attributes_from_info:
2803 * @file: input #GFile.
2804 * @info: a #GFileInfo.
2805 * @flags: #GFileQueryInfoFlags
2806 * @cancellable: optional #GCancellable object, %NULL to ignore.
2807 * @error: a #GError, or %NULL
2809 * Tries to set all attributes in the #GFileInfo on the target values,
2810 * not stopping on the first error.
2812 * If there is any error during this operation then @error will be set to
2813 * the first error. Error on particular fields are flagged by setting
2814 * the "status" field in the attribute value to
2815 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
2818 * If @cancellable is not %NULL, then the operation can be cancelled by
2819 * triggering the cancellable object from another thread. If the operation
2820 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2822 * Returns: %TRUE if there was any error, %FALSE otherwise.
2825 g_file_set_attributes_from_info (GFile *file,
2827 GFileQueryInfoFlags flags,
2828 GCancellable *cancellable,
2833 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2834 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
2836 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2839 g_file_info_clear_status (info);
2841 iface = G_FILE_GET_IFACE (file);
2843 return (* iface->set_attributes_from_info) (file,
2852 g_file_real_set_attributes_from_info (GFile *file,
2854 GFileQueryInfoFlags flags,
2855 GCancellable *cancellable,
2861 GFileAttributeValue *value;
2865 attributes = g_file_info_list_attributes (info, NULL);
2867 for (i = 0; attributes[i] != NULL; i++)
2869 value = _g_file_info_get_attribute_value (info, attributes[i]);
2871 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
2874 if (!g_file_set_attribute (file, attributes[i],
2875 value->type, _g_file_attribute_value_peek_as_pointer (value),
2876 flags, cancellable, error))
2878 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
2880 /* Don't set error multiple times */
2884 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
2887 g_strfreev (attributes);
2893 * g_file_set_attributes_async:
2894 * @file: input #GFile.
2895 * @info: a #GFileInfo.
2896 * @flags: a #GFileQueryInfoFlags.
2897 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2899 * @cancellable: optional #GCancellable object, %NULL to ignore.
2900 * @callback: a #GAsyncReadyCallback.
2901 * @user_data: a #gpointer.
2903 * Asynchronously sets the attributes of @file with @info.
2905 * For more details, see g_file_set_attributes_from_info() which is
2906 * the synchronous version of this call.
2908 * When the operation is finished, @callback will be called. You can then call
2909 * g_file_set_attributes_finish() to get the result of the operation.
2912 g_file_set_attributes_async (GFile *file,
2914 GFileQueryInfoFlags flags,
2916 GCancellable *cancellable,
2917 GAsyncReadyCallback callback,
2922 g_return_if_fail (G_IS_FILE (file));
2923 g_return_if_fail (G_IS_FILE_INFO (info));
2925 iface = G_FILE_GET_IFACE (file);
2926 (* iface->set_attributes_async) (file,
2936 * g_file_set_attributes_finish:
2937 * @file: input #GFile.
2938 * @result: a #GAsyncResult.
2939 * @info: a #GFileInfo.
2940 * @error: a #GError, or %NULL
2942 * Finishes setting an attribute started in g_file_set_attributes_async().
2944 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
2947 g_file_set_attributes_finish (GFile *file,
2948 GAsyncResult *result,
2954 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2955 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
2957 /* No standard handling of errors here, as we must set info even
2960 iface = G_FILE_GET_IFACE (file);
2961 return (* iface->set_attributes_finish) (file, result, info, error);
2965 * g_file_set_attribute_string:
2966 * @file: input #GFile.
2967 * @attribute: a string containing the attribute's name.
2968 * @value: a string containing the attribute's value.
2969 * @flags: #GFileQueryInfoFlags.
2970 * @cancellable: optional #GCancellable object, %NULL to ignore.
2971 * @error: a #GError, or %NULL
2973 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
2974 * If @attribute is of a different type, this operation will fail.
2976 * If @cancellable is not %NULL, then the operation can be cancelled by
2977 * triggering the cancellable object from another thread. If the operation
2978 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2980 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
2983 g_file_set_attribute_string (GFile *file,
2984 const char *attribute,
2986 GFileQueryInfoFlags flags,
2987 GCancellable *cancellable,
2990 return g_file_set_attribute (file, attribute,
2991 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
2992 flags, cancellable, error);
2996 * g_file_set_attribute_byte_string:
2997 * @file: input #GFile.
2998 * @attribute: a string containing the attribute's name.
2999 * @value: a string containing the attribute's new value.
3000 * @flags: a #GFileQueryInfoFlags.
3001 * @cancellable: optional #GCancellable object, %NULL to ignore.
3002 * @error: a #GError, or %NULL
3004 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3005 * If @attribute is of a different type, this operation will fail,
3008 * If @cancellable is not %NULL, then the operation can be cancelled by
3009 * triggering the cancellable object from another thread. If the operation
3010 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3012 * Returns: %TRUE if the @attribute was successfully set to @value
3013 * in the @file, %FALSE otherwise.
3016 g_file_set_attribute_byte_string (GFile *file,
3017 const char *attribute,
3019 GFileQueryInfoFlags flags,
3020 GCancellable *cancellable,
3023 return g_file_set_attribute (file, attribute,
3024 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3025 flags, cancellable, error);
3029 * g_file_set_attribute_uint32:
3030 * @file: input #GFile.
3031 * @attribute: a string containing the attribute's name.
3032 * @value: a #guint32 containing the attribute's new value.
3033 * @flags: a #GFileQueryInfoFlags.
3034 * @cancellable: optional #GCancellable object, %NULL to ignore.
3035 * @error: a #GError, or %NULL
3037 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3038 * If @attribute is of a different type, this operation will fail.
3040 * If @cancellable is not %NULL, then the operation can be cancelled by
3041 * triggering the cancellable object from another thread. If the operation
3042 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3044 * Returns: %TRUE if the @attribute was successfully set to @value
3045 * in the @file, %FALSE otherwise.
3048 g_file_set_attribute_uint32 (GFile *file,
3049 const char *attribute,
3051 GFileQueryInfoFlags flags,
3052 GCancellable *cancellable,
3055 return g_file_set_attribute (file, attribute,
3056 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3057 flags, cancellable, error);
3061 * g_file_set_attribute_int32:
3062 * @file: input #GFile.
3063 * @attribute: a string containing the attribute's name.
3064 * @value: a #gint32 containing the attribute's new value.
3065 * @flags: a #GFileQueryInfoFlags.
3066 * @cancellable: optional #GCancellable object, %NULL to ignore.
3067 * @error: a #GError, or %NULL
3069 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3070 * If @attribute is of a different type, this operation will fail.
3072 * If @cancellable is not %NULL, then the operation can be cancelled by
3073 * triggering the cancellable object from another thread. If the operation
3074 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3076 * Returns: %TRUE if the @attribute was successfully set to @value
3077 * in the @file, %FALSE otherwise.
3080 g_file_set_attribute_int32 (GFile *file,
3081 const char *attribute,
3083 GFileQueryInfoFlags flags,
3084 GCancellable *cancellable,
3087 return g_file_set_attribute (file, attribute,
3088 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3089 flags, cancellable, error);
3093 * g_file_set_attribute_uint64:
3094 * @file: input #GFile.
3095 * @attribute: a string containing the attribute's name.
3096 * @value: a #guint64 containing the attribute's new value.
3097 * @flags: a #GFileQueryInfoFlags.
3098 * @cancellable: optional #GCancellable object, %NULL to ignore.
3099 * @error: a #GError, or %NULL
3101 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3102 * If @attribute is of a different type, this operation will fail.
3104 * If @cancellable is not %NULL, then the operation can be cancelled by
3105 * triggering the cancellable object from another thread. If the operation
3106 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3108 * Returns: %TRUE if the @attribute was successfully set to @value
3109 * in the @file, %FALSE otherwise.
3112 g_file_set_attribute_uint64 (GFile *file,
3113 const char *attribute,
3115 GFileQueryInfoFlags flags,
3116 GCancellable *cancellable,
3119 return g_file_set_attribute (file, attribute,
3120 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3121 flags, cancellable, error);
3125 * g_file_set_attribute_int64:
3126 * @file: input #GFile.
3127 * @attribute: a string containing the attribute's name.
3128 * @value: a #guint64 containing the attribute's new value.
3129 * @flags: a #GFileQueryInfoFlags.
3130 * @cancellable: optional #GCancellable object, %NULL to ignore.
3131 * @error: a #GError, or %NULL
3133 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3134 * If @attribute is of a different type, this operation will fail.
3136 * If @cancellable is not %NULL, then the operation can be cancelled by
3137 * triggering the cancellable object from another thread. If the operation
3138 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3140 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3143 g_file_set_attribute_int64 (GFile *file,
3144 const char *attribute,
3146 GFileQueryInfoFlags flags,
3147 GCancellable *cancellable,
3150 return g_file_set_attribute (file, attribute,
3151 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3152 flags, cancellable, error);
3156 * g_file_mount_mountable:
3157 * @file: input #GFile.
3158 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3159 * @cancellable: optional #GCancellable object, %NULL to ignore.
3160 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3161 * @user_data: the data to pass to callback function
3163 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3164 * Using @mount_operation, you can request callbacks when, for instance,
3165 * passwords are needed during authentication.
3167 * If @cancellable is not %NULL, then the operation can be cancelled by
3168 * triggering the cancellable object from another thread. If the operation
3169 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3171 * When the operation is finished, @callback will be called. You can then call
3172 * g_file_mount_mountable_finish() to get the result of the operation.
3175 g_file_mount_mountable (GFile *file,
3176 GMountOperation *mount_operation,
3177 GCancellable *cancellable,
3178 GAsyncReadyCallback callback,
3183 g_return_if_fail (G_IS_FILE (file));
3185 iface = G_FILE_GET_IFACE (file);
3187 if (iface->mount_mountable == NULL)
3188 g_simple_async_report_error_in_idle (G_OBJECT (file),
3192 G_IO_ERROR_NOT_SUPPORTED,
3193 _("Operation not supported"));
3195 (* iface->mount_mountable) (file,
3203 * g_file_mount_mountable_finish:
3204 * @file: input #GFile.
3205 * @result: a #GAsyncResult.
3206 * @error: a #GError, or %NULL
3208 * Finishes a mount operation. See g_file_mount_mountable() for details.
3210 * Finish an asynchronous mount operation that was started
3211 * with g_file_mount_mountable().
3213 * Returns: a #GFile or %NULL on error.
3216 g_file_mount_mountable_finish (GFile *file,
3217 GAsyncResult *result,
3222 g_return_val_if_fail (G_IS_FILE (file), NULL);
3223 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3225 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3227 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3228 if (g_simple_async_result_propagate_error (simple, error))
3232 iface = G_FILE_GET_IFACE (file);
3233 return (* iface->mount_mountable_finish) (file, result, error);
3237 * g_file_unmount_mountable:
3238 * @file: input #GFile.
3239 * @flags: flags affecting the operation
3240 * @cancellable: optional #GCancellable object, %NULL to ignore.
3241 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3242 * @user_data: the data to pass to callback function
3244 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3246 * If @cancellable is not %NULL, then the operation can be cancelled by
3247 * triggering the cancellable object from another thread. If the operation
3248 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3250 * When the operation is finished, @callback will be called. You can then call
3251 * g_file_unmount_mountable_finish() to get the result of the operation.
3254 g_file_unmount_mountable (GFile *file,
3255 GMountUnmountFlags flags,
3256 GCancellable *cancellable,
3257 GAsyncReadyCallback callback,
3262 g_return_if_fail (G_IS_FILE (file));
3264 iface = G_FILE_GET_IFACE (file);
3266 if (iface->unmount_mountable == NULL)
3267 g_simple_async_report_error_in_idle (G_OBJECT (file),
3271 G_IO_ERROR_NOT_SUPPORTED,
3272 _("Operation not supported"));
3274 (* iface->unmount_mountable) (file,
3282 * g_file_unmount_mountable_finish:
3283 * @file: input #GFile.
3284 * @result: a #GAsyncResult.
3285 * @error: a #GError, or %NULL
3287 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3289 * Finish an asynchronous unmount operation that was started
3290 * with g_file_unmount_mountable().
3292 * Returns: %TRUE if the operation finished successfully. %FALSE
3296 g_file_unmount_mountable_finish (GFile *file,
3297 GAsyncResult *result,
3302 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3303 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3305 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3307 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3308 if (g_simple_async_result_propagate_error (simple, error))
3312 iface = G_FILE_GET_IFACE (file);
3313 return (* iface->unmount_mountable_finish) (file, result, error);
3317 * g_file_eject_mountable:
3318 * @file: input #GFile.
3319 * @flags: flags affecting the operation
3320 * @cancellable: optional #GCancellable object, %NULL to ignore.
3321 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3322 * @user_data: the data to pass to callback function
3324 * Starts an asynchronous eject on a mountable.
3325 * When this operation has completed, @callback will be called with
3326 * @user_user data, and the operation can be finalized with
3327 * g_file_eject_mountable_finish().
3329 * If @cancellable is not %NULL, then the operation can be cancelled by
3330 * triggering the cancellable object from another thread. If the operation
3331 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3334 g_file_eject_mountable (GFile *file,
3335 GMountUnmountFlags flags,
3336 GCancellable *cancellable,
3337 GAsyncReadyCallback callback,
3342 g_return_if_fail (G_IS_FILE (file));
3344 iface = G_FILE_GET_IFACE (file);
3346 if (iface->eject_mountable == NULL)
3347 g_simple_async_report_error_in_idle (G_OBJECT (file),
3351 G_IO_ERROR_NOT_SUPPORTED,
3352 _("Operation not supported"));
3354 (* iface->eject_mountable) (file,
3362 * g_file_eject_mountable_finish:
3363 * @file: input #GFile.
3364 * @result: a #GAsyncResult.
3365 * @error: a #GError, or %NULL
3367 * Finishes an asynchronous eject operation started by
3368 * g_file_eject_mountable().
3370 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3374 g_file_eject_mountable_finish (GFile *file,
3375 GAsyncResult *result,
3380 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3381 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3383 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3385 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3386 if (g_simple_async_result_propagate_error (simple, error))
3390 iface = G_FILE_GET_IFACE (file);
3391 return (* iface->eject_mountable_finish) (file, result, error);
3395 * g_file_monitor_directory:
3396 * @file: input #GFile.
3397 * @flags: a set of #GFileMonitorFlags.
3398 * @cancellable: optional #GCancellable object, %NULL to ignore.
3399 * @error: a #GError, or %NULL.
3401 * Obtains a directory monitor for the given file.
3402 * This may fail if directory monitoring is not supported.
3404 * If @cancellable is not %NULL, then the operation can be cancelled by
3405 * triggering the cancellable object from another thread. If the operation
3406 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3408 * Returns: a #GFileMonitor for the given @file,
3409 * or %NULL on error.
3412 g_file_monitor_directory (GFile *file,
3413 GFileMonitorFlags flags,
3414 GCancellable *cancellable,
3419 g_return_val_if_fail (G_IS_FILE (file), NULL);
3421 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3424 iface = G_FILE_GET_IFACE (file);
3426 if (iface->monitor_dir == NULL)
3428 g_set_error (error, G_IO_ERROR,
3429 G_IO_ERROR_NOT_SUPPORTED,
3430 _("Operation not supported"));
3434 return (* iface->monitor_dir) (file, flags, cancellable, error);
3438 * g_file_monitor_file:
3439 * @file: input #GFile.
3440 * @flags: a set of #GFileMonitorFlags.
3441 * @cancellable: optional #GCancellable object, %NULL to ignore.
3442 * @error: a #GError, or %NULL.
3444 * Obtains a file monitor for the given file. If no file notification
3445 * mechanism exists, then regular polling of the file is used.
3447 * If @cancellable is not %NULL, then the operation can be cancelled by
3448 * triggering the cancellable object from another thread. If the operation
3449 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3451 * Returns: a #GFileMonitor for the given @file.
3454 g_file_monitor_file (GFile *file,
3455 GFileMonitorFlags flags,
3456 GCancellable *cancellable,
3460 GFileMonitor *monitor;
3462 g_return_val_if_fail (G_IS_FILE (file), NULL);
3464 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3467 iface = G_FILE_GET_IFACE (file);
3471 if (iface->monitor_file)
3472 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3474 /* Fallback to polling */
3475 if (monitor == NULL)
3476 monitor = _g_poll_file_monitor_new (file);
3481 /********************************************
3482 * Default implementation of async ops *
3483 ********************************************/
3487 GFileQueryInfoFlags flags;
3489 } QueryInfoAsyncData;
3492 query_info_data_free (QueryInfoAsyncData *data)
3495 g_object_unref (data->info);
3496 g_free (data->attributes);
3501 query_info_async_thread (GSimpleAsyncResult *res,
3503 GCancellable *cancellable)
3505 GError *error = NULL;
3506 QueryInfoAsyncData *data;
3509 data = g_simple_async_result_get_op_res_gpointer (res);
3511 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3515 g_simple_async_result_set_from_error (res, error);
3516 g_error_free (error);
3523 g_file_real_query_info_async (GFile *file,
3524 const char *attributes,
3525 GFileQueryInfoFlags flags,
3527 GCancellable *cancellable,
3528 GAsyncReadyCallback callback,
3531 GSimpleAsyncResult *res;
3532 QueryInfoAsyncData *data;
3534 data = g_new0 (QueryInfoAsyncData, 1);
3535 data->attributes = g_strdup (attributes);
3536 data->flags = flags;
3538 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3539 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3541 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3542 g_object_unref (res);
3546 g_file_real_query_info_finish (GFile *file,
3550 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3551 QueryInfoAsyncData *data;
3553 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3555 data = g_simple_async_result_get_op_res_gpointer (simple);
3557 return g_object_ref (data->info);
3564 GFileQueryInfoFlags flags;
3565 GFileEnumerator *enumerator;
3566 } EnumerateChildrenAsyncData;
3569 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3571 if (data->enumerator)
3572 g_object_unref (data->enumerator);
3573 g_free (data->attributes);
3578 enumerate_children_async_thread (GSimpleAsyncResult *res,
3580 GCancellable *cancellable)
3582 GError *error = NULL;
3583 EnumerateChildrenAsyncData *data;
3584 GFileEnumerator *enumerator;
3586 data = g_simple_async_result_get_op_res_gpointer (res);
3588 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3590 if (enumerator == NULL)
3592 g_simple_async_result_set_from_error (res, error);
3593 g_error_free (error);
3596 data->enumerator = enumerator;
3600 g_file_real_enumerate_children_async (GFile *file,
3601 const char *attributes,
3602 GFileQueryInfoFlags flags,
3604 GCancellable *cancellable,
3605 GAsyncReadyCallback callback,
3608 GSimpleAsyncResult *res;
3609 EnumerateChildrenAsyncData *data;
3611 data = g_new0 (EnumerateChildrenAsyncData, 1);
3612 data->attributes = g_strdup (attributes);
3613 data->flags = flags;
3615 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3616 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3618 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3619 g_object_unref (res);
3622 static GFileEnumerator *
3623 g_file_real_enumerate_children_finish (GFile *file,
3627 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3628 EnumerateChildrenAsyncData *data;
3630 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3632 data = g_simple_async_result_get_op_res_gpointer (simple);
3633 if (data->enumerator)
3634 return g_object_ref (data->enumerator);
3640 open_read_async_thread (GSimpleAsyncResult *res,
3642 GCancellable *cancellable)
3645 GFileInputStream *stream;
3646 GError *error = NULL;
3648 iface = G_FILE_GET_IFACE (object);
3650 stream = iface->read_fn (G_FILE (object), cancellable, &error);
3654 g_simple_async_result_set_from_error (res, error);
3655 g_error_free (error);
3658 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3662 g_file_real_read_async (GFile *file,
3664 GCancellable *cancellable,
3665 GAsyncReadyCallback callback,
3668 GSimpleAsyncResult *res;
3670 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
3672 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
3673 g_object_unref (res);
3676 static GFileInputStream *
3677 g_file_real_read_finish (GFile *file,
3681 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3684 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
3686 op = g_simple_async_result_get_op_res_gpointer (simple);
3688 return g_object_ref (op);
3694 append_to_async_thread (GSimpleAsyncResult *res,
3696 GCancellable *cancellable)
3699 GFileCreateFlags *data;
3700 GFileOutputStream *stream;
3701 GError *error = NULL;
3703 iface = G_FILE_GET_IFACE (object);
3705 data = g_simple_async_result_get_op_res_gpointer (res);
3707 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
3711 g_simple_async_result_set_from_error (res, error);
3712 g_error_free (error);
3715 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3719 g_file_real_append_to_async (GFile *file,
3720 GFileCreateFlags flags,
3722 GCancellable *cancellable,
3723 GAsyncReadyCallback callback,
3726 GFileCreateFlags *data;
3727 GSimpleAsyncResult *res;
3729 data = g_new0 (GFileCreateFlags, 1);
3732 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
3733 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3735 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
3736 g_object_unref (res);
3739 static GFileOutputStream *
3740 g_file_real_append_to_finish (GFile *file,
3744 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3747 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
3749 op = g_simple_async_result_get_op_res_gpointer (simple);
3751 return g_object_ref (op);
3757 create_async_thread (GSimpleAsyncResult *res,
3759 GCancellable *cancellable)
3762 GFileCreateFlags *data;
3763 GFileOutputStream *stream;
3764 GError *error = NULL;
3766 iface = G_FILE_GET_IFACE (object);
3768 data = g_simple_async_result_get_op_res_gpointer (res);
3770 stream = iface->create (G_FILE (object), *data, cancellable, &error);
3774 g_simple_async_result_set_from_error (res, error);
3775 g_error_free (error);
3778 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
3782 g_file_real_create_async (GFile *file,
3783 GFileCreateFlags flags,
3785 GCancellable *cancellable,
3786 GAsyncReadyCallback callback,
3789 GFileCreateFlags *data;
3790 GSimpleAsyncResult *res;
3792 data = g_new0 (GFileCreateFlags, 1);
3795 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
3796 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
3798 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
3799 g_object_unref (res);
3802 static GFileOutputStream *
3803 g_file_real_create_finish (GFile *file,
3807 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3810 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
3812 op = g_simple_async_result_get_op_res_gpointer (simple);
3814 return g_object_ref (op);
3820 GFileOutputStream *stream;
3822 gboolean make_backup;
3823 GFileCreateFlags flags;
3827 replace_async_data_free (ReplaceAsyncData *data)
3830 g_object_unref (data->stream);
3831 g_free (data->etag);
3836 replace_async_thread (GSimpleAsyncResult *res,
3838 GCancellable *cancellable)
3841 GFileOutputStream *stream;
3842 GError *error = NULL;
3843 ReplaceAsyncData *data;
3845 iface = G_FILE_GET_IFACE (object);
3847 data = g_simple_async_result_get_op_res_gpointer (res);
3849 stream = iface->replace (G_FILE (object),
3858 g_simple_async_result_set_from_error (res, error);
3859 g_error_free (error);
3862 data->stream = stream;
3866 g_file_real_replace_async (GFile *file,
3868 gboolean make_backup,
3869 GFileCreateFlags flags,
3871 GCancellable *cancellable,
3872 GAsyncReadyCallback callback,
3875 GSimpleAsyncResult *res;
3876 ReplaceAsyncData *data;
3878 data = g_new0 (ReplaceAsyncData, 1);
3879 data->etag = g_strdup (etag);
3880 data->make_backup = make_backup;
3881 data->flags = flags;
3883 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
3884 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
3886 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
3887 g_object_unref (res);
3890 static GFileOutputStream *
3891 g_file_real_replace_finish (GFile *file,
3895 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3896 ReplaceAsyncData *data;
3898 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
3900 data = g_simple_async_result_get_op_res_gpointer (simple);
3902 return g_object_ref (data->stream);
3910 } SetDisplayNameAsyncData;
3913 set_display_name_data_free (SetDisplayNameAsyncData *data)
3915 g_free (data->name);
3917 g_object_unref (data->file);
3922 set_display_name_async_thread (GSimpleAsyncResult *res,
3924 GCancellable *cancellable)
3926 GError *error = NULL;
3927 SetDisplayNameAsyncData *data;
3930 data = g_simple_async_result_get_op_res_gpointer (res);
3932 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
3936 g_simple_async_result_set_from_error (res, error);
3937 g_error_free (error);
3944 g_file_real_set_display_name_async (GFile *file,
3945 const char *display_name,
3947 GCancellable *cancellable,
3948 GAsyncReadyCallback callback,
3951 GSimpleAsyncResult *res;
3952 SetDisplayNameAsyncData *data;
3954 data = g_new0 (SetDisplayNameAsyncData, 1);
3955 data->name = g_strdup (display_name);
3957 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
3958 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
3960 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
3961 g_object_unref (res);
3965 g_file_real_set_display_name_finish (GFile *file,
3969 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3970 SetDisplayNameAsyncData *data;
3972 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
3974 data = g_simple_async_result_get_op_res_gpointer (simple);
3976 return g_object_ref (data->file);
3982 GFileQueryInfoFlags flags;
3989 set_info_data_free (SetInfoAsyncData *data)
3992 g_object_unref (data->info);
3994 g_error_free (data->error);
3999 set_info_async_thread (GSimpleAsyncResult *res,
4001 GCancellable *cancellable)
4003 SetInfoAsyncData *data;
4005 data = g_simple_async_result_get_op_res_gpointer (res);
4008 data->res = g_file_set_attributes_from_info (G_FILE (object),
4016 g_file_real_set_attributes_async (GFile *file,
4018 GFileQueryInfoFlags flags,
4020 GCancellable *cancellable,
4021 GAsyncReadyCallback callback,
4024 GSimpleAsyncResult *res;
4025 SetInfoAsyncData *data;
4027 data = g_new0 (SetInfoAsyncData, 1);
4028 data->info = g_file_info_dup (info);
4029 data->flags = flags;
4031 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4032 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4034 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4035 g_object_unref (res);
4039 g_file_real_set_attributes_finish (GFile *file,
4044 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4045 SetInfoAsyncData *data;
4047 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4049 data = g_simple_async_result_get_op_res_gpointer (simple);
4052 *info = g_object_ref (data->info);
4054 if (error != NULL && data->error)
4055 *error = g_error_copy (data->error);
4061 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4063 GCancellable *cancellable)
4065 GError *error = NULL;
4068 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4072 g_simple_async_result_set_from_error (res, error);
4073 g_error_free (error);
4076 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4080 g_file_real_find_enclosing_mount_async (GFile *file,
4082 GCancellable *cancellable,
4083 GAsyncReadyCallback callback,
4086 GSimpleAsyncResult *res;
4088 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4090 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4091 g_object_unref (res);
4095 g_file_real_find_enclosing_mount_finish (GFile *file,
4099 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4102 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4104 mount = g_simple_async_result_get_op_res_gpointer (simple);
4105 return g_object_ref (mount);
4109 /********************************************
4110 * Default VFS operations *
4111 ********************************************/
4114 * g_file_new_for_path:
4115 * @path: a string containing a relative or absolute path.
4117 * Constructs a #GFile for a given path. This operation never
4118 * fails, but the returned object might not support any I/O
4119 * operation if @path is malformed.
4121 * Returns: a new #GFile for the given @path.
4124 g_file_new_for_path (const char *path)
4126 g_return_val_if_fail (path != NULL, NULL);
4128 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4132 * g_file_new_for_uri:
4133 * @uri: a string containing a URI.
4135 * Constructs a #GFile for a given URI. This operation never
4136 * fails, but the returned object might not support any I/O
4137 * operation if @uri is malformed or if the uri type is
4140 * Returns: a #GFile for the given @uri.
4143 g_file_new_for_uri (const char *uri)
4145 g_return_val_if_fail (uri != NULL, NULL);
4147 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4151 * g_file_parse_name:
4152 * @parse_name: a file name or path to be parsed.
4154 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4155 * This operation never fails, but the returned object might not support any I/O
4156 * operation if the @parse_name cannot be parsed.
4158 * Returns: a new #GFile.
4161 g_file_parse_name (const char *parse_name)
4163 g_return_val_if_fail (parse_name != NULL, NULL);
4165 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4169 is_valid_scheme_character (char c)
4171 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4175 has_valid_scheme (const char *uri)
4181 if (!is_valid_scheme_character (*p))
4186 } while (is_valid_scheme_character (*p));
4192 * g_file_new_for_commandline_arg:
4193 * @arg: a command line string.
4195 * Creates a #GFile with the given argument from the command line. The value of
4196 * @arg can be either a URI, an absolute path or a relative path resolved
4197 * relative to the current working directory.
4198 * This operation never fails, but the returned object might not support any
4199 * I/O operation if @arg points to a malformed path.
4201 * Returns: a new #GFile.
4204 g_file_new_for_commandline_arg (const char *arg)
4210 g_return_val_if_fail (arg != NULL, NULL);
4212 if (g_path_is_absolute (arg))
4213 return g_file_new_for_path (arg);
4215 if (has_valid_scheme (arg))
4216 return g_file_new_for_uri (arg);
4218 current_dir = g_get_current_dir ();
4219 filename = g_build_filename (current_dir, arg, NULL);
4220 g_free (current_dir);
4222 file = g_file_new_for_path (filename);
4229 * g_file_mount_enclosing_volume:
4230 * @location: input #GFile.
4231 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4232 * @cancellable: optional #GCancellable object, %NULL to ignore.
4233 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4234 * @user_data: the data to pass to callback function
4236 * Starts a @mount_operation, mounting the volume that contains the file @location.
4238 * When this operation has completed, @callback will be called with
4239 * @user_user data, and the operation can be finalized with
4240 * g_file_mount_enclosing_volume_finish().
4242 * If @cancellable is not %NULL, then the operation can be cancelled by
4243 * triggering the cancellable object from another thread. If the operation
4244 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4247 g_file_mount_enclosing_volume (GFile *location,
4248 GMountOperation *mount_operation,
4249 GCancellable *cancellable,
4250 GAsyncReadyCallback callback,
4255 g_return_if_fail (G_IS_FILE (location));
4257 iface = G_FILE_GET_IFACE (location);
4259 if (iface->mount_enclosing_volume == NULL)
4261 g_simple_async_report_error_in_idle (G_OBJECT (location),
4262 callback, user_data,
4263 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4264 _("volume doesn't implement mount"));
4269 (* iface->mount_enclosing_volume) (location, mount_operation, cancellable, callback, user_data);
4274 * g_file_mount_enclosing_volume_finish:
4275 * @location: input #GFile.
4276 * @result: a #GAsyncResult.
4277 * @error: a #GError, or %NULL
4279 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4281 * Returns: %TRUE if successful. If an error
4282 * has occurred, this function will return %FALSE and set @error
4283 * appropriately if present.
4286 g_file_mount_enclosing_volume_finish (GFile *location,
4287 GAsyncResult *result,
4292 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4293 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4295 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4297 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4298 if (g_simple_async_result_propagate_error (simple, error))
4302 iface = G_FILE_GET_IFACE (location);
4304 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4307 /********************************************
4308 * Utility functions *
4309 ********************************************/
4311 #define GET_CONTENT_BLOCK_SIZE 8192
4314 * g_file_load_contents:
4315 * @file: input #GFile.
4316 * @cancellable: optional #GCancellable object, %NULL to ignore.
4317 * @contents: a location to place the contents of the file.
4318 * @length: a location to place the length of the contents of the file.
4319 * @etag_out: a location to place the current entity tag for the file.
4320 * @error: a #GError, or %NULL
4322 * Loads the content of the file into memory, returning the size of
4323 * the data. The data is always zero terminated, but this is not
4324 * included in the resultant @length.
4326 * If @cancellable is not %NULL, then the operation can be cancelled by
4327 * triggering the cancellable object from another thread. If the operation
4328 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4330 * Returns: %TRUE if the @file's contents were successfully loaded.
4331 * %FALSE if there were errors..
4334 g_file_load_contents (GFile *file,
4335 GCancellable *cancellable,
4341 GFileInputStream *in;
4342 GByteArray *content;
4347 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4348 g_return_val_if_fail (contents != NULL, FALSE);
4350 in = g_file_read (file, cancellable, error);
4354 content = g_byte_array_new ();
4357 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4358 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4359 content->data + pos,
4360 GET_CONTENT_BLOCK_SIZE,
4361 cancellable, error)) > 0)
4364 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4371 info = g_file_input_stream_query_info (in,
4372 G_FILE_ATTRIBUTE_ETAG_VALUE,
4377 *etag_out = g_strdup (g_file_info_get_etag (info));
4378 g_object_unref (info);
4382 /* Ignore errors on close */
4383 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4384 g_object_unref (in);
4388 /* error is set already */
4389 g_byte_array_free (content, TRUE);
4396 /* Zero terminate (we got an extra byte allocated for this */
4397 content->data[pos] = 0;
4399 *contents = (char *)g_byte_array_free (content, FALSE);
4407 GCancellable *cancellable;
4408 GFileReadMoreCallback read_more_callback;
4409 GAsyncReadyCallback callback;
4411 GByteArray *content;
4418 load_contents_data_free (LoadContentsData *data)
4421 g_error_free (data->error);
4422 if (data->cancellable)
4423 g_object_unref (data->cancellable);
4425 g_byte_array_free (data->content, TRUE);
4426 g_free (data->etag);
4427 g_object_unref (data->file);
4432 load_contents_close_callback (GObject *obj,
4433 GAsyncResult *close_res,
4436 GInputStream *stream = G_INPUT_STREAM (obj);
4437 LoadContentsData *data = user_data;
4438 GSimpleAsyncResult *res;
4440 /* Ignore errors here, we're only reading anyway */
4441 g_input_stream_close_finish (stream, close_res, NULL);
4442 g_object_unref (stream);
4444 res = g_simple_async_result_new (G_OBJECT (data->file),
4447 g_file_load_contents_async);
4448 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
4449 g_simple_async_result_complete (res);
4450 g_object_unref (res);
4454 load_contents_fstat_callback (GObject *obj,
4455 GAsyncResult *stat_res,
4458 GInputStream *stream = G_INPUT_STREAM (obj);
4459 LoadContentsData *data = user_data;
4462 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
4466 data->etag = g_strdup (g_file_info_get_etag (info));
4467 g_object_unref (info);
4470 g_input_stream_close_async (stream, 0,
4472 load_contents_close_callback, data);
4476 load_contents_read_callback (GObject *obj,
4477 GAsyncResult *read_res,
4480 GInputStream *stream = G_INPUT_STREAM (obj);
4481 LoadContentsData *data = user_data;
4482 GError *error = NULL;
4485 read_size = g_input_stream_read_finish (stream, read_res, &error);
4489 /* Error or EOF, close the file */
4490 data->error = error;
4491 g_input_stream_close_async (stream, 0,
4493 load_contents_close_callback, data);
4495 else if (read_size == 0)
4497 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4498 G_FILE_ATTRIBUTE_ETAG_VALUE,
4501 load_contents_fstat_callback,
4504 else if (read_size > 0)
4506 data->pos += read_size;
4508 g_byte_array_set_size (data->content,
4509 data->pos + GET_CONTENT_BLOCK_SIZE);
4512 if (data->read_more_callback &&
4513 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
4514 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
4515 G_FILE_ATTRIBUTE_ETAG_VALUE,
4518 load_contents_fstat_callback,
4521 g_input_stream_read_async (stream,
4522 data->content->data + data->pos,
4523 GET_CONTENT_BLOCK_SIZE,
4526 load_contents_read_callback,
4532 load_contents_open_callback (GObject *obj,
4533 GAsyncResult *open_res,
4536 GFile *file = G_FILE (obj);
4537 GFileInputStream *stream;
4538 LoadContentsData *data = user_data;
4539 GError *error = NULL;
4540 GSimpleAsyncResult *res;
4542 stream = g_file_read_finish (file, open_res, &error);
4546 g_byte_array_set_size (data->content,
4547 data->pos + GET_CONTENT_BLOCK_SIZE);
4548 g_input_stream_read_async (G_INPUT_STREAM (stream),
4549 data->content->data + data->pos,
4550 GET_CONTENT_BLOCK_SIZE,
4553 load_contents_read_callback,
4559 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4563 g_simple_async_result_complete (res);
4564 g_error_free (error);
4565 load_contents_data_free (data);
4566 g_object_unref (res);
4571 * g_file_load_partial_contents_async:
4572 * @file: input #GFile.
4573 * @cancellable: optional #GCancellable object, %NULL to ignore.
4574 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
4575 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4576 * @user_data: the data to pass to the callback functions.
4578 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
4579 * used to stop reading from the file when appropriate, else this function
4580 * will behave exactly as g_file_load_contents_async(). This operation
4581 * can be finished by g_file_load_partial_contents_finish().
4583 * Users of this function should be aware that @user_data is passed to
4584 * both the @read_more_callback and the @callback.
4586 * If @cancellable is not %NULL, then the operation can be cancelled by
4587 * triggering the cancellable object from another thread. If the operation
4588 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4591 g_file_load_partial_contents_async (GFile *file,
4592 GCancellable *cancellable,
4593 GFileReadMoreCallback read_more_callback,
4594 GAsyncReadyCallback callback,
4597 LoadContentsData *data;
4599 g_return_if_fail (G_IS_FILE (file));
4601 data = g_new0 (LoadContentsData, 1);
4604 data->cancellable = g_object_ref (cancellable);
4605 data->read_more_callback = read_more_callback;
4606 data->callback = callback;
4607 data->user_data = user_data;
4608 data->content = g_byte_array_new ();
4609 data->file = g_object_ref (file);
4611 g_file_read_async (file,
4614 load_contents_open_callback,
4619 * g_file_load_partial_contents_finish:
4620 * @file: input #GFile.
4621 * @res: a #GAsyncResult.
4622 * @contents: a location to place the contents of the file.
4623 * @length: a location to place the length of the contents of the file.
4624 * @etag_out: a location to place the current entity tag for the file.
4625 * @error: a #GError, or %NULL
4627 * Finishes an asynchronous partial load operation that was started
4628 * with g_file_load_partial_contents_async().
4630 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4631 * present, it will be set appropriately.
4634 g_file_load_partial_contents_finish (GFile *file,
4641 GSimpleAsyncResult *simple;
4642 LoadContentsData *data;
4644 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4645 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
4646 g_return_val_if_fail (contents != NULL, FALSE);
4648 simple = G_SIMPLE_ASYNC_RESULT (res);
4650 if (g_simple_async_result_propagate_error (simple, error))
4653 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
4655 data = g_simple_async_result_get_op_res_gpointer (simple);
4659 g_propagate_error (error, data->error);
4668 *length = data->pos;
4672 *etag_out = data->etag;
4676 /* Zero terminate */
4677 g_byte_array_set_size (data->content, data->pos + 1);
4678 data->content->data[data->pos] = 0;
4680 *contents = (char *)g_byte_array_free (data->content, FALSE);
4681 data->content = NULL;
4687 * g_file_load_contents_async:
4688 * @file: input #GFile.
4689 * @cancellable: optional #GCancellable object, %NULL to ignore.
4690 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4691 * @user_data: the data to pass to callback function
4693 * Starts an asynchronous load of the @file's contents.
4695 * For more details, see g_file_load_contents() which is
4696 * the synchronous version of this call.
4698 * When the load operation has completed, @callback will be called
4699 * with @user data. To finish the operation, call
4700 * g_file_load_contents_finish() with the #GAsyncResult returned by
4703 * If @cancellable is not %NULL, then the operation can be cancelled by
4704 * triggering the cancellable object from another thread. If the operation
4705 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4708 g_file_load_contents_async (GFile *file,
4709 GCancellable *cancellable,
4710 GAsyncReadyCallback callback,
4713 g_file_load_partial_contents_async (file,
4716 callback, user_data);
4720 * g_file_load_contents_finish:
4721 * @file: input #GFile.
4722 * @res: a #GAsyncResult.
4723 * @contents: a location to place the contents of the file.
4724 * @length: a location to place the length of the contents of the file.
4725 * @etag_out: a location to place the current entity tag for the file.
4726 * @error: a #GError, or %NULL
4728 * Finishes an asynchronous load of the @file's contents.
4729 * The contents are placed in @contents, and @length is set to the
4730 * size of the @contents string. If @etag_out is present, it will be
4731 * set to the new entity tag for the @file.
4733 * Returns: %TRUE if the load was successful. If %FALSE and @error is
4734 * present, it will be set appropriately.
4737 g_file_load_contents_finish (GFile *file,
4744 return g_file_load_partial_contents_finish (file,
4753 * g_file_replace_contents:
4754 * @file: input #GFile.
4755 * @contents: a string containing the new contents for @file.
4756 * @length: the length of @contents in bytes.
4757 * @etag: the old <link linkend="gfile-etag">entity tag</link>
4759 * @make_backup: %TRUE if a backup should be created.
4760 * @flags: a set of #GFileCreateFlags.
4761 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
4763 * @cancellable: optional #GCancellable object, %NULL to ignore.
4764 * @error: a #GError, or %NULL
4766 * Replaces the contents of @file with @contents of @length bytes.
4768 * If @etag is specified (not %NULL) any existing file must have that etag, or
4769 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
4771 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
4773 * If @cancellable is not %NULL, then the operation can be cancelled by
4774 * triggering the cancellable object from another thread. If the operation
4775 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4777 * The returned @new_etag can be used to verify that the file hasn't changed the
4778 * next time it is saved over.
4780 * Returns: %TRUE if successful. If an error
4781 * has occurred, this function will return %FALSE and set @error
4782 * appropriately if present.
4785 g_file_replace_contents (GFile *file,
4786 const char *contents,
4789 gboolean make_backup,
4790 GFileCreateFlags flags,
4792 GCancellable *cancellable,
4795 GFileOutputStream *out;
4796 gsize pos, remainder;
4799 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4800 g_return_val_if_fail (contents != NULL, FALSE);
4802 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
4808 while (remainder > 0 &&
4809 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
4811 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
4819 if (remainder > 0 && res < 0)
4821 /* Ignore errors on close */
4822 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
4824 /* error is set already */
4828 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
4832 *new_etag = g_file_output_stream_get_etag (out);
4840 GCancellable *cancellable;
4841 GAsyncReadyCallback callback;
4843 const char *content;
4847 } ReplaceContentsData;
4850 replace_contents_data_free (ReplaceContentsData *data)
4853 g_error_free (data->error);
4854 if (data->cancellable)
4855 g_object_unref (data->cancellable);
4856 g_object_unref (data->file);
4857 g_free (data->etag);
4862 replace_contents_close_callback (GObject *obj,
4863 GAsyncResult *close_res,
4866 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4867 ReplaceContentsData *data = user_data;
4868 GSimpleAsyncResult *res;
4870 /* Ignore errors here, we're only reading anyway */
4871 g_output_stream_close_finish (stream, close_res, NULL);
4872 g_object_unref (stream);
4874 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
4876 res = g_simple_async_result_new (G_OBJECT (data->file),
4879 g_file_replace_contents_async);
4880 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
4881 g_simple_async_result_complete (res);
4882 g_object_unref (res);
4886 replace_contents_write_callback (GObject *obj,
4887 GAsyncResult *read_res,
4890 GOutputStream *stream = G_OUTPUT_STREAM (obj);
4891 ReplaceContentsData *data = user_data;
4892 GError *error = NULL;
4895 write_size = g_output_stream_write_finish (stream, read_res, &error);
4897 if (write_size <= 0)
4899 /* Error or EOF, close the file */
4901 data->error = error;
4902 g_output_stream_close_async (stream, 0,
4904 replace_contents_close_callback, data);
4906 else if (write_size > 0)
4908 data->pos += write_size;
4910 if (data->pos >= data->length)
4911 g_output_stream_close_async (stream, 0,
4913 replace_contents_close_callback, data);
4915 g_output_stream_write_async (stream,
4916 data->content + data->pos,
4917 data->length - data->pos,
4920 replace_contents_write_callback,
4926 replace_contents_open_callback (GObject *obj,
4927 GAsyncResult *open_res,
4930 GFile *file = G_FILE (obj);
4931 GFileOutputStream *stream;
4932 ReplaceContentsData *data = user_data;
4933 GError *error = NULL;
4934 GSimpleAsyncResult *res;
4936 stream = g_file_replace_finish (file, open_res, &error);
4940 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
4941 data->content + data->pos,
4942 data->length - data->pos,
4945 replace_contents_write_callback,
4951 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
4955 g_simple_async_result_complete (res);
4956 g_error_free (error);
4957 replace_contents_data_free (data);
4958 g_object_unref (res);
4963 * g_file_replace_contents_async:
4964 * @file: input #GFile.
4965 * @contents: string of contents to replace the file with.
4966 * @length: the length of @contents in bytes.
4967 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
4968 * @make_backup: %TRUE if a backup should be created.
4969 * @flags: a set of #GFileCreateFlags.
4970 * @cancellable: optional #GCancellable object, %NULL to ignore.
4971 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
4972 * @user_data: the data to pass to callback function
4974 * Starts an asynchronous replacement of @file with the given
4975 * @contents of @length bytes. @etag will replace the document's
4976 * current entity tag.
4978 * When this operation has completed, @callback will be called with
4979 * @user_user data, and the operation can be finalized with
4980 * g_file_replace_contents_finish().
4982 * If @cancellable is not %NULL, then the operation can be cancelled by
4983 * triggering the cancellable object from another thread. If the operation
4984 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4986 * If @make_backup is %TRUE, this function will attempt to
4987 * make a backup of @file.
4990 g_file_replace_contents_async (GFile *file,
4991 const char *contents,
4994 gboolean make_backup,
4995 GFileCreateFlags flags,
4996 GCancellable *cancellable,
4997 GAsyncReadyCallback callback,
5000 ReplaceContentsData *data;
5002 g_return_if_fail (G_IS_FILE (file));
5003 g_return_if_fail (contents != NULL);
5005 data = g_new0 (ReplaceContentsData, 1);
5008 data->cancellable = g_object_ref (cancellable);
5009 data->callback = callback;
5010 data->user_data = user_data;
5011 data->content = contents;
5012 data->length = length;
5014 data->file = g_object_ref (file);
5016 g_file_replace_async (file,
5022 replace_contents_open_callback,
5027 * g_file_replace_contents_finish:
5028 * @file: input #GFile.
5029 * @res: a #GAsyncResult.
5030 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5032 * @error: a #GError, or %NULL
5034 * Finishes an asynchronous replace of the given @file. See
5035 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5036 * tag for the document, if present.
5038 * Returns: %TRUE on success, %FALSE on failure.
5041 g_file_replace_contents_finish (GFile *file,
5046 GSimpleAsyncResult *simple;
5047 ReplaceContentsData *data;
5049 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5050 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5052 simple = G_SIMPLE_ASYNC_RESULT (res);
5054 if (g_simple_async_result_propagate_error (simple, error))
5057 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5059 data = g_simple_async_result_get_op_res_gpointer (simple);
5063 g_propagate_error (error, data->error);
5071 *new_etag = data->etag;
5072 data->etag = NULL; /* Take ownership */
5078 #define __G_FILE_C__
5079 #include "gioaliasdef.c"