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 GCancellable *cancellable,
2122 GFileProgressCallback progress_callback,
2123 gpointer progress_callback_data,
2126 gssize n_read, n_written;
2127 goffset current_size;
2128 char buffer[1024*64], *p;
2134 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2135 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2139 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2140 total_size = g_file_info_get_size (info);
2141 g_object_unref (info);
2144 if (total_size == -1)
2146 info = g_file_query_info (source,
2147 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2148 G_FILE_QUERY_INFO_NONE,
2152 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2153 total_size = g_file_info_get_size (info);
2154 g_object_unref (info);
2158 if (total_size == -1)
2165 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2175 current_size += n_read;
2180 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2181 if (n_written == -1)
2188 n_read -= n_written;
2194 if (progress_callback)
2195 progress_callback (current_size, total_size, progress_callback_data);
2199 error = NULL; /* Ignore further errors */
2201 /* Make sure we send full copied size */
2202 if (progress_callback)
2203 progress_callback (current_size, total_size, progress_callback_data);
2205 /* Don't care about errors in source here */
2206 g_input_stream_close (in, cancellable, NULL);
2208 /* But write errors on close are bad! */
2209 if (!g_output_stream_close (out, cancellable, error))
2212 g_object_unref (in);
2213 g_object_unref (out);
2219 file_copy_fallback (GFile *source,
2221 GFileCopyFlags flags,
2222 GCancellable *cancellable,
2223 GFileProgressCallback progress_callback,
2224 gpointer progress_callback_data,
2232 /* Maybe copy the symlink? */
2233 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2235 info = g_file_query_info (source,
2236 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2237 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2243 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2244 (target = g_file_info_get_symlink_target (info)) != NULL)
2246 if (!copy_symlink (destination, flags, cancellable, target, error))
2248 g_object_unref (info);
2252 g_object_unref (info);
2256 g_object_unref (info);
2259 in = open_source_for_copy (source, destination, flags, cancellable, error);
2263 if (flags & G_FILE_COPY_OVERWRITE)
2265 out = (GOutputStream *)g_file_replace (destination,
2267 flags & G_FILE_COPY_BACKUP,
2268 cancellable, error);
2272 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2277 g_object_unref (in);
2281 if (!copy_stream_with_progress (in, out, source, cancellable,
2282 progress_callback, progress_callback_data,
2288 /* Ignore errors here. Failure to copy metadata is not a hard error */
2289 g_file_copy_attributes (source, destination,
2290 flags, cancellable, NULL);
2297 * @source: input #GFile.
2298 * @destination: destination #GFile
2299 * @flags: set of #GFileCopyFlags
2300 * @cancellable: optional #GCancellable object, %NULL to ignore.
2301 * @progress_callback: function to callback with progress information
2302 * @progress_callback_data: user data to pass to @progress_callback
2303 * @error: #GError to set on error, or %NULL
2305 * Copies the file @source to the location specified by @destination.
2306 * Can not handle recursive copies of directories.
2308 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2309 * existing @destination file is overwritten.
2311 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2312 * will be copied as symlinks, otherwise the target of the
2313 * @source symlink will be copied.
2315 * If @cancellable is not %NULL, then the operation can be cancelled by
2316 * triggering the cancellable object from another thread. If the operation
2317 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2319 * If @progress_callback is not %NULL, then the operation can be monitored by
2320 * setting this to a #GFileProgressCallback function. @progress_callback_data
2321 * will be passed to this function. It is guaranteed that this callback will
2322 * be called after all data has been transferred with the total number of bytes
2323 * copied during the operation.
2325 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2326 * error is returned, independent on the status of the @destination.
2328 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2329 * error G_IO_ERROR_EXISTS is returned.
2331 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2332 * error is returned. If trying to overwrite a directory with a directory the
2333 * G_IO_ERROR_WOULD_MERGE error is returned.
2335 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2336 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2339 * If you are interested in copying the #GFile object itself (not the on-disk
2340 * file), see g_file_dup().
2342 * Returns: %TRUE on success, %FALSE otherwise.
2345 g_file_copy (GFile *source,
2347 GFileCopyFlags flags,
2348 GCancellable *cancellable,
2349 GFileProgressCallback progress_callback,
2350 gpointer progress_callback_data,
2357 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2358 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2360 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2363 iface = G_FILE_GET_IFACE (destination);
2367 res = (* iface->copy) (source, destination,
2369 progress_callback, progress_callback_data,
2375 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2377 g_propagate_error (error, my_error);
2382 /* If the types are different, and the destination method failed
2383 also try the source method */
2384 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2386 iface = G_FILE_GET_IFACE (source);
2391 res = (* iface->copy) (source, destination,
2393 progress_callback, progress_callback_data,
2399 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2401 g_propagate_error (error, my_error);
2407 return file_copy_fallback (source, destination, flags, cancellable,
2408 progress_callback, progress_callback_data,
2413 * g_file_copy_async:
2414 * @source: input #GFile.
2415 * @destination: destination #GFile
2416 * @flags: set of #GFileCopyFlags
2417 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2419 * @cancellable: optional #GCancellable object, %NULL to ignore.
2420 * @progress_callback: function to callback with progress information
2421 * @progress_callback_data: user data to pass to @progress_callback
2422 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2423 * @user_data: the data to pass to callback function
2425 * Copies the file @source to the location specified by @destination
2426 * asynchronously. For details of the behaviour, see g_file_copy().
2428 * If @progress_callback is not %NULL, then that function that will be called
2429 * just like in g_file_copy(), however the callback will run in the main loop,
2430 * not in the thread that is doing the I/O operation.
2432 * When the operation is finished, @callback will be called. You can then call
2433 * g_file_copy_finish() to get the result of the operation.
2436 g_file_copy_async (GFile *source,
2438 GFileCopyFlags flags,
2440 GCancellable *cancellable,
2441 GFileProgressCallback progress_callback,
2442 gpointer progress_callback_data,
2443 GAsyncReadyCallback callback,
2448 g_return_if_fail (G_IS_FILE (source));
2449 g_return_if_fail (G_IS_FILE (destination));
2451 iface = G_FILE_GET_IFACE (source);
2452 (* iface->copy_async) (source,
2458 progress_callback_data,
2464 * g_file_copy_finish:
2465 * @file: input #GFile.
2466 * @res: a #GAsyncResult.
2467 * @error: a #GError, or %NULL
2469 * Finishes copying the file started with
2470 * g_file_copy_async().
2472 * Returns: a %TRUE on success, %FALSE on error.
2475 g_file_copy_finish (GFile *file,
2481 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2482 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2484 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2486 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2488 if (g_simple_async_result_propagate_error (simple, error))
2492 iface = G_FILE_GET_IFACE (file);
2493 return (* iface->copy_finish) (file, res, error);
2498 * @source: #GFile pointing to the source location.
2499 * @destination: #GFile pointing to the destination location.
2500 * @flags: set of #GFileCopyFlags.
2501 * @cancellable: optional #GCancellable object, %NULL to ignore.
2502 * @progress_callback: #GFileProgressCallback function for updates.
2503 * @progress_callback_data: gpointer to user data for the callback function.
2504 * @error: #GError for returning error conditions, or %NULL
2507 * Tries to move the file or directory @source to the location specified by @destination.
2508 * If native move operations are supported then this is used, otherwise a copy + delete
2509 * fallback is used. The native implementation may support moving directories (for instance
2510 * on moves inside the same filesystem), but the fallback code does not.
2512 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2513 * existing @destination file is overwritten.
2515 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2516 * will be copied as symlinks, otherwise the target of the
2517 * @source symlink will be copied.
2519 * If @cancellable is not %NULL, then the operation can be cancelled by
2520 * triggering the cancellable object from another thread. If the operation
2521 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2523 * If @progress_callback is not %NULL, then the operation can be monitored by
2524 * setting this to a #GFileProgressCallback function. @progress_callback_data
2525 * will be passed to this function. It is guaranteed that this callback will
2526 * be called after all data has been transferred with the total number of bytes
2527 * copied during the operation.
2529 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2530 * error is returned, independent on the status of the @destination.
2532 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2533 * error G_IO_ERROR_EXISTS is returned.
2535 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2536 * error is returned. If trying to overwrite a directory with a directory the
2537 * G_IO_ERROR_WOULD_MERGE error is returned.
2539 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2540 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2541 * may be returned (if the native move operation isn't available).
2543 * Returns: %TRUE on successful move, %FALSE otherwise.
2546 g_file_move (GFile *source,
2548 GFileCopyFlags flags,
2549 GCancellable *cancellable,
2550 GFileProgressCallback progress_callback,
2551 gpointer progress_callback_data,
2558 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2559 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2561 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2564 iface = G_FILE_GET_IFACE (destination);
2568 res = (* iface->move) (source, destination,
2570 progress_callback, progress_callback_data,
2576 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2578 g_propagate_error (error, my_error);
2583 /* If the types are different, and the destination method failed
2584 also try the source method */
2585 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2587 iface = G_FILE_GET_IFACE (source);
2592 res = (* iface->move) (source, destination,
2594 progress_callback, progress_callback_data,
2600 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2602 g_propagate_error (error, my_error);
2608 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2610 g_set_error (error, G_IO_ERROR,
2611 G_IO_ERROR_NOT_SUPPORTED,
2612 _("Operation not supported"));
2616 flags |= G_FILE_COPY_ALL_METADATA;
2617 if (!g_file_copy (source, destination, flags, cancellable,
2618 progress_callback, progress_callback_data,
2622 return g_file_delete (source, cancellable, error);
2626 * g_file_make_directory
2627 * @file: input #GFile.
2628 * @cancellable: optional #GCancellable object, %NULL to ignore.
2629 * @error: a #GError, or %NULL
2631 * Creates a directory.
2633 * If @cancellable is not %NULL, then the operation can be cancelled by
2634 * triggering the cancellable object from another thread. If the operation
2635 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2637 * Returns: %TRUE on successful creation, %FALSE otherwise.
2640 g_file_make_directory (GFile *file,
2641 GCancellable *cancellable,
2646 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2648 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2651 iface = G_FILE_GET_IFACE (file);
2653 if (iface->make_directory == NULL)
2655 g_set_error (error, G_IO_ERROR,
2656 G_IO_ERROR_NOT_SUPPORTED,
2657 _("Operation not supported"));
2661 return (* iface->make_directory) (file, cancellable, error);
2665 * g_file_make_symbolic_link:
2666 * @file: input #GFile.
2667 * @symlink_value: a string with the value of the new symlink.
2668 * @cancellable: optional #GCancellable object, %NULL to ignore.
2669 * @error: a #GError.
2671 * Creates a symbolic link.
2673 * If @cancellable is not %NULL, then the operation can be cancelled by
2674 * triggering the cancellable object from another thread. If the operation
2675 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2677 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2680 g_file_make_symbolic_link (GFile *file,
2681 const char *symlink_value,
2682 GCancellable *cancellable,
2687 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2688 g_return_val_if_fail (symlink_value != NULL, FALSE);
2690 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2693 if (*symlink_value == '\0')
2695 g_set_error (error, G_IO_ERROR,
2696 G_IO_ERROR_INVALID_ARGUMENT,
2697 _("Invalid symlink value given"));
2701 iface = G_FILE_GET_IFACE (file);
2703 if (iface->make_symbolic_link == NULL)
2705 g_set_error (error, G_IO_ERROR,
2706 G_IO_ERROR_NOT_SUPPORTED,
2707 _("Operation not supported"));
2711 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2716 * @file: input #GFile.
2717 * @cancellable: optional #GCancellable object, %NULL to ignore.
2718 * @error: a #GError, or %NULL
2720 * Deletes a file. If the @file is a directory, it will only be deleted if it
2723 * If @cancellable is not %NULL, then the operation can be cancelled by
2724 * triggering the cancellable object from another thread. If the operation
2725 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2727 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
2730 g_file_delete (GFile *file,
2731 GCancellable *cancellable,
2736 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2738 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2741 iface = G_FILE_GET_IFACE (file);
2743 if (iface->delete_file == NULL)
2745 g_set_error (error, G_IO_ERROR,
2746 G_IO_ERROR_NOT_SUPPORTED,
2747 _("Operation not supported"));
2751 return (* iface->delete_file) (file, cancellable, error);
2756 * @file: #GFile to send to trash.
2757 * @cancellable: optional #GCancellable object, %NULL to ignore.
2758 * @error: a #GError, or %NULL
2760 * Sends @file to the "Trashcan", if possible. This is similar to
2761 * deleting it, but the user can recover it before emptying the trashcan.
2762 * Not all file systems support trashing, so this call can return the
2763 * %G_IO_ERROR_NOT_SUPPORTED error.
2766 * If @cancellable is not %NULL, then the operation can be cancelled by
2767 * triggering the cancellable object from another thread. If the operation
2768 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2770 * Returns: %TRUE on successful trash, %FALSE otherwise.
2773 g_file_trash (GFile *file,
2774 GCancellable *cancellable,
2779 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2781 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2784 iface = G_FILE_GET_IFACE (file);
2786 if (iface->trash == NULL)
2789 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2790 _("Trash not supported"));
2794 return (* iface->trash) (file, cancellable, error);
2798 * g_file_set_display_name:
2799 * @file: input #GFile.
2800 * @display_name: a string.
2801 * @cancellable: optional #GCancellable object, %NULL to ignore.
2802 * @error: a #GError, or %NULL
2804 * Renames @file to the specified display name.
2806 * The display name is converted from UTF8 to the correct encoding for the target
2807 * filesystem if possible and the @file is renamed to this.
2809 * If you want to implement a rename operation in the user interface the edit name
2810 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
2811 * widget, and then the result after editing should be passed to g_file_set_display_name().
2813 * On success the resulting converted filename is returned.
2815 * If @cancellable is not %NULL, then the operation can be cancelled by
2816 * triggering the cancellable object from another thread. If the operation
2817 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2819 * Returns: a #GFile specifying what @file was renamed to, or %NULL if there was an error.
2822 g_file_set_display_name (GFile *file,
2823 const char *display_name,
2824 GCancellable *cancellable,
2829 g_return_val_if_fail (G_IS_FILE (file), NULL);
2830 g_return_val_if_fail (display_name != NULL, NULL);
2832 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
2836 G_IO_ERROR_INVALID_ARGUMENT,
2837 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
2841 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2844 iface = G_FILE_GET_IFACE (file);
2846 return (* iface->set_display_name) (file, display_name, cancellable, error);
2850 * g_file_set_display_name_async:
2851 * @file: input #GFile.
2852 * @display_name: a string.
2853 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2855 * @cancellable: optional #GCancellable object, %NULL to ignore.
2856 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2857 * @user_data: the data to pass to callback function
2859 * Asynchronously sets the display name for a given #GFile.
2861 * For more details, see g_set_display_name() which is
2862 * the synchronous version of this call.
2864 * When the operation is finished, @callback will be called. You can then call
2865 * g_file_set_display_name_finish() to get the result of the operation.
2868 g_file_set_display_name_async (GFile *file,
2869 const char *display_name,
2871 GCancellable *cancellable,
2872 GAsyncReadyCallback callback,
2877 g_return_if_fail (G_IS_FILE (file));
2878 g_return_if_fail (display_name != NULL);
2880 iface = G_FILE_GET_IFACE (file);
2881 (* iface->set_display_name_async) (file,
2890 * g_file_set_display_name_finish:
2891 * @file: input #GFile.
2892 * @res: a #GAsyncResult.
2893 * @error: a #GError, or %NULL
2895 * Finishes setting a display name started with
2896 * g_file_set_display_name_async().
2898 * Returns: a #GFile or %NULL on error.
2901 g_file_set_display_name_finish (GFile *file,
2907 g_return_val_if_fail (G_IS_FILE (file), NULL);
2908 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2910 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2912 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2913 if (g_simple_async_result_propagate_error (simple, error))
2917 iface = G_FILE_GET_IFACE (file);
2918 return (* iface->set_display_name_finish) (file, res, error);
2922 * g_file_query_settable_attributes:
2923 * @file: input #GFile.
2924 * @cancellable: optional #GCancellable object, %NULL to ignore.
2925 * @error: a #GError, or %NULL
2927 * Obtain the list of settable attributes for the file.
2929 * Returns the type and full attribute name of all the attributes
2930 * that can be set on this file. This doesn't mean setting it will always
2931 * succeed though, you might get an access failure, or some specific
2932 * file may not support a specific attribute.
2934 * If @cancellable is not %NULL, then the operation can be cancelled by
2935 * triggering the cancellable object from another thread. If the operation
2936 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2938 * Returns: a #GFileAttributeInfoList describing the settable attributes.
2939 * When you are done with it, release it with g_file_attribute_info_list_unref()
2941 GFileAttributeInfoList *
2942 g_file_query_settable_attributes (GFile *file,
2943 GCancellable *cancellable,
2948 GFileAttributeInfoList *list;
2950 g_return_val_if_fail (G_IS_FILE (file), NULL);
2952 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2955 iface = G_FILE_GET_IFACE (file);
2957 if (iface->query_settable_attributes == NULL)
2958 return g_file_attribute_info_list_new ();
2961 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
2965 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
2967 list = g_file_attribute_info_list_new ();
2968 g_error_free (my_error);
2971 g_propagate_error (error, my_error);
2978 * g_file_query_writable_namespaces:
2979 * @file: input #GFile.
2980 * @cancellable: optional #GCancellable object, %NULL to ignore.
2981 * @error: a #GError, or %NULL
2983 * Obtain the list of attribute namespaces where new attributes
2984 * can be created by a user. An example of this is extended
2985 * attributes (in the "xattr" namespace).
2987 * If @cancellable is not %NULL, then the operation can be cancelled by
2988 * triggering the cancellable object from another thread. If the operation
2989 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2991 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
2992 * When you are done with it, release it with g_file_attribute_info_list_unref()
2994 GFileAttributeInfoList *
2995 g_file_query_writable_namespaces (GFile *file,
2996 GCancellable *cancellable,
3001 GFileAttributeInfoList *list;
3003 g_return_val_if_fail (G_IS_FILE (file), NULL);
3005 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3008 iface = G_FILE_GET_IFACE (file);
3010 if (iface->query_writable_namespaces == NULL)
3011 return g_file_attribute_info_list_new ();
3014 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3018 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3020 list = g_file_attribute_info_list_new ();
3021 g_error_free (my_error);
3024 g_propagate_error (error, my_error);
3031 * g_file_set_attribute:
3032 * @file: input #GFile.
3033 * @attribute: a string containing the attribute's name.
3034 * @type: The type of the attribute
3035 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3036 * @flags: a set of #GFileQueryInfoFlags.
3037 * @cancellable: optional #GCancellable object, %NULL to ignore.
3038 * @error: a #GError, or %NULL
3040 * Sets an attribute in the file with attribute name @attribute to @value.
3042 * If @cancellable is not %NULL, then the operation can be cancelled by
3043 * triggering the cancellable object from another thread. If the operation
3044 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3046 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3049 g_file_set_attribute (GFile *file,
3050 const char *attribute,
3051 GFileAttributeType type,
3053 GFileQueryInfoFlags flags,
3054 GCancellable *cancellable,
3059 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3060 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3062 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3065 iface = G_FILE_GET_IFACE (file);
3067 if (iface->set_attribute == NULL)
3069 g_set_error (error, G_IO_ERROR,
3070 G_IO_ERROR_NOT_SUPPORTED,
3071 _("Operation not supported"));
3075 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3079 * g_file_set_attributes_from_info:
3080 * @file: input #GFile.
3081 * @info: a #GFileInfo.
3082 * @flags: #GFileQueryInfoFlags
3083 * @cancellable: optional #GCancellable object, %NULL to ignore.
3084 * @error: a #GError, or %NULL
3086 * Tries to set all attributes in the #GFileInfo on the target values,
3087 * not stopping on the first error.
3089 * If there is any error during this operation then @error will be set to
3090 * the first error. Error on particular fields are flagged by setting
3091 * the "status" field in the attribute value to
3092 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3095 * If @cancellable is not %NULL, then the operation can be cancelled by
3096 * triggering the cancellable object from another thread. If the operation
3097 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3099 * Returns: %TRUE if there was any error, %FALSE otherwise.
3102 g_file_set_attributes_from_info (GFile *file,
3104 GFileQueryInfoFlags flags,
3105 GCancellable *cancellable,
3110 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3111 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3113 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3116 g_file_info_clear_status (info);
3118 iface = G_FILE_GET_IFACE (file);
3120 return (* iface->set_attributes_from_info) (file,
3129 g_file_real_set_attributes_from_info (GFile *file,
3131 GFileQueryInfoFlags flags,
3132 GCancellable *cancellable,
3138 GFileAttributeValue *value;
3142 attributes = g_file_info_list_attributes (info, NULL);
3144 for (i = 0; attributes[i] != NULL; i++)
3146 value = _g_file_info_get_attribute_value (info, attributes[i]);
3148 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3151 if (!g_file_set_attribute (file, attributes[i],
3152 value->type, _g_file_attribute_value_peek_as_pointer (value),
3153 flags, cancellable, error))
3155 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3157 /* Don't set error multiple times */
3161 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3164 g_strfreev (attributes);
3170 * g_file_set_attributes_async:
3171 * @file: input #GFile.
3172 * @info: a #GFileInfo.
3173 * @flags: a #GFileQueryInfoFlags.
3174 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3176 * @cancellable: optional #GCancellable object, %NULL to ignore.
3177 * @callback: a #GAsyncReadyCallback.
3178 * @user_data: a #gpointer.
3180 * Asynchronously sets the attributes of @file with @info.
3182 * For more details, see g_file_set_attributes_from_info() which is
3183 * the synchronous version of this call.
3185 * When the operation is finished, @callback will be called. You can then call
3186 * g_file_set_attributes_finish() to get the result of the operation.
3189 g_file_set_attributes_async (GFile *file,
3191 GFileQueryInfoFlags flags,
3193 GCancellable *cancellable,
3194 GAsyncReadyCallback callback,
3199 g_return_if_fail (G_IS_FILE (file));
3200 g_return_if_fail (G_IS_FILE_INFO (info));
3202 iface = G_FILE_GET_IFACE (file);
3203 (* iface->set_attributes_async) (file,
3213 * g_file_set_attributes_finish:
3214 * @file: input #GFile.
3215 * @result: a #GAsyncResult.
3216 * @info: a #GFileInfo.
3217 * @error: a #GError, or %NULL
3219 * Finishes setting an attribute started in g_file_set_attributes_async().
3221 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
3224 g_file_set_attributes_finish (GFile *file,
3225 GAsyncResult *result,
3231 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3232 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3234 /* No standard handling of errors here, as we must set info even
3237 iface = G_FILE_GET_IFACE (file);
3238 return (* iface->set_attributes_finish) (file, result, info, error);
3242 * g_file_set_attribute_string:
3243 * @file: input #GFile.
3244 * @attribute: a string containing the attribute's name.
3245 * @value: a string containing the attribute's value.
3246 * @flags: #GFileQueryInfoFlags.
3247 * @cancellable: optional #GCancellable object, %NULL to ignore.
3248 * @error: a #GError, or %NULL
3250 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
3251 * If @attribute is of a different type, this operation will fail.
3253 * If @cancellable is not %NULL, then the operation can be cancelled by
3254 * triggering the cancellable object from another thread. If the operation
3255 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3257 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3260 g_file_set_attribute_string (GFile *file,
3261 const char *attribute,
3263 GFileQueryInfoFlags flags,
3264 GCancellable *cancellable,
3267 return g_file_set_attribute (file, attribute,
3268 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
3269 flags, cancellable, error);
3273 * g_file_set_attribute_byte_string:
3274 * @file: input #GFile.
3275 * @attribute: a string containing the attribute's name.
3276 * @value: a string containing the attribute's new value.
3277 * @flags: a #GFileQueryInfoFlags.
3278 * @cancellable: optional #GCancellable object, %NULL to ignore.
3279 * @error: a #GError, or %NULL
3281 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
3282 * If @attribute is of a different type, this operation will fail,
3285 * If @cancellable is not %NULL, then the operation can be cancelled by
3286 * triggering the cancellable object from another thread. If the operation
3287 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3289 * Returns: %TRUE if the @attribute was successfully set to @value
3290 * in the @file, %FALSE otherwise.
3293 g_file_set_attribute_byte_string (GFile *file,
3294 const char *attribute,
3296 GFileQueryInfoFlags flags,
3297 GCancellable *cancellable,
3300 return g_file_set_attribute (file, attribute,
3301 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
3302 flags, cancellable, error);
3306 * g_file_set_attribute_uint32:
3307 * @file: input #GFile.
3308 * @attribute: a string containing the attribute's name.
3309 * @value: a #guint32 containing the attribute's new value.
3310 * @flags: a #GFileQueryInfoFlags.
3311 * @cancellable: optional #GCancellable object, %NULL to ignore.
3312 * @error: a #GError, or %NULL
3314 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
3315 * If @attribute is of a different type, this operation will fail.
3317 * If @cancellable is not %NULL, then the operation can be cancelled by
3318 * triggering the cancellable object from another thread. If the operation
3319 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3321 * Returns: %TRUE if the @attribute was successfully set to @value
3322 * in the @file, %FALSE otherwise.
3325 g_file_set_attribute_uint32 (GFile *file,
3326 const char *attribute,
3328 GFileQueryInfoFlags flags,
3329 GCancellable *cancellable,
3332 return g_file_set_attribute (file, attribute,
3333 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
3334 flags, cancellable, error);
3338 * g_file_set_attribute_int32:
3339 * @file: input #GFile.
3340 * @attribute: a string containing the attribute's name.
3341 * @value: a #gint32 containing the attribute's new value.
3342 * @flags: a #GFileQueryInfoFlags.
3343 * @cancellable: optional #GCancellable object, %NULL to ignore.
3344 * @error: a #GError, or %NULL
3346 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
3347 * If @attribute is of a different type, this operation will fail.
3349 * If @cancellable is not %NULL, then the operation can be cancelled by
3350 * triggering the cancellable object from another thread. If the operation
3351 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3353 * Returns: %TRUE if the @attribute was successfully set to @value
3354 * in the @file, %FALSE otherwise.
3357 g_file_set_attribute_int32 (GFile *file,
3358 const char *attribute,
3360 GFileQueryInfoFlags flags,
3361 GCancellable *cancellable,
3364 return g_file_set_attribute (file, attribute,
3365 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
3366 flags, cancellable, error);
3370 * g_file_set_attribute_uint64:
3371 * @file: input #GFile.
3372 * @attribute: a string containing the attribute's name.
3373 * @value: a #guint64 containing the attribute's new value.
3374 * @flags: a #GFileQueryInfoFlags.
3375 * @cancellable: optional #GCancellable object, %NULL to ignore.
3376 * @error: a #GError, or %NULL
3378 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
3379 * If @attribute is of a different type, this operation will fail.
3381 * If @cancellable is not %NULL, then the operation can be cancelled by
3382 * triggering the cancellable object from another thread. If the operation
3383 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3385 * Returns: %TRUE if the @attribute was successfully set to @value
3386 * in the @file, %FALSE otherwise.
3389 g_file_set_attribute_uint64 (GFile *file,
3390 const char *attribute,
3392 GFileQueryInfoFlags flags,
3393 GCancellable *cancellable,
3396 return g_file_set_attribute (file, attribute,
3397 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
3398 flags, cancellable, error);
3402 * g_file_set_attribute_int64:
3403 * @file: input #GFile.
3404 * @attribute: a string containing the attribute's name.
3405 * @value: a #guint64 containing the attribute's new value.
3406 * @flags: a #GFileQueryInfoFlags.
3407 * @cancellable: optional #GCancellable object, %NULL to ignore.
3408 * @error: a #GError, or %NULL
3410 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
3411 * If @attribute is of a different type, this operation will fail.
3413 * If @cancellable is not %NULL, then the operation can be cancelled by
3414 * triggering the cancellable object from another thread. If the operation
3415 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3417 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
3420 g_file_set_attribute_int64 (GFile *file,
3421 const char *attribute,
3423 GFileQueryInfoFlags flags,
3424 GCancellable *cancellable,
3427 return g_file_set_attribute (file, attribute,
3428 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
3429 flags, cancellable, error);
3433 * g_file_mount_mountable:
3434 * @file: input #GFile.
3435 * @flags: flags affecting the operation
3436 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
3437 * @cancellable: optional #GCancellable object, %NULL to ignore.
3438 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3439 * @user_data: the data to pass to callback function
3441 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
3442 * Using @mount_operation, you can request callbacks when, for instance,
3443 * passwords are needed during authentication.
3445 * If @cancellable is not %NULL, then the operation can be cancelled by
3446 * triggering the cancellable object from another thread. If the operation
3447 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3449 * When the operation is finished, @callback will be called. You can then call
3450 * g_file_mount_mountable_finish() to get the result of the operation.
3453 g_file_mount_mountable (GFile *file,
3454 GMountMountFlags flags,
3455 GMountOperation *mount_operation,
3456 GCancellable *cancellable,
3457 GAsyncReadyCallback callback,
3462 g_return_if_fail (G_IS_FILE (file));
3464 iface = G_FILE_GET_IFACE (file);
3466 if (iface->mount_mountable == NULL)
3468 g_simple_async_report_error_in_idle (G_OBJECT (file),
3472 G_IO_ERROR_NOT_SUPPORTED,
3473 _("Operation not supported"));
3477 (* iface->mount_mountable) (file,
3486 * g_file_mount_mountable_finish:
3487 * @file: input #GFile.
3488 * @result: a #GAsyncResult.
3489 * @error: a #GError, or %NULL
3491 * Finishes a mount operation. See g_file_mount_mountable() for details.
3493 * Finish an asynchronous mount operation that was started
3494 * with g_file_mount_mountable().
3496 * Returns: a #GFile or %NULL on error.
3499 g_file_mount_mountable_finish (GFile *file,
3500 GAsyncResult *result,
3505 g_return_val_if_fail (G_IS_FILE (file), NULL);
3506 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
3508 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3510 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3511 if (g_simple_async_result_propagate_error (simple, error))
3515 iface = G_FILE_GET_IFACE (file);
3516 return (* iface->mount_mountable_finish) (file, result, error);
3520 * g_file_unmount_mountable:
3521 * @file: input #GFile.
3522 * @flags: flags affecting the operation
3523 * @cancellable: optional #GCancellable object, %NULL to ignore.
3524 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3525 * @user_data: the data to pass to callback function
3527 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
3529 * If @cancellable is not %NULL, then the operation can be cancelled by
3530 * triggering the cancellable object from another thread. If the operation
3531 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3533 * When the operation is finished, @callback will be called. You can then call
3534 * g_file_unmount_mountable_finish() to get the result of the operation.
3537 g_file_unmount_mountable (GFile *file,
3538 GMountUnmountFlags flags,
3539 GCancellable *cancellable,
3540 GAsyncReadyCallback callback,
3545 g_return_if_fail (G_IS_FILE (file));
3547 iface = G_FILE_GET_IFACE (file);
3549 if (iface->unmount_mountable == NULL)
3551 g_simple_async_report_error_in_idle (G_OBJECT (file),
3555 G_IO_ERROR_NOT_SUPPORTED,
3556 _("Operation not supported"));
3560 (* iface->unmount_mountable) (file,
3568 * g_file_unmount_mountable_finish:
3569 * @file: input #GFile.
3570 * @result: a #GAsyncResult.
3571 * @error: a #GError, or %NULL
3573 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
3575 * Finish an asynchronous unmount operation that was started
3576 * with g_file_unmount_mountable().
3578 * Returns: %TRUE if the operation finished successfully. %FALSE
3582 g_file_unmount_mountable_finish (GFile *file,
3583 GAsyncResult *result,
3588 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3589 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3591 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3593 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3594 if (g_simple_async_result_propagate_error (simple, error))
3598 iface = G_FILE_GET_IFACE (file);
3599 return (* iface->unmount_mountable_finish) (file, result, error);
3603 * g_file_eject_mountable:
3604 * @file: input #GFile.
3605 * @flags: flags affecting the operation
3606 * @cancellable: optional #GCancellable object, %NULL to ignore.
3607 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
3608 * @user_data: the data to pass to callback function
3610 * Starts an asynchronous eject on a mountable.
3611 * When this operation has completed, @callback will be called with
3612 * @user_user data, and the operation can be finalized with
3613 * g_file_eject_mountable_finish().
3615 * If @cancellable is not %NULL, then the operation can be cancelled by
3616 * triggering the cancellable object from another thread. If the operation
3617 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3620 g_file_eject_mountable (GFile *file,
3621 GMountUnmountFlags flags,
3622 GCancellable *cancellable,
3623 GAsyncReadyCallback callback,
3628 g_return_if_fail (G_IS_FILE (file));
3630 iface = G_FILE_GET_IFACE (file);
3632 if (iface->eject_mountable == NULL)
3634 g_simple_async_report_error_in_idle (G_OBJECT (file),
3638 G_IO_ERROR_NOT_SUPPORTED,
3639 _("Operation not supported"));
3643 (* iface->eject_mountable) (file,
3651 * g_file_eject_mountable_finish:
3652 * @file: input #GFile.
3653 * @result: a #GAsyncResult.
3654 * @error: a #GError, or %NULL
3656 * Finishes an asynchronous eject operation started by
3657 * g_file_eject_mountable().
3659 * Returns: %TRUE if the @file was ejected successfully. %FALSE
3663 g_file_eject_mountable_finish (GFile *file,
3664 GAsyncResult *result,
3669 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3670 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3672 if (G_IS_SIMPLE_ASYNC_RESULT (result))
3674 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
3675 if (g_simple_async_result_propagate_error (simple, error))
3679 iface = G_FILE_GET_IFACE (file);
3680 return (* iface->eject_mountable_finish) (file, result, error);
3684 * g_file_monitor_directory:
3685 * @file: input #GFile.
3686 * @flags: a set of #GFileMonitorFlags.
3687 * @cancellable: optional #GCancellable object, %NULL to ignore.
3688 * @error: a #GError, or %NULL.
3690 * Obtains a directory monitor for the given file.
3691 * This may fail if directory monitoring is not supported.
3693 * If @cancellable is not %NULL, then the operation can be cancelled by
3694 * triggering the cancellable object from another thread. If the operation
3695 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3697 * Returns: a #GFileMonitor for the given @file,
3698 * or %NULL on error.
3701 g_file_monitor_directory (GFile *file,
3702 GFileMonitorFlags flags,
3703 GCancellable *cancellable,
3708 g_return_val_if_fail (G_IS_FILE (file), NULL);
3710 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3713 iface = G_FILE_GET_IFACE (file);
3715 if (iface->monitor_dir == NULL)
3717 g_set_error (error, G_IO_ERROR,
3718 G_IO_ERROR_NOT_SUPPORTED,
3719 _("Operation not supported"));
3723 return (* iface->monitor_dir) (file, flags, cancellable, error);
3727 * g_file_monitor_file:
3728 * @file: input #GFile.
3729 * @flags: a set of #GFileMonitorFlags.
3730 * @cancellable: optional #GCancellable object, %NULL to ignore.
3731 * @error: a #GError, or %NULL.
3733 * Obtains a file monitor for the given file. If no file notification
3734 * mechanism exists, then regular polling of the file is used.
3736 * If @cancellable is not %NULL, then the operation can be cancelled by
3737 * triggering the cancellable object from another thread. If the operation
3738 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3740 * Returns: a #GFileMonitor for the given @file.
3743 g_file_monitor_file (GFile *file,
3744 GFileMonitorFlags flags,
3745 GCancellable *cancellable,
3749 GFileMonitor *monitor;
3751 g_return_val_if_fail (G_IS_FILE (file), NULL);
3753 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3756 iface = G_FILE_GET_IFACE (file);
3760 if (iface->monitor_file)
3761 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
3763 /* Fallback to polling */
3764 if (monitor == NULL)
3765 monitor = _g_poll_file_monitor_new (file);
3770 /********************************************
3771 * Default implementation of async ops *
3772 ********************************************/
3776 GFileQueryInfoFlags flags;
3778 } QueryInfoAsyncData;
3781 query_info_data_free (QueryInfoAsyncData *data)
3784 g_object_unref (data->info);
3785 g_free (data->attributes);
3790 query_info_async_thread (GSimpleAsyncResult *res,
3792 GCancellable *cancellable)
3794 GError *error = NULL;
3795 QueryInfoAsyncData *data;
3798 data = g_simple_async_result_get_op_res_gpointer (res);
3800 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3804 g_simple_async_result_set_from_error (res, error);
3805 g_error_free (error);
3812 g_file_real_query_info_async (GFile *file,
3813 const char *attributes,
3814 GFileQueryInfoFlags flags,
3816 GCancellable *cancellable,
3817 GAsyncReadyCallback callback,
3820 GSimpleAsyncResult *res;
3821 QueryInfoAsyncData *data;
3823 data = g_new0 (QueryInfoAsyncData, 1);
3824 data->attributes = g_strdup (attributes);
3825 data->flags = flags;
3827 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
3828 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
3830 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
3831 g_object_unref (res);
3835 g_file_real_query_info_finish (GFile *file,
3839 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3840 QueryInfoAsyncData *data;
3842 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
3844 data = g_simple_async_result_get_op_res_gpointer (simple);
3846 return g_object_ref (data->info);
3854 } QueryFilesystemInfoAsyncData;
3857 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
3860 g_object_unref (data->info);
3861 g_free (data->attributes);
3866 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
3868 GCancellable *cancellable)
3870 GError *error = NULL;
3871 QueryFilesystemInfoAsyncData *data;
3874 data = g_simple_async_result_get_op_res_gpointer (res);
3876 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
3880 g_simple_async_result_set_from_error (res, error);
3881 g_error_free (error);
3888 g_file_real_query_filesystem_info_async (GFile *file,
3889 const char *attributes,
3891 GCancellable *cancellable,
3892 GAsyncReadyCallback callback,
3895 GSimpleAsyncResult *res;
3896 QueryFilesystemInfoAsyncData *data;
3898 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
3899 data->attributes = g_strdup (attributes);
3901 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
3902 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
3904 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
3905 g_object_unref (res);
3909 g_file_real_query_filesystem_info_finish (GFile *file,
3913 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3914 QueryFilesystemInfoAsyncData *data;
3916 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
3918 data = g_simple_async_result_get_op_res_gpointer (simple);
3920 return g_object_ref (data->info);
3927 GFileQueryInfoFlags flags;
3928 GFileEnumerator *enumerator;
3929 } EnumerateChildrenAsyncData;
3932 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
3934 if (data->enumerator)
3935 g_object_unref (data->enumerator);
3936 g_free (data->attributes);
3941 enumerate_children_async_thread (GSimpleAsyncResult *res,
3943 GCancellable *cancellable)
3945 GError *error = NULL;
3946 EnumerateChildrenAsyncData *data;
3947 GFileEnumerator *enumerator;
3949 data = g_simple_async_result_get_op_res_gpointer (res);
3951 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
3953 if (enumerator == NULL)
3955 g_simple_async_result_set_from_error (res, error);
3956 g_error_free (error);
3959 data->enumerator = enumerator;
3963 g_file_real_enumerate_children_async (GFile *file,
3964 const char *attributes,
3965 GFileQueryInfoFlags flags,
3967 GCancellable *cancellable,
3968 GAsyncReadyCallback callback,
3971 GSimpleAsyncResult *res;
3972 EnumerateChildrenAsyncData *data;
3974 data = g_new0 (EnumerateChildrenAsyncData, 1);
3975 data->attributes = g_strdup (attributes);
3976 data->flags = flags;
3978 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
3979 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
3981 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
3982 g_object_unref (res);
3985 static GFileEnumerator *
3986 g_file_real_enumerate_children_finish (GFile *file,
3990 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3991 EnumerateChildrenAsyncData *data;
3993 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
3995 data = g_simple_async_result_get_op_res_gpointer (simple);
3996 if (data->enumerator)
3997 return g_object_ref (data->enumerator);
4003 open_read_async_thread (GSimpleAsyncResult *res,
4005 GCancellable *cancellable)
4008 GFileInputStream *stream;
4009 GError *error = NULL;
4011 iface = G_FILE_GET_IFACE (object);
4013 stream = iface->read_fn (G_FILE (object), cancellable, &error);
4017 g_simple_async_result_set_from_error (res, error);
4018 g_error_free (error);
4021 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4025 g_file_real_read_async (GFile *file,
4027 GCancellable *cancellable,
4028 GAsyncReadyCallback callback,
4031 GSimpleAsyncResult *res;
4033 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
4035 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
4036 g_object_unref (res);
4039 static GFileInputStream *
4040 g_file_real_read_finish (GFile *file,
4044 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4047 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
4049 op = g_simple_async_result_get_op_res_gpointer (simple);
4051 return g_object_ref (op);
4057 append_to_async_thread (GSimpleAsyncResult *res,
4059 GCancellable *cancellable)
4062 GFileCreateFlags *data;
4063 GFileOutputStream *stream;
4064 GError *error = NULL;
4066 iface = G_FILE_GET_IFACE (object);
4068 data = g_simple_async_result_get_op_res_gpointer (res);
4070 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
4074 g_simple_async_result_set_from_error (res, error);
4075 g_error_free (error);
4078 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4082 g_file_real_append_to_async (GFile *file,
4083 GFileCreateFlags flags,
4085 GCancellable *cancellable,
4086 GAsyncReadyCallback callback,
4089 GFileCreateFlags *data;
4090 GSimpleAsyncResult *res;
4092 data = g_new0 (GFileCreateFlags, 1);
4095 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
4096 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4098 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
4099 g_object_unref (res);
4102 static GFileOutputStream *
4103 g_file_real_append_to_finish (GFile *file,
4107 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4110 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
4112 op = g_simple_async_result_get_op_res_gpointer (simple);
4114 return g_object_ref (op);
4120 create_async_thread (GSimpleAsyncResult *res,
4122 GCancellable *cancellable)
4125 GFileCreateFlags *data;
4126 GFileOutputStream *stream;
4127 GError *error = NULL;
4129 iface = G_FILE_GET_IFACE (object);
4131 data = g_simple_async_result_get_op_res_gpointer (res);
4133 stream = iface->create (G_FILE (object), *data, cancellable, &error);
4137 g_simple_async_result_set_from_error (res, error);
4138 g_error_free (error);
4141 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
4145 g_file_real_create_async (GFile *file,
4146 GFileCreateFlags flags,
4148 GCancellable *cancellable,
4149 GAsyncReadyCallback callback,
4152 GFileCreateFlags *data;
4153 GSimpleAsyncResult *res;
4155 data = g_new0 (GFileCreateFlags, 1);
4158 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
4159 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
4161 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
4162 g_object_unref (res);
4165 static GFileOutputStream *
4166 g_file_real_create_finish (GFile *file,
4170 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4173 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
4175 op = g_simple_async_result_get_op_res_gpointer (simple);
4177 return g_object_ref (op);
4183 GFileOutputStream *stream;
4185 gboolean make_backup;
4186 GFileCreateFlags flags;
4190 replace_async_data_free (ReplaceAsyncData *data)
4193 g_object_unref (data->stream);
4194 g_free (data->etag);
4199 replace_async_thread (GSimpleAsyncResult *res,
4201 GCancellable *cancellable)
4204 GFileOutputStream *stream;
4205 GError *error = NULL;
4206 ReplaceAsyncData *data;
4208 iface = G_FILE_GET_IFACE (object);
4210 data = g_simple_async_result_get_op_res_gpointer (res);
4212 stream = iface->replace (G_FILE (object),
4221 g_simple_async_result_set_from_error (res, error);
4222 g_error_free (error);
4225 data->stream = stream;
4229 g_file_real_replace_async (GFile *file,
4231 gboolean make_backup,
4232 GFileCreateFlags flags,
4234 GCancellable *cancellable,
4235 GAsyncReadyCallback callback,
4238 GSimpleAsyncResult *res;
4239 ReplaceAsyncData *data;
4241 data = g_new0 (ReplaceAsyncData, 1);
4242 data->etag = g_strdup (etag);
4243 data->make_backup = make_backup;
4244 data->flags = flags;
4246 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
4247 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
4249 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
4250 g_object_unref (res);
4253 static GFileOutputStream *
4254 g_file_real_replace_finish (GFile *file,
4258 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4259 ReplaceAsyncData *data;
4261 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
4263 data = g_simple_async_result_get_op_res_gpointer (simple);
4265 return g_object_ref (data->stream);
4273 } SetDisplayNameAsyncData;
4276 set_display_name_data_free (SetDisplayNameAsyncData *data)
4278 g_free (data->name);
4280 g_object_unref (data->file);
4285 set_display_name_async_thread (GSimpleAsyncResult *res,
4287 GCancellable *cancellable)
4289 GError *error = NULL;
4290 SetDisplayNameAsyncData *data;
4293 data = g_simple_async_result_get_op_res_gpointer (res);
4295 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
4299 g_simple_async_result_set_from_error (res, error);
4300 g_error_free (error);
4307 g_file_real_set_display_name_async (GFile *file,
4308 const char *display_name,
4310 GCancellable *cancellable,
4311 GAsyncReadyCallback callback,
4314 GSimpleAsyncResult *res;
4315 SetDisplayNameAsyncData *data;
4317 data = g_new0 (SetDisplayNameAsyncData, 1);
4318 data->name = g_strdup (display_name);
4320 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
4321 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
4323 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
4324 g_object_unref (res);
4328 g_file_real_set_display_name_finish (GFile *file,
4332 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4333 SetDisplayNameAsyncData *data;
4335 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
4337 data = g_simple_async_result_get_op_res_gpointer (simple);
4339 return g_object_ref (data->file);
4345 GFileQueryInfoFlags flags;
4352 set_info_data_free (SetInfoAsyncData *data)
4355 g_object_unref (data->info);
4357 g_error_free (data->error);
4362 set_info_async_thread (GSimpleAsyncResult *res,
4364 GCancellable *cancellable)
4366 SetInfoAsyncData *data;
4368 data = g_simple_async_result_get_op_res_gpointer (res);
4371 data->res = g_file_set_attributes_from_info (G_FILE (object),
4379 g_file_real_set_attributes_async (GFile *file,
4381 GFileQueryInfoFlags flags,
4383 GCancellable *cancellable,
4384 GAsyncReadyCallback callback,
4387 GSimpleAsyncResult *res;
4388 SetInfoAsyncData *data;
4390 data = g_new0 (SetInfoAsyncData, 1);
4391 data->info = g_file_info_dup (info);
4392 data->flags = flags;
4394 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
4395 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
4397 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
4398 g_object_unref (res);
4402 g_file_real_set_attributes_finish (GFile *file,
4407 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4408 SetInfoAsyncData *data;
4410 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
4412 data = g_simple_async_result_get_op_res_gpointer (simple);
4415 *info = g_object_ref (data->info);
4417 if (error != NULL && data->error)
4418 *error = g_error_copy (data->error);
4424 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
4426 GCancellable *cancellable)
4428 GError *error = NULL;
4431 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
4435 g_simple_async_result_set_from_error (res, error);
4436 g_error_free (error);
4439 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
4443 g_file_real_find_enclosing_mount_async (GFile *file,
4445 GCancellable *cancellable,
4446 GAsyncReadyCallback callback,
4449 GSimpleAsyncResult *res;
4451 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
4453 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
4454 g_object_unref (res);
4458 g_file_real_find_enclosing_mount_finish (GFile *file,
4462 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4465 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
4467 mount = g_simple_async_result_get_op_res_gpointer (simple);
4468 return g_object_ref (mount);
4475 GFileCopyFlags flags;
4476 GFileProgressCallback progress_cb;
4477 gpointer progress_cb_data;
4478 GIOSchedulerJob *job;
4482 copy_async_data_free (CopyAsyncData *data)
4484 g_object_unref (data->source);
4485 g_object_unref (data->destination);
4490 CopyAsyncData *data;
4491 goffset current_num_bytes;
4492 goffset total_num_bytes;
4496 copy_async_progress_in_main (gpointer user_data)
4498 ProgressData *progress = user_data;
4499 CopyAsyncData *data = progress->data;
4501 data->progress_cb (progress->current_num_bytes,
4502 progress->total_num_bytes,
4503 data->progress_cb_data);
4509 mainloop_barrier (gpointer user_data)
4511 /* Does nothing, but ensures all queued idles before
4518 copy_async_progress_callback (goffset current_num_bytes,
4519 goffset total_num_bytes,
4522 CopyAsyncData *data = user_data;
4523 ProgressData *progress;
4525 progress = g_new (ProgressData, 1);
4526 progress->data = data;
4527 progress->current_num_bytes = current_num_bytes;
4528 progress->total_num_bytes = total_num_bytes;
4530 g_io_scheduler_job_send_to_mainloop_async (data->job,
4531 copy_async_progress_in_main,
4537 copy_async_thread (GIOSchedulerJob *job,
4538 GCancellable *cancellable,
4541 GSimpleAsyncResult *res;
4542 CopyAsyncData *data;
4547 data = g_simple_async_result_get_op_res_gpointer (res);
4551 result = g_file_copy (data->source,
4555 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
4559 /* Ensure all progress callbacks are done running in main thread */
4560 if (data->progress_cb != NULL)
4561 g_io_scheduler_job_send_to_mainloop (job,
4567 g_simple_async_result_set_from_error (res, error);
4568 g_error_free (error);
4571 g_simple_async_result_complete_in_idle (res);
4577 g_file_real_copy_async (GFile *source,
4579 GFileCopyFlags flags,
4581 GCancellable *cancellable,
4582 GFileProgressCallback progress_callback,
4583 gpointer progress_callback_data,
4584 GAsyncReadyCallback callback,
4587 GSimpleAsyncResult *res;
4588 CopyAsyncData *data;
4590 data = g_new0 (CopyAsyncData, 1);
4591 data->source = g_object_ref (source);
4592 data->destination = g_object_ref (destination);
4593 data->flags = flags;
4594 data->progress_cb = progress_callback;
4595 data->progress_cb_data = progress_callback_data;
4597 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
4598 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
4600 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
4604 g_file_real_copy_finish (GFile *file,
4608 /* Error handled in g_file_copy_finish() */
4613 /********************************************
4614 * Default VFS operations *
4615 ********************************************/
4618 * g_file_new_for_path:
4619 * @path: a string containing a relative or absolute path.
4621 * Constructs a #GFile for a given path. This operation never
4622 * fails, but the returned object might not support any I/O
4623 * operation if @path is malformed.
4625 * Returns: a new #GFile for the given @path.
4628 g_file_new_for_path (const char *path)
4630 g_return_val_if_fail (path != NULL, NULL);
4632 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
4636 * g_file_new_for_uri:
4637 * @uri: a string containing a URI.
4639 * Constructs a #GFile for a given URI. This operation never
4640 * fails, but the returned object might not support any I/O
4641 * operation if @uri is malformed or if the uri type is
4644 * Returns: a #GFile for the given @uri.
4647 g_file_new_for_uri (const char *uri)
4649 g_return_val_if_fail (uri != NULL, NULL);
4651 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
4655 * g_file_parse_name:
4656 * @parse_name: a file name or path to be parsed.
4658 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
4659 * This operation never fails, but the returned object might not support any I/O
4660 * operation if the @parse_name cannot be parsed.
4662 * Returns: a new #GFile.
4665 g_file_parse_name (const char *parse_name)
4667 g_return_val_if_fail (parse_name != NULL, NULL);
4669 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
4673 is_valid_scheme_character (char c)
4675 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
4679 has_valid_scheme (const char *uri)
4685 if (!is_valid_scheme_character (*p))
4690 } while (is_valid_scheme_character (*p));
4696 * g_file_new_for_commandline_arg:
4697 * @arg: a command line string.
4699 * Creates a #GFile with the given argument from the command line. The value of
4700 * @arg can be either a URI, an absolute path or a relative path resolved
4701 * relative to the current working directory.
4702 * This operation never fails, but the returned object might not support any
4703 * I/O operation if @arg points to a malformed path.
4705 * Returns: a new #GFile.
4708 g_file_new_for_commandline_arg (const char *arg)
4714 g_return_val_if_fail (arg != NULL, NULL);
4716 if (g_path_is_absolute (arg))
4717 return g_file_new_for_path (arg);
4719 if (has_valid_scheme (arg))
4720 return g_file_new_for_uri (arg);
4722 current_dir = g_get_current_dir ();
4723 filename = g_build_filename (current_dir, arg, NULL);
4724 g_free (current_dir);
4726 file = g_file_new_for_path (filename);
4733 * g_file_mount_enclosing_volume:
4734 * @location: input #GFile.
4735 * @flags: flags affecting the operation
4736 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
4737 * @cancellable: optional #GCancellable object, %NULL to ignore.
4738 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4739 * @user_data: the data to pass to callback function
4741 * Starts a @mount_operation, mounting the volume that contains the file @location.
4743 * When this operation has completed, @callback will be called with
4744 * @user_user data, and the operation can be finalized with
4745 * g_file_mount_enclosing_volume_finish().
4747 * If @cancellable is not %NULL, then the operation can be cancelled by
4748 * triggering the cancellable object from another thread. If the operation
4749 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4752 g_file_mount_enclosing_volume (GFile *location,
4753 GMountMountFlags flags,
4754 GMountOperation *mount_operation,
4755 GCancellable *cancellable,
4756 GAsyncReadyCallback callback,
4761 g_return_if_fail (G_IS_FILE (location));
4763 iface = G_FILE_GET_IFACE (location);
4765 if (iface->mount_enclosing_volume == NULL)
4767 g_simple_async_report_error_in_idle (G_OBJECT (location),
4768 callback, user_data,
4769 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4770 _("volume doesn't implement mount"));
4775 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
4780 * g_file_mount_enclosing_volume_finish:
4781 * @location: input #GFile.
4782 * @result: a #GAsyncResult.
4783 * @error: a #GError, or %NULL
4785 * Finishes a mount operation started by g_file_mount_enclosing_volume().
4787 * Returns: %TRUE if successful. If an error
4788 * has occurred, this function will return %FALSE and set @error
4789 * appropriately if present.
4792 g_file_mount_enclosing_volume_finish (GFile *location,
4793 GAsyncResult *result,
4798 g_return_val_if_fail (G_IS_FILE (location), FALSE);
4799 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4801 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4803 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4804 if (g_simple_async_result_propagate_error (simple, error))
4808 iface = G_FILE_GET_IFACE (location);
4810 return (* iface->mount_enclosing_volume_finish) (location, result, error);
4813 /********************************************
4814 * Utility functions *
4815 ********************************************/
4818 * g_file_query_default_handler:
4819 * @file: a #GFile to open.
4820 * @cancellable: optional #GCancellable object, %NULL to ignore.
4821 * @error: a #GError, or %NULL
4823 * Returns the #GAppInfo that is registered as the default
4824 * application to handle the file specified by @file.
4826 * If @cancellable is not %NULL, then the operation can be cancelled by
4827 * triggering the cancellable object from another thread. If the operation
4828 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4830 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
4831 * When you are done with it, release it with g_object_unref()
4834 g_file_query_default_handler (GFile *file,
4835 GCancellable *cancellable,
4839 const char *content_type;
4844 uri_scheme = g_file_get_uri_scheme (file);
4845 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
4846 g_free (uri_scheme);
4848 if (appinfo != NULL)
4851 info = g_file_query_info (file,
4852 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
4861 content_type = g_file_info_get_content_type (info);
4864 /* Don't use is_native(), as we want to support fuse paths if availible */
4865 path = g_file_get_path (file);
4866 appinfo = g_app_info_get_default_for_type (content_type,
4871 g_object_unref (info);
4873 if (appinfo != NULL)
4876 g_set_error (error, G_IO_ERROR,
4877 G_IO_ERROR_NOT_SUPPORTED,
4878 _("No application is registered as handling this file"));
4884 #define GET_CONTENT_BLOCK_SIZE 8192
4887 * g_file_load_contents:
4888 * @file: input #GFile.
4889 * @cancellable: optional #GCancellable object, %NULL to ignore.
4890 * @contents: a location to place the contents of the file.
4891 * @length: a location to place the length of the contents of the file.
4892 * @etag_out: a location to place the current entity tag for the file.
4893 * @error: a #GError, or %NULL
4895 * Loads the content of the file into memory, returning the size of
4896 * the data. The data is always zero terminated, but this is not
4897 * included in the resultant @length.
4899 * If @cancellable is not %NULL, then the operation can be cancelled by
4900 * triggering the cancellable object from another thread. If the operation
4901 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4903 * Returns: %TRUE if the @file's contents were successfully loaded.
4904 * %FALSE if there were errors..
4907 g_file_load_contents (GFile *file,
4908 GCancellable *cancellable,
4914 GFileInputStream *in;
4915 GByteArray *content;
4920 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4921 g_return_val_if_fail (contents != NULL, FALSE);
4923 in = g_file_read (file, cancellable, error);
4927 content = g_byte_array_new ();
4930 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4931 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
4932 content->data + pos,
4933 GET_CONTENT_BLOCK_SIZE,
4934 cancellable, error)) > 0)
4937 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
4944 info = g_file_input_stream_query_info (in,
4945 G_FILE_ATTRIBUTE_ETAG_VALUE,
4950 *etag_out = g_strdup (g_file_info_get_etag (info));
4951 g_object_unref (info);
4955 /* Ignore errors on close */
4956 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
4957 g_object_unref (in);
4961 /* error is set already */
4962 g_byte_array_free (content, TRUE);
4969 /* Zero terminate (we got an extra byte allocated for this */
4970 content->data[pos] = 0;
4972 *contents = (char *)g_byte_array_free (content, FALSE);
4980 GCancellable *cancellable;
4981 GFileReadMoreCallback read_more_callback;
4982 GAsyncReadyCallback callback;
4984 GByteArray *content;
4991 load_contents_data_free (LoadContentsData *data)
4994 g_error_free (data->error);
4995 if (data->cancellable)
4996 g_object_unref (data->cancellable);
4998 g_byte_array_free (data->content, TRUE);
4999 g_free (data->etag);
5000 g_object_unref (data->file);
5005 load_contents_close_callback (GObject *obj,
5006 GAsyncResult *close_res,
5009 GInputStream *stream = G_INPUT_STREAM (obj);
5010 LoadContentsData *data = user_data;
5011 GSimpleAsyncResult *res;
5013 /* Ignore errors here, we're only reading anyway */
5014 g_input_stream_close_finish (stream, close_res, NULL);
5015 g_object_unref (stream);
5017 res = g_simple_async_result_new (G_OBJECT (data->file),
5020 g_file_load_contents_async);
5021 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
5022 g_simple_async_result_complete (res);
5023 g_object_unref (res);
5027 load_contents_fstat_callback (GObject *obj,
5028 GAsyncResult *stat_res,
5031 GInputStream *stream = G_INPUT_STREAM (obj);
5032 LoadContentsData *data = user_data;
5035 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
5039 data->etag = g_strdup (g_file_info_get_etag (info));
5040 g_object_unref (info);
5043 g_input_stream_close_async (stream, 0,
5045 load_contents_close_callback, data);
5049 load_contents_read_callback (GObject *obj,
5050 GAsyncResult *read_res,
5053 GInputStream *stream = G_INPUT_STREAM (obj);
5054 LoadContentsData *data = user_data;
5055 GError *error = NULL;
5058 read_size = g_input_stream_read_finish (stream, read_res, &error);
5062 /* Error or EOF, close the file */
5063 data->error = error;
5064 g_input_stream_close_async (stream, 0,
5066 load_contents_close_callback, data);
5068 else if (read_size == 0)
5070 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5071 G_FILE_ATTRIBUTE_ETAG_VALUE,
5074 load_contents_fstat_callback,
5077 else if (read_size > 0)
5079 data->pos += read_size;
5081 g_byte_array_set_size (data->content,
5082 data->pos + GET_CONTENT_BLOCK_SIZE);
5085 if (data->read_more_callback &&
5086 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
5087 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
5088 G_FILE_ATTRIBUTE_ETAG_VALUE,
5091 load_contents_fstat_callback,
5094 g_input_stream_read_async (stream,
5095 data->content->data + data->pos,
5096 GET_CONTENT_BLOCK_SIZE,
5099 load_contents_read_callback,
5105 load_contents_open_callback (GObject *obj,
5106 GAsyncResult *open_res,
5109 GFile *file = G_FILE (obj);
5110 GFileInputStream *stream;
5111 LoadContentsData *data = user_data;
5112 GError *error = NULL;
5113 GSimpleAsyncResult *res;
5115 stream = g_file_read_finish (file, open_res, &error);
5119 g_byte_array_set_size (data->content,
5120 data->pos + GET_CONTENT_BLOCK_SIZE);
5121 g_input_stream_read_async (G_INPUT_STREAM (stream),
5122 data->content->data + data->pos,
5123 GET_CONTENT_BLOCK_SIZE,
5126 load_contents_read_callback,
5132 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5136 g_simple_async_result_complete (res);
5137 g_error_free (error);
5138 load_contents_data_free (data);
5139 g_object_unref (res);
5144 * g_file_load_partial_contents_async:
5145 * @file: input #GFile.
5146 * @cancellable: optional #GCancellable object, %NULL to ignore.
5147 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
5148 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5149 * @user_data: the data to pass to the callback functions.
5151 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
5152 * used to stop reading from the file when appropriate, else this function
5153 * will behave exactly as g_file_load_contents_async(). This operation
5154 * can be finished by g_file_load_partial_contents_finish().
5156 * Users of this function should be aware that @user_data is passed to
5157 * both the @read_more_callback and the @callback.
5159 * If @cancellable is not %NULL, then the operation can be cancelled by
5160 * triggering the cancellable object from another thread. If the operation
5161 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5164 g_file_load_partial_contents_async (GFile *file,
5165 GCancellable *cancellable,
5166 GFileReadMoreCallback read_more_callback,
5167 GAsyncReadyCallback callback,
5170 LoadContentsData *data;
5172 g_return_if_fail (G_IS_FILE (file));
5174 data = g_new0 (LoadContentsData, 1);
5177 data->cancellable = g_object_ref (cancellable);
5178 data->read_more_callback = read_more_callback;
5179 data->callback = callback;
5180 data->user_data = user_data;
5181 data->content = g_byte_array_new ();
5182 data->file = g_object_ref (file);
5184 g_file_read_async (file,
5187 load_contents_open_callback,
5192 * g_file_load_partial_contents_finish:
5193 * @file: input #GFile.
5194 * @res: a #GAsyncResult.
5195 * @contents: a location to place the contents of the file.
5196 * @length: a location to place the length of the contents of the file.
5197 * @etag_out: a location to place the current entity tag for the file.
5198 * @error: a #GError, or %NULL
5200 * Finishes an asynchronous partial load operation that was started
5201 * with g_file_load_partial_contents_async().
5203 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5204 * present, it will be set appropriately.
5207 g_file_load_partial_contents_finish (GFile *file,
5214 GSimpleAsyncResult *simple;
5215 LoadContentsData *data;
5217 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5218 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5219 g_return_val_if_fail (contents != NULL, FALSE);
5221 simple = G_SIMPLE_ASYNC_RESULT (res);
5223 if (g_simple_async_result_propagate_error (simple, error))
5226 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
5228 data = g_simple_async_result_get_op_res_gpointer (simple);
5232 g_propagate_error (error, data->error);
5241 *length = data->pos;
5245 *etag_out = data->etag;
5249 /* Zero terminate */
5250 g_byte_array_set_size (data->content, data->pos + 1);
5251 data->content->data[data->pos] = 0;
5253 *contents = (char *)g_byte_array_free (data->content, FALSE);
5254 data->content = NULL;
5260 * g_file_load_contents_async:
5261 * @file: input #GFile.
5262 * @cancellable: optional #GCancellable object, %NULL to ignore.
5263 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5264 * @user_data: the data to pass to callback function
5266 * Starts an asynchronous load of the @file's contents.
5268 * For more details, see g_file_load_contents() which is
5269 * the synchronous version of this call.
5271 * When the load operation has completed, @callback will be called
5272 * with @user data. To finish the operation, call
5273 * g_file_load_contents_finish() with the #GAsyncResult returned by
5276 * If @cancellable is not %NULL, then the operation can be cancelled by
5277 * triggering the cancellable object from another thread. If the operation
5278 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5281 g_file_load_contents_async (GFile *file,
5282 GCancellable *cancellable,
5283 GAsyncReadyCallback callback,
5286 g_file_load_partial_contents_async (file,
5289 callback, user_data);
5293 * g_file_load_contents_finish:
5294 * @file: input #GFile.
5295 * @res: a #GAsyncResult.
5296 * @contents: a location to place the contents of the file.
5297 * @length: a location to place the length of the contents of the file.
5298 * @etag_out: a location to place the current entity tag for the file.
5299 * @error: a #GError, or %NULL
5301 * Finishes an asynchronous load of the @file's contents.
5302 * The contents are placed in @contents, and @length is set to the
5303 * size of the @contents string. If @etag_out is present, it will be
5304 * set to the new entity tag for the @file.
5306 * Returns: %TRUE if the load was successful. If %FALSE and @error is
5307 * present, it will be set appropriately.
5310 g_file_load_contents_finish (GFile *file,
5317 return g_file_load_partial_contents_finish (file,
5326 * g_file_replace_contents:
5327 * @file: input #GFile.
5328 * @contents: a string containing the new contents for @file.
5329 * @length: the length of @contents in bytes.
5330 * @etag: the old <link linkend="gfile-etag">entity tag</link>
5332 * @make_backup: %TRUE if a backup should be created.
5333 * @flags: a set of #GFileCreateFlags.
5334 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
5335 * for the document. This should be freed with g_free() when no longer
5337 * @cancellable: optional #GCancellable object, %NULL to ignore.
5338 * @error: a #GError, or %NULL
5340 * Replaces the contents of @file with @contents of @length bytes.
5342 * If @etag is specified (not %NULL) any existing file must have that etag, or
5343 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
5345 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
5347 * If @cancellable is not %NULL, then the operation can be cancelled by
5348 * triggering the cancellable object from another thread. If the operation
5349 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5351 * The returned @new_etag can be used to verify that the file hasn't changed the
5352 * next time it is saved over.
5354 * Returns: %TRUE if successful. If an error
5355 * has occurred, this function will return %FALSE and set @error
5356 * appropriately if present.
5359 g_file_replace_contents (GFile *file,
5360 const char *contents,
5363 gboolean make_backup,
5364 GFileCreateFlags flags,
5366 GCancellable *cancellable,
5369 GFileOutputStream *out;
5370 gsize pos, remainder;
5373 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5374 g_return_val_if_fail (contents != NULL, FALSE);
5376 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
5382 while (remainder > 0 &&
5383 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
5385 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
5393 if (remainder > 0 && res < 0)
5395 /* Ignore errors on close */
5396 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
5398 /* error is set already */
5402 if (!g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error))
5406 *new_etag = g_file_output_stream_get_etag (out);
5414 GCancellable *cancellable;
5415 GAsyncReadyCallback callback;
5417 const char *content;
5421 } ReplaceContentsData;
5424 replace_contents_data_free (ReplaceContentsData *data)
5427 g_error_free (data->error);
5428 if (data->cancellable)
5429 g_object_unref (data->cancellable);
5430 g_object_unref (data->file);
5431 g_free (data->etag);
5436 replace_contents_close_callback (GObject *obj,
5437 GAsyncResult *close_res,
5440 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5441 ReplaceContentsData *data = user_data;
5442 GSimpleAsyncResult *res;
5444 /* Ignore errors here, we're only reading anyway */
5445 g_output_stream_close_finish (stream, close_res, NULL);
5446 g_object_unref (stream);
5448 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
5450 res = g_simple_async_result_new (G_OBJECT (data->file),
5453 g_file_replace_contents_async);
5454 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
5455 g_simple_async_result_complete (res);
5456 g_object_unref (res);
5460 replace_contents_write_callback (GObject *obj,
5461 GAsyncResult *read_res,
5464 GOutputStream *stream = G_OUTPUT_STREAM (obj);
5465 ReplaceContentsData *data = user_data;
5466 GError *error = NULL;
5469 write_size = g_output_stream_write_finish (stream, read_res, &error);
5471 if (write_size <= 0)
5473 /* Error or EOF, close the file */
5475 data->error = error;
5476 g_output_stream_close_async (stream, 0,
5478 replace_contents_close_callback, data);
5480 else if (write_size > 0)
5482 data->pos += write_size;
5484 if (data->pos >= data->length)
5485 g_output_stream_close_async (stream, 0,
5487 replace_contents_close_callback, data);
5489 g_output_stream_write_async (stream,
5490 data->content + data->pos,
5491 data->length - data->pos,
5494 replace_contents_write_callback,
5500 replace_contents_open_callback (GObject *obj,
5501 GAsyncResult *open_res,
5504 GFile *file = G_FILE (obj);
5505 GFileOutputStream *stream;
5506 ReplaceContentsData *data = user_data;
5507 GError *error = NULL;
5508 GSimpleAsyncResult *res;
5510 stream = g_file_replace_finish (file, open_res, &error);
5514 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
5515 data->content + data->pos,
5516 data->length - data->pos,
5519 replace_contents_write_callback,
5525 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
5529 g_simple_async_result_complete (res);
5530 g_error_free (error);
5531 replace_contents_data_free (data);
5532 g_object_unref (res);
5537 * g_file_replace_contents_async:
5538 * @file: input #GFile.
5539 * @contents: string of contents to replace the file with.
5540 * @length: the length of @contents in bytes.
5541 * @etag: a new <link linkend="gfile-etag">entity tag</link> for the @file.
5542 * @make_backup: %TRUE if a backup should be created.
5543 * @flags: a set of #GFileCreateFlags.
5544 * @cancellable: optional #GCancellable object, %NULL to ignore.
5545 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
5546 * @user_data: the data to pass to callback function
5548 * Starts an asynchronous replacement of @file with the given
5549 * @contents of @length bytes. @etag will replace the document's
5550 * current entity tag.
5552 * When this operation has completed, @callback will be called with
5553 * @user_user data, and the operation can be finalized with
5554 * g_file_replace_contents_finish().
5556 * If @cancellable is not %NULL, then the operation can be cancelled by
5557 * triggering the cancellable object from another thread. If the operation
5558 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5560 * If @make_backup is %TRUE, this function will attempt to
5561 * make a backup of @file.
5564 g_file_replace_contents_async (GFile *file,
5565 const char *contents,
5568 gboolean make_backup,
5569 GFileCreateFlags flags,
5570 GCancellable *cancellable,
5571 GAsyncReadyCallback callback,
5574 ReplaceContentsData *data;
5576 g_return_if_fail (G_IS_FILE (file));
5577 g_return_if_fail (contents != NULL);
5579 data = g_new0 (ReplaceContentsData, 1);
5582 data->cancellable = g_object_ref (cancellable);
5583 data->callback = callback;
5584 data->user_data = user_data;
5585 data->content = contents;
5586 data->length = length;
5588 data->file = g_object_ref (file);
5590 g_file_replace_async (file,
5596 replace_contents_open_callback,
5601 * g_file_replace_contents_finish:
5602 * @file: input #GFile.
5603 * @res: a #GAsyncResult.
5604 * @new_etag: a location of a new <link linkend="gfile-etag">entity tag</link>
5605 * for the document. This should be freed with g_free() when it is no
5607 * @error: a #GError, or %NULL
5609 * Finishes an asynchronous replace of the given @file. See
5610 * g_file_replace_contents_async(). Sets @new_etag to the new entity
5611 * tag for the document, if present.
5613 * Returns: %TRUE on success, %FALSE on failure.
5616 g_file_replace_contents_finish (GFile *file,
5621 GSimpleAsyncResult *simple;
5622 ReplaceContentsData *data;
5624 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5625 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
5627 simple = G_SIMPLE_ASYNC_RESULT (res);
5629 if (g_simple_async_result_propagate_error (simple, error))
5632 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
5634 data = g_simple_async_result_get_op_res_gpointer (simple);
5638 g_propagate_error (error, data->error);
5646 *new_etag = data->etag;
5647 data->etag = NULL; /* Take ownership */
5653 #define __G_FILE_C__
5654 #include "gioaliasdef.c"