1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
27 #include <sys/types.h>
33 #include "gioscheduler.h"
34 #include "glocalfile.h"
35 #include "gsimpleasyncresult.h"
36 #include "gfileattribute-priv.h"
37 #include "gpollfilemonitor.h"
39 #include "gfileinputstream.h"
40 #include "gfileoutputstream.h"
41 #include "gcancellable.h"
42 #include "gasyncresult.h"
50 * @short_description: File and Directory Handling
52 * @see_also: #GFileInfo, #GFileEnumerator
54 * #GFile is a high level abstraction for manipulating files on a
55 * virtual file system. #GFile<!-- -->s are lightweight, immutable
56 * objects that do no I/O upon creation. It is necessary to understand that
57 * #GFile objects do not represent files, merely an identifier for a file. All
58 * file content I/O is implemented as streaming operations (see #GInputStream and
61 * To construct a #GFile, you can use:
62 * g_file_new_for_path() if you have a path.
63 * g_file_new_for_uri() if you have a URI.
64 * g_file_new_for_commandline_arg() for a command line argument.
65 * g_file_parse_name() from a utf8 string gotten from g_file_get_parse_name().
67 * One way to think of a #GFile is as an abstraction of a pathname. For normal
68 * files the system pathname is what is stored internally, but as #GFile<!-- -->s
69 * are extensible it could also be something else that corresponds to a pathname
70 * in a userspace implementation of a filesystem.
72 * #GFile<!-- -->s make up hierarchies of directories and files that correspond to the
73 * files on a filesystem. You can move through the file system with #GFile using
74 * g_file_get_parent() to get an identifier for the parent directory, g_file_get_child()
75 * to get a child within a directory, g_file_resolve_relative_path() to resolve a relative
76 * path between two #GFile<!-- -->s. There can be multiple hierarchies, so you may not
77 * end up at the same root if you repeatedly call g_file_get_parent() on two different
80 * All #GFile<!-- -->s have a basename (get with g_file_get_basename()). These names
81 * are byte strings that are used to identify the file on the filesystem (relative to
82 * its parent directory) and there is no guarantees that they have any particular charset
83 * encoding or even make any sense at all. If you want to use filenames in a user
84 * interface you should use the display name that you can get by requesting the
85 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
86 * This is guaranteed to be in utf8 and can be used in a user interface. But always
87 * store the real basename or the #GFile to use to actually access the file, because
88 * there is no way to go from a display name to the actual name.
90 * Using #GFile as an identifier has the same weaknesses as using a path in that
91 * there may be multiple aliases for the same file. For instance, hard or
92 * soft links may cause two different #GFile<!-- -->s to refer to the same file.
93 * Other possible causes for aliases are: case insensitive filesystems, short
94 * and long names on Fat/NTFS, or bind mounts in Linux. If you want to check if
95 * two #GFile<!-- -->s point to the same file you can query for the
96 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
97 * canonicalization of pathnames passed in, so that trivial differences in the
98 * path string used at creation (duplicated slashes, slash at end of path, "."
99 * or ".." path segments, etc) does not create different #GFile<!-- -->s.
101 * Many #GFile operations have both synchronous and asynchronous versions
102 * to suit your application. Asynchronous versions of synchronous functions
103 * simply have _async() appended to their function names. The asynchronous
104 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
105 * the operation, producing a GAsyncResult which is then passed to the
106 * function's matching _finish() operation.
108 * Some #GFile operations do not have synchronous analogs, as they may
109 * take a very long time to finish, and blocking may leave an application
110 * unusable. Notable cases include:
111 * g_file_mount_mountable() to mount a mountable file.
112 * g_file_unmount_mountable() to unmount a mountable file.
113 * g_file_eject_mountable() to eject a mountable file.
115 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
116 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
117 * short. Entity tags are somewhat like a more abstract version of the
118 * traditional mtime, and can be used to quickly determine if the file has
119 * been modified from the version on the file system. See the HTTP 1.1
120 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
121 * for HTTP Etag headers, which are a very similar concept.
125 static void g_file_base_init (gpointer g_class);
126 static void g_file_class_init (gpointer g_class,
127 gpointer class_data);
129 static void g_file_real_query_info_async (GFile *file,
130 const char *attributes,
131 GFileQueryInfoFlags flags,
133 GCancellable *cancellable,
134 GAsyncReadyCallback callback,
136 static GFileInfo * g_file_real_query_info_finish (GFile *file,
139 static void g_file_real_query_filesystem_info_async (GFile *file,
140 const char *attributes,
142 GCancellable *cancellable,
143 GAsyncReadyCallback callback,
145 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
148 static void g_file_real_enumerate_children_async (GFile *file,
149 const char *attributes,
150 GFileQueryInfoFlags flags,
152 GCancellable *cancellable,
153 GAsyncReadyCallback callback,
155 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
158 static void g_file_real_read_async (GFile *file,
160 GCancellable *cancellable,
161 GAsyncReadyCallback callback,
163 static GFileInputStream * g_file_real_read_finish (GFile *file,
166 static void g_file_real_append_to_async (GFile *file,
167 GFileCreateFlags flags,
169 GCancellable *cancellable,
170 GAsyncReadyCallback callback,
172 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
175 static void g_file_real_create_async (GFile *file,
176 GFileCreateFlags flags,
178 GCancellable *cancellable,
179 GAsyncReadyCallback callback,
181 static GFileOutputStream *g_file_real_create_finish (GFile *file,
184 static void g_file_real_replace_async (GFile *file,
186 gboolean make_backup,
187 GFileCreateFlags flags,
189 GCancellable *cancellable,
190 GAsyncReadyCallback callback,
192 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
195 static gboolean g_file_real_set_attributes_from_info (GFile *file,
197 GFileQueryInfoFlags flags,
198 GCancellable *cancellable,
200 static void g_file_real_set_display_name_async (GFile *file,
201 const char *display_name,
203 GCancellable *cancellable,
204 GAsyncReadyCallback callback,
206 static GFile * g_file_real_set_display_name_finish (GFile *file,
209 static void g_file_real_set_attributes_async (GFile *file,
211 GFileQueryInfoFlags flags,
213 GCancellable *cancellable,
214 GAsyncReadyCallback callback,
216 static gboolean g_file_real_set_attributes_finish (GFile *file,
220 static void g_file_real_find_enclosing_mount_async (GFile *file,
222 GCancellable *cancellable,
223 GAsyncReadyCallback callback,
225 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
228 static void g_file_real_copy_async (GFile *source,
230 GFileCopyFlags flags,
232 GCancellable *cancellable,
233 GFileProgressCallback progress_callback,
234 gpointer progress_callback_data,
235 GAsyncReadyCallback callback,
237 static gboolean g_file_real_copy_finish (GFile *file,
242 g_file_get_type (void)
244 static GType file_type = 0;
248 static const GTypeInfo file_info =
250 sizeof (GFileIface), /* class_size */
251 g_file_base_init, /* base_init */
252 NULL, /* base_finalize */
254 NULL, /* class_finalize */
255 NULL, /* class_data */
262 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
265 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
272 g_file_class_init (gpointer g_class,
275 GFileIface *iface = g_class;
277 iface->enumerate_children_async = g_file_real_enumerate_children_async;
278 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
279 iface->set_display_name_async = g_file_real_set_display_name_async;
280 iface->set_display_name_finish = g_file_real_set_display_name_finish;
281 iface->query_info_async = g_file_real_query_info_async;
282 iface->query_info_finish = g_file_real_query_info_finish;
283 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
284 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
285 iface->set_attributes_async = g_file_real_set_attributes_async;
286 iface->set_attributes_finish = g_file_real_set_attributes_finish;
287 iface->read_async = g_file_real_read_async;
288 iface->read_finish = g_file_real_read_finish;
289 iface->append_to_async = g_file_real_append_to_async;
290 iface->append_to_finish = g_file_real_append_to_finish;
291 iface->create_async = g_file_real_create_async;
292 iface->create_finish = g_file_real_create_finish;
293 iface->replace_async = g_file_real_replace_async;
294 iface->replace_finish = g_file_real_replace_finish;
295 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
296 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
297 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
298 iface->copy_async = g_file_real_copy_async;
299 iface->copy_finish = g_file_real_copy_finish;
303 g_file_base_init (gpointer g_class)
310 * @file: input #GFile.
312 * Checks to see if a file is native to the platform.
314 * A native file s one expressed in the platform-native filename format,
315 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
316 * as it might be on a locally mounted remote filesystem.
318 * On some systems non-native files may be available using
319 * the native filesystem via a userspace filesystem (FUSE), in
320 * these cases this call will return %FALSE, but g_file_get_path()
321 * will still return a native path.
323 * This call does no blocking i/o.
325 * Returns: %TRUE if file is native.
328 g_file_is_native (GFile *file)
332 g_return_val_if_fail (G_IS_FILE (file), FALSE);
334 iface = G_FILE_GET_IFACE (file);
336 return (* iface->is_native) (file);
341 * g_file_has_uri_scheme:
342 * @file: input #GFile.
343 * @uri_scheme: a string containing a URI scheme.
345 * Checks to see if a #GFile has a given URI scheme.
347 * This call does no blocking i/o.
349 * Returns: %TRUE if #GFile's backend supports the
350 * given URI scheme, %FALSE if URI scheme is %NULL,
351 * not supported, or #GFile is invalid.
354 g_file_has_uri_scheme (GFile *file,
355 const char *uri_scheme)
359 g_return_val_if_fail (G_IS_FILE (file), FALSE);
360 g_return_val_if_fail (uri_scheme != NULL, FALSE);
362 iface = G_FILE_GET_IFACE (file);
364 return (* iface->has_uri_scheme) (file, uri_scheme);
369 * g_file_get_uri_scheme:
370 * @file: input #GFile.
372 * Gets the URI scheme for a #GFile.
373 * RFC 3986 decodes the scheme as:
375 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
377 * Common schemes include "file", "http", "ftp", etc.
379 * This call does no blocking i/o.
381 * Returns: a string containing the URI scheme for the given
382 * #GFile. The returned string should be freed with g_free()
383 * when no longer needed.
386 g_file_get_uri_scheme (GFile *file)
390 g_return_val_if_fail (G_IS_FILE (file), NULL);
392 iface = G_FILE_GET_IFACE (file);
394 return (* iface->get_uri_scheme) (file);
399 * g_file_get_basename:
400 * @file: input #GFile.
402 * Gets the base name (the last component of the path) for a given #GFile.
404 * If called for the top level of a system (such as the filesystem root
405 * or a uri like sftp://host/) it will return a single directory separator
406 * (and on Windows, possibly a drive letter).
408 * The base name is a byte string (*not* UTF-8). It has no defined encoding
409 * or rules other than it may not contain zero bytes. If you want to use
410 * filenames in a user interface you should use the display name that you
411 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
412 * attribute with g_file_query_info().
414 * This call does no blocking i/o.
416 * Returns: string containing the #GFile's base name, or %NULL
417 * if given #GFile is invalid. The returned string should be
418 * freed with g_free() when no longer needed.
421 g_file_get_basename (GFile *file)
425 g_return_val_if_fail (G_IS_FILE (file), NULL);
427 iface = G_FILE_GET_IFACE (file);
429 return (* iface->get_basename) (file);
434 * @file: input #GFile.
436 * Gets the local pathname for #GFile, if one exists.
438 * This call does no blocking i/o.
440 * Returns: string containing the #GFile's path, or %NULL if
441 * no such path exists. The returned string should be
442 * freed with g_free() when no longer needed.
445 g_file_get_path (GFile *file)
449 g_return_val_if_fail (G_IS_FILE (file), NULL);
451 iface = G_FILE_GET_IFACE (file);
453 return (* iface->get_path) (file);
458 * @file: input #GFile.
460 * Gets the URI for the @file.
462 * This call does no blocking i/o.
464 * Returns: a string containing the #GFile's URI.
465 * The returned string should be freed with g_free() when no longer needed.
468 g_file_get_uri (GFile *file)
472 g_return_val_if_fail (G_IS_FILE (file), NULL);
474 iface = G_FILE_GET_IFACE (file);
476 return (* iface->get_uri) (file);
480 * g_file_get_parse_name:
481 * @file: input #GFile.
483 * Gets the parse name of the @file.
484 * A parse name is a UTF-8 string that describes the
485 * file such that one can get the #GFile back using
486 * g_file_parse_name().
488 * This is generally used to show the #GFile as a nice
489 * full-pathname kind of string in a user interface,
490 * like in a location entry.
492 * For local files with names that can safely be converted
493 * to UTF8 the pathname is used, otherwise the IRI is used
494 * (a form of URI that allows UTF8 characters unescaped).
496 * This call does no blocking i/o.
498 * Returns: a string containing the #GFile's parse name. The returned
499 * string should be freed with g_free() when no longer needed.
502 g_file_get_parse_name (GFile *file)
506 g_return_val_if_fail (G_IS_FILE (file), NULL);
508 iface = G_FILE_GET_IFACE (file);
510 return (* iface->get_parse_name) (file);
515 * @file: input #GFile.
517 * Duplicates a #GFile handle. This operation does not duplicate
518 * the actual file or directory represented by the #GFile; see
519 * g_file_copy() if attempting to copy a file.
521 * This call does no blocking i/o.
523 * Returns: #GFile that is a duplicate of the given #GFile.
526 g_file_dup (GFile *file)
530 g_return_val_if_fail (G_IS_FILE (file), NULL);
532 iface = G_FILE_GET_IFACE (file);
534 return (* iface->dup) (file);
539 * @file: #gconstpointer to a #GFile.
541 * Creates a hash value for a #GFile.
543 * This call does no blocking i/o.
545 * Returns: 0 if @file is not a valid #GFile, otherwise an
546 * integer that can be used as hash value for the #GFile.
547 * This function is intended for easily hashing a #GFile to
548 * add to a #GHashTable or similar data structure.
551 g_file_hash (gconstpointer file)
555 g_return_val_if_fail (G_IS_FILE (file), 0);
557 iface = G_FILE_GET_IFACE (file);
559 return (* iface->hash) ((GFile *)file);
564 * @file1: the first #GFile.
565 * @file2: the second #GFile.
567 * Checks equality of two given #GFile<!-- -->s. Note that two
568 * #GFile<!-- -->s that differ can still refer to the same
569 * file on the filesystem due to various forms of filename
572 * This call does no blocking i/o.
574 * Returns: %TRUE if @file1 and @file2 are equal.
575 * %FALSE if either is not a #GFile.
578 g_file_equal (GFile *file1,
583 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
584 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
586 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
589 iface = G_FILE_GET_IFACE (file1);
591 return (* iface->equal) (file1, file2);
597 * @file: input #GFile.
599 * Gets the parent directory for the @file.
600 * If the @file represents the root directory of the
601 * file system, then %NULL will be returned.
603 * This call does no blocking i/o.
605 * Returns: a #GFile structure to the parent of the given
606 * #GFile or %NULL if there is no parent.
607 * Free the returned object with g_object_unref().
610 g_file_get_parent (GFile *file)
614 g_return_val_if_fail (G_IS_FILE (file), NULL);
616 iface = G_FILE_GET_IFACE (file);
618 return (* iface->get_parent) (file);
623 * @file: input #GFile.
624 * @name: string containing the child's basename.
626 * Gets a child of @file with basename equal to @name.
628 * Note that the file with that specific name might not exist, but
629 * you can still have a #GFile that points to it. You can use this
630 * for instance to create that file.
632 * This call does no blocking i/o.
634 * Returns: a #GFile to a child specified by @name.
635 * Free the returned object with g_object_unref().
638 g_file_get_child (GFile *file,
641 g_return_val_if_fail (G_IS_FILE (file), NULL);
642 g_return_val_if_fail (name != NULL, NULL);
644 return g_file_resolve_relative_path (file, name);
648 * g_file_get_child_for_display_name:
649 * @file: input #GFile.
650 * @display_name: string to a possible child.
653 * Gets the child of @file for a given @display_name (i.e. a UTF8
654 * version of the name). If this function fails, it returns %NULL and @error will be
655 * set. This is very useful when constructing a GFile for a new file
656 * and the user entered the filename in the user interface, for instance
657 * when you select a directory and type a filename in the file selector.
659 * This call does no blocking i/o.
661 * Returns: a #GFile to the specified child, or
662 * %NULL if the display name couldn't be converted.
663 * Free the returned object with g_object_unref().
666 g_file_get_child_for_display_name (GFile *file,
667 const char *display_name,
672 g_return_val_if_fail (G_IS_FILE (file), NULL);
673 g_return_val_if_fail (display_name != NULL, NULL);
675 iface = G_FILE_GET_IFACE (file);
677 return (* iface->get_child_for_display_name) (file, display_name, error);
682 * @file: input #GFile.
683 * @prefix: input #GFile.
685 * Checks whether @file has the prefix specified by @prefix. In other word,
686 * if the names of inital elements of @file<!-- -->s pathname match @prefix.
688 * This call does no i/o, as it works purely on names. As such it can
689 * sometimes return %FALSE even if @file is inside a @prefix (from a
690 * filesystem point of view), because the prefix of @file is an alias
693 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix.
697 g_file_has_prefix (GFile *file,
702 g_return_val_if_fail (G_IS_FILE (file), FALSE);
703 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
705 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
708 iface = G_FILE_GET_IFACE (file);
710 /* The vtable function differs in arg order since we're
711 using the old contains_file call */
712 return (* iface->prefix_matches) (prefix, file);
716 * g_file_get_relative_path:
717 * @parent: input #GFile.
718 * @descendant: input #GFile.
720 * Gets the path for @descendant relative to @parent.
722 * This call does no blocking i/o.
724 * Returns: string with the relative path from @descendant
725 * to @parent, or %NULL if @descendant doesn't have @parent as prefix.
726 * The returned string should be freed with g_free() when no longer needed.
729 g_file_get_relative_path (GFile *parent,
734 g_return_val_if_fail (G_IS_FILE (parent), NULL);
735 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
737 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
740 iface = G_FILE_GET_IFACE (parent);
742 return (* iface->get_relative_path) (parent, descendant);
746 * g_file_resolve_relative_path:
747 * @file: input #GFile.
748 * @relative_path: a given relative path string.
750 * Resolves a relative path for @file to an absolute path.
752 * This call does no blocking i/o.
754 * Returns: #GFile to the resolved path. %NULL if @relative_path
755 * is %NULL or if @file is invalid.
756 * Free the returned object with g_object_unref().
759 g_file_resolve_relative_path (GFile *file,
760 const char *relative_path)
764 g_return_val_if_fail (G_IS_FILE (file), NULL);
765 g_return_val_if_fail (relative_path != NULL, NULL);
767 iface = G_FILE_GET_IFACE (file);
769 return (* iface->resolve_relative_path) (file, relative_path);
773 * g_file_enumerate_children:
774 * @file: input #GFile.
775 * @attributes: an attribute query string.
776 * @flags: a set of #GFileQueryInfoFlags.
777 * @cancellable: optional #GCancellable object, %NULL to ignore.
778 * @error: #GError for error reporting.
780 * Gets the requested information about the files in a directory. The result
781 * is a #GFileEnumerator object that will give out #GFileInfo objects for
782 * all the files in the directory.
784 * The @attribute value is a string that specifies the file attributes that
785 * should be gathered. It is not an error if it's not possible to read a particular
786 * requested attribute from a file - it just won't be set. @attribute should
787 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
788 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
789 * namespace. An example attribute query be "standard::*,owner::user".
790 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
792 * If @cancellable is not %NULL, then the operation can be cancelled by
793 * triggering the cancellable object from another thread. If the operation
794 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
796 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
797 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
798 * Other errors are possible too.
800 * Returns: A #GFileEnumerator if successful, %NULL on error.
803 g_file_enumerate_children (GFile *file,
804 const char *attributes,
805 GFileQueryInfoFlags flags,
806 GCancellable *cancellable,
812 g_return_val_if_fail (G_IS_FILE (file), NULL);
814 if (g_cancellable_set_error_if_cancelled (cancellable, error))
817 iface = G_FILE_GET_IFACE (file);
819 if (iface->enumerate_children == NULL)
821 g_set_error_literal (error, G_IO_ERROR,
822 G_IO_ERROR_NOT_SUPPORTED,
823 _("Operation not supported"));
827 return (* iface->enumerate_children) (file, attributes, flags,
832 * g_file_enumerate_children_async:
833 * @file: input #GFile.
834 * @attributes: an attribute query string.
835 * @flags: a set of #GFileQueryInfoFlags.
836 * @io_priority: the <link linkend="io-priority">I/O priority</link>
838 * @cancellable: optional #GCancellable object, %NULL to ignore.
839 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
840 * @user_data: the data to pass to callback function
842 * Asynchronously gets the requested information about the files in a directory. The result
843 * is a #GFileEnumerator object that will give out #GFileInfo objects for
844 * all the files in the directory.
846 * For more details, see g_file_enumerate_children() which is
847 * the synchronous version of this call.
849 * When the operation is finished, @callback will be called. You can then call
850 * g_file_enumerate_children_finish() to get the result of the operation.
853 g_file_enumerate_children_async (GFile *file,
854 const char *attributes,
855 GFileQueryInfoFlags flags,
857 GCancellable *cancellable,
858 GAsyncReadyCallback callback,
863 g_return_if_fail (G_IS_FILE (file));
865 iface = G_FILE_GET_IFACE (file);
866 (* iface->enumerate_children_async) (file,
876 * g_file_enumerate_children_finish:
877 * @file: input #GFile.
878 * @res: a #GAsyncResult.
881 * Finishes an async enumerate children operation.
882 * See g_file_enumerate_children_async().
884 * Returns: a #GFileEnumerator or %NULL if an error occurred.
887 g_file_enumerate_children_finish (GFile *file,
893 g_return_val_if_fail (G_IS_FILE (file), NULL);
894 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
896 if (G_IS_SIMPLE_ASYNC_RESULT (res))
898 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
899 if (g_simple_async_result_propagate_error (simple, error))
903 iface = G_FILE_GET_IFACE (file);
904 return (* iface->enumerate_children_finish) (file, res, error);
908 * g_file_query_exists:
909 * @file: input #GFile.
910 * @cancellable: optional #GCancellable object, %NULL to ignore.
912 * Utility function to check if a particular file exists. This is
913 * implemented using g_file_query_info() and as such does blocking I/O.
915 * Note that in many cases it is racy to first check for file existence
916 * and then execute something based on the outcome of that, because the
917 * file might have been created or removed in between the operations. The
918 * general approach to handling that is to not check, but just do the
919 * operation and handle the errors as they come.
921 * As an example of race-free checking, take the case of reading a file, and
922 * if it doesn't exist, creating it. There are two racy versions: read it, and
923 * on error create it; and: check if it exists, if not create it. These
924 * can both result in two processes creating the file (with perhaps a partially
925 * written file as the result). The correct approach is to always try to create
926 * the file with g_file_create() which will either atomically create the file
927 * or fail with a G_IO_ERROR_EXISTS error.
929 * However, in many cases an existence check is useful in a user
930 * interface, for instance to make a menu item sensitive/insensitive, so that
931 * you don't have to fool users that something is possible and then just show
932 * and error dialog. If you do this, you should make sure to also handle the
933 * errors that can happen due to races when you execute the operation.
935 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
938 g_file_query_exists (GFile *file,
939 GCancellable *cancellable)
943 g_return_val_if_fail (G_IS_FILE(file), FALSE);
945 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
946 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
949 g_object_unref (info);
957 * g_file_query_file_type:
958 * @file: input #GFile.
959 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
960 * @cancellable: optional #GCancellable object, %NULL to ignore.
962 * Utility function to inspect the #GFileType of a file. This is
963 * implemented using g_file_query_info() and as such does blocking I/O.
965 * The primary use case of this method is to check if a file is a regular file,
966 * directory, or symlink.
968 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
974 g_file_query_file_type (GFile *file,
975 GFileQueryInfoFlags flags,
976 GCancellable *cancellable)
981 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
982 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
986 file_type = g_file_info_get_file_type (info);
987 g_object_unref (info);
990 file_type = G_FILE_TYPE_UNKNOWN;
997 * @file: input #GFile.
998 * @attributes: an attribute query string.
999 * @flags: a set of #GFileQueryInfoFlags.
1000 * @cancellable: optional #GCancellable object, %NULL to ignore.
1001 * @error: a #GError.
1003 * Gets the requested information about specified @file. The result
1004 * is a #GFileInfo object that contains key-value attributes (such as
1005 * the type or size of the file).
1007 * The @attribute value is a string that specifies the file attributes that
1008 * should be gathered. It is not an error if it's not possible to read a particular
1009 * requested attribute from a file - it just won't be set. @attribute should
1010 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1011 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1012 * namespace. An example attribute query be "standard::*,owner::user".
1013 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1015 * If @cancellable is not %NULL, then the operation can be cancelled by
1016 * triggering the cancellable object from another thread. If the operation
1017 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1019 * For symlinks, normally the information about the target of the
1020 * symlink is returned, rather than information about the symlink itself.
1021 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1022 * information about the symlink itself will be returned. Also, for symlinks
1023 * that point to non-existing files the information about the symlink itself
1026 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1027 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1029 * Returns: a #GFileInfo for the given @file, or %NULL on error.
1032 g_file_query_info (GFile *file,
1033 const char *attributes,
1034 GFileQueryInfoFlags flags,
1035 GCancellable *cancellable,
1040 g_return_val_if_fail (G_IS_FILE (file), NULL);
1042 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1045 iface = G_FILE_GET_IFACE (file);
1047 if (iface->query_info == NULL)
1049 g_set_error_literal (error, G_IO_ERROR,
1050 G_IO_ERROR_NOT_SUPPORTED,
1051 _("Operation not supported"));
1055 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1059 * g_file_query_info_async:
1060 * @file: input #GFile.
1061 * @attributes: an attribute query string.
1062 * @flags: a set of #GFileQueryInfoFlags.
1063 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1065 * @cancellable: optional #GCancellable object, %NULL to ignore.
1066 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1067 * @user_data: the data to pass to callback function
1069 * Asynchronously gets the requested information about specified @file. The result
1070 * is a #GFileInfo object that contains key-value attributes (such as type or size
1073 * For more details, see g_file_query_info() which is
1074 * the synchronous version of this call.
1076 * When the operation is finished, @callback will be called. You can then call
1077 * g_file_query_info_finish() to get the result of the operation.
1080 g_file_query_info_async (GFile *file,
1081 const char *attributes,
1082 GFileQueryInfoFlags flags,
1084 GCancellable *cancellable,
1085 GAsyncReadyCallback callback,
1090 g_return_if_fail (G_IS_FILE (file));
1092 iface = G_FILE_GET_IFACE (file);
1093 (* iface->query_info_async) (file,
1103 * g_file_query_info_finish:
1104 * @file: input #GFile.
1105 * @res: a #GAsyncResult.
1106 * @error: a #GError.
1108 * Finishes an asynchronous file info query.
1109 * See g_file_query_info_async().
1111 * Returns: #GFileInfo for given @file or %NULL on error.
1114 g_file_query_info_finish (GFile *file,
1120 g_return_val_if_fail (G_IS_FILE (file), NULL);
1121 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1123 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1125 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1126 if (g_simple_async_result_propagate_error (simple, error))
1130 iface = G_FILE_GET_IFACE (file);
1131 return (* iface->query_info_finish) (file, res, error);
1135 * g_file_query_filesystem_info:
1136 * @file: input #GFile.
1137 * @attributes: an attribute query string.
1138 * @cancellable: optional #GCancellable object, %NULL to ignore.
1139 * @error: a #GError.
1141 * Similar to g_file_query_info(), but obtains information
1142 * about the filesystem the @file is on, rather than the file itself.
1143 * For instance the amount of space available and the type of
1146 * The @attribute value is a string that specifies the file attributes that
1147 * should be gathered. It is not an error if it's not possible to read a particular
1148 * requested attribute from a file - it just won't be set. @attribute should
1149 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1150 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1151 * namespace. The standard namespace for filesystem attributes is "fs".
1152 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1153 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1154 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1156 * If @cancellable is not %NULL, then the operation can be cancelled by
1157 * triggering the cancellable object from another thread. If the operation
1158 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1160 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1161 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1163 * Returns: a #GFileInfo or %NULL if there was an error.
1166 g_file_query_filesystem_info (GFile *file,
1167 const char *attributes,
1168 GCancellable *cancellable,
1173 g_return_val_if_fail (G_IS_FILE (file), NULL);
1175 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1178 iface = G_FILE_GET_IFACE (file);
1180 if (iface->query_filesystem_info == NULL)
1182 g_set_error_literal (error, G_IO_ERROR,
1183 G_IO_ERROR_NOT_SUPPORTED,
1184 _("Operation not supported"));
1188 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1192 * g_file_query_filesystem_info_async:
1193 * @file: input #GFile.
1194 * @attributes: an attribute query string.
1195 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1197 * @cancellable: optional #GCancellable object, %NULL to ignore.
1198 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1199 * @user_data: the data to pass to callback function
1201 * Asynchronously gets the requested information about the filesystem
1202 * that the specified @file is on. The result is a #GFileInfo object
1203 * that contains key-value attributes (such as type or size for the
1206 * For more details, see g_file_query_filesystem_info() which is the
1207 * synchronous version of this call.
1209 * When the operation is finished, @callback will be called. You can
1210 * then call g_file_query_info_finish() to get the result of the
1214 g_file_query_filesystem_info_async (GFile *file,
1215 const char *attributes,
1217 GCancellable *cancellable,
1218 GAsyncReadyCallback callback,
1223 g_return_if_fail (G_IS_FILE (file));
1225 iface = G_FILE_GET_IFACE (file);
1226 (* iface->query_filesystem_info_async) (file,
1235 * g_file_query_filesystem_info_finish:
1236 * @file: input #GFile.
1237 * @res: a #GAsyncResult.
1238 * @error: a #GError.
1240 * Finishes an asynchronous filesystem info query. See
1241 * g_file_query_filesystem_info_async().
1243 * Returns: #GFileInfo for given @file or %NULL on error.
1246 g_file_query_filesystem_info_finish (GFile *file,
1252 g_return_val_if_fail (G_IS_FILE (file), NULL);
1253 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1255 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1257 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1258 if (g_simple_async_result_propagate_error (simple, error))
1262 iface = G_FILE_GET_IFACE (file);
1263 return (* iface->query_filesystem_info_finish) (file, res, error);
1267 * g_file_find_enclosing_mount:
1268 * @file: input #GFile.
1269 * @cancellable: optional #GCancellable object, %NULL to ignore.
1270 * @error: a #GError.
1272 * Gets a #GMount for the #GFile.
1274 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1275 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1278 * If @cancellable is not %NULL, then the operation can be cancelled by
1279 * triggering the cancellable object from another thread. If the operation
1280 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1282 * Returns: a #GMount where the @file is located or %NULL on error.
1285 g_file_find_enclosing_mount (GFile *file,
1286 GCancellable *cancellable,
1291 g_return_val_if_fail (G_IS_FILE (file), NULL);
1293 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1296 iface = G_FILE_GET_IFACE (file);
1297 if (iface->find_enclosing_mount == NULL)
1300 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1301 /* Translators: This is an error message when trying to find the
1302 * enclosing (user visible) mount of a file, but none exists. */
1303 _("Containing mount does not exist"));
1307 return (* iface->find_enclosing_mount) (file, cancellable, error);
1310 * g_file_find_enclosing_mount_async:
1312 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1314 * @cancellable: optional #GCancellable object, %NULL to ignore.
1315 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1316 * @user_data: the data to pass to callback function
1318 * Asynchronously gets the mount for the file.
1320 * For more details, see g_file_find_enclosing_mount() which is
1321 * the synchronous version of this call.
1323 * When the operation is finished, @callback will be called. You can then call
1324 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1327 g_file_find_enclosing_mount_async (GFile *file,
1329 GCancellable *cancellable,
1330 GAsyncReadyCallback callback,
1335 g_return_if_fail (G_IS_FILE (file));
1337 iface = G_FILE_GET_IFACE (file);
1338 (* iface->find_enclosing_mount_async) (file,
1346 * g_file_find_enclosing_mount_finish:
1348 * @res: a #GAsyncResult
1351 * Finishes an asynchronous find mount request.
1352 * See g_file_find_enclosing_mount_async().
1354 * Returns: #GMount for given @file or %NULL on error.
1357 g_file_find_enclosing_mount_finish (GFile *file,
1363 g_return_val_if_fail (G_IS_FILE (file), NULL);
1364 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1366 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1368 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1369 if (g_simple_async_result_propagate_error (simple, error))
1373 iface = G_FILE_GET_IFACE (file);
1374 return (* iface->find_enclosing_mount_finish) (file, res, error);
1380 * @file: #GFile to read.
1381 * @cancellable: a #GCancellable
1382 * @error: a #GError, or %NULL
1384 * Opens a file for reading. The result is a #GFileInputStream that
1385 * can be used to read the contents of the file.
1387 * If @cancellable is not %NULL, then the operation can be cancelled by
1388 * triggering the cancellable object from another thread. If the operation
1389 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1391 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1392 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1393 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1395 * Returns: #GFileInputStream or %NULL on error.
1398 g_file_read (GFile *file,
1399 GCancellable *cancellable,
1404 g_return_val_if_fail (G_IS_FILE (file), NULL);
1406 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1409 iface = G_FILE_GET_IFACE (file);
1411 if (iface->read_fn == NULL)
1413 g_set_error_literal (error, G_IO_ERROR,
1414 G_IO_ERROR_NOT_SUPPORTED,
1415 _("Operation not supported"));
1419 return (* iface->read_fn) (file, cancellable, error);
1424 * @file: input #GFile.
1425 * @flags: a set of #GFileCreateFlags.
1426 * @cancellable: optional #GCancellable object, %NULL to ignore.
1427 * @error: a #GError, or %NULL
1429 * Gets an output stream for appending data to the file. If
1430 * the file doesn't already exist it is created.
1432 * By default files created are generally readable by everyone,
1433 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1434 * will be made readable only to the current user, to the level that
1435 * is supported on the target filesystem.
1437 * If @cancellable is not %NULL, then the operation can be cancelled by
1438 * triggering the cancellable object from another thread. If the operation
1439 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1441 * Some file systems don't allow all file names, and may
1442 * return an %G_IO_ERROR_INVALID_FILENAME error.
1443 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1444 * returned. Other errors are possible too, and depend on what kind of
1445 * filesystem the file is on.
1447 * Returns: a #GFileOutputStream, or %NULL on error
1450 g_file_append_to (GFile *file,
1451 GFileCreateFlags flags,
1452 GCancellable *cancellable,
1457 g_return_val_if_fail (G_IS_FILE (file), NULL);
1459 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1462 iface = G_FILE_GET_IFACE (file);
1464 if (iface->append_to == NULL)
1466 g_set_error_literal (error, G_IO_ERROR,
1467 G_IO_ERROR_NOT_SUPPORTED,
1468 _("Operation not supported"));
1472 return (* iface->append_to) (file, flags, cancellable, error);
1477 * @file: input #GFile.
1478 * @flags: a set of #GFileCreateFlags.
1479 * @cancellable: optional #GCancellable object, %NULL to ignore.
1480 * @error: a #GError, or %NULL
1482 * Creates a new file and returns an output stream for writing to it.
1483 * The file must not already exist.
1485 * By default files created are generally readable by everyone,
1486 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1487 * will be made readable only to the current user, to the level that
1488 * is supported on the target filesystem.
1490 * If @cancellable is not %NULL, then the operation can be cancelled by
1491 * triggering the cancellable object from another thread. If the operation
1492 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1494 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1495 * error will be returned.
1496 * Some file systems don't allow all file names, and may
1497 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1498 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1499 * Other errors are possible too, and depend on what kind of
1500 * filesystem the file is on.
1502 * Returns: a #GFileOutputStream for the newly created file, or
1506 g_file_create (GFile *file,
1507 GFileCreateFlags flags,
1508 GCancellable *cancellable,
1513 g_return_val_if_fail (G_IS_FILE (file), NULL);
1515 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1518 iface = G_FILE_GET_IFACE (file);
1520 if (iface->create == NULL)
1522 g_set_error_literal (error, G_IO_ERROR,
1523 G_IO_ERROR_NOT_SUPPORTED,
1524 _("Operation not supported"));
1528 return (* iface->create) (file, flags, cancellable, error);
1533 * @file: input #GFile.
1534 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1535 * current #GFile, or #NULL to ignore.
1536 * @make_backup: %TRUE if a backup should be created.
1537 * @flags: a set of #GFileCreateFlags.
1538 * @cancellable: optional #GCancellable object, %NULL to ignore.
1539 * @error: a #GError, or %NULL
1541 * Returns an output stream for overwriting the file, possibly
1542 * creating a backup copy of the file first. If the file doesn't exist,
1543 * it will be created.
1545 * This will try to replace the file in the safest way possible so
1546 * that any errors during the writing will not affect an already
1547 * existing copy of the file. For instance, for local files it
1548 * may write to a temporary file and then atomically rename over
1549 * the destination when the stream is closed.
1551 * By default files created are generally readable by everyone,
1552 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1553 * will be made readable only to the current user, to the level that
1554 * is supported on the target filesystem.
1556 * If @cancellable is not %NULL, then the operation can be cancelled by
1557 * triggering the cancellable object from another thread. If the operation
1558 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1560 * If you pass in a non-#NULL @etag value, then this value is
1561 * compared to the current entity tag of the file, and if they differ
1562 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1563 * that the file has been changed since you last read it. You can get
1564 * the new etag from g_file_output_stream_get_etag() after you've
1565 * finished writing and closed the #GFileOutputStream. When you load
1566 * a new file you can use g_file_input_stream_query_info() to get
1567 * the etag of the file.
1569 * If @make_backup is %TRUE, this function will attempt to make a backup
1570 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1571 * error will be returned. If you want to replace anyway, try again with
1572 * @make_backup set to %FALSE.
1574 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1575 * and if the file is some other form of non-regular file then a
1576 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1577 * Some file systems don't allow all file names, and may
1578 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1579 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1580 * Other errors are possible too, and depend on what kind of
1581 * filesystem the file is on.
1583 * Returns: a #GFileOutputStream or %NULL on error.
1586 g_file_replace (GFile *file,
1588 gboolean make_backup,
1589 GFileCreateFlags flags,
1590 GCancellable *cancellable,
1595 g_return_val_if_fail (G_IS_FILE (file), NULL);
1597 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1600 iface = G_FILE_GET_IFACE (file);
1602 if (iface->replace == NULL)
1604 g_set_error_literal (error, G_IO_ERROR,
1605 G_IO_ERROR_NOT_SUPPORTED,
1606 _("Operation not supported"));
1611 /* Handle empty tag string as NULL in consistent way. */
1612 if (etag && *etag == 0)
1615 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1619 * g_file_read_async:
1620 * @file: input #GFile.
1621 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1623 * @cancellable: optional #GCancellable object, %NULL to ignore.
1624 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1625 * @user_data: the data to pass to callback function
1627 * Asynchronously opens @file for reading.
1629 * For more details, see g_file_read() which is
1630 * the synchronous version of this call.
1632 * When the operation is finished, @callback will be called. You can then call
1633 * g_file_read_finish() to get the result of the operation.
1636 g_file_read_async (GFile *file,
1638 GCancellable *cancellable,
1639 GAsyncReadyCallback callback,
1644 g_return_if_fail (G_IS_FILE (file));
1646 iface = G_FILE_GET_IFACE (file);
1647 (* iface->read_async) (file,
1655 * g_file_read_finish:
1656 * @file: input #GFile.
1657 * @res: a #GAsyncResult.
1658 * @error: a #GError, or %NULL
1660 * Finishes an asynchronous file read operation started with
1661 * g_file_read_async().
1663 * Returns: a #GFileInputStream or %NULL on error.
1666 g_file_read_finish (GFile *file,
1672 g_return_val_if_fail (G_IS_FILE (file), NULL);
1673 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1675 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1677 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1678 if (g_simple_async_result_propagate_error (simple, error))
1682 iface = G_FILE_GET_IFACE (file);
1683 return (* iface->read_finish) (file, res, error);
1687 * g_file_append_to_async:
1688 * @file: input #GFile.
1689 * @flags: a set of #GFileCreateFlags.
1690 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1692 * @cancellable: optional #GCancellable object, %NULL to ignore.
1693 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1694 * @user_data: the data to pass to callback function
1696 * Asynchronously opens @file for appending.
1698 * For more details, see g_file_append_to() which is
1699 * the synchronous version of this call.
1701 * When the operation is finished, @callback will be called. You can then call
1702 * g_file_append_to_finish() to get the result of the operation.
1705 g_file_append_to_async (GFile *file,
1706 GFileCreateFlags flags,
1708 GCancellable *cancellable,
1709 GAsyncReadyCallback callback,
1714 g_return_if_fail (G_IS_FILE (file));
1716 iface = G_FILE_GET_IFACE (file);
1717 (* iface->append_to_async) (file,
1726 * g_file_append_to_finish:
1727 * @file: input #GFile.
1728 * @res: #GAsyncResult
1729 * @error: a #GError, or %NULL
1731 * Finishes an asynchronous file append operation started with
1732 * g_file_append_to_async().
1734 * Returns: a valid #GFileOutputStream or %NULL on error.
1737 g_file_append_to_finish (GFile *file,
1743 g_return_val_if_fail (G_IS_FILE (file), NULL);
1744 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1746 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1748 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1749 if (g_simple_async_result_propagate_error (simple, error))
1753 iface = G_FILE_GET_IFACE (file);
1754 return (* iface->append_to_finish) (file, res, error);
1758 * g_file_create_async:
1759 * @file: input #GFile.
1760 * @flags: a set of #GFileCreateFlags.
1761 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1763 * @cancellable: optional #GCancellable object, %NULL to ignore.
1764 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1765 * @user_data: the data to pass to callback function
1767 * Asynchronously creates a new file and returns an output stream for writing to it.
1768 * The file must not already exist.
1770 * For more details, see g_file_create() which is
1771 * the synchronous version of this call.
1773 * When the operation is finished, @callback will be called. You can then call
1774 * g_file_create_finish() to get the result of the operation.
1777 g_file_create_async (GFile *file,
1778 GFileCreateFlags flags,
1780 GCancellable *cancellable,
1781 GAsyncReadyCallback callback,
1786 g_return_if_fail (G_IS_FILE (file));
1788 iface = G_FILE_GET_IFACE (file);
1789 (* iface->create_async) (file,
1798 * g_file_create_finish:
1799 * @file: input #GFile.
1800 * @res: a #GAsyncResult.
1801 * @error: a #GError, or %NULL
1803 * Finishes an asynchronous file create operation started with
1804 * g_file_create_async().
1806 * Returns: a #GFileOutputStream or %NULL on error.
1809 g_file_create_finish (GFile *file,
1815 g_return_val_if_fail (G_IS_FILE (file), NULL);
1816 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1818 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1820 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1821 if (g_simple_async_result_propagate_error (simple, error))
1825 iface = G_FILE_GET_IFACE (file);
1826 return (* iface->create_finish) (file, res, error);
1830 * g_file_replace_async:
1831 * @file: input #GFile.
1832 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1833 * current #GFile, or NULL to ignore.
1834 * @make_backup: %TRUE if a backup should be created.
1835 * @flags: a set of #GFileCreateFlags.
1836 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1838 * @cancellable: optional #GCancellable object, %NULL to ignore.
1839 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1840 * @user_data: the data to pass to callback function
1842 * Asynchronously overwrites the file, replacing the contents, possibly
1843 * creating a backup copy of the file first.
1845 * For more details, see g_file_replace() which is
1846 * the synchronous version of this call.
1848 * When the operation is finished, @callback will be called. You can then call
1849 * g_file_replace_finish() to get the result of the operation.
1852 g_file_replace_async (GFile *file,
1854 gboolean make_backup,
1855 GFileCreateFlags flags,
1857 GCancellable *cancellable,
1858 GAsyncReadyCallback callback,
1863 g_return_if_fail (G_IS_FILE (file));
1865 iface = G_FILE_GET_IFACE (file);
1866 (* iface->replace_async) (file,
1877 * g_file_replace_finish:
1878 * @file: input #GFile.
1879 * @res: a #GAsyncResult.
1880 * @error: a #GError, or %NULL
1882 * Finishes an asynchronous file replace operation started with
1883 * g_file_replace_async().
1885 * Returns: a #GFileOutputStream, or %NULL on error.
1888 g_file_replace_finish (GFile *file,
1894 g_return_val_if_fail (G_IS_FILE (file), NULL);
1895 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1897 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1899 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1900 if (g_simple_async_result_propagate_error (simple, error))
1904 iface = G_FILE_GET_IFACE (file);
1905 return (* iface->replace_finish) (file, res, error);
1909 copy_symlink (GFile *destination,
1910 GFileCopyFlags flags,
1911 GCancellable *cancellable,
1916 gboolean tried_delete;
1918 GFileType file_type;
1920 tried_delete = FALSE;
1924 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1926 /* Maybe it already existed, and we want to overwrite? */
1927 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1928 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1930 g_error_free (my_error);
1933 /* Don't overwrite if the destination is a directory */
1934 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1935 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1936 cancellable, &my_error);
1939 file_type = g_file_info_get_file_type (info);
1940 g_object_unref (info);
1942 if (file_type == G_FILE_TYPE_DIRECTORY)
1944 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1945 _("Can't copy over directory"));
1950 if (!g_file_delete (destination, cancellable, error))
1953 tried_delete = TRUE;
1957 g_propagate_error (error, my_error);
1964 static GInputStream *
1965 open_source_for_copy (GFile *source,
1967 GFileCopyFlags flags,
1968 GCancellable *cancellable,
1974 GFileType file_type;
1977 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1981 /* There was an error opening the source, try to set a good error for it: */
1983 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1985 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1986 * as that is less useful to the app. Better check for errors on the
1989 g_error_free (my_error);
1992 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1993 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1994 cancellable, &my_error);
1997 file_type = g_file_info_get_file_type (info);
1998 g_object_unref (info);
2000 if (flags & G_FILE_COPY_OVERWRITE)
2002 if (file_type == G_FILE_TYPE_DIRECTORY)
2004 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2005 _("Can't copy directory over directory"));
2008 /* continue to would_recurse error */
2012 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2013 _("Target file exists"));
2019 /* Error getting info from target, return that error
2020 * (except for NOT_FOUND, which is no error here)
2022 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
2024 g_propagate_error (error, my_error);
2027 g_error_free (my_error);
2030 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2031 _("Can't recursively copy directory"));
2035 g_propagate_error (error, my_error);
2040 should_copy (GFileAttributeInfo *info,
2044 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2045 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2049 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2050 GFileAttributeInfoList *namespaces,
2058 s = g_string_new ("");
2062 for (i = 0; i < attributes->n_infos; i++)
2064 if (should_copy (&attributes->infos[i], as_move))
2069 g_string_append_c (s, ',');
2071 g_string_append (s, attributes->infos[i].name);
2078 for (i = 0; i < namespaces->n_infos; i++)
2080 if (should_copy (&namespaces->infos[i], as_move))
2085 g_string_append_c (s, ',');
2087 g_string_append (s, namespaces->infos[i].name);
2088 g_string_append (s, ":*");
2093 return g_string_free (s, FALSE);
2097 * g_file_copy_attributes:
2098 * @source: a #GFile with attributes.
2099 * @destination: a #GFile to copy attributes to.
2100 * @flags: a set of #GFileCopyFlags.
2101 * @cancellable: optional #GCancellable object, %NULL to ignore.
2102 * @error: a #GError, %NULL to ignore.
2104 * Copies the file attributes from @source to @destination.
2106 * Normally only a subset of the file attributes are copied,
2107 * those that are copies in a normal file copy operation
2108 * (which for instance does not include e.g. mtime). However
2109 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2110 * all the metadata that is possible to copy is copied.
2112 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2115 g_file_copy_attributes (GFile *source,
2117 GFileCopyFlags flags,
2118 GCancellable *cancellable,
2121 GFileAttributeInfoList *attributes, *namespaces;
2122 char *attrs_to_read;
2126 gboolean source_nofollow_symlinks;
2128 as_move = flags & G_FILE_COPY_ALL_METADATA;
2129 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2131 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2132 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2133 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2135 if (attributes == NULL && namespaces == NULL)
2138 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2140 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2141 * we just don't copy it.
2143 info = g_file_query_info (source, attrs_to_read,
2144 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2148 g_free (attrs_to_read);
2153 res = g_file_set_attributes_from_info (destination,
2157 g_object_unref (info);
2160 g_file_attribute_info_list_unref (attributes);
2161 g_file_attribute_info_list_unref (namespaces);
2166 /* Closes the streams */
2168 copy_stream_with_progress (GInputStream *in,
2171 GCancellable *cancellable,
2172 GFileProgressCallback progress_callback,
2173 gpointer progress_callback_data,
2176 gssize n_read, n_written;
2177 goffset current_size;
2178 char buffer[1024*64], *p;
2184 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2185 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2189 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2190 total_size = g_file_info_get_size (info);
2191 g_object_unref (info);
2194 if (total_size == -1)
2196 info = g_file_query_info (source,
2197 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2198 G_FILE_QUERY_INFO_NONE,
2202 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2203 total_size = g_file_info_get_size (info);
2204 g_object_unref (info);
2208 if (total_size == -1)
2215 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2225 current_size += n_read;
2230 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2231 if (n_written == -1)
2238 n_read -= n_written;
2244 if (progress_callback)
2245 progress_callback (current_size, total_size, progress_callback_data);
2249 error = NULL; /* Ignore further errors */
2251 /* Make sure we send full copied size */
2252 if (progress_callback)
2253 progress_callback (current_size, total_size, progress_callback_data);
2255 /* Don't care about errors in source here */
2256 g_input_stream_close (in, cancellable, NULL);
2258 /* But write errors on close are bad! */
2259 if (!g_output_stream_close (out, cancellable, error))
2262 g_object_unref (in);
2263 g_object_unref (out);
2269 file_copy_fallback (GFile *source,
2271 GFileCopyFlags flags,
2272 GCancellable *cancellable,
2273 GFileProgressCallback progress_callback,
2274 gpointer progress_callback_data,
2282 /* Maybe copy the symlink? */
2283 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2285 info = g_file_query_info (source,
2286 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2287 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2293 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2294 (target = g_file_info_get_symlink_target (info)) != NULL)
2296 if (!copy_symlink (destination, flags, cancellable, target, error))
2298 g_object_unref (info);
2302 g_object_unref (info);
2306 g_object_unref (info);
2309 in = open_source_for_copy (source, destination, flags, cancellable, error);
2313 if (flags & G_FILE_COPY_OVERWRITE)
2315 out = (GOutputStream *)g_file_replace (destination,
2317 flags & G_FILE_COPY_BACKUP,
2319 cancellable, error);
2323 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2328 g_object_unref (in);
2332 if (!copy_stream_with_progress (in, out, source, cancellable,
2333 progress_callback, progress_callback_data,
2339 /* Ignore errors here. Failure to copy metadata is not a hard error */
2340 g_file_copy_attributes (source, destination,
2341 flags, cancellable, NULL);
2348 * @source: input #GFile.
2349 * @destination: destination #GFile
2350 * @flags: set of #GFileCopyFlags
2351 * @cancellable: optional #GCancellable object, %NULL to ignore.
2352 * @progress_callback: function to callback with progress information
2353 * @progress_callback_data: user data to pass to @progress_callback
2354 * @error: #GError to set on error, or %NULL
2356 * Copies the file @source to the location specified by @destination.
2357 * Can not handle recursive copies of directories.
2359 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2360 * existing @destination file is overwritten.
2362 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2363 * will be copied as symlinks, otherwise the target of the
2364 * @source symlink will be copied.
2366 * If @cancellable is not %NULL, then the operation can be cancelled by
2367 * triggering the cancellable object from another thread. If the operation
2368 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2370 * If @progress_callback is not %NULL, then the operation can be monitored by
2371 * setting this to a #GFileProgressCallback function. @progress_callback_data
2372 * will be passed to this function. It is guaranteed that this callback will
2373 * be called after all data has been transferred with the total number of bytes
2374 * copied during the operation.
2376 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2377 * error is returned, independent on the status of the @destination.
2379 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2380 * error G_IO_ERROR_EXISTS is returned.
2382 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2383 * error is returned. If trying to overwrite a directory with a directory the
2384 * G_IO_ERROR_WOULD_MERGE error is returned.
2386 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2387 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2390 * If you are interested in copying the #GFile object itself (not the on-disk
2391 * file), see g_file_dup().
2393 * Returns: %TRUE on success, %FALSE otherwise.
2396 g_file_copy (GFile *source,
2398 GFileCopyFlags flags,
2399 GCancellable *cancellable,
2400 GFileProgressCallback progress_callback,
2401 gpointer progress_callback_data,
2408 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2409 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2411 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2414 iface = G_FILE_GET_IFACE (destination);
2418 res = (* iface->copy) (source, destination,
2420 progress_callback, progress_callback_data,
2426 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2428 g_propagate_error (error, my_error);
2433 /* If the types are different, and the destination method failed
2434 also try the source method */
2435 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2437 iface = G_FILE_GET_IFACE (source);
2442 res = (* iface->copy) (source, destination,
2444 progress_callback, progress_callback_data,
2450 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2452 g_propagate_error (error, my_error);
2458 return file_copy_fallback (source, destination, flags, cancellable,
2459 progress_callback, progress_callback_data,
2464 * g_file_copy_async:
2465 * @source: input #GFile.
2466 * @destination: destination #GFile
2467 * @flags: set of #GFileCopyFlags
2468 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2470 * @cancellable: optional #GCancellable object, %NULL to ignore.
2471 * @progress_callback: function to callback with progress information
2472 * @progress_callback_data: user data to pass to @progress_callback
2473 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2474 * @user_data: the data to pass to callback function
2476 * Copies the file @source to the location specified by @destination
2477 * asynchronously. For details of the behaviour, see g_file_copy().
2479 * If @progress_callback is not %NULL, then that function that will be called
2480 * just like in g_file_copy(), however the callback will run in the main loop,
2481 * not in the thread that is doing the I/O operation.
2483 * When the operation is finished, @callback will be called. You can then call
2484 * g_file_copy_finish() to get the result of the operation.
2487 g_file_copy_async (GFile *source,
2489 GFileCopyFlags flags,
2491 GCancellable *cancellable,
2492 GFileProgressCallback progress_callback,
2493 gpointer progress_callback_data,
2494 GAsyncReadyCallback callback,
2499 g_return_if_fail (G_IS_FILE (source));
2500 g_return_if_fail (G_IS_FILE (destination));
2502 iface = G_FILE_GET_IFACE (source);
2503 (* iface->copy_async) (source,
2509 progress_callback_data,
2515 * g_file_copy_finish:
2516 * @file: input #GFile.
2517 * @res: a #GAsyncResult.
2518 * @error: a #GError, or %NULL
2520 * Finishes copying the file started with
2521 * g_file_copy_async().
2523 * Returns: a %TRUE on success, %FALSE on error.
2526 g_file_copy_finish (GFile *file,
2532 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2533 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2535 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2537 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2539 if (g_simple_async_result_propagate_error (simple, error))
2543 iface = G_FILE_GET_IFACE (file);
2544 return (* iface->copy_finish) (file, res, error);
2549 * @source: #GFile pointing to the source location.
2550 * @destination: #GFile pointing to the destination location.
2551 * @flags: set of #GFileCopyFlags.
2552 * @cancellable: optional #GCancellable object, %NULL to ignore.
2553 * @progress_callback: #GFileProgressCallback function for updates.
2554 * @progress_callback_data: gpointer to user data for the callback function.
2555 * @error: #GError for returning error conditions, or %NULL
2558 * Tries to move the file or directory @source to the location specified by @destination.
2559 * If native move operations are supported then this is used, otherwise a copy + delete
2560 * fallback is used. The native implementation may support moving directories (for instance
2561 * on moves inside the same filesystem), but the fallback code does not.
2563 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2564 * existing @destination file is overwritten.
2566 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2567 * will be copied as symlinks, otherwise the target of the
2568 * @source symlink will be copied.
2570 * If @cancellable is not %NULL, then the operation can be cancelled by
2571 * triggering the cancellable object from another thread. If the operation
2572 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2574 * If @progress_callback is not %NULL, then the operation can be monitored by
2575 * setting this to a #GFileProgressCallback function. @progress_callback_data
2576 * will be passed to this function. It is guaranteed that this callback will
2577 * be called after all data has been transferred with the total number of bytes
2578 * copied during the operation.
2580 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2581 * error is returned, independent on the status of the @destination.
2583 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2584 * error G_IO_ERROR_EXISTS is returned.
2586 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2587 * error is returned. If trying to overwrite a directory with a directory the
2588 * G_IO_ERROR_WOULD_MERGE error is returned.
2590 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2591 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2592 * may be returned (if the native move operation isn't available).
2594 * Returns: %TRUE on successful move, %FALSE otherwise.
2597 g_file_move (GFile *source,
2599 GFileCopyFlags flags,
2600 GCancellable *cancellable,
2601 GFileProgressCallback progress_callback,
2602 gpointer progress_callback_data,
2609 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2610 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2612 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2615 iface = G_FILE_GET_IFACE (destination);
2619 res = (* iface->move) (source, destination,
2621 progress_callback, progress_callback_data,
2627 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2629 g_propagate_error (error, my_error);
2634 /* If the types are different, and the destination method failed
2635 also try the source method */
2636 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2638 iface = G_FILE_GET_IFACE (source);
2643 res = (* iface->move) (source, destination,
2645 progress_callback, progress_callback_data,
2651 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2653 g_propagate_error (error, my_error);
2659 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2661 g_set_error_literal (error, G_IO_ERROR,
2662 G_IO_ERROR_NOT_SUPPORTED,
2663 _("Operation not supported"));
2667 flags |= G_FILE_COPY_ALL_METADATA;
2668 if (!g_file_copy (source, destination, flags, cancellable,
2669 progress_callback, progress_callback_data,
2673 return g_file_delete (source, cancellable, error);
2677 * g_file_make_directory
2678 * @file: input #GFile.
2679 * @cancellable: optional #GCancellable object, %NULL to ignore.
2680 * @error: a #GError, or %NULL
2682 * Creates a directory. Note that this will only create a child directory of
2683 * the immediate parent directory of the path or URI given by the #GFile. To
2684 * recursively create directories, see g_file_make_directory_with_parents().
2685 * This function will fail if the parent directory does not exist, setting
2686 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
2687 * directories, this function will fail, setting @error to
2688 * %G_IO_ERROR_NOT_SUPPORTED.
2690 * If @cancellable is not %NULL, then the operation can be cancelled by
2691 * triggering the cancellable object from another thread. If the operation
2692 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2694 * Returns: %TRUE on successful creation, %FALSE otherwise.
2697 g_file_make_directory (GFile *file,
2698 GCancellable *cancellable,
2703 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2705 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2708 iface = G_FILE_GET_IFACE (file);
2710 if (iface->make_directory == NULL)
2712 g_set_error_literal (error, G_IO_ERROR,
2713 G_IO_ERROR_NOT_SUPPORTED,
2714 _("Operation not supported"));
2718 return (* iface->make_directory) (file, cancellable, error);
2722 * g_file_make_directory_with_parents:
2723 * @file: input #GFile.
2724 * @cancellable: optional #GCancellable object, %NULL to ignore.
2725 * @error: a #GError, or %NULL
2727 * Creates a directory and any parent directories that may not exist similar to
2728 * 'mkdir -p'. If the file system does not support creating directories, this
2729 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED.
2731 * If @cancellable is not %NULL, then the operation can be cancelled by
2732 * triggering the cancellable object from another thread. If the operation
2733 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2735 * Returns: %TRUE if all directories have been successfully created, %FALSE
2741 g_file_make_directory_with_parents (GFile *file,
2742 GCancellable *cancellable,
2746 GFile *parent_file, *work_file;
2747 GList *list = NULL, *l;
2748 GError *my_error = NULL;
2750 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2753 result = g_file_make_directory (file, cancellable, &my_error);
2754 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
2757 g_propagate_error (error, my_error);
2763 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2765 g_clear_error (&my_error);
2767 parent_file = g_file_get_parent (work_file);
2768 if (parent_file == NULL)
2770 result = g_file_make_directory (parent_file, cancellable, &my_error);
2772 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2773 list = g_list_prepend (list, parent_file);
2775 work_file = parent_file;
2778 for (l = list; result && l; l = l->next)
2780 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
2784 while (list != NULL)
2786 g_object_unref ((GFile *) list->data);
2787 list = g_list_remove (list, list->data);
2792 g_propagate_error (error, my_error);
2796 return g_file_make_directory (file, cancellable, error);
2800 * g_file_make_symbolic_link:
2801 * @file: input #GFile.
2802 * @symlink_value: a string with the value of the new symlink.
2803 * @cancellable: optional #GCancellable object, %NULL to ignore.
2804 * @error: a #GError.
2806 * Creates a symbolic link.
2808 * If @cancellable is not %NULL, then the operation can be cancelled by
2809 * triggering the cancellable object from another thread. If the operation
2810 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2812 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2815 g_file_make_symbolic_link (GFile *file,
2816 const char *symlink_value,
2817 GCancellable *cancellable,
2822 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2823 g_return_val_if_fail (symlink_value != NULL, FALSE);
2825 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2828 if (*symlink_value == '\0')
2830 g_set_error_literal (error, G_IO_ERROR,
2831 G_IO_ERROR_INVALID_ARGUMENT,
2832 _("Invalid symlink value given"));
2836 iface = G_FILE_GET_IFACE (file);
2838 if (iface->make_symbolic_link == NULL)
2840 g_set_error_literal (error, G_IO_ERROR,
2841 G_IO_ERROR_NOT_SUPPORTED,
2842 _("Operation not supported"));
2846 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2851 * @file: input #GFile.
2852 * @cancellable: optional #GCancellable object, %NULL to ignore.
2853 * @error: a #GError, or %NULL
2855 * Deletes a file. If the @file is a directory, it will only be deleted if it
2858 * If @cancellable is not %NULL, then the operation can be cancelled by
2859 * triggering the cancellable object from another thread. If the operation
2860 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2862 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2865 g_file_delete (GFile *file,
2866 GCancellable *cancellable,
2871 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2873 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2876 iface = G_FILE_GET_IFACE (file);
2878 if (iface->delete_file == NULL)
2880 g_set_error_literal (error, G_IO_ERROR,
2881 G_IO_ERROR_NOT_SUPPORTED,
2882 _("Operation not supported"));
2886 return (* iface->delete_file) (file, cancellable, error);
2891 * @file: #GFile to send to trash.
2892 * @cancellable: optional #GCancellable object, %NULL to ignore.
2893 * @error: a #GError, or %NULL
2895 * Sends @file to the "Trashcan", if possible. This is similar to
2896 * deleting it, but the user can recover it before emptying the trashcan.
2897 * Not all file systems support trashing, so this call can return the
2898 * %G_IO_ERROR_NOT_SUPPORTED error.
2901 * If @cancellable is not %NULL, then the operation can be cancelled by
2902 * triggering the cancellable object from another thread. If the operation
2903 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2905 * Returns: %TRUE on successful trash, %FALSE otherwise.
2908 g_file_trash (GFile *file,
2909 GCancellable *cancellable,
2914 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2916 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2919 iface = G_FILE_GET_IFACE (file);
2921 if (iface->trash == NULL)
2923 g_set_error_literal (error,
2924 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2925 _("Trash not supported"));
2929 return (* iface->trash) (file, cancellable, error);
2933 * g_file_set_display_name:
2934 * @file: input #GFile.
2935 * @display_name: a string.
2936 * @cancellable: optional #GCancellable object, %NULL to ignore.
2937 * @error: a #GError, or %NULL
2939 * Renames @file to the specified display name.
2941 * The display name is converted from UTF8 to the correct encoding for the target
2942 * filesystem if possible and the @file is renamed to this.
2944 * If you want to implement a rename operation in the user interface the edit name
2945 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2946 * widget, and then the result after editing should be passed to g_file_set_display_name().
2948 * On success the resulting converted filename is returned.
2950 * If @cancellable is not %NULL, then the operation can be cancelled by
2951 * triggering the cancellable object from another thread. If the operation
2952 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2954 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2957 g_file_set_display_name (GFile *file,
2958 const char *display_name,
2959 GCancellable *cancellable,
2964 g_return_val_if_fail (G_IS_FILE (file), NULL);
2965 g_return_val_if_fail (display_name != NULL, NULL);
2967 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2971 G_IO_ERROR_INVALID_ARGUMENT,
2972 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2976 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2979 iface = G_FILE_GET_IFACE (file);
2981 return (* iface->set_display_name) (file, display_name, cancellable, error);
2985 * g_file_set_display_name_async:
2986 * @file: input #GFile.
2987 * @display_name: a string.
2988 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2990 * @cancellable: optional #GCancellable object, %NULL to ignore.
2991 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2992 * @user_data: the data to pass to callback function
2994 * Asynchronously sets the display name for a given #GFile.
2996 * For more details, see g_set_display_name() which is
2997 * the synchronous version of this call.
2999 * When the operation is finished, @callback will be called. You can then call
3000 * g_file_set_display_name_finish() to get the result of the operation.
3003 g_file_set_display_name_async (GFile *file,
3004 const char *display_name,
3006 GCancellable *cancellable,
3007 GAsyncReadyCallback callback,
3012 g_return_if_fail (G_IS_FILE (file));
3013 g_return_if_fail (display_name != NULL);
3015 iface = G_FILE_GET_IFACE (file);
3016 (* iface->set_display_name_async) (file,
3025 * g_file_set_display_name_finish:
3026 * @file: input #GFile.
3027 * @res: a #GAsyncResult.
3028 * @error: a #GError, or %NULL
3030 * Finishes setting a display name started with
3031 * g_file_set_display_name_async().
3033 * Returns: a #GFile or %NULL on error.
3036 g_file_set_display_name_finish (GFile *file,
3042 g_return_val_if_fail (G_IS_FILE (file), NULL);
3043 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3045 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3047 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3048 if (g_simple_async_result_propagate_error (simple, error))
3052 iface = G_FILE_GET_IFACE (file);
3053 return (* iface->set_display_name_finish) (file, res, error);
3057 * g_file_query_settable_attributes:
3058 * @file: input #GFile.
3059 * @cancellable: optional #GCancellable object, %NULL to ignore.
3060 * @error: a #GError, or %NULL
3062 * Obtain the list of settable attributes for the file.
3064 * Returns the type and full attribute name of all the attributes
3065 * that can be set on this file. This doesn't mean setting it will always
3066 * succeed though, you might get an access failure, or some specific
3067 * file may not support a specific attribute.
3069 * If @cancellable is not %NULL, then the operation can be cancelled by
3070 * triggering the cancellable object from another thread. If the operation
3071 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3073 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3074 * When you are done with it, release it with g_file_attribute_info_list_unref()
3076 GFileAttributeInfoList *
3077 g_file_query_settable_attributes (GFile *file,
3078 GCancellable *cancellable,
3083 GFileAttributeInfoList *list;
3085 g_return_val_if_fail (G_IS_FILE (file), NULL);
3087 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3090 iface = G_FILE_GET_IFACE (file);
3092 if (iface->query_settable_attributes == NULL)
3093 return g_file_attribute_info_list_new ();
3096 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3100 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3102 list = g_file_attribute_info_list_new ();
3103 g_error_free (my_error);
3106 g_propagate_error (error, my_error);
3113 * g_file_query_writable_namespaces:
3114 * @file: input #GFile.
3115 * @cancellable: optional #GCancellable object, %NULL to ignore.
3116 * @error: a #GError, or %NULL
3118 * Obtain the list of attribute namespaces where new attributes
3119 * can be created by a user. An example of this is extended
3120 * attributes (in the "xattr" namespace).
3122 * If @cancellable is not %NULL, then the operation can be cancelled by
3123 * triggering the cancellable object from another thread. If the operation
3124 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3126 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3127 * When you are done with it, release it with g_file_attribute_info_list_unref()
3129 GFileAttributeInfoList *
3130 g_file_query_writable_namespaces (GFile *file,
3131 GCancellable *cancellable,
3136 GFileAttributeInfoList *list;
3138 g_return_val_if_fail (G_IS_FILE (file), NULL);
3140 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3143 iface = G_FILE_GET_IFACE (file);
3145 if (iface->query_writable_namespaces == NULL)
3146 return g_file_attribute_info_list_new ();
3149 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3153 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3155 list = g_file_attribute_info_list_new ();
3156 g_error_free (my_error);
3159 g_propagate_error (error, my_error);
3166 * g_file_set_attribute:
3167 * @file: input #GFile.
3168 * @attribute: a string containing the attribute's name.
3169 * @type: The type of the attribute
3170 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3171 * @flags: a set of #GFileQueryInfoFlags.
3172 * @cancellable: optional #GCancellable object, %NULL to ignore.
3173 * @error: a #GError, or %NULL
3175 * Sets an attribute in the file with attribute name @attribute to @value.
3177 * If @cancellable is not %NULL, then the operation can be cancelled by
3178 * triggering the cancellable object from another thread. If the operation
3179 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3181 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3184 g_file_set_attribute (GFile *file,
3185 const char *attribute,
3186 GFileAttributeType type,
3188 GFileQueryInfoFlags flags,
3189 GCancellable *cancellable,
3194 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3195 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3197 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3200 iface = G_FILE_GET_IFACE (file);
3202 if (iface->set_attribute == NULL)
3204 g_set_error_literal (error, G_IO_ERROR,
3205 G_IO_ERROR_NOT_SUPPORTED,
3206 _("Operation not supported"));
3210 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3214 * g_file_set_attributes_from_info:
3215 * @file: input #GFile.
3216 * @info: a #GFileInfo.
3217 * @flags: #GFileQueryInfoFlags
3218 * @cancellable: optional #GCancellable object, %NULL to ignore.
3219 * @error: a #GError, or %NULL
3221 * Tries to set all attributes in the #GFileInfo on the target values,
3222 * not stopping on the first error.
3224 * If there is any error during this operation then @error will be set to
3225 * the first error. Error on particular fields are flagged by setting
3226 * the "status" field in the attribute value to
3227 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3230 * If @cancellable is not %NULL, then the operation can be cancelled by
3231 * triggering the cancellable object from another thread. If the operation
3232 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3234 * Returns: %TRUE if there was any error, %FALSE otherwise.
3237 g_file_set_attributes_from_info (GFile *file,
3239 GFileQueryInfoFlags flags,
3240 GCancellable *cancellable,
3245 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3246 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3248 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3251 g_file_info_clear_status (info);
3253 iface = G_FILE_GET_IFACE (file);
3255 return (* iface->set_attributes_from_info) (file,
3264 g_file_real_set_attributes_from_info (GFile *file,
3266 GFileQueryInfoFlags flags,
3267 GCancellable *cancellable,
3273 GFileAttributeValue *value;
3277 attributes = g_file_info_list_attributes (info, NULL);
3279 for (i = 0; attributes[i] != NULL; i++)
3281 value = _g_file_info_get_attribute_value (info, attributes[i]);
3283 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3286 if (!g_file_set_attribute (file, attributes[i],
3287 value->type, _g_file_attribute_value_peek_as_pointer (value),
3288 flags, cancellable, error))
3290 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3292 /* Don't set error multiple times */
3296 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3299 g_strfreev (attributes);
3305 * g_file_set_attributes_async:
3306 * @file: input #GFile.
3307 * @info: a #GFileInfo.
3308 * @flags: a #GFileQueryInfoFlags.
3309 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3311 * @cancellable: optional #GCancellable object, %NULL to ignore.
3312 * @callback: a #GAsyncReadyCallback.
3313 * @user_data: a #gpointer.
3315 * Asynchronously sets the attributes of @file with @info.
3317 * For more details, see g_file_set_attributes_from_info() which is
3318 * the synchronous version of this call.
3320 * When the operation is finished, @callback will be called. You can then call
3321 * g_file_set_attributes_finish() to get the result of the operation.
3324 g_file_set_attributes_async (GFile *file,
3326 GFileQueryInfoFlags flags,
3328 GCancellable *cancellable,
3329 GAsyncReadyCallback callback,
3334 g_return_if_fail (G_IS_FILE (file));
3335 g_return_if_fail (G_IS_FILE_INFO (info));
3337 iface = G_FILE_GET_IFACE (file);
3338 (* iface->set_attributes_async) (file,
3348 * g_file_set_attributes_finish:
3349 * @file: input #GFile.
3350 * @result: a #GAsyncResult.
3351 * @info: a #GFileInfo.
3352 * @error: a #GError, or %NULL
3354 * Finishes setting an attribute started in g_file_set_attributes_async().
3356 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3359 g_file_set_attributes_finish (GFile *file,
3360 GAsyncResult *result,
3366 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3367 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3369 /* No standard handling of errors here, as we must set info even
3372 iface = G_FILE_GET_IFACE (file);
3373 return (* iface->set_attributes_finish) (file, result, info, error);
3377 * g_file_set_attribute_string:
3378 * @file: input #GFile.
3379 * @attribute: a string containing the attribute's name.
3380 * @value: a string containing the attribute's value.
3381 * @flags: #GFileQueryInfoFlags.
3382 * @cancellable: optional #GCancellable object, %NULL to ignore.
3383 * @error: a #GError, or %NULL
3385 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3386 * If @attribute is of a different type, this operation will fail.
3388 * If @cancellable is not %NULL, then the operation can be cancelled by
3389 * triggering the cancellable object from another thread. If the operation
3390 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3392 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3395 g_file_set_attribute_string (GFile *file,
3396 const char *attribute,
3398 GFileQueryInfoFlags flags,
3399 GCancellable *cancellable,
3402 return g_file_set_attribute (file, attribute,
3403 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3404 flags, cancellable, error);
3408 * g_file_set_attribute_byte_string:
3409 * @file: input #GFile.
3410 * @attribute: a string containing the attribute's name.
3411 * @value: a string containing the attribute's new value.
3412 * @flags: a #GFileQueryInfoFlags.
3413 * @cancellable: optional #GCancellable object, %NULL to ignore.
3414 * @error: a #GError, or %NULL
3416 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3417 * If @attribute is of a different type, this operation will fail,
3420 * If @cancellable is not %NULL, then the operation can be cancelled by
3421 * triggering the cancellable object from another thread. If the operation
3422 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3424 * Returns: %TRUE if the @attribute was successfully set to @value
3425 * in the @file, %FALSE otherwise.
3428 g_file_set_attribute_byte_string (GFile *file,
3429 const char *attribute,
3431 GFileQueryInfoFlags flags,
3432 GCancellable *cancellable,
3435 return g_file_set_attribute (file, attribute,
3436 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3437 flags, cancellable, error);
3441 * g_file_set_attribute_uint32:
3442 * @file: input #GFile.
3443 * @attribute: a string containing the attribute's name.
3444 * @value: a #guint32 containing the attribute's new value.
3445 * @flags: a #GFileQueryInfoFlags.
3446 * @cancellable: optional #GCancellable object, %NULL to ignore.
3447 * @error: a #GError, or %NULL
3449 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3450 * If @attribute is of a different type, this operation will fail.
3452 * If @cancellable is not %NULL, then the operation can be cancelled by
3453 * triggering the cancellable object from another thread. If the operation
3454 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3456 * Returns: %TRUE if the @attribute was successfully set to @value
3457 * in the @file, %FALSE otherwise.
3460 g_file_set_attribute_uint32 (GFile *file,
3461 const char *attribute,
3463 GFileQueryInfoFlags flags,
3464 GCancellable *cancellable,
3467 return g_file_set_attribute (file, attribute,
3468 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3469 flags, cancellable, error);
3473 * g_file_set_attribute_int32:
3474 * @file: input #GFile.
3475 * @attribute: a string containing the attribute's name.
3476 * @value: a #gint32 containing the attribute's new value.
3477 * @flags: a #GFileQueryInfoFlags.
3478 * @cancellable: optional #GCancellable object, %NULL to ignore.
3479 * @error: a #GError, or %NULL
3481 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3482 * If @attribute is of a different type, this operation will fail.
3484 * If @cancellable is not %NULL, then the operation can be cancelled by
3485 * triggering the cancellable object from another thread. If the operation
3486 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3488 * Returns: %TRUE if the @attribute was successfully set to @value
3489 * in the @file, %FALSE otherwise.
3492 g_file_set_attribute_int32 (GFile *file,
3493 const char *attribute,
3495 GFileQueryInfoFlags flags,
3496 GCancellable *cancellable,
3499 return g_file_set_attribute (file, attribute,
3500 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3501 flags, cancellable, error);
3505 * g_file_set_attribute_uint64:
3506 * @file: input #GFile.
3507 * @attribute: a string containing the attribute's name.
3508 * @value: a #guint64 containing the attribute's new value.
3509 * @flags: a #GFileQueryInfoFlags.
3510 * @cancellable: optional #GCancellable object, %NULL to ignore.
3511 * @error: a #GError, or %NULL
3513 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3514 * If @attribute is of a different type, this operation will fail.
3516 * If @cancellable is not %NULL, then the operation can be cancelled by
3517 * triggering the cancellable object from another thread. If the operation
3518 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3520 * Returns: %TRUE if the @attribute was successfully set to @value
3521 * in the @file, %FALSE otherwise.
3524 g_file_set_attribute_uint64 (GFile *file,
3525 const char *attribute,
3527 GFileQueryInfoFlags flags,
3528 GCancellable *cancellable,
3531 return g_file_set_attribute (file, attribute,
3532 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3533 flags, cancellable, error);
3537 * g_file_set_attribute_int64:
3538 * @file: input #GFile.
3539 * @attribute: a string containing the attribute's name.
3540 * @value: a #guint64 containing the attribute's new value.
3541 * @flags: a #GFileQueryInfoFlags.
3542 * @cancellable: optional #GCancellable object, %NULL to ignore.
3543 * @error: a #GError, or %NULL
3545 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3546 * If @attribute is of a different type, this operation will fail.
3548 * If @cancellable is not %NULL, then the operation can be cancelled by
3549 * triggering the cancellable object from another thread. If the operation
3550 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3552 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3555 g_file_set_attribute_int64 (GFile *file,
3556 const char *attribute,
3558 GFileQueryInfoFlags flags,
3559 GCancellable *cancellable,
3562 return g_file_set_attribute (file, attribute,
3563 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3564 flags, cancellable, error);
3568 * g_file_mount_mountable:
3569 * @file: input #GFile.
3570 * @flags: flags affecting the operation
3571 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3572 * @cancellable: optional #GCancellable object, %NULL to ignore.
3573 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3574 * @user_data: the data to pass to callback function
3576 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3577 * Using @mount_operation, you can request callbacks when, for instance,
3578 * passwords are needed during authentication.
3580 * If @cancellable is not %NULL, then the operation can be cancelled by
3581 * triggering the cancellable object from another thread. If the operation
3582 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3584 * When the operation is finished, @callback will be called. You can then call
3585 * g_file_mount_mountable_finish() to get the result of the operation.
3588 g_file_mount_mountable (GFile *file,
3589 GMountMountFlags flags,
3590 GMountOperation *mount_operation,
3591 GCancellable *cancellable,
3592 GAsyncReadyCallback callback,
3597 g_return_if_fail (G_IS_FILE (file));
3599 iface = G_FILE_GET_IFACE (file);
3601 if (iface->mount_mountable == NULL)
3603 g_simple_async_report_error_in_idle (G_OBJECT (file),
3607 G_IO_ERROR_NOT_SUPPORTED,
3608 _("Operation not supported"));
3612 (* iface->mount_mountable) (file,
3621 * g_file_mount_mountable_finish:
3622 * @file: input #GFile.
3623 * @result: a #GAsyncResult.
3624 * @error: a #GError, or %NULL
3626 * Finishes a mount operation. See g_file_mount_mountable() for details.
3628 * Finish an asynchronous mount operation that was started
3629 * with g_file_mount_mountable().
3631 * Returns: a #GFile or %NULL on error.
3634 g_file_mount_mountable_finish (GFile *file,
3635 GAsyncResult *result,
3640 g_return_val_if_fail (G_IS_FILE (file), NULL);
3641 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3643 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3645 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3646 if (g_simple_async_result_propagate_error (simple, error))
3650 iface = G_FILE_GET_IFACE (file);
3651 return (* iface->mount_mountable_finish) (file, result, error);
3655 * g_file_unmount_mountable:
3656 * @file: input #GFile.
3657 * @flags: flags affecting the operation
3658 * @cancellable: optional #GCancellable object, %NULL to ignore.
3659 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3660 * @user_data: the data to pass to callback function
3662 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3664 * If @cancellable is not %NULL, then the operation can be cancelled by
3665 * triggering the cancellable object from another thread. If the operation
3666 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3668 * When the operation is finished, @callback will be called. You can then call
3669 * g_file_unmount_mountable_finish() to get the result of the operation.
3672 g_file_unmount_mountable (GFile *file,
3673 GMountUnmountFlags flags,
3674 GCancellable *cancellable,
3675 GAsyncReadyCallback callback,
3680 g_return_if_fail (G_IS_FILE (file));
3682 iface = G_FILE_GET_IFACE (file);
3684 if (iface->unmount_mountable == NULL)
3686 g_simple_async_report_error_in_idle (G_OBJECT (file),
3690 G_IO_ERROR_NOT_SUPPORTED,
3691 _("Operation not supported"));
3695 (* iface->unmount_mountable) (file,
3703 * g_file_unmount_mountable_finish:
3704 * @file: input #GFile.
3705 * @result: a #GAsyncResult.
3706 * @error: a #GError, or %NULL
3708 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3710 * Finish an asynchronous unmount operation that was started
3711 * with g_file_unmount_mountable().
3713 * Returns: %TRUE if the operation finished successfully. %FALSE
3717 g_file_unmount_mountable_finish (GFile *file,
3718 GAsyncResult *result,
3723 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3724 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3726 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3728 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3729 if (g_simple_async_result_propagate_error (simple, error))
3733 iface = G_FILE_GET_IFACE (file);
3734 return (* iface->unmount_mountable_finish) (file, result, error);
3738 * g_file_eject_mountable:
3739 * @file: input #GFile.
3740 * @flags: flags affecting the operation
3741 * @cancellable: optional #GCancellable object, %NULL to ignore.
3742 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3743 * @user_data: the data to pass to callback function
3745 * Starts an asynchronous eject on a mountable.
3746 * When this operation has completed, @callback will be called with
3747 * @user_user data, and the operation can be finalized with
3748 * g_file_eject_mountable_finish().
3750 * If @cancellable is not %NULL, then the operation can be cancelled by
3751 * triggering the cancellable object from another thread. If the operation
3752 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3755 g_file_eject_mountable (GFile *file,
3756 GMountUnmountFlags flags,
3757 GCancellable *cancellable,
3758 GAsyncReadyCallback callback,
3763 g_return_if_fail (G_IS_FILE (file));
3765 iface = G_FILE_GET_IFACE (file);
3767 if (iface->eject_mountable == NULL)
3769 g_simple_async_report_error_in_idle (G_OBJECT (file),
3773 G_IO_ERROR_NOT_SUPPORTED,
3774 _("Operation not supported"));
3778 (* iface->eject_mountable) (file,
3786 * g_file_eject_mountable_finish:
3787 * @file: input #GFile.
3788 * @result: a #GAsyncResult.
3789 * @error: a #GError, or %NULL
3791 * Finishes an asynchronous eject operation started by
3792 * g_file_eject_mountable().
3794 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3798 g_file_eject_mountable_finish (GFile *file,
3799 GAsyncResult *result,
3804 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3805 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3807 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3809 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3810 if (g_simple_async_result_propagate_error (simple, error))
3814 iface = G_FILE_GET_IFACE (file);
3815 return (* iface->eject_mountable_finish) (file, result, error);
3819 * g_file_monitor_directory:
3820 * @file: input #GFile.
3821 * @flags: a set of #GFileMonitorFlags.
3822 * @cancellable: optional #GCancellable object, %NULL to ignore.
3823 * @error: a #GError, or %NULL.
3825 * Obtains a directory monitor for the given file.
3826 * This may fail if directory monitoring is not supported.
3828 * If @cancellable is not %NULL, then the operation can be cancelled by
3829 * triggering the cancellable object from another thread. If the operation
3830 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3832 * Returns: a #GFileMonitor for the given @file,
3833 * or %NULL on error.
3836 g_file_monitor_directory (GFile *file,
3837 GFileMonitorFlags flags,
3838 GCancellable *cancellable,
3843 g_return_val_if_fail (G_IS_FILE (file), NULL);
3845 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3848 iface = G_FILE_GET_IFACE (file);
3850 if (iface->monitor_dir == NULL)
3852 g_set_error_literal (error, G_IO_ERROR,
3853 G_IO_ERROR_NOT_SUPPORTED,
3854 _("Operation not supported"));
3858 return (* iface->monitor_dir) (file, flags, cancellable, error);
3862 * g_file_monitor_file:
3863 * @file: input #GFile.
3864 * @flags: a set of #GFileMonitorFlags.
3865 * @cancellable: optional #GCancellable object, %NULL to ignore.
3866 * @error: a #GError, or %NULL.
3868 * Obtains a file monitor for the given file. If no file notification
3869 * mechanism exists, then regular polling of the file is used.
3871 * If @cancellable is not %NULL, then the operation can be cancelled by
3872 * triggering the cancellable object from another thread. If the operation
3873 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3875 * Returns: a #GFileMonitor for the given @file.
3878 g_file_monitor_file (GFile *file,
3879 GFileMonitorFlags flags,
3880 GCancellable *cancellable,
3884 GFileMonitor *monitor;
3886 g_return_val_if_fail (G_IS_FILE (file), NULL);
3888 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3891 iface = G_FILE_GET_IFACE (file);
3895 if (iface->monitor_file)
3896 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3898 /* Fallback to polling */
3899 if (monitor == NULL)
3900 monitor = _g_poll_file_monitor_new (file);
3907 * @file: input #GFile
3908 * @flags: a set of #GFileMonitorFlags
3909 * @cancellable: optional #GCancellable object, %NULL to ignore
3910 * @error: a #GError, or %NULL
3912 * Obtains a file or directory monitor for the given file, depending
3913 * on the type of the file.
3915 * If @cancellable is not %NULL, then the operation can be cancelled by
3916 * triggering the cancellable object from another thread. If the operation
3917 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3919 * Returns: a #GFileMonitor for the given @file.
3924 g_file_monitor (GFile *file,
3925 GFileMonitorFlags flags,
3926 GCancellable *cancellable,
3929 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
3930 return g_file_monitor_directory (file, flags, cancellable, error);
3932 return g_file_monitor_file (file, flags, cancellable, error);
3935 /********************************************
3936 * Default implementation of async ops *
3937 ********************************************/
3941 GFileQueryInfoFlags flags;
3943 } QueryInfoAsyncData;
3946 query_info_data_free (QueryInfoAsyncData *data)
3949 g_object_unref (data->info);
3950 g_free (data->attributes);
3955 query_info_async_thread (GSimpleAsyncResult *res,
3957 GCancellable *cancellable)
3959 GError *error = NULL;
3960 QueryInfoAsyncData *data;
3963 data = g_simple_async_result_get_op_res_gpointer (res);
3965 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3969 g_simple_async_result_set_from_error (res, error);
3970 g_error_free (error);
3977 g_file_real_query_info_async (GFile *file,
3978 const char *attributes,
3979 GFileQueryInfoFlags flags,
3981 GCancellable *cancellable,
3982 GAsyncReadyCallback callback,
3985 GSimpleAsyncResult *res;
3986 QueryInfoAsyncData *data;
3988 data = g_new0 (QueryInfoAsyncData, 1);
3989 data->attributes = g_strdup (attributes);
3990 data->flags = flags;
3992 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3993 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3995 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3996 g_object_unref (res);
4000 g_file_real_query_info_finish (GFile *file,
4004 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4005 QueryInfoAsyncData *data;
4007 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4009 data = g_simple_async_result_get_op_res_gpointer (simple);
4011 return g_object_ref (data->info);
4019 } QueryFilesystemInfoAsyncData;
4022 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4025 g_object_unref (data->info);
4026 g_free (data->attributes);
4031 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4033 GCancellable *cancellable)
4035 GError *error = NULL;
4036 QueryFilesystemInfoAsyncData *data;
4039 data = g_simple_async_result_get_op_res_gpointer (res);
4041 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4045 g_simple_async_result_set_from_error (res, error);
4046 g_error_free (error);
4053 g_file_real_query_filesystem_info_async (GFile *file,
4054 const char *attributes,
4056 GCancellable *cancellable,
4057 GAsyncReadyCallback callback,
4060 GSimpleAsyncResult *res;
4061 QueryFilesystemInfoAsyncData *data;
4063 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4064 data->attributes = g_strdup (attributes);
4066 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4067 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4069 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4070 g_object_unref (res);
4074 g_file_real_query_filesystem_info_finish (GFile *file,
4078 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4079 QueryFilesystemInfoAsyncData *data;
4081 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4083 data = g_simple_async_result_get_op_res_gpointer (simple);
4085 return g_object_ref (data->info);
4092 GFileQueryInfoFlags flags;
4093 GFileEnumerator *enumerator;
4094 } EnumerateChildrenAsyncData;
4097 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4099 if (data->enumerator)
4100 g_object_unref (data->enumerator);
4101 g_free (data->attributes);
4106 enumerate_children_async_thread (GSimpleAsyncResult *res,
4108 GCancellable *cancellable)
4110 GError *error = NULL;
4111 EnumerateChildrenAsyncData *data;
4112 GFileEnumerator *enumerator;
4114 data = g_simple_async_result_get_op_res_gpointer (res);
4116 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4118 if (enumerator == NULL)
4120 g_simple_async_result_set_from_error (res, error);
4121 g_error_free (error);
4124 data->enumerator = enumerator;
4128 g_file_real_enumerate_children_async (GFile *file,
4129 const char *attributes,
4130 GFileQueryInfoFlags flags,
4132 GCancellable *cancellable,
4133 GAsyncReadyCallback callback,
4136 GSimpleAsyncResult *res;
4137 EnumerateChildrenAsyncData *data;
4139 data = g_new0 (EnumerateChildrenAsyncData, 1);
4140 data->attributes = g_strdup (attributes);
4141 data->flags = flags;
4143 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4144 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4146 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4147 g_object_unref (res);
4150 static GFileEnumerator *
4151 g_file_real_enumerate_children_finish (GFile *file,
4155 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4156 EnumerateChildrenAsyncData *data;
4158 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4160 data = g_simple_async_result_get_op_res_gpointer (simple);
4161 if (data->enumerator)
4162 return g_object_ref (data->enumerator);
4168 open_read_async_thread (GSimpleAsyncResult *res,
4170 GCancellable *cancellable)
4173 GFileInputStream *stream;
4174 GError *error = NULL;
4176 iface = G_FILE_GET_IFACE (object);
4178 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4182 g_simple_async_result_set_from_error (res, error);
4183 g_error_free (error);
4186 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4190 g_file_real_read_async (GFile *file,
4192 GCancellable *cancellable,
4193 GAsyncReadyCallback callback,
4196 GSimpleAsyncResult *res;
4198 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4200 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4201 g_object_unref (res);
4204 static GFileInputStream *
4205 g_file_real_read_finish (GFile *file,
4209 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4212 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4214 op = g_simple_async_result_get_op_res_gpointer (simple);
4216 return g_object_ref (op);
4222 append_to_async_thread (GSimpleAsyncResult *res,
4224 GCancellable *cancellable)
4227 GFileCreateFlags *data;
4228 GFileOutputStream *stream;
4229 GError *error = NULL;
4231 iface = G_FILE_GET_IFACE (object);
4233 data = g_simple_async_result_get_op_res_gpointer (res);
4235 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4239 g_simple_async_result_set_from_error (res, error);
4240 g_error_free (error);
4243 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4247 g_file_real_append_to_async (GFile *file,
4248 GFileCreateFlags flags,
4250 GCancellable *cancellable,
4251 GAsyncReadyCallback callback,
4254 GFileCreateFlags *data;
4255 GSimpleAsyncResult *res;
4257 data = g_new0 (GFileCreateFlags, 1);
4260 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4261 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4263 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4264 g_object_unref (res);
4267 static GFileOutputStream *
4268 g_file_real_append_to_finish (GFile *file,
4272 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4275 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4277 op = g_simple_async_result_get_op_res_gpointer (simple);
4279 return g_object_ref (op);
4285 create_async_thread (GSimpleAsyncResult *res,
4287 GCancellable *cancellable)
4290 GFileCreateFlags *data;
4291 GFileOutputStream *stream;
4292 GError *error = NULL;
4294 iface = G_FILE_GET_IFACE (object);
4296 data = g_simple_async_result_get_op_res_gpointer (res);
4298 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4302 g_simple_async_result_set_from_error (res, error);
4303 g_error_free (error);
4306 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4310 g_file_real_create_async (GFile *file,
4311 GFileCreateFlags flags,
4313 GCancellable *cancellable,
4314 GAsyncReadyCallback callback,
4317 GFileCreateFlags *data;
4318 GSimpleAsyncResult *res;
4320 data = g_new0 (GFileCreateFlags, 1);
4323 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4324 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4326 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4327 g_object_unref (res);
4330 static GFileOutputStream *
4331 g_file_real_create_finish (GFile *file,
4335 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4338 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4340 op = g_simple_async_result_get_op_res_gpointer (simple);
4342 return g_object_ref (op);
4348 GFileOutputStream *stream;
4350 gboolean make_backup;
4351 GFileCreateFlags flags;
4355 replace_async_data_free (ReplaceAsyncData *data)
4358 g_object_unref (data->stream);
4359 g_free (data->etag);
4364 replace_async_thread (GSimpleAsyncResult *res,
4366 GCancellable *cancellable)
4369 GFileOutputStream *stream;
4370 GError *error = NULL;
4371 ReplaceAsyncData *data;
4373 iface = G_FILE_GET_IFACE (object);
4375 data = g_simple_async_result_get_op_res_gpointer (res);
4377 stream = iface->replace (G_FILE (object),
4386 g_simple_async_result_set_from_error (res, error);
4387 g_error_free (error);
4390 data->stream = stream;
4394 g_file_real_replace_async (GFile *file,
4396 gboolean make_backup,
4397 GFileCreateFlags flags,
4399 GCancellable *cancellable,
4400 GAsyncReadyCallback callback,
4403 GSimpleAsyncResult *res;
4404 ReplaceAsyncData *data;
4406 data = g_new0 (ReplaceAsyncData, 1);
4407 data->etag = g_strdup (etag);
4408 data->make_backup = make_backup;
4409 data->flags = flags;
4411 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4412 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4414 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4415 g_object_unref (res);
4418 static GFileOutputStream *
4419 g_file_real_replace_finish (GFile *file,
4423 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4424 ReplaceAsyncData *data;
4426 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4428 data = g_simple_async_result_get_op_res_gpointer (simple);
4430 return g_object_ref (data->stream);
4438 } SetDisplayNameAsyncData;
4441 set_display_name_data_free (SetDisplayNameAsyncData *data)
4443 g_free (data->name);
4445 g_object_unref (data->file);
4450 set_display_name_async_thread (GSimpleAsyncResult *res,
4452 GCancellable *cancellable)
4454 GError *error = NULL;
4455 SetDisplayNameAsyncData *data;
4458 data = g_simple_async_result_get_op_res_gpointer (res);
4460 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4464 g_simple_async_result_set_from_error (res, error);
4465 g_error_free (error);
4472 g_file_real_set_display_name_async (GFile *file,
4473 const char *display_name,
4475 GCancellable *cancellable,
4476 GAsyncReadyCallback callback,
4479 GSimpleAsyncResult *res;
4480 SetDisplayNameAsyncData *data;
4482 data = g_new0 (SetDisplayNameAsyncData, 1);
4483 data->name = g_strdup (display_name);
4485 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4486 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4488 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4489 g_object_unref (res);
4493 g_file_real_set_display_name_finish (GFile *file,
4497 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4498 SetDisplayNameAsyncData *data;
4500 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4502 data = g_simple_async_result_get_op_res_gpointer (simple);
4504 return g_object_ref (data->file);
4510 GFileQueryInfoFlags flags;
4517 set_info_data_free (SetInfoAsyncData *data)
4520 g_object_unref (data->info);
4522 g_error_free (data->error);
4527 set_info_async_thread (GSimpleAsyncResult *res,
4529 GCancellable *cancellable)
4531 SetInfoAsyncData *data;
4533 data = g_simple_async_result_get_op_res_gpointer (res);
4536 data->res = g_file_set_attributes_from_info (G_FILE (object),
4544 g_file_real_set_attributes_async (GFile *file,
4546 GFileQueryInfoFlags flags,
4548 GCancellable *cancellable,
4549 GAsyncReadyCallback callback,
4552 GSimpleAsyncResult *res;
4553 SetInfoAsyncData *data;
4555 data = g_new0 (SetInfoAsyncData, 1);
4556 data->info = g_file_info_dup (info);
4557 data->flags = flags;
4559 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4560 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4562 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4563 g_object_unref (res);
4567 g_file_real_set_attributes_finish (GFile *file,
4572 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4573 SetInfoAsyncData *data;
4575 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4577 data = g_simple_async_result_get_op_res_gpointer (simple);
4580 *info = g_object_ref (data->info);
4582 if (error != NULL && data->error)
4583 *error = g_error_copy (data->error);
4589 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4591 GCancellable *cancellable)
4593 GError *error = NULL;
4596 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4600 g_simple_async_result_set_from_error (res, error);
4601 g_error_free (error);
4604 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4608 g_file_real_find_enclosing_mount_async (GFile *file,
4610 GCancellable *cancellable,
4611 GAsyncReadyCallback callback,
4614 GSimpleAsyncResult *res;
4616 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4618 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4619 g_object_unref (res);
4623 g_file_real_find_enclosing_mount_finish (GFile *file,
4627 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4630 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4632 mount = g_simple_async_result_get_op_res_gpointer (simple);
4633 return g_object_ref (mount);
4640 GFileCopyFlags flags;
4641 GFileProgressCallback progress_cb;
4642 gpointer progress_cb_data;
4643 GIOSchedulerJob *job;
4647 copy_async_data_free (CopyAsyncData *data)
4649 g_object_unref (data->source);
4650 g_object_unref (data->destination);
4655 CopyAsyncData *data;
4656 goffset current_num_bytes;
4657 goffset total_num_bytes;
4661 copy_async_progress_in_main (gpointer user_data)
4663 ProgressData *progress = user_data;
4664 CopyAsyncData *data = progress->data;
4666 data->progress_cb (progress->current_num_bytes,
4667 progress->total_num_bytes,
4668 data->progress_cb_data);
4674 mainloop_barrier (gpointer user_data)
4676 /* Does nothing, but ensures all queued idles before
4683 copy_async_progress_callback (goffset current_num_bytes,
4684 goffset total_num_bytes,
4687 CopyAsyncData *data = user_data;
4688 ProgressData *progress;
4690 progress = g_new (ProgressData, 1);
4691 progress->data = data;
4692 progress->current_num_bytes = current_num_bytes;
4693 progress->total_num_bytes = total_num_bytes;
4695 g_io_scheduler_job_send_to_mainloop_async (data->job,
4696 copy_async_progress_in_main,
4702 copy_async_thread (GIOSchedulerJob *job,
4703 GCancellable *cancellable,
4706 GSimpleAsyncResult *res;
4707 CopyAsyncData *data;
4712 data = g_simple_async_result_get_op_res_gpointer (res);
4716 result = g_file_copy (data->source,
4720 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4724 /* Ensure all progress callbacks are done running in main thread */
4725 if (data->progress_cb != NULL)
4726 g_io_scheduler_job_send_to_mainloop (job,
4732 g_simple_async_result_set_from_error (res, error);
4733 g_error_free (error);
4736 g_simple_async_result_complete_in_idle (res);
4742 g_file_real_copy_async (GFile *source,
4744 GFileCopyFlags flags,
4746 GCancellable *cancellable,
4747 GFileProgressCallback progress_callback,
4748 gpointer progress_callback_data,
4749 GAsyncReadyCallback callback,
4752 GSimpleAsyncResult *res;
4753 CopyAsyncData *data;
4755 data = g_new0 (CopyAsyncData, 1);
4756 data->source = g_object_ref (source);
4757 data->destination = g_object_ref (destination);
4758 data->flags = flags;
4759 data->progress_cb = progress_callback;
4760 data->progress_cb_data = progress_callback_data;
4762 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4763 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4765 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4769 g_file_real_copy_finish (GFile *file,
4773 /* Error handled in g_file_copy_finish() */
4778 /********************************************
4779 * Default VFS operations *
4780 ********************************************/
4783 * g_file_new_for_path:
4784 * @path: a string containing a relative or absolute path.
4786 * Constructs a #GFile for a given path. This operation never
4787 * fails, but the returned object might not support any I/O
4788 * operation if @path is malformed.
4790 * Returns: a new #GFile for the given @path.
4793 g_file_new_for_path (const char *path)
4795 g_return_val_if_fail (path != NULL, NULL);
4797 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4801 * g_file_new_for_uri:
4802 * @uri: a string containing a URI.
4804 * Constructs a #GFile for a given URI. This operation never
4805 * fails, but the returned object might not support any I/O
4806 * operation if @uri is malformed or if the uri type is
4809 * Returns: a #GFile for the given @uri.
4812 g_file_new_for_uri (const char *uri)
4814 g_return_val_if_fail (uri != NULL, NULL);
4816 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4820 * g_file_parse_name:
4821 * @parse_name: a file name or path to be parsed.
4823 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4824 * This operation never fails, but the returned object might not support any I/O
4825 * operation if the @parse_name cannot be parsed.
4827 * Returns: a new #GFile.
4830 g_file_parse_name (const char *parse_name)
4832 g_return_val_if_fail (parse_name != NULL, NULL);
4834 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4838 is_valid_scheme_character (char c)
4840 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4843 /* Following RFC 2396, valid schemes are built like:
4844 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
4847 has_valid_scheme (const char *uri)
4853 if (!g_ascii_isalpha (*p))
4858 } while (is_valid_scheme_character (*p));
4864 * g_file_new_for_commandline_arg:
4865 * @arg: a command line string.
4867 * Creates a #GFile with the given argument from the command line. The value of
4868 * @arg can be either a URI, an absolute path or a relative path resolved
4869 * relative to the current working directory.
4870 * This operation never fails, but the returned object might not support any
4871 * I/O operation if @arg points to a malformed path.
4873 * Returns: a new #GFile.
4876 g_file_new_for_commandline_arg (const char *arg)
4882 g_return_val_if_fail (arg != NULL, NULL);
4884 if (g_path_is_absolute (arg))
4885 return g_file_new_for_path (arg);
4887 if (has_valid_scheme (arg))
4888 return g_file_new_for_uri (arg);
4890 current_dir = g_get_current_dir ();
4891 filename = g_build_filename (current_dir, arg, NULL);
4892 g_free (current_dir);
4894 file = g_file_new_for_path (filename);
4901 * g_file_mount_enclosing_volume:
4902 * @location: input #GFile.
4903 * @flags: flags affecting the operation
4904 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4905 * @cancellable: optional #GCancellable object, %NULL to ignore.
4906 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4907 * @user_data: the data to pass to callback function
4909 * Starts a @mount_operation, mounting the volume that contains the file @location.
4911 * When this operation has completed, @callback will be called with
4912 * @user_user data, and the operation can be finalized with
4913 * g_file_mount_enclosing_volume_finish().
4915 * If @cancellable is not %NULL, then the operation can be cancelled by
4916 * triggering the cancellable object from another thread. If the operation
4917 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4920 g_file_mount_enclosing_volume (GFile *location,
4921 GMountMountFlags flags,
4922 GMountOperation *mount_operation,
4923 GCancellable *cancellable,
4924 GAsyncReadyCallback callback,
4929 g_return_if_fail (G_IS_FILE (location));
4931 iface = G_FILE_GET_IFACE (location);
4933 if (iface->mount_enclosing_volume == NULL)
4935 g_simple_async_report_error_in_idle (G_OBJECT (location),
4936 callback, user_data,
4937 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4938 _("volume doesn't implement mount"));
4943 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4948 * g_file_mount_enclosing_volume_finish:
4949 * @location: input #GFile.
4950 * @result: a #GAsyncResult.
4951 * @error: a #GError, or %NULL
4953 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4955 * Returns: %TRUE if successful. If an error
4956 * has occurred, this function will return %FALSE and set @error
4957 * appropriately if present.
4960 g_file_mount_enclosing_volume_finish (GFile *location,
4961 GAsyncResult *result,
4966 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4967 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4969 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4971 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4972 if (g_simple_async_result_propagate_error (simple, error))
4976 iface = G_FILE_GET_IFACE (location);
4978 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4981 /********************************************
4982 * Utility functions *
4983 ********************************************/
4986 * g_file_query_default_handler:
4987 * @file: a #GFile to open.
4988 * @cancellable: optional #GCancellable object, %NULL to ignore.
4989 * @error: a #GError, or %NULL
4991 * Returns the #GAppInfo that is registered as the default
4992 * application to handle the file specified by @file.
4994 * If @cancellable is not %NULL, then the operation can be cancelled by
4995 * triggering the cancellable object from another thread. If the operation
4996 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4998 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4999 * When you are done with it, release it with g_object_unref()
5002 g_file_query_default_handler (GFile *file,
5003 GCancellable *cancellable,
5007 const char *content_type;
5012 uri_scheme = g_file_get_uri_scheme (file);
5013 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
5014 g_free (uri_scheme);
5016 if (appinfo != NULL)
5019 info = g_file_query_info (file,
5020 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
5029 content_type = g_file_info_get_content_type (info);
5032 /* Don't use is_native(), as we want to support fuse paths if availible */
5033 path = g_file_get_path (file);
5034 appinfo = g_app_info_get_default_for_type (content_type,
5039 g_object_unref (info);
5041 if (appinfo != NULL)
5044 g_set_error_literal (error, G_IO_ERROR,
5045 G_IO_ERROR_NOT_SUPPORTED,
5046 _("No application is registered as handling this file"));
5052 #define GET_CONTENT_BLOCK_SIZE 8192
5055 * g_file_load_contents:
5056 * @file: input #GFile.
5057 * @cancellable: optional #GCancellable object, %NULL to ignore.
5058 * @contents: a location to place the contents of the file.
5059 * @length: a location to place the length of the contents of the file.
5060 * @etag_out: a location to place the current entity tag for the file.
5061 * @error: a #GError, or %NULL
5063 * Loads the content of the file into memory, returning the size of
5064 * the data. The data is always zero terminated, but this is not
5065 * included in the resultant @length.
5067 * If @cancellable is not %NULL, then the operation can be cancelled by
5068 * triggering the cancellable object from another thread. If the operation
5069 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5071 * Returns: %TRUE if the @file's contents were successfully loaded.
5072 * %FALSE if there were errors..
5075 g_file_load_contents (GFile *file,
5076 GCancellable *cancellable,
5082 GFileInputStream *in;
5083 GByteArray *content;
5088 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5089 g_return_val_if_fail (contents != NULL, FALSE);
5091 in = g_file_read (file, cancellable, error);
5095 content = g_byte_array_new ();
5098 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5099 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
5100 content->data + pos,
5101 GET_CONTENT_BLOCK_SIZE,
5102 cancellable, error)) > 0)
5105 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5112 info = g_file_input_stream_query_info (in,
5113 G_FILE_ATTRIBUTE_ETAG_VALUE,
5118 *etag_out = g_strdup (g_file_info_get_etag (info));
5119 g_object_unref (info);
5123 /* Ignore errors on close */
5124 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
5125 g_object_unref (in);
5129 /* error is set already */
5130 g_byte_array_free (content, TRUE);
5137 /* Zero terminate (we got an extra byte allocated for this */
5138 content->data[pos] = 0;
5140 *contents = (char *)g_byte_array_free (content, FALSE);
5148 GCancellable *cancellable;
5149 GFileReadMoreCallback read_more_callback;
5150 GAsyncReadyCallback callback;
5152 GByteArray *content;
5159 load_contents_data_free (LoadContentsData *data)
5162 g_error_free (data->error);
5163 if (data->cancellable)
5164 g_object_unref (data->cancellable);
5166 g_byte_array_free (data->content, TRUE);
5167 g_free (data->etag);
5168 g_object_unref (data->file);
5173 load_contents_close_callback (GObject *obj,
5174 GAsyncResult *close_res,
5177 GInputStream *stream = G_INPUT_STREAM (obj);
5178 LoadContentsData *data = user_data;
5179 GSimpleAsyncResult *res;
5181 /* Ignore errors here, we're only reading anyway */
5182 g_input_stream_close_finish (stream, close_res, NULL);
5183 g_object_unref (stream);
5185 res = g_simple_async_result_new (G_OBJECT (data->file),
5188 g_file_load_contents_async);
5189 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5190 g_simple_async_result_complete (res);
5191 g_object_unref (res);
5195 load_contents_fstat_callback (GObject *obj,
5196 GAsyncResult *stat_res,
5199 GInputStream *stream = G_INPUT_STREAM (obj);
5200 LoadContentsData *data = user_data;
5203 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5207 data->etag = g_strdup (g_file_info_get_etag (info));
5208 g_object_unref (info);
5211 g_input_stream_close_async (stream, 0,
5213 load_contents_close_callback, data);
5217 load_contents_read_callback (GObject *obj,
5218 GAsyncResult *read_res,
5221 GInputStream *stream = G_INPUT_STREAM (obj);
5222 LoadContentsData *data = user_data;
5223 GError *error = NULL;
5226 read_size = g_input_stream_read_finish (stream, read_res, &error);
5230 /* Error or EOF, close the file */
5231 data->error = error;
5232 g_input_stream_close_async (stream, 0,
5234 load_contents_close_callback, data);
5236 else if (read_size == 0)
5238 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5239 G_FILE_ATTRIBUTE_ETAG_VALUE,
5242 load_contents_fstat_callback,
5245 else if (read_size > 0)
5247 data->pos += read_size;
5249 g_byte_array_set_size (data->content,
5250 data->pos + GET_CONTENT_BLOCK_SIZE);
5253 if (data->read_more_callback &&
5254 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5255 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5256 G_FILE_ATTRIBUTE_ETAG_VALUE,
5259 load_contents_fstat_callback,
5262 g_input_stream_read_async (stream,
5263 data->content->data + data->pos,
5264 GET_CONTENT_BLOCK_SIZE,
5267 load_contents_read_callback,
5273 load_contents_open_callback (GObject *obj,
5274 GAsyncResult *open_res,
5277 GFile *file = G_FILE (obj);
5278 GFileInputStream *stream;
5279 LoadContentsData *data = user_data;
5280 GError *error = NULL;
5281 GSimpleAsyncResult *res;
5283 stream = g_file_read_finish (file, open_res, &error);
5287 g_byte_array_set_size (data->content,
5288 data->pos + GET_CONTENT_BLOCK_SIZE);
5289 g_input_stream_read_async (G_INPUT_STREAM (stream),
5290 data->content->data + data->pos,
5291 GET_CONTENT_BLOCK_SIZE,
5294 load_contents_read_callback,
5300 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5304 g_simple_async_result_complete (res);
5305 g_error_free (error);
5306 load_contents_data_free (data);
5307 g_object_unref (res);
5312 * g_file_load_partial_contents_async:
5313 * @file: input #GFile.
5314 * @cancellable: optional #GCancellable object, %NULL to ignore.
5315 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5316 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5317 * @user_data: the data to pass to the callback functions.
5319 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5320 * used to stop reading from the file when appropriate, else this function
5321 * will behave exactly as g_file_load_contents_async(). This operation
5322 * can be finished by g_file_load_partial_contents_finish().
5324 * Users of this function should be aware that @user_data is passed to
5325 * both the @read_more_callback and the @callback.
5327 * If @cancellable is not %NULL, then the operation can be cancelled by
5328 * triggering the cancellable object from another thread. If the operation
5329 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5332 g_file_load_partial_contents_async (GFile *file,
5333 GCancellable *cancellable,
5334 GFileReadMoreCallback read_more_callback,
5335 GAsyncReadyCallback callback,
5338 LoadContentsData *data;
5340 g_return_if_fail (G_IS_FILE (file));
5342 data = g_new0 (LoadContentsData, 1);
5345 data->cancellable = g_object_ref (cancellable);
5346 data->read_more_callback = read_more_callback;
5347 data->callback = callback;
5348 data->user_data = user_data;
5349 data->content = g_byte_array_new ();
5350 data->file = g_object_ref (file);
5352 g_file_read_async (file,
5355 load_contents_open_callback,
5360 * g_file_load_partial_contents_finish:
5361 * @file: input #GFile.
5362 * @res: a #GAsyncResult.
5363 * @contents: a location to place the contents of the file.
5364 * @length: a location to place the length of the contents of the file.
5365 * @etag_out: a location to place the current entity tag for the file.
5366 * @error: a #GError, or %NULL
5368 * Finishes an asynchronous partial load operation that was started
5369 * with g_file_load_partial_contents_async().
5371 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5372 * present, it will be set appropriately.
5375 g_file_load_partial_contents_finish (GFile *file,
5382 GSimpleAsyncResult *simple;
5383 LoadContentsData *data;
5385 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5386 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5387 g_return_val_if_fail (contents != NULL, FALSE);
5389 simple = G_SIMPLE_ASYNC_RESULT (res);
5391 if (g_simple_async_result_propagate_error (simple, error))
5394 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5396 data = g_simple_async_result_get_op_res_gpointer (simple);
5400 g_propagate_error (error, data->error);
5409 *length = data->pos;
5413 *etag_out = data->etag;
5417 /* Zero terminate */
5418 g_byte_array_set_size (data->content, data->pos + 1);
5419 data->content->data[data->pos] = 0;
5421 *contents = (char *)g_byte_array_free (data->content, FALSE);
5422 data->content = NULL;
5428 * g_file_load_contents_async:
5429 * @file: input #GFile.
5430 * @cancellable: optional #GCancellable object, %NULL to ignore.
5431 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5432 * @user_data: the data to pass to callback function
5434 * Starts an asynchronous load of the @file's contents.
5436 * For more details, see g_file_load_contents() which is
5437 * the synchronous version of this call.
5439 * When the load operation has completed, @callback will be called
5440 * with @user data. To finish the operation, call
5441 * g_file_load_contents_finish() with the #GAsyncResult returned by
5444 * If @cancellable is not %NULL, then the operation can be cancelled by
5445 * triggering the cancellable object from another thread. If the operation
5446 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5449 g_file_load_contents_async (GFile *file,
5450 GCancellable *cancellable,
5451 GAsyncReadyCallback callback,
5454 g_file_load_partial_contents_async (file,
5457 callback, user_data);
5461 * g_file_load_contents_finish:
5462 * @file: input #GFile.
5463 * @res: a #GAsyncResult.
5464 * @contents: a location to place the contents of the file.
5465 * @length: a location to place the length of the contents of the file.
5466 * @etag_out: a location to place the current entity tag for the file.
5467 * @error: a #GError, or %NULL
5469 * Finishes an asynchronous load of the @file's contents.
5470 * The contents are placed in @contents, and @length is set to the
5471 * size of the @contents string. If @etag_out is present, it will be
5472 * set to the new entity tag for the @file.
5474 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5475 * present, it will be set appropriately.
5478 g_file_load_contents_finish (GFile *file,
5485 return g_file_load_partial_contents_finish (file,
5494 * g_file_replace_contents:
5495 * @file: input #GFile.
5496 * @contents: a string containing the new contents for @file.
5497 * @length: the length of @contents in bytes.
5498 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5500 * @make_backup: %TRUE if a backup should be created.
5501 * @flags: a set of #GFileCreateFlags.
5502 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5503 * for the document. This should be freed with g_free() when no longer
5505 * @cancellable: optional #GCancellable object, %NULL to ignore.
5506 * @error: a #GError, or %NULL
5508 * Replaces the contents of @file with @contents of @length bytes.
5510 * If @etag is specified (not %NULL) any existing file must have that etag, or
5511 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5513 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5515 * If @cancellable is not %NULL, then the operation can be cancelled by
5516 * triggering the cancellable object from another thread. If the operation
5517 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5519 * The returned @new_etag can be used to verify that the file hasn't changed the
5520 * next time it is saved over.
5522 * Returns: %TRUE if successful. If an error
5523 * has occurred, this function will return %FALSE and set @error
5524 * appropriately if present.
5527 g_file_replace_contents (GFile *file,
5528 const char *contents,
5531 gboolean make_backup,
5532 GFileCreateFlags flags,
5534 GCancellable *cancellable,
5537 GFileOutputStream *out;
5538 gsize pos, remainder;
5542 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5543 g_return_val_if_fail (contents != NULL, FALSE);
5545 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5551 while (remainder > 0 &&
5552 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5554 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5562 if (remainder > 0 && res < 0)
5564 /* Ignore errors on close */
5565 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5566 g_object_unref (out);
5568 /* error is set already */
5572 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
5575 *new_etag = g_file_output_stream_get_etag (out);
5577 g_object_unref (out);
5585 GCancellable *cancellable;
5586 GAsyncReadyCallback callback;
5588 const char *content;
5592 } ReplaceContentsData;
5595 replace_contents_data_free (ReplaceContentsData *data)
5598 g_error_free (data->error);
5599 if (data->cancellable)
5600 g_object_unref (data->cancellable);
5601 g_object_unref (data->file);
5602 g_free (data->etag);
5607 replace_contents_close_callback (GObject *obj,
5608 GAsyncResult *close_res,
5611 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5612 ReplaceContentsData *data = user_data;
5613 GSimpleAsyncResult *res;
5615 /* Ignore errors here, we're only reading anyway */
5616 g_output_stream_close_finish (stream, close_res, NULL);
5617 g_object_unref (stream);
5619 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5621 res = g_simple_async_result_new (G_OBJECT (data->file),
5624 g_file_replace_contents_async);
5625 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5626 g_simple_async_result_complete (res);
5627 g_object_unref (res);
5631 replace_contents_write_callback (GObject *obj,
5632 GAsyncResult *read_res,
5635 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5636 ReplaceContentsData *data = user_data;
5637 GError *error = NULL;
5640 write_size = g_output_stream_write_finish (stream, read_res, &error);
5642 if (write_size <= 0)
5644 /* Error or EOF, close the file */
5646 data->error = error;
5647 g_output_stream_close_async (stream, 0,
5649 replace_contents_close_callback, data);
5651 else if (write_size > 0)
5653 data->pos += write_size;
5655 if (data->pos >= data->length)
5656 g_output_stream_close_async (stream, 0,
5658 replace_contents_close_callback, data);
5660 g_output_stream_write_async (stream,
5661 data->content + data->pos,
5662 data->length - data->pos,
5665 replace_contents_write_callback,
5671 replace_contents_open_callback (GObject *obj,
5672 GAsyncResult *open_res,
5675 GFile *file = G_FILE (obj);
5676 GFileOutputStream *stream;
5677 ReplaceContentsData *data = user_data;
5678 GError *error = NULL;
5679 GSimpleAsyncResult *res;
5681 stream = g_file_replace_finish (file, open_res, &error);
5685 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5686 data->content + data->pos,
5687 data->length - data->pos,
5690 replace_contents_write_callback,
5696 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5700 g_simple_async_result_complete (res);
5701 g_error_free (error);
5702 replace_contents_data_free (data);
5703 g_object_unref (res);
5708 * g_file_replace_contents_async:
5709 * @file: input #GFile.
5710 * @contents: string of contents to replace the file with.
5711 * @length: the length of @contents in bytes.
5712 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5713 * @make_backup: %TRUE if a backup should be created.
5714 * @flags: a set of #GFileCreateFlags.
5715 * @cancellable: optional #GCancellable object, %NULL to ignore.
5716 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5717 * @user_data: the data to pass to callback function
5719 * Starts an asynchronous replacement of @file with the given
5720 * @contents of @length bytes. @etag will replace the document's
5721 * current entity tag.
5723 * When this operation has completed, @callback will be called with
5724 * @user_user data, and the operation can be finalized with
5725 * g_file_replace_contents_finish().
5727 * If @cancellable is not %NULL, then the operation can be cancelled by
5728 * triggering the cancellable object from another thread. If the operation
5729 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5731 * If @make_backup is %TRUE, this function will attempt to
5732 * make a backup of @file.
5735 g_file_replace_contents_async (GFile *file,
5736 const char *contents,
5739 gboolean make_backup,
5740 GFileCreateFlags flags,
5741 GCancellable *cancellable,
5742 GAsyncReadyCallback callback,
5745 ReplaceContentsData *data;
5747 g_return_if_fail (G_IS_FILE (file));
5748 g_return_if_fail (contents != NULL);
5750 data = g_new0 (ReplaceContentsData, 1);
5753 data->cancellable = g_object_ref (cancellable);
5754 data->callback = callback;
5755 data->user_data = user_data;
5756 data->content = contents;
5757 data->length = length;
5759 data->file = g_object_ref (file);
5761 g_file_replace_async (file,
5767 replace_contents_open_callback,
5772 * g_file_replace_contents_finish:
5773 * @file: input #GFile.
5774 * @res: a #GAsyncResult.
5775 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5776 * for the document. This should be freed with g_free() when it is no
5778 * @error: a #GError, or %NULL
5780 * Finishes an asynchronous replace of the given @file. See
5781 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5782 * tag for the document, if present.
5784 * Returns: %TRUE on success, %FALSE on failure.
5787 g_file_replace_contents_finish (GFile *file,
5792 GSimpleAsyncResult *simple;
5793 ReplaceContentsData *data;
5795 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5796 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5798 simple = G_SIMPLE_ASYNC_RESULT (res);
5800 if (g_simple_async_result_propagate_error (simple, error))
5803 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5805 data = g_simple_async_result_get_op_res_gpointer (simple);
5809 g_propagate_error (error, data->error);
5817 *new_etag = data->etag;
5818 data->etag = NULL; /* Take ownership */
5824 #define __G_FILE_C__
5825 #include "gioaliasdef.c"