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 (dupplicated 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()
103 * Some #GFile operations do not have synchronous analogs, as they may
104 * take a very long time to finish, and blocking may leave an application
105 * unusable. Notable cases include:
106 * g_file_mount_mountable() to mount a mountable file.
107 * g_file_unmount_mountable() to unmount a mountable file.
108 * g_file_eject_mountable() to eject a mountable file.
110 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
111 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
112 * short. Entity tags are somewhat like a more abstract version of the
113 * traditional mtime, and can be used to quickly determine if the file has
114 * been modified from the version on the file system. See the HTTP 1.1
115 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
116 * for HTTP Etag headers, which are a very similar concept.
120 static void g_file_base_init (gpointer g_class);
121 static void g_file_class_init (gpointer g_class,
122 gpointer class_data);
124 static void g_file_real_query_info_async (GFile *file,
125 const char *attributes,
126 GFileQueryInfoFlags flags,
128 GCancellable *cancellable,
129 GAsyncReadyCallback callback,
131 static GFileInfo * g_file_real_query_info_finish (GFile *file,
134 static void g_file_real_query_filesystem_info_async (GFile *file,
135 const char *attributes,
137 GCancellable *cancellable,
138 GAsyncReadyCallback callback,
140 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
143 static void g_file_real_enumerate_children_async (GFile *file,
144 const char *attributes,
145 GFileQueryInfoFlags flags,
147 GCancellable *cancellable,
148 GAsyncReadyCallback callback,
150 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
153 static void g_file_real_read_async (GFile *file,
155 GCancellable *cancellable,
156 GAsyncReadyCallback callback,
158 static GFileInputStream * g_file_real_read_finish (GFile *file,
161 static void g_file_real_append_to_async (GFile *file,
162 GFileCreateFlags flags,
164 GCancellable *cancellable,
165 GAsyncReadyCallback callback,
167 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
170 static void g_file_real_create_async (GFile *file,
171 GFileCreateFlags flags,
173 GCancellable *cancellable,
174 GAsyncReadyCallback callback,
176 static GFileOutputStream *g_file_real_create_finish (GFile *file,
179 static void g_file_real_replace_async (GFile *file,
181 gboolean make_backup,
182 GFileCreateFlags flags,
184 GCancellable *cancellable,
185 GAsyncReadyCallback callback,
187 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
190 static gboolean g_file_real_set_attributes_from_info (GFile *file,
192 GFileQueryInfoFlags flags,
193 GCancellable *cancellable,
195 static void g_file_real_set_display_name_async (GFile *file,
196 const char *display_name,
198 GCancellable *cancellable,
199 GAsyncReadyCallback callback,
201 static GFile * g_file_real_set_display_name_finish (GFile *file,
204 static void g_file_real_set_attributes_async (GFile *file,
206 GFileQueryInfoFlags flags,
208 GCancellable *cancellable,
209 GAsyncReadyCallback callback,
211 static gboolean g_file_real_set_attributes_finish (GFile *file,
215 static void g_file_real_find_enclosing_mount_async (GFile *file,
217 GCancellable *cancellable,
218 GAsyncReadyCallback callback,
220 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
223 static void g_file_real_copy_async (GFile *source,
225 GFileCopyFlags flags,
227 GCancellable *cancellable,
228 GFileProgressCallback progress_callback,
229 gpointer progress_callback_data,
230 GAsyncReadyCallback callback,
232 static gboolean g_file_real_copy_finish (GFile *file,
237 g_file_get_type (void)
239 static GType file_type = 0;
243 static const GTypeInfo file_info =
245 sizeof (GFileIface), /* class_size */
246 g_file_base_init, /* base_init */
247 NULL, /* base_finalize */
249 NULL, /* class_finalize */
250 NULL, /* class_data */
257 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
260 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
267 g_file_class_init (gpointer g_class,
270 GFileIface *iface = g_class;
272 iface->enumerate_children_async = g_file_real_enumerate_children_async;
273 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
274 iface->set_display_name_async = g_file_real_set_display_name_async;
275 iface->set_display_name_finish = g_file_real_set_display_name_finish;
276 iface->query_info_async = g_file_real_query_info_async;
277 iface->query_info_finish = g_file_real_query_info_finish;
278 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
279 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
280 iface->set_attributes_async = g_file_real_set_attributes_async;
281 iface->set_attributes_finish = g_file_real_set_attributes_finish;
282 iface->read_async = g_file_real_read_async;
283 iface->read_finish = g_file_real_read_finish;
284 iface->append_to_async = g_file_real_append_to_async;
285 iface->append_to_finish = g_file_real_append_to_finish;
286 iface->create_async = g_file_real_create_async;
287 iface->create_finish = g_file_real_create_finish;
288 iface->replace_async = g_file_real_replace_async;
289 iface->replace_finish = g_file_real_replace_finish;
290 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
291 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
292 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
293 iface->copy_async = g_file_real_copy_async;
294 iface->copy_finish = g_file_real_copy_finish;
298 g_file_base_init (gpointer g_class)
305 * @file: input #GFile.
307 * Checks to see if a file is native to the platform.
309 * A native file s one expressed in the platform-native filename format,
310 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
311 * as it might be on a locally mounted remote filesystem.
313 * On some systems non-native files may be available using
314 * the native filesystem via a userspace filesystem (FUSE), in
315 * these cases this call will return %FALSE, but g_file_get_path()
316 * will still return a native path.
318 * This call does no blocking i/o.
320 * Returns: %TRUE if file is native.
323 g_file_is_native (GFile *file)
327 g_return_val_if_fail (G_IS_FILE (file), FALSE);
329 iface = G_FILE_GET_IFACE (file);
331 return (* iface->is_native) (file);
336 * g_file_has_uri_scheme:
337 * @file: input #GFile.
338 * @uri_scheme: a string containing a URI scheme.
340 * Checks to see if a #GFile has a given URI scheme.
342 * This call does no blocking i/o.
344 * Returns: %TRUE if #GFile's backend supports the
345 * given URI scheme, %FALSE if URI scheme is %NULL,
346 * not supported, or #GFile is invalid.
349 g_file_has_uri_scheme (GFile *file,
350 const char *uri_scheme)
354 g_return_val_if_fail (G_IS_FILE (file), FALSE);
355 g_return_val_if_fail (uri_scheme != NULL, FALSE);
357 iface = G_FILE_GET_IFACE (file);
359 return (* iface->has_uri_scheme) (file, uri_scheme);
364 * g_file_get_uri_scheme:
365 * @file: input #GFile.
367 * Gets the URI scheme for a #GFile.
368 * RFC 3986 decodes the scheme as:
370 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
372 * Common schemes include "file", "http", "ftp", etc.
374 * This call does no blocking i/o.
376 * Returns: a string containing the URI scheme for the given
377 * #GFile. The returned string should be freed with g_free()
378 * when no longer needed.
381 g_file_get_uri_scheme (GFile *file)
385 g_return_val_if_fail (G_IS_FILE (file), NULL);
387 iface = G_FILE_GET_IFACE (file);
389 return (* iface->get_uri_scheme) (file);
394 * g_file_get_basename:
395 * @file: input #GFile.
397 * Gets the base name (the last component of the path) for a given #GFile.
399 * If called for the top level of a system (such as the filesystem root
400 * or a uri like sftp://host/) it will return a single directory separator
401 * (and on Windows, possibly a drive letter).
403 * The base name is a byte string (*not* UTF-8). It has no defined encoding
404 * or rules other than it may not contain zero bytes. If you want to use
405 * filenames in a user interface you should use the display name that you
406 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
407 * attribute with g_file_query_info().
409 * This call does no blocking i/o.
411 * Returns: string containing the #GFile's base name, or %NULL
412 * if given #GFile is invalid. The returned string should be
413 * freed with g_free() when no longer needed.
416 g_file_get_basename (GFile *file)
420 g_return_val_if_fail (G_IS_FILE (file), NULL);
422 iface = G_FILE_GET_IFACE (file);
424 return (* iface->get_basename) (file);
429 * @file: input #GFile.
431 * Gets the local pathname for #GFile, if one exists.
433 * This call does no blocking i/o.
435 * Returns: string containing the #GFile's path, or %NULL if
436 * no such path exists. The returned string should be
437 * freed with g_free() when no longer needed.
440 g_file_get_path (GFile *file)
444 g_return_val_if_fail (G_IS_FILE (file), NULL);
446 iface = G_FILE_GET_IFACE (file);
448 return (* iface->get_path) (file);
453 * @file: input #GFile.
455 * Gets the URI for the @file.
457 * This call does no blocking i/o.
459 * Returns: a string containing the #GFile's URI.
460 * The returned string should be freed with g_free() when no longer needed.
463 g_file_get_uri (GFile *file)
467 g_return_val_if_fail (G_IS_FILE (file), NULL);
469 iface = G_FILE_GET_IFACE (file);
471 return (* iface->get_uri) (file);
475 * g_file_get_parse_name:
476 * @file: input #GFile.
478 * Gets the parse name of the @file.
479 * A parse name is a UTF-8 string that describes the
480 * file such that one can get the #GFile back using
481 * g_file_parse_name().
483 * This is generally used to show the #GFile as a nice
484 * full-pathname kind of string in a user interface,
485 * like in a location entry.
487 * For local files with names that can safely be converted
488 * to UTF8 the pathname is used, otherwise the IRI is used
489 * (a form of URI that allows UTF8 characters unescaped).
491 * This call does no blocking i/o.
493 * Returns: a string containing the #GFile's parse name. The returned
494 * string should be freed with g_free() when no longer needed.
497 g_file_get_parse_name (GFile *file)
501 g_return_val_if_fail (G_IS_FILE (file), NULL);
503 iface = G_FILE_GET_IFACE (file);
505 return (* iface->get_parse_name) (file);
510 * @file: input #GFile.
512 * Duplicates a #GFile handle. This operation does not duplicate
513 * the actual file or directory represented by the #GFile; see
514 * g_file_copy() if attempting to copy a file.
516 * This call does no blocking i/o.
518 * Returns: #GFile that is a duplicate of the given #GFile.
521 g_file_dup (GFile *file)
525 g_return_val_if_fail (G_IS_FILE (file), NULL);
527 iface = G_FILE_GET_IFACE (file);
529 return (* iface->dup) (file);
534 * @file: #gconstpointer to a #GFile.
536 * Creates a hash value for a #GFile.
538 * This call does no blocking i/o.
540 * Returns: 0 if @file is not a valid #GFile, otherwise an
541 * integer that can be used as hash value for the #GFile.
542 * This function is intended for easily hashing a #GFile to
543 * add to a #GHashTable or similar data structure.
546 g_file_hash (gconstpointer file)
550 g_return_val_if_fail (G_IS_FILE (file), 0);
552 iface = G_FILE_GET_IFACE (file);
554 return (* iface->hash) ((GFile *)file);
559 * @file1: the first #GFile.
560 * @file2: the second #GFile.
562 * Checks equality of two given #GFile<!-- -->s. Note that two
563 * #GFile<!-- -->s that differ can still refer to the same
564 * file on the filesystem due to various forms of filename
567 * This call does no blocking i/o.
569 * Returns: %TRUE if @file1 and @file2 are equal.
570 * %FALSE if either is not a #GFile.
573 g_file_equal (GFile *file1,
578 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
579 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
581 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
584 iface = G_FILE_GET_IFACE (file1);
586 return (* iface->equal) (file1, file2);
592 * @file: input #GFile.
594 * Gets the parent directory for the @file.
595 * If the @file represents the root directory of the
596 * file system, then %NULL will be returned.
598 * This call does no blocking i/o.
600 * Returns: a #GFile structure to the parent of the given
601 * #GFile or %NULL if there is no parent.
604 g_file_get_parent (GFile *file)
608 g_return_val_if_fail (G_IS_FILE (file), NULL);
610 iface = G_FILE_GET_IFACE (file);
612 return (* iface->get_parent) (file);
617 * @file: input #GFile.
618 * @name: string containing the child's basename.
620 * Gets a child of @file with basename equal to @name.
622 * Note that the file with that specific name might not exist, but
623 * you can still have a #GFile that points to it. You can use this
624 * for instance to create that file.
626 * This call does no blocking i/o.
628 * Returns: a #GFile to a child specified by @name.
631 g_file_get_child (GFile *file,
634 g_return_val_if_fail (G_IS_FILE (file), NULL);
635 g_return_val_if_fail (name != NULL, NULL);
637 return g_file_resolve_relative_path (file, name);
641 * g_file_get_child_for_display_name:
642 * @file: input #GFile.
643 * @display_name: string to a possible child.
646 * Gets the child of @file for a given @display_name (i.e. a UTF8
647 * version of the name). If this function fails, it returns %NULL and @error will be
648 * set. This is very useful when constructing a GFile for a new file
649 * and the user entered the filename in the user interface, for instance
650 * when you select a directory and type a filename in the file selector.
652 * This call does no blocking i/o.
654 * Returns: a #GFile to the specified child, or
655 * %NULL if the display name couldn't be converted.
658 g_file_get_child_for_display_name (GFile *file,
659 const char *display_name,
664 g_return_val_if_fail (G_IS_FILE (file), NULL);
665 g_return_val_if_fail (display_name != NULL, NULL);
667 iface = G_FILE_GET_IFACE (file);
669 return (* iface->get_child_for_display_name) (file, display_name, error);
672 /* Temporary keep this symbol for one release */
673 gboolean g_file_contains_file (GFile *parent, GFile *descendant);
675 g_file_contains_file (GFile *parent,
678 /* This function is not in the header and will not be referenced by newly built code */
679 return g_file_has_prefix (descendant, parent);
684 * @file: input #GFile.
685 * @prefix: input #GFile.
687 * Checks whether @file has the prefix specified by @prefix. In other word, if the
688 * names of inital elements of @file<!-- -->s pathname match @prefix.
690 * This call does no i/o, as it works purely on names. As such it can sometimes
691 * return %FALSE even if @file is inside a @prefix (from a filesystem point of view),
692 * because the prefix of @file is an alias of @prefix.
694 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix. %FALSE otherwise.
697 g_file_has_prefix (GFile *file,
702 g_return_val_if_fail (G_IS_FILE (file), FALSE);
703 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
705 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
708 iface = G_FILE_GET_IFACE (file);
710 /* The vtable function differs in arg order since we're
711 using the old contains_file call */
712 return (* iface->prefix_matches) (prefix, file);
716 * g_file_get_relative_path:
717 * @parent: input #GFile.
718 * @descendant: input #GFile.
720 * Gets the path for @descendant relative to @parent.
722 * This call does no blocking i/o.
724 * Returns: string with the relative path from @descendant
725 * to @parent, or %NULL if @descendant doesn't have @parent as prefix. The returned string should be freed with
726 * g_free() when no longer needed.
729 g_file_get_relative_path (GFile *parent,
734 g_return_val_if_fail (G_IS_FILE (parent), NULL);
735 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
737 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
740 iface = G_FILE_GET_IFACE (parent);
742 return (* iface->get_relative_path) (parent, descendant);
746 * g_file_resolve_relative_path:
747 * @file: input #GFile.
748 * @relative_path: a given relative path string.
750 * Resolves a relative path for @file to an absolute path.
752 * This call does no blocking i/o.
754 * Returns: #GFile to the resolved path. %NULL if @relative_path
755 * is %NULL or if @file is invalid.
758 g_file_resolve_relative_path (GFile *file,
759 const char *relative_path)
763 g_return_val_if_fail (G_IS_FILE (file), NULL);
764 g_return_val_if_fail (relative_path != NULL, NULL);
766 iface = G_FILE_GET_IFACE (file);
768 return (* iface->resolve_relative_path) (file, relative_path);
772 * g_file_enumerate_children:
773 * @file: input #GFile.
774 * @attributes: an attribute query string.
775 * @flags: a set of #GFileQueryInfoFlags.
776 * @cancellable: optional #GCancellable object, %NULL to ignore.
777 * @error: #GError for error reporting.
779 * Gets the requested information about the files in a directory. The result
780 * is a #GFileEnumerator object that will give out #GFileInfo objects for
781 * all the files in the directory.
783 * The @attribute value is a string that specifies the file attributes that
784 * should be gathered. It is not an error if it's not possible to read a particular
785 * requested attribute from a file - it just won't be set. @attribute should
786 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
787 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
788 * namespace. An example attribute query be "standard::*,owner::user".
789 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
791 * If @cancellable is not %NULL, then the operation can be cancelled by
792 * triggering the cancellable object from another thread. If the operation
793 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
795 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
796 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
797 * Other errors are possible too.
799 * Returns: A #GFileEnumerator if successful, %NULL on error.
802 g_file_enumerate_children (GFile *file,
803 const char *attributes,
804 GFileQueryInfoFlags flags,
805 GCancellable *cancellable,
811 g_return_val_if_fail (G_IS_FILE (file), NULL);
813 if (g_cancellable_set_error_if_cancelled (cancellable, error))
816 iface = G_FILE_GET_IFACE (file);
818 if (iface->enumerate_children == NULL)
820 g_set_error (error, G_IO_ERROR,
821 G_IO_ERROR_NOT_SUPPORTED,
822 _("Operation not supported"));
826 return (* iface->enumerate_children) (file, attributes, flags,
831 * g_file_enumerate_children_async:
832 * @file: input #GFile.
833 * @attributes: an attribute query string.
834 * @flags: a set of #GFileQueryInfoFlags.
835 * @io_priority: the <link linkend="io-priority">I/O priority</link>
837 * @cancellable: optional #GCancellable object, %NULL to ignore.
838 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
839 * @user_data: the data to pass to callback function
841 * Asynchronously gets the requested information about the files in a directory. The result
842 * is a #GFileEnumerator object that will give out #GFileInfo objects for
843 * all the files in the directory.
845 * For more details, see g_file_enumerate_children() which is
846 * the synchronous version of this call.
848 * When the operation is finished, @callback will be called. You can then call
849 * g_file_enumerate_children_finish() to get the result of the operation.
852 g_file_enumerate_children_async (GFile *file,
853 const char *attributes,
854 GFileQueryInfoFlags flags,
856 GCancellable *cancellable,
857 GAsyncReadyCallback callback,
862 g_return_if_fail (G_IS_FILE (file));
864 iface = G_FILE_GET_IFACE (file);
865 (* iface->enumerate_children_async) (file,
875 * g_file_enumerate_children_finish:
876 * @file: input #GFile.
877 * @res: a #GAsyncResult.
880 * Finishes an async enumerate children operation.
881 * See g_file_enumerate_children_async().
883 * Returns: a #GFileEnumerator or %NULL if an error occurred.
886 g_file_enumerate_children_finish (GFile *file,
892 g_return_val_if_fail (G_IS_FILE (file), NULL);
893 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
895 if (G_IS_SIMPLE_ASYNC_RESULT (res))
897 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
898 if (g_simple_async_result_propagate_error (simple, error))
902 iface = G_FILE_GET_IFACE (file);
903 return (* iface->enumerate_children_finish) (file, res, error);
907 * g_file_query_exists:
908 * @file: input #GFile.
909 * @cancellable: optional #GCancellable object, %NULL to ignore.
911 * Utility function to check if a particular file exists. This is
912 * implemented using g_file_query_info() and as such does blocking I/O.
914 * Note that in many cases it is racy to first check for file existance
915 * and then execute something based on the outcome of that, because the
916 * file might have been created or removed inbetween the operations. The
917 * general approach to handling that is to not check, but just do the
918 * operation and handle the errors as they come.
920 * As an example of race-free checking, take the case of reading a file, and
921 * if it doesn't exist, creating it. There are two racy versions: read it, and
922 * on error create it; and: check if it exists, if not create it. These
923 * can both result in two processes creating the file (with perhaps a partially
924 * written file as the result). The correct approach is to always try to create
925 * the file with g_file_create() which will either atomically create the file
926 * or fail with a G_IO_ERROR_EXISTS error.
928 * However, in many cases an existance check is useful in a user
929 * interface, for instance to make a menu item sensitive/insensitive, so that
930 * you don't have to fool users that something is possible and then just show
931 * and error dialog. If you do this, you should make sure to also handle the
932 * errors that can happen due to races when you execute the operation.
934 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
937 g_file_query_exists (GFile *file,
938 GCancellable *cancellable)
942 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
943 G_FILE_QUERY_INFO_NONE,
947 g_object_unref (info);
956 * @file: input #GFile.
957 * @attributes: an attribute query string.
958 * @flags: a set of #GFileQueryInfoFlags.
959 * @cancellable: optional #GCancellable object, %NULL to ignore.
962 * Gets the requested information about specified @file. The result
963 * is a #GFileInfo object that contains key-value attributes (such as
964 * the type or size of the file).
966 * The @attribute value is a string that specifies the file attributes that
967 * should be gathered. It is not an error if it's not possible to read a particular
968 * requested attribute from a file - it just won't be set. @attribute should
969 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
970 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
971 * namespace. An example attribute query be "standard::*,owner::user".
972 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
974 * If @cancellable is not %NULL, then the operation can be cancelled by
975 * triggering the cancellable object from another thread. If the operation
976 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
978 * For symlinks, normally the information about the target of the
979 * symlink is returned, rather than information about the symlink itself.
980 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
981 * information about the symlink itself will be returned. Also, for symlinks
982 * that point to non-existing files the information about the symlink itself
985 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
986 * Other errors are possible too, and depend on what kind of filesystem the file is on.
988 * Returns: a #GFileInfo for the given @file, or %NULL on error.
991 g_file_query_info (GFile *file,
992 const char *attributes,
993 GFileQueryInfoFlags flags,
994 GCancellable *cancellable,
999 g_return_val_if_fail (G_IS_FILE (file), NULL);
1001 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1004 iface = G_FILE_GET_IFACE (file);
1006 if (iface->query_info == NULL)
1008 g_set_error (error, G_IO_ERROR,
1009 G_IO_ERROR_NOT_SUPPORTED,
1010 _("Operation not supported"));
1014 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1018 * g_file_query_info_async:
1019 * @file: input #GFile.
1020 * @attributes: an attribute query string.
1021 * @flags: a set of #GFileQueryInfoFlags.
1022 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1024 * @cancellable: optional #GCancellable object, %NULL to ignore.
1025 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1026 * @user_data: the data to pass to callback function
1028 * Asynchronously gets the requested information about specified @file. The result
1029 * is a #GFileInfo object that contains key-value attributes (such as type or size
1032 * For more details, see g_file_query_info() which is
1033 * the synchronous version of this call.
1035 * When the operation is finished, @callback will be called. You can then call
1036 * g_file_query_info_finish() to get the result of the operation.
1039 g_file_query_info_async (GFile *file,
1040 const char *attributes,
1041 GFileQueryInfoFlags flags,
1043 GCancellable *cancellable,
1044 GAsyncReadyCallback callback,
1049 g_return_if_fail (G_IS_FILE (file));
1051 iface = G_FILE_GET_IFACE (file);
1052 (* iface->query_info_async) (file,
1062 * g_file_query_info_finish:
1063 * @file: input #GFile.
1064 * @res: a #GAsyncResult.
1065 * @error: a #GError.
1067 * Finishes an asynchronous file info query.
1068 * See g_file_query_info_async().
1070 * Returns: #GFileInfo for given @file or %NULL on error.
1073 g_file_query_info_finish (GFile *file,
1079 g_return_val_if_fail (G_IS_FILE (file), NULL);
1080 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1082 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1084 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1085 if (g_simple_async_result_propagate_error (simple, error))
1089 iface = G_FILE_GET_IFACE (file);
1090 return (* iface->query_info_finish) (file, res, error);
1094 * g_file_query_filesystem_info:
1095 * @file: input #GFile.
1096 * @attributes: an attribute query string.
1097 * @cancellable: optional #GCancellable object, %NULL to ignore.
1098 * @error: a #GError.
1100 * Similar to g_file_query_info(), but obtains information
1101 * about the filesystem the @file is on, rather than the file itself.
1102 * For instance the amount of space available and the type of
1105 * The @attribute value is a string that specifies the file attributes that
1106 * should be gathered. It is not an error if it's not possible to read a particular
1107 * requested attribute from a file - it just won't be set. @attribute should
1108 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1109 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1110 * namespace. The standard namespace for filesystem attributes is "fs".
1111 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1112 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1113 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1115 * If @cancellable is not %NULL, then the operation can be cancelled by
1116 * triggering the cancellable object from another thread. If the operation
1117 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1119 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1120 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1122 * Returns: a #GFileInfo or %NULL if there was an error.
1125 g_file_query_filesystem_info (GFile *file,
1126 const char *attributes,
1127 GCancellable *cancellable,
1132 g_return_val_if_fail (G_IS_FILE (file), NULL);
1134 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1137 iface = G_FILE_GET_IFACE (file);
1139 if (iface->query_filesystem_info == NULL)
1141 g_set_error (error, G_IO_ERROR,
1142 G_IO_ERROR_NOT_SUPPORTED,
1143 _("Operation not supported"));
1147 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1151 * g_file_query_filesystem_info_async:
1152 * @file: input #GFile.
1153 * @attributes: an attribute query string.
1154 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1156 * @cancellable: optional #GCancellable object, %NULL to ignore.
1157 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1158 * @user_data: the data to pass to callback function
1160 * Asynchronously gets the requested information about the filesystem
1161 * that the specified @file is on. The result is a #GFileInfo object
1162 * that contains key-value attributes (such as type or size for the
1165 * For more details, see g_file_query_filesystem_info() which is the
1166 * synchronous version of this call.
1168 * When the operation is finished, @callback will be called. You can
1169 * then call g_file_query_info_finish() to get the result of the
1173 g_file_query_filesystem_info_async (GFile *file,
1174 const char *attributes,
1176 GCancellable *cancellable,
1177 GAsyncReadyCallback callback,
1182 g_return_if_fail (G_IS_FILE (file));
1184 iface = G_FILE_GET_IFACE (file);
1185 (* iface->query_filesystem_info_async) (file,
1194 * g_file_query_filesystem_info_finish:
1195 * @file: input #GFile.
1196 * @res: a #GAsyncResult.
1197 * @error: a #GError.
1199 * Finishes an asynchronous filesystem info query. See
1200 * g_file_query_filesystem_info_async().
1202 * Returns: #GFileInfo for given @file or %NULL on error.
1205 g_file_query_filesystem_info_finish (GFile *file,
1211 g_return_val_if_fail (G_IS_FILE (file), NULL);
1212 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1214 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1216 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1217 if (g_simple_async_result_propagate_error (simple, error))
1221 iface = G_FILE_GET_IFACE (file);
1222 return (* iface->query_filesystem_info_finish) (file, res, error);
1226 * g_file_find_enclosing_mount:
1227 * @file: input #GFile.
1228 * @cancellable: optional #GCancellable object, %NULL to ignore.
1229 * @error: a #GError.
1231 * Gets a #GMount for the #GFile.
1233 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1234 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1237 * If @cancellable is not %NULL, then the operation can be cancelled by
1238 * triggering the cancellable object from another thread. If the operation
1239 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1241 * Returns: a #GMount where the @file is located or %NULL on error.
1244 g_file_find_enclosing_mount (GFile *file,
1245 GCancellable *cancellable,
1250 g_return_val_if_fail (G_IS_FILE (file), NULL);
1252 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1255 iface = G_FILE_GET_IFACE (file);
1256 if (iface->find_enclosing_mount == NULL)
1259 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1260 /* Translators: This is an error message when trying to find the
1261 * enclosing (user visible) mount of a file, but none exists. */
1262 _("Containing mount does not exist"));
1266 return (* iface->find_enclosing_mount) (file, cancellable, error);
1269 * g_file_find_enclosing_mount_async:
1271 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1273 * @cancellable: optional #GCancellable object, %NULL to ignore.
1274 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1275 * @user_data: the data to pass to callback function
1277 * Asynchronously gets the mount for the file.
1279 * For more details, see g_file_find_enclosing_mount() which is
1280 * the synchronous version of this call.
1282 * When the operation is finished, @callback will be called. You can then call
1283 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1286 g_file_find_enclosing_mount_async (GFile *file,
1288 GCancellable *cancellable,
1289 GAsyncReadyCallback callback,
1294 g_return_if_fail (G_IS_FILE (file));
1296 iface = G_FILE_GET_IFACE (file);
1297 (* iface->find_enclosing_mount_async) (file,
1305 * g_file_find_enclosing_mount_finish:
1307 * @res: a #GAsyncResult
1310 * Finishes an asynchronous find mount request.
1311 * See g_file_find_enclosing_mount_async().
1313 * Returns: #GMount for given @file or %NULL on error.
1316 g_file_find_enclosing_mount_finish (GFile *file,
1322 g_return_val_if_fail (G_IS_FILE (file), NULL);
1323 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1325 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1327 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1328 if (g_simple_async_result_propagate_error (simple, error))
1332 iface = G_FILE_GET_IFACE (file);
1333 return (* iface->find_enclosing_mount_finish) (file, res, error);
1339 * @file: #GFile to read.
1340 * @cancellable: a #GCancellable
1341 * @error: a #GError, or %NULL
1343 * Opens a file for reading. The result is a #GFileInputStream that
1344 * can be used to read the contents of the file.
1346 * If @cancellable is not %NULL, then the operation can be cancelled by
1347 * triggering the cancellable object from another thread. If the operation
1348 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1350 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1351 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1352 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1354 * Returns: #GFileInputStream or %NULL on error.
1357 g_file_read (GFile *file,
1358 GCancellable *cancellable,
1363 g_return_val_if_fail (G_IS_FILE (file), NULL);
1365 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1368 iface = G_FILE_GET_IFACE (file);
1370 if (iface->read_fn == NULL)
1372 g_set_error (error, G_IO_ERROR,
1373 G_IO_ERROR_NOT_SUPPORTED,
1374 _("Operation not supported"));
1378 return (* iface->read_fn) (file, cancellable, error);
1383 * @file: input #GFile.
1384 * @flags: a set of #GFileCreateFlags.
1385 * @cancellable: optional #GCancellable object, %NULL to ignore.
1386 * @error: a #GError, or %NULL
1388 * Gets an output stream for appending data to the file. If
1389 * the file doesn't already exist it is created.
1391 * By default files created are generally readable by everyone,
1392 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1393 * will be made readable only to the current user, to the level that
1394 * is supported on the target filesystem.
1396 * If @cancellable is not %NULL, then the operation can be cancelled by
1397 * triggering the cancellable object from another thread. If the operation
1398 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1400 * Some file systems don't allow all file names, and may
1401 * return an G_IO_ERROR_INVALID_FILENAME error.
1402 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1403 * returned. Other errors are possible too, and depend on what kind of
1404 * filesystem the file is on.
1406 * Returns: a #GFileOutputStream.
1409 g_file_append_to (GFile *file,
1410 GFileCreateFlags flags,
1411 GCancellable *cancellable,
1416 g_return_val_if_fail (G_IS_FILE (file), NULL);
1418 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1421 iface = G_FILE_GET_IFACE (file);
1423 if (iface->append_to == NULL)
1425 g_set_error (error, G_IO_ERROR,
1426 G_IO_ERROR_NOT_SUPPORTED,
1427 _("Operation not supported"));
1431 return (* iface->append_to) (file, flags, cancellable, error);
1436 * @file: input #GFile.
1437 * @flags: a set of #GFileCreateFlags.
1438 * @cancellable: optional #GCancellable object, %NULL to ignore.
1439 * @error: a #GError, or %NULL
1441 * Creates a new file and returns an output stream for writing to it.
1442 * The file must not already exists.
1444 * By default files created are generally readable by everyone,
1445 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1446 * will be made readable only to the current user, to the level that
1447 * is supported on the target filesystem.
1449 * If @cancellable is not %NULL, then the operation can be cancelled by
1450 * triggering the cancellable object from another thread. If the operation
1451 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1453 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1454 * error will be returned.
1455 * Some file systems don't allow all file names, and may
1456 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1457 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1458 * Other errors are possible too, and depend on what kind of
1459 * filesystem the file is on.
1461 * Returns: a #GFileOutputStream for the newly created file, or
1465 g_file_create (GFile *file,
1466 GFileCreateFlags flags,
1467 GCancellable *cancellable,
1472 g_return_val_if_fail (G_IS_FILE (file), NULL);
1474 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1477 iface = G_FILE_GET_IFACE (file);
1479 if (iface->create == NULL)
1481 g_set_error (error, G_IO_ERROR,
1482 G_IO_ERROR_NOT_SUPPORTED,
1483 _("Operation not supported"));
1487 return (* iface->create) (file, flags, cancellable, error);
1492 * @file: input #GFile.
1493 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1494 * current #GFile, or #NULL to ignore.
1495 * @make_backup: %TRUE if a backup should be created.
1496 * @flags: a set of #GFileCreateFlags.
1497 * @cancellable: optional #GCancellable object, %NULL to ignore.
1498 * @error: a #GError, or %NULL
1500 * Returns an output stream for overwriting the file, possibly
1501 * creating a backup copy of the file first.
1503 * This will try to replace the file in the safest way possible so
1504 * that any errors during the writing will not affect an already
1505 * existing copy of the file. For instance, for local files it
1506 * may write to a temporary file and then atomically rename over
1507 * the destination when the stream is closed.
1509 * By default files created are generally readable by everyone,
1510 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1511 * will be made readable only to the current user, to the level that
1512 * is supported on the target filesystem.
1514 * If @cancellable is not %NULL, then the operation can be cancelled by
1515 * triggering the cancellable object from another thread. If the operation
1516 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1518 * If you pass in a non-#NULL @etag value, then this value is
1519 * compared to the current entity tag of the file, and if they differ
1520 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1521 * that the file has been changed since you last read it. You can get
1522 * the new etag from g_file_output_stream_get_etag() after you've
1523 * finished writing and closed the #GFileOutputStream. When you load
1524 * a new file you can use g_file_input_stream_query_info() to get
1525 * the etag of the file.
1527 * If @make_backup is %TRUE, this function will attempt to make a backup
1528 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1529 * error will be returned. If you want to replace anyway, try again with
1530 * @make_backup set to %FALSE.
1532 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1533 * and if the file is some other form of non-regular file then a
1534 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1535 * Some file systems don't allow all file names, and may
1536 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1537 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1538 * Other errors are possible too, and depend on what kind of
1539 * filesystem the file is on.
1541 * Returns: a #GFileOutputStream or %NULL on error.
1544 g_file_replace (GFile *file,
1546 gboolean make_backup,
1547 GFileCreateFlags flags,
1548 GCancellable *cancellable,
1553 g_return_val_if_fail (G_IS_FILE (file), NULL);
1555 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1558 iface = G_FILE_GET_IFACE (file);
1560 if (iface->replace == NULL)
1562 g_set_error (error, G_IO_ERROR,
1563 G_IO_ERROR_NOT_SUPPORTED,
1564 _("Operation not supported"));
1569 /* Handle empty tag string as NULL in consistent way. */
1570 if (etag && *etag == 0)
1573 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1577 * g_file_read_async:
1578 * @file: input #GFile.
1579 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1581 * @cancellable: optional #GCancellable object, %NULL to ignore.
1582 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1583 * @user_data: the data to pass to callback function
1585 * Asynchronously opens @file for reading.
1587 * For more details, see g_file_read() which is
1588 * the synchronous version of this call.
1590 * When the operation is finished, @callback will be called. You can then call
1591 * g_file_read_finish() to get the result of the operation.
1594 g_file_read_async (GFile *file,
1596 GCancellable *cancellable,
1597 GAsyncReadyCallback callback,
1602 g_return_if_fail (G_IS_FILE (file));
1604 iface = G_FILE_GET_IFACE (file);
1605 (* iface->read_async) (file,
1613 * g_file_read_finish:
1614 * @file: input #GFile.
1615 * @res: a #GAsyncResult.
1616 * @error: a #GError, or %NULL
1618 * Finishes an asynchronous file read operation started with
1619 * g_file_read_async().
1621 * Returns: a #GFileInputStream or %NULL on error.
1624 g_file_read_finish (GFile *file,
1630 g_return_val_if_fail (G_IS_FILE (file), NULL);
1631 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1633 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1635 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1636 if (g_simple_async_result_propagate_error (simple, error))
1640 iface = G_FILE_GET_IFACE (file);
1641 return (* iface->read_finish) (file, res, error);
1645 * g_file_append_to_async:
1646 * @file: input #GFile.
1647 * @flags: a set of #GFileCreateFlags.
1648 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1650 * @cancellable: optional #GCancellable object, %NULL to ignore.
1651 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1652 * @user_data: the data to pass to callback function
1654 * Asynchronously opens @file for appending.
1656 * For more details, see g_file_append_to() which is
1657 * the synchronous version of this call.
1659 * When the operation is finished, @callback will be called. You can then call
1660 * g_file_append_to_finish() to get the result of the operation.
1663 g_file_append_to_async (GFile *file,
1664 GFileCreateFlags flags,
1666 GCancellable *cancellable,
1667 GAsyncReadyCallback callback,
1672 g_return_if_fail (G_IS_FILE (file));
1674 iface = G_FILE_GET_IFACE (file);
1675 (* iface->append_to_async) (file,
1684 * g_file_append_to_finish:
1685 * @file: input #GFile.
1686 * @res: #GAsyncResult
1687 * @error: a #GError, or %NULL
1689 * Finishes an asynchronous file append operation started with
1690 * g_file_append_to_async().
1692 * Returns: a valid #GFileOutputStream or %NULL on error.
1695 g_file_append_to_finish (GFile *file,
1701 g_return_val_if_fail (G_IS_FILE (file), NULL);
1702 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1704 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1706 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1707 if (g_simple_async_result_propagate_error (simple, error))
1711 iface = G_FILE_GET_IFACE (file);
1712 return (* iface->append_to_finish) (file, res, error);
1716 * g_file_create_async:
1717 * @file: input #GFile.
1718 * @flags: a set of #GFileCreateFlags.
1719 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1721 * @cancellable: optional #GCancellable object, %NULL to ignore.
1722 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1723 * @user_data: the data to pass to callback function
1725 * Asynchronously creates a new file and returns an output stream for writing to it.
1726 * The file must not already exists.
1728 * For more details, see g_file_create() which is
1729 * the synchronous version of this call.
1731 * When the operation is finished, @callback will be called. You can then call
1732 * g_file_create_finish() to get the result of the operation.
1735 g_file_create_async (GFile *file,
1736 GFileCreateFlags flags,
1738 GCancellable *cancellable,
1739 GAsyncReadyCallback callback,
1744 g_return_if_fail (G_IS_FILE (file));
1746 iface = G_FILE_GET_IFACE (file);
1747 (* iface->create_async) (file,
1756 * g_file_create_finish:
1757 * @file: input #GFile.
1758 * @res: a #GAsyncResult.
1759 * @error: a #GError, or %NULL
1761 * Finishes an asynchronous file create operation started with
1762 * g_file_create_async().
1764 * Returns: a #GFileOutputStream or %NULL on error.
1767 g_file_create_finish (GFile *file,
1773 g_return_val_if_fail (G_IS_FILE (file), NULL);
1774 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1776 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1778 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1779 if (g_simple_async_result_propagate_error (simple, error))
1783 iface = G_FILE_GET_IFACE (file);
1784 return (* iface->create_finish) (file, res, error);
1788 * g_file_replace_async:
1789 * @file: input #GFile.
1790 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1791 * current #GFile, or NULL to ignore.
1792 * @make_backup: %TRUE if a backup should be created.
1793 * @flags: a set of #GFileCreateFlags.
1794 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1796 * @cancellable: optional #GCancellable object, %NULL to ignore.
1797 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1798 * @user_data: the data to pass to callback function
1800 * Asynchronously overwrites the file, replacing the contents, possibly
1801 * creating a backup copy of the file first.
1803 * For more details, see g_file_replace() which is
1804 * the synchronous version of this call.
1806 * When the operation is finished, @callback will be called. You can then call
1807 * g_file_replace_finish() to get the result of the operation.
1810 g_file_replace_async (GFile *file,
1812 gboolean make_backup,
1813 GFileCreateFlags flags,
1815 GCancellable *cancellable,
1816 GAsyncReadyCallback callback,
1821 g_return_if_fail (G_IS_FILE (file));
1823 iface = G_FILE_GET_IFACE (file);
1824 (* iface->replace_async) (file,
1835 * g_file_replace_finish:
1836 * @file: input #GFile.
1837 * @res: a #GAsyncResult.
1838 * @error: a #GError, or %NULL
1840 * Finishes an asynchronous file replace operation started with
1841 * g_file_replace_async().
1843 * Returns: a #GFileOutputStream, or %NULL on error.
1846 g_file_replace_finish (GFile *file,
1852 g_return_val_if_fail (G_IS_FILE (file), NULL);
1853 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1855 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1857 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1858 if (g_simple_async_result_propagate_error (simple, error))
1862 iface = G_FILE_GET_IFACE (file);
1863 return (* iface->replace_finish) (file, res, error);
1867 copy_symlink (GFile *destination,
1868 GFileCopyFlags flags,
1869 GCancellable *cancellable,
1874 gboolean tried_delete;
1876 GFileType file_type;
1878 tried_delete = FALSE;
1882 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1884 /* Maybe it already existed, and we want to overwrite? */
1885 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1886 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1888 g_error_free (my_error);
1891 /* Don't overwrite if the destination is a directory */
1892 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1893 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1894 cancellable, &my_error);
1897 file_type = g_file_info_get_file_type (info);
1898 g_object_unref (info);
1900 if (file_type == G_FILE_TYPE_DIRECTORY)
1902 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1903 _("Can't copy over directory"));
1908 if (!g_file_delete (destination, cancellable, error))
1911 tried_delete = TRUE;
1915 g_propagate_error (error, my_error);
1922 static GInputStream *
1923 open_source_for_copy (GFile *source,
1925 GFileCopyFlags flags,
1926 GCancellable *cancellable,
1932 GFileType file_type;
1935 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1939 /* There was an error opening the source, try to set a good error for it: */
1941 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1943 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1944 * as that is less useful to the app. Better check for errors on the
1947 g_error_free (my_error);
1950 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1951 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1952 cancellable, &my_error);
1955 file_type = g_file_info_get_file_type (info);
1956 g_object_unref (info);
1958 if (flags & G_FILE_COPY_OVERWRITE)
1960 if (file_type == G_FILE_TYPE_DIRECTORY)
1962 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1963 _("Can't copy directory over directory"));
1966 /* continue to would_recurse error */
1970 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1971 _("Target file exists"));
1977 /* Error getting info from target, return that error
1978 * (except for NOT_FOUND, which is no error here)
1980 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1982 g_propagate_error (error, my_error);
1985 g_error_free (my_error);
1988 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1989 _("Can't recursively copy directory"));
1993 g_propagate_error (error, my_error);
1998 should_copy (GFileAttributeInfo *info,
2002 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2003 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2007 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2008 GFileAttributeInfoList *namespaces,
2016 s = g_string_new ("");
2020 for (i = 0; i < attributes->n_infos; i++)
2022 if (should_copy (&attributes->infos[i], as_move))
2027 g_string_append_c (s, ',');
2029 g_string_append (s, attributes->infos[i].name);
2036 for (i = 0; i < namespaces->n_infos; i++)
2038 if (should_copy (&namespaces->infos[i], as_move))
2043 g_string_append_c (s, ',');
2045 g_string_append (s, namespaces->infos[i].name);
2046 g_string_append (s, ":*");
2051 return g_string_free (s, FALSE);
2055 * g_file_copy_attributes:
2056 * @source: a #GFile with attributes.
2057 * @destination: a #GFile to copy attributes to.
2058 * @flags: a set of #GFileCopyFlags.
2059 * @cancellable: optional #GCancellable object, %NULL to ignore.
2060 * @error: a #GError, %NULL to ignore.
2062 * Copies the file attributes from @source to @destination.
2064 * Normally only a subset of the file attributes are copied,
2065 * those that are copies in a normal file copy operation
2066 * (which for instance does not include e.g. mtime). However
2067 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2068 * all the metadata that is possible to copy is copied.
2070 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2073 g_file_copy_attributes (GFile *source,
2075 GFileCopyFlags flags,
2076 GCancellable *cancellable,
2079 GFileAttributeInfoList *attributes, *namespaces;
2080 char *attrs_to_read;
2084 gboolean source_nofollow_symlinks;
2086 as_move = flags & G_FILE_COPY_ALL_METADATA;
2087 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2089 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2090 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2091 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2093 if (attributes == NULL && namespaces == NULL)
2096 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2098 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2099 * we just don't copy it.
2101 info = g_file_query_info (source, attrs_to_read,
2102 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2106 g_free (attrs_to_read);
2111 res = g_file_set_attributes_from_info (destination,
2115 g_object_unref (info);
2118 g_file_attribute_info_list_unref (attributes);
2119 g_file_attribute_info_list_unref (namespaces);
2124 /* Closes the streams */
2126 copy_stream_with_progress (GInputStream *in,
2128 GCancellable *cancellable,
2129 GFileProgressCallback progress_callback,
2130 gpointer progress_callback_data,
2133 gssize n_read, n_written;
2134 goffset current_size;
2135 char buffer[8192], *p;
2141 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2142 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2146 total_size = g_file_info_get_size (info);
2147 g_object_unref (info);
2154 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2164 current_size += n_read;
2169 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2170 if (n_written == -1)
2177 n_read -= n_written;
2183 if (progress_callback)
2184 progress_callback (current_size, total_size, progress_callback_data);
2188 error = NULL; /* Ignore further errors */
2190 /* Make sure we send full copied size */
2191 if (progress_callback)
2192 progress_callback (current_size, total_size, progress_callback_data);
2195 /* Don't care about errors in source here */
2196 g_input_stream_close (in, cancellable, NULL);
2198 /* But write errors on close are bad! */
2199 if (!g_output_stream_close (out, cancellable, error))
2202 g_object_unref (in);
2203 g_object_unref (out);
2209 file_copy_fallback (GFile *source,
2211 GFileCopyFlags flags,
2212 GCancellable *cancellable,
2213 GFileProgressCallback progress_callback,
2214 gpointer progress_callback_data,
2222 /* Maybe copy the symlink? */
2223 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2225 info = g_file_query_info (source,
2226 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2227 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2233 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2234 (target = g_file_info_get_symlink_target (info)) != NULL)
2236 if (!copy_symlink (destination, flags, cancellable, target, error))
2238 g_object_unref (info);
2242 g_object_unref (info);
2246 g_object_unref (info);
2249 in = open_source_for_copy (source, destination, flags, cancellable, error);
2253 if (flags & G_FILE_COPY_OVERWRITE)
2255 out = (GOutputStream *)g_file_replace (destination,
2257 flags & G_FILE_COPY_BACKUP,
2258 cancellable, error);
2262 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2267 g_object_unref (in);
2271 if (!copy_stream_with_progress (in, out, cancellable,
2272 progress_callback, progress_callback_data,
2278 /* Ignore errors here. Failure to copy metadata is not a hard error */
2279 g_file_copy_attributes (source, destination,
2280 flags, cancellable, NULL);
2287 * @source: input #GFile.
2288 * @destination: destination #GFile
2289 * @flags: set of #GFileCopyFlags
2290 * @cancellable: optional #GCancellable object, %NULL to ignore.
2291 * @progress_callback: function to callback with progress information
2292 * @progress_callback_data: user data to pass to @progress_callback
2293 * @error: #GError to set on error, or %NULL
2295 * Copies the file @source to the location specified by @destination.
2296 * Can not handle recursive copies of directories.
2298 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2299 * existing @destination file is overwritten.
2301 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2302 * will be copied as symlinks, otherwise the target of the
2303 * @source symlink will be copied.
2305 * If @cancellable is not %NULL, then the operation can be cancelled by
2306 * triggering the cancellable object from another thread. If the operation
2307 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2309 * If @progress_callback is not %NULL, then the operation can be monitored by
2310 * setting this to a #GFileProgressCallback function. @progress_callback_data
2311 * will be passed to this function. It is guaranteed that this callback will
2312 * be called after all data has been transferred with the total number of bytes
2313 * copied during the operation.
2315 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2316 * error is returned, independent on the status of the @destination.
2318 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2319 * error G_IO_ERROR_EXISTS is returned.
2321 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2322 * error is returned. If trying to overwrite a directory with a directory the
2323 * G_IO_ERROR_WOULD_MERGE error is returned.
2325 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2326 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2329 * If you are interested in copying the #GFile object itself (not the on-disk
2330 * file), see g_file_dup().
2332 * Returns: %TRUE on success, %FALSE otherwise.
2335 g_file_copy (GFile *source,
2337 GFileCopyFlags flags,
2338 GCancellable *cancellable,
2339 GFileProgressCallback progress_callback,
2340 gpointer progress_callback_data,
2347 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2348 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2350 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2353 iface = G_FILE_GET_IFACE (destination);
2357 res = (* iface->copy) (source, destination,
2359 progress_callback, progress_callback_data,
2365 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2367 g_propagate_error (error, my_error);
2372 /* If the types are different, and the destination method failed
2373 also try the source method */
2374 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2376 iface = G_FILE_GET_IFACE (source);
2381 res = (* iface->copy) (source, destination,
2383 progress_callback, progress_callback_data,
2389 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2391 g_propagate_error (error, my_error);
2397 return file_copy_fallback (source, destination, flags, cancellable,
2398 progress_callback, progress_callback_data,
2403 * g_file_copy_async:
2404 * @source: input #GFile.
2405 * @destination: destination #GFile
2406 * @flags: set of #GFileCopyFlags
2407 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2409 * @cancellable: optional #GCancellable object, %NULL to ignore.
2410 * @progress_callback: function to callback with progress information
2411 * @progress_callback_data: user data to pass to @progress_callback
2412 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2413 * @user_data: the data to pass to callback function
2415 * Copies the file @source to the location specified by @destination
2416 * asynchronously. For details of the behaviour, see g_file_copy().
2418 * If @progress_callback is not %NULL, then that function that will be called
2419 * just like in g_file_copy(), however the callback will run in the main loop,
2420 * not in the thread that is doing the I/O operation.
2422 * When the operation is finished, @callback will be called. You can then call
2423 * g_file_copy_finish() to get the result of the operation.
2426 g_file_copy_async (GFile *source,
2428 GFileCopyFlags flags,
2430 GCancellable *cancellable,
2431 GFileProgressCallback progress_callback,
2432 gpointer progress_callback_data,
2433 GAsyncReadyCallback callback,
2438 g_return_if_fail (G_IS_FILE (source));
2439 g_return_if_fail (G_IS_FILE (destination));
2441 iface = G_FILE_GET_IFACE (source);
2442 (* iface->copy_async) (source,
2448 progress_callback_data,
2454 * g_file_copy_finish:
2455 * @file: input #GFile.
2456 * @res: a #GAsyncResult.
2457 * @error: a #GError, or %NULL
2459 * Finishes copying the file started with
2460 * g_file_copy_async().
2462 * Returns: a %TRUE on success, %FALSE on error.
2465 g_file_copy_finish (GFile *file,
2471 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2472 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2474 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2476 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2478 if (g_simple_async_result_propagate_error (simple, error))
2482 iface = G_FILE_GET_IFACE (file);
2483 return (* iface->copy_finish) (file, res, error);
2488 * @source: #GFile pointing to the source location.
2489 * @destination: #GFile pointing to the destination location.
2490 * @flags: set of #GFileCopyFlags.
2491 * @cancellable: optional #GCancellable object, %NULL to ignore.
2492 * @progress_callback: #GFileProgressCallback function for updates.
2493 * @progress_callback_data: gpointer to user data for the callback function.
2494 * @error: #GError for returning error conditions, or %NULL
2497 * Tries to move the file or directory @source to the location specified by @destination.
2498 * If native move operations are supported then this is used, otherwise a copy + delete
2499 * fallback is used. The native implementation may support moving directories (for instance
2500 * on moves inside the same filesystem), but the fallback code does not.
2502 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2503 * existing @destination file is overwritten.
2505 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2506 * will be copied as symlinks, otherwise the target of the
2507 * @source symlink will be copied.
2509 * If @cancellable is not %NULL, then the operation can be cancelled by
2510 * triggering the cancellable object from another thread. If the operation
2511 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2513 * If @progress_callback is not %NULL, then the operation can be monitored by
2514 * setting this to a #GFileProgressCallback function. @progress_callback_data
2515 * will be passed to this function. It is guaranteed that this callback will
2516 * be called after all data has been transferred with the total number of bytes
2517 * copied during the operation.
2519 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2520 * error is returned, independent on the status of the @destination.
2522 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2523 * error G_IO_ERROR_EXISTS is returned.
2525 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2526 * error is returned. If trying to overwrite a directory with a directory the
2527 * G_IO_ERROR_WOULD_MERGE error is returned.
2529 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2530 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2531 * may be returned (if the native move operation isn't available).
2533 * Returns: %TRUE on successful move, %FALSE otherwise.
2536 g_file_move (GFile *source,
2538 GFileCopyFlags flags,
2539 GCancellable *cancellable,
2540 GFileProgressCallback progress_callback,
2541 gpointer progress_callback_data,
2548 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2549 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2551 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2554 iface = G_FILE_GET_IFACE (destination);
2558 res = (* iface->move) (source, destination,
2560 progress_callback, progress_callback_data,
2566 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2568 g_propagate_error (error, my_error);
2573 /* If the types are different, and the destination method failed
2574 also try the source method */
2575 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2577 iface = G_FILE_GET_IFACE (source);
2582 res = (* iface->move) (source, destination,
2584 progress_callback, progress_callback_data,
2590 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2592 g_propagate_error (error, my_error);
2598 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2600 g_set_error (error, G_IO_ERROR,
2601 G_IO_ERROR_NOT_SUPPORTED,
2602 _("Operation not supported"));
2606 flags |= G_FILE_COPY_ALL_METADATA;
2607 if (!g_file_copy (source, destination, flags, cancellable,
2608 progress_callback, progress_callback_data,
2612 return g_file_delete (source, cancellable, error);
2616 * g_file_make_directory
2617 * @file: input #GFile.
2618 * @cancellable: optional #GCancellable object, %NULL to ignore.
2619 * @error: a #GError, or %NULL
2621 * Creates a directory.
2623 * If @cancellable is not %NULL, then the operation can be cancelled by
2624 * triggering the cancellable object from another thread. If the operation
2625 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2627 * Returns: %TRUE on successful creation, %FALSE otherwise.
2630 g_file_make_directory (GFile *file,
2631 GCancellable *cancellable,
2636 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2638 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2641 iface = G_FILE_GET_IFACE (file);
2643 if (iface->make_directory == NULL)
2645 g_set_error (error, G_IO_ERROR,
2646 G_IO_ERROR_NOT_SUPPORTED,
2647 _("Operation not supported"));
2651 return (* iface->make_directory) (file, cancellable, error);
2655 * g_file_make_symbolic_link:
2656 * @file: input #GFile.
2657 * @symlink_value: a string with the value of the new symlink.
2658 * @cancellable: optional #GCancellable object, %NULL to ignore.
2659 * @error: a #GError.
2661 * Creates a symbolic link.
2663 * If @cancellable is not %NULL, then the operation can be cancelled by
2664 * triggering the cancellable object from another thread. If the operation
2665 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2667 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2670 g_file_make_symbolic_link (GFile *file,
2671 const char *symlink_value,
2672 GCancellable *cancellable,
2677 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2678 g_return_val_if_fail (symlink_value != NULL, FALSE);
2680 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2683 if (*symlink_value == '\0')
2685 g_set_error (error, G_IO_ERROR,
2686 G_IO_ERROR_INVALID_ARGUMENT,
2687 _("Invalid symlink value given"));
2691 iface = G_FILE_GET_IFACE (file);
2693 if (iface->make_symbolic_link == NULL)
2695 g_set_error (error, G_IO_ERROR,
2696 G_IO_ERROR_NOT_SUPPORTED,
2697 _("Operation not supported"));
2701 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2706 * @file: input #GFile.
2707 * @cancellable: optional #GCancellable object, %NULL to ignore.
2708 * @error: a #GError, or %NULL
2712 * If @cancellable is not %NULL, then the operation can be cancelled by
2713 * triggering the cancellable object from another thread. If the operation
2714 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2716 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2719 g_file_delete (GFile *file,
2720 GCancellable *cancellable,
2725 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2727 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2730 iface = G_FILE_GET_IFACE (file);
2732 if (iface->delete_file == NULL)
2734 g_set_error (error, G_IO_ERROR,
2735 G_IO_ERROR_NOT_SUPPORTED,
2736 _("Operation not supported"));
2740 return (* iface->delete_file) (file, cancellable, error);
2745 * @file: #GFile to send to trash.
2746 * @cancellable: optional #GCancellable object, %NULL to ignore.
2747 * @error: a #GError, or %NULL
2749 * Sends @file to the "Trashcan", if possible. This is similar to
2750 * deleting it, but the user can recover it before emptying the trashcan.
2751 * Not all file systems support trashing, so this call can return the
2752 * %G_IO_ERROR_NOT_SUPPORTED error.
2755 * If @cancellable is not %NULL, then the operation can be cancelled by
2756 * triggering the cancellable object from another thread. If the operation
2757 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2759 * Returns: %TRUE on successful trash, %FALSE otherwise.
2762 g_file_trash (GFile *file,
2763 GCancellable *cancellable,
2768 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2770 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2773 iface = G_FILE_GET_IFACE (file);
2775 if (iface->trash == NULL)
2778 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2779 _("Trash not supported"));
2783 return (* iface->trash) (file, cancellable, error);
2787 * g_file_set_display_name:
2788 * @file: input #GFile.
2789 * @display_name: a string.
2790 * @cancellable: optional #GCancellable object, %NULL to ignore.
2791 * @error: a #GError, or %NULL
2793 * Renames @file to the specified display name.
2795 * The display name is converted from UTF8 to the correct encoding for the target
2796 * filesystem if possible and the @file is renamed to this.
2798 * If you want to implement a rename operation in the user interface the edit name
2799 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2800 * widget, and then the result after editing should be passed to g_file_set_display_name().
2802 * On success the resulting converted filename is returned.
2804 * If @cancellable is not %NULL, then the operation can be cancelled by
2805 * triggering the cancellable object from another thread. If the operation
2806 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2808 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2811 g_file_set_display_name (GFile *file,
2812 const char *display_name,
2813 GCancellable *cancellable,
2818 g_return_val_if_fail (G_IS_FILE (file), NULL);
2819 g_return_val_if_fail (display_name != NULL, NULL);
2821 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2825 G_IO_ERROR_INVALID_ARGUMENT,
2826 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2830 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2833 iface = G_FILE_GET_IFACE (file);
2835 return (* iface->set_display_name) (file, display_name, cancellable, error);
2839 * g_file_set_display_name_async:
2840 * @file: input #GFile.
2841 * @display_name: a string.
2842 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2844 * @cancellable: optional #GCancellable object, %NULL to ignore.
2845 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2846 * @user_data: the data to pass to callback function
2848 * Asynchronously sets the display name for a given #GFile.
2850 * For more details, see g_set_display_name() which is
2851 * the synchronous version of this call.
2853 * When the operation is finished, @callback will be called. You can then call
2854 * g_file_set_display_name_finish() to get the result of the operation.
2857 g_file_set_display_name_async (GFile *file,
2858 const char *display_name,
2860 GCancellable *cancellable,
2861 GAsyncReadyCallback callback,
2866 g_return_if_fail (G_IS_FILE (file));
2867 g_return_if_fail (display_name != NULL);
2869 iface = G_FILE_GET_IFACE (file);
2870 (* iface->set_display_name_async) (file,
2879 * g_file_set_display_name_finish:
2880 * @file: input #GFile.
2881 * @res: a #GAsyncResult.
2882 * @error: a #GError, or %NULL
2884 * Finishes setting a display name started with
2885 * g_file_set_display_name_async().
2887 * Returns: a #GFile or %NULL on error.
2890 g_file_set_display_name_finish (GFile *file,
2896 g_return_val_if_fail (G_IS_FILE (file), NULL);
2897 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2899 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2901 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2902 if (g_simple_async_result_propagate_error (simple, error))
2906 iface = G_FILE_GET_IFACE (file);
2907 return (* iface->set_display_name_finish) (file, res, error);
2911 * g_file_query_settable_attributes:
2912 * @file: input #GFile.
2913 * @cancellable: optional #GCancellable object, %NULL to ignore.
2914 * @error: a #GError, or %NULL
2916 * Obtain the list of settable attributes for the file.
2918 * Returns the type and full attribute name of all the attributes
2919 * that can be set on this file. This doesn't mean setting it will always
2920 * succeed though, you might get an access failure, or some specific
2921 * file may not support a specific attribute.
2923 * If @cancellable is not %NULL, then the operation can be cancelled by
2924 * triggering the cancellable object from another thread. If the operation
2925 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2927 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2928 * When you are done with it, release it with g_file_attribute_info_list_unref()
2930 GFileAttributeInfoList *
2931 g_file_query_settable_attributes (GFile *file,
2932 GCancellable *cancellable,
2937 GFileAttributeInfoList *list;
2939 g_return_val_if_fail (G_IS_FILE (file), NULL);
2941 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2944 iface = G_FILE_GET_IFACE (file);
2946 if (iface->query_settable_attributes == NULL)
2947 return g_file_attribute_info_list_new ();
2950 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2954 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2956 list = g_file_attribute_info_list_new ();
2957 g_error_free (my_error);
2960 g_propagate_error (error, my_error);
2967 * g_file_query_writable_namespaces:
2968 * @file: input #GFile.
2969 * @cancellable: optional #GCancellable object, %NULL to ignore.
2970 * @error: a #GError, or %NULL
2972 * Obtain the list of attribute namespaces where new attributes
2973 * can be created by a user. An example of this is extended
2974 * attributes (in the "xattr" namespace).
2976 * If @cancellable is not %NULL, then the operation can be cancelled by
2977 * triggering the cancellable object from another thread. If the operation
2978 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2980 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2981 * When you are done with it, release it with g_file_attribute_info_list_unref()
2983 GFileAttributeInfoList *
2984 g_file_query_writable_namespaces (GFile *file,
2985 GCancellable *cancellable,
2990 GFileAttributeInfoList *list;
2992 g_return_val_if_fail (G_IS_FILE (file), NULL);
2994 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2997 iface = G_FILE_GET_IFACE (file);
2999 if (iface->query_writable_namespaces == NULL)
3000 return g_file_attribute_info_list_new ();
3003 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3007 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3009 list = g_file_attribute_info_list_new ();
3010 g_error_free (my_error);
3013 g_propagate_error (error, my_error);
3020 * g_file_set_attribute:
3021 * @file: input #GFile.
3022 * @attribute: a string containing the attribute's name.
3023 * @type: The type of the attribute
3024 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3025 * @flags: a set of #GFileQueryInfoFlags.
3026 * @cancellable: optional #GCancellable object, %NULL to ignore.
3027 * @error: a #GError, or %NULL
3029 * Sets an attribute in the file with attribute name @attribute to @value.
3031 * If @cancellable is not %NULL, then the operation can be cancelled by
3032 * triggering the cancellable object from another thread. If the operation
3033 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3035 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3038 g_file_set_attribute (GFile *file,
3039 const char *attribute,
3040 GFileAttributeType type,
3042 GFileQueryInfoFlags flags,
3043 GCancellable *cancellable,
3048 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3049 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3051 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3054 iface = G_FILE_GET_IFACE (file);
3056 if (iface->set_attribute == NULL)
3058 g_set_error (error, G_IO_ERROR,
3059 G_IO_ERROR_NOT_SUPPORTED,
3060 _("Operation not supported"));
3064 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3068 * g_file_set_attributes_from_info:
3069 * @file: input #GFile.
3070 * @info: a #GFileInfo.
3071 * @flags: #GFileQueryInfoFlags
3072 * @cancellable: optional #GCancellable object, %NULL to ignore.
3073 * @error: a #GError, or %NULL
3075 * Tries to set all attributes in the #GFileInfo on the target values,
3076 * not stopping on the first error.
3078 * If there is any error during this operation then @error will be set to
3079 * the first error. Error on particular fields are flagged by setting
3080 * the "status" field in the attribute value to
3081 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3084 * If @cancellable is not %NULL, then the operation can be cancelled by
3085 * triggering the cancellable object from another thread. If the operation
3086 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3088 * Returns: %TRUE if there was any error, %FALSE otherwise.
3091 g_file_set_attributes_from_info (GFile *file,
3093 GFileQueryInfoFlags flags,
3094 GCancellable *cancellable,
3099 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3100 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3102 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3105 g_file_info_clear_status (info);
3107 iface = G_FILE_GET_IFACE (file);
3109 return (* iface->set_attributes_from_info) (file,
3118 g_file_real_set_attributes_from_info (GFile *file,
3120 GFileQueryInfoFlags flags,
3121 GCancellable *cancellable,
3127 GFileAttributeValue *value;
3131 attributes = g_file_info_list_attributes (info, NULL);
3133 for (i = 0; attributes[i] != NULL; i++)
3135 value = _g_file_info_get_attribute_value (info, attributes[i]);
3137 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3140 if (!g_file_set_attribute (file, attributes[i],
3141 value->type, _g_file_attribute_value_peek_as_pointer (value),
3142 flags, cancellable, error))
3144 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3146 /* Don't set error multiple times */
3150 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3153 g_strfreev (attributes);
3159 * g_file_set_attributes_async:
3160 * @file: input #GFile.
3161 * @info: a #GFileInfo.
3162 * @flags: a #GFileQueryInfoFlags.
3163 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3165 * @cancellable: optional #GCancellable object, %NULL to ignore.
3166 * @callback: a #GAsyncReadyCallback.
3167 * @user_data: a #gpointer.
3169 * Asynchronously sets the attributes of @file with @info.
3171 * For more details, see g_file_set_attributes_from_info() which is
3172 * the synchronous version of this call.
3174 * When the operation is finished, @callback will be called. You can then call
3175 * g_file_set_attributes_finish() to get the result of the operation.
3178 g_file_set_attributes_async (GFile *file,
3180 GFileQueryInfoFlags flags,
3182 GCancellable *cancellable,
3183 GAsyncReadyCallback callback,
3188 g_return_if_fail (G_IS_FILE (file));
3189 g_return_if_fail (G_IS_FILE_INFO (info));
3191 iface = G_FILE_GET_IFACE (file);
3192 (* iface->set_attributes_async) (file,
3202 * g_file_set_attributes_finish:
3203 * @file: input #GFile.
3204 * @result: a #GAsyncResult.
3205 * @info: a #GFileInfo.
3206 * @error: a #GError, or %NULL
3208 * Finishes setting an attribute started in g_file_set_attributes_async().
3210 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3213 g_file_set_attributes_finish (GFile *file,
3214 GAsyncResult *result,
3220 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3221 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3223 /* No standard handling of errors here, as we must set info even
3226 iface = G_FILE_GET_IFACE (file);
3227 return (* iface->set_attributes_finish) (file, result, info, error);
3231 * g_file_set_attribute_string:
3232 * @file: input #GFile.
3233 * @attribute: a string containing the attribute's name.
3234 * @value: a string containing the attribute's value.
3235 * @flags: #GFileQueryInfoFlags.
3236 * @cancellable: optional #GCancellable object, %NULL to ignore.
3237 * @error: a #GError, or %NULL
3239 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3240 * If @attribute is of a different type, this operation will fail.
3242 * If @cancellable is not %NULL, then the operation can be cancelled by
3243 * triggering the cancellable object from another thread. If the operation
3244 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3246 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3249 g_file_set_attribute_string (GFile *file,
3250 const char *attribute,
3252 GFileQueryInfoFlags flags,
3253 GCancellable *cancellable,
3256 return g_file_set_attribute (file, attribute,
3257 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3258 flags, cancellable, error);
3262 * g_file_set_attribute_byte_string:
3263 * @file: input #GFile.
3264 * @attribute: a string containing the attribute's name.
3265 * @value: a string containing the attribute's new value.
3266 * @flags: a #GFileQueryInfoFlags.
3267 * @cancellable: optional #GCancellable object, %NULL to ignore.
3268 * @error: a #GError, or %NULL
3270 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3271 * If @attribute is of a different type, this operation will fail,
3274 * If @cancellable is not %NULL, then the operation can be cancelled by
3275 * triggering the cancellable object from another thread. If the operation
3276 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3278 * Returns: %TRUE if the @attribute was successfully set to @value
3279 * in the @file, %FALSE otherwise.
3282 g_file_set_attribute_byte_string (GFile *file,
3283 const char *attribute,
3285 GFileQueryInfoFlags flags,
3286 GCancellable *cancellable,
3289 return g_file_set_attribute (file, attribute,
3290 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3291 flags, cancellable, error);
3295 * g_file_set_attribute_uint32:
3296 * @file: input #GFile.
3297 * @attribute: a string containing the attribute's name.
3298 * @value: a #guint32 containing the attribute's new value.
3299 * @flags: a #GFileQueryInfoFlags.
3300 * @cancellable: optional #GCancellable object, %NULL to ignore.
3301 * @error: a #GError, or %NULL
3303 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3304 * If @attribute is of a different type, this operation will fail.
3306 * If @cancellable is not %NULL, then the operation can be cancelled by
3307 * triggering the cancellable object from another thread. If the operation
3308 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3310 * Returns: %TRUE if the @attribute was successfully set to @value
3311 * in the @file, %FALSE otherwise.
3314 g_file_set_attribute_uint32 (GFile *file,
3315 const char *attribute,
3317 GFileQueryInfoFlags flags,
3318 GCancellable *cancellable,
3321 return g_file_set_attribute (file, attribute,
3322 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3323 flags, cancellable, error);
3327 * g_file_set_attribute_int32:
3328 * @file: input #GFile.
3329 * @attribute: a string containing the attribute's name.
3330 * @value: a #gint32 containing the attribute's new value.
3331 * @flags: a #GFileQueryInfoFlags.
3332 * @cancellable: optional #GCancellable object, %NULL to ignore.
3333 * @error: a #GError, or %NULL
3335 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3336 * If @attribute is of a different type, this operation will fail.
3338 * If @cancellable is not %NULL, then the operation can be cancelled by
3339 * triggering the cancellable object from another thread. If the operation
3340 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3342 * Returns: %TRUE if the @attribute was successfully set to @value
3343 * in the @file, %FALSE otherwise.
3346 g_file_set_attribute_int32 (GFile *file,
3347 const char *attribute,
3349 GFileQueryInfoFlags flags,
3350 GCancellable *cancellable,
3353 return g_file_set_attribute (file, attribute,
3354 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3355 flags, cancellable, error);
3359 * g_file_set_attribute_uint64:
3360 * @file: input #GFile.
3361 * @attribute: a string containing the attribute's name.
3362 * @value: a #guint64 containing the attribute's new value.
3363 * @flags: a #GFileQueryInfoFlags.
3364 * @cancellable: optional #GCancellable object, %NULL to ignore.
3365 * @error: a #GError, or %NULL
3367 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3368 * If @attribute is of a different type, this operation will fail.
3370 * If @cancellable is not %NULL, then the operation can be cancelled by
3371 * triggering the cancellable object from another thread. If the operation
3372 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3374 * Returns: %TRUE if the @attribute was successfully set to @value
3375 * in the @file, %FALSE otherwise.
3378 g_file_set_attribute_uint64 (GFile *file,
3379 const char *attribute,
3381 GFileQueryInfoFlags flags,
3382 GCancellable *cancellable,
3385 return g_file_set_attribute (file, attribute,
3386 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3387 flags, cancellable, error);
3391 * g_file_set_attribute_int64:
3392 * @file: input #GFile.
3393 * @attribute: a string containing the attribute's name.
3394 * @value: a #guint64 containing the attribute's new value.
3395 * @flags: a #GFileQueryInfoFlags.
3396 * @cancellable: optional #GCancellable object, %NULL to ignore.
3397 * @error: a #GError, or %NULL
3399 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3400 * If @attribute is of a different type, this operation will fail.
3402 * If @cancellable is not %NULL, then the operation can be cancelled by
3403 * triggering the cancellable object from another thread. If the operation
3404 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3406 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3409 g_file_set_attribute_int64 (GFile *file,
3410 const char *attribute,
3412 GFileQueryInfoFlags flags,
3413 GCancellable *cancellable,
3416 return g_file_set_attribute (file, attribute,
3417 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3418 flags, cancellable, error);
3422 * g_file_mount_mountable:
3423 * @file: input #GFile.
3424 * @flags: flags affecting the operation
3425 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3426 * @cancellable: optional #GCancellable object, %NULL to ignore.
3427 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3428 * @user_data: the data to pass to callback function
3430 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3431 * Using @mount_operation, you can request callbacks when, for instance,
3432 * passwords are needed during authentication.
3434 * If @cancellable is not %NULL, then the operation can be cancelled by
3435 * triggering the cancellable object from another thread. If the operation
3436 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3438 * When the operation is finished, @callback will be called. You can then call
3439 * g_file_mount_mountable_finish() to get the result of the operation.
3442 g_file_mount_mountable (GFile *file,
3443 GMountMountFlags flags,
3444 GMountOperation *mount_operation,
3445 GCancellable *cancellable,
3446 GAsyncReadyCallback callback,
3451 g_return_if_fail (G_IS_FILE (file));
3453 iface = G_FILE_GET_IFACE (file);
3455 if (iface->mount_mountable == NULL)
3457 g_simple_async_report_error_in_idle (G_OBJECT (file),
3461 G_IO_ERROR_NOT_SUPPORTED,
3462 _("Operation not supported"));
3466 (* iface->mount_mountable) (file,
3475 * g_file_mount_mountable_finish:
3476 * @file: input #GFile.
3477 * @result: a #GAsyncResult.
3478 * @error: a #GError, or %NULL
3480 * Finishes a mount operation. See g_file_mount_mountable() for details.
3482 * Finish an asynchronous mount operation that was started
3483 * with g_file_mount_mountable().
3485 * Returns: a #GFile or %NULL on error.
3488 g_file_mount_mountable_finish (GFile *file,
3489 GAsyncResult *result,
3494 g_return_val_if_fail (G_IS_FILE (file), NULL);
3495 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3497 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3499 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3500 if (g_simple_async_result_propagate_error (simple, error))
3504 iface = G_FILE_GET_IFACE (file);
3505 return (* iface->mount_mountable_finish) (file, result, error);
3509 * g_file_unmount_mountable:
3510 * @file: input #GFile.
3511 * @flags: flags affecting the operation
3512 * @cancellable: optional #GCancellable object, %NULL to ignore.
3513 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3514 * @user_data: the data to pass to callback function
3516 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3518 * If @cancellable is not %NULL, then the operation can be cancelled by
3519 * triggering the cancellable object from another thread. If the operation
3520 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3522 * When the operation is finished, @callback will be called. You can then call
3523 * g_file_unmount_mountable_finish() to get the result of the operation.
3526 g_file_unmount_mountable (GFile *file,
3527 GMountUnmountFlags flags,
3528 GCancellable *cancellable,
3529 GAsyncReadyCallback callback,
3534 g_return_if_fail (G_IS_FILE (file));
3536 iface = G_FILE_GET_IFACE (file);
3538 if (iface->unmount_mountable == NULL)
3540 g_simple_async_report_error_in_idle (G_OBJECT (file),
3544 G_IO_ERROR_NOT_SUPPORTED,
3545 _("Operation not supported"));
3549 (* iface->unmount_mountable) (file,
3557 * g_file_unmount_mountable_finish:
3558 * @file: input #GFile.
3559 * @result: a #GAsyncResult.
3560 * @error: a #GError, or %NULL
3562 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3564 * Finish an asynchronous unmount operation that was started
3565 * with g_file_unmount_mountable().
3567 * Returns: %TRUE if the operation finished successfully. %FALSE
3571 g_file_unmount_mountable_finish (GFile *file,
3572 GAsyncResult *result,
3577 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3578 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3580 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3582 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3583 if (g_simple_async_result_propagate_error (simple, error))
3587 iface = G_FILE_GET_IFACE (file);
3588 return (* iface->unmount_mountable_finish) (file, result, error);
3592 * g_file_eject_mountable:
3593 * @file: input #GFile.
3594 * @flags: flags affecting the operation
3595 * @cancellable: optional #GCancellable object, %NULL to ignore.
3596 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3597 * @user_data: the data to pass to callback function
3599 * Starts an asynchronous eject on a mountable.
3600 * When this operation has completed, @callback will be called with
3601 * @user_user data, and the operation can be finalized with
3602 * g_file_eject_mountable_finish().
3604 * If @cancellable is not %NULL, then the operation can be cancelled by
3605 * triggering the cancellable object from another thread. If the operation
3606 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3609 g_file_eject_mountable (GFile *file,
3610 GMountUnmountFlags flags,
3611 GCancellable *cancellable,
3612 GAsyncReadyCallback callback,
3617 g_return_if_fail (G_IS_FILE (file));
3619 iface = G_FILE_GET_IFACE (file);
3621 if (iface->eject_mountable == NULL)
3623 g_simple_async_report_error_in_idle (G_OBJECT (file),
3627 G_IO_ERROR_NOT_SUPPORTED,
3628 _("Operation not supported"));
3632 (* iface->eject_mountable) (file,
3640 * g_file_eject_mountable_finish:
3641 * @file: input #GFile.
3642 * @result: a #GAsyncResult.
3643 * @error: a #GError, or %NULL
3645 * Finishes an asynchronous eject operation started by
3646 * g_file_eject_mountable().
3648 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3652 g_file_eject_mountable_finish (GFile *file,
3653 GAsyncResult *result,
3658 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3659 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3661 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3663 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3664 if (g_simple_async_result_propagate_error (simple, error))
3668 iface = G_FILE_GET_IFACE (file);
3669 return (* iface->eject_mountable_finish) (file, result, error);
3673 * g_file_monitor_directory:
3674 * @file: input #GFile.
3675 * @flags: a set of #GFileMonitorFlags.
3676 * @cancellable: optional #GCancellable object, %NULL to ignore.
3677 * @error: a #GError, or %NULL.
3679 * Obtains a directory monitor for the given file.
3680 * This may fail if directory monitoring is not supported.
3682 * If @cancellable is not %NULL, then the operation can be cancelled by
3683 * triggering the cancellable object from another thread. If the operation
3684 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3686 * Returns: a #GFileMonitor for the given @file,
3687 * or %NULL on error.
3690 g_file_monitor_directory (GFile *file,
3691 GFileMonitorFlags flags,
3692 GCancellable *cancellable,
3697 g_return_val_if_fail (G_IS_FILE (file), NULL);
3699 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3702 iface = G_FILE_GET_IFACE (file);
3704 if (iface->monitor_dir == NULL)
3706 g_set_error (error, G_IO_ERROR,
3707 G_IO_ERROR_NOT_SUPPORTED,
3708 _("Operation not supported"));
3712 return (* iface->monitor_dir) (file, flags, cancellable, error);
3716 * g_file_monitor_file:
3717 * @file: input #GFile.
3718 * @flags: a set of #GFileMonitorFlags.
3719 * @cancellable: optional #GCancellable object, %NULL to ignore.
3720 * @error: a #GError, or %NULL.
3722 * Obtains a file monitor for the given file. If no file notification
3723 * mechanism exists, then regular polling of the file is used.
3725 * If @cancellable is not %NULL, then the operation can be cancelled by
3726 * triggering the cancellable object from another thread. If the operation
3727 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3729 * Returns: a #GFileMonitor for the given @file.
3732 g_file_monitor_file (GFile *file,
3733 GFileMonitorFlags flags,
3734 GCancellable *cancellable,
3738 GFileMonitor *monitor;
3740 g_return_val_if_fail (G_IS_FILE (file), NULL);
3742 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3745 iface = G_FILE_GET_IFACE (file);
3749 if (iface->monitor_file)
3750 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3752 /* Fallback to polling */
3753 if (monitor == NULL)
3754 monitor = _g_poll_file_monitor_new (file);
3759 /********************************************
3760 * Default implementation of async ops *
3761 ********************************************/
3765 GFileQueryInfoFlags flags;
3767 } QueryInfoAsyncData;
3770 query_info_data_free (QueryInfoAsyncData *data)
3773 g_object_unref (data->info);
3774 g_free (data->attributes);
3779 query_info_async_thread (GSimpleAsyncResult *res,
3781 GCancellable *cancellable)
3783 GError *error = NULL;
3784 QueryInfoAsyncData *data;
3787 data = g_simple_async_result_get_op_res_gpointer (res);
3789 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3793 g_simple_async_result_set_from_error (res, error);
3794 g_error_free (error);
3801 g_file_real_query_info_async (GFile *file,
3802 const char *attributes,
3803 GFileQueryInfoFlags flags,
3805 GCancellable *cancellable,
3806 GAsyncReadyCallback callback,
3809 GSimpleAsyncResult *res;
3810 QueryInfoAsyncData *data;
3812 data = g_new0 (QueryInfoAsyncData, 1);
3813 data->attributes = g_strdup (attributes);
3814 data->flags = flags;
3816 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3817 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3819 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3820 g_object_unref (res);
3824 g_file_real_query_info_finish (GFile *file,
3828 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3829 QueryInfoAsyncData *data;
3831 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3833 data = g_simple_async_result_get_op_res_gpointer (simple);
3835 return g_object_ref (data->info);
3843 } QueryFilesystemInfoAsyncData;
3846 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
3849 g_object_unref (data->info);
3850 g_free (data->attributes);
3855 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
3857 GCancellable *cancellable)
3859 GError *error = NULL;
3860 QueryFilesystemInfoAsyncData *data;
3863 data = g_simple_async_result_get_op_res_gpointer (res);
3865 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
3869 g_simple_async_result_set_from_error (res, error);
3870 g_error_free (error);
3877 g_file_real_query_filesystem_info_async (GFile *file,
3878 const char *attributes,
3880 GCancellable *cancellable,
3881 GAsyncReadyCallback callback,
3884 GSimpleAsyncResult *res;
3885 QueryFilesystemInfoAsyncData *data;
3887 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
3888 data->attributes = g_strdup (attributes);
3890 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
3891 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
3893 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
3894 g_object_unref (res);
3898 g_file_real_query_filesystem_info_finish (GFile *file,
3902 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3903 QueryFilesystemInfoAsyncData *data;
3905 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
3907 data = g_simple_async_result_get_op_res_gpointer (simple);
3909 return g_object_ref (data->info);
3916 GFileQueryInfoFlags flags;
3917 GFileEnumerator *enumerator;
3918 } EnumerateChildrenAsyncData;
3921 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3923 if (data->enumerator)
3924 g_object_unref (data->enumerator);
3925 g_free (data->attributes);
3930 enumerate_children_async_thread (GSimpleAsyncResult *res,
3932 GCancellable *cancellable)
3934 GError *error = NULL;
3935 EnumerateChildrenAsyncData *data;
3936 GFileEnumerator *enumerator;
3938 data = g_simple_async_result_get_op_res_gpointer (res);
3940 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3942 if (enumerator == NULL)
3944 g_simple_async_result_set_from_error (res, error);
3945 g_error_free (error);
3948 data->enumerator = enumerator;
3952 g_file_real_enumerate_children_async (GFile *file,
3953 const char *attributes,
3954 GFileQueryInfoFlags flags,
3956 GCancellable *cancellable,
3957 GAsyncReadyCallback callback,
3960 GSimpleAsyncResult *res;
3961 EnumerateChildrenAsyncData *data;
3963 data = g_new0 (EnumerateChildrenAsyncData, 1);
3964 data->attributes = g_strdup (attributes);
3965 data->flags = flags;
3967 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3968 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3970 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3971 g_object_unref (res);
3974 static GFileEnumerator *
3975 g_file_real_enumerate_children_finish (GFile *file,
3979 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3980 EnumerateChildrenAsyncData *data;
3982 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3984 data = g_simple_async_result_get_op_res_gpointer (simple);
3985 if (data->enumerator)
3986 return g_object_ref (data->enumerator);
3992 open_read_async_thread (GSimpleAsyncResult *res,
3994 GCancellable *cancellable)
3997 GFileInputStream *stream;
3998 GError *error = NULL;
4000 iface = G_FILE_GET_IFACE (object);
4002 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4006 g_simple_async_result_set_from_error (res, error);
4007 g_error_free (error);
4010 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4014 g_file_real_read_async (GFile *file,
4016 GCancellable *cancellable,
4017 GAsyncReadyCallback callback,
4020 GSimpleAsyncResult *res;
4022 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4024 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4025 g_object_unref (res);
4028 static GFileInputStream *
4029 g_file_real_read_finish (GFile *file,
4033 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4036 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4038 op = g_simple_async_result_get_op_res_gpointer (simple);
4040 return g_object_ref (op);
4046 append_to_async_thread (GSimpleAsyncResult *res,
4048 GCancellable *cancellable)
4051 GFileCreateFlags *data;
4052 GFileOutputStream *stream;
4053 GError *error = NULL;
4055 iface = G_FILE_GET_IFACE (object);
4057 data = g_simple_async_result_get_op_res_gpointer (res);
4059 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4063 g_simple_async_result_set_from_error (res, error);
4064 g_error_free (error);
4067 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4071 g_file_real_append_to_async (GFile *file,
4072 GFileCreateFlags flags,
4074 GCancellable *cancellable,
4075 GAsyncReadyCallback callback,
4078 GFileCreateFlags *data;
4079 GSimpleAsyncResult *res;
4081 data = g_new0 (GFileCreateFlags, 1);
4084 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4085 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4087 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4088 g_object_unref (res);
4091 static GFileOutputStream *
4092 g_file_real_append_to_finish (GFile *file,
4096 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4099 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4101 op = g_simple_async_result_get_op_res_gpointer (simple);
4103 return g_object_ref (op);
4109 create_async_thread (GSimpleAsyncResult *res,
4111 GCancellable *cancellable)
4114 GFileCreateFlags *data;
4115 GFileOutputStream *stream;
4116 GError *error = NULL;
4118 iface = G_FILE_GET_IFACE (object);
4120 data = g_simple_async_result_get_op_res_gpointer (res);
4122 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4126 g_simple_async_result_set_from_error (res, error);
4127 g_error_free (error);
4130 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4134 g_file_real_create_async (GFile *file,
4135 GFileCreateFlags flags,
4137 GCancellable *cancellable,
4138 GAsyncReadyCallback callback,
4141 GFileCreateFlags *data;
4142 GSimpleAsyncResult *res;
4144 data = g_new0 (GFileCreateFlags, 1);
4147 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4148 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4150 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4151 g_object_unref (res);
4154 static GFileOutputStream *
4155 g_file_real_create_finish (GFile *file,
4159 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4162 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4164 op = g_simple_async_result_get_op_res_gpointer (simple);
4166 return g_object_ref (op);
4172 GFileOutputStream *stream;
4174 gboolean make_backup;
4175 GFileCreateFlags flags;
4179 replace_async_data_free (ReplaceAsyncData *data)
4182 g_object_unref (data->stream);
4183 g_free (data->etag);
4188 replace_async_thread (GSimpleAsyncResult *res,
4190 GCancellable *cancellable)
4193 GFileOutputStream *stream;
4194 GError *error = NULL;
4195 ReplaceAsyncData *data;
4197 iface = G_FILE_GET_IFACE (object);
4199 data = g_simple_async_result_get_op_res_gpointer (res);
4201 stream = iface->replace (G_FILE (object),
4210 g_simple_async_result_set_from_error (res, error);
4211 g_error_free (error);
4214 data->stream = stream;
4218 g_file_real_replace_async (GFile *file,
4220 gboolean make_backup,
4221 GFileCreateFlags flags,
4223 GCancellable *cancellable,
4224 GAsyncReadyCallback callback,
4227 GSimpleAsyncResult *res;
4228 ReplaceAsyncData *data;
4230 data = g_new0 (ReplaceAsyncData, 1);
4231 data->etag = g_strdup (etag);
4232 data->make_backup = make_backup;
4233 data->flags = flags;
4235 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4236 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4238 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4239 g_object_unref (res);
4242 static GFileOutputStream *
4243 g_file_real_replace_finish (GFile *file,
4247 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4248 ReplaceAsyncData *data;
4250 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4252 data = g_simple_async_result_get_op_res_gpointer (simple);
4254 return g_object_ref (data->stream);
4262 } SetDisplayNameAsyncData;
4265 set_display_name_data_free (SetDisplayNameAsyncData *data)
4267 g_free (data->name);
4269 g_object_unref (data->file);
4274 set_display_name_async_thread (GSimpleAsyncResult *res,
4276 GCancellable *cancellable)
4278 GError *error = NULL;
4279 SetDisplayNameAsyncData *data;
4282 data = g_simple_async_result_get_op_res_gpointer (res);
4284 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4288 g_simple_async_result_set_from_error (res, error);
4289 g_error_free (error);
4296 g_file_real_set_display_name_async (GFile *file,
4297 const char *display_name,
4299 GCancellable *cancellable,
4300 GAsyncReadyCallback callback,
4303 GSimpleAsyncResult *res;
4304 SetDisplayNameAsyncData *data;
4306 data = g_new0 (SetDisplayNameAsyncData, 1);
4307 data->name = g_strdup (display_name);
4309 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4310 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4312 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4313 g_object_unref (res);
4317 g_file_real_set_display_name_finish (GFile *file,
4321 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4322 SetDisplayNameAsyncData *data;
4324 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4326 data = g_simple_async_result_get_op_res_gpointer (simple);
4328 return g_object_ref (data->file);
4334 GFileQueryInfoFlags flags;
4341 set_info_data_free (SetInfoAsyncData *data)
4344 g_object_unref (data->info);
4346 g_error_free (data->error);
4351 set_info_async_thread (GSimpleAsyncResult *res,
4353 GCancellable *cancellable)
4355 SetInfoAsyncData *data;
4357 data = g_simple_async_result_get_op_res_gpointer (res);
4360 data->res = g_file_set_attributes_from_info (G_FILE (object),
4368 g_file_real_set_attributes_async (GFile *file,
4370 GFileQueryInfoFlags flags,
4372 GCancellable *cancellable,
4373 GAsyncReadyCallback callback,
4376 GSimpleAsyncResult *res;
4377 SetInfoAsyncData *data;
4379 data = g_new0 (SetInfoAsyncData, 1);
4380 data->info = g_file_info_dup (info);
4381 data->flags = flags;
4383 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4384 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4386 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4387 g_object_unref (res);
4391 g_file_real_set_attributes_finish (GFile *file,
4396 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4397 SetInfoAsyncData *data;
4399 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4401 data = g_simple_async_result_get_op_res_gpointer (simple);
4404 *info = g_object_ref (data->info);
4406 if (error != NULL && data->error)
4407 *error = g_error_copy (data->error);
4413 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4415 GCancellable *cancellable)
4417 GError *error = NULL;
4420 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4424 g_simple_async_result_set_from_error (res, error);
4425 g_error_free (error);
4428 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4432 g_file_real_find_enclosing_mount_async (GFile *file,
4434 GCancellable *cancellable,
4435 GAsyncReadyCallback callback,
4438 GSimpleAsyncResult *res;
4440 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4442 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4443 g_object_unref (res);
4447 g_file_real_find_enclosing_mount_finish (GFile *file,
4451 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4454 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4456 mount = g_simple_async_result_get_op_res_gpointer (simple);
4457 return g_object_ref (mount);
4464 GFileCopyFlags flags;
4465 GFileProgressCallback progress_cb;
4466 gpointer progress_cb_data;
4467 GIOSchedulerJob *job;
4471 copy_async_data_free (CopyAsyncData *data)
4473 g_object_unref (data->source);
4474 g_object_unref (data->destination);
4479 CopyAsyncData *data;
4480 goffset current_num_bytes;
4481 goffset total_num_bytes;
4485 copy_async_progress_in_main (gpointer user_data)
4487 ProgressData *progress = user_data;
4488 CopyAsyncData *data = progress->data;
4490 data->progress_cb (progress->current_num_bytes,
4491 progress->total_num_bytes,
4492 data->progress_cb_data);
4498 mainloop_barrier (gpointer user_data)
4500 /* Does nothing, but ensures all queued idles before
4507 copy_async_progress_callback (goffset current_num_bytes,
4508 goffset total_num_bytes,
4511 CopyAsyncData *data = user_data;
4512 ProgressData *progress;
4514 progress = g_new (ProgressData, 1);
4515 progress->data = data;
4516 progress->current_num_bytes = current_num_bytes;
4517 progress->total_num_bytes = total_num_bytes;
4519 g_io_scheduler_job_send_to_mainloop_async (data->job,
4520 copy_async_progress_in_main,
4526 copy_async_thread (GIOSchedulerJob *job,
4527 GCancellable *cancellable,
4530 GSimpleAsyncResult *res;
4531 CopyAsyncData *data;
4536 data = g_simple_async_result_get_op_res_gpointer (res);
4540 result = g_file_copy (data->source,
4544 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4548 /* Ensure all progress callbacks are done running in main thread */
4549 if (data->progress_cb != NULL)
4550 g_io_scheduler_job_send_to_mainloop (job,
4556 g_simple_async_result_set_from_error (res, error);
4557 g_error_free (error);
4560 g_simple_async_result_complete_in_idle (res);
4566 g_file_real_copy_async (GFile *source,
4568 GFileCopyFlags flags,
4570 GCancellable *cancellable,
4571 GFileProgressCallback progress_callback,
4572 gpointer progress_callback_data,
4573 GAsyncReadyCallback callback,
4576 GSimpleAsyncResult *res;
4577 CopyAsyncData *data;
4579 data = g_new0 (CopyAsyncData, 1);
4580 data->source = g_object_ref (source);
4581 data->destination = g_object_ref (destination);
4582 data->flags = flags;
4583 data->progress_cb = progress_callback;
4584 data->progress_cb_data = progress_callback_data;
4586 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4587 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4589 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4593 g_file_real_copy_finish (GFile *file,
4597 /* Error handled in g_file_copy_finish() */
4602 /********************************************
4603 * Default VFS operations *
4604 ********************************************/
4607 * g_file_new_for_path:
4608 * @path: a string containing a relative or absolute path.
4610 * Constructs a #GFile for a given path. This operation never
4611 * fails, but the returned object might not support any I/O
4612 * operation if @path is malformed.
4614 * Returns: a new #GFile for the given @path.
4617 g_file_new_for_path (const char *path)
4619 g_return_val_if_fail (path != NULL, NULL);
4621 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4625 * g_file_new_for_uri:
4626 * @uri: a string containing a URI.
4628 * Constructs a #GFile for a given URI. This operation never
4629 * fails, but the returned object might not support any I/O
4630 * operation if @uri is malformed or if the uri type is
4633 * Returns: a #GFile for the given @uri.
4636 g_file_new_for_uri (const char *uri)
4638 g_return_val_if_fail (uri != NULL, NULL);
4640 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4644 * g_file_parse_name:
4645 * @parse_name: a file name or path to be parsed.
4647 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4648 * This operation never fails, but the returned object might not support any I/O
4649 * operation if the @parse_name cannot be parsed.
4651 * Returns: a new #GFile.
4654 g_file_parse_name (const char *parse_name)
4656 g_return_val_if_fail (parse_name != NULL, NULL);
4658 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4662 is_valid_scheme_character (char c)
4664 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4668 has_valid_scheme (const char *uri)
4674 if (!is_valid_scheme_character (*p))
4679 } while (is_valid_scheme_character (*p));
4685 * g_file_new_for_commandline_arg:
4686 * @arg: a command line string.
4688 * Creates a #GFile with the given argument from the command line. The value of
4689 * @arg can be either a URI, an absolute path or a relative path resolved
4690 * relative to the current working directory.
4691 * This operation never fails, but the returned object might not support any
4692 * I/O operation if @arg points to a malformed path.
4694 * Returns: a new #GFile.
4697 g_file_new_for_commandline_arg (const char *arg)
4703 g_return_val_if_fail (arg != NULL, NULL);
4705 if (g_path_is_absolute (arg))
4706 return g_file_new_for_path (arg);
4708 if (has_valid_scheme (arg))
4709 return g_file_new_for_uri (arg);
4711 current_dir = g_get_current_dir ();
4712 filename = g_build_filename (current_dir, arg, NULL);
4713 g_free (current_dir);
4715 file = g_file_new_for_path (filename);
4722 * g_file_mount_enclosing_volume:
4723 * @location: input #GFile.
4724 * @flags: flags affecting the operation
4725 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4726 * @cancellable: optional #GCancellable object, %NULL to ignore.
4727 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4728 * @user_data: the data to pass to callback function
4730 * Starts a @mount_operation, mounting the volume that contains the file @location.
4732 * When this operation has completed, @callback will be called with
4733 * @user_user data, and the operation can be finalized with
4734 * g_file_mount_enclosing_volume_finish().
4736 * If @cancellable is not %NULL, then the operation can be cancelled by
4737 * triggering the cancellable object from another thread. If the operation
4738 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4741 g_file_mount_enclosing_volume (GFile *location,
4742 GMountMountFlags flags,
4743 GMountOperation *mount_operation,
4744 GCancellable *cancellable,
4745 GAsyncReadyCallback callback,
4750 g_return_if_fail (G_IS_FILE (location));
4752 iface = G_FILE_GET_IFACE (location);
4754 if (iface->mount_enclosing_volume == NULL)
4756 g_simple_async_report_error_in_idle (G_OBJECT (location),
4757 callback, user_data,
4758 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4759 _("volume doesn't implement mount"));
4764 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4769 * g_file_mount_enclosing_volume_finish:
4770 * @location: input #GFile.
4771 * @result: a #GAsyncResult.
4772 * @error: a #GError, or %NULL
4774 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4776 * Returns: %TRUE if successful. If an error
4777 * has occurred, this function will return %FALSE and set @error
4778 * appropriately if present.
4781 g_file_mount_enclosing_volume_finish (GFile *location,
4782 GAsyncResult *result,
4787 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4788 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4790 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4792 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4793 if (g_simple_async_result_propagate_error (simple, error))
4797 iface = G_FILE_GET_IFACE (location);
4799 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4802 /********************************************
4803 * Utility functions *
4804 ********************************************/
4807 * g_file_query_default_handler:
4808 * @file: a #GFile to open.
4809 * @cancellable: optional #GCancellable object, %NULL to ignore.
4810 * @error: a #GError, or %NULL
4812 * Returns the #GAppInfo that is registered as the default
4813 * application to handle the file specified by @file.
4815 * If @cancellable is not %NULL, then the operation can be cancelled by
4816 * triggering the cancellable object from another thread. If the operation
4817 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4819 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4820 * When you are done with it, release it with g_object_unref()
4823 g_file_query_default_handler (GFile *file,
4824 GCancellable *cancellable,
4828 const char *content_type;
4833 uri_scheme = g_file_get_uri_scheme (file);
4834 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4835 g_free (uri_scheme);
4837 if (appinfo != NULL)
4840 info = g_file_query_info (file,
4841 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4850 content_type = g_file_info_get_content_type (info);
4853 /* Don't use is_native(), as we want to support fuse paths if availible */
4854 path = g_file_get_path (file);
4855 appinfo = g_app_info_get_default_for_type (content_type,
4860 g_object_unref (info);
4862 if (appinfo != NULL)
4865 g_set_error (error, G_IO_ERROR,
4866 G_IO_ERROR_NOT_SUPPORTED,
4867 _("No application is registered as handling this file"));
4873 #define GET_CONTENT_BLOCK_SIZE 8192
4876 * g_file_load_contents:
4877 * @file: input #GFile.
4878 * @cancellable: optional #GCancellable object, %NULL to ignore.
4879 * @contents: a location to place the contents of the file.
4880 * @length: a location to place the length of the contents of the file.
4881 * @etag_out: a location to place the current entity tag for the file.
4882 * @error: a #GError, or %NULL
4884 * Loads the content of the file into memory, returning the size of
4885 * the data. The data is always zero terminated, but this is not
4886 * included in the resultant @length.
4888 * If @cancellable is not %NULL, then the operation can be cancelled by
4889 * triggering the cancellable object from another thread. If the operation
4890 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4892 * Returns: %TRUE if the @file's contents were successfully loaded.
4893 * %FALSE if there were errors..
4896 g_file_load_contents (GFile *file,
4897 GCancellable *cancellable,
4903 GFileInputStream *in;
4904 GByteArray *content;
4909 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4910 g_return_val_if_fail (contents != NULL, FALSE);
4912 in = g_file_read (file, cancellable, error);
4916 content = g_byte_array_new ();
4919 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4920 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4921 content->data + pos,
4922 GET_CONTENT_BLOCK_SIZE,
4923 cancellable, error)) > 0)
4926 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4933 info = g_file_input_stream_query_info (in,
4934 G_FILE_ATTRIBUTE_ETAG_VALUE,
4939 *etag_out = g_strdup (g_file_info_get_etag (info));
4940 g_object_unref (info);
4944 /* Ignore errors on close */
4945 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4946 g_object_unref (in);
4950 /* error is set already */
4951 g_byte_array_free (content, TRUE);
4958 /* Zero terminate (we got an extra byte allocated for this */
4959 content->data[pos] = 0;
4961 *contents = (char *)g_byte_array_free (content, FALSE);
4969 GCancellable *cancellable;
4970 GFileReadMoreCallback read_more_callback;
4971 GAsyncReadyCallback callback;
4973 GByteArray *content;
4980 load_contents_data_free (LoadContentsData *data)
4983 g_error_free (data->error);
4984 if (data->cancellable)
4985 g_object_unref (data->cancellable);
4987 g_byte_array_free (data->content, TRUE);
4988 g_free (data->etag);
4989 g_object_unref (data->file);
4994 load_contents_close_callback (GObject *obj,
4995 GAsyncResult *close_res,
4998 GInputStream *stream = G_INPUT_STREAM (obj);
4999 LoadContentsData *data = user_data;
5000 GSimpleAsyncResult *res;
5002 /* Ignore errors here, we're only reading anyway */
5003 g_input_stream_close_finish (stream, close_res, NULL);
5004 g_object_unref (stream);
5006 res = g_simple_async_result_new (G_OBJECT (data->file),
5009 g_file_load_contents_async);
5010 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5011 g_simple_async_result_complete (res);
5012 g_object_unref (res);
5016 load_contents_fstat_callback (GObject *obj,
5017 GAsyncResult *stat_res,
5020 GInputStream *stream = G_INPUT_STREAM (obj);
5021 LoadContentsData *data = user_data;
5024 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5028 data->etag = g_strdup (g_file_info_get_etag (info));
5029 g_object_unref (info);
5032 g_input_stream_close_async (stream, 0,
5034 load_contents_close_callback, data);
5038 load_contents_read_callback (GObject *obj,
5039 GAsyncResult *read_res,
5042 GInputStream *stream = G_INPUT_STREAM (obj);
5043 LoadContentsData *data = user_data;
5044 GError *error = NULL;
5047 read_size = g_input_stream_read_finish (stream, read_res, &error);
5051 /* Error or EOF, close the file */
5052 data->error = error;
5053 g_input_stream_close_async (stream, 0,
5055 load_contents_close_callback, data);
5057 else if (read_size == 0)
5059 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5060 G_FILE_ATTRIBUTE_ETAG_VALUE,
5063 load_contents_fstat_callback,
5066 else if (read_size > 0)
5068 data->pos += read_size;
5070 g_byte_array_set_size (data->content,
5071 data->pos + GET_CONTENT_BLOCK_SIZE);
5074 if (data->read_more_callback &&
5075 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5076 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5077 G_FILE_ATTRIBUTE_ETAG_VALUE,
5080 load_contents_fstat_callback,
5083 g_input_stream_read_async (stream,
5084 data->content->data + data->pos,
5085 GET_CONTENT_BLOCK_SIZE,
5088 load_contents_read_callback,
5094 load_contents_open_callback (GObject *obj,
5095 GAsyncResult *open_res,
5098 GFile *file = G_FILE (obj);
5099 GFileInputStream *stream;
5100 LoadContentsData *data = user_data;
5101 GError *error = NULL;
5102 GSimpleAsyncResult *res;
5104 stream = g_file_read_finish (file, open_res, &error);
5108 g_byte_array_set_size (data->content,
5109 data->pos + GET_CONTENT_BLOCK_SIZE);
5110 g_input_stream_read_async (G_INPUT_STREAM (stream),
5111 data->content->data + data->pos,
5112 GET_CONTENT_BLOCK_SIZE,
5115 load_contents_read_callback,
5121 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5125 g_simple_async_result_complete (res);
5126 g_error_free (error);
5127 load_contents_data_free (data);
5128 g_object_unref (res);
5133 * g_file_load_partial_contents_async:
5134 * @file: input #GFile.
5135 * @cancellable: optional #GCancellable object, %NULL to ignore.
5136 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5137 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5138 * @user_data: the data to pass to the callback functions.
5140 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5141 * used to stop reading from the file when appropriate, else this function
5142 * will behave exactly as g_file_load_contents_async(). This operation
5143 * can be finished by g_file_load_partial_contents_finish().
5145 * Users of this function should be aware that @user_data is passed to
5146 * both the @read_more_callback and the @callback.
5148 * If @cancellable is not %NULL, then the operation can be cancelled by
5149 * triggering the cancellable object from another thread. If the operation
5150 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5153 g_file_load_partial_contents_async (GFile *file,
5154 GCancellable *cancellable,
5155 GFileReadMoreCallback read_more_callback,
5156 GAsyncReadyCallback callback,
5159 LoadContentsData *data;
5161 g_return_if_fail (G_IS_FILE (file));
5163 data = g_new0 (LoadContentsData, 1);
5166 data->cancellable = g_object_ref (cancellable);
5167 data->read_more_callback = read_more_callback;
5168 data->callback = callback;
5169 data->user_data = user_data;
5170 data->content = g_byte_array_new ();
5171 data->file = g_object_ref (file);
5173 g_file_read_async (file,
5176 load_contents_open_callback,
5181 * g_file_load_partial_contents_finish:
5182 * @file: input #GFile.
5183 * @res: a #GAsyncResult.
5184 * @contents: a location to place the contents of the file.
5185 * @length: a location to place the length of the contents of the file.
5186 * @etag_out: a location to place the current entity tag for the file.
5187 * @error: a #GError, or %NULL
5189 * Finishes an asynchronous partial load operation that was started
5190 * with g_file_load_partial_contents_async().
5192 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5193 * present, it will be set appropriately.
5196 g_file_load_partial_contents_finish (GFile *file,
5203 GSimpleAsyncResult *simple;
5204 LoadContentsData *data;
5206 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5207 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5208 g_return_val_if_fail (contents != NULL, FALSE);
5210 simple = G_SIMPLE_ASYNC_RESULT (res);
5212 if (g_simple_async_result_propagate_error (simple, error))
5215 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5217 data = g_simple_async_result_get_op_res_gpointer (simple);
5221 g_propagate_error (error, data->error);
5230 *length = data->pos;
5234 *etag_out = data->etag;
5238 /* Zero terminate */
5239 g_byte_array_set_size (data->content, data->pos + 1);
5240 data->content->data[data->pos] = 0;
5242 *contents = (char *)g_byte_array_free (data->content, FALSE);
5243 data->content = NULL;
5249 * g_file_load_contents_async:
5250 * @file: input #GFile.
5251 * @cancellable: optional #GCancellable object, %NULL to ignore.
5252 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5253 * @user_data: the data to pass to callback function
5255 * Starts an asynchronous load of the @file's contents.
5257 * For more details, see g_file_load_contents() which is
5258 * the synchronous version of this call.
5260 * When the load operation has completed, @callback will be called
5261 * with @user data. To finish the operation, call
5262 * g_file_load_contents_finish() with the #GAsyncResult returned by
5265 * If @cancellable is not %NULL, then the operation can be cancelled by
5266 * triggering the cancellable object from another thread. If the operation
5267 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5270 g_file_load_contents_async (GFile *file,
5271 GCancellable *cancellable,
5272 GAsyncReadyCallback callback,
5275 g_file_load_partial_contents_async (file,
5278 callback, user_data);
5282 * g_file_load_contents_finish:
5283 * @file: input #GFile.
5284 * @res: a #GAsyncResult.
5285 * @contents: a location to place the contents of the file.
5286 * @length: a location to place the length of the contents of the file.
5287 * @etag_out: a location to place the current entity tag for the file.
5288 * @error: a #GError, or %NULL
5290 * Finishes an asynchronous load of the @file's contents.
5291 * The contents are placed in @contents, and @length is set to the
5292 * size of the @contents string. If @etag_out is present, it will be
5293 * set to the new entity tag for the @file.
5295 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5296 * present, it will be set appropriately.
5299 g_file_load_contents_finish (GFile *file,
5306 return g_file_load_partial_contents_finish (file,
5315 * g_file_replace_contents:
5316 * @file: input #GFile.
5317 * @contents: a string containing the new contents for @file.
5318 * @length: the length of @contents in bytes.
5319 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5321 * @make_backup: %TRUE if a backup should be created.
5322 * @flags: a set of #GFileCreateFlags.
5323 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5324 * for the document. This should be freed with g_free() when no longer
5326 * @cancellable: optional #GCancellable object, %NULL to ignore.
5327 * @error: a #GError, or %NULL
5329 * Replaces the contents of @file with @contents of @length bytes.
5331 * If @etag is specified (not %NULL) any existing file must have that etag, or
5332 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5334 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5336 * If @cancellable is not %NULL, then the operation can be cancelled by
5337 * triggering the cancellable object from another thread. If the operation
5338 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5340 * The returned @new_etag can be used to verify that the file hasn't changed the
5341 * next time it is saved over.
5343 * Returns: %TRUE if successful. If an error
5344 * has occurred, this function will return %FALSE and set @error
5345 * appropriately if present.
5348 g_file_replace_contents (GFile *file,
5349 const char *contents,
5352 gboolean make_backup,
5353 GFileCreateFlags flags,
5355 GCancellable *cancellable,
5358 GFileOutputStream *out;
5359 gsize pos, remainder;
5362 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5363 g_return_val_if_fail (contents != NULL, FALSE);
5365 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5371 while (remainder > 0 &&
5372 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5374 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5382 if (remainder > 0 && res < 0)
5384 /* Ignore errors on close */
5385 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5387 /* error is set already */
5391 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5395 *new_etag = g_file_output_stream_get_etag (out);
5403 GCancellable *cancellable;
5404 GAsyncReadyCallback callback;
5406 const char *content;
5410 } ReplaceContentsData;
5413 replace_contents_data_free (ReplaceContentsData *data)
5416 g_error_free (data->error);
5417 if (data->cancellable)
5418 g_object_unref (data->cancellable);
5419 g_object_unref (data->file);
5420 g_free (data->etag);
5425 replace_contents_close_callback (GObject *obj,
5426 GAsyncResult *close_res,
5429 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5430 ReplaceContentsData *data = user_data;
5431 GSimpleAsyncResult *res;
5433 /* Ignore errors here, we're only reading anyway */
5434 g_output_stream_close_finish (stream, close_res, NULL);
5435 g_object_unref (stream);
5437 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5439 res = g_simple_async_result_new (G_OBJECT (data->file),
5442 g_file_replace_contents_async);
5443 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5444 g_simple_async_result_complete (res);
5445 g_object_unref (res);
5449 replace_contents_write_callback (GObject *obj,
5450 GAsyncResult *read_res,
5453 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5454 ReplaceContentsData *data = user_data;
5455 GError *error = NULL;
5458 write_size = g_output_stream_write_finish (stream, read_res, &error);
5460 if (write_size <= 0)
5462 /* Error or EOF, close the file */
5464 data->error = error;
5465 g_output_stream_close_async (stream, 0,
5467 replace_contents_close_callback, data);
5469 else if (write_size > 0)
5471 data->pos += write_size;
5473 if (data->pos >= data->length)
5474 g_output_stream_close_async (stream, 0,
5476 replace_contents_close_callback, data);
5478 g_output_stream_write_async (stream,
5479 data->content + data->pos,
5480 data->length - data->pos,
5483 replace_contents_write_callback,
5489 replace_contents_open_callback (GObject *obj,
5490 GAsyncResult *open_res,
5493 GFile *file = G_FILE (obj);
5494 GFileOutputStream *stream;
5495 ReplaceContentsData *data = user_data;
5496 GError *error = NULL;
5497 GSimpleAsyncResult *res;
5499 stream = g_file_replace_finish (file, open_res, &error);
5503 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5504 data->content + data->pos,
5505 data->length - data->pos,
5508 replace_contents_write_callback,
5514 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5518 g_simple_async_result_complete (res);
5519 g_error_free (error);
5520 replace_contents_data_free (data);
5521 g_object_unref (res);
5526 * g_file_replace_contents_async:
5527 * @file: input #GFile.
5528 * @contents: string of contents to replace the file with.
5529 * @length: the length of @contents in bytes.
5530 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5531 * @make_backup: %TRUE if a backup should be created.
5532 * @flags: a set of #GFileCreateFlags.
5533 * @cancellable: optional #GCancellable object, %NULL to ignore.
5534 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5535 * @user_data: the data to pass to callback function
5537 * Starts an asynchronous replacement of @file with the given
5538 * @contents of @length bytes. @etag will replace the document's
5539 * current entity tag.
5541 * When this operation has completed, @callback will be called with
5542 * @user_user data, and the operation can be finalized with
5543 * g_file_replace_contents_finish().
5545 * If @cancellable is not %NULL, then the operation can be cancelled by
5546 * triggering the cancellable object from another thread. If the operation
5547 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5549 * If @make_backup is %TRUE, this function will attempt to
5550 * make a backup of @file.
5553 g_file_replace_contents_async (GFile *file,
5554 const char *contents,
5557 gboolean make_backup,
5558 GFileCreateFlags flags,
5559 GCancellable *cancellable,
5560 GAsyncReadyCallback callback,
5563 ReplaceContentsData *data;
5565 g_return_if_fail (G_IS_FILE (file));
5566 g_return_if_fail (contents != NULL);
5568 data = g_new0 (ReplaceContentsData, 1);
5571 data->cancellable = g_object_ref (cancellable);
5572 data->callback = callback;
5573 data->user_data = user_data;
5574 data->content = contents;
5575 data->length = length;
5577 data->file = g_object_ref (file);
5579 g_file_replace_async (file,
5585 replace_contents_open_callback,
5590 * g_file_replace_contents_finish:
5591 * @file: input #GFile.
5592 * @res: a #GAsyncResult.
5593 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5594 * for the document. This should be freed with g_free() when it is no
5596 * @error: a #GError, or %NULL
5598 * Finishes an asynchronous replace of the given @file. See
5599 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5600 * tag for the document, if present.
5602 * Returns: %TRUE on success, %FALSE on failure.
5605 g_file_replace_contents_finish (GFile *file,
5610 GSimpleAsyncResult *simple;
5611 ReplaceContentsData *data;
5613 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5614 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5616 simple = G_SIMPLE_ASYNC_RESULT (res);
5618 if (g_simple_async_result_propagate_error (simple, error))
5621 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5623 data = g_simple_async_result_get_op_res_gpointer (simple);
5627 g_propagate_error (error, data->error);
5635 *new_etag = data->etag;
5636 data->etag = NULL; /* Take ownership */
5642 #define __G_FILE_C__
5643 #include "gioaliasdef.c"