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
2710 * Deletes a file. If the @file is a directory, it will only be deleted if it
2713 * If @cancellable is not %NULL, then the operation can be cancelled by
2714 * triggering the cancellable object from another thread. If the operation
2715 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2717 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2720 g_file_delete (GFile *file,
2721 GCancellable *cancellable,
2726 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2728 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2731 iface = G_FILE_GET_IFACE (file);
2733 if (iface->delete_file == NULL)
2735 g_set_error (error, G_IO_ERROR,
2736 G_IO_ERROR_NOT_SUPPORTED,
2737 _("Operation not supported"));
2741 return (* iface->delete_file) (file, cancellable, error);
2746 * @file: #GFile to send to trash.
2747 * @cancellable: optional #GCancellable object, %NULL to ignore.
2748 * @error: a #GError, or %NULL
2750 * Sends @file to the "Trashcan", if possible. This is similar to
2751 * deleting it, but the user can recover it before emptying the trashcan.
2752 * Not all file systems support trashing, so this call can return the
2753 * %G_IO_ERROR_NOT_SUPPORTED error.
2756 * If @cancellable is not %NULL, then the operation can be cancelled by
2757 * triggering the cancellable object from another thread. If the operation
2758 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2760 * Returns: %TRUE on successful trash, %FALSE otherwise.
2763 g_file_trash (GFile *file,
2764 GCancellable *cancellable,
2769 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2771 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2774 iface = G_FILE_GET_IFACE (file);
2776 if (iface->trash == NULL)
2779 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2780 _("Trash not supported"));
2784 return (* iface->trash) (file, cancellable, error);
2788 * g_file_set_display_name:
2789 * @file: input #GFile.
2790 * @display_name: a string.
2791 * @cancellable: optional #GCancellable object, %NULL to ignore.
2792 * @error: a #GError, or %NULL
2794 * Renames @file to the specified display name.
2796 * The display name is converted from UTF8 to the correct encoding for the target
2797 * filesystem if possible and the @file is renamed to this.
2799 * If you want to implement a rename operation in the user interface the edit name
2800 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2801 * widget, and then the result after editing should be passed to g_file_set_display_name().
2803 * On success the resulting converted filename is returned.
2805 * If @cancellable is not %NULL, then the operation can be cancelled by
2806 * triggering the cancellable object from another thread. If the operation
2807 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2809 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2812 g_file_set_display_name (GFile *file,
2813 const char *display_name,
2814 GCancellable *cancellable,
2819 g_return_val_if_fail (G_IS_FILE (file), NULL);
2820 g_return_val_if_fail (display_name != NULL, NULL);
2822 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2826 G_IO_ERROR_INVALID_ARGUMENT,
2827 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2831 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2834 iface = G_FILE_GET_IFACE (file);
2836 return (* iface->set_display_name) (file, display_name, cancellable, error);
2840 * g_file_set_display_name_async:
2841 * @file: input #GFile.
2842 * @display_name: a string.
2843 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2845 * @cancellable: optional #GCancellable object, %NULL to ignore.
2846 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2847 * @user_data: the data to pass to callback function
2849 * Asynchronously sets the display name for a given #GFile.
2851 * For more details, see g_set_display_name() which is
2852 * the synchronous version of this call.
2854 * When the operation is finished, @callback will be called. You can then call
2855 * g_file_set_display_name_finish() to get the result of the operation.
2858 g_file_set_display_name_async (GFile *file,
2859 const char *display_name,
2861 GCancellable *cancellable,
2862 GAsyncReadyCallback callback,
2867 g_return_if_fail (G_IS_FILE (file));
2868 g_return_if_fail (display_name != NULL);
2870 iface = G_FILE_GET_IFACE (file);
2871 (* iface->set_display_name_async) (file,
2880 * g_file_set_display_name_finish:
2881 * @file: input #GFile.
2882 * @res: a #GAsyncResult.
2883 * @error: a #GError, or %NULL
2885 * Finishes setting a display name started with
2886 * g_file_set_display_name_async().
2888 * Returns: a #GFile or %NULL on error.
2891 g_file_set_display_name_finish (GFile *file,
2897 g_return_val_if_fail (G_IS_FILE (file), NULL);
2898 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2900 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2902 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2903 if (g_simple_async_result_propagate_error (simple, error))
2907 iface = G_FILE_GET_IFACE (file);
2908 return (* iface->set_display_name_finish) (file, res, error);
2912 * g_file_query_settable_attributes:
2913 * @file: input #GFile.
2914 * @cancellable: optional #GCancellable object, %NULL to ignore.
2915 * @error: a #GError, or %NULL
2917 * Obtain the list of settable attributes for the file.
2919 * Returns the type and full attribute name of all the attributes
2920 * that can be set on this file. This doesn't mean setting it will always
2921 * succeed though, you might get an access failure, or some specific
2922 * file may not support a specific attribute.
2924 * If @cancellable is not %NULL, then the operation can be cancelled by
2925 * triggering the cancellable object from another thread. If the operation
2926 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2928 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2929 * When you are done with it, release it with g_file_attribute_info_list_unref()
2931 GFileAttributeInfoList *
2932 g_file_query_settable_attributes (GFile *file,
2933 GCancellable *cancellable,
2938 GFileAttributeInfoList *list;
2940 g_return_val_if_fail (G_IS_FILE (file), NULL);
2942 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2945 iface = G_FILE_GET_IFACE (file);
2947 if (iface->query_settable_attributes == NULL)
2948 return g_file_attribute_info_list_new ();
2951 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2955 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2957 list = g_file_attribute_info_list_new ();
2958 g_error_free (my_error);
2961 g_propagate_error (error, my_error);
2968 * g_file_query_writable_namespaces:
2969 * @file: input #GFile.
2970 * @cancellable: optional #GCancellable object, %NULL to ignore.
2971 * @error: a #GError, or %NULL
2973 * Obtain the list of attribute namespaces where new attributes
2974 * can be created by a user. An example of this is extended
2975 * attributes (in the "xattr" namespace).
2977 * If @cancellable is not %NULL, then the operation can be cancelled by
2978 * triggering the cancellable object from another thread. If the operation
2979 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2981 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2982 * When you are done with it, release it with g_file_attribute_info_list_unref()
2984 GFileAttributeInfoList *
2985 g_file_query_writable_namespaces (GFile *file,
2986 GCancellable *cancellable,
2991 GFileAttributeInfoList *list;
2993 g_return_val_if_fail (G_IS_FILE (file), NULL);
2995 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2998 iface = G_FILE_GET_IFACE (file);
3000 if (iface->query_writable_namespaces == NULL)
3001 return g_file_attribute_info_list_new ();
3004 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3008 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3010 list = g_file_attribute_info_list_new ();
3011 g_error_free (my_error);
3014 g_propagate_error (error, my_error);
3021 * g_file_set_attribute:
3022 * @file: input #GFile.
3023 * @attribute: a string containing the attribute's name.
3024 * @type: The type of the attribute
3025 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3026 * @flags: a set of #GFileQueryInfoFlags.
3027 * @cancellable: optional #GCancellable object, %NULL to ignore.
3028 * @error: a #GError, or %NULL
3030 * Sets an attribute in the file with attribute name @attribute to @value.
3032 * If @cancellable is not %NULL, then the operation can be cancelled by
3033 * triggering the cancellable object from another thread. If the operation
3034 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3036 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3039 g_file_set_attribute (GFile *file,
3040 const char *attribute,
3041 GFileAttributeType type,
3043 GFileQueryInfoFlags flags,
3044 GCancellable *cancellable,
3049 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3050 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3052 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3055 iface = G_FILE_GET_IFACE (file);
3057 if (iface->set_attribute == NULL)
3059 g_set_error (error, G_IO_ERROR,
3060 G_IO_ERROR_NOT_SUPPORTED,
3061 _("Operation not supported"));
3065 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3069 * g_file_set_attributes_from_info:
3070 * @file: input #GFile.
3071 * @info: a #GFileInfo.
3072 * @flags: #GFileQueryInfoFlags
3073 * @cancellable: optional #GCancellable object, %NULL to ignore.
3074 * @error: a #GError, or %NULL
3076 * Tries to set all attributes in the #GFileInfo on the target values,
3077 * not stopping on the first error.
3079 * If there is any error during this operation then @error will be set to
3080 * the first error. Error on particular fields are flagged by setting
3081 * the "status" field in the attribute value to
3082 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3085 * If @cancellable is not %NULL, then the operation can be cancelled by
3086 * triggering the cancellable object from another thread. If the operation
3087 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3089 * Returns: %TRUE if there was any error, %FALSE otherwise.
3092 g_file_set_attributes_from_info (GFile *file,
3094 GFileQueryInfoFlags flags,
3095 GCancellable *cancellable,
3100 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3101 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3103 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3106 g_file_info_clear_status (info);
3108 iface = G_FILE_GET_IFACE (file);
3110 return (* iface->set_attributes_from_info) (file,
3119 g_file_real_set_attributes_from_info (GFile *file,
3121 GFileQueryInfoFlags flags,
3122 GCancellable *cancellable,
3128 GFileAttributeValue *value;
3132 attributes = g_file_info_list_attributes (info, NULL);
3134 for (i = 0; attributes[i] != NULL; i++)
3136 value = _g_file_info_get_attribute_value (info, attributes[i]);
3138 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3141 if (!g_file_set_attribute (file, attributes[i],
3142 value->type, _g_file_attribute_value_peek_as_pointer (value),
3143 flags, cancellable, error))
3145 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3147 /* Don't set error multiple times */
3151 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3154 g_strfreev (attributes);
3160 * g_file_set_attributes_async:
3161 * @file: input #GFile.
3162 * @info: a #GFileInfo.
3163 * @flags: a #GFileQueryInfoFlags.
3164 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3166 * @cancellable: optional #GCancellable object, %NULL to ignore.
3167 * @callback: a #GAsyncReadyCallback.
3168 * @user_data: a #gpointer.
3170 * Asynchronously sets the attributes of @file with @info.
3172 * For more details, see g_file_set_attributes_from_info() which is
3173 * the synchronous version of this call.
3175 * When the operation is finished, @callback will be called. You can then call
3176 * g_file_set_attributes_finish() to get the result of the operation.
3179 g_file_set_attributes_async (GFile *file,
3181 GFileQueryInfoFlags flags,
3183 GCancellable *cancellable,
3184 GAsyncReadyCallback callback,
3189 g_return_if_fail (G_IS_FILE (file));
3190 g_return_if_fail (G_IS_FILE_INFO (info));
3192 iface = G_FILE_GET_IFACE (file);
3193 (* iface->set_attributes_async) (file,
3203 * g_file_set_attributes_finish:
3204 * @file: input #GFile.
3205 * @result: a #GAsyncResult.
3206 * @info: a #GFileInfo.
3207 * @error: a #GError, or %NULL
3209 * Finishes setting an attribute started in g_file_set_attributes_async().
3211 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3214 g_file_set_attributes_finish (GFile *file,
3215 GAsyncResult *result,
3221 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3222 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3224 /* No standard handling of errors here, as we must set info even
3227 iface = G_FILE_GET_IFACE (file);
3228 return (* iface->set_attributes_finish) (file, result, info, error);
3232 * g_file_set_attribute_string:
3233 * @file: input #GFile.
3234 * @attribute: a string containing the attribute's name.
3235 * @value: a string containing the attribute's value.
3236 * @flags: #GFileQueryInfoFlags.
3237 * @cancellable: optional #GCancellable object, %NULL to ignore.
3238 * @error: a #GError, or %NULL
3240 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3241 * If @attribute is of a different type, this operation will fail.
3243 * If @cancellable is not %NULL, then the operation can be cancelled by
3244 * triggering the cancellable object from another thread. If the operation
3245 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3247 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3250 g_file_set_attribute_string (GFile *file,
3251 const char *attribute,
3253 GFileQueryInfoFlags flags,
3254 GCancellable *cancellable,
3257 return g_file_set_attribute (file, attribute,
3258 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3259 flags, cancellable, error);
3263 * g_file_set_attribute_byte_string:
3264 * @file: input #GFile.
3265 * @attribute: a string containing the attribute's name.
3266 * @value: a string containing the attribute's new value.
3267 * @flags: a #GFileQueryInfoFlags.
3268 * @cancellable: optional #GCancellable object, %NULL to ignore.
3269 * @error: a #GError, or %NULL
3271 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3272 * If @attribute is of a different type, this operation will fail,
3275 * If @cancellable is not %NULL, then the operation can be cancelled by
3276 * triggering the cancellable object from another thread. If the operation
3277 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3279 * Returns: %TRUE if the @attribute was successfully set to @value
3280 * in the @file, %FALSE otherwise.
3283 g_file_set_attribute_byte_string (GFile *file,
3284 const char *attribute,
3286 GFileQueryInfoFlags flags,
3287 GCancellable *cancellable,
3290 return g_file_set_attribute (file, attribute,
3291 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3292 flags, cancellable, error);
3296 * g_file_set_attribute_uint32:
3297 * @file: input #GFile.
3298 * @attribute: a string containing the attribute's name.
3299 * @value: a #guint32 containing the attribute's new value.
3300 * @flags: a #GFileQueryInfoFlags.
3301 * @cancellable: optional #GCancellable object, %NULL to ignore.
3302 * @error: a #GError, or %NULL
3304 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3305 * If @attribute is of a different type, this operation will fail.
3307 * If @cancellable is not %NULL, then the operation can be cancelled by
3308 * triggering the cancellable object from another thread. If the operation
3309 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3311 * Returns: %TRUE if the @attribute was successfully set to @value
3312 * in the @file, %FALSE otherwise.
3315 g_file_set_attribute_uint32 (GFile *file,
3316 const char *attribute,
3318 GFileQueryInfoFlags flags,
3319 GCancellable *cancellable,
3322 return g_file_set_attribute (file, attribute,
3323 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3324 flags, cancellable, error);
3328 * g_file_set_attribute_int32:
3329 * @file: input #GFile.
3330 * @attribute: a string containing the attribute's name.
3331 * @value: a #gint32 containing the attribute's new value.
3332 * @flags: a #GFileQueryInfoFlags.
3333 * @cancellable: optional #GCancellable object, %NULL to ignore.
3334 * @error: a #GError, or %NULL
3336 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3337 * If @attribute is of a different type, this operation will fail.
3339 * If @cancellable is not %NULL, then the operation can be cancelled by
3340 * triggering the cancellable object from another thread. If the operation
3341 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3343 * Returns: %TRUE if the @attribute was successfully set to @value
3344 * in the @file, %FALSE otherwise.
3347 g_file_set_attribute_int32 (GFile *file,
3348 const char *attribute,
3350 GFileQueryInfoFlags flags,
3351 GCancellable *cancellable,
3354 return g_file_set_attribute (file, attribute,
3355 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3356 flags, cancellable, error);
3360 * g_file_set_attribute_uint64:
3361 * @file: input #GFile.
3362 * @attribute: a string containing the attribute's name.
3363 * @value: a #guint64 containing the attribute's new value.
3364 * @flags: a #GFileQueryInfoFlags.
3365 * @cancellable: optional #GCancellable object, %NULL to ignore.
3366 * @error: a #GError, or %NULL
3368 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3369 * If @attribute is of a different type, this operation will fail.
3371 * If @cancellable is not %NULL, then the operation can be cancelled by
3372 * triggering the cancellable object from another thread. If the operation
3373 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3375 * Returns: %TRUE if the @attribute was successfully set to @value
3376 * in the @file, %FALSE otherwise.
3379 g_file_set_attribute_uint64 (GFile *file,
3380 const char *attribute,
3382 GFileQueryInfoFlags flags,
3383 GCancellable *cancellable,
3386 return g_file_set_attribute (file, attribute,
3387 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3388 flags, cancellable, error);
3392 * g_file_set_attribute_int64:
3393 * @file: input #GFile.
3394 * @attribute: a string containing the attribute's name.
3395 * @value: a #guint64 containing the attribute's new value.
3396 * @flags: a #GFileQueryInfoFlags.
3397 * @cancellable: optional #GCancellable object, %NULL to ignore.
3398 * @error: a #GError, or %NULL
3400 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3401 * If @attribute is of a different type, this operation will fail.
3403 * If @cancellable is not %NULL, then the operation can be cancelled by
3404 * triggering the cancellable object from another thread. If the operation
3405 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3407 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3410 g_file_set_attribute_int64 (GFile *file,
3411 const char *attribute,
3413 GFileQueryInfoFlags flags,
3414 GCancellable *cancellable,
3417 return g_file_set_attribute (file, attribute,
3418 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3419 flags, cancellable, error);
3423 * g_file_mount_mountable:
3424 * @file: input #GFile.
3425 * @flags: flags affecting the operation
3426 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3427 * @cancellable: optional #GCancellable object, %NULL to ignore.
3428 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3429 * @user_data: the data to pass to callback function
3431 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3432 * Using @mount_operation, you can request callbacks when, for instance,
3433 * passwords are needed during authentication.
3435 * If @cancellable is not %NULL, then the operation can be cancelled by
3436 * triggering the cancellable object from another thread. If the operation
3437 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3439 * When the operation is finished, @callback will be called. You can then call
3440 * g_file_mount_mountable_finish() to get the result of the operation.
3443 g_file_mount_mountable (GFile *file,
3444 GMountMountFlags flags,
3445 GMountOperation *mount_operation,
3446 GCancellable *cancellable,
3447 GAsyncReadyCallback callback,
3452 g_return_if_fail (G_IS_FILE (file));
3454 iface = G_FILE_GET_IFACE (file);
3456 if (iface->mount_mountable == NULL)
3458 g_simple_async_report_error_in_idle (G_OBJECT (file),
3462 G_IO_ERROR_NOT_SUPPORTED,
3463 _("Operation not supported"));
3467 (* iface->mount_mountable) (file,
3476 * g_file_mount_mountable_finish:
3477 * @file: input #GFile.
3478 * @result: a #GAsyncResult.
3479 * @error: a #GError, or %NULL
3481 * Finishes a mount operation. See g_file_mount_mountable() for details.
3483 * Finish an asynchronous mount operation that was started
3484 * with g_file_mount_mountable().
3486 * Returns: a #GFile or %NULL on error.
3489 g_file_mount_mountable_finish (GFile *file,
3490 GAsyncResult *result,
3495 g_return_val_if_fail (G_IS_FILE (file), NULL);
3496 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3498 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3500 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3501 if (g_simple_async_result_propagate_error (simple, error))
3505 iface = G_FILE_GET_IFACE (file);
3506 return (* iface->mount_mountable_finish) (file, result, error);
3510 * g_file_unmount_mountable:
3511 * @file: input #GFile.
3512 * @flags: flags affecting the operation
3513 * @cancellable: optional #GCancellable object, %NULL to ignore.
3514 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3515 * @user_data: the data to pass to callback function
3517 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3519 * If @cancellable is not %NULL, then the operation can be cancelled by
3520 * triggering the cancellable object from another thread. If the operation
3521 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3523 * When the operation is finished, @callback will be called. You can then call
3524 * g_file_unmount_mountable_finish() to get the result of the operation.
3527 g_file_unmount_mountable (GFile *file,
3528 GMountUnmountFlags flags,
3529 GCancellable *cancellable,
3530 GAsyncReadyCallback callback,
3535 g_return_if_fail (G_IS_FILE (file));
3537 iface = G_FILE_GET_IFACE (file);
3539 if (iface->unmount_mountable == NULL)
3541 g_simple_async_report_error_in_idle (G_OBJECT (file),
3545 G_IO_ERROR_NOT_SUPPORTED,
3546 _("Operation not supported"));
3550 (* iface->unmount_mountable) (file,
3558 * g_file_unmount_mountable_finish:
3559 * @file: input #GFile.
3560 * @result: a #GAsyncResult.
3561 * @error: a #GError, or %NULL
3563 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3565 * Finish an asynchronous unmount operation that was started
3566 * with g_file_unmount_mountable().
3568 * Returns: %TRUE if the operation finished successfully. %FALSE
3572 g_file_unmount_mountable_finish (GFile *file,
3573 GAsyncResult *result,
3578 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3579 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3581 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3583 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3584 if (g_simple_async_result_propagate_error (simple, error))
3588 iface = G_FILE_GET_IFACE (file);
3589 return (* iface->unmount_mountable_finish) (file, result, error);
3593 * g_file_eject_mountable:
3594 * @file: input #GFile.
3595 * @flags: flags affecting the operation
3596 * @cancellable: optional #GCancellable object, %NULL to ignore.
3597 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3598 * @user_data: the data to pass to callback function
3600 * Starts an asynchronous eject on a mountable.
3601 * When this operation has completed, @callback will be called with
3602 * @user_user data, and the operation can be finalized with
3603 * g_file_eject_mountable_finish().
3605 * If @cancellable is not %NULL, then the operation can be cancelled by
3606 * triggering the cancellable object from another thread. If the operation
3607 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3610 g_file_eject_mountable (GFile *file,
3611 GMountUnmountFlags flags,
3612 GCancellable *cancellable,
3613 GAsyncReadyCallback callback,
3618 g_return_if_fail (G_IS_FILE (file));
3620 iface = G_FILE_GET_IFACE (file);
3622 if (iface->eject_mountable == NULL)
3624 g_simple_async_report_error_in_idle (G_OBJECT (file),
3628 G_IO_ERROR_NOT_SUPPORTED,
3629 _("Operation not supported"));
3633 (* iface->eject_mountable) (file,
3641 * g_file_eject_mountable_finish:
3642 * @file: input #GFile.
3643 * @result: a #GAsyncResult.
3644 * @error: a #GError, or %NULL
3646 * Finishes an asynchronous eject operation started by
3647 * g_file_eject_mountable().
3649 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3653 g_file_eject_mountable_finish (GFile *file,
3654 GAsyncResult *result,
3659 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3660 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3662 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3664 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3665 if (g_simple_async_result_propagate_error (simple, error))
3669 iface = G_FILE_GET_IFACE (file);
3670 return (* iface->eject_mountable_finish) (file, result, error);
3674 * g_file_monitor_directory:
3675 * @file: input #GFile.
3676 * @flags: a set of #GFileMonitorFlags.
3677 * @cancellable: optional #GCancellable object, %NULL to ignore.
3678 * @error: a #GError, or %NULL.
3680 * Obtains a directory monitor for the given file.
3681 * This may fail if directory monitoring is not supported.
3683 * If @cancellable is not %NULL, then the operation can be cancelled by
3684 * triggering the cancellable object from another thread. If the operation
3685 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3687 * Returns: a #GFileMonitor for the given @file,
3688 * or %NULL on error.
3691 g_file_monitor_directory (GFile *file,
3692 GFileMonitorFlags flags,
3693 GCancellable *cancellable,
3698 g_return_val_if_fail (G_IS_FILE (file), NULL);
3700 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3703 iface = G_FILE_GET_IFACE (file);
3705 if (iface->monitor_dir == NULL)
3707 g_set_error (error, G_IO_ERROR,
3708 G_IO_ERROR_NOT_SUPPORTED,
3709 _("Operation not supported"));
3713 return (* iface->monitor_dir) (file, flags, cancellable, error);
3717 * g_file_monitor_file:
3718 * @file: input #GFile.
3719 * @flags: a set of #GFileMonitorFlags.
3720 * @cancellable: optional #GCancellable object, %NULL to ignore.
3721 * @error: a #GError, or %NULL.
3723 * Obtains a file monitor for the given file. If no file notification
3724 * mechanism exists, then regular polling of the file is used.
3726 * If @cancellable is not %NULL, then the operation can be cancelled by
3727 * triggering the cancellable object from another thread. If the operation
3728 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3730 * Returns: a #GFileMonitor for the given @file.
3733 g_file_monitor_file (GFile *file,
3734 GFileMonitorFlags flags,
3735 GCancellable *cancellable,
3739 GFileMonitor *monitor;
3741 g_return_val_if_fail (G_IS_FILE (file), NULL);
3743 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3746 iface = G_FILE_GET_IFACE (file);
3750 if (iface->monitor_file)
3751 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3753 /* Fallback to polling */
3754 if (monitor == NULL)
3755 monitor = _g_poll_file_monitor_new (file);
3760 /********************************************
3761 * Default implementation of async ops *
3762 ********************************************/
3766 GFileQueryInfoFlags flags;
3768 } QueryInfoAsyncData;
3771 query_info_data_free (QueryInfoAsyncData *data)
3774 g_object_unref (data->info);
3775 g_free (data->attributes);
3780 query_info_async_thread (GSimpleAsyncResult *res,
3782 GCancellable *cancellable)
3784 GError *error = NULL;
3785 QueryInfoAsyncData *data;
3788 data = g_simple_async_result_get_op_res_gpointer (res);
3790 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3794 g_simple_async_result_set_from_error (res, error);
3795 g_error_free (error);
3802 g_file_real_query_info_async (GFile *file,
3803 const char *attributes,
3804 GFileQueryInfoFlags flags,
3806 GCancellable *cancellable,
3807 GAsyncReadyCallback callback,
3810 GSimpleAsyncResult *res;
3811 QueryInfoAsyncData *data;
3813 data = g_new0 (QueryInfoAsyncData, 1);
3814 data->attributes = g_strdup (attributes);
3815 data->flags = flags;
3817 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3818 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3820 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3821 g_object_unref (res);
3825 g_file_real_query_info_finish (GFile *file,
3829 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3830 QueryInfoAsyncData *data;
3832 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3834 data = g_simple_async_result_get_op_res_gpointer (simple);
3836 return g_object_ref (data->info);
3844 } QueryFilesystemInfoAsyncData;
3847 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
3850 g_object_unref (data->info);
3851 g_free (data->attributes);
3856 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
3858 GCancellable *cancellable)
3860 GError *error = NULL;
3861 QueryFilesystemInfoAsyncData *data;
3864 data = g_simple_async_result_get_op_res_gpointer (res);
3866 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
3870 g_simple_async_result_set_from_error (res, error);
3871 g_error_free (error);
3878 g_file_real_query_filesystem_info_async (GFile *file,
3879 const char *attributes,
3881 GCancellable *cancellable,
3882 GAsyncReadyCallback callback,
3885 GSimpleAsyncResult *res;
3886 QueryFilesystemInfoAsyncData *data;
3888 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
3889 data->attributes = g_strdup (attributes);
3891 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
3892 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
3894 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
3895 g_object_unref (res);
3899 g_file_real_query_filesystem_info_finish (GFile *file,
3903 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3904 QueryFilesystemInfoAsyncData *data;
3906 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
3908 data = g_simple_async_result_get_op_res_gpointer (simple);
3910 return g_object_ref (data->info);
3917 GFileQueryInfoFlags flags;
3918 GFileEnumerator *enumerator;
3919 } EnumerateChildrenAsyncData;
3922 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3924 if (data->enumerator)
3925 g_object_unref (data->enumerator);
3926 g_free (data->attributes);
3931 enumerate_children_async_thread (GSimpleAsyncResult *res,
3933 GCancellable *cancellable)
3935 GError *error = NULL;
3936 EnumerateChildrenAsyncData *data;
3937 GFileEnumerator *enumerator;
3939 data = g_simple_async_result_get_op_res_gpointer (res);
3941 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3943 if (enumerator == NULL)
3945 g_simple_async_result_set_from_error (res, error);
3946 g_error_free (error);
3949 data->enumerator = enumerator;
3953 g_file_real_enumerate_children_async (GFile *file,
3954 const char *attributes,
3955 GFileQueryInfoFlags flags,
3957 GCancellable *cancellable,
3958 GAsyncReadyCallback callback,
3961 GSimpleAsyncResult *res;
3962 EnumerateChildrenAsyncData *data;
3964 data = g_new0 (EnumerateChildrenAsyncData, 1);
3965 data->attributes = g_strdup (attributes);
3966 data->flags = flags;
3968 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3969 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3971 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3972 g_object_unref (res);
3975 static GFileEnumerator *
3976 g_file_real_enumerate_children_finish (GFile *file,
3980 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3981 EnumerateChildrenAsyncData *data;
3983 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3985 data = g_simple_async_result_get_op_res_gpointer (simple);
3986 if (data->enumerator)
3987 return g_object_ref (data->enumerator);
3993 open_read_async_thread (GSimpleAsyncResult *res,
3995 GCancellable *cancellable)
3998 GFileInputStream *stream;
3999 GError *error = NULL;
4001 iface = G_FILE_GET_IFACE (object);
4003 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4007 g_simple_async_result_set_from_error (res, error);
4008 g_error_free (error);
4011 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4015 g_file_real_read_async (GFile *file,
4017 GCancellable *cancellable,
4018 GAsyncReadyCallback callback,
4021 GSimpleAsyncResult *res;
4023 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4025 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4026 g_object_unref (res);
4029 static GFileInputStream *
4030 g_file_real_read_finish (GFile *file,
4034 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4037 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4039 op = g_simple_async_result_get_op_res_gpointer (simple);
4041 return g_object_ref (op);
4047 append_to_async_thread (GSimpleAsyncResult *res,
4049 GCancellable *cancellable)
4052 GFileCreateFlags *data;
4053 GFileOutputStream *stream;
4054 GError *error = NULL;
4056 iface = G_FILE_GET_IFACE (object);
4058 data = g_simple_async_result_get_op_res_gpointer (res);
4060 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4064 g_simple_async_result_set_from_error (res, error);
4065 g_error_free (error);
4068 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4072 g_file_real_append_to_async (GFile *file,
4073 GFileCreateFlags flags,
4075 GCancellable *cancellable,
4076 GAsyncReadyCallback callback,
4079 GFileCreateFlags *data;
4080 GSimpleAsyncResult *res;
4082 data = g_new0 (GFileCreateFlags, 1);
4085 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4086 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4088 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4089 g_object_unref (res);
4092 static GFileOutputStream *
4093 g_file_real_append_to_finish (GFile *file,
4097 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4100 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4102 op = g_simple_async_result_get_op_res_gpointer (simple);
4104 return g_object_ref (op);
4110 create_async_thread (GSimpleAsyncResult *res,
4112 GCancellable *cancellable)
4115 GFileCreateFlags *data;
4116 GFileOutputStream *stream;
4117 GError *error = NULL;
4119 iface = G_FILE_GET_IFACE (object);
4121 data = g_simple_async_result_get_op_res_gpointer (res);
4123 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4127 g_simple_async_result_set_from_error (res, error);
4128 g_error_free (error);
4131 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4135 g_file_real_create_async (GFile *file,
4136 GFileCreateFlags flags,
4138 GCancellable *cancellable,
4139 GAsyncReadyCallback callback,
4142 GFileCreateFlags *data;
4143 GSimpleAsyncResult *res;
4145 data = g_new0 (GFileCreateFlags, 1);
4148 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4149 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4151 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4152 g_object_unref (res);
4155 static GFileOutputStream *
4156 g_file_real_create_finish (GFile *file,
4160 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4163 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4165 op = g_simple_async_result_get_op_res_gpointer (simple);
4167 return g_object_ref (op);
4173 GFileOutputStream *stream;
4175 gboolean make_backup;
4176 GFileCreateFlags flags;
4180 replace_async_data_free (ReplaceAsyncData *data)
4183 g_object_unref (data->stream);
4184 g_free (data->etag);
4189 replace_async_thread (GSimpleAsyncResult *res,
4191 GCancellable *cancellable)
4194 GFileOutputStream *stream;
4195 GError *error = NULL;
4196 ReplaceAsyncData *data;
4198 iface = G_FILE_GET_IFACE (object);
4200 data = g_simple_async_result_get_op_res_gpointer (res);
4202 stream = iface->replace (G_FILE (object),
4211 g_simple_async_result_set_from_error (res, error);
4212 g_error_free (error);
4215 data->stream = stream;
4219 g_file_real_replace_async (GFile *file,
4221 gboolean make_backup,
4222 GFileCreateFlags flags,
4224 GCancellable *cancellable,
4225 GAsyncReadyCallback callback,
4228 GSimpleAsyncResult *res;
4229 ReplaceAsyncData *data;
4231 data = g_new0 (ReplaceAsyncData, 1);
4232 data->etag = g_strdup (etag);
4233 data->make_backup = make_backup;
4234 data->flags = flags;
4236 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4237 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4239 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4240 g_object_unref (res);
4243 static GFileOutputStream *
4244 g_file_real_replace_finish (GFile *file,
4248 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4249 ReplaceAsyncData *data;
4251 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4253 data = g_simple_async_result_get_op_res_gpointer (simple);
4255 return g_object_ref (data->stream);
4263 } SetDisplayNameAsyncData;
4266 set_display_name_data_free (SetDisplayNameAsyncData *data)
4268 g_free (data->name);
4270 g_object_unref (data->file);
4275 set_display_name_async_thread (GSimpleAsyncResult *res,
4277 GCancellable *cancellable)
4279 GError *error = NULL;
4280 SetDisplayNameAsyncData *data;
4283 data = g_simple_async_result_get_op_res_gpointer (res);
4285 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4289 g_simple_async_result_set_from_error (res, error);
4290 g_error_free (error);
4297 g_file_real_set_display_name_async (GFile *file,
4298 const char *display_name,
4300 GCancellable *cancellable,
4301 GAsyncReadyCallback callback,
4304 GSimpleAsyncResult *res;
4305 SetDisplayNameAsyncData *data;
4307 data = g_new0 (SetDisplayNameAsyncData, 1);
4308 data->name = g_strdup (display_name);
4310 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4311 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4313 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4314 g_object_unref (res);
4318 g_file_real_set_display_name_finish (GFile *file,
4322 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4323 SetDisplayNameAsyncData *data;
4325 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4327 data = g_simple_async_result_get_op_res_gpointer (simple);
4329 return g_object_ref (data->file);
4335 GFileQueryInfoFlags flags;
4342 set_info_data_free (SetInfoAsyncData *data)
4345 g_object_unref (data->info);
4347 g_error_free (data->error);
4352 set_info_async_thread (GSimpleAsyncResult *res,
4354 GCancellable *cancellable)
4356 SetInfoAsyncData *data;
4358 data = g_simple_async_result_get_op_res_gpointer (res);
4361 data->res = g_file_set_attributes_from_info (G_FILE (object),
4369 g_file_real_set_attributes_async (GFile *file,
4371 GFileQueryInfoFlags flags,
4373 GCancellable *cancellable,
4374 GAsyncReadyCallback callback,
4377 GSimpleAsyncResult *res;
4378 SetInfoAsyncData *data;
4380 data = g_new0 (SetInfoAsyncData, 1);
4381 data->info = g_file_info_dup (info);
4382 data->flags = flags;
4384 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4385 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4387 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4388 g_object_unref (res);
4392 g_file_real_set_attributes_finish (GFile *file,
4397 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4398 SetInfoAsyncData *data;
4400 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4402 data = g_simple_async_result_get_op_res_gpointer (simple);
4405 *info = g_object_ref (data->info);
4407 if (error != NULL && data->error)
4408 *error = g_error_copy (data->error);
4414 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4416 GCancellable *cancellable)
4418 GError *error = NULL;
4421 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4425 g_simple_async_result_set_from_error (res, error);
4426 g_error_free (error);
4429 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4433 g_file_real_find_enclosing_mount_async (GFile *file,
4435 GCancellable *cancellable,
4436 GAsyncReadyCallback callback,
4439 GSimpleAsyncResult *res;
4441 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4443 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4444 g_object_unref (res);
4448 g_file_real_find_enclosing_mount_finish (GFile *file,
4452 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4455 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4457 mount = g_simple_async_result_get_op_res_gpointer (simple);
4458 return g_object_ref (mount);
4465 GFileCopyFlags flags;
4466 GFileProgressCallback progress_cb;
4467 gpointer progress_cb_data;
4468 GIOSchedulerJob *job;
4472 copy_async_data_free (CopyAsyncData *data)
4474 g_object_unref (data->source);
4475 g_object_unref (data->destination);
4480 CopyAsyncData *data;
4481 goffset current_num_bytes;
4482 goffset total_num_bytes;
4486 copy_async_progress_in_main (gpointer user_data)
4488 ProgressData *progress = user_data;
4489 CopyAsyncData *data = progress->data;
4491 data->progress_cb (progress->current_num_bytes,
4492 progress->total_num_bytes,
4493 data->progress_cb_data);
4499 mainloop_barrier (gpointer user_data)
4501 /* Does nothing, but ensures all queued idles before
4508 copy_async_progress_callback (goffset current_num_bytes,
4509 goffset total_num_bytes,
4512 CopyAsyncData *data = user_data;
4513 ProgressData *progress;
4515 progress = g_new (ProgressData, 1);
4516 progress->data = data;
4517 progress->current_num_bytes = current_num_bytes;
4518 progress->total_num_bytes = total_num_bytes;
4520 g_io_scheduler_job_send_to_mainloop_async (data->job,
4521 copy_async_progress_in_main,
4527 copy_async_thread (GIOSchedulerJob *job,
4528 GCancellable *cancellable,
4531 GSimpleAsyncResult *res;
4532 CopyAsyncData *data;
4537 data = g_simple_async_result_get_op_res_gpointer (res);
4541 result = g_file_copy (data->source,
4545 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4549 /* Ensure all progress callbacks are done running in main thread */
4550 if (data->progress_cb != NULL)
4551 g_io_scheduler_job_send_to_mainloop (job,
4557 g_simple_async_result_set_from_error (res, error);
4558 g_error_free (error);
4561 g_simple_async_result_complete_in_idle (res);
4567 g_file_real_copy_async (GFile *source,
4569 GFileCopyFlags flags,
4571 GCancellable *cancellable,
4572 GFileProgressCallback progress_callback,
4573 gpointer progress_callback_data,
4574 GAsyncReadyCallback callback,
4577 GSimpleAsyncResult *res;
4578 CopyAsyncData *data;
4580 data = g_new0 (CopyAsyncData, 1);
4581 data->source = g_object_ref (source);
4582 data->destination = g_object_ref (destination);
4583 data->flags = flags;
4584 data->progress_cb = progress_callback;
4585 data->progress_cb_data = progress_callback_data;
4587 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4588 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4590 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4594 g_file_real_copy_finish (GFile *file,
4598 /* Error handled in g_file_copy_finish() */
4603 /********************************************
4604 * Default VFS operations *
4605 ********************************************/
4608 * g_file_new_for_path:
4609 * @path: a string containing a relative or absolute path.
4611 * Constructs a #GFile for a given path. This operation never
4612 * fails, but the returned object might not support any I/O
4613 * operation if @path is malformed.
4615 * Returns: a new #GFile for the given @path.
4618 g_file_new_for_path (const char *path)
4620 g_return_val_if_fail (path != NULL, NULL);
4622 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4626 * g_file_new_for_uri:
4627 * @uri: a string containing a URI.
4629 * Constructs a #GFile for a given URI. This operation never
4630 * fails, but the returned object might not support any I/O
4631 * operation if @uri is malformed or if the uri type is
4634 * Returns: a #GFile for the given @uri.
4637 g_file_new_for_uri (const char *uri)
4639 g_return_val_if_fail (uri != NULL, NULL);
4641 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4645 * g_file_parse_name:
4646 * @parse_name: a file name or path to be parsed.
4648 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4649 * This operation never fails, but the returned object might not support any I/O
4650 * operation if the @parse_name cannot be parsed.
4652 * Returns: a new #GFile.
4655 g_file_parse_name (const char *parse_name)
4657 g_return_val_if_fail (parse_name != NULL, NULL);
4659 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4663 is_valid_scheme_character (char c)
4665 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4669 has_valid_scheme (const char *uri)
4675 if (!is_valid_scheme_character (*p))
4680 } while (is_valid_scheme_character (*p));
4686 * g_file_new_for_commandline_arg:
4687 * @arg: a command line string.
4689 * Creates a #GFile with the given argument from the command line. The value of
4690 * @arg can be either a URI, an absolute path or a relative path resolved
4691 * relative to the current working directory.
4692 * This operation never fails, but the returned object might not support any
4693 * I/O operation if @arg points to a malformed path.
4695 * Returns: a new #GFile.
4698 g_file_new_for_commandline_arg (const char *arg)
4704 g_return_val_if_fail (arg != NULL, NULL);
4706 if (g_path_is_absolute (arg))
4707 return g_file_new_for_path (arg);
4709 if (has_valid_scheme (arg))
4710 return g_file_new_for_uri (arg);
4712 current_dir = g_get_current_dir ();
4713 filename = g_build_filename (current_dir, arg, NULL);
4714 g_free (current_dir);
4716 file = g_file_new_for_path (filename);
4723 * g_file_mount_enclosing_volume:
4724 * @location: input #GFile.
4725 * @flags: flags affecting the operation
4726 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4727 * @cancellable: optional #GCancellable object, %NULL to ignore.
4728 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4729 * @user_data: the data to pass to callback function
4731 * Starts a @mount_operation, mounting the volume that contains the file @location.
4733 * When this operation has completed, @callback will be called with
4734 * @user_user data, and the operation can be finalized with
4735 * g_file_mount_enclosing_volume_finish().
4737 * If @cancellable is not %NULL, then the operation can be cancelled by
4738 * triggering the cancellable object from another thread. If the operation
4739 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4742 g_file_mount_enclosing_volume (GFile *location,
4743 GMountMountFlags flags,
4744 GMountOperation *mount_operation,
4745 GCancellable *cancellable,
4746 GAsyncReadyCallback callback,
4751 g_return_if_fail (G_IS_FILE (location));
4753 iface = G_FILE_GET_IFACE (location);
4755 if (iface->mount_enclosing_volume == NULL)
4757 g_simple_async_report_error_in_idle (G_OBJECT (location),
4758 callback, user_data,
4759 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4760 _("volume doesn't implement mount"));
4765 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4770 * g_file_mount_enclosing_volume_finish:
4771 * @location: input #GFile.
4772 * @result: a #GAsyncResult.
4773 * @error: a #GError, or %NULL
4775 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4777 * Returns: %TRUE if successful. If an error
4778 * has occurred, this function will return %FALSE and set @error
4779 * appropriately if present.
4782 g_file_mount_enclosing_volume_finish (GFile *location,
4783 GAsyncResult *result,
4788 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4789 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4791 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4793 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4794 if (g_simple_async_result_propagate_error (simple, error))
4798 iface = G_FILE_GET_IFACE (location);
4800 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4803 /********************************************
4804 * Utility functions *
4805 ********************************************/
4808 * g_file_query_default_handler:
4809 * @file: a #GFile to open.
4810 * @cancellable: optional #GCancellable object, %NULL to ignore.
4811 * @error: a #GError, or %NULL
4813 * Returns the #GAppInfo that is registered as the default
4814 * application to handle the file specified by @file.
4816 * If @cancellable is not %NULL, then the operation can be cancelled by
4817 * triggering the cancellable object from another thread. If the operation
4818 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4820 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4821 * When you are done with it, release it with g_object_unref()
4824 g_file_query_default_handler (GFile *file,
4825 GCancellable *cancellable,
4829 const char *content_type;
4834 uri_scheme = g_file_get_uri_scheme (file);
4835 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4836 g_free (uri_scheme);
4838 if (appinfo != NULL)
4841 info = g_file_query_info (file,
4842 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4851 content_type = g_file_info_get_content_type (info);
4854 /* Don't use is_native(), as we want to support fuse paths if availible */
4855 path = g_file_get_path (file);
4856 appinfo = g_app_info_get_default_for_type (content_type,
4861 g_object_unref (info);
4863 if (appinfo != NULL)
4866 g_set_error (error, G_IO_ERROR,
4867 G_IO_ERROR_NOT_SUPPORTED,
4868 _("No application is registered as handling this file"));
4874 #define GET_CONTENT_BLOCK_SIZE 8192
4877 * g_file_load_contents:
4878 * @file: input #GFile.
4879 * @cancellable: optional #GCancellable object, %NULL to ignore.
4880 * @contents: a location to place the contents of the file.
4881 * @length: a location to place the length of the contents of the file.
4882 * @etag_out: a location to place the current entity tag for the file.
4883 * @error: a #GError, or %NULL
4885 * Loads the content of the file into memory, returning the size of
4886 * the data. The data is always zero terminated, but this is not
4887 * included in the resultant @length.
4889 * If @cancellable is not %NULL, then the operation can be cancelled by
4890 * triggering the cancellable object from another thread. If the operation
4891 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4893 * Returns: %TRUE if the @file's contents were successfully loaded.
4894 * %FALSE if there were errors..
4897 g_file_load_contents (GFile *file,
4898 GCancellable *cancellable,
4904 GFileInputStream *in;
4905 GByteArray *content;
4910 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4911 g_return_val_if_fail (contents != NULL, FALSE);
4913 in = g_file_read (file, cancellable, error);
4917 content = g_byte_array_new ();
4920 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4921 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4922 content->data + pos,
4923 GET_CONTENT_BLOCK_SIZE,
4924 cancellable, error)) > 0)
4927 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4934 info = g_file_input_stream_query_info (in,
4935 G_FILE_ATTRIBUTE_ETAG_VALUE,
4940 *etag_out = g_strdup (g_file_info_get_etag (info));
4941 g_object_unref (info);
4945 /* Ignore errors on close */
4946 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4947 g_object_unref (in);
4951 /* error is set already */
4952 g_byte_array_free (content, TRUE);
4959 /* Zero terminate (we got an extra byte allocated for this */
4960 content->data[pos] = 0;
4962 *contents = (char *)g_byte_array_free (content, FALSE);
4970 GCancellable *cancellable;
4971 GFileReadMoreCallback read_more_callback;
4972 GAsyncReadyCallback callback;
4974 GByteArray *content;
4981 load_contents_data_free (LoadContentsData *data)
4984 g_error_free (data->error);
4985 if (data->cancellable)
4986 g_object_unref (data->cancellable);
4988 g_byte_array_free (data->content, TRUE);
4989 g_free (data->etag);
4990 g_object_unref (data->file);
4995 load_contents_close_callback (GObject *obj,
4996 GAsyncResult *close_res,
4999 GInputStream *stream = G_INPUT_STREAM (obj);
5000 LoadContentsData *data = user_data;
5001 GSimpleAsyncResult *res;
5003 /* Ignore errors here, we're only reading anyway */
5004 g_input_stream_close_finish (stream, close_res, NULL);
5005 g_object_unref (stream);
5007 res = g_simple_async_result_new (G_OBJECT (data->file),
5010 g_file_load_contents_async);
5011 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5012 g_simple_async_result_complete (res);
5013 g_object_unref (res);
5017 load_contents_fstat_callback (GObject *obj,
5018 GAsyncResult *stat_res,
5021 GInputStream *stream = G_INPUT_STREAM (obj);
5022 LoadContentsData *data = user_data;
5025 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5029 data->etag = g_strdup (g_file_info_get_etag (info));
5030 g_object_unref (info);
5033 g_input_stream_close_async (stream, 0,
5035 load_contents_close_callback, data);
5039 load_contents_read_callback (GObject *obj,
5040 GAsyncResult *read_res,
5043 GInputStream *stream = G_INPUT_STREAM (obj);
5044 LoadContentsData *data = user_data;
5045 GError *error = NULL;
5048 read_size = g_input_stream_read_finish (stream, read_res, &error);
5052 /* Error or EOF, close the file */
5053 data->error = error;
5054 g_input_stream_close_async (stream, 0,
5056 load_contents_close_callback, data);
5058 else if (read_size == 0)
5060 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5061 G_FILE_ATTRIBUTE_ETAG_VALUE,
5064 load_contents_fstat_callback,
5067 else if (read_size > 0)
5069 data->pos += read_size;
5071 g_byte_array_set_size (data->content,
5072 data->pos + GET_CONTENT_BLOCK_SIZE);
5075 if (data->read_more_callback &&
5076 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5077 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5078 G_FILE_ATTRIBUTE_ETAG_VALUE,
5081 load_contents_fstat_callback,
5084 g_input_stream_read_async (stream,
5085 data->content->data + data->pos,
5086 GET_CONTENT_BLOCK_SIZE,
5089 load_contents_read_callback,
5095 load_contents_open_callback (GObject *obj,
5096 GAsyncResult *open_res,
5099 GFile *file = G_FILE (obj);
5100 GFileInputStream *stream;
5101 LoadContentsData *data = user_data;
5102 GError *error = NULL;
5103 GSimpleAsyncResult *res;
5105 stream = g_file_read_finish (file, open_res, &error);
5109 g_byte_array_set_size (data->content,
5110 data->pos + GET_CONTENT_BLOCK_SIZE);
5111 g_input_stream_read_async (G_INPUT_STREAM (stream),
5112 data->content->data + data->pos,
5113 GET_CONTENT_BLOCK_SIZE,
5116 load_contents_read_callback,
5122 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5126 g_simple_async_result_complete (res);
5127 g_error_free (error);
5128 load_contents_data_free (data);
5129 g_object_unref (res);
5134 * g_file_load_partial_contents_async:
5135 * @file: input #GFile.
5136 * @cancellable: optional #GCancellable object, %NULL to ignore.
5137 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5138 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5139 * @user_data: the data to pass to the callback functions.
5141 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5142 * used to stop reading from the file when appropriate, else this function
5143 * will behave exactly as g_file_load_contents_async(). This operation
5144 * can be finished by g_file_load_partial_contents_finish().
5146 * Users of this function should be aware that @user_data is passed to
5147 * both the @read_more_callback and the @callback.
5149 * If @cancellable is not %NULL, then the operation can be cancelled by
5150 * triggering the cancellable object from another thread. If the operation
5151 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5154 g_file_load_partial_contents_async (GFile *file,
5155 GCancellable *cancellable,
5156 GFileReadMoreCallback read_more_callback,
5157 GAsyncReadyCallback callback,
5160 LoadContentsData *data;
5162 g_return_if_fail (G_IS_FILE (file));
5164 data = g_new0 (LoadContentsData, 1);
5167 data->cancellable = g_object_ref (cancellable);
5168 data->read_more_callback = read_more_callback;
5169 data->callback = callback;
5170 data->user_data = user_data;
5171 data->content = g_byte_array_new ();
5172 data->file = g_object_ref (file);
5174 g_file_read_async (file,
5177 load_contents_open_callback,
5182 * g_file_load_partial_contents_finish:
5183 * @file: input #GFile.
5184 * @res: a #GAsyncResult.
5185 * @contents: a location to place the contents of the file.
5186 * @length: a location to place the length of the contents of the file.
5187 * @etag_out: a location to place the current entity tag for the file.
5188 * @error: a #GError, or %NULL
5190 * Finishes an asynchronous partial load operation that was started
5191 * with g_file_load_partial_contents_async().
5193 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5194 * present, it will be set appropriately.
5197 g_file_load_partial_contents_finish (GFile *file,
5204 GSimpleAsyncResult *simple;
5205 LoadContentsData *data;
5207 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5208 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5209 g_return_val_if_fail (contents != NULL, FALSE);
5211 simple = G_SIMPLE_ASYNC_RESULT (res);
5213 if (g_simple_async_result_propagate_error (simple, error))
5216 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5218 data = g_simple_async_result_get_op_res_gpointer (simple);
5222 g_propagate_error (error, data->error);
5231 *length = data->pos;
5235 *etag_out = data->etag;
5239 /* Zero terminate */
5240 g_byte_array_set_size (data->content, data->pos + 1);
5241 data->content->data[data->pos] = 0;
5243 *contents = (char *)g_byte_array_free (data->content, FALSE);
5244 data->content = NULL;
5250 * g_file_load_contents_async:
5251 * @file: input #GFile.
5252 * @cancellable: optional #GCancellable object, %NULL to ignore.
5253 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5254 * @user_data: the data to pass to callback function
5256 * Starts an asynchronous load of the @file's contents.
5258 * For more details, see g_file_load_contents() which is
5259 * the synchronous version of this call.
5261 * When the load operation has completed, @callback will be called
5262 * with @user data. To finish the operation, call
5263 * g_file_load_contents_finish() with the #GAsyncResult returned by
5266 * If @cancellable is not %NULL, then the operation can be cancelled by
5267 * triggering the cancellable object from another thread. If the operation
5268 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5271 g_file_load_contents_async (GFile *file,
5272 GCancellable *cancellable,
5273 GAsyncReadyCallback callback,
5276 g_file_load_partial_contents_async (file,
5279 callback, user_data);
5283 * g_file_load_contents_finish:
5284 * @file: input #GFile.
5285 * @res: a #GAsyncResult.
5286 * @contents: a location to place the contents of the file.
5287 * @length: a location to place the length of the contents of the file.
5288 * @etag_out: a location to place the current entity tag for the file.
5289 * @error: a #GError, or %NULL
5291 * Finishes an asynchronous load of the @file's contents.
5292 * The contents are placed in @contents, and @length is set to the
5293 * size of the @contents string. If @etag_out is present, it will be
5294 * set to the new entity tag for the @file.
5296 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5297 * present, it will be set appropriately.
5300 g_file_load_contents_finish (GFile *file,
5307 return g_file_load_partial_contents_finish (file,
5316 * g_file_replace_contents:
5317 * @file: input #GFile.
5318 * @contents: a string containing the new contents for @file.
5319 * @length: the length of @contents in bytes.
5320 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5322 * @make_backup: %TRUE if a backup should be created.
5323 * @flags: a set of #GFileCreateFlags.
5324 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5325 * for the document. This should be freed with g_free() when no longer
5327 * @cancellable: optional #GCancellable object, %NULL to ignore.
5328 * @error: a #GError, or %NULL
5330 * Replaces the contents of @file with @contents of @length bytes.
5332 * If @etag is specified (not %NULL) any existing file must have that etag, or
5333 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5335 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5337 * If @cancellable is not %NULL, then the operation can be cancelled by
5338 * triggering the cancellable object from another thread. If the operation
5339 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5341 * The returned @new_etag can be used to verify that the file hasn't changed the
5342 * next time it is saved over.
5344 * Returns: %TRUE if successful. If an error
5345 * has occurred, this function will return %FALSE and set @error
5346 * appropriately if present.
5349 g_file_replace_contents (GFile *file,
5350 const char *contents,
5353 gboolean make_backup,
5354 GFileCreateFlags flags,
5356 GCancellable *cancellable,
5359 GFileOutputStream *out;
5360 gsize pos, remainder;
5363 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5364 g_return_val_if_fail (contents != NULL, FALSE);
5366 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5372 while (remainder > 0 &&
5373 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5375 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5383 if (remainder > 0 && res < 0)
5385 /* Ignore errors on close */
5386 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5388 /* error is set already */
5392 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5396 *new_etag = g_file_output_stream_get_etag (out);
5404 GCancellable *cancellable;
5405 GAsyncReadyCallback callback;
5407 const char *content;
5411 } ReplaceContentsData;
5414 replace_contents_data_free (ReplaceContentsData *data)
5417 g_error_free (data->error);
5418 if (data->cancellable)
5419 g_object_unref (data->cancellable);
5420 g_object_unref (data->file);
5421 g_free (data->etag);
5426 replace_contents_close_callback (GObject *obj,
5427 GAsyncResult *close_res,
5430 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5431 ReplaceContentsData *data = user_data;
5432 GSimpleAsyncResult *res;
5434 /* Ignore errors here, we're only reading anyway */
5435 g_output_stream_close_finish (stream, close_res, NULL);
5436 g_object_unref (stream);
5438 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5440 res = g_simple_async_result_new (G_OBJECT (data->file),
5443 g_file_replace_contents_async);
5444 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5445 g_simple_async_result_complete (res);
5446 g_object_unref (res);
5450 replace_contents_write_callback (GObject *obj,
5451 GAsyncResult *read_res,
5454 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5455 ReplaceContentsData *data = user_data;
5456 GError *error = NULL;
5459 write_size = g_output_stream_write_finish (stream, read_res, &error);
5461 if (write_size <= 0)
5463 /* Error or EOF, close the file */
5465 data->error = error;
5466 g_output_stream_close_async (stream, 0,
5468 replace_contents_close_callback, data);
5470 else if (write_size > 0)
5472 data->pos += write_size;
5474 if (data->pos >= data->length)
5475 g_output_stream_close_async (stream, 0,
5477 replace_contents_close_callback, data);
5479 g_output_stream_write_async (stream,
5480 data->content + data->pos,
5481 data->length - data->pos,
5484 replace_contents_write_callback,
5490 replace_contents_open_callback (GObject *obj,
5491 GAsyncResult *open_res,
5494 GFile *file = G_FILE (obj);
5495 GFileOutputStream *stream;
5496 ReplaceContentsData *data = user_data;
5497 GError *error = NULL;
5498 GSimpleAsyncResult *res;
5500 stream = g_file_replace_finish (file, open_res, &error);
5504 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5505 data->content + data->pos,
5506 data->length - data->pos,
5509 replace_contents_write_callback,
5515 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5519 g_simple_async_result_complete (res);
5520 g_error_free (error);
5521 replace_contents_data_free (data);
5522 g_object_unref (res);
5527 * g_file_replace_contents_async:
5528 * @file: input #GFile.
5529 * @contents: string of contents to replace the file with.
5530 * @length: the length of @contents in bytes.
5531 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5532 * @make_backup: %TRUE if a backup should be created.
5533 * @flags: a set of #GFileCreateFlags.
5534 * @cancellable: optional #GCancellable object, %NULL to ignore.
5535 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5536 * @user_data: the data to pass to callback function
5538 * Starts an asynchronous replacement of @file with the given
5539 * @contents of @length bytes. @etag will replace the document's
5540 * current entity tag.
5542 * When this operation has completed, @callback will be called with
5543 * @user_user data, and the operation can be finalized with
5544 * g_file_replace_contents_finish().
5546 * If @cancellable is not %NULL, then the operation can be cancelled by
5547 * triggering the cancellable object from another thread. If the operation
5548 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5550 * If @make_backup is %TRUE, this function will attempt to
5551 * make a backup of @file.
5554 g_file_replace_contents_async (GFile *file,
5555 const char *contents,
5558 gboolean make_backup,
5559 GFileCreateFlags flags,
5560 GCancellable *cancellable,
5561 GAsyncReadyCallback callback,
5564 ReplaceContentsData *data;
5566 g_return_if_fail (G_IS_FILE (file));
5567 g_return_if_fail (contents != NULL);
5569 data = g_new0 (ReplaceContentsData, 1);
5572 data->cancellable = g_object_ref (cancellable);
5573 data->callback = callback;
5574 data->user_data = user_data;
5575 data->content = contents;
5576 data->length = length;
5578 data->file = g_object_ref (file);
5580 g_file_replace_async (file,
5586 replace_contents_open_callback,
5591 * g_file_replace_contents_finish:
5592 * @file: input #GFile.
5593 * @res: a #GAsyncResult.
5594 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5595 * for the document. This should be freed with g_free() when it is no
5597 * @error: a #GError, or %NULL
5599 * Finishes an asynchronous replace of the given @file. See
5600 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5601 * tag for the document, if present.
5603 * Returns: %TRUE on success, %FALSE on failure.
5606 g_file_replace_contents_finish (GFile *file,
5611 GSimpleAsyncResult *simple;
5612 ReplaceContentsData *data;
5614 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5615 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5617 simple = G_SIMPLE_ASYNC_RESULT (res);
5619 if (g_simple_async_result_propagate_error (simple, error))
5622 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5624 data = g_simple_async_result_get_op_res_gpointer (simple);
5628 g_propagate_error (error, data->error);
5636 *new_etag = data->etag;
5637 data->etag = NULL; /* Take ownership */
5643 #define __G_FILE_C__
5644 #include "gioaliasdef.c"