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 interanally, but as #GFile<!-- -->s
63 * are extensible it could also be something else that corresponds to a pathname
64 * in a userspace implementation of a filesystem.
66 * #GFile<!-- -->s make up hierarchies of directories and files that correspond to the
67 * files on a filesystem. You can move through the file system with #GFile using
68 * g_file_get_parent() to get an identifier for the parent directory, g_file_get_child()
69 * to get a child within a directory, g_file_resolve_relative_path() to resolve a relative
70 * path between two #GFile<!-- -->s. There can be multiple hierarchies, so you may not
71 * end up at the same root if you repeatedly call g_file_get_parent() on two different
74 * All #GFile<!-- -->s have a basename (get with g_file_get_basename()). These names
75 * are byte strings that are used to identify the file on the filesystem (relative to
76 * its parent directory) and there is no guarantees that they have any particular charset
77 * encoding or even make any sense at all. If you want to use filenames in a user
78 * interface you should use the display name that you can get by requesting the
79 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
80 * This is guaranteed to be in utf8 and can be used in a user interface. But always
81 * store the real basename or the #GFile to use to actually access the file, because
82 * there is no way to go from a display name to the actual name.
84 * Using #GFile as an identifier has the same weaknesses as using a path in that
85 * there may be multiple aliases for the same file. For instance, hard or
86 * soft links may cause two different #GFile<!-- -->s to refer to the same file.
87 * Other possible causes for aliases are: case insensitive filesystems, short
88 * and long names on Fat/NTFS, or bind mounts in linux. If you want to check if
89 * two #GFile<!-- -->s point to the same file you can query for the
90 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
91 * canonicalization of pathnames passed in, so that trivial differences in the
92 * path string used at creation (dupplicated slashes, slash at end of path, "."
93 * or ".." path segments, etc) does not create different #GFile<!-- -->s.
95 * Many #GFile operations have both synchronous and asynchronous versions
96 * to suit your application. Asynchronous versions of synchronous functions
97 * simply have _async() appended to their function names. The asynchronous
98 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
99 * the operation, producing a GAsyncResult which is then passed to the
100 * function's matching _finish()
103 * Some #GFile operations do not have synchronous analogs, as they may
104 * take a very long time to finish, and blocking may leave an application
105 * unusable. Notable cases include:
106 * g_file_mount_mountable() to mount a mountable file.
107 * g_file_unmount_mountable() to unmount a mountable file.
108 * g_file_eject_mountable() to eject a mountable file.
110 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
111 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
112 * short. Entity tags are somewhat like a more abstract version of the
113 * traditional mtime, and can be used to quickly determine if the file has
114 * been modified from the version on the file system. See the HTTP 1.1
115 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
116 * for HTTP Etag headers, which are a very similar concept.
120 static void g_file_base_init (gpointer g_class);
121 static void g_file_class_init (gpointer g_class,
122 gpointer class_data);
124 static void g_file_real_query_info_async (GFile *file,
125 const char *attributes,
126 GFileQueryInfoFlags flags,
128 GCancellable *cancellable,
129 GAsyncReadyCallback callback,
131 static GFileInfo * g_file_real_query_info_finish (GFile *file,
134 static void g_file_real_query_filesystem_info_async (GFile *file,
135 const char *attributes,
137 GCancellable *cancellable,
138 GAsyncReadyCallback callback,
140 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
143 static void g_file_real_enumerate_children_async (GFile *file,
144 const char *attributes,
145 GFileQueryInfoFlags flags,
147 GCancellable *cancellable,
148 GAsyncReadyCallback callback,
150 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
153 static void g_file_real_read_async (GFile *file,
155 GCancellable *cancellable,
156 GAsyncReadyCallback callback,
158 static GFileInputStream * g_file_real_read_finish (GFile *file,
161 static void g_file_real_append_to_async (GFile *file,
162 GFileCreateFlags flags,
164 GCancellable *cancellable,
165 GAsyncReadyCallback callback,
167 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
170 static void g_file_real_create_async (GFile *file,
171 GFileCreateFlags flags,
173 GCancellable *cancellable,
174 GAsyncReadyCallback callback,
176 static GFileOutputStream *g_file_real_create_finish (GFile *file,
179 static void g_file_real_replace_async (GFile *file,
181 gboolean make_backup,
182 GFileCreateFlags flags,
184 GCancellable *cancellable,
185 GAsyncReadyCallback callback,
187 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
190 static gboolean g_file_real_set_attributes_from_info (GFile *file,
192 GFileQueryInfoFlags flags,
193 GCancellable *cancellable,
195 static void g_file_real_set_display_name_async (GFile *file,
196 const char *display_name,
198 GCancellable *cancellable,
199 GAsyncReadyCallback callback,
201 static GFile * g_file_real_set_display_name_finish (GFile *file,
204 static void g_file_real_set_attributes_async (GFile *file,
206 GFileQueryInfoFlags flags,
208 GCancellable *cancellable,
209 GAsyncReadyCallback callback,
211 static gboolean g_file_real_set_attributes_finish (GFile *file,
215 static void g_file_real_find_enclosing_mount_async (GFile *file,
217 GCancellable *cancellable,
218 GAsyncReadyCallback callback,
220 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
223 static void g_file_real_copy_async (GFile *source,
225 GFileCopyFlags flags,
227 GCancellable *cancellable,
228 GFileProgressCallback progress_callback,
229 gpointer progress_callback_data,
230 GAsyncReadyCallback callback,
232 static gboolean g_file_real_copy_finish (GFile *file,
237 g_file_get_type (void)
239 static GType file_type = 0;
243 static const GTypeInfo file_info =
245 sizeof (GFileIface), /* class_size */
246 g_file_base_init, /* base_init */
247 NULL, /* base_finalize */
249 NULL, /* class_finalize */
250 NULL, /* class_data */
257 g_type_register_static (G_TYPE_INTERFACE, I_("GFile"),
260 g_type_interface_add_prerequisite (file_type, G_TYPE_OBJECT);
267 g_file_class_init (gpointer g_class,
270 GFileIface *iface = g_class;
272 iface->enumerate_children_async = g_file_real_enumerate_children_async;
273 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
274 iface->set_display_name_async = g_file_real_set_display_name_async;
275 iface->set_display_name_finish = g_file_real_set_display_name_finish;
276 iface->query_info_async = g_file_real_query_info_async;
277 iface->query_info_finish = g_file_real_query_info_finish;
278 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
279 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
280 iface->set_attributes_async = g_file_real_set_attributes_async;
281 iface->set_attributes_finish = g_file_real_set_attributes_finish;
282 iface->read_async = g_file_real_read_async;
283 iface->read_finish = g_file_real_read_finish;
284 iface->append_to_async = g_file_real_append_to_async;
285 iface->append_to_finish = g_file_real_append_to_finish;
286 iface->create_async = g_file_real_create_async;
287 iface->create_finish = g_file_real_create_finish;
288 iface->replace_async = g_file_real_replace_async;
289 iface->replace_finish = g_file_real_replace_finish;
290 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
291 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
292 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
293 iface->copy_async = g_file_real_copy_async;
294 iface->copy_finish = g_file_real_copy_finish;
298 g_file_base_init (gpointer g_class)
305 * @file: input #GFile.
307 * Checks to see if a file is native to the platform.
309 * A native file s one expressed in the platform-native filename format,
310 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
311 * as it might be on a locally mounted remote filesystem.
313 * On some systems non-native files may be available using
314 * the native filesystem via a userspace filesystem (FUSE), in
315 * these cases this call will return %FALSE, but g_file_get_path()
316 * will still return a native path.
318 * This call does no blocking i/o.
320 * Returns: %TRUE if file is native.
323 g_file_is_native (GFile *file)
327 g_return_val_if_fail (G_IS_FILE (file), FALSE);
329 iface = G_FILE_GET_IFACE (file);
331 return (* iface->is_native) (file);
336 * g_file_has_uri_scheme:
337 * @file: input #GFile.
338 * @uri_scheme: a string containing a URI scheme.
340 * Checks to see if a #GFile has a given URI scheme.
342 * This call does no blocking i/o.
344 * Returns: %TRUE if #GFile's backend supports the
345 * given URI scheme, %FALSE if URI scheme is %NULL,
346 * not supported, or #GFile is invalid.
349 g_file_has_uri_scheme (GFile *file,
350 const char *uri_scheme)
354 g_return_val_if_fail (G_IS_FILE (file), FALSE);
355 g_return_val_if_fail (uri_scheme != NULL, FALSE);
357 iface = G_FILE_GET_IFACE (file);
359 return (* iface->has_uri_scheme) (file, uri_scheme);
364 * g_file_get_uri_scheme:
365 * @file: input #GFile.
367 * Gets the URI scheme for a #GFile.
368 * RFC 3986 decodes the scheme as:
370 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
372 * Common schemes include "file", "http", "ftp", etc.
374 * This call does no blocking i/o.
376 * Returns: a string containing the URI scheme for the given
377 * #GFile. The returned string should be freed with g_free()
378 * when no longer needed.
381 g_file_get_uri_scheme (GFile *file)
385 g_return_val_if_fail (G_IS_FILE (file), NULL);
387 iface = G_FILE_GET_IFACE (file);
389 return (* iface->get_uri_scheme) (file);
394 * g_file_get_basename:
395 * @file: input #GFile.
397 * Gets the base name (the last component of the path) for a given #GFile.
399 * If called for the top level of a system (such as the filesystem root
400 * or a uri like sftp://host/) it will return a single directory separator
401 * (and on Windows, possibly a drive letter).
403 * The base name is a byte string (*not* UTF-8). It has no defined encoding
404 * or rules other than it may not contain zero bytes. If you want to use
405 * filenames in a user interface you should use the display name that you
406 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
407 * attribute with g_file_query_info().
409 * This call does no blocking i/o.
411 * Returns: string containing the #GFile's base name, or %NULL
412 * if given #GFile is invalid. The returned string should be
413 * freed with g_free() when no longer needed.
416 g_file_get_basename (GFile *file)
420 g_return_val_if_fail (G_IS_FILE (file), NULL);
422 iface = G_FILE_GET_IFACE (file);
424 return (* iface->get_basename) (file);
429 * @file: input #GFile.
431 * Gets the local pathname for #GFile, if one exists.
433 * This call does no blocking i/o.
435 * Returns: string containing the #GFile's path, or %NULL if
436 * no such path exists. The returned string should be
437 * freed with g_free() when no longer needed.
440 g_file_get_path (GFile *file)
444 g_return_val_if_fail (G_IS_FILE (file), NULL);
446 iface = G_FILE_GET_IFACE (file);
448 return (* iface->get_path) (file);
453 * @file: input #GFile.
455 * Gets the URI for the @file.
457 * This call does no blocking i/o.
459 * Returns: a string containing the #GFile's URI.
460 * The returned string should be freed with g_free() when no longer needed.
463 g_file_get_uri (GFile *file)
467 g_return_val_if_fail (G_IS_FILE (file), NULL);
469 iface = G_FILE_GET_IFACE (file);
471 return (* iface->get_uri) (file);
475 * g_file_get_parse_name:
476 * @file: input #GFile.
478 * Gets the parse name of the @file.
479 * A parse name is a UTF-8 string that describes the
480 * file such that one can get the #GFile back using
481 * g_file_parse_name().
483 * This is generally used to show the #GFile as a nice
484 * full-pathname kind of string in a user interface,
485 * like in a location entry.
487 * For local files with names that can safely be converted
488 * to UTF8 the pathname is used, otherwise the IRI is used
489 * (a form of URI that allows UTF8 characters unescaped).
491 * This call does no blocking i/o.
493 * Returns: a string containing the #GFile's parse name. The returned
494 * string should be freed with g_free() when no longer needed.
497 g_file_get_parse_name (GFile *file)
501 g_return_val_if_fail (G_IS_FILE (file), NULL);
503 iface = G_FILE_GET_IFACE (file);
505 return (* iface->get_parse_name) (file);
510 * @file: input #GFile.
512 * Duplicates a #GFile handle. This operation does not duplicate
513 * the actual file or directory represented by the #GFile; see
514 * g_file_copy() if attempting to copy a file.
516 * This call does no blocking i/o.
518 * Returns: #GFile that is a duplicate of the given #GFile.
521 g_file_dup (GFile *file)
525 g_return_val_if_fail (G_IS_FILE (file), NULL);
527 iface = G_FILE_GET_IFACE (file);
529 return (* iface->dup) (file);
534 * @file: #gconstpointer to a #GFile.
536 * Creates a hash value for a #GFile.
538 * This call does no blocking i/o.
540 * Returns: 0 if @file is not a valid #GFile, otherwise an
541 * integer that can be used as hash value for the #GFile.
542 * This function is intended for easily hashing a #GFile to
543 * add to a #GHashTable or similar data structure.
546 g_file_hash (gconstpointer file)
550 g_return_val_if_fail (G_IS_FILE (file), 0);
552 iface = G_FILE_GET_IFACE (file);
554 return (* iface->hash) ((GFile *)file);
559 * @file1: the first #GFile.
560 * @file2: the second #GFile.
562 * Checks equality of two given #GFile<!-- -->s. Note that two
563 * #GFile<!-- -->s that differ can still refer to the same
564 * file on the filesystem due to various forms of filename
567 * This call does no blocking i/o.
569 * Returns: %TRUE if @file1 and @file2 are equal.
570 * %FALSE if either is not a #GFile.
573 g_file_equal (GFile *file1,
578 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
579 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
581 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
584 iface = G_FILE_GET_IFACE (file1);
586 return (* iface->equal) (file1, file2);
592 * @file: input #GFile.
594 * Gets the parent directory for the @file.
595 * If the @file represents the root directory of the
596 * file system, then %NULL will be returned.
598 * This call does no blocking i/o.
600 * Returns: a #GFile structure to the parent of the given
601 * #GFile or %NULL if there is no parent.
604 g_file_get_parent (GFile *file)
608 g_return_val_if_fail (G_IS_FILE (file), NULL);
610 iface = G_FILE_GET_IFACE (file);
612 return (* iface->get_parent) (file);
617 * @file: input #GFile.
618 * @name: string containing the child's basename.
620 * Gets a child of @file with basename equal to @name.
622 * Note that the file with that specific name might not exist, but
623 * you can still have a #GFile that points to it. You can use this
624 * for instance to create that file.
626 * This call does no blocking i/o.
628 * Returns: a #GFile to a child specified by @name.
631 g_file_get_child (GFile *file,
634 g_return_val_if_fail (G_IS_FILE (file), NULL);
635 g_return_val_if_fail (name != NULL, NULL);
637 return g_file_resolve_relative_path (file, name);
641 * g_file_get_child_for_display_name:
642 * @file: input #GFile.
643 * @display_name: string to a possible child.
646 * Gets the child of @file for a given @display_name (i.e. a UTF8
647 * version of the name). If this function fails, it returns %NULL and @error will be
648 * set. This is very useful when constructing a GFile for a new file
649 * and the user entered the filename in the user interface, for instance
650 * when you select a directory and type a filename in the file selector.
652 * This call does no blocking i/o.
654 * Returns: a #GFile to the specified child, or
655 * %NULL if the display name couldn't be converted.
658 g_file_get_child_for_display_name (GFile *file,
659 const char *display_name,
664 g_return_val_if_fail (G_IS_FILE (file), NULL);
665 g_return_val_if_fail (display_name != NULL, NULL);
667 iface = G_FILE_GET_IFACE (file);
669 return (* iface->get_child_for_display_name) (file, display_name, error);
672 #undef g_file_contains_file
675 * g_file_contains_file:
676 * @parent: input #GFile.
677 * @descendant: input #GFile.
679 * Deprecated version of g_file_has_prefix().
681 * Returns: %TRUE if the @descendant's parent, grandparent, etc is @parent. %FALSE otherwise.
683 * Deprecated:2.16: The initial chosen name was unfortunate, as it
684 * may cause you to think this function did more than just
685 * filename comparisons.
688 g_file_contains_file (GFile *parent,
691 /* This function is not in the header and will not be referenced by newly built code */
692 return g_file_has_prefix (descendant, parent);
697 * @file: input #GFile.
698 * @prefix: input #GFile.
700 * Checks whether @file has the prefix specified by @prefix. In other word, if the
701 * names of inital elements of @file<!-- -->s pathname match @prefix.
703 * This call does no i/o, as it works purely on names. As such it can sometimes
704 * return %FALSE even if @file is inside a @prefix (from a filesystem point of view),
705 * because the prefix of @file is an alias of @prefix.
707 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix. %FALSE otherwise.
710 g_file_has_prefix (GFile *file,
715 g_return_val_if_fail (G_IS_FILE (file), FALSE);
716 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
718 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
721 iface = G_FILE_GET_IFACE (file);
723 /* The vtable function differs in arg order since we're
724 using the old contains_file call */
725 return (* iface->prefix_matches) (prefix, file);
729 * g_file_get_relative_path:
730 * @parent: input #GFile.
731 * @descendant: input #GFile.
733 * Gets the path for @descendant relative to @parent.
735 * This call does no blocking i/o.
737 * Returns: string with the relative path from @descendant
738 * to @parent, or %NULL if @descendant doesn't have @parent as prefix. The returned string should be freed with
739 * g_free() when no longer needed.
742 g_file_get_relative_path (GFile *parent,
747 g_return_val_if_fail (G_IS_FILE (parent), NULL);
748 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
750 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
753 iface = G_FILE_GET_IFACE (parent);
755 return (* iface->get_relative_path) (parent, descendant);
759 * g_file_resolve_relative_path:
760 * @file: input #GFile.
761 * @relative_path: a given relative path string.
763 * Resolves a relative path for @file to an absolute path.
765 * This call does no blocking i/o.
767 * Returns: #GFile to the resolved path. %NULL if @relative_path
768 * is %NULL or if @file is invalid.
771 g_file_resolve_relative_path (GFile *file,
772 const char *relative_path)
776 g_return_val_if_fail (G_IS_FILE (file), NULL);
777 g_return_val_if_fail (relative_path != NULL, NULL);
779 iface = G_FILE_GET_IFACE (file);
781 return (* iface->resolve_relative_path) (file, relative_path);
785 * g_file_enumerate_children:
786 * @file: input #GFile.
787 * @attributes: an attribute query string.
788 * @flags: a set of #GFileQueryInfoFlags.
789 * @cancellable: optional #GCancellable object, %NULL to ignore.
790 * @error: #GError for error reporting.
792 * Gets the requested information about the files in a directory. The result
793 * is a #GFileEnumerator object that will give out #GFileInfo objects for
794 * all the files in the directory.
796 * The @attribute value is a string that specifies the file attributes that
797 * should be gathered. It is not an error if it's not possible to read a particular
798 * requested attribute from a file - it just won't be set. @attribute should
799 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
800 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
801 * namespace. An example attribute query be "standard::*,owner::user".
802 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
804 * If @cancellable is not %NULL, then the operation can be cancelled by
805 * triggering the cancellable object from another thread. If the operation
806 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
808 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
809 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
810 * Other errors are possible too.
812 * Returns: A #GFileEnumerator if successful, %NULL on error.
815 g_file_enumerate_children (GFile *file,
816 const char *attributes,
817 GFileQueryInfoFlags flags,
818 GCancellable *cancellable,
824 g_return_val_if_fail (G_IS_FILE (file), NULL);
826 if (g_cancellable_set_error_if_cancelled (cancellable, error))
829 iface = G_FILE_GET_IFACE (file);
831 if (iface->enumerate_children == NULL)
833 g_set_error (error, G_IO_ERROR,
834 G_IO_ERROR_NOT_SUPPORTED,
835 _("Operation not supported"));
839 return (* iface->enumerate_children) (file, attributes, flags,
844 * g_file_enumerate_children_async:
845 * @file: input #GFile.
846 * @attributes: an attribute query string.
847 * @flags: a set of #GFileQueryInfoFlags.
848 * @io_priority: the <link linkend="io-priority">I/O priority</link>
850 * @cancellable: optional #GCancellable object, %NULL to ignore.
851 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
852 * @user_data: the data to pass to callback function
854 * Asynchronously gets the requested information about the files in a directory. The result
855 * is a #GFileEnumerator object that will give out #GFileInfo objects for
856 * all the files in the directory.
858 * For more details, see g_file_enumerate_children() which is
859 * the synchronous version of this call.
861 * When the operation is finished, @callback will be called. You can then call
862 * g_file_enumerate_children_finish() to get the result of the operation.
865 g_file_enumerate_children_async (GFile *file,
866 const char *attributes,
867 GFileQueryInfoFlags flags,
869 GCancellable *cancellable,
870 GAsyncReadyCallback callback,
875 g_return_if_fail (G_IS_FILE (file));
877 iface = G_FILE_GET_IFACE (file);
878 (* iface->enumerate_children_async) (file,
888 * g_file_enumerate_children_finish:
889 * @file: input #GFile.
890 * @res: a #GAsyncResult.
893 * Finishes an async enumerate children operation.
894 * See g_file_enumerate_children_async().
896 * Returns: a #GFileEnumerator or %NULL if an error occurred.
899 g_file_enumerate_children_finish (GFile *file,
905 g_return_val_if_fail (G_IS_FILE (file), NULL);
906 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
908 if (G_IS_SIMPLE_ASYNC_RESULT (res))
910 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
911 if (g_simple_async_result_propagate_error (simple, error))
915 iface = G_FILE_GET_IFACE (file);
916 return (* iface->enumerate_children_finish) (file, res, error);
920 * g_file_query_exists:
921 * @file: input #GFile.
922 * @cancellable: optional #GCancellable object, %NULL to ignore.
924 * Utility function to check if a particular file exists. This is
925 * implemented using g_file_query_info() and as such does blocking I/O.
927 * Note that in many cases it is racy to first check for file existance
928 * and then execute something based on the outcome of that, because the
929 * file might have been created or removed inbetween the operations. The
930 * general approach to handling that is to not check, but just do the
931 * operation and handle the errors as they come.
933 * As an example of race-free checking, take the case of reading a file, and
934 * if it doesn't exist, creating it. There are two racy versions: read it, and
935 * on error create it; and: check if it exists, if not create it. These
936 * can both result in two processes creating the file (with perhaps a partially
937 * written file as the result). The correct approach is to always try to create
938 * the file with g_file_create() which will either atomically create the file
939 * or fail with a G_IO_ERROR_EXISTS error.
941 * However, in many cases an existance check is useful in a user
942 * interface, for instance to make a menu item sensitive/insensitive, so that
943 * you don't have to fool users that something is possible and then just show
944 * and error dialog. If you do this, you should make sure to also handle the
945 * errors that can happen due to races when you execute the operation.
947 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
950 g_file_query_exists (GFile *file,
951 GCancellable *cancellable)
955 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
956 G_FILE_QUERY_INFO_NONE,
960 g_object_unref (info);
969 * @file: input #GFile.
970 * @attributes: an attribute query string.
971 * @flags: a set of #GFileQueryInfoFlags.
972 * @cancellable: optional #GCancellable object, %NULL to ignore.
975 * Gets the requested information about specified @file. The result
976 * is a #GFileInfo object that contains key-value attributes (such as
977 * the type or size of the file).
979 * The @attribute value is a string that specifies the file attributes that
980 * should be gathered. It is not an error if it's not possible to read a particular
981 * requested attribute from a file - it just won't be set. @attribute should
982 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
983 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
984 * namespace. An example attribute query be "standard::*,owner::user".
985 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
987 * If @cancellable is not %NULL, then the operation can be cancelled by
988 * triggering the cancellable object from another thread. If the operation
989 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
991 * For symlinks, normally the information about the target of the
992 * symlink is returned, rather than information about the symlink itself.
993 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
994 * information about the symlink itself will be returned. Also, for symlinks
995 * that point to non-existing files the information about the symlink itself
998 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
999 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1001 * Returns: a #GFileInfo for the given @file, or %NULL on error.
1004 g_file_query_info (GFile *file,
1005 const char *attributes,
1006 GFileQueryInfoFlags flags,
1007 GCancellable *cancellable,
1012 g_return_val_if_fail (G_IS_FILE (file), NULL);
1014 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1017 iface = G_FILE_GET_IFACE (file);
1019 if (iface->query_info == NULL)
1021 g_set_error (error, G_IO_ERROR,
1022 G_IO_ERROR_NOT_SUPPORTED,
1023 _("Operation not supported"));
1027 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1031 * g_file_query_info_async:
1032 * @file: input #GFile.
1033 * @attributes: an attribute query string.
1034 * @flags: a set of #GFileQueryInfoFlags.
1035 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1037 * @cancellable: optional #GCancellable object, %NULL to ignore.
1038 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1039 * @user_data: the data to pass to callback function
1041 * Asynchronously gets the requested information about specified @file. The result
1042 * is a #GFileInfo object that contains key-value attributes (such as type or size
1045 * For more details, see g_file_query_info() which is
1046 * the synchronous version of this call.
1048 * When the operation is finished, @callback will be called. You can then call
1049 * g_file_query_info_finish() to get the result of the operation.
1052 g_file_query_info_async (GFile *file,
1053 const char *attributes,
1054 GFileQueryInfoFlags flags,
1056 GCancellable *cancellable,
1057 GAsyncReadyCallback callback,
1062 g_return_if_fail (G_IS_FILE (file));
1064 iface = G_FILE_GET_IFACE (file);
1065 (* iface->query_info_async) (file,
1075 * g_file_query_info_finish:
1076 * @file: input #GFile.
1077 * @res: a #GAsyncResult.
1078 * @error: a #GError.
1080 * Finishes an asynchronous file info query.
1081 * See g_file_query_info_async().
1083 * Returns: #GFileInfo for given @file or %NULL on error.
1086 g_file_query_info_finish (GFile *file,
1092 g_return_val_if_fail (G_IS_FILE (file), NULL);
1093 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1095 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1097 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1098 if (g_simple_async_result_propagate_error (simple, error))
1102 iface = G_FILE_GET_IFACE (file);
1103 return (* iface->query_info_finish) (file, res, error);
1107 * g_file_query_filesystem_info:
1108 * @file: input #GFile.
1109 * @attributes: an attribute query string.
1110 * @cancellable: optional #GCancellable object, %NULL to ignore.
1111 * @error: a #GError.
1113 * Similar to g_file_query_info(), but obtains information
1114 * about the filesystem the @file is on, rather than the file itself.
1115 * For instance the amount of space available and the type of
1118 * The @attribute value is a string that specifies the file attributes that
1119 * should be gathered. It is not an error if it's not possible to read a particular
1120 * requested attribute from a file - it just won't be set. @attribute should
1121 * be a comma-separated list of attribute or attribute wildcards. The wildcard "*"
1122 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1123 * namespace. The standard namespace for filesystem attributes is "fs".
1124 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1125 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1126 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1128 * If @cancellable is not %NULL, then the operation can be cancelled by
1129 * triggering the cancellable object from another thread. If the operation
1130 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1132 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1133 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1135 * Returns: a #GFileInfo or %NULL if there was an error.
1138 g_file_query_filesystem_info (GFile *file,
1139 const char *attributes,
1140 GCancellable *cancellable,
1145 g_return_val_if_fail (G_IS_FILE (file), NULL);
1147 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1150 iface = G_FILE_GET_IFACE (file);
1152 if (iface->query_filesystem_info == NULL)
1154 g_set_error (error, G_IO_ERROR,
1155 G_IO_ERROR_NOT_SUPPORTED,
1156 _("Operation not supported"));
1160 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1164 * g_file_query_filesystem_info_async:
1165 * @file: input #GFile.
1166 * @attributes: an attribute query string.
1167 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1169 * @cancellable: optional #GCancellable object, %NULL to ignore.
1170 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1171 * @user_data: the data to pass to callback function
1173 * Asynchronously gets the requested information about the filesystem
1174 * that the specified @file is on. The result is a #GFileInfo object
1175 * that contains key-value attributes (such as type or size for the
1178 * For more details, see g_file_query_filesystem_info() which is the
1179 * synchronous version of this call.
1181 * When the operation is finished, @callback will be called. You can
1182 * then call g_file_query_info_finish() to get the result of the
1186 g_file_query_filesystem_info_async (GFile *file,
1187 const char *attributes,
1189 GCancellable *cancellable,
1190 GAsyncReadyCallback callback,
1195 g_return_if_fail (G_IS_FILE (file));
1197 iface = G_FILE_GET_IFACE (file);
1198 (* iface->query_filesystem_info_async) (file,
1207 * g_file_query_filesystem_info_finish:
1208 * @file: input #GFile.
1209 * @res: a #GAsyncResult.
1210 * @error: a #GError.
1212 * Finishes an asynchronous filesystem info query. See
1213 * g_file_query_filesystem_info_async().
1215 * Returns: #GFileInfo for given @file or %NULL on error.
1218 g_file_query_filesystem_info_finish (GFile *file,
1224 g_return_val_if_fail (G_IS_FILE (file), NULL);
1225 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1227 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1229 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1230 if (g_simple_async_result_propagate_error (simple, error))
1234 iface = G_FILE_GET_IFACE (file);
1235 return (* iface->query_filesystem_info_finish) (file, res, error);
1239 * g_file_find_enclosing_mount:
1240 * @file: input #GFile.
1241 * @cancellable: optional #GCancellable object, %NULL to ignore.
1242 * @error: a #GError.
1244 * Gets a #GMount for the #GFile.
1246 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1247 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1250 * If @cancellable is not %NULL, then the operation can be cancelled by
1251 * triggering the cancellable object from another thread. If the operation
1252 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1254 * Returns: a #GMount where the @file is located or %NULL on error.
1257 g_file_find_enclosing_mount (GFile *file,
1258 GCancellable *cancellable,
1263 g_return_val_if_fail (G_IS_FILE (file), NULL);
1265 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1268 iface = G_FILE_GET_IFACE (file);
1269 if (iface->find_enclosing_mount == NULL)
1271 g_set_error (error, G_IO_ERROR,
1272 G_IO_ERROR_NOT_FOUND,
1273 _("Containing mount does not exist"));
1277 return (* iface->find_enclosing_mount) (file, cancellable, error);
1280 * g_file_find_enclosing_mount_async:
1282 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1284 * @cancellable: optional #GCancellable object, %NULL to ignore.
1285 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1286 * @user_data: the data to pass to callback function
1288 * Asynchronously gets the mount for the file.
1290 * For more details, see g_file_find_enclosing_mount() which is
1291 * the synchronous version of this call.
1293 * When the operation is finished, @callback will be called. You can then call
1294 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1297 g_file_find_enclosing_mount_async (GFile *file,
1299 GCancellable *cancellable,
1300 GAsyncReadyCallback callback,
1305 g_return_if_fail (G_IS_FILE (file));
1307 iface = G_FILE_GET_IFACE (file);
1308 (* iface->find_enclosing_mount_async) (file,
1316 * g_file_find_enclosing_mount_finish:
1318 * @res: a #GAsyncResult
1321 * Finishes an asynchronous find mount request.
1322 * See g_file_find_enclosing_mount_async().
1324 * Returns: #GMount for given @file or %NULL on error.
1327 g_file_find_enclosing_mount_finish (GFile *file,
1333 g_return_val_if_fail (G_IS_FILE (file), NULL);
1334 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1336 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1338 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1339 if (g_simple_async_result_propagate_error (simple, error))
1343 iface = G_FILE_GET_IFACE (file);
1344 return (* iface->find_enclosing_mount_finish) (file, res, error);
1350 * @file: #GFile to read.
1351 * @cancellable: a #GCancellable
1352 * @error: a #GError, or %NULL
1354 * Opens a file for reading. The result is a #GFileInputStream that
1355 * can be used to read the contents of the file.
1357 * If @cancellable is not %NULL, then the operation can be cancelled by
1358 * triggering the cancellable object from another thread. If the operation
1359 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1361 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1362 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1363 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1365 * Returns: #GFileInputStream or %NULL on error.
1368 g_file_read (GFile *file,
1369 GCancellable *cancellable,
1374 g_return_val_if_fail (G_IS_FILE (file), NULL);
1376 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1379 iface = G_FILE_GET_IFACE (file);
1381 if (iface->read_fn == NULL)
1383 g_set_error (error, G_IO_ERROR,
1384 G_IO_ERROR_NOT_SUPPORTED,
1385 _("Operation not supported"));
1389 return (* iface->read_fn) (file, cancellable, error);
1394 * @file: input #GFile.
1395 * @flags: a set of #GFileCreateFlags.
1396 * @cancellable: optional #GCancellable object, %NULL to ignore.
1397 * @error: a #GError, or %NULL
1399 * Gets an output stream for appending data to the file. If
1400 * the file doesn't already exist it is created.
1402 * By default files created are generally readable by everyone,
1403 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1404 * will be made readable only to the current user, to the level that
1405 * is supported on the target filesystem.
1407 * If @cancellable is not %NULL, then the operation can be cancelled by
1408 * triggering the cancellable object from another thread. If the operation
1409 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1411 * Some file systems don't allow all file names, and may
1412 * return an G_IO_ERROR_INVALID_FILENAME error.
1413 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be
1414 * returned. Other errors are possible too, and depend on what kind of
1415 * filesystem the file is on.
1417 * Returns: a #GFileOutputStream.
1420 g_file_append_to (GFile *file,
1421 GFileCreateFlags flags,
1422 GCancellable *cancellable,
1427 g_return_val_if_fail (G_IS_FILE (file), NULL);
1429 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1432 iface = G_FILE_GET_IFACE (file);
1434 if (iface->append_to == NULL)
1436 g_set_error (error, G_IO_ERROR,
1437 G_IO_ERROR_NOT_SUPPORTED,
1438 _("Operation not supported"));
1442 return (* iface->append_to) (file, flags, cancellable, error);
1447 * @file: input #GFile.
1448 * @flags: a set of #GFileCreateFlags.
1449 * @cancellable: optional #GCancellable object, %NULL to ignore.
1450 * @error: a #GError, or %NULL
1452 * Creates a new file and returns an output stream for writing to it.
1453 * The file must not already exists.
1455 * By default files created are generally readable by everyone,
1456 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1457 * will be made readable only to the current user, to the level that
1458 * is supported on the target filesystem.
1460 * If @cancellable is not %NULL, then the operation can be cancelled by
1461 * triggering the cancellable object from another thread. If the operation
1462 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1464 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1465 * error will be returned.
1466 * Some file systems don't allow all file names, and may
1467 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1468 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1469 * Other errors are possible too, and depend on what kind of
1470 * filesystem the file is on.
1472 * Returns: a #GFileOutputStream for the newly created file, or
1476 g_file_create (GFile *file,
1477 GFileCreateFlags flags,
1478 GCancellable *cancellable,
1483 g_return_val_if_fail (G_IS_FILE (file), NULL);
1485 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1488 iface = G_FILE_GET_IFACE (file);
1490 if (iface->create == NULL)
1492 g_set_error (error, G_IO_ERROR,
1493 G_IO_ERROR_NOT_SUPPORTED,
1494 _("Operation not supported"));
1498 return (* iface->create) (file, flags, cancellable, error);
1503 * @file: input #GFile.
1504 * @etag: an optional <link linkend="gfile-etag">entity tag</link> for the
1505 * current #GFile, or #NULL to ignore.
1506 * @make_backup: %TRUE if a backup should be created.
1507 * @flags: a set of #GFileCreateFlags.
1508 * @cancellable: optional #GCancellable object, %NULL to ignore.
1509 * @error: a #GError, or %NULL
1511 * Returns an output stream for overwriting the file, possibly
1512 * creating a backup copy of the file first.
1514 * This will try to replace the file in the safest way possible so
1515 * that any errors during the writing will not affect an already
1516 * existing copy of the file. For instance, for local files it
1517 * may write to a temporary file and then atomically rename over
1518 * the destination when the stream is closed.
1520 * By default files created are generally readable by everyone,
1521 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1522 * will be made readable only to the current user, to the level that
1523 * is supported on the target filesystem.
1525 * If @cancellable is not %NULL, then the operation can be cancelled by
1526 * triggering the cancellable object from another thread. If the operation
1527 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1529 * If you pass in a non-#NULL @etag value, then this value is
1530 * compared to the current entity tag of the file, and if they differ
1531 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1532 * that the file has been changed since you last read it. You can get
1533 * the new etag from g_file_output_stream_get_etag() after you've
1534 * finished writing and closed the #GFileOutputStream. When you load
1535 * a new file you can use g_file_input_stream_query_info() to get
1536 * the etag of the file.
1538 * If @make_backup is %TRUE, this function will attempt to make a backup
1539 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1540 * error will be returned. If you want to replace anyway, try again with
1541 * @make_backup set to %FALSE.
1543 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1544 * and if the file is some other form of non-regular file then a
1545 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1546 * Some file systems don't allow all file names, and may
1547 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1548 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1549 * Other errors are possible too, and depend on what kind of
1550 * filesystem the file is on.
1552 * Returns: a #GFileOutputStream or %NULL on error.
1555 g_file_replace (GFile *file,
1557 gboolean make_backup,
1558 GFileCreateFlags flags,
1559 GCancellable *cancellable,
1564 g_return_val_if_fail (G_IS_FILE (file), NULL);
1566 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1569 iface = G_FILE_GET_IFACE (file);
1571 if (iface->replace == NULL)
1573 g_set_error (error, G_IO_ERROR,
1574 G_IO_ERROR_NOT_SUPPORTED,
1575 _("Operation not supported"));
1580 /* Handle empty tag string as NULL in consistent way. */
1581 if (etag && *etag == 0)
1584 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1588 * g_file_read_async:
1589 * @file: input #GFile.
1590 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1592 * @cancellable: optional #GCancellable object, %NULL to ignore.
1593 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1594 * @user_data: the data to pass to callback function
1596 * Asynchronously opens @file for reading.
1598 * For more details, see g_file_read() which is
1599 * the synchronous version of this call.
1601 * When the operation is finished, @callback will be called. You can then call
1602 * g_file_read_finish() to get the result of the operation.
1605 g_file_read_async (GFile *file,
1607 GCancellable *cancellable,
1608 GAsyncReadyCallback callback,
1613 g_return_if_fail (G_IS_FILE (file));
1615 iface = G_FILE_GET_IFACE (file);
1616 (* iface->read_async) (file,
1624 * g_file_read_finish:
1625 * @file: input #GFile.
1626 * @res: a #GAsyncResult.
1627 * @error: a #GError, or %NULL
1629 * Finishes an asynchronous file read operation started with
1630 * g_file_read_async().
1632 * Returns: a #GFileInputStream or %NULL on error.
1635 g_file_read_finish (GFile *file,
1641 g_return_val_if_fail (G_IS_FILE (file), NULL);
1642 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1644 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1646 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1647 if (g_simple_async_result_propagate_error (simple, error))
1651 iface = G_FILE_GET_IFACE (file);
1652 return (* iface->read_finish) (file, res, error);
1656 * g_file_append_to_async:
1657 * @file: input #GFile.
1658 * @flags: a set of #GFileCreateFlags.
1659 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1661 * @cancellable: optional #GCancellable object, %NULL to ignore.
1662 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1663 * @user_data: the data to pass to callback function
1665 * Asynchronously opens @file for appending.
1667 * For more details, see g_file_append_to() which is
1668 * the synchronous version of this call.
1670 * When the operation is finished, @callback will be called. You can then call
1671 * g_file_append_to_finish() to get the result of the operation.
1674 g_file_append_to_async (GFile *file,
1675 GFileCreateFlags flags,
1677 GCancellable *cancellable,
1678 GAsyncReadyCallback callback,
1683 g_return_if_fail (G_IS_FILE (file));
1685 iface = G_FILE_GET_IFACE (file);
1686 (* iface->append_to_async) (file,
1695 * g_file_append_to_finish:
1696 * @file: input #GFile.
1697 * @res: #GAsyncResult
1698 * @error: a #GError, or %NULL
1700 * Finishes an asynchronous file append operation started with
1701 * g_file_append_to_async().
1703 * Returns: a valid #GFileOutputStream or %NULL on error.
1706 g_file_append_to_finish (GFile *file,
1712 g_return_val_if_fail (G_IS_FILE (file), NULL);
1713 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1715 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1717 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1718 if (g_simple_async_result_propagate_error (simple, error))
1722 iface = G_FILE_GET_IFACE (file);
1723 return (* iface->append_to_finish) (file, res, error);
1727 * g_file_create_async:
1728 * @file: input #GFile.
1729 * @flags: a set of #GFileCreateFlags.
1730 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1732 * @cancellable: optional #GCancellable object, %NULL to ignore.
1733 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1734 * @user_data: the data to pass to callback function
1736 * Asynchronously creates a new file and returns an output stream for writing to it.
1737 * The file must not already exists.
1739 * For more details, see g_file_create() which is
1740 * the synchronous version of this call.
1742 * When the operation is finished, @callback will be called. You can then call
1743 * g_file_create_finish() to get the result of the operation.
1746 g_file_create_async (GFile *file,
1747 GFileCreateFlags flags,
1749 GCancellable *cancellable,
1750 GAsyncReadyCallback callback,
1755 g_return_if_fail (G_IS_FILE (file));
1757 iface = G_FILE_GET_IFACE (file);
1758 (* iface->create_async) (file,
1767 * g_file_create_finish:
1768 * @file: input #GFile.
1769 * @res: a #GAsyncResult.
1770 * @error: a #GError, or %NULL
1772 * Finishes an asynchronous file create operation started with
1773 * g_file_create_async().
1775 * Returns: a #GFileOutputStream or %NULL on error.
1778 g_file_create_finish (GFile *file,
1784 g_return_val_if_fail (G_IS_FILE (file), NULL);
1785 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1787 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1789 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1790 if (g_simple_async_result_propagate_error (simple, error))
1794 iface = G_FILE_GET_IFACE (file);
1795 return (* iface->create_finish) (file, res, error);
1799 * g_file_replace_async:
1800 * @file: input #GFile.
1801 * @etag: an <link linkend="gfile-etag">entity tag</link> for the
1802 * current #GFile, or NULL to ignore.
1803 * @make_backup: %TRUE if a backup should be created.
1804 * @flags: a set of #GFileCreateFlags.
1805 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1807 * @cancellable: optional #GCancellable object, %NULL to ignore.
1808 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1809 * @user_data: the data to pass to callback function
1811 * Asynchronously overwrites the file, replacing the contents, possibly
1812 * creating a backup copy of the file first.
1814 * For more details, see g_file_replace() which is
1815 * the synchronous version of this call.
1817 * When the operation is finished, @callback will be called. You can then call
1818 * g_file_replace_finish() to get the result of the operation.
1821 g_file_replace_async (GFile *file,
1823 gboolean make_backup,
1824 GFileCreateFlags flags,
1826 GCancellable *cancellable,
1827 GAsyncReadyCallback callback,
1832 g_return_if_fail (G_IS_FILE (file));
1834 iface = G_FILE_GET_IFACE (file);
1835 (* iface->replace_async) (file,
1846 * g_file_replace_finish:
1847 * @file: input #GFile.
1848 * @res: a #GAsyncResult.
1849 * @error: a #GError, or %NULL
1851 * Finishes an asynchronous file replace operation started with
1852 * g_file_replace_async().
1854 * Returns: a #GFileOutputStream, or %NULL on error.
1857 g_file_replace_finish (GFile *file,
1863 g_return_val_if_fail (G_IS_FILE (file), NULL);
1864 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1866 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1868 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1869 if (g_simple_async_result_propagate_error (simple, error))
1873 iface = G_FILE_GET_IFACE (file);
1874 return (* iface->replace_finish) (file, res, error);
1878 copy_symlink (GFile *destination,
1879 GFileCopyFlags flags,
1880 GCancellable *cancellable,
1885 gboolean tried_delete;
1887 GFileType file_type;
1889 tried_delete = FALSE;
1893 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
1895 /* Maybe it already existed, and we want to overwrite? */
1896 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
1897 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
1899 g_error_free (my_error);
1902 /* Don't overwrite if the destination is a directory */
1903 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1904 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1905 cancellable, &my_error);
1908 file_type = g_file_info_get_file_type (info);
1909 g_object_unref (info);
1911 if (file_type == G_FILE_TYPE_DIRECTORY)
1913 g_set_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
1914 _("Can't copy over directory"));
1919 if (!g_file_delete (destination, cancellable, error))
1922 tried_delete = TRUE;
1926 g_propagate_error (error, my_error);
1933 static GInputStream *
1934 open_source_for_copy (GFile *source,
1936 GFileCopyFlags flags,
1937 GCancellable *cancellable,
1943 GFileType file_type;
1946 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
1950 /* There was an error opening the source, try to set a good error for it: */
1952 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
1954 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
1955 * as that is less useful to the app. Better check for errors on the
1958 g_error_free (my_error);
1961 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1962 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1963 cancellable, &my_error);
1966 file_type = g_file_info_get_file_type (info);
1967 g_object_unref (info);
1969 if (flags & G_FILE_COPY_OVERWRITE)
1971 if (file_type == G_FILE_TYPE_DIRECTORY)
1973 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
1974 _("Can't copy directory over directory"));
1977 /* continue to would_recurse error */
1981 g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
1982 _("Target file exists"));
1988 /* Error getting info from target, return that error
1989 * (except for NOT_FOUND, which is no error here)
1991 if (my_error->domain != G_IO_ERROR && my_error->code != G_IO_ERROR_NOT_FOUND)
1993 g_propagate_error (error, my_error);
1996 g_error_free (my_error);
1999 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2000 _("Can't recursively copy directory"));
2004 g_propagate_error (error, my_error);
2009 should_copy (GFileAttributeInfo *info,
2013 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2014 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2018 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2019 GFileAttributeInfoList *namespaces,
2027 s = g_string_new ("");
2031 for (i = 0; i < attributes->n_infos; i++)
2033 if (should_copy (&attributes->infos[i], as_move))
2038 g_string_append_c (s, ',');
2040 g_string_append (s, attributes->infos[i].name);
2047 for (i = 0; i < namespaces->n_infos; i++)
2049 if (should_copy (&namespaces->infos[i], as_move))
2054 g_string_append_c (s, ',');
2056 g_string_append (s, namespaces->infos[i].name);
2057 g_string_append (s, ":*");
2062 return g_string_free (s, FALSE);
2066 * g_file_copy_attributes:
2067 * @source: a #GFile with attributes.
2068 * @destination: a #GFile to copy attributes to.
2069 * @flags: a set of #GFileCopyFlags.
2070 * @cancellable: optional #GCancellable object, %NULL to ignore.
2071 * @error: a #GError, %NULL to ignore.
2073 * Copies the file attributes from @source to @destination.
2075 * Normally only a subset of the file attributes are copied,
2076 * those that are copies in a normal file copy operation
2077 * (which for instance does not include e.g. mtime). However
2078 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2079 * all the metadata that is possible to copy is copied.
2081 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2084 g_file_copy_attributes (GFile *source,
2086 GFileCopyFlags flags,
2087 GCancellable *cancellable,
2090 GFileAttributeInfoList *attributes, *namespaces;
2091 char *attrs_to_read;
2095 gboolean source_nofollow_symlinks;
2097 as_move = flags & G_FILE_COPY_ALL_METADATA;
2098 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2100 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2101 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2102 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2104 if (attributes == NULL && namespaces == NULL)
2107 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move);
2109 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2110 * we just don't copy it.
2112 info = g_file_query_info (source, attrs_to_read,
2113 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2117 g_free (attrs_to_read);
2122 res = g_file_set_attributes_from_info (destination,
2126 g_object_unref (info);
2129 g_file_attribute_info_list_unref (attributes);
2130 g_file_attribute_info_list_unref (namespaces);
2135 /* Closes the streams */
2137 copy_stream_with_progress (GInputStream *in,
2139 GCancellable *cancellable,
2140 GFileProgressCallback progress_callback,
2141 gpointer progress_callback_data,
2144 gssize n_read, n_written;
2145 goffset current_size;
2146 char buffer[8192], *p;
2152 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2153 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2157 total_size = g_file_info_get_size (info);
2158 g_object_unref (info);
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);
2206 /* Don't care about errors in source here */
2207 g_input_stream_close (in, cancellable, NULL);
2209 /* But write errors on close are bad! */
2210 if (!g_output_stream_close (out, cancellable, error))
2213 g_object_unref (in);
2214 g_object_unref (out);
2220 file_copy_fallback (GFile *source,
2222 GFileCopyFlags flags,
2223 GCancellable *cancellable,
2224 GFileProgressCallback progress_callback,
2225 gpointer progress_callback_data,
2233 /* Maybe copy the symlink? */
2234 if (flags & G_FILE_COPY_NOFOLLOW_SYMLINKS)
2236 info = g_file_query_info (source,
2237 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2238 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2244 if (g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK &&
2245 (target = g_file_info_get_symlink_target (info)) != NULL)
2247 if (!copy_symlink (destination, flags, cancellable, target, error))
2249 g_object_unref (info);
2253 g_object_unref (info);
2257 g_object_unref (info);
2260 in = open_source_for_copy (source, destination, flags, cancellable, error);
2264 if (flags & G_FILE_COPY_OVERWRITE)
2266 out = (GOutputStream *)g_file_replace (destination,
2268 flags & G_FILE_COPY_BACKUP,
2269 cancellable, error);
2273 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2278 g_object_unref (in);
2282 if (!copy_stream_with_progress (in, out, cancellable,
2283 progress_callback, progress_callback_data,
2289 /* Ignore errors here. Failure to copy metadata is not a hard error */
2290 g_file_copy_attributes (source, destination,
2291 flags, cancellable, NULL);
2298 * @source: input #GFile.
2299 * @destination: destination #GFile
2300 * @flags: set of #GFileCopyFlags
2301 * @cancellable: optional #GCancellable object, %NULL to ignore.
2302 * @progress_callback: function to callback with progress information
2303 * @progress_callback_data: user data to pass to @progress_callback
2304 * @error: #GError to set on error, or %NULL
2306 * Copies the file @source to the location specified by @destination.
2307 * Can not handle recursive copies of directories.
2309 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2310 * existing @destination file is overwritten.
2312 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2313 * will be copied as symlinks, otherwise the target of the
2314 * @source symlink will be copied.
2316 * If @cancellable is not %NULL, then the operation can be cancelled by
2317 * triggering the cancellable object from another thread. If the operation
2318 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2320 * If @progress_callback is not %NULL, then the operation can be monitored by
2321 * setting this to a #GFileProgressCallback function. @progress_callback_data
2322 * will be passed to this function. It is guaranteed that this callback will
2323 * be called after all data has been transferred with the total number of bytes
2324 * copied during the operation.
2326 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2327 * error is returned, independent on the status of the @destination.
2329 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2330 * error G_IO_ERROR_EXISTS is returned.
2332 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2333 * error is returned. If trying to overwrite a directory with a directory the
2334 * G_IO_ERROR_WOULD_MERGE error is returned.
2336 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2337 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2340 * If you are interested in copying the #GFile object itself (not the on-disk
2341 * file), see g_file_dup().
2343 * Returns: %TRUE on success, %FALSE otherwise.
2346 g_file_copy (GFile *source,
2348 GFileCopyFlags flags,
2349 GCancellable *cancellable,
2350 GFileProgressCallback progress_callback,
2351 gpointer progress_callback_data,
2358 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2359 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2361 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2364 iface = G_FILE_GET_IFACE (destination);
2368 res = (* iface->copy) (source, destination,
2370 progress_callback, progress_callback_data,
2376 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2378 g_propagate_error (error, my_error);
2383 /* If the types are different, and the destination method failed
2384 also try the source method */
2385 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2387 iface = G_FILE_GET_IFACE (source);
2392 res = (* iface->copy) (source, destination,
2394 progress_callback, progress_callback_data,
2400 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2402 g_propagate_error (error, my_error);
2408 return file_copy_fallback (source, destination, flags, cancellable,
2409 progress_callback, progress_callback_data,
2414 * g_file_copy_async:
2415 * @source: input #GFile.
2416 * @destination: destination #GFile
2417 * @flags: set of #GFileCopyFlags
2418 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2420 * @cancellable: optional #GCancellable object, %NULL to ignore.
2421 * @progress_callback: function to callback with progress information
2422 * @progress_callback_data: user data to pass to @progress_callback
2423 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2424 * @user_data: the data to pass to callback function
2426 * Copies the file @source to the location specified by @destination
2427 * asynchronously. For details of the behaviour, see g_file_copy().
2429 * If @progress_callback is not %NULL, then that function that will be called
2430 * just like in g_file_copy(), however the callback will run in the main loop,
2431 * not in the thread that is doing the I/O operation.
2433 * When the operation is finished, @callback will be called. You can then call
2434 * g_file_copy_finish() to get the result of the operation.
2437 g_file_copy_async (GFile *source,
2439 GFileCopyFlags flags,
2441 GCancellable *cancellable,
2442 GFileProgressCallback progress_callback,
2443 gpointer progress_callback_data,
2444 GAsyncReadyCallback callback,
2449 g_return_if_fail (G_IS_FILE (source));
2450 g_return_if_fail (G_IS_FILE (destination));
2452 iface = G_FILE_GET_IFACE (source);
2453 (* iface->copy_async) (source,
2459 progress_callback_data,
2465 * g_file_copy_finish:
2466 * @file: input #GFile.
2467 * @res: a #GAsyncResult.
2468 * @error: a #GError, or %NULL
2470 * Finishes copying the file started with
2471 * g_file_copy_async().
2473 * Returns: a %TRUE on success, %FALSE on error.
2476 g_file_copy_finish (GFile *file,
2482 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2483 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
2485 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2487 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2489 if (g_simple_async_result_propagate_error (simple, error))
2493 iface = G_FILE_GET_IFACE (file);
2494 return (* iface->copy_finish) (file, res, error);
2499 * @source: #GFile pointing to the source location.
2500 * @destination: #GFile pointing to the destination location.
2501 * @flags: set of #GFileCopyFlags.
2502 * @cancellable: optional #GCancellable object, %NULL to ignore.
2503 * @progress_callback: #GFileProgressCallback function for updates.
2504 * @progress_callback_data: gpointer to user data for the callback function.
2505 * @error: #GError for returning error conditions, or %NULL
2508 * Tries to move the file or directory @source to the location specified by @destination.
2509 * If native move operations are supported then this is used, otherwise a copy + delete
2510 * fallback is used. The native implementation may support moving directories (for instance
2511 * on moves inside the same filesystem), but the fallback code does not.
2513 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2514 * existing @destination file is overwritten.
2516 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2517 * will be copied as symlinks, otherwise the target of the
2518 * @source symlink will be copied.
2520 * If @cancellable is not %NULL, then the operation can be cancelled by
2521 * triggering the cancellable object from another thread. If the operation
2522 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2524 * If @progress_callback is not %NULL, then the operation can be monitored by
2525 * setting this to a #GFileProgressCallback function. @progress_callback_data
2526 * will be passed to this function. It is guaranteed that this callback will
2527 * be called after all data has been transferred with the total number of bytes
2528 * copied during the operation.
2530 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2531 * error is returned, independent on the status of the @destination.
2533 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2534 * error G_IO_ERROR_EXISTS is returned.
2536 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2537 * error is returned. If trying to overwrite a directory with a directory the
2538 * G_IO_ERROR_WOULD_MERGE error is returned.
2540 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
2541 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
2542 * may be returned (if the native move operation isn't available).
2544 * Returns: %TRUE on successful move, %FALSE otherwise.
2547 g_file_move (GFile *source,
2549 GFileCopyFlags flags,
2550 GCancellable *cancellable,
2551 GFileProgressCallback progress_callback,
2552 gpointer progress_callback_data,
2559 g_return_val_if_fail (G_IS_FILE (source), FALSE);
2560 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
2562 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2565 iface = G_FILE_GET_IFACE (destination);
2569 res = (* iface->move) (source, destination,
2571 progress_callback, progress_callback_data,
2577 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2579 g_propagate_error (error, my_error);
2584 /* If the types are different, and the destination method failed
2585 also try the source method */
2586 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
2588 iface = G_FILE_GET_IFACE (source);
2593 res = (* iface->move) (source, destination,
2595 progress_callback, progress_callback_data,
2601 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
2603 g_propagate_error (error, my_error);
2609 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
2611 g_set_error (error, G_IO_ERROR,
2612 G_IO_ERROR_NOT_SUPPORTED,
2613 _("Operation not supported"));
2617 flags |= G_FILE_COPY_ALL_METADATA;
2618 if (!g_file_copy (source, destination, flags, cancellable,
2619 progress_callback, progress_callback_data,
2623 return g_file_delete (source, cancellable, error);
2627 * g_file_make_directory
2628 * @file: input #GFile.
2629 * @cancellable: optional #GCancellable object, %NULL to ignore.
2630 * @error: a #GError, or %NULL
2632 * Creates a directory.
2634 * If @cancellable is not %NULL, then the operation can be cancelled by
2635 * triggering the cancellable object from another thread. If the operation
2636 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2638 * Returns: %TRUE on successful creation, %FALSE otherwise.
2641 g_file_make_directory (GFile *file,
2642 GCancellable *cancellable,
2647 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2649 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2652 iface = G_FILE_GET_IFACE (file);
2654 if (iface->make_directory == NULL)
2656 g_set_error (error, G_IO_ERROR,
2657 G_IO_ERROR_NOT_SUPPORTED,
2658 _("Operation not supported"));
2662 return (* iface->make_directory) (file, cancellable, error);
2666 * g_file_make_symbolic_link:
2667 * @file: input #GFile.
2668 * @symlink_value: a string with the value of the new symlink.
2669 * @cancellable: optional #GCancellable object, %NULL to ignore.
2670 * @error: a #GError.
2672 * Creates a symbolic link.
2674 * If @cancellable is not %NULL, then the operation can be cancelled by
2675 * triggering the cancellable object from another thread. If the operation
2676 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2678 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
2681 g_file_make_symbolic_link (GFile *file,
2682 const char *symlink_value,
2683 GCancellable *cancellable,
2688 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2689 g_return_val_if_fail (symlink_value != NULL, FALSE);
2691 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2694 if (*symlink_value == '\0')
2696 g_set_error (error, G_IO_ERROR,
2697 G_IO_ERROR_INVALID_ARGUMENT,
2698 _("Invalid symlink value given"));
2702 iface = G_FILE_GET_IFACE (file);
2704 if (iface->make_symbolic_link == NULL)
2706 g_set_error (error, G_IO_ERROR,
2707 G_IO_ERROR_NOT_SUPPORTED,
2708 _("Operation not supported"));
2712 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
2717 * @file: input #GFile.
2718 * @cancellable: optional #GCancellable object, %NULL to ignore.
2719 * @error: a #GError, or %NULL
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"