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,
938 g_object_unref (info);
947 * @file: input #GFile.
948 * @attributes: an attribute query string.
949 * @flags: a set of #GFileQueryInfoFlags.
950 * @cancellable: optional #GCancellable object, %NULL to ignore.
953 * Gets the requested information about specified @file. The result
954 * is a #GFileInfo object that contains key-value attributes (such as
955 * the type or size of the file).
957 * The @attribute value is a string that specifies the file attributes that
958 * should be gathered. It is not an error if it's not possible to read a particular
959 * requested attribute from a file - it just won't be set. @attribute should
960 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
961 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
962 * namespace. An example attribute query be "standard::*,owner::user".
963 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
965 * If @cancellable is not %NULL, then the operation can be cancelled by
966 * triggering the cancellable object from another thread. If the operation
967 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
969 * For symlinks, normally the information about the target of the
970 * symlink is returned, rather than information about the symlink itself.
971 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
972 * information about the symlink itself will be returned. Also, for symlinks
973 * that point to non-existing files the information about the symlink itself
976 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
977 * Other errors are possible too, and depend on what kind of filesystem the file is on.
979 * Returns: a #GFileInfo for the given @file, or %NULL on error.
982 g_file_query_info (GFile *file,
983 const char *attributes,
984 GFileQueryInfoFlags flags,
985 GCancellable *cancellable,
990 g_return_val_if_fail (G_IS_FILE (file), NULL);
992 if (g_cancellable_set_error_if_cancelled (cancellable, error))
995 iface = G_FILE_GET_IFACE (file);
997 if (iface->query_info == NULL)
999 g_set_error (error, G_IO_ERROR,
1000 G_IO_ERROR_NOT_SUPPORTED,
1001 _("Operation not supported"));
1005 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1009 * g_file_query_info_async:
1010 * @file: input #GFile.
1011 * @attributes: an attribute query string.
1012 * @flags: a set of #GFileQueryInfoFlags.
1013 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1015 * @cancellable: optional #GCancellable object, %NULL to ignore.
1016 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1017 * @user_data: the data to pass to callback function
1019 * Asynchronously gets the requested information about specified @file. The result
1020 * is a #GFileInfo object that contains key-value attributes (such as type or size
1023 * For more details, see g_file_query_info() which is
1024 * the synchronous version of this call.
1026 * When the operation is finished, @callback will be called. You can then call
1027 * g_file_query_info_finish() to get the result of the operation.
1030 g_file_query_info_async (GFile *file,
1031 const char *attributes,
1032 GFileQueryInfoFlags flags,
1034 GCancellable *cancellable,
1035 GAsyncReadyCallback callback,
1040 g_return_if_fail (G_IS_FILE (file));
1042 iface = G_FILE_GET_IFACE (file);
1043 (* iface->query_info_async) (file,
1053 * g_file_query_info_finish:
1054 * @file: input #GFile.
1055 * @res: a #GAsyncResult.
1056 * @error: a #GError.
1058 * Finishes an asynchronous file info query.
1059 * See g_file_query_info_async().
1061 * Returns: #GFileInfo for given @file or %NULL on error.
1064 g_file_query_info_finish (GFile *file,
1070 g_return_val_if_fail (G_IS_FILE (file), NULL);
1071 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1073 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1075 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1076 if (g_simple_async_result_propagate_error (simple, error))
1080 iface = G_FILE_GET_IFACE (file);
1081 return (* iface->query_info_finish) (file, res, error);
1085 * g_file_query_filesystem_info:
1086 * @file: input #GFile.
1087 * @attributes: an attribute query string.
1088 * @cancellable: optional #GCancellable object, %NULL to ignore.
1089 * @error: a #GError.
1091 * Similar to g_file_query_info(), but obtains information
1092 * about the filesystem the @file is on, rather than the file itself.
1093 * For instance the amount of space available and the type of
1096 * The @attribute value is a string that specifies the file attributes that
1097 * should be gathered. It is not an error if it's not possible to read a particular
1098 * requested attribute from a file - it just won't be set. @attribute should
1099 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1100 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1101 * namespace. The standard namespace for filesystem attributes is "fs".
1102 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1103 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1104 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1106 * If @cancellable is not %NULL, then the operation can be cancelled by
1107 * triggering the cancellable object from another thread. If the operation
1108 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1110 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1111 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1113 * Returns: a #GFileInfo or %NULL if there was an error.
1116 g_file_query_filesystem_info (GFile *file,
1117 const char *attributes,
1118 GCancellable *cancellable,
1123 g_return_val_if_fail (G_IS_FILE (file), NULL);
1125 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1128 iface = G_FILE_GET_IFACE (file);
1130 if (iface->query_filesystem_info == NULL)
1132 g_set_error (error, G_IO_ERROR,
1133 G_IO_ERROR_NOT_SUPPORTED,
1134 _("Operation not supported"));
1138 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1142 * g_file_query_filesystem_info_async:
1143 * @file: input #GFile.
1144 * @attributes: an attribute query string.
1145 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1147 * @cancellable: optional #GCancellable object, %NULL to ignore.
1148 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1149 * @user_data: the data to pass to callback function
1151 * Asynchronously gets the requested information about the filesystem
1152 * that the specified @file is on. The result is a #GFileInfo object
1153 * that contains key-value attributes (such as type or size for the
1156 * For more details, see g_file_query_filesystem_info() which is the
1157 * synchronous version of this call.
1159 * When the operation is finished, @callback will be called. You can
1160 * then call g_file_query_info_finish() to get the result of the
1164 g_file_query_filesystem_info_async (GFile *file,
1165 const char *attributes,
1167 GCancellable *cancellable,
1168 GAsyncReadyCallback callback,
1173 g_return_if_fail (G_IS_FILE (file));
1175 iface = G_FILE_GET_IFACE (file);
1176 (* iface->query_filesystem_info_async) (file,
1185 * g_file_query_filesystem_info_finish:
1186 * @file: input #GFile.
1187 * @res: a #GAsyncResult.
1188 * @error: a #GError.
1190 * Finishes an asynchronous filesystem info query. See
1191 * g_file_query_filesystem_info_async().
1193 * Returns: #GFileInfo for given @file or %NULL on error.
1196 g_file_query_filesystem_info_finish (GFile *file,
1202 g_return_val_if_fail (G_IS_FILE (file), NULL);
1203 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1205 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1207 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1208 if (g_simple_async_result_propagate_error (simple, error))
1212 iface = G_FILE_GET_IFACE (file);
1213 return (* iface->query_filesystem_info_finish) (file, res, error);
1217 * g_file_find_enclosing_mount:
1218 * @file: input #GFile.
1219 * @cancellable: optional #GCancellable object, %NULL to ignore.
1220 * @error: a #GError.
1222 * Gets a #GMount for the #GFile.
1224 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1225 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1228 * If @cancellable is not %NULL, then the operation can be cancelled by
1229 * triggering the cancellable object from another thread. If the operation
1230 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1232 * Returns: a #GMount where the @file is located or %NULL on error.
1235 g_file_find_enclosing_mount (GFile *file,
1236 GCancellable *cancellable,
1241 g_return_val_if_fail (G_IS_FILE (file), NULL);
1243 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1246 iface = G_FILE_GET_IFACE (file);
1247 if (iface->find_enclosing_mount == NULL)
1250 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1251 /* Translators: This is an error message when trying to find the
1252 * enclosing (user visible) mount of a file, but none exists. */
1253 _("Containing mount does not exist"));
1257 return (* iface->find_enclosing_mount) (file, cancellable, error);
1260 * g_file_find_enclosing_mount_async:
1262 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1264 * @cancellable: optional #GCancellable object, %NULL to ignore.
1265 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1266 * @user_data: the data to pass to callback function
1268 * Asynchronously gets the mount for the file.
1270 * For more details, see g_file_find_enclosing_mount() which is
1271 * the synchronous version of this call.
1273 * When the operation is finished, @callback will be called. You can then call
1274 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1277 g_file_find_enclosing_mount_async (GFile *file,
1279 GCancellable *cancellable,
1280 GAsyncReadyCallback callback,
1285 g_return_if_fail (G_IS_FILE (file));
1287 iface = G_FILE_GET_IFACE (file);
1288 (* iface->find_enclosing_mount_async) (file,
1296 * g_file_find_enclosing_mount_finish:
1298 * @res: a #GAsyncResult
1301 * Finishes an asynchronous find mount request.
1302 * See g_file_find_enclosing_mount_async().
1304 * Returns: #GMount for given @file or %NULL on error.
1307 g_file_find_enclosing_mount_finish (GFile *file,
1313 g_return_val_if_fail (G_IS_FILE (file), NULL);
1314 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1316 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1318 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1319 if (g_simple_async_result_propagate_error (simple, error))
1323 iface = G_FILE_GET_IFACE (file);
1324 return (* iface->find_enclosing_mount_finish) (file, res, error);
1330 * @file: #GFile to read.
1331 * @cancellable: a #GCancellable
1332 * @error: a #GError, or %NULL
1334 * Opens a file for reading. The result is a #GFileInputStream that
1335 * can be used to read the contents of the file.
1337 * If @cancellable is not %NULL, then the operation can be cancelled by
1338 * triggering the cancellable object from another thread. If the operation
1339 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1341 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1342 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1343 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1345 * Returns: #GFileInputStream or %NULL on error.
1348 g_file_read (GFile *file,
1349 GCancellable *cancellable,
1354 g_return_val_if_fail (G_IS_FILE (file), NULL);
1356 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1359 iface = G_FILE_GET_IFACE (file);
1361 if (iface->read_fn == NULL)
1363 g_set_error (error, G_IO_ERROR,
1364 G_IO_ERROR_NOT_SUPPORTED,
1365 _("Operation not supported"));
1369 return (* iface->read_fn) (file, cancellable, error);
1374 * @file: input #GFile.
1375 * @flags: a set of #GFileCreateFlags.
1376 * @cancellable: optional #GCancellable object, %NULL to ignore.
1377 * @error: a #GError, or %NULL
1379 * Gets an output stream for appending data to the file. If
1380 * the file doesn't already exist it is created.
1382 * By default files created are generally readable by everyone,
1383 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1384 * will be made readable only to the current user, to the level that
1385 * is supported on the target filesystem.
1387 * If @cancellable is not %NULL, then the operation can be cancelled by
1388 * triggering the cancellable object from another thread. If the operation
1389 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1391 * Some file systems don't allow all file names, and may
1392 * return an G_IO_ERROR_INVALID_FILENAME error.
1393 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1394 * returned. Other errors are possible too, and depend on what kind of
1395 * filesystem the file is on.
1397 * Returns: a #GFileOutputStream.
1400 g_file_append_to (GFile *file,
1401 GFileCreateFlags flags,
1402 GCancellable *cancellable,
1407 g_return_val_if_fail (G_IS_FILE (file), NULL);
1409 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1412 iface = G_FILE_GET_IFACE (file);
1414 if (iface->append_to == NULL)
1416 g_set_error (error, G_IO_ERROR,
1417 G_IO_ERROR_NOT_SUPPORTED,
1418 _("Operation not supported"));
1422 return (* iface->append_to) (file, flags, cancellable, error);
1427 * @file: input #GFile.
1428 * @flags: a set of #GFileCreateFlags.
1429 * @cancellable: optional #GCancellable object, %NULL to ignore.
1430 * @error: a #GError, or %NULL
1432 * Creates a new file and returns an output stream for writing to it.
1433 * The file must not already exists.
1435 * By default files created are generally readable by everyone,
1436 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1437 * will be made readable only to the current user, to the level that
1438 * is supported on the target filesystem.
1440 * If @cancellable is not %NULL, then the operation can be cancelled by
1441 * triggering the cancellable object from another thread. If the operation
1442 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1444 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1445 * error will be returned.
1446 * Some file systems don't allow all file names, and may
1447 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1448 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1449 * Other errors are possible too, and depend on what kind of
1450 * filesystem the file is on.
1452 * Returns: a #GFileOutputStream for the newly created file, or
1456 g_file_create (GFile *file,
1457 GFileCreateFlags flags,
1458 GCancellable *cancellable,
1463 g_return_val_if_fail (G_IS_FILE (file), NULL);
1465 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1468 iface = G_FILE_GET_IFACE (file);
1470 if (iface->create == NULL)
1472 g_set_error (error, G_IO_ERROR,
1473 G_IO_ERROR_NOT_SUPPORTED,
1474 _("Operation not supported"));
1478 return (* iface->create) (file, flags, cancellable, error);
1483 * @file: input #GFile.
1484 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1485 * current #GFile, or #NULL to ignore.
1486 * @make_backup: %TRUE if a backup should be created.
1487 * @flags: a set of #GFileCreateFlags.
1488 * @cancellable: optional #GCancellable object, %NULL to ignore.
1489 * @error: a #GError, or %NULL
1491 * Returns an output stream for overwriting the file, possibly
1492 * creating a backup copy of the file first.
1494 * This will try to replace the file in the safest way possible so
1495 * that any errors during the writing will not affect an already
1496 * existing copy of the file. For instance, for local files it
1497 * may write to a temporary file and then atomically rename over
1498 * the destination when the stream is closed.
1500 * By default files created are generally readable by everyone,
1501 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1502 * will be made readable only to the current user, to the level that
1503 * is supported on the target filesystem.
1505 * If @cancellable is not %NULL, then the operation can be cancelled by
1506 * triggering the cancellable object from another thread. If the operation
1507 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1509 * If you pass in a non-#NULL @etag value, then this value is
1510 * compared to the current entity tag of the file, and if they differ
1511 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1512 * that the file has been changed since you last read it. You can get
1513 * the new etag from g_file_output_stream_get_etag() after you've
1514 * finished writing and closed the #GFileOutputStream. When you load
1515 * a new file you can use g_file_input_stream_query_info() to get
1516 * the etag of the file.
1518 * If @make_backup is %TRUE, this function will attempt to make a backup
1519 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1520 * error will be returned. If you want to replace anyway, try again with
1521 * @make_backup set to %FALSE.
1523 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1524 * and if the file is some other form of non-regular file then a
1525 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1526 * Some file systems don't allow all file names, and may
1527 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1528 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1529 * Other errors are possible too, and depend on what kind of
1530 * filesystem the file is on.
1532 * Returns: a #GFileOutputStream or %NULL on error.
1535 g_file_replace (GFile *file,
1537 gboolean make_backup,
1538 GFileCreateFlags flags,
1539 GCancellable *cancellable,
1544 g_return_val_if_fail (G_IS_FILE (file), NULL);
1546 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1549 iface = G_FILE_GET_IFACE (file);
1551 if (iface->replace == NULL)
1553 g_set_error (error, G_IO_ERROR,
1554 G_IO_ERROR_NOT_SUPPORTED,
1555 _("Operation not supported"));
1560 /* Handle empty tag string as NULL in consistent way. */
1561 if (etag && *etag == 0)
1564 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1568 * g_file_read_async:
1569 * @file: input #GFile.
1570 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1572 * @cancellable: optional #GCancellable object, %NULL to ignore.
1573 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1574 * @user_data: the data to pass to callback function
1576 * Asynchronously opens @file for reading.
1578 * For more details, see g_file_read() which is
1579 * the synchronous version of this call.
1581 * When the operation is finished, @callback will be called. You can then call
1582 * g_file_read_finish() to get the result of the operation.
1585 g_file_read_async (GFile *file,
1587 GCancellable *cancellable,
1588 GAsyncReadyCallback callback,
1593 g_return_if_fail (G_IS_FILE (file));
1595 iface = G_FILE_GET_IFACE (file);
1596 (* iface->read_async) (file,
1604 * g_file_read_finish:
1605 * @file: input #GFile.
1606 * @res: a #GAsyncResult.
1607 * @error: a #GError, or %NULL
1609 * Finishes an asynchronous file read operation started with
1610 * g_file_read_async().
1612 * Returns: a #GFileInputStream or %NULL on error.
1615 g_file_read_finish (GFile *file,
1621 g_return_val_if_fail (G_IS_FILE (file), NULL);
1622 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1624 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1626 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1627 if (g_simple_async_result_propagate_error (simple, error))
1631 iface = G_FILE_GET_IFACE (file);
1632 return (* iface->read_finish) (file, res, error);
1636 * g_file_append_to_async:
1637 * @file: input #GFile.
1638 * @flags: a set of #GFileCreateFlags.
1639 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1641 * @cancellable: optional #GCancellable object, %NULL to ignore.
1642 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1643 * @user_data: the data to pass to callback function
1645 * Asynchronously opens @file for appending.
1647 * For more details, see g_file_append_to() which is
1648 * the synchronous version of this call.
1650 * When the operation is finished, @callback will be called. You can then call
1651 * g_file_append_to_finish() to get the result of the operation.
1654 g_file_append_to_async (GFile *file,
1655 GFileCreateFlags flags,
1657 GCancellable *cancellable,
1658 GAsyncReadyCallback callback,
1663 g_return_if_fail (G_IS_FILE (file));
1665 iface = G_FILE_GET_IFACE (file);
1666 (* iface->append_to_async) (file,
1675 * g_file_append_to_finish:
1676 * @file: input #GFile.
1677 * @res: #GAsyncResult
1678 * @error: a #GError, or %NULL
1680 * Finishes an asynchronous file append operation started with
1681 * g_file_append_to_async().
1683 * Returns: a valid #GFileOutputStream or %NULL on error.
1686 g_file_append_to_finish (GFile *file,
1692 g_return_val_if_fail (G_IS_FILE (file), NULL);
1693 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1695 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1697 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1698 if (g_simple_async_result_propagate_error (simple, error))
1702 iface = G_FILE_GET_IFACE (file);
1703 return (* iface->append_to_finish) (file, res, error);
1707 * g_file_create_async:
1708 * @file: input #GFile.
1709 * @flags: a set of #GFileCreateFlags.
1710 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1712 * @cancellable: optional #GCancellable object, %NULL to ignore.
1713 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1714 * @user_data: the data to pass to callback function
1716 * Asynchronously creates a new file and returns an output stream for writing to it.
1717 * The file must not already exists.
1719 * For more details, see g_file_create() which is
1720 * the synchronous version of this call.
1722 * When the operation is finished, @callback will be called. You can then call
1723 * g_file_create_finish() to get the result of the operation.
1726 g_file_create_async (GFile *file,
1727 GFileCreateFlags flags,
1729 GCancellable *cancellable,
1730 GAsyncReadyCallback callback,
1735 g_return_if_fail (G_IS_FILE (file));
1737 iface = G_FILE_GET_IFACE (file);
1738 (* iface->create_async) (file,
1747 * g_file_create_finish:
1748 * @file: input #GFile.
1749 * @res: a #GAsyncResult.
1750 * @error: a #GError, or %NULL
1752 * Finishes an asynchronous file create operation started with
1753 * g_file_create_async().
1755 * Returns: a #GFileOutputStream or %NULL on error.
1758 g_file_create_finish (GFile *file,
1764 g_return_val_if_fail (G_IS_FILE (file), NULL);
1765 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1767 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1769 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1770 if (g_simple_async_result_propagate_error (simple, error))
1774 iface = G_FILE_GET_IFACE (file);
1775 return (* iface->create_finish) (file, res, error);
1779 * g_file_replace_async:
1780 * @file: input #GFile.
1781 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1782 * current #GFile, or NULL to ignore.
1783 * @make_backup: %TRUE if a backup should be created.
1784 * @flags: a set of #GFileCreateFlags.
1785 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1787 * @cancellable: optional #GCancellable object, %NULL to ignore.
1788 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1789 * @user_data: the data to pass to callback function
1791 * Asynchronously overwrites the file, replacing the contents, possibly
1792 * creating a backup copy of the file first.
1794 * For more details, see g_file_replace() which is
1795 * the synchronous version of this call.
1797 * When the operation is finished, @callback will be called. You can then call
1798 * g_file_replace_finish() to get the result of the operation.
1801 g_file_replace_async (GFile *file,
1803 gboolean make_backup,
1804 GFileCreateFlags flags,
1806 GCancellable *cancellable,
1807 GAsyncReadyCallback callback,
1812 g_return_if_fail (G_IS_FILE (file));
1814 iface = G_FILE_GET_IFACE (file);
1815 (* iface->replace_async) (file,
1826 * g_file_replace_finish:
1827 * @file: input #GFile.
1828 * @res: a #GAsyncResult.
1829 * @error: a #GError, or %NULL
1831 * Finishes an asynchronous file replace operation started with
1832 * g_file_replace_async().
1834 * Returns: a #GFileOutputStream, or %NULL on error.
1837 g_file_replace_finish (GFile *file,
1843 g_return_val_if_fail (G_IS_FILE (file), NULL);
1844 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1846 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1848 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1849 if (g_simple_async_result_propagate_error (simple, error))
1853 iface = G_FILE_GET_IFACE (file);
1854 return (* iface->replace_finish) (file, res, error);
1858 copy_symlink (GFile *destination,
1859 GFileCopyFlags flags,
1860 GCancellable *cancellable,
1865 gboolean tried_delete;
1867 GFileType file_type;
1869 tried_delete = FALSE;
1873 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1875 /* Maybe it already existed, and we want to overwrite? */
1876 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1877 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1879 g_error_free (my_error);
1882 /* Don't overwrite if the destination is a directory */
1883 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1884 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1885 cancellable, &my_error);
1888 file_type = g_file_info_get_file_type (info);
1889 g_object_unref (info);
1891 if (file_type == G_FILE_TYPE_DIRECTORY)
1893 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1894 _("Can't copy over directory"));
1899 if (!g_file_delete (destination, cancellable, error))
1902 tried_delete = TRUE;
1906 g_propagate_error (error, my_error);
1913 static GInputStream *
1914 open_source_for_copy (GFile *source,
1916 GFileCopyFlags flags,
1917 GCancellable *cancellable,
1923 GFileType file_type;
1926 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1930 /* There was an error opening the source, try to set a good error for it: */
1932 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1934 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1935 * as that is less useful to the app. Better check for errors on the
1938 g_error_free (my_error);
1941 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1942 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1943 cancellable, &my_error);
1946 file_type = g_file_info_get_file_type (info);
1947 g_object_unref (info);
1949 if (flags & G_FILE_COPY_OVERWRITE)
1951 if (file_type == G_FILE_TYPE_DIRECTORY)
1953 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1954 _("Can't copy directory over directory"));
1957 /* continue to would_recurse error */
1961 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1962 _("Target file exists"));
1968 /* Error getting info from target, return that error
1969 * (except for NOT_FOUND, which is no error here)
1971 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1973 g_propagate_error (error, my_error);
1976 g_error_free (my_error);
1979 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1980 _("Can't recursively copy directory"));
1984 g_propagate_error (error, my_error);
1989 should_copy (GFileAttributeInfo *info,
1993 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1994 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1998 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
1999 GFileAttributeInfoList *namespaces,
2007 s = g_string_new ("");
2011 for (i = 0; i < attributes->n_infos; i++)
2013 if (should_copy (&attributes->infos[i], as_move))
2018 g_string_append_c (s, ',');
2020 g_string_append (s, attributes->infos[i].name);
2027 for (i = 0; i < namespaces->n_infos; i++)
2029 if (should_copy (&namespaces->infos[i], as_move))
2034 g_string_append_c (s, ',');
2036 g_string_append (s, namespaces->infos[i].name);
2037 g_string_append (s, ":*");
2042 return g_string_free (s, FALSE);
2046 * g_file_copy_attributes:
2047 * @source: a #GFile with attributes.
2048 * @destination: a #GFile to copy attributes to.
2049 * @flags: a set of #GFileCopyFlags.
2050 * @cancellable: optional #GCancellable object, %NULL to ignore.
2051 * @error: a #GError, %NULL to ignore.
2053 * Copies the file attributes from @source to @destination.
2055 * Normally only a subset of the file attributes are copied,
2056 * those that are copies in a normal file copy operation
2057 * (which for instance does not include e.g. mtime). However
2058 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2059 * all the metadata that is possible to copy is copied.
2061 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2064 g_file_copy_attributes (GFile *source,
2066 GFileCopyFlags flags,
2067 GCancellable *cancellable,
2070 GFileAttributeInfoList *attributes, *namespaces;
2071 char *attrs_to_read;
2075 gboolean source_nofollow_symlinks;
2077 as_move = flags & G_FILE_COPY_ALL_METADATA;
2078 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2080 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2081 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2082 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2084 if (attributes == NULL && namespaces == NULL)
2087 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2089 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2090 * we just don't copy it.
2092 info = g_file_query_info (source, attrs_to_read,
2093 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2097 g_free (attrs_to_read);
2102 res = g_file_set_attributes_from_info (destination,
2106 g_object_unref (info);
2109 g_file_attribute_info_list_unref (attributes);
2110 g_file_attribute_info_list_unref (namespaces);
2115 /* Closes the streams */
2117 copy_stream_with_progress (GInputStream *in,
2120 GCancellable *cancellable,
2121 GFileProgressCallback progress_callback,
2122 gpointer progress_callback_data,
2125 gssize n_read, n_written;
2126 goffset current_size;
2127 char buffer[1024*64], *p;
2133 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2134 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2138 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2139 total_size = g_file_info_get_size (info);
2140 g_object_unref (info);
2143 if (total_size == -1)
2145 info = g_file_query_info (source,
2146 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2147 G_FILE_QUERY_INFO_NONE,
2151 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2152 total_size = g_file_info_get_size (info);
2153 g_object_unref (info);
2157 if (total_size == -1)
2164 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2174 current_size += n_read;
2179 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2180 if (n_written == -1)
2187 n_read -= n_written;
2193 if (progress_callback)
2194 progress_callback (current_size, total_size, progress_callback_data);
2198 error = NULL; /* Ignore further errors */
2200 /* Make sure we send full copied size */
2201 if (progress_callback)
2202 progress_callback (current_size, total_size, progress_callback_data);
2204 /* Don't care about errors in source here */
2205 g_input_stream_close (in, cancellable, NULL);
2207 /* But write errors on close are bad! */
2208 if (!g_output_stream_close (out, cancellable, error))
2211 g_object_unref (in);
2212 g_object_unref (out);
2218 file_copy_fallback (GFile *source,
2220 GFileCopyFlags flags,
2221 GCancellable *cancellable,
2222 GFileProgressCallback progress_callback,
2223 gpointer progress_callback_data,
2231 /* Maybe copy the symlink? */
2232 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2234 info = g_file_query_info (source,
2235 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2236 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2242 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2243 (target = g_file_info_get_symlink_target (info)) != NULL)
2245 if (!copy_symlink (destination, flags, cancellable, target, error))
2247 g_object_unref (info);
2251 g_object_unref (info);
2255 g_object_unref (info);
2258 in = open_source_for_copy (source, destination, flags, cancellable, error);
2262 if (flags & G_FILE_COPY_OVERWRITE)
2264 out = (GOutputStream *)g_file_replace (destination,
2266 flags & G_FILE_COPY_BACKUP,
2267 cancellable, error);
2271 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2276 g_object_unref (in);
2280 if (!copy_stream_with_progress (in, out, source, cancellable,
2281 progress_callback, progress_callback_data,
2287 /* Ignore errors here. Failure to copy metadata is not a hard error */
2288 g_file_copy_attributes (source, destination,
2289 flags, cancellable, NULL);
2296 * @source: input #GFile.
2297 * @destination: destination #GFile
2298 * @flags: set of #GFileCopyFlags
2299 * @cancellable: optional #GCancellable object, %NULL to ignore.
2300 * @progress_callback: function to callback with progress information
2301 * @progress_callback_data: user data to pass to @progress_callback
2302 * @error: #GError to set on error, or %NULL
2304 * Copies the file @source to the location specified by @destination.
2305 * Can not handle recursive copies of directories.
2307 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2308 * existing @destination file is overwritten.
2310 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2311 * will be copied as symlinks, otherwise the target of the
2312 * @source symlink will be copied.
2314 * If @cancellable is not %NULL, then the operation can be cancelled by
2315 * triggering the cancellable object from another thread. If the operation
2316 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2318 * If @progress_callback is not %NULL, then the operation can be monitored by
2319 * setting this to a #GFileProgressCallback function. @progress_callback_data
2320 * will be passed to this function. It is guaranteed that this callback will
2321 * be called after all data has been transferred with the total number of bytes
2322 * copied during the operation.
2324 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2325 * error is returned, independent on the status of the @destination.
2327 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2328 * error G_IO_ERROR_EXISTS is returned.
2330 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2331 * error is returned. If trying to overwrite a directory with a directory the
2332 * G_IO_ERROR_WOULD_MERGE error is returned.
2334 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2335 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2338 * If you are interested in copying the #GFile object itself (not the on-disk
2339 * file), see g_file_dup().
2341 * Returns: %TRUE on success, %FALSE otherwise.
2344 g_file_copy (GFile *source,
2346 GFileCopyFlags flags,
2347 GCancellable *cancellable,
2348 GFileProgressCallback progress_callback,
2349 gpointer progress_callback_data,
2356 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2357 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2359 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2362 iface = G_FILE_GET_IFACE (destination);
2366 res = (* iface->copy) (source, destination,
2368 progress_callback, progress_callback_data,
2374 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2376 g_propagate_error (error, my_error);
2381 /* If the types are different, and the destination method failed
2382 also try the source method */
2383 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2385 iface = G_FILE_GET_IFACE (source);
2390 res = (* iface->copy) (source, destination,
2392 progress_callback, progress_callback_data,
2398 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2400 g_propagate_error (error, my_error);
2406 return file_copy_fallback (source, destination, flags, cancellable,
2407 progress_callback, progress_callback_data,
2412 * g_file_copy_async:
2413 * @source: input #GFile.
2414 * @destination: destination #GFile
2415 * @flags: set of #GFileCopyFlags
2416 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2418 * @cancellable: optional #GCancellable object, %NULL to ignore.
2419 * @progress_callback: function to callback with progress information
2420 * @progress_callback_data: user data to pass to @progress_callback
2421 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2422 * @user_data: the data to pass to callback function
2424 * Copies the file @source to the location specified by @destination
2425 * asynchronously. For details of the behaviour, see g_file_copy().
2427 * If @progress_callback is not %NULL, then that function that will be called
2428 * just like in g_file_copy(), however the callback will run in the main loop,
2429 * not in the thread that is doing the I/O operation.
2431 * When the operation is finished, @callback will be called. You can then call
2432 * g_file_copy_finish() to get the result of the operation.
2435 g_file_copy_async (GFile *source,
2437 GFileCopyFlags flags,
2439 GCancellable *cancellable,
2440 GFileProgressCallback progress_callback,
2441 gpointer progress_callback_data,
2442 GAsyncReadyCallback callback,
2447 g_return_if_fail (G_IS_FILE (source));
2448 g_return_if_fail (G_IS_FILE (destination));
2450 iface = G_FILE_GET_IFACE (source);
2451 (* iface->copy_async) (source,
2457 progress_callback_data,
2463 * g_file_copy_finish:
2464 * @file: input #GFile.
2465 * @res: a #GAsyncResult.
2466 * @error: a #GError, or %NULL
2468 * Finishes copying the file started with
2469 * g_file_copy_async().
2471 * Returns: a %TRUE on success, %FALSE on error.
2474 g_file_copy_finish (GFile *file,
2480 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2481 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2483 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2485 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2487 if (g_simple_async_result_propagate_error (simple, error))
2491 iface = G_FILE_GET_IFACE (file);
2492 return (* iface->copy_finish) (file, res, error);
2497 * @source: #GFile pointing to the source location.
2498 * @destination: #GFile pointing to the destination location.
2499 * @flags: set of #GFileCopyFlags.
2500 * @cancellable: optional #GCancellable object, %NULL to ignore.
2501 * @progress_callback: #GFileProgressCallback function for updates.
2502 * @progress_callback_data: gpointer to user data for the callback function.
2503 * @error: #GError for returning error conditions, or %NULL
2506 * Tries to move the file or directory @source to the location specified by @destination.
2507 * If native move operations are supported then this is used, otherwise a copy + delete
2508 * fallback is used. The native implementation may support moving directories (for instance
2509 * on moves inside the same filesystem), but the fallback code does not.
2511 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2512 * existing @destination file is overwritten.
2514 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2515 * will be copied as symlinks, otherwise the target of the
2516 * @source symlink will be copied.
2518 * If @cancellable is not %NULL, then the operation can be cancelled by
2519 * triggering the cancellable object from another thread. If the operation
2520 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2522 * If @progress_callback is not %NULL, then the operation can be monitored by
2523 * setting this to a #GFileProgressCallback function. @progress_callback_data
2524 * will be passed to this function. It is guaranteed that this callback will
2525 * be called after all data has been transferred with the total number of bytes
2526 * copied during the operation.
2528 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2529 * error is returned, independent on the status of the @destination.
2531 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2532 * error G_IO_ERROR_EXISTS is returned.
2534 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2535 * error is returned. If trying to overwrite a directory with a directory the
2536 * G_IO_ERROR_WOULD_MERGE error is returned.
2538 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2539 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2540 * may be returned (if the native move operation isn't available).
2542 * Returns: %TRUE on successful move, %FALSE otherwise.
2545 g_file_move (GFile *source,
2547 GFileCopyFlags flags,
2548 GCancellable *cancellable,
2549 GFileProgressCallback progress_callback,
2550 gpointer progress_callback_data,
2557 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2558 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2560 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2563 iface = G_FILE_GET_IFACE (destination);
2567 res = (* iface->move) (source, destination,
2569 progress_callback, progress_callback_data,
2575 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2577 g_propagate_error (error, my_error);
2582 /* If the types are different, and the destination method failed
2583 also try the source method */
2584 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2586 iface = G_FILE_GET_IFACE (source);
2591 res = (* iface->move) (source, destination,
2593 progress_callback, progress_callback_data,
2599 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2601 g_propagate_error (error, my_error);
2607 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2609 g_set_error (error, G_IO_ERROR,
2610 G_IO_ERROR_NOT_SUPPORTED,
2611 _("Operation not supported"));
2615 flags |= G_FILE_COPY_ALL_METADATA;
2616 if (!g_file_copy (source, destination, flags, cancellable,
2617 progress_callback, progress_callback_data,
2621 return g_file_delete (source, cancellable, error);
2625 * g_file_make_directory
2626 * @file: input #GFile.
2627 * @cancellable: optional #GCancellable object, %NULL to ignore.
2628 * @error: a #GError, or %NULL
2630 * Creates a directory.
2632 * If @cancellable is not %NULL, then the operation can be cancelled by
2633 * triggering the cancellable object from another thread. If the operation
2634 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2636 * Returns: %TRUE on successful creation, %FALSE otherwise.
2639 g_file_make_directory (GFile *file,
2640 GCancellable *cancellable,
2645 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2647 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2650 iface = G_FILE_GET_IFACE (file);
2652 if (iface->make_directory == NULL)
2654 g_set_error (error, G_IO_ERROR,
2655 G_IO_ERROR_NOT_SUPPORTED,
2656 _("Operation not supported"));
2660 return (* iface->make_directory) (file, cancellable, error);
2664 * g_file_make_symbolic_link:
2665 * @file: input #GFile.
2666 * @symlink_value: a string with the value of the new symlink.
2667 * @cancellable: optional #GCancellable object, %NULL to ignore.
2668 * @error: a #GError.
2670 * Creates a symbolic link.
2672 * If @cancellable is not %NULL, then the operation can be cancelled by
2673 * triggering the cancellable object from another thread. If the operation
2674 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2676 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2679 g_file_make_symbolic_link (GFile *file,
2680 const char *symlink_value,
2681 GCancellable *cancellable,
2686 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2687 g_return_val_if_fail (symlink_value != NULL, FALSE);
2689 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2692 if (*symlink_value == '\0')
2694 g_set_error (error, G_IO_ERROR,
2695 G_IO_ERROR_INVALID_ARGUMENT,
2696 _("Invalid symlink value given"));
2700 iface = G_FILE_GET_IFACE (file);
2702 if (iface->make_symbolic_link == NULL)
2704 g_set_error (error, G_IO_ERROR,
2705 G_IO_ERROR_NOT_SUPPORTED,
2706 _("Operation not supported"));
2710 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2715 * @file: input #GFile.
2716 * @cancellable: optional #GCancellable object, %NULL to ignore.
2717 * @error: a #GError, or %NULL
2719 * Deletes a file. If the @file is a directory, it will only be deleted if it
2722 * If @cancellable is not %NULL, then the operation can be cancelled by
2723 * triggering the cancellable object from another thread. If the operation
2724 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2726 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2729 g_file_delete (GFile *file,
2730 GCancellable *cancellable,
2735 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2737 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2740 iface = G_FILE_GET_IFACE (file);
2742 if (iface->delete_file == NULL)
2744 g_set_error (error, G_IO_ERROR,
2745 G_IO_ERROR_NOT_SUPPORTED,
2746 _("Operation not supported"));
2750 return (* iface->delete_file) (file, cancellable, error);
2755 * @file: #GFile to send to trash.
2756 * @cancellable: optional #GCancellable object, %NULL to ignore.
2757 * @error: a #GError, or %NULL
2759 * Sends @file to the "Trashcan", if possible. This is similar to
2760 * deleting it, but the user can recover it before emptying the trashcan.
2761 * Not all file systems support trashing, so this call can return the
2762 * %G_IO_ERROR_NOT_SUPPORTED error.
2765 * If @cancellable is not %NULL, then the operation can be cancelled by
2766 * triggering the cancellable object from another thread. If the operation
2767 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2769 * Returns: %TRUE on successful trash, %FALSE otherwise.
2772 g_file_trash (GFile *file,
2773 GCancellable *cancellable,
2778 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2780 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2783 iface = G_FILE_GET_IFACE (file);
2785 if (iface->trash == NULL)
2788 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2789 _("Trash not supported"));
2793 return (* iface->trash) (file, cancellable, error);
2797 * g_file_set_display_name:
2798 * @file: input #GFile.
2799 * @display_name: a string.
2800 * @cancellable: optional #GCancellable object, %NULL to ignore.
2801 * @error: a #GError, or %NULL
2803 * Renames @file to the specified display name.
2805 * The display name is converted from UTF8 to the correct encoding for the target
2806 * filesystem if possible and the @file is renamed to this.
2808 * If you want to implement a rename operation in the user interface the edit name
2809 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2810 * widget, and then the result after editing should be passed to g_file_set_display_name().
2812 * On success the resulting converted filename is returned.
2814 * If @cancellable is not %NULL, then the operation can be cancelled by
2815 * triggering the cancellable object from another thread. If the operation
2816 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2818 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2821 g_file_set_display_name (GFile *file,
2822 const char *display_name,
2823 GCancellable *cancellable,
2828 g_return_val_if_fail (G_IS_FILE (file), NULL);
2829 g_return_val_if_fail (display_name != NULL, NULL);
2831 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2835 G_IO_ERROR_INVALID_ARGUMENT,
2836 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2840 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2843 iface = G_FILE_GET_IFACE (file);
2845 return (* iface->set_display_name) (file, display_name, cancellable, error);
2849 * g_file_set_display_name_async:
2850 * @file: input #GFile.
2851 * @display_name: a string.
2852 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2854 * @cancellable: optional #GCancellable object, %NULL to ignore.
2855 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2856 * @user_data: the data to pass to callback function
2858 * Asynchronously sets the display name for a given #GFile.
2860 * For more details, see g_set_display_name() which is
2861 * the synchronous version of this call.
2863 * When the operation is finished, @callback will be called. You can then call
2864 * g_file_set_display_name_finish() to get the result of the operation.
2867 g_file_set_display_name_async (GFile *file,
2868 const char *display_name,
2870 GCancellable *cancellable,
2871 GAsyncReadyCallback callback,
2876 g_return_if_fail (G_IS_FILE (file));
2877 g_return_if_fail (display_name != NULL);
2879 iface = G_FILE_GET_IFACE (file);
2880 (* iface->set_display_name_async) (file,
2889 * g_file_set_display_name_finish:
2890 * @file: input #GFile.
2891 * @res: a #GAsyncResult.
2892 * @error: a #GError, or %NULL
2894 * Finishes setting a display name started with
2895 * g_file_set_display_name_async().
2897 * Returns: a #GFile or %NULL on error.
2900 g_file_set_display_name_finish (GFile *file,
2906 g_return_val_if_fail (G_IS_FILE (file), NULL);
2907 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2909 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2911 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2912 if (g_simple_async_result_propagate_error (simple, error))
2916 iface = G_FILE_GET_IFACE (file);
2917 return (* iface->set_display_name_finish) (file, res, error);
2921 * g_file_query_settable_attributes:
2922 * @file: input #GFile.
2923 * @cancellable: optional #GCancellable object, %NULL to ignore.
2924 * @error: a #GError, or %NULL
2926 * Obtain the list of settable attributes for the file.
2928 * Returns the type and full attribute name of all the attributes
2929 * that can be set on this file. This doesn't mean setting it will always
2930 * succeed though, you might get an access failure, or some specific
2931 * file may not support a specific attribute.
2933 * If @cancellable is not %NULL, then the operation can be cancelled by
2934 * triggering the cancellable object from another thread. If the operation
2935 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2937 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2938 * When you are done with it, release it with g_file_attribute_info_list_unref()
2940 GFileAttributeInfoList *
2941 g_file_query_settable_attributes (GFile *file,
2942 GCancellable *cancellable,
2947 GFileAttributeInfoList *list;
2949 g_return_val_if_fail (G_IS_FILE (file), NULL);
2951 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2954 iface = G_FILE_GET_IFACE (file);
2956 if (iface->query_settable_attributes == NULL)
2957 return g_file_attribute_info_list_new ();
2960 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2964 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2966 list = g_file_attribute_info_list_new ();
2967 g_error_free (my_error);
2970 g_propagate_error (error, my_error);
2977 * g_file_query_writable_namespaces:
2978 * @file: input #GFile.
2979 * @cancellable: optional #GCancellable object, %NULL to ignore.
2980 * @error: a #GError, or %NULL
2982 * Obtain the list of attribute namespaces where new attributes
2983 * can be created by a user. An example of this is extended
2984 * attributes (in the "xattr" namespace).
2986 * If @cancellable is not %NULL, then the operation can be cancelled by
2987 * triggering the cancellable object from another thread. If the operation
2988 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2990 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2991 * When you are done with it, release it with g_file_attribute_info_list_unref()
2993 GFileAttributeInfoList *
2994 g_file_query_writable_namespaces (GFile *file,
2995 GCancellable *cancellable,
3000 GFileAttributeInfoList *list;
3002 g_return_val_if_fail (G_IS_FILE (file), NULL);
3004 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3007 iface = G_FILE_GET_IFACE (file);
3009 if (iface->query_writable_namespaces == NULL)
3010 return g_file_attribute_info_list_new ();
3013 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3017 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3019 list = g_file_attribute_info_list_new ();
3020 g_error_free (my_error);
3023 g_propagate_error (error, my_error);
3030 * g_file_set_attribute:
3031 * @file: input #GFile.
3032 * @attribute: a string containing the attribute's name.
3033 * @type: The type of the attribute
3034 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3035 * @flags: a set of #GFileQueryInfoFlags.
3036 * @cancellable: optional #GCancellable object, %NULL to ignore.
3037 * @error: a #GError, or %NULL
3039 * Sets an attribute in the file with attribute name @attribute to @value.
3041 * If @cancellable is not %NULL, then the operation can be cancelled by
3042 * triggering the cancellable object from another thread. If the operation
3043 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3045 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3048 g_file_set_attribute (GFile *file,
3049 const char *attribute,
3050 GFileAttributeType type,
3052 GFileQueryInfoFlags flags,
3053 GCancellable *cancellable,
3058 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3059 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3061 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3064 iface = G_FILE_GET_IFACE (file);
3066 if (iface->set_attribute == NULL)
3068 g_set_error (error, G_IO_ERROR,
3069 G_IO_ERROR_NOT_SUPPORTED,
3070 _("Operation not supported"));
3074 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3078 * g_file_set_attributes_from_info:
3079 * @file: input #GFile.
3080 * @info: a #GFileInfo.
3081 * @flags: #GFileQueryInfoFlags
3082 * @cancellable: optional #GCancellable object, %NULL to ignore.
3083 * @error: a #GError, or %NULL
3085 * Tries to set all attributes in the #GFileInfo on the target values,
3086 * not stopping on the first error.
3088 * If there is any error during this operation then @error will be set to
3089 * the first error. Error on particular fields are flagged by setting
3090 * the "status" field in the attribute value to
3091 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3094 * If @cancellable is not %NULL, then the operation can be cancelled by
3095 * triggering the cancellable object from another thread. If the operation
3096 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3098 * Returns: %TRUE if there was any error, %FALSE otherwise.
3101 g_file_set_attributes_from_info (GFile *file,
3103 GFileQueryInfoFlags flags,
3104 GCancellable *cancellable,
3109 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3110 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3112 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3115 g_file_info_clear_status (info);
3117 iface = G_FILE_GET_IFACE (file);
3119 return (* iface->set_attributes_from_info) (file,
3128 g_file_real_set_attributes_from_info (GFile *file,
3130 GFileQueryInfoFlags flags,
3131 GCancellable *cancellable,
3137 GFileAttributeValue *value;
3141 attributes = g_file_info_list_attributes (info, NULL);
3143 for (i = 0; attributes[i] != NULL; i++)
3145 value = _g_file_info_get_attribute_value (info, attributes[i]);
3147 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3150 if (!g_file_set_attribute (file, attributes[i],
3151 value->type, _g_file_attribute_value_peek_as_pointer (value),
3152 flags, cancellable, error))
3154 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3156 /* Don't set error multiple times */
3160 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3163 g_strfreev (attributes);
3169 * g_file_set_attributes_async:
3170 * @file: input #GFile.
3171 * @info: a #GFileInfo.
3172 * @flags: a #GFileQueryInfoFlags.
3173 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3175 * @cancellable: optional #GCancellable object, %NULL to ignore.
3176 * @callback: a #GAsyncReadyCallback.
3177 * @user_data: a #gpointer.
3179 * Asynchronously sets the attributes of @file with @info.
3181 * For more details, see g_file_set_attributes_from_info() which is
3182 * the synchronous version of this call.
3184 * When the operation is finished, @callback will be called. You can then call
3185 * g_file_set_attributes_finish() to get the result of the operation.
3188 g_file_set_attributes_async (GFile *file,
3190 GFileQueryInfoFlags flags,
3192 GCancellable *cancellable,
3193 GAsyncReadyCallback callback,
3198 g_return_if_fail (G_IS_FILE (file));
3199 g_return_if_fail (G_IS_FILE_INFO (info));
3201 iface = G_FILE_GET_IFACE (file);
3202 (* iface->set_attributes_async) (file,
3212 * g_file_set_attributes_finish:
3213 * @file: input #GFile.
3214 * @result: a #GAsyncResult.
3215 * @info: a #GFileInfo.
3216 * @error: a #GError, or %NULL
3218 * Finishes setting an attribute started in g_file_set_attributes_async().
3220 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3223 g_file_set_attributes_finish (GFile *file,
3224 GAsyncResult *result,
3230 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3231 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3233 /* No standard handling of errors here, as we must set info even
3236 iface = G_FILE_GET_IFACE (file);
3237 return (* iface->set_attributes_finish) (file, result, info, error);
3241 * g_file_set_attribute_string:
3242 * @file: input #GFile.
3243 * @attribute: a string containing the attribute's name.
3244 * @value: a string containing the attribute's value.
3245 * @flags: #GFileQueryInfoFlags.
3246 * @cancellable: optional #GCancellable object, %NULL to ignore.
3247 * @error: a #GError, or %NULL
3249 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3250 * If @attribute is of a different type, this operation will fail.
3252 * If @cancellable is not %NULL, then the operation can be cancelled by
3253 * triggering the cancellable object from another thread. If the operation
3254 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3256 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3259 g_file_set_attribute_string (GFile *file,
3260 const char *attribute,
3262 GFileQueryInfoFlags flags,
3263 GCancellable *cancellable,
3266 return g_file_set_attribute (file, attribute,
3267 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3268 flags, cancellable, error);
3272 * g_file_set_attribute_byte_string:
3273 * @file: input #GFile.
3274 * @attribute: a string containing the attribute's name.
3275 * @value: a string containing the attribute's new value.
3276 * @flags: a #GFileQueryInfoFlags.
3277 * @cancellable: optional #GCancellable object, %NULL to ignore.
3278 * @error: a #GError, or %NULL
3280 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3281 * If @attribute is of a different type, this operation will fail,
3284 * If @cancellable is not %NULL, then the operation can be cancelled by
3285 * triggering the cancellable object from another thread. If the operation
3286 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3288 * Returns: %TRUE if the @attribute was successfully set to @value
3289 * in the @file, %FALSE otherwise.
3292 g_file_set_attribute_byte_string (GFile *file,
3293 const char *attribute,
3295 GFileQueryInfoFlags flags,
3296 GCancellable *cancellable,
3299 return g_file_set_attribute (file, attribute,
3300 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3301 flags, cancellable, error);
3305 * g_file_set_attribute_uint32:
3306 * @file: input #GFile.
3307 * @attribute: a string containing the attribute's name.
3308 * @value: a #guint32 containing the attribute's new value.
3309 * @flags: a #GFileQueryInfoFlags.
3310 * @cancellable: optional #GCancellable object, %NULL to ignore.
3311 * @error: a #GError, or %NULL
3313 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3314 * If @attribute is of a different type, this operation will fail.
3316 * If @cancellable is not %NULL, then the operation can be cancelled by
3317 * triggering the cancellable object from another thread. If the operation
3318 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3320 * Returns: %TRUE if the @attribute was successfully set to @value
3321 * in the @file, %FALSE otherwise.
3324 g_file_set_attribute_uint32 (GFile *file,
3325 const char *attribute,
3327 GFileQueryInfoFlags flags,
3328 GCancellable *cancellable,
3331 return g_file_set_attribute (file, attribute,
3332 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3333 flags, cancellable, error);
3337 * g_file_set_attribute_int32:
3338 * @file: input #GFile.
3339 * @attribute: a string containing the attribute's name.
3340 * @value: a #gint32 containing the attribute's new value.
3341 * @flags: a #GFileQueryInfoFlags.
3342 * @cancellable: optional #GCancellable object, %NULL to ignore.
3343 * @error: a #GError, or %NULL
3345 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3346 * If @attribute is of a different type, this operation will fail.
3348 * If @cancellable is not %NULL, then the operation can be cancelled by
3349 * triggering the cancellable object from another thread. If the operation
3350 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3352 * Returns: %TRUE if the @attribute was successfully set to @value
3353 * in the @file, %FALSE otherwise.
3356 g_file_set_attribute_int32 (GFile *file,
3357 const char *attribute,
3359 GFileQueryInfoFlags flags,
3360 GCancellable *cancellable,
3363 return g_file_set_attribute (file, attribute,
3364 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3365 flags, cancellable, error);
3369 * g_file_set_attribute_uint64:
3370 * @file: input #GFile.
3371 * @attribute: a string containing the attribute's name.
3372 * @value: a #guint64 containing the attribute's new value.
3373 * @flags: a #GFileQueryInfoFlags.
3374 * @cancellable: optional #GCancellable object, %NULL to ignore.
3375 * @error: a #GError, or %NULL
3377 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3378 * If @attribute is of a different type, this operation will fail.
3380 * If @cancellable is not %NULL, then the operation can be cancelled by
3381 * triggering the cancellable object from another thread. If the operation
3382 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3384 * Returns: %TRUE if the @attribute was successfully set to @value
3385 * in the @file, %FALSE otherwise.
3388 g_file_set_attribute_uint64 (GFile *file,
3389 const char *attribute,
3391 GFileQueryInfoFlags flags,
3392 GCancellable *cancellable,
3395 return g_file_set_attribute (file, attribute,
3396 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3397 flags, cancellable, error);
3401 * g_file_set_attribute_int64:
3402 * @file: input #GFile.
3403 * @attribute: a string containing the attribute's name.
3404 * @value: a #guint64 containing the attribute's new value.
3405 * @flags: a #GFileQueryInfoFlags.
3406 * @cancellable: optional #GCancellable object, %NULL to ignore.
3407 * @error: a #GError, or %NULL
3409 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3410 * If @attribute is of a different type, this operation will fail.
3412 * If @cancellable is not %NULL, then the operation can be cancelled by
3413 * triggering the cancellable object from another thread. If the operation
3414 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3416 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3419 g_file_set_attribute_int64 (GFile *file,
3420 const char *attribute,
3422 GFileQueryInfoFlags flags,
3423 GCancellable *cancellable,
3426 return g_file_set_attribute (file, attribute,
3427 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3428 flags, cancellable, error);
3432 * g_file_mount_mountable:
3433 * @file: input #GFile.
3434 * @flags: flags affecting the operation
3435 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3436 * @cancellable: optional #GCancellable object, %NULL to ignore.
3437 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3438 * @user_data: the data to pass to callback function
3440 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3441 * Using @mount_operation, you can request callbacks when, for instance,
3442 * passwords are needed during authentication.
3444 * If @cancellable is not %NULL, then the operation can be cancelled by
3445 * triggering the cancellable object from another thread. If the operation
3446 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3448 * When the operation is finished, @callback will be called. You can then call
3449 * g_file_mount_mountable_finish() to get the result of the operation.
3452 g_file_mount_mountable (GFile *file,
3453 GMountMountFlags flags,
3454 GMountOperation *mount_operation,
3455 GCancellable *cancellable,
3456 GAsyncReadyCallback callback,
3461 g_return_if_fail (G_IS_FILE (file));
3463 iface = G_FILE_GET_IFACE (file);
3465 if (iface->mount_mountable == NULL)
3467 g_simple_async_report_error_in_idle (G_OBJECT (file),
3471 G_IO_ERROR_NOT_SUPPORTED,
3472 _("Operation not supported"));
3476 (* iface->mount_mountable) (file,
3485 * g_file_mount_mountable_finish:
3486 * @file: input #GFile.
3487 * @result: a #GAsyncResult.
3488 * @error: a #GError, or %NULL
3490 * Finishes a mount operation. See g_file_mount_mountable() for details.
3492 * Finish an asynchronous mount operation that was started
3493 * with g_file_mount_mountable().
3495 * Returns: a #GFile or %NULL on error.
3498 g_file_mount_mountable_finish (GFile *file,
3499 GAsyncResult *result,
3504 g_return_val_if_fail (G_IS_FILE (file), NULL);
3505 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3507 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3509 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3510 if (g_simple_async_result_propagate_error (simple, error))
3514 iface = G_FILE_GET_IFACE (file);
3515 return (* iface->mount_mountable_finish) (file, result, error);
3519 * g_file_unmount_mountable:
3520 * @file: input #GFile.
3521 * @flags: flags affecting the operation
3522 * @cancellable: optional #GCancellable object, %NULL to ignore.
3523 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3524 * @user_data: the data to pass to callback function
3526 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3528 * If @cancellable is not %NULL, then the operation can be cancelled by
3529 * triggering the cancellable object from another thread. If the operation
3530 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3532 * When the operation is finished, @callback will be called. You can then call
3533 * g_file_unmount_mountable_finish() to get the result of the operation.
3536 g_file_unmount_mountable (GFile *file,
3537 GMountUnmountFlags flags,
3538 GCancellable *cancellable,
3539 GAsyncReadyCallback callback,
3544 g_return_if_fail (G_IS_FILE (file));
3546 iface = G_FILE_GET_IFACE (file);
3548 if (iface->unmount_mountable == NULL)
3550 g_simple_async_report_error_in_idle (G_OBJECT (file),
3554 G_IO_ERROR_NOT_SUPPORTED,
3555 _("Operation not supported"));
3559 (* iface->unmount_mountable) (file,
3567 * g_file_unmount_mountable_finish:
3568 * @file: input #GFile.
3569 * @result: a #GAsyncResult.
3570 * @error: a #GError, or %NULL
3572 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3574 * Finish an asynchronous unmount operation that was started
3575 * with g_file_unmount_mountable().
3577 * Returns: %TRUE if the operation finished successfully. %FALSE
3581 g_file_unmount_mountable_finish (GFile *file,
3582 GAsyncResult *result,
3587 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3588 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3590 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3592 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3593 if (g_simple_async_result_propagate_error (simple, error))
3597 iface = G_FILE_GET_IFACE (file);
3598 return (* iface->unmount_mountable_finish) (file, result, error);
3602 * g_file_eject_mountable:
3603 * @file: input #GFile.
3604 * @flags: flags affecting the operation
3605 * @cancellable: optional #GCancellable object, %NULL to ignore.
3606 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3607 * @user_data: the data to pass to callback function
3609 * Starts an asynchronous eject on a mountable.
3610 * When this operation has completed, @callback will be called with
3611 * @user_user data, and the operation can be finalized with
3612 * g_file_eject_mountable_finish().
3614 * If @cancellable is not %NULL, then the operation can be cancelled by
3615 * triggering the cancellable object from another thread. If the operation
3616 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3619 g_file_eject_mountable (GFile *file,
3620 GMountUnmountFlags flags,
3621 GCancellable *cancellable,
3622 GAsyncReadyCallback callback,
3627 g_return_if_fail (G_IS_FILE (file));
3629 iface = G_FILE_GET_IFACE (file);
3631 if (iface->eject_mountable == NULL)
3633 g_simple_async_report_error_in_idle (G_OBJECT (file),
3637 G_IO_ERROR_NOT_SUPPORTED,
3638 _("Operation not supported"));
3642 (* iface->eject_mountable) (file,
3650 * g_file_eject_mountable_finish:
3651 * @file: input #GFile.
3652 * @result: a #GAsyncResult.
3653 * @error: a #GError, or %NULL
3655 * Finishes an asynchronous eject operation started by
3656 * g_file_eject_mountable().
3658 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3662 g_file_eject_mountable_finish (GFile *file,
3663 GAsyncResult *result,
3668 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3669 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3671 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3673 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3674 if (g_simple_async_result_propagate_error (simple, error))
3678 iface = G_FILE_GET_IFACE (file);
3679 return (* iface->eject_mountable_finish) (file, result, error);
3683 * g_file_monitor_directory:
3684 * @file: input #GFile.
3685 * @flags: a set of #GFileMonitorFlags.
3686 * @cancellable: optional #GCancellable object, %NULL to ignore.
3687 * @error: a #GError, or %NULL.
3689 * Obtains a directory monitor for the given file.
3690 * This may fail if directory monitoring is not supported.
3692 * If @cancellable is not %NULL, then the operation can be cancelled by
3693 * triggering the cancellable object from another thread. If the operation
3694 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3696 * Returns: a #GFileMonitor for the given @file,
3697 * or %NULL on error.
3700 g_file_monitor_directory (GFile *file,
3701 GFileMonitorFlags flags,
3702 GCancellable *cancellable,
3707 g_return_val_if_fail (G_IS_FILE (file), NULL);
3709 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3712 iface = G_FILE_GET_IFACE (file);
3714 if (iface->monitor_dir == NULL)
3716 g_set_error (error, G_IO_ERROR,
3717 G_IO_ERROR_NOT_SUPPORTED,
3718 _("Operation not supported"));
3722 return (* iface->monitor_dir) (file, flags, cancellable, error);
3726 * g_file_monitor_file:
3727 * @file: input #GFile.
3728 * @flags: a set of #GFileMonitorFlags.
3729 * @cancellable: optional #GCancellable object, %NULL to ignore.
3730 * @error: a #GError, or %NULL.
3732 * Obtains a file monitor for the given file. If no file notification
3733 * mechanism exists, then regular polling of the file is used.
3735 * If @cancellable is not %NULL, then the operation can be cancelled by
3736 * triggering the cancellable object from another thread. If the operation
3737 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3739 * Returns: a #GFileMonitor for the given @file.
3742 g_file_monitor_file (GFile *file,
3743 GFileMonitorFlags flags,
3744 GCancellable *cancellable,
3748 GFileMonitor *monitor;
3750 g_return_val_if_fail (G_IS_FILE (file), NULL);
3752 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3755 iface = G_FILE_GET_IFACE (file);
3759 if (iface->monitor_file)
3760 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3762 /* Fallback to polling */
3763 if (monitor == NULL)
3764 monitor = _g_poll_file_monitor_new (file);
3769 /********************************************
3770 * Default implementation of async ops *
3771 ********************************************/
3775 GFileQueryInfoFlags flags;
3777 } QueryInfoAsyncData;
3780 query_info_data_free (QueryInfoAsyncData *data)
3783 g_object_unref (data->info);
3784 g_free (data->attributes);
3789 query_info_async_thread (GSimpleAsyncResult *res,
3791 GCancellable *cancellable)
3793 GError *error = NULL;
3794 QueryInfoAsyncData *data;
3797 data = g_simple_async_result_get_op_res_gpointer (res);
3799 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3803 g_simple_async_result_set_from_error (res, error);
3804 g_error_free (error);
3811 g_file_real_query_info_async (GFile *file,
3812 const char *attributes,
3813 GFileQueryInfoFlags flags,
3815 GCancellable *cancellable,
3816 GAsyncReadyCallback callback,
3819 GSimpleAsyncResult *res;
3820 QueryInfoAsyncData *data;
3822 data = g_new0 (QueryInfoAsyncData, 1);
3823 data->attributes = g_strdup (attributes);
3824 data->flags = flags;
3826 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3827 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3829 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3830 g_object_unref (res);
3834 g_file_real_query_info_finish (GFile *file,
3838 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3839 QueryInfoAsyncData *data;
3841 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3843 data = g_simple_async_result_get_op_res_gpointer (simple);
3845 return g_object_ref (data->info);
3853 } QueryFilesystemInfoAsyncData;
3856 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
3859 g_object_unref (data->info);
3860 g_free (data->attributes);
3865 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
3867 GCancellable *cancellable)
3869 GError *error = NULL;
3870 QueryFilesystemInfoAsyncData *data;
3873 data = g_simple_async_result_get_op_res_gpointer (res);
3875 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
3879 g_simple_async_result_set_from_error (res, error);
3880 g_error_free (error);
3887 g_file_real_query_filesystem_info_async (GFile *file,
3888 const char *attributes,
3890 GCancellable *cancellable,
3891 GAsyncReadyCallback callback,
3894 GSimpleAsyncResult *res;
3895 QueryFilesystemInfoAsyncData *data;
3897 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
3898 data->attributes = g_strdup (attributes);
3900 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
3901 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
3903 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
3904 g_object_unref (res);
3908 g_file_real_query_filesystem_info_finish (GFile *file,
3912 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3913 QueryFilesystemInfoAsyncData *data;
3915 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
3917 data = g_simple_async_result_get_op_res_gpointer (simple);
3919 return g_object_ref (data->info);
3926 GFileQueryInfoFlags flags;
3927 GFileEnumerator *enumerator;
3928 } EnumerateChildrenAsyncData;
3931 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3933 if (data->enumerator)
3934 g_object_unref (data->enumerator);
3935 g_free (data->attributes);
3940 enumerate_children_async_thread (GSimpleAsyncResult *res,
3942 GCancellable *cancellable)
3944 GError *error = NULL;
3945 EnumerateChildrenAsyncData *data;
3946 GFileEnumerator *enumerator;
3948 data = g_simple_async_result_get_op_res_gpointer (res);
3950 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3952 if (enumerator == NULL)
3954 g_simple_async_result_set_from_error (res, error);
3955 g_error_free (error);
3958 data->enumerator = enumerator;
3962 g_file_real_enumerate_children_async (GFile *file,
3963 const char *attributes,
3964 GFileQueryInfoFlags flags,
3966 GCancellable *cancellable,
3967 GAsyncReadyCallback callback,
3970 GSimpleAsyncResult *res;
3971 EnumerateChildrenAsyncData *data;
3973 data = g_new0 (EnumerateChildrenAsyncData, 1);
3974 data->attributes = g_strdup (attributes);
3975 data->flags = flags;
3977 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3978 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3980 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3981 g_object_unref (res);
3984 static GFileEnumerator *
3985 g_file_real_enumerate_children_finish (GFile *file,
3989 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3990 EnumerateChildrenAsyncData *data;
3992 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3994 data = g_simple_async_result_get_op_res_gpointer (simple);
3995 if (data->enumerator)
3996 return g_object_ref (data->enumerator);
4002 open_read_async_thread (GSimpleAsyncResult *res,
4004 GCancellable *cancellable)
4007 GFileInputStream *stream;
4008 GError *error = NULL;
4010 iface = G_FILE_GET_IFACE (object);
4012 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4016 g_simple_async_result_set_from_error (res, error);
4017 g_error_free (error);
4020 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4024 g_file_real_read_async (GFile *file,
4026 GCancellable *cancellable,
4027 GAsyncReadyCallback callback,
4030 GSimpleAsyncResult *res;
4032 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4034 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4035 g_object_unref (res);
4038 static GFileInputStream *
4039 g_file_real_read_finish (GFile *file,
4043 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4046 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4048 op = g_simple_async_result_get_op_res_gpointer (simple);
4050 return g_object_ref (op);
4056 append_to_async_thread (GSimpleAsyncResult *res,
4058 GCancellable *cancellable)
4061 GFileCreateFlags *data;
4062 GFileOutputStream *stream;
4063 GError *error = NULL;
4065 iface = G_FILE_GET_IFACE (object);
4067 data = g_simple_async_result_get_op_res_gpointer (res);
4069 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4073 g_simple_async_result_set_from_error (res, error);
4074 g_error_free (error);
4077 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4081 g_file_real_append_to_async (GFile *file,
4082 GFileCreateFlags flags,
4084 GCancellable *cancellable,
4085 GAsyncReadyCallback callback,
4088 GFileCreateFlags *data;
4089 GSimpleAsyncResult *res;
4091 data = g_new0 (GFileCreateFlags, 1);
4094 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4095 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4097 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4098 g_object_unref (res);
4101 static GFileOutputStream *
4102 g_file_real_append_to_finish (GFile *file,
4106 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4109 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4111 op = g_simple_async_result_get_op_res_gpointer (simple);
4113 return g_object_ref (op);
4119 create_async_thread (GSimpleAsyncResult *res,
4121 GCancellable *cancellable)
4124 GFileCreateFlags *data;
4125 GFileOutputStream *stream;
4126 GError *error = NULL;
4128 iface = G_FILE_GET_IFACE (object);
4130 data = g_simple_async_result_get_op_res_gpointer (res);
4132 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4136 g_simple_async_result_set_from_error (res, error);
4137 g_error_free (error);
4140 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4144 g_file_real_create_async (GFile *file,
4145 GFileCreateFlags flags,
4147 GCancellable *cancellable,
4148 GAsyncReadyCallback callback,
4151 GFileCreateFlags *data;
4152 GSimpleAsyncResult *res;
4154 data = g_new0 (GFileCreateFlags, 1);
4157 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4158 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4160 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4161 g_object_unref (res);
4164 static GFileOutputStream *
4165 g_file_real_create_finish (GFile *file,
4169 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4172 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4174 op = g_simple_async_result_get_op_res_gpointer (simple);
4176 return g_object_ref (op);
4182 GFileOutputStream *stream;
4184 gboolean make_backup;
4185 GFileCreateFlags flags;
4189 replace_async_data_free (ReplaceAsyncData *data)
4192 g_object_unref (data->stream);
4193 g_free (data->etag);
4198 replace_async_thread (GSimpleAsyncResult *res,
4200 GCancellable *cancellable)
4203 GFileOutputStream *stream;
4204 GError *error = NULL;
4205 ReplaceAsyncData *data;
4207 iface = G_FILE_GET_IFACE (object);
4209 data = g_simple_async_result_get_op_res_gpointer (res);
4211 stream = iface->replace (G_FILE (object),
4220 g_simple_async_result_set_from_error (res, error);
4221 g_error_free (error);
4224 data->stream = stream;
4228 g_file_real_replace_async (GFile *file,
4230 gboolean make_backup,
4231 GFileCreateFlags flags,
4233 GCancellable *cancellable,
4234 GAsyncReadyCallback callback,
4237 GSimpleAsyncResult *res;
4238 ReplaceAsyncData *data;
4240 data = g_new0 (ReplaceAsyncData, 1);
4241 data->etag = g_strdup (etag);
4242 data->make_backup = make_backup;
4243 data->flags = flags;
4245 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4246 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4248 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4249 g_object_unref (res);
4252 static GFileOutputStream *
4253 g_file_real_replace_finish (GFile *file,
4257 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4258 ReplaceAsyncData *data;
4260 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4262 data = g_simple_async_result_get_op_res_gpointer (simple);
4264 return g_object_ref (data->stream);
4272 } SetDisplayNameAsyncData;
4275 set_display_name_data_free (SetDisplayNameAsyncData *data)
4277 g_free (data->name);
4279 g_object_unref (data->file);
4284 set_display_name_async_thread (GSimpleAsyncResult *res,
4286 GCancellable *cancellable)
4288 GError *error = NULL;
4289 SetDisplayNameAsyncData *data;
4292 data = g_simple_async_result_get_op_res_gpointer (res);
4294 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4298 g_simple_async_result_set_from_error (res, error);
4299 g_error_free (error);
4306 g_file_real_set_display_name_async (GFile *file,
4307 const char *display_name,
4309 GCancellable *cancellable,
4310 GAsyncReadyCallback callback,
4313 GSimpleAsyncResult *res;
4314 SetDisplayNameAsyncData *data;
4316 data = g_new0 (SetDisplayNameAsyncData, 1);
4317 data->name = g_strdup (display_name);
4319 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4320 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4322 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4323 g_object_unref (res);
4327 g_file_real_set_display_name_finish (GFile *file,
4331 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4332 SetDisplayNameAsyncData *data;
4334 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4336 data = g_simple_async_result_get_op_res_gpointer (simple);
4338 return g_object_ref (data->file);
4344 GFileQueryInfoFlags flags;
4351 set_info_data_free (SetInfoAsyncData *data)
4354 g_object_unref (data->info);
4356 g_error_free (data->error);
4361 set_info_async_thread (GSimpleAsyncResult *res,
4363 GCancellable *cancellable)
4365 SetInfoAsyncData *data;
4367 data = g_simple_async_result_get_op_res_gpointer (res);
4370 data->res = g_file_set_attributes_from_info (G_FILE (object),
4378 g_file_real_set_attributes_async (GFile *file,
4380 GFileQueryInfoFlags flags,
4382 GCancellable *cancellable,
4383 GAsyncReadyCallback callback,
4386 GSimpleAsyncResult *res;
4387 SetInfoAsyncData *data;
4389 data = g_new0 (SetInfoAsyncData, 1);
4390 data->info = g_file_info_dup (info);
4391 data->flags = flags;
4393 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4394 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4396 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4397 g_object_unref (res);
4401 g_file_real_set_attributes_finish (GFile *file,
4406 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4407 SetInfoAsyncData *data;
4409 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4411 data = g_simple_async_result_get_op_res_gpointer (simple);
4414 *info = g_object_ref (data->info);
4416 if (error != NULL && data->error)
4417 *error = g_error_copy (data->error);
4423 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4425 GCancellable *cancellable)
4427 GError *error = NULL;
4430 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4434 g_simple_async_result_set_from_error (res, error);
4435 g_error_free (error);
4438 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4442 g_file_real_find_enclosing_mount_async (GFile *file,
4444 GCancellable *cancellable,
4445 GAsyncReadyCallback callback,
4448 GSimpleAsyncResult *res;
4450 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4452 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4453 g_object_unref (res);
4457 g_file_real_find_enclosing_mount_finish (GFile *file,
4461 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4464 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4466 mount = g_simple_async_result_get_op_res_gpointer (simple);
4467 return g_object_ref (mount);
4474 GFileCopyFlags flags;
4475 GFileProgressCallback progress_cb;
4476 gpointer progress_cb_data;
4477 GIOSchedulerJob *job;
4481 copy_async_data_free (CopyAsyncData *data)
4483 g_object_unref (data->source);
4484 g_object_unref (data->destination);
4489 CopyAsyncData *data;
4490 goffset current_num_bytes;
4491 goffset total_num_bytes;
4495 copy_async_progress_in_main (gpointer user_data)
4497 ProgressData *progress = user_data;
4498 CopyAsyncData *data = progress->data;
4500 data->progress_cb (progress->current_num_bytes,
4501 progress->total_num_bytes,
4502 data->progress_cb_data);
4508 mainloop_barrier (gpointer user_data)
4510 /* Does nothing, but ensures all queued idles before
4517 copy_async_progress_callback (goffset current_num_bytes,
4518 goffset total_num_bytes,
4521 CopyAsyncData *data = user_data;
4522 ProgressData *progress;
4524 progress = g_new (ProgressData, 1);
4525 progress->data = data;
4526 progress->current_num_bytes = current_num_bytes;
4527 progress->total_num_bytes = total_num_bytes;
4529 g_io_scheduler_job_send_to_mainloop_async (data->job,
4530 copy_async_progress_in_main,
4536 copy_async_thread (GIOSchedulerJob *job,
4537 GCancellable *cancellable,
4540 GSimpleAsyncResult *res;
4541 CopyAsyncData *data;
4546 data = g_simple_async_result_get_op_res_gpointer (res);
4550 result = g_file_copy (data->source,
4554 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4558 /* Ensure all progress callbacks are done running in main thread */
4559 if (data->progress_cb != NULL)
4560 g_io_scheduler_job_send_to_mainloop (job,
4566 g_simple_async_result_set_from_error (res, error);
4567 g_error_free (error);
4570 g_simple_async_result_complete_in_idle (res);
4576 g_file_real_copy_async (GFile *source,
4578 GFileCopyFlags flags,
4580 GCancellable *cancellable,
4581 GFileProgressCallback progress_callback,
4582 gpointer progress_callback_data,
4583 GAsyncReadyCallback callback,
4586 GSimpleAsyncResult *res;
4587 CopyAsyncData *data;
4589 data = g_new0 (CopyAsyncData, 1);
4590 data->source = g_object_ref (source);
4591 data->destination = g_object_ref (destination);
4592 data->flags = flags;
4593 data->progress_cb = progress_callback;
4594 data->progress_cb_data = progress_callback_data;
4596 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4597 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4599 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4603 g_file_real_copy_finish (GFile *file,
4607 /* Error handled in g_file_copy_finish() */
4612 /********************************************
4613 * Default VFS operations *
4614 ********************************************/
4617 * g_file_new_for_path:
4618 * @path: a string containing a relative or absolute path.
4620 * Constructs a #GFile for a given path. This operation never
4621 * fails, but the returned object might not support any I/O
4622 * operation if @path is malformed.
4624 * Returns: a new #GFile for the given @path.
4627 g_file_new_for_path (const char *path)
4629 g_return_val_if_fail (path != NULL, NULL);
4631 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4635 * g_file_new_for_uri:
4636 * @uri: a string containing a URI.
4638 * Constructs a #GFile for a given URI. This operation never
4639 * fails, but the returned object might not support any I/O
4640 * operation if @uri is malformed or if the uri type is
4643 * Returns: a #GFile for the given @uri.
4646 g_file_new_for_uri (const char *uri)
4648 g_return_val_if_fail (uri != NULL, NULL);
4650 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4654 * g_file_parse_name:
4655 * @parse_name: a file name or path to be parsed.
4657 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4658 * This operation never fails, but the returned object might not support any I/O
4659 * operation if the @parse_name cannot be parsed.
4661 * Returns: a new #GFile.
4664 g_file_parse_name (const char *parse_name)
4666 g_return_val_if_fail (parse_name != NULL, NULL);
4668 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4672 is_valid_scheme_character (char c)
4674 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4678 has_valid_scheme (const char *uri)
4684 if (!is_valid_scheme_character (*p))
4689 } while (is_valid_scheme_character (*p));
4695 * g_file_new_for_commandline_arg:
4696 * @arg: a command line string.
4698 * Creates a #GFile with the given argument from the command line. The value of
4699 * @arg can be either a URI, an absolute path or a relative path resolved
4700 * relative to the current working directory.
4701 * This operation never fails, but the returned object might not support any
4702 * I/O operation if @arg points to a malformed path.
4704 * Returns: a new #GFile.
4707 g_file_new_for_commandline_arg (const char *arg)
4713 g_return_val_if_fail (arg != NULL, NULL);
4715 if (g_path_is_absolute (arg))
4716 return g_file_new_for_path (arg);
4718 if (has_valid_scheme (arg))
4719 return g_file_new_for_uri (arg);
4721 current_dir = g_get_current_dir ();
4722 filename = g_build_filename (current_dir, arg, NULL);
4723 g_free (current_dir);
4725 file = g_file_new_for_path (filename);
4732 * g_file_mount_enclosing_volume:
4733 * @location: input #GFile.
4734 * @flags: flags affecting the operation
4735 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4736 * @cancellable: optional #GCancellable object, %NULL to ignore.
4737 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4738 * @user_data: the data to pass to callback function
4740 * Starts a @mount_operation, mounting the volume that contains the file @location.
4742 * When this operation has completed, @callback will be called with
4743 * @user_user data, and the operation can be finalized with
4744 * g_file_mount_enclosing_volume_finish().
4746 * If @cancellable is not %NULL, then the operation can be cancelled by
4747 * triggering the cancellable object from another thread. If the operation
4748 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4751 g_file_mount_enclosing_volume (GFile *location,
4752 GMountMountFlags flags,
4753 GMountOperation *mount_operation,
4754 GCancellable *cancellable,
4755 GAsyncReadyCallback callback,
4760 g_return_if_fail (G_IS_FILE (location));
4762 iface = G_FILE_GET_IFACE (location);
4764 if (iface->mount_enclosing_volume == NULL)
4766 g_simple_async_report_error_in_idle (G_OBJECT (location),
4767 callback, user_data,
4768 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4769 _("volume doesn't implement mount"));
4774 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4779 * g_file_mount_enclosing_volume_finish:
4780 * @location: input #GFile.
4781 * @result: a #GAsyncResult.
4782 * @error: a #GError, or %NULL
4784 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4786 * Returns: %TRUE if successful. If an error
4787 * has occurred, this function will return %FALSE and set @error
4788 * appropriately if present.
4791 g_file_mount_enclosing_volume_finish (GFile *location,
4792 GAsyncResult *result,
4797 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4798 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4800 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4802 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4803 if (g_simple_async_result_propagate_error (simple, error))
4807 iface = G_FILE_GET_IFACE (location);
4809 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4812 /********************************************
4813 * Utility functions *
4814 ********************************************/
4817 * g_file_query_default_handler:
4818 * @file: a #GFile to open.
4819 * @cancellable: optional #GCancellable object, %NULL to ignore.
4820 * @error: a #GError, or %NULL
4822 * Returns the #GAppInfo that is registered as the default
4823 * application to handle the file specified by @file.
4825 * If @cancellable is not %NULL, then the operation can be cancelled by
4826 * triggering the cancellable object from another thread. If the operation
4827 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4829 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4830 * When you are done with it, release it with g_object_unref()
4833 g_file_query_default_handler (GFile *file,
4834 GCancellable *cancellable,
4838 const char *content_type;
4843 uri_scheme = g_file_get_uri_scheme (file);
4844 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4845 g_free (uri_scheme);
4847 if (appinfo != NULL)
4850 info = g_file_query_info (file,
4851 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4860 content_type = g_file_info_get_content_type (info);
4863 /* Don't use is_native(), as we want to support fuse paths if availible */
4864 path = g_file_get_path (file);
4865 appinfo = g_app_info_get_default_for_type (content_type,
4870 g_object_unref (info);
4872 if (appinfo != NULL)
4875 g_set_error (error, G_IO_ERROR,
4876 G_IO_ERROR_NOT_SUPPORTED,
4877 _("No application is registered as handling this file"));
4883 #define GET_CONTENT_BLOCK_SIZE 8192
4886 * g_file_load_contents:
4887 * @file: input #GFile.
4888 * @cancellable: optional #GCancellable object, %NULL to ignore.
4889 * @contents: a location to place the contents of the file.
4890 * @length: a location to place the length of the contents of the file.
4891 * @etag_out: a location to place the current entity tag for the file.
4892 * @error: a #GError, or %NULL
4894 * Loads the content of the file into memory, returning the size of
4895 * the data. The data is always zero terminated, but this is not
4896 * included in the resultant @length.
4898 * If @cancellable is not %NULL, then the operation can be cancelled by
4899 * triggering the cancellable object from another thread. If the operation
4900 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4902 * Returns: %TRUE if the @file's contents were successfully loaded.
4903 * %FALSE if there were errors..
4906 g_file_load_contents (GFile *file,
4907 GCancellable *cancellable,
4913 GFileInputStream *in;
4914 GByteArray *content;
4919 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4920 g_return_val_if_fail (contents != NULL, FALSE);
4922 in = g_file_read (file, cancellable, error);
4926 content = g_byte_array_new ();
4929 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4930 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4931 content->data + pos,
4932 GET_CONTENT_BLOCK_SIZE,
4933 cancellable, error)) > 0)
4936 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4943 info = g_file_input_stream_query_info (in,
4944 G_FILE_ATTRIBUTE_ETAG_VALUE,
4949 *etag_out = g_strdup (g_file_info_get_etag (info));
4950 g_object_unref (info);
4954 /* Ignore errors on close */
4955 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4956 g_object_unref (in);
4960 /* error is set already */
4961 g_byte_array_free (content, TRUE);
4968 /* Zero terminate (we got an extra byte allocated for this */
4969 content->data[pos] = 0;
4971 *contents = (char *)g_byte_array_free (content, FALSE);
4979 GCancellable *cancellable;
4980 GFileReadMoreCallback read_more_callback;
4981 GAsyncReadyCallback callback;
4983 GByteArray *content;
4990 load_contents_data_free (LoadContentsData *data)
4993 g_error_free (data->error);
4994 if (data->cancellable)
4995 g_object_unref (data->cancellable);
4997 g_byte_array_free (data->content, TRUE);
4998 g_free (data->etag);
4999 g_object_unref (data->file);
5004 load_contents_close_callback (GObject *obj,
5005 GAsyncResult *close_res,
5008 GInputStream *stream = G_INPUT_STREAM (obj);
5009 LoadContentsData *data = user_data;
5010 GSimpleAsyncResult *res;
5012 /* Ignore errors here, we're only reading anyway */
5013 g_input_stream_close_finish (stream, close_res, NULL);
5014 g_object_unref (stream);
5016 res = g_simple_async_result_new (G_OBJECT (data->file),
5019 g_file_load_contents_async);
5020 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5021 g_simple_async_result_complete (res);
5022 g_object_unref (res);
5026 load_contents_fstat_callback (GObject *obj,
5027 GAsyncResult *stat_res,
5030 GInputStream *stream = G_INPUT_STREAM (obj);
5031 LoadContentsData *data = user_data;
5034 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5038 data->etag = g_strdup (g_file_info_get_etag (info));
5039 g_object_unref (info);
5042 g_input_stream_close_async (stream, 0,
5044 load_contents_close_callback, data);
5048 load_contents_read_callback (GObject *obj,
5049 GAsyncResult *read_res,
5052 GInputStream *stream = G_INPUT_STREAM (obj);
5053 LoadContentsData *data = user_data;
5054 GError *error = NULL;
5057 read_size = g_input_stream_read_finish (stream, read_res, &error);
5061 /* Error or EOF, close the file */
5062 data->error = error;
5063 g_input_stream_close_async (stream, 0,
5065 load_contents_close_callback, data);
5067 else if (read_size == 0)
5069 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5070 G_FILE_ATTRIBUTE_ETAG_VALUE,
5073 load_contents_fstat_callback,
5076 else if (read_size > 0)
5078 data->pos += read_size;
5080 g_byte_array_set_size (data->content,
5081 data->pos + GET_CONTENT_BLOCK_SIZE);
5084 if (data->read_more_callback &&
5085 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5086 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5087 G_FILE_ATTRIBUTE_ETAG_VALUE,
5090 load_contents_fstat_callback,
5093 g_input_stream_read_async (stream,
5094 data->content->data + data->pos,
5095 GET_CONTENT_BLOCK_SIZE,
5098 load_contents_read_callback,
5104 load_contents_open_callback (GObject *obj,
5105 GAsyncResult *open_res,
5108 GFile *file = G_FILE (obj);
5109 GFileInputStream *stream;
5110 LoadContentsData *data = user_data;
5111 GError *error = NULL;
5112 GSimpleAsyncResult *res;
5114 stream = g_file_read_finish (file, open_res, &error);
5118 g_byte_array_set_size (data->content,
5119 data->pos + GET_CONTENT_BLOCK_SIZE);
5120 g_input_stream_read_async (G_INPUT_STREAM (stream),
5121 data->content->data + data->pos,
5122 GET_CONTENT_BLOCK_SIZE,
5125 load_contents_read_callback,
5131 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5135 g_simple_async_result_complete (res);
5136 g_error_free (error);
5137 load_contents_data_free (data);
5138 g_object_unref (res);
5143 * g_file_load_partial_contents_async:
5144 * @file: input #GFile.
5145 * @cancellable: optional #GCancellable object, %NULL to ignore.
5146 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5147 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5148 * @user_data: the data to pass to the callback functions.
5150 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5151 * used to stop reading from the file when appropriate, else this function
5152 * will behave exactly as g_file_load_contents_async(). This operation
5153 * can be finished by g_file_load_partial_contents_finish().
5155 * Users of this function should be aware that @user_data is passed to
5156 * both the @read_more_callback and the @callback.
5158 * If @cancellable is not %NULL, then the operation can be cancelled by
5159 * triggering the cancellable object from another thread. If the operation
5160 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5163 g_file_load_partial_contents_async (GFile *file,
5164 GCancellable *cancellable,
5165 GFileReadMoreCallback read_more_callback,
5166 GAsyncReadyCallback callback,
5169 LoadContentsData *data;
5171 g_return_if_fail (G_IS_FILE (file));
5173 data = g_new0 (LoadContentsData, 1);
5176 data->cancellable = g_object_ref (cancellable);
5177 data->read_more_callback = read_more_callback;
5178 data->callback = callback;
5179 data->user_data = user_data;
5180 data->content = g_byte_array_new ();
5181 data->file = g_object_ref (file);
5183 g_file_read_async (file,
5186 load_contents_open_callback,
5191 * g_file_load_partial_contents_finish:
5192 * @file: input #GFile.
5193 * @res: a #GAsyncResult.
5194 * @contents: a location to place the contents of the file.
5195 * @length: a location to place the length of the contents of the file.
5196 * @etag_out: a location to place the current entity tag for the file.
5197 * @error: a #GError, or %NULL
5199 * Finishes an asynchronous partial load operation that was started
5200 * with g_file_load_partial_contents_async().
5202 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5203 * present, it will be set appropriately.
5206 g_file_load_partial_contents_finish (GFile *file,
5213 GSimpleAsyncResult *simple;
5214 LoadContentsData *data;
5216 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5217 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5218 g_return_val_if_fail (contents != NULL, FALSE);
5220 simple = G_SIMPLE_ASYNC_RESULT (res);
5222 if (g_simple_async_result_propagate_error (simple, error))
5225 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5227 data = g_simple_async_result_get_op_res_gpointer (simple);
5231 g_propagate_error (error, data->error);
5240 *length = data->pos;
5244 *etag_out = data->etag;
5248 /* Zero terminate */
5249 g_byte_array_set_size (data->content, data->pos + 1);
5250 data->content->data[data->pos] = 0;
5252 *contents = (char *)g_byte_array_free (data->content, FALSE);
5253 data->content = NULL;
5259 * g_file_load_contents_async:
5260 * @file: input #GFile.
5261 * @cancellable: optional #GCancellable object, %NULL to ignore.
5262 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5263 * @user_data: the data to pass to callback function
5265 * Starts an asynchronous load of the @file's contents.
5267 * For more details, see g_file_load_contents() which is
5268 * the synchronous version of this call.
5270 * When the load operation has completed, @callback will be called
5271 * with @user data. To finish the operation, call
5272 * g_file_load_contents_finish() with the #GAsyncResult returned by
5275 * If @cancellable is not %NULL, then the operation can be cancelled by
5276 * triggering the cancellable object from another thread. If the operation
5277 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5280 g_file_load_contents_async (GFile *file,
5281 GCancellable *cancellable,
5282 GAsyncReadyCallback callback,
5285 g_file_load_partial_contents_async (file,
5288 callback, user_data);
5292 * g_file_load_contents_finish:
5293 * @file: input #GFile.
5294 * @res: a #GAsyncResult.
5295 * @contents: a location to place the contents of the file.
5296 * @length: a location to place the length of the contents of the file.
5297 * @etag_out: a location to place the current entity tag for the file.
5298 * @error: a #GError, or %NULL
5300 * Finishes an asynchronous load of the @file's contents.
5301 * The contents are placed in @contents, and @length is set to the
5302 * size of the @contents string. If @etag_out is present, it will be
5303 * set to the new entity tag for the @file.
5305 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5306 * present, it will be set appropriately.
5309 g_file_load_contents_finish (GFile *file,
5316 return g_file_load_partial_contents_finish (file,
5325 * g_file_replace_contents:
5326 * @file: input #GFile.
5327 * @contents: a string containing the new contents for @file.
5328 * @length: the length of @contents in bytes.
5329 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5331 * @make_backup: %TRUE if a backup should be created.
5332 * @flags: a set of #GFileCreateFlags.
5333 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5334 * for the document. This should be freed with g_free() when no longer
5336 * @cancellable: optional #GCancellable object, %NULL to ignore.
5337 * @error: a #GError, or %NULL
5339 * Replaces the contents of @file with @contents of @length bytes.
5341 * If @etag is specified (not %NULL) any existing file must have that etag, or
5342 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5344 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5346 * If @cancellable is not %NULL, then the operation can be cancelled by
5347 * triggering the cancellable object from another thread. If the operation
5348 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5350 * The returned @new_etag can be used to verify that the file hasn't changed the
5351 * next time it is saved over.
5353 * Returns: %TRUE if successful. If an error
5354 * has occurred, this function will return %FALSE and set @error
5355 * appropriately if present.
5358 g_file_replace_contents (GFile *file,
5359 const char *contents,
5362 gboolean make_backup,
5363 GFileCreateFlags flags,
5365 GCancellable *cancellable,
5368 GFileOutputStream *out;
5369 gsize pos, remainder;
5372 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5373 g_return_val_if_fail (contents != NULL, FALSE);
5375 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5381 while (remainder > 0 &&
5382 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5384 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5392 if (remainder > 0 && res < 0)
5394 /* Ignore errors on close */
5395 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5397 /* error is set already */
5401 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5405 *new_etag = g_file_output_stream_get_etag (out);
5413 GCancellable *cancellable;
5414 GAsyncReadyCallback callback;
5416 const char *content;
5420 } ReplaceContentsData;
5423 replace_contents_data_free (ReplaceContentsData *data)
5426 g_error_free (data->error);
5427 if (data->cancellable)
5428 g_object_unref (data->cancellable);
5429 g_object_unref (data->file);
5430 g_free (data->etag);
5435 replace_contents_close_callback (GObject *obj,
5436 GAsyncResult *close_res,
5439 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5440 ReplaceContentsData *data = user_data;
5441 GSimpleAsyncResult *res;
5443 /* Ignore errors here, we're only reading anyway */
5444 g_output_stream_close_finish (stream, close_res, NULL);
5445 g_object_unref (stream);
5447 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5449 res = g_simple_async_result_new (G_OBJECT (data->file),
5452 g_file_replace_contents_async);
5453 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5454 g_simple_async_result_complete (res);
5455 g_object_unref (res);
5459 replace_contents_write_callback (GObject *obj,
5460 GAsyncResult *read_res,
5463 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5464 ReplaceContentsData *data = user_data;
5465 GError *error = NULL;
5468 write_size = g_output_stream_write_finish (stream, read_res, &error);
5470 if (write_size <= 0)
5472 /* Error or EOF, close the file */
5474 data->error = error;
5475 g_output_stream_close_async (stream, 0,
5477 replace_contents_close_callback, data);
5479 else if (write_size > 0)
5481 data->pos += write_size;
5483 if (data->pos >= data->length)
5484 g_output_stream_close_async (stream, 0,
5486 replace_contents_close_callback, data);
5488 g_output_stream_write_async (stream,
5489 data->content + data->pos,
5490 data->length - data->pos,
5493 replace_contents_write_callback,
5499 replace_contents_open_callback (GObject *obj,
5500 GAsyncResult *open_res,
5503 GFile *file = G_FILE (obj);
5504 GFileOutputStream *stream;
5505 ReplaceContentsData *data = user_data;
5506 GError *error = NULL;
5507 GSimpleAsyncResult *res;
5509 stream = g_file_replace_finish (file, open_res, &error);
5513 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5514 data->content + data->pos,
5515 data->length - data->pos,
5518 replace_contents_write_callback,
5524 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5528 g_simple_async_result_complete (res);
5529 g_error_free (error);
5530 replace_contents_data_free (data);
5531 g_object_unref (res);
5536 * g_file_replace_contents_async:
5537 * @file: input #GFile.
5538 * @contents: string of contents to replace the file with.
5539 * @length: the length of @contents in bytes.
5540 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5541 * @make_backup: %TRUE if a backup should be created.
5542 * @flags: a set of #GFileCreateFlags.
5543 * @cancellable: optional #GCancellable object, %NULL to ignore.
5544 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5545 * @user_data: the data to pass to callback function
5547 * Starts an asynchronous replacement of @file with the given
5548 * @contents of @length bytes. @etag will replace the document's
5549 * current entity tag.
5551 * When this operation has completed, @callback will be called with
5552 * @user_user data, and the operation can be finalized with
5553 * g_file_replace_contents_finish().
5555 * If @cancellable is not %NULL, then the operation can be cancelled by
5556 * triggering the cancellable object from another thread. If the operation
5557 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5559 * If @make_backup is %TRUE, this function will attempt to
5560 * make a backup of @file.
5563 g_file_replace_contents_async (GFile *file,
5564 const char *contents,
5567 gboolean make_backup,
5568 GFileCreateFlags flags,
5569 GCancellable *cancellable,
5570 GAsyncReadyCallback callback,
5573 ReplaceContentsData *data;
5575 g_return_if_fail (G_IS_FILE (file));
5576 g_return_if_fail (contents != NULL);
5578 data = g_new0 (ReplaceContentsData, 1);
5581 data->cancellable = g_object_ref (cancellable);
5582 data->callback = callback;
5583 data->user_data = user_data;
5584 data->content = contents;
5585 data->length = length;
5587 data->file = g_object_ref (file);
5589 g_file_replace_async (file,
5595 replace_contents_open_callback,
5600 * g_file_replace_contents_finish:
5601 * @file: input #GFile.
5602 * @res: a #GAsyncResult.
5603 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5604 * for the document. This should be freed with g_free() when it is no
5606 * @error: a #GError, or %NULL
5608 * Finishes an asynchronous replace of the given @file. See
5609 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5610 * tag for the document, if present.
5612 * Returns: %TRUE on success, %FALSE on failure.
5615 g_file_replace_contents_finish (GFile *file,
5620 GSimpleAsyncResult *simple;
5621 ReplaceContentsData *data;
5623 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5624 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5626 simple = G_SIMPLE_ASYNC_RESULT (res);
5628 if (g_simple_async_result_propagate_error (simple, error))
5631 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5633 data = g_simple_async_result_get_op_res_gpointer (simple);
5637 g_propagate_error (error, data->error);
5645 *new_etag = data->etag;
5646 data->etag = NULL; /* Take ownership */
5652 #define __G_FILE_C__
5653 #include "gioaliasdef.c"