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);
3899 /********************************************
3900 * Default implementation of async ops *
3901 ********************************************/
3905 GFileQueryInfoFlags flags;
3907 } QueryInfoAsyncData;
3910 query_info_data_free (QueryInfoAsyncData *data)
3913 g_object_unref (data->info);
3914 g_free (data->attributes);
3919 query_info_async_thread (GSimpleAsyncResult *res,
3921 GCancellable *cancellable)
3923 GError *error = NULL;
3924 QueryInfoAsyncData *data;
3927 data = g_simple_async_result_get_op_res_gpointer (res);
3929 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3933 g_simple_async_result_set_from_error (res, error);
3934 g_error_free (error);
3941 g_file_real_query_info_async (GFile *file,
3942 const char *attributes,
3943 GFileQueryInfoFlags flags,
3945 GCancellable *cancellable,
3946 GAsyncReadyCallback callback,
3949 GSimpleAsyncResult *res;
3950 QueryInfoAsyncData *data;
3952 data = g_new0 (QueryInfoAsyncData, 1);
3953 data->attributes = g_strdup (attributes);
3954 data->flags = flags;
3956 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3957 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3959 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3960 g_object_unref (res);
3964 g_file_real_query_info_finish (GFile *file,
3968 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3969 QueryInfoAsyncData *data;
3971 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3973 data = g_simple_async_result_get_op_res_gpointer (simple);
3975 return g_object_ref (data->info);
3983 } QueryFilesystemInfoAsyncData;
3986 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
3989 g_object_unref (data->info);
3990 g_free (data->attributes);
3995 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
3997 GCancellable *cancellable)
3999 GError *error = NULL;
4000 QueryFilesystemInfoAsyncData *data;
4003 data = g_simple_async_result_get_op_res_gpointer (res);
4005 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4009 g_simple_async_result_set_from_error (res, error);
4010 g_error_free (error);
4017 g_file_real_query_filesystem_info_async (GFile *file,
4018 const char *attributes,
4020 GCancellable *cancellable,
4021 GAsyncReadyCallback callback,
4024 GSimpleAsyncResult *res;
4025 QueryFilesystemInfoAsyncData *data;
4027 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4028 data->attributes = g_strdup (attributes);
4030 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4031 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4033 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4034 g_object_unref (res);
4038 g_file_real_query_filesystem_info_finish (GFile *file,
4042 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4043 QueryFilesystemInfoAsyncData *data;
4045 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4047 data = g_simple_async_result_get_op_res_gpointer (simple);
4049 return g_object_ref (data->info);
4056 GFileQueryInfoFlags flags;
4057 GFileEnumerator *enumerator;
4058 } EnumerateChildrenAsyncData;
4061 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4063 if (data->enumerator)
4064 g_object_unref (data->enumerator);
4065 g_free (data->attributes);
4070 enumerate_children_async_thread (GSimpleAsyncResult *res,
4072 GCancellable *cancellable)
4074 GError *error = NULL;
4075 EnumerateChildrenAsyncData *data;
4076 GFileEnumerator *enumerator;
4078 data = g_simple_async_result_get_op_res_gpointer (res);
4080 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4082 if (enumerator == NULL)
4084 g_simple_async_result_set_from_error (res, error);
4085 g_error_free (error);
4088 data->enumerator = enumerator;
4092 g_file_real_enumerate_children_async (GFile *file,
4093 const char *attributes,
4094 GFileQueryInfoFlags flags,
4096 GCancellable *cancellable,
4097 GAsyncReadyCallback callback,
4100 GSimpleAsyncResult *res;
4101 EnumerateChildrenAsyncData *data;
4103 data = g_new0 (EnumerateChildrenAsyncData, 1);
4104 data->attributes = g_strdup (attributes);
4105 data->flags = flags;
4107 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4108 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4110 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4111 g_object_unref (res);
4114 static GFileEnumerator *
4115 g_file_real_enumerate_children_finish (GFile *file,
4119 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4120 EnumerateChildrenAsyncData *data;
4122 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4124 data = g_simple_async_result_get_op_res_gpointer (simple);
4125 if (data->enumerator)
4126 return g_object_ref (data->enumerator);
4132 open_read_async_thread (GSimpleAsyncResult *res,
4134 GCancellable *cancellable)
4137 GFileInputStream *stream;
4138 GError *error = NULL;
4140 iface = G_FILE_GET_IFACE (object);
4142 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4146 g_simple_async_result_set_from_error (res, error);
4147 g_error_free (error);
4150 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4154 g_file_real_read_async (GFile *file,
4156 GCancellable *cancellable,
4157 GAsyncReadyCallback callback,
4160 GSimpleAsyncResult *res;
4162 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4164 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4165 g_object_unref (res);
4168 static GFileInputStream *
4169 g_file_real_read_finish (GFile *file,
4173 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4176 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4178 op = g_simple_async_result_get_op_res_gpointer (simple);
4180 return g_object_ref (op);
4186 append_to_async_thread (GSimpleAsyncResult *res,
4188 GCancellable *cancellable)
4191 GFileCreateFlags *data;
4192 GFileOutputStream *stream;
4193 GError *error = NULL;
4195 iface = G_FILE_GET_IFACE (object);
4197 data = g_simple_async_result_get_op_res_gpointer (res);
4199 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4203 g_simple_async_result_set_from_error (res, error);
4204 g_error_free (error);
4207 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4211 g_file_real_append_to_async (GFile *file,
4212 GFileCreateFlags flags,
4214 GCancellable *cancellable,
4215 GAsyncReadyCallback callback,
4218 GFileCreateFlags *data;
4219 GSimpleAsyncResult *res;
4221 data = g_new0 (GFileCreateFlags, 1);
4224 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4225 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4227 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4228 g_object_unref (res);
4231 static GFileOutputStream *
4232 g_file_real_append_to_finish (GFile *file,
4236 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4239 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4241 op = g_simple_async_result_get_op_res_gpointer (simple);
4243 return g_object_ref (op);
4249 create_async_thread (GSimpleAsyncResult *res,
4251 GCancellable *cancellable)
4254 GFileCreateFlags *data;
4255 GFileOutputStream *stream;
4256 GError *error = NULL;
4258 iface = G_FILE_GET_IFACE (object);
4260 data = g_simple_async_result_get_op_res_gpointer (res);
4262 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4266 g_simple_async_result_set_from_error (res, error);
4267 g_error_free (error);
4270 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4274 g_file_real_create_async (GFile *file,
4275 GFileCreateFlags flags,
4277 GCancellable *cancellable,
4278 GAsyncReadyCallback callback,
4281 GFileCreateFlags *data;
4282 GSimpleAsyncResult *res;
4284 data = g_new0 (GFileCreateFlags, 1);
4287 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4288 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4290 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4291 g_object_unref (res);
4294 static GFileOutputStream *
4295 g_file_real_create_finish (GFile *file,
4299 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4302 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4304 op = g_simple_async_result_get_op_res_gpointer (simple);
4306 return g_object_ref (op);
4312 GFileOutputStream *stream;
4314 gboolean make_backup;
4315 GFileCreateFlags flags;
4319 replace_async_data_free (ReplaceAsyncData *data)
4322 g_object_unref (data->stream);
4323 g_free (data->etag);
4328 replace_async_thread (GSimpleAsyncResult *res,
4330 GCancellable *cancellable)
4333 GFileOutputStream *stream;
4334 GError *error = NULL;
4335 ReplaceAsyncData *data;
4337 iface = G_FILE_GET_IFACE (object);
4339 data = g_simple_async_result_get_op_res_gpointer (res);
4341 stream = iface->replace (G_FILE (object),
4350 g_simple_async_result_set_from_error (res, error);
4351 g_error_free (error);
4354 data->stream = stream;
4358 g_file_real_replace_async (GFile *file,
4360 gboolean make_backup,
4361 GFileCreateFlags flags,
4363 GCancellable *cancellable,
4364 GAsyncReadyCallback callback,
4367 GSimpleAsyncResult *res;
4368 ReplaceAsyncData *data;
4370 data = g_new0 (ReplaceAsyncData, 1);
4371 data->etag = g_strdup (etag);
4372 data->make_backup = make_backup;
4373 data->flags = flags;
4375 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4376 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4378 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4379 g_object_unref (res);
4382 static GFileOutputStream *
4383 g_file_real_replace_finish (GFile *file,
4387 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4388 ReplaceAsyncData *data;
4390 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4392 data = g_simple_async_result_get_op_res_gpointer (simple);
4394 return g_object_ref (data->stream);
4402 } SetDisplayNameAsyncData;
4405 set_display_name_data_free (SetDisplayNameAsyncData *data)
4407 g_free (data->name);
4409 g_object_unref (data->file);
4414 set_display_name_async_thread (GSimpleAsyncResult *res,
4416 GCancellable *cancellable)
4418 GError *error = NULL;
4419 SetDisplayNameAsyncData *data;
4422 data = g_simple_async_result_get_op_res_gpointer (res);
4424 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4428 g_simple_async_result_set_from_error (res, error);
4429 g_error_free (error);
4436 g_file_real_set_display_name_async (GFile *file,
4437 const char *display_name,
4439 GCancellable *cancellable,
4440 GAsyncReadyCallback callback,
4443 GSimpleAsyncResult *res;
4444 SetDisplayNameAsyncData *data;
4446 data = g_new0 (SetDisplayNameAsyncData, 1);
4447 data->name = g_strdup (display_name);
4449 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4450 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4452 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4453 g_object_unref (res);
4457 g_file_real_set_display_name_finish (GFile *file,
4461 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4462 SetDisplayNameAsyncData *data;
4464 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4466 data = g_simple_async_result_get_op_res_gpointer (simple);
4468 return g_object_ref (data->file);
4474 GFileQueryInfoFlags flags;
4481 set_info_data_free (SetInfoAsyncData *data)
4484 g_object_unref (data->info);
4486 g_error_free (data->error);
4491 set_info_async_thread (GSimpleAsyncResult *res,
4493 GCancellable *cancellable)
4495 SetInfoAsyncData *data;
4497 data = g_simple_async_result_get_op_res_gpointer (res);
4500 data->res = g_file_set_attributes_from_info (G_FILE (object),
4508 g_file_real_set_attributes_async (GFile *file,
4510 GFileQueryInfoFlags flags,
4512 GCancellable *cancellable,
4513 GAsyncReadyCallback callback,
4516 GSimpleAsyncResult *res;
4517 SetInfoAsyncData *data;
4519 data = g_new0 (SetInfoAsyncData, 1);
4520 data->info = g_file_info_dup (info);
4521 data->flags = flags;
4523 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4524 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4526 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4527 g_object_unref (res);
4531 g_file_real_set_attributes_finish (GFile *file,
4536 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4537 SetInfoAsyncData *data;
4539 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4541 data = g_simple_async_result_get_op_res_gpointer (simple);
4544 *info = g_object_ref (data->info);
4546 if (error != NULL && data->error)
4547 *error = g_error_copy (data->error);
4553 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4555 GCancellable *cancellable)
4557 GError *error = NULL;
4560 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4564 g_simple_async_result_set_from_error (res, error);
4565 g_error_free (error);
4568 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4572 g_file_real_find_enclosing_mount_async (GFile *file,
4574 GCancellable *cancellable,
4575 GAsyncReadyCallback callback,
4578 GSimpleAsyncResult *res;
4580 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4582 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4583 g_object_unref (res);
4587 g_file_real_find_enclosing_mount_finish (GFile *file,
4591 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4594 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4596 mount = g_simple_async_result_get_op_res_gpointer (simple);
4597 return g_object_ref (mount);
4604 GFileCopyFlags flags;
4605 GFileProgressCallback progress_cb;
4606 gpointer progress_cb_data;
4607 GIOSchedulerJob *job;
4611 copy_async_data_free (CopyAsyncData *data)
4613 g_object_unref (data->source);
4614 g_object_unref (data->destination);
4619 CopyAsyncData *data;
4620 goffset current_num_bytes;
4621 goffset total_num_bytes;
4625 copy_async_progress_in_main (gpointer user_data)
4627 ProgressData *progress = user_data;
4628 CopyAsyncData *data = progress->data;
4630 data->progress_cb (progress->current_num_bytes,
4631 progress->total_num_bytes,
4632 data->progress_cb_data);
4638 mainloop_barrier (gpointer user_data)
4640 /* Does nothing, but ensures all queued idles before
4647 copy_async_progress_callback (goffset current_num_bytes,
4648 goffset total_num_bytes,
4651 CopyAsyncData *data = user_data;
4652 ProgressData *progress;
4654 progress = g_new (ProgressData, 1);
4655 progress->data = data;
4656 progress->current_num_bytes = current_num_bytes;
4657 progress->total_num_bytes = total_num_bytes;
4659 g_io_scheduler_job_send_to_mainloop_async (data->job,
4660 copy_async_progress_in_main,
4666 copy_async_thread (GIOSchedulerJob *job,
4667 GCancellable *cancellable,
4670 GSimpleAsyncResult *res;
4671 CopyAsyncData *data;
4676 data = g_simple_async_result_get_op_res_gpointer (res);
4680 result = g_file_copy (data->source,
4684 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4688 /* Ensure all progress callbacks are done running in main thread */
4689 if (data->progress_cb != NULL)
4690 g_io_scheduler_job_send_to_mainloop (job,
4696 g_simple_async_result_set_from_error (res, error);
4697 g_error_free (error);
4700 g_simple_async_result_complete_in_idle (res);
4706 g_file_real_copy_async (GFile *source,
4708 GFileCopyFlags flags,
4710 GCancellable *cancellable,
4711 GFileProgressCallback progress_callback,
4712 gpointer progress_callback_data,
4713 GAsyncReadyCallback callback,
4716 GSimpleAsyncResult *res;
4717 CopyAsyncData *data;
4719 data = g_new0 (CopyAsyncData, 1);
4720 data->source = g_object_ref (source);
4721 data->destination = g_object_ref (destination);
4722 data->flags = flags;
4723 data->progress_cb = progress_callback;
4724 data->progress_cb_data = progress_callback_data;
4726 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4727 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4729 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4733 g_file_real_copy_finish (GFile *file,
4737 /* Error handled in g_file_copy_finish() */
4742 /********************************************
4743 * Default VFS operations *
4744 ********************************************/
4747 * g_file_new_for_path:
4748 * @path: a string containing a relative or absolute path.
4750 * Constructs a #GFile for a given path. This operation never
4751 * fails, but the returned object might not support any I/O
4752 * operation if @path is malformed.
4754 * Returns: a new #GFile for the given @path.
4757 g_file_new_for_path (const char *path)
4759 g_return_val_if_fail (path != NULL, NULL);
4761 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4765 * g_file_new_for_uri:
4766 * @uri: a string containing a URI.
4768 * Constructs a #GFile for a given URI. This operation never
4769 * fails, but the returned object might not support any I/O
4770 * operation if @uri is malformed or if the uri type is
4773 * Returns: a #GFile for the given @uri.
4776 g_file_new_for_uri (const char *uri)
4778 g_return_val_if_fail (uri != NULL, NULL);
4780 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4784 * g_file_parse_name:
4785 * @parse_name: a file name or path to be parsed.
4787 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4788 * This operation never fails, but the returned object might not support any I/O
4789 * operation if the @parse_name cannot be parsed.
4791 * Returns: a new #GFile.
4794 g_file_parse_name (const char *parse_name)
4796 g_return_val_if_fail (parse_name != NULL, NULL);
4798 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4802 is_valid_scheme_character (char c)
4804 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4807 /* Following RFC 2396, valid schemes are built like:
4808 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
4811 has_valid_scheme (const char *uri)
4817 if (!g_ascii_isalpha (*p))
4822 } while (is_valid_scheme_character (*p));
4828 * g_file_new_for_commandline_arg:
4829 * @arg: a command line string.
4831 * Creates a #GFile with the given argument from the command line. The value of
4832 * @arg can be either a URI, an absolute path or a relative path resolved
4833 * relative to the current working directory.
4834 * This operation never fails, but the returned object might not support any
4835 * I/O operation if @arg points to a malformed path.
4837 * Returns: a new #GFile.
4840 g_file_new_for_commandline_arg (const char *arg)
4846 g_return_val_if_fail (arg != NULL, NULL);
4848 if (g_path_is_absolute (arg))
4849 return g_file_new_for_path (arg);
4851 if (has_valid_scheme (arg))
4852 return g_file_new_for_uri (arg);
4854 current_dir = g_get_current_dir ();
4855 filename = g_build_filename (current_dir, arg, NULL);
4856 g_free (current_dir);
4858 file = g_file_new_for_path (filename);
4865 * g_file_mount_enclosing_volume:
4866 * @location: input #GFile.
4867 * @flags: flags affecting the operation
4868 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4869 * @cancellable: optional #GCancellable object, %NULL to ignore.
4870 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4871 * @user_data: the data to pass to callback function
4873 * Starts a @mount_operation, mounting the volume that contains the file @location.
4875 * When this operation has completed, @callback will be called with
4876 * @user_user data, and the operation can be finalized with
4877 * g_file_mount_enclosing_volume_finish().
4879 * If @cancellable is not %NULL, then the operation can be cancelled by
4880 * triggering the cancellable object from another thread. If the operation
4881 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4884 g_file_mount_enclosing_volume (GFile *location,
4885 GMountMountFlags flags,
4886 GMountOperation *mount_operation,
4887 GCancellable *cancellable,
4888 GAsyncReadyCallback callback,
4893 g_return_if_fail (G_IS_FILE (location));
4895 iface = G_FILE_GET_IFACE (location);
4897 if (iface->mount_enclosing_volume == NULL)
4899 g_simple_async_report_error_in_idle (G_OBJECT (location),
4900 callback, user_data,
4901 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4902 _("volume doesn't implement mount"));
4907 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4912 * g_file_mount_enclosing_volume_finish:
4913 * @location: input #GFile.
4914 * @result: a #GAsyncResult.
4915 * @error: a #GError, or %NULL
4917 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4919 * Returns: %TRUE if successful. If an error
4920 * has occurred, this function will return %FALSE and set @error
4921 * appropriately if present.
4924 g_file_mount_enclosing_volume_finish (GFile *location,
4925 GAsyncResult *result,
4930 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4931 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4933 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4935 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4936 if (g_simple_async_result_propagate_error (simple, error))
4940 iface = G_FILE_GET_IFACE (location);
4942 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4945 /********************************************
4946 * Utility functions *
4947 ********************************************/
4950 * g_file_query_default_handler:
4951 * @file: a #GFile to open.
4952 * @cancellable: optional #GCancellable object, %NULL to ignore.
4953 * @error: a #GError, or %NULL
4955 * Returns the #GAppInfo that is registered as the default
4956 * application to handle the file specified by @file.
4958 * If @cancellable is not %NULL, then the operation can be cancelled by
4959 * triggering the cancellable object from another thread. If the operation
4960 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4962 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4963 * When you are done with it, release it with g_object_unref()
4966 g_file_query_default_handler (GFile *file,
4967 GCancellable *cancellable,
4971 const char *content_type;
4976 uri_scheme = g_file_get_uri_scheme (file);
4977 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4978 g_free (uri_scheme);
4980 if (appinfo != NULL)
4983 info = g_file_query_info (file,
4984 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4993 content_type = g_file_info_get_content_type (info);
4996 /* Don't use is_native(), as we want to support fuse paths if availible */
4997 path = g_file_get_path (file);
4998 appinfo = g_app_info_get_default_for_type (content_type,
5003 g_object_unref (info);
5005 if (appinfo != NULL)
5008 g_set_error_literal (error, G_IO_ERROR,
5009 G_IO_ERROR_NOT_SUPPORTED,
5010 _("No application is registered as handling this file"));
5016 #define GET_CONTENT_BLOCK_SIZE 8192
5019 * g_file_load_contents:
5020 * @file: input #GFile.
5021 * @cancellable: optional #GCancellable object, %NULL to ignore.
5022 * @contents: a location to place the contents of the file.
5023 * @length: a location to place the length of the contents of the file.
5024 * @etag_out: a location to place the current entity tag for the file.
5025 * @error: a #GError, or %NULL
5027 * Loads the content of the file into memory, returning the size of
5028 * the data. The data is always zero terminated, but this is not
5029 * included in the resultant @length.
5031 * If @cancellable is not %NULL, then the operation can be cancelled by
5032 * triggering the cancellable object from another thread. If the operation
5033 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5035 * Returns: %TRUE if the @file's contents were successfully loaded.
5036 * %FALSE if there were errors..
5039 g_file_load_contents (GFile *file,
5040 GCancellable *cancellable,
5046 GFileInputStream *in;
5047 GByteArray *content;
5052 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5053 g_return_val_if_fail (contents != NULL, FALSE);
5055 in = g_file_read (file, cancellable, error);
5059 content = g_byte_array_new ();
5062 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5063 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
5064 content->data + pos,
5065 GET_CONTENT_BLOCK_SIZE,
5066 cancellable, error)) > 0)
5069 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
5076 info = g_file_input_stream_query_info (in,
5077 G_FILE_ATTRIBUTE_ETAG_VALUE,
5082 *etag_out = g_strdup (g_file_info_get_etag (info));
5083 g_object_unref (info);
5087 /* Ignore errors on close */
5088 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
5089 g_object_unref (in);
5093 /* error is set already */
5094 g_byte_array_free (content, TRUE);
5101 /* Zero terminate (we got an extra byte allocated for this */
5102 content->data[pos] = 0;
5104 *contents = (char *)g_byte_array_free (content, FALSE);
5112 GCancellable *cancellable;
5113 GFileReadMoreCallback read_more_callback;
5114 GAsyncReadyCallback callback;
5116 GByteArray *content;
5123 load_contents_data_free (LoadContentsData *data)
5126 g_error_free (data->error);
5127 if (data->cancellable)
5128 g_object_unref (data->cancellable);
5130 g_byte_array_free (data->content, TRUE);
5131 g_free (data->etag);
5132 g_object_unref (data->file);
5137 load_contents_close_callback (GObject *obj,
5138 GAsyncResult *close_res,
5141 GInputStream *stream = G_INPUT_STREAM (obj);
5142 LoadContentsData *data = user_data;
5143 GSimpleAsyncResult *res;
5145 /* Ignore errors here, we're only reading anyway */
5146 g_input_stream_close_finish (stream, close_res, NULL);
5147 g_object_unref (stream);
5149 res = g_simple_async_result_new (G_OBJECT (data->file),
5152 g_file_load_contents_async);
5153 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5154 g_simple_async_result_complete (res);
5155 g_object_unref (res);
5159 load_contents_fstat_callback (GObject *obj,
5160 GAsyncResult *stat_res,
5163 GInputStream *stream = G_INPUT_STREAM (obj);
5164 LoadContentsData *data = user_data;
5167 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5171 data->etag = g_strdup (g_file_info_get_etag (info));
5172 g_object_unref (info);
5175 g_input_stream_close_async (stream, 0,
5177 load_contents_close_callback, data);
5181 load_contents_read_callback (GObject *obj,
5182 GAsyncResult *read_res,
5185 GInputStream *stream = G_INPUT_STREAM (obj);
5186 LoadContentsData *data = user_data;
5187 GError *error = NULL;
5190 read_size = g_input_stream_read_finish (stream, read_res, &error);
5194 /* Error or EOF, close the file */
5195 data->error = error;
5196 g_input_stream_close_async (stream, 0,
5198 load_contents_close_callback, data);
5200 else if (read_size == 0)
5202 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5203 G_FILE_ATTRIBUTE_ETAG_VALUE,
5206 load_contents_fstat_callback,
5209 else if (read_size > 0)
5211 data->pos += read_size;
5213 g_byte_array_set_size (data->content,
5214 data->pos + GET_CONTENT_BLOCK_SIZE);
5217 if (data->read_more_callback &&
5218 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5219 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5220 G_FILE_ATTRIBUTE_ETAG_VALUE,
5223 load_contents_fstat_callback,
5226 g_input_stream_read_async (stream,
5227 data->content->data + data->pos,
5228 GET_CONTENT_BLOCK_SIZE,
5231 load_contents_read_callback,
5237 load_contents_open_callback (GObject *obj,
5238 GAsyncResult *open_res,
5241 GFile *file = G_FILE (obj);
5242 GFileInputStream *stream;
5243 LoadContentsData *data = user_data;
5244 GError *error = NULL;
5245 GSimpleAsyncResult *res;
5247 stream = g_file_read_finish (file, open_res, &error);
5251 g_byte_array_set_size (data->content,
5252 data->pos + GET_CONTENT_BLOCK_SIZE);
5253 g_input_stream_read_async (G_INPUT_STREAM (stream),
5254 data->content->data + data->pos,
5255 GET_CONTENT_BLOCK_SIZE,
5258 load_contents_read_callback,
5264 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5268 g_simple_async_result_complete (res);
5269 g_error_free (error);
5270 load_contents_data_free (data);
5271 g_object_unref (res);
5276 * g_file_load_partial_contents_async:
5277 * @file: input #GFile.
5278 * @cancellable: optional #GCancellable object, %NULL to ignore.
5279 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5280 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5281 * @user_data: the data to pass to the callback functions.
5283 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5284 * used to stop reading from the file when appropriate, else this function
5285 * will behave exactly as g_file_load_contents_async(). This operation
5286 * can be finished by g_file_load_partial_contents_finish().
5288 * Users of this function should be aware that @user_data is passed to
5289 * both the @read_more_callback and the @callback.
5291 * If @cancellable is not %NULL, then the operation can be cancelled by
5292 * triggering the cancellable object from another thread. If the operation
5293 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5296 g_file_load_partial_contents_async (GFile *file,
5297 GCancellable *cancellable,
5298 GFileReadMoreCallback read_more_callback,
5299 GAsyncReadyCallback callback,
5302 LoadContentsData *data;
5304 g_return_if_fail (G_IS_FILE (file));
5306 data = g_new0 (LoadContentsData, 1);
5309 data->cancellable = g_object_ref (cancellable);
5310 data->read_more_callback = read_more_callback;
5311 data->callback = callback;
5312 data->user_data = user_data;
5313 data->content = g_byte_array_new ();
5314 data->file = g_object_ref (file);
5316 g_file_read_async (file,
5319 load_contents_open_callback,
5324 * g_file_load_partial_contents_finish:
5325 * @file: input #GFile.
5326 * @res: a #GAsyncResult.
5327 * @contents: a location to place the contents of the file.
5328 * @length: a location to place the length of the contents of the file.
5329 * @etag_out: a location to place the current entity tag for the file.
5330 * @error: a #GError, or %NULL
5332 * Finishes an asynchronous partial load operation that was started
5333 * with g_file_load_partial_contents_async().
5335 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5336 * present, it will be set appropriately.
5339 g_file_load_partial_contents_finish (GFile *file,
5346 GSimpleAsyncResult *simple;
5347 LoadContentsData *data;
5349 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5350 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5351 g_return_val_if_fail (contents != NULL, FALSE);
5353 simple = G_SIMPLE_ASYNC_RESULT (res);
5355 if (g_simple_async_result_propagate_error (simple, error))
5358 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5360 data = g_simple_async_result_get_op_res_gpointer (simple);
5364 g_propagate_error (error, data->error);
5373 *length = data->pos;
5377 *etag_out = data->etag;
5381 /* Zero terminate */
5382 g_byte_array_set_size (data->content, data->pos + 1);
5383 data->content->data[data->pos] = 0;
5385 *contents = (char *)g_byte_array_free (data->content, FALSE);
5386 data->content = NULL;
5392 * g_file_load_contents_async:
5393 * @file: input #GFile.
5394 * @cancellable: optional #GCancellable object, %NULL to ignore.
5395 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5396 * @user_data: the data to pass to callback function
5398 * Starts an asynchronous load of the @file's contents.
5400 * For more details, see g_file_load_contents() which is
5401 * the synchronous version of this call.
5403 * When the load operation has completed, @callback will be called
5404 * with @user data. To finish the operation, call
5405 * g_file_load_contents_finish() with the #GAsyncResult returned by
5408 * If @cancellable is not %NULL, then the operation can be cancelled by
5409 * triggering the cancellable object from another thread. If the operation
5410 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5413 g_file_load_contents_async (GFile *file,
5414 GCancellable *cancellable,
5415 GAsyncReadyCallback callback,
5418 g_file_load_partial_contents_async (file,
5421 callback, user_data);
5425 * g_file_load_contents_finish:
5426 * @file: input #GFile.
5427 * @res: a #GAsyncResult.
5428 * @contents: a location to place the contents of the file.
5429 * @length: a location to place the length of the contents of the file.
5430 * @etag_out: a location to place the current entity tag for the file.
5431 * @error: a #GError, or %NULL
5433 * Finishes an asynchronous load of the @file's contents.
5434 * The contents are placed in @contents, and @length is set to the
5435 * size of the @contents string. If @etag_out is present, it will be
5436 * set to the new entity tag for the @file.
5438 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5439 * present, it will be set appropriately.
5442 g_file_load_contents_finish (GFile *file,
5449 return g_file_load_partial_contents_finish (file,
5458 * g_file_replace_contents:
5459 * @file: input #GFile.
5460 * @contents: a string containing the new contents for @file.
5461 * @length: the length of @contents in bytes.
5462 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5464 * @make_backup: %TRUE if a backup should be created.
5465 * @flags: a set of #GFileCreateFlags.
5466 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5467 * for the document. This should be freed with g_free() when no longer
5469 * @cancellable: optional #GCancellable object, %NULL to ignore.
5470 * @error: a #GError, or %NULL
5472 * Replaces the contents of @file with @contents of @length bytes.
5474 * If @etag is specified (not %NULL) any existing file must have that etag, or
5475 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5477 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5479 * If @cancellable is not %NULL, then the operation can be cancelled by
5480 * triggering the cancellable object from another thread. If the operation
5481 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5483 * The returned @new_etag can be used to verify that the file hasn't changed the
5484 * next time it is saved over.
5486 * Returns: %TRUE if successful. If an error
5487 * has occurred, this function will return %FALSE and set @error
5488 * appropriately if present.
5491 g_file_replace_contents (GFile *file,
5492 const char *contents,
5495 gboolean make_backup,
5496 GFileCreateFlags flags,
5498 GCancellable *cancellable,
5501 GFileOutputStream *out;
5502 gsize pos, remainder;
5506 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5507 g_return_val_if_fail (contents != NULL, FALSE);
5509 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5515 while (remainder > 0 &&
5516 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5518 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5526 if (remainder > 0 && res < 0)
5528 /* Ignore errors on close */
5529 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5530 g_object_unref (out);
5532 /* error is set already */
5536 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
5539 *new_etag = g_file_output_stream_get_etag (out);
5541 g_object_unref (out);
5549 GCancellable *cancellable;
5550 GAsyncReadyCallback callback;
5552 const char *content;
5556 } ReplaceContentsData;
5559 replace_contents_data_free (ReplaceContentsData *data)
5562 g_error_free (data->error);
5563 if (data->cancellable)
5564 g_object_unref (data->cancellable);
5565 g_object_unref (data->file);
5566 g_free (data->etag);
5571 replace_contents_close_callback (GObject *obj,
5572 GAsyncResult *close_res,
5575 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5576 ReplaceContentsData *data = user_data;
5577 GSimpleAsyncResult *res;
5579 /* Ignore errors here, we're only reading anyway */
5580 g_output_stream_close_finish (stream, close_res, NULL);
5581 g_object_unref (stream);
5583 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5585 res = g_simple_async_result_new (G_OBJECT (data->file),
5588 g_file_replace_contents_async);
5589 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5590 g_simple_async_result_complete (res);
5591 g_object_unref (res);
5595 replace_contents_write_callback (GObject *obj,
5596 GAsyncResult *read_res,
5599 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5600 ReplaceContentsData *data = user_data;
5601 GError *error = NULL;
5604 write_size = g_output_stream_write_finish (stream, read_res, &error);
5606 if (write_size <= 0)
5608 /* Error or EOF, close the file */
5610 data->error = error;
5611 g_output_stream_close_async (stream, 0,
5613 replace_contents_close_callback, data);
5615 else if (write_size > 0)
5617 data->pos += write_size;
5619 if (data->pos >= data->length)
5620 g_output_stream_close_async (stream, 0,
5622 replace_contents_close_callback, data);
5624 g_output_stream_write_async (stream,
5625 data->content + data->pos,
5626 data->length - data->pos,
5629 replace_contents_write_callback,
5635 replace_contents_open_callback (GObject *obj,
5636 GAsyncResult *open_res,
5639 GFile *file = G_FILE (obj);
5640 GFileOutputStream *stream;
5641 ReplaceContentsData *data = user_data;
5642 GError *error = NULL;
5643 GSimpleAsyncResult *res;
5645 stream = g_file_replace_finish (file, open_res, &error);
5649 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5650 data->content + data->pos,
5651 data->length - data->pos,
5654 replace_contents_write_callback,
5660 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5664 g_simple_async_result_complete (res);
5665 g_error_free (error);
5666 replace_contents_data_free (data);
5667 g_object_unref (res);
5672 * g_file_replace_contents_async:
5673 * @file: input #GFile.
5674 * @contents: string of contents to replace the file with.
5675 * @length: the length of @contents in bytes.
5676 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5677 * @make_backup: %TRUE if a backup should be created.
5678 * @flags: a set of #GFileCreateFlags.
5679 * @cancellable: optional #GCancellable object, %NULL to ignore.
5680 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5681 * @user_data: the data to pass to callback function
5683 * Starts an asynchronous replacement of @file with the given
5684 * @contents of @length bytes. @etag will replace the document's
5685 * current entity tag.
5687 * When this operation has completed, @callback will be called with
5688 * @user_user data, and the operation can be finalized with
5689 * g_file_replace_contents_finish().
5691 * If @cancellable is not %NULL, then the operation can be cancelled by
5692 * triggering the cancellable object from another thread. If the operation
5693 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5695 * If @make_backup is %TRUE, this function will attempt to
5696 * make a backup of @file.
5699 g_file_replace_contents_async (GFile *file,
5700 const char *contents,
5703 gboolean make_backup,
5704 GFileCreateFlags flags,
5705 GCancellable *cancellable,
5706 GAsyncReadyCallback callback,
5709 ReplaceContentsData *data;
5711 g_return_if_fail (G_IS_FILE (file));
5712 g_return_if_fail (contents != NULL);
5714 data = g_new0 (ReplaceContentsData, 1);
5717 data->cancellable = g_object_ref (cancellable);
5718 data->callback = callback;
5719 data->user_data = user_data;
5720 data->content = contents;
5721 data->length = length;
5723 data->file = g_object_ref (file);
5725 g_file_replace_async (file,
5731 replace_contents_open_callback,
5736 * g_file_replace_contents_finish:
5737 * @file: input #GFile.
5738 * @res: a #GAsyncResult.
5739 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5740 * for the document. This should be freed with g_free() when it is no
5742 * @error: a #GError, or %NULL
5744 * Finishes an asynchronous replace of the given @file. See
5745 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5746 * tag for the document, if present.
5748 * Returns: %TRUE on success, %FALSE on failure.
5751 g_file_replace_contents_finish (GFile *file,
5756 GSimpleAsyncResult *simple;
5757 ReplaceContentsData *data;
5759 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5760 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5762 simple = G_SIMPLE_ASYNC_RESULT (res);
5764 if (g_simple_async_result_propagate_error (simple, error))
5767 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5769 data = g_simple_async_result_get_op_res_gpointer (simple);
5773 g_propagate_error (error, data->error);
5781 *new_etag = data->etag;
5782 data->etag = NULL; /* Take ownership */
5788 #define __G_FILE_C__
5789 #include "gioaliasdef.c"