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_delete_async (GFile *file,
201 GCancellable *cancellable,
202 GAsyncReadyCallback callback,
204 static gboolean g_file_real_delete_finish (GFile *file,
207 static void g_file_real_open_readwrite_async (GFile *file,
209 GCancellable *cancellable,
210 GAsyncReadyCallback callback,
212 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
215 static void g_file_real_create_readwrite_async (GFile *file,
216 GFileCreateFlags flags,
218 GCancellable *cancellable,
219 GAsyncReadyCallback callback,
221 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
224 static void g_file_real_replace_readwrite_async (GFile *file,
226 gboolean make_backup,
227 GFileCreateFlags flags,
229 GCancellable *cancellable,
230 GAsyncReadyCallback callback,
232 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
235 static gboolean g_file_real_set_attributes_from_info (GFile *file,
237 GFileQueryInfoFlags flags,
238 GCancellable *cancellable,
240 static void g_file_real_set_display_name_async (GFile *file,
241 const char *display_name,
243 GCancellable *cancellable,
244 GAsyncReadyCallback callback,
246 static GFile * g_file_real_set_display_name_finish (GFile *file,
249 static void g_file_real_set_attributes_async (GFile *file,
251 GFileQueryInfoFlags flags,
253 GCancellable *cancellable,
254 GAsyncReadyCallback callback,
256 static gboolean g_file_real_set_attributes_finish (GFile *file,
260 static void g_file_real_find_enclosing_mount_async (GFile *file,
262 GCancellable *cancellable,
263 GAsyncReadyCallback callback,
265 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
268 static void g_file_real_copy_async (GFile *source,
270 GFileCopyFlags flags,
272 GCancellable *cancellable,
273 GFileProgressCallback progress_callback,
274 gpointer progress_callback_data,
275 GAsyncReadyCallback callback,
277 static gboolean g_file_real_copy_finish (GFile *file,
281 typedef GFileIface GFileInterface;
282 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
285 g_file_default_init (GFileIface *iface)
287 iface->enumerate_children_async = g_file_real_enumerate_children_async;
288 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
289 iface->set_display_name_async = g_file_real_set_display_name_async;
290 iface->set_display_name_finish = g_file_real_set_display_name_finish;
291 iface->query_info_async = g_file_real_query_info_async;
292 iface->query_info_finish = g_file_real_query_info_finish;
293 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
294 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
295 iface->set_attributes_async = g_file_real_set_attributes_async;
296 iface->set_attributes_finish = g_file_real_set_attributes_finish;
297 iface->read_async = g_file_real_read_async;
298 iface->read_finish = g_file_real_read_finish;
299 iface->append_to_async = g_file_real_append_to_async;
300 iface->append_to_finish = g_file_real_append_to_finish;
301 iface->create_async = g_file_real_create_async;
302 iface->create_finish = g_file_real_create_finish;
303 iface->replace_async = g_file_real_replace_async;
304 iface->replace_finish = g_file_real_replace_finish;
305 iface->delete_file_async = g_file_real_delete_async;
306 iface->delete_file_finish = g_file_real_delete_finish;
307 iface->open_readwrite_async = g_file_real_open_readwrite_async;
308 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
309 iface->create_readwrite_async = g_file_real_create_readwrite_async;
310 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
311 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
312 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
313 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
314 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
315 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
316 iface->copy_async = g_file_real_copy_async;
317 iface->copy_finish = g_file_real_copy_finish;
323 * @file: input #GFile.
325 * Checks to see if a file is native to the platform.
327 * A native file s one expressed in the platform-native filename format,
328 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
329 * as it might be on a locally mounted remote filesystem.
331 * On some systems non-native files may be available using
332 * the native filesystem via a userspace filesystem (FUSE), in
333 * these cases this call will return %FALSE, but g_file_get_path()
334 * will still return a native path.
336 * This call does no blocking i/o.
338 * Returns: %TRUE if file is native.
341 g_file_is_native (GFile *file)
345 g_return_val_if_fail (G_IS_FILE (file), FALSE);
347 iface = G_FILE_GET_IFACE (file);
349 return (* iface->is_native) (file);
354 * g_file_has_uri_scheme:
355 * @file: input #GFile.
356 * @uri_scheme: a string containing a URI scheme.
358 * Checks to see if a #GFile has a given URI scheme.
360 * This call does no blocking i/o.
362 * Returns: %TRUE if #GFile's backend supports the
363 * given URI scheme, %FALSE if URI scheme is %NULL,
364 * not supported, or #GFile is invalid.
367 g_file_has_uri_scheme (GFile *file,
368 const char *uri_scheme)
372 g_return_val_if_fail (G_IS_FILE (file), FALSE);
373 g_return_val_if_fail (uri_scheme != NULL, FALSE);
375 iface = G_FILE_GET_IFACE (file);
377 return (* iface->has_uri_scheme) (file, uri_scheme);
382 * g_file_get_uri_scheme:
383 * @file: input #GFile.
385 * Gets the URI scheme for a #GFile.
386 * RFC 3986 decodes the scheme as:
388 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
390 * Common schemes include "file", "http", "ftp", etc.
392 * This call does no blocking i/o.
394 * Returns: a string containing the URI scheme for the given
395 * #GFile. The returned string should be freed with g_free()
396 * when no longer needed.
399 g_file_get_uri_scheme (GFile *file)
403 g_return_val_if_fail (G_IS_FILE (file), NULL);
405 iface = G_FILE_GET_IFACE (file);
407 return (* iface->get_uri_scheme) (file);
412 * g_file_get_basename:
413 * @file: input #GFile.
415 * Gets the base name (the last component of the path) for a given #GFile.
417 * If called for the top level of a system (such as the filesystem root
418 * or a uri like sftp://host/) it will return a single directory separator
419 * (and on Windows, possibly a drive letter).
421 * The base name is a byte string (*not* UTF-8). It has no defined encoding
422 * or rules other than it may not contain zero bytes. If you want to use
423 * filenames in a user interface you should use the display name that you
424 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
425 * attribute with g_file_query_info().
427 * This call does no blocking i/o.
429 * Returns: string containing the #GFile's base name, or %NULL
430 * if given #GFile is invalid. The returned string should be
431 * freed with g_free() when no longer needed.
434 g_file_get_basename (GFile *file)
438 g_return_val_if_fail (G_IS_FILE (file), NULL);
440 iface = G_FILE_GET_IFACE (file);
442 return (* iface->get_basename) (file);
447 * @file: input #GFile.
449 * Gets the local pathname for #GFile, if one exists.
451 * This call does no blocking i/o.
453 * Returns: string containing the #GFile's path, or %NULL if
454 * no such path exists. The returned string should be
455 * freed with g_free() when no longer needed.
458 g_file_get_path (GFile *file)
462 g_return_val_if_fail (G_IS_FILE (file), NULL);
464 iface = G_FILE_GET_IFACE (file);
466 return (* iface->get_path) (file);
471 * @file: input #GFile.
473 * Gets the URI for the @file.
475 * This call does no blocking i/o.
477 * Returns: a string containing the #GFile's URI.
478 * The returned string should be freed with g_free() when no longer needed.
481 g_file_get_uri (GFile *file)
485 g_return_val_if_fail (G_IS_FILE (file), NULL);
487 iface = G_FILE_GET_IFACE (file);
489 return (* iface->get_uri) (file);
493 * g_file_get_parse_name:
494 * @file: input #GFile.
496 * Gets the parse name of the @file.
497 * A parse name is a UTF-8 string that describes the
498 * file such that one can get the #GFile back using
499 * g_file_parse_name().
501 * This is generally used to show the #GFile as a nice
502 * full-pathname kind of string in a user interface,
503 * like in a location entry.
505 * For local files with names that can safely be converted
506 * to UTF8 the pathname is used, otherwise the IRI is used
507 * (a form of URI that allows UTF8 characters unescaped).
509 * This call does no blocking i/o.
511 * Returns: a string containing the #GFile's parse name. The returned
512 * string should be freed with g_free() when no longer needed.
515 g_file_get_parse_name (GFile *file)
519 g_return_val_if_fail (G_IS_FILE (file), NULL);
521 iface = G_FILE_GET_IFACE (file);
523 return (* iface->get_parse_name) (file);
528 * @file: input #GFile.
530 * Duplicates a #GFile handle. This operation does not duplicate
531 * the actual file or directory represented by the #GFile; see
532 * g_file_copy() if attempting to copy a file.
534 * This call does no blocking i/o.
536 * Returns: (transfer full): a new #GFile that is a duplicate of the given #GFile.
539 g_file_dup (GFile *file)
543 g_return_val_if_fail (G_IS_FILE (file), NULL);
545 iface = G_FILE_GET_IFACE (file);
547 return (* iface->dup) (file);
552 * @file: (type GFile): #gconstpointer to a #GFile.
554 * Creates a hash value for a #GFile.
556 * This call does no blocking i/o.
559 * Returns: 0 if @file is not a valid #GFile, otherwise an
560 * integer that can be used as hash value for the #GFile.
561 * This function is intended for easily hashing a #GFile to
562 * add to a #GHashTable or similar data structure.
565 g_file_hash (gconstpointer file)
569 g_return_val_if_fail (G_IS_FILE (file), 0);
571 iface = G_FILE_GET_IFACE (file);
573 return (* iface->hash) ((GFile *)file);
578 * @file1: the first #GFile.
579 * @file2: the second #GFile.
581 * Checks equality of two given #GFile<!-- -->s. Note that two
582 * #GFile<!-- -->s that differ can still refer to the same
583 * file on the filesystem due to various forms of filename
586 * This call does no blocking i/o.
588 * Returns: %TRUE if @file1 and @file2 are equal.
589 * %FALSE if either is not a #GFile.
592 g_file_equal (GFile *file1,
597 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
598 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
600 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
603 iface = G_FILE_GET_IFACE (file1);
605 return (* iface->equal) (file1, file2);
611 * @file: input #GFile.
613 * Gets the parent directory for the @file.
614 * If the @file represents the root directory of the
615 * file system, then %NULL will be returned.
617 * This call does no blocking i/o.
619 * Returns: (transfer full): a #GFile structure to the parent of the given
620 * #GFile or %NULL if there is no parent.
621 * Free the returned object with g_object_unref().
624 g_file_get_parent (GFile *file)
628 g_return_val_if_fail (G_IS_FILE (file), NULL);
630 iface = G_FILE_GET_IFACE (file);
632 return (* iface->get_parent) (file);
637 * @file: input #GFile
638 * @parent: (allow-none): the parent to check for, or %NULL
640 * Checks if @file has a parent, and optionally, if it is @parent.
642 * If @parent is %NULL then this function returns %TRUE if @file has any
643 * parent at all. If @parent is non-%NULL then %TRUE is only returned
644 * if @file is a child of @parent.
646 * Returns: %TRUE if @file is a child of @parent (or any parent in the
647 * case that @parent is %NULL).
652 g_file_has_parent (GFile *file,
655 GFile *actual_parent;
658 g_return_val_if_fail (G_IS_FILE (file), FALSE);
659 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
661 actual_parent = g_file_get_parent (file);
663 if (actual_parent != NULL)
666 result = g_file_equal (parent, actual_parent);
670 g_object_unref (actual_parent);
680 * @file: input #GFile.
681 * @name: string containing the child's basename.
683 * Gets a child of @file with basename equal to @name.
685 * Note that the file with that specific name might not exist, but
686 * you can still have a #GFile that points to it. You can use this
687 * for instance to create that file.
689 * This call does no blocking i/o.
691 * Returns: (transfer full): a #GFile to a child specified by @name.
692 * Free the returned object with g_object_unref().
695 g_file_get_child (GFile *file,
698 g_return_val_if_fail (G_IS_FILE (file), NULL);
699 g_return_val_if_fail (name != NULL, NULL);
701 return g_file_resolve_relative_path (file, name);
705 * g_file_get_child_for_display_name:
706 * @file: input #GFile.
707 * @display_name: string to a possible child.
710 * Gets the child of @file for a given @display_name (i.e. a UTF8
711 * version of the name). If this function fails, it returns %NULL and @error will be
712 * set. This is very useful when constructing a GFile for a new file
713 * and the user entered the filename in the user interface, for instance
714 * when you select a directory and type a filename in the file selector.
716 * This call does no blocking i/o.
718 * Returns: (transfer full): a #GFile to the specified child, or
719 * %NULL if the display name couldn't be converted.
720 * Free the returned object with g_object_unref().
723 g_file_get_child_for_display_name (GFile *file,
724 const char *display_name,
729 g_return_val_if_fail (G_IS_FILE (file), NULL);
730 g_return_val_if_fail (display_name != NULL, NULL);
732 iface = G_FILE_GET_IFACE (file);
734 return (* iface->get_child_for_display_name) (file, display_name, error);
739 * @file: input #GFile.
740 * @prefix: input #GFile.
742 * Checks whether @file has the prefix specified by @prefix. In other word,
743 * if the names of initial elements of @file<!-- -->s pathname match @prefix.
744 * Only full pathname elements are matched, so a path like /foo is not
745 * considered a prefix of /foobar, only of /foo/bar.
747 * This call does no i/o, as it works purely on names. As such it can
748 * sometimes return %FALSE even if @file is inside a @prefix (from a
749 * filesystem point of view), because the prefix of @file is an alias
752 * Virtual: prefix_matches
753 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix.
757 g_file_has_prefix (GFile *file,
762 g_return_val_if_fail (G_IS_FILE (file), FALSE);
763 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
765 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
768 iface = G_FILE_GET_IFACE (file);
770 /* The vtable function differs in arg order since we're
771 using the old contains_file call */
772 return (* iface->prefix_matches) (prefix, file);
776 * g_file_get_relative_path:
777 * @parent: input #GFile.
778 * @descendant: input #GFile.
780 * Gets the path for @descendant relative to @parent.
782 * This call does no blocking i/o.
784 * Returns: string with the relative path from @descendant
785 * to @parent, or %NULL if @descendant doesn't have @parent as prefix.
786 * The returned string should be freed with g_free() when no longer needed.
789 g_file_get_relative_path (GFile *parent,
794 g_return_val_if_fail (G_IS_FILE (parent), NULL);
795 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
797 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
800 iface = G_FILE_GET_IFACE (parent);
802 return (* iface->get_relative_path) (parent, descendant);
806 * g_file_resolve_relative_path:
807 * @file: input #GFile.
808 * @relative_path: a given relative path string.
810 * Resolves a relative path for @file to an absolute path.
812 * This call does no blocking i/o.
814 * Returns: (transfer full): #GFile to the resolved path. %NULL if @relative_path
815 * is %NULL or if @file is invalid.
816 * Free the returned object with g_object_unref().
819 g_file_resolve_relative_path (GFile *file,
820 const char *relative_path)
824 g_return_val_if_fail (G_IS_FILE (file), NULL);
825 g_return_val_if_fail (relative_path != NULL, NULL);
827 iface = G_FILE_GET_IFACE (file);
829 return (* iface->resolve_relative_path) (file, relative_path);
833 * g_file_enumerate_children:
834 * @file: input #GFile.
835 * @attributes: an attribute query string.
836 * @flags: a set of #GFileQueryInfoFlags.
837 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
838 * @error: #GError for error reporting.
840 * Gets the requested information about the files in a directory. The result
841 * is a #GFileEnumerator object that will give out #GFileInfo objects for
842 * all the files in the directory.
844 * The @attributes value is a string that specifies the file attributes that
845 * should be gathered. It is not an error if it's not possible to read a particular
846 * requested attribute from a file - it just won't be set. @attributes should
847 * be a comma-separated list of attributes or attribute wildcards. The wildcard "*"
848 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
849 * namespace. An example attribute query be "standard::*,owner::user".
850 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
852 * If @cancellable is not %NULL, then the operation can be cancelled by
853 * triggering the cancellable object from another thread. If the operation
854 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
856 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
857 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
858 * Other errors are possible too.
860 * Returns: (transfer full): A #GFileEnumerator if successful, %NULL on error.
861 * Free the returned object with g_object_unref().
864 g_file_enumerate_children (GFile *file,
865 const char *attributes,
866 GFileQueryInfoFlags flags,
867 GCancellable *cancellable,
873 g_return_val_if_fail (G_IS_FILE (file), NULL);
875 if (g_cancellable_set_error_if_cancelled (cancellable, error))
878 iface = G_FILE_GET_IFACE (file);
880 if (iface->enumerate_children == NULL)
882 g_set_error_literal (error, G_IO_ERROR,
883 G_IO_ERROR_NOT_SUPPORTED,
884 _("Operation not supported"));
888 return (* iface->enumerate_children) (file, attributes, flags,
893 * g_file_enumerate_children_async:
894 * @file: input #GFile.
895 * @attributes: an attribute query string.
896 * @flags: a set of #GFileQueryInfoFlags.
897 * @io_priority: the <link linkend="io-priority">I/O priority</link>
899 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
900 * @callback: (scope async) : a #GAsyncReadyCallback to call when the
901 * request is satisfied
902 * @user_data: (closure): the data to pass to callback function
904 * Asynchronously gets the requested information about the files in a directory. The result
905 * is a #GFileEnumerator object that will give out #GFileInfo objects for
906 * all the files in the directory.
908 * For more details, see g_file_enumerate_children() which is
909 * the synchronous version of this call.
911 * When the operation is finished, @callback will be called. You can then call
912 * g_file_enumerate_children_finish() to get the result of the operation.
915 g_file_enumerate_children_async (GFile *file,
916 const char *attributes,
917 GFileQueryInfoFlags flags,
919 GCancellable *cancellable,
920 GAsyncReadyCallback callback,
925 g_return_if_fail (G_IS_FILE (file));
927 iface = G_FILE_GET_IFACE (file);
928 (* iface->enumerate_children_async) (file,
938 * g_file_enumerate_children_finish:
939 * @file: input #GFile.
940 * @res: a #GAsyncResult.
943 * Finishes an async enumerate children operation.
944 * See g_file_enumerate_children_async().
946 * Returns: (transfer full): a #GFileEnumerator or %NULL if an error occurred.
947 * Free the returned object with g_object_unref().
950 g_file_enumerate_children_finish (GFile *file,
956 g_return_val_if_fail (G_IS_FILE (file), NULL);
957 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
959 if (g_async_result_legacy_propagate_error (res, error))
962 iface = G_FILE_GET_IFACE (file);
963 return (* iface->enumerate_children_finish) (file, res, error);
967 * g_file_query_exists:
968 * @file: input #GFile.
969 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
971 * Utility function to check if a particular file exists. This is
972 * implemented using g_file_query_info() and as such does blocking I/O.
974 * Note that in many cases it is racy to first check for file existence
975 * and then execute something based on the outcome of that, because the
976 * file might have been created or removed in between the operations. The
977 * general approach to handling that is to not check, but just do the
978 * operation and handle the errors as they come.
980 * As an example of race-free checking, take the case of reading a file, and
981 * if it doesn't exist, creating it. There are two racy versions: read it, and
982 * on error create it; and: check if it exists, if not create it. These
983 * can both result in two processes creating the file (with perhaps a partially
984 * written file as the result). The correct approach is to always try to create
985 * the file with g_file_create() which will either atomically create the file
986 * or fail with a G_IO_ERROR_EXISTS error.
988 * However, in many cases an existence check is useful in a user
989 * interface, for instance to make a menu item sensitive/insensitive, so that
990 * you don't have to fool users that something is possible and then just show
991 * and error dialog. If you do this, you should make sure to also handle the
992 * errors that can happen due to races when you execute the operation.
994 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
997 g_file_query_exists (GFile *file,
998 GCancellable *cancellable)
1002 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1004 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1005 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1008 g_object_unref (info);
1016 * g_file_query_file_type:
1017 * @file: input #GFile.
1018 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
1019 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1021 * Utility function to inspect the #GFileType of a file. This is
1022 * implemented using g_file_query_info() and as such does blocking I/O.
1024 * The primary use case of this method is to check if a file is a regular file,
1025 * directory, or symlink.
1027 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
1033 g_file_query_file_type (GFile *file,
1034 GFileQueryInfoFlags flags,
1035 GCancellable *cancellable)
1038 GFileType file_type;
1040 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1041 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1045 file_type = g_file_info_get_file_type (info);
1046 g_object_unref (info);
1049 file_type = G_FILE_TYPE_UNKNOWN;
1055 * g_file_query_info:
1056 * @file: input #GFile.
1057 * @attributes: an attribute query string.
1058 * @flags: a set of #GFileQueryInfoFlags.
1059 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1060 * @error: a #GError.
1062 * Gets the requested information about specified @file. The result
1063 * is a #GFileInfo object that contains key-value attributes (such as
1064 * the type or size of the file).
1066 * The @attributes value is a string that specifies the file attributes that
1067 * should be gathered. It is not an error if it's not possible to read a particular
1068 * requested attribute from a file - it just won't be set. @attributes should
1069 * be a comma-separated list of attributes or attribute wildcards. The wildcard "*"
1070 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1071 * namespace. An example attribute query be "standard::*,owner::user".
1072 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1074 * If @cancellable is not %NULL, then the operation can be cancelled by
1075 * triggering the cancellable object from another thread. If the operation
1076 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1078 * For symlinks, normally the information about the target of the
1079 * symlink is returned, rather than information about the symlink itself.
1080 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1081 * information about the symlink itself will be returned. Also, for symlinks
1082 * that point to non-existing files the information about the symlink itself
1085 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1086 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1088 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL on error.
1089 * Free the returned object with g_object_unref().
1092 g_file_query_info (GFile *file,
1093 const char *attributes,
1094 GFileQueryInfoFlags flags,
1095 GCancellable *cancellable,
1100 g_return_val_if_fail (G_IS_FILE (file), NULL);
1102 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1105 iface = G_FILE_GET_IFACE (file);
1107 if (iface->query_info == NULL)
1109 g_set_error_literal (error, G_IO_ERROR,
1110 G_IO_ERROR_NOT_SUPPORTED,
1111 _("Operation not supported"));
1115 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1119 * g_file_query_info_async:
1120 * @file: input #GFile.
1121 * @attributes: an attribute query string.
1122 * @flags: a set of #GFileQueryInfoFlags.
1123 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1125 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1126 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1127 * @user_data: (closure): the data to pass to callback function
1129 * Asynchronously gets the requested information about specified @file. The result
1130 * is a #GFileInfo object that contains key-value attributes (such as type or size
1133 * For more details, see g_file_query_info() which is
1134 * the synchronous version of this call.
1136 * When the operation is finished, @callback will be called. You can then call
1137 * g_file_query_info_finish() to get the result of the operation.
1140 g_file_query_info_async (GFile *file,
1141 const char *attributes,
1142 GFileQueryInfoFlags flags,
1144 GCancellable *cancellable,
1145 GAsyncReadyCallback callback,
1150 g_return_if_fail (G_IS_FILE (file));
1152 iface = G_FILE_GET_IFACE (file);
1153 (* iface->query_info_async) (file,
1163 * g_file_query_info_finish:
1164 * @file: input #GFile.
1165 * @res: a #GAsyncResult.
1166 * @error: a #GError.
1168 * Finishes an asynchronous file info query.
1169 * See g_file_query_info_async().
1171 * Returns: (transfer full): #GFileInfo for given @file or %NULL on error.
1172 * Free the returned object with g_object_unref().
1175 g_file_query_info_finish (GFile *file,
1181 g_return_val_if_fail (G_IS_FILE (file), NULL);
1182 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1184 if (g_async_result_legacy_propagate_error (res, error))
1187 iface = G_FILE_GET_IFACE (file);
1188 return (* iface->query_info_finish) (file, res, error);
1192 * g_file_query_filesystem_info:
1193 * @file: input #GFile.
1194 * @attributes: an attribute query string.
1195 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1196 * @error: a #GError.
1198 * Similar to g_file_query_info(), but obtains information
1199 * about the filesystem the @file is on, rather than the file itself.
1200 * For instance the amount of space available and the type of
1203 * The @attributes value is a string that specifies the file attributes that
1204 * should be gathered. It is not an error if it's not possible to read a particular
1205 * requested attribute from a file - it just won't be set. @attributes should
1206 * be a comma-separated list of attributes or attribute wildcards. The wildcard "*"
1207 * means all attributes, and a wildcard like "filesystem::*" means all attributes in the
1208 * filesystem namespace. The standard namespace for filesystem attributes is "filesystem".
1209 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1210 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1211 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1213 * If @cancellable is not %NULL, then the operation can be cancelled by
1214 * triggering the cancellable object from another thread. If the operation
1215 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1217 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1218 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1220 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1221 * Free the returned object with g_object_unref().
1224 g_file_query_filesystem_info (GFile *file,
1225 const char *attributes,
1226 GCancellable *cancellable,
1231 g_return_val_if_fail (G_IS_FILE (file), NULL);
1233 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1236 iface = G_FILE_GET_IFACE (file);
1238 if (iface->query_filesystem_info == NULL)
1240 g_set_error_literal (error, G_IO_ERROR,
1241 G_IO_ERROR_NOT_SUPPORTED,
1242 _("Operation not supported"));
1246 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1250 * g_file_query_filesystem_info_async:
1251 * @file: input #GFile.
1252 * @attributes: an attribute query string.
1253 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1255 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1256 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1257 * @user_data: (closure): the data to pass to callback function
1259 * Asynchronously gets the requested information about the filesystem
1260 * that the specified @file is on. The result is a #GFileInfo object
1261 * that contains key-value attributes (such as type or size for the
1264 * For more details, see g_file_query_filesystem_info() which is the
1265 * synchronous version of this call.
1267 * When the operation is finished, @callback will be called. You can
1268 * then call g_file_query_info_finish() to get the result of the
1272 g_file_query_filesystem_info_async (GFile *file,
1273 const char *attributes,
1275 GCancellable *cancellable,
1276 GAsyncReadyCallback callback,
1281 g_return_if_fail (G_IS_FILE (file));
1283 iface = G_FILE_GET_IFACE (file);
1284 (* iface->query_filesystem_info_async) (file,
1293 * g_file_query_filesystem_info_finish:
1294 * @file: input #GFile.
1295 * @res: a #GAsyncResult.
1296 * @error: a #GError.
1298 * Finishes an asynchronous filesystem info query. See
1299 * g_file_query_filesystem_info_async().
1301 * Returns: (transfer full): #GFileInfo for given @file or %NULL on error.
1302 * Free the returned object with g_object_unref().
1305 g_file_query_filesystem_info_finish (GFile *file,
1311 g_return_val_if_fail (G_IS_FILE (file), NULL);
1312 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1314 if (g_async_result_legacy_propagate_error (res, error))
1317 iface = G_FILE_GET_IFACE (file);
1318 return (* iface->query_filesystem_info_finish) (file, res, error);
1322 * g_file_find_enclosing_mount:
1323 * @file: input #GFile.
1324 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1325 * @error: a #GError.
1327 * Gets a #GMount for the #GFile.
1329 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1330 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1333 * If @cancellable is not %NULL, then the operation can be cancelled by
1334 * triggering the cancellable object from another thread. If the operation
1335 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1337 * Returns: (transfer full): a #GMount where the @file is located or %NULL on error.
1338 * Free the returned object with g_object_unref().
1341 g_file_find_enclosing_mount (GFile *file,
1342 GCancellable *cancellable,
1347 g_return_val_if_fail (G_IS_FILE (file), NULL);
1349 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1352 iface = G_FILE_GET_IFACE (file);
1353 if (iface->find_enclosing_mount == NULL)
1356 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1357 /* Translators: This is an error message when trying to find the
1358 * enclosing (user visible) mount of a file, but none exists. */
1359 _("Containing mount does not exist"));
1363 return (* iface->find_enclosing_mount) (file, cancellable, error);
1367 * g_file_find_enclosing_mount_async:
1369 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1371 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1372 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1373 * @user_data: (closure): the data to pass to callback function
1375 * Asynchronously gets the mount for the file.
1377 * For more details, see g_file_find_enclosing_mount() which is
1378 * the synchronous version of this call.
1380 * When the operation is finished, @callback will be called. You can then call
1381 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1384 g_file_find_enclosing_mount_async (GFile *file,
1386 GCancellable *cancellable,
1387 GAsyncReadyCallback callback,
1392 g_return_if_fail (G_IS_FILE (file));
1394 iface = G_FILE_GET_IFACE (file);
1395 (* iface->find_enclosing_mount_async) (file,
1403 * g_file_find_enclosing_mount_finish:
1405 * @res: a #GAsyncResult
1408 * Finishes an asynchronous find mount request.
1409 * See g_file_find_enclosing_mount_async().
1411 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1412 * Free the returned object with g_object_unref().
1415 g_file_find_enclosing_mount_finish (GFile *file,
1421 g_return_val_if_fail (G_IS_FILE (file), NULL);
1422 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1424 if (g_async_result_legacy_propagate_error (res, error))
1427 iface = G_FILE_GET_IFACE (file);
1428 return (* iface->find_enclosing_mount_finish) (file, res, error);
1434 * @file: #GFile to read.
1435 * @cancellable: (allow-none): a #GCancellable
1436 * @error: a #GError, or %NULL
1438 * Opens a file for reading. The result is a #GFileInputStream that
1439 * can be used to read the contents of the file.
1441 * If @cancellable is not %NULL, then the operation can be cancelled by
1442 * triggering the cancellable object from another thread. If the operation
1443 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1445 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1446 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1447 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1450 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1451 * Free the returned object with g_object_unref().
1454 g_file_read (GFile *file,
1455 GCancellable *cancellable,
1460 g_return_val_if_fail (G_IS_FILE (file), NULL);
1462 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1465 iface = G_FILE_GET_IFACE (file);
1467 if (iface->read_fn == NULL)
1469 g_set_error_literal (error, G_IO_ERROR,
1470 G_IO_ERROR_NOT_SUPPORTED,
1471 _("Operation not supported"));
1475 return (* iface->read_fn) (file, cancellable, error);
1480 * @file: input #GFile.
1481 * @flags: a set of #GFileCreateFlags.
1482 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1483 * @error: a #GError, or %NULL
1485 * Gets an output stream for appending data to the file. If
1486 * the file doesn't already exist it is created.
1488 * By default files created are generally readable by everyone,
1489 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1490 * will be made readable only to the current user, to the level that
1491 * is supported on the target filesystem.
1493 * If @cancellable is not %NULL, then the operation can be cancelled by
1494 * triggering the cancellable object from another thread. If the operation
1495 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1497 * Some file systems don't allow all file names, and may
1498 * return an %G_IO_ERROR_INVALID_FILENAME error.
1499 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1500 * returned. Other errors are possible too, and depend on what kind of
1501 * filesystem the file is on.
1503 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1504 * Free the returned object with g_object_unref().
1507 g_file_append_to (GFile *file,
1508 GFileCreateFlags flags,
1509 GCancellable *cancellable,
1514 g_return_val_if_fail (G_IS_FILE (file), NULL);
1516 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1519 iface = G_FILE_GET_IFACE (file);
1521 if (iface->append_to == NULL)
1523 g_set_error_literal (error, G_IO_ERROR,
1524 G_IO_ERROR_NOT_SUPPORTED,
1525 _("Operation not supported"));
1529 return (* iface->append_to) (file, flags, cancellable, error);
1534 * @file: input #GFile.
1535 * @flags: a set of #GFileCreateFlags.
1536 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1537 * @error: a #GError, or %NULL
1539 * Creates a new file and returns an output stream for writing to it.
1540 * The file must not already exist.
1542 * By default files created are generally readable by everyone,
1543 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1544 * will be made readable only to the current user, to the level that
1545 * is supported on the target filesystem.
1547 * If @cancellable is not %NULL, then the operation can be cancelled by
1548 * triggering the cancellable object from another thread. If the operation
1549 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1551 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1552 * error will be returned.
1553 * Some file systems don't allow all file names, and may
1554 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1555 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1556 * Other errors are possible too, and depend on what kind of
1557 * filesystem the file is on.
1559 * Returns: (transfer full): a #GFileOutputStream for the newly created file, or
1561 * Free the returned object with g_object_unref().
1564 g_file_create (GFile *file,
1565 GFileCreateFlags flags,
1566 GCancellable *cancellable,
1571 g_return_val_if_fail (G_IS_FILE (file), NULL);
1573 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1576 iface = G_FILE_GET_IFACE (file);
1578 if (iface->create == NULL)
1580 g_set_error_literal (error, G_IO_ERROR,
1581 G_IO_ERROR_NOT_SUPPORTED,
1582 _("Operation not supported"));
1586 return (* iface->create) (file, flags, cancellable, error);
1591 * @file: input #GFile.
1592 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link> for the
1593 * current #GFile, or #NULL to ignore.
1594 * @make_backup: %TRUE if a backup should be created.
1595 * @flags: a set of #GFileCreateFlags.
1596 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1597 * @error: a #GError, or %NULL
1599 * Returns an output stream for overwriting the file, possibly
1600 * creating a backup copy of the file first. If the file doesn't exist,
1601 * it will be created.
1603 * This will try to replace the file in the safest way possible so
1604 * that any errors during the writing will not affect an already
1605 * existing copy of the file. For instance, for local files it
1606 * may write to a temporary file and then atomically rename over
1607 * the destination when the stream is closed.
1609 * By default files created are generally readable by everyone,
1610 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1611 * will be made readable only to the current user, to the level that
1612 * is supported on the target filesystem.
1614 * If @cancellable is not %NULL, then the operation can be cancelled by
1615 * triggering the cancellable object from another thread. If the operation
1616 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1618 * If you pass in a non-#NULL @etag value, then this value is
1619 * compared to the current entity tag of the file, and if they differ
1620 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1621 * that the file has been changed since you last read it. You can get
1622 * the new etag from g_file_output_stream_get_etag() after you've
1623 * finished writing and closed the #GFileOutputStream. When you load
1624 * a new file you can use g_file_input_stream_query_info() to get
1625 * the etag of the file.
1627 * If @make_backup is %TRUE, this function will attempt to make a backup
1628 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1629 * error will be returned. If you want to replace anyway, try again with
1630 * @make_backup set to %FALSE.
1632 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1633 * and if the file is some other form of non-regular file then a
1634 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1635 * Some file systems don't allow all file names, and may
1636 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1637 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1638 * Other errors are possible too, and depend on what kind of
1639 * filesystem the file is on.
1641 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1642 * Free the returned object with g_object_unref().
1645 g_file_replace (GFile *file,
1647 gboolean make_backup,
1648 GFileCreateFlags flags,
1649 GCancellable *cancellable,
1654 g_return_val_if_fail (G_IS_FILE (file), NULL);
1656 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1659 iface = G_FILE_GET_IFACE (file);
1661 if (iface->replace == NULL)
1663 g_set_error_literal (error, G_IO_ERROR,
1664 G_IO_ERROR_NOT_SUPPORTED,
1665 _("Operation not supported"));
1670 /* Handle empty tag string as NULL in consistent way. */
1671 if (etag && *etag == 0)
1674 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1678 * g_file_open_readwrite:
1679 * @file: #GFile to open
1680 * @cancellable: (allow-none): a #GCancellable
1681 * @error: a #GError, or %NULL
1683 * Opens an existing file for reading and writing. The result is
1684 * a #GFileIOStream that can be used to read and write the contents of the file.
1686 * If @cancellable is not %NULL, then the operation can be cancelled by
1687 * triggering the cancellable object from another thread. If the operation
1688 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1690 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1691 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1692 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1693 * Note that in many non-local file cases read and write streams are not supported,
1694 * so make sure you really need to do read and write streaming, rather than
1695 * just opening for reading or writing.
1697 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1698 * Free the returned object with g_object_unref().
1703 g_file_open_readwrite (GFile *file,
1704 GCancellable *cancellable,
1709 g_return_val_if_fail (G_IS_FILE (file), NULL);
1711 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1714 iface = G_FILE_GET_IFACE (file);
1716 if (iface->open_readwrite == NULL)
1718 g_set_error_literal (error, G_IO_ERROR,
1719 G_IO_ERROR_NOT_SUPPORTED,
1720 _("Operation not supported"));
1724 return (* iface->open_readwrite) (file, cancellable, error);
1728 * g_file_create_readwrite:
1730 * @flags: a set of #GFileCreateFlags
1731 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
1732 * @error: return location for a #GError, or %NULL
1734 * Creates a new file and returns a stream for reading and writing to it.
1735 * The file must not already exist.
1737 * By default files created are generally readable by everyone,
1738 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1739 * will be made readable only to the current user, to the level that
1740 * is supported on the target filesystem.
1742 * If @cancellable is not %NULL, then the operation can be cancelled by
1743 * triggering the cancellable object from another thread. If the operation
1744 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1746 * If a file or directory with this name already exists the %G_IO_ERROR_EXISTS
1747 * error will be returned. Some file systems don't allow all file names,
1748 * and may return an %G_IO_ERROR_INVALID_FILENAME error, and if the name
1749 * is too long, %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors
1750 * are possible too, and depend on what kind of filesystem the file is on.
1752 * Note that in many non-local file cases read and write streams are not
1753 * supported, so make sure you really need to do read and write streaming,
1754 * rather than just opening for reading or writing.
1756 * Returns: (transfer full): a #GFileIOStream for the newly created file, or %NULL on error.
1757 * Free the returned object with g_object_unref().
1762 g_file_create_readwrite (GFile *file,
1763 GFileCreateFlags flags,
1764 GCancellable *cancellable,
1769 g_return_val_if_fail (G_IS_FILE (file), NULL);
1771 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1774 iface = G_FILE_GET_IFACE (file);
1776 if (iface->create_readwrite == NULL)
1778 g_set_error_literal (error, G_IO_ERROR,
1779 G_IO_ERROR_NOT_SUPPORTED,
1780 _("Operation not supported"));
1784 return (* iface->create_readwrite) (file, flags, cancellable, error);
1788 * g_file_replace_readwrite:
1790 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link> for the
1791 * current #GFile, or #NULL to ignore
1792 * @make_backup: %TRUE if a backup should be created
1793 * @flags: a set of #GFileCreateFlags
1794 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
1795 * @error: return location for a #GError, or %NULL
1797 * Returns an output stream for overwriting the file in readwrite mode,
1798 * possibly creating a backup copy of the file first. If the file doesn't
1799 * exist, it will be created.
1801 * For details about the behaviour, see g_file_replace() which does the same
1802 * thing but returns an output stream only.
1804 * Note that in many non-local file cases read and write streams are not
1805 * supported, so make sure you really need to do read and write streaming,
1806 * rather than just opening for reading or writing.
1808 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1809 * Free the returned object with g_object_unref().
1814 g_file_replace_readwrite (GFile *file,
1816 gboolean make_backup,
1817 GFileCreateFlags flags,
1818 GCancellable *cancellable,
1823 g_return_val_if_fail (G_IS_FILE (file), NULL);
1825 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1828 iface = G_FILE_GET_IFACE (file);
1830 if (iface->replace_readwrite == NULL)
1832 g_set_error_literal (error, G_IO_ERROR,
1833 G_IO_ERROR_NOT_SUPPORTED,
1834 _("Operation not supported"));
1838 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1842 * g_file_read_async:
1843 * @file: input #GFile
1844 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1846 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1847 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1848 * @user_data: (closure): the data to pass to callback function
1850 * Asynchronously opens @file for reading.
1852 * For more details, see g_file_read() which is
1853 * the synchronous version of this call.
1855 * When the operation is finished, @callback will be called. You can then call
1856 * g_file_read_finish() to get the result of the operation.
1859 g_file_read_async (GFile *file,
1861 GCancellable *cancellable,
1862 GAsyncReadyCallback callback,
1867 g_return_if_fail (G_IS_FILE (file));
1869 iface = G_FILE_GET_IFACE (file);
1870 (* iface->read_async) (file,
1878 * g_file_read_finish:
1879 * @file: input #GFile.
1880 * @res: a #GAsyncResult.
1881 * @error: a #GError, or %NULL
1883 * Finishes an asynchronous file read operation started with
1884 * g_file_read_async().
1886 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
1887 * Free the returned object with g_object_unref().
1890 g_file_read_finish (GFile *file,
1896 g_return_val_if_fail (G_IS_FILE (file), NULL);
1897 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1899 if (g_async_result_legacy_propagate_error (res, error))
1902 iface = G_FILE_GET_IFACE (file);
1903 return (* iface->read_finish) (file, res, error);
1907 * g_file_append_to_async:
1908 * @file: input #GFile.
1909 * @flags: a set of #GFileCreateFlags.
1910 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1912 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1913 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1914 * @user_data: (closure): the data to pass to callback function
1916 * Asynchronously opens @file for appending.
1918 * For more details, see g_file_append_to() which is
1919 * the synchronous version of this call.
1921 * When the operation is finished, @callback will be called. You can then call
1922 * g_file_append_to_finish() to get the result of the operation.
1925 g_file_append_to_async (GFile *file,
1926 GFileCreateFlags flags,
1928 GCancellable *cancellable,
1929 GAsyncReadyCallback callback,
1934 g_return_if_fail (G_IS_FILE (file));
1936 iface = G_FILE_GET_IFACE (file);
1937 (* iface->append_to_async) (file,
1946 * g_file_append_to_finish:
1947 * @file: input #GFile.
1948 * @res: #GAsyncResult
1949 * @error: a #GError, or %NULL
1951 * Finishes an asynchronous file append operation started with
1952 * g_file_append_to_async().
1954 * Returns: (transfer full): a valid #GFileOutputStream or %NULL on error.
1955 * Free the returned object with g_object_unref().
1958 g_file_append_to_finish (GFile *file,
1964 g_return_val_if_fail (G_IS_FILE (file), NULL);
1965 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1967 if (g_async_result_legacy_propagate_error (res, error))
1970 iface = G_FILE_GET_IFACE (file);
1971 return (* iface->append_to_finish) (file, res, error);
1975 * g_file_create_async:
1976 * @file: input #GFile.
1977 * @flags: a set of #GFileCreateFlags.
1978 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1980 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1981 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1982 * @user_data: (closure): the data to pass to callback function
1984 * Asynchronously creates a new file and returns an output stream for writing to it.
1985 * The file must not already exist.
1987 * For more details, see g_file_create() which is
1988 * the synchronous version of this call.
1990 * When the operation is finished, @callback will be called. You can then call
1991 * g_file_create_finish() to get the result of the operation.
1994 g_file_create_async (GFile *file,
1995 GFileCreateFlags flags,
1997 GCancellable *cancellable,
1998 GAsyncReadyCallback callback,
2003 g_return_if_fail (G_IS_FILE (file));
2005 iface = G_FILE_GET_IFACE (file);
2006 (* iface->create_async) (file,
2015 * g_file_create_finish:
2016 * @file: input #GFile.
2017 * @res: a #GAsyncResult.
2018 * @error: a #GError, or %NULL
2020 * Finishes an asynchronous file create operation started with
2021 * g_file_create_async().
2023 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2024 * Free the returned object with g_object_unref().
2027 g_file_create_finish (GFile *file,
2033 g_return_val_if_fail (G_IS_FILE (file), NULL);
2034 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2036 if (g_async_result_legacy_propagate_error (res, error))
2039 iface = G_FILE_GET_IFACE (file);
2040 return (* iface->create_finish) (file, res, error);
2044 * g_file_replace_async:
2045 * @file: input #GFile.
2046 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link> for the
2047 * current #GFile, or NULL to ignore.
2048 * @make_backup: %TRUE if a backup should be created.
2049 * @flags: a set of #GFileCreateFlags.
2050 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2052 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
2053 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
2054 * @user_data: (closure): the data to pass to callback function
2056 * Asynchronously overwrites the file, replacing the contents, possibly
2057 * creating a backup copy of the file first.
2059 * For more details, see g_file_replace() which is
2060 * the synchronous version of this call.
2062 * When the operation is finished, @callback will be called. You can then call
2063 * g_file_replace_finish() to get the result of the operation.
2066 g_file_replace_async (GFile *file,
2068 gboolean make_backup,
2069 GFileCreateFlags flags,
2071 GCancellable *cancellable,
2072 GAsyncReadyCallback callback,
2077 g_return_if_fail (G_IS_FILE (file));
2079 iface = G_FILE_GET_IFACE (file);
2080 (* iface->replace_async) (file,
2091 * g_file_replace_finish:
2092 * @file: input #GFile.
2093 * @res: a #GAsyncResult.
2094 * @error: a #GError, or %NULL
2096 * Finishes an asynchronous file replace operation started with
2097 * g_file_replace_async().
2099 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2100 * Free the returned object with g_object_unref().
2103 g_file_replace_finish (GFile *file,
2109 g_return_val_if_fail (G_IS_FILE (file), NULL);
2110 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2112 if (g_async_result_legacy_propagate_error (res, error))
2115 iface = G_FILE_GET_IFACE (file);
2116 return (* iface->replace_finish) (file, res, error);
2121 * g_file_open_readwrite_async:
2122 * @file: input #GFile.
2123 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2125 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
2126 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
2127 * @user_data: (closure): the data to pass to callback function
2129 * Asynchronously opens @file for reading and writing.
2131 * For more details, see g_file_open_readwrite() which is
2132 * the synchronous version of this call.
2134 * When the operation is finished, @callback will be called. You can then call
2135 * g_file_open_readwrite_finish() to get the result of the operation.
2140 g_file_open_readwrite_async (GFile *file,
2142 GCancellable *cancellable,
2143 GAsyncReadyCallback callback,
2148 g_return_if_fail (G_IS_FILE (file));
2150 iface = G_FILE_GET_IFACE (file);
2151 (* iface->open_readwrite_async) (file,
2159 * g_file_open_readwrite_finish:
2160 * @file: input #GFile.
2161 * @res: a #GAsyncResult.
2162 * @error: a #GError, or %NULL
2164 * Finishes an asynchronous file read operation started with
2165 * g_file_open_readwrite_async().
2167 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2168 * Free the returned object with g_object_unref().
2173 g_file_open_readwrite_finish (GFile *file,
2179 g_return_val_if_fail (G_IS_FILE (file), NULL);
2180 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2182 if (g_async_result_legacy_propagate_error (res, error))
2185 iface = G_FILE_GET_IFACE (file);
2186 return (* iface->open_readwrite_finish) (file, res, error);
2191 * g_file_create_readwrite_async:
2192 * @file: input #GFile
2193 * @flags: a set of #GFileCreateFlags
2194 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2196 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
2197 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
2198 * @user_data: (closure): the data to pass to callback function
2200 * Asynchronously creates a new file and returns a stream for reading and
2201 * writing to it. The file must not already exist.
2203 * For more details, see g_file_create_readwrite() which is
2204 * the synchronous version of this call.
2206 * When the operation is finished, @callback will be called. You can then
2207 * call g_file_create_readwrite_finish() to get the result of the operation.
2212 g_file_create_readwrite_async (GFile *file,
2213 GFileCreateFlags flags,
2215 GCancellable *cancellable,
2216 GAsyncReadyCallback callback,
2221 g_return_if_fail (G_IS_FILE (file));
2223 iface = G_FILE_GET_IFACE (file);
2224 (* iface->create_readwrite_async) (file,
2233 * g_file_create_readwrite_finish:
2234 * @file: input #GFile
2235 * @res: a #GAsyncResult
2236 * @error: a #GError, or %NULL
2238 * Finishes an asynchronous file create operation started with
2239 * g_file_create_readwrite_async().
2241 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2242 * Free the returned object with g_object_unref().
2247 g_file_create_readwrite_finish (GFile *file,
2253 g_return_val_if_fail (G_IS_FILE (file), NULL);
2254 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2256 if (g_async_result_legacy_propagate_error (res, error))
2259 iface = G_FILE_GET_IFACE (file);
2260 return (* iface->create_readwrite_finish) (file, res, error);
2264 * g_file_replace_readwrite_async:
2265 * @file: input #GFile.
2266 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link> for the
2267 * current #GFile, or NULL to ignore.
2268 * @make_backup: %TRUE if a backup should be created.
2269 * @flags: a set of #GFileCreateFlags.
2270 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2272 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
2273 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
2274 * @user_data: (closure): the data to pass to callback function
2276 * Asynchronously overwrites the file in read-write mode, replacing the
2277 * contents, possibly creating a backup copy of the file first.
2279 * For more details, see g_file_replace_readwrite() which is
2280 * the synchronous version of this call.
2282 * When the operation is finished, @callback will be called. You can then
2283 * call g_file_replace_readwrite_finish() to get the result of the operation.
2288 g_file_replace_readwrite_async (GFile *file,
2290 gboolean make_backup,
2291 GFileCreateFlags flags,
2293 GCancellable *cancellable,
2294 GAsyncReadyCallback callback,
2299 g_return_if_fail (G_IS_FILE (file));
2301 iface = G_FILE_GET_IFACE (file);
2302 (* iface->replace_readwrite_async) (file,
2313 * g_file_replace_readwrite_finish:
2314 * @file: input #GFile.
2315 * @res: a #GAsyncResult.
2316 * @error: a #GError, or %NULL
2318 * Finishes an asynchronous file replace operation started with
2319 * g_file_replace_readwrite_async().
2321 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2322 * Free the returned object with g_object_unref().
2327 g_file_replace_readwrite_finish (GFile *file,
2333 g_return_val_if_fail (G_IS_FILE (file), NULL);
2334 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2336 if (g_async_result_legacy_propagate_error (res, error))
2339 iface = G_FILE_GET_IFACE (file);
2340 return (* iface->replace_readwrite_finish) (file, res, error);
2344 copy_symlink (GFile *destination,
2345 GFileCopyFlags flags,
2346 GCancellable *cancellable,
2351 gboolean tried_delete;
2353 GFileType file_type;
2355 tried_delete = FALSE;
2359 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2361 /* Maybe it already existed, and we want to overwrite? */
2362 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2363 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2365 g_error_free (my_error);
2368 /* Don't overwrite if the destination is a directory */
2369 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2370 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2371 cancellable, &my_error);
2374 file_type = g_file_info_get_file_type (info);
2375 g_object_unref (info);
2377 if (file_type == G_FILE_TYPE_DIRECTORY)
2379 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2380 _("Can't copy over directory"));
2385 if (!g_file_delete (destination, cancellable, error))
2388 tried_delete = TRUE;
2392 g_propagate_error (error, my_error);
2399 static GInputStream *
2400 open_source_for_copy (GFile *source,
2402 GFileCopyFlags flags,
2403 GCancellable *cancellable,
2409 GFileType file_type;
2412 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2416 /* There was an error opening the source, try to set a good error for it: */
2418 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2420 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2421 * as that is less useful to the app. Better check for errors on the
2424 g_error_free (my_error);
2427 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2428 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2429 cancellable, &my_error);
2431 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2433 file_type = g_file_info_get_file_type (info);
2434 g_object_unref (info);
2436 if (flags & G_FILE_COPY_OVERWRITE)
2438 if (file_type == G_FILE_TYPE_DIRECTORY)
2440 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2441 _("Can't copy directory over directory"));
2444 /* continue to would_recurse error */
2448 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2449 _("Target file exists"));
2455 /* Error getting info from target, return that error
2456 * (except for NOT_FOUND, which is no error here)
2458 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2460 g_propagate_error (error, my_error);
2463 g_clear_error (&my_error);
2466 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2467 _("Can't recursively copy directory"));
2471 g_propagate_error (error, my_error);
2476 should_copy (GFileAttributeInfo *info,
2478 gboolean skip_perms)
2480 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2484 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2485 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2489 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2490 GFileAttributeInfoList *namespaces,
2492 gboolean skip_perms)
2499 s = g_string_new ("");
2503 for (i = 0; i < attributes->n_infos; i++)
2505 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2510 g_string_append_c (s, ',');
2512 g_string_append (s, attributes->infos[i].name);
2519 for (i = 0; i < namespaces->n_infos; i++)
2521 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2526 g_string_append_c (s, ',');
2528 g_string_append (s, namespaces->infos[i].name);
2529 g_string_append (s, "::*");
2534 return g_string_free (s, FALSE);
2538 * g_file_copy_attributes:
2539 * @source: a #GFile with attributes.
2540 * @destination: a #GFile to copy attributes to.
2541 * @flags: a set of #GFileCopyFlags.
2542 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
2543 * @error: a #GError, %NULL to ignore.
2545 * Copies the file attributes from @source to @destination.
2547 * Normally only a subset of the file attributes are copied,
2548 * those that are copies in a normal file copy operation
2549 * (which for instance does not include e.g. owner). However
2550 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2551 * all the metadata that is possible to copy is copied. This
2552 * is useful when implementing move by copy + delete source.
2554 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2557 g_file_copy_attributes (GFile *source,
2559 GFileCopyFlags flags,
2560 GCancellable *cancellable,
2563 GFileAttributeInfoList *attributes, *namespaces;
2564 char *attrs_to_read;
2568 gboolean source_nofollow_symlinks;
2569 gboolean skip_perms;
2571 as_move = flags & G_FILE_COPY_ALL_METADATA;
2572 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2573 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2575 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2576 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2577 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2579 if (attributes == NULL && namespaces == NULL)
2582 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2584 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2585 * we just don't copy it.
2587 info = g_file_query_info (source, attrs_to_read,
2588 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2592 g_free (attrs_to_read);
2597 res = g_file_set_attributes_from_info (destination,
2599 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2602 g_object_unref (info);
2605 g_file_attribute_info_list_unref (attributes);
2606 g_file_attribute_info_list_unref (namespaces);
2612 copy_stream_with_progress (GInputStream *in,
2615 GCancellable *cancellable,
2616 GFileProgressCallback progress_callback,
2617 gpointer progress_callback_data,
2620 gssize n_read, n_written;
2621 goffset current_size;
2622 char buffer[1024*64], *p;
2628 /* avoid performance impact of querying total size when it's not needed */
2629 if (progress_callback)
2631 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2632 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2636 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2637 total_size = g_file_info_get_size (info);
2638 g_object_unref (info);
2641 if (total_size == -1)
2643 info = g_file_query_info (source,
2644 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2645 G_FILE_QUERY_INFO_NONE,
2649 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2650 total_size = g_file_info_get_size (info);
2651 g_object_unref (info);
2656 if (total_size == -1)
2663 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2673 current_size += n_read;
2678 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2679 if (n_written == -1)
2686 n_read -= n_written;
2692 if (progress_callback)
2693 progress_callback (current_size, total_size, progress_callback_data);
2696 /* Make sure we send full copied size */
2697 if (progress_callback)
2698 progress_callback (current_size, total_size, progress_callback_data);
2706 do_splice (int fd_in,
2711 long *bytes_transferd,
2717 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2725 else if (errsv == ENOSYS || errsv == EINVAL)
2726 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2727 _("Splice not supported"));
2729 g_set_error (error, G_IO_ERROR,
2730 g_io_error_from_errno (errsv),
2731 _("Error splicing file: %s"),
2732 g_strerror (errsv));
2737 *bytes_transferd = result;
2742 splice_stream_with_progress (GInputStream *in,
2744 GCancellable *cancellable,
2745 GFileProgressCallback progress_callback,
2746 gpointer progress_callback_data,
2756 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2757 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2759 if (pipe (buffer) != 0)
2761 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2762 "Pipe creation failed");
2767 /* avoid performance impact of querying total size when it's not needed */
2768 if (progress_callback)
2772 if (fstat (fd_in, &sbuf) == 0)
2773 total_size = sbuf.st_size;
2776 if (total_size == -1)
2779 offset_in = offset_out = 0;
2786 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2789 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2800 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2803 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2806 n_read -= n_written;
2809 if (progress_callback)
2810 progress_callback (offset_in, total_size, progress_callback_data);
2813 /* Make sure we send full copied size */
2814 if (progress_callback)
2815 progress_callback (offset_in, total_size, progress_callback_data);
2826 file_copy_fallback (GFile *source,
2828 GFileCopyFlags flags,
2829 GCancellable *cancellable,
2830 GFileProgressCallback progress_callback,
2831 gpointer progress_callback_data,
2840 gboolean fallback = TRUE;
2843 /* need to know the file type */
2844 info = g_file_query_info (source,
2845 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2846 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2853 /* Maybe copy the symlink? */
2854 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
2855 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
2857 target = g_file_info_get_symlink_target (info);
2860 if (!copy_symlink (destination, flags, cancellable, target, error))
2862 g_object_unref (info);
2866 g_object_unref (info);
2869 /* ... else fall back on a regular file copy */
2870 g_object_unref (info);
2872 /* Handle "special" files (pipes, device nodes, ...)? */
2873 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
2875 /* FIXME: could try to recreate device nodes and others? */
2877 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2878 _("Can't copy special file"));
2879 g_object_unref (info);
2882 /* Everything else should just fall back on a regular copy. */
2884 g_object_unref (info);
2886 in = open_source_for_copy (source, destination, flags, cancellable, error);
2890 if (flags & G_FILE_COPY_OVERWRITE)
2892 out = (GOutputStream *)g_file_replace (destination,
2894 flags & G_FILE_COPY_BACKUP,
2895 G_FILE_CREATE_REPLACE_DESTINATION,
2896 cancellable, error);
2900 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2905 g_object_unref (in);
2910 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
2912 GError *splice_err = NULL;
2914 result = splice_stream_with_progress (in, out, cancellable,
2915 progress_callback, progress_callback_data,
2918 if (result || !g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
2922 g_propagate_error (error, splice_err);
2925 g_clear_error (&splice_err);
2930 result = copy_stream_with_progress (in, out, source, cancellable,
2931 progress_callback, progress_callback_data,
2934 /* Don't care about errors in source here */
2935 g_input_stream_close (in, cancellable, NULL);
2937 /* But write errors on close are bad! */
2938 if (!g_output_stream_close (out, cancellable, result ? error : NULL))
2941 g_object_unref (in);
2942 g_object_unref (out);
2944 if (result == FALSE)
2948 /* Ignore errors here. Failure to copy metadata is not a hard error */
2949 g_file_copy_attributes (source, destination,
2950 flags, cancellable, NULL);
2957 * @source: input #GFile.
2958 * @destination: destination #GFile
2959 * @flags: set of #GFileCopyFlags
2960 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
2961 * @progress_callback: (allow-none) (scope call): function to callback with
2962 * progress information, or %NULL if progress information is not needed
2963 * @progress_callback_data: (closure): user data to pass to @progress_callback
2964 * @error: #GError to set on error, or %NULL
2966 * Copies the file @source to the location specified by @destination.
2967 * Can not handle recursive copies of directories.
2969 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2970 * existing @destination file is overwritten.
2972 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
2973 * will be copied as symlinks, otherwise the target of the
2974 * @source symlink will be copied.
2976 * If @cancellable is not %NULL, then the operation can be cancelled by
2977 * triggering the cancellable object from another thread. If the operation
2978 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
2980 * If @progress_callback is not %NULL, then the operation can be monitored by
2981 * setting this to a #GFileProgressCallback function. @progress_callback_data
2982 * will be passed to this function. It is guaranteed that this callback will
2983 * be called after all data has been transferred with the total number of bytes
2984 * copied during the operation.
2986 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
2987 * error is returned, independent on the status of the @destination.
2989 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
2990 * error G_IO_ERROR_EXISTS is returned.
2992 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
2993 * error is returned. If trying to overwrite a directory with a directory the
2994 * G_IO_ERROR_WOULD_MERGE error is returned.
2996 * If the source is a directory and the target does not exist, or
2997 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
2998 * G_IO_ERROR_WOULD_RECURSE error is returned.
3000 * If you are interested in copying the #GFile object itself (not the on-disk
3001 * file), see g_file_dup().
3003 * Returns: %TRUE on success, %FALSE otherwise.
3006 g_file_copy (GFile *source,
3008 GFileCopyFlags flags,
3009 GCancellable *cancellable,
3010 GFileProgressCallback progress_callback,
3011 gpointer progress_callback_data,
3018 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3019 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3021 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3024 iface = G_FILE_GET_IFACE (destination);
3028 res = (* iface->copy) (source, destination,
3030 progress_callback, progress_callback_data,
3036 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3038 g_propagate_error (error, my_error);
3042 g_clear_error (&my_error);
3045 /* If the types are different, and the destination method failed
3046 also try the source method */
3047 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3049 iface = G_FILE_GET_IFACE (source);
3054 res = (* iface->copy) (source, destination,
3056 progress_callback, progress_callback_data,
3062 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3064 g_propagate_error (error, my_error);
3068 g_clear_error (&my_error);
3072 return file_copy_fallback (source, destination, flags, cancellable,
3073 progress_callback, progress_callback_data,
3078 * g_file_copy_async: (skip)
3079 * @source: input #GFile.
3080 * @destination: destination #GFile
3081 * @flags: set of #GFileCopyFlags
3082 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3084 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
3085 * @progress_callback: (allow-none): function to callback with progress
3086 * information, or %NULL if progress information is not needed
3087 * @progress_callback_data: (closure): user data to pass to @progress_callback
3088 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3089 * @user_data: the data to pass to callback function
3091 * Copies the file @source to the location specified by @destination
3092 * asynchronously. For details of the behaviour, see g_file_copy().
3094 * If @progress_callback is not %NULL, then that function that will be called
3095 * just like in g_file_copy(), however the callback will run in the main loop,
3096 * not in the thread that is doing the I/O operation.
3098 * When the operation is finished, @callback will be called. You can then call
3099 * g_file_copy_finish() to get the result of the operation.
3102 g_file_copy_async (GFile *source,
3104 GFileCopyFlags flags,
3106 GCancellable *cancellable,
3107 GFileProgressCallback progress_callback,
3108 gpointer progress_callback_data,
3109 GAsyncReadyCallback callback,
3114 g_return_if_fail (G_IS_FILE (source));
3115 g_return_if_fail (G_IS_FILE (destination));
3117 iface = G_FILE_GET_IFACE (source);
3118 (* iface->copy_async) (source,
3124 progress_callback_data,
3130 * g_file_copy_finish:
3131 * @file: input #GFile.
3132 * @res: a #GAsyncResult.
3133 * @error: a #GError, or %NULL
3135 * Finishes copying the file started with
3136 * g_file_copy_async().
3138 * Returns: a %TRUE on success, %FALSE on error.
3141 g_file_copy_finish (GFile *file,
3147 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3148 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3150 if (g_async_result_legacy_propagate_error (res, error))
3153 iface = G_FILE_GET_IFACE (file);
3154 return (* iface->copy_finish) (file, res, error);
3159 * @source: #GFile pointing to the source location.
3160 * @destination: #GFile pointing to the destination location.
3161 * @flags: set of #GFileCopyFlags.
3162 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3163 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback function for updates.
3164 * @progress_callback_data: (closure): gpointer to user data for the callback function.
3165 * @error: #GError for returning error conditions, or %NULL
3168 * Tries to move the file or directory @source to the location specified by @destination.
3169 * If native move operations are supported then this is used, otherwise a copy + delete
3170 * fallback is used. The native implementation may support moving directories (for instance
3171 * on moves inside the same filesystem), but the fallback code does not.
3173 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3174 * existing @destination file is overwritten.
3176 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3177 * will be copied as symlinks, otherwise the target of the
3178 * @source symlink will be copied.
3180 * If @cancellable is not %NULL, then the operation can be cancelled by
3181 * triggering the cancellable object from another thread. If the operation
3182 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3184 * If @progress_callback is not %NULL, then the operation can be monitored by
3185 * setting this to a #GFileProgressCallback function. @progress_callback_data
3186 * will be passed to this function. It is guaranteed that this callback will
3187 * be called after all data has been transferred with the total number of bytes
3188 * copied during the operation.
3190 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
3191 * error is returned, independent on the status of the @destination.
3193 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
3194 * error G_IO_ERROR_EXISTS is returned.
3196 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
3197 * error is returned. If trying to overwrite a directory with a directory the
3198 * G_IO_ERROR_WOULD_MERGE error is returned.
3200 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
3201 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
3202 * may be returned (if the native move operation isn't available).
3204 * Returns: %TRUE on successful move, %FALSE otherwise.
3207 g_file_move (GFile *source,
3209 GFileCopyFlags flags,
3210 GCancellable *cancellable,
3211 GFileProgressCallback progress_callback,
3212 gpointer progress_callback_data,
3219 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3220 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3222 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3225 iface = G_FILE_GET_IFACE (destination);
3229 res = (* iface->move) (source, destination,
3231 progress_callback, progress_callback_data,
3237 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3239 g_propagate_error (error, my_error);
3244 /* If the types are different, and the destination method failed
3245 also try the source method */
3246 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3248 iface = G_FILE_GET_IFACE (source);
3253 res = (* iface->move) (source, destination,
3255 progress_callback, progress_callback_data,
3261 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3263 g_propagate_error (error, my_error);
3269 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3271 g_set_error_literal (error, G_IO_ERROR,
3272 G_IO_ERROR_NOT_SUPPORTED,
3273 _("Operation not supported"));
3277 flags |= G_FILE_COPY_ALL_METADATA;
3278 if (!g_file_copy (source, destination, flags, cancellable,
3279 progress_callback, progress_callback_data,
3283 return g_file_delete (source, cancellable, error);
3287 * g_file_make_directory:
3288 * @file: input #GFile.
3289 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3290 * @error: a #GError, or %NULL
3292 * Creates a directory. Note that this will only create a child directory of
3293 * the immediate parent directory of the path or URI given by the #GFile. To
3294 * recursively create directories, see g_file_make_directory_with_parents().
3295 * This function will fail if the parent directory does not exist, setting
3296 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
3297 * directories, this function will fail, setting @error to
3298 * %G_IO_ERROR_NOT_SUPPORTED.
3300 * For a local #GFile the newly created directory will have the default
3301 * (current) ownership and permissions of the current process.
3303 * If @cancellable is not %NULL, then the operation can be cancelled by
3304 * triggering the cancellable object from another thread. If the operation
3305 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3307 * Returns: %TRUE on successful creation, %FALSE otherwise.
3310 g_file_make_directory (GFile *file,
3311 GCancellable *cancellable,
3316 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3318 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3321 iface = G_FILE_GET_IFACE (file);
3323 if (iface->make_directory == NULL)
3325 g_set_error_literal (error, G_IO_ERROR,
3326 G_IO_ERROR_NOT_SUPPORTED,
3327 _("Operation not supported"));
3331 return (* iface->make_directory) (file, cancellable, error);
3335 * g_file_make_directory_with_parents:
3336 * @file: input #GFile.
3337 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3338 * @error: a #GError, or %NULL
3340 * Creates a directory and any parent directories that may not exist similar to
3341 * 'mkdir -p'. If the file system does not support creating directories, this
3342 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED. If the
3343 * directory itself already exists, this function will fail setting @error
3344 * to %G_IO_ERROR_EXISTS, unlike the similar g_mkdir_with_parents().
3346 * For a local #GFile the newly created directories will have the default
3347 * (current) ownership and permissions of the current process.
3349 * If @cancellable is not %NULL, then the operation can be cancelled by
3350 * triggering the cancellable object from another thread. If the operation
3351 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3353 * Returns: %TRUE if all directories have been successfully created, %FALSE
3359 g_file_make_directory_with_parents (GFile *file,
3360 GCancellable *cancellable,
3364 GFile *work_file = NULL;
3365 GList *list = NULL, *l;
3366 GError *my_error = NULL;
3368 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3370 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3373 result = g_file_make_directory (file, cancellable, &my_error);
3374 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
3377 g_propagate_error (error, my_error);
3381 work_file = g_object_ref (file);
3383 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3387 g_clear_error (&my_error);
3389 parent_file = g_file_get_parent (work_file);
3390 if (parent_file == NULL)
3392 result = g_file_make_directory (parent_file, cancellable, &my_error);
3394 g_object_unref (work_file);
3395 work_file = g_object_ref (parent_file);
3397 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3399 g_clear_error (&my_error);
3400 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3403 g_object_unref (parent_file);
3406 for (l = list; result && l; l = l->next)
3408 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3412 g_object_unref (work_file);
3415 while (list != NULL)
3417 g_object_unref ((GFile *) list->data);
3418 list = g_list_remove (list, list->data);
3423 g_propagate_error (error, my_error);
3427 return g_file_make_directory (file, cancellable, error);
3431 * g_file_make_symbolic_link:
3432 * @file: a #GFile with the name of the symlink to create
3433 * @symlink_value: a string with the path for the target of the new symlink
3434 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3435 * @error: a #GError.
3437 * Creates a symbolic link named @file which contains the string
3440 * If @cancellable is not %NULL, then the operation can be cancelled by
3441 * triggering the cancellable object from another thread. If the operation
3442 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3444 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3447 g_file_make_symbolic_link (GFile *file,
3448 const char *symlink_value,
3449 GCancellable *cancellable,
3454 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3455 g_return_val_if_fail (symlink_value != NULL, FALSE);
3457 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3460 if (*symlink_value == '\0')
3462 g_set_error_literal (error, G_IO_ERROR,
3463 G_IO_ERROR_INVALID_ARGUMENT,
3464 _("Invalid symlink value given"));
3468 iface = G_FILE_GET_IFACE (file);
3470 if (iface->make_symbolic_link == NULL)
3472 g_set_error_literal (error, G_IO_ERROR,
3473 G_IO_ERROR_NOT_SUPPORTED,
3474 _("Operation not supported"));
3478 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3483 * @file: input #GFile.
3484 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3485 * @error: a #GError, or %NULL
3487 * Deletes a file. If the @file is a directory, it will only be deleted if it
3488 * is empty. This has the same semantics as g_unlink().
3490 * If @cancellable is not %NULL, then the operation can be cancelled by
3491 * triggering the cancellable object from another thread. If the operation
3492 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3494 * Virtual: delete_file
3495 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3498 g_file_delete (GFile *file,
3499 GCancellable *cancellable,
3504 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3506 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3509 iface = G_FILE_GET_IFACE (file);
3511 if (iface->delete_file == NULL)
3513 g_set_error_literal (error, G_IO_ERROR,
3514 G_IO_ERROR_NOT_SUPPORTED,
3515 _("Operation not supported"));
3519 return (* iface->delete_file) (file, cancellable, error);
3523 * g_file_delete_async:
3524 * @file: input #GFile.
3525 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3527 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3528 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3529 * @user_data: the data to pass to callback function
3531 * Asynchronously delete a file. If the @file is a directory, it will
3532 * only be deleted if it is empty. This has the same semantics as
3535 * Virtual: delete_file_async
3539 g_file_delete_async (GFile *file,
3541 GCancellable *cancellable,
3542 GAsyncReadyCallback callback,
3547 g_return_if_fail (G_IS_FILE (file));
3549 iface = G_FILE_GET_IFACE (file);
3550 (* iface->delete_file_async) (file,
3558 * g_file_delete_finish:
3559 * @file: input #GFile.
3560 * @res: a #GAsyncResult.
3561 * @error: a #GError, or %NULL
3563 * Finishes deleting a file started with
3564 * g_file_delete_async().
3566 * Virtual: delete_file_finish
3570 g_file_delete_finish (GFile *file,
3571 GAsyncResult *result,
3576 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3577 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3579 if (g_async_result_legacy_propagate_error (result, error))
3582 iface = G_FILE_GET_IFACE (file);
3583 return (* iface->delete_file_finish) (file, result, error);
3588 * @file: #GFile to send to trash.
3589 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3590 * @error: a #GError, or %NULL
3592 * Sends @file to the "Trashcan", if possible. This is similar to
3593 * deleting it, but the user can recover it before emptying the trashcan.
3594 * Not all file systems support trashing, so this call can return the
3595 * %G_IO_ERROR_NOT_SUPPORTED error.
3598 * If @cancellable is not %NULL, then the operation can be cancelled by
3599 * triggering the cancellable object from another thread. If the operation
3600 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3602 * Returns: %TRUE on successful trash, %FALSE otherwise.
3605 g_file_trash (GFile *file,
3606 GCancellable *cancellable,
3611 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3613 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3616 iface = G_FILE_GET_IFACE (file);
3618 if (iface->trash == NULL)
3620 g_set_error_literal (error,
3621 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3622 _("Trash not supported"));
3626 return (* iface->trash) (file, cancellable, error);
3630 * g_file_set_display_name:
3631 * @file: input #GFile.
3632 * @display_name: a string.
3633 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3634 * @error: a #GError, or %NULL
3636 * Renames @file to the specified display name.
3638 * The display name is converted from UTF8 to the correct encoding for the target
3639 * filesystem if possible and the @file is renamed to this.
3641 * If you want to implement a rename operation in the user interface the edit name
3642 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
3643 * widget, and then the result after editing should be passed to g_file_set_display_name().
3645 * On success the resulting converted filename is returned.
3647 * If @cancellable is not %NULL, then the operation can be cancelled by
3648 * triggering the cancellable object from another thread. If the operation
3649 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3651 * Returns: (transfer full): a #GFile specifying what @file was renamed to, or %NULL
3652 * if there was an error.
3653 * Free the returned object with g_object_unref().
3656 g_file_set_display_name (GFile *file,
3657 const char *display_name,
3658 GCancellable *cancellable,
3663 g_return_val_if_fail (G_IS_FILE (file), NULL);
3664 g_return_val_if_fail (display_name != NULL, NULL);
3666 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3670 G_IO_ERROR_INVALID_ARGUMENT,
3671 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3675 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3678 iface = G_FILE_GET_IFACE (file);
3680 return (* iface->set_display_name) (file, display_name, cancellable, error);
3684 * g_file_set_display_name_async:
3685 * @file: input #GFile.
3686 * @display_name: a string.
3687 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3689 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3690 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
3691 * @user_data: (closure): the data to pass to callback function
3693 * Asynchronously sets the display name for a given #GFile.
3695 * For more details, see g_file_set_display_name() which is
3696 * the synchronous version of this call.
3698 * When the operation is finished, @callback will be called. You can then call
3699 * g_file_set_display_name_finish() to get the result of the operation.
3702 g_file_set_display_name_async (GFile *file,
3703 const char *display_name,
3705 GCancellable *cancellable,
3706 GAsyncReadyCallback callback,
3711 g_return_if_fail (G_IS_FILE (file));
3712 g_return_if_fail (display_name != NULL);
3714 iface = G_FILE_GET_IFACE (file);
3715 (* iface->set_display_name_async) (file,
3724 * g_file_set_display_name_finish:
3725 * @file: input #GFile.
3726 * @res: a #GAsyncResult.
3727 * @error: a #GError, or %NULL
3729 * Finishes setting a display name started with
3730 * g_file_set_display_name_async().
3732 * Returns: (transfer full): a #GFile or %NULL on error.
3733 * Free the returned object with g_object_unref().
3736 g_file_set_display_name_finish (GFile *file,
3742 g_return_val_if_fail (G_IS_FILE (file), NULL);
3743 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3745 if (g_async_result_legacy_propagate_error (res, error))
3748 iface = G_FILE_GET_IFACE (file);
3749 return (* iface->set_display_name_finish) (file, res, error);
3753 * g_file_query_settable_attributes:
3754 * @file: input #GFile.
3755 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3756 * @error: a #GError, or %NULL
3758 * Obtain the list of settable attributes for the file.
3760 * Returns the type and full attribute name of all the attributes
3761 * that can be set on this file. This doesn't mean setting it will always
3762 * succeed though, you might get an access failure, or some specific
3763 * file may not support a specific attribute.
3765 * If @cancellable is not %NULL, then the operation can be cancelled by
3766 * triggering the cancellable object from another thread. If the operation
3767 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3769 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3770 * When you are done with it, release it with g_file_attribute_info_list_unref()
3772 GFileAttributeInfoList *
3773 g_file_query_settable_attributes (GFile *file,
3774 GCancellable *cancellable,
3779 GFileAttributeInfoList *list;
3781 g_return_val_if_fail (G_IS_FILE (file), NULL);
3783 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3786 iface = G_FILE_GET_IFACE (file);
3788 if (iface->query_settable_attributes == NULL)
3789 return g_file_attribute_info_list_new ();
3792 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3796 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3798 list = g_file_attribute_info_list_new ();
3799 g_error_free (my_error);
3802 g_propagate_error (error, my_error);
3809 * g_file_query_writable_namespaces:
3810 * @file: input #GFile.
3811 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3812 * @error: a #GError, or %NULL
3814 * Obtain the list of attribute namespaces where new attributes
3815 * can be created by a user. An example of this is extended
3816 * attributes (in the "xattr" namespace).
3818 * If @cancellable is not %NULL, then the operation can be cancelled by
3819 * triggering the cancellable object from another thread. If the operation
3820 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3822 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3823 * When you are done with it, release it with g_file_attribute_info_list_unref()
3825 GFileAttributeInfoList *
3826 g_file_query_writable_namespaces (GFile *file,
3827 GCancellable *cancellable,
3832 GFileAttributeInfoList *list;
3834 g_return_val_if_fail (G_IS_FILE (file), NULL);
3836 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3839 iface = G_FILE_GET_IFACE (file);
3841 if (iface->query_writable_namespaces == NULL)
3842 return g_file_attribute_info_list_new ();
3845 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3849 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3851 list = g_file_attribute_info_list_new ();
3852 g_error_free (my_error);
3855 g_propagate_error (error, my_error);
3862 * g_file_set_attribute:
3863 * @file: input #GFile.
3864 * @attribute: a string containing the attribute's name.
3865 * @type: The type of the attribute
3866 * @value_p: (allow-none): a pointer to the value (or the pointer itself if the type is a pointer type)
3867 * @flags: a set of #GFileQueryInfoFlags.
3868 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3869 * @error: a #GError, or %NULL
3871 * Sets an attribute in the file with attribute name @attribute to @value.
3873 * Some attributes can be unset by setting @attribute to
3874 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
3876 * If @cancellable is not %NULL, then the operation can be cancelled by
3877 * triggering the cancellable object from another thread. If the operation
3878 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3880 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3883 g_file_set_attribute (GFile *file,
3884 const char *attribute,
3885 GFileAttributeType type,
3887 GFileQueryInfoFlags flags,
3888 GCancellable *cancellable,
3893 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3894 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3896 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3899 iface = G_FILE_GET_IFACE (file);
3901 if (iface->set_attribute == NULL)
3903 g_set_error_literal (error, G_IO_ERROR,
3904 G_IO_ERROR_NOT_SUPPORTED,
3905 _("Operation not supported"));
3909 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3913 * g_file_set_attributes_from_info:
3914 * @file: input #GFile.
3915 * @info: a #GFileInfo.
3916 * @flags: #GFileQueryInfoFlags
3917 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
3918 * @error: a #GError, or %NULL
3920 * Tries to set all attributes in the #GFileInfo on the target values,
3921 * not stopping on the first error.
3923 * If there is any error during this operation then @error will be set to
3924 * the first error. Error on particular fields are flagged by setting
3925 * the "status" field in the attribute value to
3926 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3929 * If @cancellable is not %NULL, then the operation can be cancelled by
3930 * triggering the cancellable object from another thread. If the operation
3931 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3933 * Returns: %TRUE if there was any error, %FALSE otherwise.
3936 g_file_set_attributes_from_info (GFile *file,
3938 GFileQueryInfoFlags flags,
3939 GCancellable *cancellable,
3944 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3945 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3947 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3950 g_file_info_clear_status (info);
3952 iface = G_FILE_GET_IFACE (file);
3954 return (* iface->set_attributes_from_info) (file,
3963 g_file_real_set_attributes_from_info (GFile *file,
3965 GFileQueryInfoFlags flags,
3966 GCancellable *cancellable,
3972 GFileAttributeValue *value;
3976 attributes = g_file_info_list_attributes (info, NULL);
3978 for (i = 0; attributes[i] != NULL; i++)
3980 value = _g_file_info_get_attribute_value (info, attributes[i]);
3982 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3985 if (!g_file_set_attribute (file, attributes[i],
3986 value->type, _g_file_attribute_value_peek_as_pointer (value),
3987 flags, cancellable, error))
3989 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3991 /* Don't set error multiple times */
3995 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3998 g_strfreev (attributes);
4004 * g_file_set_attributes_async:
4005 * @file: input #GFile.
4006 * @info: a #GFileInfo.
4007 * @flags: a #GFileQueryInfoFlags.
4008 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4010 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4011 * @callback: (scope async): a #GAsyncReadyCallback.
4012 * @user_data: (closure): a #gpointer.
4014 * Asynchronously sets the attributes of @file with @info.
4016 * For more details, see g_file_set_attributes_from_info() which is
4017 * the synchronous version of this call.
4019 * When the operation is finished, @callback will be called. You can then call
4020 * g_file_set_attributes_finish() to get the result of the operation.
4023 g_file_set_attributes_async (GFile *file,
4025 GFileQueryInfoFlags flags,
4027 GCancellable *cancellable,
4028 GAsyncReadyCallback callback,
4033 g_return_if_fail (G_IS_FILE (file));
4034 g_return_if_fail (G_IS_FILE_INFO (info));
4036 iface = G_FILE_GET_IFACE (file);
4037 (* iface->set_attributes_async) (file,
4047 * g_file_set_attributes_finish:
4048 * @file: input #GFile.
4049 * @result: a #GAsyncResult.
4050 * @info: (out) (transfer full): a #GFileInfo.
4051 * @error: a #GError, or %NULL
4053 * Finishes setting an attribute started in g_file_set_attributes_async().
4055 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4058 g_file_set_attributes_finish (GFile *file,
4059 GAsyncResult *result,
4065 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4066 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4068 /* No standard handling of errors here, as we must set info even
4071 iface = G_FILE_GET_IFACE (file);
4072 return (* iface->set_attributes_finish) (file, result, info, error);
4076 * g_file_set_attribute_string:
4077 * @file: input #GFile.
4078 * @attribute: a string containing the attribute's name.
4079 * @value: a string containing the attribute's value.
4080 * @flags: #GFileQueryInfoFlags.
4081 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4082 * @error: a #GError, or %NULL
4084 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4085 * If @attribute is of a different type, this operation will fail.
4087 * If @cancellable is not %NULL, then the operation can be cancelled by
4088 * triggering the cancellable object from another thread. If the operation
4089 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4091 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4094 g_file_set_attribute_string (GFile *file,
4095 const char *attribute,
4097 GFileQueryInfoFlags flags,
4098 GCancellable *cancellable,
4101 return g_file_set_attribute (file, attribute,
4102 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4103 flags, cancellable, error);
4107 * g_file_set_attribute_byte_string:
4108 * @file: input #GFile.
4109 * @attribute: a string containing the attribute's name.
4110 * @value: a string containing the attribute's new value.
4111 * @flags: a #GFileQueryInfoFlags.
4112 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4113 * @error: a #GError, or %NULL
4115 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4116 * If @attribute is of a different type, this operation will fail,
4119 * If @cancellable is not %NULL, then the operation can be cancelled by
4120 * triggering the cancellable object from another thread. If the operation
4121 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4123 * Returns: %TRUE if the @attribute was successfully set to @value
4124 * in the @file, %FALSE otherwise.
4127 g_file_set_attribute_byte_string (GFile *file,
4128 const char *attribute,
4130 GFileQueryInfoFlags flags,
4131 GCancellable *cancellable,
4134 return g_file_set_attribute (file, attribute,
4135 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4136 flags, cancellable, error);
4140 * g_file_set_attribute_uint32:
4141 * @file: input #GFile.
4142 * @attribute: a string containing the attribute's name.
4143 * @value: a #guint32 containing the attribute's new value.
4144 * @flags: a #GFileQueryInfoFlags.
4145 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4146 * @error: a #GError, or %NULL
4148 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4149 * If @attribute is of a different type, this operation will fail.
4151 * If @cancellable is not %NULL, then the operation can be cancelled by
4152 * triggering the cancellable object from another thread. If the operation
4153 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4155 * Returns: %TRUE if the @attribute was successfully set to @value
4156 * in the @file, %FALSE otherwise.
4159 g_file_set_attribute_uint32 (GFile *file,
4160 const char *attribute,
4162 GFileQueryInfoFlags flags,
4163 GCancellable *cancellable,
4166 return g_file_set_attribute (file, attribute,
4167 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4168 flags, cancellable, error);
4172 * g_file_set_attribute_int32:
4173 * @file: input #GFile.
4174 * @attribute: a string containing the attribute's name.
4175 * @value: a #gint32 containing the attribute's new value.
4176 * @flags: a #GFileQueryInfoFlags.
4177 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4178 * @error: a #GError, or %NULL
4180 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4181 * If @attribute is of a different type, this operation will fail.
4183 * If @cancellable is not %NULL, then the operation can be cancelled by
4184 * triggering the cancellable object from another thread. If the operation
4185 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4187 * Returns: %TRUE if the @attribute was successfully set to @value
4188 * in the @file, %FALSE otherwise.
4191 g_file_set_attribute_int32 (GFile *file,
4192 const char *attribute,
4194 GFileQueryInfoFlags flags,
4195 GCancellable *cancellable,
4198 return g_file_set_attribute (file, attribute,
4199 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4200 flags, cancellable, error);
4204 * g_file_set_attribute_uint64:
4205 * @file: input #GFile.
4206 * @attribute: a string containing the attribute's name.
4207 * @value: a #guint64 containing the attribute's new value.
4208 * @flags: a #GFileQueryInfoFlags.
4209 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4210 * @error: a #GError, or %NULL
4212 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4213 * If @attribute is of a different type, this operation will fail.
4215 * If @cancellable is not %NULL, then the operation can be cancelled by
4216 * triggering the cancellable object from another thread. If the operation
4217 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4219 * Returns: %TRUE if the @attribute was successfully set to @value
4220 * in the @file, %FALSE otherwise.
4223 g_file_set_attribute_uint64 (GFile *file,
4224 const char *attribute,
4226 GFileQueryInfoFlags flags,
4227 GCancellable *cancellable,
4230 return g_file_set_attribute (file, attribute,
4231 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4232 flags, cancellable, error);
4236 * g_file_set_attribute_int64:
4237 * @file: input #GFile.
4238 * @attribute: a string containing the attribute's name.
4239 * @value: a #guint64 containing the attribute's new value.
4240 * @flags: a #GFileQueryInfoFlags.
4241 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4242 * @error: a #GError, or %NULL
4244 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4245 * If @attribute is of a different type, this operation will fail.
4247 * If @cancellable is not %NULL, then the operation can be cancelled by
4248 * triggering the cancellable object from another thread. If the operation
4249 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4251 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4254 g_file_set_attribute_int64 (GFile *file,
4255 const char *attribute,
4257 GFileQueryInfoFlags flags,
4258 GCancellable *cancellable,
4261 return g_file_set_attribute (file, attribute,
4262 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4263 flags, cancellable, error);
4267 * g_file_mount_mountable:
4268 * @file: input #GFile.
4269 * @flags: flags affecting the operation
4270 * @mount_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
4271 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4272 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4273 * @user_data: (closure): the data to pass to callback function
4275 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4276 * Using @mount_operation, you can request callbacks when, for instance,
4277 * passwords are needed during authentication.
4279 * If @cancellable is not %NULL, then the operation can be cancelled by
4280 * triggering the cancellable object from another thread. If the operation
4281 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4283 * When the operation is finished, @callback will be called. You can then call
4284 * g_file_mount_mountable_finish() to get the result of the operation.
4287 g_file_mount_mountable (GFile *file,
4288 GMountMountFlags flags,
4289 GMountOperation *mount_operation,
4290 GCancellable *cancellable,
4291 GAsyncReadyCallback callback,
4296 g_return_if_fail (G_IS_FILE (file));
4298 iface = G_FILE_GET_IFACE (file);
4300 if (iface->mount_mountable == NULL)
4302 g_simple_async_report_error_in_idle (G_OBJECT (file),
4306 G_IO_ERROR_NOT_SUPPORTED,
4307 _("Operation not supported"));
4311 (* iface->mount_mountable) (file,
4320 * g_file_mount_mountable_finish:
4321 * @file: input #GFile.
4322 * @result: a #GAsyncResult.
4323 * @error: a #GError, or %NULL
4325 * Finishes a mount operation. See g_file_mount_mountable() for details.
4327 * Finish an asynchronous mount operation that was started
4328 * with g_file_mount_mountable().
4330 * Returns: (transfer full): a #GFile or %NULL on error.
4331 * Free the returned object with g_object_unref().
4334 g_file_mount_mountable_finish (GFile *file,
4335 GAsyncResult *result,
4340 g_return_val_if_fail (G_IS_FILE (file), NULL);
4341 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4343 if (g_async_result_legacy_propagate_error (result, error))
4346 iface = G_FILE_GET_IFACE (file);
4347 return (* iface->mount_mountable_finish) (file, result, error);
4351 * g_file_unmount_mountable:
4352 * @file: input #GFile.
4353 * @flags: flags affecting the operation
4354 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4355 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4356 * @user_data: (closure): the data to pass to callback function
4358 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4360 * If @cancellable is not %NULL, then the operation can be cancelled by
4361 * triggering the cancellable object from another thread. If the operation
4362 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4364 * When the operation is finished, @callback will be called. You can then call
4365 * g_file_unmount_mountable_finish() to get the result of the operation.
4367 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4370 g_file_unmount_mountable (GFile *file,
4371 GMountUnmountFlags flags,
4372 GCancellable *cancellable,
4373 GAsyncReadyCallback callback,
4378 g_return_if_fail (G_IS_FILE (file));
4380 iface = G_FILE_GET_IFACE (file);
4382 if (iface->unmount_mountable == NULL)
4384 g_simple_async_report_error_in_idle (G_OBJECT (file),
4388 G_IO_ERROR_NOT_SUPPORTED,
4389 _("Operation not supported"));
4393 (* iface->unmount_mountable) (file,
4401 * g_file_unmount_mountable_finish:
4402 * @file: input #GFile.
4403 * @result: a #GAsyncResult.
4404 * @error: a #GError, or %NULL
4406 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4408 * Finish an asynchronous unmount operation that was started
4409 * with g_file_unmount_mountable().
4411 * Returns: %TRUE if the operation finished successfully. %FALSE
4414 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish() instead.
4417 g_file_unmount_mountable_finish (GFile *file,
4418 GAsyncResult *result,
4423 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4424 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4426 if (g_async_result_legacy_propagate_error (result, error))
4429 iface = G_FILE_GET_IFACE (file);
4430 return (* iface->unmount_mountable_finish) (file, result, error);
4434 * g_file_unmount_mountable_with_operation:
4435 * @file: input #GFile.
4436 * @flags: flags affecting the operation
4437 * @mount_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
4438 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4439 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4440 * @user_data: (closure): the data to pass to callback function
4442 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4444 * If @cancellable is not %NULL, then the operation can be cancelled by
4445 * triggering the cancellable object from another thread. If the operation
4446 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4448 * When the operation is finished, @callback will be called. You can then call
4449 * g_file_unmount_mountable_finish() to get the result of the operation.
4454 g_file_unmount_mountable_with_operation (GFile *file,
4455 GMountUnmountFlags flags,
4456 GMountOperation *mount_operation,
4457 GCancellable *cancellable,
4458 GAsyncReadyCallback callback,
4463 g_return_if_fail (G_IS_FILE (file));
4465 iface = G_FILE_GET_IFACE (file);
4467 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4469 g_simple_async_report_error_in_idle (G_OBJECT (file),
4473 G_IO_ERROR_NOT_SUPPORTED,
4474 _("Operation not supported"));
4478 if (iface->unmount_mountable_with_operation != NULL)
4479 (* iface->unmount_mountable_with_operation) (file,
4486 (* iface->unmount_mountable) (file,
4494 * g_file_unmount_mountable_with_operation_finish:
4495 * @file: input #GFile.
4496 * @result: a #GAsyncResult.
4497 * @error: a #GError, or %NULL
4499 * Finishes an unmount operation, see g_file_unmount_mountable_with_operation() for details.
4501 * Finish an asynchronous unmount operation that was started
4502 * with g_file_unmount_mountable_with_operation().
4504 * Returns: %TRUE if the operation finished successfully. %FALSE
4510 g_file_unmount_mountable_with_operation_finish (GFile *file,
4511 GAsyncResult *result,
4516 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4517 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4519 if (g_async_result_legacy_propagate_error (result, error))
4522 iface = G_FILE_GET_IFACE (file);
4523 if (iface->unmount_mountable_with_operation_finish != NULL)
4524 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4526 return (* iface->unmount_mountable_finish) (file, result, error);
4530 * g_file_eject_mountable:
4531 * @file: input #GFile.
4532 * @flags: flags affecting the operation
4533 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4534 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4535 * @user_data: (closure): the data to pass to callback function
4537 * Starts an asynchronous eject on a mountable.
4538 * When this operation has completed, @callback will be called with
4539 * @user_user data, and the operation can be finalized with
4540 * g_file_eject_mountable_finish().
4542 * If @cancellable is not %NULL, then the operation can be cancelled by
4543 * triggering the cancellable object from another thread. If the operation
4544 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4546 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4549 g_file_eject_mountable (GFile *file,
4550 GMountUnmountFlags flags,
4551 GCancellable *cancellable,
4552 GAsyncReadyCallback callback,
4557 g_return_if_fail (G_IS_FILE (file));
4559 iface = G_FILE_GET_IFACE (file);
4561 if (iface->eject_mountable == NULL)
4563 g_simple_async_report_error_in_idle (G_OBJECT (file),
4567 G_IO_ERROR_NOT_SUPPORTED,
4568 _("Operation not supported"));
4572 (* iface->eject_mountable) (file,
4580 * g_file_eject_mountable_finish:
4581 * @file: input #GFile.
4582 * @result: a #GAsyncResult.
4583 * @error: a #GError, or %NULL
4585 * Finishes an asynchronous eject operation started by
4586 * g_file_eject_mountable().
4588 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4591 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish() instead.
4594 g_file_eject_mountable_finish (GFile *file,
4595 GAsyncResult *result,
4600 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4601 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4603 if (g_async_result_legacy_propagate_error (result, error))
4606 iface = G_FILE_GET_IFACE (file);
4607 return (* iface->eject_mountable_finish) (file, result, error);
4611 * g_file_eject_mountable_with_operation:
4612 * @file: input #GFile.
4613 * @flags: flags affecting the operation
4614 * @mount_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
4615 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4616 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4617 * @user_data: (closure): the data to pass to callback function
4619 * Starts an asynchronous eject on a mountable.
4620 * When this operation has completed, @callback will be called with
4621 * @user_user data, and the operation can be finalized with
4622 * g_file_eject_mountable_with_operation_finish().
4624 * If @cancellable is not %NULL, then the operation can be cancelled by
4625 * triggering the cancellable object from another thread. If the operation
4626 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4631 g_file_eject_mountable_with_operation (GFile *file,
4632 GMountUnmountFlags flags,
4633 GMountOperation *mount_operation,
4634 GCancellable *cancellable,
4635 GAsyncReadyCallback callback,
4640 g_return_if_fail (G_IS_FILE (file));
4642 iface = G_FILE_GET_IFACE (file);
4644 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4646 g_simple_async_report_error_in_idle (G_OBJECT (file),
4650 G_IO_ERROR_NOT_SUPPORTED,
4651 _("Operation not supported"));
4655 if (iface->eject_mountable_with_operation != NULL)
4656 (* iface->eject_mountable_with_operation) (file,
4663 (* iface->eject_mountable) (file,
4671 * g_file_eject_mountable_with_operation_finish:
4672 * @file: input #GFile.
4673 * @result: a #GAsyncResult.
4674 * @error: a #GError, or %NULL
4676 * Finishes an asynchronous eject operation started by
4677 * g_file_eject_mountable_with_operation().
4679 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4685 g_file_eject_mountable_with_operation_finish (GFile *file,
4686 GAsyncResult *result,
4691 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4692 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4694 if (g_async_result_legacy_propagate_error (result, error))
4697 iface = G_FILE_GET_IFACE (file);
4698 if (iface->eject_mountable_with_operation_finish != NULL)
4699 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4701 return (* iface->eject_mountable_finish) (file, result, error);
4705 * g_file_monitor_directory:
4706 * @file: input #GFile.
4707 * @flags: a set of #GFileMonitorFlags.
4708 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4709 * @error: a #GError, or %NULL.
4711 * Obtains a directory monitor for the given file.
4712 * This may fail if directory monitoring is not supported.
4714 * If @cancellable is not %NULL, then the operation can be cancelled by
4715 * triggering the cancellable object from another thread. If the operation
4716 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4718 * Virtual: monitor_dir
4719 * Returns: (transfer full): a #GFileMonitor for the given @file, or %NULL on error.
4720 * Free the returned object with g_object_unref().
4723 g_file_monitor_directory (GFile *file,
4724 GFileMonitorFlags flags,
4725 GCancellable *cancellable,
4730 g_return_val_if_fail (G_IS_FILE (file), NULL);
4732 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4735 iface = G_FILE_GET_IFACE (file);
4737 if (iface->monitor_dir == NULL)
4739 g_set_error_literal (error, G_IO_ERROR,
4740 G_IO_ERROR_NOT_SUPPORTED,
4741 _("Operation not supported"));
4745 return (* iface->monitor_dir) (file, flags, cancellable, error);
4749 * g_file_monitor_file:
4750 * @file: input #GFile.
4751 * @flags: a set of #GFileMonitorFlags.
4752 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
4753 * @error: a #GError, or %NULL.
4755 * Obtains a file monitor for the given file. If no file notification
4756 * mechanism exists, then regular polling of the file is used.
4758 * If @cancellable is not %NULL, then the operation can be cancelled by
4759 * triggering the cancellable object from another thread. If the operation
4760 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4762 * Returns: (transfer full): a #GFileMonitor for the given @file, or %NULL on error.
4763 * Free the returned object with g_object_unref().
4766 g_file_monitor_file (GFile *file,
4767 GFileMonitorFlags flags,
4768 GCancellable *cancellable,
4772 GFileMonitor *monitor;
4774 g_return_val_if_fail (G_IS_FILE (file), NULL);
4776 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4779 iface = G_FILE_GET_IFACE (file);
4783 if (iface->monitor_file)
4784 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4786 /* Fallback to polling */
4787 if (monitor == NULL)
4788 monitor = _g_poll_file_monitor_new (file);
4795 * @file: input #GFile
4796 * @flags: a set of #GFileMonitorFlags
4797 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
4798 * @error: a #GError, or %NULL
4800 * Obtains a file or directory monitor for the given file, depending
4801 * on the type of the file.
4803 * If @cancellable is not %NULL, then the operation can be cancelled by
4804 * triggering the cancellable object from another thread. If the operation
4805 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4807 * Returns: (transfer full): a #GFileMonitor for the given @file, or %NULL on error.
4808 * Free the returned object with g_object_unref().
4813 g_file_monitor (GFile *file,
4814 GFileMonitorFlags flags,
4815 GCancellable *cancellable,
4818 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4819 return g_file_monitor_directory (file, flags, cancellable, error);
4821 return g_file_monitor_file (file, flags, cancellable, error);
4824 /********************************************
4825 * Default implementation of async ops *
4826 ********************************************/
4830 GFileQueryInfoFlags flags;
4832 } QueryInfoAsyncData;
4835 query_info_data_free (QueryInfoAsyncData *data)
4838 g_object_unref (data->info);
4839 g_free (data->attributes);
4844 query_info_async_thread (GSimpleAsyncResult *res,
4846 GCancellable *cancellable)
4848 GError *error = NULL;
4849 QueryInfoAsyncData *data;
4852 data = g_simple_async_result_get_op_res_gpointer (res);
4854 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4857 g_simple_async_result_take_error (res, error);
4863 g_file_real_query_info_async (GFile *file,
4864 const char *attributes,
4865 GFileQueryInfoFlags flags,
4867 GCancellable *cancellable,
4868 GAsyncReadyCallback callback,
4871 GSimpleAsyncResult *res;
4872 QueryInfoAsyncData *data;
4874 data = g_new0 (QueryInfoAsyncData, 1);
4875 data->attributes = g_strdup (attributes);
4876 data->flags = flags;
4878 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
4879 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
4881 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
4882 g_object_unref (res);
4886 g_file_real_query_info_finish (GFile *file,
4890 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4891 QueryInfoAsyncData *data;
4893 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4895 if (g_simple_async_result_propagate_error (simple, error))
4898 data = g_simple_async_result_get_op_res_gpointer (simple);
4900 return g_object_ref (data->info);
4908 } QueryFilesystemInfoAsyncData;
4911 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4914 g_object_unref (data->info);
4915 g_free (data->attributes);
4920 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4922 GCancellable *cancellable)
4924 GError *error = NULL;
4925 QueryFilesystemInfoAsyncData *data;
4928 data = g_simple_async_result_get_op_res_gpointer (res);
4930 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4933 g_simple_async_result_take_error (res, error);
4939 g_file_real_query_filesystem_info_async (GFile *file,
4940 const char *attributes,
4942 GCancellable *cancellable,
4943 GAsyncReadyCallback callback,
4946 GSimpleAsyncResult *res;
4947 QueryFilesystemInfoAsyncData *data;
4949 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4950 data->attributes = g_strdup (attributes);
4952 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4953 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4955 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4956 g_object_unref (res);
4960 g_file_real_query_filesystem_info_finish (GFile *file,
4964 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4965 QueryFilesystemInfoAsyncData *data;
4967 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4969 if (g_simple_async_result_propagate_error (simple, error))
4972 data = g_simple_async_result_get_op_res_gpointer (simple);
4974 return g_object_ref (data->info);
4981 GFileQueryInfoFlags flags;
4982 GFileEnumerator *enumerator;
4983 } EnumerateChildrenAsyncData;
4986 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4988 if (data->enumerator)
4989 g_object_unref (data->enumerator);
4990 g_free (data->attributes);
4995 enumerate_children_async_thread (GSimpleAsyncResult *res,
4997 GCancellable *cancellable)
4999 GError *error = NULL;
5000 EnumerateChildrenAsyncData *data;
5001 GFileEnumerator *enumerator;
5003 data = g_simple_async_result_get_op_res_gpointer (res);
5005 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5007 if (enumerator == NULL)
5008 g_simple_async_result_take_error (res, error);
5010 data->enumerator = enumerator;
5014 g_file_real_enumerate_children_async (GFile *file,
5015 const char *attributes,
5016 GFileQueryInfoFlags flags,
5018 GCancellable *cancellable,
5019 GAsyncReadyCallback callback,
5022 GSimpleAsyncResult *res;
5023 EnumerateChildrenAsyncData *data;
5025 data = g_new0 (EnumerateChildrenAsyncData, 1);
5026 data->attributes = g_strdup (attributes);
5027 data->flags = flags;
5029 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
5030 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
5032 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
5033 g_object_unref (res);
5036 static GFileEnumerator *
5037 g_file_real_enumerate_children_finish (GFile *file,
5041 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5042 EnumerateChildrenAsyncData *data;
5044 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
5046 if (g_simple_async_result_propagate_error (simple, error))
5049 data = g_simple_async_result_get_op_res_gpointer (simple);
5050 if (data->enumerator)
5051 return g_object_ref (data->enumerator);
5057 open_read_async_thread (GSimpleAsyncResult *res,
5059 GCancellable *cancellable)
5062 GFileInputStream *stream;
5063 GError *error = NULL;
5065 iface = G_FILE_GET_IFACE (object);
5067 if (iface->read_fn == NULL)
5069 g_set_error_literal (&error, G_IO_ERROR,
5070 G_IO_ERROR_NOT_SUPPORTED,
5071 _("Operation not supported"));
5073 g_simple_async_result_take_error (res, error);
5078 stream = iface->read_fn (G_FILE (object), cancellable, &error);
5081 g_simple_async_result_take_error (res, error);
5083 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5087 g_file_real_read_async (GFile *file,
5089 GCancellable *cancellable,
5090 GAsyncReadyCallback callback,
5093 GSimpleAsyncResult *res;
5095 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
5097 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
5098 g_object_unref (res);
5101 static GFileInputStream *
5102 g_file_real_read_finish (GFile *file,
5106 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5109 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
5111 if (g_simple_async_result_propagate_error (simple, error))
5114 op = g_simple_async_result_get_op_res_gpointer (simple);
5116 return g_object_ref (op);
5122 append_to_async_thread (GSimpleAsyncResult *res,
5124 GCancellable *cancellable)
5127 GFileCreateFlags *data;
5128 GFileOutputStream *stream;
5129 GError *error = NULL;
5131 iface = G_FILE_GET_IFACE (object);
5133 data = g_simple_async_result_get_op_res_gpointer (res);
5135 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
5138 g_simple_async_result_take_error (res, error);
5140 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5144 g_file_real_append_to_async (GFile *file,
5145 GFileCreateFlags flags,
5147 GCancellable *cancellable,
5148 GAsyncReadyCallback callback,
5151 GFileCreateFlags *data;
5152 GSimpleAsyncResult *res;
5154 data = g_new0 (GFileCreateFlags, 1);
5157 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
5158 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5160 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
5161 g_object_unref (res);
5164 static GFileOutputStream *
5165 g_file_real_append_to_finish (GFile *file,
5169 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5172 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
5174 if (g_simple_async_result_propagate_error (simple, error))
5177 op = g_simple_async_result_get_op_res_gpointer (simple);
5179 return g_object_ref (op);
5185 create_async_thread (GSimpleAsyncResult *res,
5187 GCancellable *cancellable)
5190 GFileCreateFlags *data;
5191 GFileOutputStream *stream;
5192 GError *error = NULL;
5194 iface = G_FILE_GET_IFACE (object);
5196 data = g_simple_async_result_get_op_res_gpointer (res);
5198 stream = iface->create (G_FILE (object), *data, cancellable, &error);
5201 g_simple_async_result_take_error (res, error);
5203 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5207 g_file_real_create_async (GFile *file,
5208 GFileCreateFlags flags,
5210 GCancellable *cancellable,
5211 GAsyncReadyCallback callback,
5214 GFileCreateFlags *data;
5215 GSimpleAsyncResult *res;
5217 data = g_new0 (GFileCreateFlags, 1);
5220 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
5221 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5223 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
5224 g_object_unref (res);
5227 static GFileOutputStream *
5228 g_file_real_create_finish (GFile *file,
5232 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5235 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
5237 if (g_simple_async_result_propagate_error (simple, error))
5240 op = g_simple_async_result_get_op_res_gpointer (simple);
5242 return g_object_ref (op);
5248 GFileOutputStream *stream;
5250 gboolean make_backup;
5251 GFileCreateFlags flags;
5255 replace_async_data_free (ReplaceAsyncData *data)
5258 g_object_unref (data->stream);
5259 g_free (data->etag);
5264 replace_async_thread (GSimpleAsyncResult *res,
5266 GCancellable *cancellable)
5269 GFileOutputStream *stream;
5270 GError *error = NULL;
5271 ReplaceAsyncData *data;
5273 iface = G_FILE_GET_IFACE (object);
5275 data = g_simple_async_result_get_op_res_gpointer (res);
5277 stream = iface->replace (G_FILE (object),
5285 g_simple_async_result_take_error (res, error);
5287 data->stream = stream;
5291 g_file_real_replace_async (GFile *file,
5293 gboolean make_backup,
5294 GFileCreateFlags flags,
5296 GCancellable *cancellable,
5297 GAsyncReadyCallback callback,
5300 GSimpleAsyncResult *res;
5301 ReplaceAsyncData *data;
5303 data = g_new0 (ReplaceAsyncData, 1);
5304 data->etag = g_strdup (etag);
5305 data->make_backup = make_backup;
5306 data->flags = flags;
5308 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
5309 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
5311 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
5312 g_object_unref (res);
5315 static GFileOutputStream *
5316 g_file_real_replace_finish (GFile *file,
5320 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5321 ReplaceAsyncData *data;
5323 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
5325 if (g_simple_async_result_propagate_error (simple, error))
5328 data = g_simple_async_result_get_op_res_gpointer (simple);
5330 return g_object_ref (data->stream);
5336 delete_async_thread (GSimpleAsyncResult *res,
5338 GCancellable *cancellable)
5341 GError *error = NULL;
5343 iface = G_FILE_GET_IFACE (object);
5345 if (!iface->delete_file (G_FILE (object),
5348 g_simple_async_result_take_error (res, error);
5352 g_file_real_delete_async (GFile *file,
5354 GCancellable *cancellable,
5355 GAsyncReadyCallback callback,
5358 GSimpleAsyncResult *res;
5360 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_delete_async);
5361 g_simple_async_result_run_in_thread (res, delete_async_thread, io_priority, cancellable);
5362 g_object_unref (res);
5366 g_file_real_delete_finish (GFile *file,
5370 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5372 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_delete_async);
5374 if (g_simple_async_result_propagate_error (simple, error))
5381 open_readwrite_async_thread (GSimpleAsyncResult *res,
5383 GCancellable *cancellable)
5386 GFileIOStream *stream;
5387 GError *error = NULL;
5389 iface = G_FILE_GET_IFACE (object);
5391 if (iface->open_readwrite == NULL)
5393 g_set_error_literal (&error, G_IO_ERROR,
5394 G_IO_ERROR_NOT_SUPPORTED,
5395 _("Operation not supported"));
5397 g_simple_async_result_take_error (res, error);
5402 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5405 g_simple_async_result_take_error (res, error);
5407 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5411 g_file_real_open_readwrite_async (GFile *file,
5413 GCancellable *cancellable,
5414 GAsyncReadyCallback callback,
5417 GSimpleAsyncResult *res;
5419 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_open_readwrite_async);
5421 g_simple_async_result_run_in_thread (res, open_readwrite_async_thread, io_priority, cancellable);
5422 g_object_unref (res);
5425 static GFileIOStream *
5426 g_file_real_open_readwrite_finish (GFile *file,
5430 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5433 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_open_readwrite_async);
5435 if (g_simple_async_result_propagate_error (simple, error))
5438 op = g_simple_async_result_get_op_res_gpointer (simple);
5440 return g_object_ref (op);
5446 create_readwrite_async_thread (GSimpleAsyncResult *res,
5448 GCancellable *cancellable)
5451 GFileCreateFlags *data;
5452 GFileIOStream *stream;
5453 GError *error = NULL;
5455 iface = G_FILE_GET_IFACE (object);
5457 data = g_simple_async_result_get_op_res_gpointer (res);
5459 if (iface->create_readwrite == NULL)
5461 g_set_error_literal (&error, G_IO_ERROR,
5462 G_IO_ERROR_NOT_SUPPORTED,
5463 _("Operation not supported"));
5465 g_simple_async_result_take_error (res, error);
5470 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5473 g_simple_async_result_take_error (res, error);
5475 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5479 g_file_real_create_readwrite_async (GFile *file,
5480 GFileCreateFlags flags,
5482 GCancellable *cancellable,
5483 GAsyncReadyCallback callback,
5486 GFileCreateFlags *data;
5487 GSimpleAsyncResult *res;
5489 data = g_new0 (GFileCreateFlags, 1);
5492 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_readwrite_async);
5493 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5495 g_simple_async_result_run_in_thread (res, create_readwrite_async_thread, io_priority, cancellable);
5496 g_object_unref (res);
5499 static GFileIOStream *
5500 g_file_real_create_readwrite_finish (GFile *file,
5504 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5507 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_readwrite_async);
5509 if (g_simple_async_result_propagate_error (simple, error))
5512 op = g_simple_async_result_get_op_res_gpointer (simple);
5514 return g_object_ref (op);
5520 GFileIOStream *stream;
5522 gboolean make_backup;
5523 GFileCreateFlags flags;
5524 } ReplaceRWAsyncData;
5527 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5530 g_object_unref (data->stream);
5531 g_free (data->etag);
5536 replace_readwrite_async_thread (GSimpleAsyncResult *res,
5538 GCancellable *cancellable)
5541 GFileIOStream *stream;
5542 GError *error = NULL;
5543 ReplaceRWAsyncData *data;
5545 iface = G_FILE_GET_IFACE (object);
5547 data = g_simple_async_result_get_op_res_gpointer (res);
5549 stream = iface->replace_readwrite (G_FILE (object),
5557 g_simple_async_result_take_error (res, error);
5559 data->stream = stream;
5563 g_file_real_replace_readwrite_async (GFile *file,
5565 gboolean make_backup,
5566 GFileCreateFlags flags,
5568 GCancellable *cancellable,
5569 GAsyncReadyCallback callback,
5572 GSimpleAsyncResult *res;
5573 ReplaceRWAsyncData *data;
5575 data = g_new0 (ReplaceRWAsyncData, 1);
5576 data->etag = g_strdup (etag);
5577 data->make_backup = make_backup;
5578 data->flags = flags;
5580 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_readwrite_async);
5581 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_rw_async_data_free);
5583 g_simple_async_result_run_in_thread (res, replace_readwrite_async_thread, io_priority, cancellable);
5584 g_object_unref (res);
5587 static GFileIOStream *
5588 g_file_real_replace_readwrite_finish (GFile *file,
5592 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5593 ReplaceRWAsyncData *data;
5595 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_readwrite_async);
5597 if (g_simple_async_result_propagate_error (simple, error))
5600 data = g_simple_async_result_get_op_res_gpointer (simple);
5602 return g_object_ref (data->stream);
5610 } SetDisplayNameAsyncData;
5613 set_display_name_data_free (SetDisplayNameAsyncData *data)
5615 g_free (data->name);
5617 g_object_unref (data->file);
5622 set_display_name_async_thread (GSimpleAsyncResult *res,
5624 GCancellable *cancellable)
5626 GError *error = NULL;
5627 SetDisplayNameAsyncData *data;
5630 data = g_simple_async_result_get_op_res_gpointer (res);
5632 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
5635 g_simple_async_result_take_error (res, error);
5641 g_file_real_set_display_name_async (GFile *file,
5642 const char *display_name,
5644 GCancellable *cancellable,
5645 GAsyncReadyCallback callback,
5648 GSimpleAsyncResult *res;
5649 SetDisplayNameAsyncData *data;
5651 data = g_new0 (SetDisplayNameAsyncData, 1);
5652 data->name = g_strdup (display_name);
5654 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
5655 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
5657 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
5658 g_object_unref (res);
5662 g_file_real_set_display_name_finish (GFile *file,
5666 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5667 SetDisplayNameAsyncData *data;
5669 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
5671 if (g_simple_async_result_propagate_error (simple, error))
5674 data = g_simple_async_result_get_op_res_gpointer (simple);
5676 return g_object_ref (data->file);
5682 GFileQueryInfoFlags flags;
5689 set_info_data_free (SetInfoAsyncData *data)
5692 g_object_unref (data->info);
5694 g_error_free (data->error);
5699 set_info_async_thread (GSimpleAsyncResult *res,
5701 GCancellable *cancellable)
5703 SetInfoAsyncData *data;
5705 data = g_simple_async_result_get_op_res_gpointer (res);
5708 data->res = g_file_set_attributes_from_info (G_FILE (object),
5716 g_file_real_set_attributes_async (GFile *file,
5718 GFileQueryInfoFlags flags,
5720 GCancellable *cancellable,
5721 GAsyncReadyCallback callback,
5724 GSimpleAsyncResult *res;
5725 SetInfoAsyncData *data;
5727 data = g_new0 (SetInfoAsyncData, 1);
5728 data->info = g_file_info_dup (info);
5729 data->flags = flags;
5731 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
5732 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
5734 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
5735 g_object_unref (res);
5739 g_file_real_set_attributes_finish (GFile *file,
5744 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5745 SetInfoAsyncData *data;
5747 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
5749 data = g_simple_async_result_get_op_res_gpointer (simple);
5752 *info = g_object_ref (data->info);
5754 if (error != NULL && data->error)
5755 *error = g_error_copy (data->error);
5761 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
5763 GCancellable *cancellable)
5765 GError *error = NULL;
5768 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5771 g_simple_async_result_take_error (res, error);
5773 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
5777 g_file_real_find_enclosing_mount_async (GFile *file,
5779 GCancellable *cancellable,
5780 GAsyncReadyCallback callback,
5783 GSimpleAsyncResult *res;
5785 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
5787 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
5788 g_object_unref (res);
5792 g_file_real_find_enclosing_mount_finish (GFile *file,
5796 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5799 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
5801 if (g_simple_async_result_propagate_error (simple, error))
5804 mount = g_simple_async_result_get_op_res_gpointer (simple);
5805 return g_object_ref (mount);
5812 GFileCopyFlags flags;
5813 GFileProgressCallback progress_cb;
5814 gpointer progress_cb_data;
5815 GIOSchedulerJob *job;
5819 copy_async_data_free (CopyAsyncData *data)
5821 g_object_unref (data->source);
5822 g_object_unref (data->destination);
5827 CopyAsyncData *data;
5828 goffset current_num_bytes;
5829 goffset total_num_bytes;
5833 copy_async_progress_in_main (gpointer user_data)
5835 ProgressData *progress = user_data;
5836 CopyAsyncData *data = progress->data;
5838 data->progress_cb (progress->current_num_bytes,
5839 progress->total_num_bytes,
5840 data->progress_cb_data);
5846 copy_async_progress_callback (goffset current_num_bytes,
5847 goffset total_num_bytes,
5850 CopyAsyncData *data = user_data;
5851 ProgressData *progress;
5853 progress = g_new (ProgressData, 1);
5854 progress->data = data;
5855 progress->current_num_bytes = current_num_bytes;
5856 progress->total_num_bytes = total_num_bytes;
5858 g_io_scheduler_job_send_to_mainloop_async (data->job,
5859 copy_async_progress_in_main,
5865 copy_async_thread (GIOSchedulerJob *job,
5866 GCancellable *cancellable,
5869 GSimpleAsyncResult *res;
5870 CopyAsyncData *data;
5875 data = g_simple_async_result_get_op_res_gpointer (res);
5879 result = g_file_copy (data->source,
5883 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5888 g_simple_async_result_take_error (res, error);
5890 g_simple_async_result_complete_in_idle (res);
5896 g_file_real_copy_async (GFile *source,
5898 GFileCopyFlags flags,
5900 GCancellable *cancellable,
5901 GFileProgressCallback progress_callback,
5902 gpointer progress_callback_data,
5903 GAsyncReadyCallback callback,
5906 GSimpleAsyncResult *res;
5907 CopyAsyncData *data;
5909 data = g_new0 (CopyAsyncData, 1);
5910 data->source = g_object_ref (source);
5911 data->destination = g_object_ref (destination);
5912 data->flags = flags;
5913 data->progress_cb = progress_callback;
5914 data->progress_cb_data = progress_callback_data;
5916 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
5917 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
5919 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
5923 g_file_real_copy_finish (GFile *file,
5927 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5929 if (g_simple_async_result_propagate_error (simple, error))
5936 /********************************************
5937 * Default VFS operations *
5938 ********************************************/
5941 * g_file_new_for_path:
5942 * @path: a string containing a relative or absolute path. The string
5943 * must be encoded in the glib filename encoding.
5945 * Constructs a #GFile for a given path. This operation never
5946 * fails, but the returned object might not support any I/O
5947 * operation if @path is malformed.
5949 * Returns: (transfer full): a new #GFile for the given @path.
5950 * Free the returned object with g_object_unref().
5953 g_file_new_for_path (const char *path)
5955 g_return_val_if_fail (path != NULL, NULL);
5957 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5961 * g_file_new_for_uri:
5962 * @uri: a UTF8 string containing a URI.
5964 * Constructs a #GFile for a given URI. This operation never
5965 * fails, but the returned object might not support any I/O
5966 * operation if @uri is malformed or if the uri type is
5969 * Returns: (transfer full): a new #GFile for the given @uri.
5970 * Free the returned object with g_object_unref().
5973 g_file_new_for_uri (const char *uri)
5975 g_return_val_if_fail (uri != NULL, NULL);
5977 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
5982 * @tmpl: (type filename) (allow-none): Template for the file
5983 * name, as in g_file_open_tmp(), or %NULL for a default template.
5984 * @iostream: (out): on return, a #GFileIOStream for the created file.
5985 * @error: a #GError, or %NULL
5987 * Opens a file in the preferred directory for temporary files (as
5988 * returned by g_get_tmp_dir()) and returns a #GFile and
5989 * #GFileIOStream pointing to it.
5991 * @tmpl should be a string in the GLib file name encoding
5992 * containing a sequence of six 'X' characters, and containing no
5993 * directory components. If it is %NULL, a default template is used.
5995 * Unlike the other #GFile constructors, this will return %NULL if
5996 * a temporary file could not be created.
5998 * Returns: (transfer full): a new #GFile.
5999 * Free the returned object with g_object_unref().
6004 g_file_new_tmp (const char *tmpl,
6005 GFileIOStream **iostream,
6011 GFileOutputStream *output;
6013 g_return_val_if_fail (iostream != NULL, NULL);
6015 fd = g_file_open_tmp (tmpl, &path, error);
6019 file = g_file_new_for_path (path);
6021 output = _g_local_file_output_stream_new (fd);
6022 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6024 g_object_unref (output);
6031 * g_file_parse_name:
6032 * @parse_name: a file name or path to be parsed.
6034 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
6035 * This operation never fails, but the returned object might not support any I/O
6036 * operation if the @parse_name cannot be parsed.
6038 * Returns: (transfer full): a new #GFile.
6041 g_file_parse_name (const char *parse_name)
6043 g_return_val_if_fail (parse_name != NULL, NULL);
6045 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6049 is_valid_scheme_character (char c)
6051 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6054 /* Following RFC 2396, valid schemes are built like:
6055 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6058 has_valid_scheme (const char *uri)
6064 if (!g_ascii_isalpha (*p))
6069 } while (is_valid_scheme_character (*p));
6075 * g_file_new_for_commandline_arg:
6076 * @arg: a command line string.
6078 * Creates a #GFile with the given argument from the command line. The value of
6079 * @arg can be either a URI, an absolute path or a relative path resolved
6080 * relative to the current working directory.
6081 * This operation never fails, but the returned object might not support any
6082 * I/O operation if @arg points to a malformed path.
6084 * Returns: (transfer full): a new #GFile.
6085 * Free the returned object with g_object_unref().
6088 g_file_new_for_commandline_arg (const char *arg)
6094 g_return_val_if_fail (arg != NULL, NULL);
6096 if (g_path_is_absolute (arg))
6097 return g_file_new_for_path (arg);
6099 if (has_valid_scheme (arg))
6100 return g_file_new_for_uri (arg);
6102 current_dir = g_get_current_dir ();
6103 filename = g_build_filename (current_dir, arg, NULL);
6104 g_free (current_dir);
6106 file = g_file_new_for_path (filename);
6113 * g_file_mount_enclosing_volume:
6114 * @location: input #GFile.
6115 * @flags: flags affecting the operation
6116 * @mount_operation: (allow-none): a #GMountOperation or %NULL to avoid user interaction.
6117 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
6118 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
6119 * @user_data: the data to pass to callback function
6121 * Starts a @mount_operation, mounting the volume that contains the file @location.
6123 * When this operation has completed, @callback will be called with
6124 * @user_user data, and the operation can be finalized with
6125 * g_file_mount_enclosing_volume_finish().
6127 * If @cancellable is not %NULL, then the operation can be cancelled by
6128 * triggering the cancellable object from another thread. If the operation
6129 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6132 g_file_mount_enclosing_volume (GFile *location,
6133 GMountMountFlags flags,
6134 GMountOperation *mount_operation,
6135 GCancellable *cancellable,
6136 GAsyncReadyCallback callback,
6141 g_return_if_fail (G_IS_FILE (location));
6143 iface = G_FILE_GET_IFACE (location);
6145 if (iface->mount_enclosing_volume == NULL)
6147 g_simple_async_report_error_in_idle (G_OBJECT (location),
6148 callback, user_data,
6149 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6150 _("volume doesn't implement mount"));
6155 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6160 * g_file_mount_enclosing_volume_finish:
6161 * @location: input #GFile.
6162 * @result: a #GAsyncResult.
6163 * @error: a #GError, or %NULL
6165 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6167 * Returns: %TRUE if successful. If an error
6168 * has occurred, this function will return %FALSE and set @error
6169 * appropriately if present.
6172 g_file_mount_enclosing_volume_finish (GFile *location,
6173 GAsyncResult *result,
6178 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6179 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6181 if (g_async_result_legacy_propagate_error (result, error))
6184 iface = G_FILE_GET_IFACE (location);
6186 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6189 /********************************************
6190 * Utility functions *
6191 ********************************************/
6194 * g_file_query_default_handler:
6195 * @file: a #GFile to open.
6196 * @cancellable: optional #GCancellable object, %NULL to ignore.
6197 * @error: a #GError, or %NULL
6199 * Returns the #GAppInfo that is registered as the default
6200 * application to handle the file specified by @file.
6202 * If @cancellable is not %NULL, then the operation can be cancelled by
6203 * triggering the cancellable object from another thread. If the operation
6204 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6206 * Returns: (transfer full): a #GAppInfo if the handle was found, %NULL if there were errors.
6207 * When you are done with it, release it with g_object_unref()
6210 g_file_query_default_handler (GFile *file,
6211 GCancellable *cancellable,
6215 const char *content_type;
6220 uri_scheme = g_file_get_uri_scheme (file);
6221 if (uri_scheme && uri_scheme[0] != '\0')
6223 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6224 g_free (uri_scheme);
6226 if (appinfo != NULL)
6230 info = g_file_query_info (file,
6231 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6240 content_type = g_file_info_get_content_type (info);
6243 /* Don't use is_native(), as we want to support fuse paths if available */
6244 path = g_file_get_path (file);
6245 appinfo = g_app_info_get_default_for_type (content_type,
6250 g_object_unref (info);
6252 if (appinfo != NULL)
6255 g_set_error_literal (error, G_IO_ERROR,
6256 G_IO_ERROR_NOT_SUPPORTED,
6257 _("No application is registered as handling this file"));
6263 #define GET_CONTENT_BLOCK_SIZE 8192
6266 * g_file_load_contents:
6267 * @file: input #GFile.
6268 * @cancellable: optional #GCancellable object, %NULL to ignore.
6269 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file.
6270 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6271 * or %NULL if the length is not needed
6272 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6273 * or %NULL if the entity tag is not needed
6274 * @error: a #GError, or %NULL
6276 * Loads the content of the file into memory. The data is always
6277 * zero-terminated, but this is not included in the resultant @length.
6278 * The returned @content should be freed with g_free() when no longer
6281 * If @cancellable is not %NULL, then the operation can be cancelled by
6282 * triggering the cancellable object from another thread. If the operation
6283 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6285 * Returns: %TRUE if the @file's contents were successfully loaded.
6286 * %FALSE if there were errors.
6289 g_file_load_contents (GFile *file,
6290 GCancellable *cancellable,
6296 GFileInputStream *in;
6297 GByteArray *content;
6302 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6303 g_return_val_if_fail (contents != NULL, FALSE);
6305 in = g_file_read (file, cancellable, error);
6309 content = g_byte_array_new ();
6312 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6313 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6314 content->data + pos,
6315 GET_CONTENT_BLOCK_SIZE,
6316 cancellable, error)) > 0)
6319 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6326 info = g_file_input_stream_query_info (in,
6327 G_FILE_ATTRIBUTE_ETAG_VALUE,
6332 *etag_out = g_strdup (g_file_info_get_etag (info));
6333 g_object_unref (info);
6337 /* Ignore errors on close */
6338 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6339 g_object_unref (in);
6343 /* error is set already */
6344 g_byte_array_free (content, TRUE);
6351 /* Zero terminate (we got an extra byte allocated for this */
6352 content->data[pos] = 0;
6354 *contents = (char *)g_byte_array_free (content, FALSE);
6362 GCancellable *cancellable;
6363 GFileReadMoreCallback read_more_callback;
6364 GAsyncReadyCallback callback;
6366 GByteArray *content;
6373 load_contents_data_free (LoadContentsData *data)
6376 g_error_free (data->error);
6377 if (data->cancellable)
6378 g_object_unref (data->cancellable);
6380 g_byte_array_free (data->content, TRUE);
6381 g_free (data->etag);
6382 g_object_unref (data->file);
6387 load_contents_close_callback (GObject *obj,
6388 GAsyncResult *close_res,
6391 GInputStream *stream = G_INPUT_STREAM (obj);
6392 LoadContentsData *data = user_data;
6393 GSimpleAsyncResult *res;
6395 /* Ignore errors here, we're only reading anyway */
6396 g_input_stream_close_finish (stream, close_res, NULL);
6397 g_object_unref (stream);
6399 res = g_simple_async_result_new (G_OBJECT (data->file),
6402 g_file_load_contents_async);
6403 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
6404 g_simple_async_result_complete (res);
6405 g_object_unref (res);
6409 load_contents_fstat_callback (GObject *obj,
6410 GAsyncResult *stat_res,
6413 GInputStream *stream = G_INPUT_STREAM (obj);
6414 LoadContentsData *data = user_data;
6417 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6421 data->etag = g_strdup (g_file_info_get_etag (info));
6422 g_object_unref (info);
6425 g_input_stream_close_async (stream, 0,
6427 load_contents_close_callback, data);
6431 load_contents_read_callback (GObject *obj,
6432 GAsyncResult *read_res,
6435 GInputStream *stream = G_INPUT_STREAM (obj);
6436 LoadContentsData *data = user_data;
6437 GError *error = NULL;
6440 read_size = g_input_stream_read_finish (stream, read_res, &error);
6444 /* Error or EOF, close the file */
6445 data->error = error;
6446 g_input_stream_close_async (stream, 0,
6448 load_contents_close_callback, data);
6450 else if (read_size == 0)
6452 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6453 G_FILE_ATTRIBUTE_ETAG_VALUE,
6456 load_contents_fstat_callback,
6459 else if (read_size > 0)
6461 data->pos += read_size;
6463 g_byte_array_set_size (data->content,
6464 data->pos + GET_CONTENT_BLOCK_SIZE);
6467 if (data->read_more_callback &&
6468 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
6469 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6470 G_FILE_ATTRIBUTE_ETAG_VALUE,
6473 load_contents_fstat_callback,
6476 g_input_stream_read_async (stream,
6477 data->content->data + data->pos,
6478 GET_CONTENT_BLOCK_SIZE,
6481 load_contents_read_callback,
6487 load_contents_open_callback (GObject *obj,
6488 GAsyncResult *open_res,
6491 GFile *file = G_FILE (obj);
6492 GFileInputStream *stream;
6493 LoadContentsData *data = user_data;
6494 GError *error = NULL;
6495 GSimpleAsyncResult *res;
6497 stream = g_file_read_finish (file, open_res, &error);
6501 g_byte_array_set_size (data->content,
6502 data->pos + GET_CONTENT_BLOCK_SIZE);
6503 g_input_stream_read_async (G_INPUT_STREAM (stream),
6504 data->content->data + data->pos,
6505 GET_CONTENT_BLOCK_SIZE,
6508 load_contents_read_callback,
6514 res = g_simple_async_result_new_take_error (G_OBJECT (data->file),
6518 g_simple_async_result_complete (res);
6519 load_contents_data_free (data);
6520 g_object_unref (res);
6525 * g_file_load_partial_contents_async: (skip)
6526 * @file: input #GFile.
6527 * @cancellable: optional #GCancellable object, %NULL to ignore.
6528 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
6529 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6530 * @user_data: the data to pass to the callback functions.
6532 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
6533 * used to stop reading from the file when appropriate, else this function
6534 * will behave exactly as g_file_load_contents_async(). This operation
6535 * can be finished by g_file_load_partial_contents_finish().
6537 * Users of this function should be aware that @user_data is passed to
6538 * both the @read_more_callback and the @callback.
6540 * If @cancellable is not %NULL, then the operation can be cancelled by
6541 * triggering the cancellable object from another thread. If the operation
6542 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6545 g_file_load_partial_contents_async (GFile *file,
6546 GCancellable *cancellable,
6547 GFileReadMoreCallback read_more_callback,
6548 GAsyncReadyCallback callback,
6551 LoadContentsData *data;
6553 g_return_if_fail (G_IS_FILE (file));
6555 data = g_new0 (LoadContentsData, 1);
6558 data->cancellable = g_object_ref (cancellable);
6559 data->read_more_callback = read_more_callback;
6560 data->callback = callback;
6561 data->user_data = user_data;
6562 data->content = g_byte_array_new ();
6563 data->file = g_object_ref (file);
6565 g_file_read_async (file,
6568 load_contents_open_callback,
6573 * g_file_load_partial_contents_finish:
6574 * @file: input #GFile.
6575 * @res: a #GAsyncResult.
6576 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file.
6577 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6578 * or %NULL if the length is not needed
6579 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6580 * or %NULL if the entity tag is not needed
6581 * @error: a #GError, or %NULL
6583 * Finishes an asynchronous partial load operation that was started
6584 * with g_file_load_partial_contents_async(). The data is always
6585 * zero-terminated, but this is not included in the resultant @length.
6586 * The returned @content should be freed with g_free() when no longer
6589 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6590 * present, it will be set appropriately.
6593 g_file_load_partial_contents_finish (GFile *file,
6600 GSimpleAsyncResult *simple;
6601 LoadContentsData *data;
6603 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6604 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6605 g_return_val_if_fail (contents != NULL, FALSE);
6607 simple = G_SIMPLE_ASYNC_RESULT (res);
6609 if (g_simple_async_result_propagate_error (simple, error))
6612 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
6614 data = g_simple_async_result_get_op_res_gpointer (simple);
6618 g_propagate_error (error, data->error);
6627 *length = data->pos;
6631 *etag_out = data->etag;
6635 /* Zero terminate */
6636 g_byte_array_set_size (data->content, data->pos + 1);
6637 data->content->data[data->pos] = 0;
6639 *contents = (char *)g_byte_array_free (data->content, FALSE);
6640 data->content = NULL;
6646 * g_file_load_contents_async:
6647 * @file: input #GFile.
6648 * @cancellable: optional #GCancellable object, %NULL to ignore.
6649 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6650 * @user_data: the data to pass to callback function
6652 * Starts an asynchronous load of the @file's contents.
6654 * For more details, see g_file_load_contents() which is
6655 * the synchronous version of this call.
6657 * When the load operation has completed, @callback will be called
6658 * with @user data. To finish the operation, call
6659 * g_file_load_contents_finish() with the #GAsyncResult returned by
6662 * If @cancellable is not %NULL, then the operation can be cancelled by
6663 * triggering the cancellable object from another thread. If the operation
6664 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6667 g_file_load_contents_async (GFile *file,
6668 GCancellable *cancellable,
6669 GAsyncReadyCallback callback,
6672 g_file_load_partial_contents_async (file,
6675 callback, user_data);
6679 * g_file_load_contents_finish:
6680 * @file: input #GFile.
6681 * @res: a #GAsyncResult.
6682 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file.
6683 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6684 * or %NULL if the length is not needed
6685 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6686 * or %NULL if the entity tag is not needed
6687 * @error: a #GError, or %NULL
6689 * Finishes an asynchronous load of the @file's contents.
6690 * The contents are placed in @contents, and @length is set to the
6691 * size of the @contents string. The @content should be freed with
6692 * g_free() when no longer needed. If @etag_out is present, it will be
6693 * set to the new entity tag for the @file.
6695 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6696 * present, it will be set appropriately.
6699 g_file_load_contents_finish (GFile *file,
6706 return g_file_load_partial_contents_finish (file,
6715 * g_file_replace_contents:
6716 * @file: input #GFile.
6717 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file.
6718 * @length: the length of @contents in bytes.
6719 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6720 * for the document, or %NULL
6721 * @make_backup: %TRUE if a backup should be created.
6722 * @flags: a set of #GFileCreateFlags.
6723 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6724 * for the document. This should be freed with g_free() when no longer
6726 * @cancellable: optional #GCancellable object, %NULL to ignore.
6727 * @error: a #GError, or %NULL
6729 * Replaces the contents of @file with @contents of @length bytes.
6731 * If @etag is specified (not %NULL) any existing file must have that etag, or
6732 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
6734 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
6736 * If @cancellable is not %NULL, then the operation can be cancelled by
6737 * triggering the cancellable object from another thread. If the operation
6738 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6740 * The returned @new_etag can be used to verify that the file hasn't changed the
6741 * next time it is saved over.
6743 * Returns: %TRUE if successful. If an error
6744 * has occurred, this function will return %FALSE and set @error
6745 * appropriately if present.
6748 g_file_replace_contents (GFile *file,
6749 const char *contents,
6752 gboolean make_backup,
6753 GFileCreateFlags flags,
6755 GCancellable *cancellable,
6758 GFileOutputStream *out;
6759 gsize pos, remainder;
6763 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6764 g_return_val_if_fail (contents != NULL, FALSE);
6766 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6772 while (remainder > 0 &&
6773 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6775 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6783 if (remainder > 0 && res < 0)
6785 /* Ignore errors on close */
6786 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6787 g_object_unref (out);
6789 /* error is set already */
6793 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6796 *new_etag = g_file_output_stream_get_etag (out);
6798 g_object_unref (out);
6806 GCancellable *cancellable;
6807 GAsyncReadyCallback callback;
6809 const char *content;
6813 } ReplaceContentsData;
6816 replace_contents_data_free (ReplaceContentsData *data)
6819 g_error_free (data->error);
6820 if (data->cancellable)
6821 g_object_unref (data->cancellable);
6822 g_object_unref (data->file);
6823 g_free (data->etag);
6828 replace_contents_close_callback (GObject *obj,
6829 GAsyncResult *close_res,
6832 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6833 ReplaceContentsData *data = user_data;
6834 GSimpleAsyncResult *res;
6836 /* Ignore errors here, we're only reading anyway */
6837 g_output_stream_close_finish (stream, close_res, NULL);
6838 g_object_unref (stream);
6840 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6842 res = g_simple_async_result_new (G_OBJECT (data->file),
6845 g_file_replace_contents_async);
6846 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
6847 g_simple_async_result_complete (res);
6848 g_object_unref (res);
6852 replace_contents_write_callback (GObject *obj,
6853 GAsyncResult *read_res,
6856 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6857 ReplaceContentsData *data = user_data;
6858 GError *error = NULL;
6861 write_size = g_output_stream_write_finish (stream, read_res, &error);
6863 if (write_size <= 0)
6865 /* Error or EOF, close the file */
6867 data->error = error;
6868 g_output_stream_close_async (stream, 0,
6870 replace_contents_close_callback, data);
6872 else if (write_size > 0)
6874 data->pos += write_size;
6876 if (data->pos >= data->length)
6877 g_output_stream_close_async (stream, 0,
6879 replace_contents_close_callback, data);
6881 g_output_stream_write_async (stream,
6882 data->content + data->pos,
6883 data->length - data->pos,
6886 replace_contents_write_callback,
6892 replace_contents_open_callback (GObject *obj,
6893 GAsyncResult *open_res,
6896 GFile *file = G_FILE (obj);
6897 GFileOutputStream *stream;
6898 ReplaceContentsData *data = user_data;
6899 GError *error = NULL;
6900 GSimpleAsyncResult *res;
6902 stream = g_file_replace_finish (file, open_res, &error);
6906 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6907 data->content + data->pos,
6908 data->length - data->pos,
6911 replace_contents_write_callback,
6917 res = g_simple_async_result_new_take_error (G_OBJECT (data->file),
6921 g_simple_async_result_complete (res);
6922 replace_contents_data_free (data);
6923 g_object_unref (res);
6928 * g_file_replace_contents_async:
6929 * @file: input #GFile.
6930 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with.
6931 * @length: the length of @contents in bytes.
6932 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6933 * @make_backup: %TRUE if a backup should be created.
6934 * @flags: a set of #GFileCreateFlags.
6935 * @cancellable: optional #GCancellable object, %NULL to ignore.
6936 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6937 * @user_data: the data to pass to callback function
6939 * Starts an asynchronous replacement of @file with the given
6940 * @contents of @length bytes. @etag will replace the document's
6941 * current entity tag.
6943 * When this operation has completed, @callback will be called with
6944 * @user_user data, and the operation can be finalized with
6945 * g_file_replace_contents_finish().
6947 * If @cancellable is not %NULL, then the operation can be cancelled by
6948 * triggering the cancellable object from another thread. If the operation
6949 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6951 * If @make_backup is %TRUE, this function will attempt to
6952 * make a backup of @file.
6955 g_file_replace_contents_async (GFile *file,
6956 const char *contents,
6959 gboolean make_backup,
6960 GFileCreateFlags flags,
6961 GCancellable *cancellable,
6962 GAsyncReadyCallback callback,
6965 ReplaceContentsData *data;
6967 g_return_if_fail (G_IS_FILE (file));
6968 g_return_if_fail (contents != NULL);
6970 data = g_new0 (ReplaceContentsData, 1);
6973 data->cancellable = g_object_ref (cancellable);
6974 data->callback = callback;
6975 data->user_data = user_data;
6976 data->content = contents;
6977 data->length = length;
6979 data->file = g_object_ref (file);
6981 g_file_replace_async (file,
6987 replace_contents_open_callback,
6992 * g_file_replace_contents_finish:
6993 * @file: input #GFile.
6994 * @res: a #GAsyncResult.
6995 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
6996 * for the document. This should be freed with g_free() when it is no
6997 * longer needed, or %NULL
6998 * @error: a #GError, or %NULL
7000 * Finishes an asynchronous replace of the given @file. See
7001 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7002 * tag for the document, if present.
7004 * Returns: %TRUE on success, %FALSE on failure.
7007 g_file_replace_contents_finish (GFile *file,
7012 GSimpleAsyncResult *simple;
7013 ReplaceContentsData *data;
7015 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7016 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
7018 simple = G_SIMPLE_ASYNC_RESULT (res);
7020 if (g_simple_async_result_propagate_error (simple, error))
7023 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
7025 data = g_simple_async_result_get_op_res_gpointer (simple);
7029 g_propagate_error (error, data->error);
7037 *new_etag = data->etag;
7038 data->etag = NULL; /* Take ownership */
7045 * g_file_start_mountable:
7046 * @file: input #GFile.
7047 * @flags: flags affecting the operation
7048 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
7049 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
7050 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
7051 * @user_data: the data to pass to callback function
7053 * Starts a file of type G_FILE_TYPE_MOUNTABLE.
7054 * Using @start_operation, you can request callbacks when, for instance,
7055 * passwords are needed during authentication.
7057 * If @cancellable is not %NULL, then the operation can be cancelled by
7058 * triggering the cancellable object from another thread. If the operation
7059 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7061 * When the operation is finished, @callback will be called. You can then call
7062 * g_file_mount_mountable_finish() to get the result of the operation.
7067 g_file_start_mountable (GFile *file,
7068 GDriveStartFlags flags,
7069 GMountOperation *start_operation,
7070 GCancellable *cancellable,
7071 GAsyncReadyCallback callback,
7076 g_return_if_fail (G_IS_FILE (file));
7078 iface = G_FILE_GET_IFACE (file);
7080 if (iface->start_mountable == NULL)
7082 g_simple_async_report_error_in_idle (G_OBJECT (file),
7086 G_IO_ERROR_NOT_SUPPORTED,
7087 _("Operation not supported"));
7091 (* iface->start_mountable) (file,
7100 * g_file_start_mountable_finish:
7101 * @file: input #GFile.
7102 * @result: a #GAsyncResult.
7103 * @error: a #GError, or %NULL
7105 * Finishes a start operation. See g_file_start_mountable() for details.
7107 * Finish an asynchronous start operation that was started
7108 * with g_file_start_mountable().
7110 * Returns: %TRUE if the operation finished successfully. %FALSE
7116 g_file_start_mountable_finish (GFile *file,
7117 GAsyncResult *result,
7122 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7123 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7125 if (g_async_result_legacy_propagate_error (result, error))
7128 iface = G_FILE_GET_IFACE (file);
7129 return (* iface->start_mountable_finish) (file, result, error);
7133 * g_file_stop_mountable:
7134 * @file: input #GFile.
7135 * @flags: flags affecting the operation
7136 * @mount_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction.
7137 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
7138 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
7139 * @user_data: the data to pass to callback function
7141 * Stops a file of type G_FILE_TYPE_MOUNTABLE.
7143 * If @cancellable is not %NULL, then the operation can be cancelled by
7144 * triggering the cancellable object from another thread. If the operation
7145 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7147 * When the operation is finished, @callback will be called. You can then call
7148 * g_file_stop_mountable_finish() to get the result of the operation.
7153 g_file_stop_mountable (GFile *file,
7154 GMountUnmountFlags flags,
7155 GMountOperation *mount_operation,
7156 GCancellable *cancellable,
7157 GAsyncReadyCallback callback,
7162 g_return_if_fail (G_IS_FILE (file));
7164 iface = G_FILE_GET_IFACE (file);
7166 if (iface->stop_mountable == NULL)
7168 g_simple_async_report_error_in_idle (G_OBJECT (file),
7172 G_IO_ERROR_NOT_SUPPORTED,
7173 _("Operation not supported"));
7177 (* iface->stop_mountable) (file,
7186 * g_file_stop_mountable_finish:
7187 * @file: input #GFile.
7188 * @result: a #GAsyncResult.
7189 * @error: a #GError, or %NULL
7191 * Finishes an stop operation, see g_file_stop_mountable() for details.
7193 * Finish an asynchronous stop operation that was started
7194 * with g_file_stop_mountable().
7196 * Returns: %TRUE if the operation finished successfully. %FALSE
7202 g_file_stop_mountable_finish (GFile *file,
7203 GAsyncResult *result,
7208 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7209 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7211 if (g_async_result_legacy_propagate_error (result, error))
7214 iface = G_FILE_GET_IFACE (file);
7215 return (* iface->stop_mountable_finish) (file, result, error);
7219 * g_file_poll_mountable:
7220 * @file: input #GFile.
7221 * @cancellable: optional #GCancellable object, %NULL to ignore.
7222 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
7223 * @user_data: the data to pass to callback function
7225 * Polls a file of type G_FILE_TYPE_MOUNTABLE.
7227 * If @cancellable is not %NULL, then the operation can be cancelled by
7228 * triggering the cancellable object from another thread. If the operation
7229 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7231 * When the operation is finished, @callback will be called. You can then call
7232 * g_file_mount_mountable_finish() to get the result of the operation.
7237 g_file_poll_mountable (GFile *file,
7238 GCancellable *cancellable,
7239 GAsyncReadyCallback callback,
7244 g_return_if_fail (G_IS_FILE (file));
7246 iface = G_FILE_GET_IFACE (file);
7248 if (iface->poll_mountable == NULL)
7250 g_simple_async_report_error_in_idle (G_OBJECT (file),
7254 G_IO_ERROR_NOT_SUPPORTED,
7255 _("Operation not supported"));
7259 (* iface->poll_mountable) (file,
7266 * g_file_poll_mountable_finish:
7267 * @file: input #GFile.
7268 * @result: a #GAsyncResult.
7269 * @error: a #GError, or %NULL
7271 * Finishes a poll operation. See g_file_poll_mountable() for details.
7273 * Finish an asynchronous poll operation that was polled
7274 * with g_file_poll_mountable().
7276 * Returns: %TRUE if the operation finished successfully. %FALSE
7282 g_file_poll_mountable_finish (GFile *file,
7283 GAsyncResult *result,
7288 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7289 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7291 if (g_async_result_legacy_propagate_error (result, error))
7294 iface = G_FILE_GET_IFACE (file);
7295 return (* iface->poll_mountable_finish) (file, result, error);
7299 * g_file_supports_thread_contexts:
7302 * Checks if @file supports <link
7303 * linkend="g-main-context-push-thread-default-context">thread-default
7304 * contexts</link>. If this returns %FALSE, you cannot perform
7305 * asynchronous operations on @file in a thread that has a
7306 * thread-default context.
7308 * Returns: Whether or not @file supports thread-default contexts.
7313 g_file_supports_thread_contexts (GFile *file)
7317 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7319 iface = G_FILE_GET_IFACE (file);
7320 return iface->supports_thread_contexts;