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);
1311 * g_file_find_enclosing_mount_async:
1313 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1315 * @cancellable: optional #GCancellable object, %NULL to ignore.
1316 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1317 * @user_data: the data to pass to callback function
1319 * Asynchronously gets the mount for the file.
1321 * For more details, see g_file_find_enclosing_mount() which is
1322 * the synchronous version of this call.
1324 * When the operation is finished, @callback will be called. You can then call
1325 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1328 g_file_find_enclosing_mount_async (GFile *file,
1330 GCancellable *cancellable,
1331 GAsyncReadyCallback callback,
1336 g_return_if_fail (G_IS_FILE (file));
1338 iface = G_FILE_GET_IFACE (file);
1339 (* iface->find_enclosing_mount_async) (file,
1347 * g_file_find_enclosing_mount_finish:
1349 * @res: a #GAsyncResult
1352 * Finishes an asynchronous find mount request.
1353 * See g_file_find_enclosing_mount_async().
1355 * Returns: #GMount for given @file or %NULL on error.
1358 g_file_find_enclosing_mount_finish (GFile *file,
1364 g_return_val_if_fail (G_IS_FILE (file), NULL);
1365 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1367 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1369 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1370 if (g_simple_async_result_propagate_error (simple, error))
1374 iface = G_FILE_GET_IFACE (file);
1375 return (* iface->find_enclosing_mount_finish) (file, res, error);
1381 * @file: #GFile to read.
1382 * @cancellable: a #GCancellable
1383 * @error: a #GError, or %NULL
1385 * Opens a file for reading. The result is a #GFileInputStream that
1386 * can be used to read the contents of the file.
1388 * If @cancellable is not %NULL, then the operation can be cancelled by
1389 * triggering the cancellable object from another thread. If the operation
1390 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1392 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1393 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1394 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1396 * Returns: #GFileInputStream or %NULL on error.
1399 g_file_read (GFile *file,
1400 GCancellable *cancellable,
1405 g_return_val_if_fail (G_IS_FILE (file), NULL);
1407 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1410 iface = G_FILE_GET_IFACE (file);
1412 if (iface->read_fn == NULL)
1414 g_set_error_literal (error, G_IO_ERROR,
1415 G_IO_ERROR_NOT_SUPPORTED,
1416 _("Operation not supported"));
1420 return (* iface->read_fn) (file, cancellable, error);
1425 * @file: input #GFile.
1426 * @flags: a set of #GFileCreateFlags.
1427 * @cancellable: optional #GCancellable object, %NULL to ignore.
1428 * @error: a #GError, or %NULL
1430 * Gets an output stream for appending data to the file. If
1431 * the file doesn't already exist it is created.
1433 * By default files created are generally readable by everyone,
1434 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1435 * will be made readable only to the current user, to the level that
1436 * is supported on the target filesystem.
1438 * If @cancellable is not %NULL, then the operation can be cancelled by
1439 * triggering the cancellable object from another thread. If the operation
1440 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1442 * Some file systems don't allow all file names, and may
1443 * return an %G_IO_ERROR_INVALID_FILENAME error.
1444 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1445 * returned. Other errors are possible too, and depend on what kind of
1446 * filesystem the file is on.
1448 * Returns: a #GFileOutputStream, or %NULL on error
1451 g_file_append_to (GFile *file,
1452 GFileCreateFlags flags,
1453 GCancellable *cancellable,
1458 g_return_val_if_fail (G_IS_FILE (file), NULL);
1460 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1463 iface = G_FILE_GET_IFACE (file);
1465 if (iface->append_to == NULL)
1467 g_set_error_literal (error, G_IO_ERROR,
1468 G_IO_ERROR_NOT_SUPPORTED,
1469 _("Operation not supported"));
1473 return (* iface->append_to) (file, flags, cancellable, error);
1478 * @file: input #GFile.
1479 * @flags: a set of #GFileCreateFlags.
1480 * @cancellable: optional #GCancellable object, %NULL to ignore.
1481 * @error: a #GError, or %NULL
1483 * Creates a new file and returns an output stream for writing to it.
1484 * The file must not already exist.
1486 * By default files created are generally readable by everyone,
1487 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1488 * will be made readable only to the current user, to the level that
1489 * is supported on the target filesystem.
1491 * If @cancellable is not %NULL, then the operation can be cancelled by
1492 * triggering the cancellable object from another thread. If the operation
1493 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1495 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1496 * error will be returned.
1497 * Some file systems don't allow all file names, and may
1498 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1499 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1500 * Other errors are possible too, and depend on what kind of
1501 * filesystem the file is on.
1503 * Returns: a #GFileOutputStream for the newly created file, or
1507 g_file_create (GFile *file,
1508 GFileCreateFlags flags,
1509 GCancellable *cancellable,
1514 g_return_val_if_fail (G_IS_FILE (file), NULL);
1516 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1519 iface = G_FILE_GET_IFACE (file);
1521 if (iface->create == NULL)
1523 g_set_error_literal (error, G_IO_ERROR,
1524 G_IO_ERROR_NOT_SUPPORTED,
1525 _("Operation not supported"));
1529 return (* iface->create) (file, flags, cancellable, error);
1534 * @file: input #GFile.
1535 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1536 * current #GFile, or #NULL to ignore.
1537 * @make_backup: %TRUE if a backup should be created.
1538 * @flags: a set of #GFileCreateFlags.
1539 * @cancellable: optional #GCancellable object, %NULL to ignore.
1540 * @error: a #GError, or %NULL
1542 * Returns an output stream for overwriting the file, possibly
1543 * creating a backup copy of the file first. If the file doesn't exist,
1544 * it will be created.
1546 * This will try to replace the file in the safest way possible so
1547 * that any errors during the writing will not affect an already
1548 * existing copy of the file. For instance, for local files it
1549 * may write to a temporary file and then atomically rename over
1550 * the destination when the stream is closed.
1552 * By default files created are generally readable by everyone,
1553 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1554 * will be made readable only to the current user, to the level that
1555 * is supported on the target filesystem.
1557 * If @cancellable is not %NULL, then the operation can be cancelled by
1558 * triggering the cancellable object from another thread. If the operation
1559 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1561 * If you pass in a non-#NULL @etag value, then this value is
1562 * compared to the current entity tag of the file, and if they differ
1563 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1564 * that the file has been changed since you last read it. You can get
1565 * the new etag from g_file_output_stream_get_etag() after you've
1566 * finished writing and closed the #GFileOutputStream. When you load
1567 * a new file you can use g_file_input_stream_query_info() to get
1568 * the etag of the file.
1570 * If @make_backup is %TRUE, this function will attempt to make a backup
1571 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1572 * error will be returned. If you want to replace anyway, try again with
1573 * @make_backup set to %FALSE.
1575 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1576 * and if the file is some other form of non-regular file then a
1577 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1578 * Some file systems don't allow all file names, and may
1579 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1580 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1581 * Other errors are possible too, and depend on what kind of
1582 * filesystem the file is on.
1584 * Returns: a #GFileOutputStream or %NULL on error.
1587 g_file_replace (GFile *file,
1589 gboolean make_backup,
1590 GFileCreateFlags flags,
1591 GCancellable *cancellable,
1596 g_return_val_if_fail (G_IS_FILE (file), NULL);
1598 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1601 iface = G_FILE_GET_IFACE (file);
1603 if (iface->replace == NULL)
1605 g_set_error_literal (error, G_IO_ERROR,
1606 G_IO_ERROR_NOT_SUPPORTED,
1607 _("Operation not supported"));
1612 /* Handle empty tag string as NULL in consistent way. */
1613 if (etag && *etag == 0)
1616 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1620 * g_file_read_async:
1621 * @file: input #GFile.
1622 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1624 * @cancellable: optional #GCancellable object, %NULL to ignore.
1625 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1626 * @user_data: the data to pass to callback function
1628 * Asynchronously opens @file for reading.
1630 * For more details, see g_file_read() which is
1631 * the synchronous version of this call.
1633 * When the operation is finished, @callback will be called. You can then call
1634 * g_file_read_finish() to get the result of the operation.
1637 g_file_read_async (GFile *file,
1639 GCancellable *cancellable,
1640 GAsyncReadyCallback callback,
1645 g_return_if_fail (G_IS_FILE (file));
1647 iface = G_FILE_GET_IFACE (file);
1648 (* iface->read_async) (file,
1656 * g_file_read_finish:
1657 * @file: input #GFile.
1658 * @res: a #GAsyncResult.
1659 * @error: a #GError, or %NULL
1661 * Finishes an asynchronous file read operation started with
1662 * g_file_read_async().
1664 * Returns: a #GFileInputStream or %NULL on error.
1667 g_file_read_finish (GFile *file,
1673 g_return_val_if_fail (G_IS_FILE (file), NULL);
1674 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1676 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1678 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1679 if (g_simple_async_result_propagate_error (simple, error))
1683 iface = G_FILE_GET_IFACE (file);
1684 return (* iface->read_finish) (file, res, error);
1688 * g_file_append_to_async:
1689 * @file: input #GFile.
1690 * @flags: a set of #GFileCreateFlags.
1691 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1693 * @cancellable: optional #GCancellable object, %NULL to ignore.
1694 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1695 * @user_data: the data to pass to callback function
1697 * Asynchronously opens @file for appending.
1699 * For more details, see g_file_append_to() which is
1700 * the synchronous version of this call.
1702 * When the operation is finished, @callback will be called. You can then call
1703 * g_file_append_to_finish() to get the result of the operation.
1706 g_file_append_to_async (GFile *file,
1707 GFileCreateFlags flags,
1709 GCancellable *cancellable,
1710 GAsyncReadyCallback callback,
1715 g_return_if_fail (G_IS_FILE (file));
1717 iface = G_FILE_GET_IFACE (file);
1718 (* iface->append_to_async) (file,
1727 * g_file_append_to_finish:
1728 * @file: input #GFile.
1729 * @res: #GAsyncResult
1730 * @error: a #GError, or %NULL
1732 * Finishes an asynchronous file append operation started with
1733 * g_file_append_to_async().
1735 * Returns: a valid #GFileOutputStream or %NULL on error.
1738 g_file_append_to_finish (GFile *file,
1744 g_return_val_if_fail (G_IS_FILE (file), NULL);
1745 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1747 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1749 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1750 if (g_simple_async_result_propagate_error (simple, error))
1754 iface = G_FILE_GET_IFACE (file);
1755 return (* iface->append_to_finish) (file, res, error);
1759 * g_file_create_async:
1760 * @file: input #GFile.
1761 * @flags: a set of #GFileCreateFlags.
1762 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1764 * @cancellable: optional #GCancellable object, %NULL to ignore.
1765 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1766 * @user_data: the data to pass to callback function
1768 * Asynchronously creates a new file and returns an output stream for writing to it.
1769 * The file must not already exist.
1771 * For more details, see g_file_create() which is
1772 * the synchronous version of this call.
1774 * When the operation is finished, @callback will be called. You can then call
1775 * g_file_create_finish() to get the result of the operation.
1778 g_file_create_async (GFile *file,
1779 GFileCreateFlags flags,
1781 GCancellable *cancellable,
1782 GAsyncReadyCallback callback,
1787 g_return_if_fail (G_IS_FILE (file));
1789 iface = G_FILE_GET_IFACE (file);
1790 (* iface->create_async) (file,
1799 * g_file_create_finish:
1800 * @file: input #GFile.
1801 * @res: a #GAsyncResult.
1802 * @error: a #GError, or %NULL
1804 * Finishes an asynchronous file create operation started with
1805 * g_file_create_async().
1807 * Returns: a #GFileOutputStream or %NULL on error.
1810 g_file_create_finish (GFile *file,
1816 g_return_val_if_fail (G_IS_FILE (file), NULL);
1817 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1819 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1821 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1822 if (g_simple_async_result_propagate_error (simple, error))
1826 iface = G_FILE_GET_IFACE (file);
1827 return (* iface->create_finish) (file, res, error);
1831 * g_file_replace_async:
1832 * @file: input #GFile.
1833 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1834 * current #GFile, or NULL to ignore.
1835 * @make_backup: %TRUE if a backup should be created.
1836 * @flags: a set of #GFileCreateFlags.
1837 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1839 * @cancellable: optional #GCancellable object, %NULL to ignore.
1840 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1841 * @user_data: the data to pass to callback function
1843 * Asynchronously overwrites the file, replacing the contents, possibly
1844 * creating a backup copy of the file first.
1846 * For more details, see g_file_replace() which is
1847 * the synchronous version of this call.
1849 * When the operation is finished, @callback will be called. You can then call
1850 * g_file_replace_finish() to get the result of the operation.
1853 g_file_replace_async (GFile *file,
1855 gboolean make_backup,
1856 GFileCreateFlags flags,
1858 GCancellable *cancellable,
1859 GAsyncReadyCallback callback,
1864 g_return_if_fail (G_IS_FILE (file));
1866 iface = G_FILE_GET_IFACE (file);
1867 (* iface->replace_async) (file,
1878 * g_file_replace_finish:
1879 * @file: input #GFile.
1880 * @res: a #GAsyncResult.
1881 * @error: a #GError, or %NULL
1883 * Finishes an asynchronous file replace operation started with
1884 * g_file_replace_async().
1886 * Returns: a #GFileOutputStream, or %NULL on error.
1889 g_file_replace_finish (GFile *file,
1895 g_return_val_if_fail (G_IS_FILE (file), NULL);
1896 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1898 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1900 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1901 if (g_simple_async_result_propagate_error (simple, error))
1905 iface = G_FILE_GET_IFACE (file);
1906 return (* iface->replace_finish) (file, res, error);
1910 copy_symlink (GFile *destination,
1911 GFileCopyFlags flags,
1912 GCancellable *cancellable,
1917 gboolean tried_delete;
1919 GFileType file_type;
1921 tried_delete = FALSE;
1925 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1927 /* Maybe it already existed, and we want to overwrite? */
1928 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1929 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1931 g_error_free (my_error);
1934 /* Don't overwrite if the destination is a directory */
1935 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1936 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1937 cancellable, &my_error);
1940 file_type = g_file_info_get_file_type (info);
1941 g_object_unref (info);
1943 if (file_type == G_FILE_TYPE_DIRECTORY)
1945 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1946 _("Can't copy over directory"));
1951 if (!g_file_delete (destination, cancellable, error))
1954 tried_delete = TRUE;
1958 g_propagate_error (error, my_error);
1965 static GInputStream *
1966 open_source_for_copy (GFile *source,
1968 GFileCopyFlags flags,
1969 GCancellable *cancellable,
1975 GFileType file_type;
1978 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1982 /* There was an error opening the source, try to set a good error for it: */
1984 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1986 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1987 * as that is less useful to the app. Better check for errors on the
1990 g_error_free (my_error);
1993 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1994 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1995 cancellable, &my_error);
1998 file_type = g_file_info_get_file_type (info);
1999 g_object_unref (info);
2001 if (flags & G_FILE_COPY_OVERWRITE)
2003 if (file_type == G_FILE_TYPE_DIRECTORY)
2005 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2006 _("Can't copy directory over directory"));
2009 /* continue to would_recurse error */
2013 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2014 _("Target file exists"));
2020 /* Error getting info from target, return that error
2021 * (except for NOT_FOUND, which is no error here)
2023 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
2025 g_propagate_error (error, my_error);
2028 g_error_free (my_error);
2031 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2032 _("Can't recursively copy directory"));
2036 g_propagate_error (error, my_error);
2041 should_copy (GFileAttributeInfo *info,
2045 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2046 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2050 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2051 GFileAttributeInfoList *namespaces,
2059 s = g_string_new ("");
2063 for (i = 0; i < attributes->n_infos; i++)
2065 if (should_copy (&attributes->infos[i], as_move))
2070 g_string_append_c (s, ',');
2072 g_string_append (s, attributes->infos[i].name);
2079 for (i = 0; i < namespaces->n_infos; i++)
2081 if (should_copy (&namespaces->infos[i], as_move))
2086 g_string_append_c (s, ',');
2088 g_string_append (s, namespaces->infos[i].name);
2089 g_string_append (s, ":*");
2094 return g_string_free (s, FALSE);
2098 * g_file_copy_attributes:
2099 * @source: a #GFile with attributes.
2100 * @destination: a #GFile to copy attributes to.
2101 * @flags: a set of #GFileCopyFlags.
2102 * @cancellable: optional #GCancellable object, %NULL to ignore.
2103 * @error: a #GError, %NULL to ignore.
2105 * Copies the file attributes from @source to @destination.
2107 * Normally only a subset of the file attributes are copied,
2108 * those that are copies in a normal file copy operation
2109 * (which for instance does not include e.g. mtime). However
2110 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2111 * all the metadata that is possible to copy is copied.
2113 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2116 g_file_copy_attributes (GFile *source,
2118 GFileCopyFlags flags,
2119 GCancellable *cancellable,
2122 GFileAttributeInfoList *attributes, *namespaces;
2123 char *attrs_to_read;
2127 gboolean source_nofollow_symlinks;
2129 as_move = flags & G_FILE_COPY_ALL_METADATA;
2130 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2132 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2133 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2134 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2136 if (attributes == NULL && namespaces == NULL)
2139 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2141 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2142 * we just don't copy it.
2144 info = g_file_query_info (source, attrs_to_read,
2145 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2149 g_free (attrs_to_read);
2154 res = g_file_set_attributes_from_info (destination,
2158 g_object_unref (info);
2161 g_file_attribute_info_list_unref (attributes);
2162 g_file_attribute_info_list_unref (namespaces);
2167 /* Closes the streams */
2169 copy_stream_with_progress (GInputStream *in,
2172 GCancellable *cancellable,
2173 GFileProgressCallback progress_callback,
2174 gpointer progress_callback_data,
2177 gssize n_read, n_written;
2178 goffset current_size;
2179 char buffer[1024*64], *p;
2185 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2186 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2190 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2191 total_size = g_file_info_get_size (info);
2192 g_object_unref (info);
2195 if (total_size == -1)
2197 info = g_file_query_info (source,
2198 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2199 G_FILE_QUERY_INFO_NONE,
2203 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2204 total_size = g_file_info_get_size (info);
2205 g_object_unref (info);
2209 if (total_size == -1)
2216 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2226 current_size += n_read;
2231 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2232 if (n_written == -1)
2239 n_read -= n_written;
2245 if (progress_callback)
2246 progress_callback (current_size, total_size, progress_callback_data);
2250 error = NULL; /* Ignore further errors */
2252 /* Make sure we send full copied size */
2253 if (progress_callback)
2254 progress_callback (current_size, total_size, progress_callback_data);
2256 /* Don't care about errors in source here */
2257 g_input_stream_close (in, cancellable, NULL);
2259 /* But write errors on close are bad! */
2260 if (!g_output_stream_close (out, cancellable, error))
2263 g_object_unref (in);
2264 g_object_unref (out);
2270 file_copy_fallback (GFile *source,
2272 GFileCopyFlags flags,
2273 GCancellable *cancellable,
2274 GFileProgressCallback progress_callback,
2275 gpointer progress_callback_data,
2283 /* Maybe copy the symlink? */
2284 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2286 info = g_file_query_info (source,
2287 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2288 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2294 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2295 (target = g_file_info_get_symlink_target (info)) != NULL)
2297 if (!copy_symlink (destination, flags, cancellable, target, error))
2299 g_object_unref (info);
2303 g_object_unref (info);
2307 g_object_unref (info);
2310 in = open_source_for_copy (source, destination, flags, cancellable, error);
2314 if (flags & G_FILE_COPY_OVERWRITE)
2316 out = (GOutputStream *)g_file_replace (destination,
2318 flags & G_FILE_COPY_BACKUP,
2320 cancellable, error);
2324 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2329 g_object_unref (in);
2333 if (!copy_stream_with_progress (in, out, source, cancellable,
2334 progress_callback, progress_callback_data,
2340 /* Ignore errors here. Failure to copy metadata is not a hard error */
2341 g_file_copy_attributes (source, destination,
2342 flags, cancellable, NULL);
2349 * @source: input #GFile.
2350 * @destination: destination #GFile
2351 * @flags: set of #GFileCopyFlags
2352 * @cancellable: optional #GCancellable object, %NULL to ignore.
2353 * @progress_callback: function to callback with progress information
2354 * @progress_callback_data: user data to pass to @progress_callback
2355 * @error: #GError to set on error, or %NULL
2357 * Copies the file @source to the location specified by @destination.
2358 * Can not handle recursive copies of directories.
2360 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2361 * existing @destination file is overwritten.
2363 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2364 * will be copied as symlinks, otherwise the target of the
2365 * @source symlink will be copied.
2367 * If @cancellable is not %NULL, then the operation can be cancelled by
2368 * triggering the cancellable object from another thread. If the operation
2369 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2371 * If @progress_callback is not %NULL, then the operation can be monitored by
2372 * setting this to a #GFileProgressCallback function. @progress_callback_data
2373 * will be passed to this function. It is guaranteed that this callback will
2374 * be called after all data has been transferred with the total number of bytes
2375 * copied during the operation.
2377 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2378 * error is returned, independent on the status of the @destination.
2380 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2381 * error G_IO_ERROR_EXISTS is returned.
2383 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2384 * error is returned. If trying to overwrite a directory with a directory the
2385 * G_IO_ERROR_WOULD_MERGE error is returned.
2387 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2388 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2391 * If you are interested in copying the #GFile object itself (not the on-disk
2392 * file), see g_file_dup().
2394 * Returns: %TRUE on success, %FALSE otherwise.
2397 g_file_copy (GFile *source,
2399 GFileCopyFlags flags,
2400 GCancellable *cancellable,
2401 GFileProgressCallback progress_callback,
2402 gpointer progress_callback_data,
2409 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2410 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2412 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2415 iface = G_FILE_GET_IFACE (destination);
2419 res = (* iface->copy) (source, destination,
2421 progress_callback, progress_callback_data,
2427 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2429 g_propagate_error (error, my_error);
2434 /* If the types are different, and the destination method failed
2435 also try the source method */
2436 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2438 iface = G_FILE_GET_IFACE (source);
2443 res = (* iface->copy) (source, destination,
2445 progress_callback, progress_callback_data,
2451 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2453 g_propagate_error (error, my_error);
2459 return file_copy_fallback (source, destination, flags, cancellable,
2460 progress_callback, progress_callback_data,
2465 * g_file_copy_async:
2466 * @source: input #GFile.
2467 * @destination: destination #GFile
2468 * @flags: set of #GFileCopyFlags
2469 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2471 * @cancellable: optional #GCancellable object, %NULL to ignore.
2472 * @progress_callback: function to callback with progress information
2473 * @progress_callback_data: user data to pass to @progress_callback
2474 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2475 * @user_data: the data to pass to callback function
2477 * Copies the file @source to the location specified by @destination
2478 * asynchronously. For details of the behaviour, see g_file_copy().
2480 * If @progress_callback is not %NULL, then that function that will be called
2481 * just like in g_file_copy(), however the callback will run in the main loop,
2482 * not in the thread that is doing the I/O operation.
2484 * When the operation is finished, @callback will be called. You can then call
2485 * g_file_copy_finish() to get the result of the operation.
2488 g_file_copy_async (GFile *source,
2490 GFileCopyFlags flags,
2492 GCancellable *cancellable,
2493 GFileProgressCallback progress_callback,
2494 gpointer progress_callback_data,
2495 GAsyncReadyCallback callback,
2500 g_return_if_fail (G_IS_FILE (source));
2501 g_return_if_fail (G_IS_FILE (destination));
2503 iface = G_FILE_GET_IFACE (source);
2504 (* iface->copy_async) (source,
2510 progress_callback_data,
2516 * g_file_copy_finish:
2517 * @file: input #GFile.
2518 * @res: a #GAsyncResult.
2519 * @error: a #GError, or %NULL
2521 * Finishes copying the file started with
2522 * g_file_copy_async().
2524 * Returns: a %TRUE on success, %FALSE on error.
2527 g_file_copy_finish (GFile *file,
2533 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2534 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2536 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2538 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2540 if (g_simple_async_result_propagate_error (simple, error))
2544 iface = G_FILE_GET_IFACE (file);
2545 return (* iface->copy_finish) (file, res, error);
2550 * @source: #GFile pointing to the source location.
2551 * @destination: #GFile pointing to the destination location.
2552 * @flags: set of #GFileCopyFlags.
2553 * @cancellable: optional #GCancellable object, %NULL to ignore.
2554 * @progress_callback: #GFileProgressCallback function for updates.
2555 * @progress_callback_data: gpointer to user data for the callback function.
2556 * @error: #GError for returning error conditions, or %NULL
2559 * Tries to move the file or directory @source to the location specified by @destination.
2560 * If native move operations are supported then this is used, otherwise a copy + delete
2561 * fallback is used. The native implementation may support moving directories (for instance
2562 * on moves inside the same filesystem), but the fallback code does not.
2564 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2565 * existing @destination file is overwritten.
2567 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2568 * will be copied as symlinks, otherwise the target of the
2569 * @source symlink will be copied.
2571 * If @cancellable is not %NULL, then the operation can be cancelled by
2572 * triggering the cancellable object from another thread. If the operation
2573 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2575 * If @progress_callback is not %NULL, then the operation can be monitored by
2576 * setting this to a #GFileProgressCallback function. @progress_callback_data
2577 * will be passed to this function. It is guaranteed that this callback will
2578 * be called after all data has been transferred with the total number of bytes
2579 * copied during the operation.
2581 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2582 * error is returned, independent on the status of the @destination.
2584 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2585 * error G_IO_ERROR_EXISTS is returned.
2587 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2588 * error is returned. If trying to overwrite a directory with a directory the
2589 * G_IO_ERROR_WOULD_MERGE error is returned.
2591 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2592 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2593 * may be returned (if the native move operation isn't available).
2595 * Returns: %TRUE on successful move, %FALSE otherwise.
2598 g_file_move (GFile *source,
2600 GFileCopyFlags flags,
2601 GCancellable *cancellable,
2602 GFileProgressCallback progress_callback,
2603 gpointer progress_callback_data,
2610 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2611 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2613 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2616 iface = G_FILE_GET_IFACE (destination);
2620 res = (* iface->move) (source, destination,
2622 progress_callback, progress_callback_data,
2628 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2630 g_propagate_error (error, my_error);
2635 /* If the types are different, and the destination method failed
2636 also try the source method */
2637 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2639 iface = G_FILE_GET_IFACE (source);
2644 res = (* iface->move) (source, destination,
2646 progress_callback, progress_callback_data,
2652 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2654 g_propagate_error (error, my_error);
2660 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2662 g_set_error_literal (error, G_IO_ERROR,
2663 G_IO_ERROR_NOT_SUPPORTED,
2664 _("Operation not supported"));
2668 flags |= G_FILE_COPY_ALL_METADATA;
2669 if (!g_file_copy (source, destination, flags, cancellable,
2670 progress_callback, progress_callback_data,
2674 return g_file_delete (source, cancellable, error);
2678 * g_file_make_directory
2679 * @file: input #GFile.
2680 * @cancellable: optional #GCancellable object, %NULL to ignore.
2681 * @error: a #GError, or %NULL
2683 * Creates a directory. Note that this will only create a child directory of
2684 * the immediate parent directory of the path or URI given by the #GFile. To
2685 * recursively create directories, see g_file_make_directory_with_parents().
2686 * This function will fail if the parent directory does not exist, setting
2687 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
2688 * directories, this function will fail, setting @error to
2689 * %G_IO_ERROR_NOT_SUPPORTED.
2691 * If @cancellable is not %NULL, then the operation can be cancelled by
2692 * triggering the cancellable object from another thread. If the operation
2693 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2695 * Returns: %TRUE on successful creation, %FALSE otherwise.
2698 g_file_make_directory (GFile *file,
2699 GCancellable *cancellable,
2704 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2706 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2709 iface = G_FILE_GET_IFACE (file);
2711 if (iface->make_directory == NULL)
2713 g_set_error_literal (error, G_IO_ERROR,
2714 G_IO_ERROR_NOT_SUPPORTED,
2715 _("Operation not supported"));
2719 return (* iface->make_directory) (file, cancellable, error);
2723 * g_file_make_directory_with_parents:
2724 * @file: input #GFile.
2725 * @cancellable: optional #GCancellable object, %NULL to ignore.
2726 * @error: a #GError, or %NULL
2728 * Creates a directory and any parent directories that may not exist similar to
2729 * 'mkdir -p'. If the file system does not support creating directories, this
2730 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED.
2732 * If @cancellable is not %NULL, then the operation can be cancelled by
2733 * triggering the cancellable object from another thread. If the operation
2734 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2736 * Returns: %TRUE if all directories have been successfully created, %FALSE
2742 g_file_make_directory_with_parents (GFile *file,
2743 GCancellable *cancellable,
2747 GFile *parent_file, *work_file;
2748 GList *list = NULL, *l;
2749 GError *my_error = NULL;
2751 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2754 result = g_file_make_directory (file, cancellable, &my_error);
2755 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
2758 g_propagate_error (error, my_error);
2764 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2766 g_clear_error (&my_error);
2768 parent_file = g_file_get_parent (work_file);
2769 if (parent_file == NULL)
2771 result = g_file_make_directory (parent_file, cancellable, &my_error);
2773 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2774 list = g_list_prepend (list, parent_file);
2776 work_file = parent_file;
2779 for (l = list; result && l; l = l->next)
2781 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
2785 while (list != NULL)
2787 g_object_unref ((GFile *) list->data);
2788 list = g_list_remove (list, list->data);
2793 g_propagate_error (error, my_error);
2797 return g_file_make_directory (file, cancellable, error);
2801 * g_file_make_symbolic_link:
2802 * @file: input #GFile.
2803 * @symlink_value: a string with the value of the new symlink.
2804 * @cancellable: optional #GCancellable object, %NULL to ignore.
2805 * @error: a #GError.
2807 * Creates a symbolic link.
2809 * If @cancellable is not %NULL, then the operation can be cancelled by
2810 * triggering the cancellable object from another thread. If the operation
2811 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2813 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2816 g_file_make_symbolic_link (GFile *file,
2817 const char *symlink_value,
2818 GCancellable *cancellable,
2823 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2824 g_return_val_if_fail (symlink_value != NULL, FALSE);
2826 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2829 if (*symlink_value == '\0')
2831 g_set_error_literal (error, G_IO_ERROR,
2832 G_IO_ERROR_INVALID_ARGUMENT,
2833 _("Invalid symlink value given"));
2837 iface = G_FILE_GET_IFACE (file);
2839 if (iface->make_symbolic_link == NULL)
2841 g_set_error_literal (error, G_IO_ERROR,
2842 G_IO_ERROR_NOT_SUPPORTED,
2843 _("Operation not supported"));
2847 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2852 * @file: input #GFile.
2853 * @cancellable: optional #GCancellable object, %NULL to ignore.
2854 * @error: a #GError, or %NULL
2856 * Deletes a file. If the @file is a directory, it will only be deleted if it
2859 * If @cancellable is not %NULL, then the operation can be cancelled by
2860 * triggering the cancellable object from another thread. If the operation
2861 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2863 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2866 g_file_delete (GFile *file,
2867 GCancellable *cancellable,
2872 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2874 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2877 iface = G_FILE_GET_IFACE (file);
2879 if (iface->delete_file == NULL)
2881 g_set_error_literal (error, G_IO_ERROR,
2882 G_IO_ERROR_NOT_SUPPORTED,
2883 _("Operation not supported"));
2887 return (* iface->delete_file) (file, cancellable, error);
2892 * @file: #GFile to send to trash.
2893 * @cancellable: optional #GCancellable object, %NULL to ignore.
2894 * @error: a #GError, or %NULL
2896 * Sends @file to the "Trashcan", if possible. This is similar to
2897 * deleting it, but the user can recover it before emptying the trashcan.
2898 * Not all file systems support trashing, so this call can return the
2899 * %G_IO_ERROR_NOT_SUPPORTED error.
2902 * If @cancellable is not %NULL, then the operation can be cancelled by
2903 * triggering the cancellable object from another thread. If the operation
2904 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2906 * Returns: %TRUE on successful trash, %FALSE otherwise.
2909 g_file_trash (GFile *file,
2910 GCancellable *cancellable,
2915 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2917 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2920 iface = G_FILE_GET_IFACE (file);
2922 if (iface->trash == NULL)
2924 g_set_error_literal (error,
2925 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2926 _("Trash not supported"));
2930 return (* iface->trash) (file, cancellable, error);
2934 * g_file_set_display_name:
2935 * @file: input #GFile.
2936 * @display_name: a string.
2937 * @cancellable: optional #GCancellable object, %NULL to ignore.
2938 * @error: a #GError, or %NULL
2940 * Renames @file to the specified display name.
2942 * The display name is converted from UTF8 to the correct encoding for the target
2943 * filesystem if possible and the @file is renamed to this.
2945 * If you want to implement a rename operation in the user interface the edit name
2946 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2947 * widget, and then the result after editing should be passed to g_file_set_display_name().
2949 * On success the resulting converted filename is returned.
2951 * If @cancellable is not %NULL, then the operation can be cancelled by
2952 * triggering the cancellable object from another thread. If the operation
2953 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2955 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2958 g_file_set_display_name (GFile *file,
2959 const char *display_name,
2960 GCancellable *cancellable,
2965 g_return_val_if_fail (G_IS_FILE (file), NULL);
2966 g_return_val_if_fail (display_name != NULL, NULL);
2968 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2972 G_IO_ERROR_INVALID_ARGUMENT,
2973 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2977 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2980 iface = G_FILE_GET_IFACE (file);
2982 return (* iface->set_display_name) (file, display_name, cancellable, error);
2986 * g_file_set_display_name_async:
2987 * @file: input #GFile.
2988 * @display_name: a string.
2989 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2991 * @cancellable: optional #GCancellable object, %NULL to ignore.
2992 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2993 * @user_data: the data to pass to callback function
2995 * Asynchronously sets the display name for a given #GFile.
2997 * For more details, see g_set_display_name() which is
2998 * the synchronous version of this call.
3000 * When the operation is finished, @callback will be called. You can then call
3001 * g_file_set_display_name_finish() to get the result of the operation.
3004 g_file_set_display_name_async (GFile *file,
3005 const char *display_name,
3007 GCancellable *cancellable,
3008 GAsyncReadyCallback callback,
3013 g_return_if_fail (G_IS_FILE (file));
3014 g_return_if_fail (display_name != NULL);
3016 iface = G_FILE_GET_IFACE (file);
3017 (* iface->set_display_name_async) (file,
3026 * g_file_set_display_name_finish:
3027 * @file: input #GFile.
3028 * @res: a #GAsyncResult.
3029 * @error: a #GError, or %NULL
3031 * Finishes setting a display name started with
3032 * g_file_set_display_name_async().
3034 * Returns: a #GFile or %NULL on error.
3037 g_file_set_display_name_finish (GFile *file,
3043 g_return_val_if_fail (G_IS_FILE (file), NULL);
3044 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3046 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3048 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3049 if (g_simple_async_result_propagate_error (simple, error))
3053 iface = G_FILE_GET_IFACE (file);
3054 return (* iface->set_display_name_finish) (file, res, error);
3058 * g_file_query_settable_attributes:
3059 * @file: input #GFile.
3060 * @cancellable: optional #GCancellable object, %NULL to ignore.
3061 * @error: a #GError, or %NULL
3063 * Obtain the list of settable attributes for the file.
3065 * Returns the type and full attribute name of all the attributes
3066 * that can be set on this file. This doesn't mean setting it will always
3067 * succeed though, you might get an access failure, or some specific
3068 * file may not support a specific attribute.
3070 * If @cancellable is not %NULL, then the operation can be cancelled by
3071 * triggering the cancellable object from another thread. If the operation
3072 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3074 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3075 * When you are done with it, release it with g_file_attribute_info_list_unref()
3077 GFileAttributeInfoList *
3078 g_file_query_settable_attributes (GFile *file,
3079 GCancellable *cancellable,
3084 GFileAttributeInfoList *list;
3086 g_return_val_if_fail (G_IS_FILE (file), NULL);
3088 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3091 iface = G_FILE_GET_IFACE (file);
3093 if (iface->query_settable_attributes == NULL)
3094 return g_file_attribute_info_list_new ();
3097 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3101 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3103 list = g_file_attribute_info_list_new ();
3104 g_error_free (my_error);
3107 g_propagate_error (error, my_error);
3114 * g_file_query_writable_namespaces:
3115 * @file: input #GFile.
3116 * @cancellable: optional #GCancellable object, %NULL to ignore.
3117 * @error: a #GError, or %NULL
3119 * Obtain the list of attribute namespaces where new attributes
3120 * can be created by a user. An example of this is extended
3121 * attributes (in the "xattr" namespace).
3123 * If @cancellable is not %NULL, then the operation can be cancelled by
3124 * triggering the cancellable object from another thread. If the operation
3125 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3127 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3128 * When you are done with it, release it with g_file_attribute_info_list_unref()
3130 GFileAttributeInfoList *
3131 g_file_query_writable_namespaces (GFile *file,
3132 GCancellable *cancellable,
3137 GFileAttributeInfoList *list;
3139 g_return_val_if_fail (G_IS_FILE (file), NULL);
3141 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3144 iface = G_FILE_GET_IFACE (file);
3146 if (iface->query_writable_namespaces == NULL)
3147 return g_file_attribute_info_list_new ();
3150 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3154 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3156 list = g_file_attribute_info_list_new ();
3157 g_error_free (my_error);
3160 g_propagate_error (error, my_error);
3167 * g_file_set_attribute:
3168 * @file: input #GFile.
3169 * @attribute: a string containing the attribute's name.
3170 * @type: The type of the attribute
3171 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3172 * @flags: a set of #GFileQueryInfoFlags.
3173 * @cancellable: optional #GCancellable object, %NULL to ignore.
3174 * @error: a #GError, or %NULL
3176 * Sets an attribute in the file with attribute name @attribute to @value.
3178 * If @cancellable is not %NULL, then the operation can be cancelled by
3179 * triggering the cancellable object from another thread. If the operation
3180 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3182 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3185 g_file_set_attribute (GFile *file,
3186 const char *attribute,
3187 GFileAttributeType type,
3189 GFileQueryInfoFlags flags,
3190 GCancellable *cancellable,
3195 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3196 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3198 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3201 iface = G_FILE_GET_IFACE (file);
3203 if (iface->set_attribute == NULL)
3205 g_set_error_literal (error, G_IO_ERROR,
3206 G_IO_ERROR_NOT_SUPPORTED,
3207 _("Operation not supported"));
3211 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3215 * g_file_set_attributes_from_info:
3216 * @file: input #GFile.
3217 * @info: a #GFileInfo.
3218 * @flags: #GFileQueryInfoFlags
3219 * @cancellable: optional #GCancellable object, %NULL to ignore.
3220 * @error: a #GError, or %NULL
3222 * Tries to set all attributes in the #GFileInfo on the target values,
3223 * not stopping on the first error.
3225 * If there is any error during this operation then @error will be set to
3226 * the first error. Error on particular fields are flagged by setting
3227 * the "status" field in the attribute value to
3228 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3231 * If @cancellable is not %NULL, then the operation can be cancelled by
3232 * triggering the cancellable object from another thread. If the operation
3233 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3235 * Returns: %TRUE if there was any error, %FALSE otherwise.
3238 g_file_set_attributes_from_info (GFile *file,
3240 GFileQueryInfoFlags flags,
3241 GCancellable *cancellable,
3246 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3247 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3249 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3252 g_file_info_clear_status (info);
3254 iface = G_FILE_GET_IFACE (file);
3256 return (* iface->set_attributes_from_info) (file,
3265 g_file_real_set_attributes_from_info (GFile *file,
3267 GFileQueryInfoFlags flags,
3268 GCancellable *cancellable,
3274 GFileAttributeValue *value;
3278 attributes = g_file_info_list_attributes (info, NULL);
3280 for (i = 0; attributes[i] != NULL; i++)
3282 value = _g_file_info_get_attribute_value (info, attributes[i]);
3284 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3287 if (!g_file_set_attribute (file, attributes[i],
3288 value->type, _g_file_attribute_value_peek_as_pointer (value),
3289 flags, cancellable, error))
3291 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3293 /* Don't set error multiple times */
3297 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3300 g_strfreev (attributes);
3306 * g_file_set_attributes_async:
3307 * @file: input #GFile.
3308 * @info: a #GFileInfo.
3309 * @flags: a #GFileQueryInfoFlags.
3310 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3312 * @cancellable: optional #GCancellable object, %NULL to ignore.
3313 * @callback: a #GAsyncReadyCallback.
3314 * @user_data: a #gpointer.
3316 * Asynchronously sets the attributes of @file with @info.
3318 * For more details, see g_file_set_attributes_from_info() which is
3319 * the synchronous version of this call.
3321 * When the operation is finished, @callback will be called. You can then call
3322 * g_file_set_attributes_finish() to get the result of the operation.
3325 g_file_set_attributes_async (GFile *file,
3327 GFileQueryInfoFlags flags,
3329 GCancellable *cancellable,
3330 GAsyncReadyCallback callback,
3335 g_return_if_fail (G_IS_FILE (file));
3336 g_return_if_fail (G_IS_FILE_INFO (info));
3338 iface = G_FILE_GET_IFACE (file);
3339 (* iface->set_attributes_async) (file,
3349 * g_file_set_attributes_finish:
3350 * @file: input #GFile.
3351 * @result: a #GAsyncResult.
3352 * @info: a #GFileInfo.
3353 * @error: a #GError, or %NULL
3355 * Finishes setting an attribute started in g_file_set_attributes_async().
3357 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3360 g_file_set_attributes_finish (GFile *file,
3361 GAsyncResult *result,
3367 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3368 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3370 /* No standard handling of errors here, as we must set info even
3373 iface = G_FILE_GET_IFACE (file);
3374 return (* iface->set_attributes_finish) (file, result, info, error);
3378 * g_file_set_attribute_string:
3379 * @file: input #GFile.
3380 * @attribute: a string containing the attribute's name.
3381 * @value: a string containing the attribute's value.
3382 * @flags: #GFileQueryInfoFlags.
3383 * @cancellable: optional #GCancellable object, %NULL to ignore.
3384 * @error: a #GError, or %NULL
3386 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3387 * If @attribute is of a different type, this operation will fail.
3389 * If @cancellable is not %NULL, then the operation can be cancelled by
3390 * triggering the cancellable object from another thread. If the operation
3391 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3393 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3396 g_file_set_attribute_string (GFile *file,
3397 const char *attribute,
3399 GFileQueryInfoFlags flags,
3400 GCancellable *cancellable,
3403 return g_file_set_attribute (file, attribute,
3404 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3405 flags, cancellable, error);
3409 * g_file_set_attribute_byte_string:
3410 * @file: input #GFile.
3411 * @attribute: a string containing the attribute's name.
3412 * @value: a string containing the attribute's new value.
3413 * @flags: a #GFileQueryInfoFlags.
3414 * @cancellable: optional #GCancellable object, %NULL to ignore.
3415 * @error: a #GError, or %NULL
3417 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3418 * If @attribute is of a different type, this operation will fail,
3421 * If @cancellable is not %NULL, then the operation can be cancelled by
3422 * triggering the cancellable object from another thread. If the operation
3423 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3425 * Returns: %TRUE if the @attribute was successfully set to @value
3426 * in the @file, %FALSE otherwise.
3429 g_file_set_attribute_byte_string (GFile *file,
3430 const char *attribute,
3432 GFileQueryInfoFlags flags,
3433 GCancellable *cancellable,
3436 return g_file_set_attribute (file, attribute,
3437 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3438 flags, cancellable, error);
3442 * g_file_set_attribute_uint32:
3443 * @file: input #GFile.
3444 * @attribute: a string containing the attribute's name.
3445 * @value: a #guint32 containing the attribute's new value.
3446 * @flags: a #GFileQueryInfoFlags.
3447 * @cancellable: optional #GCancellable object, %NULL to ignore.
3448 * @error: a #GError, or %NULL
3450 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3451 * If @attribute is of a different type, this operation will fail.
3453 * If @cancellable is not %NULL, then the operation can be cancelled by
3454 * triggering the cancellable object from another thread. If the operation
3455 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3457 * Returns: %TRUE if the @attribute was successfully set to @value
3458 * in the @file, %FALSE otherwise.
3461 g_file_set_attribute_uint32 (GFile *file,
3462 const char *attribute,
3464 GFileQueryInfoFlags flags,
3465 GCancellable *cancellable,
3468 return g_file_set_attribute (file, attribute,
3469 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3470 flags, cancellable, error);
3474 * g_file_set_attribute_int32:
3475 * @file: input #GFile.
3476 * @attribute: a string containing the attribute's name.
3477 * @value: a #gint32 containing the attribute's new value.
3478 * @flags: a #GFileQueryInfoFlags.
3479 * @cancellable: optional #GCancellable object, %NULL to ignore.
3480 * @error: a #GError, or %NULL
3482 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3483 * If @attribute is of a different type, this operation will fail.
3485 * If @cancellable is not %NULL, then the operation can be cancelled by
3486 * triggering the cancellable object from another thread. If the operation
3487 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3489 * Returns: %TRUE if the @attribute was successfully set to @value
3490 * in the @file, %FALSE otherwise.
3493 g_file_set_attribute_int32 (GFile *file,
3494 const char *attribute,
3496 GFileQueryInfoFlags flags,
3497 GCancellable *cancellable,
3500 return g_file_set_attribute (file, attribute,
3501 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3502 flags, cancellable, error);
3506 * g_file_set_attribute_uint64:
3507 * @file: input #GFile.
3508 * @attribute: a string containing the attribute's name.
3509 * @value: a #guint64 containing the attribute's new value.
3510 * @flags: a #GFileQueryInfoFlags.
3511 * @cancellable: optional #GCancellable object, %NULL to ignore.
3512 * @error: a #GError, or %NULL
3514 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3515 * If @attribute is of a different type, this operation will fail.
3517 * If @cancellable is not %NULL, then the operation can be cancelled by
3518 * triggering the cancellable object from another thread. If the operation
3519 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3521 * Returns: %TRUE if the @attribute was successfully set to @value
3522 * in the @file, %FALSE otherwise.
3525 g_file_set_attribute_uint64 (GFile *file,
3526 const char *attribute,
3528 GFileQueryInfoFlags flags,
3529 GCancellable *cancellable,
3532 return g_file_set_attribute (file, attribute,
3533 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3534 flags, cancellable, error);
3538 * g_file_set_attribute_int64:
3539 * @file: input #GFile.
3540 * @attribute: a string containing the attribute's name.
3541 * @value: a #guint64 containing the attribute's new value.
3542 * @flags: a #GFileQueryInfoFlags.
3543 * @cancellable: optional #GCancellable object, %NULL to ignore.
3544 * @error: a #GError, or %NULL
3546 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3547 * If @attribute is of a different type, this operation will fail.
3549 * If @cancellable is not %NULL, then the operation can be cancelled by
3550 * triggering the cancellable object from another thread. If the operation
3551 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3553 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3556 g_file_set_attribute_int64 (GFile *file,
3557 const char *attribute,
3559 GFileQueryInfoFlags flags,
3560 GCancellable *cancellable,
3563 return g_file_set_attribute (file, attribute,
3564 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3565 flags, cancellable, error);
3569 * g_file_mount_mountable:
3570 * @file: input #GFile.
3571 * @flags: flags affecting the operation
3572 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3573 * @cancellable: optional #GCancellable object, %NULL to ignore.
3574 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3575 * @user_data: the data to pass to callback function
3577 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3578 * Using @mount_operation, you can request callbacks when, for instance,
3579 * passwords are needed during authentication.
3581 * If @cancellable is not %NULL, then the operation can be cancelled by
3582 * triggering the cancellable object from another thread. If the operation
3583 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3585 * When the operation is finished, @callback will be called. You can then call
3586 * g_file_mount_mountable_finish() to get the result of the operation.
3589 g_file_mount_mountable (GFile *file,
3590 GMountMountFlags flags,
3591 GMountOperation *mount_operation,
3592 GCancellable *cancellable,
3593 GAsyncReadyCallback callback,
3598 g_return_if_fail (G_IS_FILE (file));
3600 iface = G_FILE_GET_IFACE (file);
3602 if (iface->mount_mountable == NULL)
3604 g_simple_async_report_error_in_idle (G_OBJECT (file),
3608 G_IO_ERROR_NOT_SUPPORTED,
3609 _("Operation not supported"));
3613 (* iface->mount_mountable) (file,
3622 * g_file_mount_mountable_finish:
3623 * @file: input #GFile.
3624 * @result: a #GAsyncResult.
3625 * @error: a #GError, or %NULL
3627 * Finishes a mount operation. See g_file_mount_mountable() for details.
3629 * Finish an asynchronous mount operation that was started
3630 * with g_file_mount_mountable().
3632 * Returns: a #GFile or %NULL on error.
3635 g_file_mount_mountable_finish (GFile *file,
3636 GAsyncResult *result,
3641 g_return_val_if_fail (G_IS_FILE (file), NULL);
3642 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3644 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3646 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3647 if (g_simple_async_result_propagate_error (simple, error))
3651 iface = G_FILE_GET_IFACE (file);
3652 return (* iface->mount_mountable_finish) (file, result, error);
3656 * g_file_unmount_mountable:
3657 * @file: input #GFile.
3658 * @flags: flags affecting the operation
3659 * @cancellable: optional #GCancellable object, %NULL to ignore.
3660 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3661 * @user_data: the data to pass to callback function
3663 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3665 * If @cancellable is not %NULL, then the operation can be cancelled by
3666 * triggering the cancellable object from another thread. If the operation
3667 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3669 * When the operation is finished, @callback will be called. You can then call
3670 * g_file_unmount_mountable_finish() to get the result of the operation.
3673 g_file_unmount_mountable (GFile *file,
3674 GMountUnmountFlags flags,
3675 GCancellable *cancellable,
3676 GAsyncReadyCallback callback,
3681 g_return_if_fail (G_IS_FILE (file));
3683 iface = G_FILE_GET_IFACE (file);
3685 if (iface->unmount_mountable == NULL)
3687 g_simple_async_report_error_in_idle (G_OBJECT (file),
3691 G_IO_ERROR_NOT_SUPPORTED,
3692 _("Operation not supported"));
3696 (* iface->unmount_mountable) (file,
3704 * g_file_unmount_mountable_finish:
3705 * @file: input #GFile.
3706 * @result: a #GAsyncResult.
3707 * @error: a #GError, or %NULL
3709 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3711 * Finish an asynchronous unmount operation that was started
3712 * with g_file_unmount_mountable().
3714 * Returns: %TRUE if the operation finished successfully. %FALSE
3718 g_file_unmount_mountable_finish (GFile *file,
3719 GAsyncResult *result,
3724 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3725 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3727 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3729 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3730 if (g_simple_async_result_propagate_error (simple, error))
3734 iface = G_FILE_GET_IFACE (file);
3735 return (* iface->unmount_mountable_finish) (file, result, error);
3739 * g_file_eject_mountable:
3740 * @file: input #GFile.
3741 * @flags: flags affecting the operation
3742 * @cancellable: optional #GCancellable object, %NULL to ignore.
3743 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3744 * @user_data: the data to pass to callback function
3746 * Starts an asynchronous eject on a mountable.
3747 * When this operation has completed, @callback will be called with
3748 * @user_user data, and the operation can be finalized with
3749 * g_file_eject_mountable_finish().
3751 * If @cancellable is not %NULL, then the operation can be cancelled by
3752 * triggering the cancellable object from another thread. If the operation
3753 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3756 g_file_eject_mountable (GFile *file,
3757 GMountUnmountFlags flags,
3758 GCancellable *cancellable,
3759 GAsyncReadyCallback callback,
3764 g_return_if_fail (G_IS_FILE (file));
3766 iface = G_FILE_GET_IFACE (file);
3768 if (iface->eject_mountable == NULL)
3770 g_simple_async_report_error_in_idle (G_OBJECT (file),
3774 G_IO_ERROR_NOT_SUPPORTED,
3775 _("Operation not supported"));
3779 (* iface->eject_mountable) (file,
3787 * g_file_eject_mountable_finish:
3788 * @file: input #GFile.
3789 * @result: a #GAsyncResult.
3790 * @error: a #GError, or %NULL
3792 * Finishes an asynchronous eject operation started by
3793 * g_file_eject_mountable().
3795 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3799 g_file_eject_mountable_finish (GFile *file,
3800 GAsyncResult *result,
3805 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3806 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3808 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3810 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3811 if (g_simple_async_result_propagate_error (simple, error))
3815 iface = G_FILE_GET_IFACE (file);
3816 return (* iface->eject_mountable_finish) (file, result, error);
3820 * g_file_monitor_directory:
3821 * @file: input #GFile.
3822 * @flags: a set of #GFileMonitorFlags.
3823 * @cancellable: optional #GCancellable object, %NULL to ignore.
3824 * @error: a #GError, or %NULL.
3826 * Obtains a directory monitor for the given file.
3827 * This may fail if directory monitoring is not supported.
3829 * If @cancellable is not %NULL, then the operation can be cancelled by
3830 * triggering the cancellable object from another thread. If the operation
3831 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3833 * Returns: a #GFileMonitor for the given @file,
3834 * or %NULL on error.
3837 g_file_monitor_directory (GFile *file,
3838 GFileMonitorFlags flags,
3839 GCancellable *cancellable,
3844 g_return_val_if_fail (G_IS_FILE (file), NULL);
3846 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3849 iface = G_FILE_GET_IFACE (file);
3851 if (iface->monitor_dir == NULL)
3853 g_set_error_literal (error, G_IO_ERROR,
3854 G_IO_ERROR_NOT_SUPPORTED,
3855 _("Operation not supported"));
3859 return (* iface->monitor_dir) (file, flags, cancellable, error);
3863 * g_file_monitor_file:
3864 * @file: input #GFile.
3865 * @flags: a set of #GFileMonitorFlags.
3866 * @cancellable: optional #GCancellable object, %NULL to ignore.
3867 * @error: a #GError, or %NULL.
3869 * Obtains a file monitor for the given file. If no file notification
3870 * mechanism exists, then regular polling of the file is used.
3872 * If @cancellable is not %NULL, then the operation can be cancelled by
3873 * triggering the cancellable object from another thread. If the operation
3874 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3876 * Returns: a #GFileMonitor for the given @file.
3879 g_file_monitor_file (GFile *file,
3880 GFileMonitorFlags flags,
3881 GCancellable *cancellable,
3885 GFileMonitor *monitor;
3887 g_return_val_if_fail (G_IS_FILE (file), NULL);
3889 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3892 iface = G_FILE_GET_IFACE (file);
3896 if (iface->monitor_file)
3897 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3899 /* Fallback to polling */
3900 if (monitor == NULL)
3901 monitor = _g_poll_file_monitor_new (file);
3908 * @file: input #GFile
3909 * @flags: a set of #GFileMonitorFlags
3910 * @cancellable: optional #GCancellable object, %NULL to ignore
3911 * @error: a #GError, or %NULL
3913 * Obtains a file or directory monitor for the given file, depending
3914 * on the type of the file.
3916 * If @cancellable is not %NULL, then the operation can be cancelled by
3917 * triggering the cancellable object from another thread. If the operation
3918 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3920 * Returns: a #GFileMonitor for the given @file.
3925 g_file_monitor (GFile *file,
3926 GFileMonitorFlags flags,
3927 GCancellable *cancellable,
3930 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
3931 return g_file_monitor_directory (file, flags, cancellable, error);
3933 return g_file_monitor_file (file, flags, cancellable, error);
3936 /********************************************
3937 * Default implementation of async ops *
3938 ********************************************/
3942 GFileQueryInfoFlags flags;
3944 } QueryInfoAsyncData;
3947 query_info_data_free (QueryInfoAsyncData *data)
3950 g_object_unref (data->info);
3951 g_free (data->attributes);
3956 query_info_async_thread (GSimpleAsyncResult *res,
3958 GCancellable *cancellable)
3960 GError *error = NULL;
3961 QueryInfoAsyncData *data;
3964 data = g_simple_async_result_get_op_res_gpointer (res);
3966 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3970 g_simple_async_result_set_from_error (res, error);
3971 g_error_free (error);
3978 g_file_real_query_info_async (GFile *file,
3979 const char *attributes,
3980 GFileQueryInfoFlags flags,
3982 GCancellable *cancellable,
3983 GAsyncReadyCallback callback,
3986 GSimpleAsyncResult *res;
3987 QueryInfoAsyncData *data;
3989 data = g_new0 (QueryInfoAsyncData, 1);
3990 data->attributes = g_strdup (attributes);
3991 data->flags = flags;
3993 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3994 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3996 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3997 g_object_unref (res);
4001 g_file_real_query_info_finish (GFile *file,
4005 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4006 QueryInfoAsyncData *data;
4008 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4010 data = g_simple_async_result_get_op_res_gpointer (simple);
4012 return g_object_ref (data->info);
4020 } QueryFilesystemInfoAsyncData;
4023 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4026 g_object_unref (data->info);
4027 g_free (data->attributes);
4032 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4034 GCancellable *cancellable)
4036 GError *error = NULL;
4037 QueryFilesystemInfoAsyncData *data;
4040 data = g_simple_async_result_get_op_res_gpointer (res);
4042 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4046 g_simple_async_result_set_from_error (res, error);
4047 g_error_free (error);
4054 g_file_real_query_filesystem_info_async (GFile *file,
4055 const char *attributes,
4057 GCancellable *cancellable,
4058 GAsyncReadyCallback callback,
4061 GSimpleAsyncResult *res;
4062 QueryFilesystemInfoAsyncData *data;
4064 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4065 data->attributes = g_strdup (attributes);
4067 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4068 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4070 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4071 g_object_unref (res);
4075 g_file_real_query_filesystem_info_finish (GFile *file,
4079 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4080 QueryFilesystemInfoAsyncData *data;
4082 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4084 data = g_simple_async_result_get_op_res_gpointer (simple);
4086 return g_object_ref (data->info);
4093 GFileQueryInfoFlags flags;
4094 GFileEnumerator *enumerator;
4095 } EnumerateChildrenAsyncData;
4098 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4100 if (data->enumerator)
4101 g_object_unref (data->enumerator);
4102 g_free (data->attributes);
4107 enumerate_children_async_thread (GSimpleAsyncResult *res,
4109 GCancellable *cancellable)
4111 GError *error = NULL;
4112 EnumerateChildrenAsyncData *data;
4113 GFileEnumerator *enumerator;
4115 data = g_simple_async_result_get_op_res_gpointer (res);
4117 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4119 if (enumerator == NULL)
4121 g_simple_async_result_set_from_error (res, error);
4122 g_error_free (error);
4125 data->enumerator = enumerator;
4129 g_file_real_enumerate_children_async (GFile *file,
4130 const char *attributes,
4131 GFileQueryInfoFlags flags,
4133 GCancellable *cancellable,
4134 GAsyncReadyCallback callback,
4137 GSimpleAsyncResult *res;
4138 EnumerateChildrenAsyncData *data;
4140 data = g_new0 (EnumerateChildrenAsyncData, 1);
4141 data->attributes = g_strdup (attributes);
4142 data->flags = flags;
4144 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4145 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4147 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4148 g_object_unref (res);
4151 static GFileEnumerator *
4152 g_file_real_enumerate_children_finish (GFile *file,
4156 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4157 EnumerateChildrenAsyncData *data;
4159 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4161 data = g_simple_async_result_get_op_res_gpointer (simple);
4162 if (data->enumerator)
4163 return g_object_ref (data->enumerator);
4169 open_read_async_thread (GSimpleAsyncResult *res,
4171 GCancellable *cancellable)
4174 GFileInputStream *stream;
4175 GError *error = NULL;
4177 iface = G_FILE_GET_IFACE (object);
4179 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4183 g_simple_async_result_set_from_error (res, error);
4184 g_error_free (error);
4187 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4191 g_file_real_read_async (GFile *file,
4193 GCancellable *cancellable,
4194 GAsyncReadyCallback callback,
4197 GSimpleAsyncResult *res;
4199 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4201 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4202 g_object_unref (res);
4205 static GFileInputStream *
4206 g_file_real_read_finish (GFile *file,
4210 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4213 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4215 op = g_simple_async_result_get_op_res_gpointer (simple);
4217 return g_object_ref (op);
4223 append_to_async_thread (GSimpleAsyncResult *res,
4225 GCancellable *cancellable)
4228 GFileCreateFlags *data;
4229 GFileOutputStream *stream;
4230 GError *error = NULL;
4232 iface = G_FILE_GET_IFACE (object);
4234 data = g_simple_async_result_get_op_res_gpointer (res);
4236 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4240 g_simple_async_result_set_from_error (res, error);
4241 g_error_free (error);
4244 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4248 g_file_real_append_to_async (GFile *file,
4249 GFileCreateFlags flags,
4251 GCancellable *cancellable,
4252 GAsyncReadyCallback callback,
4255 GFileCreateFlags *data;
4256 GSimpleAsyncResult *res;
4258 data = g_new0 (GFileCreateFlags, 1);
4261 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4262 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4264 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4265 g_object_unref (res);
4268 static GFileOutputStream *
4269 g_file_real_append_to_finish (GFile *file,
4273 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4276 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4278 op = g_simple_async_result_get_op_res_gpointer (simple);
4280 return g_object_ref (op);
4286 create_async_thread (GSimpleAsyncResult *res,
4288 GCancellable *cancellable)
4291 GFileCreateFlags *data;
4292 GFileOutputStream *stream;
4293 GError *error = NULL;
4295 iface = G_FILE_GET_IFACE (object);
4297 data = g_simple_async_result_get_op_res_gpointer (res);
4299 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4303 g_simple_async_result_set_from_error (res, error);
4304 g_error_free (error);
4307 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4311 g_file_real_create_async (GFile *file,
4312 GFileCreateFlags flags,
4314 GCancellable *cancellable,
4315 GAsyncReadyCallback callback,
4318 GFileCreateFlags *data;
4319 GSimpleAsyncResult *res;
4321 data = g_new0 (GFileCreateFlags, 1);
4324 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4325 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4327 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4328 g_object_unref (res);
4331 static GFileOutputStream *
4332 g_file_real_create_finish (GFile *file,
4336 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4339 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4341 op = g_simple_async_result_get_op_res_gpointer (simple);
4343 return g_object_ref (op);
4349 GFileOutputStream *stream;
4351 gboolean make_backup;
4352 GFileCreateFlags flags;
4356 replace_async_data_free (ReplaceAsyncData *data)
4359 g_object_unref (data->stream);
4360 g_free (data->etag);
4365 replace_async_thread (GSimpleAsyncResult *res,
4367 GCancellable *cancellable)
4370 GFileOutputStream *stream;
4371 GError *error = NULL;
4372 ReplaceAsyncData *data;
4374 iface = G_FILE_GET_IFACE (object);
4376 data = g_simple_async_result_get_op_res_gpointer (res);
4378 stream = iface->replace (G_FILE (object),
4387 g_simple_async_result_set_from_error (res, error);
4388 g_error_free (error);
4391 data->stream = stream;
4395 g_file_real_replace_async (GFile *file,
4397 gboolean make_backup,
4398 GFileCreateFlags flags,
4400 GCancellable *cancellable,
4401 GAsyncReadyCallback callback,
4404 GSimpleAsyncResult *res;
4405 ReplaceAsyncData *data;
4407 data = g_new0 (ReplaceAsyncData, 1);
4408 data->etag = g_strdup (etag);
4409 data->make_backup = make_backup;
4410 data->flags = flags;
4412 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4413 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4415 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4416 g_object_unref (res);
4419 static GFileOutputStream *
4420 g_file_real_replace_finish (GFile *file,
4424 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4425 ReplaceAsyncData *data;
4427 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4429 data = g_simple_async_result_get_op_res_gpointer (simple);
4431 return g_object_ref (data->stream);
4439 } SetDisplayNameAsyncData;
4442 set_display_name_data_free (SetDisplayNameAsyncData *data)
4444 g_free (data->name);
4446 g_object_unref (data->file);
4451 set_display_name_async_thread (GSimpleAsyncResult *res,
4453 GCancellable *cancellable)
4455 GError *error = NULL;
4456 SetDisplayNameAsyncData *data;
4459 data = g_simple_async_result_get_op_res_gpointer (res);
4461 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4465 g_simple_async_result_set_from_error (res, error);
4466 g_error_free (error);
4473 g_file_real_set_display_name_async (GFile *file,
4474 const char *display_name,
4476 GCancellable *cancellable,
4477 GAsyncReadyCallback callback,
4480 GSimpleAsyncResult *res;
4481 SetDisplayNameAsyncData *data;
4483 data = g_new0 (SetDisplayNameAsyncData, 1);
4484 data->name = g_strdup (display_name);
4486 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4487 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4489 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4490 g_object_unref (res);
4494 g_file_real_set_display_name_finish (GFile *file,
4498 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4499 SetDisplayNameAsyncData *data;
4501 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4503 data = g_simple_async_result_get_op_res_gpointer (simple);
4505 return g_object_ref (data->file);
4511 GFileQueryInfoFlags flags;
4518 set_info_data_free (SetInfoAsyncData *data)
4521 g_object_unref (data->info);
4523 g_error_free (data->error);
4528 set_info_async_thread (GSimpleAsyncResult *res,
4530 GCancellable *cancellable)
4532 SetInfoAsyncData *data;
4534 data = g_simple_async_result_get_op_res_gpointer (res);
4537 data->res = g_file_set_attributes_from_info (G_FILE (object),
4545 g_file_real_set_attributes_async (GFile *file,
4547 GFileQueryInfoFlags flags,
4549 GCancellable *cancellable,
4550 GAsyncReadyCallback callback,
4553 GSimpleAsyncResult *res;
4554 SetInfoAsyncData *data;
4556 data = g_new0 (SetInfoAsyncData, 1);
4557 data->info = g_file_info_dup (info);
4558 data->flags = flags;
4560 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4561 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4563 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4564 g_object_unref (res);
4568 g_file_real_set_attributes_finish (GFile *file,
4573 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4574 SetInfoAsyncData *data;
4576 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4578 data = g_simple_async_result_get_op_res_gpointer (simple);
4581 *info = g_object_ref (data->info);
4583 if (error != NULL && data->error)
4584 *error = g_error_copy (data->error);
4590 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4592 GCancellable *cancellable)
4594 GError *error = NULL;
4597 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4601 g_simple_async_result_set_from_error (res, error);
4602 g_error_free (error);
4605 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4609 g_file_real_find_enclosing_mount_async (GFile *file,
4611 GCancellable *cancellable,
4612 GAsyncReadyCallback callback,
4615 GSimpleAsyncResult *res;
4617 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4619 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4620 g_object_unref (res);
4624 g_file_real_find_enclosing_mount_finish (GFile *file,
4628 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4631 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4633 mount = g_simple_async_result_get_op_res_gpointer (simple);
4634 return g_object_ref (mount);
4641 GFileCopyFlags flags;
4642 GFileProgressCallback progress_cb;
4643 gpointer progress_cb_data;
4644 GIOSchedulerJob *job;
4648 copy_async_data_free (CopyAsyncData *data)
4650 g_object_unref (data->source);
4651 g_object_unref (data->destination);
4656 CopyAsyncData *data;
4657 goffset current_num_bytes;
4658 goffset total_num_bytes;
4662 copy_async_progress_in_main (gpointer user_data)
4664 ProgressData *progress = user_data;
4665 CopyAsyncData *data = progress->data;
4667 data->progress_cb (progress->current_num_bytes,
4668 progress->total_num_bytes,
4669 data->progress_cb_data);
4675 mainloop_barrier (gpointer user_data)
4677 /* Does nothing, but ensures all queued idles before
4684 copy_async_progress_callback (goffset current_num_bytes,
4685 goffset total_num_bytes,
4688 CopyAsyncData *data = user_data;
4689 ProgressData *progress;
4691 progress = g_new (ProgressData, 1);
4692 progress->data = data;
4693 progress->current_num_bytes = current_num_bytes;
4694 progress->total_num_bytes = total_num_bytes;
4696 g_io_scheduler_job_send_to_mainloop_async (data->job,
4697 copy_async_progress_in_main,
4703 copy_async_thread (GIOSchedulerJob *job,
4704 GCancellable *cancellable,
4707 GSimpleAsyncResult *res;
4708 CopyAsyncData *data;
4713 data = g_simple_async_result_get_op_res_gpointer (res);
4717 result = g_file_copy (data->source,
4721 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4725 /* Ensure all progress callbacks are done running in main thread */
4726 if (data->progress_cb != NULL)
4727 g_io_scheduler_job_send_to_mainloop (job,
4733 g_simple_async_result_set_from_error (res, error);
4734 g_error_free (error);
4737 g_simple_async_result_complete_in_idle (res);
4743 g_file_real_copy_async (GFile *source,
4745 GFileCopyFlags flags,
4747 GCancellable *cancellable,
4748 GFileProgressCallback progress_callback,
4749 gpointer progress_callback_data,
4750 GAsyncReadyCallback callback,
4753 GSimpleAsyncResult *res;
4754 CopyAsyncData *data;
4756 data = g_new0 (CopyAsyncData, 1);
4757 data->source = g_object_ref (source);
4758 data->destination = g_object_ref (destination);
4759 data->flags = flags;
4760 data->progress_cb = progress_callback;
4761 data->progress_cb_data = progress_callback_data;
4763 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4764 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4766 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4770 g_file_real_copy_finish (GFile *file,
4774 /* Error handled in g_file_copy_finish() */
4779 /********************************************
4780 * Default VFS operations *
4781 ********************************************/
4784 * g_file_new_for_path:
4785 * @path: a string containing a relative or absolute path.
4787 * Constructs a #GFile for a given path. This operation never
4788 * fails, but the returned object might not support any I/O
4789 * operation if @path is malformed.
4791 * Returns: a new #GFile for the given @path.
4794 g_file_new_for_path (const char *path)
4796 g_return_val_if_fail (path != NULL, NULL);
4798 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4802 * g_file_new_for_uri:
4803 * @uri: a string containing a URI.
4805 * Constructs a #GFile for a given URI. This operation never
4806 * fails, but the returned object might not support any I/O
4807 * operation if @uri is malformed or if the uri type is
4810 * Returns: a #GFile for the given @uri.
4813 g_file_new_for_uri (const char *uri)
4815 g_return_val_if_fail (uri != NULL, NULL);
4817 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4821 * g_file_parse_name:
4822 * @parse_name: a file name or path to be parsed.
4824 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4825 * This operation never fails, but the returned object might not support any I/O
4826 * operation if the @parse_name cannot be parsed.
4828 * Returns: a new #GFile.
4831 g_file_parse_name (const char *parse_name)
4833 g_return_val_if_fail (parse_name != NULL, NULL);
4835 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4839 is_valid_scheme_character (char c)
4841 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4844 /* Following RFC 2396, valid schemes are built like:
4845 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
4848 has_valid_scheme (const char *uri)
4854 if (!g_ascii_isalpha (*p))
4859 } while (is_valid_scheme_character (*p));
4865 * g_file_new_for_commandline_arg:
4866 * @arg: a command line string.
4868 * Creates a #GFile with the given argument from the command line. The value of
4869 * @arg can be either a URI, an absolute path or a relative path resolved
4870 * relative to the current working directory.
4871 * This operation never fails, but the returned object might not support any
4872 * I/O operation if @arg points to a malformed path.
4874 * Returns: a new #GFile.
4877 g_file_new_for_commandline_arg (const char *arg)
4883 g_return_val_if_fail (arg != NULL, NULL);
4885 if (g_path_is_absolute (arg))
4886 return g_file_new_for_path (arg);
4888 if (has_valid_scheme (arg))
4889 return g_file_new_for_uri (arg);
4891 current_dir = g_get_current_dir ();
4892 filename = g_build_filename (current_dir, arg, NULL);
4893 g_free (current_dir);
4895 file = g_file_new_for_path (filename);
4902 * g_file_mount_enclosing_volume:
4903 * @location: input #GFile.
4904 * @flags: flags affecting the operation
4905 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4906 * @cancellable: optional #GCancellable object, %NULL to ignore.
4907 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4908 * @user_data: the data to pass to callback function
4910 * Starts a @mount_operation, mounting the volume that contains the file @location.
4912 * When this operation has completed, @callback will be called with
4913 * @user_user data, and the operation can be finalized with
4914 * g_file_mount_enclosing_volume_finish().
4916 * If @cancellable is not %NULL, then the operation can be cancelled by
4917 * triggering the cancellable object from another thread. If the operation
4918 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4921 g_file_mount_enclosing_volume (GFile *location,
4922 GMountMountFlags flags,
4923 GMountOperation *mount_operation,
4924 GCancellable *cancellable,
4925 GAsyncReadyCallback callback,
4930 g_return_if_fail (G_IS_FILE (location));
4932 iface = G_FILE_GET_IFACE (location);
4934 if (iface->mount_enclosing_volume == NULL)
4936 g_simple_async_report_error_in_idle (G_OBJECT (location),
4937 callback, user_data,
4938 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4939 _("volume doesn't implement mount"));
4944 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4949 * g_file_mount_enclosing_volume_finish:
4950 * @location: input #GFile.
4951 * @result: a #GAsyncResult.
4952 * @error: a #GError, or %NULL
4954 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4956 * Returns: %TRUE if successful. If an error
4957 * has occurred, this function will return %FALSE and set @error
4958 * appropriately if present.
4961 g_file_mount_enclosing_volume_finish (GFile *location,
4962 GAsyncResult *result,
4967 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4968 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4970 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4972 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4973 if (g_simple_async_result_propagate_error (simple, error))
4977 iface = G_FILE_GET_IFACE (location);
4979 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4982 /********************************************
4983 * Utility functions *
4984 ********************************************/
4987 * g_file_query_default_handler:
4988 * @file: a #GFile to open.
4989 * @cancellable: optional #GCancellable object, %NULL to ignore.
4990 * @error: a #GError, or %NULL
4992 * Returns the #GAppInfo that is registered as the default
4993 * application to handle the file specified by @file.
4995 * If @cancellable is not %NULL, then the operation can be cancelled by
4996 * triggering the cancellable object from another thread. If the operation
4997 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4999 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
5000 * When you are done with it, release it with g_object_unref()
5003 g_file_query_default_handler (GFile *file,
5004 GCancellable *cancellable,
5008 const char *content_type;
5013 uri_scheme = g_file_get_uri_scheme (file);
5014 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
5015 g_free (uri_scheme);
5017 if (appinfo != NULL)
5020 info = g_file_query_info (file,
5021 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
5030 content_type = g_file_info_get_content_type (info);
5033 /* Don't use is_native(), as we want to support fuse paths if availible */
5034 path = g_file_get_path (file);
5035 appinfo = g_app_info_get_default_for_type (content_type,
5040 g_object_unref (info);
5042 if (appinfo != NULL)
5045 g_set_error_literal (error, G_IO_ERROR,
5046 G_IO_ERROR_NOT_SUPPORTED,
5047 _("No application is registered as handling this file"));
5053 #define GET_CONTENT_BLOCK_SIZE 8192
5056 * g_file_load_contents:
5057 * @file: input #GFile.
5058 * @cancellable: optional #GCancellable object, %NULL to ignore.
5059 * @contents: a location to place the contents of the file.
5060 * @length: a location to place the length of the contents of the file,
5061 * or %NULL if the length is not needed
5062 * @etag_out: a location to place the current entity tag for the file,
5063 * or %NULL if the entity tag is not needed
5064 * @error: a #GError, or %NULL
5066 * Loads the content of the file into memory. The data is always
5067 * zero-terminated, but this is not included in the resultant @length.
5068 * The returned @content should be freed with g_free() when no longer
5071 * If @cancellable is not %NULL, then the operation can be cancelled by
5072 * triggering the cancellable object from another thread. If the operation
5073 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5075 * Returns: %TRUE if the @file's contents were successfully loaded.
5076 * %FALSE if there were errors.
5079 g_file_load_contents (GFile *file,
5080 GCancellable *cancellable,
5086 GFileInputStream *in;
5087 GByteArray *content;
5092 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5093 g_return_val_if_fail (contents != NULL, FALSE);
5095 in = g_file_read (file, cancellable, error);
5099 content = g_byte_array_new ();
5102 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5103 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
5104 content->data + pos,
5105 GET_CONTENT_BLOCK_SIZE,
5106 cancellable, error)) > 0)
5109 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5116 info = g_file_input_stream_query_info (in,
5117 G_FILE_ATTRIBUTE_ETAG_VALUE,
5122 *etag_out = g_strdup (g_file_info_get_etag (info));
5123 g_object_unref (info);
5127 /* Ignore errors on close */
5128 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
5129 g_object_unref (in);
5133 /* error is set already */
5134 g_byte_array_free (content, TRUE);
5141 /* Zero terminate (we got an extra byte allocated for this */
5142 content->data[pos] = 0;
5144 *contents = (char *)g_byte_array_free (content, FALSE);
5152 GCancellable *cancellable;
5153 GFileReadMoreCallback read_more_callback;
5154 GAsyncReadyCallback callback;
5156 GByteArray *content;
5163 load_contents_data_free (LoadContentsData *data)
5166 g_error_free (data->error);
5167 if (data->cancellable)
5168 g_object_unref (data->cancellable);
5170 g_byte_array_free (data->content, TRUE);
5171 g_free (data->etag);
5172 g_object_unref (data->file);
5177 load_contents_close_callback (GObject *obj,
5178 GAsyncResult *close_res,
5181 GInputStream *stream = G_INPUT_STREAM (obj);
5182 LoadContentsData *data = user_data;
5183 GSimpleAsyncResult *res;
5185 /* Ignore errors here, we're only reading anyway */
5186 g_input_stream_close_finish (stream, close_res, NULL);
5187 g_object_unref (stream);
5189 res = g_simple_async_result_new (G_OBJECT (data->file),
5192 g_file_load_contents_async);
5193 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5194 g_simple_async_result_complete (res);
5195 g_object_unref (res);
5199 load_contents_fstat_callback (GObject *obj,
5200 GAsyncResult *stat_res,
5203 GInputStream *stream = G_INPUT_STREAM (obj);
5204 LoadContentsData *data = user_data;
5207 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5211 data->etag = g_strdup (g_file_info_get_etag (info));
5212 g_object_unref (info);
5215 g_input_stream_close_async (stream, 0,
5217 load_contents_close_callback, data);
5221 load_contents_read_callback (GObject *obj,
5222 GAsyncResult *read_res,
5225 GInputStream *stream = G_INPUT_STREAM (obj);
5226 LoadContentsData *data = user_data;
5227 GError *error = NULL;
5230 read_size = g_input_stream_read_finish (stream, read_res, &error);
5234 /* Error or EOF, close the file */
5235 data->error = error;
5236 g_input_stream_close_async (stream, 0,
5238 load_contents_close_callback, data);
5240 else if (read_size == 0)
5242 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5243 G_FILE_ATTRIBUTE_ETAG_VALUE,
5246 load_contents_fstat_callback,
5249 else if (read_size > 0)
5251 data->pos += read_size;
5253 g_byte_array_set_size (data->content,
5254 data->pos + GET_CONTENT_BLOCK_SIZE);
5257 if (data->read_more_callback &&
5258 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5259 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5260 G_FILE_ATTRIBUTE_ETAG_VALUE,
5263 load_contents_fstat_callback,
5266 g_input_stream_read_async (stream,
5267 data->content->data + data->pos,
5268 GET_CONTENT_BLOCK_SIZE,
5271 load_contents_read_callback,
5277 load_contents_open_callback (GObject *obj,
5278 GAsyncResult *open_res,
5281 GFile *file = G_FILE (obj);
5282 GFileInputStream *stream;
5283 LoadContentsData *data = user_data;
5284 GError *error = NULL;
5285 GSimpleAsyncResult *res;
5287 stream = g_file_read_finish (file, open_res, &error);
5291 g_byte_array_set_size (data->content,
5292 data->pos + GET_CONTENT_BLOCK_SIZE);
5293 g_input_stream_read_async (G_INPUT_STREAM (stream),
5294 data->content->data + data->pos,
5295 GET_CONTENT_BLOCK_SIZE,
5298 load_contents_read_callback,
5304 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5308 g_simple_async_result_complete (res);
5309 g_error_free (error);
5310 load_contents_data_free (data);
5311 g_object_unref (res);
5316 * g_file_load_partial_contents_async:
5317 * @file: input #GFile.
5318 * @cancellable: optional #GCancellable object, %NULL to ignore.
5319 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5320 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5321 * @user_data: the data to pass to the callback functions.
5323 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5324 * used to stop reading from the file when appropriate, else this function
5325 * will behave exactly as g_file_load_contents_async(). This operation
5326 * can be finished by g_file_load_partial_contents_finish().
5328 * Users of this function should be aware that @user_data is passed to
5329 * both the @read_more_callback and the @callback.
5331 * If @cancellable is not %NULL, then the operation can be cancelled by
5332 * triggering the cancellable object from another thread. If the operation
5333 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5336 g_file_load_partial_contents_async (GFile *file,
5337 GCancellable *cancellable,
5338 GFileReadMoreCallback read_more_callback,
5339 GAsyncReadyCallback callback,
5342 LoadContentsData *data;
5344 g_return_if_fail (G_IS_FILE (file));
5346 data = g_new0 (LoadContentsData, 1);
5349 data->cancellable = g_object_ref (cancellable);
5350 data->read_more_callback = read_more_callback;
5351 data->callback = callback;
5352 data->user_data = user_data;
5353 data->content = g_byte_array_new ();
5354 data->file = g_object_ref (file);
5356 g_file_read_async (file,
5359 load_contents_open_callback,
5364 * g_file_load_partial_contents_finish:
5365 * @file: input #GFile.
5366 * @res: a #GAsyncResult.
5367 * @contents: a location to place the contents of the file.
5368 * @length: a location to place the length of the contents of the file,
5369 * or %NULL if the length is not needed
5370 * @etag_out: a location to place the current entity tag for the file,
5371 * or %NULL if the entity tag is not needed
5372 * @error: a #GError, or %NULL
5374 * Finishes an asynchronous partial load operation that was started
5375 * with g_file_load_partial_contents_async(). The data is always
5376 * zero-terminated, but this is not included in the resultant @length.
5377 * The returned @content should be freed with g_free() when no longer
5380 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5381 * present, it will be set appropriately.
5384 g_file_load_partial_contents_finish (GFile *file,
5391 GSimpleAsyncResult *simple;
5392 LoadContentsData *data;
5394 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5395 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5396 g_return_val_if_fail (contents != NULL, FALSE);
5398 simple = G_SIMPLE_ASYNC_RESULT (res);
5400 if (g_simple_async_result_propagate_error (simple, error))
5403 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5405 data = g_simple_async_result_get_op_res_gpointer (simple);
5409 g_propagate_error (error, data->error);
5418 *length = data->pos;
5422 *etag_out = data->etag;
5426 /* Zero terminate */
5427 g_byte_array_set_size (data->content, data->pos + 1);
5428 data->content->data[data->pos] = 0;
5430 *contents = (char *)g_byte_array_free (data->content, FALSE);
5431 data->content = NULL;
5437 * g_file_load_contents_async:
5438 * @file: input #GFile.
5439 * @cancellable: optional #GCancellable object, %NULL to ignore.
5440 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5441 * @user_data: the data to pass to callback function
5443 * Starts an asynchronous load of the @file's contents.
5445 * For more details, see g_file_load_contents() which is
5446 * the synchronous version of this call.
5448 * When the load operation has completed, @callback will be called
5449 * with @user data. To finish the operation, call
5450 * g_file_load_contents_finish() with the #GAsyncResult returned by
5453 * If @cancellable is not %NULL, then the operation can be cancelled by
5454 * triggering the cancellable object from another thread. If the operation
5455 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5458 g_file_load_contents_async (GFile *file,
5459 GCancellable *cancellable,
5460 GAsyncReadyCallback callback,
5463 g_file_load_partial_contents_async (file,
5466 callback, user_data);
5470 * g_file_load_contents_finish:
5471 * @file: input #GFile.
5472 * @res: a #GAsyncResult.
5473 * @contents: a location to place the contents of the file.
5474 * @length: a location to place the length of the contents of the file,
5475 * or %NULL if the length is not needed
5476 * @etag_out: a location to place the current entity tag for the file,
5477 * or %NULL if the entity tag is not needed
5478 * @error: a #GError, or %NULL
5480 * Finishes an asynchronous load of the @file's contents.
5481 * The contents are placed in @contents, and @length is set to the
5482 * size of the @contents string. The @content should be freed with
5483 * g_free() when no longer needed. If @etag_out is present, it will be
5484 * set to the new entity tag for the @file.
5486 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5487 * present, it will be set appropriately.
5490 g_file_load_contents_finish (GFile *file,
5497 return g_file_load_partial_contents_finish (file,
5506 * g_file_replace_contents:
5507 * @file: input #GFile.
5508 * @contents: a string containing the new contents for @file.
5509 * @length: the length of @contents in bytes.
5510 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5511 * for the document, or %NULL
5512 * @make_backup: %TRUE if a backup should be created.
5513 * @flags: a set of #GFileCreateFlags.
5514 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5515 * for the document. This should be freed with g_free() when no longer
5517 * @cancellable: optional #GCancellable object, %NULL to ignore.
5518 * @error: a #GError, or %NULL
5520 * Replaces the contents of @file with @contents of @length bytes.
5522 * If @etag is specified (not %NULL) any existing file must have that etag, or
5523 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5525 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5527 * If @cancellable is not %NULL, then the operation can be cancelled by
5528 * triggering the cancellable object from another thread. If the operation
5529 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5531 * The returned @new_etag can be used to verify that the file hasn't changed the
5532 * next time it is saved over.
5534 * Returns: %TRUE if successful. If an error
5535 * has occurred, this function will return %FALSE and set @error
5536 * appropriately if present.
5539 g_file_replace_contents (GFile *file,
5540 const char *contents,
5543 gboolean make_backup,
5544 GFileCreateFlags flags,
5546 GCancellable *cancellable,
5549 GFileOutputStream *out;
5550 gsize pos, remainder;
5554 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5555 g_return_val_if_fail (contents != NULL, FALSE);
5557 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5563 while (remainder > 0 &&
5564 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5566 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5574 if (remainder > 0 && res < 0)
5576 /* Ignore errors on close */
5577 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5578 g_object_unref (out);
5580 /* error is set already */
5584 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
5587 *new_etag = g_file_output_stream_get_etag (out);
5589 g_object_unref (out);
5597 GCancellable *cancellable;
5598 GAsyncReadyCallback callback;
5600 const char *content;
5604 } ReplaceContentsData;
5607 replace_contents_data_free (ReplaceContentsData *data)
5610 g_error_free (data->error);
5611 if (data->cancellable)
5612 g_object_unref (data->cancellable);
5613 g_object_unref (data->file);
5614 g_free (data->etag);
5619 replace_contents_close_callback (GObject *obj,
5620 GAsyncResult *close_res,
5623 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5624 ReplaceContentsData *data = user_data;
5625 GSimpleAsyncResult *res;
5627 /* Ignore errors here, we're only reading anyway */
5628 g_output_stream_close_finish (stream, close_res, NULL);
5629 g_object_unref (stream);
5631 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5633 res = g_simple_async_result_new (G_OBJECT (data->file),
5636 g_file_replace_contents_async);
5637 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5638 g_simple_async_result_complete (res);
5639 g_object_unref (res);
5643 replace_contents_write_callback (GObject *obj,
5644 GAsyncResult *read_res,
5647 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5648 ReplaceContentsData *data = user_data;
5649 GError *error = NULL;
5652 write_size = g_output_stream_write_finish (stream, read_res, &error);
5654 if (write_size <= 0)
5656 /* Error or EOF, close the file */
5658 data->error = error;
5659 g_output_stream_close_async (stream, 0,
5661 replace_contents_close_callback, data);
5663 else if (write_size > 0)
5665 data->pos += write_size;
5667 if (data->pos >= data->length)
5668 g_output_stream_close_async (stream, 0,
5670 replace_contents_close_callback, data);
5672 g_output_stream_write_async (stream,
5673 data->content + data->pos,
5674 data->length - data->pos,
5677 replace_contents_write_callback,
5683 replace_contents_open_callback (GObject *obj,
5684 GAsyncResult *open_res,
5687 GFile *file = G_FILE (obj);
5688 GFileOutputStream *stream;
5689 ReplaceContentsData *data = user_data;
5690 GError *error = NULL;
5691 GSimpleAsyncResult *res;
5693 stream = g_file_replace_finish (file, open_res, &error);
5697 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5698 data->content + data->pos,
5699 data->length - data->pos,
5702 replace_contents_write_callback,
5708 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5712 g_simple_async_result_complete (res);
5713 g_error_free (error);
5714 replace_contents_data_free (data);
5715 g_object_unref (res);
5720 * g_file_replace_contents_async:
5721 * @file: input #GFile.
5722 * @contents: string of contents to replace the file with.
5723 * @length: the length of @contents in bytes.
5724 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
5725 * @make_backup: %TRUE if a backup should be created.
5726 * @flags: a set of #GFileCreateFlags.
5727 * @cancellable: optional #GCancellable object, %NULL to ignore.
5728 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5729 * @user_data: the data to pass to callback function
5731 * Starts an asynchronous replacement of @file with the given
5732 * @contents of @length bytes. @etag will replace the document's
5733 * current entity tag.
5735 * When this operation has completed, @callback will be called with
5736 * @user_user data, and the operation can be finalized with
5737 * g_file_replace_contents_finish().
5739 * If @cancellable is not %NULL, then the operation can be cancelled by
5740 * triggering the cancellable object from another thread. If the operation
5741 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5743 * If @make_backup is %TRUE, this function will attempt to
5744 * make a backup of @file.
5747 g_file_replace_contents_async (GFile *file,
5748 const char *contents,
5751 gboolean make_backup,
5752 GFileCreateFlags flags,
5753 GCancellable *cancellable,
5754 GAsyncReadyCallback callback,
5757 ReplaceContentsData *data;
5759 g_return_if_fail (G_IS_FILE (file));
5760 g_return_if_fail (contents != NULL);
5762 data = g_new0 (ReplaceContentsData, 1);
5765 data->cancellable = g_object_ref (cancellable);
5766 data->callback = callback;
5767 data->user_data = user_data;
5768 data->content = contents;
5769 data->length = length;
5771 data->file = g_object_ref (file);
5773 g_file_replace_async (file,
5779 replace_contents_open_callback,
5784 * g_file_replace_contents_finish:
5785 * @file: input #GFile.
5786 * @res: a #GAsyncResult.
5787 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5788 * for the document. This should be freed with g_free() when it is no
5789 * longer needed, or %NULL
5790 * @error: a #GError, or %NULL
5792 * Finishes an asynchronous replace of the given @file. See
5793 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5794 * tag for the document, if present.
5796 * Returns: %TRUE on success, %FALSE on failure.
5799 g_file_replace_contents_finish (GFile *file,
5804 GSimpleAsyncResult *simple;
5805 ReplaceContentsData *data;
5807 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5808 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5810 simple = G_SIMPLE_ASYNC_RESULT (res);
5812 if (g_simple_async_result_propagate_error (simple, error))
5815 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5817 data = g_simple_async_result_get_op_res_gpointer (simple);
5821 g_propagate_error (error, data->error);
5829 *new_etag = data->etag;
5830 data->etag = NULL; /* Take ownership */
5836 #define __G_FILE_C__
5837 #include "gioaliasdef.c"