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 existence
905 * and then execute something based on the outcome of that, because the
906 * file might have been created or removed in between 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 existence 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,
2121 GFileQueryInfoFlags info_flags,
2122 GCancellable *cancellable,
2123 GFileProgressCallback progress_callback,
2124 gpointer progress_callback_data,
2127 gssize n_read, n_written;
2128 goffset current_size;
2129 char buffer[1024*64], *p;
2135 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2136 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2140 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2141 total_size = g_file_info_get_size (info);
2142 g_object_unref (info);
2145 if (total_size == -1)
2147 info = g_file_query_info (source,
2148 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2149 G_FILE_QUERY_INFO_NONE,
2153 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2154 total_size = g_file_info_get_size (info);
2155 g_object_unref (info);
2159 if (total_size == -1)
2166 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2176 current_size += n_read;
2181 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2182 if (n_written == -1)
2189 n_read -= n_written;
2195 if (progress_callback)
2196 progress_callback (current_size, total_size, progress_callback_data);
2200 error = NULL; /* Ignore further errors */
2202 /* Make sure we send full copied size */
2203 if (progress_callback)
2204 progress_callback (current_size, total_size, progress_callback_data);
2206 /* Don't care about errors in source here */
2207 g_input_stream_close (in, cancellable, NULL);
2209 /* But write errors on close are bad! */
2210 if (!g_output_stream_close (out, cancellable, error))
2213 g_object_unref (in);
2214 g_object_unref (out);
2220 file_copy_fallback (GFile *source,
2222 GFileCopyFlags flags,
2223 GCancellable *cancellable,
2224 GFileProgressCallback progress_callback,
2225 gpointer progress_callback_data,
2233 /* Maybe copy the symlink? */
2234 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2236 info = g_file_query_info (source,
2237 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2238 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2244 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2245 (target = g_file_info_get_symlink_target (info)) != NULL)
2247 if (!copy_symlink (destination, flags, cancellable, target, error))
2249 g_object_unref (info);
2253 g_object_unref (info);
2257 g_object_unref (info);
2260 in = open_source_for_copy (source, destination, flags, cancellable, error);
2264 if (flags & G_FILE_COPY_OVERWRITE)
2266 out = (GOutputStream *)g_file_replace (destination,
2268 flags & G_FILE_COPY_BACKUP,
2269 cancellable, error);
2273 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2278 g_object_unref (in);
2282 if (!copy_stream_with_progress (in, out, source, cancellable,
2283 progress_callback, progress_callback_data,
2289 /* Ignore errors here. Failure to copy metadata is not a hard error */
2290 g_file_copy_attributes (source, destination,
2291 flags, cancellable, NULL);
2298 * @source: input #GFile.
2299 * @destination: destination #GFile
2300 * @flags: set of #GFileCopyFlags
2301 * @cancellable: optional #GCancellable object, %NULL to ignore.
2302 * @progress_callback: function to callback with progress information
2303 * @progress_callback_data: user data to pass to @progress_callback
2304 * @error: #GError to set on error, or %NULL
2306 * Copies the file @source to the location specified by @destination.
2307 * Can not handle recursive copies of directories.
2309 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2310 * existing @destination file is overwritten.
2312 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2313 * will be copied as symlinks, otherwise the target of the
2314 * @source symlink will be copied.
2316 * If @cancellable is not %NULL, then the operation can be cancelled by
2317 * triggering the cancellable object from another thread. If the operation
2318 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2320 * If @progress_callback is not %NULL, then the operation can be monitored by
2321 * setting this to a #GFileProgressCallback function. @progress_callback_data
2322 * will be passed to this function. It is guaranteed that this callback will
2323 * be called after all data has been transferred with the total number of bytes
2324 * copied during the operation.
2326 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2327 * error is returned, independent on the status of the @destination.
2329 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2330 * error G_IO_ERROR_EXISTS is returned.
2332 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2333 * error is returned. If trying to overwrite a directory with a directory the
2334 * G_IO_ERROR_WOULD_MERGE error is returned.
2336 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2337 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2340 * If you are interested in copying the #GFile object itself (not the on-disk
2341 * file), see g_file_dup().
2343 * Returns: %TRUE on success, %FALSE otherwise.
2346 g_file_copy (GFile *source,
2348 GFileCopyFlags flags,
2349 GCancellable *cancellable,
2350 GFileProgressCallback progress_callback,
2351 gpointer progress_callback_data,
2358 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2359 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2361 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2364 iface = G_FILE_GET_IFACE (destination);
2368 res = (* iface->copy) (source, destination,
2370 progress_callback, progress_callback_data,
2376 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2378 g_propagate_error (error, my_error);
2383 /* If the types are different, and the destination method failed
2384 also try the source method */
2385 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2387 iface = G_FILE_GET_IFACE (source);
2392 res = (* iface->copy) (source, destination,
2394 progress_callback, progress_callback_data,
2400 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2402 g_propagate_error (error, my_error);
2408 return file_copy_fallback (source, destination, flags, cancellable,
2409 progress_callback, progress_callback_data,
2414 * g_file_copy_async:
2415 * @source: input #GFile.
2416 * @destination: destination #GFile
2417 * @flags: set of #GFileCopyFlags
2418 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2420 * @cancellable: optional #GCancellable object, %NULL to ignore.
2421 * @progress_callback: function to callback with progress information
2422 * @progress_callback_data: user data to pass to @progress_callback
2423 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2424 * @user_data: the data to pass to callback function
2426 * Copies the file @source to the location specified by @destination
2427 * asynchronously. For details of the behaviour, see g_file_copy().
2429 * If @progress_callback is not %NULL, then that function that will be called
2430 * just like in g_file_copy(), however the callback will run in the main loop,
2431 * not in the thread that is doing the I/O operation.
2433 * When the operation is finished, @callback will be called. You can then call
2434 * g_file_copy_finish() to get the result of the operation.
2437 g_file_copy_async (GFile *source,
2439 GFileCopyFlags flags,
2441 GCancellable *cancellable,
2442 GFileProgressCallback progress_callback,
2443 gpointer progress_callback_data,
2444 GAsyncReadyCallback callback,
2449 g_return_if_fail (G_IS_FILE (source));
2450 g_return_if_fail (G_IS_FILE (destination));
2452 iface = G_FILE_GET_IFACE (source);
2453 (* iface->copy_async) (source,
2459 progress_callback_data,
2465 * g_file_copy_finish:
2466 * @file: input #GFile.
2467 * @res: a #GAsyncResult.
2468 * @error: a #GError, or %NULL
2470 * Finishes copying the file started with
2471 * g_file_copy_async().
2473 * Returns: a %TRUE on success, %FALSE on error.
2476 g_file_copy_finish (GFile *file,
2482 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2483 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2485 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2487 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2489 if (g_simple_async_result_propagate_error (simple, error))
2493 iface = G_FILE_GET_IFACE (file);
2494 return (* iface->copy_finish) (file, res, error);
2499 * @source: #GFile pointing to the source location.
2500 * @destination: #GFile pointing to the destination location.
2501 * @flags: set of #GFileCopyFlags.
2502 * @cancellable: optional #GCancellable object, %NULL to ignore.
2503 * @progress_callback: #GFileProgressCallback function for updates.
2504 * @progress_callback_data: gpointer to user data for the callback function.
2505 * @error: #GError for returning error conditions, or %NULL
2508 * Tries to move the file or directory @source to the location specified by @destination.
2509 * If native move operations are supported then this is used, otherwise a copy + delete
2510 * fallback is used. The native implementation may support moving directories (for instance
2511 * on moves inside the same filesystem), but the fallback code does not.
2513 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2514 * existing @destination file is overwritten.
2516 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2517 * will be copied as symlinks, otherwise the target of the
2518 * @source symlink will be copied.
2520 * If @cancellable is not %NULL, then the operation can be cancelled by
2521 * triggering the cancellable object from another thread. If the operation
2522 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2524 * If @progress_callback is not %NULL, then the operation can be monitored by
2525 * setting this to a #GFileProgressCallback function. @progress_callback_data
2526 * will be passed to this function. It is guaranteed that this callback will
2527 * be called after all data has been transferred with the total number of bytes
2528 * copied during the operation.
2530 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2531 * error is returned, independent on the status of the @destination.
2533 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2534 * error G_IO_ERROR_EXISTS is returned.
2536 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2537 * error is returned. If trying to overwrite a directory with a directory the
2538 * G_IO_ERROR_WOULD_MERGE error is returned.
2540 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2541 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2542 * may be returned (if the native move operation isn't available).
2544 * Returns: %TRUE on successful move, %FALSE otherwise.
2547 g_file_move (GFile *source,
2549 GFileCopyFlags flags,
2550 GCancellable *cancellable,
2551 GFileProgressCallback progress_callback,
2552 gpointer progress_callback_data,
2559 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2560 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2562 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2565 iface = G_FILE_GET_IFACE (destination);
2569 res = (* iface->move) (source, destination,
2571 progress_callback, progress_callback_data,
2577 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2579 g_propagate_error (error, my_error);
2584 /* If the types are different, and the destination method failed
2585 also try the source method */
2586 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2588 iface = G_FILE_GET_IFACE (source);
2593 res = (* iface->move) (source, destination,
2595 progress_callback, progress_callback_data,
2601 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2603 g_propagate_error (error, my_error);
2609 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2611 g_set_error (error, G_IO_ERROR,
2612 G_IO_ERROR_NOT_SUPPORTED,
2613 _("Operation not supported"));
2617 flags |= G_FILE_COPY_ALL_METADATA;
2618 if (!g_file_copy (source, destination, flags, cancellable,
2619 progress_callback, progress_callback_data,
2623 return g_file_delete (source, cancellable, error);
2627 * g_file_make_directory
2628 * @file: input #GFile.
2629 * @cancellable: optional #GCancellable object, %NULL to ignore.
2630 * @error: a #GError, or %NULL
2632 * Creates a directory.
2634 * If @cancellable is not %NULL, then the operation can be cancelled by
2635 * triggering the cancellable object from another thread. If the operation
2636 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2638 * Returns: %TRUE on successful creation, %FALSE otherwise.
2641 g_file_make_directory (GFile *file,
2642 GCancellable *cancellable,
2647 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2649 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2652 iface = G_FILE_GET_IFACE (file);
2654 if (iface->make_directory == NULL)
2656 g_set_error (error, G_IO_ERROR,
2657 G_IO_ERROR_NOT_SUPPORTED,
2658 _("Operation not supported"));
2662 return (* iface->make_directory) (file, cancellable, error);
2666 * g_file_make_symbolic_link:
2667 * @file: input #GFile.
2668 * @symlink_value: a string with the value of the new symlink.
2669 * @cancellable: optional #GCancellable object, %NULL to ignore.
2670 * @error: a #GError.
2672 * Creates a symbolic link.
2674 * If @cancellable is not %NULL, then the operation can be cancelled by
2675 * triggering the cancellable object from another thread. If the operation
2676 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2678 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2681 g_file_make_symbolic_link (GFile *file,
2682 const char *symlink_value,
2683 GCancellable *cancellable,
2688 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2689 g_return_val_if_fail (symlink_value != NULL, FALSE);
2691 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2694 if (*symlink_value == '\0')
2696 g_set_error (error, G_IO_ERROR,
2697 G_IO_ERROR_INVALID_ARGUMENT,
2698 _("Invalid symlink value given"));
2702 iface = G_FILE_GET_IFACE (file);
2704 if (iface->make_symbolic_link == NULL)
2706 g_set_error (error, G_IO_ERROR,
2707 G_IO_ERROR_NOT_SUPPORTED,
2708 _("Operation not supported"));
2712 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2717 * @file: input #GFile.
2718 * @cancellable: optional #GCancellable object, %NULL to ignore.
2719 * @error: a #GError, or %NULL
2721 * Deletes a file. If the @file is a directory, it will only be deleted if it
2724 * If @cancellable is not %NULL, then the operation can be cancelled by
2725 * triggering the cancellable object from another thread. If the operation
2726 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2728 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2731 g_file_delete (GFile *file,
2732 GCancellable *cancellable,
2737 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2739 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2742 iface = G_FILE_GET_IFACE (file);
2744 if (iface->delete_file == NULL)
2746 g_set_error (error, G_IO_ERROR,
2747 G_IO_ERROR_NOT_SUPPORTED,
2748 _("Operation not supported"));
2752 return (* iface->delete_file) (file, cancellable, error);
2757 * @file: #GFile to send to trash.
2758 * @cancellable: optional #GCancellable object, %NULL to ignore.
2759 * @error: a #GError, or %NULL
2761 * Sends @file to the "Trashcan", if possible. This is similar to
2762 * deleting it, but the user can recover it before emptying the trashcan.
2763 * Not all file systems support trashing, so this call can return the
2764 * %G_IO_ERROR_NOT_SUPPORTED error.
2767 * If @cancellable is not %NULL, then the operation can be cancelled by
2768 * triggering the cancellable object from another thread. If the operation
2769 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2771 * Returns: %TRUE on successful trash, %FALSE otherwise.
2774 g_file_trash (GFile *file,
2775 GCancellable *cancellable,
2780 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2782 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2785 iface = G_FILE_GET_IFACE (file);
2787 if (iface->trash == NULL)
2790 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2791 _("Trash not supported"));
2795 return (* iface->trash) (file, cancellable, error);
2799 * g_file_set_display_name:
2800 * @file: input #GFile.
2801 * @display_name: a string.
2802 * @cancellable: optional #GCancellable object, %NULL to ignore.
2803 * @error: a #GError, or %NULL
2805 * Renames @file to the specified display name.
2807 * The display name is converted from UTF8 to the correct encoding for the target
2808 * filesystem if possible and the @file is renamed to this.
2810 * If you want to implement a rename operation in the user interface the edit name
2811 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2812 * widget, and then the result after editing should be passed to g_file_set_display_name().
2814 * On success the resulting converted filename is returned.
2816 * If @cancellable is not %NULL, then the operation can be cancelled by
2817 * triggering the cancellable object from another thread. If the operation
2818 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2820 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2823 g_file_set_display_name (GFile *file,
2824 const char *display_name,
2825 GCancellable *cancellable,
2830 g_return_val_if_fail (G_IS_FILE (file), NULL);
2831 g_return_val_if_fail (display_name != NULL, NULL);
2833 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2837 G_IO_ERROR_INVALID_ARGUMENT,
2838 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2842 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2845 iface = G_FILE_GET_IFACE (file);
2847 return (* iface->set_display_name) (file, display_name, cancellable, error);
2851 * g_file_set_display_name_async:
2852 * @file: input #GFile.
2853 * @display_name: a string.
2854 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2856 * @cancellable: optional #GCancellable object, %NULL to ignore.
2857 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2858 * @user_data: the data to pass to callback function
2860 * Asynchronously sets the display name for a given #GFile.
2862 * For more details, see g_set_display_name() which is
2863 * the synchronous version of this call.
2865 * When the operation is finished, @callback will be called. You can then call
2866 * g_file_set_display_name_finish() to get the result of the operation.
2869 g_file_set_display_name_async (GFile *file,
2870 const char *display_name,
2872 GCancellable *cancellable,
2873 GAsyncReadyCallback callback,
2878 g_return_if_fail (G_IS_FILE (file));
2879 g_return_if_fail (display_name != NULL);
2881 iface = G_FILE_GET_IFACE (file);
2882 (* iface->set_display_name_async) (file,
2891 * g_file_set_display_name_finish:
2892 * @file: input #GFile.
2893 * @res: a #GAsyncResult.
2894 * @error: a #GError, or %NULL
2896 * Finishes setting a display name started with
2897 * g_file_set_display_name_async().
2899 * Returns: a #GFile or %NULL on error.
2902 g_file_set_display_name_finish (GFile *file,
2908 g_return_val_if_fail (G_IS_FILE (file), NULL);
2909 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2911 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2913 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2914 if (g_simple_async_result_propagate_error (simple, error))
2918 iface = G_FILE_GET_IFACE (file);
2919 return (* iface->set_display_name_finish) (file, res, error);
2923 * g_file_query_settable_attributes:
2924 * @file: input #GFile.
2925 * @cancellable: optional #GCancellable object, %NULL to ignore.
2926 * @error: a #GError, or %NULL
2928 * Obtain the list of settable attributes for the file.
2930 * Returns the type and full attribute name of all the attributes
2931 * that can be set on this file. This doesn't mean setting it will always
2932 * succeed though, you might get an access failure, or some specific
2933 * file may not support a specific attribute.
2935 * If @cancellable is not %NULL, then the operation can be cancelled by
2936 * triggering the cancellable object from another thread. If the operation
2937 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2939 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2940 * When you are done with it, release it with g_file_attribute_info_list_unref()
2942 GFileAttributeInfoList *
2943 g_file_query_settable_attributes (GFile *file,
2944 GCancellable *cancellable,
2949 GFileAttributeInfoList *list;
2951 g_return_val_if_fail (G_IS_FILE (file), NULL);
2953 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2956 iface = G_FILE_GET_IFACE (file);
2958 if (iface->query_settable_attributes == NULL)
2959 return g_file_attribute_info_list_new ();
2962 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2966 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2968 list = g_file_attribute_info_list_new ();
2969 g_error_free (my_error);
2972 g_propagate_error (error, my_error);
2979 * g_file_query_writable_namespaces:
2980 * @file: input #GFile.
2981 * @cancellable: optional #GCancellable object, %NULL to ignore.
2982 * @error: a #GError, or %NULL
2984 * Obtain the list of attribute namespaces where new attributes
2985 * can be created by a user. An example of this is extended
2986 * attributes (in the "xattr" namespace).
2988 * If @cancellable is not %NULL, then the operation can be cancelled by
2989 * triggering the cancellable object from another thread. If the operation
2990 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2992 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2993 * When you are done with it, release it with g_file_attribute_info_list_unref()
2995 GFileAttributeInfoList *
2996 g_file_query_writable_namespaces (GFile *file,
2997 GCancellable *cancellable,
3002 GFileAttributeInfoList *list;
3004 g_return_val_if_fail (G_IS_FILE (file), NULL);
3006 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3009 iface = G_FILE_GET_IFACE (file);
3011 if (iface->query_writable_namespaces == NULL)
3012 return g_file_attribute_info_list_new ();
3015 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3019 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3021 list = g_file_attribute_info_list_new ();
3022 g_error_free (my_error);
3025 g_propagate_error (error, my_error);
3032 * g_file_set_attribute:
3033 * @file: input #GFile.
3034 * @attribute: a string containing the attribute's name.
3035 * @type: The type of the attribute
3036 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3037 * @flags: a set of #GFileQueryInfoFlags.
3038 * @cancellable: optional #GCancellable object, %NULL to ignore.
3039 * @error: a #GError, or %NULL
3041 * Sets an attribute in the file with attribute name @attribute to @value.
3043 * If @cancellable is not %NULL, then the operation can be cancelled by
3044 * triggering the cancellable object from another thread. If the operation
3045 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3047 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3050 g_file_set_attribute (GFile *file,
3051 const char *attribute,
3052 GFileAttributeType type,
3054 GFileQueryInfoFlags flags,
3055 GCancellable *cancellable,
3060 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3061 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3063 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3066 iface = G_FILE_GET_IFACE (file);
3068 if (iface->set_attribute == NULL)
3070 g_set_error (error, G_IO_ERROR,
3071 G_IO_ERROR_NOT_SUPPORTED,
3072 _("Operation not supported"));
3076 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3080 * g_file_set_attributes_from_info:
3081 * @file: input #GFile.
3082 * @info: a #GFileInfo.
3083 * @flags: #GFileQueryInfoFlags
3084 * @cancellable: optional #GCancellable object, %NULL to ignore.
3085 * @error: a #GError, or %NULL
3087 * Tries to set all attributes in the #GFileInfo on the target values,
3088 * not stopping on the first error.
3090 * If there is any error during this operation then @error will be set to
3091 * the first error. Error on particular fields are flagged by setting
3092 * the "status" field in the attribute value to
3093 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3096 * If @cancellable is not %NULL, then the operation can be cancelled by
3097 * triggering the cancellable object from another thread. If the operation
3098 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3100 * Returns: %TRUE if there was any error, %FALSE otherwise.
3103 g_file_set_attributes_from_info (GFile *file,
3105 GFileQueryInfoFlags flags,
3106 GCancellable *cancellable,
3111 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3112 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3114 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3117 g_file_info_clear_status (info);
3119 iface = G_FILE_GET_IFACE (file);
3121 return (* iface->set_attributes_from_info) (file,
3130 g_file_real_set_attributes_from_info (GFile *file,
3132 GFileQueryInfoFlags flags,
3133 GCancellable *cancellable,
3139 GFileAttributeValue *value;
3143 attributes = g_file_info_list_attributes (info, NULL);
3145 for (i = 0; attributes[i] != NULL; i++)
3147 value = _g_file_info_get_attribute_value (info, attributes[i]);
3149 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3152 if (!g_file_set_attribute (file, attributes[i],
3153 value->type, _g_file_attribute_value_peek_as_pointer (value),
3154 flags, cancellable, error))
3156 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3158 /* Don't set error multiple times */
3162 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3165 g_strfreev (attributes);
3171 * g_file_set_attributes_async:
3172 * @file: input #GFile.
3173 * @info: a #GFileInfo.
3174 * @flags: a #GFileQueryInfoFlags.
3175 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3177 * @cancellable: optional #GCancellable object, %NULL to ignore.
3178 * @callback: a #GAsyncReadyCallback.
3179 * @user_data: a #gpointer.
3181 * Asynchronously sets the attributes of @file with @info.
3183 * For more details, see g_file_set_attributes_from_info() which is
3184 * the synchronous version of this call.
3186 * When the operation is finished, @callback will be called. You can then call
3187 * g_file_set_attributes_finish() to get the result of the operation.
3190 g_file_set_attributes_async (GFile *file,
3192 GFileQueryInfoFlags flags,
3194 GCancellable *cancellable,
3195 GAsyncReadyCallback callback,
3200 g_return_if_fail (G_IS_FILE (file));
3201 g_return_if_fail (G_IS_FILE_INFO (info));
3203 iface = G_FILE_GET_IFACE (file);
3204 (* iface->set_attributes_async) (file,
3214 * g_file_set_attributes_finish:
3215 * @file: input #GFile.
3216 * @result: a #GAsyncResult.
3217 * @info: a #GFileInfo.
3218 * @error: a #GError, or %NULL
3220 * Finishes setting an attribute started in g_file_set_attributes_async().
3222 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3225 g_file_set_attributes_finish (GFile *file,
3226 GAsyncResult *result,
3232 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3233 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3235 /* No standard handling of errors here, as we must set info even
3238 iface = G_FILE_GET_IFACE (file);
3239 return (* iface->set_attributes_finish) (file, result, info, error);
3243 * g_file_set_attribute_string:
3244 * @file: input #GFile.
3245 * @attribute: a string containing the attribute's name.
3246 * @value: a string containing the attribute's value.
3247 * @flags: #GFileQueryInfoFlags.
3248 * @cancellable: optional #GCancellable object, %NULL to ignore.
3249 * @error: a #GError, or %NULL
3251 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3252 * If @attribute is of a different type, this operation will fail.
3254 * If @cancellable is not %NULL, then the operation can be cancelled by
3255 * triggering the cancellable object from another thread. If the operation
3256 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3258 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3261 g_file_set_attribute_string (GFile *file,
3262 const char *attribute,
3264 GFileQueryInfoFlags flags,
3265 GCancellable *cancellable,
3268 return g_file_set_attribute (file, attribute,
3269 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3270 flags, cancellable, error);
3274 * g_file_set_attribute_byte_string:
3275 * @file: input #GFile.
3276 * @attribute: a string containing the attribute's name.
3277 * @value: a string containing the attribute's new value.
3278 * @flags: a #GFileQueryInfoFlags.
3279 * @cancellable: optional #GCancellable object, %NULL to ignore.
3280 * @error: a #GError, or %NULL
3282 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3283 * If @attribute is of a different type, this operation will fail,
3286 * If @cancellable is not %NULL, then the operation can be cancelled by
3287 * triggering the cancellable object from another thread. If the operation
3288 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3290 * Returns: %TRUE if the @attribute was successfully set to @value
3291 * in the @file, %FALSE otherwise.
3294 g_file_set_attribute_byte_string (GFile *file,
3295 const char *attribute,
3297 GFileQueryInfoFlags flags,
3298 GCancellable *cancellable,
3301 return g_file_set_attribute (file, attribute,
3302 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3303 flags, cancellable, error);
3307 * g_file_set_attribute_uint32:
3308 * @file: input #GFile.
3309 * @attribute: a string containing the attribute's name.
3310 * @value: a #guint32 containing the attribute's new value.
3311 * @flags: a #GFileQueryInfoFlags.
3312 * @cancellable: optional #GCancellable object, %NULL to ignore.
3313 * @error: a #GError, or %NULL
3315 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3316 * If @attribute is of a different type, this operation will fail.
3318 * If @cancellable is not %NULL, then the operation can be cancelled by
3319 * triggering the cancellable object from another thread. If the operation
3320 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3322 * Returns: %TRUE if the @attribute was successfully set to @value
3323 * in the @file, %FALSE otherwise.
3326 g_file_set_attribute_uint32 (GFile *file,
3327 const char *attribute,
3329 GFileQueryInfoFlags flags,
3330 GCancellable *cancellable,
3333 return g_file_set_attribute (file, attribute,
3334 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3335 flags, cancellable, error);
3339 * g_file_set_attribute_int32:
3340 * @file: input #GFile.
3341 * @attribute: a string containing the attribute's name.
3342 * @value: a #gint32 containing the attribute's new value.
3343 * @flags: a #GFileQueryInfoFlags.
3344 * @cancellable: optional #GCancellable object, %NULL to ignore.
3345 * @error: a #GError, or %NULL
3347 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3348 * If @attribute is of a different type, this operation will fail.
3350 * If @cancellable is not %NULL, then the operation can be cancelled by
3351 * triggering the cancellable object from another thread. If the operation
3352 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3354 * Returns: %TRUE if the @attribute was successfully set to @value
3355 * in the @file, %FALSE otherwise.
3358 g_file_set_attribute_int32 (GFile *file,
3359 const char *attribute,
3361 GFileQueryInfoFlags flags,
3362 GCancellable *cancellable,
3365 return g_file_set_attribute (file, attribute,
3366 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3367 flags, cancellable, error);
3371 * g_file_set_attribute_uint64:
3372 * @file: input #GFile.
3373 * @attribute: a string containing the attribute's name.
3374 * @value: a #guint64 containing the attribute's new value.
3375 * @flags: a #GFileQueryInfoFlags.
3376 * @cancellable: optional #GCancellable object, %NULL to ignore.
3377 * @error: a #GError, or %NULL
3379 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3380 * If @attribute is of a different type, this operation will fail.
3382 * If @cancellable is not %NULL, then the operation can be cancelled by
3383 * triggering the cancellable object from another thread. If the operation
3384 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3386 * Returns: %TRUE if the @attribute was successfully set to @value
3387 * in the @file, %FALSE otherwise.
3390 g_file_set_attribute_uint64 (GFile *file,
3391 const char *attribute,
3393 GFileQueryInfoFlags flags,
3394 GCancellable *cancellable,
3397 return g_file_set_attribute (file, attribute,
3398 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3399 flags, cancellable, error);
3403 * g_file_set_attribute_int64:
3404 * @file: input #GFile.
3405 * @attribute: a string containing the attribute's name.
3406 * @value: a #guint64 containing the attribute's new value.
3407 * @flags: a #GFileQueryInfoFlags.
3408 * @cancellable: optional #GCancellable object, %NULL to ignore.
3409 * @error: a #GError, or %NULL
3411 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3412 * If @attribute is of a different type, this operation will fail.
3414 * If @cancellable is not %NULL, then the operation can be cancelled by
3415 * triggering the cancellable object from another thread. If the operation
3416 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3418 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3421 g_file_set_attribute_int64 (GFile *file,
3422 const char *attribute,
3424 GFileQueryInfoFlags flags,
3425 GCancellable *cancellable,
3428 return g_file_set_attribute (file, attribute,
3429 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3430 flags, cancellable, error);
3434 * g_file_mount_mountable:
3435 * @file: input #GFile.
3436 * @flags: flags affecting the operation
3437 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3438 * @cancellable: optional #GCancellable object, %NULL to ignore.
3439 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3440 * @user_data: the data to pass to callback function
3442 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3443 * Using @mount_operation, you can request callbacks when, for instance,
3444 * passwords are needed during authentication.
3446 * If @cancellable is not %NULL, then the operation can be cancelled by
3447 * triggering the cancellable object from another thread. If the operation
3448 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3450 * When the operation is finished, @callback will be called. You can then call
3451 * g_file_mount_mountable_finish() to get the result of the operation.
3454 g_file_mount_mountable (GFile *file,
3455 GMountMountFlags flags,
3456 GMountOperation *mount_operation,
3457 GCancellable *cancellable,
3458 GAsyncReadyCallback callback,
3463 g_return_if_fail (G_IS_FILE (file));
3465 iface = G_FILE_GET_IFACE (file);
3467 if (iface->mount_mountable == NULL)
3469 g_simple_async_report_error_in_idle (G_OBJECT (file),
3473 G_IO_ERROR_NOT_SUPPORTED,
3474 _("Operation not supported"));
3478 (* iface->mount_mountable) (file,
3487 * g_file_mount_mountable_finish:
3488 * @file: input #GFile.
3489 * @result: a #GAsyncResult.
3490 * @error: a #GError, or %NULL
3492 * Finishes a mount operation. See g_file_mount_mountable() for details.
3494 * Finish an asynchronous mount operation that was started
3495 * with g_file_mount_mountable().
3497 * Returns: a #GFile or %NULL on error.
3500 g_file_mount_mountable_finish (GFile *file,
3501 GAsyncResult *result,
3506 g_return_val_if_fail (G_IS_FILE (file), NULL);
3507 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3509 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3511 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3512 if (g_simple_async_result_propagate_error (simple, error))
3516 iface = G_FILE_GET_IFACE (file);
3517 return (* iface->mount_mountable_finish) (file, result, error);
3521 * g_file_unmount_mountable:
3522 * @file: input #GFile.
3523 * @flags: flags affecting the operation
3524 * @cancellable: optional #GCancellable object, %NULL to ignore.
3525 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3526 * @user_data: the data to pass to callback function
3528 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3530 * If @cancellable is not %NULL, then the operation can be cancelled by
3531 * triggering the cancellable object from another thread. If the operation
3532 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3534 * When the operation is finished, @callback will be called. You can then call
3535 * g_file_unmount_mountable_finish() to get the result of the operation.
3538 g_file_unmount_mountable (GFile *file,
3539 GMountUnmountFlags flags,
3540 GCancellable *cancellable,
3541 GAsyncReadyCallback callback,
3546 g_return_if_fail (G_IS_FILE (file));
3548 iface = G_FILE_GET_IFACE (file);
3550 if (iface->unmount_mountable == NULL)
3552 g_simple_async_report_error_in_idle (G_OBJECT (file),
3556 G_IO_ERROR_NOT_SUPPORTED,
3557 _("Operation not supported"));
3561 (* iface->unmount_mountable) (file,
3569 * g_file_unmount_mountable_finish:
3570 * @file: input #GFile.
3571 * @result: a #GAsyncResult.
3572 * @error: a #GError, or %NULL
3574 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3576 * Finish an asynchronous unmount operation that was started
3577 * with g_file_unmount_mountable().
3579 * Returns: %TRUE if the operation finished successfully. %FALSE
3583 g_file_unmount_mountable_finish (GFile *file,
3584 GAsyncResult *result,
3589 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3590 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3592 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3594 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3595 if (g_simple_async_result_propagate_error (simple, error))
3599 iface = G_FILE_GET_IFACE (file);
3600 return (* iface->unmount_mountable_finish) (file, result, error);
3604 * g_file_eject_mountable:
3605 * @file: input #GFile.
3606 * @flags: flags affecting the operation
3607 * @cancellable: optional #GCancellable object, %NULL to ignore.
3608 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3609 * @user_data: the data to pass to callback function
3611 * Starts an asynchronous eject on a mountable.
3612 * When this operation has completed, @callback will be called with
3613 * @user_user data, and the operation can be finalized with
3614 * g_file_eject_mountable_finish().
3616 * If @cancellable is not %NULL, then the operation can be cancelled by
3617 * triggering the cancellable object from another thread. If the operation
3618 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3621 g_file_eject_mountable (GFile *file,
3622 GMountUnmountFlags flags,
3623 GCancellable *cancellable,
3624 GAsyncReadyCallback callback,
3629 g_return_if_fail (G_IS_FILE (file));
3631 iface = G_FILE_GET_IFACE (file);
3633 if (iface->eject_mountable == NULL)
3635 g_simple_async_report_error_in_idle (G_OBJECT (file),
3639 G_IO_ERROR_NOT_SUPPORTED,
3640 _("Operation not supported"));
3644 (* iface->eject_mountable) (file,
3652 * g_file_eject_mountable_finish:
3653 * @file: input #GFile.
3654 * @result: a #GAsyncResult.
3655 * @error: a #GError, or %NULL
3657 * Finishes an asynchronous eject operation started by
3658 * g_file_eject_mountable().
3660 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3664 g_file_eject_mountable_finish (GFile *file,
3665 GAsyncResult *result,
3670 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3671 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3673 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3675 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3676 if (g_simple_async_result_propagate_error (simple, error))
3680 iface = G_FILE_GET_IFACE (file);
3681 return (* iface->eject_mountable_finish) (file, result, error);
3685 * g_file_monitor_directory:
3686 * @file: input #GFile.
3687 * @flags: a set of #GFileMonitorFlags.
3688 * @cancellable: optional #GCancellable object, %NULL to ignore.
3689 * @error: a #GError, or %NULL.
3691 * Obtains a directory monitor for the given file.
3692 * This may fail if directory monitoring is not supported.
3694 * If @cancellable is not %NULL, then the operation can be cancelled by
3695 * triggering the cancellable object from another thread. If the operation
3696 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3698 * Returns: a #GFileMonitor for the given @file,
3699 * or %NULL on error.
3702 g_file_monitor_directory (GFile *file,
3703 GFileMonitorFlags flags,
3704 GCancellable *cancellable,
3709 g_return_val_if_fail (G_IS_FILE (file), NULL);
3711 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3714 iface = G_FILE_GET_IFACE (file);
3716 if (iface->monitor_dir == NULL)
3718 g_set_error (error, G_IO_ERROR,
3719 G_IO_ERROR_NOT_SUPPORTED,
3720 _("Operation not supported"));
3724 return (* iface->monitor_dir) (file, flags, cancellable, error);
3728 * g_file_monitor_file:
3729 * @file: input #GFile.
3730 * @flags: a set of #GFileMonitorFlags.
3731 * @cancellable: optional #GCancellable object, %NULL to ignore.
3732 * @error: a #GError, or %NULL.
3734 * Obtains a file monitor for the given file. If no file notification
3735 * mechanism exists, then regular polling of the file is used.
3737 * If @cancellable is not %NULL, then the operation can be cancelled by
3738 * triggering the cancellable object from another thread. If the operation
3739 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3741 * Returns: a #GFileMonitor for the given @file.
3744 g_file_monitor_file (GFile *file,
3745 GFileMonitorFlags flags,
3746 GCancellable *cancellable,
3750 GFileMonitor *monitor;
3752 g_return_val_if_fail (G_IS_FILE (file), NULL);
3754 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3757 iface = G_FILE_GET_IFACE (file);
3761 if (iface->monitor_file)
3762 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3764 /* Fallback to polling */
3765 if (monitor == NULL)
3766 monitor = _g_poll_file_monitor_new (file);
3771 /********************************************
3772 * Default implementation of async ops *
3773 ********************************************/
3777 GFileQueryInfoFlags flags;
3779 } QueryInfoAsyncData;
3782 query_info_data_free (QueryInfoAsyncData *data)
3785 g_object_unref (data->info);
3786 g_free (data->attributes);
3791 query_info_async_thread (GSimpleAsyncResult *res,
3793 GCancellable *cancellable)
3795 GError *error = NULL;
3796 QueryInfoAsyncData *data;
3799 data = g_simple_async_result_get_op_res_gpointer (res);
3801 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3805 g_simple_async_result_set_from_error (res, error);
3806 g_error_free (error);
3813 g_file_real_query_info_async (GFile *file,
3814 const char *attributes,
3815 GFileQueryInfoFlags flags,
3817 GCancellable *cancellable,
3818 GAsyncReadyCallback callback,
3821 GSimpleAsyncResult *res;
3822 QueryInfoAsyncData *data;
3824 data = g_new0 (QueryInfoAsyncData, 1);
3825 data->attributes = g_strdup (attributes);
3826 data->flags = flags;
3828 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3829 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3831 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3832 g_object_unref (res);
3836 g_file_real_query_info_finish (GFile *file,
3840 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3841 QueryInfoAsyncData *data;
3843 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3845 data = g_simple_async_result_get_op_res_gpointer (simple);
3847 return g_object_ref (data->info);
3855 } QueryFilesystemInfoAsyncData;
3858 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
3861 g_object_unref (data->info);
3862 g_free (data->attributes);
3867 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
3869 GCancellable *cancellable)
3871 GError *error = NULL;
3872 QueryFilesystemInfoAsyncData *data;
3875 data = g_simple_async_result_get_op_res_gpointer (res);
3877 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
3881 g_simple_async_result_set_from_error (res, error);
3882 g_error_free (error);
3889 g_file_real_query_filesystem_info_async (GFile *file,
3890 const char *attributes,
3892 GCancellable *cancellable,
3893 GAsyncReadyCallback callback,
3896 GSimpleAsyncResult *res;
3897 QueryFilesystemInfoAsyncData *data;
3899 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
3900 data->attributes = g_strdup (attributes);
3902 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
3903 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
3905 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
3906 g_object_unref (res);
3910 g_file_real_query_filesystem_info_finish (GFile *file,
3914 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3915 QueryFilesystemInfoAsyncData *data;
3917 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
3919 data = g_simple_async_result_get_op_res_gpointer (simple);
3921 return g_object_ref (data->info);
3928 GFileQueryInfoFlags flags;
3929 GFileEnumerator *enumerator;
3930 } EnumerateChildrenAsyncData;
3933 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3935 if (data->enumerator)
3936 g_object_unref (data->enumerator);
3937 g_free (data->attributes);
3942 enumerate_children_async_thread (GSimpleAsyncResult *res,
3944 GCancellable *cancellable)
3946 GError *error = NULL;
3947 EnumerateChildrenAsyncData *data;
3948 GFileEnumerator *enumerator;
3950 data = g_simple_async_result_get_op_res_gpointer (res);
3952 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3954 if (enumerator == NULL)
3956 g_simple_async_result_set_from_error (res, error);
3957 g_error_free (error);
3960 data->enumerator = enumerator;
3964 g_file_real_enumerate_children_async (GFile *file,
3965 const char *attributes,
3966 GFileQueryInfoFlags flags,
3968 GCancellable *cancellable,
3969 GAsyncReadyCallback callback,
3972 GSimpleAsyncResult *res;
3973 EnumerateChildrenAsyncData *data;
3975 data = g_new0 (EnumerateChildrenAsyncData, 1);
3976 data->attributes = g_strdup (attributes);
3977 data->flags = flags;
3979 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3980 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3982 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3983 g_object_unref (res);
3986 static GFileEnumerator *
3987 g_file_real_enumerate_children_finish (GFile *file,
3991 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3992 EnumerateChildrenAsyncData *data;
3994 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3996 data = g_simple_async_result_get_op_res_gpointer (simple);
3997 if (data->enumerator)
3998 return g_object_ref (data->enumerator);
4004 open_read_async_thread (GSimpleAsyncResult *res,
4006 GCancellable *cancellable)
4009 GFileInputStream *stream;
4010 GError *error = NULL;
4012 iface = G_FILE_GET_IFACE (object);
4014 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4018 g_simple_async_result_set_from_error (res, error);
4019 g_error_free (error);
4022 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4026 g_file_real_read_async (GFile *file,
4028 GCancellable *cancellable,
4029 GAsyncReadyCallback callback,
4032 GSimpleAsyncResult *res;
4034 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4036 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4037 g_object_unref (res);
4040 static GFileInputStream *
4041 g_file_real_read_finish (GFile *file,
4045 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4048 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4050 op = g_simple_async_result_get_op_res_gpointer (simple);
4052 return g_object_ref (op);
4058 append_to_async_thread (GSimpleAsyncResult *res,
4060 GCancellable *cancellable)
4063 GFileCreateFlags *data;
4064 GFileOutputStream *stream;
4065 GError *error = NULL;
4067 iface = G_FILE_GET_IFACE (object);
4069 data = g_simple_async_result_get_op_res_gpointer (res);
4071 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4075 g_simple_async_result_set_from_error (res, error);
4076 g_error_free (error);
4079 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4083 g_file_real_append_to_async (GFile *file,
4084 GFileCreateFlags flags,
4086 GCancellable *cancellable,
4087 GAsyncReadyCallback callback,
4090 GFileCreateFlags *data;
4091 GSimpleAsyncResult *res;
4093 data = g_new0 (GFileCreateFlags, 1);
4096 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4097 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4099 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4100 g_object_unref (res);
4103 static GFileOutputStream *
4104 g_file_real_append_to_finish (GFile *file,
4108 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4111 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4113 op = g_simple_async_result_get_op_res_gpointer (simple);
4115 return g_object_ref (op);
4121 create_async_thread (GSimpleAsyncResult *res,
4123 GCancellable *cancellable)
4126 GFileCreateFlags *data;
4127 GFileOutputStream *stream;
4128 GError *error = NULL;
4130 iface = G_FILE_GET_IFACE (object);
4132 data = g_simple_async_result_get_op_res_gpointer (res);
4134 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4138 g_simple_async_result_set_from_error (res, error);
4139 g_error_free (error);
4142 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4146 g_file_real_create_async (GFile *file,
4147 GFileCreateFlags flags,
4149 GCancellable *cancellable,
4150 GAsyncReadyCallback callback,
4153 GFileCreateFlags *data;
4154 GSimpleAsyncResult *res;
4156 data = g_new0 (GFileCreateFlags, 1);
4159 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4160 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4162 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4163 g_object_unref (res);
4166 static GFileOutputStream *
4167 g_file_real_create_finish (GFile *file,
4171 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4174 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4176 op = g_simple_async_result_get_op_res_gpointer (simple);
4178 return g_object_ref (op);
4184 GFileOutputStream *stream;
4186 gboolean make_backup;
4187 GFileCreateFlags flags;
4191 replace_async_data_free (ReplaceAsyncData *data)
4194 g_object_unref (data->stream);
4195 g_free (data->etag);
4200 replace_async_thread (GSimpleAsyncResult *res,
4202 GCancellable *cancellable)
4205 GFileOutputStream *stream;
4206 GError *error = NULL;
4207 ReplaceAsyncData *data;
4209 iface = G_FILE_GET_IFACE (object);
4211 data = g_simple_async_result_get_op_res_gpointer (res);
4213 stream = iface->replace (G_FILE (object),
4222 g_simple_async_result_set_from_error (res, error);
4223 g_error_free (error);
4226 data->stream = stream;
4230 g_file_real_replace_async (GFile *file,
4232 gboolean make_backup,
4233 GFileCreateFlags flags,
4235 GCancellable *cancellable,
4236 GAsyncReadyCallback callback,
4239 GSimpleAsyncResult *res;
4240 ReplaceAsyncData *data;
4242 data = g_new0 (ReplaceAsyncData, 1);
4243 data->etag = g_strdup (etag);
4244 data->make_backup = make_backup;
4245 data->flags = flags;
4247 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4248 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4250 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4251 g_object_unref (res);
4254 static GFileOutputStream *
4255 g_file_real_replace_finish (GFile *file,
4259 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4260 ReplaceAsyncData *data;
4262 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4264 data = g_simple_async_result_get_op_res_gpointer (simple);
4266 return g_object_ref (data->stream);
4274 } SetDisplayNameAsyncData;
4277 set_display_name_data_free (SetDisplayNameAsyncData *data)
4279 g_free (data->name);
4281 g_object_unref (data->file);
4286 set_display_name_async_thread (GSimpleAsyncResult *res,
4288 GCancellable *cancellable)
4290 GError *error = NULL;
4291 SetDisplayNameAsyncData *data;
4294 data = g_simple_async_result_get_op_res_gpointer (res);
4296 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4300 g_simple_async_result_set_from_error (res, error);
4301 g_error_free (error);
4308 g_file_real_set_display_name_async (GFile *file,
4309 const char *display_name,
4311 GCancellable *cancellable,
4312 GAsyncReadyCallback callback,
4315 GSimpleAsyncResult *res;
4316 SetDisplayNameAsyncData *data;
4318 data = g_new0 (SetDisplayNameAsyncData, 1);
4319 data->name = g_strdup (display_name);
4321 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4322 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4324 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4325 g_object_unref (res);
4329 g_file_real_set_display_name_finish (GFile *file,
4333 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4334 SetDisplayNameAsyncData *data;
4336 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4338 data = g_simple_async_result_get_op_res_gpointer (simple);
4340 return g_object_ref (data->file);
4346 GFileQueryInfoFlags flags;
4353 set_info_data_free (SetInfoAsyncData *data)
4356 g_object_unref (data->info);
4358 g_error_free (data->error);
4363 set_info_async_thread (GSimpleAsyncResult *res,
4365 GCancellable *cancellable)
4367 SetInfoAsyncData *data;
4369 data = g_simple_async_result_get_op_res_gpointer (res);
4372 data->res = g_file_set_attributes_from_info (G_FILE (object),
4380 g_file_real_set_attributes_async (GFile *file,
4382 GFileQueryInfoFlags flags,
4384 GCancellable *cancellable,
4385 GAsyncReadyCallback callback,
4388 GSimpleAsyncResult *res;
4389 SetInfoAsyncData *data;
4391 data = g_new0 (SetInfoAsyncData, 1);
4392 data->info = g_file_info_dup (info);
4393 data->flags = flags;
4395 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4396 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4398 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4399 g_object_unref (res);
4403 g_file_real_set_attributes_finish (GFile *file,
4408 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4409 SetInfoAsyncData *data;
4411 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4413 data = g_simple_async_result_get_op_res_gpointer (simple);
4416 *info = g_object_ref (data->info);
4418 if (error != NULL && data->error)
4419 *error = g_error_copy (data->error);
4425 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4427 GCancellable *cancellable)
4429 GError *error = NULL;
4432 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4436 g_simple_async_result_set_from_error (res, error);
4437 g_error_free (error);
4440 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4444 g_file_real_find_enclosing_mount_async (GFile *file,
4446 GCancellable *cancellable,
4447 GAsyncReadyCallback callback,
4450 GSimpleAsyncResult *res;
4452 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4454 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4455 g_object_unref (res);
4459 g_file_real_find_enclosing_mount_finish (GFile *file,
4463 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4466 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4468 mount = g_simple_async_result_get_op_res_gpointer (simple);
4469 return g_object_ref (mount);
4476 GFileCopyFlags flags;
4477 GFileProgressCallback progress_cb;
4478 gpointer progress_cb_data;
4479 GIOSchedulerJob *job;
4483 copy_async_data_free (CopyAsyncData *data)
4485 g_object_unref (data->source);
4486 g_object_unref (data->destination);
4491 CopyAsyncData *data;
4492 goffset current_num_bytes;
4493 goffset total_num_bytes;
4497 copy_async_progress_in_main (gpointer user_data)
4499 ProgressData *progress = user_data;
4500 CopyAsyncData *data = progress->data;
4502 data->progress_cb (progress->current_num_bytes,
4503 progress->total_num_bytes,
4504 data->progress_cb_data);
4510 mainloop_barrier (gpointer user_data)
4512 /* Does nothing, but ensures all queued idles before
4519 copy_async_progress_callback (goffset current_num_bytes,
4520 goffset total_num_bytes,
4523 CopyAsyncData *data = user_data;
4524 ProgressData *progress;
4526 progress = g_new (ProgressData, 1);
4527 progress->data = data;
4528 progress->current_num_bytes = current_num_bytes;
4529 progress->total_num_bytes = total_num_bytes;
4531 g_io_scheduler_job_send_to_mainloop_async (data->job,
4532 copy_async_progress_in_main,
4538 copy_async_thread (GIOSchedulerJob *job,
4539 GCancellable *cancellable,
4542 GSimpleAsyncResult *res;
4543 CopyAsyncData *data;
4548 data = g_simple_async_result_get_op_res_gpointer (res);
4552 result = g_file_copy (data->source,
4556 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4560 /* Ensure all progress callbacks are done running in main thread */
4561 if (data->progress_cb != NULL)
4562 g_io_scheduler_job_send_to_mainloop (job,
4568 g_simple_async_result_set_from_error (res, error);
4569 g_error_free (error);
4572 g_simple_async_result_complete_in_idle (res);
4578 g_file_real_copy_async (GFile *source,
4580 GFileCopyFlags flags,
4582 GCancellable *cancellable,
4583 GFileProgressCallback progress_callback,
4584 gpointer progress_callback_data,
4585 GAsyncReadyCallback callback,
4588 GSimpleAsyncResult *res;
4589 CopyAsyncData *data;
4591 data = g_new0 (CopyAsyncData, 1);
4592 data->source = g_object_ref (source);
4593 data->destination = g_object_ref (destination);
4594 data->flags = flags;
4595 data->progress_cb = progress_callback;
4596 data->progress_cb_data = progress_callback_data;
4598 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4599 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4601 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4605 g_file_real_copy_finish (GFile *file,
4609 /* Error handled in g_file_copy_finish() */
4614 /********************************************
4615 * Default VFS operations *
4616 ********************************************/
4619 * g_file_new_for_path:
4620 * @path: a string containing a relative or absolute path.
4622 * Constructs a #GFile for a given path. This operation never
4623 * fails, but the returned object might not support any I/O
4624 * operation if @path is malformed.
4626 * Returns: a new #GFile for the given @path.
4629 g_file_new_for_path (const char *path)
4631 g_return_val_if_fail (path != NULL, NULL);
4633 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4637 * g_file_new_for_uri:
4638 * @uri: a string containing a URI.
4640 * Constructs a #GFile for a given URI. This operation never
4641 * fails, but the returned object might not support any I/O
4642 * operation if @uri is malformed or if the uri type is
4645 * Returns: a #GFile for the given @uri.
4648 g_file_new_for_uri (const char *uri)
4650 g_return_val_if_fail (uri != NULL, NULL);
4652 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4656 * g_file_parse_name:
4657 * @parse_name: a file name or path to be parsed.
4659 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4660 * This operation never fails, but the returned object might not support any I/O
4661 * operation if the @parse_name cannot be parsed.
4663 * Returns: a new #GFile.
4666 g_file_parse_name (const char *parse_name)
4668 g_return_val_if_fail (parse_name != NULL, NULL);
4670 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4674 is_valid_scheme_character (char c)
4676 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4680 has_valid_scheme (const char *uri)
4686 if (!is_valid_scheme_character (*p))
4691 } while (is_valid_scheme_character (*p));
4697 * g_file_new_for_commandline_arg:
4698 * @arg: a command line string.
4700 * Creates a #GFile with the given argument from the command line. The value of
4701 * @arg can be either a URI, an absolute path or a relative path resolved
4702 * relative to the current working directory.
4703 * This operation never fails, but the returned object might not support any
4704 * I/O operation if @arg points to a malformed path.
4706 * Returns: a new #GFile.
4709 g_file_new_for_commandline_arg (const char *arg)
4715 g_return_val_if_fail (arg != NULL, NULL);
4717 if (g_path_is_absolute (arg))
4718 return g_file_new_for_path (arg);
4720 if (has_valid_scheme (arg))
4721 return g_file_new_for_uri (arg);
4723 current_dir = g_get_current_dir ();
4724 filename = g_build_filename (current_dir, arg, NULL);
4725 g_free (current_dir);
4727 file = g_file_new_for_path (filename);
4734 * g_file_mount_enclosing_volume:
4735 * @location: input #GFile.
4736 * @flags: flags affecting the operation
4737 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4738 * @cancellable: optional #GCancellable object, %NULL to ignore.
4739 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4740 * @user_data: the data to pass to callback function
4742 * Starts a @mount_operation, mounting the volume that contains the file @location.
4744 * When this operation has completed, @callback will be called with
4745 * @user_user data, and the operation can be finalized with
4746 * g_file_mount_enclosing_volume_finish().
4748 * If @cancellable is not %NULL, then the operation can be cancelled by
4749 * triggering the cancellable object from another thread. If the operation
4750 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4753 g_file_mount_enclosing_volume (GFile *location,
4754 GMountMountFlags flags,
4755 GMountOperation *mount_operation,
4756 GCancellable *cancellable,
4757 GAsyncReadyCallback callback,
4762 g_return_if_fail (G_IS_FILE (location));
4764 iface = G_FILE_GET_IFACE (location);
4766 if (iface->mount_enclosing_volume == NULL)
4768 g_simple_async_report_error_in_idle (G_OBJECT (location),
4769 callback, user_data,
4770 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4771 _("volume doesn't implement mount"));
4776 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4781 * g_file_mount_enclosing_volume_finish:
4782 * @location: input #GFile.
4783 * @result: a #GAsyncResult.
4784 * @error: a #GError, or %NULL
4786 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4788 * Returns: %TRUE if successful. If an error
4789 * has occurred, this function will return %FALSE and set @error
4790 * appropriately if present.
4793 g_file_mount_enclosing_volume_finish (GFile *location,
4794 GAsyncResult *result,
4799 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4800 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4802 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4804 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4805 if (g_simple_async_result_propagate_error (simple, error))
4809 iface = G_FILE_GET_IFACE (location);
4811 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4814 /********************************************
4815 * Utility functions *
4816 ********************************************/
4819 * g_file_query_default_handler:
4820 * @file: a #GFile to open.
4821 * @cancellable: optional #GCancellable object, %NULL to ignore.
4822 * @error: a #GError, or %NULL
4824 * Returns the #GAppInfo that is registered as the default
4825 * application to handle the file specified by @file.
4827 * If @cancellable is not %NULL, then the operation can be cancelled by
4828 * triggering the cancellable object from another thread. If the operation
4829 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4831 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4832 * When you are done with it, release it with g_object_unref()
4835 g_file_query_default_handler (GFile *file,
4836 GCancellable *cancellable,
4840 const char *content_type;
4845 uri_scheme = g_file_get_uri_scheme (file);
4846 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4847 g_free (uri_scheme);
4849 if (appinfo != NULL)
4852 info = g_file_query_info (file,
4853 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4862 content_type = g_file_info_get_content_type (info);
4865 /* Don't use is_native(), as we want to support fuse paths if availible */
4866 path = g_file_get_path (file);
4867 appinfo = g_app_info_get_default_for_type (content_type,
4872 g_object_unref (info);
4874 if (appinfo != NULL)
4877 g_set_error (error, G_IO_ERROR,
4878 G_IO_ERROR_NOT_SUPPORTED,
4879 _("No application is registered as handling this file"));
4885 #define GET_CONTENT_BLOCK_SIZE 8192
4888 * g_file_load_contents:
4889 * @file: input #GFile.
4890 * @cancellable: optional #GCancellable object, %NULL to ignore.
4891 * @contents: a location to place the contents of the file.
4892 * @length: a location to place the length of the contents of the file.
4893 * @etag_out: a location to place the current entity tag for the file.
4894 * @error: a #GError, or %NULL
4896 * Loads the content of the file into memory, returning the size of
4897 * the data. The data is always zero terminated, but this is not
4898 * included in the resultant @length.
4900 * If @cancellable is not %NULL, then the operation can be cancelled by
4901 * triggering the cancellable object from another thread. If the operation
4902 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4904 * Returns: %TRUE if the @file's contents were successfully loaded.
4905 * %FALSE if there were errors..
4908 g_file_load_contents (GFile *file,
4909 GCancellable *cancellable,
4915 GFileInputStream *in;
4916 GByteArray *content;
4921 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4922 g_return_val_if_fail (contents != NULL, FALSE);
4924 in = g_file_read (file, cancellable, error);
4928 content = g_byte_array_new ();
4931 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4932 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4933 content->data + pos,
4934 GET_CONTENT_BLOCK_SIZE,
4935 cancellable, error)) > 0)
4938 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4945 info = g_file_input_stream_query_info (in,
4946 G_FILE_ATTRIBUTE_ETAG_VALUE,
4951 *etag_out = g_strdup (g_file_info_get_etag (info));
4952 g_object_unref (info);
4956 /* Ignore errors on close */
4957 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4958 g_object_unref (in);
4962 /* error is set already */
4963 g_byte_array_free (content, TRUE);
4970 /* Zero terminate (we got an extra byte allocated for this */
4971 content->data[pos] = 0;
4973 *contents = (char *)g_byte_array_free (content, FALSE);
4981 GCancellable *cancellable;
4982 GFileReadMoreCallback read_more_callback;
4983 GAsyncReadyCallback callback;
4985 GByteArray *content;
4992 load_contents_data_free (LoadContentsData *data)
4995 g_error_free (data->error);
4996 if (data->cancellable)
4997 g_object_unref (data->cancellable);
4999 g_byte_array_free (data->content, TRUE);
5000 g_free (data->etag);
5001 g_object_unref (data->file);
5006 load_contents_close_callback (GObject *obj,
5007 GAsyncResult *close_res,
5010 GInputStream *stream = G_INPUT_STREAM (obj);
5011 LoadContentsData *data = user_data;
5012 GSimpleAsyncResult *res;
5014 /* Ignore errors here, we're only reading anyway */
5015 g_input_stream_close_finish (stream, close_res, NULL);
5016 g_object_unref (stream);
5018 res = g_simple_async_result_new (G_OBJECT (data->file),
5021 g_file_load_contents_async);
5022 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5023 g_simple_async_result_complete (res);
5024 g_object_unref (res);
5028 load_contents_fstat_callback (GObject *obj,
5029 GAsyncResult *stat_res,
5032 GInputStream *stream = G_INPUT_STREAM (obj);
5033 LoadContentsData *data = user_data;
5036 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5040 data->etag = g_strdup (g_file_info_get_etag (info));
5041 g_object_unref (info);
5044 g_input_stream_close_async (stream, 0,
5046 load_contents_close_callback, data);
5050 load_contents_read_callback (GObject *obj,
5051 GAsyncResult *read_res,
5054 GInputStream *stream = G_INPUT_STREAM (obj);
5055 LoadContentsData *data = user_data;
5056 GError *error = NULL;
5059 read_size = g_input_stream_read_finish (stream, read_res, &error);
5063 /* Error or EOF, close the file */
5064 data->error = error;
5065 g_input_stream_close_async (stream, 0,
5067 load_contents_close_callback, data);
5069 else if (read_size == 0)
5071 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5072 G_FILE_ATTRIBUTE_ETAG_VALUE,
5075 load_contents_fstat_callback,
5078 else if (read_size > 0)
5080 data->pos += read_size;
5082 g_byte_array_set_size (data->content,
5083 data->pos + GET_CONTENT_BLOCK_SIZE);
5086 if (data->read_more_callback &&
5087 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5088 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5089 G_FILE_ATTRIBUTE_ETAG_VALUE,
5092 load_contents_fstat_callback,
5095 g_input_stream_read_async (stream,
5096 data->content->data + data->pos,
5097 GET_CONTENT_BLOCK_SIZE,
5100 load_contents_read_callback,
5106 load_contents_open_callback (GObject *obj,
5107 GAsyncResult *open_res,
5110 GFile *file = G_FILE (obj);
5111 GFileInputStream *stream;
5112 LoadContentsData *data = user_data;
5113 GError *error = NULL;
5114 GSimpleAsyncResult *res;
5116 stream = g_file_read_finish (file, open_res, &error);
5120 g_byte_array_set_size (data->content,
5121 data->pos + GET_CONTENT_BLOCK_SIZE);
5122 g_input_stream_read_async (G_INPUT_STREAM (stream),
5123 data->content->data + data->pos,
5124 GET_CONTENT_BLOCK_SIZE,
5127 load_contents_read_callback,
5133 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5137 g_simple_async_result_complete (res);
5138 g_error_free (error);
5139 load_contents_data_free (data);
5140 g_object_unref (res);
5145 * g_file_load_partial_contents_async:
5146 * @file: input #GFile.
5147 * @cancellable: optional #GCancellable object, %NULL to ignore.
5148 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5149 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5150 * @user_data: the data to pass to the callback functions.
5152 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5153 * used to stop reading from the file when appropriate, else this function
5154 * will behave exactly as g_file_load_contents_async(). This operation
5155 * can be finished by g_file_load_partial_contents_finish().
5157 * Users of this function should be aware that @user_data is passed to
5158 * both the @read_more_callback and the @callback.
5160 * If @cancellable is not %NULL, then the operation can be cancelled by
5161 * triggering the cancellable object from another thread. If the operation
5162 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5165 g_file_load_partial_contents_async (GFile *file,
5166 GCancellable *cancellable,
5167 GFileReadMoreCallback read_more_callback,
5168 GAsyncReadyCallback callback,
5171 LoadContentsData *data;
5173 g_return_if_fail (G_IS_FILE (file));
5175 data = g_new0 (LoadContentsData, 1);
5178 data->cancellable = g_object_ref (cancellable);
5179 data->read_more_callback = read_more_callback;
5180 data->callback = callback;
5181 data->user_data = user_data;
5182 data->content = g_byte_array_new ();
5183 data->file = g_object_ref (file);
5185 g_file_read_async (file,
5188 load_contents_open_callback,
5193 * g_file_load_partial_contents_finish:
5194 * @file: input #GFile.
5195 * @res: a #GAsyncResult.
5196 * @contents: a location to place the contents of the file.
5197 * @length: a location to place the length of the contents of the file.
5198 * @etag_out: a location to place the current entity tag for the file.
5199 * @error: a #GError, or %NULL
5201 * Finishes an asynchronous partial load operation that was started
5202 * with g_file_load_partial_contents_async().
5204 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5205 * present, it will be set appropriately.
5208 g_file_load_partial_contents_finish (GFile *file,
5215 GSimpleAsyncResult *simple;
5216 LoadContentsData *data;
5218 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5219 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5220 g_return_val_if_fail (contents != NULL, FALSE);
5222 simple = G_SIMPLE_ASYNC_RESULT (res);
5224 if (g_simple_async_result_propagate_error (simple, error))
5227 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5229 data = g_simple_async_result_get_op_res_gpointer (simple);
5233 g_propagate_error (error, data->error);
5242 *length = data->pos;
5246 *etag_out = data->etag;
5250 /* Zero terminate */
5251 g_byte_array_set_size (data->content, data->pos + 1);
5252 data->content->data[data->pos] = 0;
5254 *contents = (char *)g_byte_array_free (data->content, FALSE);
5255 data->content = NULL;
5261 * g_file_load_contents_async:
5262 * @file: input #GFile.
5263 * @cancellable: optional #GCancellable object, %NULL to ignore.
5264 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5265 * @user_data: the data to pass to callback function
5267 * Starts an asynchronous load of the @file's contents.
5269 * For more details, see g_file_load_contents() which is
5270 * the synchronous version of this call.
5272 * When the load operation has completed, @callback will be called
5273 * with @user data. To finish the operation, call
5274 * g_file_load_contents_finish() with the #GAsyncResult returned by
5277 * If @cancellable is not %NULL, then the operation can be cancelled by
5278 * triggering the cancellable object from another thread. If the operation
5279 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5282 g_file_load_contents_async (GFile *file,
5283 GCancellable *cancellable,
5284 GAsyncReadyCallback callback,
5287 g_file_load_partial_contents_async (file,
5290 callback, user_data);
5294 * g_file_load_contents_finish:
5295 * @file: input #GFile.
5296 * @res: a #GAsyncResult.
5297 * @contents: a location to place the contents of the file.
5298 * @length: a location to place the length of the contents of the file.
5299 * @etag_out: a location to place the current entity tag for the file.
5300 * @error: a #GError, or %NULL
5302 * Finishes an asynchronous load of the @file's contents.
5303 * The contents are placed in @contents, and @length is set to the
5304 * size of the @contents string. If @etag_out is present, it will be
5305 * set to the new entity tag for the @file.
5307 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5308 * present, it will be set appropriately.
5311 g_file_load_contents_finish (GFile *file,
5318 return g_file_load_partial_contents_finish (file,
5327 * g_file_replace_contents:
5328 * @file: input #GFile.
5329 * @contents: a string containing the new contents for @file.
5330 * @length: the length of @contents in bytes.
5331 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5333 * @make_backup: %TRUE if a backup should be created.
5334 * @flags: a set of #GFileCreateFlags.
5335 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5336 * for the document. This should be freed with g_free() when no longer
5338 * @cancellable: optional #GCancellable object, %NULL to ignore.
5339 * @error: a #GError, or %NULL
5341 * Replaces the contents of @file with @contents of @length bytes.
5343 * If @etag is specified (not %NULL) any existing file must have that etag, or
5344 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5346 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5348 * If @cancellable is not %NULL, then the operation can be cancelled by
5349 * triggering the cancellable object from another thread. If the operation
5350 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5352 * The returned @new_etag can be used to verify that the file hasn't changed the
5353 * next time it is saved over.
5355 * Returns: %TRUE if successful. If an error
5356 * has occurred, this function will return %FALSE and set @error
5357 * appropriately if present.
5360 g_file_replace_contents (GFile *file,
5361 const char *contents,
5364 gboolean make_backup,
5365 GFileCreateFlags flags,
5367 GCancellable *cancellable,
5370 GFileOutputStream *out;
5371 gsize pos, remainder;
5374 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5375 g_return_val_if_fail (contents != NULL, FALSE);
5377 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5383 while (remainder > 0 &&
5384 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5386 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5394 if (remainder > 0 && res < 0)
5396 /* Ignore errors on close */
5397 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5399 /* error is set already */
5403 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5407 *new_etag = g_file_output_stream_get_etag (out);
5415 GCancellable *cancellable;
5416 GAsyncReadyCallback callback;
5418 const char *content;
5422 } ReplaceContentsData;
5425 replace_contents_data_free (ReplaceContentsData *data)
5428 g_error_free (data->error);
5429 if (data->cancellable)
5430 g_object_unref (data->cancellable);
5431 g_object_unref (data->file);
5432 g_free (data->etag);
5437 replace_contents_close_callback (GObject *obj,
5438 GAsyncResult *close_res,
5441 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5442 ReplaceContentsData *data = user_data;
5443 GSimpleAsyncResult *res;
5445 /* Ignore errors here, we're only reading anyway */
5446 g_output_stream_close_finish (stream, close_res, NULL);
5447 g_object_unref (stream);
5449 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5451 res = g_simple_async_result_new (G_OBJECT (data->file),
5454 g_file_replace_contents_async);
5455 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5456 g_simple_async_result_complete (res);
5457 g_object_unref (res);
5461 replace_contents_write_callback (GObject *obj,
5462 GAsyncResult *read_res,
5465 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5466 ReplaceContentsData *data = user_data;
5467 GError *error = NULL;
5470 write_size = g_output_stream_write_finish (stream, read_res, &error);
5472 if (write_size <= 0)
5474 /* Error or EOF, close the file */
5476 data->error = error;
5477 g_output_stream_close_async (stream, 0,
5479 replace_contents_close_callback, data);
5481 else if (write_size > 0)
5483 data->pos += write_size;
5485 if (data->pos >= data->length)
5486 g_output_stream_close_async (stream, 0,
5488 replace_contents_close_callback, data);
5490 g_output_stream_write_async (stream,
5491 data->content + data->pos,
5492 data->length - data->pos,
5495 replace_contents_write_callback,
5501 replace_contents_open_callback (GObject *obj,
5502 GAsyncResult *open_res,
5505 GFile *file = G_FILE (obj);
5506 GFileOutputStream *stream;
5507 ReplaceContentsData *data = user_data;
5508 GError *error = NULL;
5509 GSimpleAsyncResult *res;
5511 stream = g_file_replace_finish (file, open_res, &error);
5515 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5516 data->content + data->pos,
5517 data->length - data->pos,
5520 replace_contents_write_callback,
5526 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5530 g_simple_async_result_complete (res);
5531 g_error_free (error);
5532 replace_contents_data_free (data);
5533 g_object_unref (res);
5538 * g_file_replace_contents_async:
5539 * @file: input #GFile.
5540 * @contents: string of contents to replace the file with.
5541 * @length: the length of @contents in bytes.
5542 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5543 * @make_backup: %TRUE if a backup should be created.
5544 * @flags: a set of #GFileCreateFlags.
5545 * @cancellable: optional #GCancellable object, %NULL to ignore.
5546 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5547 * @user_data: the data to pass to callback function
5549 * Starts an asynchronous replacement of @file with the given
5550 * @contents of @length bytes. @etag will replace the document's
5551 * current entity tag.
5553 * When this operation has completed, @callback will be called with
5554 * @user_user data, and the operation can be finalized with
5555 * g_file_replace_contents_finish().
5557 * If @cancellable is not %NULL, then the operation can be cancelled by
5558 * triggering the cancellable object from another thread. If the operation
5559 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5561 * If @make_backup is %TRUE, this function will attempt to
5562 * make a backup of @file.
5565 g_file_replace_contents_async (GFile *file,
5566 const char *contents,
5569 gboolean make_backup,
5570 GFileCreateFlags flags,
5571 GCancellable *cancellable,
5572 GAsyncReadyCallback callback,
5575 ReplaceContentsData *data;
5577 g_return_if_fail (G_IS_FILE (file));
5578 g_return_if_fail (contents != NULL);
5580 data = g_new0 (ReplaceContentsData, 1);
5583 data->cancellable = g_object_ref (cancellable);
5584 data->callback = callback;
5585 data->user_data = user_data;
5586 data->content = contents;
5587 data->length = length;
5589 data->file = g_object_ref (file);
5591 g_file_replace_async (file,
5597 replace_contents_open_callback,
5602 * g_file_replace_contents_finish:
5603 * @file: input #GFile.
5604 * @res: a #GAsyncResult.
5605 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5606 * for the document. This should be freed with g_free() when it is no
5608 * @error: a #GError, or %NULL
5610 * Finishes an asynchronous replace of the given @file. See
5611 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5612 * tag for the document, if present.
5614 * Returns: %TRUE on success, %FALSE on failure.
5617 g_file_replace_contents_finish (GFile *file,
5622 GSimpleAsyncResult *simple;
5623 ReplaceContentsData *data;
5625 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5626 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5628 simple = G_SIMPLE_ASYNC_RESULT (res);
5630 if (g_simple_async_result_propagate_error (simple, error))
5633 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5635 data = g_simple_async_result_get_op_res_gpointer (simple);
5639 g_propagate_error (error, data->error);
5647 *new_etag = data->etag;
5648 data->etag = NULL; /* Take ownership */
5654 #define __G_FILE_C__
5655 #include "gioaliasdef.c"