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);
674 * @file: input #GFile.
675 * @prefix: input #GFile.
677 * Checks whether @file has the prefix specified by @prefix. In other word, if the
678 * names of inital elements of @file<!-- -->s pathname match @prefix.
680 * This call does no i/o, as it works purely on names. As such it can sometimes
681 * return %FALSE even if @file is inside a @prefix (from a filesystem point of view),
682 * because the prefix of @file is an alias of @prefix.
684 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix. %FALSE otherwise.
687 g_file_has_prefix (GFile *file,
692 g_return_val_if_fail (G_IS_FILE (file), FALSE);
693 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
695 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
698 iface = G_FILE_GET_IFACE (file);
700 /* The vtable function differs in arg order since we're
701 using the old contains_file call */
702 return (* iface->prefix_matches) (prefix, file);
706 * g_file_get_relative_path:
707 * @parent: input #GFile.
708 * @descendant: input #GFile.
710 * Gets the path for @descendant relative to @parent.
712 * This call does no blocking i/o.
714 * Returns: string with the relative path from @descendant
715 * to @parent, or %NULL if @descendant doesn't have @parent as prefix. The returned string should be freed with
716 * g_free() when no longer needed.
719 g_file_get_relative_path (GFile *parent,
724 g_return_val_if_fail (G_IS_FILE (parent), NULL);
725 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
727 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
730 iface = G_FILE_GET_IFACE (parent);
732 return (* iface->get_relative_path) (parent, descendant);
736 * g_file_resolve_relative_path:
737 * @file: input #GFile.
738 * @relative_path: a given relative path string.
740 * Resolves a relative path for @file to an absolute path.
742 * This call does no blocking i/o.
744 * Returns: #GFile to the resolved path. %NULL if @relative_path
745 * is %NULL or if @file is invalid.
748 g_file_resolve_relative_path (GFile *file,
749 const char *relative_path)
753 g_return_val_if_fail (G_IS_FILE (file), NULL);
754 g_return_val_if_fail (relative_path != NULL, NULL);
756 iface = G_FILE_GET_IFACE (file);
758 return (* iface->resolve_relative_path) (file, relative_path);
762 * g_file_enumerate_children:
763 * @file: input #GFile.
764 * @attributes: an attribute query string.
765 * @flags: a set of #GFileQueryInfoFlags.
766 * @cancellable: optional #GCancellable object, %NULL to ignore.
767 * @error: #GError for error reporting.
769 * Gets the requested information about the files in a directory. The result
770 * is a #GFileEnumerator object that will give out #GFileInfo objects for
771 * all the files in the directory.
773 * The @attribute value is a string that specifies the file attributes that
774 * should be gathered. It is not an error if it's not possible to read a particular
775 * requested attribute from a file - it just won't be set. @attribute should
776 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
777 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
778 * namespace. An example attribute query be "standard::*,owner::user".
779 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
781 * If @cancellable is not %NULL, then the operation can be cancelled by
782 * triggering the cancellable object from another thread. If the operation
783 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
785 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
786 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
787 * Other errors are possible too.
789 * Returns: A #GFileEnumerator if successful, %NULL on error.
792 g_file_enumerate_children (GFile *file,
793 const char *attributes,
794 GFileQueryInfoFlags flags,
795 GCancellable *cancellable,
801 g_return_val_if_fail (G_IS_FILE (file), NULL);
803 if (g_cancellable_set_error_if_cancelled (cancellable, error))
806 iface = G_FILE_GET_IFACE (file);
808 if (iface->enumerate_children == NULL)
810 g_set_error (error, G_IO_ERROR,
811 G_IO_ERROR_NOT_SUPPORTED,
812 _("Operation not supported"));
816 return (* iface->enumerate_children) (file, attributes, flags,
821 * g_file_enumerate_children_async:
822 * @file: input #GFile.
823 * @attributes: an attribute query string.
824 * @flags: a set of #GFileQueryInfoFlags.
825 * @io_priority: the <link linkend="io-priority">I/O priority</link>
827 * @cancellable: optional #GCancellable object, %NULL to ignore.
828 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
829 * @user_data: the data to pass to callback function
831 * Asynchronously gets the requested information about the files in a directory. The result
832 * is a #GFileEnumerator object that will give out #GFileInfo objects for
833 * all the files in the directory.
835 * For more details, see g_file_enumerate_children() which is
836 * the synchronous version of this call.
838 * When the operation is finished, @callback will be called. You can then call
839 * g_file_enumerate_children_finish() to get the result of the operation.
842 g_file_enumerate_children_async (GFile *file,
843 const char *attributes,
844 GFileQueryInfoFlags flags,
846 GCancellable *cancellable,
847 GAsyncReadyCallback callback,
852 g_return_if_fail (G_IS_FILE (file));
854 iface = G_FILE_GET_IFACE (file);
855 (* iface->enumerate_children_async) (file,
865 * g_file_enumerate_children_finish:
866 * @file: input #GFile.
867 * @res: a #GAsyncResult.
870 * Finishes an async enumerate children operation.
871 * See g_file_enumerate_children_async().
873 * Returns: a #GFileEnumerator or %NULL if an error occurred.
876 g_file_enumerate_children_finish (GFile *file,
882 g_return_val_if_fail (G_IS_FILE (file), NULL);
883 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
885 if (G_IS_SIMPLE_ASYNC_RESULT (res))
887 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
888 if (g_simple_async_result_propagate_error (simple, error))
892 iface = G_FILE_GET_IFACE (file);
893 return (* iface->enumerate_children_finish) (file, res, error);
897 * g_file_query_exists:
898 * @file: input #GFile.
899 * @cancellable: optional #GCancellable object, %NULL to ignore.
901 * Utility function to check if a particular file exists. This is
902 * implemented using g_file_query_info() and as such does blocking I/O.
904 * Note that in many cases it is racy to first check for file existance
905 * and then execute something based on the outcome of that, because the
906 * file might have been created or removed inbetween the operations. The
907 * general approach to handling that is to not check, but just do the
908 * operation and handle the errors as they come.
910 * As an example of race-free checking, take the case of reading a file, and
911 * if it doesn't exist, creating it. There are two racy versions: read it, and
912 * on error create it; and: check if it exists, if not create it. These
913 * can both result in two processes creating the file (with perhaps a partially
914 * written file as the result). The correct approach is to always try to create
915 * the file with g_file_create() which will either atomically create the file
916 * or fail with a G_IO_ERROR_EXISTS error.
918 * However, in many cases an existance check is useful in a user
919 * interface, for instance to make a menu item sensitive/insensitive, so that
920 * you don't have to fool users that something is possible and then just show
921 * and error dialog. If you do this, you should make sure to also handle the
922 * errors that can happen due to races when you execute the operation.
924 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
927 g_file_query_exists (GFile *file,
928 GCancellable *cancellable)
932 g_return_val_if_fail (G_IS_FILE(file), FALSE);
934 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
935 G_FILE_QUERY_INFO_NONE,
939 g_object_unref (info);
948 * @file: input #GFile.
949 * @attributes: an attribute query string.
950 * @flags: a set of #GFileQueryInfoFlags.
951 * @cancellable: optional #GCancellable object, %NULL to ignore.
954 * Gets the requested information about specified @file. The result
955 * is a #GFileInfo object that contains key-value attributes (such as
956 * the type or size of the file).
958 * The @attribute value is a string that specifies the file attributes that
959 * should be gathered. It is not an error if it's not possible to read a particular
960 * requested attribute from a file - it just won't be set. @attribute should
961 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
962 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
963 * namespace. An example attribute query be "standard::*,owner::user".
964 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
966 * If @cancellable is not %NULL, then the operation can be cancelled by
967 * triggering the cancellable object from another thread. If the operation
968 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
970 * For symlinks, normally the information about the target of the
971 * symlink is returned, rather than information about the symlink itself.
972 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
973 * information about the symlink itself will be returned. Also, for symlinks
974 * that point to non-existing files the information about the symlink itself
977 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
978 * Other errors are possible too, and depend on what kind of filesystem the file is on.
980 * Returns: a #GFileInfo for the given @file, or %NULL on error.
983 g_file_query_info (GFile *file,
984 const char *attributes,
985 GFileQueryInfoFlags flags,
986 GCancellable *cancellable,
991 g_return_val_if_fail (G_IS_FILE (file), NULL);
993 if (g_cancellable_set_error_if_cancelled (cancellable, error))
996 iface = G_FILE_GET_IFACE (file);
998 if (iface->query_info == NULL)
1000 g_set_error (error, G_IO_ERROR,
1001 G_IO_ERROR_NOT_SUPPORTED,
1002 _("Operation not supported"));
1006 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1010 * g_file_query_info_async:
1011 * @file: input #GFile.
1012 * @attributes: an attribute query string.
1013 * @flags: a set of #GFileQueryInfoFlags.
1014 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1016 * @cancellable: optional #GCancellable object, %NULL to ignore.
1017 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1018 * @user_data: the data to pass to callback function
1020 * Asynchronously gets the requested information about specified @file. The result
1021 * is a #GFileInfo object that contains key-value attributes (such as type or size
1024 * For more details, see g_file_query_info() which is
1025 * the synchronous version of this call.
1027 * When the operation is finished, @callback will be called. You can then call
1028 * g_file_query_info_finish() to get the result of the operation.
1031 g_file_query_info_async (GFile *file,
1032 const char *attributes,
1033 GFileQueryInfoFlags flags,
1035 GCancellable *cancellable,
1036 GAsyncReadyCallback callback,
1041 g_return_if_fail (G_IS_FILE (file));
1043 iface = G_FILE_GET_IFACE (file);
1044 (* iface->query_info_async) (file,
1054 * g_file_query_info_finish:
1055 * @file: input #GFile.
1056 * @res: a #GAsyncResult.
1057 * @error: a #GError.
1059 * Finishes an asynchronous file info query.
1060 * See g_file_query_info_async().
1062 * Returns: #GFileInfo for given @file or %NULL on error.
1065 g_file_query_info_finish (GFile *file,
1071 g_return_val_if_fail (G_IS_FILE (file), NULL);
1072 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1074 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1076 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1077 if (g_simple_async_result_propagate_error (simple, error))
1081 iface = G_FILE_GET_IFACE (file);
1082 return (* iface->query_info_finish) (file, res, error);
1086 * g_file_query_filesystem_info:
1087 * @file: input #GFile.
1088 * @attributes: an attribute query string.
1089 * @cancellable: optional #GCancellable object, %NULL to ignore.
1090 * @error: a #GError.
1092 * Similar to g_file_query_info(), but obtains information
1093 * about the filesystem the @file is on, rather than the file itself.
1094 * For instance the amount of space available and the type of
1097 * The @attribute value is a string that specifies the file attributes that
1098 * should be gathered. It is not an error if it's not possible to read a particular
1099 * requested attribute from a file - it just won't be set. @attribute should
1100 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1101 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1102 * namespace. The standard namespace for filesystem attributes is "fs".
1103 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1104 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1105 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1107 * If @cancellable is not %NULL, then the operation can be cancelled by
1108 * triggering the cancellable object from another thread. If the operation
1109 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1111 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1112 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1114 * Returns: a #GFileInfo or %NULL if there was an error.
1117 g_file_query_filesystem_info (GFile *file,
1118 const char *attributes,
1119 GCancellable *cancellable,
1124 g_return_val_if_fail (G_IS_FILE (file), NULL);
1126 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1129 iface = G_FILE_GET_IFACE (file);
1131 if (iface->query_filesystem_info == NULL)
1133 g_set_error (error, G_IO_ERROR,
1134 G_IO_ERROR_NOT_SUPPORTED,
1135 _("Operation not supported"));
1139 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1143 * g_file_query_filesystem_info_async:
1144 * @file: input #GFile.
1145 * @attributes: an attribute query string.
1146 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1148 * @cancellable: optional #GCancellable object, %NULL to ignore.
1149 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1150 * @user_data: the data to pass to callback function
1152 * Asynchronously gets the requested information about the filesystem
1153 * that the specified @file is on. The result is a #GFileInfo object
1154 * that contains key-value attributes (such as type or size for the
1157 * For more details, see g_file_query_filesystem_info() which is the
1158 * synchronous version of this call.
1160 * When the operation is finished, @callback will be called. You can
1161 * then call g_file_query_info_finish() to get the result of the
1165 g_file_query_filesystem_info_async (GFile *file,
1166 const char *attributes,
1168 GCancellable *cancellable,
1169 GAsyncReadyCallback callback,
1174 g_return_if_fail (G_IS_FILE (file));
1176 iface = G_FILE_GET_IFACE (file);
1177 (* iface->query_filesystem_info_async) (file,
1186 * g_file_query_filesystem_info_finish:
1187 * @file: input #GFile.
1188 * @res: a #GAsyncResult.
1189 * @error: a #GError.
1191 * Finishes an asynchronous filesystem info query. See
1192 * g_file_query_filesystem_info_async().
1194 * Returns: #GFileInfo for given @file or %NULL on error.
1197 g_file_query_filesystem_info_finish (GFile *file,
1203 g_return_val_if_fail (G_IS_FILE (file), NULL);
1204 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1206 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1208 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1209 if (g_simple_async_result_propagate_error (simple, error))
1213 iface = G_FILE_GET_IFACE (file);
1214 return (* iface->query_filesystem_info_finish) (file, res, error);
1218 * g_file_find_enclosing_mount:
1219 * @file: input #GFile.
1220 * @cancellable: optional #GCancellable object, %NULL to ignore.
1221 * @error: a #GError.
1223 * Gets a #GMount for the #GFile.
1225 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1226 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1229 * If @cancellable is not %NULL, then the operation can be cancelled by
1230 * triggering the cancellable object from another thread. If the operation
1231 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1233 * Returns: a #GMount where the @file is located or %NULL on error.
1236 g_file_find_enclosing_mount (GFile *file,
1237 GCancellable *cancellable,
1242 g_return_val_if_fail (G_IS_FILE (file), NULL);
1244 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1247 iface = G_FILE_GET_IFACE (file);
1248 if (iface->find_enclosing_mount == NULL)
1251 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1252 /* Translators: This is an error message when trying to find the
1253 * enclosing (user visible) mount of a file, but none exists. */
1254 _("Containing mount does not exist"));
1258 return (* iface->find_enclosing_mount) (file, cancellable, error);
1261 * g_file_find_enclosing_mount_async:
1263 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1265 * @cancellable: optional #GCancellable object, %NULL to ignore.
1266 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1267 * @user_data: the data to pass to callback function
1269 * Asynchronously gets the mount for the file.
1271 * For more details, see g_file_find_enclosing_mount() which is
1272 * the synchronous version of this call.
1274 * When the operation is finished, @callback will be called. You can then call
1275 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1278 g_file_find_enclosing_mount_async (GFile *file,
1280 GCancellable *cancellable,
1281 GAsyncReadyCallback callback,
1286 g_return_if_fail (G_IS_FILE (file));
1288 iface = G_FILE_GET_IFACE (file);
1289 (* iface->find_enclosing_mount_async) (file,
1297 * g_file_find_enclosing_mount_finish:
1299 * @res: a #GAsyncResult
1302 * Finishes an asynchronous find mount request.
1303 * See g_file_find_enclosing_mount_async().
1305 * Returns: #GMount for given @file or %NULL on error.
1308 g_file_find_enclosing_mount_finish (GFile *file,
1314 g_return_val_if_fail (G_IS_FILE (file), NULL);
1315 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1317 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1319 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1320 if (g_simple_async_result_propagate_error (simple, error))
1324 iface = G_FILE_GET_IFACE (file);
1325 return (* iface->find_enclosing_mount_finish) (file, res, error);
1331 * @file: #GFile to read.
1332 * @cancellable: a #GCancellable
1333 * @error: a #GError, or %NULL
1335 * Opens a file for reading. The result is a #GFileInputStream that
1336 * can be used to read the contents of the file.
1338 * If @cancellable is not %NULL, then the operation can be cancelled by
1339 * triggering the cancellable object from another thread. If the operation
1340 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1342 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1343 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1344 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1346 * Returns: #GFileInputStream or %NULL on error.
1349 g_file_read (GFile *file,
1350 GCancellable *cancellable,
1355 g_return_val_if_fail (G_IS_FILE (file), NULL);
1357 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1360 iface = G_FILE_GET_IFACE (file);
1362 if (iface->read_fn == NULL)
1364 g_set_error (error, G_IO_ERROR,
1365 G_IO_ERROR_NOT_SUPPORTED,
1366 _("Operation not supported"));
1370 return (* iface->read_fn) (file, cancellable, error);
1375 * @file: input #GFile.
1376 * @flags: a set of #GFileCreateFlags.
1377 * @cancellable: optional #GCancellable object, %NULL to ignore.
1378 * @error: a #GError, or %NULL
1380 * Gets an output stream for appending data to the file. If
1381 * the file doesn't already exist it is created.
1383 * By default files created are generally readable by everyone,
1384 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1385 * will be made readable only to the current user, to the level that
1386 * is supported on the target filesystem.
1388 * If @cancellable is not %NULL, then the operation can be cancelled by
1389 * triggering the cancellable object from another thread. If the operation
1390 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1392 * Some file systems don't allow all file names, and may
1393 * return an G_IO_ERROR_INVALID_FILENAME error.
1394 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1395 * returned. Other errors are possible too, and depend on what kind of
1396 * filesystem the file is on.
1398 * Returns: a #GFileOutputStream.
1401 g_file_append_to (GFile *file,
1402 GFileCreateFlags flags,
1403 GCancellable *cancellable,
1408 g_return_val_if_fail (G_IS_FILE (file), NULL);
1410 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1413 iface = G_FILE_GET_IFACE (file);
1415 if (iface->append_to == NULL)
1417 g_set_error (error, G_IO_ERROR,
1418 G_IO_ERROR_NOT_SUPPORTED,
1419 _("Operation not supported"));
1423 return (* iface->append_to) (file, flags, cancellable, error);
1428 * @file: input #GFile.
1429 * @flags: a set of #GFileCreateFlags.
1430 * @cancellable: optional #GCancellable object, %NULL to ignore.
1431 * @error: a #GError, or %NULL
1433 * Creates a new file and returns an output stream for writing to it.
1434 * The file must not already exists.
1436 * By default files created are generally readable by everyone,
1437 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1438 * will be made readable only to the current user, to the level that
1439 * is supported on the target filesystem.
1441 * If @cancellable is not %NULL, then the operation can be cancelled by
1442 * triggering the cancellable object from another thread. If the operation
1443 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1445 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1446 * error will be returned.
1447 * Some file systems don't allow all file names, and may
1448 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1449 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1450 * Other errors are possible too, and depend on what kind of
1451 * filesystem the file is on.
1453 * Returns: a #GFileOutputStream for the newly created file, or
1457 g_file_create (GFile *file,
1458 GFileCreateFlags flags,
1459 GCancellable *cancellable,
1464 g_return_val_if_fail (G_IS_FILE (file), NULL);
1466 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1469 iface = G_FILE_GET_IFACE (file);
1471 if (iface->create == NULL)
1473 g_set_error (error, G_IO_ERROR,
1474 G_IO_ERROR_NOT_SUPPORTED,
1475 _("Operation not supported"));
1479 return (* iface->create) (file, flags, cancellable, error);
1484 * @file: input #GFile.
1485 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1486 * current #GFile, or #NULL to ignore.
1487 * @make_backup: %TRUE if a backup should be created.
1488 * @flags: a set of #GFileCreateFlags.
1489 * @cancellable: optional #GCancellable object, %NULL to ignore.
1490 * @error: a #GError, or %NULL
1492 * Returns an output stream for overwriting the file, possibly
1493 * creating a backup copy of the file first.
1495 * This will try to replace the file in the safest way possible so
1496 * that any errors during the writing will not affect an already
1497 * existing copy of the file. For instance, for local files it
1498 * may write to a temporary file and then atomically rename over
1499 * the destination when the stream is closed.
1501 * By default files created are generally readable by everyone,
1502 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1503 * will be made readable only to the current user, to the level that
1504 * is supported on the target filesystem.
1506 * If @cancellable is not %NULL, then the operation can be cancelled by
1507 * triggering the cancellable object from another thread. If the operation
1508 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1510 * If you pass in a non-#NULL @etag value, then this value is
1511 * compared to the current entity tag of the file, and if they differ
1512 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1513 * that the file has been changed since you last read it. You can get
1514 * the new etag from g_file_output_stream_get_etag() after you've
1515 * finished writing and closed the #GFileOutputStream. When you load
1516 * a new file you can use g_file_input_stream_query_info() to get
1517 * the etag of the file.
1519 * If @make_backup is %TRUE, this function will attempt to make a backup
1520 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1521 * error will be returned. If you want to replace anyway, try again with
1522 * @make_backup set to %FALSE.
1524 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1525 * and if the file is some other form of non-regular file then a
1526 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1527 * Some file systems don't allow all file names, and may
1528 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1529 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1530 * Other errors are possible too, and depend on what kind of
1531 * filesystem the file is on.
1533 * Returns: a #GFileOutputStream or %NULL on error.
1536 g_file_replace (GFile *file,
1538 gboolean make_backup,
1539 GFileCreateFlags flags,
1540 GCancellable *cancellable,
1545 g_return_val_if_fail (G_IS_FILE (file), NULL);
1547 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1550 iface = G_FILE_GET_IFACE (file);
1552 if (iface->replace == NULL)
1554 g_set_error (error, G_IO_ERROR,
1555 G_IO_ERROR_NOT_SUPPORTED,
1556 _("Operation not supported"));
1561 /* Handle empty tag string as NULL in consistent way. */
1562 if (etag && *etag == 0)
1565 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1569 * g_file_read_async:
1570 * @file: input #GFile.
1571 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1573 * @cancellable: optional #GCancellable object, %NULL to ignore.
1574 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1575 * @user_data: the data to pass to callback function
1577 * Asynchronously opens @file for reading.
1579 * For more details, see g_file_read() which is
1580 * the synchronous version of this call.
1582 * When the operation is finished, @callback will be called. You can then call
1583 * g_file_read_finish() to get the result of the operation.
1586 g_file_read_async (GFile *file,
1588 GCancellable *cancellable,
1589 GAsyncReadyCallback callback,
1594 g_return_if_fail (G_IS_FILE (file));
1596 iface = G_FILE_GET_IFACE (file);
1597 (* iface->read_async) (file,
1605 * g_file_read_finish:
1606 * @file: input #GFile.
1607 * @res: a #GAsyncResult.
1608 * @error: a #GError, or %NULL
1610 * Finishes an asynchronous file read operation started with
1611 * g_file_read_async().
1613 * Returns: a #GFileInputStream or %NULL on error.
1616 g_file_read_finish (GFile *file,
1622 g_return_val_if_fail (G_IS_FILE (file), NULL);
1623 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1625 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1627 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1628 if (g_simple_async_result_propagate_error (simple, error))
1632 iface = G_FILE_GET_IFACE (file);
1633 return (* iface->read_finish) (file, res, error);
1637 * g_file_append_to_async:
1638 * @file: input #GFile.
1639 * @flags: a set of #GFileCreateFlags.
1640 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1642 * @cancellable: optional #GCancellable object, %NULL to ignore.
1643 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1644 * @user_data: the data to pass to callback function
1646 * Asynchronously opens @file for appending.
1648 * For more details, see g_file_append_to() which is
1649 * the synchronous version of this call.
1651 * When the operation is finished, @callback will be called. You can then call
1652 * g_file_append_to_finish() to get the result of the operation.
1655 g_file_append_to_async (GFile *file,
1656 GFileCreateFlags flags,
1658 GCancellable *cancellable,
1659 GAsyncReadyCallback callback,
1664 g_return_if_fail (G_IS_FILE (file));
1666 iface = G_FILE_GET_IFACE (file);
1667 (* iface->append_to_async) (file,
1676 * g_file_append_to_finish:
1677 * @file: input #GFile.
1678 * @res: #GAsyncResult
1679 * @error: a #GError, or %NULL
1681 * Finishes an asynchronous file append operation started with
1682 * g_file_append_to_async().
1684 * Returns: a valid #GFileOutputStream or %NULL on error.
1687 g_file_append_to_finish (GFile *file,
1693 g_return_val_if_fail (G_IS_FILE (file), NULL);
1694 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1696 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1698 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1699 if (g_simple_async_result_propagate_error (simple, error))
1703 iface = G_FILE_GET_IFACE (file);
1704 return (* iface->append_to_finish) (file, res, error);
1708 * g_file_create_async:
1709 * @file: input #GFile.
1710 * @flags: a set of #GFileCreateFlags.
1711 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1713 * @cancellable: optional #GCancellable object, %NULL to ignore.
1714 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1715 * @user_data: the data to pass to callback function
1717 * Asynchronously creates a new file and returns an output stream for writing to it.
1718 * The file must not already exists.
1720 * For more details, see g_file_create() which is
1721 * the synchronous version of this call.
1723 * When the operation is finished, @callback will be called. You can then call
1724 * g_file_create_finish() to get the result of the operation.
1727 g_file_create_async (GFile *file,
1728 GFileCreateFlags flags,
1730 GCancellable *cancellable,
1731 GAsyncReadyCallback callback,
1736 g_return_if_fail (G_IS_FILE (file));
1738 iface = G_FILE_GET_IFACE (file);
1739 (* iface->create_async) (file,
1748 * g_file_create_finish:
1749 * @file: input #GFile.
1750 * @res: a #GAsyncResult.
1751 * @error: a #GError, or %NULL
1753 * Finishes an asynchronous file create operation started with
1754 * g_file_create_async().
1756 * Returns: a #GFileOutputStream or %NULL on error.
1759 g_file_create_finish (GFile *file,
1765 g_return_val_if_fail (G_IS_FILE (file), NULL);
1766 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1768 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1770 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1771 if (g_simple_async_result_propagate_error (simple, error))
1775 iface = G_FILE_GET_IFACE (file);
1776 return (* iface->create_finish) (file, res, error);
1780 * g_file_replace_async:
1781 * @file: input #GFile.
1782 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1783 * current #GFile, or NULL to ignore.
1784 * @make_backup: %TRUE if a backup should be created.
1785 * @flags: a set of #GFileCreateFlags.
1786 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1788 * @cancellable: optional #GCancellable object, %NULL to ignore.
1789 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1790 * @user_data: the data to pass to callback function
1792 * Asynchronously overwrites the file, replacing the contents, possibly
1793 * creating a backup copy of the file first.
1795 * For more details, see g_file_replace() which is
1796 * the synchronous version of this call.
1798 * When the operation is finished, @callback will be called. You can then call
1799 * g_file_replace_finish() to get the result of the operation.
1802 g_file_replace_async (GFile *file,
1804 gboolean make_backup,
1805 GFileCreateFlags flags,
1807 GCancellable *cancellable,
1808 GAsyncReadyCallback callback,
1813 g_return_if_fail (G_IS_FILE (file));
1815 iface = G_FILE_GET_IFACE (file);
1816 (* iface->replace_async) (file,
1827 * g_file_replace_finish:
1828 * @file: input #GFile.
1829 * @res: a #GAsyncResult.
1830 * @error: a #GError, or %NULL
1832 * Finishes an asynchronous file replace operation started with
1833 * g_file_replace_async().
1835 * Returns: a #GFileOutputStream, or %NULL on error.
1838 g_file_replace_finish (GFile *file,
1844 g_return_val_if_fail (G_IS_FILE (file), NULL);
1845 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1847 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1849 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1850 if (g_simple_async_result_propagate_error (simple, error))
1854 iface = G_FILE_GET_IFACE (file);
1855 return (* iface->replace_finish) (file, res, error);
1859 copy_symlink (GFile *destination,
1860 GFileCopyFlags flags,
1861 GCancellable *cancellable,
1866 gboolean tried_delete;
1868 GFileType file_type;
1870 tried_delete = FALSE;
1874 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1876 /* Maybe it already existed, and we want to overwrite? */
1877 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1878 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1880 g_error_free (my_error);
1883 /* Don't overwrite if the destination is a directory */
1884 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1885 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1886 cancellable, &my_error);
1889 file_type = g_file_info_get_file_type (info);
1890 g_object_unref (info);
1892 if (file_type == G_FILE_TYPE_DIRECTORY)
1894 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1895 _("Can't copy over directory"));
1900 if (!g_file_delete (destination, cancellable, error))
1903 tried_delete = TRUE;
1907 g_propagate_error (error, my_error);
1914 static GInputStream *
1915 open_source_for_copy (GFile *source,
1917 GFileCopyFlags flags,
1918 GCancellable *cancellable,
1924 GFileType file_type;
1927 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1931 /* There was an error opening the source, try to set a good error for it: */
1933 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1935 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1936 * as that is less useful to the app. Better check for errors on the
1939 g_error_free (my_error);
1942 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1943 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1944 cancellable, &my_error);
1947 file_type = g_file_info_get_file_type (info);
1948 g_object_unref (info);
1950 if (flags & G_FILE_COPY_OVERWRITE)
1952 if (file_type == G_FILE_TYPE_DIRECTORY)
1954 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1955 _("Can't copy directory over directory"));
1958 /* continue to would_recurse error */
1962 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1963 _("Target file exists"));
1969 /* Error getting info from target, return that error
1970 * (except for NOT_FOUND, which is no error here)
1972 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1974 g_propagate_error (error, my_error);
1977 g_error_free (my_error);
1980 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
1981 _("Can't recursively copy directory"));
1985 g_propagate_error (error, my_error);
1990 should_copy (GFileAttributeInfo *info,
1994 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
1995 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
1999 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2000 GFileAttributeInfoList *namespaces,
2008 s = g_string_new ("");
2012 for (i = 0; i < attributes->n_infos; i++)
2014 if (should_copy (&attributes->infos[i], as_move))
2019 g_string_append_c (s, ',');
2021 g_string_append (s, attributes->infos[i].name);
2028 for (i = 0; i < namespaces->n_infos; i++)
2030 if (should_copy (&namespaces->infos[i], as_move))
2035 g_string_append_c (s, ',');
2037 g_string_append (s, namespaces->infos[i].name);
2038 g_string_append (s, ":*");
2043 return g_string_free (s, FALSE);
2047 * g_file_copy_attributes:
2048 * @source: a #GFile with attributes.
2049 * @destination: a #GFile to copy attributes to.
2050 * @flags: a set of #GFileCopyFlags.
2051 * @cancellable: optional #GCancellable object, %NULL to ignore.
2052 * @error: a #GError, %NULL to ignore.
2054 * Copies the file attributes from @source to @destination.
2056 * Normally only a subset of the file attributes are copied,
2057 * those that are copies in a normal file copy operation
2058 * (which for instance does not include e.g. mtime). However
2059 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2060 * all the metadata that is possible to copy is copied.
2062 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2065 g_file_copy_attributes (GFile *source,
2067 GFileCopyFlags flags,
2068 GCancellable *cancellable,
2071 GFileAttributeInfoList *attributes, *namespaces;
2072 char *attrs_to_read;
2076 gboolean source_nofollow_symlinks;
2078 as_move = flags & G_FILE_COPY_ALL_METADATA;
2079 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2081 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2082 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2083 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2085 if (attributes == NULL && namespaces == NULL)
2088 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2090 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2091 * we just don't copy it.
2093 info = g_file_query_info (source, attrs_to_read,
2094 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2098 g_free (attrs_to_read);
2103 res = g_file_set_attributes_from_info (destination,
2107 g_object_unref (info);
2110 g_file_attribute_info_list_unref (attributes);
2111 g_file_attribute_info_list_unref (namespaces);
2116 /* Closes the streams */
2118 copy_stream_with_progress (GInputStream *in,
2120 GCancellable *cancellable,
2121 GFileProgressCallback progress_callback,
2122 gpointer progress_callback_data,
2125 gssize n_read, n_written;
2126 goffset current_size;
2127 char buffer[8192], *p;
2133 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2134 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2138 total_size = g_file_info_get_size (info);
2139 g_object_unref (info);
2146 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2156 current_size += n_read;
2161 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2162 if (n_written == -1)
2169 n_read -= n_written;
2175 if (progress_callback)
2176 progress_callback (current_size, total_size, progress_callback_data);
2180 error = NULL; /* Ignore further errors */
2182 /* Make sure we send full copied size */
2183 if (progress_callback)
2184 progress_callback (current_size, total_size, progress_callback_data);
2187 /* Don't care about errors in source here */
2188 g_input_stream_close (in, cancellable, NULL);
2190 /* But write errors on close are bad! */
2191 if (!g_output_stream_close (out, cancellable, error))
2194 g_object_unref (in);
2195 g_object_unref (out);
2201 file_copy_fallback (GFile *source,
2203 GFileCopyFlags flags,
2204 GCancellable *cancellable,
2205 GFileProgressCallback progress_callback,
2206 gpointer progress_callback_data,
2214 /* Maybe copy the symlink? */
2215 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2217 info = g_file_query_info (source,
2218 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2219 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2225 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2226 (target = g_file_info_get_symlink_target (info)) != NULL)
2228 if (!copy_symlink (destination, flags, cancellable, target, error))
2230 g_object_unref (info);
2234 g_object_unref (info);
2238 g_object_unref (info);
2241 in = open_source_for_copy (source, destination, flags, cancellable, error);
2245 if (flags & G_FILE_COPY_OVERWRITE)
2247 out = (GOutputStream *)g_file_replace (destination,
2249 flags & G_FILE_COPY_BACKUP,
2250 cancellable, error);
2254 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2259 g_object_unref (in);
2263 if (!copy_stream_with_progress (in, out, cancellable,
2264 progress_callback, progress_callback_data,
2270 /* Ignore errors here. Failure to copy metadata is not a hard error */
2271 g_file_copy_attributes (source, destination,
2272 flags, cancellable, NULL);
2279 * @source: input #GFile.
2280 * @destination: destination #GFile
2281 * @flags: set of #GFileCopyFlags
2282 * @cancellable: optional #GCancellable object, %NULL to ignore.
2283 * @progress_callback: function to callback with progress information
2284 * @progress_callback_data: user data to pass to @progress_callback
2285 * @error: #GError to set on error, or %NULL
2287 * Copies the file @source to the location specified by @destination.
2288 * Can not handle recursive copies of directories.
2290 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2291 * existing @destination file is overwritten.
2293 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2294 * will be copied as symlinks, otherwise the target of the
2295 * @source symlink will be copied.
2297 * If @cancellable is not %NULL, then the operation can be cancelled by
2298 * triggering the cancellable object from another thread. If the operation
2299 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2301 * If @progress_callback is not %NULL, then the operation can be monitored by
2302 * setting this to a #GFileProgressCallback function. @progress_callback_data
2303 * will be passed to this function. It is guaranteed that this callback will
2304 * be called after all data has been transferred with the total number of bytes
2305 * copied during the operation.
2307 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2308 * error is returned, independent on the status of the @destination.
2310 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2311 * error G_IO_ERROR_EXISTS is returned.
2313 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2314 * error is returned. If trying to overwrite a directory with a directory the
2315 * G_IO_ERROR_WOULD_MERGE error is returned.
2317 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2318 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2321 * If you are interested in copying the #GFile object itself (not the on-disk
2322 * file), see g_file_dup().
2324 * Returns: %TRUE on success, %FALSE otherwise.
2327 g_file_copy (GFile *source,
2329 GFileCopyFlags flags,
2330 GCancellable *cancellable,
2331 GFileProgressCallback progress_callback,
2332 gpointer progress_callback_data,
2339 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2340 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2342 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2345 iface = G_FILE_GET_IFACE (destination);
2349 res = (* iface->copy) (source, destination,
2351 progress_callback, progress_callback_data,
2357 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2359 g_propagate_error (error, my_error);
2364 /* If the types are different, and the destination method failed
2365 also try the source method */
2366 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2368 iface = G_FILE_GET_IFACE (source);
2373 res = (* iface->copy) (source, destination,
2375 progress_callback, progress_callback_data,
2381 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2383 g_propagate_error (error, my_error);
2389 return file_copy_fallback (source, destination, flags, cancellable,
2390 progress_callback, progress_callback_data,
2395 * g_file_copy_async:
2396 * @source: input #GFile.
2397 * @destination: destination #GFile
2398 * @flags: set of #GFileCopyFlags
2399 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2401 * @cancellable: optional #GCancellable object, %NULL to ignore.
2402 * @progress_callback: function to callback with progress information
2403 * @progress_callback_data: user data to pass to @progress_callback
2404 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2405 * @user_data: the data to pass to callback function
2407 * Copies the file @source to the location specified by @destination
2408 * asynchronously. For details of the behaviour, see g_file_copy().
2410 * If @progress_callback is not %NULL, then that function that will be called
2411 * just like in g_file_copy(), however the callback will run in the main loop,
2412 * not in the thread that is doing the I/O operation.
2414 * When the operation is finished, @callback will be called. You can then call
2415 * g_file_copy_finish() to get the result of the operation.
2418 g_file_copy_async (GFile *source,
2420 GFileCopyFlags flags,
2422 GCancellable *cancellable,
2423 GFileProgressCallback progress_callback,
2424 gpointer progress_callback_data,
2425 GAsyncReadyCallback callback,
2430 g_return_if_fail (G_IS_FILE (source));
2431 g_return_if_fail (G_IS_FILE (destination));
2433 iface = G_FILE_GET_IFACE (source);
2434 (* iface->copy_async) (source,
2440 progress_callback_data,
2446 * g_file_copy_finish:
2447 * @file: input #GFile.
2448 * @res: a #GAsyncResult.
2449 * @error: a #GError, or %NULL
2451 * Finishes copying the file started with
2452 * g_file_copy_async().
2454 * Returns: a %TRUE on success, %FALSE on error.
2457 g_file_copy_finish (GFile *file,
2463 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2464 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2466 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2468 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2470 if (g_simple_async_result_propagate_error (simple, error))
2474 iface = G_FILE_GET_IFACE (file);
2475 return (* iface->copy_finish) (file, res, error);
2480 * @source: #GFile pointing to the source location.
2481 * @destination: #GFile pointing to the destination location.
2482 * @flags: set of #GFileCopyFlags.
2483 * @cancellable: optional #GCancellable object, %NULL to ignore.
2484 * @progress_callback: #GFileProgressCallback function for updates.
2485 * @progress_callback_data: gpointer to user data for the callback function.
2486 * @error: #GError for returning error conditions, or %NULL
2489 * Tries to move the file or directory @source to the location specified by @destination.
2490 * If native move operations are supported then this is used, otherwise a copy + delete
2491 * fallback is used. The native implementation may support moving directories (for instance
2492 * on moves inside the same filesystem), but the fallback code does not.
2494 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2495 * existing @destination file is overwritten.
2497 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2498 * will be copied as symlinks, otherwise the target of the
2499 * @source symlink will be copied.
2501 * If @cancellable is not %NULL, then the operation can be cancelled by
2502 * triggering the cancellable object from another thread. If the operation
2503 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2505 * If @progress_callback is not %NULL, then the operation can be monitored by
2506 * setting this to a #GFileProgressCallback function. @progress_callback_data
2507 * will be passed to this function. It is guaranteed that this callback will
2508 * be called after all data has been transferred with the total number of bytes
2509 * copied during the operation.
2511 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2512 * error is returned, independent on the status of the @destination.
2514 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2515 * error G_IO_ERROR_EXISTS is returned.
2517 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2518 * error is returned. If trying to overwrite a directory with a directory the
2519 * G_IO_ERROR_WOULD_MERGE error is returned.
2521 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2522 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2523 * may be returned (if the native move operation isn't available).
2525 * Returns: %TRUE on successful move, %FALSE otherwise.
2528 g_file_move (GFile *source,
2530 GFileCopyFlags flags,
2531 GCancellable *cancellable,
2532 GFileProgressCallback progress_callback,
2533 gpointer progress_callback_data,
2540 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2541 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2543 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2546 iface = G_FILE_GET_IFACE (destination);
2550 res = (* iface->move) (source, destination,
2552 progress_callback, progress_callback_data,
2558 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2560 g_propagate_error (error, my_error);
2565 /* If the types are different, and the destination method failed
2566 also try the source method */
2567 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2569 iface = G_FILE_GET_IFACE (source);
2574 res = (* iface->move) (source, destination,
2576 progress_callback, progress_callback_data,
2582 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2584 g_propagate_error (error, my_error);
2590 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2592 g_set_error (error, G_IO_ERROR,
2593 G_IO_ERROR_NOT_SUPPORTED,
2594 _("Operation not supported"));
2598 flags |= G_FILE_COPY_ALL_METADATA;
2599 if (!g_file_copy (source, destination, flags, cancellable,
2600 progress_callback, progress_callback_data,
2604 return g_file_delete (source, cancellable, error);
2608 * g_file_make_directory
2609 * @file: input #GFile.
2610 * @cancellable: optional #GCancellable object, %NULL to ignore.
2611 * @error: a #GError, or %NULL
2613 * Creates a directory.
2615 * If @cancellable is not %NULL, then the operation can be cancelled by
2616 * triggering the cancellable object from another thread. If the operation
2617 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2619 * Returns: %TRUE on successful creation, %FALSE otherwise.
2622 g_file_make_directory (GFile *file,
2623 GCancellable *cancellable,
2628 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2630 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2633 iface = G_FILE_GET_IFACE (file);
2635 if (iface->make_directory == NULL)
2637 g_set_error (error, G_IO_ERROR,
2638 G_IO_ERROR_NOT_SUPPORTED,
2639 _("Operation not supported"));
2643 return (* iface->make_directory) (file, cancellable, error);
2647 * g_file_make_symbolic_link:
2648 * @file: input #GFile.
2649 * @symlink_value: a string with the value of the new symlink.
2650 * @cancellable: optional #GCancellable object, %NULL to ignore.
2651 * @error: a #GError.
2653 * Creates a symbolic link.
2655 * If @cancellable is not %NULL, then the operation can be cancelled by
2656 * triggering the cancellable object from another thread. If the operation
2657 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2659 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2662 g_file_make_symbolic_link (GFile *file,
2663 const char *symlink_value,
2664 GCancellable *cancellable,
2669 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2670 g_return_val_if_fail (symlink_value != NULL, FALSE);
2672 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2675 if (*symlink_value == '\0')
2677 g_set_error (error, G_IO_ERROR,
2678 G_IO_ERROR_INVALID_ARGUMENT,
2679 _("Invalid symlink value given"));
2683 iface = G_FILE_GET_IFACE (file);
2685 if (iface->make_symbolic_link == NULL)
2687 g_set_error (error, G_IO_ERROR,
2688 G_IO_ERROR_NOT_SUPPORTED,
2689 _("Operation not supported"));
2693 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2698 * @file: input #GFile.
2699 * @cancellable: optional #GCancellable object, %NULL to ignore.
2700 * @error: a #GError, or %NULL
2702 * Deletes a file. If the @file is a directory, it will only be deleted if it
2705 * If @cancellable is not %NULL, then the operation can be cancelled by
2706 * triggering the cancellable object from another thread. If the operation
2707 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2709 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2712 g_file_delete (GFile *file,
2713 GCancellable *cancellable,
2718 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2720 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2723 iface = G_FILE_GET_IFACE (file);
2725 if (iface->delete_file == NULL)
2727 g_set_error (error, G_IO_ERROR,
2728 G_IO_ERROR_NOT_SUPPORTED,
2729 _("Operation not supported"));
2733 return (* iface->delete_file) (file, cancellable, error);
2738 * @file: #GFile to send to trash.
2739 * @cancellable: optional #GCancellable object, %NULL to ignore.
2740 * @error: a #GError, or %NULL
2742 * Sends @file to the "Trashcan", if possible. This is similar to
2743 * deleting it, but the user can recover it before emptying the trashcan.
2744 * Not all file systems support trashing, so this call can return the
2745 * %G_IO_ERROR_NOT_SUPPORTED error.
2748 * If @cancellable is not %NULL, then the operation can be cancelled by
2749 * triggering the cancellable object from another thread. If the operation
2750 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2752 * Returns: %TRUE on successful trash, %FALSE otherwise.
2755 g_file_trash (GFile *file,
2756 GCancellable *cancellable,
2761 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2763 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2766 iface = G_FILE_GET_IFACE (file);
2768 if (iface->trash == NULL)
2771 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2772 _("Trash not supported"));
2776 return (* iface->trash) (file, cancellable, error);
2780 * g_file_set_display_name:
2781 * @file: input #GFile.
2782 * @display_name: a string.
2783 * @cancellable: optional #GCancellable object, %NULL to ignore.
2784 * @error: a #GError, or %NULL
2786 * Renames @file to the specified display name.
2788 * The display name is converted from UTF8 to the correct encoding for the target
2789 * filesystem if possible and the @file is renamed to this.
2791 * If you want to implement a rename operation in the user interface the edit name
2792 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2793 * widget, and then the result after editing should be passed to g_file_set_display_name().
2795 * On success the resulting converted filename is returned.
2797 * If @cancellable is not %NULL, then the operation can be cancelled by
2798 * triggering the cancellable object from another thread. If the operation
2799 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2801 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2804 g_file_set_display_name (GFile *file,
2805 const char *display_name,
2806 GCancellable *cancellable,
2811 g_return_val_if_fail (G_IS_FILE (file), NULL);
2812 g_return_val_if_fail (display_name != NULL, NULL);
2814 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2818 G_IO_ERROR_INVALID_ARGUMENT,
2819 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2823 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2826 iface = G_FILE_GET_IFACE (file);
2828 return (* iface->set_display_name) (file, display_name, cancellable, error);
2832 * g_file_set_display_name_async:
2833 * @file: input #GFile.
2834 * @display_name: a string.
2835 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2837 * @cancellable: optional #GCancellable object, %NULL to ignore.
2838 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2839 * @user_data: the data to pass to callback function
2841 * Asynchronously sets the display name for a given #GFile.
2843 * For more details, see g_set_display_name() which is
2844 * the synchronous version of this call.
2846 * When the operation is finished, @callback will be called. You can then call
2847 * g_file_set_display_name_finish() to get the result of the operation.
2850 g_file_set_display_name_async (GFile *file,
2851 const char *display_name,
2853 GCancellable *cancellable,
2854 GAsyncReadyCallback callback,
2859 g_return_if_fail (G_IS_FILE (file));
2860 g_return_if_fail (display_name != NULL);
2862 iface = G_FILE_GET_IFACE (file);
2863 (* iface->set_display_name_async) (file,
2872 * g_file_set_display_name_finish:
2873 * @file: input #GFile.
2874 * @res: a #GAsyncResult.
2875 * @error: a #GError, or %NULL
2877 * Finishes setting a display name started with
2878 * g_file_set_display_name_async().
2880 * Returns: a #GFile or %NULL on error.
2883 g_file_set_display_name_finish (GFile *file,
2889 g_return_val_if_fail (G_IS_FILE (file), NULL);
2890 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2892 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2894 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2895 if (g_simple_async_result_propagate_error (simple, error))
2899 iface = G_FILE_GET_IFACE (file);
2900 return (* iface->set_display_name_finish) (file, res, error);
2904 * g_file_query_settable_attributes:
2905 * @file: input #GFile.
2906 * @cancellable: optional #GCancellable object, %NULL to ignore.
2907 * @error: a #GError, or %NULL
2909 * Obtain the list of settable attributes for the file.
2911 * Returns the type and full attribute name of all the attributes
2912 * that can be set on this file. This doesn't mean setting it will always
2913 * succeed though, you might get an access failure, or some specific
2914 * file may not support a specific attribute.
2916 * If @cancellable is not %NULL, then the operation can be cancelled by
2917 * triggering the cancellable object from another thread. If the operation
2918 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2920 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2921 * When you are done with it, release it with g_file_attribute_info_list_unref()
2923 GFileAttributeInfoList *
2924 g_file_query_settable_attributes (GFile *file,
2925 GCancellable *cancellable,
2930 GFileAttributeInfoList *list;
2932 g_return_val_if_fail (G_IS_FILE (file), NULL);
2934 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2937 iface = G_FILE_GET_IFACE (file);
2939 if (iface->query_settable_attributes == NULL)
2940 return g_file_attribute_info_list_new ();
2943 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2947 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2949 list = g_file_attribute_info_list_new ();
2950 g_error_free (my_error);
2953 g_propagate_error (error, my_error);
2960 * g_file_query_writable_namespaces:
2961 * @file: input #GFile.
2962 * @cancellable: optional #GCancellable object, %NULL to ignore.
2963 * @error: a #GError, or %NULL
2965 * Obtain the list of attribute namespaces where new attributes
2966 * can be created by a user. An example of this is extended
2967 * attributes (in the "xattr" namespace).
2969 * If @cancellable is not %NULL, then the operation can be cancelled by
2970 * triggering the cancellable object from another thread. If the operation
2971 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2973 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2974 * When you are done with it, release it with g_file_attribute_info_list_unref()
2976 GFileAttributeInfoList *
2977 g_file_query_writable_namespaces (GFile *file,
2978 GCancellable *cancellable,
2983 GFileAttributeInfoList *list;
2985 g_return_val_if_fail (G_IS_FILE (file), NULL);
2987 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2990 iface = G_FILE_GET_IFACE (file);
2992 if (iface->query_writable_namespaces == NULL)
2993 return g_file_attribute_info_list_new ();
2996 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3000 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3002 list = g_file_attribute_info_list_new ();
3003 g_error_free (my_error);
3006 g_propagate_error (error, my_error);
3013 * g_file_set_attribute:
3014 * @file: input #GFile.
3015 * @attribute: a string containing the attribute's name.
3016 * @type: The type of the attribute
3017 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3018 * @flags: a set of #GFileQueryInfoFlags.
3019 * @cancellable: optional #GCancellable object, %NULL to ignore.
3020 * @error: a #GError, or %NULL
3022 * Sets an attribute in the file with attribute name @attribute to @value.
3024 * If @cancellable is not %NULL, then the operation can be cancelled by
3025 * triggering the cancellable object from another thread. If the operation
3026 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3028 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3031 g_file_set_attribute (GFile *file,
3032 const char *attribute,
3033 GFileAttributeType type,
3035 GFileQueryInfoFlags flags,
3036 GCancellable *cancellable,
3041 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3042 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3044 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3047 iface = G_FILE_GET_IFACE (file);
3049 if (iface->set_attribute == NULL)
3051 g_set_error (error, G_IO_ERROR,
3052 G_IO_ERROR_NOT_SUPPORTED,
3053 _("Operation not supported"));
3057 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3061 * g_file_set_attributes_from_info:
3062 * @file: input #GFile.
3063 * @info: a #GFileInfo.
3064 * @flags: #GFileQueryInfoFlags
3065 * @cancellable: optional #GCancellable object, %NULL to ignore.
3066 * @error: a #GError, or %NULL
3068 * Tries to set all attributes in the #GFileInfo on the target values,
3069 * not stopping on the first error.
3071 * If there is any error during this operation then @error will be set to
3072 * the first error. Error on particular fields are flagged by setting
3073 * the "status" field in the attribute value to
3074 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3077 * If @cancellable is not %NULL, then the operation can be cancelled by
3078 * triggering the cancellable object from another thread. If the operation
3079 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3081 * Returns: %TRUE if there was any error, %FALSE otherwise.
3084 g_file_set_attributes_from_info (GFile *file,
3086 GFileQueryInfoFlags flags,
3087 GCancellable *cancellable,
3092 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3093 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3095 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3098 g_file_info_clear_status (info);
3100 iface = G_FILE_GET_IFACE (file);
3102 return (* iface->set_attributes_from_info) (file,
3111 g_file_real_set_attributes_from_info (GFile *file,
3113 GFileQueryInfoFlags flags,
3114 GCancellable *cancellable,
3120 GFileAttributeValue *value;
3124 attributes = g_file_info_list_attributes (info, NULL);
3126 for (i = 0; attributes[i] != NULL; i++)
3128 value = _g_file_info_get_attribute_value (info, attributes[i]);
3130 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3133 if (!g_file_set_attribute (file, attributes[i],
3134 value->type, _g_file_attribute_value_peek_as_pointer (value),
3135 flags, cancellable, error))
3137 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3139 /* Don't set error multiple times */
3143 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3146 g_strfreev (attributes);
3152 * g_file_set_attributes_async:
3153 * @file: input #GFile.
3154 * @info: a #GFileInfo.
3155 * @flags: a #GFileQueryInfoFlags.
3156 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3158 * @cancellable: optional #GCancellable object, %NULL to ignore.
3159 * @callback: a #GAsyncReadyCallback.
3160 * @user_data: a #gpointer.
3162 * Asynchronously sets the attributes of @file with @info.
3164 * For more details, see g_file_set_attributes_from_info() which is
3165 * the synchronous version of this call.
3167 * When the operation is finished, @callback will be called. You can then call
3168 * g_file_set_attributes_finish() to get the result of the operation.
3171 g_file_set_attributes_async (GFile *file,
3173 GFileQueryInfoFlags flags,
3175 GCancellable *cancellable,
3176 GAsyncReadyCallback callback,
3181 g_return_if_fail (G_IS_FILE (file));
3182 g_return_if_fail (G_IS_FILE_INFO (info));
3184 iface = G_FILE_GET_IFACE (file);
3185 (* iface->set_attributes_async) (file,
3195 * g_file_set_attributes_finish:
3196 * @file: input #GFile.
3197 * @result: a #GAsyncResult.
3198 * @info: a #GFileInfo.
3199 * @error: a #GError, or %NULL
3201 * Finishes setting an attribute started in g_file_set_attributes_async().
3203 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3206 g_file_set_attributes_finish (GFile *file,
3207 GAsyncResult *result,
3213 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3214 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3216 /* No standard handling of errors here, as we must set info even
3219 iface = G_FILE_GET_IFACE (file);
3220 return (* iface->set_attributes_finish) (file, result, info, error);
3224 * g_file_set_attribute_string:
3225 * @file: input #GFile.
3226 * @attribute: a string containing the attribute's name.
3227 * @value: a string containing the attribute's value.
3228 * @flags: #GFileQueryInfoFlags.
3229 * @cancellable: optional #GCancellable object, %NULL to ignore.
3230 * @error: a #GError, or %NULL
3232 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3233 * If @attribute is of a different type, this operation will fail.
3235 * If @cancellable is not %NULL, then the operation can be cancelled by
3236 * triggering the cancellable object from another thread. If the operation
3237 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3239 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3242 g_file_set_attribute_string (GFile *file,
3243 const char *attribute,
3245 GFileQueryInfoFlags flags,
3246 GCancellable *cancellable,
3249 return g_file_set_attribute (file, attribute,
3250 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3251 flags, cancellable, error);
3255 * g_file_set_attribute_byte_string:
3256 * @file: input #GFile.
3257 * @attribute: a string containing the attribute's name.
3258 * @value: a string containing the attribute's new value.
3259 * @flags: a #GFileQueryInfoFlags.
3260 * @cancellable: optional #GCancellable object, %NULL to ignore.
3261 * @error: a #GError, or %NULL
3263 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3264 * If @attribute is of a different type, this operation will fail,
3267 * If @cancellable is not %NULL, then the operation can be cancelled by
3268 * triggering the cancellable object from another thread. If the operation
3269 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3271 * Returns: %TRUE if the @attribute was successfully set to @value
3272 * in the @file, %FALSE otherwise.
3275 g_file_set_attribute_byte_string (GFile *file,
3276 const char *attribute,
3278 GFileQueryInfoFlags flags,
3279 GCancellable *cancellable,
3282 return g_file_set_attribute (file, attribute,
3283 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3284 flags, cancellable, error);
3288 * g_file_set_attribute_uint32:
3289 * @file: input #GFile.
3290 * @attribute: a string containing the attribute's name.
3291 * @value: a #guint32 containing the attribute's new value.
3292 * @flags: a #GFileQueryInfoFlags.
3293 * @cancellable: optional #GCancellable object, %NULL to ignore.
3294 * @error: a #GError, or %NULL
3296 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3297 * If @attribute is of a different type, this operation will fail.
3299 * If @cancellable is not %NULL, then the operation can be cancelled by
3300 * triggering the cancellable object from another thread. If the operation
3301 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3303 * Returns: %TRUE if the @attribute was successfully set to @value
3304 * in the @file, %FALSE otherwise.
3307 g_file_set_attribute_uint32 (GFile *file,
3308 const char *attribute,
3310 GFileQueryInfoFlags flags,
3311 GCancellable *cancellable,
3314 return g_file_set_attribute (file, attribute,
3315 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3316 flags, cancellable, error);
3320 * g_file_set_attribute_int32:
3321 * @file: input #GFile.
3322 * @attribute: a string containing the attribute's name.
3323 * @value: a #gint32 containing the attribute's new value.
3324 * @flags: a #GFileQueryInfoFlags.
3325 * @cancellable: optional #GCancellable object, %NULL to ignore.
3326 * @error: a #GError, or %NULL
3328 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3329 * If @attribute is of a different type, this operation will fail.
3331 * If @cancellable is not %NULL, then the operation can be cancelled by
3332 * triggering the cancellable object from another thread. If the operation
3333 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3335 * Returns: %TRUE if the @attribute was successfully set to @value
3336 * in the @file, %FALSE otherwise.
3339 g_file_set_attribute_int32 (GFile *file,
3340 const char *attribute,
3342 GFileQueryInfoFlags flags,
3343 GCancellable *cancellable,
3346 return g_file_set_attribute (file, attribute,
3347 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3348 flags, cancellable, error);
3352 * g_file_set_attribute_uint64:
3353 * @file: input #GFile.
3354 * @attribute: a string containing the attribute's name.
3355 * @value: a #guint64 containing the attribute's new value.
3356 * @flags: a #GFileQueryInfoFlags.
3357 * @cancellable: optional #GCancellable object, %NULL to ignore.
3358 * @error: a #GError, or %NULL
3360 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3361 * If @attribute is of a different type, this operation will fail.
3363 * If @cancellable is not %NULL, then the operation can be cancelled by
3364 * triggering the cancellable object from another thread. If the operation
3365 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3367 * Returns: %TRUE if the @attribute was successfully set to @value
3368 * in the @file, %FALSE otherwise.
3371 g_file_set_attribute_uint64 (GFile *file,
3372 const char *attribute,
3374 GFileQueryInfoFlags flags,
3375 GCancellable *cancellable,
3378 return g_file_set_attribute (file, attribute,
3379 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3380 flags, cancellable, error);
3384 * g_file_set_attribute_int64:
3385 * @file: input #GFile.
3386 * @attribute: a string containing the attribute's name.
3387 * @value: a #guint64 containing the attribute's new value.
3388 * @flags: a #GFileQueryInfoFlags.
3389 * @cancellable: optional #GCancellable object, %NULL to ignore.
3390 * @error: a #GError, or %NULL
3392 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3393 * If @attribute is of a different type, this operation will fail.
3395 * If @cancellable is not %NULL, then the operation can be cancelled by
3396 * triggering the cancellable object from another thread. If the operation
3397 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3399 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3402 g_file_set_attribute_int64 (GFile *file,
3403 const char *attribute,
3405 GFileQueryInfoFlags flags,
3406 GCancellable *cancellable,
3409 return g_file_set_attribute (file, attribute,
3410 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3411 flags, cancellable, error);
3415 * g_file_mount_mountable:
3416 * @file: input #GFile.
3417 * @flags: flags affecting the operation
3418 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3419 * @cancellable: optional #GCancellable object, %NULL to ignore.
3420 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3421 * @user_data: the data to pass to callback function
3423 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3424 * Using @mount_operation, you can request callbacks when, for instance,
3425 * passwords are needed during authentication.
3427 * If @cancellable is not %NULL, then the operation can be cancelled by
3428 * triggering the cancellable object from another thread. If the operation
3429 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3431 * When the operation is finished, @callback will be called. You can then call
3432 * g_file_mount_mountable_finish() to get the result of the operation.
3435 g_file_mount_mountable (GFile *file,
3436 GMountMountFlags flags,
3437 GMountOperation *mount_operation,
3438 GCancellable *cancellable,
3439 GAsyncReadyCallback callback,
3444 g_return_if_fail (G_IS_FILE (file));
3446 iface = G_FILE_GET_IFACE (file);
3448 if (iface->mount_mountable == NULL)
3450 g_simple_async_report_error_in_idle (G_OBJECT (file),
3454 G_IO_ERROR_NOT_SUPPORTED,
3455 _("Operation not supported"));
3459 (* iface->mount_mountable) (file,
3468 * g_file_mount_mountable_finish:
3469 * @file: input #GFile.
3470 * @result: a #GAsyncResult.
3471 * @error: a #GError, or %NULL
3473 * Finishes a mount operation. See g_file_mount_mountable() for details.
3475 * Finish an asynchronous mount operation that was started
3476 * with g_file_mount_mountable().
3478 * Returns: a #GFile or %NULL on error.
3481 g_file_mount_mountable_finish (GFile *file,
3482 GAsyncResult *result,
3487 g_return_val_if_fail (G_IS_FILE (file), NULL);
3488 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3490 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3492 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3493 if (g_simple_async_result_propagate_error (simple, error))
3497 iface = G_FILE_GET_IFACE (file);
3498 return (* iface->mount_mountable_finish) (file, result, error);
3502 * g_file_unmount_mountable:
3503 * @file: input #GFile.
3504 * @flags: flags affecting the operation
3505 * @cancellable: optional #GCancellable object, %NULL to ignore.
3506 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3507 * @user_data: the data to pass to callback function
3509 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3511 * If @cancellable is not %NULL, then the operation can be cancelled by
3512 * triggering the cancellable object from another thread. If the operation
3513 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3515 * When the operation is finished, @callback will be called. You can then call
3516 * g_file_unmount_mountable_finish() to get the result of the operation.
3519 g_file_unmount_mountable (GFile *file,
3520 GMountUnmountFlags flags,
3521 GCancellable *cancellable,
3522 GAsyncReadyCallback callback,
3527 g_return_if_fail (G_IS_FILE (file));
3529 iface = G_FILE_GET_IFACE (file);
3531 if (iface->unmount_mountable == NULL)
3533 g_simple_async_report_error_in_idle (G_OBJECT (file),
3537 G_IO_ERROR_NOT_SUPPORTED,
3538 _("Operation not supported"));
3542 (* iface->unmount_mountable) (file,
3550 * g_file_unmount_mountable_finish:
3551 * @file: input #GFile.
3552 * @result: a #GAsyncResult.
3553 * @error: a #GError, or %NULL
3555 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3557 * Finish an asynchronous unmount operation that was started
3558 * with g_file_unmount_mountable().
3560 * Returns: %TRUE if the operation finished successfully. %FALSE
3564 g_file_unmount_mountable_finish (GFile *file,
3565 GAsyncResult *result,
3570 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3571 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3573 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3575 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3576 if (g_simple_async_result_propagate_error (simple, error))
3580 iface = G_FILE_GET_IFACE (file);
3581 return (* iface->unmount_mountable_finish) (file, result, error);
3585 * g_file_eject_mountable:
3586 * @file: input #GFile.
3587 * @flags: flags affecting the operation
3588 * @cancellable: optional #GCancellable object, %NULL to ignore.
3589 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3590 * @user_data: the data to pass to callback function
3592 * Starts an asynchronous eject on a mountable.
3593 * When this operation has completed, @callback will be called with
3594 * @user_user data, and the operation can be finalized with
3595 * g_file_eject_mountable_finish().
3597 * If @cancellable is not %NULL, then the operation can be cancelled by
3598 * triggering the cancellable object from another thread. If the operation
3599 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3602 g_file_eject_mountable (GFile *file,
3603 GMountUnmountFlags flags,
3604 GCancellable *cancellable,
3605 GAsyncReadyCallback callback,
3610 g_return_if_fail (G_IS_FILE (file));
3612 iface = G_FILE_GET_IFACE (file);
3614 if (iface->eject_mountable == NULL)
3616 g_simple_async_report_error_in_idle (G_OBJECT (file),
3620 G_IO_ERROR_NOT_SUPPORTED,
3621 _("Operation not supported"));
3625 (* iface->eject_mountable) (file,
3633 * g_file_eject_mountable_finish:
3634 * @file: input #GFile.
3635 * @result: a #GAsyncResult.
3636 * @error: a #GError, or %NULL
3638 * Finishes an asynchronous eject operation started by
3639 * g_file_eject_mountable().
3641 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3645 g_file_eject_mountable_finish (GFile *file,
3646 GAsyncResult *result,
3651 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3652 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3654 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3656 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3657 if (g_simple_async_result_propagate_error (simple, error))
3661 iface = G_FILE_GET_IFACE (file);
3662 return (* iface->eject_mountable_finish) (file, result, error);
3666 * g_file_monitor_directory:
3667 * @file: input #GFile.
3668 * @flags: a set of #GFileMonitorFlags.
3669 * @cancellable: optional #GCancellable object, %NULL to ignore.
3670 * @error: a #GError, or %NULL.
3672 * Obtains a directory monitor for the given file.
3673 * This may fail if directory monitoring is not supported.
3675 * If @cancellable is not %NULL, then the operation can be cancelled by
3676 * triggering the cancellable object from another thread. If the operation
3677 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3679 * Returns: a #GFileMonitor for the given @file,
3680 * or %NULL on error.
3683 g_file_monitor_directory (GFile *file,
3684 GFileMonitorFlags flags,
3685 GCancellable *cancellable,
3690 g_return_val_if_fail (G_IS_FILE (file), NULL);
3692 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3695 iface = G_FILE_GET_IFACE (file);
3697 if (iface->monitor_dir == NULL)
3699 g_set_error (error, G_IO_ERROR,
3700 G_IO_ERROR_NOT_SUPPORTED,
3701 _("Operation not supported"));
3705 return (* iface->monitor_dir) (file, flags, cancellable, error);
3709 * g_file_monitor_file:
3710 * @file: input #GFile.
3711 * @flags: a set of #GFileMonitorFlags.
3712 * @cancellable: optional #GCancellable object, %NULL to ignore.
3713 * @error: a #GError, or %NULL.
3715 * Obtains a file monitor for the given file. If no file notification
3716 * mechanism exists, then regular polling of the file is used.
3718 * If @cancellable is not %NULL, then the operation can be cancelled by
3719 * triggering the cancellable object from another thread. If the operation
3720 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3722 * Returns: a #GFileMonitor for the given @file.
3725 g_file_monitor_file (GFile *file,
3726 GFileMonitorFlags flags,
3727 GCancellable *cancellable,
3731 GFileMonitor *monitor;
3733 g_return_val_if_fail (G_IS_FILE (file), NULL);
3735 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3738 iface = G_FILE_GET_IFACE (file);
3742 if (iface->monitor_file)
3743 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3745 /* Fallback to polling */
3746 if (monitor == NULL)
3747 monitor = _g_poll_file_monitor_new (file);
3752 /********************************************
3753 * Default implementation of async ops *
3754 ********************************************/
3758 GFileQueryInfoFlags flags;
3760 } QueryInfoAsyncData;
3763 query_info_data_free (QueryInfoAsyncData *data)
3766 g_object_unref (data->info);
3767 g_free (data->attributes);
3772 query_info_async_thread (GSimpleAsyncResult *res,
3774 GCancellable *cancellable)
3776 GError *error = NULL;
3777 QueryInfoAsyncData *data;
3780 data = g_simple_async_result_get_op_res_gpointer (res);
3782 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3786 g_simple_async_result_set_from_error (res, error);
3787 g_error_free (error);
3794 g_file_real_query_info_async (GFile *file,
3795 const char *attributes,
3796 GFileQueryInfoFlags flags,
3798 GCancellable *cancellable,
3799 GAsyncReadyCallback callback,
3802 GSimpleAsyncResult *res;
3803 QueryInfoAsyncData *data;
3805 data = g_new0 (QueryInfoAsyncData, 1);
3806 data->attributes = g_strdup (attributes);
3807 data->flags = flags;
3809 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3810 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3812 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3813 g_object_unref (res);
3817 g_file_real_query_info_finish (GFile *file,
3821 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3822 QueryInfoAsyncData *data;
3824 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3826 data = g_simple_async_result_get_op_res_gpointer (simple);
3828 return g_object_ref (data->info);
3836 } QueryFilesystemInfoAsyncData;
3839 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
3842 g_object_unref (data->info);
3843 g_free (data->attributes);
3848 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
3850 GCancellable *cancellable)
3852 GError *error = NULL;
3853 QueryFilesystemInfoAsyncData *data;
3856 data = g_simple_async_result_get_op_res_gpointer (res);
3858 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
3862 g_simple_async_result_set_from_error (res, error);
3863 g_error_free (error);
3870 g_file_real_query_filesystem_info_async (GFile *file,
3871 const char *attributes,
3873 GCancellable *cancellable,
3874 GAsyncReadyCallback callback,
3877 GSimpleAsyncResult *res;
3878 QueryFilesystemInfoAsyncData *data;
3880 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
3881 data->attributes = g_strdup (attributes);
3883 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
3884 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
3886 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
3887 g_object_unref (res);
3891 g_file_real_query_filesystem_info_finish (GFile *file,
3895 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3896 QueryFilesystemInfoAsyncData *data;
3898 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
3900 data = g_simple_async_result_get_op_res_gpointer (simple);
3902 return g_object_ref (data->info);
3909 GFileQueryInfoFlags flags;
3910 GFileEnumerator *enumerator;
3911 } EnumerateChildrenAsyncData;
3914 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3916 if (data->enumerator)
3917 g_object_unref (data->enumerator);
3918 g_free (data->attributes);
3923 enumerate_children_async_thread (GSimpleAsyncResult *res,
3925 GCancellable *cancellable)
3927 GError *error = NULL;
3928 EnumerateChildrenAsyncData *data;
3929 GFileEnumerator *enumerator;
3931 data = g_simple_async_result_get_op_res_gpointer (res);
3933 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3935 if (enumerator == NULL)
3937 g_simple_async_result_set_from_error (res, error);
3938 g_error_free (error);
3941 data->enumerator = enumerator;
3945 g_file_real_enumerate_children_async (GFile *file,
3946 const char *attributes,
3947 GFileQueryInfoFlags flags,
3949 GCancellable *cancellable,
3950 GAsyncReadyCallback callback,
3953 GSimpleAsyncResult *res;
3954 EnumerateChildrenAsyncData *data;
3956 data = g_new0 (EnumerateChildrenAsyncData, 1);
3957 data->attributes = g_strdup (attributes);
3958 data->flags = flags;
3960 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3961 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3963 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3964 g_object_unref (res);
3967 static GFileEnumerator *
3968 g_file_real_enumerate_children_finish (GFile *file,
3972 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3973 EnumerateChildrenAsyncData *data;
3975 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3977 data = g_simple_async_result_get_op_res_gpointer (simple);
3978 if (data->enumerator)
3979 return g_object_ref (data->enumerator);
3985 open_read_async_thread (GSimpleAsyncResult *res,
3987 GCancellable *cancellable)
3990 GFileInputStream *stream;
3991 GError *error = NULL;
3993 iface = G_FILE_GET_IFACE (object);
3995 stream = iface->read_fn (G_FILE (object), cancellable, &error);
3999 g_simple_async_result_set_from_error (res, error);
4000 g_error_free (error);
4003 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4007 g_file_real_read_async (GFile *file,
4009 GCancellable *cancellable,
4010 GAsyncReadyCallback callback,
4013 GSimpleAsyncResult *res;
4015 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4017 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4018 g_object_unref (res);
4021 static GFileInputStream *
4022 g_file_real_read_finish (GFile *file,
4026 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4029 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4031 op = g_simple_async_result_get_op_res_gpointer (simple);
4033 return g_object_ref (op);
4039 append_to_async_thread (GSimpleAsyncResult *res,
4041 GCancellable *cancellable)
4044 GFileCreateFlags *data;
4045 GFileOutputStream *stream;
4046 GError *error = NULL;
4048 iface = G_FILE_GET_IFACE (object);
4050 data = g_simple_async_result_get_op_res_gpointer (res);
4052 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4056 g_simple_async_result_set_from_error (res, error);
4057 g_error_free (error);
4060 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4064 g_file_real_append_to_async (GFile *file,
4065 GFileCreateFlags flags,
4067 GCancellable *cancellable,
4068 GAsyncReadyCallback callback,
4071 GFileCreateFlags *data;
4072 GSimpleAsyncResult *res;
4074 data = g_new0 (GFileCreateFlags, 1);
4077 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4078 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4080 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4081 g_object_unref (res);
4084 static GFileOutputStream *
4085 g_file_real_append_to_finish (GFile *file,
4089 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4092 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4094 op = g_simple_async_result_get_op_res_gpointer (simple);
4096 return g_object_ref (op);
4102 create_async_thread (GSimpleAsyncResult *res,
4104 GCancellable *cancellable)
4107 GFileCreateFlags *data;
4108 GFileOutputStream *stream;
4109 GError *error = NULL;
4111 iface = G_FILE_GET_IFACE (object);
4113 data = g_simple_async_result_get_op_res_gpointer (res);
4115 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4119 g_simple_async_result_set_from_error (res, error);
4120 g_error_free (error);
4123 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4127 g_file_real_create_async (GFile *file,
4128 GFileCreateFlags flags,
4130 GCancellable *cancellable,
4131 GAsyncReadyCallback callback,
4134 GFileCreateFlags *data;
4135 GSimpleAsyncResult *res;
4137 data = g_new0 (GFileCreateFlags, 1);
4140 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4141 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4143 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4144 g_object_unref (res);
4147 static GFileOutputStream *
4148 g_file_real_create_finish (GFile *file,
4152 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4155 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4157 op = g_simple_async_result_get_op_res_gpointer (simple);
4159 return g_object_ref (op);
4165 GFileOutputStream *stream;
4167 gboolean make_backup;
4168 GFileCreateFlags flags;
4172 replace_async_data_free (ReplaceAsyncData *data)
4175 g_object_unref (data->stream);
4176 g_free (data->etag);
4181 replace_async_thread (GSimpleAsyncResult *res,
4183 GCancellable *cancellable)
4186 GFileOutputStream *stream;
4187 GError *error = NULL;
4188 ReplaceAsyncData *data;
4190 iface = G_FILE_GET_IFACE (object);
4192 data = g_simple_async_result_get_op_res_gpointer (res);
4194 stream = iface->replace (G_FILE (object),
4203 g_simple_async_result_set_from_error (res, error);
4204 g_error_free (error);
4207 data->stream = stream;
4211 g_file_real_replace_async (GFile *file,
4213 gboolean make_backup,
4214 GFileCreateFlags flags,
4216 GCancellable *cancellable,
4217 GAsyncReadyCallback callback,
4220 GSimpleAsyncResult *res;
4221 ReplaceAsyncData *data;
4223 data = g_new0 (ReplaceAsyncData, 1);
4224 data->etag = g_strdup (etag);
4225 data->make_backup = make_backup;
4226 data->flags = flags;
4228 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4229 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4231 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4232 g_object_unref (res);
4235 static GFileOutputStream *
4236 g_file_real_replace_finish (GFile *file,
4240 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4241 ReplaceAsyncData *data;
4243 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4245 data = g_simple_async_result_get_op_res_gpointer (simple);
4247 return g_object_ref (data->stream);
4255 } SetDisplayNameAsyncData;
4258 set_display_name_data_free (SetDisplayNameAsyncData *data)
4260 g_free (data->name);
4262 g_object_unref (data->file);
4267 set_display_name_async_thread (GSimpleAsyncResult *res,
4269 GCancellable *cancellable)
4271 GError *error = NULL;
4272 SetDisplayNameAsyncData *data;
4275 data = g_simple_async_result_get_op_res_gpointer (res);
4277 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4281 g_simple_async_result_set_from_error (res, error);
4282 g_error_free (error);
4289 g_file_real_set_display_name_async (GFile *file,
4290 const char *display_name,
4292 GCancellable *cancellable,
4293 GAsyncReadyCallback callback,
4296 GSimpleAsyncResult *res;
4297 SetDisplayNameAsyncData *data;
4299 data = g_new0 (SetDisplayNameAsyncData, 1);
4300 data->name = g_strdup (display_name);
4302 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4303 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4305 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4306 g_object_unref (res);
4310 g_file_real_set_display_name_finish (GFile *file,
4314 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4315 SetDisplayNameAsyncData *data;
4317 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4319 data = g_simple_async_result_get_op_res_gpointer (simple);
4321 return g_object_ref (data->file);
4327 GFileQueryInfoFlags flags;
4334 set_info_data_free (SetInfoAsyncData *data)
4337 g_object_unref (data->info);
4339 g_error_free (data->error);
4344 set_info_async_thread (GSimpleAsyncResult *res,
4346 GCancellable *cancellable)
4348 SetInfoAsyncData *data;
4350 data = g_simple_async_result_get_op_res_gpointer (res);
4353 data->res = g_file_set_attributes_from_info (G_FILE (object),
4361 g_file_real_set_attributes_async (GFile *file,
4363 GFileQueryInfoFlags flags,
4365 GCancellable *cancellable,
4366 GAsyncReadyCallback callback,
4369 GSimpleAsyncResult *res;
4370 SetInfoAsyncData *data;
4372 data = g_new0 (SetInfoAsyncData, 1);
4373 data->info = g_file_info_dup (info);
4374 data->flags = flags;
4376 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4377 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4379 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4380 g_object_unref (res);
4384 g_file_real_set_attributes_finish (GFile *file,
4389 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4390 SetInfoAsyncData *data;
4392 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4394 data = g_simple_async_result_get_op_res_gpointer (simple);
4397 *info = g_object_ref (data->info);
4399 if (error != NULL && data->error)
4400 *error = g_error_copy (data->error);
4406 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4408 GCancellable *cancellable)
4410 GError *error = NULL;
4413 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4417 g_simple_async_result_set_from_error (res, error);
4418 g_error_free (error);
4421 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4425 g_file_real_find_enclosing_mount_async (GFile *file,
4427 GCancellable *cancellable,
4428 GAsyncReadyCallback callback,
4431 GSimpleAsyncResult *res;
4433 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4435 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4436 g_object_unref (res);
4440 g_file_real_find_enclosing_mount_finish (GFile *file,
4444 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4447 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4449 mount = g_simple_async_result_get_op_res_gpointer (simple);
4450 return g_object_ref (mount);
4457 GFileCopyFlags flags;
4458 GFileProgressCallback progress_cb;
4459 gpointer progress_cb_data;
4460 GIOSchedulerJob *job;
4464 copy_async_data_free (CopyAsyncData *data)
4466 g_object_unref (data->source);
4467 g_object_unref (data->destination);
4472 CopyAsyncData *data;
4473 goffset current_num_bytes;
4474 goffset total_num_bytes;
4478 copy_async_progress_in_main (gpointer user_data)
4480 ProgressData *progress = user_data;
4481 CopyAsyncData *data = progress->data;
4483 data->progress_cb (progress->current_num_bytes,
4484 progress->total_num_bytes,
4485 data->progress_cb_data);
4491 mainloop_barrier (gpointer user_data)
4493 /* Does nothing, but ensures all queued idles before
4500 copy_async_progress_callback (goffset current_num_bytes,
4501 goffset total_num_bytes,
4504 CopyAsyncData *data = user_data;
4505 ProgressData *progress;
4507 progress = g_new (ProgressData, 1);
4508 progress->data = data;
4509 progress->current_num_bytes = current_num_bytes;
4510 progress->total_num_bytes = total_num_bytes;
4512 g_io_scheduler_job_send_to_mainloop_async (data->job,
4513 copy_async_progress_in_main,
4519 copy_async_thread (GIOSchedulerJob *job,
4520 GCancellable *cancellable,
4523 GSimpleAsyncResult *res;
4524 CopyAsyncData *data;
4529 data = g_simple_async_result_get_op_res_gpointer (res);
4533 result = g_file_copy (data->source,
4537 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4541 /* Ensure all progress callbacks are done running in main thread */
4542 if (data->progress_cb != NULL)
4543 g_io_scheduler_job_send_to_mainloop (job,
4549 g_simple_async_result_set_from_error (res, error);
4550 g_error_free (error);
4553 g_simple_async_result_complete_in_idle (res);
4559 g_file_real_copy_async (GFile *source,
4561 GFileCopyFlags flags,
4563 GCancellable *cancellable,
4564 GFileProgressCallback progress_callback,
4565 gpointer progress_callback_data,
4566 GAsyncReadyCallback callback,
4569 GSimpleAsyncResult *res;
4570 CopyAsyncData *data;
4572 data = g_new0 (CopyAsyncData, 1);
4573 data->source = g_object_ref (source);
4574 data->destination = g_object_ref (destination);
4575 data->flags = flags;
4576 data->progress_cb = progress_callback;
4577 data->progress_cb_data = progress_callback_data;
4579 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4580 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4582 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4586 g_file_real_copy_finish (GFile *file,
4590 /* Error handled in g_file_copy_finish() */
4595 /********************************************
4596 * Default VFS operations *
4597 ********************************************/
4600 * g_file_new_for_path:
4601 * @path: a string containing a relative or absolute path.
4603 * Constructs a #GFile for a given path. This operation never
4604 * fails, but the returned object might not support any I/O
4605 * operation if @path is malformed.
4607 * Returns: a new #GFile for the given @path.
4610 g_file_new_for_path (const char *path)
4612 g_return_val_if_fail (path != NULL, NULL);
4614 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4618 * g_file_new_for_uri:
4619 * @uri: a string containing a URI.
4621 * Constructs a #GFile for a given URI. This operation never
4622 * fails, but the returned object might not support any I/O
4623 * operation if @uri is malformed or if the uri type is
4626 * Returns: a #GFile for the given @uri.
4629 g_file_new_for_uri (const char *uri)
4631 g_return_val_if_fail (uri != NULL, NULL);
4633 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4637 * g_file_parse_name:
4638 * @parse_name: a file name or path to be parsed.
4640 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4641 * This operation never fails, but the returned object might not support any I/O
4642 * operation if the @parse_name cannot be parsed.
4644 * Returns: a new #GFile.
4647 g_file_parse_name (const char *parse_name)
4649 g_return_val_if_fail (parse_name != NULL, NULL);
4651 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4655 is_valid_scheme_character (char c)
4657 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4661 has_valid_scheme (const char *uri)
4667 if (!is_valid_scheme_character (*p))
4672 } while (is_valid_scheme_character (*p));
4678 * g_file_new_for_commandline_arg:
4679 * @arg: a command line string.
4681 * Creates a #GFile with the given argument from the command line. The value of
4682 * @arg can be either a URI, an absolute path or a relative path resolved
4683 * relative to the current working directory.
4684 * This operation never fails, but the returned object might not support any
4685 * I/O operation if @arg points to a malformed path.
4687 * Returns: a new #GFile.
4690 g_file_new_for_commandline_arg (const char *arg)
4696 g_return_val_if_fail (arg != NULL, NULL);
4698 if (g_path_is_absolute (arg))
4699 return g_file_new_for_path (arg);
4701 if (has_valid_scheme (arg))
4702 return g_file_new_for_uri (arg);
4704 current_dir = g_get_current_dir ();
4705 filename = g_build_filename (current_dir, arg, NULL);
4706 g_free (current_dir);
4708 file = g_file_new_for_path (filename);
4715 * g_file_mount_enclosing_volume:
4716 * @location: input #GFile.
4717 * @flags: flags affecting the operation
4718 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4719 * @cancellable: optional #GCancellable object, %NULL to ignore.
4720 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4721 * @user_data: the data to pass to callback function
4723 * Starts a @mount_operation, mounting the volume that contains the file @location.
4725 * When this operation has completed, @callback will be called with
4726 * @user_user data, and the operation can be finalized with
4727 * g_file_mount_enclosing_volume_finish().
4729 * If @cancellable is not %NULL, then the operation can be cancelled by
4730 * triggering the cancellable object from another thread. If the operation
4731 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4734 g_file_mount_enclosing_volume (GFile *location,
4735 GMountMountFlags flags,
4736 GMountOperation *mount_operation,
4737 GCancellable *cancellable,
4738 GAsyncReadyCallback callback,
4743 g_return_if_fail (G_IS_FILE (location));
4745 iface = G_FILE_GET_IFACE (location);
4747 if (iface->mount_enclosing_volume == NULL)
4749 g_simple_async_report_error_in_idle (G_OBJECT (location),
4750 callback, user_data,
4751 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4752 _("volume doesn't implement mount"));
4757 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4762 * g_file_mount_enclosing_volume_finish:
4763 * @location: input #GFile.
4764 * @result: a #GAsyncResult.
4765 * @error: a #GError, or %NULL
4767 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4769 * Returns: %TRUE if successful. If an error
4770 * has occurred, this function will return %FALSE and set @error
4771 * appropriately if present.
4774 g_file_mount_enclosing_volume_finish (GFile *location,
4775 GAsyncResult *result,
4780 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4781 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4783 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4785 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4786 if (g_simple_async_result_propagate_error (simple, error))
4790 iface = G_FILE_GET_IFACE (location);
4792 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4795 /********************************************
4796 * Utility functions *
4797 ********************************************/
4800 * g_file_query_default_handler:
4801 * @file: a #GFile to open.
4802 * @cancellable: optional #GCancellable object, %NULL to ignore.
4803 * @error: a #GError, or %NULL
4805 * Returns the #GAppInfo that is registered as the default
4806 * application to handle the file specified by @file.
4808 * If @cancellable is not %NULL, then the operation can be cancelled by
4809 * triggering the cancellable object from another thread. If the operation
4810 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4812 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4813 * When you are done with it, release it with g_object_unref()
4816 g_file_query_default_handler (GFile *file,
4817 GCancellable *cancellable,
4821 const char *content_type;
4826 uri_scheme = g_file_get_uri_scheme (file);
4827 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4828 g_free (uri_scheme);
4830 if (appinfo != NULL)
4833 info = g_file_query_info (file,
4834 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4843 content_type = g_file_info_get_content_type (info);
4846 /* Don't use is_native(), as we want to support fuse paths if availible */
4847 path = g_file_get_path (file);
4848 appinfo = g_app_info_get_default_for_type (content_type,
4853 g_object_unref (info);
4855 if (appinfo != NULL)
4858 g_set_error (error, G_IO_ERROR,
4859 G_IO_ERROR_NOT_SUPPORTED,
4860 _("No application is registered as handling this file"));
4866 #define GET_CONTENT_BLOCK_SIZE 8192
4869 * g_file_load_contents:
4870 * @file: input #GFile.
4871 * @cancellable: optional #GCancellable object, %NULL to ignore.
4872 * @contents: a location to place the contents of the file.
4873 * @length: a location to place the length of the contents of the file.
4874 * @etag_out: a location to place the current entity tag for the file.
4875 * @error: a #GError, or %NULL
4877 * Loads the content of the file into memory, returning the size of
4878 * the data. The data is always zero terminated, but this is not
4879 * included in the resultant @length.
4881 * If @cancellable is not %NULL, then the operation can be cancelled by
4882 * triggering the cancellable object from another thread. If the operation
4883 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4885 * Returns: %TRUE if the @file's contents were successfully loaded.
4886 * %FALSE if there were errors..
4889 g_file_load_contents (GFile *file,
4890 GCancellable *cancellable,
4896 GFileInputStream *in;
4897 GByteArray *content;
4902 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4903 g_return_val_if_fail (contents != NULL, FALSE);
4905 in = g_file_read (file, cancellable, error);
4909 content = g_byte_array_new ();
4912 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4913 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4914 content->data + pos,
4915 GET_CONTENT_BLOCK_SIZE,
4916 cancellable, error)) > 0)
4919 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4926 info = g_file_input_stream_query_info (in,
4927 G_FILE_ATTRIBUTE_ETAG_VALUE,
4932 *etag_out = g_strdup (g_file_info_get_etag (info));
4933 g_object_unref (info);
4937 /* Ignore errors on close */
4938 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4939 g_object_unref (in);
4943 /* error is set already */
4944 g_byte_array_free (content, TRUE);
4951 /* Zero terminate (we got an extra byte allocated for this */
4952 content->data[pos] = 0;
4954 *contents = (char *)g_byte_array_free (content, FALSE);
4962 GCancellable *cancellable;
4963 GFileReadMoreCallback read_more_callback;
4964 GAsyncReadyCallback callback;
4966 GByteArray *content;
4973 load_contents_data_free (LoadContentsData *data)
4976 g_error_free (data->error);
4977 if (data->cancellable)
4978 g_object_unref (data->cancellable);
4980 g_byte_array_free (data->content, TRUE);
4981 g_free (data->etag);
4982 g_object_unref (data->file);
4987 load_contents_close_callback (GObject *obj,
4988 GAsyncResult *close_res,
4991 GInputStream *stream = G_INPUT_STREAM (obj);
4992 LoadContentsData *data = user_data;
4993 GSimpleAsyncResult *res;
4995 /* Ignore errors here, we're only reading anyway */
4996 g_input_stream_close_finish (stream, close_res, NULL);
4997 g_object_unref (stream);
4999 res = g_simple_async_result_new (G_OBJECT (data->file),
5002 g_file_load_contents_async);
5003 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5004 g_simple_async_result_complete (res);
5005 g_object_unref (res);
5009 load_contents_fstat_callback (GObject *obj,
5010 GAsyncResult *stat_res,
5013 GInputStream *stream = G_INPUT_STREAM (obj);
5014 LoadContentsData *data = user_data;
5017 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5021 data->etag = g_strdup (g_file_info_get_etag (info));
5022 g_object_unref (info);
5025 g_input_stream_close_async (stream, 0,
5027 load_contents_close_callback, data);
5031 load_contents_read_callback (GObject *obj,
5032 GAsyncResult *read_res,
5035 GInputStream *stream = G_INPUT_STREAM (obj);
5036 LoadContentsData *data = user_data;
5037 GError *error = NULL;
5040 read_size = g_input_stream_read_finish (stream, read_res, &error);
5044 /* Error or EOF, close the file */
5045 data->error = error;
5046 g_input_stream_close_async (stream, 0,
5048 load_contents_close_callback, data);
5050 else if (read_size == 0)
5052 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5053 G_FILE_ATTRIBUTE_ETAG_VALUE,
5056 load_contents_fstat_callback,
5059 else if (read_size > 0)
5061 data->pos += read_size;
5063 g_byte_array_set_size (data->content,
5064 data->pos + GET_CONTENT_BLOCK_SIZE);
5067 if (data->read_more_callback &&
5068 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5069 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5070 G_FILE_ATTRIBUTE_ETAG_VALUE,
5073 load_contents_fstat_callback,
5076 g_input_stream_read_async (stream,
5077 data->content->data + data->pos,
5078 GET_CONTENT_BLOCK_SIZE,
5081 load_contents_read_callback,
5087 load_contents_open_callback (GObject *obj,
5088 GAsyncResult *open_res,
5091 GFile *file = G_FILE (obj);
5092 GFileInputStream *stream;
5093 LoadContentsData *data = user_data;
5094 GError *error = NULL;
5095 GSimpleAsyncResult *res;
5097 stream = g_file_read_finish (file, open_res, &error);
5101 g_byte_array_set_size (data->content,
5102 data->pos + GET_CONTENT_BLOCK_SIZE);
5103 g_input_stream_read_async (G_INPUT_STREAM (stream),
5104 data->content->data + data->pos,
5105 GET_CONTENT_BLOCK_SIZE,
5108 load_contents_read_callback,
5114 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5118 g_simple_async_result_complete (res);
5119 g_error_free (error);
5120 load_contents_data_free (data);
5121 g_object_unref (res);
5126 * g_file_load_partial_contents_async:
5127 * @file: input #GFile.
5128 * @cancellable: optional #GCancellable object, %NULL to ignore.
5129 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5130 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5131 * @user_data: the data to pass to the callback functions.
5133 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5134 * used to stop reading from the file when appropriate, else this function
5135 * will behave exactly as g_file_load_contents_async(). This operation
5136 * can be finished by g_file_load_partial_contents_finish().
5138 * Users of this function should be aware that @user_data is passed to
5139 * both the @read_more_callback and the @callback.
5141 * If @cancellable is not %NULL, then the operation can be cancelled by
5142 * triggering the cancellable object from another thread. If the operation
5143 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5146 g_file_load_partial_contents_async (GFile *file,
5147 GCancellable *cancellable,
5148 GFileReadMoreCallback read_more_callback,
5149 GAsyncReadyCallback callback,
5152 LoadContentsData *data;
5154 g_return_if_fail (G_IS_FILE (file));
5156 data = g_new0 (LoadContentsData, 1);
5159 data->cancellable = g_object_ref (cancellable);
5160 data->read_more_callback = read_more_callback;
5161 data->callback = callback;
5162 data->user_data = user_data;
5163 data->content = g_byte_array_new ();
5164 data->file = g_object_ref (file);
5166 g_file_read_async (file,
5169 load_contents_open_callback,
5174 * g_file_load_partial_contents_finish:
5175 * @file: input #GFile.
5176 * @res: a #GAsyncResult.
5177 * @contents: a location to place the contents of the file.
5178 * @length: a location to place the length of the contents of the file.
5179 * @etag_out: a location to place the current entity tag for the file.
5180 * @error: a #GError, or %NULL
5182 * Finishes an asynchronous partial load operation that was started
5183 * with g_file_load_partial_contents_async().
5185 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5186 * present, it will be set appropriately.
5189 g_file_load_partial_contents_finish (GFile *file,
5196 GSimpleAsyncResult *simple;
5197 LoadContentsData *data;
5199 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5200 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5201 g_return_val_if_fail (contents != NULL, FALSE);
5203 simple = G_SIMPLE_ASYNC_RESULT (res);
5205 if (g_simple_async_result_propagate_error (simple, error))
5208 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5210 data = g_simple_async_result_get_op_res_gpointer (simple);
5214 g_propagate_error (error, data->error);
5223 *length = data->pos;
5227 *etag_out = data->etag;
5231 /* Zero terminate */
5232 g_byte_array_set_size (data->content, data->pos + 1);
5233 data->content->data[data->pos] = 0;
5235 *contents = (char *)g_byte_array_free (data->content, FALSE);
5236 data->content = NULL;
5242 * g_file_load_contents_async:
5243 * @file: input #GFile.
5244 * @cancellable: optional #GCancellable object, %NULL to ignore.
5245 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5246 * @user_data: the data to pass to callback function
5248 * Starts an asynchronous load of the @file's contents.
5250 * For more details, see g_file_load_contents() which is
5251 * the synchronous version of this call.
5253 * When the load operation has completed, @callback will be called
5254 * with @user data. To finish the operation, call
5255 * g_file_load_contents_finish() with the #GAsyncResult returned by
5258 * If @cancellable is not %NULL, then the operation can be cancelled by
5259 * triggering the cancellable object from another thread. If the operation
5260 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5263 g_file_load_contents_async (GFile *file,
5264 GCancellable *cancellable,
5265 GAsyncReadyCallback callback,
5268 g_file_load_partial_contents_async (file,
5271 callback, user_data);
5275 * g_file_load_contents_finish:
5276 * @file: input #GFile.
5277 * @res: a #GAsyncResult.
5278 * @contents: a location to place the contents of the file.
5279 * @length: a location to place the length of the contents of the file.
5280 * @etag_out: a location to place the current entity tag for the file.
5281 * @error: a #GError, or %NULL
5283 * Finishes an asynchronous load of the @file's contents.
5284 * The contents are placed in @contents, and @length is set to the
5285 * size of the @contents string. If @etag_out is present, it will be
5286 * set to the new entity tag for the @file.
5288 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5289 * present, it will be set appropriately.
5292 g_file_load_contents_finish (GFile *file,
5299 return g_file_load_partial_contents_finish (file,
5308 * g_file_replace_contents:
5309 * @file: input #GFile.
5310 * @contents: a string containing the new contents for @file.
5311 * @length: the length of @contents in bytes.
5312 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5314 * @make_backup: %TRUE if a backup should be created.
5315 * @flags: a set of #GFileCreateFlags.
5316 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5317 * for the document. This should be freed with g_free() when no longer
5319 * @cancellable: optional #GCancellable object, %NULL to ignore.
5320 * @error: a #GError, or %NULL
5322 * Replaces the contents of @file with @contents of @length bytes.
5324 * If @etag is specified (not %NULL) any existing file must have that etag, or
5325 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5327 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5329 * If @cancellable is not %NULL, then the operation can be cancelled by
5330 * triggering the cancellable object from another thread. If the operation
5331 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5333 * The returned @new_etag can be used to verify that the file hasn't changed the
5334 * next time it is saved over.
5336 * Returns: %TRUE if successful. If an error
5337 * has occurred, this function will return %FALSE and set @error
5338 * appropriately if present.
5341 g_file_replace_contents (GFile *file,
5342 const char *contents,
5345 gboolean make_backup,
5346 GFileCreateFlags flags,
5348 GCancellable *cancellable,
5351 GFileOutputStream *out;
5352 gsize pos, remainder;
5355 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5356 g_return_val_if_fail (contents != NULL, FALSE);
5358 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5364 while (remainder > 0 &&
5365 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5367 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5375 if (remainder > 0 && res < 0)
5377 /* Ignore errors on close */
5378 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5380 /* error is set already */
5384 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5388 *new_etag = g_file_output_stream_get_etag (out);
5396 GCancellable *cancellable;
5397 GAsyncReadyCallback callback;
5399 const char *content;
5403 } ReplaceContentsData;
5406 replace_contents_data_free (ReplaceContentsData *data)
5409 g_error_free (data->error);
5410 if (data->cancellable)
5411 g_object_unref (data->cancellable);
5412 g_object_unref (data->file);
5413 g_free (data->etag);
5418 replace_contents_close_callback (GObject *obj,
5419 GAsyncResult *close_res,
5422 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5423 ReplaceContentsData *data = user_data;
5424 GSimpleAsyncResult *res;
5426 /* Ignore errors here, we're only reading anyway */
5427 g_output_stream_close_finish (stream, close_res, NULL);
5428 g_object_unref (stream);
5430 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5432 res = g_simple_async_result_new (G_OBJECT (data->file),
5435 g_file_replace_contents_async);
5436 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5437 g_simple_async_result_complete (res);
5438 g_object_unref (res);
5442 replace_contents_write_callback (GObject *obj,
5443 GAsyncResult *read_res,
5446 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5447 ReplaceContentsData *data = user_data;
5448 GError *error = NULL;
5451 write_size = g_output_stream_write_finish (stream, read_res, &error);
5453 if (write_size <= 0)
5455 /* Error or EOF, close the file */
5457 data->error = error;
5458 g_output_stream_close_async (stream, 0,
5460 replace_contents_close_callback, data);
5462 else if (write_size > 0)
5464 data->pos += write_size;
5466 if (data->pos >= data->length)
5467 g_output_stream_close_async (stream, 0,
5469 replace_contents_close_callback, data);
5471 g_output_stream_write_async (stream,
5472 data->content + data->pos,
5473 data->length - data->pos,
5476 replace_contents_write_callback,
5482 replace_contents_open_callback (GObject *obj,
5483 GAsyncResult *open_res,
5486 GFile *file = G_FILE (obj);
5487 GFileOutputStream *stream;
5488 ReplaceContentsData *data = user_data;
5489 GError *error = NULL;
5490 GSimpleAsyncResult *res;
5492 stream = g_file_replace_finish (file, open_res, &error);
5496 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5497 data->content + data->pos,
5498 data->length - data->pos,
5501 replace_contents_write_callback,
5507 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5511 g_simple_async_result_complete (res);
5512 g_error_free (error);
5513 replace_contents_data_free (data);
5514 g_object_unref (res);
5519 * g_file_replace_contents_async:
5520 * @file: input #GFile.
5521 * @contents: string of contents to replace the file with.
5522 * @length: the length of @contents in bytes.
5523 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5524 * @make_backup: %TRUE if a backup should be created.
5525 * @flags: a set of #GFileCreateFlags.
5526 * @cancellable: optional #GCancellable object, %NULL to ignore.
5527 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5528 * @user_data: the data to pass to callback function
5530 * Starts an asynchronous replacement of @file with the given
5531 * @contents of @length bytes. @etag will replace the document's
5532 * current entity tag.
5534 * When this operation has completed, @callback will be called with
5535 * @user_user data, and the operation can be finalized with
5536 * g_file_replace_contents_finish().
5538 * If @cancellable is not %NULL, then the operation can be cancelled by
5539 * triggering the cancellable object from another thread. If the operation
5540 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5542 * If @make_backup is %TRUE, this function will attempt to
5543 * make a backup of @file.
5546 g_file_replace_contents_async (GFile *file,
5547 const char *contents,
5550 gboolean make_backup,
5551 GFileCreateFlags flags,
5552 GCancellable *cancellable,
5553 GAsyncReadyCallback callback,
5556 ReplaceContentsData *data;
5558 g_return_if_fail (G_IS_FILE (file));
5559 g_return_if_fail (contents != NULL);
5561 data = g_new0 (ReplaceContentsData, 1);
5564 data->cancellable = g_object_ref (cancellable);
5565 data->callback = callback;
5566 data->user_data = user_data;
5567 data->content = contents;
5568 data->length = length;
5570 data->file = g_object_ref (file);
5572 g_file_replace_async (file,
5578 replace_contents_open_callback,
5583 * g_file_replace_contents_finish:
5584 * @file: input #GFile.
5585 * @res: a #GAsyncResult.
5586 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5587 * for the document. This should be freed with g_free() when it is no
5589 * @error: a #GError, or %NULL
5591 * Finishes an asynchronous replace of the given @file. See
5592 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5593 * tag for the document, if present.
5595 * Returns: %TRUE on success, %FALSE on failure.
5598 g_file_replace_contents_finish (GFile *file,
5603 GSimpleAsyncResult *simple;
5604 ReplaceContentsData *data;
5606 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5607 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5609 simple = G_SIMPLE_ASYNC_RESULT (res);
5611 if (g_simple_async_result_propagate_error (simple, error))
5614 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5616 data = g_simple_async_result_get_op_res_gpointer (simple);
5620 g_propagate_error (error, data->error);
5628 *new_etag = data->etag;
5629 data->etag = NULL; /* Take ownership */
5635 #define __G_FILE_C__
5636 #include "gioaliasdef.c"