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>
33 #include <sys/types.h>
39 #include "gioscheduler.h"
40 #include "gsimpleasyncresult.h"
41 #include "gfileattribute-priv.h"
42 #include "gfiledescriptorbased.h"
43 #include "gpollfilemonitor.h"
45 #include "gfileinputstream.h"
46 #include "gfileoutputstream.h"
47 #include "glocalfileoutputstream.h"
48 #include "glocalfileiostream.h"
49 #include "gcancellable.h"
50 #include "gasyncresult.h"
57 * @short_description: File and Directory Handling
59 * @see_also: #GFileInfo, #GFileEnumerator
61 * #GFile is a high level abstraction for manipulating files on a
62 * virtual file system. #GFile<!-- -->s are lightweight, immutable
63 * objects that do no I/O upon creation. It is necessary to understand that
64 * #GFile objects do not represent files, merely an identifier for a file. All
65 * file content I/O is implemented as streaming operations (see #GInputStream and
68 * To construct a #GFile, you can use:
69 * g_file_new_for_path() if you have a path.
70 * g_file_new_for_uri() if you have a URI.
71 * g_file_new_for_commandline_arg() for a command line argument.
72 * g_file_new_tmp() to create a temporary file from a template.
73 * g_file_parse_name() from a utf8 string gotten from g_file_get_parse_name().
75 * One way to think of a #GFile is as an abstraction of a pathname. For normal
76 * files the system pathname is what is stored internally, but as #GFile<!-- -->s
77 * are extensible it could also be something else that corresponds to a pathname
78 * in a userspace implementation of a filesystem.
80 * #GFile<!-- -->s make up hierarchies of directories and files that correspond to the
81 * files on a filesystem. You can move through the file system with #GFile using
82 * g_file_get_parent() to get an identifier for the parent directory, g_file_get_child()
83 * to get a child within a directory, g_file_resolve_relative_path() to resolve a relative
84 * path between two #GFile<!-- -->s. There can be multiple hierarchies, so you may not
85 * end up at the same root if you repeatedly call g_file_get_parent() on two different
88 * All #GFile<!-- -->s have a basename (get with g_file_get_basename()). These names
89 * are byte strings that are used to identify the file on the filesystem (relative to
90 * its parent directory) and there is no guarantees that they have any particular charset
91 * encoding or even make any sense at all. If you want to use filenames in a user
92 * interface you should use the display name that you can get by requesting the
93 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
94 * This is guaranteed to be in utf8 and can be used in a user interface. But always
95 * store the real basename or the #GFile to use to actually access the file, because
96 * there is no way to go from a display name to the actual name.
98 * Using #GFile as an identifier has the same weaknesses as using a path in that
99 * there may be multiple aliases for the same file. For instance, hard or
100 * soft links may cause two different #GFile<!-- -->s to refer to the same file.
101 * Other possible causes for aliases are: case insensitive filesystems, short
102 * and long names on Fat/NTFS, or bind mounts in Linux. If you want to check if
103 * two #GFile<!-- -->s point to the same file you can query for the
104 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
105 * canonicalization of pathnames passed in, so that trivial differences in the
106 * path string used at creation (duplicated slashes, slash at end of path, "."
107 * or ".." path segments, etc) does not create different #GFile<!-- -->s.
109 * Many #GFile operations have both synchronous and asynchronous versions
110 * to suit your application. Asynchronous versions of synchronous functions
111 * simply have _async() appended to their function names. The asynchronous
112 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
113 * the operation, producing a GAsyncResult which is then passed to the
114 * function's matching _finish() operation.
116 * Some #GFile operations do not have synchronous analogs, as they may
117 * take a very long time to finish, and blocking may leave an application
118 * unusable. Notable cases include:
119 * g_file_mount_mountable() to mount a mountable file.
120 * g_file_unmount_mountable_with_operation() to unmount a mountable file.
121 * g_file_eject_mountable_with_operation() to eject a mountable file.
123 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
124 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
125 * short. Entity tags are somewhat like a more abstract version of the
126 * traditional mtime, and can be used to quickly determine if the file has
127 * been modified from the version on the file system. See the HTTP 1.1
128 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
129 * for HTTP Etag headers, which are a very similar concept.
133 static void g_file_real_query_info_async (GFile *file,
134 const char *attributes,
135 GFileQueryInfoFlags flags,
137 GCancellable *cancellable,
138 GAsyncReadyCallback callback,
140 static GFileInfo * g_file_real_query_info_finish (GFile *file,
143 static void g_file_real_query_filesystem_info_async (GFile *file,
144 const char *attributes,
146 GCancellable *cancellable,
147 GAsyncReadyCallback callback,
149 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
152 static void g_file_real_enumerate_children_async (GFile *file,
153 const char *attributes,
154 GFileQueryInfoFlags flags,
156 GCancellable *cancellable,
157 GAsyncReadyCallback callback,
159 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
162 static void g_file_real_read_async (GFile *file,
164 GCancellable *cancellable,
165 GAsyncReadyCallback callback,
167 static GFileInputStream * g_file_real_read_finish (GFile *file,
170 static void g_file_real_append_to_async (GFile *file,
171 GFileCreateFlags flags,
173 GCancellable *cancellable,
174 GAsyncReadyCallback callback,
176 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
179 static void g_file_real_create_async (GFile *file,
180 GFileCreateFlags flags,
182 GCancellable *cancellable,
183 GAsyncReadyCallback callback,
185 static GFileOutputStream *g_file_real_create_finish (GFile *file,
188 static void g_file_real_replace_async (GFile *file,
190 gboolean make_backup,
191 GFileCreateFlags flags,
193 GCancellable *cancellable,
194 GAsyncReadyCallback callback,
196 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
199 static void g_file_real_open_readwrite_async (GFile *file,
201 GCancellable *cancellable,
202 GAsyncReadyCallback callback,
204 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
207 static void g_file_real_create_readwrite_async (GFile *file,
208 GFileCreateFlags flags,
210 GCancellable *cancellable,
211 GAsyncReadyCallback callback,
213 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
216 static void g_file_real_replace_readwrite_async (GFile *file,
218 gboolean make_backup,
219 GFileCreateFlags flags,
221 GCancellable *cancellable,
222 GAsyncReadyCallback callback,
224 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
227 static gboolean g_file_real_set_attributes_from_info (GFile *file,
229 GFileQueryInfoFlags flags,
230 GCancellable *cancellable,
232 static void g_file_real_set_display_name_async (GFile *file,
233 const char *display_name,
235 GCancellable *cancellable,
236 GAsyncReadyCallback callback,
238 static GFile * g_file_real_set_display_name_finish (GFile *file,
241 static void g_file_real_set_attributes_async (GFile *file,
243 GFileQueryInfoFlags flags,
245 GCancellable *cancellable,
246 GAsyncReadyCallback callback,
248 static gboolean g_file_real_set_attributes_finish (GFile *file,
252 static void g_file_real_find_enclosing_mount_async (GFile *file,
254 GCancellable *cancellable,
255 GAsyncReadyCallback callback,
257 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
260 static void g_file_real_copy_async (GFile *source,
262 GFileCopyFlags flags,
264 GCancellable *cancellable,
265 GFileProgressCallback progress_callback,
266 gpointer progress_callback_data,
267 GAsyncReadyCallback callback,
269 static gboolean g_file_real_copy_finish (GFile *file,
273 typedef GFileIface GFileInterface;
274 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
277 g_file_default_init (GFileIface *iface)
279 iface->enumerate_children_async = g_file_real_enumerate_children_async;
280 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
281 iface->set_display_name_async = g_file_real_set_display_name_async;
282 iface->set_display_name_finish = g_file_real_set_display_name_finish;
283 iface->query_info_async = g_file_real_query_info_async;
284 iface->query_info_finish = g_file_real_query_info_finish;
285 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
286 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
287 iface->set_attributes_async = g_file_real_set_attributes_async;
288 iface->set_attributes_finish = g_file_real_set_attributes_finish;
289 iface->read_async = g_file_real_read_async;
290 iface->read_finish = g_file_real_read_finish;
291 iface->append_to_async = g_file_real_append_to_async;
292 iface->append_to_finish = g_file_real_append_to_finish;
293 iface->create_async = g_file_real_create_async;
294 iface->create_finish = g_file_real_create_finish;
295 iface->replace_async = g_file_real_replace_async;
296 iface->replace_finish = g_file_real_replace_finish;
297 iface->open_readwrite_async = g_file_real_open_readwrite_async;
298 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
299 iface->create_readwrite_async = g_file_real_create_readwrite_async;
300 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
301 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
302 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
303 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
304 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
305 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
306 iface->copy_async = g_file_real_copy_async;
307 iface->copy_finish = g_file_real_copy_finish;
313 * @file: input #GFile.
315 * Checks to see if a file is native to the platform.
317 * A native file s one expressed in the platform-native filename format,
318 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
319 * as it might be on a locally mounted remote filesystem.
321 * On some systems non-native files may be available using
322 * the native filesystem via a userspace filesystem (FUSE), in
323 * these cases this call will return %FALSE, but g_file_get_path()
324 * will still return a native path.
326 * This call does no blocking i/o.
328 * Returns: %TRUE if file is native.
331 g_file_is_native (GFile *file)
335 g_return_val_if_fail (G_IS_FILE (file), FALSE);
337 iface = G_FILE_GET_IFACE (file);
339 return (* iface->is_native) (file);
344 * g_file_has_uri_scheme:
345 * @file: input #GFile.
346 * @uri_scheme: a string containing a URI scheme.
348 * Checks to see if a #GFile has a given URI scheme.
350 * This call does no blocking i/o.
352 * Returns: %TRUE if #GFile's backend supports the
353 * given URI scheme, %FALSE if URI scheme is %NULL,
354 * not supported, or #GFile is invalid.
357 g_file_has_uri_scheme (GFile *file,
358 const char *uri_scheme)
362 g_return_val_if_fail (G_IS_FILE (file), FALSE);
363 g_return_val_if_fail (uri_scheme != NULL, FALSE);
365 iface = G_FILE_GET_IFACE (file);
367 return (* iface->has_uri_scheme) (file, uri_scheme);
372 * g_file_get_uri_scheme:
373 * @file: input #GFile.
375 * Gets the URI scheme for a #GFile.
376 * RFC 3986 decodes the scheme as:
378 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
380 * Common schemes include "file", "http", "ftp", etc.
382 * This call does no blocking i/o.
384 * Returns: a string containing the URI scheme for the given
385 * #GFile. The returned string should be freed with g_free()
386 * when no longer needed.
389 g_file_get_uri_scheme (GFile *file)
393 g_return_val_if_fail (G_IS_FILE (file), NULL);
395 iface = G_FILE_GET_IFACE (file);
397 return (* iface->get_uri_scheme) (file);
402 * g_file_get_basename:
403 * @file: input #GFile.
405 * Gets the base name (the last component of the path) for a given #GFile.
407 * If called for the top level of a system (such as the filesystem root
408 * or a uri like sftp://host/) it will return a single directory separator
409 * (and on Windows, possibly a drive letter).
411 * The base name is a byte string (*not* UTF-8). It has no defined encoding
412 * or rules other than it may not contain zero bytes. If you want to use
413 * filenames in a user interface you should use the display name that you
414 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
415 * attribute with g_file_query_info().
417 * This call does no blocking i/o.
419 * Returns: string containing the #GFile's base name, or %NULL
420 * if given #GFile is invalid. The returned string should be
421 * freed with g_free() when no longer needed.
424 g_file_get_basename (GFile *file)
428 g_return_val_if_fail (G_IS_FILE (file), NULL);
430 iface = G_FILE_GET_IFACE (file);
432 return (* iface->get_basename) (file);
437 * @file: input #GFile.
439 * Gets the local pathname for #GFile, if one exists.
441 * This call does no blocking i/o.
443 * Returns: string containing the #GFile's path, or %NULL if
444 * no such path exists. The returned string should be
445 * freed with g_free() when no longer needed.
448 g_file_get_path (GFile *file)
452 g_return_val_if_fail (G_IS_FILE (file), NULL);
454 iface = G_FILE_GET_IFACE (file);
456 return (* iface->get_path) (file);
461 * @file: input #GFile.
463 * Gets the URI for the @file.
465 * This call does no blocking i/o.
467 * Returns: a string containing the #GFile's URI.
468 * The returned string should be freed with g_free() when no longer needed.
471 g_file_get_uri (GFile *file)
475 g_return_val_if_fail (G_IS_FILE (file), NULL);
477 iface = G_FILE_GET_IFACE (file);
479 return (* iface->get_uri) (file);
483 * g_file_get_parse_name:
484 * @file: input #GFile.
486 * Gets the parse name of the @file.
487 * A parse name is a UTF-8 string that describes the
488 * file such that one can get the #GFile back using
489 * g_file_parse_name().
491 * This is generally used to show the #GFile as a nice
492 * full-pathname kind of string in a user interface,
493 * like in a location entry.
495 * For local files with names that can safely be converted
496 * to UTF8 the pathname is used, otherwise the IRI is used
497 * (a form of URI that allows UTF8 characters unescaped).
499 * This call does no blocking i/o.
501 * Returns: a string containing the #GFile's parse name. The returned
502 * string should be freed with g_free() when no longer needed.
505 g_file_get_parse_name (GFile *file)
509 g_return_val_if_fail (G_IS_FILE (file), NULL);
511 iface = G_FILE_GET_IFACE (file);
513 return (* iface->get_parse_name) (file);
518 * @file: input #GFile.
520 * Duplicates a #GFile handle. This operation does not duplicate
521 * the actual file or directory represented by the #GFile; see
522 * g_file_copy() if attempting to copy a file.
524 * This call does no blocking i/o.
526 * Returns: (transfer full): a new #GFile that is a duplicate of the given #GFile.
529 g_file_dup (GFile *file)
533 g_return_val_if_fail (G_IS_FILE (file), NULL);
535 iface = G_FILE_GET_IFACE (file);
537 return (* iface->dup) (file);
542 * @file: (type GFile): #gconstpointer to a #GFile.
544 * Creates a hash value for a #GFile.
546 * This call does no blocking i/o.
549 * Returns: 0 if @file is not a valid #GFile, otherwise an
550 * integer that can be used as hash value for the #GFile.
551 * This function is intended for easily hashing a #GFile to
552 * add to a #GHashTable or similar data structure.
555 g_file_hash (gconstpointer file)
559 g_return_val_if_fail (G_IS_FILE (file), 0);
561 iface = G_FILE_GET_IFACE (file);
563 return (* iface->hash) ((GFile *)file);
568 * @file1: the first #GFile.
569 * @file2: the second #GFile.
571 * Checks equality of two given #GFile<!-- -->s. Note that two
572 * #GFile<!-- -->s that differ can still refer to the same
573 * file on the filesystem due to various forms of filename
576 * This call does no blocking i/o.
578 * Returns: %TRUE if @file1 and @file2 are equal.
579 * %FALSE if either is not a #GFile.
582 g_file_equal (GFile *file1,
587 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
588 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
590 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
593 iface = G_FILE_GET_IFACE (file1);
595 return (* iface->equal) (file1, file2);
601 * @file: input #GFile.
603 * Gets the parent directory for the @file.
604 * If the @file represents the root directory of the
605 * file system, then %NULL will be returned.
607 * This call does no blocking i/o.
609 * Returns: (transfer full): a #GFile structure to the parent of the given
610 * #GFile or %NULL if there is no parent.
611 * Free the returned object with g_object_unref().
614 g_file_get_parent (GFile *file)
618 g_return_val_if_fail (G_IS_FILE (file), NULL);
620 iface = G_FILE_GET_IFACE (file);
622 return (* iface->get_parent) (file);
627 * @file: input #GFile
628 * @parent: (allow-none): the parent to check for, or %NULL
630 * Checks if @file has a parent, and optionally, if it is @parent.
632 * If @parent is %NULL then this function returns %TRUE if @file has any
633 * parent at all. If @parent is non-%NULL then %TRUE is only returned
634 * if @file is a child of @parent.
636 * Returns: %TRUE if @file is a child of @parent (or any parent in the
637 * case that @parent is %NULL).
642 g_file_has_parent (GFile *file,
645 GFile *actual_parent;
648 g_return_val_if_fail (G_IS_FILE (file), FALSE);
649 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
651 actual_parent = g_file_get_parent (file);
653 if (actual_parent != NULL)
656 result = g_file_equal (parent, actual_parent);
660 g_object_unref (actual_parent);
670 * @file: input #GFile.
671 * @name: string containing the child's basename.
673 * Gets a child of @file with basename equal to @name.
675 * Note that the file with that specific name might not exist, but
676 * you can still have a #GFile that points to it. You can use this
677 * for instance to create that file.
679 * This call does no blocking i/o.
681 * Returns: (transfer full): a #GFile to a child specified by @name.
682 * Free the returned object with g_object_unref().
685 g_file_get_child (GFile *file,
688 g_return_val_if_fail (G_IS_FILE (file), NULL);
689 g_return_val_if_fail (name != NULL, NULL);
691 return g_file_resolve_relative_path (file, name);
695 * g_file_get_child_for_display_name:
696 * @file: input #GFile.
697 * @display_name: string to a possible child.
700 * Gets the child of @file for a given @display_name (i.e. a UTF8
701 * version of the name). If this function fails, it returns %NULL and @error will be
702 * set. This is very useful when constructing a GFile for a new file
703 * and the user entered the filename in the user interface, for instance
704 * when you select a directory and type a filename in the file selector.
706 * This call does no blocking i/o.
708 * Returns: (transfer full): a #GFile to the specified child, or
709 * %NULL if the display name couldn't be converted.
710 * Free the returned object with g_object_unref().
713 g_file_get_child_for_display_name (GFile *file,
714 const char *display_name,
719 g_return_val_if_fail (G_IS_FILE (file), NULL);
720 g_return_val_if_fail (display_name != NULL, NULL);
722 iface = G_FILE_GET_IFACE (file);
724 return (* iface->get_child_for_display_name) (file, display_name, error);
729 * @file: input #GFile.
730 * @prefix: input #GFile.
732 * Checks whether @file has the prefix specified by @prefix. In other word,
733 * if the names of initial elements of @file<!-- -->s pathname match @prefix.
734 * Only full pathname elements are matched, so a path like /foo is not
735 * considered a prefix of /foobar, only of /foo/bar.
737 * This call does no i/o, as it works purely on names. As such it can
738 * sometimes return %FALSE even if @file is inside a @prefix (from a
739 * filesystem point of view), because the prefix of @file is an alias
742 * Virtual: prefix_matches
743 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix.
747 g_file_has_prefix (GFile *file,
752 g_return_val_if_fail (G_IS_FILE (file), FALSE);
753 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
755 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
758 iface = G_FILE_GET_IFACE (file);
760 /* The vtable function differs in arg order since we're
761 using the old contains_file call */
762 return (* iface->prefix_matches) (prefix, file);
766 * g_file_get_relative_path:
767 * @parent: input #GFile.
768 * @descendant: input #GFile.
770 * Gets the path for @descendant relative to @parent.
772 * This call does no blocking i/o.
774 * Returns: string with the relative path from @descendant
775 * to @parent, or %NULL if @descendant doesn't have @parent as prefix.
776 * The returned string should be freed with g_free() when no longer needed.
779 g_file_get_relative_path (GFile *parent,
784 g_return_val_if_fail (G_IS_FILE (parent), NULL);
785 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
787 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
790 iface = G_FILE_GET_IFACE (parent);
792 return (* iface->get_relative_path) (parent, descendant);
796 * g_file_resolve_relative_path:
797 * @file: input #GFile.
798 * @relative_path: a given relative path string.
800 * Resolves a relative path for @file to an absolute path.
802 * This call does no blocking i/o.
804 * Returns: (transfer full): #GFile to the resolved path. %NULL if @relative_path
805 * is %NULL or if @file is invalid.
806 * Free the returned object with g_object_unref().
809 g_file_resolve_relative_path (GFile *file,
810 const char *relative_path)
814 g_return_val_if_fail (G_IS_FILE (file), NULL);
815 g_return_val_if_fail (relative_path != NULL, NULL);
817 iface = G_FILE_GET_IFACE (file);
819 return (* iface->resolve_relative_path) (file, relative_path);
823 * g_file_enumerate_children:
824 * @file: input #GFile.
825 * @attributes: an attribute query string.
826 * @flags: a set of #GFileQueryInfoFlags.
827 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
828 * @error: #GError for error reporting.
830 * Gets the requested information about the files in a directory. The result
831 * is a #GFileEnumerator object that will give out #GFileInfo objects for
832 * all the files in the directory.
834 * The @attributes value is a string that specifies the file attributes that
835 * should be gathered. It is not an error if it's not possible to read a particular
836 * requested attribute from a file - it just won't be set. @attributes should
837 * be a comma-separated list of attributes or attribute wildcards. The wildcard "*"
838 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
839 * namespace. An example attribute query be "standard::*,owner::user".
840 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
842 * If @cancellable is not %NULL, then the operation can be cancelled by
843 * triggering the cancellable object from another thread. If the operation
844 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
846 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
847 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
848 * Other errors are possible too.
850 * Returns: (transfer full): A #GFileEnumerator if successful, %NULL on error.
851 * Free the returned object with g_object_unref().
854 g_file_enumerate_children (GFile *file,
855 const char *attributes,
856 GFileQueryInfoFlags flags,
857 GCancellable *cancellable,
863 g_return_val_if_fail (G_IS_FILE (file), NULL);
865 if (g_cancellable_set_error_if_cancelled (cancellable, error))
868 iface = G_FILE_GET_IFACE (file);
870 if (iface->enumerate_children == NULL)
872 g_set_error_literal (error, G_IO_ERROR,
873 G_IO_ERROR_NOT_SUPPORTED,
874 _("Operation not supported"));
878 return (* iface->enumerate_children) (file, attributes, flags,
883 * g_file_enumerate_children_async:
884 * @file: input #GFile.
885 * @attributes: an attribute query string.
886 * @flags: a set of #GFileQueryInfoFlags.
887 * @io_priority: the <link linkend="io-priority">I/O priority</link>
889 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
890 * @callback: (scope async) : a #GAsyncReadyCallback to call when the
891 * request is satisfied
892 * @user_data: (closure): the data to pass to callback function
894 * Asynchronously gets the requested information about the files in a directory. The result
895 * is a #GFileEnumerator object that will give out #GFileInfo objects for
896 * all the files in the directory.
898 * For more details, see g_file_enumerate_children() which is
899 * the synchronous version of this call.
901 * When the operation is finished, @callback will be called. You can then call
902 * g_file_enumerate_children_finish() to get the result of the operation.
905 g_file_enumerate_children_async (GFile *file,
906 const char *attributes,
907 GFileQueryInfoFlags flags,
909 GCancellable *cancellable,
910 GAsyncReadyCallback callback,
915 g_return_if_fail (G_IS_FILE (file));
917 iface = G_FILE_GET_IFACE (file);
918 (* iface->enumerate_children_async) (file,
928 * g_file_enumerate_children_finish:
929 * @file: input #GFile.
930 * @res: a #GAsyncResult.
933 * Finishes an async enumerate children operation.
934 * See g_file_enumerate_children_async().
936 * Returns: (transfer full): a #GFileEnumerator or %NULL if an error occurred.
937 * Free the returned object with g_object_unref().
940 g_file_enumerate_children_finish (GFile *file,
946 g_return_val_if_fail (G_IS_FILE (file), NULL);
947 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
949 if (G_IS_SIMPLE_ASYNC_RESULT (res))
951 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
952 if (g_simple_async_result_propagate_error (simple, error))
956 iface = G_FILE_GET_IFACE (file);
957 return (* iface->enumerate_children_finish) (file, res, error);
961 * g_file_query_exists:
962 * @file: input #GFile.
963 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
965 * Utility function to check if a particular file exists. This is
966 * implemented using g_file_query_info() and as such does blocking I/O.
968 * Note that in many cases it is racy to first check for file existence
969 * and then execute something based on the outcome of that, because the
970 * file might have been created or removed in between the operations. The
971 * general approach to handling that is to not check, but just do the
972 * operation and handle the errors as they come.
974 * As an example of race-free checking, take the case of reading a file, and
975 * if it doesn't exist, creating it. There are two racy versions: read it, and
976 * on error create it; and: check if it exists, if not create it. These
977 * can both result in two processes creating the file (with perhaps a partially
978 * written file as the result). The correct approach is to always try to create
979 * the file with g_file_create() which will either atomically create the file
980 * or fail with a G_IO_ERROR_EXISTS error.
982 * However, in many cases an existence check is useful in a user
983 * interface, for instance to make a menu item sensitive/insensitive, so that
984 * you don't have to fool users that something is possible and then just show
985 * and error dialog. If you do this, you should make sure to also handle the
986 * errors that can happen due to races when you execute the operation.
988 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
991 g_file_query_exists (GFile *file,
992 GCancellable *cancellable)
996 g_return_val_if_fail (G_IS_FILE(file), FALSE);
998 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
999 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1002 g_object_unref (info);
1010 * g_file_query_file_type:
1011 * @file: input #GFile.
1012 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
1013 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1015 * Utility function to inspect the #GFileType of a file. This is
1016 * implemented using g_file_query_info() and as such does blocking I/O.
1018 * The primary use case of this method is to check if a file is a regular file,
1019 * directory, or symlink.
1021 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
1027 g_file_query_file_type (GFile *file,
1028 GFileQueryInfoFlags flags,
1029 GCancellable *cancellable)
1032 GFileType file_type;
1034 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1035 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1039 file_type = g_file_info_get_file_type (info);
1040 g_object_unref (info);
1043 file_type = G_FILE_TYPE_UNKNOWN;
1049 * g_file_query_info:
1050 * @file: input #GFile.
1051 * @attributes: an attribute query string.
1052 * @flags: a set of #GFileQueryInfoFlags.
1053 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1054 * @error: a #GError.
1056 * Gets the requested information about specified @file. The result
1057 * is a #GFileInfo object that contains key-value attributes (such as
1058 * the type or size of the file).
1060 * The @attributes value is a string that specifies the file attributes that
1061 * should be gathered. It is not an error if it's not possible to read a particular
1062 * requested attribute from a file - it just won't be set. @attributes should
1063 * be a comma-separated list of attributes or attribute wildcards. The wildcard "*"
1064 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1065 * namespace. An example attribute query be "standard::*,owner::user".
1066 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1068 * If @cancellable is not %NULL, then the operation can be cancelled by
1069 * triggering the cancellable object from another thread. If the operation
1070 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1072 * For symlinks, normally the information about the target of the
1073 * symlink is returned, rather than information about the symlink itself.
1074 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1075 * information about the symlink itself will be returned. Also, for symlinks
1076 * that point to non-existing files the information about the symlink itself
1079 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1080 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1082 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL on error.
1083 * Free the returned object with g_object_unref().
1086 g_file_query_info (GFile *file,
1087 const char *attributes,
1088 GFileQueryInfoFlags flags,
1089 GCancellable *cancellable,
1094 g_return_val_if_fail (G_IS_FILE (file), NULL);
1096 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1099 iface = G_FILE_GET_IFACE (file);
1101 if (iface->query_info == NULL)
1103 g_set_error_literal (error, G_IO_ERROR,
1104 G_IO_ERROR_NOT_SUPPORTED,
1105 _("Operation not supported"));
1109 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1113 * g_file_query_info_async:
1114 * @file: input #GFile.
1115 * @attributes: an attribute query string.
1116 * @flags: a set of #GFileQueryInfoFlags.
1117 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1119 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1120 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1121 * @user_data: (closure): the data to pass to callback function
1123 * Asynchronously gets the requested information about specified @file. The result
1124 * is a #GFileInfo object that contains key-value attributes (such as type or size
1127 * For more details, see g_file_query_info() which is
1128 * the synchronous version of this call.
1130 * When the operation is finished, @callback will be called. You can then call
1131 * g_file_query_info_finish() to get the result of the operation.
1134 g_file_query_info_async (GFile *file,
1135 const char *attributes,
1136 GFileQueryInfoFlags flags,
1138 GCancellable *cancellable,
1139 GAsyncReadyCallback callback,
1144 g_return_if_fail (G_IS_FILE (file));
1146 iface = G_FILE_GET_IFACE (file);
1147 (* iface->query_info_async) (file,
1157 * g_file_query_info_finish:
1158 * @file: input #GFile.
1159 * @res: a #GAsyncResult.
1160 * @error: a #GError.
1162 * Finishes an asynchronous file info query.
1163 * See g_file_query_info_async().
1165 * Returns: (transfer full): #GFileInfo for given @file or %NULL on error.
1166 * Free the returned object with g_object_unref().
1169 g_file_query_info_finish (GFile *file,
1175 g_return_val_if_fail (G_IS_FILE (file), NULL);
1176 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1178 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1180 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1181 if (g_simple_async_result_propagate_error (simple, error))
1185 iface = G_FILE_GET_IFACE (file);
1186 return (* iface->query_info_finish) (file, res, error);
1190 * g_file_query_filesystem_info:
1191 * @file: input #GFile.
1192 * @attributes: an attribute query string.
1193 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1194 * @error: a #GError.
1196 * Similar to g_file_query_info(), but obtains information
1197 * about the filesystem the @file is on, rather than the file itself.
1198 * For instance the amount of space available and the type of
1201 * The @attributes value is a string that specifies the file attributes that
1202 * should be gathered. It is not an error if it's not possible to read a particular
1203 * requested attribute from a file - it just won't be set. @attributes should
1204 * be a comma-separated list of attributes or attribute wildcards. The wildcard "*"
1205 * means all attributes, and a wildcard like "filesystem::*" means all attributes in the
1206 * filesystem namespace. The standard namespace for filesystem attributes is "filesystem".
1207 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1208 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1209 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1211 * If @cancellable is not %NULL, then the operation can be cancelled by
1212 * triggering the cancellable object from another thread. If the operation
1213 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1215 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1216 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1218 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1219 * Free the returned object with g_object_unref().
1222 g_file_query_filesystem_info (GFile *file,
1223 const char *attributes,
1224 GCancellable *cancellable,
1229 g_return_val_if_fail (G_IS_FILE (file), NULL);
1231 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1234 iface = G_FILE_GET_IFACE (file);
1236 if (iface->query_filesystem_info == NULL)
1238 g_set_error_literal (error, G_IO_ERROR,
1239 G_IO_ERROR_NOT_SUPPORTED,
1240 _("Operation not supported"));
1244 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1248 * g_file_query_filesystem_info_async:
1249 * @file: input #GFile.
1250 * @attributes: an attribute query string.
1251 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1253 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1254 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1255 * @user_data: (closure): the data to pass to callback function
1257 * Asynchronously gets the requested information about the filesystem
1258 * that the specified @file is on. The result is a #GFileInfo object
1259 * that contains key-value attributes (such as type or size for the
1262 * For more details, see g_file_query_filesystem_info() which is the
1263 * synchronous version of this call.
1265 * When the operation is finished, @callback will be called. You can
1266 * then call g_file_query_info_finish() to get the result of the
1270 g_file_query_filesystem_info_async (GFile *file,
1271 const char *attributes,
1273 GCancellable *cancellable,
1274 GAsyncReadyCallback callback,
1279 g_return_if_fail (G_IS_FILE (file));
1281 iface = G_FILE_GET_IFACE (file);
1282 (* iface->query_filesystem_info_async) (file,
1291 * g_file_query_filesystem_info_finish:
1292 * @file: input #GFile.
1293 * @res: a #GAsyncResult.
1294 * @error: a #GError.
1296 * Finishes an asynchronous filesystem info query. See
1297 * g_file_query_filesystem_info_async().
1299 * Returns: (transfer full): #GFileInfo for given @file or %NULL on error.
1300 * Free the returned object with g_object_unref().
1303 g_file_query_filesystem_info_finish (GFile *file,
1309 g_return_val_if_fail (G_IS_FILE (file), NULL);
1310 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1312 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1314 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1315 if (g_simple_async_result_propagate_error (simple, error))
1319 iface = G_FILE_GET_IFACE (file);
1320 return (* iface->query_filesystem_info_finish) (file, res, error);
1324 * g_file_find_enclosing_mount:
1325 * @file: input #GFile.
1326 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1327 * @error: a #GError.
1329 * Gets a #GMount for the #GFile.
1331 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1332 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1335 * If @cancellable is not %NULL, then the operation can be cancelled by
1336 * triggering the cancellable object from another thread. If the operation
1337 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1339 * Returns: (transfer full): a #GMount where the @file is located or %NULL on error.
1340 * Free the returned object with g_object_unref().
1343 g_file_find_enclosing_mount (GFile *file,
1344 GCancellable *cancellable,
1349 g_return_val_if_fail (G_IS_FILE (file), NULL);
1351 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1354 iface = G_FILE_GET_IFACE (file);
1355 if (iface->find_enclosing_mount == NULL)
1358 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1359 /* Translators: This is an error message when trying to find the
1360 * enclosing (user visible) mount of a file, but none exists. */
1361 _("Containing mount does not exist"));
1365 return (* iface->find_enclosing_mount) (file, cancellable, error);
1369 * g_file_find_enclosing_mount_async:
1371 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1373 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1374 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1375 * @user_data: (closure): the data to pass to callback function
1377 * Asynchronously gets the mount for the file.
1379 * For more details, see g_file_find_enclosing_mount() which is
1380 * the synchronous version of this call.
1382 * When the operation is finished, @callback will be called. You can then call
1383 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1386 g_file_find_enclosing_mount_async (GFile *file,
1388 GCancellable *cancellable,
1389 GAsyncReadyCallback callback,
1394 g_return_if_fail (G_IS_FILE (file));
1396 iface = G_FILE_GET_IFACE (file);
1397 (* iface->find_enclosing_mount_async) (file,
1405 * g_file_find_enclosing_mount_finish:
1407 * @res: a #GAsyncResult
1410 * Finishes an asynchronous find mount request.
1411 * See g_file_find_enclosing_mount_async().
1413 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1414 * Free the returned object with g_object_unref().
1417 g_file_find_enclosing_mount_finish (GFile *file,
1423 g_return_val_if_fail (G_IS_FILE (file), NULL);
1424 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1426 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1428 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1429 if (g_simple_async_result_propagate_error (simple, error))
1433 iface = G_FILE_GET_IFACE (file);
1434 return (* iface->find_enclosing_mount_finish) (file, res, error);
1440 * @file: #GFile to read.
1441 * @cancellable: (allow-none): a #GCancellable
1442 * @error: a #GError, or %NULL
1444 * Opens a file for reading. The result is a #GFileInputStream that
1445 * can be used to read the contents of the file.
1447 * If @cancellable is not %NULL, then the operation can be cancelled by
1448 * triggering the cancellable object from another thread. If the operation
1449 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1451 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1452 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1453 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1456 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1457 * Free the returned object with g_object_unref().
1460 g_file_read (GFile *file,
1461 GCancellable *cancellable,
1466 g_return_val_if_fail (G_IS_FILE (file), NULL);
1468 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1471 iface = G_FILE_GET_IFACE (file);
1473 if (iface->read_fn == NULL)
1475 g_set_error_literal (error, G_IO_ERROR,
1476 G_IO_ERROR_NOT_SUPPORTED,
1477 _("Operation not supported"));
1481 return (* iface->read_fn) (file, cancellable, error);
1486 * @file: input #GFile.
1487 * @flags: a set of #GFileCreateFlags.
1488 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1489 * @error: a #GError, or %NULL
1491 * Gets an output stream for appending data to the file. If
1492 * the file doesn't already exist it is created.
1494 * By default files created are generally readable by everyone,
1495 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1496 * will be made readable only to the current user, to the level that
1497 * is supported on the target filesystem.
1499 * If @cancellable is not %NULL, then the operation can be cancelled by
1500 * triggering the cancellable object from another thread. If the operation
1501 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1503 * Some file systems don't allow all file names, and may
1504 * return an %G_IO_ERROR_INVALID_FILENAME error.
1505 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1506 * returned. Other errors are possible too, and depend on what kind of
1507 * filesystem the file is on.
1509 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1510 * Free the returned object with g_object_unref().
1513 g_file_append_to (GFile *file,
1514 GFileCreateFlags flags,
1515 GCancellable *cancellable,
1520 g_return_val_if_fail (G_IS_FILE (file), NULL);
1522 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1525 iface = G_FILE_GET_IFACE (file);
1527 if (iface->append_to == NULL)
1529 g_set_error_literal (error, G_IO_ERROR,
1530 G_IO_ERROR_NOT_SUPPORTED,
1531 _("Operation not supported"));
1535 return (* iface->append_to) (file, flags, cancellable, error);
1540 * @file: input #GFile.
1541 * @flags: a set of #GFileCreateFlags.
1542 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1543 * @error: a #GError, or %NULL
1545 * Creates a new file and returns an output stream for writing to it.
1546 * The file must not already exist.
1548 * By default files created are generally readable by everyone,
1549 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1550 * will be made readable only to the current user, to the level that
1551 * is supported on the target filesystem.
1553 * If @cancellable is not %NULL, then the operation can be cancelled by
1554 * triggering the cancellable object from another thread. If the operation
1555 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1557 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1558 * error will be returned.
1559 * Some file systems don't allow all file names, and may
1560 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1561 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1562 * Other errors are possible too, and depend on what kind of
1563 * filesystem the file is on.
1565 * Returns: (transfer full): a #GFileOutputStream for the newly created file, or
1567 * Free the returned object with g_object_unref().
1570 g_file_create (GFile *file,
1571 GFileCreateFlags flags,
1572 GCancellable *cancellable,
1577 g_return_val_if_fail (G_IS_FILE (file), NULL);
1579 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1582 iface = G_FILE_GET_IFACE (file);
1584 if (iface->create == NULL)
1586 g_set_error_literal (error, G_IO_ERROR,
1587 G_IO_ERROR_NOT_SUPPORTED,
1588 _("Operation not supported"));
1592 return (* iface->create) (file, flags, cancellable, error);
1597 * @file: input #GFile.
1598 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link> for the
1599 * current #GFile, or #NULL to ignore.
1600 * @make_backup: %TRUE if a backup should be created.
1601 * @flags: a set of #GFileCreateFlags.
1602 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1603 * @error: a #GError, or %NULL
1605 * Returns an output stream for overwriting the file, possibly
1606 * creating a backup copy of the file first. If the file doesn't exist,
1607 * it will be created.
1609 * This will try to replace the file in the safest way possible so
1610 * that any errors during the writing will not affect an already
1611 * existing copy of the file. For instance, for local files it
1612 * may write to a temporary file and then atomically rename over
1613 * the destination when the stream is closed.
1615 * By default files created are generally readable by everyone,
1616 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1617 * will be made readable only to the current user, to the level that
1618 * is supported on the target filesystem.
1620 * If @cancellable is not %NULL, then the operation can be cancelled by
1621 * triggering the cancellable object from another thread. If the operation
1622 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1624 * If you pass in a non-#NULL @etag value, then this value is
1625 * compared to the current entity tag of the file, and if they differ
1626 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1627 * that the file has been changed since you last read it. You can get
1628 * the new etag from g_file_output_stream_get_etag() after you've
1629 * finished writing and closed the #GFileOutputStream. When you load
1630 * a new file you can use g_file_input_stream_query_info() to get
1631 * the etag of the file.
1633 * If @make_backup is %TRUE, this function will attempt to make a backup
1634 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1635 * error will be returned. If you want to replace anyway, try again with
1636 * @make_backup set to %FALSE.
1638 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1639 * and if the file is some other form of non-regular file then a
1640 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1641 * Some file systems don't allow all file names, and may
1642 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1643 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1644 * Other errors are possible too, and depend on what kind of
1645 * filesystem the file is on.
1647 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1648 * Free the returned object with g_object_unref().
1651 g_file_replace (GFile *file,
1653 gboolean make_backup,
1654 GFileCreateFlags flags,
1655 GCancellable *cancellable,
1660 g_return_val_if_fail (G_IS_FILE (file), NULL);
1662 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1665 iface = G_FILE_GET_IFACE (file);
1667 if (iface->replace == NULL)
1669 g_set_error_literal (error, G_IO_ERROR,
1670 G_IO_ERROR_NOT_SUPPORTED,
1671 _("Operation not supported"));
1676 /* Handle empty tag string as NULL in consistent way. */
1677 if (etag && *etag == 0)
1680 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1684 * g_file_open_readwrite:
1685 * @file: #GFile to open
1686 * @cancellable: (allow-none): a #GCancellable
1687 * @error: a #GError, or %NULL
1689 * Opens an existing file for reading and writing. The result is
1690 * a #GFileIOStream that can be used to read and write the contents of the file.
1692 * If @cancellable is not %NULL, then the operation can be cancelled by
1693 * triggering the cancellable object from another thread. If the operation
1694 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1696 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1697 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1698 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1699 * Note that in many non-local file cases read and write streams are not supported,
1700 * so make sure you really need to do read and write streaming, rather than
1701 * just opening for reading or writing.
1703 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1704 * Free the returned object with g_object_unref().
1709 g_file_open_readwrite (GFile *file,
1710 GCancellable *cancellable,
1715 g_return_val_if_fail (G_IS_FILE (file), NULL);
1717 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1720 iface = G_FILE_GET_IFACE (file);
1722 if (iface->open_readwrite == NULL)
1724 g_set_error_literal (error, G_IO_ERROR,
1725 G_IO_ERROR_NOT_SUPPORTED,
1726 _("Operation not supported"));
1730 return (* iface->open_readwrite) (file, cancellable, error);
1734 * g_file_create_readwrite:
1736 * @flags: a set of #GFileCreateFlags
1737 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
1738 * @error: return location for a #GError, or %NULL
1740 * Creates a new file and returns a stream for reading and writing to it.
1741 * The file must not already exist.
1743 * By default files created are generally readable by everyone,
1744 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1745 * will be made readable only to the current user, to the level that
1746 * is supported on the target filesystem.
1748 * If @cancellable is not %NULL, then the operation can be cancelled by
1749 * triggering the cancellable object from another thread. If the operation
1750 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1752 * If a file or directory with this name already exists the %G_IO_ERROR_EXISTS
1753 * error will be returned. Some file systems don't allow all file names,
1754 * and may return an %G_IO_ERROR_INVALID_FILENAME error, and if the name
1755 * is too long, %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors
1756 * are possible too, and depend on what kind of filesystem the file is on.
1758 * Note that in many non-local file cases read and write streams are not
1759 * supported, so make sure you really need to do read and write streaming,
1760 * rather than just opening for reading or writing.
1762 * Returns: (transfer full): a #GFileIOStream for the newly created file, or %NULL on error.
1763 * Free the returned object with g_object_unref().
1768 g_file_create_readwrite (GFile *file,
1769 GFileCreateFlags flags,
1770 GCancellable *cancellable,
1775 g_return_val_if_fail (G_IS_FILE (file), NULL);
1777 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1780 iface = G_FILE_GET_IFACE (file);
1782 if (iface->create_readwrite == NULL)
1784 g_set_error_literal (error, G_IO_ERROR,
1785 G_IO_ERROR_NOT_SUPPORTED,
1786 _("Operation not supported"));
1790 return (* iface->create_readwrite) (file, flags, cancellable, error);
1794 * g_file_replace_readwrite:
1796 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link> for the
1797 * current #GFile, or #NULL to ignore
1798 * @make_backup: %TRUE if a backup should be created
1799 * @flags: a set of #GFileCreateFlags
1800 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
1801 * @error: return location for a #GError, or %NULL
1803 * Returns an output stream for overwriting the file in readwrite mode,
1804 * possibly creating a backup copy of the file first. If the file doesn't
1805 * exist, it will be created.
1807 * For details about the behaviour, see g_file_replace() which does the same
1808 * thing but returns an output stream only.
1810 * Note that in many non-local file cases read and write streams are not
1811 * supported, so make sure you really need to do read and write streaming,
1812 * rather than just opening for reading or writing.
1814 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1815 * Free the returned object with g_object_unref().
1820 g_file_replace_readwrite (GFile *file,
1822 gboolean make_backup,
1823 GFileCreateFlags flags,
1824 GCancellable *cancellable,
1829 g_return_val_if_fail (G_IS_FILE (file), NULL);
1831 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1834 iface = G_FILE_GET_IFACE (file);
1836 if (iface->replace_readwrite == NULL)
1838 g_set_error_literal (error, G_IO_ERROR,
1839 G_IO_ERROR_NOT_SUPPORTED,
1840 _("Operation not supported"));
1844 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1848 * g_file_read_async:
1849 * @file: input #GFile
1850 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1852 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1853 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1854 * @user_data: (closure): the data to pass to callback function
1856 * Asynchronously opens @file for reading.
1858 * For more details, see g_file_read() which is
1859 * the synchronous version of this call.
1861 * When the operation is finished, @callback will be called. You can then call
1862 * g_file_read_finish() to get the result of the operation.
1865 g_file_read_async (GFile *file,
1867 GCancellable *cancellable,
1868 GAsyncReadyCallback callback,
1873 g_return_if_fail (G_IS_FILE (file));
1875 iface = G_FILE_GET_IFACE (file);
1876 (* iface->read_async) (file,
1884 * g_file_read_finish:
1885 * @file: input #GFile.
1886 * @res: a #GAsyncResult.
1887 * @error: a #GError, or %NULL
1889 * Finishes an asynchronous file read operation started with
1890 * g_file_read_async().
1892 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
1893 * Free the returned object with g_object_unref().
1896 g_file_read_finish (GFile *file,
1902 g_return_val_if_fail (G_IS_FILE (file), NULL);
1903 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1905 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1907 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1908 if (g_simple_async_result_propagate_error (simple, error))
1912 iface = G_FILE_GET_IFACE (file);
1913 return (* iface->read_finish) (file, res, error);
1917 * g_file_append_to_async:
1918 * @file: input #GFile.
1919 * @flags: a set of #GFileCreateFlags.
1920 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1922 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1923 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1924 * @user_data: (closure): the data to pass to callback function
1926 * Asynchronously opens @file for appending.
1928 * For more details, see g_file_append_to() which is
1929 * the synchronous version of this call.
1931 * When the operation is finished, @callback will be called. You can then call
1932 * g_file_append_to_finish() to get the result of the operation.
1935 g_file_append_to_async (GFile *file,
1936 GFileCreateFlags flags,
1938 GCancellable *cancellable,
1939 GAsyncReadyCallback callback,
1944 g_return_if_fail (G_IS_FILE (file));
1946 iface = G_FILE_GET_IFACE (file);
1947 (* iface->append_to_async) (file,
1956 * g_file_append_to_finish:
1957 * @file: input #GFile.
1958 * @res: #GAsyncResult
1959 * @error: a #GError, or %NULL
1961 * Finishes an asynchronous file append operation started with
1962 * g_file_append_to_async().
1964 * Returns: (transfer full): a valid #GFileOutputStream or %NULL on error.
1965 * Free the returned object with g_object_unref().
1968 g_file_append_to_finish (GFile *file,
1974 g_return_val_if_fail (G_IS_FILE (file), NULL);
1975 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1977 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1979 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1980 if (g_simple_async_result_propagate_error (simple, error))
1984 iface = G_FILE_GET_IFACE (file);
1985 return (* iface->append_to_finish) (file, res, error);
1989 * g_file_create_async:
1990 * @file: input #GFile.
1991 * @flags: a set of #GFileCreateFlags.
1992 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1994 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1995 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1996 * @user_data: (closure): the data to pass to callback function
1998 * Asynchronously creates a new file and returns an output stream for writing to it.
1999 * The file must not already exist.
2001 * For more details, see g_file_create() which is
2002 * the synchronous version of this call.
2004 * When the operation is finished, @callback will be called. You can then call
2005 * g_file_create_finish() to get the result of the operation.
2008 g_file_create_async (GFile *file,
2009 GFileCreateFlags flags,
2011 GCancellable *cancellable,
2012 GAsyncReadyCallback callback,
2017 g_return_if_fail (G_IS_FILE (file));
2019 iface = G_FILE_GET_IFACE (file);
2020 (* iface->create_async) (file,
2029 * g_file_create_finish:
2030 * @file: input #GFile.
2031 * @res: a #GAsyncResult.
2032 * @error: a #GError, or %NULL
2034 * Finishes an asynchronous file create operation started with
2035 * g_file_create_async().
2037 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2038 * Free the returned object with g_object_unref().
2041 g_file_create_finish (GFile *file,
2047 g_return_val_if_fail (G_IS_FILE (file), NULL);
2048 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2050 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2052 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2053 if (g_simple_async_result_propagate_error (simple, error))
2057 iface = G_FILE_GET_IFACE (file);
2058 return (* iface->create_finish) (file, res, error);
2062 * g_file_replace_async:
2063 * @file: input #GFile.
2064 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link> for the
2065 * current #GFile, or NULL to ignore.
2066 * @make_backup: %TRUE if a backup should be created.
2067 * @flags: a set of #GFileCreateFlags.
2068 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2070 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
2071 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
2072 * @user_data: (closure): the data to pass to callback function
2074 * Asynchronously overwrites the file, replacing the contents, possibly
2075 * creating a backup copy of the file first.
2077 * For more details, see g_file_replace() which is
2078 * the synchronous version of this call.
2080 * When the operation is finished, @callback will be called. You can then call
2081 * g_file_replace_finish() to get the result of the operation.
2084 g_file_replace_async (GFile *file,
2086 gboolean make_backup,
2087 GFileCreateFlags flags,
2089 GCancellable *cancellable,
2090 GAsyncReadyCallback callback,
2095 g_return_if_fail (G_IS_FILE (file));
2097 iface = G_FILE_GET_IFACE (file);
2098 (* iface->replace_async) (file,
2109 * g_file_replace_finish:
2110 * @file: input #GFile.
2111 * @res: a #GAsyncResult.
2112 * @error: a #GError, or %NULL
2114 * Finishes an asynchronous file replace operation started with
2115 * g_file_replace_async().
2117 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2118 * Free the returned object with g_object_unref().
2121 g_file_replace_finish (GFile *file,
2127 g_return_val_if_fail (G_IS_FILE (file), NULL);
2128 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2130 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2132 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2133 if (g_simple_async_result_propagate_error (simple, error))
2137 iface = G_FILE_GET_IFACE (file);
2138 return (* iface->replace_finish) (file, res, error);
2143 * g_file_open_readwrite_async:
2144 * @file: input #GFile.
2145 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2147 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
2148 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
2149 * @user_data: (closure): the data to pass to callback function
2151 * Asynchronously opens @file for reading and writing.
2153 * For more details, see g_file_open_readwrite() which is
2154 * the synchronous version of this call.
2156 * When the operation is finished, @callback will be called. You can then call
2157 * g_file_open_readwrite_finish() to get the result of the operation.
2162 g_file_open_readwrite_async (GFile *file,
2164 GCancellable *cancellable,
2165 GAsyncReadyCallback callback,
2170 g_return_if_fail (G_IS_FILE (file));
2172 iface = G_FILE_GET_IFACE (file);
2173 (* iface->open_readwrite_async) (file,
2181 * g_file_open_readwrite_finish:
2182 * @file: input #GFile.
2183 * @res: a #GAsyncResult.
2184 * @error: a #GError, or %NULL
2186 * Finishes an asynchronous file read operation started with
2187 * g_file_open_readwrite_async().
2189 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2190 * Free the returned object with g_object_unref().
2195 g_file_open_readwrite_finish (GFile *file,
2201 g_return_val_if_fail (G_IS_FILE (file), NULL);
2202 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2204 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2206 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2207 if (g_simple_async_result_propagate_error (simple, error))
2211 iface = G_FILE_GET_IFACE (file);
2212 return (* iface->open_readwrite_finish) (file, res, error);
2217 * g_file_create_readwrite_async:
2218 * @file: input #GFile
2219 * @flags: a set of #GFileCreateFlags
2220 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2222 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
2223 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
2224 * @user_data: (closure): the data to pass to callback function
2226 * Asynchronously creates a new file and returns a stream for reading and
2227 * writing to it. The file must not already exist.
2229 * For more details, see g_file_create_readwrite() which is
2230 * the synchronous version of this call.
2232 * When the operation is finished, @callback will be called. You can then
2233 * call g_file_create_readwrite_finish() to get the result of the operation.
2238 g_file_create_readwrite_async (GFile *file,
2239 GFileCreateFlags flags,
2241 GCancellable *cancellable,
2242 GAsyncReadyCallback callback,
2247 g_return_if_fail (G_IS_FILE (file));
2249 iface = G_FILE_GET_IFACE (file);
2250 (* iface->create_readwrite_async) (file,
2259 * g_file_create_readwrite_finish:
2260 * @file: input #GFile
2261 * @res: a #GAsyncResult
2262 * @error: a #GError, or %NULL
2264 * Finishes an asynchronous file create operation started with
2265 * g_file_create_readwrite_async().
2267 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2268 * Free the returned object with g_object_unref().
2273 g_file_create_readwrite_finish (GFile *file,
2279 g_return_val_if_fail (G_IS_FILE (file), NULL);
2280 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2282 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2284 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2285 if (g_simple_async_result_propagate_error (simple, error))
2289 iface = G_FILE_GET_IFACE (file);
2290 return (* iface->create_readwrite_finish) (file, res, error);
2294 * g_file_replace_readwrite_async:
2295 * @file: input #GFile.
2296 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link> for the
2297 * current #GFile, or NULL to ignore.
2298 * @make_backup: %TRUE if a backup should be created.
2299 * @flags: a set of #GFileCreateFlags.
2300 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2302 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
2303 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
2304 * @user_data: (closure): the data to pass to callback function
2306 * Asynchronously overwrites the file in read-write mode, replacing the
2307 * contents, possibly creating a backup copy of the file first.
2309 * For more details, see g_file_replace_readwrite() which is
2310 * the synchronous version of this call.
2312 * When the operation is finished, @callback will be called. You can then
2313 * call g_file_replace_readwrite_finish() to get the result of the operation.
2318 g_file_replace_readwrite_async (GFile *file,
2320 gboolean make_backup,
2321 GFileCreateFlags flags,
2323 GCancellable *cancellable,
2324 GAsyncReadyCallback callback,
2329 g_return_if_fail (G_IS_FILE (file));
2331 iface = G_FILE_GET_IFACE (file);
2332 (* iface->replace_readwrite_async) (file,
2343 * g_file_replace_readwrite_finish:
2344 * @file: input #GFile.
2345 * @res: a #GAsyncResult.
2346 * @error: a #GError, or %NULL
2348 * Finishes an asynchronous file replace operation started with
2349 * g_file_replace_readwrite_async().
2351 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2352 * Free the returned object with g_object_unref().
2357 g_file_replace_readwrite_finish (GFile *file,
2363 g_return_val_if_fail (G_IS_FILE (file), NULL);
2364 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2366 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2368 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2369 if (g_simple_async_result_propagate_error (simple, error))
2373 iface = G_FILE_GET_IFACE (file);
2374 return (* iface->replace_readwrite_finish) (file, res, error);
2378 copy_symlink (GFile *destination,
2379 GFileCopyFlags flags,
2380 GCancellable *cancellable,
2385 gboolean tried_delete;
2387 GFileType file_type;
2389 tried_delete = FALSE;
2393 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2395 /* Maybe it already existed, and we want to overwrite? */
2396 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2397 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2399 g_error_free (my_error);
2402 /* Don't overwrite if the destination is a directory */
2403 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2404 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2405 cancellable, &my_error);
2408 file_type = g_file_info_get_file_type (info);
2409 g_object_unref (info);
2411 if (file_type == G_FILE_TYPE_DIRECTORY)
2413 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2414 _("Can't copy over directory"));
2419 if (!g_file_delete (destination, cancellable, error))
2422 tried_delete = TRUE;
2426 g_propagate_error (error, my_error);
2433 static GInputStream *
2434 open_source_for_copy (GFile *source,
2436 GFileCopyFlags flags,
2437 GCancellable *cancellable,
2443 GFileType file_type;
2446 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2450 /* There was an error opening the source, try to set a good error for it: */
2452 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2454 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2455 * as that is less useful to the app. Better check for errors on the
2458 g_error_free (my_error);
2461 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2462 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2463 cancellable, &my_error);
2465 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2467 file_type = g_file_info_get_file_type (info);
2468 g_object_unref (info);
2470 if (flags & G_FILE_COPY_OVERWRITE)
2472 if (file_type == G_FILE_TYPE_DIRECTORY)
2474 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2475 _("Can't copy directory over directory"));
2478 /* continue to would_recurse error */
2482 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2483 _("Target file exists"));
2489 /* Error getting info from target, return that error
2490 * (except for NOT_FOUND, which is no error here)
2492 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2494 g_propagate_error (error, my_error);
2497 g_clear_error (&my_error);
2500 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2501 _("Can't recursively copy directory"));
2505 g_propagate_error (error, my_error);
2510 should_copy (GFileAttributeInfo *info,
2512 gboolean skip_perms)
2514 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2518 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2519 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2523 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2524 GFileAttributeInfoList *namespaces,
2526 gboolean skip_perms)
2533 s = g_string_new ("");
2537 for (i = 0; i < attributes->n_infos; i++)
2539 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2544 g_string_append_c (s, ',');
2546 g_string_append (s, attributes->infos[i].name);
2553 for (i = 0; i < namespaces->n_infos; i++)
2555 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2560 g_string_append_c (s, ',');
2562 g_string_append (s, namespaces->infos[i].name);
2563 g_string_append (s, "::*");
2568 return g_string_free (s, FALSE);
2572 * g_file_copy_attributes:
2573 * @source: a #GFile with attributes.
2574 * @destination: a #GFile to copy attributes to.
2575 * @flags: a set of #GFileCopyFlags.
2576 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
2577 * @error: a #GError, %NULL to ignore.
2579 * Copies the file attributes from @source to @destination.
2581 * Normally only a subset of the file attributes are copied,
2582 * those that are copies in a normal file copy operation
2583 * (which for instance does not include e.g. owner). However
2584 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2585 * all the metadata that is possible to copy is copied. This
2586 * is useful when implementing move by copy + delete source.
2588 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2591 g_file_copy_attributes (GFile *source,
2593 GFileCopyFlags flags,
2594 GCancellable *cancellable,
2597 GFileAttributeInfoList *attributes, *namespaces;
2598 char *attrs_to_read;
2602 gboolean source_nofollow_symlinks;
2603 gboolean skip_perms;
2605 as_move = flags & G_FILE_COPY_ALL_METADATA;
2606 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2607 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2609 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2610 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2611 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2613 if (attributes == NULL && namespaces == NULL)
2616 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2618 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2619 * we just don't copy it.
2621 info = g_file_query_info (source, attrs_to_read,
2622 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2626 g_free (attrs_to_read);
2631 res = g_file_set_attributes_from_info (destination,
2633 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2636 g_object_unref (info);
2639 g_file_attribute_info_list_unref (attributes);
2640 g_file_attribute_info_list_unref (namespaces);
2646 copy_stream_with_progress (GInputStream *in,
2649 GCancellable *cancellable,
2650 GFileProgressCallback progress_callback,
2651 gpointer progress_callback_data,
2654 gssize n_read, n_written;
2655 goffset current_size;
2656 char buffer[1024*64], *p;
2662 /* avoid performance impact of querying total size when it's not needed */
2663 if (progress_callback)
2665 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2666 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2670 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2671 total_size = g_file_info_get_size (info);
2672 g_object_unref (info);
2675 if (total_size == -1)
2677 info = g_file_query_info (source,
2678 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2679 G_FILE_QUERY_INFO_NONE,
2683 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2684 total_size = g_file_info_get_size (info);
2685 g_object_unref (info);
2690 if (total_size == -1)
2697 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2707 current_size += n_read;
2712 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2713 if (n_written == -1)
2720 n_read -= n_written;
2726 if (progress_callback)
2727 progress_callback (current_size, total_size, progress_callback_data);
2730 /* Make sure we send full copied size */
2731 if (progress_callback)
2732 progress_callback (current_size, total_size, progress_callback_data);
2740 do_splice (int fd_in,
2745 long *bytes_transferd,
2751 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2759 else if (errsv == ENOSYS || errsv == EINVAL)
2760 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2761 _("Splice not supported"));
2763 g_set_error (error, G_IO_ERROR,
2764 g_io_error_from_errno (errsv),
2765 _("Error splicing file: %s"),
2766 g_strerror (errsv));
2771 *bytes_transferd = result;
2776 splice_stream_with_progress (GInputStream *in,
2778 GCancellable *cancellable,
2779 GFileProgressCallback progress_callback,
2780 gpointer progress_callback_data,
2790 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2791 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2793 if (pipe (buffer) != 0)
2795 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2796 "Pipe creation failed");
2801 /* avoid performance impact of querying total size when it's not needed */
2802 if (progress_callback)
2806 if (fstat (fd_in, &sbuf) == 0)
2807 total_size = sbuf.st_size;
2810 if (total_size == -1)
2813 offset_in = offset_out = 0;
2820 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2823 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2834 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2837 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2840 n_read -= n_written;
2843 if (progress_callback)
2844 progress_callback (offset_in, total_size, progress_callback_data);
2847 /* Make sure we send full copied size */
2848 if (progress_callback)
2849 progress_callback (offset_in, total_size, progress_callback_data);
2860 file_copy_fallback (GFile *source,
2862 GFileCopyFlags flags,
2863 GCancellable *cancellable,
2864 GFileProgressCallback progress_callback,
2865 gpointer progress_callback_data,
2874 gboolean fallback = TRUE;
2877 /* need to know the file type */
2878 info = g_file_query_info (source,
2879 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2880 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2887 /* Maybe copy the symlink? */
2888 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
2889 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
2891 target = g_file_info_get_symlink_target (info);
2894 if (!copy_symlink (destination, flags, cancellable, target, error))
2896 g_object_unref (info);
2900 g_object_unref (info);
2903 /* ... else fall back on a regular file copy */
2904 g_object_unref (info);
2906 /* Handle "special" files (pipes, device nodes, ...)? */
2907 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
2909 /* FIXME: could try to recreate device nodes and others? */
2911 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2912 _("Can't copy special file"));
2913 g_object_unref (info);
2916 /* Everything else should just fall back on a regular copy. */
2918 g_object_unref (info);
2920 in = open_source_for_copy (source, destination, flags, cancellable, error);
2924 if (flags & G_FILE_COPY_OVERWRITE)
2926 out = (GOutputStream *)g_file_replace (destination,
2928 flags & G_FILE_COPY_BACKUP,
2929 G_FILE_CREATE_REPLACE_DESTINATION,
2930 cancellable, error);
2934 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2939 g_object_unref (in);
2944 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
2946 GError *splice_err = NULL;
2948 result = splice_stream_with_progress (in, out, cancellable,
2949 progress_callback, progress_callback_data,
2952 if (result || !g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
2956 g_propagate_error (error, splice_err);
2959 g_clear_error (&splice_err);
2964 result = copy_stream_with_progress (in, out, source, cancellable,
2965 progress_callback, progress_callback_data,
2968 /* Don't care about errors in source here */
2969 g_input_stream_close (in, cancellable, NULL);
2971 /* But write errors on close are bad! */
2972 if (!g_output_stream_close (out, cancellable, result ? error : NULL))
2975 g_object_unref (in);
2976 g_object_unref (out);
2978 if (result == FALSE)
2982 /* Ignore errors here. Failure to copy metadata is not a hard error */
2983 g_file_copy_attributes (source, destination,
2984 flags, cancellable, NULL);
2991 * @source: input #GFile.
2992 * @destination: destination #GFile
2993 * @flags: set of #GFileCopyFlags
2994 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
2995 * @progress_callback: (allow-none) (scope call): function to callback with
2996 * progress information, or %NULL if progress information is not needed
2997 * @progress_callback_data: (closure): user data to pass to @progress_callback
2998 * @error: #GError to set on error, or %NULL
3000 * Copies the file @source to the location specified by @destination.
3001 * Can not handle recursive copies of directories.
3003 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3004 * existing @destination file is overwritten.
3006 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3007 * will be copied as symlinks, otherwise the target of the
3008 * @source symlink will be copied.
3010 * If @cancellable is not %NULL, then the operation can be cancelled by
3011 * triggering the cancellable object from another thread. If the operation
3012 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3014 * If @progress_callback is not %NULL, then the operation can be monitored by
3015 * setting this to a #GFileProgressCallback function. @progress_callback_data
3016 * will be passed to this function. It is guaranteed that this callback will
3017 * be called after all data has been transferred with the total number of bytes
3018 * copied during the operation.
3020 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
3021 * error is returned, independent on the status of the @destination.
3023 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
3024 * error G_IO_ERROR_EXISTS is returned.
3026 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
3027 * error is returned. If trying to overwrite a directory with a directory the
3028 * G_IO_ERROR_WOULD_MERGE error is returned.
3030 * If the source is a directory and the target does not exist, or
3031 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3032 * G_IO_ERROR_WOULD_RECURSE error is returned.
3034 * If you are interested in copying the #GFile object itself (not the on-disk
3035 * file), see g_file_dup().
3037 * Returns: %TRUE on success, %FALSE otherwise.
3040 g_file_copy (GFile *source,
3042 GFileCopyFlags flags,
3043 GCancellable *cancellable,
3044 GFileProgressCallback progress_callback,
3045 gpointer progress_callback_data,
3052 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3053 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3055 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3058 iface = G_FILE_GET_IFACE (destination);
3062 res = (* iface->copy) (source, destination,
3064 progress_callback, progress_callback_data,
3070 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3072 g_propagate_error (error, my_error);
3076 g_clear_error (&my_error);
3079 /* If the types are different, and the destination method failed
3080 also try the source method */
3081 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3083 iface = G_FILE_GET_IFACE (source);
3088 res = (* iface->copy) (source, destination,
3090 progress_callback, progress_callback_data,
3096 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3098 g_propagate_error (error, my_error);
3102 g_clear_error (&my_error);
3106 return file_copy_fallback (source, destination, flags, cancellable,
3107 progress_callback, progress_callback_data,
3112 * g_file_copy_async: (skip)
3113 * @source: input #GFile.
3114 * @destination: destination #GFile
3115 * @flags: set of #GFileCopyFlags
3116 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3118 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
3119 * @progress_callback: (allow-none): function to callback with progress
3120 * information, or %NULL if progress information is not needed
3121 * @progress_callback_data: (closure): user data to pass to @progress_callback
3122 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3123 * @user_data: the data to pass to callback function
3125 * Copies the file @source to the location specified by @destination
3126 * asynchronously. For details of the behaviour, see g_file_copy().
3128 * If @progress_callback is not %NULL, then that function that will be called
3129 * just like in g_file_copy(), however the callback will run in the main loop,
3130 * not in the thread that is doing the I/O operation.
3132 * When the operation is finished, @callback will be called. You can then call
3133 * g_file_copy_finish() to get the result of the operation.
3136 g_file_copy_async (GFile *source,
3138 GFileCopyFlags flags,
3140 GCancellable *cancellable,
3141 GFileProgressCallback progress_callback,
3142 gpointer progress_callback_data,
3143 GAsyncReadyCallback callback,
3148 g_return_if_fail (G_IS_FILE (source));
3149 g_return_if_fail (G_IS_FILE (destination));
3151 iface = G_FILE_GET_IFACE (source);
3152 (* iface->copy_async) (source,
3158 progress_callback_data,
3164 * g_file_copy_finish:
3165 * @file: input #GFile.
3166 * @res: a #GAsyncResult.
3167 * @error: a #GError, or %NULL
3169 * Finishes copying the file started with
3170 * g_file_copy_async().
3172 * Returns: a %TRUE on success, %FALSE on error.
3175 g_file_copy_finish (GFile *file,
3181 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3182 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3184 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3186 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3188 if (g_simple_async_result_propagate_error (simple, error))
3192 iface = G_FILE_GET_IFACE (file);
3193 return (* iface->copy_finish) (file, res, error);
3198 * @source: #GFile pointing to the source location.
3199 * @destination: #GFile pointing to the destination location.
3200 * @flags: set of #GFileCopyFlags.
3201 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3202 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback function for updates.
3203 * @progress_callback_data: (closure): gpointer to user data for the callback function.
3204 * @error: #GError for returning error conditions, or %NULL
3207 * Tries to move the file or directory @source to the location specified by @destination.
3208 * If native move operations are supported then this is used, otherwise a copy + delete
3209 * fallback is used. The native implementation may support moving directories (for instance
3210 * on moves inside the same filesystem), but the fallback code does not.
3212 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3213 * existing @destination file is overwritten.
3215 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3216 * will be copied as symlinks, otherwise the target of the
3217 * @source symlink will be copied.
3219 * If @cancellable is not %NULL, then the operation can be cancelled by
3220 * triggering the cancellable object from another thread. If the operation
3221 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3223 * If @progress_callback is not %NULL, then the operation can be monitored by
3224 * setting this to a #GFileProgressCallback function. @progress_callback_data
3225 * will be passed to this function. It is guaranteed that this callback will
3226 * be called after all data has been transferred with the total number of bytes
3227 * copied during the operation.
3229 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
3230 * error is returned, independent on the status of the @destination.
3232 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
3233 * error G_IO_ERROR_EXISTS is returned.
3235 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
3236 * error is returned. If trying to overwrite a directory with a directory the
3237 * G_IO_ERROR_WOULD_MERGE error is returned.
3239 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
3240 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
3241 * may be returned (if the native move operation isn't available).
3243 * Returns: %TRUE on successful move, %FALSE otherwise.
3246 g_file_move (GFile *source,
3248 GFileCopyFlags flags,
3249 GCancellable *cancellable,
3250 GFileProgressCallback progress_callback,
3251 gpointer progress_callback_data,
3258 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3259 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3261 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3264 iface = G_FILE_GET_IFACE (destination);
3268 res = (* iface->move) (source, destination,
3270 progress_callback, progress_callback_data,
3276 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3278 g_propagate_error (error, my_error);
3283 /* If the types are different, and the destination method failed
3284 also try the source method */
3285 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3287 iface = G_FILE_GET_IFACE (source);
3292 res = (* iface->move) (source, destination,
3294 progress_callback, progress_callback_data,
3300 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3302 g_propagate_error (error, my_error);
3308 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3310 g_set_error_literal (error, G_IO_ERROR,
3311 G_IO_ERROR_NOT_SUPPORTED,
3312 _("Operation not supported"));
3316 flags |= G_FILE_COPY_ALL_METADATA;
3317 if (!g_file_copy (source, destination, flags, cancellable,
3318 progress_callback, progress_callback_data,
3322 return g_file_delete (source, cancellable, error);
3326 * g_file_make_directory:
3327 * @file: input #GFile.
3328 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3329 * @error: a #GError, or %NULL
3331 * Creates a directory. Note that this will only create a child directory of
3332 * the immediate parent directory of the path or URI given by the #GFile. To
3333 * recursively create directories, see g_file_make_directory_with_parents().
3334 * This function will fail if the parent directory does not exist, setting
3335 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
3336 * directories, this function will fail, setting @error to
3337 * %G_IO_ERROR_NOT_SUPPORTED.
3339 * For a local #GFile the newly created directory will have the default
3340 * (current) ownership and permissions of the current process.
3342 * If @cancellable is not %NULL, then the operation can be cancelled by
3343 * triggering the cancellable object from another thread. If the operation
3344 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3346 * Returns: %TRUE on successful creation, %FALSE otherwise.
3349 g_file_make_directory (GFile *file,
3350 GCancellable *cancellable,
3355 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3357 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3360 iface = G_FILE_GET_IFACE (file);
3362 if (iface->make_directory == NULL)
3364 g_set_error_literal (error, G_IO_ERROR,
3365 G_IO_ERROR_NOT_SUPPORTED,
3366 _("Operation not supported"));
3370 return (* iface->make_directory) (file, cancellable, error);
3374 * g_file_make_directory_with_parents:
3375 * @file: input #GFile.
3376 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3377 * @error: a #GError, or %NULL
3379 * Creates a directory and any parent directories that may not exist similar to
3380 * 'mkdir -p'. If the file system does not support creating directories, this
3381 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED. If the
3382 * directory itself already exists, this function will fail setting @error
3383 * to %G_IO_ERROR_EXISTS, unlike the similar g_mkdir_with_parents().
3385 * For a local #GFile the newly created directories will have the default
3386 * (current) ownership and permissions of the current process.
3388 * If @cancellable is not %NULL, then the operation can be cancelled by
3389 * triggering the cancellable object from another thread. If the operation
3390 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3392 * Returns: %TRUE if all directories have been successfully created, %FALSE
3398 g_file_make_directory_with_parents (GFile *file,
3399 GCancellable *cancellable,
3403 GFile *work_file = NULL;
3404 GList *list = NULL, *l;
3405 GError *my_error = NULL;
3407 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3409 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3412 result = g_file_make_directory (file, cancellable, &my_error);
3413 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
3416 g_propagate_error (error, my_error);
3420 work_file = g_object_ref (file);
3422 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3426 g_clear_error (&my_error);
3428 parent_file = g_file_get_parent (work_file);
3429 if (parent_file == NULL)
3431 result = g_file_make_directory (parent_file, cancellable, &my_error);
3433 g_object_unref (work_file);
3434 work_file = g_object_ref (parent_file);
3436 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3437 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3439 g_object_unref (parent_file);
3442 g_clear_error (&my_error);
3443 for (l = list; result && l; l = l->next)
3445 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3449 g_object_unref (work_file);
3452 while (list != NULL)
3454 g_object_unref ((GFile *) list->data);
3455 list = g_list_remove (list, list->data);
3460 g_propagate_error (error, my_error);
3464 return g_file_make_directory (file, cancellable, error);
3468 * g_file_make_symbolic_link:
3469 * @file: a #GFile with the name of the symlink to create
3470 * @symlink_value: a string with the path for the target of the new symlink
3471 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3472 * @error: a #GError.
3474 * Creates a symbolic link named @file which contains the string
3477 * If @cancellable is not %NULL, then the operation can be cancelled by
3478 * triggering the cancellable object from another thread. If the operation
3479 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3481 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3484 g_file_make_symbolic_link (GFile *file,
3485 const char *symlink_value,
3486 GCancellable *cancellable,
3491 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3492 g_return_val_if_fail (symlink_value != NULL, FALSE);
3494 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3497 if (*symlink_value == '\0')
3499 g_set_error_literal (error, G_IO_ERROR,
3500 G_IO_ERROR_INVALID_ARGUMENT,
3501 _("Invalid symlink value given"));
3505 iface = G_FILE_GET_IFACE (file);
3507 if (iface->make_symbolic_link == NULL)
3509 g_set_error_literal (error, G_IO_ERROR,
3510 G_IO_ERROR_NOT_SUPPORTED,
3511 _("Operation not supported"));
3515 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3520 * @file: input #GFile.
3521 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3522 * @error: a #GError, or %NULL
3524 * Deletes a file. If the @file is a directory, it will only be deleted if it
3527 * If @cancellable is not %NULL, then the operation can be cancelled by
3528 * triggering the cancellable object from another thread. If the operation
3529 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3531 * Virtual: delete_file
3532 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3535 g_file_delete (GFile *file,
3536 GCancellable *cancellable,
3541 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3543 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3546 iface = G_FILE_GET_IFACE (file);
3548 if (iface->delete_file == NULL)
3550 g_set_error_literal (error, G_IO_ERROR,
3551 G_IO_ERROR_NOT_SUPPORTED,
3552 _("Operation not supported"));
3556 return (* iface->delete_file) (file, cancellable, error);
3561 * @file: #GFile to send to trash.
3562 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3563 * @error: a #GError, or %NULL
3565 * Sends @file to the "Trashcan", if possible. This is similar to
3566 * deleting it, but the user can recover it before emptying the trashcan.
3567 * Not all file systems support trashing, so this call can return the
3568 * %G_IO_ERROR_NOT_SUPPORTED error.
3571 * If @cancellable is not %NULL, then the operation can be cancelled by
3572 * triggering the cancellable object from another thread. If the operation
3573 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3575 * Returns: %TRUE on successful trash, %FALSE otherwise.
3578 g_file_trash (GFile *file,
3579 GCancellable *cancellable,
3584 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3586 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3589 iface = G_FILE_GET_IFACE (file);
3591 if (iface->trash == NULL)
3593 g_set_error_literal (error,
3594 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3595 _("Trash not supported"));
3599 return (* iface->trash) (file, cancellable, error);
3603 * g_file_set_display_name:
3604 * @file: input #GFile.
3605 * @display_name: a string.
3606 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3607 * @error: a #GError, or %NULL
3609 * Renames @file to the specified display name.
3611 * The display name is converted from UTF8 to the correct encoding for the target
3612 * filesystem if possible and the @file is renamed to this.
3614 * If you want to implement a rename operation in the user interface the edit name
3615 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
3616 * widget, and then the result after editing should be passed to g_file_set_display_name().
3618 * On success the resulting converted filename is returned.
3620 * If @cancellable is not %NULL, then the operation can be cancelled by
3621 * triggering the cancellable object from another thread. If the operation
3622 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3624 * Returns: (transfer full): a #GFile specifying what @file was renamed to, or %NULL
3625 * if there was an error.
3626 * Free the returned object with g_object_unref().
3629 g_file_set_display_name (GFile *file,
3630 const char *display_name,
3631 GCancellable *cancellable,
3636 g_return_val_if_fail (G_IS_FILE (file), NULL);
3637 g_return_val_if_fail (display_name != NULL, NULL);
3639 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3643 G_IO_ERROR_INVALID_ARGUMENT,
3644 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3648 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3651 iface = G_FILE_GET_IFACE (file);
3653 return (* iface->set_display_name) (file, display_name, cancellable, error);
3657 * g_file_set_display_name_async:
3658 * @file: input #GFile.
3659 * @display_name: a string.
3660 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3662 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3663 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
3664 * @user_data: (closure): the data to pass to callback function
3666 * Asynchronously sets the display name for a given #GFile.
3668 * For more details, see g_file_set_display_name() which is
3669 * the synchronous version of this call.
3671 * When the operation is finished, @callback will be called. You can then call
3672 * g_file_set_display_name_finish() to get the result of the operation.
3675 g_file_set_display_name_async (GFile *file,
3676 const char *display_name,
3678 GCancellable *cancellable,
3679 GAsyncReadyCallback callback,
3684 g_return_if_fail (G_IS_FILE (file));
3685 g_return_if_fail (display_name != NULL);
3687 iface = G_FILE_GET_IFACE (file);
3688 (* iface->set_display_name_async) (file,
3697 * g_file_set_display_name_finish:
3698 * @file: input #GFile.
3699 * @res: a #GAsyncResult.
3700 * @error: a #GError, or %NULL
3702 * Finishes setting a display name started with
3703 * g_file_set_display_name_async().
3705 * Returns: (transfer full): a #GFile or %NULL on error.
3706 * Free the returned object with g_object_unref().
3709 g_file_set_display_name_finish (GFile *file,
3715 g_return_val_if_fail (G_IS_FILE (file), NULL);
3716 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3718 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3720 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3721 if (g_simple_async_result_propagate_error (simple, error))
3725 iface = G_FILE_GET_IFACE (file);
3726 return (* iface->set_display_name_finish) (file, res, error);
3730 * g_file_query_settable_attributes:
3731 * @file: input #GFile.
3732 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3733 * @error: a #GError, or %NULL
3735 * Obtain the list of settable attributes for the file.
3737 * Returns the type and full attribute name of all the attributes
3738 * that can be set on this file. This doesn't mean setting it will always
3739 * succeed though, you might get an access failure, or some specific
3740 * file may not support a specific attribute.
3742 * If @cancellable is not %NULL, then the operation can be cancelled by
3743 * triggering the cancellable object from another thread. If the operation
3744 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3746 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3747 * When you are done with it, release it with g_file_attribute_info_list_unref()
3749 GFileAttributeInfoList *
3750 g_file_query_settable_attributes (GFile *file,
3751 GCancellable *cancellable,
3756 GFileAttributeInfoList *list;
3758 g_return_val_if_fail (G_IS_FILE (file), NULL);
3760 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3763 iface = G_FILE_GET_IFACE (file);
3765 if (iface->query_settable_attributes == NULL)
3766 return g_file_attribute_info_list_new ();
3769 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3773 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3775 list = g_file_attribute_info_list_new ();
3776 g_error_free (my_error);
3779 g_propagate_error (error, my_error);
3786 * g_file_query_writable_namespaces:
3787 * @file: input #GFile.
3788 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3789 * @error: a #GError, or %NULL
3791 * Obtain the list of attribute namespaces where new attributes
3792 * can be created by a user. An example of this is extended
3793 * attributes (in the "xattr" namespace).
3795 * If @cancellable is not %NULL, then the operation can be cancelled by
3796 * triggering the cancellable object from another thread. If the operation
3797 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3799 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3800 * When you are done with it, release it with g_file_attribute_info_list_unref()
3802 GFileAttributeInfoList *
3803 g_file_query_writable_namespaces (GFile *file,
3804 GCancellable *cancellable,
3809 GFileAttributeInfoList *list;
3811 g_return_val_if_fail (G_IS_FILE (file), NULL);
3813 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3816 iface = G_FILE_GET_IFACE (file);
3818 if (iface->query_writable_namespaces == NULL)
3819 return g_file_attribute_info_list_new ();
3822 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3826 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3828 list = g_file_attribute_info_list_new ();
3829 g_error_free (my_error);
3832 g_propagate_error (error, my_error);
3839 * g_file_set_attribute:
3840 * @file: input #GFile.
3841 * @attribute: a string containing the attribute's name.
3842 * @type: The type of the attribute
3843 * @value_p: (allow-none): a pointer to the value (or the pointer itself if the type is a pointer type)
3844 * @flags: a set of #GFileQueryInfoFlags.
3845 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3846 * @error: a #GError, or %NULL
3848 * Sets an attribute in the file with attribute name @attribute to @value.
3850 * Some attributes can be unset by setting @attribute to
3851 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
3853 * If @cancellable is not %NULL, then the operation can be cancelled by
3854 * triggering the cancellable object from another thread. If the operation
3855 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3857 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3860 g_file_set_attribute (GFile *file,
3861 const char *attribute,
3862 GFileAttributeType type,
3864 GFileQueryInfoFlags flags,
3865 GCancellable *cancellable,
3870 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3871 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3873 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3876 iface = G_FILE_GET_IFACE (file);
3878 if (iface->set_attribute == NULL)
3880 g_set_error_literal (error, G_IO_ERROR,
3881 G_IO_ERROR_NOT_SUPPORTED,
3882 _("Operation not supported"));
3886 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3890 * g_file_set_attributes_from_info:
3891 * @file: input #GFile.
3892 * @info: a #GFileInfo.
3893 * @flags: #GFileQueryInfoFlags
3894 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3895 * @error: a #GError, or %NULL
3897 * Tries to set all attributes in the #GFileInfo on the target values,
3898 * not stopping on the first error.
3900 * If there is any error during this operation then @error will be set to
3901 * the first error. Error on particular fields are flagged by setting
3902 * the "status" field in the attribute value to
3903 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3906 * If @cancellable is not %NULL, then the operation can be cancelled by
3907 * triggering the cancellable object from another thread. If the operation
3908 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3910 * Returns: %TRUE if there was any error, %FALSE otherwise.
3913 g_file_set_attributes_from_info (GFile *file,
3915 GFileQueryInfoFlags flags,
3916 GCancellable *cancellable,
3921 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3922 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3924 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3927 g_file_info_clear_status (info);
3929 iface = G_FILE_GET_IFACE (file);
3931 return (* iface->set_attributes_from_info) (file,
3940 g_file_real_set_attributes_from_info (GFile *file,
3942 GFileQueryInfoFlags flags,
3943 GCancellable *cancellable,
3949 GFileAttributeValue *value;
3953 attributes = g_file_info_list_attributes (info, NULL);
3955 for (i = 0; attributes[i] != NULL; i++)
3957 value = _g_file_info_get_attribute_value (info, attributes[i]);
3959 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3962 if (!g_file_set_attribute (file, attributes[i],
3963 value->type, _g_file_attribute_value_peek_as_pointer (value),
3964 flags, cancellable, error))
3966 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3968 /* Don't set error multiple times */
3972 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3975 g_strfreev (attributes);
3981 * g_file_set_attributes_async:
3982 * @file: input #GFile.
3983 * @info: a #GFileInfo.
3984 * @flags: a #GFileQueryInfoFlags.
3985 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3987 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3988 * @callback: (scope async): a #GAsyncReadyCallback.
3989 * @user_data: (closure): a #gpointer.
3991 * Asynchronously sets the attributes of @file with @info.
3993 * For more details, see g_file_set_attributes_from_info() which is
3994 * the synchronous version of this call.
3996 * When the operation is finished, @callback will be called. You can then call
3997 * g_file_set_attributes_finish() to get the result of the operation.
4000 g_file_set_attributes_async (GFile *file,
4002 GFileQueryInfoFlags flags,
4004 GCancellable *cancellable,
4005 GAsyncReadyCallback callback,
4010 g_return_if_fail (G_IS_FILE (file));
4011 g_return_if_fail (G_IS_FILE_INFO (info));
4013 iface = G_FILE_GET_IFACE (file);
4014 (* iface->set_attributes_async) (file,
4024 * g_file_set_attributes_finish:
4025 * @file: input #GFile.
4026 * @result: a #GAsyncResult.
4027 * @info: (out) (transfer full): a #GFileInfo.
4028 * @error: a #GError, or %NULL
4030 * Finishes setting an attribute started in g_file_set_attributes_async().
4032 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4035 g_file_set_attributes_finish (GFile *file,
4036 GAsyncResult *result,
4042 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4043 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4045 /* No standard handling of errors here, as we must set info even
4048 iface = G_FILE_GET_IFACE (file);
4049 return (* iface->set_attributes_finish) (file, result, info, error);
4053 * g_file_set_attribute_string:
4054 * @file: input #GFile.
4055 * @attribute: a string containing the attribute's name.
4056 * @value: a string containing the attribute's value.
4057 * @flags: #GFileQueryInfoFlags.
4058 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4059 * @error: a #GError, or %NULL
4061 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4062 * If @attribute is of a different type, this operation will fail.
4064 * If @cancellable is not %NULL, then the operation can be cancelled by
4065 * triggering the cancellable object from another thread. If the operation
4066 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4068 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4071 g_file_set_attribute_string (GFile *file,
4072 const char *attribute,
4074 GFileQueryInfoFlags flags,
4075 GCancellable *cancellable,
4078 return g_file_set_attribute (file, attribute,
4079 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4080 flags, cancellable, error);
4084 * g_file_set_attribute_byte_string:
4085 * @file: input #GFile.
4086 * @attribute: a string containing the attribute's name.
4087 * @value: a string containing the attribute's new value.
4088 * @flags: a #GFileQueryInfoFlags.
4089 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4090 * @error: a #GError, or %NULL
4092 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4093 * If @attribute is of a different type, this operation will fail,
4096 * If @cancellable is not %NULL, then the operation can be cancelled by
4097 * triggering the cancellable object from another thread. If the operation
4098 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4100 * Returns: %TRUE if the @attribute was successfully set to @value
4101 * in the @file, %FALSE otherwise.
4104 g_file_set_attribute_byte_string (GFile *file,
4105 const char *attribute,
4107 GFileQueryInfoFlags flags,
4108 GCancellable *cancellable,
4111 return g_file_set_attribute (file, attribute,
4112 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4113 flags, cancellable, error);
4117 * g_file_set_attribute_uint32:
4118 * @file: input #GFile.
4119 * @attribute: a string containing the attribute's name.
4120 * @value: a #guint32 containing the attribute's new value.
4121 * @flags: a #GFileQueryInfoFlags.
4122 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4123 * @error: a #GError, or %NULL
4125 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4126 * If @attribute is of a different type, this operation will fail.
4128 * If @cancellable is not %NULL, then the operation can be cancelled by
4129 * triggering the cancellable object from another thread. If the operation
4130 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4132 * Returns: %TRUE if the @attribute was successfully set to @value
4133 * in the @file, %FALSE otherwise.
4136 g_file_set_attribute_uint32 (GFile *file,
4137 const char *attribute,
4139 GFileQueryInfoFlags flags,
4140 GCancellable *cancellable,
4143 return g_file_set_attribute (file, attribute,
4144 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4145 flags, cancellable, error);
4149 * g_file_set_attribute_int32:
4150 * @file: input #GFile.
4151 * @attribute: a string containing the attribute's name.
4152 * @value: a #gint32 containing the attribute's new value.
4153 * @flags: a #GFileQueryInfoFlags.
4154 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4155 * @error: a #GError, or %NULL
4157 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4158 * If @attribute is of a different type, this operation will fail.
4160 * If @cancellable is not %NULL, then the operation can be cancelled by
4161 * triggering the cancellable object from another thread. If the operation
4162 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4164 * Returns: %TRUE if the @attribute was successfully set to @value
4165 * in the @file, %FALSE otherwise.
4168 g_file_set_attribute_int32 (GFile *file,
4169 const char *attribute,
4171 GFileQueryInfoFlags flags,
4172 GCancellable *cancellable,
4175 return g_file_set_attribute (file, attribute,
4176 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4177 flags, cancellable, error);
4181 * g_file_set_attribute_uint64:
4182 * @file: input #GFile.
4183 * @attribute: a string containing the attribute's name.
4184 * @value: a #guint64 containing the attribute's new value.
4185 * @flags: a #GFileQueryInfoFlags.
4186 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4187 * @error: a #GError, or %NULL
4189 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4190 * If @attribute is of a different type, this operation will fail.
4192 * If @cancellable is not %NULL, then the operation can be cancelled by
4193 * triggering the cancellable object from another thread. If the operation
4194 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4196 * Returns: %TRUE if the @attribute was successfully set to @value
4197 * in the @file, %FALSE otherwise.
4200 g_file_set_attribute_uint64 (GFile *file,
4201 const char *attribute,
4203 GFileQueryInfoFlags flags,
4204 GCancellable *cancellable,
4207 return g_file_set_attribute (file, attribute,
4208 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4209 flags, cancellable, error);
4213 * g_file_set_attribute_int64:
4214 * @file: input #GFile.
4215 * @attribute: a string containing the attribute's name.
4216 * @value: a #guint64 containing the attribute's new value.
4217 * @flags: a #GFileQueryInfoFlags.
4218 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4219 * @error: a #GError, or %NULL
4221 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4222 * If @attribute is of a different type, this operation will fail.
4224 * If @cancellable is not %NULL, then the operation can be cancelled by
4225 * triggering the cancellable object from another thread. If the operation
4226 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4228 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4231 g_file_set_attribute_int64 (GFile *file,
4232 const char *attribute,
4234 GFileQueryInfoFlags flags,
4235 GCancellable *cancellable,
4238 return g_file_set_attribute (file, attribute,
4239 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4240 flags, cancellable, error);
4244 * g_file_mount_mountable:
4245 * @file: input #GFile.
4246 * @flags: flags affecting the operation
4247 * @mount_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
4248 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4249 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4250 * @user_data: (closure): the data to pass to callback function
4252 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4253 * Using @mount_operation, you can request callbacks when, for instance,
4254 * passwords are needed during authentication.
4256 * If @cancellable is not %NULL, then the operation can be cancelled by
4257 * triggering the cancellable object from another thread. If the operation
4258 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4260 * When the operation is finished, @callback will be called. You can then call
4261 * g_file_mount_mountable_finish() to get the result of the operation.
4264 g_file_mount_mountable (GFile *file,
4265 GMountMountFlags flags,
4266 GMountOperation *mount_operation,
4267 GCancellable *cancellable,
4268 GAsyncReadyCallback callback,
4273 g_return_if_fail (G_IS_FILE (file));
4275 iface = G_FILE_GET_IFACE (file);
4277 if (iface->mount_mountable == NULL)
4279 g_simple_async_report_error_in_idle (G_OBJECT (file),
4283 G_IO_ERROR_NOT_SUPPORTED,
4284 _("Operation not supported"));
4288 (* iface->mount_mountable) (file,
4297 * g_file_mount_mountable_finish:
4298 * @file: input #GFile.
4299 * @result: a #GAsyncResult.
4300 * @error: a #GError, or %NULL
4302 * Finishes a mount operation. See g_file_mount_mountable() for details.
4304 * Finish an asynchronous mount operation that was started
4305 * with g_file_mount_mountable().
4307 * Returns: (transfer full): a #GFile or %NULL on error.
4308 * Free the returned object with g_object_unref().
4311 g_file_mount_mountable_finish (GFile *file,
4312 GAsyncResult *result,
4317 g_return_val_if_fail (G_IS_FILE (file), NULL);
4318 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4320 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4322 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4323 if (g_simple_async_result_propagate_error (simple, error))
4327 iface = G_FILE_GET_IFACE (file);
4328 return (* iface->mount_mountable_finish) (file, result, error);
4332 * g_file_unmount_mountable:
4333 * @file: input #GFile.
4334 * @flags: flags affecting the operation
4335 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4336 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4337 * @user_data: (closure): the data to pass to callback function
4339 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4341 * If @cancellable is not %NULL, then the operation can be cancelled by
4342 * triggering the cancellable object from another thread. If the operation
4343 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4345 * When the operation is finished, @callback will be called. You can then call
4346 * g_file_unmount_mountable_finish() to get the result of the operation.
4348 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4351 g_file_unmount_mountable (GFile *file,
4352 GMountUnmountFlags flags,
4353 GCancellable *cancellable,
4354 GAsyncReadyCallback callback,
4359 g_return_if_fail (G_IS_FILE (file));
4361 iface = G_FILE_GET_IFACE (file);
4363 if (iface->unmount_mountable == NULL)
4365 g_simple_async_report_error_in_idle (G_OBJECT (file),
4369 G_IO_ERROR_NOT_SUPPORTED,
4370 _("Operation not supported"));
4374 (* iface->unmount_mountable) (file,
4382 * g_file_unmount_mountable_finish:
4383 * @file: input #GFile.
4384 * @result: a #GAsyncResult.
4385 * @error: a #GError, or %NULL
4387 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4389 * Finish an asynchronous unmount operation that was started
4390 * with g_file_unmount_mountable().
4392 * Returns: %TRUE if the operation finished successfully. %FALSE
4395 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish() instead.
4398 g_file_unmount_mountable_finish (GFile *file,
4399 GAsyncResult *result,
4404 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4405 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4407 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4409 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4410 if (g_simple_async_result_propagate_error (simple, error))
4414 iface = G_FILE_GET_IFACE (file);
4415 return (* iface->unmount_mountable_finish) (file, result, error);
4419 * g_file_unmount_mountable_with_operation:
4420 * @file: input #GFile.
4421 * @flags: flags affecting the operation
4422 * @mount_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
4423 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4424 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4425 * @user_data: (closure): the data to pass to callback function
4427 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4429 * If @cancellable is not %NULL, then the operation can be cancelled by
4430 * triggering the cancellable object from another thread. If the operation
4431 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4433 * When the operation is finished, @callback will be called. You can then call
4434 * g_file_unmount_mountable_finish() to get the result of the operation.
4439 g_file_unmount_mountable_with_operation (GFile *file,
4440 GMountUnmountFlags flags,
4441 GMountOperation *mount_operation,
4442 GCancellable *cancellable,
4443 GAsyncReadyCallback callback,
4448 g_return_if_fail (G_IS_FILE (file));
4450 iface = G_FILE_GET_IFACE (file);
4452 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4454 g_simple_async_report_error_in_idle (G_OBJECT (file),
4458 G_IO_ERROR_NOT_SUPPORTED,
4459 _("Operation not supported"));
4463 if (iface->unmount_mountable_with_operation != NULL)
4464 (* iface->unmount_mountable_with_operation) (file,
4471 (* iface->unmount_mountable) (file,
4479 * g_file_unmount_mountable_with_operation_finish:
4480 * @file: input #GFile.
4481 * @result: a #GAsyncResult.
4482 * @error: a #GError, or %NULL
4484 * Finishes an unmount operation, see g_file_unmount_mountable_with_operation() for details.
4486 * Finish an asynchronous unmount operation that was started
4487 * with g_file_unmount_mountable_with_operation().
4489 * Returns: %TRUE if the operation finished successfully. %FALSE
4495 g_file_unmount_mountable_with_operation_finish (GFile *file,
4496 GAsyncResult *result,
4501 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4502 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4504 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4506 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4507 if (g_simple_async_result_propagate_error (simple, error))
4511 iface = G_FILE_GET_IFACE (file);
4512 if (iface->unmount_mountable_with_operation_finish != NULL)
4513 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4515 return (* iface->unmount_mountable_finish) (file, result, error);
4519 * g_file_eject_mountable:
4520 * @file: input #GFile.
4521 * @flags: flags affecting the operation
4522 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4523 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4524 * @user_data: (closure): the data to pass to callback function
4526 * Starts an asynchronous eject on a mountable.
4527 * When this operation has completed, @callback will be called with
4528 * @user_user data, and the operation can be finalized with
4529 * g_file_eject_mountable_finish().
4531 * If @cancellable is not %NULL, then the operation can be cancelled by
4532 * triggering the cancellable object from another thread. If the operation
4533 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4535 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4538 g_file_eject_mountable (GFile *file,
4539 GMountUnmountFlags flags,
4540 GCancellable *cancellable,
4541 GAsyncReadyCallback callback,
4546 g_return_if_fail (G_IS_FILE (file));
4548 iface = G_FILE_GET_IFACE (file);
4550 if (iface->eject_mountable == NULL)
4552 g_simple_async_report_error_in_idle (G_OBJECT (file),
4556 G_IO_ERROR_NOT_SUPPORTED,
4557 _("Operation not supported"));
4561 (* iface->eject_mountable) (file,
4569 * g_file_eject_mountable_finish:
4570 * @file: input #GFile.
4571 * @result: a #GAsyncResult.
4572 * @error: a #GError, or %NULL
4574 * Finishes an asynchronous eject operation started by
4575 * g_file_eject_mountable().
4577 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4580 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish() instead.
4583 g_file_eject_mountable_finish (GFile *file,
4584 GAsyncResult *result,
4589 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4590 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4592 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4594 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4595 if (g_simple_async_result_propagate_error (simple, error))
4599 iface = G_FILE_GET_IFACE (file);
4600 return (* iface->eject_mountable_finish) (file, result, error);
4604 * g_file_eject_mountable_with_operation:
4605 * @file: input #GFile.
4606 * @flags: flags affecting the operation
4607 * @mount_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
4608 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4609 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4610 * @user_data: (closure): the data to pass to callback function
4612 * Starts an asynchronous eject on a mountable.
4613 * When this operation has completed, @callback will be called with
4614 * @user_user data, and the operation can be finalized with
4615 * g_file_eject_mountable_with_operation_finish().
4617 * If @cancellable is not %NULL, then the operation can be cancelled by
4618 * triggering the cancellable object from another thread. If the operation
4619 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4624 g_file_eject_mountable_with_operation (GFile *file,
4625 GMountUnmountFlags flags,
4626 GMountOperation *mount_operation,
4627 GCancellable *cancellable,
4628 GAsyncReadyCallback callback,
4633 g_return_if_fail (G_IS_FILE (file));
4635 iface = G_FILE_GET_IFACE (file);
4637 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4639 g_simple_async_report_error_in_idle (G_OBJECT (file),
4643 G_IO_ERROR_NOT_SUPPORTED,
4644 _("Operation not supported"));
4648 if (iface->eject_mountable_with_operation != NULL)
4649 (* iface->eject_mountable_with_operation) (file,
4656 (* iface->eject_mountable) (file,
4664 * g_file_eject_mountable_with_operation_finish:
4665 * @file: input #GFile.
4666 * @result: a #GAsyncResult.
4667 * @error: a #GError, or %NULL
4669 * Finishes an asynchronous eject operation started by
4670 * g_file_eject_mountable_with_operation().
4672 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4678 g_file_eject_mountable_with_operation_finish (GFile *file,
4679 GAsyncResult *result,
4684 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4685 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4687 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4689 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4690 if (g_simple_async_result_propagate_error (simple, error))
4694 iface = G_FILE_GET_IFACE (file);
4695 if (iface->eject_mountable_with_operation_finish != NULL)
4696 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4698 return (* iface->eject_mountable_finish) (file, result, error);
4702 * g_file_monitor_directory:
4703 * @file: input #GFile.
4704 * @flags: a set of #GFileMonitorFlags.
4705 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4706 * @error: a #GError, or %NULL.
4708 * Obtains a directory monitor for the given file.
4709 * This may fail if directory monitoring is not supported.
4711 * If @cancellable is not %NULL, then the operation can be cancelled by
4712 * triggering the cancellable object from another thread. If the operation
4713 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4715 * Virtual: monitor_dir
4716 * Returns: (transfer full): a #GFileMonitor for the given @file, or %NULL on error.
4717 * Free the returned object with g_object_unref().
4720 g_file_monitor_directory (GFile *file,
4721 GFileMonitorFlags flags,
4722 GCancellable *cancellable,
4727 g_return_val_if_fail (G_IS_FILE (file), NULL);
4729 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4732 iface = G_FILE_GET_IFACE (file);
4734 if (iface->monitor_dir == NULL)
4736 g_set_error_literal (error, G_IO_ERROR,
4737 G_IO_ERROR_NOT_SUPPORTED,
4738 _("Operation not supported"));
4742 return (* iface->monitor_dir) (file, flags, cancellable, error);
4746 * g_file_monitor_file:
4747 * @file: input #GFile.
4748 * @flags: a set of #GFileMonitorFlags.
4749 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4750 * @error: a #GError, or %NULL.
4752 * Obtains a file monitor for the given file. If no file notification
4753 * mechanism exists, then regular polling of the file is used.
4755 * If @cancellable is not %NULL, then the operation can be cancelled by
4756 * triggering the cancellable object from another thread. If the operation
4757 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4759 * Returns: (transfer full): a #GFileMonitor for the given @file, or %NULL on error.
4760 * Free the returned object with g_object_unref().
4763 g_file_monitor_file (GFile *file,
4764 GFileMonitorFlags flags,
4765 GCancellable *cancellable,
4769 GFileMonitor *monitor;
4771 g_return_val_if_fail (G_IS_FILE (file), NULL);
4773 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4776 iface = G_FILE_GET_IFACE (file);
4780 if (iface->monitor_file)
4781 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4783 /* Fallback to polling */
4784 if (monitor == NULL)
4785 monitor = _g_poll_file_monitor_new (file);
4792 * @file: input #GFile
4793 * @flags: a set of #GFileMonitorFlags
4794 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
4795 * @error: a #GError, or %NULL
4797 * Obtains a file or directory monitor for the given file, depending
4798 * on the type of the file.
4800 * If @cancellable is not %NULL, then the operation can be cancelled by
4801 * triggering the cancellable object from another thread. If the operation
4802 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4804 * Returns: (transfer full): a #GFileMonitor for the given @file, or %NULL on error.
4805 * Free the returned object with g_object_unref().
4810 g_file_monitor (GFile *file,
4811 GFileMonitorFlags flags,
4812 GCancellable *cancellable,
4815 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4816 return g_file_monitor_directory (file, flags, cancellable, error);
4818 return g_file_monitor_file (file, flags, cancellable, error);
4821 /********************************************
4822 * Default implementation of async ops *
4823 ********************************************/
4827 GFileQueryInfoFlags flags;
4829 } QueryInfoAsyncData;
4832 query_info_data_free (QueryInfoAsyncData *data)
4835 g_object_unref (data->info);
4836 g_free (data->attributes);
4841 query_info_async_thread (GSimpleAsyncResult *res,
4843 GCancellable *cancellable)
4845 GError *error = NULL;
4846 QueryInfoAsyncData *data;
4849 data = g_simple_async_result_get_op_res_gpointer (res);
4851 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4854 g_simple_async_result_take_error (res, error);
4860 g_file_real_query_info_async (GFile *file,
4861 const char *attributes,
4862 GFileQueryInfoFlags flags,
4864 GCancellable *cancellable,
4865 GAsyncReadyCallback callback,
4868 GSimpleAsyncResult *res;
4869 QueryInfoAsyncData *data;
4871 data = g_new0 (QueryInfoAsyncData, 1);
4872 data->attributes = g_strdup (attributes);
4873 data->flags = flags;
4875 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
4876 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
4878 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
4879 g_object_unref (res);
4883 g_file_real_query_info_finish (GFile *file,
4887 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4888 QueryInfoAsyncData *data;
4890 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4892 data = g_simple_async_result_get_op_res_gpointer (simple);
4894 return g_object_ref (data->info);
4902 } QueryFilesystemInfoAsyncData;
4905 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4908 g_object_unref (data->info);
4909 g_free (data->attributes);
4914 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4916 GCancellable *cancellable)
4918 GError *error = NULL;
4919 QueryFilesystemInfoAsyncData *data;
4922 data = g_simple_async_result_get_op_res_gpointer (res);
4924 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4927 g_simple_async_result_take_error (res, error);
4933 g_file_real_query_filesystem_info_async (GFile *file,
4934 const char *attributes,
4936 GCancellable *cancellable,
4937 GAsyncReadyCallback callback,
4940 GSimpleAsyncResult *res;
4941 QueryFilesystemInfoAsyncData *data;
4943 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4944 data->attributes = g_strdup (attributes);
4946 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4947 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4949 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4950 g_object_unref (res);
4954 g_file_real_query_filesystem_info_finish (GFile *file,
4958 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4959 QueryFilesystemInfoAsyncData *data;
4961 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4963 data = g_simple_async_result_get_op_res_gpointer (simple);
4965 return g_object_ref (data->info);
4972 GFileQueryInfoFlags flags;
4973 GFileEnumerator *enumerator;
4974 } EnumerateChildrenAsyncData;
4977 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4979 if (data->enumerator)
4980 g_object_unref (data->enumerator);
4981 g_free (data->attributes);
4986 enumerate_children_async_thread (GSimpleAsyncResult *res,
4988 GCancellable *cancellable)
4990 GError *error = NULL;
4991 EnumerateChildrenAsyncData *data;
4992 GFileEnumerator *enumerator;
4994 data = g_simple_async_result_get_op_res_gpointer (res);
4996 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4998 if (enumerator == NULL)
4999 g_simple_async_result_take_error (res, error);
5001 data->enumerator = enumerator;
5005 g_file_real_enumerate_children_async (GFile *file,
5006 const char *attributes,
5007 GFileQueryInfoFlags flags,
5009 GCancellable *cancellable,
5010 GAsyncReadyCallback callback,
5013 GSimpleAsyncResult *res;
5014 EnumerateChildrenAsyncData *data;
5016 data = g_new0 (EnumerateChildrenAsyncData, 1);
5017 data->attributes = g_strdup (attributes);
5018 data->flags = flags;
5020 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
5021 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
5023 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
5024 g_object_unref (res);
5027 static GFileEnumerator *
5028 g_file_real_enumerate_children_finish (GFile *file,
5032 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5033 EnumerateChildrenAsyncData *data;
5035 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
5037 data = g_simple_async_result_get_op_res_gpointer (simple);
5038 if (data->enumerator)
5039 return g_object_ref (data->enumerator);
5045 open_read_async_thread (GSimpleAsyncResult *res,
5047 GCancellable *cancellable)
5050 GFileInputStream *stream;
5051 GError *error = NULL;
5053 iface = G_FILE_GET_IFACE (object);
5055 if (iface->read_fn == NULL)
5057 g_set_error_literal (&error, G_IO_ERROR,
5058 G_IO_ERROR_NOT_SUPPORTED,
5059 _("Operation not supported"));
5061 g_simple_async_result_take_error (res, error);
5066 stream = iface->read_fn (G_FILE (object), cancellable, &error);
5069 g_simple_async_result_take_error (res, error);
5071 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5075 g_file_real_read_async (GFile *file,
5077 GCancellable *cancellable,
5078 GAsyncReadyCallback callback,
5081 GSimpleAsyncResult *res;
5083 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
5085 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
5086 g_object_unref (res);
5089 static GFileInputStream *
5090 g_file_real_read_finish (GFile *file,
5094 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5097 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
5099 op = g_simple_async_result_get_op_res_gpointer (simple);
5101 return g_object_ref (op);
5107 append_to_async_thread (GSimpleAsyncResult *res,
5109 GCancellable *cancellable)
5112 GFileCreateFlags *data;
5113 GFileOutputStream *stream;
5114 GError *error = NULL;
5116 iface = G_FILE_GET_IFACE (object);
5118 data = g_simple_async_result_get_op_res_gpointer (res);
5120 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
5123 g_simple_async_result_take_error (res, error);
5125 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5129 g_file_real_append_to_async (GFile *file,
5130 GFileCreateFlags flags,
5132 GCancellable *cancellable,
5133 GAsyncReadyCallback callback,
5136 GFileCreateFlags *data;
5137 GSimpleAsyncResult *res;
5139 data = g_new0 (GFileCreateFlags, 1);
5142 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
5143 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5145 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
5146 g_object_unref (res);
5149 static GFileOutputStream *
5150 g_file_real_append_to_finish (GFile *file,
5154 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5157 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
5159 op = g_simple_async_result_get_op_res_gpointer (simple);
5161 return g_object_ref (op);
5167 create_async_thread (GSimpleAsyncResult *res,
5169 GCancellable *cancellable)
5172 GFileCreateFlags *data;
5173 GFileOutputStream *stream;
5174 GError *error = NULL;
5176 iface = G_FILE_GET_IFACE (object);
5178 data = g_simple_async_result_get_op_res_gpointer (res);
5180 stream = iface->create (G_FILE (object), *data, cancellable, &error);
5183 g_simple_async_result_take_error (res, error);
5185 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5189 g_file_real_create_async (GFile *file,
5190 GFileCreateFlags flags,
5192 GCancellable *cancellable,
5193 GAsyncReadyCallback callback,
5196 GFileCreateFlags *data;
5197 GSimpleAsyncResult *res;
5199 data = g_new0 (GFileCreateFlags, 1);
5202 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
5203 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5205 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
5206 g_object_unref (res);
5209 static GFileOutputStream *
5210 g_file_real_create_finish (GFile *file,
5214 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5217 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
5219 op = g_simple_async_result_get_op_res_gpointer (simple);
5221 return g_object_ref (op);
5227 GFileOutputStream *stream;
5229 gboolean make_backup;
5230 GFileCreateFlags flags;
5234 replace_async_data_free (ReplaceAsyncData *data)
5237 g_object_unref (data->stream);
5238 g_free (data->etag);
5243 replace_async_thread (GSimpleAsyncResult *res,
5245 GCancellable *cancellable)
5248 GFileOutputStream *stream;
5249 GError *error = NULL;
5250 ReplaceAsyncData *data;
5252 iface = G_FILE_GET_IFACE (object);
5254 data = g_simple_async_result_get_op_res_gpointer (res);
5256 stream = iface->replace (G_FILE (object),
5264 g_simple_async_result_take_error (res, error);
5266 data->stream = stream;
5270 g_file_real_replace_async (GFile *file,
5272 gboolean make_backup,
5273 GFileCreateFlags flags,
5275 GCancellable *cancellable,
5276 GAsyncReadyCallback callback,
5279 GSimpleAsyncResult *res;
5280 ReplaceAsyncData *data;
5282 data = g_new0 (ReplaceAsyncData, 1);
5283 data->etag = g_strdup (etag);
5284 data->make_backup = make_backup;
5285 data->flags = flags;
5287 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
5288 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
5290 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
5291 g_object_unref (res);
5294 static GFileOutputStream *
5295 g_file_real_replace_finish (GFile *file,
5299 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5300 ReplaceAsyncData *data;
5302 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
5304 data = g_simple_async_result_get_op_res_gpointer (simple);
5306 return g_object_ref (data->stream);
5312 open_readwrite_async_thread (GSimpleAsyncResult *res,
5314 GCancellable *cancellable)
5317 GFileIOStream *stream;
5318 GError *error = NULL;
5320 iface = G_FILE_GET_IFACE (object);
5322 if (iface->open_readwrite == NULL)
5324 g_set_error_literal (&error, G_IO_ERROR,
5325 G_IO_ERROR_NOT_SUPPORTED,
5326 _("Operation not supported"));
5328 g_simple_async_result_take_error (res, error);
5333 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5336 g_simple_async_result_take_error (res, error);
5338 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5342 g_file_real_open_readwrite_async (GFile *file,
5344 GCancellable *cancellable,
5345 GAsyncReadyCallback callback,
5348 GSimpleAsyncResult *res;
5350 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_open_readwrite_async);
5352 g_simple_async_result_run_in_thread (res, open_readwrite_async_thread, io_priority, cancellable);
5353 g_object_unref (res);
5356 static GFileIOStream *
5357 g_file_real_open_readwrite_finish (GFile *file,
5361 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5364 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_open_readwrite_async);
5366 op = g_simple_async_result_get_op_res_gpointer (simple);
5368 return g_object_ref (op);
5374 create_readwrite_async_thread (GSimpleAsyncResult *res,
5376 GCancellable *cancellable)
5379 GFileCreateFlags *data;
5380 GFileIOStream *stream;
5381 GError *error = NULL;
5383 iface = G_FILE_GET_IFACE (object);
5385 data = g_simple_async_result_get_op_res_gpointer (res);
5387 if (iface->create_readwrite == NULL)
5389 g_set_error_literal (&error, G_IO_ERROR,
5390 G_IO_ERROR_NOT_SUPPORTED,
5391 _("Operation not supported"));
5393 g_simple_async_result_take_error (res, error);
5398 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5401 g_simple_async_result_take_error (res, error);
5403 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5407 g_file_real_create_readwrite_async (GFile *file,
5408 GFileCreateFlags flags,
5410 GCancellable *cancellable,
5411 GAsyncReadyCallback callback,
5414 GFileCreateFlags *data;
5415 GSimpleAsyncResult *res;
5417 data = g_new0 (GFileCreateFlags, 1);
5420 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_readwrite_async);
5421 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5423 g_simple_async_result_run_in_thread (res, create_readwrite_async_thread, io_priority, cancellable);
5424 g_object_unref (res);
5427 static GFileIOStream *
5428 g_file_real_create_readwrite_finish (GFile *file,
5432 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5435 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_readwrite_async);
5437 op = g_simple_async_result_get_op_res_gpointer (simple);
5439 return g_object_ref (op);
5445 GFileIOStream *stream;
5447 gboolean make_backup;
5448 GFileCreateFlags flags;
5449 } ReplaceRWAsyncData;
5452 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5455 g_object_unref (data->stream);
5456 g_free (data->etag);
5461 replace_readwrite_async_thread (GSimpleAsyncResult *res,
5463 GCancellable *cancellable)
5466 GFileIOStream *stream;
5467 GError *error = NULL;
5468 ReplaceRWAsyncData *data;
5470 iface = G_FILE_GET_IFACE (object);
5472 data = g_simple_async_result_get_op_res_gpointer (res);
5474 stream = iface->replace_readwrite (G_FILE (object),
5482 g_simple_async_result_take_error (res, error);
5484 data->stream = stream;
5488 g_file_real_replace_readwrite_async (GFile *file,
5490 gboolean make_backup,
5491 GFileCreateFlags flags,
5493 GCancellable *cancellable,
5494 GAsyncReadyCallback callback,
5497 GSimpleAsyncResult *res;
5498 ReplaceRWAsyncData *data;
5500 data = g_new0 (ReplaceRWAsyncData, 1);
5501 data->etag = g_strdup (etag);
5502 data->make_backup = make_backup;
5503 data->flags = flags;
5505 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_readwrite_async);
5506 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_rw_async_data_free);
5508 g_simple_async_result_run_in_thread (res, replace_readwrite_async_thread, io_priority, cancellable);
5509 g_object_unref (res);
5512 static GFileIOStream *
5513 g_file_real_replace_readwrite_finish (GFile *file,
5517 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5518 ReplaceRWAsyncData *data;
5520 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_readwrite_async);
5522 data = g_simple_async_result_get_op_res_gpointer (simple);
5524 return g_object_ref (data->stream);
5532 } SetDisplayNameAsyncData;
5535 set_display_name_data_free (SetDisplayNameAsyncData *data)
5537 g_free (data->name);
5539 g_object_unref (data->file);
5544 set_display_name_async_thread (GSimpleAsyncResult *res,
5546 GCancellable *cancellable)
5548 GError *error = NULL;
5549 SetDisplayNameAsyncData *data;
5552 data = g_simple_async_result_get_op_res_gpointer (res);
5554 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
5557 g_simple_async_result_take_error (res, error);
5563 g_file_real_set_display_name_async (GFile *file,
5564 const char *display_name,
5566 GCancellable *cancellable,
5567 GAsyncReadyCallback callback,
5570 GSimpleAsyncResult *res;
5571 SetDisplayNameAsyncData *data;
5573 data = g_new0 (SetDisplayNameAsyncData, 1);
5574 data->name = g_strdup (display_name);
5576 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
5577 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
5579 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
5580 g_object_unref (res);
5584 g_file_real_set_display_name_finish (GFile *file,
5588 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5589 SetDisplayNameAsyncData *data;
5591 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
5593 data = g_simple_async_result_get_op_res_gpointer (simple);
5595 return g_object_ref (data->file);
5601 GFileQueryInfoFlags flags;
5608 set_info_data_free (SetInfoAsyncData *data)
5611 g_object_unref (data->info);
5613 g_error_free (data->error);
5618 set_info_async_thread (GSimpleAsyncResult *res,
5620 GCancellable *cancellable)
5622 SetInfoAsyncData *data;
5624 data = g_simple_async_result_get_op_res_gpointer (res);
5627 data->res = g_file_set_attributes_from_info (G_FILE (object),
5635 g_file_real_set_attributes_async (GFile *file,
5637 GFileQueryInfoFlags flags,
5639 GCancellable *cancellable,
5640 GAsyncReadyCallback callback,
5643 GSimpleAsyncResult *res;
5644 SetInfoAsyncData *data;
5646 data = g_new0 (SetInfoAsyncData, 1);
5647 data->info = g_file_info_dup (info);
5648 data->flags = flags;
5650 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
5651 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
5653 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
5654 g_object_unref (res);
5658 g_file_real_set_attributes_finish (GFile *file,
5663 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5664 SetInfoAsyncData *data;
5666 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
5668 data = g_simple_async_result_get_op_res_gpointer (simple);
5671 *info = g_object_ref (data->info);
5673 if (error != NULL && data->error)
5674 *error = g_error_copy (data->error);
5680 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
5682 GCancellable *cancellable)
5684 GError *error = NULL;
5687 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5690 g_simple_async_result_take_error (res, error);
5692 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
5696 g_file_real_find_enclosing_mount_async (GFile *file,
5698 GCancellable *cancellable,
5699 GAsyncReadyCallback callback,
5702 GSimpleAsyncResult *res;
5704 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
5706 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
5707 g_object_unref (res);
5711 g_file_real_find_enclosing_mount_finish (GFile *file,
5715 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5718 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
5720 mount = g_simple_async_result_get_op_res_gpointer (simple);
5721 return g_object_ref (mount);
5728 GFileCopyFlags flags;
5729 GFileProgressCallback progress_cb;
5730 gpointer progress_cb_data;
5731 GIOSchedulerJob *job;
5735 copy_async_data_free (CopyAsyncData *data)
5737 g_object_unref (data->source);
5738 g_object_unref (data->destination);
5743 CopyAsyncData *data;
5744 goffset current_num_bytes;
5745 goffset total_num_bytes;
5749 copy_async_progress_in_main (gpointer user_data)
5751 ProgressData *progress = user_data;
5752 CopyAsyncData *data = progress->data;
5754 data->progress_cb (progress->current_num_bytes,
5755 progress->total_num_bytes,
5756 data->progress_cb_data);
5762 mainloop_barrier (gpointer user_data)
5764 /* Does nothing, but ensures all queued idles before
5771 copy_async_progress_callback (goffset current_num_bytes,
5772 goffset total_num_bytes,
5775 CopyAsyncData *data = user_data;
5776 ProgressData *progress;
5778 progress = g_new (ProgressData, 1);
5779 progress->data = data;
5780 progress->current_num_bytes = current_num_bytes;
5781 progress->total_num_bytes = total_num_bytes;
5783 g_io_scheduler_job_send_to_mainloop_async (data->job,
5784 copy_async_progress_in_main,
5790 copy_async_thread (GIOSchedulerJob *job,
5791 GCancellable *cancellable,
5794 GSimpleAsyncResult *res;
5795 CopyAsyncData *data;
5800 data = g_simple_async_result_get_op_res_gpointer (res);
5804 result = g_file_copy (data->source,
5808 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5812 /* Ensure all progress callbacks are done running in main thread */
5813 if (data->progress_cb != NULL)
5814 g_io_scheduler_job_send_to_mainloop (job,
5819 g_simple_async_result_take_error (res, error);
5821 g_simple_async_result_complete_in_idle (res);
5827 g_file_real_copy_async (GFile *source,
5829 GFileCopyFlags flags,
5831 GCancellable *cancellable,
5832 GFileProgressCallback progress_callback,
5833 gpointer progress_callback_data,
5834 GAsyncReadyCallback callback,
5837 GSimpleAsyncResult *res;
5838 CopyAsyncData *data;
5840 data = g_new0 (CopyAsyncData, 1);
5841 data->source = g_object_ref (source);
5842 data->destination = g_object_ref (destination);
5843 data->flags = flags;
5844 data->progress_cb = progress_callback;
5845 data->progress_cb_data = progress_callback_data;
5847 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
5848 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
5850 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
5854 g_file_real_copy_finish (GFile *file,
5858 /* Error handled in g_file_copy_finish() */
5863 /********************************************
5864 * Default VFS operations *
5865 ********************************************/
5868 * g_file_new_for_path:
5869 * @path: a string containing a relative or absolute path. The string
5870 * must be encoded in the glib filename encoding.
5872 * Constructs a #GFile for a given path. This operation never
5873 * fails, but the returned object might not support any I/O
5874 * operation if @path is malformed.
5876 * Returns: (transfer full): a new #GFile for the given @path.
5877 * Free the returned object with g_object_unref().
5880 g_file_new_for_path (const char *path)
5882 g_return_val_if_fail (path != NULL, NULL);
5884 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5888 * g_file_new_for_uri:
5889 * @uri: a UTF8 string containing a URI.
5891 * Constructs a #GFile for a given URI. This operation never
5892 * fails, but the returned object might not support any I/O
5893 * operation if @uri is malformed or if the uri type is
5896 * Returns: (transfer full): a new #GFile for the given @uri.
5897 * Free the returned object with g_object_unref().
5900 g_file_new_for_uri (const char *uri)
5902 g_return_val_if_fail (uri != NULL, NULL);
5904 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
5909 * @tmpl: (type filename) (allow-none): Template for the file
5910 * name, as in g_file_open_tmp(), or %NULL for a default template.
5911 * @iostream: (out): on return, a #GFileIOStream for the created file.
5912 * @error: a #GError, or %NULL
5914 * Opens a file in the preferred directory for temporary files (as
5915 * returned by g_get_tmp_dir()) and returns a #GFile and
5916 * #GFileIOStream pointing to it.
5918 * @tmpl should be a string in the GLib file name encoding
5919 * containing a sequence of six 'X' characters, and containing no
5920 * directory components. If it is %NULL, a default template is used.
5922 * Unlike the other #GFile constructors, this will return %NULL if
5923 * a temporary file could not be created.
5925 * Returns: (transfer full): a new #GFile.
5926 * Free the returned object with g_object_unref().
5931 g_file_new_tmp (const char *tmpl,
5932 GFileIOStream **iostream,
5938 GFileOutputStream *output;
5940 g_return_val_if_fail (iostream != NULL, NULL);
5942 fd = g_file_open_tmp (tmpl, &path, error);
5946 file = g_file_new_for_path (path);
5948 output = _g_local_file_output_stream_new (fd);
5949 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
5951 g_object_unref (output);
5958 * g_file_parse_name:
5959 * @parse_name: a file name or path to be parsed.
5961 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
5962 * This operation never fails, but the returned object might not support any I/O
5963 * operation if the @parse_name cannot be parsed.
5965 * Returns: (transfer full): a new #GFile.
5968 g_file_parse_name (const char *parse_name)
5970 g_return_val_if_fail (parse_name != NULL, NULL);
5972 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
5976 is_valid_scheme_character (char c)
5978 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
5981 /* Following RFC 2396, valid schemes are built like:
5982 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
5985 has_valid_scheme (const char *uri)
5991 if (!g_ascii_isalpha (*p))
5996 } while (is_valid_scheme_character (*p));
6002 * g_file_new_for_commandline_arg:
6003 * @arg: a command line string.
6005 * Creates a #GFile with the given argument from the command line. The value of
6006 * @arg can be either a URI, an absolute path or a relative path resolved
6007 * relative to the current working directory.
6008 * This operation never fails, but the returned object might not support any
6009 * I/O operation if @arg points to a malformed path.
6011 * Returns: (transfer full): a new #GFile.
6012 * Free the returned object with g_object_unref().
6015 g_file_new_for_commandline_arg (const char *arg)
6021 g_return_val_if_fail (arg != NULL, NULL);
6023 if (g_path_is_absolute (arg))
6024 return g_file_new_for_path (arg);
6026 if (has_valid_scheme (arg))
6027 return g_file_new_for_uri (arg);
6029 current_dir = g_get_current_dir ();
6030 filename = g_build_filename (current_dir, arg, NULL);
6031 g_free (current_dir);
6033 file = g_file_new_for_path (filename);
6040 * g_file_mount_enclosing_volume:
6041 * @location: input #GFile.
6042 * @flags: flags affecting the operation
6043 * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid user interaction.
6044 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
6045 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
6046 * @user_data: the data to pass to callback function
6048 * Starts a @mount_operation, mounting the volume that contains the file @location.
6050 * When this operation has completed, @callback will be called with
6051 * @user_user data, and the operation can be finalized with
6052 * g_file_mount_enclosing_volume_finish().
6054 * If @cancellable is not %NULL, then the operation can be cancelled by
6055 * triggering the cancellable object from another thread. If the operation
6056 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6059 g_file_mount_enclosing_volume (GFile *location,
6060 GMountMountFlags flags,
6061 GMountOperation *mount_operation,
6062 GCancellable *cancellable,
6063 GAsyncReadyCallback callback,
6068 g_return_if_fail (G_IS_FILE (location));
6070 iface = G_FILE_GET_IFACE (location);
6072 if (iface->mount_enclosing_volume == NULL)
6074 g_simple_async_report_error_in_idle (G_OBJECT (location),
6075 callback, user_data,
6076 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6077 _("volume doesn't implement mount"));
6082 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6087 * g_file_mount_enclosing_volume_finish:
6088 * @location: input #GFile.
6089 * @result: a #GAsyncResult.
6090 * @error: a #GError, or %NULL
6092 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6094 * Returns: %TRUE if successful. If an error
6095 * has occurred, this function will return %FALSE and set @error
6096 * appropriately if present.
6099 g_file_mount_enclosing_volume_finish (GFile *location,
6100 GAsyncResult *result,
6105 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6106 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6108 if (G_IS_SIMPLE_ASYNC_RESULT (result))
6110 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
6111 if (g_simple_async_result_propagate_error (simple, error))
6115 iface = G_FILE_GET_IFACE (location);
6117 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6120 /********************************************
6121 * Utility functions *
6122 ********************************************/
6125 * g_file_query_default_handler:
6126 * @file: a #GFile to open.
6127 * @cancellable: optional #GCancellable object, %NULL to ignore.
6128 * @error: a #GError, or %NULL
6130 * Returns the #GAppInfo that is registered as the default
6131 * application to handle the file specified by @file.
6133 * If @cancellable is not %NULL, then the operation can be cancelled by
6134 * triggering the cancellable object from another thread. If the operation
6135 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6137 * Returns: (transfer full): a #GAppInfo if the handle was found, %NULL if there were errors.
6138 * When you are done with it, release it with g_object_unref()
6141 g_file_query_default_handler (GFile *file,
6142 GCancellable *cancellable,
6146 const char *content_type;
6151 uri_scheme = g_file_get_uri_scheme (file);
6152 if (uri_scheme && uri_scheme[0] != '\0')
6154 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6155 g_free (uri_scheme);
6157 if (appinfo != NULL)
6161 info = g_file_query_info (file,
6162 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6171 content_type = g_file_info_get_content_type (info);
6174 /* Don't use is_native(), as we want to support fuse paths if available */
6175 path = g_file_get_path (file);
6176 appinfo = g_app_info_get_default_for_type (content_type,
6181 g_object_unref (info);
6183 if (appinfo != NULL)
6186 g_set_error_literal (error, G_IO_ERROR,
6187 G_IO_ERROR_NOT_SUPPORTED,
6188 _("No application is registered as handling this file"));
6194 #define GET_CONTENT_BLOCK_SIZE 8192
6197 * g_file_load_contents:
6198 * @file: input #GFile.
6199 * @cancellable: optional #GCancellable object, %NULL to ignore.
6200 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file.
6201 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6202 * or %NULL if the length is not needed
6203 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6204 * or %NULL if the entity tag is not needed
6205 * @error: a #GError, or %NULL
6207 * Loads the content of the file into memory. The data is always
6208 * zero-terminated, but this is not included in the resultant @length.
6209 * The returned @content should be freed with g_free() when no longer
6212 * If @cancellable is not %NULL, then the operation can be cancelled by
6213 * triggering the cancellable object from another thread. If the operation
6214 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6216 * Returns: %TRUE if the @file's contents were successfully loaded.
6217 * %FALSE if there were errors.
6220 g_file_load_contents (GFile *file,
6221 GCancellable *cancellable,
6227 GFileInputStream *in;
6228 GByteArray *content;
6233 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6234 g_return_val_if_fail (contents != NULL, FALSE);
6236 in = g_file_read (file, cancellable, error);
6240 content = g_byte_array_new ();
6243 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6244 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6245 content->data + pos,
6246 GET_CONTENT_BLOCK_SIZE,
6247 cancellable, error)) > 0)
6250 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6257 info = g_file_input_stream_query_info (in,
6258 G_FILE_ATTRIBUTE_ETAG_VALUE,
6263 *etag_out = g_strdup (g_file_info_get_etag (info));
6264 g_object_unref (info);
6268 /* Ignore errors on close */
6269 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6270 g_object_unref (in);
6274 /* error is set already */
6275 g_byte_array_free (content, TRUE);
6282 /* Zero terminate (we got an extra byte allocated for this */
6283 content->data[pos] = 0;
6285 *contents = (char *)g_byte_array_free (content, FALSE);
6293 GCancellable *cancellable;
6294 GFileReadMoreCallback read_more_callback;
6295 GAsyncReadyCallback callback;
6297 GByteArray *content;
6304 load_contents_data_free (LoadContentsData *data)
6307 g_error_free (data->error);
6308 if (data->cancellable)
6309 g_object_unref (data->cancellable);
6311 g_byte_array_free (data->content, TRUE);
6312 g_free (data->etag);
6313 g_object_unref (data->file);
6318 load_contents_close_callback (GObject *obj,
6319 GAsyncResult *close_res,
6322 GInputStream *stream = G_INPUT_STREAM (obj);
6323 LoadContentsData *data = user_data;
6324 GSimpleAsyncResult *res;
6326 /* Ignore errors here, we're only reading anyway */
6327 g_input_stream_close_finish (stream, close_res, NULL);
6328 g_object_unref (stream);
6330 res = g_simple_async_result_new (G_OBJECT (data->file),
6333 g_file_load_contents_async);
6334 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
6335 g_simple_async_result_complete (res);
6336 g_object_unref (res);
6340 load_contents_fstat_callback (GObject *obj,
6341 GAsyncResult *stat_res,
6344 GInputStream *stream = G_INPUT_STREAM (obj);
6345 LoadContentsData *data = user_data;
6348 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6352 data->etag = g_strdup (g_file_info_get_etag (info));
6353 g_object_unref (info);
6356 g_input_stream_close_async (stream, 0,
6358 load_contents_close_callback, data);
6362 load_contents_read_callback (GObject *obj,
6363 GAsyncResult *read_res,
6366 GInputStream *stream = G_INPUT_STREAM (obj);
6367 LoadContentsData *data = user_data;
6368 GError *error = NULL;
6371 read_size = g_input_stream_read_finish (stream, read_res, &error);
6375 /* Error or EOF, close the file */
6376 data->error = error;
6377 g_input_stream_close_async (stream, 0,
6379 load_contents_close_callback, data);
6381 else if (read_size == 0)
6383 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6384 G_FILE_ATTRIBUTE_ETAG_VALUE,
6387 load_contents_fstat_callback,
6390 else if (read_size > 0)
6392 data->pos += read_size;
6394 g_byte_array_set_size (data->content,
6395 data->pos + GET_CONTENT_BLOCK_SIZE);
6398 if (data->read_more_callback &&
6399 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
6400 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6401 G_FILE_ATTRIBUTE_ETAG_VALUE,
6404 load_contents_fstat_callback,
6407 g_input_stream_read_async (stream,
6408 data->content->data + data->pos,
6409 GET_CONTENT_BLOCK_SIZE,
6412 load_contents_read_callback,
6418 load_contents_open_callback (GObject *obj,
6419 GAsyncResult *open_res,
6422 GFile *file = G_FILE (obj);
6423 GFileInputStream *stream;
6424 LoadContentsData *data = user_data;
6425 GError *error = NULL;
6426 GSimpleAsyncResult *res;
6428 stream = g_file_read_finish (file, open_res, &error);
6432 g_byte_array_set_size (data->content,
6433 data->pos + GET_CONTENT_BLOCK_SIZE);
6434 g_input_stream_read_async (G_INPUT_STREAM (stream),
6435 data->content->data + data->pos,
6436 GET_CONTENT_BLOCK_SIZE,
6439 load_contents_read_callback,
6445 res = g_simple_async_result_new_take_error (G_OBJECT (data->file),
6449 g_simple_async_result_complete (res);
6450 load_contents_data_free (data);
6451 g_object_unref (res);
6456 * g_file_load_partial_contents_async: (skip)
6457 * @file: input #GFile.
6458 * @cancellable: optional #GCancellable object, %NULL to ignore.
6459 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
6460 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6461 * @user_data: the data to pass to the callback functions.
6463 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
6464 * used to stop reading from the file when appropriate, else this function
6465 * will behave exactly as g_file_load_contents_async(). This operation
6466 * can be finished by g_file_load_partial_contents_finish().
6468 * Users of this function should be aware that @user_data is passed to
6469 * both the @read_more_callback and the @callback.
6471 * If @cancellable is not %NULL, then the operation can be cancelled by
6472 * triggering the cancellable object from another thread. If the operation
6473 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6476 g_file_load_partial_contents_async (GFile *file,
6477 GCancellable *cancellable,
6478 GFileReadMoreCallback read_more_callback,
6479 GAsyncReadyCallback callback,
6482 LoadContentsData *data;
6484 g_return_if_fail (G_IS_FILE (file));
6486 data = g_new0 (LoadContentsData, 1);
6489 data->cancellable = g_object_ref (cancellable);
6490 data->read_more_callback = read_more_callback;
6491 data->callback = callback;
6492 data->user_data = user_data;
6493 data->content = g_byte_array_new ();
6494 data->file = g_object_ref (file);
6496 g_file_read_async (file,
6499 load_contents_open_callback,
6504 * g_file_load_partial_contents_finish:
6505 * @file: input #GFile.
6506 * @res: a #GAsyncResult.
6507 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file.
6508 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6509 * or %NULL if the length is not needed
6510 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6511 * or %NULL if the entity tag is not needed
6512 * @error: a #GError, or %NULL
6514 * Finishes an asynchronous partial load operation that was started
6515 * with g_file_load_partial_contents_async(). The data is always
6516 * zero-terminated, but this is not included in the resultant @length.
6517 * The returned @content should be freed with g_free() when no longer
6520 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6521 * present, it will be set appropriately.
6524 g_file_load_partial_contents_finish (GFile *file,
6531 GSimpleAsyncResult *simple;
6532 LoadContentsData *data;
6534 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6535 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6536 g_return_val_if_fail (contents != NULL, FALSE);
6538 simple = G_SIMPLE_ASYNC_RESULT (res);
6540 if (g_simple_async_result_propagate_error (simple, error))
6543 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
6545 data = g_simple_async_result_get_op_res_gpointer (simple);
6549 g_propagate_error (error, data->error);
6558 *length = data->pos;
6562 *etag_out = data->etag;
6566 /* Zero terminate */
6567 g_byte_array_set_size (data->content, data->pos + 1);
6568 data->content->data[data->pos] = 0;
6570 *contents = (char *)g_byte_array_free (data->content, FALSE);
6571 data->content = NULL;
6577 * g_file_load_contents_async:
6578 * @file: input #GFile.
6579 * @cancellable: optional #GCancellable object, %NULL to ignore.
6580 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6581 * @user_data: the data to pass to callback function
6583 * Starts an asynchronous load of the @file's contents.
6585 * For more details, see g_file_load_contents() which is
6586 * the synchronous version of this call.
6588 * When the load operation has completed, @callback will be called
6589 * with @user data. To finish the operation, call
6590 * g_file_load_contents_finish() with the #GAsyncResult returned by
6593 * If @cancellable is not %NULL, then the operation can be cancelled by
6594 * triggering the cancellable object from another thread. If the operation
6595 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6598 g_file_load_contents_async (GFile *file,
6599 GCancellable *cancellable,
6600 GAsyncReadyCallback callback,
6603 g_file_load_partial_contents_async (file,
6606 callback, user_data);
6610 * g_file_load_contents_finish:
6611 * @file: input #GFile.
6612 * @res: a #GAsyncResult.
6613 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file.
6614 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6615 * or %NULL if the length is not needed
6616 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6617 * or %NULL if the entity tag is not needed
6618 * @error: a #GError, or %NULL
6620 * Finishes an asynchronous load of the @file's contents.
6621 * The contents are placed in @contents, and @length is set to the
6622 * size of the @contents string. The @content should be freed with
6623 * g_free() when no longer needed. If @etag_out is present, it will be
6624 * set to the new entity tag for the @file.
6626 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6627 * present, it will be set appropriately.
6630 g_file_load_contents_finish (GFile *file,
6637 return g_file_load_partial_contents_finish (file,
6646 * g_file_replace_contents:
6647 * @file: input #GFile.
6648 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file.
6649 * @length: the length of @contents in bytes.
6650 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6651 * for the document, or %NULL
6652 * @make_backup: %TRUE if a backup should be created.
6653 * @flags: a set of #GFileCreateFlags.
6654 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6655 * for the document. This should be freed with g_free() when no longer
6657 * @cancellable: optional #GCancellable object, %NULL to ignore.
6658 * @error: a #GError, or %NULL
6660 * Replaces the contents of @file with @contents of @length bytes.
6662 * If @etag is specified (not %NULL) any existing file must have that etag, or
6663 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
6665 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
6667 * If @cancellable is not %NULL, then the operation can be cancelled by
6668 * triggering the cancellable object from another thread. If the operation
6669 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6671 * The returned @new_etag can be used to verify that the file hasn't changed the
6672 * next time it is saved over.
6674 * Returns: %TRUE if successful. If an error
6675 * has occurred, this function will return %FALSE and set @error
6676 * appropriately if present.
6679 g_file_replace_contents (GFile *file,
6680 const char *contents,
6683 gboolean make_backup,
6684 GFileCreateFlags flags,
6686 GCancellable *cancellable,
6689 GFileOutputStream *out;
6690 gsize pos, remainder;
6694 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6695 g_return_val_if_fail (contents != NULL, FALSE);
6697 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6703 while (remainder > 0 &&
6704 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6706 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6714 if (remainder > 0 && res < 0)
6716 /* Ignore errors on close */
6717 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6718 g_object_unref (out);
6720 /* error is set already */
6724 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6727 *new_etag = g_file_output_stream_get_etag (out);
6729 g_object_unref (out);
6737 GCancellable *cancellable;
6738 GAsyncReadyCallback callback;
6740 const char *content;
6744 } ReplaceContentsData;
6747 replace_contents_data_free (ReplaceContentsData *data)
6750 g_error_free (data->error);
6751 if (data->cancellable)
6752 g_object_unref (data->cancellable);
6753 g_object_unref (data->file);
6754 g_free (data->etag);
6759 replace_contents_close_callback (GObject *obj,
6760 GAsyncResult *close_res,
6763 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6764 ReplaceContentsData *data = user_data;
6765 GSimpleAsyncResult *res;
6767 /* Ignore errors here, we're only reading anyway */
6768 g_output_stream_close_finish (stream, close_res, NULL);
6769 g_object_unref (stream);
6771 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6773 res = g_simple_async_result_new (G_OBJECT (data->file),
6776 g_file_replace_contents_async);
6777 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
6778 g_simple_async_result_complete (res);
6779 g_object_unref (res);
6783 replace_contents_write_callback (GObject *obj,
6784 GAsyncResult *read_res,
6787 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6788 ReplaceContentsData *data = user_data;
6789 GError *error = NULL;
6792 write_size = g_output_stream_write_finish (stream, read_res, &error);
6794 if (write_size <= 0)
6796 /* Error or EOF, close the file */
6798 data->error = error;
6799 g_output_stream_close_async (stream, 0,
6801 replace_contents_close_callback, data);
6803 else if (write_size > 0)
6805 data->pos += write_size;
6807 if (data->pos >= data->length)
6808 g_output_stream_close_async (stream, 0,
6810 replace_contents_close_callback, data);
6812 g_output_stream_write_async (stream,
6813 data->content + data->pos,
6814 data->length - data->pos,
6817 replace_contents_write_callback,
6823 replace_contents_open_callback (GObject *obj,
6824 GAsyncResult *open_res,
6827 GFile *file = G_FILE (obj);
6828 GFileOutputStream *stream;
6829 ReplaceContentsData *data = user_data;
6830 GError *error = NULL;
6831 GSimpleAsyncResult *res;
6833 stream = g_file_replace_finish (file, open_res, &error);
6837 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6838 data->content + data->pos,
6839 data->length - data->pos,
6842 replace_contents_write_callback,
6848 res = g_simple_async_result_new_take_error (G_OBJECT (data->file),
6852 g_simple_async_result_complete (res);
6853 replace_contents_data_free (data);
6854 g_object_unref (res);
6859 * g_file_replace_contents_async:
6860 * @file: input #GFile.
6861 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with.
6862 * @length: the length of @contents in bytes.
6863 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6864 * @make_backup: %TRUE if a backup should be created.
6865 * @flags: a set of #GFileCreateFlags.
6866 * @cancellable: optional #GCancellable object, %NULL to ignore.
6867 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6868 * @user_data: the data to pass to callback function
6870 * Starts an asynchronous replacement of @file with the given
6871 * @contents of @length bytes. @etag will replace the document's
6872 * current entity tag.
6874 * When this operation has completed, @callback will be called with
6875 * @user_user data, and the operation can be finalized with
6876 * g_file_replace_contents_finish().
6878 * If @cancellable is not %NULL, then the operation can be cancelled by
6879 * triggering the cancellable object from another thread. If the operation
6880 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6882 * If @make_backup is %TRUE, this function will attempt to
6883 * make a backup of @file.
6886 g_file_replace_contents_async (GFile *file,
6887 const char *contents,
6890 gboolean make_backup,
6891 GFileCreateFlags flags,
6892 GCancellable *cancellable,
6893 GAsyncReadyCallback callback,
6896 ReplaceContentsData *data;
6898 g_return_if_fail (G_IS_FILE (file));
6899 g_return_if_fail (contents != NULL);
6901 data = g_new0 (ReplaceContentsData, 1);
6904 data->cancellable = g_object_ref (cancellable);
6905 data->callback = callback;
6906 data->user_data = user_data;
6907 data->content = contents;
6908 data->length = length;
6910 data->file = g_object_ref (file);
6912 g_file_replace_async (file,
6918 replace_contents_open_callback,
6923 * g_file_replace_contents_finish:
6924 * @file: input #GFile.
6925 * @res: a #GAsyncResult.
6926 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
6927 * for the document. This should be freed with g_free() when it is no
6928 * longer needed, or %NULL
6929 * @error: a #GError, or %NULL
6931 * Finishes an asynchronous replace of the given @file. See
6932 * g_file_replace_contents_async(). Sets @new_etag to the new entity
6933 * tag for the document, if present.
6935 * Returns: %TRUE on success, %FALSE on failure.
6938 g_file_replace_contents_finish (GFile *file,
6943 GSimpleAsyncResult *simple;
6944 ReplaceContentsData *data;
6946 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6947 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6949 simple = G_SIMPLE_ASYNC_RESULT (res);
6951 if (g_simple_async_result_propagate_error (simple, error))
6954 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
6956 data = g_simple_async_result_get_op_res_gpointer (simple);
6960 g_propagate_error (error, data->error);
6968 *new_etag = data->etag;
6969 data->etag = NULL; /* Take ownership */
6976 * g_file_start_mountable:
6977 * @file: input #GFile.
6978 * @flags: flags affecting the operation
6979 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
6980 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
6981 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
6982 * @user_data: the data to pass to callback function
6984 * Starts a file of type G_FILE_TYPE_MOUNTABLE.
6985 * Using @start_operation, you can request callbacks when, for instance,
6986 * passwords are needed during authentication.
6988 * If @cancellable is not %NULL, then the operation can be cancelled by
6989 * triggering the cancellable object from another thread. If the operation
6990 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6992 * When the operation is finished, @callback will be called. You can then call
6993 * g_file_mount_mountable_finish() to get the result of the operation.
6998 g_file_start_mountable (GFile *file,
6999 GDriveStartFlags flags,
7000 GMountOperation *start_operation,
7001 GCancellable *cancellable,
7002 GAsyncReadyCallback callback,
7007 g_return_if_fail (G_IS_FILE (file));
7009 iface = G_FILE_GET_IFACE (file);
7011 if (iface->start_mountable == NULL)
7013 g_simple_async_report_error_in_idle (G_OBJECT (file),
7017 G_IO_ERROR_NOT_SUPPORTED,
7018 _("Operation not supported"));
7022 (* iface->start_mountable) (file,
7031 * g_file_start_mountable_finish:
7032 * @file: input #GFile.
7033 * @result: a #GAsyncResult.
7034 * @error: a #GError, or %NULL
7036 * Finishes a start operation. See g_file_start_mountable() for details.
7038 * Finish an asynchronous start operation that was started
7039 * with g_file_start_mountable().
7041 * Returns: %TRUE if the operation finished successfully. %FALSE
7047 g_file_start_mountable_finish (GFile *file,
7048 GAsyncResult *result,
7053 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7054 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7056 if (G_IS_SIMPLE_ASYNC_RESULT (result))
7058 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
7059 if (g_simple_async_result_propagate_error (simple, error))
7063 iface = G_FILE_GET_IFACE (file);
7064 return (* iface->start_mountable_finish) (file, result, error);
7068 * g_file_stop_mountable:
7069 * @file: input #GFile.
7070 * @flags: flags affecting the operation
7071 * @mount_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
7072 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
7073 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
7074 * @user_data: the data to pass to callback function
7076 * Stops a file of type G_FILE_TYPE_MOUNTABLE.
7078 * If @cancellable is not %NULL, then the operation can be cancelled by
7079 * triggering the cancellable object from another thread. If the operation
7080 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7082 * When the operation is finished, @callback will be called. You can then call
7083 * g_file_stop_mountable_finish() to get the result of the operation.
7088 g_file_stop_mountable (GFile *file,
7089 GMountUnmountFlags flags,
7090 GMountOperation *mount_operation,
7091 GCancellable *cancellable,
7092 GAsyncReadyCallback callback,
7097 g_return_if_fail (G_IS_FILE (file));
7099 iface = G_FILE_GET_IFACE (file);
7101 if (iface->stop_mountable == NULL)
7103 g_simple_async_report_error_in_idle (G_OBJECT (file),
7107 G_IO_ERROR_NOT_SUPPORTED,
7108 _("Operation not supported"));
7112 (* iface->stop_mountable) (file,
7121 * g_file_stop_mountable_finish:
7122 * @file: input #GFile.
7123 * @result: a #GAsyncResult.
7124 * @error: a #GError, or %NULL
7126 * Finishes an stop operation, see g_file_stop_mountable() for details.
7128 * Finish an asynchronous stop operation that was started
7129 * with g_file_stop_mountable().
7131 * Returns: %TRUE if the operation finished successfully. %FALSE
7137 g_file_stop_mountable_finish (GFile *file,
7138 GAsyncResult *result,
7143 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7144 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7146 if (G_IS_SIMPLE_ASYNC_RESULT (result))
7148 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
7149 if (g_simple_async_result_propagate_error (simple, error))
7153 iface = G_FILE_GET_IFACE (file);
7154 return (* iface->stop_mountable_finish) (file, result, error);
7158 * g_file_poll_mountable:
7159 * @file: input #GFile.
7160 * @cancellable: optional #GCancellable object, %NULL to ignore.
7161 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
7162 * @user_data: the data to pass to callback function
7164 * Polls a file of type G_FILE_TYPE_MOUNTABLE.
7166 * If @cancellable is not %NULL, then the operation can be cancelled by
7167 * triggering the cancellable object from another thread. If the operation
7168 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7170 * When the operation is finished, @callback will be called. You can then call
7171 * g_file_mount_mountable_finish() to get the result of the operation.
7176 g_file_poll_mountable (GFile *file,
7177 GCancellable *cancellable,
7178 GAsyncReadyCallback callback,
7183 g_return_if_fail (G_IS_FILE (file));
7185 iface = G_FILE_GET_IFACE (file);
7187 if (iface->poll_mountable == NULL)
7189 g_simple_async_report_error_in_idle (G_OBJECT (file),
7193 G_IO_ERROR_NOT_SUPPORTED,
7194 _("Operation not supported"));
7198 (* iface->poll_mountable) (file,
7205 * g_file_poll_mountable_finish:
7206 * @file: input #GFile.
7207 * @result: a #GAsyncResult.
7208 * @error: a #GError, or %NULL
7210 * Finishes a poll operation. See g_file_poll_mountable() for details.
7212 * Finish an asynchronous poll operation that was polled
7213 * with g_file_poll_mountable().
7215 * Returns: %TRUE if the operation finished successfully. %FALSE
7221 g_file_poll_mountable_finish (GFile *file,
7222 GAsyncResult *result,
7227 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7228 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7230 if (G_IS_SIMPLE_ASYNC_RESULT (result))
7232 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
7233 if (g_simple_async_result_propagate_error (simple, error))
7237 iface = G_FILE_GET_IFACE (file);
7238 return (* iface->poll_mountable_finish) (file, result, error);
7242 * g_file_supports_thread_contexts:
7245 * Checks if @file supports <link
7246 * linkend="g-main-context-push-thread-default-context">thread-default
7247 * contexts</link>. If this returns %FALSE, you cannot perform
7248 * asynchronous operations on @file in a thread that has a
7249 * thread-default context.
7251 * Returns: Whether or not @file supports thread-default contexts.
7256 g_file_supports_thread_contexts (GFile *file)
7260 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7262 iface = G_FILE_GET_IFACE (file);
7263 return iface->supports_thread_contexts;