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"
44 * @short_description: File and Directory Handling
46 * @see_also: #GFileInfo, #GFileEnumerator
48 * #GFile is a high level abstraction for manipulating files on a
49 * virtual file system. #GFile<!-- -->s are lightweight, immutable
50 * objects that do no I/O upon creation. It is necessary to understand that
51 * #GFile objects do not represent files, merely an identifier for a file. All
52 * file content I/O is implemented as streaming operations (see #GInputStream and
55 * To construct a #GFile, you can use:
56 * g_file_new_for_path() if you have a path.
57 * g_file_new_for_uri() if you have a URI.
58 * g_file_new_for_commandline_arg() for a command line argument.
59 * g_file_parse_name() from a utf8 string gotten from g_file_get_parse_name().
61 * One way to think of a #GFile is as an abstraction of a pathname. For normal
62 * files the system pathname is what is stored internally, but as #GFile<!-- -->s
63 * are extensible it could also be something else that corresponds to a pathname
64 * in a userspace implementation of a filesystem.
66 * #GFile<!-- -->s make up hierarchies of directories and files that correspond to the
67 * files on a filesystem. You can move through the file system with #GFile using
68 * g_file_get_parent() to get an identifier for the parent directory, g_file_get_child()
69 * to get a child within a directory, g_file_resolve_relative_path() to resolve a relative
70 * path between two #GFile<!-- -->s. There can be multiple hierarchies, so you may not
71 * end up at the same root if you repeatedly call g_file_get_parent() on two different
74 * All #GFile<!-- -->s have a basename (get with g_file_get_basename()). These names
75 * are byte strings that are used to identify the file on the filesystem (relative to
76 * its parent directory) and there is no guarantees that they have any particular charset
77 * encoding or even make any sense at all. If you want to use filenames in a user
78 * interface you should use the display name that you can get by requesting the
79 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
80 * This is guaranteed to be in utf8 and can be used in a user interface. But always
81 * store the real basename or the #GFile to use to actually access the file, because
82 * there is no way to go from a display name to the actual name.
84 * Using #GFile as an identifier has the same weaknesses as using a path in that
85 * there may be multiple aliases for the same file. For instance, hard or
86 * soft links may cause two different #GFile<!-- -->s to refer to the same file.
87 * Other possible causes for aliases are: case insensitive filesystems, short
88 * and long names on Fat/NTFS, or bind mounts in Linux. If you want to check if
89 * two #GFile<!-- -->s point to the same file you can query for the
90 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
91 * canonicalization of pathnames passed in, so that trivial differences in the
92 * path string used at creation (duplicated slashes, slash at end of path, "."
93 * or ".." path segments, etc) does not create different #GFile<!-- -->s.
95 * Many #GFile operations have both synchronous and asynchronous versions
96 * to suit your application. Asynchronous versions of synchronous functions
97 * simply have _async() appended to their function names. The asynchronous
98 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
99 * the operation, producing a GAsyncResult which is then passed to the
100 * function's matching _finish() operation.
102 * Some #GFile operations do not have synchronous analogs, as they may
103 * take a very long time to finish, and blocking may leave an application
104 * unusable. Notable cases include:
105 * g_file_mount_mountable() to mount a mountable file.
106 * g_file_unmount_mountable() to unmount a mountable file.
107 * g_file_eject_mountable() to eject a mountable file.
109 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
110 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
111 * short. Entity tags are somewhat like a more abstract version of the
112 * traditional mtime, and can be used to quickly determine if the file has
113 * been modified from the version on the file system. See the HTTP 1.1
114 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
115 * for HTTP Etag headers, which are a very similar concept.
119 static void g_file_base_init (gpointer g_class);
120 static void g_file_class_init (gpointer g_class,
121 gpointer class_data);
123 static void g_file_real_query_info_async (GFile *file,
124 const char *attributes,
125 GFileQueryInfoFlags flags,
127 GCancellable *cancellable,
128 GAsyncReadyCallback callback,
130 static GFileInfo * g_file_real_query_info_finish (GFile *file,
133 static void g_file_real_query_filesystem_info_async (GFile *file,
134 const char *attributes,
136 GCancellable *cancellable,
137 GAsyncReadyCallback callback,
139 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
142 static void g_file_real_enumerate_children_async (GFile *file,
143 const char *attributes,
144 GFileQueryInfoFlags flags,
146 GCancellable *cancellable,
147 GAsyncReadyCallback callback,
149 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
152 static void g_file_real_read_async (GFile *file,
154 GCancellable *cancellable,
155 GAsyncReadyCallback callback,
157 static GFileInputStream * g_file_real_read_finish (GFile *file,
160 static void g_file_real_append_to_async (GFile *file,
161 GFileCreateFlags flags,
163 GCancellable *cancellable,
164 GAsyncReadyCallback callback,
166 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
169 static void g_file_real_create_async (GFile *file,
170 GFileCreateFlags flags,
172 GCancellable *cancellable,
173 GAsyncReadyCallback callback,
175 static GFileOutputStream *g_file_real_create_finish (GFile *file,
178 static void g_file_real_replace_async (GFile *file,
180 gboolean make_backup,
181 GFileCreateFlags flags,
183 GCancellable *cancellable,
184 GAsyncReadyCallback callback,
186 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
189 static gboolean g_file_real_set_attributes_from_info (GFile *file,
191 GFileQueryInfoFlags flags,
192 GCancellable *cancellable,
194 static void g_file_real_set_display_name_async (GFile *file,
195 const char *display_name,
197 GCancellable *cancellable,
198 GAsyncReadyCallback callback,
200 static GFile * g_file_real_set_display_name_finish (GFile *file,
203 static void g_file_real_set_attributes_async (GFile *file,
205 GFileQueryInfoFlags flags,
207 GCancellable *cancellable,
208 GAsyncReadyCallback callback,
210 static gboolean g_file_real_set_attributes_finish (GFile *file,
214 static void g_file_real_find_enclosing_mount_async (GFile *file,
216 GCancellable *cancellable,
217 GAsyncReadyCallback callback,
219 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
222 static void g_file_real_copy_async (GFile *source,
224 GFileCopyFlags flags,
226 GCancellable *cancellable,
227 GFileProgressCallback progress_callback,
228 gpointer progress_callback_data,
229 GAsyncReadyCallback callback,
231 static gboolean g_file_real_copy_finish (GFile *file,
236 g_file_get_type (void)
238 static GType file_type = 0;
242 static const GTypeInfo file_info =
244 sizeof (GFileIface), /* class_size */
245 g_file_base_init, /* base_init */
246 NULL, /* base_finalize */
248 NULL, /* class_finalize */
249 NULL, /* class_data */
256 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
259 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
266 g_file_class_init (gpointer g_class,
269 GFileIface *iface = g_class;
271 iface->enumerate_children_async = g_file_real_enumerate_children_async;
272 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
273 iface->set_display_name_async = g_file_real_set_display_name_async;
274 iface->set_display_name_finish = g_file_real_set_display_name_finish;
275 iface->query_info_async = g_file_real_query_info_async;
276 iface->query_info_finish = g_file_real_query_info_finish;
277 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
278 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
279 iface->set_attributes_async = g_file_real_set_attributes_async;
280 iface->set_attributes_finish = g_file_real_set_attributes_finish;
281 iface->read_async = g_file_real_read_async;
282 iface->read_finish = g_file_real_read_finish;
283 iface->append_to_async = g_file_real_append_to_async;
284 iface->append_to_finish = g_file_real_append_to_finish;
285 iface->create_async = g_file_real_create_async;
286 iface->create_finish = g_file_real_create_finish;
287 iface->replace_async = g_file_real_replace_async;
288 iface->replace_finish = g_file_real_replace_finish;
289 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
290 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
291 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
292 iface->copy_async = g_file_real_copy_async;
293 iface->copy_finish = g_file_real_copy_finish;
297 g_file_base_init (gpointer g_class)
304 * @file: input #GFile.
306 * Checks to see if a file is native to the platform.
308 * A native file s one expressed in the platform-native filename format,
309 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
310 * as it might be on a locally mounted remote filesystem.
312 * On some systems non-native files may be available using
313 * the native filesystem via a userspace filesystem (FUSE), in
314 * these cases this call will return %FALSE, but g_file_get_path()
315 * will still return a native path.
317 * This call does no blocking i/o.
319 * Returns: %TRUE if file is native.
322 g_file_is_native (GFile *file)
326 g_return_val_if_fail (G_IS_FILE (file), FALSE);
328 iface = G_FILE_GET_IFACE (file);
330 return (* iface->is_native) (file);
335 * g_file_has_uri_scheme:
336 * @file: input #GFile.
337 * @uri_scheme: a string containing a URI scheme.
339 * Checks to see if a #GFile has a given URI scheme.
341 * This call does no blocking i/o.
343 * Returns: %TRUE if #GFile's backend supports the
344 * given URI scheme, %FALSE if URI scheme is %NULL,
345 * not supported, or #GFile is invalid.
348 g_file_has_uri_scheme (GFile *file,
349 const char *uri_scheme)
353 g_return_val_if_fail (G_IS_FILE (file), FALSE);
354 g_return_val_if_fail (uri_scheme != NULL, FALSE);
356 iface = G_FILE_GET_IFACE (file);
358 return (* iface->has_uri_scheme) (file, uri_scheme);
363 * g_file_get_uri_scheme:
364 * @file: input #GFile.
366 * Gets the URI scheme for a #GFile.
367 * RFC 3986 decodes the scheme as:
369 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
371 * Common schemes include "file", "http", "ftp", etc.
373 * This call does no blocking i/o.
375 * Returns: a string containing the URI scheme for the given
376 * #GFile. The returned string should be freed with g_free()
377 * when no longer needed.
380 g_file_get_uri_scheme (GFile *file)
384 g_return_val_if_fail (G_IS_FILE (file), NULL);
386 iface = G_FILE_GET_IFACE (file);
388 return (* iface->get_uri_scheme) (file);
393 * g_file_get_basename:
394 * @file: input #GFile.
396 * Gets the base name (the last component of the path) for a given #GFile.
398 * If called for the top level of a system (such as the filesystem root
399 * or a uri like sftp://host/) it will return a single directory separator
400 * (and on Windows, possibly a drive letter).
402 * The base name is a byte string (*not* UTF-8). It has no defined encoding
403 * or rules other than it may not contain zero bytes. If you want to use
404 * filenames in a user interface you should use the display name that you
405 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
406 * attribute with g_file_query_info().
408 * This call does no blocking i/o.
410 * Returns: string containing the #GFile's base name, or %NULL
411 * if given #GFile is invalid. The returned string should be
412 * freed with g_free() when no longer needed.
415 g_file_get_basename (GFile *file)
419 g_return_val_if_fail (G_IS_FILE (file), NULL);
421 iface = G_FILE_GET_IFACE (file);
423 return (* iface->get_basename) (file);
428 * @file: input #GFile.
430 * Gets the local pathname for #GFile, if one exists.
432 * This call does no blocking i/o.
434 * Returns: string containing the #GFile's path, or %NULL if
435 * no such path exists. The returned string should be
436 * freed with g_free() when no longer needed.
439 g_file_get_path (GFile *file)
443 g_return_val_if_fail (G_IS_FILE (file), NULL);
445 iface = G_FILE_GET_IFACE (file);
447 return (* iface->get_path) (file);
452 * @file: input #GFile.
454 * Gets the URI for the @file.
456 * This call does no blocking i/o.
458 * Returns: a string containing the #GFile's URI.
459 * The returned string should be freed with g_free() when no longer needed.
462 g_file_get_uri (GFile *file)
466 g_return_val_if_fail (G_IS_FILE (file), NULL);
468 iface = G_FILE_GET_IFACE (file);
470 return (* iface->get_uri) (file);
474 * g_file_get_parse_name:
475 * @file: input #GFile.
477 * Gets the parse name of the @file.
478 * A parse name is a UTF-8 string that describes the
479 * file such that one can get the #GFile back using
480 * g_file_parse_name().
482 * This is generally used to show the #GFile as a nice
483 * full-pathname kind of string in a user interface,
484 * like in a location entry.
486 * For local files with names that can safely be converted
487 * to UTF8 the pathname is used, otherwise the IRI is used
488 * (a form of URI that allows UTF8 characters unescaped).
490 * This call does no blocking i/o.
492 * Returns: a string containing the #GFile's parse name. The returned
493 * string should be freed with g_free() when no longer needed.
496 g_file_get_parse_name (GFile *file)
500 g_return_val_if_fail (G_IS_FILE (file), NULL);
502 iface = G_FILE_GET_IFACE (file);
504 return (* iface->get_parse_name) (file);
509 * @file: input #GFile.
511 * Duplicates a #GFile handle. This operation does not duplicate
512 * the actual file or directory represented by the #GFile; see
513 * g_file_copy() if attempting to copy a file.
515 * This call does no blocking i/o.
517 * Returns: #GFile that is a duplicate of the given #GFile.
520 g_file_dup (GFile *file)
524 g_return_val_if_fail (G_IS_FILE (file), NULL);
526 iface = G_FILE_GET_IFACE (file);
528 return (* iface->dup) (file);
533 * @file: #gconstpointer to a #GFile.
535 * Creates a hash value for a #GFile.
537 * This call does no blocking i/o.
539 * Returns: 0 if @file is not a valid #GFile, otherwise an
540 * integer that can be used as hash value for the #GFile.
541 * This function is intended for easily hashing a #GFile to
542 * add to a #GHashTable or similar data structure.
545 g_file_hash (gconstpointer file)
549 g_return_val_if_fail (G_IS_FILE (file), 0);
551 iface = G_FILE_GET_IFACE (file);
553 return (* iface->hash) ((GFile *)file);
558 * @file1: the first #GFile.
559 * @file2: the second #GFile.
561 * Checks equality of two given #GFile<!-- -->s. Note that two
562 * #GFile<!-- -->s that differ can still refer to the same
563 * file on the filesystem due to various forms of filename
566 * This call does no blocking i/o.
568 * Returns: %TRUE if @file1 and @file2 are equal.
569 * %FALSE if either is not a #GFile.
572 g_file_equal (GFile *file1,
577 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
578 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
580 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
583 iface = G_FILE_GET_IFACE (file1);
585 return (* iface->equal) (file1, file2);
591 * @file: input #GFile.
593 * Gets the parent directory for the @file.
594 * If the @file represents the root directory of the
595 * file system, then %NULL will be returned.
597 * This call does no blocking i/o.
599 * Returns: a #GFile structure to the parent of the given
600 * #GFile or %NULL if there is no parent.
603 g_file_get_parent (GFile *file)
607 g_return_val_if_fail (G_IS_FILE (file), NULL);
609 iface = G_FILE_GET_IFACE (file);
611 return (* iface->get_parent) (file);
616 * @file: input #GFile.
617 * @name: string containing the child's basename.
619 * Gets a child of @file with basename equal to @name.
621 * Note that the file with that specific name might not exist, but
622 * you can still have a #GFile that points to it. You can use this
623 * for instance to create that file.
625 * This call does no blocking i/o.
627 * Returns: a #GFile to a child specified by @name.
630 g_file_get_child (GFile *file,
633 g_return_val_if_fail (G_IS_FILE (file), NULL);
634 g_return_val_if_fail (name != NULL, NULL);
636 return g_file_resolve_relative_path (file, name);
640 * g_file_get_child_for_display_name:
641 * @file: input #GFile.
642 * @display_name: string to a possible child.
645 * Gets the child of @file for a given @display_name (i.e. a UTF8
646 * version of the name). If this function fails, it returns %NULL and @error will be
647 * set. This is very useful when constructing a GFile for a new file
648 * and the user entered the filename in the user interface, for instance
649 * when you select a directory and type a filename in the file selector.
651 * This call does no blocking i/o.
653 * Returns: a #GFile to the specified child, or
654 * %NULL if the display name couldn't be converted.
657 g_file_get_child_for_display_name (GFile *file,
658 const char *display_name,
663 g_return_val_if_fail (G_IS_FILE (file), NULL);
664 g_return_val_if_fail (display_name != NULL, NULL);
666 iface = G_FILE_GET_IFACE (file);
668 return (* iface->get_child_for_display_name) (file, display_name, error);
673 * @file: input #GFile.
674 * @prefix: input #GFile.
676 * Checks whether @file has the prefix specified by @prefix. In other word, if the
677 * names of inital elements of @file<!-- -->s pathname match @prefix.
679 * This call does no i/o, as it works purely on names. As such it can sometimes
680 * return %FALSE even if @file is inside a @prefix (from a filesystem point of view),
681 * because the prefix of @file is an alias of @prefix.
683 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix. %FALSE otherwise.
686 g_file_has_prefix (GFile *file,
691 g_return_val_if_fail (G_IS_FILE (file), FALSE);
692 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
694 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
697 iface = G_FILE_GET_IFACE (file);
699 /* The vtable function differs in arg order since we're
700 using the old contains_file call */
701 return (* iface->prefix_matches) (prefix, file);
705 * g_file_get_relative_path:
706 * @parent: input #GFile.
707 * @descendant: input #GFile.
709 * Gets the path for @descendant relative to @parent.
711 * This call does no blocking i/o.
713 * Returns: string with the relative path from @descendant
714 * to @parent, or %NULL if @descendant doesn't have @parent as prefix. The returned string should be freed with
715 * g_free() when no longer needed.
718 g_file_get_relative_path (GFile *parent,
723 g_return_val_if_fail (G_IS_FILE (parent), NULL);
724 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
726 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
729 iface = G_FILE_GET_IFACE (parent);
731 return (* iface->get_relative_path) (parent, descendant);
735 * g_file_resolve_relative_path:
736 * @file: input #GFile.
737 * @relative_path: a given relative path string.
739 * Resolves a relative path for @file to an absolute path.
741 * This call does no blocking i/o.
743 * Returns: #GFile to the resolved path. %NULL if @relative_path
744 * is %NULL or if @file is invalid.
747 g_file_resolve_relative_path (GFile *file,
748 const char *relative_path)
752 g_return_val_if_fail (G_IS_FILE (file), NULL);
753 g_return_val_if_fail (relative_path != NULL, NULL);
755 iface = G_FILE_GET_IFACE (file);
757 return (* iface->resolve_relative_path) (file, relative_path);
761 * g_file_enumerate_children:
762 * @file: input #GFile.
763 * @attributes: an attribute query string.
764 * @flags: a set of #GFileQueryInfoFlags.
765 * @cancellable: optional #GCancellable object, %NULL to ignore.
766 * @error: #GError for error reporting.
768 * Gets the requested information about the files in a directory. The result
769 * is a #GFileEnumerator object that will give out #GFileInfo objects for
770 * all the files in the directory.
772 * The @attribute value is a string that specifies the file attributes that
773 * should be gathered. It is not an error if it's not possible to read a particular
774 * requested attribute from a file - it just won't be set. @attribute should
775 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
776 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
777 * namespace. An example attribute query be "standard::*,owner::user".
778 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
780 * If @cancellable is not %NULL, then the operation can be cancelled by
781 * triggering the cancellable object from another thread. If the operation
782 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
784 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
785 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
786 * Other errors are possible too.
788 * Returns: A #GFileEnumerator if successful, %NULL on error.
791 g_file_enumerate_children (GFile *file,
792 const char *attributes,
793 GFileQueryInfoFlags flags,
794 GCancellable *cancellable,
800 g_return_val_if_fail (G_IS_FILE (file), NULL);
802 if (g_cancellable_set_error_if_cancelled (cancellable, error))
805 iface = G_FILE_GET_IFACE (file);
807 if (iface->enumerate_children == NULL)
809 g_set_error (error, G_IO_ERROR,
810 G_IO_ERROR_NOT_SUPPORTED,
811 _("Operation not supported"));
815 return (* iface->enumerate_children) (file, attributes, flags,
820 * g_file_enumerate_children_async:
821 * @file: input #GFile.
822 * @attributes: an attribute query string.
823 * @flags: a set of #GFileQueryInfoFlags.
824 * @io_priority: the <link linkend="io-priority">I/O priority</link>
826 * @cancellable: optional #GCancellable object, %NULL to ignore.
827 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
828 * @user_data: the data to pass to callback function
830 * Asynchronously gets the requested information about the files in a directory. The result
831 * is a #GFileEnumerator object that will give out #GFileInfo objects for
832 * all the files in the directory.
834 * For more details, see g_file_enumerate_children() which is
835 * the synchronous version of this call.
837 * When the operation is finished, @callback will be called. You can then call
838 * g_file_enumerate_children_finish() to get the result of the operation.
841 g_file_enumerate_children_async (GFile *file,
842 const char *attributes,
843 GFileQueryInfoFlags flags,
845 GCancellable *cancellable,
846 GAsyncReadyCallback callback,
851 g_return_if_fail (G_IS_FILE (file));
853 iface = G_FILE_GET_IFACE (file);
854 (* iface->enumerate_children_async) (file,
864 * g_file_enumerate_children_finish:
865 * @file: input #GFile.
866 * @res: a #GAsyncResult.
869 * Finishes an async enumerate children operation.
870 * See g_file_enumerate_children_async().
872 * Returns: a #GFileEnumerator or %NULL if an error occurred.
875 g_file_enumerate_children_finish (GFile *file,
881 g_return_val_if_fail (G_IS_FILE (file), NULL);
882 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
884 if (G_IS_SIMPLE_ASYNC_RESULT (res))
886 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
887 if (g_simple_async_result_propagate_error (simple, error))
891 iface = G_FILE_GET_IFACE (file);
892 return (* iface->enumerate_children_finish) (file, res, error);
896 * g_file_query_exists:
897 * @file: input #GFile.
898 * @cancellable: optional #GCancellable object, %NULL to ignore.
900 * Utility function to check if a particular file exists. This is
901 * implemented using g_file_query_info() and as such does blocking I/O.
903 * Note that in many cases it is racy to first check for file existence
904 * and then execute something based on the outcome of that, because the
905 * file might have been created or removed in between the operations. The
906 * general approach to handling that is to not check, but just do the
907 * operation and handle the errors as they come.
909 * As an example of race-free checking, take the case of reading a file, and
910 * if it doesn't exist, creating it. There are two racy versions: read it, and
911 * on error create it; and: check if it exists, if not create it. These
912 * can both result in two processes creating the file (with perhaps a partially
913 * written file as the result). The correct approach is to always try to create
914 * the file with g_file_create() which will either atomically create the file
915 * or fail with a G_IO_ERROR_EXISTS error.
917 * However, in many cases an existence check is useful in a user
918 * interface, for instance to make a menu item sensitive/insensitive, so that
919 * you don't have to fool users that something is possible and then just show
920 * and error dialog. If you do this, you should make sure to also handle the
921 * errors that can happen due to races when you execute the operation.
923 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
926 g_file_query_exists (GFile *file,
927 GCancellable *cancellable)
931 g_return_val_if_fail (G_IS_FILE(file), FALSE);
933 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
934 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
937 g_object_unref (info);
945 * g_file_query_file_type:
946 * @file: input #GFile.
947 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
948 * @cancellable: optional #GCancellable object, %NULL to ignore.
950 * Utility function to inspect the #GFileType of a file. This is
951 * implemented using g_file_query_info() and as such does blocking I/O.
953 * The primary use case of this method is to check if a file is a regular file,
954 * directory, or symlink.
956 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
962 g_file_query_file_type (GFile *file,
963 GFileQueryInfoFlags flags,
964 GCancellable *cancellable)
969 g_return_val_if_fail (G_IS_FILE(file), FALSE);
970 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
974 file_type = g_file_info_get_file_type (info);
975 g_object_unref (info);
978 file_type = G_FILE_TYPE_UNKNOWN;
985 * @file: input #GFile.
986 * @attributes: an attribute query string.
987 * @flags: a set of #GFileQueryInfoFlags.
988 * @cancellable: optional #GCancellable object, %NULL to ignore.
991 * Gets the requested information about specified @file. The result
992 * is a #GFileInfo object that contains key-value attributes (such as
993 * the type or size of the file).
995 * The @attribute value is a string that specifies the file attributes that
996 * should be gathered. It is not an error if it's not possible to read a particular
997 * requested attribute from a file - it just won't be set. @attribute should
998 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
999 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1000 * namespace. An example attribute query be "standard::*,owner::user".
1001 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1003 * If @cancellable is not %NULL, then the operation can be cancelled by
1004 * triggering the cancellable object from another thread. If the operation
1005 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1007 * For symlinks, normally the information about the target of the
1008 * symlink is returned, rather than information about the symlink itself.
1009 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1010 * information about the symlink itself will be returned. Also, for symlinks
1011 * that point to non-existing files the information about the symlink itself
1014 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1015 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1017 * Returns: a #GFileInfo for the given @file, or %NULL on error.
1020 g_file_query_info (GFile *file,
1021 const char *attributes,
1022 GFileQueryInfoFlags flags,
1023 GCancellable *cancellable,
1028 g_return_val_if_fail (G_IS_FILE (file), NULL);
1030 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1033 iface = G_FILE_GET_IFACE (file);
1035 if (iface->query_info == NULL)
1037 g_set_error (error, G_IO_ERROR,
1038 G_IO_ERROR_NOT_SUPPORTED,
1039 _("Operation not supported"));
1043 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1047 * g_file_query_info_async:
1048 * @file: input #GFile.
1049 * @attributes: an attribute query string.
1050 * @flags: a set of #GFileQueryInfoFlags.
1051 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1053 * @cancellable: optional #GCancellable object, %NULL to ignore.
1054 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1055 * @user_data: the data to pass to callback function
1057 * Asynchronously gets the requested information about specified @file. The result
1058 * is a #GFileInfo object that contains key-value attributes (such as type or size
1061 * For more details, see g_file_query_info() which is
1062 * the synchronous version of this call.
1064 * When the operation is finished, @callback will be called. You can then call
1065 * g_file_query_info_finish() to get the result of the operation.
1068 g_file_query_info_async (GFile *file,
1069 const char *attributes,
1070 GFileQueryInfoFlags flags,
1072 GCancellable *cancellable,
1073 GAsyncReadyCallback callback,
1078 g_return_if_fail (G_IS_FILE (file));
1080 iface = G_FILE_GET_IFACE (file);
1081 (* iface->query_info_async) (file,
1091 * g_file_query_info_finish:
1092 * @file: input #GFile.
1093 * @res: a #GAsyncResult.
1094 * @error: a #GError.
1096 * Finishes an asynchronous file info query.
1097 * See g_file_query_info_async().
1099 * Returns: #GFileInfo for given @file or %NULL on error.
1102 g_file_query_info_finish (GFile *file,
1108 g_return_val_if_fail (G_IS_FILE (file), NULL);
1109 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1111 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1113 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1114 if (g_simple_async_result_propagate_error (simple, error))
1118 iface = G_FILE_GET_IFACE (file);
1119 return (* iface->query_info_finish) (file, res, error);
1123 * g_file_query_filesystem_info:
1124 * @file: input #GFile.
1125 * @attributes: an attribute query string.
1126 * @cancellable: optional #GCancellable object, %NULL to ignore.
1127 * @error: a #GError.
1129 * Similar to g_file_query_info(), but obtains information
1130 * about the filesystem the @file is on, rather than the file itself.
1131 * For instance the amount of space available and the type of
1134 * The @attribute value is a string that specifies the file attributes that
1135 * should be gathered. It is not an error if it's not possible to read a particular
1136 * requested attribute from a file - it just won't be set. @attribute should
1137 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1138 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1139 * namespace. The standard namespace for filesystem attributes is "fs".
1140 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1141 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1142 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1144 * If @cancellable is not %NULL, then the operation can be cancelled by
1145 * triggering the cancellable object from another thread. If the operation
1146 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1148 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1149 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1151 * Returns: a #GFileInfo or %NULL if there was an error.
1154 g_file_query_filesystem_info (GFile *file,
1155 const char *attributes,
1156 GCancellable *cancellable,
1161 g_return_val_if_fail (G_IS_FILE (file), NULL);
1163 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1166 iface = G_FILE_GET_IFACE (file);
1168 if (iface->query_filesystem_info == NULL)
1170 g_set_error (error, G_IO_ERROR,
1171 G_IO_ERROR_NOT_SUPPORTED,
1172 _("Operation not supported"));
1176 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1180 * g_file_query_filesystem_info_async:
1181 * @file: input #GFile.
1182 * @attributes: an attribute query string.
1183 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1185 * @cancellable: optional #GCancellable object, %NULL to ignore.
1186 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1187 * @user_data: the data to pass to callback function
1189 * Asynchronously gets the requested information about the filesystem
1190 * that the specified @file is on. The result is a #GFileInfo object
1191 * that contains key-value attributes (such as type or size for the
1194 * For more details, see g_file_query_filesystem_info() which is the
1195 * synchronous version of this call.
1197 * When the operation is finished, @callback will be called. You can
1198 * then call g_file_query_info_finish() to get the result of the
1202 g_file_query_filesystem_info_async (GFile *file,
1203 const char *attributes,
1205 GCancellable *cancellable,
1206 GAsyncReadyCallback callback,
1211 g_return_if_fail (G_IS_FILE (file));
1213 iface = G_FILE_GET_IFACE (file);
1214 (* iface->query_filesystem_info_async) (file,
1223 * g_file_query_filesystem_info_finish:
1224 * @file: input #GFile.
1225 * @res: a #GAsyncResult.
1226 * @error: a #GError.
1228 * Finishes an asynchronous filesystem info query. See
1229 * g_file_query_filesystem_info_async().
1231 * Returns: #GFileInfo for given @file or %NULL on error.
1234 g_file_query_filesystem_info_finish (GFile *file,
1240 g_return_val_if_fail (G_IS_FILE (file), NULL);
1241 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1243 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1245 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1246 if (g_simple_async_result_propagate_error (simple, error))
1250 iface = G_FILE_GET_IFACE (file);
1251 return (* iface->query_filesystem_info_finish) (file, res, error);
1255 * g_file_find_enclosing_mount:
1256 * @file: input #GFile.
1257 * @cancellable: optional #GCancellable object, %NULL to ignore.
1258 * @error: a #GError.
1260 * Gets a #GMount for the #GFile.
1262 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1263 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1266 * If @cancellable is not %NULL, then the operation can be cancelled by
1267 * triggering the cancellable object from another thread. If the operation
1268 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1270 * Returns: a #GMount where the @file is located or %NULL on error.
1273 g_file_find_enclosing_mount (GFile *file,
1274 GCancellable *cancellable,
1279 g_return_val_if_fail (G_IS_FILE (file), NULL);
1281 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1284 iface = G_FILE_GET_IFACE (file);
1285 if (iface->find_enclosing_mount == NULL)
1288 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1289 /* Translators: This is an error message when trying to find the
1290 * enclosing (user visible) mount of a file, but none exists. */
1291 _("Containing mount does not exist"));
1295 return (* iface->find_enclosing_mount) (file, cancellable, error);
1298 * g_file_find_enclosing_mount_async:
1300 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1302 * @cancellable: optional #GCancellable object, %NULL to ignore.
1303 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1304 * @user_data: the data to pass to callback function
1306 * Asynchronously gets the mount for the file.
1308 * For more details, see g_file_find_enclosing_mount() which is
1309 * the synchronous version of this call.
1311 * When the operation is finished, @callback will be called. You can then call
1312 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1315 g_file_find_enclosing_mount_async (GFile *file,
1317 GCancellable *cancellable,
1318 GAsyncReadyCallback callback,
1323 g_return_if_fail (G_IS_FILE (file));
1325 iface = G_FILE_GET_IFACE (file);
1326 (* iface->find_enclosing_mount_async) (file,
1334 * g_file_find_enclosing_mount_finish:
1336 * @res: a #GAsyncResult
1339 * Finishes an asynchronous find mount request.
1340 * See g_file_find_enclosing_mount_async().
1342 * Returns: #GMount for given @file or %NULL on error.
1345 g_file_find_enclosing_mount_finish (GFile *file,
1351 g_return_val_if_fail (G_IS_FILE (file), NULL);
1352 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1354 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1356 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1357 if (g_simple_async_result_propagate_error (simple, error))
1361 iface = G_FILE_GET_IFACE (file);
1362 return (* iface->find_enclosing_mount_finish) (file, res, error);
1368 * @file: #GFile to read.
1369 * @cancellable: a #GCancellable
1370 * @error: a #GError, or %NULL
1372 * Opens a file for reading. The result is a #GFileInputStream that
1373 * can be used to read the contents of the file.
1375 * If @cancellable is not %NULL, then the operation can be cancelled by
1376 * triggering the cancellable object from another thread. If the operation
1377 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1379 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1380 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1381 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1383 * Returns: #GFileInputStream or %NULL on error.
1386 g_file_read (GFile *file,
1387 GCancellable *cancellable,
1392 g_return_val_if_fail (G_IS_FILE (file), NULL);
1394 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1397 iface = G_FILE_GET_IFACE (file);
1399 if (iface->read_fn == NULL)
1401 g_set_error (error, G_IO_ERROR,
1402 G_IO_ERROR_NOT_SUPPORTED,
1403 _("Operation not supported"));
1407 return (* iface->read_fn) (file, cancellable, error);
1412 * @file: input #GFile.
1413 * @flags: a set of #GFileCreateFlags.
1414 * @cancellable: optional #GCancellable object, %NULL to ignore.
1415 * @error: a #GError, or %NULL
1417 * Gets an output stream for appending data to the file. If
1418 * the file doesn't already exist it is created.
1420 * By default files created are generally readable by everyone,
1421 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1422 * will be made readable only to the current user, to the level that
1423 * is supported on the target filesystem.
1425 * If @cancellable is not %NULL, then the operation can be cancelled by
1426 * triggering the cancellable object from another thread. If the operation
1427 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1429 * Some file systems don't allow all file names, and may
1430 * return an G_IO_ERROR_INVALID_FILENAME error.
1431 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1432 * returned. Other errors are possible too, and depend on what kind of
1433 * filesystem the file is on.
1435 * Returns: a #GFileOutputStream.
1438 g_file_append_to (GFile *file,
1439 GFileCreateFlags flags,
1440 GCancellable *cancellable,
1445 g_return_val_if_fail (G_IS_FILE (file), NULL);
1447 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1450 iface = G_FILE_GET_IFACE (file);
1452 if (iface->append_to == NULL)
1454 g_set_error (error, G_IO_ERROR,
1455 G_IO_ERROR_NOT_SUPPORTED,
1456 _("Operation not supported"));
1460 return (* iface->append_to) (file, flags, cancellable, error);
1465 * @file: input #GFile.
1466 * @flags: a set of #GFileCreateFlags.
1467 * @cancellable: optional #GCancellable object, %NULL to ignore.
1468 * @error: a #GError, or %NULL
1470 * Creates a new file and returns an output stream for writing to it.
1471 * The file must not already exists.
1473 * By default files created are generally readable by everyone,
1474 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1475 * will be made readable only to the current user, to the level that
1476 * is supported on the target filesystem.
1478 * If @cancellable is not %NULL, then the operation can be cancelled by
1479 * triggering the cancellable object from another thread. If the operation
1480 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1482 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1483 * error will be returned.
1484 * Some file systems don't allow all file names, and may
1485 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1486 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1487 * Other errors are possible too, and depend on what kind of
1488 * filesystem the file is on.
1490 * Returns: a #GFileOutputStream for the newly created file, or
1494 g_file_create (GFile *file,
1495 GFileCreateFlags flags,
1496 GCancellable *cancellable,
1501 g_return_val_if_fail (G_IS_FILE (file), NULL);
1503 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1506 iface = G_FILE_GET_IFACE (file);
1508 if (iface->create == NULL)
1510 g_set_error (error, G_IO_ERROR,
1511 G_IO_ERROR_NOT_SUPPORTED,
1512 _("Operation not supported"));
1516 return (* iface->create) (file, flags, cancellable, error);
1521 * @file: input #GFile.
1522 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1523 * current #GFile, or #NULL to ignore.
1524 * @make_backup: %TRUE if a backup should be created.
1525 * @flags: a set of #GFileCreateFlags.
1526 * @cancellable: optional #GCancellable object, %NULL to ignore.
1527 * @error: a #GError, or %NULL
1529 * Returns an output stream for overwriting the file, possibly
1530 * creating a backup copy of the file first.
1532 * This will try to replace the file in the safest way possible so
1533 * that any errors during the writing will not affect an already
1534 * existing copy of the file. For instance, for local files it
1535 * may write to a temporary file and then atomically rename over
1536 * the destination when the stream is closed.
1538 * By default files created are generally readable by everyone,
1539 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1540 * will be made readable only to the current user, to the level that
1541 * is supported on the target filesystem.
1543 * If @cancellable is not %NULL, then the operation can be cancelled by
1544 * triggering the cancellable object from another thread. If the operation
1545 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1547 * If you pass in a non-#NULL @etag value, then this value is
1548 * compared to the current entity tag of the file, and if they differ
1549 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1550 * that the file has been changed since you last read it. You can get
1551 * the new etag from g_file_output_stream_get_etag() after you've
1552 * finished writing and closed the #GFileOutputStream. When you load
1553 * a new file you can use g_file_input_stream_query_info() to get
1554 * the etag of the file.
1556 * If @make_backup is %TRUE, this function will attempt to make a backup
1557 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1558 * error will be returned. If you want to replace anyway, try again with
1559 * @make_backup set to %FALSE.
1561 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1562 * and if the file is some other form of non-regular file then a
1563 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1564 * Some file systems don't allow all file names, and may
1565 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1566 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1567 * Other errors are possible too, and depend on what kind of
1568 * filesystem the file is on.
1570 * Returns: a #GFileOutputStream or %NULL on error.
1573 g_file_replace (GFile *file,
1575 gboolean make_backup,
1576 GFileCreateFlags flags,
1577 GCancellable *cancellable,
1582 g_return_val_if_fail (G_IS_FILE (file), NULL);
1584 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1587 iface = G_FILE_GET_IFACE (file);
1589 if (iface->replace == NULL)
1591 g_set_error (error, G_IO_ERROR,
1592 G_IO_ERROR_NOT_SUPPORTED,
1593 _("Operation not supported"));
1598 /* Handle empty tag string as NULL in consistent way. */
1599 if (etag && *etag == 0)
1602 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1606 * g_file_read_async:
1607 * @file: input #GFile.
1608 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1610 * @cancellable: optional #GCancellable object, %NULL to ignore.
1611 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1612 * @user_data: the data to pass to callback function
1614 * Asynchronously opens @file for reading.
1616 * For more details, see g_file_read() which is
1617 * the synchronous version of this call.
1619 * When the operation is finished, @callback will be called. You can then call
1620 * g_file_read_finish() to get the result of the operation.
1623 g_file_read_async (GFile *file,
1625 GCancellable *cancellable,
1626 GAsyncReadyCallback callback,
1631 g_return_if_fail (G_IS_FILE (file));
1633 iface = G_FILE_GET_IFACE (file);
1634 (* iface->read_async) (file,
1642 * g_file_read_finish:
1643 * @file: input #GFile.
1644 * @res: a #GAsyncResult.
1645 * @error: a #GError, or %NULL
1647 * Finishes an asynchronous file read operation started with
1648 * g_file_read_async().
1650 * Returns: a #GFileInputStream or %NULL on error.
1653 g_file_read_finish (GFile *file,
1659 g_return_val_if_fail (G_IS_FILE (file), NULL);
1660 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1662 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1664 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1665 if (g_simple_async_result_propagate_error (simple, error))
1669 iface = G_FILE_GET_IFACE (file);
1670 return (* iface->read_finish) (file, res, error);
1674 * g_file_append_to_async:
1675 * @file: input #GFile.
1676 * @flags: a set of #GFileCreateFlags.
1677 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1679 * @cancellable: optional #GCancellable object, %NULL to ignore.
1680 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1681 * @user_data: the data to pass to callback function
1683 * Asynchronously opens @file for appending.
1685 * For more details, see g_file_append_to() which is
1686 * the synchronous version of this call.
1688 * When the operation is finished, @callback will be called. You can then call
1689 * g_file_append_to_finish() to get the result of the operation.
1692 g_file_append_to_async (GFile *file,
1693 GFileCreateFlags flags,
1695 GCancellable *cancellable,
1696 GAsyncReadyCallback callback,
1701 g_return_if_fail (G_IS_FILE (file));
1703 iface = G_FILE_GET_IFACE (file);
1704 (* iface->append_to_async) (file,
1713 * g_file_append_to_finish:
1714 * @file: input #GFile.
1715 * @res: #GAsyncResult
1716 * @error: a #GError, or %NULL
1718 * Finishes an asynchronous file append operation started with
1719 * g_file_append_to_async().
1721 * Returns: a valid #GFileOutputStream or %NULL on error.
1724 g_file_append_to_finish (GFile *file,
1730 g_return_val_if_fail (G_IS_FILE (file), NULL);
1731 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1733 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1735 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1736 if (g_simple_async_result_propagate_error (simple, error))
1740 iface = G_FILE_GET_IFACE (file);
1741 return (* iface->append_to_finish) (file, res, error);
1745 * g_file_create_async:
1746 * @file: input #GFile.
1747 * @flags: a set of #GFileCreateFlags.
1748 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1750 * @cancellable: optional #GCancellable object, %NULL to ignore.
1751 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1752 * @user_data: the data to pass to callback function
1754 * Asynchronously creates a new file and returns an output stream for writing to it.
1755 * The file must not already exists.
1757 * For more details, see g_file_create() which is
1758 * the synchronous version of this call.
1760 * When the operation is finished, @callback will be called. You can then call
1761 * g_file_create_finish() to get the result of the operation.
1764 g_file_create_async (GFile *file,
1765 GFileCreateFlags flags,
1767 GCancellable *cancellable,
1768 GAsyncReadyCallback callback,
1773 g_return_if_fail (G_IS_FILE (file));
1775 iface = G_FILE_GET_IFACE (file);
1776 (* iface->create_async) (file,
1785 * g_file_create_finish:
1786 * @file: input #GFile.
1787 * @res: a #GAsyncResult.
1788 * @error: a #GError, or %NULL
1790 * Finishes an asynchronous file create operation started with
1791 * g_file_create_async().
1793 * Returns: a #GFileOutputStream or %NULL on error.
1796 g_file_create_finish (GFile *file,
1802 g_return_val_if_fail (G_IS_FILE (file), NULL);
1803 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1805 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1807 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1808 if (g_simple_async_result_propagate_error (simple, error))
1812 iface = G_FILE_GET_IFACE (file);
1813 return (* iface->create_finish) (file, res, error);
1817 * g_file_replace_async:
1818 * @file: input #GFile.
1819 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1820 * current #GFile, or NULL to ignore.
1821 * @make_backup: %TRUE if a backup should be created.
1822 * @flags: a set of #GFileCreateFlags.
1823 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1825 * @cancellable: optional #GCancellable object, %NULL to ignore.
1826 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1827 * @user_data: the data to pass to callback function
1829 * Asynchronously overwrites the file, replacing the contents, possibly
1830 * creating a backup copy of the file first.
1832 * For more details, see g_file_replace() which is
1833 * the synchronous version of this call.
1835 * When the operation is finished, @callback will be called. You can then call
1836 * g_file_replace_finish() to get the result of the operation.
1839 g_file_replace_async (GFile *file,
1841 gboolean make_backup,
1842 GFileCreateFlags flags,
1844 GCancellable *cancellable,
1845 GAsyncReadyCallback callback,
1850 g_return_if_fail (G_IS_FILE (file));
1852 iface = G_FILE_GET_IFACE (file);
1853 (* iface->replace_async) (file,
1864 * g_file_replace_finish:
1865 * @file: input #GFile.
1866 * @res: a #GAsyncResult.
1867 * @error: a #GError, or %NULL
1869 * Finishes an asynchronous file replace operation started with
1870 * g_file_replace_async().
1872 * Returns: a #GFileOutputStream, or %NULL on error.
1875 g_file_replace_finish (GFile *file,
1881 g_return_val_if_fail (G_IS_FILE (file), NULL);
1882 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1884 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1886 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1887 if (g_simple_async_result_propagate_error (simple, error))
1891 iface = G_FILE_GET_IFACE (file);
1892 return (* iface->replace_finish) (file, res, error);
1896 copy_symlink (GFile *destination,
1897 GFileCopyFlags flags,
1898 GCancellable *cancellable,
1903 gboolean tried_delete;
1905 GFileType file_type;
1907 tried_delete = FALSE;
1911 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1913 /* Maybe it already existed, and we want to overwrite? */
1914 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1915 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1917 g_error_free (my_error);
1920 /* Don't overwrite if the destination is a directory */
1921 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1922 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1923 cancellable, &my_error);
1926 file_type = g_file_info_get_file_type (info);
1927 g_object_unref (info);
1929 if (file_type == G_FILE_TYPE_DIRECTORY)
1931 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1932 _("Can't copy over directory"));
1937 if (!g_file_delete (destination, cancellable, error))
1940 tried_delete = TRUE;
1944 g_propagate_error (error, my_error);
1951 static GInputStream *
1952 open_source_for_copy (GFile *source,
1954 GFileCopyFlags flags,
1955 GCancellable *cancellable,
1961 GFileType file_type;
1964 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1968 /* There was an error opening the source, try to set a good error for it: */
1970 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1972 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1973 * as that is less useful to the app. Better check for errors on the
1976 g_error_free (my_error);
1979 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1980 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1981 cancellable, &my_error);
1984 file_type = g_file_info_get_file_type (info);
1985 g_object_unref (info);
1987 if (flags & G_FILE_COPY_OVERWRITE)
1989 if (file_type == G_FILE_TYPE_DIRECTORY)
1991 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1992 _("Can't copy directory over directory"));
1995 /* continue to would_recurse error */
1999 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2000 _("Target file exists"));
2006 /* Error getting info from target, return that error
2007 * (except for NOT_FOUND, which is no error here)
2009 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
2011 g_propagate_error (error, my_error);
2014 g_error_free (my_error);
2017 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2018 _("Can't recursively copy directory"));
2022 g_propagate_error (error, my_error);
2027 should_copy (GFileAttributeInfo *info,
2031 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2032 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2036 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2037 GFileAttributeInfoList *namespaces,
2045 s = g_string_new ("");
2049 for (i = 0; i < attributes->n_infos; i++)
2051 if (should_copy (&attributes->infos[i], as_move))
2056 g_string_append_c (s, ',');
2058 g_string_append (s, attributes->infos[i].name);
2065 for (i = 0; i < namespaces->n_infos; i++)
2067 if (should_copy (&namespaces->infos[i], as_move))
2072 g_string_append_c (s, ',');
2074 g_string_append (s, namespaces->infos[i].name);
2075 g_string_append (s, ":*");
2080 return g_string_free (s, FALSE);
2084 * g_file_copy_attributes:
2085 * @source: a #GFile with attributes.
2086 * @destination: a #GFile to copy attributes to.
2087 * @flags: a set of #GFileCopyFlags.
2088 * @cancellable: optional #GCancellable object, %NULL to ignore.
2089 * @error: a #GError, %NULL to ignore.
2091 * Copies the file attributes from @source to @destination.
2093 * Normally only a subset of the file attributes are copied,
2094 * those that are copies in a normal file copy operation
2095 * (which for instance does not include e.g. mtime). However
2096 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2097 * all the metadata that is possible to copy is copied.
2099 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2102 g_file_copy_attributes (GFile *source,
2104 GFileCopyFlags flags,
2105 GCancellable *cancellable,
2108 GFileAttributeInfoList *attributes, *namespaces;
2109 char *attrs_to_read;
2113 gboolean source_nofollow_symlinks;
2115 as_move = flags & G_FILE_COPY_ALL_METADATA;
2116 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2118 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2119 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2120 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2122 if (attributes == NULL && namespaces == NULL)
2125 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2127 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2128 * we just don't copy it.
2130 info = g_file_query_info (source, attrs_to_read,
2131 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2135 g_free (attrs_to_read);
2140 res = g_file_set_attributes_from_info (destination,
2144 g_object_unref (info);
2147 g_file_attribute_info_list_unref (attributes);
2148 g_file_attribute_info_list_unref (namespaces);
2153 /* Closes the streams */
2155 copy_stream_with_progress (GInputStream *in,
2158 GCancellable *cancellable,
2159 GFileProgressCallback progress_callback,
2160 gpointer progress_callback_data,
2163 gssize n_read, n_written;
2164 goffset current_size;
2165 char buffer[1024*64], *p;
2171 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2172 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2176 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2177 total_size = g_file_info_get_size (info);
2178 g_object_unref (info);
2181 if (total_size == -1)
2183 info = g_file_query_info (source,
2184 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2185 G_FILE_QUERY_INFO_NONE,
2189 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2190 total_size = g_file_info_get_size (info);
2191 g_object_unref (info);
2195 if (total_size == -1)
2202 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2212 current_size += n_read;
2217 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2218 if (n_written == -1)
2225 n_read -= n_written;
2231 if (progress_callback)
2232 progress_callback (current_size, total_size, progress_callback_data);
2236 error = NULL; /* Ignore further errors */
2238 /* Make sure we send full copied size */
2239 if (progress_callback)
2240 progress_callback (current_size, total_size, progress_callback_data);
2242 /* Don't care about errors in source here */
2243 g_input_stream_close (in, cancellable, NULL);
2245 /* But write errors on close are bad! */
2246 if (!g_output_stream_close (out, cancellable, error))
2249 g_object_unref (in);
2250 g_object_unref (out);
2256 file_copy_fallback (GFile *source,
2258 GFileCopyFlags flags,
2259 GCancellable *cancellable,
2260 GFileProgressCallback progress_callback,
2261 gpointer progress_callback_data,
2269 /* Maybe copy the symlink? */
2270 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2272 info = g_file_query_info (source,
2273 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2274 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2280 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2281 (target = g_file_info_get_symlink_target (info)) != NULL)
2283 if (!copy_symlink (destination, flags, cancellable, target, error))
2285 g_object_unref (info);
2289 g_object_unref (info);
2293 g_object_unref (info);
2296 in = open_source_for_copy (source, destination, flags, cancellable, error);
2300 if (flags & G_FILE_COPY_OVERWRITE)
2302 out = (GOutputStream *)g_file_replace (destination,
2304 flags & G_FILE_COPY_BACKUP,
2305 cancellable, error);
2309 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2314 g_object_unref (in);
2318 if (!copy_stream_with_progress (in, out, source, cancellable,
2319 progress_callback, progress_callback_data,
2325 /* Ignore errors here. Failure to copy metadata is not a hard error */
2326 g_file_copy_attributes (source, destination,
2327 flags, cancellable, NULL);
2334 * @source: input #GFile.
2335 * @destination: destination #GFile
2336 * @flags: set of #GFileCopyFlags
2337 * @cancellable: optional #GCancellable object, %NULL to ignore.
2338 * @progress_callback: function to callback with progress information
2339 * @progress_callback_data: user data to pass to @progress_callback
2340 * @error: #GError to set on error, or %NULL
2342 * Copies the file @source to the location specified by @destination.
2343 * Can not handle recursive copies of directories.
2345 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2346 * existing @destination file is overwritten.
2348 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2349 * will be copied as symlinks, otherwise the target of the
2350 * @source symlink will be copied.
2352 * If @cancellable is not %NULL, then the operation can be cancelled by
2353 * triggering the cancellable object from another thread. If the operation
2354 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2356 * If @progress_callback is not %NULL, then the operation can be monitored by
2357 * setting this to a #GFileProgressCallback function. @progress_callback_data
2358 * will be passed to this function. It is guaranteed that this callback will
2359 * be called after all data has been transferred with the total number of bytes
2360 * copied during the operation.
2362 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2363 * error is returned, independent on the status of the @destination.
2365 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2366 * error G_IO_ERROR_EXISTS is returned.
2368 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2369 * error is returned. If trying to overwrite a directory with a directory the
2370 * G_IO_ERROR_WOULD_MERGE error is returned.
2372 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2373 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2376 * If you are interested in copying the #GFile object itself (not the on-disk
2377 * file), see g_file_dup().
2379 * Returns: %TRUE on success, %FALSE otherwise.
2382 g_file_copy (GFile *source,
2384 GFileCopyFlags flags,
2385 GCancellable *cancellable,
2386 GFileProgressCallback progress_callback,
2387 gpointer progress_callback_data,
2394 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2395 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2397 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2400 iface = G_FILE_GET_IFACE (destination);
2404 res = (* iface->copy) (source, destination,
2406 progress_callback, progress_callback_data,
2412 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2414 g_propagate_error (error, my_error);
2419 /* If the types are different, and the destination method failed
2420 also try the source method */
2421 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2423 iface = G_FILE_GET_IFACE (source);
2428 res = (* iface->copy) (source, destination,
2430 progress_callback, progress_callback_data,
2436 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2438 g_propagate_error (error, my_error);
2444 return file_copy_fallback (source, destination, flags, cancellable,
2445 progress_callback, progress_callback_data,
2450 * g_file_copy_async:
2451 * @source: input #GFile.
2452 * @destination: destination #GFile
2453 * @flags: set of #GFileCopyFlags
2454 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2456 * @cancellable: optional #GCancellable object, %NULL to ignore.
2457 * @progress_callback: function to callback with progress information
2458 * @progress_callback_data: user data to pass to @progress_callback
2459 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2460 * @user_data: the data to pass to callback function
2462 * Copies the file @source to the location specified by @destination
2463 * asynchronously. For details of the behaviour, see g_file_copy().
2465 * If @progress_callback is not %NULL, then that function that will be called
2466 * just like in g_file_copy(), however the callback will run in the main loop,
2467 * not in the thread that is doing the I/O operation.
2469 * When the operation is finished, @callback will be called. You can then call
2470 * g_file_copy_finish() to get the result of the operation.
2473 g_file_copy_async (GFile *source,
2475 GFileCopyFlags flags,
2477 GCancellable *cancellable,
2478 GFileProgressCallback progress_callback,
2479 gpointer progress_callback_data,
2480 GAsyncReadyCallback callback,
2485 g_return_if_fail (G_IS_FILE (source));
2486 g_return_if_fail (G_IS_FILE (destination));
2488 iface = G_FILE_GET_IFACE (source);
2489 (* iface->copy_async) (source,
2495 progress_callback_data,
2501 * g_file_copy_finish:
2502 * @file: input #GFile.
2503 * @res: a #GAsyncResult.
2504 * @error: a #GError, or %NULL
2506 * Finishes copying the file started with
2507 * g_file_copy_async().
2509 * Returns: a %TRUE on success, %FALSE on error.
2512 g_file_copy_finish (GFile *file,
2518 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2519 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2521 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2523 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2525 if (g_simple_async_result_propagate_error (simple, error))
2529 iface = G_FILE_GET_IFACE (file);
2530 return (* iface->copy_finish) (file, res, error);
2535 * @source: #GFile pointing to the source location.
2536 * @destination: #GFile pointing to the destination location.
2537 * @flags: set of #GFileCopyFlags.
2538 * @cancellable: optional #GCancellable object, %NULL to ignore.
2539 * @progress_callback: #GFileProgressCallback function for updates.
2540 * @progress_callback_data: gpointer to user data for the callback function.
2541 * @error: #GError for returning error conditions, or %NULL
2544 * Tries to move the file or directory @source to the location specified by @destination.
2545 * If native move operations are supported then this is used, otherwise a copy + delete
2546 * fallback is used. The native implementation may support moving directories (for instance
2547 * on moves inside the same filesystem), but the fallback code does not.
2549 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2550 * existing @destination file is overwritten.
2552 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2553 * will be copied as symlinks, otherwise the target of the
2554 * @source symlink will be copied.
2556 * If @cancellable is not %NULL, then the operation can be cancelled by
2557 * triggering the cancellable object from another thread. If the operation
2558 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2560 * If @progress_callback is not %NULL, then the operation can be monitored by
2561 * setting this to a #GFileProgressCallback function. @progress_callback_data
2562 * will be passed to this function. It is guaranteed that this callback will
2563 * be called after all data has been transferred with the total number of bytes
2564 * copied during the operation.
2566 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2567 * error is returned, independent on the status of the @destination.
2569 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2570 * error G_IO_ERROR_EXISTS is returned.
2572 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2573 * error is returned. If trying to overwrite a directory with a directory the
2574 * G_IO_ERROR_WOULD_MERGE error is returned.
2576 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2577 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2578 * may be returned (if the native move operation isn't available).
2580 * Returns: %TRUE on successful move, %FALSE otherwise.
2583 g_file_move (GFile *source,
2585 GFileCopyFlags flags,
2586 GCancellable *cancellable,
2587 GFileProgressCallback progress_callback,
2588 gpointer progress_callback_data,
2595 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2596 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2598 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2601 iface = G_FILE_GET_IFACE (destination);
2605 res = (* iface->move) (source, destination,
2607 progress_callback, progress_callback_data,
2613 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2615 g_propagate_error (error, my_error);
2620 /* If the types are different, and the destination method failed
2621 also try the source method */
2622 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2624 iface = G_FILE_GET_IFACE (source);
2629 res = (* iface->move) (source, destination,
2631 progress_callback, progress_callback_data,
2637 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2639 g_propagate_error (error, my_error);
2645 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2647 g_set_error (error, G_IO_ERROR,
2648 G_IO_ERROR_NOT_SUPPORTED,
2649 _("Operation not supported"));
2653 flags |= G_FILE_COPY_ALL_METADATA;
2654 if (!g_file_copy (source, destination, flags, cancellable,
2655 progress_callback, progress_callback_data,
2659 return g_file_delete (source, cancellable, error);
2663 * g_file_make_directory
2664 * @file: input #GFile.
2665 * @cancellable: optional #GCancellable object, %NULL to ignore.
2666 * @error: a #GError, or %NULL
2668 * Creates a directory.
2670 * If @cancellable is not %NULL, then the operation can be cancelled by
2671 * triggering the cancellable object from another thread. If the operation
2672 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2674 * Returns: %TRUE on successful creation, %FALSE otherwise.
2677 g_file_make_directory (GFile *file,
2678 GCancellable *cancellable,
2683 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2685 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2688 iface = G_FILE_GET_IFACE (file);
2690 if (iface->make_directory == NULL)
2692 g_set_error (error, G_IO_ERROR,
2693 G_IO_ERROR_NOT_SUPPORTED,
2694 _("Operation not supported"));
2698 return (* iface->make_directory) (file, cancellable, error);
2702 * g_file_make_symbolic_link:
2703 * @file: input #GFile.
2704 * @symlink_value: a string with the value of the new symlink.
2705 * @cancellable: optional #GCancellable object, %NULL to ignore.
2706 * @error: a #GError.
2708 * Creates a symbolic link.
2710 * If @cancellable is not %NULL, then the operation can be cancelled by
2711 * triggering the cancellable object from another thread. If the operation
2712 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2714 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2717 g_file_make_symbolic_link (GFile *file,
2718 const char *symlink_value,
2719 GCancellable *cancellable,
2724 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2725 g_return_val_if_fail (symlink_value != NULL, FALSE);
2727 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2730 if (*symlink_value == '\0')
2732 g_set_error (error, G_IO_ERROR,
2733 G_IO_ERROR_INVALID_ARGUMENT,
2734 _("Invalid symlink value given"));
2738 iface = G_FILE_GET_IFACE (file);
2740 if (iface->make_symbolic_link == NULL)
2742 g_set_error (error, G_IO_ERROR,
2743 G_IO_ERROR_NOT_SUPPORTED,
2744 _("Operation not supported"));
2748 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2753 * @file: input #GFile.
2754 * @cancellable: optional #GCancellable object, %NULL to ignore.
2755 * @error: a #GError, or %NULL
2757 * Deletes a file. If the @file is a directory, it will only be deleted if it
2760 * If @cancellable is not %NULL, then the operation can be cancelled by
2761 * triggering the cancellable object from another thread. If the operation
2762 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2764 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2767 g_file_delete (GFile *file,
2768 GCancellable *cancellable,
2773 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2775 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2778 iface = G_FILE_GET_IFACE (file);
2780 if (iface->delete_file == NULL)
2782 g_set_error (error, G_IO_ERROR,
2783 G_IO_ERROR_NOT_SUPPORTED,
2784 _("Operation not supported"));
2788 return (* iface->delete_file) (file, cancellable, error);
2793 * @file: #GFile to send to trash.
2794 * @cancellable: optional #GCancellable object, %NULL to ignore.
2795 * @error: a #GError, or %NULL
2797 * Sends @file to the "Trashcan", if possible. This is similar to
2798 * deleting it, but the user can recover it before emptying the trashcan.
2799 * Not all file systems support trashing, so this call can return the
2800 * %G_IO_ERROR_NOT_SUPPORTED error.
2803 * If @cancellable is not %NULL, then the operation can be cancelled by
2804 * triggering the cancellable object from another thread. If the operation
2805 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2807 * Returns: %TRUE on successful trash, %FALSE otherwise.
2810 g_file_trash (GFile *file,
2811 GCancellable *cancellable,
2816 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2818 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2821 iface = G_FILE_GET_IFACE (file);
2823 if (iface->trash == NULL)
2826 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2827 _("Trash not supported"));
2831 return (* iface->trash) (file, cancellable, error);
2835 * g_file_set_display_name:
2836 * @file: input #GFile.
2837 * @display_name: a string.
2838 * @cancellable: optional #GCancellable object, %NULL to ignore.
2839 * @error: a #GError, or %NULL
2841 * Renames @file to the specified display name.
2843 * The display name is converted from UTF8 to the correct encoding for the target
2844 * filesystem if possible and the @file is renamed to this.
2846 * If you want to implement a rename operation in the user interface the edit name
2847 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2848 * widget, and then the result after editing should be passed to g_file_set_display_name().
2850 * On success the resulting converted filename is returned.
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: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2859 g_file_set_display_name (GFile *file,
2860 const char *display_name,
2861 GCancellable *cancellable,
2866 g_return_val_if_fail (G_IS_FILE (file), NULL);
2867 g_return_val_if_fail (display_name != NULL, NULL);
2869 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2873 G_IO_ERROR_INVALID_ARGUMENT,
2874 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2878 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2881 iface = G_FILE_GET_IFACE (file);
2883 return (* iface->set_display_name) (file, display_name, cancellable, error);
2887 * g_file_set_display_name_async:
2888 * @file: input #GFile.
2889 * @display_name: a string.
2890 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2892 * @cancellable: optional #GCancellable object, %NULL to ignore.
2893 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2894 * @user_data: the data to pass to callback function
2896 * Asynchronously sets the display name for a given #GFile.
2898 * For more details, see g_set_display_name() which is
2899 * the synchronous version of this call.
2901 * When the operation is finished, @callback will be called. You can then call
2902 * g_file_set_display_name_finish() to get the result of the operation.
2905 g_file_set_display_name_async (GFile *file,
2906 const char *display_name,
2908 GCancellable *cancellable,
2909 GAsyncReadyCallback callback,
2914 g_return_if_fail (G_IS_FILE (file));
2915 g_return_if_fail (display_name != NULL);
2917 iface = G_FILE_GET_IFACE (file);
2918 (* iface->set_display_name_async) (file,
2927 * g_file_set_display_name_finish:
2928 * @file: input #GFile.
2929 * @res: a #GAsyncResult.
2930 * @error: a #GError, or %NULL
2932 * Finishes setting a display name started with
2933 * g_file_set_display_name_async().
2935 * Returns: a #GFile or %NULL on error.
2938 g_file_set_display_name_finish (GFile *file,
2944 g_return_val_if_fail (G_IS_FILE (file), NULL);
2945 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2947 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2949 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2950 if (g_simple_async_result_propagate_error (simple, error))
2954 iface = G_FILE_GET_IFACE (file);
2955 return (* iface->set_display_name_finish) (file, res, error);
2959 * g_file_query_settable_attributes:
2960 * @file: input #GFile.
2961 * @cancellable: optional #GCancellable object, %NULL to ignore.
2962 * @error: a #GError, or %NULL
2964 * Obtain the list of settable attributes for the file.
2966 * Returns the type and full attribute name of all the attributes
2967 * that can be set on this file. This doesn't mean setting it will always
2968 * succeed though, you might get an access failure, or some specific
2969 * file may not support a specific attribute.
2971 * If @cancellable is not %NULL, then the operation can be cancelled by
2972 * triggering the cancellable object from another thread. If the operation
2973 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2975 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2976 * When you are done with it, release it with g_file_attribute_info_list_unref()
2978 GFileAttributeInfoList *
2979 g_file_query_settable_attributes (GFile *file,
2980 GCancellable *cancellable,
2985 GFileAttributeInfoList *list;
2987 g_return_val_if_fail (G_IS_FILE (file), NULL);
2989 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2992 iface = G_FILE_GET_IFACE (file);
2994 if (iface->query_settable_attributes == NULL)
2995 return g_file_attribute_info_list_new ();
2998 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3002 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3004 list = g_file_attribute_info_list_new ();
3005 g_error_free (my_error);
3008 g_propagate_error (error, my_error);
3015 * g_file_query_writable_namespaces:
3016 * @file: input #GFile.
3017 * @cancellable: optional #GCancellable object, %NULL to ignore.
3018 * @error: a #GError, or %NULL
3020 * Obtain the list of attribute namespaces where new attributes
3021 * can be created by a user. An example of this is extended
3022 * attributes (in the "xattr" namespace).
3024 * If @cancellable is not %NULL, then the operation can be cancelled by
3025 * triggering the cancellable object from another thread. If the operation
3026 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3028 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3029 * When you are done with it, release it with g_file_attribute_info_list_unref()
3031 GFileAttributeInfoList *
3032 g_file_query_writable_namespaces (GFile *file,
3033 GCancellable *cancellable,
3038 GFileAttributeInfoList *list;
3040 g_return_val_if_fail (G_IS_FILE (file), NULL);
3042 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3045 iface = G_FILE_GET_IFACE (file);
3047 if (iface->query_writable_namespaces == NULL)
3048 return g_file_attribute_info_list_new ();
3051 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3055 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3057 list = g_file_attribute_info_list_new ();
3058 g_error_free (my_error);
3061 g_propagate_error (error, my_error);
3068 * g_file_set_attribute:
3069 * @file: input #GFile.
3070 * @attribute: a string containing the attribute's name.
3071 * @type: The type of the attribute
3072 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3073 * @flags: a set of #GFileQueryInfoFlags.
3074 * @cancellable: optional #GCancellable object, %NULL to ignore.
3075 * @error: a #GError, or %NULL
3077 * Sets an attribute in the file with attribute name @attribute to @value.
3079 * If @cancellable is not %NULL, then the operation can be cancelled by
3080 * triggering the cancellable object from another thread. If the operation
3081 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3083 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3086 g_file_set_attribute (GFile *file,
3087 const char *attribute,
3088 GFileAttributeType type,
3090 GFileQueryInfoFlags flags,
3091 GCancellable *cancellable,
3096 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3097 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3099 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3102 iface = G_FILE_GET_IFACE (file);
3104 if (iface->set_attribute == NULL)
3106 g_set_error (error, G_IO_ERROR,
3107 G_IO_ERROR_NOT_SUPPORTED,
3108 _("Operation not supported"));
3112 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3116 * g_file_set_attributes_from_info:
3117 * @file: input #GFile.
3118 * @info: a #GFileInfo.
3119 * @flags: #GFileQueryInfoFlags
3120 * @cancellable: optional #GCancellable object, %NULL to ignore.
3121 * @error: a #GError, or %NULL
3123 * Tries to set all attributes in the #GFileInfo on the target values,
3124 * not stopping on the first error.
3126 * If there is any error during this operation then @error will be set to
3127 * the first error. Error on particular fields are flagged by setting
3128 * the "status" field in the attribute value to
3129 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3132 * If @cancellable is not %NULL, then the operation can be cancelled by
3133 * triggering the cancellable object from another thread. If the operation
3134 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3136 * Returns: %TRUE if there was any error, %FALSE otherwise.
3139 g_file_set_attributes_from_info (GFile *file,
3141 GFileQueryInfoFlags flags,
3142 GCancellable *cancellable,
3147 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3148 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3150 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3153 g_file_info_clear_status (info);
3155 iface = G_FILE_GET_IFACE (file);
3157 return (* iface->set_attributes_from_info) (file,
3166 g_file_real_set_attributes_from_info (GFile *file,
3168 GFileQueryInfoFlags flags,
3169 GCancellable *cancellable,
3175 GFileAttributeValue *value;
3179 attributes = g_file_info_list_attributes (info, NULL);
3181 for (i = 0; attributes[i] != NULL; i++)
3183 value = _g_file_info_get_attribute_value (info, attributes[i]);
3185 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3188 if (!g_file_set_attribute (file, attributes[i],
3189 value->type, _g_file_attribute_value_peek_as_pointer (value),
3190 flags, cancellable, error))
3192 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3194 /* Don't set error multiple times */
3198 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3201 g_strfreev (attributes);
3207 * g_file_set_attributes_async:
3208 * @file: input #GFile.
3209 * @info: a #GFileInfo.
3210 * @flags: a #GFileQueryInfoFlags.
3211 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3213 * @cancellable: optional #GCancellable object, %NULL to ignore.
3214 * @callback: a #GAsyncReadyCallback.
3215 * @user_data: a #gpointer.
3217 * Asynchronously sets the attributes of @file with @info.
3219 * For more details, see g_file_set_attributes_from_info() which is
3220 * the synchronous version of this call.
3222 * When the operation is finished, @callback will be called. You can then call
3223 * g_file_set_attributes_finish() to get the result of the operation.
3226 g_file_set_attributes_async (GFile *file,
3228 GFileQueryInfoFlags flags,
3230 GCancellable *cancellable,
3231 GAsyncReadyCallback callback,
3236 g_return_if_fail (G_IS_FILE (file));
3237 g_return_if_fail (G_IS_FILE_INFO (info));
3239 iface = G_FILE_GET_IFACE (file);
3240 (* iface->set_attributes_async) (file,
3250 * g_file_set_attributes_finish:
3251 * @file: input #GFile.
3252 * @result: a #GAsyncResult.
3253 * @info: a #GFileInfo.
3254 * @error: a #GError, or %NULL
3256 * Finishes setting an attribute started in g_file_set_attributes_async().
3258 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3261 g_file_set_attributes_finish (GFile *file,
3262 GAsyncResult *result,
3268 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3269 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3271 /* No standard handling of errors here, as we must set info even
3274 iface = G_FILE_GET_IFACE (file);
3275 return (* iface->set_attributes_finish) (file, result, info, error);
3279 * g_file_set_attribute_string:
3280 * @file: input #GFile.
3281 * @attribute: a string containing the attribute's name.
3282 * @value: a string containing the attribute's value.
3283 * @flags: #GFileQueryInfoFlags.
3284 * @cancellable: optional #GCancellable object, %NULL to ignore.
3285 * @error: a #GError, or %NULL
3287 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3288 * If @attribute is of a different type, this operation will fail.
3290 * If @cancellable is not %NULL, then the operation can be cancelled by
3291 * triggering the cancellable object from another thread. If the operation
3292 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3294 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3297 g_file_set_attribute_string (GFile *file,
3298 const char *attribute,
3300 GFileQueryInfoFlags flags,
3301 GCancellable *cancellable,
3304 return g_file_set_attribute (file, attribute,
3305 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3306 flags, cancellable, error);
3310 * g_file_set_attribute_byte_string:
3311 * @file: input #GFile.
3312 * @attribute: a string containing the attribute's name.
3313 * @value: a string containing the attribute's new value.
3314 * @flags: a #GFileQueryInfoFlags.
3315 * @cancellable: optional #GCancellable object, %NULL to ignore.
3316 * @error: a #GError, or %NULL
3318 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3319 * If @attribute is of a different type, this operation will fail,
3322 * If @cancellable is not %NULL, then the operation can be cancelled by
3323 * triggering the cancellable object from another thread. If the operation
3324 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3326 * Returns: %TRUE if the @attribute was successfully set to @value
3327 * in the @file, %FALSE otherwise.
3330 g_file_set_attribute_byte_string (GFile *file,
3331 const char *attribute,
3333 GFileQueryInfoFlags flags,
3334 GCancellable *cancellable,
3337 return g_file_set_attribute (file, attribute,
3338 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3339 flags, cancellable, error);
3343 * g_file_set_attribute_uint32:
3344 * @file: input #GFile.
3345 * @attribute: a string containing the attribute's name.
3346 * @value: a #guint32 containing the attribute's new value.
3347 * @flags: a #GFileQueryInfoFlags.
3348 * @cancellable: optional #GCancellable object, %NULL to ignore.
3349 * @error: a #GError, or %NULL
3351 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3352 * If @attribute is of a different type, this operation will fail.
3354 * If @cancellable is not %NULL, then the operation can be cancelled by
3355 * triggering the cancellable object from another thread. If the operation
3356 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3358 * Returns: %TRUE if the @attribute was successfully set to @value
3359 * in the @file, %FALSE otherwise.
3362 g_file_set_attribute_uint32 (GFile *file,
3363 const char *attribute,
3365 GFileQueryInfoFlags flags,
3366 GCancellable *cancellable,
3369 return g_file_set_attribute (file, attribute,
3370 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3371 flags, cancellable, error);
3375 * g_file_set_attribute_int32:
3376 * @file: input #GFile.
3377 * @attribute: a string containing the attribute's name.
3378 * @value: a #gint32 containing the attribute's new value.
3379 * @flags: a #GFileQueryInfoFlags.
3380 * @cancellable: optional #GCancellable object, %NULL to ignore.
3381 * @error: a #GError, or %NULL
3383 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3384 * If @attribute is of a different type, this operation will fail.
3386 * If @cancellable is not %NULL, then the operation can be cancelled by
3387 * triggering the cancellable object from another thread. If the operation
3388 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3390 * Returns: %TRUE if the @attribute was successfully set to @value
3391 * in the @file, %FALSE otherwise.
3394 g_file_set_attribute_int32 (GFile *file,
3395 const char *attribute,
3397 GFileQueryInfoFlags flags,
3398 GCancellable *cancellable,
3401 return g_file_set_attribute (file, attribute,
3402 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3403 flags, cancellable, error);
3407 * g_file_set_attribute_uint64:
3408 * @file: input #GFile.
3409 * @attribute: a string containing the attribute's name.
3410 * @value: a #guint64 containing the attribute's new value.
3411 * @flags: a #GFileQueryInfoFlags.
3412 * @cancellable: optional #GCancellable object, %NULL to ignore.
3413 * @error: a #GError, or %NULL
3415 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3416 * If @attribute is of a different type, this operation will fail.
3418 * If @cancellable is not %NULL, then the operation can be cancelled by
3419 * triggering the cancellable object from another thread. If the operation
3420 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3422 * Returns: %TRUE if the @attribute was successfully set to @value
3423 * in the @file, %FALSE otherwise.
3426 g_file_set_attribute_uint64 (GFile *file,
3427 const char *attribute,
3429 GFileQueryInfoFlags flags,
3430 GCancellable *cancellable,
3433 return g_file_set_attribute (file, attribute,
3434 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3435 flags, cancellable, error);
3439 * g_file_set_attribute_int64:
3440 * @file: input #GFile.
3441 * @attribute: a string containing the attribute's name.
3442 * @value: a #guint64 containing the attribute's new value.
3443 * @flags: a #GFileQueryInfoFlags.
3444 * @cancellable: optional #GCancellable object, %NULL to ignore.
3445 * @error: a #GError, or %NULL
3447 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3448 * If @attribute is of a different type, this operation will fail.
3450 * If @cancellable is not %NULL, then the operation can be cancelled by
3451 * triggering the cancellable object from another thread. If the operation
3452 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3454 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3457 g_file_set_attribute_int64 (GFile *file,
3458 const char *attribute,
3460 GFileQueryInfoFlags flags,
3461 GCancellable *cancellable,
3464 return g_file_set_attribute (file, attribute,
3465 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3466 flags, cancellable, error);
3470 * g_file_mount_mountable:
3471 * @file: input #GFile.
3472 * @flags: flags affecting the operation
3473 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3474 * @cancellable: optional #GCancellable object, %NULL to ignore.
3475 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3476 * @user_data: the data to pass to callback function
3478 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3479 * Using @mount_operation, you can request callbacks when, for instance,
3480 * passwords are needed during authentication.
3482 * If @cancellable is not %NULL, then the operation can be cancelled by
3483 * triggering the cancellable object from another thread. If the operation
3484 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3486 * When the operation is finished, @callback will be called. You can then call
3487 * g_file_mount_mountable_finish() to get the result of the operation.
3490 g_file_mount_mountable (GFile *file,
3491 GMountMountFlags flags,
3492 GMountOperation *mount_operation,
3493 GCancellable *cancellable,
3494 GAsyncReadyCallback callback,
3499 g_return_if_fail (G_IS_FILE (file));
3501 iface = G_FILE_GET_IFACE (file);
3503 if (iface->mount_mountable == NULL)
3505 g_simple_async_report_error_in_idle (G_OBJECT (file),
3509 G_IO_ERROR_NOT_SUPPORTED,
3510 _("Operation not supported"));
3514 (* iface->mount_mountable) (file,
3523 * g_file_mount_mountable_finish:
3524 * @file: input #GFile.
3525 * @result: a #GAsyncResult.
3526 * @error: a #GError, or %NULL
3528 * Finishes a mount operation. See g_file_mount_mountable() for details.
3530 * Finish an asynchronous mount operation that was started
3531 * with g_file_mount_mountable().
3533 * Returns: a #GFile or %NULL on error.
3536 g_file_mount_mountable_finish (GFile *file,
3537 GAsyncResult *result,
3542 g_return_val_if_fail (G_IS_FILE (file), NULL);
3543 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3545 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3547 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3548 if (g_simple_async_result_propagate_error (simple, error))
3552 iface = G_FILE_GET_IFACE (file);
3553 return (* iface->mount_mountable_finish) (file, result, error);
3557 * g_file_unmount_mountable:
3558 * @file: input #GFile.
3559 * @flags: flags affecting the operation
3560 * @cancellable: optional #GCancellable object, %NULL to ignore.
3561 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3562 * @user_data: the data to pass to callback function
3564 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3566 * If @cancellable is not %NULL, then the operation can be cancelled by
3567 * triggering the cancellable object from another thread. If the operation
3568 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3570 * When the operation is finished, @callback will be called. You can then call
3571 * g_file_unmount_mountable_finish() to get the result of the operation.
3574 g_file_unmount_mountable (GFile *file,
3575 GMountUnmountFlags flags,
3576 GCancellable *cancellable,
3577 GAsyncReadyCallback callback,
3582 g_return_if_fail (G_IS_FILE (file));
3584 iface = G_FILE_GET_IFACE (file);
3586 if (iface->unmount_mountable == NULL)
3588 g_simple_async_report_error_in_idle (G_OBJECT (file),
3592 G_IO_ERROR_NOT_SUPPORTED,
3593 _("Operation not supported"));
3597 (* iface->unmount_mountable) (file,
3605 * g_file_unmount_mountable_finish:
3606 * @file: input #GFile.
3607 * @result: a #GAsyncResult.
3608 * @error: a #GError, or %NULL
3610 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3612 * Finish an asynchronous unmount operation that was started
3613 * with g_file_unmount_mountable().
3615 * Returns: %TRUE if the operation finished successfully. %FALSE
3619 g_file_unmount_mountable_finish (GFile *file,
3620 GAsyncResult *result,
3625 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3626 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3628 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3630 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3631 if (g_simple_async_result_propagate_error (simple, error))
3635 iface = G_FILE_GET_IFACE (file);
3636 return (* iface->unmount_mountable_finish) (file, result, error);
3640 * g_file_eject_mountable:
3641 * @file: input #GFile.
3642 * @flags: flags affecting the operation
3643 * @cancellable: optional #GCancellable object, %NULL to ignore.
3644 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3645 * @user_data: the data to pass to callback function
3647 * Starts an asynchronous eject on a mountable.
3648 * When this operation has completed, @callback will be called with
3649 * @user_user data, and the operation can be finalized with
3650 * g_file_eject_mountable_finish().
3652 * If @cancellable is not %NULL, then the operation can be cancelled by
3653 * triggering the cancellable object from another thread. If the operation
3654 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3657 g_file_eject_mountable (GFile *file,
3658 GMountUnmountFlags flags,
3659 GCancellable *cancellable,
3660 GAsyncReadyCallback callback,
3665 g_return_if_fail (G_IS_FILE (file));
3667 iface = G_FILE_GET_IFACE (file);
3669 if (iface->eject_mountable == NULL)
3671 g_simple_async_report_error_in_idle (G_OBJECT (file),
3675 G_IO_ERROR_NOT_SUPPORTED,
3676 _("Operation not supported"));
3680 (* iface->eject_mountable) (file,
3688 * g_file_eject_mountable_finish:
3689 * @file: input #GFile.
3690 * @result: a #GAsyncResult.
3691 * @error: a #GError, or %NULL
3693 * Finishes an asynchronous eject operation started by
3694 * g_file_eject_mountable().
3696 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3700 g_file_eject_mountable_finish (GFile *file,
3701 GAsyncResult *result,
3706 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3707 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3709 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3711 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3712 if (g_simple_async_result_propagate_error (simple, error))
3716 iface = G_FILE_GET_IFACE (file);
3717 return (* iface->eject_mountable_finish) (file, result, error);
3721 * g_file_monitor_directory:
3722 * @file: input #GFile.
3723 * @flags: a set of #GFileMonitorFlags.
3724 * @cancellable: optional #GCancellable object, %NULL to ignore.
3725 * @error: a #GError, or %NULL.
3727 * Obtains a directory monitor for the given file.
3728 * This may fail if directory monitoring is not supported.
3730 * If @cancellable is not %NULL, then the operation can be cancelled by
3731 * triggering the cancellable object from another thread. If the operation
3732 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3734 * Returns: a #GFileMonitor for the given @file,
3735 * or %NULL on error.
3738 g_file_monitor_directory (GFile *file,
3739 GFileMonitorFlags flags,
3740 GCancellable *cancellable,
3745 g_return_val_if_fail (G_IS_FILE (file), NULL);
3747 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3750 iface = G_FILE_GET_IFACE (file);
3752 if (iface->monitor_dir == NULL)
3754 g_set_error (error, G_IO_ERROR,
3755 G_IO_ERROR_NOT_SUPPORTED,
3756 _("Operation not supported"));
3760 return (* iface->monitor_dir) (file, flags, cancellable, error);
3764 * g_file_monitor_file:
3765 * @file: input #GFile.
3766 * @flags: a set of #GFileMonitorFlags.
3767 * @cancellable: optional #GCancellable object, %NULL to ignore.
3768 * @error: a #GError, or %NULL.
3770 * Obtains a file monitor for the given file. If no file notification
3771 * mechanism exists, then regular polling of the file is used.
3773 * If @cancellable is not %NULL, then the operation can be cancelled by
3774 * triggering the cancellable object from another thread. If the operation
3775 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3777 * Returns: a #GFileMonitor for the given @file.
3780 g_file_monitor_file (GFile *file,
3781 GFileMonitorFlags flags,
3782 GCancellable *cancellable,
3786 GFileMonitor *monitor;
3788 g_return_val_if_fail (G_IS_FILE (file), NULL);
3790 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3793 iface = G_FILE_GET_IFACE (file);
3797 if (iface->monitor_file)
3798 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3800 /* Fallback to polling */
3801 if (monitor == NULL)
3802 monitor = _g_poll_file_monitor_new (file);
3807 /********************************************
3808 * Default implementation of async ops *
3809 ********************************************/
3813 GFileQueryInfoFlags flags;
3815 } QueryInfoAsyncData;
3818 query_info_data_free (QueryInfoAsyncData *data)
3821 g_object_unref (data->info);
3822 g_free (data->attributes);
3827 query_info_async_thread (GSimpleAsyncResult *res,
3829 GCancellable *cancellable)
3831 GError *error = NULL;
3832 QueryInfoAsyncData *data;
3835 data = g_simple_async_result_get_op_res_gpointer (res);
3837 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3841 g_simple_async_result_set_from_error (res, error);
3842 g_error_free (error);
3849 g_file_real_query_info_async (GFile *file,
3850 const char *attributes,
3851 GFileQueryInfoFlags flags,
3853 GCancellable *cancellable,
3854 GAsyncReadyCallback callback,
3857 GSimpleAsyncResult *res;
3858 QueryInfoAsyncData *data;
3860 data = g_new0 (QueryInfoAsyncData, 1);
3861 data->attributes = g_strdup (attributes);
3862 data->flags = flags;
3864 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3865 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3867 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3868 g_object_unref (res);
3872 g_file_real_query_info_finish (GFile *file,
3876 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3877 QueryInfoAsyncData *data;
3879 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3881 data = g_simple_async_result_get_op_res_gpointer (simple);
3883 return g_object_ref (data->info);
3891 } QueryFilesystemInfoAsyncData;
3894 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
3897 g_object_unref (data->info);
3898 g_free (data->attributes);
3903 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
3905 GCancellable *cancellable)
3907 GError *error = NULL;
3908 QueryFilesystemInfoAsyncData *data;
3911 data = g_simple_async_result_get_op_res_gpointer (res);
3913 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
3917 g_simple_async_result_set_from_error (res, error);
3918 g_error_free (error);
3925 g_file_real_query_filesystem_info_async (GFile *file,
3926 const char *attributes,
3928 GCancellable *cancellable,
3929 GAsyncReadyCallback callback,
3932 GSimpleAsyncResult *res;
3933 QueryFilesystemInfoAsyncData *data;
3935 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
3936 data->attributes = g_strdup (attributes);
3938 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
3939 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
3941 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
3942 g_object_unref (res);
3946 g_file_real_query_filesystem_info_finish (GFile *file,
3950 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3951 QueryFilesystemInfoAsyncData *data;
3953 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
3955 data = g_simple_async_result_get_op_res_gpointer (simple);
3957 return g_object_ref (data->info);
3964 GFileQueryInfoFlags flags;
3965 GFileEnumerator *enumerator;
3966 } EnumerateChildrenAsyncData;
3969 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3971 if (data->enumerator)
3972 g_object_unref (data->enumerator);
3973 g_free (data->attributes);
3978 enumerate_children_async_thread (GSimpleAsyncResult *res,
3980 GCancellable *cancellable)
3982 GError *error = NULL;
3983 EnumerateChildrenAsyncData *data;
3984 GFileEnumerator *enumerator;
3986 data = g_simple_async_result_get_op_res_gpointer (res);
3988 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3990 if (enumerator == NULL)
3992 g_simple_async_result_set_from_error (res, error);
3993 g_error_free (error);
3996 data->enumerator = enumerator;
4000 g_file_real_enumerate_children_async (GFile *file,
4001 const char *attributes,
4002 GFileQueryInfoFlags flags,
4004 GCancellable *cancellable,
4005 GAsyncReadyCallback callback,
4008 GSimpleAsyncResult *res;
4009 EnumerateChildrenAsyncData *data;
4011 data = g_new0 (EnumerateChildrenAsyncData, 1);
4012 data->attributes = g_strdup (attributes);
4013 data->flags = flags;
4015 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4016 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4018 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4019 g_object_unref (res);
4022 static GFileEnumerator *
4023 g_file_real_enumerate_children_finish (GFile *file,
4027 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4028 EnumerateChildrenAsyncData *data;
4030 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4032 data = g_simple_async_result_get_op_res_gpointer (simple);
4033 if (data->enumerator)
4034 return g_object_ref (data->enumerator);
4040 open_read_async_thread (GSimpleAsyncResult *res,
4042 GCancellable *cancellable)
4045 GFileInputStream *stream;
4046 GError *error = NULL;
4048 iface = G_FILE_GET_IFACE (object);
4050 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4054 g_simple_async_result_set_from_error (res, error);
4055 g_error_free (error);
4058 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4062 g_file_real_read_async (GFile *file,
4064 GCancellable *cancellable,
4065 GAsyncReadyCallback callback,
4068 GSimpleAsyncResult *res;
4070 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4072 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4073 g_object_unref (res);
4076 static GFileInputStream *
4077 g_file_real_read_finish (GFile *file,
4081 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4084 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4086 op = g_simple_async_result_get_op_res_gpointer (simple);
4088 return g_object_ref (op);
4094 append_to_async_thread (GSimpleAsyncResult *res,
4096 GCancellable *cancellable)
4099 GFileCreateFlags *data;
4100 GFileOutputStream *stream;
4101 GError *error = NULL;
4103 iface = G_FILE_GET_IFACE (object);
4105 data = g_simple_async_result_get_op_res_gpointer (res);
4107 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4111 g_simple_async_result_set_from_error (res, error);
4112 g_error_free (error);
4115 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4119 g_file_real_append_to_async (GFile *file,
4120 GFileCreateFlags flags,
4122 GCancellable *cancellable,
4123 GAsyncReadyCallback callback,
4126 GFileCreateFlags *data;
4127 GSimpleAsyncResult *res;
4129 data = g_new0 (GFileCreateFlags, 1);
4132 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4133 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4135 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4136 g_object_unref (res);
4139 static GFileOutputStream *
4140 g_file_real_append_to_finish (GFile *file,
4144 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4147 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4149 op = g_simple_async_result_get_op_res_gpointer (simple);
4151 return g_object_ref (op);
4157 create_async_thread (GSimpleAsyncResult *res,
4159 GCancellable *cancellable)
4162 GFileCreateFlags *data;
4163 GFileOutputStream *stream;
4164 GError *error = NULL;
4166 iface = G_FILE_GET_IFACE (object);
4168 data = g_simple_async_result_get_op_res_gpointer (res);
4170 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4174 g_simple_async_result_set_from_error (res, error);
4175 g_error_free (error);
4178 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4182 g_file_real_create_async (GFile *file,
4183 GFileCreateFlags flags,
4185 GCancellable *cancellable,
4186 GAsyncReadyCallback callback,
4189 GFileCreateFlags *data;
4190 GSimpleAsyncResult *res;
4192 data = g_new0 (GFileCreateFlags, 1);
4195 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4196 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4198 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4199 g_object_unref (res);
4202 static GFileOutputStream *
4203 g_file_real_create_finish (GFile *file,
4207 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4210 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4212 op = g_simple_async_result_get_op_res_gpointer (simple);
4214 return g_object_ref (op);
4220 GFileOutputStream *stream;
4222 gboolean make_backup;
4223 GFileCreateFlags flags;
4227 replace_async_data_free (ReplaceAsyncData *data)
4230 g_object_unref (data->stream);
4231 g_free (data->etag);
4236 replace_async_thread (GSimpleAsyncResult *res,
4238 GCancellable *cancellable)
4241 GFileOutputStream *stream;
4242 GError *error = NULL;
4243 ReplaceAsyncData *data;
4245 iface = G_FILE_GET_IFACE (object);
4247 data = g_simple_async_result_get_op_res_gpointer (res);
4249 stream = iface->replace (G_FILE (object),
4258 g_simple_async_result_set_from_error (res, error);
4259 g_error_free (error);
4262 data->stream = stream;
4266 g_file_real_replace_async (GFile *file,
4268 gboolean make_backup,
4269 GFileCreateFlags flags,
4271 GCancellable *cancellable,
4272 GAsyncReadyCallback callback,
4275 GSimpleAsyncResult *res;
4276 ReplaceAsyncData *data;
4278 data = g_new0 (ReplaceAsyncData, 1);
4279 data->etag = g_strdup (etag);
4280 data->make_backup = make_backup;
4281 data->flags = flags;
4283 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4284 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4286 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4287 g_object_unref (res);
4290 static GFileOutputStream *
4291 g_file_real_replace_finish (GFile *file,
4295 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4296 ReplaceAsyncData *data;
4298 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4300 data = g_simple_async_result_get_op_res_gpointer (simple);
4302 return g_object_ref (data->stream);
4310 } SetDisplayNameAsyncData;
4313 set_display_name_data_free (SetDisplayNameAsyncData *data)
4315 g_free (data->name);
4317 g_object_unref (data->file);
4322 set_display_name_async_thread (GSimpleAsyncResult *res,
4324 GCancellable *cancellable)
4326 GError *error = NULL;
4327 SetDisplayNameAsyncData *data;
4330 data = g_simple_async_result_get_op_res_gpointer (res);
4332 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4336 g_simple_async_result_set_from_error (res, error);
4337 g_error_free (error);
4344 g_file_real_set_display_name_async (GFile *file,
4345 const char *display_name,
4347 GCancellable *cancellable,
4348 GAsyncReadyCallback callback,
4351 GSimpleAsyncResult *res;
4352 SetDisplayNameAsyncData *data;
4354 data = g_new0 (SetDisplayNameAsyncData, 1);
4355 data->name = g_strdup (display_name);
4357 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4358 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4360 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4361 g_object_unref (res);
4365 g_file_real_set_display_name_finish (GFile *file,
4369 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4370 SetDisplayNameAsyncData *data;
4372 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4374 data = g_simple_async_result_get_op_res_gpointer (simple);
4376 return g_object_ref (data->file);
4382 GFileQueryInfoFlags flags;
4389 set_info_data_free (SetInfoAsyncData *data)
4392 g_object_unref (data->info);
4394 g_error_free (data->error);
4399 set_info_async_thread (GSimpleAsyncResult *res,
4401 GCancellable *cancellable)
4403 SetInfoAsyncData *data;
4405 data = g_simple_async_result_get_op_res_gpointer (res);
4408 data->res = g_file_set_attributes_from_info (G_FILE (object),
4416 g_file_real_set_attributes_async (GFile *file,
4418 GFileQueryInfoFlags flags,
4420 GCancellable *cancellable,
4421 GAsyncReadyCallback callback,
4424 GSimpleAsyncResult *res;
4425 SetInfoAsyncData *data;
4427 data = g_new0 (SetInfoAsyncData, 1);
4428 data->info = g_file_info_dup (info);
4429 data->flags = flags;
4431 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4432 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4434 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4435 g_object_unref (res);
4439 g_file_real_set_attributes_finish (GFile *file,
4444 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4445 SetInfoAsyncData *data;
4447 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4449 data = g_simple_async_result_get_op_res_gpointer (simple);
4452 *info = g_object_ref (data->info);
4454 if (error != NULL && data->error)
4455 *error = g_error_copy (data->error);
4461 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4463 GCancellable *cancellable)
4465 GError *error = NULL;
4468 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4472 g_simple_async_result_set_from_error (res, error);
4473 g_error_free (error);
4476 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4480 g_file_real_find_enclosing_mount_async (GFile *file,
4482 GCancellable *cancellable,
4483 GAsyncReadyCallback callback,
4486 GSimpleAsyncResult *res;
4488 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4490 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4491 g_object_unref (res);
4495 g_file_real_find_enclosing_mount_finish (GFile *file,
4499 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4502 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4504 mount = g_simple_async_result_get_op_res_gpointer (simple);
4505 return g_object_ref (mount);
4512 GFileCopyFlags flags;
4513 GFileProgressCallback progress_cb;
4514 gpointer progress_cb_data;
4515 GIOSchedulerJob *job;
4519 copy_async_data_free (CopyAsyncData *data)
4521 g_object_unref (data->source);
4522 g_object_unref (data->destination);
4527 CopyAsyncData *data;
4528 goffset current_num_bytes;
4529 goffset total_num_bytes;
4533 copy_async_progress_in_main (gpointer user_data)
4535 ProgressData *progress = user_data;
4536 CopyAsyncData *data = progress->data;
4538 data->progress_cb (progress->current_num_bytes,
4539 progress->total_num_bytes,
4540 data->progress_cb_data);
4546 mainloop_barrier (gpointer user_data)
4548 /* Does nothing, but ensures all queued idles before
4555 copy_async_progress_callback (goffset current_num_bytes,
4556 goffset total_num_bytes,
4559 CopyAsyncData *data = user_data;
4560 ProgressData *progress;
4562 progress = g_new (ProgressData, 1);
4563 progress->data = data;
4564 progress->current_num_bytes = current_num_bytes;
4565 progress->total_num_bytes = total_num_bytes;
4567 g_io_scheduler_job_send_to_mainloop_async (data->job,
4568 copy_async_progress_in_main,
4574 copy_async_thread (GIOSchedulerJob *job,
4575 GCancellable *cancellable,
4578 GSimpleAsyncResult *res;
4579 CopyAsyncData *data;
4584 data = g_simple_async_result_get_op_res_gpointer (res);
4588 result = g_file_copy (data->source,
4592 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4596 /* Ensure all progress callbacks are done running in main thread */
4597 if (data->progress_cb != NULL)
4598 g_io_scheduler_job_send_to_mainloop (job,
4604 g_simple_async_result_set_from_error (res, error);
4605 g_error_free (error);
4608 g_simple_async_result_complete_in_idle (res);
4614 g_file_real_copy_async (GFile *source,
4616 GFileCopyFlags flags,
4618 GCancellable *cancellable,
4619 GFileProgressCallback progress_callback,
4620 gpointer progress_callback_data,
4621 GAsyncReadyCallback callback,
4624 GSimpleAsyncResult *res;
4625 CopyAsyncData *data;
4627 data = g_new0 (CopyAsyncData, 1);
4628 data->source = g_object_ref (source);
4629 data->destination = g_object_ref (destination);
4630 data->flags = flags;
4631 data->progress_cb = progress_callback;
4632 data->progress_cb_data = progress_callback_data;
4634 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4635 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4637 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4641 g_file_real_copy_finish (GFile *file,
4645 /* Error handled in g_file_copy_finish() */
4650 /********************************************
4651 * Default VFS operations *
4652 ********************************************/
4655 * g_file_new_for_path:
4656 * @path: a string containing a relative or absolute path.
4658 * Constructs a #GFile for a given path. This operation never
4659 * fails, but the returned object might not support any I/O
4660 * operation if @path is malformed.
4662 * Returns: a new #GFile for the given @path.
4665 g_file_new_for_path (const char *path)
4667 g_return_val_if_fail (path != NULL, NULL);
4669 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4673 * g_file_new_for_uri:
4674 * @uri: a string containing a URI.
4676 * Constructs a #GFile for a given URI. This operation never
4677 * fails, but the returned object might not support any I/O
4678 * operation if @uri is malformed or if the uri type is
4681 * Returns: a #GFile for the given @uri.
4684 g_file_new_for_uri (const char *uri)
4686 g_return_val_if_fail (uri != NULL, NULL);
4688 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4692 * g_file_parse_name:
4693 * @parse_name: a file name or path to be parsed.
4695 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4696 * This operation never fails, but the returned object might not support any I/O
4697 * operation if the @parse_name cannot be parsed.
4699 * Returns: a new #GFile.
4702 g_file_parse_name (const char *parse_name)
4704 g_return_val_if_fail (parse_name != NULL, NULL);
4706 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4710 is_valid_scheme_character (char c)
4712 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4716 has_valid_scheme (const char *uri)
4722 if (!is_valid_scheme_character (*p))
4727 } while (is_valid_scheme_character (*p));
4733 * g_file_new_for_commandline_arg:
4734 * @arg: a command line string.
4736 * Creates a #GFile with the given argument from the command line. The value of
4737 * @arg can be either a URI, an absolute path or a relative path resolved
4738 * relative to the current working directory.
4739 * This operation never fails, but the returned object might not support any
4740 * I/O operation if @arg points to a malformed path.
4742 * Returns: a new #GFile.
4745 g_file_new_for_commandline_arg (const char *arg)
4751 g_return_val_if_fail (arg != NULL, NULL);
4753 if (g_path_is_absolute (arg))
4754 return g_file_new_for_path (arg);
4756 if (has_valid_scheme (arg))
4757 return g_file_new_for_uri (arg);
4759 current_dir = g_get_current_dir ();
4760 filename = g_build_filename (current_dir, arg, NULL);
4761 g_free (current_dir);
4763 file = g_file_new_for_path (filename);
4770 * g_file_mount_enclosing_volume:
4771 * @location: input #GFile.
4772 * @flags: flags affecting the operation
4773 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4774 * @cancellable: optional #GCancellable object, %NULL to ignore.
4775 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4776 * @user_data: the data to pass to callback function
4778 * Starts a @mount_operation, mounting the volume that contains the file @location.
4780 * When this operation has completed, @callback will be called with
4781 * @user_user data, and the operation can be finalized with
4782 * g_file_mount_enclosing_volume_finish().
4784 * If @cancellable is not %NULL, then the operation can be cancelled by
4785 * triggering the cancellable object from another thread. If the operation
4786 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4789 g_file_mount_enclosing_volume (GFile *location,
4790 GMountMountFlags flags,
4791 GMountOperation *mount_operation,
4792 GCancellable *cancellable,
4793 GAsyncReadyCallback callback,
4798 g_return_if_fail (G_IS_FILE (location));
4800 iface = G_FILE_GET_IFACE (location);
4802 if (iface->mount_enclosing_volume == NULL)
4804 g_simple_async_report_error_in_idle (G_OBJECT (location),
4805 callback, user_data,
4806 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4807 _("volume doesn't implement mount"));
4812 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4817 * g_file_mount_enclosing_volume_finish:
4818 * @location: input #GFile.
4819 * @result: a #GAsyncResult.
4820 * @error: a #GError, or %NULL
4822 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4824 * Returns: %TRUE if successful. If an error
4825 * has occurred, this function will return %FALSE and set @error
4826 * appropriately if present.
4829 g_file_mount_enclosing_volume_finish (GFile *location,
4830 GAsyncResult *result,
4835 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4836 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4838 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4840 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4841 if (g_simple_async_result_propagate_error (simple, error))
4845 iface = G_FILE_GET_IFACE (location);
4847 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4850 /********************************************
4851 * Utility functions *
4852 ********************************************/
4855 * g_file_query_default_handler:
4856 * @file: a #GFile to open.
4857 * @cancellable: optional #GCancellable object, %NULL to ignore.
4858 * @error: a #GError, or %NULL
4860 * Returns the #GAppInfo that is registered as the default
4861 * application to handle the file specified by @file.
4863 * If @cancellable is not %NULL, then the operation can be cancelled by
4864 * triggering the cancellable object from another thread. If the operation
4865 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4867 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4868 * When you are done with it, release it with g_object_unref()
4871 g_file_query_default_handler (GFile *file,
4872 GCancellable *cancellable,
4876 const char *content_type;
4881 uri_scheme = g_file_get_uri_scheme (file);
4882 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4883 g_free (uri_scheme);
4885 if (appinfo != NULL)
4888 info = g_file_query_info (file,
4889 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4898 content_type = g_file_info_get_content_type (info);
4901 /* Don't use is_native(), as we want to support fuse paths if availible */
4902 path = g_file_get_path (file);
4903 appinfo = g_app_info_get_default_for_type (content_type,
4908 g_object_unref (info);
4910 if (appinfo != NULL)
4913 g_set_error (error, G_IO_ERROR,
4914 G_IO_ERROR_NOT_SUPPORTED,
4915 _("No application is registered as handling this file"));
4921 #define GET_CONTENT_BLOCK_SIZE 8192
4924 * g_file_load_contents:
4925 * @file: input #GFile.
4926 * @cancellable: optional #GCancellable object, %NULL to ignore.
4927 * @contents: a location to place the contents of the file.
4928 * @length: a location to place the length of the contents of the file.
4929 * @etag_out: a location to place the current entity tag for the file.
4930 * @error: a #GError, or %NULL
4932 * Loads the content of the file into memory, returning the size of
4933 * the data. The data is always zero terminated, but this is not
4934 * included in the resultant @length.
4936 * If @cancellable is not %NULL, then the operation can be cancelled by
4937 * triggering the cancellable object from another thread. If the operation
4938 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4940 * Returns: %TRUE if the @file's contents were successfully loaded.
4941 * %FALSE if there were errors..
4944 g_file_load_contents (GFile *file,
4945 GCancellable *cancellable,
4951 GFileInputStream *in;
4952 GByteArray *content;
4957 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4958 g_return_val_if_fail (contents != NULL, FALSE);
4960 in = g_file_read (file, cancellable, error);
4964 content = g_byte_array_new ();
4967 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4968 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4969 content->data + pos,
4970 GET_CONTENT_BLOCK_SIZE,
4971 cancellable, error)) > 0)
4974 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4981 info = g_file_input_stream_query_info (in,
4982 G_FILE_ATTRIBUTE_ETAG_VALUE,
4987 *etag_out = g_strdup (g_file_info_get_etag (info));
4988 g_object_unref (info);
4992 /* Ignore errors on close */
4993 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4994 g_object_unref (in);
4998 /* error is set already */
4999 g_byte_array_free (content, TRUE);
5006 /* Zero terminate (we got an extra byte allocated for this */
5007 content->data[pos] = 0;
5009 *contents = (char *)g_byte_array_free (content, FALSE);
5017 GCancellable *cancellable;
5018 GFileReadMoreCallback read_more_callback;
5019 GAsyncReadyCallback callback;
5021 GByteArray *content;
5028 load_contents_data_free (LoadContentsData *data)
5031 g_error_free (data->error);
5032 if (data->cancellable)
5033 g_object_unref (data->cancellable);
5035 g_byte_array_free (data->content, TRUE);
5036 g_free (data->etag);
5037 g_object_unref (data->file);
5042 load_contents_close_callback (GObject *obj,
5043 GAsyncResult *close_res,
5046 GInputStream *stream = G_INPUT_STREAM (obj);
5047 LoadContentsData *data = user_data;
5048 GSimpleAsyncResult *res;
5050 /* Ignore errors here, we're only reading anyway */
5051 g_input_stream_close_finish (stream, close_res, NULL);
5052 g_object_unref (stream);
5054 res = g_simple_async_result_new (G_OBJECT (data->file),
5057 g_file_load_contents_async);
5058 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5059 g_simple_async_result_complete (res);
5060 g_object_unref (res);
5064 load_contents_fstat_callback (GObject *obj,
5065 GAsyncResult *stat_res,
5068 GInputStream *stream = G_INPUT_STREAM (obj);
5069 LoadContentsData *data = user_data;
5072 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5076 data->etag = g_strdup (g_file_info_get_etag (info));
5077 g_object_unref (info);
5080 g_input_stream_close_async (stream, 0,
5082 load_contents_close_callback, data);
5086 load_contents_read_callback (GObject *obj,
5087 GAsyncResult *read_res,
5090 GInputStream *stream = G_INPUT_STREAM (obj);
5091 LoadContentsData *data = user_data;
5092 GError *error = NULL;
5095 read_size = g_input_stream_read_finish (stream, read_res, &error);
5099 /* Error or EOF, close the file */
5100 data->error = error;
5101 g_input_stream_close_async (stream, 0,
5103 load_contents_close_callback, data);
5105 else if (read_size == 0)
5107 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5108 G_FILE_ATTRIBUTE_ETAG_VALUE,
5111 load_contents_fstat_callback,
5114 else if (read_size > 0)
5116 data->pos += read_size;
5118 g_byte_array_set_size (data->content,
5119 data->pos + GET_CONTENT_BLOCK_SIZE);
5122 if (data->read_more_callback &&
5123 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5124 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5125 G_FILE_ATTRIBUTE_ETAG_VALUE,
5128 load_contents_fstat_callback,
5131 g_input_stream_read_async (stream,
5132 data->content->data + data->pos,
5133 GET_CONTENT_BLOCK_SIZE,
5136 load_contents_read_callback,
5142 load_contents_open_callback (GObject *obj,
5143 GAsyncResult *open_res,
5146 GFile *file = G_FILE (obj);
5147 GFileInputStream *stream;
5148 LoadContentsData *data = user_data;
5149 GError *error = NULL;
5150 GSimpleAsyncResult *res;
5152 stream = g_file_read_finish (file, open_res, &error);
5156 g_byte_array_set_size (data->content,
5157 data->pos + GET_CONTENT_BLOCK_SIZE);
5158 g_input_stream_read_async (G_INPUT_STREAM (stream),
5159 data->content->data + data->pos,
5160 GET_CONTENT_BLOCK_SIZE,
5163 load_contents_read_callback,
5169 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5173 g_simple_async_result_complete (res);
5174 g_error_free (error);
5175 load_contents_data_free (data);
5176 g_object_unref (res);
5181 * g_file_load_partial_contents_async:
5182 * @file: input #GFile.
5183 * @cancellable: optional #GCancellable object, %NULL to ignore.
5184 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5185 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5186 * @user_data: the data to pass to the callback functions.
5188 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5189 * used to stop reading from the file when appropriate, else this function
5190 * will behave exactly as g_file_load_contents_async(). This operation
5191 * can be finished by g_file_load_partial_contents_finish().
5193 * Users of this function should be aware that @user_data is passed to
5194 * both the @read_more_callback and the @callback.
5196 * If @cancellable is not %NULL, then the operation can be cancelled by
5197 * triggering the cancellable object from another thread. If the operation
5198 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5201 g_file_load_partial_contents_async (GFile *file,
5202 GCancellable *cancellable,
5203 GFileReadMoreCallback read_more_callback,
5204 GAsyncReadyCallback callback,
5207 LoadContentsData *data;
5209 g_return_if_fail (G_IS_FILE (file));
5211 data = g_new0 (LoadContentsData, 1);
5214 data->cancellable = g_object_ref (cancellable);
5215 data->read_more_callback = read_more_callback;
5216 data->callback = callback;
5217 data->user_data = user_data;
5218 data->content = g_byte_array_new ();
5219 data->file = g_object_ref (file);
5221 g_file_read_async (file,
5224 load_contents_open_callback,
5229 * g_file_load_partial_contents_finish:
5230 * @file: input #GFile.
5231 * @res: a #GAsyncResult.
5232 * @contents: a location to place the contents of the file.
5233 * @length: a location to place the length of the contents of the file.
5234 * @etag_out: a location to place the current entity tag for the file.
5235 * @error: a #GError, or %NULL
5237 * Finishes an asynchronous partial load operation that was started
5238 * with g_file_load_partial_contents_async().
5240 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5241 * present, it will be set appropriately.
5244 g_file_load_partial_contents_finish (GFile *file,
5251 GSimpleAsyncResult *simple;
5252 LoadContentsData *data;
5254 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5255 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5256 g_return_val_if_fail (contents != NULL, FALSE);
5258 simple = G_SIMPLE_ASYNC_RESULT (res);
5260 if (g_simple_async_result_propagate_error (simple, error))
5263 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5265 data = g_simple_async_result_get_op_res_gpointer (simple);
5269 g_propagate_error (error, data->error);
5278 *length = data->pos;
5282 *etag_out = data->etag;
5286 /* Zero terminate */
5287 g_byte_array_set_size (data->content, data->pos + 1);
5288 data->content->data[data->pos] = 0;
5290 *contents = (char *)g_byte_array_free (data->content, FALSE);
5291 data->content = NULL;
5297 * g_file_load_contents_async:
5298 * @file: input #GFile.
5299 * @cancellable: optional #GCancellable object, %NULL to ignore.
5300 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5301 * @user_data: the data to pass to callback function
5303 * Starts an asynchronous load of the @file's contents.
5305 * For more details, see g_file_load_contents() which is
5306 * the synchronous version of this call.
5308 * When the load operation has completed, @callback will be called
5309 * with @user data. To finish the operation, call
5310 * g_file_load_contents_finish() with the #GAsyncResult returned by
5313 * If @cancellable is not %NULL, then the operation can be cancelled by
5314 * triggering the cancellable object from another thread. If the operation
5315 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5318 g_file_load_contents_async (GFile *file,
5319 GCancellable *cancellable,
5320 GAsyncReadyCallback callback,
5323 g_file_load_partial_contents_async (file,
5326 callback, user_data);
5330 * g_file_load_contents_finish:
5331 * @file: input #GFile.
5332 * @res: a #GAsyncResult.
5333 * @contents: a location to place the contents of the file.
5334 * @length: a location to place the length of the contents of the file.
5335 * @etag_out: a location to place the current entity tag for the file.
5336 * @error: a #GError, or %NULL
5338 * Finishes an asynchronous load of the @file's contents.
5339 * The contents are placed in @contents, and @length is set to the
5340 * size of the @contents string. If @etag_out is present, it will be
5341 * set to the new entity tag for the @file.
5343 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5344 * present, it will be set appropriately.
5347 g_file_load_contents_finish (GFile *file,
5354 return g_file_load_partial_contents_finish (file,
5363 * g_file_replace_contents:
5364 * @file: input #GFile.
5365 * @contents: a string containing the new contents for @file.
5366 * @length: the length of @contents in bytes.
5367 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5369 * @make_backup: %TRUE if a backup should be created.
5370 * @flags: a set of #GFileCreateFlags.
5371 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5372 * for the document. This should be freed with g_free() when no longer
5374 * @cancellable: optional #GCancellable object, %NULL to ignore.
5375 * @error: a #GError, or %NULL
5377 * Replaces the contents of @file with @contents of @length bytes.
5379 * If @etag is specified (not %NULL) any existing file must have that etag, or
5380 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5382 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5384 * If @cancellable is not %NULL, then the operation can be cancelled by
5385 * triggering the cancellable object from another thread. If the operation
5386 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5388 * The returned @new_etag can be used to verify that the file hasn't changed the
5389 * next time it is saved over.
5391 * Returns: %TRUE if successful. If an error
5392 * has occurred, this function will return %FALSE and set @error
5393 * appropriately if present.
5396 g_file_replace_contents (GFile *file,
5397 const char *contents,
5400 gboolean make_backup,
5401 GFileCreateFlags flags,
5403 GCancellable *cancellable,
5406 GFileOutputStream *out;
5407 gsize pos, remainder;
5410 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5411 g_return_val_if_fail (contents != NULL, FALSE);
5413 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5419 while (remainder > 0 &&
5420 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5422 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5430 if (remainder > 0 && res < 0)
5432 /* Ignore errors on close */
5433 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5435 /* error is set already */
5439 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5443 *new_etag = g_file_output_stream_get_etag (out);
5451 GCancellable *cancellable;
5452 GAsyncReadyCallback callback;
5454 const char *content;
5458 } ReplaceContentsData;
5461 replace_contents_data_free (ReplaceContentsData *data)
5464 g_error_free (data->error);
5465 if (data->cancellable)
5466 g_object_unref (data->cancellable);
5467 g_object_unref (data->file);
5468 g_free (data->etag);
5473 replace_contents_close_callback (GObject *obj,
5474 GAsyncResult *close_res,
5477 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5478 ReplaceContentsData *data = user_data;
5479 GSimpleAsyncResult *res;
5481 /* Ignore errors here, we're only reading anyway */
5482 g_output_stream_close_finish (stream, close_res, NULL);
5483 g_object_unref (stream);
5485 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5487 res = g_simple_async_result_new (G_OBJECT (data->file),
5490 g_file_replace_contents_async);
5491 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5492 g_simple_async_result_complete (res);
5493 g_object_unref (res);
5497 replace_contents_write_callback (GObject *obj,
5498 GAsyncResult *read_res,
5501 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5502 ReplaceContentsData *data = user_data;
5503 GError *error = NULL;
5506 write_size = g_output_stream_write_finish (stream, read_res, &error);
5508 if (write_size <= 0)
5510 /* Error or EOF, close the file */
5512 data->error = error;
5513 g_output_stream_close_async (stream, 0,
5515 replace_contents_close_callback, data);
5517 else if (write_size > 0)
5519 data->pos += write_size;
5521 if (data->pos >= data->length)
5522 g_output_stream_close_async (stream, 0,
5524 replace_contents_close_callback, data);
5526 g_output_stream_write_async (stream,
5527 data->content + data->pos,
5528 data->length - data->pos,
5531 replace_contents_write_callback,
5537 replace_contents_open_callback (GObject *obj,
5538 GAsyncResult *open_res,
5541 GFile *file = G_FILE (obj);
5542 GFileOutputStream *stream;
5543 ReplaceContentsData *data = user_data;
5544 GError *error = NULL;
5545 GSimpleAsyncResult *res;
5547 stream = g_file_replace_finish (file, open_res, &error);
5551 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5552 data->content + data->pos,
5553 data->length - data->pos,
5556 replace_contents_write_callback,
5562 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5566 g_simple_async_result_complete (res);
5567 g_error_free (error);
5568 replace_contents_data_free (data);
5569 g_object_unref (res);
5574 * g_file_replace_contents_async:
5575 * @file: input #GFile.
5576 * @contents: string of contents to replace the file with.
5577 * @length: the length of @contents in bytes.
5578 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5579 * @make_backup: %TRUE if a backup should be created.
5580 * @flags: a set of #GFileCreateFlags.
5581 * @cancellable: optional #GCancellable object, %NULL to ignore.
5582 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5583 * @user_data: the data to pass to callback function
5585 * Starts an asynchronous replacement of @file with the given
5586 * @contents of @length bytes. @etag will replace the document's
5587 * current entity tag.
5589 * When this operation has completed, @callback will be called with
5590 * @user_user data, and the operation can be finalized with
5591 * g_file_replace_contents_finish().
5593 * If @cancellable is not %NULL, then the operation can be cancelled by
5594 * triggering the cancellable object from another thread. If the operation
5595 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5597 * If @make_backup is %TRUE, this function will attempt to
5598 * make a backup of @file.
5601 g_file_replace_contents_async (GFile *file,
5602 const char *contents,
5605 gboolean make_backup,
5606 GFileCreateFlags flags,
5607 GCancellable *cancellable,
5608 GAsyncReadyCallback callback,
5611 ReplaceContentsData *data;
5613 g_return_if_fail (G_IS_FILE (file));
5614 g_return_if_fail (contents != NULL);
5616 data = g_new0 (ReplaceContentsData, 1);
5619 data->cancellable = g_object_ref (cancellable);
5620 data->callback = callback;
5621 data->user_data = user_data;
5622 data->content = contents;
5623 data->length = length;
5625 data->file = g_object_ref (file);
5627 g_file_replace_async (file,
5633 replace_contents_open_callback,
5638 * g_file_replace_contents_finish:
5639 * @file: input #GFile.
5640 * @res: a #GAsyncResult.
5641 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5642 * for the document. This should be freed with g_free() when it is no
5644 * @error: a #GError, or %NULL
5646 * Finishes an asynchronous replace of the given @file. See
5647 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5648 * tag for the document, if present.
5650 * Returns: %TRUE on success, %FALSE on failure.
5653 g_file_replace_contents_finish (GFile *file,
5658 GSimpleAsyncResult *simple;
5659 ReplaceContentsData *data;
5661 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5662 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5664 simple = G_SIMPLE_ASYNC_RESULT (res);
5666 if (g_simple_async_result_propagate_error (simple, error))
5669 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5671 data = g_simple_async_result_get_op_res_gpointer (simple);
5675 g_propagate_error (error, data->error);
5683 *new_etag = data->etag;
5684 data->etag = NULL; /* Take ownership */
5690 #define __G_FILE_C__
5691 #include "gioaliasdef.c"