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), G_FILE_TYPE_UNKNOWN);
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,
2306 cancellable, error);
2310 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2315 g_object_unref (in);
2319 if (!copy_stream_with_progress (in, out, source, cancellable,
2320 progress_callback, progress_callback_data,
2326 /* Ignore errors here. Failure to copy metadata is not a hard error */
2327 g_file_copy_attributes (source, destination,
2328 flags, cancellable, NULL);
2335 * @source: input #GFile.
2336 * @destination: destination #GFile
2337 * @flags: set of #GFileCopyFlags
2338 * @cancellable: optional #GCancellable object, %NULL to ignore.
2339 * @progress_callback: function to callback with progress information
2340 * @progress_callback_data: user data to pass to @progress_callback
2341 * @error: #GError to set on error, or %NULL
2343 * Copies the file @source to the location specified by @destination.
2344 * Can not handle recursive copies of directories.
2346 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2347 * existing @destination file is overwritten.
2349 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2350 * will be copied as symlinks, otherwise the target of the
2351 * @source symlink will be copied.
2353 * If @cancellable is not %NULL, then the operation can be cancelled by
2354 * triggering the cancellable object from another thread. If the operation
2355 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2357 * If @progress_callback is not %NULL, then the operation can be monitored by
2358 * setting this to a #GFileProgressCallback function. @progress_callback_data
2359 * will be passed to this function. It is guaranteed that this callback will
2360 * be called after all data has been transferred with the total number of bytes
2361 * copied during the operation.
2363 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2364 * error is returned, independent on the status of the @destination.
2366 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2367 * error G_IO_ERROR_EXISTS is returned.
2369 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2370 * error is returned. If trying to overwrite a directory with a directory the
2371 * G_IO_ERROR_WOULD_MERGE error is returned.
2373 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2374 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2377 * If you are interested in copying the #GFile object itself (not the on-disk
2378 * file), see g_file_dup().
2380 * Returns: %TRUE on success, %FALSE otherwise.
2383 g_file_copy (GFile *source,
2385 GFileCopyFlags flags,
2386 GCancellable *cancellable,
2387 GFileProgressCallback progress_callback,
2388 gpointer progress_callback_data,
2395 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2396 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2398 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2401 iface = G_FILE_GET_IFACE (destination);
2405 res = (* iface->copy) (source, destination,
2407 progress_callback, progress_callback_data,
2413 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2415 g_propagate_error (error, my_error);
2420 /* If the types are different, and the destination method failed
2421 also try the source method */
2422 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2424 iface = G_FILE_GET_IFACE (source);
2429 res = (* iface->copy) (source, destination,
2431 progress_callback, progress_callback_data,
2437 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2439 g_propagate_error (error, my_error);
2445 return file_copy_fallback (source, destination, flags, cancellable,
2446 progress_callback, progress_callback_data,
2451 * g_file_copy_async:
2452 * @source: input #GFile.
2453 * @destination: destination #GFile
2454 * @flags: set of #GFileCopyFlags
2455 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2457 * @cancellable: optional #GCancellable object, %NULL to ignore.
2458 * @progress_callback: function to callback with progress information
2459 * @progress_callback_data: user data to pass to @progress_callback
2460 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2461 * @user_data: the data to pass to callback function
2463 * Copies the file @source to the location specified by @destination
2464 * asynchronously. For details of the behaviour, see g_file_copy().
2466 * If @progress_callback is not %NULL, then that function that will be called
2467 * just like in g_file_copy(), however the callback will run in the main loop,
2468 * not in the thread that is doing the I/O operation.
2470 * When the operation is finished, @callback will be called. You can then call
2471 * g_file_copy_finish() to get the result of the operation.
2474 g_file_copy_async (GFile *source,
2476 GFileCopyFlags flags,
2478 GCancellable *cancellable,
2479 GFileProgressCallback progress_callback,
2480 gpointer progress_callback_data,
2481 GAsyncReadyCallback callback,
2486 g_return_if_fail (G_IS_FILE (source));
2487 g_return_if_fail (G_IS_FILE (destination));
2489 iface = G_FILE_GET_IFACE (source);
2490 (* iface->copy_async) (source,
2496 progress_callback_data,
2502 * g_file_copy_finish:
2503 * @file: input #GFile.
2504 * @res: a #GAsyncResult.
2505 * @error: a #GError, or %NULL
2507 * Finishes copying the file started with
2508 * g_file_copy_async().
2510 * Returns: a %TRUE on success, %FALSE on error.
2513 g_file_copy_finish (GFile *file,
2519 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2520 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2522 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2524 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2526 if (g_simple_async_result_propagate_error (simple, error))
2530 iface = G_FILE_GET_IFACE (file);
2531 return (* iface->copy_finish) (file, res, error);
2536 * @source: #GFile pointing to the source location.
2537 * @destination: #GFile pointing to the destination location.
2538 * @flags: set of #GFileCopyFlags.
2539 * @cancellable: optional #GCancellable object, %NULL to ignore.
2540 * @progress_callback: #GFileProgressCallback function for updates.
2541 * @progress_callback_data: gpointer to user data for the callback function.
2542 * @error: #GError for returning error conditions, or %NULL
2545 * Tries to move the file or directory @source to the location specified by @destination.
2546 * If native move operations are supported then this is used, otherwise a copy + delete
2547 * fallback is used. The native implementation may support moving directories (for instance
2548 * on moves inside the same filesystem), but the fallback code does not.
2550 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2551 * existing @destination file is overwritten.
2553 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2554 * will be copied as symlinks, otherwise the target of the
2555 * @source symlink will be copied.
2557 * If @cancellable is not %NULL, then the operation can be cancelled by
2558 * triggering the cancellable object from another thread. If the operation
2559 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2561 * If @progress_callback is not %NULL, then the operation can be monitored by
2562 * setting this to a #GFileProgressCallback function. @progress_callback_data
2563 * will be passed to this function. It is guaranteed that this callback will
2564 * be called after all data has been transferred with the total number of bytes
2565 * copied during the operation.
2567 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2568 * error is returned, independent on the status of the @destination.
2570 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2571 * error G_IO_ERROR_EXISTS is returned.
2573 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2574 * error is returned. If trying to overwrite a directory with a directory the
2575 * G_IO_ERROR_WOULD_MERGE error is returned.
2577 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2578 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2579 * may be returned (if the native move operation isn't available).
2581 * Returns: %TRUE on successful move, %FALSE otherwise.
2584 g_file_move (GFile *source,
2586 GFileCopyFlags flags,
2587 GCancellable *cancellable,
2588 GFileProgressCallback progress_callback,
2589 gpointer progress_callback_data,
2596 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2597 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2599 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2602 iface = G_FILE_GET_IFACE (destination);
2606 res = (* iface->move) (source, destination,
2608 progress_callback, progress_callback_data,
2614 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2616 g_propagate_error (error, my_error);
2621 /* If the types are different, and the destination method failed
2622 also try the source method */
2623 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2625 iface = G_FILE_GET_IFACE (source);
2630 res = (* iface->move) (source, destination,
2632 progress_callback, progress_callback_data,
2638 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2640 g_propagate_error (error, my_error);
2646 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2648 g_set_error (error, G_IO_ERROR,
2649 G_IO_ERROR_NOT_SUPPORTED,
2650 _("Operation not supported"));
2654 flags |= G_FILE_COPY_ALL_METADATA;
2655 if (!g_file_copy (source, destination, flags, cancellable,
2656 progress_callback, progress_callback_data,
2660 return g_file_delete (source, cancellable, error);
2664 * g_file_make_directory
2665 * @file: input #GFile.
2666 * @cancellable: optional #GCancellable object, %NULL to ignore.
2667 * @error: a #GError, or %NULL
2669 * Creates a directory.
2671 * If @cancellable is not %NULL, then the operation can be cancelled by
2672 * triggering the cancellable object from another thread. If the operation
2673 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2675 * Returns: %TRUE on successful creation, %FALSE otherwise.
2678 g_file_make_directory (GFile *file,
2679 GCancellable *cancellable,
2684 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2686 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2689 iface = G_FILE_GET_IFACE (file);
2691 if (iface->make_directory == NULL)
2693 g_set_error (error, G_IO_ERROR,
2694 G_IO_ERROR_NOT_SUPPORTED,
2695 _("Operation not supported"));
2699 return (* iface->make_directory) (file, cancellable, error);
2703 * g_file_make_symbolic_link:
2704 * @file: input #GFile.
2705 * @symlink_value: a string with the value of the new symlink.
2706 * @cancellable: optional #GCancellable object, %NULL to ignore.
2707 * @error: a #GError.
2709 * Creates a symbolic link.
2711 * If @cancellable is not %NULL, then the operation can be cancelled by
2712 * triggering the cancellable object from another thread. If the operation
2713 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2715 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2718 g_file_make_symbolic_link (GFile *file,
2719 const char *symlink_value,
2720 GCancellable *cancellable,
2725 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2726 g_return_val_if_fail (symlink_value != NULL, FALSE);
2728 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2731 if (*symlink_value == '\0')
2733 g_set_error (error, G_IO_ERROR,
2734 G_IO_ERROR_INVALID_ARGUMENT,
2735 _("Invalid symlink value given"));
2739 iface = G_FILE_GET_IFACE (file);
2741 if (iface->make_symbolic_link == NULL)
2743 g_set_error (error, G_IO_ERROR,
2744 G_IO_ERROR_NOT_SUPPORTED,
2745 _("Operation not supported"));
2749 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2754 * @file: input #GFile.
2755 * @cancellable: optional #GCancellable object, %NULL to ignore.
2756 * @error: a #GError, or %NULL
2758 * Deletes a file. If the @file is a directory, it will only be deleted if it
2761 * If @cancellable is not %NULL, then the operation can be cancelled by
2762 * triggering the cancellable object from another thread. If the operation
2763 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2765 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2768 g_file_delete (GFile *file,
2769 GCancellable *cancellable,
2774 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2776 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2779 iface = G_FILE_GET_IFACE (file);
2781 if (iface->delete_file == NULL)
2783 g_set_error (error, G_IO_ERROR,
2784 G_IO_ERROR_NOT_SUPPORTED,
2785 _("Operation not supported"));
2789 return (* iface->delete_file) (file, cancellable, error);
2794 * @file: #GFile to send to trash.
2795 * @cancellable: optional #GCancellable object, %NULL to ignore.
2796 * @error: a #GError, or %NULL
2798 * Sends @file to the "Trashcan", if possible. This is similar to
2799 * deleting it, but the user can recover it before emptying the trashcan.
2800 * Not all file systems support trashing, so this call can return the
2801 * %G_IO_ERROR_NOT_SUPPORTED error.
2804 * If @cancellable is not %NULL, then the operation can be cancelled by
2805 * triggering the cancellable object from another thread. If the operation
2806 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2808 * Returns: %TRUE on successful trash, %FALSE otherwise.
2811 g_file_trash (GFile *file,
2812 GCancellable *cancellable,
2817 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2819 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2822 iface = G_FILE_GET_IFACE (file);
2824 if (iface->trash == NULL)
2827 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2828 _("Trash not supported"));
2832 return (* iface->trash) (file, cancellable, error);
2836 * g_file_set_display_name:
2837 * @file: input #GFile.
2838 * @display_name: a string.
2839 * @cancellable: optional #GCancellable object, %NULL to ignore.
2840 * @error: a #GError, or %NULL
2842 * Renames @file to the specified display name.
2844 * The display name is converted from UTF8 to the correct encoding for the target
2845 * filesystem if possible and the @file is renamed to this.
2847 * If you want to implement a rename operation in the user interface the edit name
2848 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2849 * widget, and then the result after editing should be passed to g_file_set_display_name().
2851 * On success the resulting converted filename is returned.
2853 * If @cancellable is not %NULL, then the operation can be cancelled by
2854 * triggering the cancellable object from another thread. If the operation
2855 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2857 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2860 g_file_set_display_name (GFile *file,
2861 const char *display_name,
2862 GCancellable *cancellable,
2867 g_return_val_if_fail (G_IS_FILE (file), NULL);
2868 g_return_val_if_fail (display_name != NULL, NULL);
2870 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2874 G_IO_ERROR_INVALID_ARGUMENT,
2875 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2879 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2882 iface = G_FILE_GET_IFACE (file);
2884 return (* iface->set_display_name) (file, display_name, cancellable, error);
2888 * g_file_set_display_name_async:
2889 * @file: input #GFile.
2890 * @display_name: a string.
2891 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2893 * @cancellable: optional #GCancellable object, %NULL to ignore.
2894 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2895 * @user_data: the data to pass to callback function
2897 * Asynchronously sets the display name for a given #GFile.
2899 * For more details, see g_set_display_name() which is
2900 * the synchronous version of this call.
2902 * When the operation is finished, @callback will be called. You can then call
2903 * g_file_set_display_name_finish() to get the result of the operation.
2906 g_file_set_display_name_async (GFile *file,
2907 const char *display_name,
2909 GCancellable *cancellable,
2910 GAsyncReadyCallback callback,
2915 g_return_if_fail (G_IS_FILE (file));
2916 g_return_if_fail (display_name != NULL);
2918 iface = G_FILE_GET_IFACE (file);
2919 (* iface->set_display_name_async) (file,
2928 * g_file_set_display_name_finish:
2929 * @file: input #GFile.
2930 * @res: a #GAsyncResult.
2931 * @error: a #GError, or %NULL
2933 * Finishes setting a display name started with
2934 * g_file_set_display_name_async().
2936 * Returns: a #GFile or %NULL on error.
2939 g_file_set_display_name_finish (GFile *file,
2945 g_return_val_if_fail (G_IS_FILE (file), NULL);
2946 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2948 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2950 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2951 if (g_simple_async_result_propagate_error (simple, error))
2955 iface = G_FILE_GET_IFACE (file);
2956 return (* iface->set_display_name_finish) (file, res, error);
2960 * g_file_query_settable_attributes:
2961 * @file: input #GFile.
2962 * @cancellable: optional #GCancellable object, %NULL to ignore.
2963 * @error: a #GError, or %NULL
2965 * Obtain the list of settable attributes for the file.
2967 * Returns the type and full attribute name of all the attributes
2968 * that can be set on this file. This doesn't mean setting it will always
2969 * succeed though, you might get an access failure, or some specific
2970 * file may not support a specific attribute.
2972 * If @cancellable is not %NULL, then the operation can be cancelled by
2973 * triggering the cancellable object from another thread. If the operation
2974 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2976 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2977 * When you are done with it, release it with g_file_attribute_info_list_unref()
2979 GFileAttributeInfoList *
2980 g_file_query_settable_attributes (GFile *file,
2981 GCancellable *cancellable,
2986 GFileAttributeInfoList *list;
2988 g_return_val_if_fail (G_IS_FILE (file), NULL);
2990 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2993 iface = G_FILE_GET_IFACE (file);
2995 if (iface->query_settable_attributes == NULL)
2996 return g_file_attribute_info_list_new ();
2999 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3003 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3005 list = g_file_attribute_info_list_new ();
3006 g_error_free (my_error);
3009 g_propagate_error (error, my_error);
3016 * g_file_query_writable_namespaces:
3017 * @file: input #GFile.
3018 * @cancellable: optional #GCancellable object, %NULL to ignore.
3019 * @error: a #GError, or %NULL
3021 * Obtain the list of attribute namespaces where new attributes
3022 * can be created by a user. An example of this is extended
3023 * attributes (in the "xattr" namespace).
3025 * If @cancellable is not %NULL, then the operation can be cancelled by
3026 * triggering the cancellable object from another thread. If the operation
3027 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3029 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3030 * When you are done with it, release it with g_file_attribute_info_list_unref()
3032 GFileAttributeInfoList *
3033 g_file_query_writable_namespaces (GFile *file,
3034 GCancellable *cancellable,
3039 GFileAttributeInfoList *list;
3041 g_return_val_if_fail (G_IS_FILE (file), NULL);
3043 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3046 iface = G_FILE_GET_IFACE (file);
3048 if (iface->query_writable_namespaces == NULL)
3049 return g_file_attribute_info_list_new ();
3052 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3056 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3058 list = g_file_attribute_info_list_new ();
3059 g_error_free (my_error);
3062 g_propagate_error (error, my_error);
3069 * g_file_set_attribute:
3070 * @file: input #GFile.
3071 * @attribute: a string containing the attribute's name.
3072 * @type: The type of the attribute
3073 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3074 * @flags: a set of #GFileQueryInfoFlags.
3075 * @cancellable: optional #GCancellable object, %NULL to ignore.
3076 * @error: a #GError, or %NULL
3078 * Sets an attribute in the file with attribute name @attribute to @value.
3080 * If @cancellable is not %NULL, then the operation can be cancelled by
3081 * triggering the cancellable object from another thread. If the operation
3082 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3084 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3087 g_file_set_attribute (GFile *file,
3088 const char *attribute,
3089 GFileAttributeType type,
3091 GFileQueryInfoFlags flags,
3092 GCancellable *cancellable,
3097 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3098 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3100 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3103 iface = G_FILE_GET_IFACE (file);
3105 if (iface->set_attribute == NULL)
3107 g_set_error (error, G_IO_ERROR,
3108 G_IO_ERROR_NOT_SUPPORTED,
3109 _("Operation not supported"));
3113 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3117 * g_file_set_attributes_from_info:
3118 * @file: input #GFile.
3119 * @info: a #GFileInfo.
3120 * @flags: #GFileQueryInfoFlags
3121 * @cancellable: optional #GCancellable object, %NULL to ignore.
3122 * @error: a #GError, or %NULL
3124 * Tries to set all attributes in the #GFileInfo on the target values,
3125 * not stopping on the first error.
3127 * If there is any error during this operation then @error will be set to
3128 * the first error. Error on particular fields are flagged by setting
3129 * the "status" field in the attribute value to
3130 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3133 * If @cancellable is not %NULL, then the operation can be cancelled by
3134 * triggering the cancellable object from another thread. If the operation
3135 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3137 * Returns: %TRUE if there was any error, %FALSE otherwise.
3140 g_file_set_attributes_from_info (GFile *file,
3142 GFileQueryInfoFlags flags,
3143 GCancellable *cancellable,
3148 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3149 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3151 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3154 g_file_info_clear_status (info);
3156 iface = G_FILE_GET_IFACE (file);
3158 return (* iface->set_attributes_from_info) (file,
3167 g_file_real_set_attributes_from_info (GFile *file,
3169 GFileQueryInfoFlags flags,
3170 GCancellable *cancellable,
3176 GFileAttributeValue *value;
3180 attributes = g_file_info_list_attributes (info, NULL);
3182 for (i = 0; attributes[i] != NULL; i++)
3184 value = _g_file_info_get_attribute_value (info, attributes[i]);
3186 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3189 if (!g_file_set_attribute (file, attributes[i],
3190 value->type, _g_file_attribute_value_peek_as_pointer (value),
3191 flags, cancellable, error))
3193 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3195 /* Don't set error multiple times */
3199 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3202 g_strfreev (attributes);
3208 * g_file_set_attributes_async:
3209 * @file: input #GFile.
3210 * @info: a #GFileInfo.
3211 * @flags: a #GFileQueryInfoFlags.
3212 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3214 * @cancellable: optional #GCancellable object, %NULL to ignore.
3215 * @callback: a #GAsyncReadyCallback.
3216 * @user_data: a #gpointer.
3218 * Asynchronously sets the attributes of @file with @info.
3220 * For more details, see g_file_set_attributes_from_info() which is
3221 * the synchronous version of this call.
3223 * When the operation is finished, @callback will be called. You can then call
3224 * g_file_set_attributes_finish() to get the result of the operation.
3227 g_file_set_attributes_async (GFile *file,
3229 GFileQueryInfoFlags flags,
3231 GCancellable *cancellable,
3232 GAsyncReadyCallback callback,
3237 g_return_if_fail (G_IS_FILE (file));
3238 g_return_if_fail (G_IS_FILE_INFO (info));
3240 iface = G_FILE_GET_IFACE (file);
3241 (* iface->set_attributes_async) (file,
3251 * g_file_set_attributes_finish:
3252 * @file: input #GFile.
3253 * @result: a #GAsyncResult.
3254 * @info: a #GFileInfo.
3255 * @error: a #GError, or %NULL
3257 * Finishes setting an attribute started in g_file_set_attributes_async().
3259 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3262 g_file_set_attributes_finish (GFile *file,
3263 GAsyncResult *result,
3269 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3270 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3272 /* No standard handling of errors here, as we must set info even
3275 iface = G_FILE_GET_IFACE (file);
3276 return (* iface->set_attributes_finish) (file, result, info, error);
3280 * g_file_set_attribute_string:
3281 * @file: input #GFile.
3282 * @attribute: a string containing the attribute's name.
3283 * @value: a string containing the attribute's value.
3284 * @flags: #GFileQueryInfoFlags.
3285 * @cancellable: optional #GCancellable object, %NULL to ignore.
3286 * @error: a #GError, or %NULL
3288 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3289 * If @attribute is of a different type, this operation will fail.
3291 * If @cancellable is not %NULL, then the operation can be cancelled by
3292 * triggering the cancellable object from another thread. If the operation
3293 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3295 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3298 g_file_set_attribute_string (GFile *file,
3299 const char *attribute,
3301 GFileQueryInfoFlags flags,
3302 GCancellable *cancellable,
3305 return g_file_set_attribute (file, attribute,
3306 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3307 flags, cancellable, error);
3311 * g_file_set_attribute_byte_string:
3312 * @file: input #GFile.
3313 * @attribute: a string containing the attribute's name.
3314 * @value: a string containing the attribute's new value.
3315 * @flags: a #GFileQueryInfoFlags.
3316 * @cancellable: optional #GCancellable object, %NULL to ignore.
3317 * @error: a #GError, or %NULL
3319 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3320 * If @attribute is of a different type, this operation will fail,
3323 * If @cancellable is not %NULL, then the operation can be cancelled by
3324 * triggering the cancellable object from another thread. If the operation
3325 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3327 * Returns: %TRUE if the @attribute was successfully set to @value
3328 * in the @file, %FALSE otherwise.
3331 g_file_set_attribute_byte_string (GFile *file,
3332 const char *attribute,
3334 GFileQueryInfoFlags flags,
3335 GCancellable *cancellable,
3338 return g_file_set_attribute (file, attribute,
3339 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3340 flags, cancellable, error);
3344 * g_file_set_attribute_uint32:
3345 * @file: input #GFile.
3346 * @attribute: a string containing the attribute's name.
3347 * @value: a #guint32 containing the attribute's new value.
3348 * @flags: a #GFileQueryInfoFlags.
3349 * @cancellable: optional #GCancellable object, %NULL to ignore.
3350 * @error: a #GError, or %NULL
3352 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3353 * If @attribute is of a different type, this operation will fail.
3355 * If @cancellable is not %NULL, then the operation can be cancelled by
3356 * triggering the cancellable object from another thread. If the operation
3357 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3359 * Returns: %TRUE if the @attribute was successfully set to @value
3360 * in the @file, %FALSE otherwise.
3363 g_file_set_attribute_uint32 (GFile *file,
3364 const char *attribute,
3366 GFileQueryInfoFlags flags,
3367 GCancellable *cancellable,
3370 return g_file_set_attribute (file, attribute,
3371 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3372 flags, cancellable, error);
3376 * g_file_set_attribute_int32:
3377 * @file: input #GFile.
3378 * @attribute: a string containing the attribute's name.
3379 * @value: a #gint32 containing the attribute's new value.
3380 * @flags: a #GFileQueryInfoFlags.
3381 * @cancellable: optional #GCancellable object, %NULL to ignore.
3382 * @error: a #GError, or %NULL
3384 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3385 * If @attribute is of a different type, this operation will fail.
3387 * If @cancellable is not %NULL, then the operation can be cancelled by
3388 * triggering the cancellable object from another thread. If the operation
3389 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3391 * Returns: %TRUE if the @attribute was successfully set to @value
3392 * in the @file, %FALSE otherwise.
3395 g_file_set_attribute_int32 (GFile *file,
3396 const char *attribute,
3398 GFileQueryInfoFlags flags,
3399 GCancellable *cancellable,
3402 return g_file_set_attribute (file, attribute,
3403 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3404 flags, cancellable, error);
3408 * g_file_set_attribute_uint64:
3409 * @file: input #GFile.
3410 * @attribute: a string containing the attribute's name.
3411 * @value: a #guint64 containing the attribute's new value.
3412 * @flags: a #GFileQueryInfoFlags.
3413 * @cancellable: optional #GCancellable object, %NULL to ignore.
3414 * @error: a #GError, or %NULL
3416 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3417 * If @attribute is of a different type, this operation will fail.
3419 * If @cancellable is not %NULL, then the operation can be cancelled by
3420 * triggering the cancellable object from another thread. If the operation
3421 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3423 * Returns: %TRUE if the @attribute was successfully set to @value
3424 * in the @file, %FALSE otherwise.
3427 g_file_set_attribute_uint64 (GFile *file,
3428 const char *attribute,
3430 GFileQueryInfoFlags flags,
3431 GCancellable *cancellable,
3434 return g_file_set_attribute (file, attribute,
3435 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3436 flags, cancellable, error);
3440 * g_file_set_attribute_int64:
3441 * @file: input #GFile.
3442 * @attribute: a string containing the attribute's name.
3443 * @value: a #guint64 containing the attribute's new value.
3444 * @flags: a #GFileQueryInfoFlags.
3445 * @cancellable: optional #GCancellable object, %NULL to ignore.
3446 * @error: a #GError, or %NULL
3448 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3449 * If @attribute is of a different type, this operation will fail.
3451 * If @cancellable is not %NULL, then the operation can be cancelled by
3452 * triggering the cancellable object from another thread. If the operation
3453 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3455 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3458 g_file_set_attribute_int64 (GFile *file,
3459 const char *attribute,
3461 GFileQueryInfoFlags flags,
3462 GCancellable *cancellable,
3465 return g_file_set_attribute (file, attribute,
3466 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3467 flags, cancellable, error);
3471 * g_file_mount_mountable:
3472 * @file: input #GFile.
3473 * @flags: flags affecting the operation
3474 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3475 * @cancellable: optional #GCancellable object, %NULL to ignore.
3476 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3477 * @user_data: the data to pass to callback function
3479 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3480 * Using @mount_operation, you can request callbacks when, for instance,
3481 * passwords are needed during authentication.
3483 * If @cancellable is not %NULL, then the operation can be cancelled by
3484 * triggering the cancellable object from another thread. If the operation
3485 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3487 * When the operation is finished, @callback will be called. You can then call
3488 * g_file_mount_mountable_finish() to get the result of the operation.
3491 g_file_mount_mountable (GFile *file,
3492 GMountMountFlags flags,
3493 GMountOperation *mount_operation,
3494 GCancellable *cancellable,
3495 GAsyncReadyCallback callback,
3500 g_return_if_fail (G_IS_FILE (file));
3502 iface = G_FILE_GET_IFACE (file);
3504 if (iface->mount_mountable == NULL)
3506 g_simple_async_report_error_in_idle (G_OBJECT (file),
3510 G_IO_ERROR_NOT_SUPPORTED,
3511 _("Operation not supported"));
3515 (* iface->mount_mountable) (file,
3524 * g_file_mount_mountable_finish:
3525 * @file: input #GFile.
3526 * @result: a #GAsyncResult.
3527 * @error: a #GError, or %NULL
3529 * Finishes a mount operation. See g_file_mount_mountable() for details.
3531 * Finish an asynchronous mount operation that was started
3532 * with g_file_mount_mountable().
3534 * Returns: a #GFile or %NULL on error.
3537 g_file_mount_mountable_finish (GFile *file,
3538 GAsyncResult *result,
3543 g_return_val_if_fail (G_IS_FILE (file), NULL);
3544 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3546 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3548 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3549 if (g_simple_async_result_propagate_error (simple, error))
3553 iface = G_FILE_GET_IFACE (file);
3554 return (* iface->mount_mountable_finish) (file, result, error);
3558 * g_file_unmount_mountable:
3559 * @file: input #GFile.
3560 * @flags: flags affecting the operation
3561 * @cancellable: optional #GCancellable object, %NULL to ignore.
3562 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3563 * @user_data: the data to pass to callback function
3565 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3567 * If @cancellable is not %NULL, then the operation can be cancelled by
3568 * triggering the cancellable object from another thread. If the operation
3569 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3571 * When the operation is finished, @callback will be called. You can then call
3572 * g_file_unmount_mountable_finish() to get the result of the operation.
3575 g_file_unmount_mountable (GFile *file,
3576 GMountUnmountFlags flags,
3577 GCancellable *cancellable,
3578 GAsyncReadyCallback callback,
3583 g_return_if_fail (G_IS_FILE (file));
3585 iface = G_FILE_GET_IFACE (file);
3587 if (iface->unmount_mountable == NULL)
3589 g_simple_async_report_error_in_idle (G_OBJECT (file),
3593 G_IO_ERROR_NOT_SUPPORTED,
3594 _("Operation not supported"));
3598 (* iface->unmount_mountable) (file,
3606 * g_file_unmount_mountable_finish:
3607 * @file: input #GFile.
3608 * @result: a #GAsyncResult.
3609 * @error: a #GError, or %NULL
3611 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3613 * Finish an asynchronous unmount operation that was started
3614 * with g_file_unmount_mountable().
3616 * Returns: %TRUE if the operation finished successfully. %FALSE
3620 g_file_unmount_mountable_finish (GFile *file,
3621 GAsyncResult *result,
3626 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3627 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3629 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3631 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3632 if (g_simple_async_result_propagate_error (simple, error))
3636 iface = G_FILE_GET_IFACE (file);
3637 return (* iface->unmount_mountable_finish) (file, result, error);
3641 * g_file_eject_mountable:
3642 * @file: input #GFile.
3643 * @flags: flags affecting the operation
3644 * @cancellable: optional #GCancellable object, %NULL to ignore.
3645 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3646 * @user_data: the data to pass to callback function
3648 * Starts an asynchronous eject on a mountable.
3649 * When this operation has completed, @callback will be called with
3650 * @user_user data, and the operation can be finalized with
3651 * g_file_eject_mountable_finish().
3653 * If @cancellable is not %NULL, then the operation can be cancelled by
3654 * triggering the cancellable object from another thread. If the operation
3655 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3658 g_file_eject_mountable (GFile *file,
3659 GMountUnmountFlags flags,
3660 GCancellable *cancellable,
3661 GAsyncReadyCallback callback,
3666 g_return_if_fail (G_IS_FILE (file));
3668 iface = G_FILE_GET_IFACE (file);
3670 if (iface->eject_mountable == NULL)
3672 g_simple_async_report_error_in_idle (G_OBJECT (file),
3676 G_IO_ERROR_NOT_SUPPORTED,
3677 _("Operation not supported"));
3681 (* iface->eject_mountable) (file,
3689 * g_file_eject_mountable_finish:
3690 * @file: input #GFile.
3691 * @result: a #GAsyncResult.
3692 * @error: a #GError, or %NULL
3694 * Finishes an asynchronous eject operation started by
3695 * g_file_eject_mountable().
3697 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3701 g_file_eject_mountable_finish (GFile *file,
3702 GAsyncResult *result,
3707 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3708 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3710 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3712 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3713 if (g_simple_async_result_propagate_error (simple, error))
3717 iface = G_FILE_GET_IFACE (file);
3718 return (* iface->eject_mountable_finish) (file, result, error);
3722 * g_file_monitor_directory:
3723 * @file: input #GFile.
3724 * @flags: a set of #GFileMonitorFlags.
3725 * @cancellable: optional #GCancellable object, %NULL to ignore.
3726 * @error: a #GError, or %NULL.
3728 * Obtains a directory monitor for the given file.
3729 * This may fail if directory monitoring is not supported.
3731 * If @cancellable is not %NULL, then the operation can be cancelled by
3732 * triggering the cancellable object from another thread. If the operation
3733 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3735 * Returns: a #GFileMonitor for the given @file,
3736 * or %NULL on error.
3739 g_file_monitor_directory (GFile *file,
3740 GFileMonitorFlags flags,
3741 GCancellable *cancellable,
3746 g_return_val_if_fail (G_IS_FILE (file), NULL);
3748 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3751 iface = G_FILE_GET_IFACE (file);
3753 if (iface->monitor_dir == NULL)
3755 g_set_error (error, G_IO_ERROR,
3756 G_IO_ERROR_NOT_SUPPORTED,
3757 _("Operation not supported"));
3761 return (* iface->monitor_dir) (file, flags, cancellable, error);
3765 * g_file_monitor_file:
3766 * @file: input #GFile.
3767 * @flags: a set of #GFileMonitorFlags.
3768 * @cancellable: optional #GCancellable object, %NULL to ignore.
3769 * @error: a #GError, or %NULL.
3771 * Obtains a file monitor for the given file. If no file notification
3772 * mechanism exists, then regular polling of the file is used.
3774 * If @cancellable is not %NULL, then the operation can be cancelled by
3775 * triggering the cancellable object from another thread. If the operation
3776 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3778 * Returns: a #GFileMonitor for the given @file.
3781 g_file_monitor_file (GFile *file,
3782 GFileMonitorFlags flags,
3783 GCancellable *cancellable,
3787 GFileMonitor *monitor;
3789 g_return_val_if_fail (G_IS_FILE (file), NULL);
3791 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3794 iface = G_FILE_GET_IFACE (file);
3798 if (iface->monitor_file)
3799 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3801 /* Fallback to polling */
3802 if (monitor == NULL)
3803 monitor = _g_poll_file_monitor_new (file);
3808 /********************************************
3809 * Default implementation of async ops *
3810 ********************************************/
3814 GFileQueryInfoFlags flags;
3816 } QueryInfoAsyncData;
3819 query_info_data_free (QueryInfoAsyncData *data)
3822 g_object_unref (data->info);
3823 g_free (data->attributes);
3828 query_info_async_thread (GSimpleAsyncResult *res,
3830 GCancellable *cancellable)
3832 GError *error = NULL;
3833 QueryInfoAsyncData *data;
3836 data = g_simple_async_result_get_op_res_gpointer (res);
3838 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3842 g_simple_async_result_set_from_error (res, error);
3843 g_error_free (error);
3850 g_file_real_query_info_async (GFile *file,
3851 const char *attributes,
3852 GFileQueryInfoFlags flags,
3854 GCancellable *cancellable,
3855 GAsyncReadyCallback callback,
3858 GSimpleAsyncResult *res;
3859 QueryInfoAsyncData *data;
3861 data = g_new0 (QueryInfoAsyncData, 1);
3862 data->attributes = g_strdup (attributes);
3863 data->flags = flags;
3865 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3866 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3868 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3869 g_object_unref (res);
3873 g_file_real_query_info_finish (GFile *file,
3877 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3878 QueryInfoAsyncData *data;
3880 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3882 data = g_simple_async_result_get_op_res_gpointer (simple);
3884 return g_object_ref (data->info);
3892 } QueryFilesystemInfoAsyncData;
3895 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
3898 g_object_unref (data->info);
3899 g_free (data->attributes);
3904 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
3906 GCancellable *cancellable)
3908 GError *error = NULL;
3909 QueryFilesystemInfoAsyncData *data;
3912 data = g_simple_async_result_get_op_res_gpointer (res);
3914 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
3918 g_simple_async_result_set_from_error (res, error);
3919 g_error_free (error);
3926 g_file_real_query_filesystem_info_async (GFile *file,
3927 const char *attributes,
3929 GCancellable *cancellable,
3930 GAsyncReadyCallback callback,
3933 GSimpleAsyncResult *res;
3934 QueryFilesystemInfoAsyncData *data;
3936 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
3937 data->attributes = g_strdup (attributes);
3939 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
3940 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
3942 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
3943 g_object_unref (res);
3947 g_file_real_query_filesystem_info_finish (GFile *file,
3951 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3952 QueryFilesystemInfoAsyncData *data;
3954 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
3956 data = g_simple_async_result_get_op_res_gpointer (simple);
3958 return g_object_ref (data->info);
3965 GFileQueryInfoFlags flags;
3966 GFileEnumerator *enumerator;
3967 } EnumerateChildrenAsyncData;
3970 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3972 if (data->enumerator)
3973 g_object_unref (data->enumerator);
3974 g_free (data->attributes);
3979 enumerate_children_async_thread (GSimpleAsyncResult *res,
3981 GCancellable *cancellable)
3983 GError *error = NULL;
3984 EnumerateChildrenAsyncData *data;
3985 GFileEnumerator *enumerator;
3987 data = g_simple_async_result_get_op_res_gpointer (res);
3989 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3991 if (enumerator == NULL)
3993 g_simple_async_result_set_from_error (res, error);
3994 g_error_free (error);
3997 data->enumerator = enumerator;
4001 g_file_real_enumerate_children_async (GFile *file,
4002 const char *attributes,
4003 GFileQueryInfoFlags flags,
4005 GCancellable *cancellable,
4006 GAsyncReadyCallback callback,
4009 GSimpleAsyncResult *res;
4010 EnumerateChildrenAsyncData *data;
4012 data = g_new0 (EnumerateChildrenAsyncData, 1);
4013 data->attributes = g_strdup (attributes);
4014 data->flags = flags;
4016 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
4017 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
4019 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
4020 g_object_unref (res);
4023 static GFileEnumerator *
4024 g_file_real_enumerate_children_finish (GFile *file,
4028 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4029 EnumerateChildrenAsyncData *data;
4031 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
4033 data = g_simple_async_result_get_op_res_gpointer (simple);
4034 if (data->enumerator)
4035 return g_object_ref (data->enumerator);
4041 open_read_async_thread (GSimpleAsyncResult *res,
4043 GCancellable *cancellable)
4046 GFileInputStream *stream;
4047 GError *error = NULL;
4049 iface = G_FILE_GET_IFACE (object);
4051 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4055 g_simple_async_result_set_from_error (res, error);
4056 g_error_free (error);
4059 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4063 g_file_real_read_async (GFile *file,
4065 GCancellable *cancellable,
4066 GAsyncReadyCallback callback,
4069 GSimpleAsyncResult *res;
4071 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4073 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4074 g_object_unref (res);
4077 static GFileInputStream *
4078 g_file_real_read_finish (GFile *file,
4082 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4085 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4087 op = g_simple_async_result_get_op_res_gpointer (simple);
4089 return g_object_ref (op);
4095 append_to_async_thread (GSimpleAsyncResult *res,
4097 GCancellable *cancellable)
4100 GFileCreateFlags *data;
4101 GFileOutputStream *stream;
4102 GError *error = NULL;
4104 iface = G_FILE_GET_IFACE (object);
4106 data = g_simple_async_result_get_op_res_gpointer (res);
4108 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4112 g_simple_async_result_set_from_error (res, error);
4113 g_error_free (error);
4116 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4120 g_file_real_append_to_async (GFile *file,
4121 GFileCreateFlags flags,
4123 GCancellable *cancellable,
4124 GAsyncReadyCallback callback,
4127 GFileCreateFlags *data;
4128 GSimpleAsyncResult *res;
4130 data = g_new0 (GFileCreateFlags, 1);
4133 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4134 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4136 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4137 g_object_unref (res);
4140 static GFileOutputStream *
4141 g_file_real_append_to_finish (GFile *file,
4145 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4148 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4150 op = g_simple_async_result_get_op_res_gpointer (simple);
4152 return g_object_ref (op);
4158 create_async_thread (GSimpleAsyncResult *res,
4160 GCancellable *cancellable)
4163 GFileCreateFlags *data;
4164 GFileOutputStream *stream;
4165 GError *error = NULL;
4167 iface = G_FILE_GET_IFACE (object);
4169 data = g_simple_async_result_get_op_res_gpointer (res);
4171 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4175 g_simple_async_result_set_from_error (res, error);
4176 g_error_free (error);
4179 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4183 g_file_real_create_async (GFile *file,
4184 GFileCreateFlags flags,
4186 GCancellable *cancellable,
4187 GAsyncReadyCallback callback,
4190 GFileCreateFlags *data;
4191 GSimpleAsyncResult *res;
4193 data = g_new0 (GFileCreateFlags, 1);
4196 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4197 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4199 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4200 g_object_unref (res);
4203 static GFileOutputStream *
4204 g_file_real_create_finish (GFile *file,
4208 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4211 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4213 op = g_simple_async_result_get_op_res_gpointer (simple);
4215 return g_object_ref (op);
4221 GFileOutputStream *stream;
4223 gboolean make_backup;
4224 GFileCreateFlags flags;
4228 replace_async_data_free (ReplaceAsyncData *data)
4231 g_object_unref (data->stream);
4232 g_free (data->etag);
4237 replace_async_thread (GSimpleAsyncResult *res,
4239 GCancellable *cancellable)
4242 GFileOutputStream *stream;
4243 GError *error = NULL;
4244 ReplaceAsyncData *data;
4246 iface = G_FILE_GET_IFACE (object);
4248 data = g_simple_async_result_get_op_res_gpointer (res);
4250 stream = iface->replace (G_FILE (object),
4259 g_simple_async_result_set_from_error (res, error);
4260 g_error_free (error);
4263 data->stream = stream;
4267 g_file_real_replace_async (GFile *file,
4269 gboolean make_backup,
4270 GFileCreateFlags flags,
4272 GCancellable *cancellable,
4273 GAsyncReadyCallback callback,
4276 GSimpleAsyncResult *res;
4277 ReplaceAsyncData *data;
4279 data = g_new0 (ReplaceAsyncData, 1);
4280 data->etag = g_strdup (etag);
4281 data->make_backup = make_backup;
4282 data->flags = flags;
4284 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4285 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4287 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4288 g_object_unref (res);
4291 static GFileOutputStream *
4292 g_file_real_replace_finish (GFile *file,
4296 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4297 ReplaceAsyncData *data;
4299 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4301 data = g_simple_async_result_get_op_res_gpointer (simple);
4303 return g_object_ref (data->stream);
4311 } SetDisplayNameAsyncData;
4314 set_display_name_data_free (SetDisplayNameAsyncData *data)
4316 g_free (data->name);
4318 g_object_unref (data->file);
4323 set_display_name_async_thread (GSimpleAsyncResult *res,
4325 GCancellable *cancellable)
4327 GError *error = NULL;
4328 SetDisplayNameAsyncData *data;
4331 data = g_simple_async_result_get_op_res_gpointer (res);
4333 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4337 g_simple_async_result_set_from_error (res, error);
4338 g_error_free (error);
4345 g_file_real_set_display_name_async (GFile *file,
4346 const char *display_name,
4348 GCancellable *cancellable,
4349 GAsyncReadyCallback callback,
4352 GSimpleAsyncResult *res;
4353 SetDisplayNameAsyncData *data;
4355 data = g_new0 (SetDisplayNameAsyncData, 1);
4356 data->name = g_strdup (display_name);
4358 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4359 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4361 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4362 g_object_unref (res);
4366 g_file_real_set_display_name_finish (GFile *file,
4370 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4371 SetDisplayNameAsyncData *data;
4373 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4375 data = g_simple_async_result_get_op_res_gpointer (simple);
4377 return g_object_ref (data->file);
4383 GFileQueryInfoFlags flags;
4390 set_info_data_free (SetInfoAsyncData *data)
4393 g_object_unref (data->info);
4395 g_error_free (data->error);
4400 set_info_async_thread (GSimpleAsyncResult *res,
4402 GCancellable *cancellable)
4404 SetInfoAsyncData *data;
4406 data = g_simple_async_result_get_op_res_gpointer (res);
4409 data->res = g_file_set_attributes_from_info (G_FILE (object),
4417 g_file_real_set_attributes_async (GFile *file,
4419 GFileQueryInfoFlags flags,
4421 GCancellable *cancellable,
4422 GAsyncReadyCallback callback,
4425 GSimpleAsyncResult *res;
4426 SetInfoAsyncData *data;
4428 data = g_new0 (SetInfoAsyncData, 1);
4429 data->info = g_file_info_dup (info);
4430 data->flags = flags;
4432 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4433 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4435 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4436 g_object_unref (res);
4440 g_file_real_set_attributes_finish (GFile *file,
4445 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4446 SetInfoAsyncData *data;
4448 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4450 data = g_simple_async_result_get_op_res_gpointer (simple);
4453 *info = g_object_ref (data->info);
4455 if (error != NULL && data->error)
4456 *error = g_error_copy (data->error);
4462 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4464 GCancellable *cancellable)
4466 GError *error = NULL;
4469 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4473 g_simple_async_result_set_from_error (res, error);
4474 g_error_free (error);
4477 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4481 g_file_real_find_enclosing_mount_async (GFile *file,
4483 GCancellable *cancellable,
4484 GAsyncReadyCallback callback,
4487 GSimpleAsyncResult *res;
4489 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4491 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4492 g_object_unref (res);
4496 g_file_real_find_enclosing_mount_finish (GFile *file,
4500 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4503 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4505 mount = g_simple_async_result_get_op_res_gpointer (simple);
4506 return g_object_ref (mount);
4513 GFileCopyFlags flags;
4514 GFileProgressCallback progress_cb;
4515 gpointer progress_cb_data;
4516 GIOSchedulerJob *job;
4520 copy_async_data_free (CopyAsyncData *data)
4522 g_object_unref (data->source);
4523 g_object_unref (data->destination);
4528 CopyAsyncData *data;
4529 goffset current_num_bytes;
4530 goffset total_num_bytes;
4534 copy_async_progress_in_main (gpointer user_data)
4536 ProgressData *progress = user_data;
4537 CopyAsyncData *data = progress->data;
4539 data->progress_cb (progress->current_num_bytes,
4540 progress->total_num_bytes,
4541 data->progress_cb_data);
4547 mainloop_barrier (gpointer user_data)
4549 /* Does nothing, but ensures all queued idles before
4556 copy_async_progress_callback (goffset current_num_bytes,
4557 goffset total_num_bytes,
4560 CopyAsyncData *data = user_data;
4561 ProgressData *progress;
4563 progress = g_new (ProgressData, 1);
4564 progress->data = data;
4565 progress->current_num_bytes = current_num_bytes;
4566 progress->total_num_bytes = total_num_bytes;
4568 g_io_scheduler_job_send_to_mainloop_async (data->job,
4569 copy_async_progress_in_main,
4575 copy_async_thread (GIOSchedulerJob *job,
4576 GCancellable *cancellable,
4579 GSimpleAsyncResult *res;
4580 CopyAsyncData *data;
4585 data = g_simple_async_result_get_op_res_gpointer (res);
4589 result = g_file_copy (data->source,
4593 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4597 /* Ensure all progress callbacks are done running in main thread */
4598 if (data->progress_cb != NULL)
4599 g_io_scheduler_job_send_to_mainloop (job,
4605 g_simple_async_result_set_from_error (res, error);
4606 g_error_free (error);
4609 g_simple_async_result_complete_in_idle (res);
4615 g_file_real_copy_async (GFile *source,
4617 GFileCopyFlags flags,
4619 GCancellable *cancellable,
4620 GFileProgressCallback progress_callback,
4621 gpointer progress_callback_data,
4622 GAsyncReadyCallback callback,
4625 GSimpleAsyncResult *res;
4626 CopyAsyncData *data;
4628 data = g_new0 (CopyAsyncData, 1);
4629 data->source = g_object_ref (source);
4630 data->destination = g_object_ref (destination);
4631 data->flags = flags;
4632 data->progress_cb = progress_callback;
4633 data->progress_cb_data = progress_callback_data;
4635 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4636 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4638 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4642 g_file_real_copy_finish (GFile *file,
4646 /* Error handled in g_file_copy_finish() */
4651 /********************************************
4652 * Default VFS operations *
4653 ********************************************/
4656 * g_file_new_for_path:
4657 * @path: a string containing a relative or absolute path.
4659 * Constructs a #GFile for a given path. This operation never
4660 * fails, but the returned object might not support any I/O
4661 * operation if @path is malformed.
4663 * Returns: a new #GFile for the given @path.
4666 g_file_new_for_path (const char *path)
4668 g_return_val_if_fail (path != NULL, NULL);
4670 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4674 * g_file_new_for_uri:
4675 * @uri: a string containing a URI.
4677 * Constructs a #GFile for a given URI. This operation never
4678 * fails, but the returned object might not support any I/O
4679 * operation if @uri is malformed or if the uri type is
4682 * Returns: a #GFile for the given @uri.
4685 g_file_new_for_uri (const char *uri)
4687 g_return_val_if_fail (uri != NULL, NULL);
4689 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4693 * g_file_parse_name:
4694 * @parse_name: a file name or path to be parsed.
4696 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4697 * This operation never fails, but the returned object might not support any I/O
4698 * operation if the @parse_name cannot be parsed.
4700 * Returns: a new #GFile.
4703 g_file_parse_name (const char *parse_name)
4705 g_return_val_if_fail (parse_name != NULL, NULL);
4707 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4711 is_valid_scheme_character (char c)
4713 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4717 has_valid_scheme (const char *uri)
4723 if (!is_valid_scheme_character (*p))
4728 } while (is_valid_scheme_character (*p));
4734 * g_file_new_for_commandline_arg:
4735 * @arg: a command line string.
4737 * Creates a #GFile with the given argument from the command line. The value of
4738 * @arg can be either a URI, an absolute path or a relative path resolved
4739 * relative to the current working directory.
4740 * This operation never fails, but the returned object might not support any
4741 * I/O operation if @arg points to a malformed path.
4743 * Returns: a new #GFile.
4746 g_file_new_for_commandline_arg (const char *arg)
4752 g_return_val_if_fail (arg != NULL, NULL);
4754 if (g_path_is_absolute (arg))
4755 return g_file_new_for_path (arg);
4757 if (has_valid_scheme (arg))
4758 return g_file_new_for_uri (arg);
4760 current_dir = g_get_current_dir ();
4761 filename = g_build_filename (current_dir, arg, NULL);
4762 g_free (current_dir);
4764 file = g_file_new_for_path (filename);
4771 * g_file_mount_enclosing_volume:
4772 * @location: input #GFile.
4773 * @flags: flags affecting the operation
4774 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4775 * @cancellable: optional #GCancellable object, %NULL to ignore.
4776 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4777 * @user_data: the data to pass to callback function
4779 * Starts a @mount_operation, mounting the volume that contains the file @location.
4781 * When this operation has completed, @callback will be called with
4782 * @user_user data, and the operation can be finalized with
4783 * g_file_mount_enclosing_volume_finish().
4785 * If @cancellable is not %NULL, then the operation can be cancelled by
4786 * triggering the cancellable object from another thread. If the operation
4787 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4790 g_file_mount_enclosing_volume (GFile *location,
4791 GMountMountFlags flags,
4792 GMountOperation *mount_operation,
4793 GCancellable *cancellable,
4794 GAsyncReadyCallback callback,
4799 g_return_if_fail (G_IS_FILE (location));
4801 iface = G_FILE_GET_IFACE (location);
4803 if (iface->mount_enclosing_volume == NULL)
4805 g_simple_async_report_error_in_idle (G_OBJECT (location),
4806 callback, user_data,
4807 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4808 _("volume doesn't implement mount"));
4813 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4818 * g_file_mount_enclosing_volume_finish:
4819 * @location: input #GFile.
4820 * @result: a #GAsyncResult.
4821 * @error: a #GError, or %NULL
4823 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4825 * Returns: %TRUE if successful. If an error
4826 * has occurred, this function will return %FALSE and set @error
4827 * appropriately if present.
4830 g_file_mount_enclosing_volume_finish (GFile *location,
4831 GAsyncResult *result,
4836 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4837 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4839 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4841 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4842 if (g_simple_async_result_propagate_error (simple, error))
4846 iface = G_FILE_GET_IFACE (location);
4848 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4851 /********************************************
4852 * Utility functions *
4853 ********************************************/
4856 * g_file_query_default_handler:
4857 * @file: a #GFile to open.
4858 * @cancellable: optional #GCancellable object, %NULL to ignore.
4859 * @error: a #GError, or %NULL
4861 * Returns the #GAppInfo that is registered as the default
4862 * application to handle the file specified by @file.
4864 * If @cancellable is not %NULL, then the operation can be cancelled by
4865 * triggering the cancellable object from another thread. If the operation
4866 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4868 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4869 * When you are done with it, release it with g_object_unref()
4872 g_file_query_default_handler (GFile *file,
4873 GCancellable *cancellable,
4877 const char *content_type;
4882 uri_scheme = g_file_get_uri_scheme (file);
4883 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4884 g_free (uri_scheme);
4886 if (appinfo != NULL)
4889 info = g_file_query_info (file,
4890 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4899 content_type = g_file_info_get_content_type (info);
4902 /* Don't use is_native(), as we want to support fuse paths if availible */
4903 path = g_file_get_path (file);
4904 appinfo = g_app_info_get_default_for_type (content_type,
4909 g_object_unref (info);
4911 if (appinfo != NULL)
4914 g_set_error (error, G_IO_ERROR,
4915 G_IO_ERROR_NOT_SUPPORTED,
4916 _("No application is registered as handling this file"));
4922 #define GET_CONTENT_BLOCK_SIZE 8192
4925 * g_file_load_contents:
4926 * @file: input #GFile.
4927 * @cancellable: optional #GCancellable object, %NULL to ignore.
4928 * @contents: a location to place the contents of the file.
4929 * @length: a location to place the length of the contents of the file.
4930 * @etag_out: a location to place the current entity tag for the file.
4931 * @error: a #GError, or %NULL
4933 * Loads the content of the file into memory, returning the size of
4934 * the data. The data is always zero terminated, but this is not
4935 * included in the resultant @length.
4937 * If @cancellable is not %NULL, then the operation can be cancelled by
4938 * triggering the cancellable object from another thread. If the operation
4939 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4941 * Returns: %TRUE if the @file's contents were successfully loaded.
4942 * %FALSE if there were errors..
4945 g_file_load_contents (GFile *file,
4946 GCancellable *cancellable,
4952 GFileInputStream *in;
4953 GByteArray *content;
4958 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4959 g_return_val_if_fail (contents != NULL, FALSE);
4961 in = g_file_read (file, cancellable, error);
4965 content = g_byte_array_new ();
4968 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4969 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4970 content->data + pos,
4971 GET_CONTENT_BLOCK_SIZE,
4972 cancellable, error)) > 0)
4975 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4982 info = g_file_input_stream_query_info (in,
4983 G_FILE_ATTRIBUTE_ETAG_VALUE,
4988 *etag_out = g_strdup (g_file_info_get_etag (info));
4989 g_object_unref (info);
4993 /* Ignore errors on close */
4994 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4995 g_object_unref (in);
4999 /* error is set already */
5000 g_byte_array_free (content, TRUE);
5007 /* Zero terminate (we got an extra byte allocated for this */
5008 content->data[pos] = 0;
5010 *contents = (char *)g_byte_array_free (content, FALSE);
5018 GCancellable *cancellable;
5019 GFileReadMoreCallback read_more_callback;
5020 GAsyncReadyCallback callback;
5022 GByteArray *content;
5029 load_contents_data_free (LoadContentsData *data)
5032 g_error_free (data->error);
5033 if (data->cancellable)
5034 g_object_unref (data->cancellable);
5036 g_byte_array_free (data->content, TRUE);
5037 g_free (data->etag);
5038 g_object_unref (data->file);
5043 load_contents_close_callback (GObject *obj,
5044 GAsyncResult *close_res,
5047 GInputStream *stream = G_INPUT_STREAM (obj);
5048 LoadContentsData *data = user_data;
5049 GSimpleAsyncResult *res;
5051 /* Ignore errors here, we're only reading anyway */
5052 g_input_stream_close_finish (stream, close_res, NULL);
5053 g_object_unref (stream);
5055 res = g_simple_async_result_new (G_OBJECT (data->file),
5058 g_file_load_contents_async);
5059 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5060 g_simple_async_result_complete (res);
5061 g_object_unref (res);
5065 load_contents_fstat_callback (GObject *obj,
5066 GAsyncResult *stat_res,
5069 GInputStream *stream = G_INPUT_STREAM (obj);
5070 LoadContentsData *data = user_data;
5073 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5077 data->etag = g_strdup (g_file_info_get_etag (info));
5078 g_object_unref (info);
5081 g_input_stream_close_async (stream, 0,
5083 load_contents_close_callback, data);
5087 load_contents_read_callback (GObject *obj,
5088 GAsyncResult *read_res,
5091 GInputStream *stream = G_INPUT_STREAM (obj);
5092 LoadContentsData *data = user_data;
5093 GError *error = NULL;
5096 read_size = g_input_stream_read_finish (stream, read_res, &error);
5100 /* Error or EOF, close the file */
5101 data->error = error;
5102 g_input_stream_close_async (stream, 0,
5104 load_contents_close_callback, data);
5106 else if (read_size == 0)
5108 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5109 G_FILE_ATTRIBUTE_ETAG_VALUE,
5112 load_contents_fstat_callback,
5115 else if (read_size > 0)
5117 data->pos += read_size;
5119 g_byte_array_set_size (data->content,
5120 data->pos + GET_CONTENT_BLOCK_SIZE);
5123 if (data->read_more_callback &&
5124 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5125 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5126 G_FILE_ATTRIBUTE_ETAG_VALUE,
5129 load_contents_fstat_callback,
5132 g_input_stream_read_async (stream,
5133 data->content->data + data->pos,
5134 GET_CONTENT_BLOCK_SIZE,
5137 load_contents_read_callback,
5143 load_contents_open_callback (GObject *obj,
5144 GAsyncResult *open_res,
5147 GFile *file = G_FILE (obj);
5148 GFileInputStream *stream;
5149 LoadContentsData *data = user_data;
5150 GError *error = NULL;
5151 GSimpleAsyncResult *res;
5153 stream = g_file_read_finish (file, open_res, &error);
5157 g_byte_array_set_size (data->content,
5158 data->pos + GET_CONTENT_BLOCK_SIZE);
5159 g_input_stream_read_async (G_INPUT_STREAM (stream),
5160 data->content->data + data->pos,
5161 GET_CONTENT_BLOCK_SIZE,
5164 load_contents_read_callback,
5170 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5174 g_simple_async_result_complete (res);
5175 g_error_free (error);
5176 load_contents_data_free (data);
5177 g_object_unref (res);
5182 * g_file_load_partial_contents_async:
5183 * @file: input #GFile.
5184 * @cancellable: optional #GCancellable object, %NULL to ignore.
5185 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5186 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5187 * @user_data: the data to pass to the callback functions.
5189 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5190 * used to stop reading from the file when appropriate, else this function
5191 * will behave exactly as g_file_load_contents_async(). This operation
5192 * can be finished by g_file_load_partial_contents_finish().
5194 * Users of this function should be aware that @user_data is passed to
5195 * both the @read_more_callback and the @callback.
5197 * If @cancellable is not %NULL, then the operation can be cancelled by
5198 * triggering the cancellable object from another thread. If the operation
5199 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5202 g_file_load_partial_contents_async (GFile *file,
5203 GCancellable *cancellable,
5204 GFileReadMoreCallback read_more_callback,
5205 GAsyncReadyCallback callback,
5208 LoadContentsData *data;
5210 g_return_if_fail (G_IS_FILE (file));
5212 data = g_new0 (LoadContentsData, 1);
5215 data->cancellable = g_object_ref (cancellable);
5216 data->read_more_callback = read_more_callback;
5217 data->callback = callback;
5218 data->user_data = user_data;
5219 data->content = g_byte_array_new ();
5220 data->file = g_object_ref (file);
5222 g_file_read_async (file,
5225 load_contents_open_callback,
5230 * g_file_load_partial_contents_finish:
5231 * @file: input #GFile.
5232 * @res: a #GAsyncResult.
5233 * @contents: a location to place the contents of the file.
5234 * @length: a location to place the length of the contents of the file.
5235 * @etag_out: a location to place the current entity tag for the file.
5236 * @error: a #GError, or %NULL
5238 * Finishes an asynchronous partial load operation that was started
5239 * with g_file_load_partial_contents_async().
5241 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5242 * present, it will be set appropriately.
5245 g_file_load_partial_contents_finish (GFile *file,
5252 GSimpleAsyncResult *simple;
5253 LoadContentsData *data;
5255 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5256 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5257 g_return_val_if_fail (contents != NULL, FALSE);
5259 simple = G_SIMPLE_ASYNC_RESULT (res);
5261 if (g_simple_async_result_propagate_error (simple, error))
5264 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5266 data = g_simple_async_result_get_op_res_gpointer (simple);
5270 g_propagate_error (error, data->error);
5279 *length = data->pos;
5283 *etag_out = data->etag;
5287 /* Zero terminate */
5288 g_byte_array_set_size (data->content, data->pos + 1);
5289 data->content->data[data->pos] = 0;
5291 *contents = (char *)g_byte_array_free (data->content, FALSE);
5292 data->content = NULL;
5298 * g_file_load_contents_async:
5299 * @file: input #GFile.
5300 * @cancellable: optional #GCancellable object, %NULL to ignore.
5301 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5302 * @user_data: the data to pass to callback function
5304 * Starts an asynchronous load of the @file's contents.
5306 * For more details, see g_file_load_contents() which is
5307 * the synchronous version of this call.
5309 * When the load operation has completed, @callback will be called
5310 * with @user data. To finish the operation, call
5311 * g_file_load_contents_finish() with the #GAsyncResult returned by
5314 * If @cancellable is not %NULL, then the operation can be cancelled by
5315 * triggering the cancellable object from another thread. If the operation
5316 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5319 g_file_load_contents_async (GFile *file,
5320 GCancellable *cancellable,
5321 GAsyncReadyCallback callback,
5324 g_file_load_partial_contents_async (file,
5327 callback, user_data);
5331 * g_file_load_contents_finish:
5332 * @file: input #GFile.
5333 * @res: a #GAsyncResult.
5334 * @contents: a location to place the contents of the file.
5335 * @length: a location to place the length of the contents of the file.
5336 * @etag_out: a location to place the current entity tag for the file.
5337 * @error: a #GError, or %NULL
5339 * Finishes an asynchronous load of the @file's contents.
5340 * The contents are placed in @contents, and @length is set to the
5341 * size of the @contents string. If @etag_out is present, it will be
5342 * set to the new entity tag for the @file.
5344 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5345 * present, it will be set appropriately.
5348 g_file_load_contents_finish (GFile *file,
5355 return g_file_load_partial_contents_finish (file,
5364 * g_file_replace_contents:
5365 * @file: input #GFile.
5366 * @contents: a string containing the new contents for @file.
5367 * @length: the length of @contents in bytes.
5368 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5370 * @make_backup: %TRUE if a backup should be created.
5371 * @flags: a set of #GFileCreateFlags.
5372 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5373 * for the document. This should be freed with g_free() when no longer
5375 * @cancellable: optional #GCancellable object, %NULL to ignore.
5376 * @error: a #GError, or %NULL
5378 * Replaces the contents of @file with @contents of @length bytes.
5380 * If @etag is specified (not %NULL) any existing file must have that etag, or
5381 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5383 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5385 * If @cancellable is not %NULL, then the operation can be cancelled by
5386 * triggering the cancellable object from another thread. If the operation
5387 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5389 * The returned @new_etag can be used to verify that the file hasn't changed the
5390 * next time it is saved over.
5392 * Returns: %TRUE if successful. If an error
5393 * has occurred, this function will return %FALSE and set @error
5394 * appropriately if present.
5397 g_file_replace_contents (GFile *file,
5398 const char *contents,
5401 gboolean make_backup,
5402 GFileCreateFlags flags,
5404 GCancellable *cancellable,
5407 GFileOutputStream *out;
5408 gsize pos, remainder;
5411 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5412 g_return_val_if_fail (contents != NULL, FALSE);
5414 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5420 while (remainder > 0 &&
5421 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5423 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5431 if (remainder > 0 && res < 0)
5433 /* Ignore errors on close */
5434 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5436 /* error is set already */
5440 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5444 *new_etag = g_file_output_stream_get_etag (out);
5452 GCancellable *cancellable;
5453 GAsyncReadyCallback callback;
5455 const char *content;
5459 } ReplaceContentsData;
5462 replace_contents_data_free (ReplaceContentsData *data)
5465 g_error_free (data->error);
5466 if (data->cancellable)
5467 g_object_unref (data->cancellable);
5468 g_object_unref (data->file);
5469 g_free (data->etag);
5474 replace_contents_close_callback (GObject *obj,
5475 GAsyncResult *close_res,
5478 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5479 ReplaceContentsData *data = user_data;
5480 GSimpleAsyncResult *res;
5482 /* Ignore errors here, we're only reading anyway */
5483 g_output_stream_close_finish (stream, close_res, NULL);
5484 g_object_unref (stream);
5486 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5488 res = g_simple_async_result_new (G_OBJECT (data->file),
5491 g_file_replace_contents_async);
5492 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5493 g_simple_async_result_complete (res);
5494 g_object_unref (res);
5498 replace_contents_write_callback (GObject *obj,
5499 GAsyncResult *read_res,
5502 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5503 ReplaceContentsData *data = user_data;
5504 GError *error = NULL;
5507 write_size = g_output_stream_write_finish (stream, read_res, &error);
5509 if (write_size <= 0)
5511 /* Error or EOF, close the file */
5513 data->error = error;
5514 g_output_stream_close_async (stream, 0,
5516 replace_contents_close_callback, data);
5518 else if (write_size > 0)
5520 data->pos += write_size;
5522 if (data->pos >= data->length)
5523 g_output_stream_close_async (stream, 0,
5525 replace_contents_close_callback, data);
5527 g_output_stream_write_async (stream,
5528 data->content + data->pos,
5529 data->length - data->pos,
5532 replace_contents_write_callback,
5538 replace_contents_open_callback (GObject *obj,
5539 GAsyncResult *open_res,
5542 GFile *file = G_FILE (obj);
5543 GFileOutputStream *stream;
5544 ReplaceContentsData *data = user_data;
5545 GError *error = NULL;
5546 GSimpleAsyncResult *res;
5548 stream = g_file_replace_finish (file, open_res, &error);
5552 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5553 data->content + data->pos,
5554 data->length - data->pos,
5557 replace_contents_write_callback,
5563 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5567 g_simple_async_result_complete (res);
5568 g_error_free (error);
5569 replace_contents_data_free (data);
5570 g_object_unref (res);
5575 * g_file_replace_contents_async:
5576 * @file: input #GFile.
5577 * @contents: string of contents to replace the file with.
5578 * @length: the length of @contents in bytes.
5579 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5580 * @make_backup: %TRUE if a backup should be created.
5581 * @flags: a set of #GFileCreateFlags.
5582 * @cancellable: optional #GCancellable object, %NULL to ignore.
5583 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5584 * @user_data: the data to pass to callback function
5586 * Starts an asynchronous replacement of @file with the given
5587 * @contents of @length bytes. @etag will replace the document's
5588 * current entity tag.
5590 * When this operation has completed, @callback will be called with
5591 * @user_user data, and the operation can be finalized with
5592 * g_file_replace_contents_finish().
5594 * If @cancellable is not %NULL, then the operation can be cancelled by
5595 * triggering the cancellable object from another thread. If the operation
5596 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5598 * If @make_backup is %TRUE, this function will attempt to
5599 * make a backup of @file.
5602 g_file_replace_contents_async (GFile *file,
5603 const char *contents,
5606 gboolean make_backup,
5607 GFileCreateFlags flags,
5608 GCancellable *cancellable,
5609 GAsyncReadyCallback callback,
5612 ReplaceContentsData *data;
5614 g_return_if_fail (G_IS_FILE (file));
5615 g_return_if_fail (contents != NULL);
5617 data = g_new0 (ReplaceContentsData, 1);
5620 data->cancellable = g_object_ref (cancellable);
5621 data->callback = callback;
5622 data->user_data = user_data;
5623 data->content = contents;
5624 data->length = length;
5626 data->file = g_object_ref (file);
5628 g_file_replace_async (file,
5634 replace_contents_open_callback,
5639 * g_file_replace_contents_finish:
5640 * @file: input #GFile.
5641 * @res: a #GAsyncResult.
5642 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5643 * for the document. This should be freed with g_free() when it is no
5645 * @error: a #GError, or %NULL
5647 * Finishes an asynchronous replace of the given @file. See
5648 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5649 * tag for the document, if present.
5651 * Returns: %TRUE on success, %FALSE on failure.
5654 g_file_replace_contents_finish (GFile *file,
5659 GSimpleAsyncResult *simple;
5660 ReplaceContentsData *data;
5662 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5663 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5665 simple = G_SIMPLE_ASYNC_RESULT (res);
5667 if (g_simple_async_result_propagate_error (simple, error))
5670 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5672 data = g_simple_async_result_get_op_res_gpointer (simple);
5676 g_propagate_error (error, data->error);
5684 *new_etag = data->etag;
5685 data->etag = NULL; /* Take ownership */
5691 #define __G_FILE_C__
5692 #include "gioaliasdef.c"