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.
609 g_file_get_parent (GFile *file)
613 g_return_val_if_fail (G_IS_FILE (file), NULL);
615 iface = G_FILE_GET_IFACE (file);
617 return (* iface->get_parent) (file);
622 * @file: input #GFile.
623 * @name: string containing the child's basename.
625 * Gets a child of @file with basename equal to @name.
627 * Note that the file with that specific name might not exist, but
628 * you can still have a #GFile that points to it. You can use this
629 * for instance to create that file.
631 * This call does no blocking i/o.
633 * Returns: a #GFile to a child specified by @name.
636 g_file_get_child (GFile *file,
639 g_return_val_if_fail (G_IS_FILE (file), NULL);
640 g_return_val_if_fail (name != NULL, NULL);
642 return g_file_resolve_relative_path (file, name);
646 * g_file_get_child_for_display_name:
647 * @file: input #GFile.
648 * @display_name: string to a possible child.
651 * Gets the child of @file for a given @display_name (i.e. a UTF8
652 * version of the name). If this function fails, it returns %NULL and @error will be
653 * set. This is very useful when constructing a GFile for a new file
654 * and the user entered the filename in the user interface, for instance
655 * when you select a directory and type a filename in the file selector.
657 * This call does no blocking i/o.
659 * Returns: a #GFile to the specified child, or
660 * %NULL if the display name couldn't be converted.
663 g_file_get_child_for_display_name (GFile *file,
664 const char *display_name,
669 g_return_val_if_fail (G_IS_FILE (file), NULL);
670 g_return_val_if_fail (display_name != NULL, NULL);
672 iface = G_FILE_GET_IFACE (file);
674 return (* iface->get_child_for_display_name) (file, display_name, error);
679 * @file: input #GFile.
680 * @prefix: input #GFile.
682 * Checks whether @file has the prefix specified by @prefix. In other word, if the
683 * names of inital elements of @file<!-- -->s pathname match @prefix.
685 * This call does no i/o, as it works purely on names. As such it can sometimes
686 * return %FALSE even if @file is inside a @prefix (from a filesystem point of view),
687 * because the prefix of @file is an alias of @prefix.
689 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix. %FALSE otherwise.
692 g_file_has_prefix (GFile *file,
697 g_return_val_if_fail (G_IS_FILE (file), FALSE);
698 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
700 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
703 iface = G_FILE_GET_IFACE (file);
705 /* The vtable function differs in arg order since we're
706 using the old contains_file call */
707 return (* iface->prefix_matches) (prefix, file);
711 * g_file_get_relative_path:
712 * @parent: input #GFile.
713 * @descendant: input #GFile.
715 * Gets the path for @descendant relative to @parent.
717 * This call does no blocking i/o.
719 * Returns: string with the relative path from @descendant
720 * to @parent, or %NULL if @descendant doesn't have @parent as prefix. The returned string should be freed with
721 * g_free() when no longer needed.
724 g_file_get_relative_path (GFile *parent,
729 g_return_val_if_fail (G_IS_FILE (parent), NULL);
730 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
732 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
735 iface = G_FILE_GET_IFACE (parent);
737 return (* iface->get_relative_path) (parent, descendant);
741 * g_file_resolve_relative_path:
742 * @file: input #GFile.
743 * @relative_path: a given relative path string.
745 * Resolves a relative path for @file to an absolute path.
747 * This call does no blocking i/o.
749 * Returns: #GFile to the resolved path. %NULL if @relative_path
750 * is %NULL or if @file is invalid.
753 g_file_resolve_relative_path (GFile *file,
754 const char *relative_path)
758 g_return_val_if_fail (G_IS_FILE (file), NULL);
759 g_return_val_if_fail (relative_path != NULL, NULL);
761 iface = G_FILE_GET_IFACE (file);
763 return (* iface->resolve_relative_path) (file, relative_path);
767 * g_file_enumerate_children:
768 * @file: input #GFile.
769 * @attributes: an attribute query string.
770 * @flags: a set of #GFileQueryInfoFlags.
771 * @cancellable: optional #GCancellable object, %NULL to ignore.
772 * @error: #GError for error reporting.
774 * Gets the requested information about the files in a directory. The result
775 * is a #GFileEnumerator object that will give out #GFileInfo objects for
776 * all the files in the directory.
778 * The @attribute value is a string that specifies the file attributes that
779 * should be gathered. It is not an error if it's not possible to read a particular
780 * requested attribute from a file - it just won't be set. @attribute should
781 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
782 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
783 * namespace. An example attribute query be "standard::*,owner::user".
784 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
786 * If @cancellable is not %NULL, then the operation can be cancelled by
787 * triggering the cancellable object from another thread. If the operation
788 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
790 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
791 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
792 * Other errors are possible too.
794 * Returns: A #GFileEnumerator if successful, %NULL on error.
797 g_file_enumerate_children (GFile *file,
798 const char *attributes,
799 GFileQueryInfoFlags flags,
800 GCancellable *cancellable,
806 g_return_val_if_fail (G_IS_FILE (file), NULL);
808 if (g_cancellable_set_error_if_cancelled (cancellable, error))
811 iface = G_FILE_GET_IFACE (file);
813 if (iface->enumerate_children == NULL)
815 g_set_error_literal (error, G_IO_ERROR,
816 G_IO_ERROR_NOT_SUPPORTED,
817 _("Operation not supported"));
821 return (* iface->enumerate_children) (file, attributes, flags,
826 * g_file_enumerate_children_async:
827 * @file: input #GFile.
828 * @attributes: an attribute query string.
829 * @flags: a set of #GFileQueryInfoFlags.
830 * @io_priority: the <link linkend="io-priority">I/O priority</link>
832 * @cancellable: optional #GCancellable object, %NULL to ignore.
833 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
834 * @user_data: the data to pass to callback function
836 * Asynchronously gets the requested information about the files in a directory. The result
837 * is a #GFileEnumerator object that will give out #GFileInfo objects for
838 * all the files in the directory.
840 * For more details, see g_file_enumerate_children() which is
841 * the synchronous version of this call.
843 * When the operation is finished, @callback will be called. You can then call
844 * g_file_enumerate_children_finish() to get the result of the operation.
847 g_file_enumerate_children_async (GFile *file,
848 const char *attributes,
849 GFileQueryInfoFlags flags,
851 GCancellable *cancellable,
852 GAsyncReadyCallback callback,
857 g_return_if_fail (G_IS_FILE (file));
859 iface = G_FILE_GET_IFACE (file);
860 (* iface->enumerate_children_async) (file,
870 * g_file_enumerate_children_finish:
871 * @file: input #GFile.
872 * @res: a #GAsyncResult.
875 * Finishes an async enumerate children operation.
876 * See g_file_enumerate_children_async().
878 * Returns: a #GFileEnumerator or %NULL if an error occurred.
881 g_file_enumerate_children_finish (GFile *file,
887 g_return_val_if_fail (G_IS_FILE (file), NULL);
888 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
890 if (G_IS_SIMPLE_ASYNC_RESULT (res))
892 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
893 if (g_simple_async_result_propagate_error (simple, error))
897 iface = G_FILE_GET_IFACE (file);
898 return (* iface->enumerate_children_finish) (file, res, error);
902 * g_file_query_exists:
903 * @file: input #GFile.
904 * @cancellable: optional #GCancellable object, %NULL to ignore.
906 * Utility function to check if a particular file exists. This is
907 * implemented using g_file_query_info() and as such does blocking I/O.
909 * Note that in many cases it is racy to first check for file existence
910 * and then execute something based on the outcome of that, because the
911 * file might have been created or removed in between the operations. The
912 * general approach to handling that is to not check, but just do the
913 * operation and handle the errors as they come.
915 * As an example of race-free checking, take the case of reading a file, and
916 * if it doesn't exist, creating it. There are two racy versions: read it, and
917 * on error create it; and: check if it exists, if not create it. These
918 * can both result in two processes creating the file (with perhaps a partially
919 * written file as the result). The correct approach is to always try to create
920 * the file with g_file_create() which will either atomically create the file
921 * or fail with a G_IO_ERROR_EXISTS error.
923 * However, in many cases an existence check is useful in a user
924 * interface, for instance to make a menu item sensitive/insensitive, so that
925 * you don't have to fool users that something is possible and then just show
926 * and error dialog. If you do this, you should make sure to also handle the
927 * errors that can happen due to races when you execute the operation.
929 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
932 g_file_query_exists (GFile *file,
933 GCancellable *cancellable)
937 g_return_val_if_fail (G_IS_FILE(file), FALSE);
939 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
940 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
943 g_object_unref (info);
951 * g_file_query_file_type:
952 * @file: input #GFile.
953 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
954 * @cancellable: optional #GCancellable object, %NULL to ignore.
956 * Utility function to inspect the #GFileType of a file. This is
957 * implemented using g_file_query_info() and as such does blocking I/O.
959 * The primary use case of this method is to check if a file is a regular file,
960 * directory, or symlink.
962 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
968 g_file_query_file_type (GFile *file,
969 GFileQueryInfoFlags flags,
970 GCancellable *cancellable)
975 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
976 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
980 file_type = g_file_info_get_file_type (info);
981 g_object_unref (info);
984 file_type = G_FILE_TYPE_UNKNOWN;
991 * @file: input #GFile.
992 * @attributes: an attribute query string.
993 * @flags: a set of #GFileQueryInfoFlags.
994 * @cancellable: optional #GCancellable object, %NULL to ignore.
997 * Gets the requested information about specified @file. The result
998 * is a #GFileInfo object that contains key-value attributes (such as
999 * the type or size of the file).
1001 * The @attribute value is a string that specifies the file attributes that
1002 * should be gathered. It is not an error if it's not possible to read a particular
1003 * requested attribute from a file - it just won't be set. @attribute should
1004 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1005 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1006 * namespace. An example attribute query be "standard::*,owner::user".
1007 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1009 * If @cancellable is not %NULL, then the operation can be cancelled by
1010 * triggering the cancellable object from another thread. If the operation
1011 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1013 * For symlinks, normally the information about the target of the
1014 * symlink is returned, rather than information about the symlink itself.
1015 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1016 * information about the symlink itself will be returned. Also, for symlinks
1017 * that point to non-existing files the information about the symlink itself
1020 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1021 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1023 * Returns: a #GFileInfo for the given @file, or %NULL on error.
1026 g_file_query_info (GFile *file,
1027 const char *attributes,
1028 GFileQueryInfoFlags flags,
1029 GCancellable *cancellable,
1034 g_return_val_if_fail (G_IS_FILE (file), NULL);
1036 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1039 iface = G_FILE_GET_IFACE (file);
1041 if (iface->query_info == NULL)
1043 g_set_error_literal (error, G_IO_ERROR,
1044 G_IO_ERROR_NOT_SUPPORTED,
1045 _("Operation not supported"));
1049 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1053 * g_file_query_info_async:
1054 * @file: input #GFile.
1055 * @attributes: an attribute query string.
1056 * @flags: a set of #GFileQueryInfoFlags.
1057 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1059 * @cancellable: optional #GCancellable object, %NULL to ignore.
1060 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1061 * @user_data: the data to pass to callback function
1063 * Asynchronously gets the requested information about specified @file. The result
1064 * is a #GFileInfo object that contains key-value attributes (such as type or size
1067 * For more details, see g_file_query_info() which is
1068 * the synchronous version of this call.
1070 * When the operation is finished, @callback will be called. You can then call
1071 * g_file_query_info_finish() to get the result of the operation.
1074 g_file_query_info_async (GFile *file,
1075 const char *attributes,
1076 GFileQueryInfoFlags flags,
1078 GCancellable *cancellable,
1079 GAsyncReadyCallback callback,
1084 g_return_if_fail (G_IS_FILE (file));
1086 iface = G_FILE_GET_IFACE (file);
1087 (* iface->query_info_async) (file,
1097 * g_file_query_info_finish:
1098 * @file: input #GFile.
1099 * @res: a #GAsyncResult.
1100 * @error: a #GError.
1102 * Finishes an asynchronous file info query.
1103 * See g_file_query_info_async().
1105 * Returns: #GFileInfo for given @file or %NULL on error.
1108 g_file_query_info_finish (GFile *file,
1114 g_return_val_if_fail (G_IS_FILE (file), NULL);
1115 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1117 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1119 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1120 if (g_simple_async_result_propagate_error (simple, error))
1124 iface = G_FILE_GET_IFACE (file);
1125 return (* iface->query_info_finish) (file, res, error);
1129 * g_file_query_filesystem_info:
1130 * @file: input #GFile.
1131 * @attributes: an attribute query string.
1132 * @cancellable: optional #GCancellable object, %NULL to ignore.
1133 * @error: a #GError.
1135 * Similar to g_file_query_info(), but obtains information
1136 * about the filesystem the @file is on, rather than the file itself.
1137 * For instance the amount of space available and the type of
1140 * The @attribute value is a string that specifies the file attributes that
1141 * should be gathered. It is not an error if it's not possible to read a particular
1142 * requested attribute from a file - it just won't be set. @attribute should
1143 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1144 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1145 * namespace. The standard namespace for filesystem attributes is "fs".
1146 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1147 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1148 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1150 * If @cancellable is not %NULL, then the operation can be cancelled by
1151 * triggering the cancellable object from another thread. If the operation
1152 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1154 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1155 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1157 * Returns: a #GFileInfo or %NULL if there was an error.
1160 g_file_query_filesystem_info (GFile *file,
1161 const char *attributes,
1162 GCancellable *cancellable,
1167 g_return_val_if_fail (G_IS_FILE (file), NULL);
1169 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1172 iface = G_FILE_GET_IFACE (file);
1174 if (iface->query_filesystem_info == NULL)
1176 g_set_error_literal (error, G_IO_ERROR,
1177 G_IO_ERROR_NOT_SUPPORTED,
1178 _("Operation not supported"));
1182 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1186 * g_file_query_filesystem_info_async:
1187 * @file: input #GFile.
1188 * @attributes: an attribute query string.
1189 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1191 * @cancellable: optional #GCancellable object, %NULL to ignore.
1192 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1193 * @user_data: the data to pass to callback function
1195 * Asynchronously gets the requested information about the filesystem
1196 * that the specified @file is on. The result is a #GFileInfo object
1197 * that contains key-value attributes (such as type or size for the
1200 * For more details, see g_file_query_filesystem_info() which is the
1201 * synchronous version of this call.
1203 * When the operation is finished, @callback will be called. You can
1204 * then call g_file_query_info_finish() to get the result of the
1208 g_file_query_filesystem_info_async (GFile *file,
1209 const char *attributes,
1211 GCancellable *cancellable,
1212 GAsyncReadyCallback callback,
1217 g_return_if_fail (G_IS_FILE (file));
1219 iface = G_FILE_GET_IFACE (file);
1220 (* iface->query_filesystem_info_async) (file,
1229 * g_file_query_filesystem_info_finish:
1230 * @file: input #GFile.
1231 * @res: a #GAsyncResult.
1232 * @error: a #GError.
1234 * Finishes an asynchronous filesystem info query. See
1235 * g_file_query_filesystem_info_async().
1237 * Returns: #GFileInfo for given @file or %NULL on error.
1240 g_file_query_filesystem_info_finish (GFile *file,
1246 g_return_val_if_fail (G_IS_FILE (file), NULL);
1247 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1249 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1251 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1252 if (g_simple_async_result_propagate_error (simple, error))
1256 iface = G_FILE_GET_IFACE (file);
1257 return (* iface->query_filesystem_info_finish) (file, res, error);
1261 * g_file_find_enclosing_mount:
1262 * @file: input #GFile.
1263 * @cancellable: optional #GCancellable object, %NULL to ignore.
1264 * @error: a #GError.
1266 * Gets a #GMount for the #GFile.
1268 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1269 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1272 * If @cancellable is not %NULL, then the operation can be cancelled by
1273 * triggering the cancellable object from another thread. If the operation
1274 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1276 * Returns: a #GMount where the @file is located or %NULL on error.
1279 g_file_find_enclosing_mount (GFile *file,
1280 GCancellable *cancellable,
1285 g_return_val_if_fail (G_IS_FILE (file), NULL);
1287 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1290 iface = G_FILE_GET_IFACE (file);
1291 if (iface->find_enclosing_mount == NULL)
1294 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1295 /* Translators: This is an error message when trying to find the
1296 * enclosing (user visible) mount of a file, but none exists. */
1297 _("Containing mount does not exist"));
1301 return (* iface->find_enclosing_mount) (file, cancellable, error);
1304 * g_file_find_enclosing_mount_async:
1306 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1308 * @cancellable: optional #GCancellable object, %NULL to ignore.
1309 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1310 * @user_data: the data to pass to callback function
1312 * Asynchronously gets the mount for the file.
1314 * For more details, see g_file_find_enclosing_mount() which is
1315 * the synchronous version of this call.
1317 * When the operation is finished, @callback will be called. You can then call
1318 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1321 g_file_find_enclosing_mount_async (GFile *file,
1323 GCancellable *cancellable,
1324 GAsyncReadyCallback callback,
1329 g_return_if_fail (G_IS_FILE (file));
1331 iface = G_FILE_GET_IFACE (file);
1332 (* iface->find_enclosing_mount_async) (file,
1340 * g_file_find_enclosing_mount_finish:
1342 * @res: a #GAsyncResult
1345 * Finishes an asynchronous find mount request.
1346 * See g_file_find_enclosing_mount_async().
1348 * Returns: #GMount for given @file or %NULL on error.
1351 g_file_find_enclosing_mount_finish (GFile *file,
1357 g_return_val_if_fail (G_IS_FILE (file), NULL);
1358 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1360 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1362 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1363 if (g_simple_async_result_propagate_error (simple, error))
1367 iface = G_FILE_GET_IFACE (file);
1368 return (* iface->find_enclosing_mount_finish) (file, res, error);
1374 * @file: #GFile to read.
1375 * @cancellable: a #GCancellable
1376 * @error: a #GError, or %NULL
1378 * Opens a file for reading. The result is a #GFileInputStream that
1379 * can be used to read the contents of the file.
1381 * If @cancellable is not %NULL, then the operation can be cancelled by
1382 * triggering the cancellable object from another thread. If the operation
1383 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1385 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1386 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1387 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1389 * Returns: #GFileInputStream or %NULL on error.
1392 g_file_read (GFile *file,
1393 GCancellable *cancellable,
1398 g_return_val_if_fail (G_IS_FILE (file), NULL);
1400 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1403 iface = G_FILE_GET_IFACE (file);
1405 if (iface->read_fn == NULL)
1407 g_set_error_literal (error, G_IO_ERROR,
1408 G_IO_ERROR_NOT_SUPPORTED,
1409 _("Operation not supported"));
1413 return (* iface->read_fn) (file, cancellable, error);
1418 * @file: input #GFile.
1419 * @flags: a set of #GFileCreateFlags.
1420 * @cancellable: optional #GCancellable object, %NULL to ignore.
1421 * @error: a #GError, or %NULL
1423 * Gets an output stream for appending data to the file. If
1424 * the file doesn't already exist it is created.
1426 * By default files created are generally readable by everyone,
1427 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1428 * will be made readable only to the current user, to the level that
1429 * is supported on the target filesystem.
1431 * If @cancellable is not %NULL, then the operation can be cancelled by
1432 * triggering the cancellable object from another thread. If the operation
1433 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1435 * Some file systems don't allow all file names, and may
1436 * return an %G_IO_ERROR_INVALID_FILENAME error.
1437 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1438 * returned. Other errors are possible too, and depend on what kind of
1439 * filesystem the file is on.
1441 * Returns: a #GFileOutputStream, or %NULL on error
1444 g_file_append_to (GFile *file,
1445 GFileCreateFlags flags,
1446 GCancellable *cancellable,
1451 g_return_val_if_fail (G_IS_FILE (file), NULL);
1453 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1456 iface = G_FILE_GET_IFACE (file);
1458 if (iface->append_to == NULL)
1460 g_set_error_literal (error, G_IO_ERROR,
1461 G_IO_ERROR_NOT_SUPPORTED,
1462 _("Operation not supported"));
1466 return (* iface->append_to) (file, flags, cancellable, error);
1471 * @file: input #GFile.
1472 * @flags: a set of #GFileCreateFlags.
1473 * @cancellable: optional #GCancellable object, %NULL to ignore.
1474 * @error: a #GError, or %NULL
1476 * Creates a new file and returns an output stream for writing to it.
1477 * The file must not already exist.
1479 * By default files created are generally readable by everyone,
1480 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1481 * will be made readable only to the current user, to the level that
1482 * is supported on the target filesystem.
1484 * If @cancellable is not %NULL, then the operation can be cancelled by
1485 * triggering the cancellable object from another thread. If the operation
1486 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1488 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1489 * error will be returned.
1490 * Some file systems don't allow all file names, and may
1491 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1492 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1493 * Other errors are possible too, and depend on what kind of
1494 * filesystem the file is on.
1496 * Returns: a #GFileOutputStream for the newly created file, or
1500 g_file_create (GFile *file,
1501 GFileCreateFlags flags,
1502 GCancellable *cancellable,
1507 g_return_val_if_fail (G_IS_FILE (file), NULL);
1509 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1512 iface = G_FILE_GET_IFACE (file);
1514 if (iface->create == NULL)
1516 g_set_error_literal (error, G_IO_ERROR,
1517 G_IO_ERROR_NOT_SUPPORTED,
1518 _("Operation not supported"));
1522 return (* iface->create) (file, flags, cancellable, error);
1527 * @file: input #GFile.
1528 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1529 * current #GFile, or #NULL to ignore.
1530 * @make_backup: %TRUE if a backup should be created.
1531 * @flags: a set of #GFileCreateFlags.
1532 * @cancellable: optional #GCancellable object, %NULL to ignore.
1533 * @error: a #GError, or %NULL
1535 * Returns an output stream for overwriting the file, possibly
1536 * creating a backup copy of the file first. If the file doesn't exist,
1537 * it will be created.
1539 * This will try to replace the file in the safest way possible so
1540 * that any errors during the writing will not affect an already
1541 * existing copy of the file. For instance, for local files it
1542 * may write to a temporary file and then atomically rename over
1543 * the destination when the stream is closed.
1545 * By default files created are generally readable by everyone,
1546 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1547 * will be made readable only to the current user, to the level that
1548 * is supported on the target filesystem.
1550 * If @cancellable is not %NULL, then the operation can be cancelled by
1551 * triggering the cancellable object from another thread. If the operation
1552 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1554 * If you pass in a non-#NULL @etag value, then this value is
1555 * compared to the current entity tag of the file, and if they differ
1556 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1557 * that the file has been changed since you last read it. You can get
1558 * the new etag from g_file_output_stream_get_etag() after you've
1559 * finished writing and closed the #GFileOutputStream. When you load
1560 * a new file you can use g_file_input_stream_query_info() to get
1561 * the etag of the file.
1563 * If @make_backup is %TRUE, this function will attempt to make a backup
1564 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1565 * error will be returned. If you want to replace anyway, try again with
1566 * @make_backup set to %FALSE.
1568 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1569 * and if the file is some other form of non-regular file then a
1570 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1571 * Some file systems don't allow all file names, and may
1572 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1573 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1574 * Other errors are possible too, and depend on what kind of
1575 * filesystem the file is on.
1577 * Returns: a #GFileOutputStream or %NULL on error.
1580 g_file_replace (GFile *file,
1582 gboolean make_backup,
1583 GFileCreateFlags flags,
1584 GCancellable *cancellable,
1589 g_return_val_if_fail (G_IS_FILE (file), NULL);
1591 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1594 iface = G_FILE_GET_IFACE (file);
1596 if (iface->replace == NULL)
1598 g_set_error_literal (error, G_IO_ERROR,
1599 G_IO_ERROR_NOT_SUPPORTED,
1600 _("Operation not supported"));
1605 /* Handle empty tag string as NULL in consistent way. */
1606 if (etag && *etag == 0)
1609 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1613 * g_file_read_async:
1614 * @file: input #GFile.
1615 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1617 * @cancellable: optional #GCancellable object, %NULL to ignore.
1618 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1619 * @user_data: the data to pass to callback function
1621 * Asynchronously opens @file for reading.
1623 * For more details, see g_file_read() which is
1624 * the synchronous version of this call.
1626 * When the operation is finished, @callback will be called. You can then call
1627 * g_file_read_finish() to get the result of the operation.
1630 g_file_read_async (GFile *file,
1632 GCancellable *cancellable,
1633 GAsyncReadyCallback callback,
1638 g_return_if_fail (G_IS_FILE (file));
1640 iface = G_FILE_GET_IFACE (file);
1641 (* iface->read_async) (file,
1649 * g_file_read_finish:
1650 * @file: input #GFile.
1651 * @res: a #GAsyncResult.
1652 * @error: a #GError, or %NULL
1654 * Finishes an asynchronous file read operation started with
1655 * g_file_read_async().
1657 * Returns: a #GFileInputStream or %NULL on error.
1660 g_file_read_finish (GFile *file,
1666 g_return_val_if_fail (G_IS_FILE (file), NULL);
1667 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1669 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1671 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1672 if (g_simple_async_result_propagate_error (simple, error))
1676 iface = G_FILE_GET_IFACE (file);
1677 return (* iface->read_finish) (file, res, error);
1681 * g_file_append_to_async:
1682 * @file: input #GFile.
1683 * @flags: a set of #GFileCreateFlags.
1684 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1686 * @cancellable: optional #GCancellable object, %NULL to ignore.
1687 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1688 * @user_data: the data to pass to callback function
1690 * Asynchronously opens @file for appending.
1692 * For more details, see g_file_append_to() which is
1693 * the synchronous version of this call.
1695 * When the operation is finished, @callback will be called. You can then call
1696 * g_file_append_to_finish() to get the result of the operation.
1699 g_file_append_to_async (GFile *file,
1700 GFileCreateFlags flags,
1702 GCancellable *cancellable,
1703 GAsyncReadyCallback callback,
1708 g_return_if_fail (G_IS_FILE (file));
1710 iface = G_FILE_GET_IFACE (file);
1711 (* iface->append_to_async) (file,
1720 * g_file_append_to_finish:
1721 * @file: input #GFile.
1722 * @res: #GAsyncResult
1723 * @error: a #GError, or %NULL
1725 * Finishes an asynchronous file append operation started with
1726 * g_file_append_to_async().
1728 * Returns: a valid #GFileOutputStream or %NULL on error.
1731 g_file_append_to_finish (GFile *file,
1737 g_return_val_if_fail (G_IS_FILE (file), NULL);
1738 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1740 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1742 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1743 if (g_simple_async_result_propagate_error (simple, error))
1747 iface = G_FILE_GET_IFACE (file);
1748 return (* iface->append_to_finish) (file, res, error);
1752 * g_file_create_async:
1753 * @file: input #GFile.
1754 * @flags: a set of #GFileCreateFlags.
1755 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1757 * @cancellable: optional #GCancellable object, %NULL to ignore.
1758 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1759 * @user_data: the data to pass to callback function
1761 * Asynchronously creates a new file and returns an output stream for writing to it.
1762 * The file must not already exist.
1764 * For more details, see g_file_create() which is
1765 * the synchronous version of this call.
1767 * When the operation is finished, @callback will be called. You can then call
1768 * g_file_create_finish() to get the result of the operation.
1771 g_file_create_async (GFile *file,
1772 GFileCreateFlags flags,
1774 GCancellable *cancellable,
1775 GAsyncReadyCallback callback,
1780 g_return_if_fail (G_IS_FILE (file));
1782 iface = G_FILE_GET_IFACE (file);
1783 (* iface->create_async) (file,
1792 * g_file_create_finish:
1793 * @file: input #GFile.
1794 * @res: a #GAsyncResult.
1795 * @error: a #GError, or %NULL
1797 * Finishes an asynchronous file create operation started with
1798 * g_file_create_async().
1800 * Returns: a #GFileOutputStream or %NULL on error.
1803 g_file_create_finish (GFile *file,
1809 g_return_val_if_fail (G_IS_FILE (file), NULL);
1810 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1812 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1814 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1815 if (g_simple_async_result_propagate_error (simple, error))
1819 iface = G_FILE_GET_IFACE (file);
1820 return (* iface->create_finish) (file, res, error);
1824 * g_file_replace_async:
1825 * @file: input #GFile.
1826 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1827 * current #GFile, or NULL to ignore.
1828 * @make_backup: %TRUE if a backup should be created.
1829 * @flags: a set of #GFileCreateFlags.
1830 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1832 * @cancellable: optional #GCancellable object, %NULL to ignore.
1833 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1834 * @user_data: the data to pass to callback function
1836 * Asynchronously overwrites the file, replacing the contents, possibly
1837 * creating a backup copy of the file first.
1839 * For more details, see g_file_replace() which is
1840 * the synchronous version of this call.
1842 * When the operation is finished, @callback will be called. You can then call
1843 * g_file_replace_finish() to get the result of the operation.
1846 g_file_replace_async (GFile *file,
1848 gboolean make_backup,
1849 GFileCreateFlags flags,
1851 GCancellable *cancellable,
1852 GAsyncReadyCallback callback,
1857 g_return_if_fail (G_IS_FILE (file));
1859 iface = G_FILE_GET_IFACE (file);
1860 (* iface->replace_async) (file,
1871 * g_file_replace_finish:
1872 * @file: input #GFile.
1873 * @res: a #GAsyncResult.
1874 * @error: a #GError, or %NULL
1876 * Finishes an asynchronous file replace operation started with
1877 * g_file_replace_async().
1879 * Returns: a #GFileOutputStream, or %NULL on error.
1882 g_file_replace_finish (GFile *file,
1888 g_return_val_if_fail (G_IS_FILE (file), NULL);
1889 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1891 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1893 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1894 if (g_simple_async_result_propagate_error (simple, error))
1898 iface = G_FILE_GET_IFACE (file);
1899 return (* iface->replace_finish) (file, res, error);
1903 copy_symlink (GFile *destination,
1904 GFileCopyFlags flags,
1905 GCancellable *cancellable,
1910 gboolean tried_delete;
1912 GFileType file_type;
1914 tried_delete = FALSE;
1918 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1920 /* Maybe it already existed, and we want to overwrite? */
1921 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1922 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1924 g_error_free (my_error);
1927 /* Don't overwrite if the destination is a directory */
1928 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1929 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1930 cancellable, &my_error);
1933 file_type = g_file_info_get_file_type (info);
1934 g_object_unref (info);
1936 if (file_type == G_FILE_TYPE_DIRECTORY)
1938 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1939 _("Can't copy over directory"));
1944 if (!g_file_delete (destination, cancellable, error))
1947 tried_delete = TRUE;
1951 g_propagate_error (error, my_error);
1958 static GInputStream *
1959 open_source_for_copy (GFile *source,
1961 GFileCopyFlags flags,
1962 GCancellable *cancellable,
1968 GFileType file_type;
1971 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1975 /* There was an error opening the source, try to set a good error for it: */
1977 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1979 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1980 * as that is less useful to the app. Better check for errors on the
1983 g_error_free (my_error);
1986 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1987 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1988 cancellable, &my_error);
1991 file_type = g_file_info_get_file_type (info);
1992 g_object_unref (info);
1994 if (flags & G_FILE_COPY_OVERWRITE)
1996 if (file_type == G_FILE_TYPE_DIRECTORY)
1998 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1999 _("Can't copy directory over directory"));
2002 /* continue to would_recurse error */
2006 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2007 _("Target file exists"));
2013 /* Error getting info from target, return that error
2014 * (except for NOT_FOUND, which is no error here)
2016 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
2018 g_propagate_error (error, my_error);
2021 g_error_free (my_error);
2024 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2025 _("Can't recursively copy directory"));
2029 g_propagate_error (error, my_error);
2034 should_copy (GFileAttributeInfo *info,
2038 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2039 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2043 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2044 GFileAttributeInfoList *namespaces,
2052 s = g_string_new ("");
2056 for (i = 0; i < attributes->n_infos; i++)
2058 if (should_copy (&attributes->infos[i], as_move))
2063 g_string_append_c (s, ',');
2065 g_string_append (s, attributes->infos[i].name);
2072 for (i = 0; i < namespaces->n_infos; i++)
2074 if (should_copy (&namespaces->infos[i], as_move))
2079 g_string_append_c (s, ',');
2081 g_string_append (s, namespaces->infos[i].name);
2082 g_string_append (s, ":*");
2087 return g_string_free (s, FALSE);
2091 * g_file_copy_attributes:
2092 * @source: a #GFile with attributes.
2093 * @destination: a #GFile to copy attributes to.
2094 * @flags: a set of #GFileCopyFlags.
2095 * @cancellable: optional #GCancellable object, %NULL to ignore.
2096 * @error: a #GError, %NULL to ignore.
2098 * Copies the file attributes from @source to @destination.
2100 * Normally only a subset of the file attributes are copied,
2101 * those that are copies in a normal file copy operation
2102 * (which for instance does not include e.g. mtime). However
2103 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2104 * all the metadata that is possible to copy is copied.
2106 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2109 g_file_copy_attributes (GFile *source,
2111 GFileCopyFlags flags,
2112 GCancellable *cancellable,
2115 GFileAttributeInfoList *attributes, *namespaces;
2116 char *attrs_to_read;
2120 gboolean source_nofollow_symlinks;
2122 as_move = flags & G_FILE_COPY_ALL_METADATA;
2123 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2125 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2126 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2127 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2129 if (attributes == NULL && namespaces == NULL)
2132 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2134 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2135 * we just don't copy it.
2137 info = g_file_query_info (source, attrs_to_read,
2138 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2142 g_free (attrs_to_read);
2147 res = g_file_set_attributes_from_info (destination,
2151 g_object_unref (info);
2154 g_file_attribute_info_list_unref (attributes);
2155 g_file_attribute_info_list_unref (namespaces);
2160 /* Closes the streams */
2162 copy_stream_with_progress (GInputStream *in,
2165 GCancellable *cancellable,
2166 GFileProgressCallback progress_callback,
2167 gpointer progress_callback_data,
2170 gssize n_read, n_written;
2171 goffset current_size;
2172 char buffer[1024*64], *p;
2178 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2179 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2183 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2184 total_size = g_file_info_get_size (info);
2185 g_object_unref (info);
2188 if (total_size == -1)
2190 info = g_file_query_info (source,
2191 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2192 G_FILE_QUERY_INFO_NONE,
2196 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2197 total_size = g_file_info_get_size (info);
2198 g_object_unref (info);
2202 if (total_size == -1)
2209 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2219 current_size += n_read;
2224 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2225 if (n_written == -1)
2232 n_read -= n_written;
2238 if (progress_callback)
2239 progress_callback (current_size, total_size, progress_callback_data);
2243 error = NULL; /* Ignore further errors */
2245 /* Make sure we send full copied size */
2246 if (progress_callback)
2247 progress_callback (current_size, total_size, progress_callback_data);
2249 /* Don't care about errors in source here */
2250 g_input_stream_close (in, cancellable, NULL);
2252 /* But write errors on close are bad! */
2253 if (!g_output_stream_close (out, cancellable, error))
2256 g_object_unref (in);
2257 g_object_unref (out);
2263 file_copy_fallback (GFile *source,
2265 GFileCopyFlags flags,
2266 GCancellable *cancellable,
2267 GFileProgressCallback progress_callback,
2268 gpointer progress_callback_data,
2276 /* Maybe copy the symlink? */
2277 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2279 info = g_file_query_info (source,
2280 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2281 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2287 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2288 (target = g_file_info_get_symlink_target (info)) != NULL)
2290 if (!copy_symlink (destination, flags, cancellable, target, error))
2292 g_object_unref (info);
2296 g_object_unref (info);
2300 g_object_unref (info);
2303 in = open_source_for_copy (source, destination, flags, cancellable, error);
2307 if (flags & G_FILE_COPY_OVERWRITE)
2309 out = (GOutputStream *)g_file_replace (destination,
2311 flags & G_FILE_COPY_BACKUP,
2313 cancellable, error);
2317 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2322 g_object_unref (in);
2326 if (!copy_stream_with_progress (in, out, source, cancellable,
2327 progress_callback, progress_callback_data,
2333 /* Ignore errors here. Failure to copy metadata is not a hard error */
2334 g_file_copy_attributes (source, destination,
2335 flags, cancellable, NULL);
2342 * @source: input #GFile.
2343 * @destination: destination #GFile
2344 * @flags: set of #GFileCopyFlags
2345 * @cancellable: optional #GCancellable object, %NULL to ignore.
2346 * @progress_callback: function to callback with progress information
2347 * @progress_callback_data: user data to pass to @progress_callback
2348 * @error: #GError to set on error, or %NULL
2350 * Copies the file @source to the location specified by @destination.
2351 * Can not handle recursive copies of directories.
2353 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2354 * existing @destination file is overwritten.
2356 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2357 * will be copied as symlinks, otherwise the target of the
2358 * @source symlink will be copied.
2360 * If @cancellable is not %NULL, then the operation can be cancelled by
2361 * triggering the cancellable object from another thread. If the operation
2362 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2364 * If @progress_callback is not %NULL, then the operation can be monitored by
2365 * setting this to a #GFileProgressCallback function. @progress_callback_data
2366 * will be passed to this function. It is guaranteed that this callback will
2367 * be called after all data has been transferred with the total number of bytes
2368 * copied during the operation.
2370 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2371 * error is returned, independent on the status of the @destination.
2373 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2374 * error G_IO_ERROR_EXISTS is returned.
2376 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2377 * error is returned. If trying to overwrite a directory with a directory the
2378 * G_IO_ERROR_WOULD_MERGE error is returned.
2380 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2381 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2384 * If you are interested in copying the #GFile object itself (not the on-disk
2385 * file), see g_file_dup().
2387 * Returns: %TRUE on success, %FALSE otherwise.
2390 g_file_copy (GFile *source,
2392 GFileCopyFlags flags,
2393 GCancellable *cancellable,
2394 GFileProgressCallback progress_callback,
2395 gpointer progress_callback_data,
2402 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2403 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2405 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2408 iface = G_FILE_GET_IFACE (destination);
2412 res = (* iface->copy) (source, destination,
2414 progress_callback, progress_callback_data,
2420 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2422 g_propagate_error (error, my_error);
2427 /* If the types are different, and the destination method failed
2428 also try the source method */
2429 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2431 iface = G_FILE_GET_IFACE (source);
2436 res = (* iface->copy) (source, destination,
2438 progress_callback, progress_callback_data,
2444 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2446 g_propagate_error (error, my_error);
2452 return file_copy_fallback (source, destination, flags, cancellable,
2453 progress_callback, progress_callback_data,
2458 * g_file_copy_async:
2459 * @source: input #GFile.
2460 * @destination: destination #GFile
2461 * @flags: set of #GFileCopyFlags
2462 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2464 * @cancellable: optional #GCancellable object, %NULL to ignore.
2465 * @progress_callback: function to callback with progress information
2466 * @progress_callback_data: user data to pass to @progress_callback
2467 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2468 * @user_data: the data to pass to callback function
2470 * Copies the file @source to the location specified by @destination
2471 * asynchronously. For details of the behaviour, see g_file_copy().
2473 * If @progress_callback is not %NULL, then that function that will be called
2474 * just like in g_file_copy(), however the callback will run in the main loop,
2475 * not in the thread that is doing the I/O operation.
2477 * When the operation is finished, @callback will be called. You can then call
2478 * g_file_copy_finish() to get the result of the operation.
2481 g_file_copy_async (GFile *source,
2483 GFileCopyFlags flags,
2485 GCancellable *cancellable,
2486 GFileProgressCallback progress_callback,
2487 gpointer progress_callback_data,
2488 GAsyncReadyCallback callback,
2493 g_return_if_fail (G_IS_FILE (source));
2494 g_return_if_fail (G_IS_FILE (destination));
2496 iface = G_FILE_GET_IFACE (source);
2497 (* iface->copy_async) (source,
2503 progress_callback_data,
2509 * g_file_copy_finish:
2510 * @file: input #GFile.
2511 * @res: a #GAsyncResult.
2512 * @error: a #GError, or %NULL
2514 * Finishes copying the file started with
2515 * g_file_copy_async().
2517 * Returns: a %TRUE on success, %FALSE on error.
2520 g_file_copy_finish (GFile *file,
2526 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2527 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2529 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2531 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2533 if (g_simple_async_result_propagate_error (simple, error))
2537 iface = G_FILE_GET_IFACE (file);
2538 return (* iface->copy_finish) (file, res, error);
2543 * @source: #GFile pointing to the source location.
2544 * @destination: #GFile pointing to the destination location.
2545 * @flags: set of #GFileCopyFlags.
2546 * @cancellable: optional #GCancellable object, %NULL to ignore.
2547 * @progress_callback: #GFileProgressCallback function for updates.
2548 * @progress_callback_data: gpointer to user data for the callback function.
2549 * @error: #GError for returning error conditions, or %NULL
2552 * Tries to move the file or directory @source to the location specified by @destination.
2553 * If native move operations are supported then this is used, otherwise a copy + delete
2554 * fallback is used. The native implementation may support moving directories (for instance
2555 * on moves inside the same filesystem), but the fallback code does not.
2557 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2558 * existing @destination file is overwritten.
2560 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2561 * will be copied as symlinks, otherwise the target of the
2562 * @source symlink will be copied.
2564 * If @cancellable is not %NULL, then the operation can be cancelled by
2565 * triggering the cancellable object from another thread. If the operation
2566 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2568 * If @progress_callback is not %NULL, then the operation can be monitored by
2569 * setting this to a #GFileProgressCallback function. @progress_callback_data
2570 * will be passed to this function. It is guaranteed that this callback will
2571 * be called after all data has been transferred with the total number of bytes
2572 * copied during the operation.
2574 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2575 * error is returned, independent on the status of the @destination.
2577 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2578 * error G_IO_ERROR_EXISTS is returned.
2580 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2581 * error is returned. If trying to overwrite a directory with a directory the
2582 * G_IO_ERROR_WOULD_MERGE error is returned.
2584 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2585 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2586 * may be returned (if the native move operation isn't available).
2588 * Returns: %TRUE on successful move, %FALSE otherwise.
2591 g_file_move (GFile *source,
2593 GFileCopyFlags flags,
2594 GCancellable *cancellable,
2595 GFileProgressCallback progress_callback,
2596 gpointer progress_callback_data,
2603 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2604 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2606 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2609 iface = G_FILE_GET_IFACE (destination);
2613 res = (* iface->move) (source, destination,
2615 progress_callback, progress_callback_data,
2621 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2623 g_propagate_error (error, my_error);
2628 /* If the types are different, and the destination method failed
2629 also try the source method */
2630 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2632 iface = G_FILE_GET_IFACE (source);
2637 res = (* iface->move) (source, destination,
2639 progress_callback, progress_callback_data,
2645 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2647 g_propagate_error (error, my_error);
2653 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2655 g_set_error_literal (error, G_IO_ERROR,
2656 G_IO_ERROR_NOT_SUPPORTED,
2657 _("Operation not supported"));
2661 flags |= G_FILE_COPY_ALL_METADATA;
2662 if (!g_file_copy (source, destination, flags, cancellable,
2663 progress_callback, progress_callback_data,
2667 return g_file_delete (source, cancellable, error);
2671 * g_file_make_directory
2672 * @file: input #GFile.
2673 * @cancellable: optional #GCancellable object, %NULL to ignore.
2674 * @error: a #GError, or %NULL
2676 * Creates a directory. Note that this will only create a child directory of
2677 * the immediate parent directory of the path or URI given by the #GFile. To
2678 * recursively create directories, see g_file_make_directory_with_parents().
2679 * This function will fail if the parent directory does not exist, setting
2680 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
2681 * directories, this function will fail, setting @error to
2682 * %G_IO_ERROR_NOT_SUPPORTED.
2684 * If @cancellable is not %NULL, then the operation can be cancelled by
2685 * triggering the cancellable object from another thread. If the operation
2686 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2688 * Returns: %TRUE on successful creation, %FALSE otherwise.
2691 g_file_make_directory (GFile *file,
2692 GCancellable *cancellable,
2697 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2699 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2702 iface = G_FILE_GET_IFACE (file);
2704 if (iface->make_directory == NULL)
2706 g_set_error_literal (error, G_IO_ERROR,
2707 G_IO_ERROR_NOT_SUPPORTED,
2708 _("Operation not supported"));
2712 return (* iface->make_directory) (file, cancellable, error);
2716 * g_file_make_directory_with_parents:
2717 * @file: input #GFile.
2718 * @cancellable: optional #GCancellable object, %NULL to ignore.
2719 * @error: a #GError, or %NULL
2721 * Creates a directory and any parent directories that may not exist similar to
2722 * 'mkdir -p'. If the file system does not support creating directories, this
2723 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED.
2725 * If @cancellable is not %NULL, then the operation can be cancelled by
2726 * triggering the cancellable object from another thread. If the operation
2727 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2729 * Returns: %TRUE if all directories have been successfully created, %FALSE
2735 g_file_make_directory_with_parents (GFile *file,
2736 GCancellable *cancellable,
2740 GFile *parent_file, *work_file;
2741 GList *list = NULL, *l;
2742 GError *my_error = NULL;
2744 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2747 result = g_file_make_directory (file, cancellable, &my_error);
2748 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
2751 g_propagate_error (error, my_error);
2757 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2759 g_clear_error (&my_error);
2761 parent_file = g_file_get_parent (work_file);
2762 if (parent_file == NULL)
2764 result = g_file_make_directory (parent_file, cancellable, &my_error);
2766 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
2767 list = g_list_prepend (list, parent_file);
2769 work_file = parent_file;
2772 for (l = list; result && l; l = l->next)
2774 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
2778 while (list != NULL)
2780 g_object_unref ((GFile *) list->data);
2781 list = g_list_remove (list, list->data);
2786 g_propagate_error (error, my_error);
2790 return g_file_make_directory (file, cancellable, error);
2794 * g_file_make_symbolic_link:
2795 * @file: input #GFile.
2796 * @symlink_value: a string with the value of the new symlink.
2797 * @cancellable: optional #GCancellable object, %NULL to ignore.
2798 * @error: a #GError.
2800 * Creates a symbolic link.
2802 * If @cancellable is not %NULL, then the operation can be cancelled by
2803 * triggering the cancellable object from another thread. If the operation
2804 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2806 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2809 g_file_make_symbolic_link (GFile *file,
2810 const char *symlink_value,
2811 GCancellable *cancellable,
2816 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2817 g_return_val_if_fail (symlink_value != NULL, FALSE);
2819 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2822 if (*symlink_value == '\0')
2824 g_set_error_literal (error, G_IO_ERROR,
2825 G_IO_ERROR_INVALID_ARGUMENT,
2826 _("Invalid symlink value given"));
2830 iface = G_FILE_GET_IFACE (file);
2832 if (iface->make_symbolic_link == NULL)
2834 g_set_error_literal (error, G_IO_ERROR,
2835 G_IO_ERROR_NOT_SUPPORTED,
2836 _("Operation not supported"));
2840 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2845 * @file: input #GFile.
2846 * @cancellable: optional #GCancellable object, %NULL to ignore.
2847 * @error: a #GError, or %NULL
2849 * Deletes a file. If the @file is a directory, it will only be deleted if it
2852 * If @cancellable is not %NULL, then the operation can be cancelled by
2853 * triggering the cancellable object from another thread. If the operation
2854 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2856 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2859 g_file_delete (GFile *file,
2860 GCancellable *cancellable,
2865 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2867 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2870 iface = G_FILE_GET_IFACE (file);
2872 if (iface->delete_file == NULL)
2874 g_set_error_literal (error, G_IO_ERROR,
2875 G_IO_ERROR_NOT_SUPPORTED,
2876 _("Operation not supported"));
2880 return (* iface->delete_file) (file, cancellable, error);
2885 * @file: #GFile to send to trash.
2886 * @cancellable: optional #GCancellable object, %NULL to ignore.
2887 * @error: a #GError, or %NULL
2889 * Sends @file to the "Trashcan", if possible. This is similar to
2890 * deleting it, but the user can recover it before emptying the trashcan.
2891 * Not all file systems support trashing, so this call can return the
2892 * %G_IO_ERROR_NOT_SUPPORTED error.
2895 * If @cancellable is not %NULL, then the operation can be cancelled by
2896 * triggering the cancellable object from another thread. If the operation
2897 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2899 * Returns: %TRUE on successful trash, %FALSE otherwise.
2902 g_file_trash (GFile *file,
2903 GCancellable *cancellable,
2908 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2910 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2913 iface = G_FILE_GET_IFACE (file);
2915 if (iface->trash == NULL)
2917 g_set_error_literal (error,
2918 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2919 _("Trash not supported"));
2923 return (* iface->trash) (file, cancellable, error);
2927 * g_file_set_display_name:
2928 * @file: input #GFile.
2929 * @display_name: a string.
2930 * @cancellable: optional #GCancellable object, %NULL to ignore.
2931 * @error: a #GError, or %NULL
2933 * Renames @file to the specified display name.
2935 * The display name is converted from UTF8 to the correct encoding for the target
2936 * filesystem if possible and the @file is renamed to this.
2938 * If you want to implement a rename operation in the user interface the edit name
2939 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2940 * widget, and then the result after editing should be passed to g_file_set_display_name().
2942 * On success the resulting converted filename is returned.
2944 * If @cancellable is not %NULL, then the operation can be cancelled by
2945 * triggering the cancellable object from another thread. If the operation
2946 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2948 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2951 g_file_set_display_name (GFile *file,
2952 const char *display_name,
2953 GCancellable *cancellable,
2958 g_return_val_if_fail (G_IS_FILE (file), NULL);
2959 g_return_val_if_fail (display_name != NULL, NULL);
2961 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2965 G_IO_ERROR_INVALID_ARGUMENT,
2966 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2970 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2973 iface = G_FILE_GET_IFACE (file);
2975 return (* iface->set_display_name) (file, display_name, cancellable, error);
2979 * g_file_set_display_name_async:
2980 * @file: input #GFile.
2981 * @display_name: a string.
2982 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2984 * @cancellable: optional #GCancellable object, %NULL to ignore.
2985 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2986 * @user_data: the data to pass to callback function
2988 * Asynchronously sets the display name for a given #GFile.
2990 * For more details, see g_set_display_name() which is
2991 * the synchronous version of this call.
2993 * When the operation is finished, @callback will be called. You can then call
2994 * g_file_set_display_name_finish() to get the result of the operation.
2997 g_file_set_display_name_async (GFile *file,
2998 const char *display_name,
3000 GCancellable *cancellable,
3001 GAsyncReadyCallback callback,
3006 g_return_if_fail (G_IS_FILE (file));
3007 g_return_if_fail (display_name != NULL);
3009 iface = G_FILE_GET_IFACE (file);
3010 (* iface->set_display_name_async) (file,
3019 * g_file_set_display_name_finish:
3020 * @file: input #GFile.
3021 * @res: a #GAsyncResult.
3022 * @error: a #GError, or %NULL
3024 * Finishes setting a display name started with
3025 * g_file_set_display_name_async().
3027 * Returns: a #GFile or %NULL on error.
3030 g_file_set_display_name_finish (GFile *file,
3036 g_return_val_if_fail (G_IS_FILE (file), NULL);
3037 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3039 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3041 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3042 if (g_simple_async_result_propagate_error (simple, error))
3046 iface = G_FILE_GET_IFACE (file);
3047 return (* iface->set_display_name_finish) (file, res, error);
3051 * g_file_query_settable_attributes:
3052 * @file: input #GFile.
3053 * @cancellable: optional #GCancellable object, %NULL to ignore.
3054 * @error: a #GError, or %NULL
3056 * Obtain the list of settable attributes for the file.
3058 * Returns the type and full attribute name of all the attributes
3059 * that can be set on this file. This doesn't mean setting it will always
3060 * succeed though, you might get an access failure, or some specific
3061 * file may not support a specific attribute.
3063 * If @cancellable is not %NULL, then the operation can be cancelled by
3064 * triggering the cancellable object from another thread. If the operation
3065 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3067 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3068 * When you are done with it, release it with g_file_attribute_info_list_unref()
3070 GFileAttributeInfoList *
3071 g_file_query_settable_attributes (GFile *file,
3072 GCancellable *cancellable,
3077 GFileAttributeInfoList *list;
3079 g_return_val_if_fail (G_IS_FILE (file), NULL);
3081 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3084 iface = G_FILE_GET_IFACE (file);
3086 if (iface->query_settable_attributes == NULL)
3087 return g_file_attribute_info_list_new ();
3090 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3094 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3096 list = g_file_attribute_info_list_new ();
3097 g_error_free (my_error);
3100 g_propagate_error (error, my_error);
3107 * g_file_query_writable_namespaces:
3108 * @file: input #GFile.
3109 * @cancellable: optional #GCancellable object, %NULL to ignore.
3110 * @error: a #GError, or %NULL
3112 * Obtain the list of attribute namespaces where new attributes
3113 * can be created by a user. An example of this is extended
3114 * attributes (in the "xattr" namespace).
3116 * If @cancellable is not %NULL, then the operation can be cancelled by
3117 * triggering the cancellable object from another thread. If the operation
3118 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3120 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3121 * When you are done with it, release it with g_file_attribute_info_list_unref()
3123 GFileAttributeInfoList *
3124 g_file_query_writable_namespaces (GFile *file,
3125 GCancellable *cancellable,
3130 GFileAttributeInfoList *list;
3132 g_return_val_if_fail (G_IS_FILE (file), NULL);
3134 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3137 iface = G_FILE_GET_IFACE (file);
3139 if (iface->query_writable_namespaces == NULL)
3140 return g_file_attribute_info_list_new ();
3143 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3147 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3149 list = g_file_attribute_info_list_new ();
3150 g_error_free (my_error);
3153 g_propagate_error (error, my_error);
3160 * g_file_set_attribute:
3161 * @file: input #GFile.
3162 * @attribute: a string containing the attribute's name.
3163 * @type: The type of the attribute
3164 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3165 * @flags: a set of #GFileQueryInfoFlags.
3166 * @cancellable: optional #GCancellable object, %NULL to ignore.
3167 * @error: a #GError, or %NULL
3169 * Sets an attribute in the file with attribute name @attribute to @value.
3171 * If @cancellable is not %NULL, then the operation can be cancelled by
3172 * triggering the cancellable object from another thread. If the operation
3173 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3175 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3178 g_file_set_attribute (GFile *file,
3179 const char *attribute,
3180 GFileAttributeType type,
3182 GFileQueryInfoFlags flags,
3183 GCancellable *cancellable,
3188 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3189 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3191 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3194 iface = G_FILE_GET_IFACE (file);
3196 if (iface->set_attribute == NULL)
3198 g_set_error_literal (error, G_IO_ERROR,
3199 G_IO_ERROR_NOT_SUPPORTED,
3200 _("Operation not supported"));
3204 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3208 * g_file_set_attributes_from_info:
3209 * @file: input #GFile.
3210 * @info: a #GFileInfo.
3211 * @flags: #GFileQueryInfoFlags
3212 * @cancellable: optional #GCancellable object, %NULL to ignore.
3213 * @error: a #GError, or %NULL
3215 * Tries to set all attributes in the #GFileInfo on the target values,
3216 * not stopping on the first error.
3218 * If there is any error during this operation then @error will be set to
3219 * the first error. Error on particular fields are flagged by setting
3220 * the "status" field in the attribute value to
3221 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3224 * If @cancellable is not %NULL, then the operation can be cancelled by
3225 * triggering the cancellable object from another thread. If the operation
3226 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3228 * Returns: %TRUE if there was any error, %FALSE otherwise.
3231 g_file_set_attributes_from_info (GFile *file,
3233 GFileQueryInfoFlags flags,
3234 GCancellable *cancellable,
3239 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3240 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3242 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3245 g_file_info_clear_status (info);
3247 iface = G_FILE_GET_IFACE (file);
3249 return (* iface->set_attributes_from_info) (file,
3258 g_file_real_set_attributes_from_info (GFile *file,
3260 GFileQueryInfoFlags flags,
3261 GCancellable *cancellable,
3267 GFileAttributeValue *value;
3271 attributes = g_file_info_list_attributes (info, NULL);
3273 for (i = 0; attributes[i] != NULL; i++)
3275 value = _g_file_info_get_attribute_value (info, attributes[i]);
3277 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3280 if (!g_file_set_attribute (file, attributes[i],
3281 value->type, _g_file_attribute_value_peek_as_pointer (value),
3282 flags, cancellable, error))
3284 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3286 /* Don't set error multiple times */
3290 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3293 g_strfreev (attributes);
3299 * g_file_set_attributes_async:
3300 * @file: input #GFile.
3301 * @info: a #GFileInfo.
3302 * @flags: a #GFileQueryInfoFlags.
3303 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3305 * @cancellable: optional #GCancellable object, %NULL to ignore.
3306 * @callback: a #GAsyncReadyCallback.
3307 * @user_data: a #gpointer.
3309 * Asynchronously sets the attributes of @file with @info.
3311 * For more details, see g_file_set_attributes_from_info() which is
3312 * the synchronous version of this call.
3314 * When the operation is finished, @callback will be called. You can then call
3315 * g_file_set_attributes_finish() to get the result of the operation.
3318 g_file_set_attributes_async (GFile *file,
3320 GFileQueryInfoFlags flags,
3322 GCancellable *cancellable,
3323 GAsyncReadyCallback callback,
3328 g_return_if_fail (G_IS_FILE (file));
3329 g_return_if_fail (G_IS_FILE_INFO (info));
3331 iface = G_FILE_GET_IFACE (file);
3332 (* iface->set_attributes_async) (file,
3342 * g_file_set_attributes_finish:
3343 * @file: input #GFile.
3344 * @result: a #GAsyncResult.
3345 * @info: a #GFileInfo.
3346 * @error: a #GError, or %NULL
3348 * Finishes setting an attribute started in g_file_set_attributes_async().
3350 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3353 g_file_set_attributes_finish (GFile *file,
3354 GAsyncResult *result,
3360 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3361 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3363 /* No standard handling of errors here, as we must set info even
3366 iface = G_FILE_GET_IFACE (file);
3367 return (* iface->set_attributes_finish) (file, result, info, error);
3371 * g_file_set_attribute_string:
3372 * @file: input #GFile.
3373 * @attribute: a string containing the attribute's name.
3374 * @value: a string containing the attribute's value.
3375 * @flags: #GFileQueryInfoFlags.
3376 * @cancellable: optional #GCancellable object, %NULL to ignore.
3377 * @error: a #GError, or %NULL
3379 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3380 * If @attribute is of a different type, this operation will fail.
3382 * If @cancellable is not %NULL, then the operation can be cancelled by
3383 * triggering the cancellable object from another thread. If the operation
3384 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3386 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3389 g_file_set_attribute_string (GFile *file,
3390 const char *attribute,
3392 GFileQueryInfoFlags flags,
3393 GCancellable *cancellable,
3396 return g_file_set_attribute (file, attribute,
3397 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3398 flags, cancellable, error);
3402 * g_file_set_attribute_byte_string:
3403 * @file: input #GFile.
3404 * @attribute: a string containing the attribute's name.
3405 * @value: a string containing the attribute's new value.
3406 * @flags: a #GFileQueryInfoFlags.
3407 * @cancellable: optional #GCancellable object, %NULL to ignore.
3408 * @error: a #GError, or %NULL
3410 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3411 * If @attribute is of a different type, this operation will fail,
3414 * If @cancellable is not %NULL, then the operation can be cancelled by
3415 * triggering the cancellable object from another thread. If the operation
3416 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3418 * Returns: %TRUE if the @attribute was successfully set to @value
3419 * in the @file, %FALSE otherwise.
3422 g_file_set_attribute_byte_string (GFile *file,
3423 const char *attribute,
3425 GFileQueryInfoFlags flags,
3426 GCancellable *cancellable,
3429 return g_file_set_attribute (file, attribute,
3430 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3431 flags, cancellable, error);
3435 * g_file_set_attribute_uint32:
3436 * @file: input #GFile.
3437 * @attribute: a string containing the attribute's name.
3438 * @value: a #guint32 containing the attribute's new value.
3439 * @flags: a #GFileQueryInfoFlags.
3440 * @cancellable: optional #GCancellable object, %NULL to ignore.
3441 * @error: a #GError, or %NULL
3443 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3444 * If @attribute is of a different type, this operation will fail.
3446 * If @cancellable is not %NULL, then the operation can be cancelled by
3447 * triggering the cancellable object from another thread. If the operation
3448 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3450 * Returns: %TRUE if the @attribute was successfully set to @value
3451 * in the @file, %FALSE otherwise.
3454 g_file_set_attribute_uint32 (GFile *file,
3455 const char *attribute,
3457 GFileQueryInfoFlags flags,
3458 GCancellable *cancellable,
3461 return g_file_set_attribute (file, attribute,
3462 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3463 flags, cancellable, error);
3467 * g_file_set_attribute_int32:
3468 * @file: input #GFile.
3469 * @attribute: a string containing the attribute's name.
3470 * @value: a #gint32 containing the attribute's new value.
3471 * @flags: a #GFileQueryInfoFlags.
3472 * @cancellable: optional #GCancellable object, %NULL to ignore.
3473 * @error: a #GError, or %NULL
3475 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3476 * If @attribute is of a different type, this operation will fail.
3478 * If @cancellable is not %NULL, then the operation can be cancelled by
3479 * triggering the cancellable object from another thread. If the operation
3480 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3482 * Returns: %TRUE if the @attribute was successfully set to @value
3483 * in the @file, %FALSE otherwise.
3486 g_file_set_attribute_int32 (GFile *file,
3487 const char *attribute,
3489 GFileQueryInfoFlags flags,
3490 GCancellable *cancellable,
3493 return g_file_set_attribute (file, attribute,
3494 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3495 flags, cancellable, error);
3499 * g_file_set_attribute_uint64:
3500 * @file: input #GFile.
3501 * @attribute: a string containing the attribute's name.
3502 * @value: a #guint64 containing the attribute's new value.
3503 * @flags: a #GFileQueryInfoFlags.
3504 * @cancellable: optional #GCancellable object, %NULL to ignore.
3505 * @error: a #GError, or %NULL
3507 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3508 * If @attribute is of a different type, this operation will fail.
3510 * If @cancellable is not %NULL, then the operation can be cancelled by
3511 * triggering the cancellable object from another thread. If the operation
3512 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3514 * Returns: %TRUE if the @attribute was successfully set to @value
3515 * in the @file, %FALSE otherwise.
3518 g_file_set_attribute_uint64 (GFile *file,
3519 const char *attribute,
3521 GFileQueryInfoFlags flags,
3522 GCancellable *cancellable,
3525 return g_file_set_attribute (file, attribute,
3526 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3527 flags, cancellable, error);
3531 * g_file_set_attribute_int64:
3532 * @file: input #GFile.
3533 * @attribute: a string containing the attribute's name.
3534 * @value: a #guint64 containing the attribute's new value.
3535 * @flags: a #GFileQueryInfoFlags.
3536 * @cancellable: optional #GCancellable object, %NULL to ignore.
3537 * @error: a #GError, or %NULL
3539 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3540 * If @attribute is of a different type, this operation will fail.
3542 * If @cancellable is not %NULL, then the operation can be cancelled by
3543 * triggering the cancellable object from another thread. If the operation
3544 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3546 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3549 g_file_set_attribute_int64 (GFile *file,
3550 const char *attribute,
3552 GFileQueryInfoFlags flags,
3553 GCancellable *cancellable,
3556 return g_file_set_attribute (file, attribute,
3557 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3558 flags, cancellable, error);
3562 * g_file_mount_mountable:
3563 * @file: input #GFile.
3564 * @flags: flags affecting the operation
3565 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3566 * @cancellable: optional #GCancellable object, %NULL to ignore.
3567 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3568 * @user_data: the data to pass to callback function
3570 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3571 * Using @mount_operation, you can request callbacks when, for instance,
3572 * passwords are needed during authentication.
3574 * If @cancellable is not %NULL, then the operation can be cancelled by
3575 * triggering the cancellable object from another thread. If the operation
3576 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3578 * When the operation is finished, @callback will be called. You can then call
3579 * g_file_mount_mountable_finish() to get the result of the operation.
3582 g_file_mount_mountable (GFile *file,
3583 GMountMountFlags flags,
3584 GMountOperation *mount_operation,
3585 GCancellable *cancellable,
3586 GAsyncReadyCallback callback,
3591 g_return_if_fail (G_IS_FILE (file));
3593 iface = G_FILE_GET_IFACE (file);
3595 if (iface->mount_mountable == NULL)
3597 g_simple_async_report_error_in_idle (G_OBJECT (file),
3601 G_IO_ERROR_NOT_SUPPORTED,
3602 _("Operation not supported"));
3606 (* iface->mount_mountable) (file,
3615 * g_file_mount_mountable_finish:
3616 * @file: input #GFile.
3617 * @result: a #GAsyncResult.
3618 * @error: a #GError, or %NULL
3620 * Finishes a mount operation. See g_file_mount_mountable() for details.
3622 * Finish an asynchronous mount operation that was started
3623 * with g_file_mount_mountable().
3625 * Returns: a #GFile or %NULL on error.
3628 g_file_mount_mountable_finish (GFile *file,
3629 GAsyncResult *result,
3634 g_return_val_if_fail (G_IS_FILE (file), NULL);
3635 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3637 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3639 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3640 if (g_simple_async_result_propagate_error (simple, error))
3644 iface = G_FILE_GET_IFACE (file);
3645 return (* iface->mount_mountable_finish) (file, result, error);
3649 * g_file_unmount_mountable:
3650 * @file: input #GFile.
3651 * @flags: flags affecting the operation
3652 * @cancellable: optional #GCancellable object, %NULL to ignore.
3653 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3654 * @user_data: the data to pass to callback function
3656 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3658 * If @cancellable is not %NULL, then the operation can be cancelled by
3659 * triggering the cancellable object from another thread. If the operation
3660 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3662 * When the operation is finished, @callback will be called. You can then call
3663 * g_file_unmount_mountable_finish() to get the result of the operation.
3666 g_file_unmount_mountable (GFile *file,
3667 GMountUnmountFlags flags,
3668 GCancellable *cancellable,
3669 GAsyncReadyCallback callback,
3674 g_return_if_fail (G_IS_FILE (file));
3676 iface = G_FILE_GET_IFACE (file);
3678 if (iface->unmount_mountable == NULL)
3680 g_simple_async_report_error_in_idle (G_OBJECT (file),
3684 G_IO_ERROR_NOT_SUPPORTED,
3685 _("Operation not supported"));
3689 (* iface->unmount_mountable) (file,
3697 * g_file_unmount_mountable_finish:
3698 * @file: input #GFile.
3699 * @result: a #GAsyncResult.
3700 * @error: a #GError, or %NULL
3702 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3704 * Finish an asynchronous unmount operation that was started
3705 * with g_file_unmount_mountable().
3707 * Returns: %TRUE if the operation finished successfully. %FALSE
3711 g_file_unmount_mountable_finish (GFile *file,
3712 GAsyncResult *result,
3717 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3718 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3720 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3722 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3723 if (g_simple_async_result_propagate_error (simple, error))
3727 iface = G_FILE_GET_IFACE (file);
3728 return (* iface->unmount_mountable_finish) (file, result, error);
3732 * g_file_eject_mountable:
3733 * @file: input #GFile.
3734 * @flags: flags affecting the operation
3735 * @cancellable: optional #GCancellable object, %NULL to ignore.
3736 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3737 * @user_data: the data to pass to callback function
3739 * Starts an asynchronous eject on a mountable.
3740 * When this operation has completed, @callback will be called with
3741 * @user_user data, and the operation can be finalized with
3742 * g_file_eject_mountable_finish().
3744 * If @cancellable is not %NULL, then the operation can be cancelled by
3745 * triggering the cancellable object from another thread. If the operation
3746 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3749 g_file_eject_mountable (GFile *file,
3750 GMountUnmountFlags flags,
3751 GCancellable *cancellable,
3752 GAsyncReadyCallback callback,
3757 g_return_if_fail (G_IS_FILE (file));
3759 iface = G_FILE_GET_IFACE (file);
3761 if (iface->eject_mountable == NULL)
3763 g_simple_async_report_error_in_idle (G_OBJECT (file),
3767 G_IO_ERROR_NOT_SUPPORTED,
3768 _("Operation not supported"));
3772 (* iface->eject_mountable) (file,
3780 * g_file_eject_mountable_finish:
3781 * @file: input #GFile.
3782 * @result: a #GAsyncResult.
3783 * @error: a #GError, or %NULL
3785 * Finishes an asynchronous eject operation started by
3786 * g_file_eject_mountable().
3788 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3792 g_file_eject_mountable_finish (GFile *file,
3793 GAsyncResult *result,
3798 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3799 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3801 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3803 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3804 if (g_simple_async_result_propagate_error (simple, error))
3808 iface = G_FILE_GET_IFACE (file);
3809 return (* iface->eject_mountable_finish) (file, result, error);
3813 * g_file_monitor_directory:
3814 * @file: input #GFile.
3815 * @flags: a set of #GFileMonitorFlags.
3816 * @cancellable: optional #GCancellable object, %NULL to ignore.
3817 * @error: a #GError, or %NULL.
3819 * Obtains a directory monitor for the given file.
3820 * This may fail if directory monitoring is not supported.
3822 * If @cancellable is not %NULL, then the operation can be cancelled by
3823 * triggering the cancellable object from another thread. If the operation
3824 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3826 * Returns: a #GFileMonitor for the given @file,
3827 * or %NULL on error.
3830 g_file_monitor_directory (GFile *file,
3831 GFileMonitorFlags flags,
3832 GCancellable *cancellable,
3837 g_return_val_if_fail (G_IS_FILE (file), NULL);
3839 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3842 iface = G_FILE_GET_IFACE (file);
3844 if (iface->monitor_dir == NULL)
3846 g_set_error_literal (error, G_IO_ERROR,
3847 G_IO_ERROR_NOT_SUPPORTED,
3848 _("Operation not supported"));
3852 return (* iface->monitor_dir) (file, flags, cancellable, error);
3856 * g_file_monitor_file:
3857 * @file: input #GFile.
3858 * @flags: a set of #GFileMonitorFlags.
3859 * @cancellable: optional #GCancellable object, %NULL to ignore.
3860 * @error: a #GError, or %NULL.
3862 * Obtains a file monitor for the given file. If no file notification
3863 * mechanism exists, then regular polling of the file is used.
3865 * If @cancellable is not %NULL, then the operation can be cancelled by
3866 * triggering the cancellable object from another thread. If the operation
3867 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3869 * Returns: a #GFileMonitor for the given @file.
3872 g_file_monitor_file (GFile *file,
3873 GFileMonitorFlags flags,
3874 GCancellable *cancellable,
3878 GFileMonitor *monitor;
3880 g_return_val_if_fail (G_IS_FILE (file), NULL);
3882 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3885 iface = G_FILE_GET_IFACE (file);
3889 if (iface->monitor_file)
3890 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3892 /* Fallback to polling */
3893 if (monitor == NULL)
3894 monitor = _g_poll_file_monitor_new (file);
3901 * @file: input #GFile
3902 * @flags: a set of #GFileMonitorFlags
3903 * @cancellable: optional #GCancellable object, %NULL to ignore
3904 * @error: a #GError, or %NULL
3906 * Obtains a file or directory monitor for the given file, depending
3907 * on the type of the file.
3909 * If @cancellable is not %NULL, then the operation can be cancelled by
3910 * triggering the cancellable object from another thread. If the operation
3911 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3913 * Returns: a #GFileMonitor for the given @file.
3918 g_file_monitor (GFile *file,
3919 GFileMonitorFlags flags,
3920 GCancellable *cancellable,
3923 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
3924 return g_file_monitor_directory (file, flags, cancellable, error);
3926 return g_file_monitor_file (file, flags, cancellable, error);
3929 /********************************************
3930 * Default implementation of async ops *
3931 ********************************************/
3935 GFileQueryInfoFlags flags;
3937 } QueryInfoAsyncData;
3940 query_info_data_free (QueryInfoAsyncData *data)
3943 g_object_unref (data->info);
3944 g_free (data->attributes);
3949 query_info_async_thread (GSimpleAsyncResult *res,
3951 GCancellable *cancellable)
3953 GError *error = NULL;
3954 QueryInfoAsyncData *data;
3957 data = g_simple_async_result_get_op_res_gpointer (res);
3959 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3963 g_simple_async_result_set_from_error (res, error);
3964 g_error_free (error);
3971 g_file_real_query_info_async (GFile *file,
3972 const char *attributes,
3973 GFileQueryInfoFlags flags,
3975 GCancellable *cancellable,
3976 GAsyncReadyCallback callback,
3979 GSimpleAsyncResult *res;
3980 QueryInfoAsyncData *data;
3982 data = g_new0 (QueryInfoAsyncData, 1);
3983 data->attributes = g_strdup (attributes);
3984 data->flags = flags;
3986 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3987 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3989 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3990 g_object_unref (res);
3994 g_file_real_query_info_finish (GFile *file,
3998 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3999 QueryInfoAsyncData *data;
4001 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4003 data = g_simple_async_result_get_op_res_gpointer (simple);
4005 return g_object_ref (data->info);
4013 } QueryFilesystemInfoAsyncData;
4016 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4019 g_object_unref (data->info);
4020 g_free (data->attributes);
4025 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4027 GCancellable *cancellable)
4029 GError *error = NULL;
4030 QueryFilesystemInfoAsyncData *data;
4033 data = g_simple_async_result_get_op_res_gpointer (res);
4035 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4039 g_simple_async_result_set_from_error (res, error);
4040 g_error_free (error);
4047 g_file_real_query_filesystem_info_async (GFile *file,
4048 const char *attributes,
4050 GCancellable *cancellable,
4051 GAsyncReadyCallback callback,
4054 GSimpleAsyncResult *res;
4055 QueryFilesystemInfoAsyncData *data;
4057 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4058 data->attributes = g_strdup (attributes);
4060 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4061 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4063 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4064 g_object_unref (res);
4068 g_file_real_query_filesystem_info_finish (GFile *file,
4072 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4073 QueryFilesystemInfoAsyncData *data;
4075 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4077 data = g_simple_async_result_get_op_res_gpointer (simple);
4079 return g_object_ref (data->info);
4086 GFileQueryInfoFlags flags;
4087 GFileEnumerator *enumerator;
4088 } EnumerateChildrenAsyncData;
4091 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4093 if (data->enumerator)
4094 g_object_unref (data->enumerator);
4095 g_free (data->attributes);
4100 enumerate_children_async_thread (GSimpleAsyncResult *res,
4102 GCancellable *cancellable)
4104 GError *error = NULL;
4105 EnumerateChildrenAsyncData *data;
4106 GFileEnumerator *enumerator;
4108 data = g_simple_async_result_get_op_res_gpointer (res);
4110 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4112 if (enumerator == NULL)
4114 g_simple_async_result_set_from_error (res, error);
4115 g_error_free (error);
4118 data->enumerator = enumerator;
4122 g_file_real_enumerate_children_async (GFile *file,
4123 const char *attributes,
4124 GFileQueryInfoFlags flags,
4126 GCancellable *cancellable,
4127 GAsyncReadyCallback callback,
4130 GSimpleAsyncResult *res;
4131 EnumerateChildrenAsyncData *data;
4133 data = g_new0 (EnumerateChildrenAsyncData, 1);
4134 data->attributes = g_strdup (attributes);
4135 data->flags = flags;
4137 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4138 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4140 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4141 g_object_unref (res);
4144 static GFileEnumerator *
4145 g_file_real_enumerate_children_finish (GFile *file,
4149 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4150 EnumerateChildrenAsyncData *data;
4152 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4154 data = g_simple_async_result_get_op_res_gpointer (simple);
4155 if (data->enumerator)
4156 return g_object_ref (data->enumerator);
4162 open_read_async_thread (GSimpleAsyncResult *res,
4164 GCancellable *cancellable)
4167 GFileInputStream *stream;
4168 GError *error = NULL;
4170 iface = G_FILE_GET_IFACE (object);
4172 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4176 g_simple_async_result_set_from_error (res, error);
4177 g_error_free (error);
4180 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4184 g_file_real_read_async (GFile *file,
4186 GCancellable *cancellable,
4187 GAsyncReadyCallback callback,
4190 GSimpleAsyncResult *res;
4192 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4194 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4195 g_object_unref (res);
4198 static GFileInputStream *
4199 g_file_real_read_finish (GFile *file,
4203 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4206 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4208 op = g_simple_async_result_get_op_res_gpointer (simple);
4210 return g_object_ref (op);
4216 append_to_async_thread (GSimpleAsyncResult *res,
4218 GCancellable *cancellable)
4221 GFileCreateFlags *data;
4222 GFileOutputStream *stream;
4223 GError *error = NULL;
4225 iface = G_FILE_GET_IFACE (object);
4227 data = g_simple_async_result_get_op_res_gpointer (res);
4229 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4233 g_simple_async_result_set_from_error (res, error);
4234 g_error_free (error);
4237 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4241 g_file_real_append_to_async (GFile *file,
4242 GFileCreateFlags flags,
4244 GCancellable *cancellable,
4245 GAsyncReadyCallback callback,
4248 GFileCreateFlags *data;
4249 GSimpleAsyncResult *res;
4251 data = g_new0 (GFileCreateFlags, 1);
4254 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4255 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4257 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4258 g_object_unref (res);
4261 static GFileOutputStream *
4262 g_file_real_append_to_finish (GFile *file,
4266 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4269 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4271 op = g_simple_async_result_get_op_res_gpointer (simple);
4273 return g_object_ref (op);
4279 create_async_thread (GSimpleAsyncResult *res,
4281 GCancellable *cancellable)
4284 GFileCreateFlags *data;
4285 GFileOutputStream *stream;
4286 GError *error = NULL;
4288 iface = G_FILE_GET_IFACE (object);
4290 data = g_simple_async_result_get_op_res_gpointer (res);
4292 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4296 g_simple_async_result_set_from_error (res, error);
4297 g_error_free (error);
4300 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4304 g_file_real_create_async (GFile *file,
4305 GFileCreateFlags flags,
4307 GCancellable *cancellable,
4308 GAsyncReadyCallback callback,
4311 GFileCreateFlags *data;
4312 GSimpleAsyncResult *res;
4314 data = g_new0 (GFileCreateFlags, 1);
4317 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4318 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4320 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4321 g_object_unref (res);
4324 static GFileOutputStream *
4325 g_file_real_create_finish (GFile *file,
4329 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4332 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4334 op = g_simple_async_result_get_op_res_gpointer (simple);
4336 return g_object_ref (op);
4342 GFileOutputStream *stream;
4344 gboolean make_backup;
4345 GFileCreateFlags flags;
4349 replace_async_data_free (ReplaceAsyncData *data)
4352 g_object_unref (data->stream);
4353 g_free (data->etag);
4358 replace_async_thread (GSimpleAsyncResult *res,
4360 GCancellable *cancellable)
4363 GFileOutputStream *stream;
4364 GError *error = NULL;
4365 ReplaceAsyncData *data;
4367 iface = G_FILE_GET_IFACE (object);
4369 data = g_simple_async_result_get_op_res_gpointer (res);
4371 stream = iface->replace (G_FILE (object),
4380 g_simple_async_result_set_from_error (res, error);
4381 g_error_free (error);
4384 data->stream = stream;
4388 g_file_real_replace_async (GFile *file,
4390 gboolean make_backup,
4391 GFileCreateFlags flags,
4393 GCancellable *cancellable,
4394 GAsyncReadyCallback callback,
4397 GSimpleAsyncResult *res;
4398 ReplaceAsyncData *data;
4400 data = g_new0 (ReplaceAsyncData, 1);
4401 data->etag = g_strdup (etag);
4402 data->make_backup = make_backup;
4403 data->flags = flags;
4405 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4406 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4408 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4409 g_object_unref (res);
4412 static GFileOutputStream *
4413 g_file_real_replace_finish (GFile *file,
4417 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4418 ReplaceAsyncData *data;
4420 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4422 data = g_simple_async_result_get_op_res_gpointer (simple);
4424 return g_object_ref (data->stream);
4432 } SetDisplayNameAsyncData;
4435 set_display_name_data_free (SetDisplayNameAsyncData *data)
4437 g_free (data->name);
4439 g_object_unref (data->file);
4444 set_display_name_async_thread (GSimpleAsyncResult *res,
4446 GCancellable *cancellable)
4448 GError *error = NULL;
4449 SetDisplayNameAsyncData *data;
4452 data = g_simple_async_result_get_op_res_gpointer (res);
4454 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4458 g_simple_async_result_set_from_error (res, error);
4459 g_error_free (error);
4466 g_file_real_set_display_name_async (GFile *file,
4467 const char *display_name,
4469 GCancellable *cancellable,
4470 GAsyncReadyCallback callback,
4473 GSimpleAsyncResult *res;
4474 SetDisplayNameAsyncData *data;
4476 data = g_new0 (SetDisplayNameAsyncData, 1);
4477 data->name = g_strdup (display_name);
4479 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4480 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4482 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4483 g_object_unref (res);
4487 g_file_real_set_display_name_finish (GFile *file,
4491 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4492 SetDisplayNameAsyncData *data;
4494 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4496 data = g_simple_async_result_get_op_res_gpointer (simple);
4498 return g_object_ref (data->file);
4504 GFileQueryInfoFlags flags;
4511 set_info_data_free (SetInfoAsyncData *data)
4514 g_object_unref (data->info);
4516 g_error_free (data->error);
4521 set_info_async_thread (GSimpleAsyncResult *res,
4523 GCancellable *cancellable)
4525 SetInfoAsyncData *data;
4527 data = g_simple_async_result_get_op_res_gpointer (res);
4530 data->res = g_file_set_attributes_from_info (G_FILE (object),
4538 g_file_real_set_attributes_async (GFile *file,
4540 GFileQueryInfoFlags flags,
4542 GCancellable *cancellable,
4543 GAsyncReadyCallback callback,
4546 GSimpleAsyncResult *res;
4547 SetInfoAsyncData *data;
4549 data = g_new0 (SetInfoAsyncData, 1);
4550 data->info = g_file_info_dup (info);
4551 data->flags = flags;
4553 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4554 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4556 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4557 g_object_unref (res);
4561 g_file_real_set_attributes_finish (GFile *file,
4566 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4567 SetInfoAsyncData *data;
4569 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4571 data = g_simple_async_result_get_op_res_gpointer (simple);
4574 *info = g_object_ref (data->info);
4576 if (error != NULL && data->error)
4577 *error = g_error_copy (data->error);
4583 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4585 GCancellable *cancellable)
4587 GError *error = NULL;
4590 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4594 g_simple_async_result_set_from_error (res, error);
4595 g_error_free (error);
4598 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4602 g_file_real_find_enclosing_mount_async (GFile *file,
4604 GCancellable *cancellable,
4605 GAsyncReadyCallback callback,
4608 GSimpleAsyncResult *res;
4610 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4612 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4613 g_object_unref (res);
4617 g_file_real_find_enclosing_mount_finish (GFile *file,
4621 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4624 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4626 mount = g_simple_async_result_get_op_res_gpointer (simple);
4627 return g_object_ref (mount);
4634 GFileCopyFlags flags;
4635 GFileProgressCallback progress_cb;
4636 gpointer progress_cb_data;
4637 GIOSchedulerJob *job;
4641 copy_async_data_free (CopyAsyncData *data)
4643 g_object_unref (data->source);
4644 g_object_unref (data->destination);
4649 CopyAsyncData *data;
4650 goffset current_num_bytes;
4651 goffset total_num_bytes;
4655 copy_async_progress_in_main (gpointer user_data)
4657 ProgressData *progress = user_data;
4658 CopyAsyncData *data = progress->data;
4660 data->progress_cb (progress->current_num_bytes,
4661 progress->total_num_bytes,
4662 data->progress_cb_data);
4668 mainloop_barrier (gpointer user_data)
4670 /* Does nothing, but ensures all queued idles before
4677 copy_async_progress_callback (goffset current_num_bytes,
4678 goffset total_num_bytes,
4681 CopyAsyncData *data = user_data;
4682 ProgressData *progress;
4684 progress = g_new (ProgressData, 1);
4685 progress->data = data;
4686 progress->current_num_bytes = current_num_bytes;
4687 progress->total_num_bytes = total_num_bytes;
4689 g_io_scheduler_job_send_to_mainloop_async (data->job,
4690 copy_async_progress_in_main,
4696 copy_async_thread (GIOSchedulerJob *job,
4697 GCancellable *cancellable,
4700 GSimpleAsyncResult *res;
4701 CopyAsyncData *data;
4706 data = g_simple_async_result_get_op_res_gpointer (res);
4710 result = g_file_copy (data->source,
4714 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4718 /* Ensure all progress callbacks are done running in main thread */
4719 if (data->progress_cb != NULL)
4720 g_io_scheduler_job_send_to_mainloop (job,
4726 g_simple_async_result_set_from_error (res, error);
4727 g_error_free (error);
4730 g_simple_async_result_complete_in_idle (res);
4736 g_file_real_copy_async (GFile *source,
4738 GFileCopyFlags flags,
4740 GCancellable *cancellable,
4741 GFileProgressCallback progress_callback,
4742 gpointer progress_callback_data,
4743 GAsyncReadyCallback callback,
4746 GSimpleAsyncResult *res;
4747 CopyAsyncData *data;
4749 data = g_new0 (CopyAsyncData, 1);
4750 data->source = g_object_ref (source);
4751 data->destination = g_object_ref (destination);
4752 data->flags = flags;
4753 data->progress_cb = progress_callback;
4754 data->progress_cb_data = progress_callback_data;
4756 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4757 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4759 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4763 g_file_real_copy_finish (GFile *file,
4767 /* Error handled in g_file_copy_finish() */
4772 /********************************************
4773 * Default VFS operations *
4774 ********************************************/
4777 * g_file_new_for_path:
4778 * @path: a string containing a relative or absolute path.
4780 * Constructs a #GFile for a given path. This operation never
4781 * fails, but the returned object might not support any I/O
4782 * operation if @path is malformed.
4784 * Returns: a new #GFile for the given @path.
4787 g_file_new_for_path (const char *path)
4789 g_return_val_if_fail (path != NULL, NULL);
4791 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4795 * g_file_new_for_uri:
4796 * @uri: a string containing a URI.
4798 * Constructs a #GFile for a given URI. This operation never
4799 * fails, but the returned object might not support any I/O
4800 * operation if @uri is malformed or if the uri type is
4803 * Returns: a #GFile for the given @uri.
4806 g_file_new_for_uri (const char *uri)
4808 g_return_val_if_fail (uri != NULL, NULL);
4810 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4814 * g_file_parse_name:
4815 * @parse_name: a file name or path to be parsed.
4817 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4818 * This operation never fails, but the returned object might not support any I/O
4819 * operation if the @parse_name cannot be parsed.
4821 * Returns: a new #GFile.
4824 g_file_parse_name (const char *parse_name)
4826 g_return_val_if_fail (parse_name != NULL, NULL);
4828 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4832 is_valid_scheme_character (char c)
4834 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4837 /* Following RFC 2396, valid schemes are built like:
4838 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
4841 has_valid_scheme (const char *uri)
4847 if (!g_ascii_isalpha (*p))
4852 } while (is_valid_scheme_character (*p));
4858 * g_file_new_for_commandline_arg:
4859 * @arg: a command line string.
4861 * Creates a #GFile with the given argument from the command line. The value of
4862 * @arg can be either a URI, an absolute path or a relative path resolved
4863 * relative to the current working directory.
4864 * This operation never fails, but the returned object might not support any
4865 * I/O operation if @arg points to a malformed path.
4867 * Returns: a new #GFile.
4870 g_file_new_for_commandline_arg (const char *arg)
4876 g_return_val_if_fail (arg != NULL, NULL);
4878 if (g_path_is_absolute (arg))
4879 return g_file_new_for_path (arg);
4881 if (has_valid_scheme (arg))
4882 return g_file_new_for_uri (arg);
4884 current_dir = g_get_current_dir ();
4885 filename = g_build_filename (current_dir, arg, NULL);
4886 g_free (current_dir);
4888 file = g_file_new_for_path (filename);
4895 * g_file_mount_enclosing_volume:
4896 * @location: input #GFile.
4897 * @flags: flags affecting the operation
4898 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4899 * @cancellable: optional #GCancellable object, %NULL to ignore.
4900 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4901 * @user_data: the data to pass to callback function
4903 * Starts a @mount_operation, mounting the volume that contains the file @location.
4905 * When this operation has completed, @callback will be called with
4906 * @user_user data, and the operation can be finalized with
4907 * g_file_mount_enclosing_volume_finish().
4909 * If @cancellable is not %NULL, then the operation can be cancelled by
4910 * triggering the cancellable object from another thread. If the operation
4911 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4914 g_file_mount_enclosing_volume (GFile *location,
4915 GMountMountFlags flags,
4916 GMountOperation *mount_operation,
4917 GCancellable *cancellable,
4918 GAsyncReadyCallback callback,
4923 g_return_if_fail (G_IS_FILE (location));
4925 iface = G_FILE_GET_IFACE (location);
4927 if (iface->mount_enclosing_volume == NULL)
4929 g_simple_async_report_error_in_idle (G_OBJECT (location),
4930 callback, user_data,
4931 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4932 _("volume doesn't implement mount"));
4937 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4942 * g_file_mount_enclosing_volume_finish:
4943 * @location: input #GFile.
4944 * @result: a #GAsyncResult.
4945 * @error: a #GError, or %NULL
4947 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4949 * Returns: %TRUE if successful. If an error
4950 * has occurred, this function will return %FALSE and set @error
4951 * appropriately if present.
4954 g_file_mount_enclosing_volume_finish (GFile *location,
4955 GAsyncResult *result,
4960 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4961 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4963 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4965 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4966 if (g_simple_async_result_propagate_error (simple, error))
4970 iface = G_FILE_GET_IFACE (location);
4972 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4975 /********************************************
4976 * Utility functions *
4977 ********************************************/
4980 * g_file_query_default_handler:
4981 * @file: a #GFile to open.
4982 * @cancellable: optional #GCancellable object, %NULL to ignore.
4983 * @error: a #GError, or %NULL
4985 * Returns the #GAppInfo that is registered as the default
4986 * application to handle the file specified by @file.
4988 * If @cancellable is not %NULL, then the operation can be cancelled by
4989 * triggering the cancellable object from another thread. If the operation
4990 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4992 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4993 * When you are done with it, release it with g_object_unref()
4996 g_file_query_default_handler (GFile *file,
4997 GCancellable *cancellable,
5001 const char *content_type;
5006 uri_scheme = g_file_get_uri_scheme (file);
5007 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
5008 g_free (uri_scheme);
5010 if (appinfo != NULL)
5013 info = g_file_query_info (file,
5014 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
5023 content_type = g_file_info_get_content_type (info);
5026 /* Don't use is_native(), as we want to support fuse paths if availible */
5027 path = g_file_get_path (file);
5028 appinfo = g_app_info_get_default_for_type (content_type,
5033 g_object_unref (info);
5035 if (appinfo != NULL)
5038 g_set_error_literal (error, G_IO_ERROR,
5039 G_IO_ERROR_NOT_SUPPORTED,
5040 _("No application is registered as handling this file"));
5046 #define GET_CONTENT_BLOCK_SIZE 8192
5049 * g_file_load_contents:
5050 * @file: input #GFile.
5051 * @cancellable: optional #GCancellable object, %NULL to ignore.
5052 * @contents: a location to place the contents of the file.
5053 * @length: a location to place the length of the contents of the file.
5054 * @etag_out: a location to place the current entity tag for the file.
5055 * @error: a #GError, or %NULL
5057 * Loads the content of the file into memory, returning the size of
5058 * the data. The data is always zero terminated, but this is not
5059 * included in the resultant @length.
5061 * If @cancellable is not %NULL, then the operation can be cancelled by
5062 * triggering the cancellable object from another thread. If the operation
5063 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5065 * Returns: %TRUE if the @file's contents were successfully loaded.
5066 * %FALSE if there were errors..
5069 g_file_load_contents (GFile *file,
5070 GCancellable *cancellable,
5076 GFileInputStream *in;
5077 GByteArray *content;
5082 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5083 g_return_val_if_fail (contents != NULL, FALSE);
5085 in = g_file_read (file, cancellable, error);
5089 content = g_byte_array_new ();
5092 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5093 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
5094 content->data + pos,
5095 GET_CONTENT_BLOCK_SIZE,
5096 cancellable, error)) > 0)
5099 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5106 info = g_file_input_stream_query_info (in,
5107 G_FILE_ATTRIBUTE_ETAG_VALUE,
5112 *etag_out = g_strdup (g_file_info_get_etag (info));
5113 g_object_unref (info);
5117 /* Ignore errors on close */
5118 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
5119 g_object_unref (in);
5123 /* error is set already */
5124 g_byte_array_free (content, TRUE);
5131 /* Zero terminate (we got an extra byte allocated for this */
5132 content->data[pos] = 0;
5134 *contents = (char *)g_byte_array_free (content, FALSE);
5142 GCancellable *cancellable;
5143 GFileReadMoreCallback read_more_callback;
5144 GAsyncReadyCallback callback;
5146 GByteArray *content;
5153 load_contents_data_free (LoadContentsData *data)
5156 g_error_free (data->error);
5157 if (data->cancellable)
5158 g_object_unref (data->cancellable);
5160 g_byte_array_free (data->content, TRUE);
5161 g_free (data->etag);
5162 g_object_unref (data->file);
5167 load_contents_close_callback (GObject *obj,
5168 GAsyncResult *close_res,
5171 GInputStream *stream = G_INPUT_STREAM (obj);
5172 LoadContentsData *data = user_data;
5173 GSimpleAsyncResult *res;
5175 /* Ignore errors here, we're only reading anyway */
5176 g_input_stream_close_finish (stream, close_res, NULL);
5177 g_object_unref (stream);
5179 res = g_simple_async_result_new (G_OBJECT (data->file),
5182 g_file_load_contents_async);
5183 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5184 g_simple_async_result_complete (res);
5185 g_object_unref (res);
5189 load_contents_fstat_callback (GObject *obj,
5190 GAsyncResult *stat_res,
5193 GInputStream *stream = G_INPUT_STREAM (obj);
5194 LoadContentsData *data = user_data;
5197 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5201 data->etag = g_strdup (g_file_info_get_etag (info));
5202 g_object_unref (info);
5205 g_input_stream_close_async (stream, 0,
5207 load_contents_close_callback, data);
5211 load_contents_read_callback (GObject *obj,
5212 GAsyncResult *read_res,
5215 GInputStream *stream = G_INPUT_STREAM (obj);
5216 LoadContentsData *data = user_data;
5217 GError *error = NULL;
5220 read_size = g_input_stream_read_finish (stream, read_res, &error);
5224 /* Error or EOF, close the file */
5225 data->error = error;
5226 g_input_stream_close_async (stream, 0,
5228 load_contents_close_callback, data);
5230 else if (read_size == 0)
5232 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5233 G_FILE_ATTRIBUTE_ETAG_VALUE,
5236 load_contents_fstat_callback,
5239 else if (read_size > 0)
5241 data->pos += read_size;
5243 g_byte_array_set_size (data->content,
5244 data->pos + GET_CONTENT_BLOCK_SIZE);
5247 if (data->read_more_callback &&
5248 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5249 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5250 G_FILE_ATTRIBUTE_ETAG_VALUE,
5253 load_contents_fstat_callback,
5256 g_input_stream_read_async (stream,
5257 data->content->data + data->pos,
5258 GET_CONTENT_BLOCK_SIZE,
5261 load_contents_read_callback,
5267 load_contents_open_callback (GObject *obj,
5268 GAsyncResult *open_res,
5271 GFile *file = G_FILE (obj);
5272 GFileInputStream *stream;
5273 LoadContentsData *data = user_data;
5274 GError *error = NULL;
5275 GSimpleAsyncResult *res;
5277 stream = g_file_read_finish (file, open_res, &error);
5281 g_byte_array_set_size (data->content,
5282 data->pos + GET_CONTENT_BLOCK_SIZE);
5283 g_input_stream_read_async (G_INPUT_STREAM (stream),
5284 data->content->data + data->pos,
5285 GET_CONTENT_BLOCK_SIZE,
5288 load_contents_read_callback,
5294 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5298 g_simple_async_result_complete (res);
5299 g_error_free (error);
5300 load_contents_data_free (data);
5301 g_object_unref (res);
5306 * g_file_load_partial_contents_async:
5307 * @file: input #GFile.
5308 * @cancellable: optional #GCancellable object, %NULL to ignore.
5309 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5310 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5311 * @user_data: the data to pass to the callback functions.
5313 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5314 * used to stop reading from the file when appropriate, else this function
5315 * will behave exactly as g_file_load_contents_async(). This operation
5316 * can be finished by g_file_load_partial_contents_finish().
5318 * Users of this function should be aware that @user_data is passed to
5319 * both the @read_more_callback and the @callback.
5321 * If @cancellable is not %NULL, then the operation can be cancelled by
5322 * triggering the cancellable object from another thread. If the operation
5323 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5326 g_file_load_partial_contents_async (GFile *file,
5327 GCancellable *cancellable,
5328 GFileReadMoreCallback read_more_callback,
5329 GAsyncReadyCallback callback,
5332 LoadContentsData *data;
5334 g_return_if_fail (G_IS_FILE (file));
5336 data = g_new0 (LoadContentsData, 1);
5339 data->cancellable = g_object_ref (cancellable);
5340 data->read_more_callback = read_more_callback;
5341 data->callback = callback;
5342 data->user_data = user_data;
5343 data->content = g_byte_array_new ();
5344 data->file = g_object_ref (file);
5346 g_file_read_async (file,
5349 load_contents_open_callback,
5354 * g_file_load_partial_contents_finish:
5355 * @file: input #GFile.
5356 * @res: a #GAsyncResult.
5357 * @contents: a location to place the contents of the file.
5358 * @length: a location to place the length of the contents of the file.
5359 * @etag_out: a location to place the current entity tag for the file.
5360 * @error: a #GError, or %NULL
5362 * Finishes an asynchronous partial load operation that was started
5363 * with g_file_load_partial_contents_async().
5365 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5366 * present, it will be set appropriately.
5369 g_file_load_partial_contents_finish (GFile *file,
5376 GSimpleAsyncResult *simple;
5377 LoadContentsData *data;
5379 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5380 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5381 g_return_val_if_fail (contents != NULL, FALSE);
5383 simple = G_SIMPLE_ASYNC_RESULT (res);
5385 if (g_simple_async_result_propagate_error (simple, error))
5388 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5390 data = g_simple_async_result_get_op_res_gpointer (simple);
5394 g_propagate_error (error, data->error);
5403 *length = data->pos;
5407 *etag_out = data->etag;
5411 /* Zero terminate */
5412 g_byte_array_set_size (data->content, data->pos + 1);
5413 data->content->data[data->pos] = 0;
5415 *contents = (char *)g_byte_array_free (data->content, FALSE);
5416 data->content = NULL;
5422 * g_file_load_contents_async:
5423 * @file: input #GFile.
5424 * @cancellable: optional #GCancellable object, %NULL to ignore.
5425 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5426 * @user_data: the data to pass to callback function
5428 * Starts an asynchronous load of the @file's contents.
5430 * For more details, see g_file_load_contents() which is
5431 * the synchronous version of this call.
5433 * When the load operation has completed, @callback will be called
5434 * with @user data. To finish the operation, call
5435 * g_file_load_contents_finish() with the #GAsyncResult returned by
5438 * If @cancellable is not %NULL, then the operation can be cancelled by
5439 * triggering the cancellable object from another thread. If the operation
5440 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5443 g_file_load_contents_async (GFile *file,
5444 GCancellable *cancellable,
5445 GAsyncReadyCallback callback,
5448 g_file_load_partial_contents_async (file,
5451 callback, user_data);
5455 * g_file_load_contents_finish:
5456 * @file: input #GFile.
5457 * @res: a #GAsyncResult.
5458 * @contents: a location to place the contents of the file.
5459 * @length: a location to place the length of the contents of the file.
5460 * @etag_out: a location to place the current entity tag for the file.
5461 * @error: a #GError, or %NULL
5463 * Finishes an asynchronous load of the @file's contents.
5464 * The contents are placed in @contents, and @length is set to the
5465 * size of the @contents string. If @etag_out is present, it will be
5466 * set to the new entity tag for the @file.
5468 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5469 * present, it will be set appropriately.
5472 g_file_load_contents_finish (GFile *file,
5479 return g_file_load_partial_contents_finish (file,
5488 * g_file_replace_contents:
5489 * @file: input #GFile.
5490 * @contents: a string containing the new contents for @file.
5491 * @length: the length of @contents in bytes.
5492 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5494 * @make_backup: %TRUE if a backup should be created.
5495 * @flags: a set of #GFileCreateFlags.
5496 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5497 * for the document. This should be freed with g_free() when no longer
5499 * @cancellable: optional #GCancellable object, %NULL to ignore.
5500 * @error: a #GError, or %NULL
5502 * Replaces the contents of @file with @contents of @length bytes.
5504 * If @etag is specified (not %NULL) any existing file must have that etag, or
5505 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5507 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5509 * If @cancellable is not %NULL, then the operation can be cancelled by
5510 * triggering the cancellable object from another thread. If the operation
5511 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5513 * The returned @new_etag can be used to verify that the file hasn't changed the
5514 * next time it is saved over.
5516 * Returns: %TRUE if successful. If an error
5517 * has occurred, this function will return %FALSE and set @error
5518 * appropriately if present.
5521 g_file_replace_contents (GFile *file,
5522 const char *contents,
5525 gboolean make_backup,
5526 GFileCreateFlags flags,
5528 GCancellable *cancellable,
5531 GFileOutputStream *out;
5532 gsize pos, remainder;
5536 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5537 g_return_val_if_fail (contents != NULL, FALSE);
5539 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5545 while (remainder > 0 &&
5546 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5548 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5556 if (remainder > 0 && res < 0)
5558 /* Ignore errors on close */
5559 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5560 g_object_unref (out);
5562 /* error is set already */
5566 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
5569 *new_etag = g_file_output_stream_get_etag (out);
5571 g_object_unref (out);
5579 GCancellable *cancellable;
5580 GAsyncReadyCallback callback;
5582 const char *content;
5586 } ReplaceContentsData;
5589 replace_contents_data_free (ReplaceContentsData *data)
5592 g_error_free (data->error);
5593 if (data->cancellable)
5594 g_object_unref (data->cancellable);
5595 g_object_unref (data->file);
5596 g_free (data->etag);
5601 replace_contents_close_callback (GObject *obj,
5602 GAsyncResult *close_res,
5605 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5606 ReplaceContentsData *data = user_data;
5607 GSimpleAsyncResult *res;
5609 /* Ignore errors here, we're only reading anyway */
5610 g_output_stream_close_finish (stream, close_res, NULL);
5611 g_object_unref (stream);
5613 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5615 res = g_simple_async_result_new (G_OBJECT (data->file),
5618 g_file_replace_contents_async);
5619 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5620 g_simple_async_result_complete (res);
5621 g_object_unref (res);
5625 replace_contents_write_callback (GObject *obj,
5626 GAsyncResult *read_res,
5629 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5630 ReplaceContentsData *data = user_data;
5631 GError *error = NULL;
5634 write_size = g_output_stream_write_finish (stream, read_res, &error);
5636 if (write_size <= 0)
5638 /* Error or EOF, close the file */
5640 data->error = error;
5641 g_output_stream_close_async (stream, 0,
5643 replace_contents_close_callback, data);
5645 else if (write_size > 0)
5647 data->pos += write_size;
5649 if (data->pos >= data->length)
5650 g_output_stream_close_async (stream, 0,
5652 replace_contents_close_callback, data);
5654 g_output_stream_write_async (stream,
5655 data->content + data->pos,
5656 data->length - data->pos,
5659 replace_contents_write_callback,
5665 replace_contents_open_callback (GObject *obj,
5666 GAsyncResult *open_res,
5669 GFile *file = G_FILE (obj);
5670 GFileOutputStream *stream;
5671 ReplaceContentsData *data = user_data;
5672 GError *error = NULL;
5673 GSimpleAsyncResult *res;
5675 stream = g_file_replace_finish (file, open_res, &error);
5679 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5680 data->content + data->pos,
5681 data->length - data->pos,
5684 replace_contents_write_callback,
5690 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5694 g_simple_async_result_complete (res);
5695 g_error_free (error);
5696 replace_contents_data_free (data);
5697 g_object_unref (res);
5702 * g_file_replace_contents_async:
5703 * @file: input #GFile.
5704 * @contents: string of contents to replace the file with.
5705 * @length: the length of @contents in bytes.
5706 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5707 * @make_backup: %TRUE if a backup should be created.
5708 * @flags: a set of #GFileCreateFlags.
5709 * @cancellable: optional #GCancellable object, %NULL to ignore.
5710 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5711 * @user_data: the data to pass to callback function
5713 * Starts an asynchronous replacement of @file with the given
5714 * @contents of @length bytes. @etag will replace the document's
5715 * current entity tag.
5717 * When this operation has completed, @callback will be called with
5718 * @user_user data, and the operation can be finalized with
5719 * g_file_replace_contents_finish().
5721 * If @cancellable is not %NULL, then the operation can be cancelled by
5722 * triggering the cancellable object from another thread. If the operation
5723 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5725 * If @make_backup is %TRUE, this function will attempt to
5726 * make a backup of @file.
5729 g_file_replace_contents_async (GFile *file,
5730 const char *contents,
5733 gboolean make_backup,
5734 GFileCreateFlags flags,
5735 GCancellable *cancellable,
5736 GAsyncReadyCallback callback,
5739 ReplaceContentsData *data;
5741 g_return_if_fail (G_IS_FILE (file));
5742 g_return_if_fail (contents != NULL);
5744 data = g_new0 (ReplaceContentsData, 1);
5747 data->cancellable = g_object_ref (cancellable);
5748 data->callback = callback;
5749 data->user_data = user_data;
5750 data->content = contents;
5751 data->length = length;
5753 data->file = g_object_ref (file);
5755 g_file_replace_async (file,
5761 replace_contents_open_callback,
5766 * g_file_replace_contents_finish:
5767 * @file: input #GFile.
5768 * @res: a #GAsyncResult.
5769 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5770 * for the document. This should be freed with g_free() when it is no
5772 * @error: a #GError, or %NULL
5774 * Finishes an asynchronous replace of the given @file. See
5775 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5776 * tag for the document, if present.
5778 * Returns: %TRUE on success, %FALSE on failure.
5781 g_file_replace_contents_finish (GFile *file,
5786 GSimpleAsyncResult *simple;
5787 ReplaceContentsData *data;
5789 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5790 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5792 simple = G_SIMPLE_ASYNC_RESULT (res);
5794 if (g_simple_async_result_propagate_error (simple, error))
5797 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5799 data = g_simple_async_result_get_op_res_gpointer (simple);
5803 g_propagate_error (error, data->error);
5811 *new_etag = data->etag;
5812 data->etag = NULL; /* Take ownership */
5818 #define __G_FILE_C__
5819 #include "gioaliasdef.c"