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.1 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, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <sys/ioctl.h>
28 /* See linux.git/fs/btrfs/ioctl.h */
29 #define BTRFS_IOCTL_MAGIC 0x94
30 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
41 #include <sys/types.h>
44 #include "glib/gstdio.h"
46 #include "glib-unix.h"
50 #include "gfileattribute-priv.h"
51 #include "gfiledescriptorbased.h"
52 #include "gpollfilemonitor.h"
54 #include "gfileinputstream.h"
55 #include "gfileoutputstream.h"
56 #include "glocalfileoutputstream.h"
57 #include "glocalfileiostream.h"
58 #include "glocalfile.h"
59 #include "gcancellable.h"
60 #include "gasyncresult.h"
67 * @short_description: File and Directory Handling
69 * @see_also: #GFileInfo, #GFileEnumerator
71 * #GFile is a high level abstraction for manipulating files on a
72 * virtual file system. #GFiles are lightweight, immutable objects
73 * that do no I/O upon creation. It is necessary to understand that
74 * #GFile objects do not represent files, merely an identifier for a
75 * file. All file content I/O is implemented as streaming operations
76 * (see #GInputStream and #GOutputStream).
78 * To construct a #GFile, you can use:
79 * - g_file_new_for_path() if you have a path.
80 * - g_file_new_for_uri() if you have a URI.
81 * - g_file_new_for_commandline_arg() for a command line argument.
82 * - g_file_new_tmp() to create a temporary file from a template.
83 * - g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().
84 * - g_file_new_build_filename() to create a file from path elements.
86 * One way to think of a #GFile is as an abstraction of a pathname. For
87 * normal files the system pathname is what is stored internally, but as
88 * #GFiles are extensible it could also be something else that corresponds
89 * to a pathname in a userspace implementation of a filesystem.
91 * #GFiles make up hierarchies of directories and files that correspond to
92 * the files on a filesystem. You can move through the file system with
93 * #GFile using g_file_get_parent() to get an identifier for the parent
94 * directory, g_file_get_child() to get a child within a directory,
95 * g_file_resolve_relative_path() to resolve a relative path between two
96 * #GFiles. There can be multiple hierarchies, so you may not end up at
97 * the same root if you repeatedly call g_file_get_parent() on two different
100 * All #GFiles have a basename (get with g_file_get_basename()). These names
101 * are byte strings that are used to identify the file on the filesystem
102 * (relative to its parent directory) and there is no guarantees that they
103 * have any particular charset encoding or even make any sense at all. If
104 * you want to use filenames in a user interface you should use the display
105 * name that you can get by requesting the
106 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
107 * This is guaranteed to be in UTF-8 and can be used in a user interface.
108 * But always store the real basename or the #GFile to use to actually
109 * access the file, because there is no way to go from a display name to
112 * Using #GFile as an identifier has the same weaknesses as using a path
113 * in that there may be multiple aliases for the same file. For instance,
114 * hard or soft links may cause two different #GFiles to refer to the same
115 * file. Other possible causes for aliases are: case insensitive filesystems,
116 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
117 * check if two #GFiles point to the same file you can query for the
118 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
119 * canonicalization of pathnames passed in, so that trivial differences in
120 * the path string used at creation (duplicated slashes, slash at end of
121 * path, "." or ".." path segments, etc) does not create different #GFiles.
123 * Many #GFile operations have both synchronous and asynchronous versions
124 * to suit your application. Asynchronous versions of synchronous functions
125 * simply have _async() appended to their function names. The asynchronous
126 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
127 * the operation, producing a GAsyncResult which is then passed to the
128 * function's matching _finish() operation.
130 * It is highly recommended to use asynchronous calls when running within a
131 * shared main loop, such as in the main thread of an application. This avoids
132 * I/O operations blocking other sources on the main loop from being dispatched.
133 * Synchronous I/O operations should be performed from worker threads. See the
134 * [introduction to asynchronous programming section][async-programming] for
137 * Some #GFile operations almost always take a noticeable amount of time, and
138 * so do not have synchronous analogs. Notable cases include:
139 * - g_file_mount_mountable() to mount a mountable file.
140 * - g_file_unmount_mountable_with_operation() to unmount a mountable file.
141 * - g_file_eject_mountable_with_operation() to eject a mountable file.
143 * ## Entity Tags # {#gfile-etag}
145 * One notable feature of #GFiles are entity tags, or "etags" for
146 * short. Entity tags are somewhat like a more abstract version of the
147 * traditional mtime, and can be used to quickly determine if the file
148 * has been modified from the version on the file system. See the
150 * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
151 * for HTTP Etag headers, which are a very similar concept.
154 static void g_file_real_query_info_async (GFile *file,
155 const char *attributes,
156 GFileQueryInfoFlags flags,
158 GCancellable *cancellable,
159 GAsyncReadyCallback callback,
161 static GFileInfo * g_file_real_query_info_finish (GFile *file,
164 static void g_file_real_query_filesystem_info_async (GFile *file,
165 const char *attributes,
167 GCancellable *cancellable,
168 GAsyncReadyCallback callback,
170 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
173 static void g_file_real_enumerate_children_async (GFile *file,
174 const char *attributes,
175 GFileQueryInfoFlags flags,
177 GCancellable *cancellable,
178 GAsyncReadyCallback callback,
180 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
183 static void g_file_real_read_async (GFile *file,
185 GCancellable *cancellable,
186 GAsyncReadyCallback callback,
188 static GFileInputStream * g_file_real_read_finish (GFile *file,
191 static void g_file_real_append_to_async (GFile *file,
192 GFileCreateFlags flags,
194 GCancellable *cancellable,
195 GAsyncReadyCallback callback,
197 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
200 static void g_file_real_create_async (GFile *file,
201 GFileCreateFlags flags,
203 GCancellable *cancellable,
204 GAsyncReadyCallback callback,
206 static GFileOutputStream *g_file_real_create_finish (GFile *file,
209 static void g_file_real_replace_async (GFile *file,
211 gboolean make_backup,
212 GFileCreateFlags flags,
214 GCancellable *cancellable,
215 GAsyncReadyCallback callback,
217 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
220 static void g_file_real_delete_async (GFile *file,
222 GCancellable *cancellable,
223 GAsyncReadyCallback callback,
225 static gboolean g_file_real_delete_finish (GFile *file,
228 static void g_file_real_trash_async (GFile *file,
230 GCancellable *cancellable,
231 GAsyncReadyCallback callback,
233 static gboolean g_file_real_trash_finish (GFile *file,
236 static void g_file_real_make_directory_async (GFile *file,
238 GCancellable *cancellable,
239 GAsyncReadyCallback callback,
241 static gboolean g_file_real_make_directory_finish (GFile *file,
244 static void g_file_real_open_readwrite_async (GFile *file,
246 GCancellable *cancellable,
247 GAsyncReadyCallback callback,
249 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
252 static void g_file_real_create_readwrite_async (GFile *file,
253 GFileCreateFlags flags,
255 GCancellable *cancellable,
256 GAsyncReadyCallback callback,
258 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
261 static void g_file_real_replace_readwrite_async (GFile *file,
263 gboolean make_backup,
264 GFileCreateFlags flags,
266 GCancellable *cancellable,
267 GAsyncReadyCallback callback,
269 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
272 static gboolean g_file_real_set_attributes_from_info (GFile *file,
274 GFileQueryInfoFlags flags,
275 GCancellable *cancellable,
277 static void g_file_real_set_display_name_async (GFile *file,
278 const char *display_name,
280 GCancellable *cancellable,
281 GAsyncReadyCallback callback,
283 static GFile * g_file_real_set_display_name_finish (GFile *file,
286 static void g_file_real_set_attributes_async (GFile *file,
288 GFileQueryInfoFlags flags,
290 GCancellable *cancellable,
291 GAsyncReadyCallback callback,
293 static gboolean g_file_real_set_attributes_finish (GFile *file,
297 static void g_file_real_find_enclosing_mount_async (GFile *file,
299 GCancellable *cancellable,
300 GAsyncReadyCallback callback,
302 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
305 static void g_file_real_copy_async (GFile *source,
307 GFileCopyFlags flags,
309 GCancellable *cancellable,
310 GFileProgressCallback progress_callback,
311 gpointer progress_callback_data,
312 GAsyncReadyCallback callback,
314 static gboolean g_file_real_copy_finish (GFile *file,
318 static gboolean g_file_real_measure_disk_usage (GFile *file,
319 GFileMeasureFlags flags,
320 GCancellable *cancellable,
321 GFileMeasureProgressCallback progress_callback,
322 gpointer progress_data,
327 static void g_file_real_measure_disk_usage_async (GFile *file,
328 GFileMeasureFlags flags,
330 GCancellable *cancellable,
331 GFileMeasureProgressCallback progress_callback,
332 gpointer progress_data,
333 GAsyncReadyCallback callback,
335 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
336 GAsyncResult *result,
342 typedef GFileIface GFileInterface;
343 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
346 g_file_default_init (GFileIface *iface)
348 iface->enumerate_children_async = g_file_real_enumerate_children_async;
349 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
350 iface->set_display_name_async = g_file_real_set_display_name_async;
351 iface->set_display_name_finish = g_file_real_set_display_name_finish;
352 iface->query_info_async = g_file_real_query_info_async;
353 iface->query_info_finish = g_file_real_query_info_finish;
354 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
355 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
356 iface->set_attributes_async = g_file_real_set_attributes_async;
357 iface->set_attributes_finish = g_file_real_set_attributes_finish;
358 iface->read_async = g_file_real_read_async;
359 iface->read_finish = g_file_real_read_finish;
360 iface->append_to_async = g_file_real_append_to_async;
361 iface->append_to_finish = g_file_real_append_to_finish;
362 iface->create_async = g_file_real_create_async;
363 iface->create_finish = g_file_real_create_finish;
364 iface->replace_async = g_file_real_replace_async;
365 iface->replace_finish = g_file_real_replace_finish;
366 iface->delete_file_async = g_file_real_delete_async;
367 iface->delete_file_finish = g_file_real_delete_finish;
368 iface->trash_async = g_file_real_trash_async;
369 iface->trash_finish = g_file_real_trash_finish;
370 iface->make_directory_async = g_file_real_make_directory_async;
371 iface->make_directory_finish = g_file_real_make_directory_finish;
372 iface->open_readwrite_async = g_file_real_open_readwrite_async;
373 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
374 iface->create_readwrite_async = g_file_real_create_readwrite_async;
375 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
376 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
377 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
378 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
379 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
380 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
381 iface->copy_async = g_file_real_copy_async;
382 iface->copy_finish = g_file_real_copy_finish;
383 iface->measure_disk_usage = g_file_real_measure_disk_usage;
384 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
385 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
391 * @file: input #GFile
393 * Checks to see if a file is native to the platform.
395 * A native file s one expressed in the platform-native filename format,
396 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
397 * as it might be on a locally mounted remote filesystem.
399 * On some systems non-native files may be available using the native
400 * filesystem via a userspace filesystem (FUSE), in these cases this call
401 * will return %FALSE, but g_file_get_path() will still return a native path.
403 * This call does no blocking I/O.
405 * Returns: %TRUE if @file is native
408 g_file_is_native (GFile *file)
412 g_return_val_if_fail (G_IS_FILE (file), FALSE);
414 iface = G_FILE_GET_IFACE (file);
416 return (* iface->is_native) (file);
421 * g_file_has_uri_scheme:
422 * @file: input #GFile
423 * @uri_scheme: a string containing a URI scheme
425 * Checks to see if a #GFile has a given URI scheme.
427 * This call does no blocking I/O.
429 * Returns: %TRUE if #GFile's backend supports the
430 * given URI scheme, %FALSE if URI scheme is %NULL,
431 * not supported, or #GFile is invalid.
434 g_file_has_uri_scheme (GFile *file,
435 const char *uri_scheme)
439 g_return_val_if_fail (G_IS_FILE (file), FALSE);
440 g_return_val_if_fail (uri_scheme != NULL, FALSE);
442 iface = G_FILE_GET_IFACE (file);
444 return (* iface->has_uri_scheme) (file, uri_scheme);
449 * g_file_get_uri_scheme:
450 * @file: input #GFile
452 * Gets the URI scheme for a #GFile.
453 * RFC 3986 decodes the scheme as:
455 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
457 * Common schemes include "file", "http", "ftp", etc.
459 * This call does no blocking I/O.
461 * Returns: a string containing the URI scheme for the given
462 * #GFile. The returned string should be freed with g_free()
463 * when no longer needed.
466 g_file_get_uri_scheme (GFile *file)
470 g_return_val_if_fail (G_IS_FILE (file), NULL);
472 iface = G_FILE_GET_IFACE (file);
474 return (* iface->get_uri_scheme) (file);
479 * g_file_get_basename:
480 * @file: input #GFile
482 * Gets the base name (the last component of the path) for a given #GFile.
484 * If called for the top level of a system (such as the filesystem root
485 * or a uri like sftp://host/) it will return a single directory separator
486 * (and on Windows, possibly a drive letter).
488 * The base name is a byte string (not UTF-8). It has no defined encoding
489 * or rules other than it may not contain zero bytes. If you want to use
490 * filenames in a user interface you should use the display name that you
491 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
492 * attribute with g_file_query_info().
494 * This call does no blocking I/O.
496 * Returns: (type filename) (nullable): string containing the #GFile's
497 * base name, or %NULL if given #GFile is invalid. The returned string
498 * should be freed with g_free() when no longer needed.
501 g_file_get_basename (GFile *file)
505 g_return_val_if_fail (G_IS_FILE (file), NULL);
507 iface = G_FILE_GET_IFACE (file);
509 return (* iface->get_basename) (file);
514 * @file: input #GFile
516 * Gets the local pathname for #GFile, if one exists. If non-%NULL, this is
517 * guaranteed to be an absolute, canonical path. It might contain symlinks.
519 * This call does no blocking I/O.
521 * Returns: (type filename) (nullable): string containing the #GFile's path,
522 * or %NULL if no such path exists. The returned string should be freed
523 * with g_free() when no longer needed.
526 g_file_get_path (GFile *file)
530 g_return_val_if_fail (G_IS_FILE (file), NULL);
532 iface = G_FILE_GET_IFACE (file);
534 return (* iface->get_path) (file);
537 /* Original commit introducing this in libgsystem:
539 * fileutil: Handle recent: and trash: URIs
541 * The gs_file_get_path_cached() was rather brittle in its handling
542 * of URIs. It would assert() when a GFile didn't have a backing path
543 * (such as when handling trash: or recent: URIs), and didn't know
544 * how to get the target URI for those items either.
546 * Make sure that we do not assert() when a backing path cannot be
547 * found, and handle recent: and trash: URIs.
549 * https://bugzilla.gnome.org/show_bug.cgi?id=708435
552 file_get_target_path (GFile *file)
558 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI, G_FILE_QUERY_INFO_NONE, NULL, NULL);
561 target = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI);
562 path = g_filename_from_uri (target, NULL, NULL);
563 g_object_unref (info);
569 file_peek_path_generic (GFile *file)
572 static GQuark _file_path_quark = 0;
574 if (G_UNLIKELY (_file_path_quark) == 0)
575 _file_path_quark = g_quark_from_static_string ("gio-file-path");
577 /* We need to be careful about threading, as two threads calling
578 * g_file_peek_path() on the same file could race: both would see
579 * (g_object_get_qdata(…) == NULL) to begin with, both would generate and add
580 * the path, but the second thread to add it would end up freeing the path
581 * set by the first thread. The first thread would still return the pointer
582 * to that freed path, though, resulting an a read-after-free. Handle that
583 * with a compare-and-swap loop. The g_object_*_qdata() functions are atomic. */
587 gchar *new_path = NULL;
589 path = g_object_get_qdata ((GObject*)file, _file_path_quark);
594 if (g_file_has_uri_scheme (file, "trash") ||
595 g_file_has_uri_scheme (file, "recent"))
596 new_path = file_get_target_path (file);
598 new_path = g_file_get_path (file);
599 if (new_path == NULL)
602 /* By passing NULL here, we ensure we never replace existing data: */
603 if (g_object_replace_qdata ((GObject *) file, _file_path_quark,
604 NULL, (gpointer) new_path,
605 (GDestroyNotify) g_free, NULL))
616 * @file: input #GFile
618 * Exactly like g_file_get_path(), but caches the result via
619 * g_object_set_qdata_full(). This is useful for example in C
620 * applications which mix `g_file_*` APIs with native ones. It
621 * also avoids an extra duplicated string when possible, so will be
622 * generally more efficient.
624 * This call does no blocking I/O.
626 * Returns: (type filename) (nullable): string containing the #GFile's path,
627 * or %NULL if no such path exists. The returned string is owned by @file.
631 g_file_peek_path (GFile *file)
633 if (G_IS_LOCAL_FILE (file))
634 return _g_local_file_get_filename ((GLocalFile *) file);
635 return file_peek_path_generic (file);
640 * @file: input #GFile
642 * Gets the URI for the @file.
644 * This call does no blocking I/O.
646 * Returns: a string containing the #GFile's URI.
647 * The returned string should be freed with g_free()
648 * when no longer needed.
651 g_file_get_uri (GFile *file)
655 g_return_val_if_fail (G_IS_FILE (file), NULL);
657 iface = G_FILE_GET_IFACE (file);
659 return (* iface->get_uri) (file);
663 * g_file_get_parse_name:
664 * @file: input #GFile
666 * Gets the parse name of the @file.
667 * A parse name is a UTF-8 string that describes the
668 * file such that one can get the #GFile back using
669 * g_file_parse_name().
671 * This is generally used to show the #GFile as a nice
672 * full-pathname kind of string in a user interface,
673 * like in a location entry.
675 * For local files with names that can safely be converted
676 * to UTF-8 the pathname is used, otherwise the IRI is used
677 * (a form of URI that allows UTF-8 characters unescaped).
679 * This call does no blocking I/O.
681 * Returns: a string containing the #GFile's parse name.
682 * The returned string should be freed with g_free()
683 * when no longer needed.
686 g_file_get_parse_name (GFile *file)
690 g_return_val_if_fail (G_IS_FILE (file), NULL);
692 iface = G_FILE_GET_IFACE (file);
694 return (* iface->get_parse_name) (file);
699 * @file: input #GFile
701 * Duplicates a #GFile handle. This operation does not duplicate
702 * the actual file or directory represented by the #GFile; see
703 * g_file_copy() if attempting to copy a file.
705 * This call does no blocking I/O.
707 * Returns: (transfer full): a new #GFile that is a duplicate
708 * of the given #GFile.
711 g_file_dup (GFile *file)
715 g_return_val_if_fail (G_IS_FILE (file), NULL);
717 iface = G_FILE_GET_IFACE (file);
719 return (* iface->dup) (file);
724 * @file: (type GFile): #gconstpointer to a #GFile
726 * Creates a hash value for a #GFile.
728 * This call does no blocking I/O.
731 * Returns: 0 if @file is not a valid #GFile, otherwise an
732 * integer that can be used as hash value for the #GFile.
733 * This function is intended for easily hashing a #GFile to
734 * add to a #GHashTable or similar data structure.
737 g_file_hash (gconstpointer file)
741 g_return_val_if_fail (G_IS_FILE (file), 0);
743 iface = G_FILE_GET_IFACE (file);
745 return (* iface->hash) ((GFile *)file);
750 * @file1: the first #GFile
751 * @file2: the second #GFile
753 * Checks if the two given #GFiles refer to the same file.
755 * Note that two #GFiles that differ can still refer to the same
756 * file on the filesystem due to various forms of filename
759 * This call does no blocking I/O.
761 * Returns: %TRUE if @file1 and @file2 are equal.
764 g_file_equal (GFile *file1,
769 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
770 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
775 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
778 iface = G_FILE_GET_IFACE (file1);
780 return (* iface->equal) (file1, file2);
786 * @file: input #GFile
788 * Gets the parent directory for the @file.
789 * If the @file represents the root directory of the
790 * file system, then %NULL will be returned.
792 * This call does no blocking I/O.
794 * Returns: (nullable) (transfer full): a #GFile structure to the
795 * parent of the given #GFile or %NULL if there is no parent. Free
796 * the returned object with g_object_unref().
799 g_file_get_parent (GFile *file)
803 g_return_val_if_fail (G_IS_FILE (file), NULL);
805 iface = G_FILE_GET_IFACE (file);
807 return (* iface->get_parent) (file);
812 * @file: input #GFile
813 * @parent: (nullable): the parent to check for, or %NULL
815 * Checks if @file has a parent, and optionally, if it is @parent.
817 * If @parent is %NULL then this function returns %TRUE if @file has any
818 * parent at all. If @parent is non-%NULL then %TRUE is only returned
819 * if @file is an immediate child of @parent.
821 * Returns: %TRUE if @file is an immediate child of @parent (or any parent in
822 * the case that @parent is %NULL).
827 g_file_has_parent (GFile *file,
830 GFile *actual_parent;
833 g_return_val_if_fail (G_IS_FILE (file), FALSE);
834 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
836 actual_parent = g_file_get_parent (file);
838 if (actual_parent != NULL)
841 result = g_file_equal (parent, actual_parent);
845 g_object_unref (actual_parent);
855 * @file: input #GFile
856 * @name: (type filename): string containing the child's basename
858 * Gets a child of @file with basename equal to @name.
860 * Note that the file with that specific name might not exist, but
861 * you can still have a #GFile that points to it. You can use this
862 * for instance to create that file.
864 * This call does no blocking I/O.
866 * Returns: (transfer full): a #GFile to a child specified by @name.
867 * Free the returned object with g_object_unref().
870 g_file_get_child (GFile *file,
873 g_return_val_if_fail (G_IS_FILE (file), NULL);
874 g_return_val_if_fail (name != NULL, NULL);
876 return g_file_resolve_relative_path (file, name);
880 * g_file_get_child_for_display_name:
881 * @file: input #GFile
882 * @display_name: string to a possible child
883 * @error: return location for an error
885 * Gets the child of @file for a given @display_name (i.e. a UTF-8
886 * version of the name). If this function fails, it returns %NULL
887 * and @error will be set. This is very useful when constructing a
888 * #GFile for a new file and the user entered the filename in the
889 * user interface, for instance when you select a directory and
890 * type a filename in the file selector.
892 * This call does no blocking I/O.
894 * Returns: (transfer full): a #GFile to the specified child, or
895 * %NULL if the display name couldn't be converted.
896 * Free the returned object with g_object_unref().
899 g_file_get_child_for_display_name (GFile *file,
900 const char *display_name,
905 g_return_val_if_fail (G_IS_FILE (file), NULL);
906 g_return_val_if_fail (display_name != NULL, NULL);
908 iface = G_FILE_GET_IFACE (file);
910 return (* iface->get_child_for_display_name) (file, display_name, error);
915 * @file: input #GFile
916 * @prefix: input #GFile
918 * Checks whether @file has the prefix specified by @prefix.
920 * In other words, if the names of initial elements of @file's
921 * pathname match @prefix. Only full pathname elements are matched,
922 * so a path like /foo is not considered a prefix of /foobar, only
925 * A #GFile is not a prefix of itself. If you want to check for
926 * equality, use g_file_equal().
928 * This call does no I/O, as it works purely on names. As such it can
929 * sometimes return %FALSE even if @file is inside a @prefix (from a
930 * filesystem point of view), because the prefix of @file is an alias
933 * Virtual: prefix_matches
934 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
938 g_file_has_prefix (GFile *file,
943 g_return_val_if_fail (G_IS_FILE (file), FALSE);
944 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
946 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
949 iface = G_FILE_GET_IFACE (file);
951 /* The vtable function differs in arg order since
952 * we're using the old contains_file call
954 return (* iface->prefix_matches) (prefix, file);
958 * g_file_get_relative_path:
959 * @parent: input #GFile
960 * @descendant: input #GFile
962 * Gets the path for @descendant relative to @parent.
964 * This call does no blocking I/O.
966 * Returns: (type filename) (nullable): string with the relative path from
967 * @descendant to @parent, or %NULL if @descendant doesn't have @parent as
968 * prefix. The returned string should be freed with g_free() when
972 g_file_get_relative_path (GFile *parent,
977 g_return_val_if_fail (G_IS_FILE (parent), NULL);
978 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
980 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
983 iface = G_FILE_GET_IFACE (parent);
985 return (* iface->get_relative_path) (parent, descendant);
989 * g_file_resolve_relative_path:
990 * @file: input #GFile
991 * @relative_path: (type filename): a given relative path string
993 * Resolves a relative path for @file to an absolute path.
995 * This call does no blocking I/O.
997 * Returns: (transfer full): #GFile to the resolved path.
998 * %NULL if @relative_path is %NULL or if @file is invalid.
999 * Free the returned object with g_object_unref().
1002 g_file_resolve_relative_path (GFile *file,
1003 const char *relative_path)
1007 g_return_val_if_fail (G_IS_FILE (file), NULL);
1008 g_return_val_if_fail (relative_path != NULL, NULL);
1010 iface = G_FILE_GET_IFACE (file);
1012 return (* iface->resolve_relative_path) (file, relative_path);
1016 * g_file_enumerate_children:
1017 * @file: input #GFile
1018 * @attributes: an attribute query string
1019 * @flags: a set of #GFileQueryInfoFlags
1020 * @cancellable: (nullable): optional #GCancellable object,
1022 * @error: #GError for error reporting
1024 * Gets the requested information about the files in a directory.
1025 * The result is a #GFileEnumerator object that will give out
1026 * #GFileInfo objects for all the files in the directory.
1028 * The @attributes value is a string that specifies the file
1029 * attributes that should be gathered. It is not an error if
1030 * it's not possible to read a particular requested attribute
1031 * from a file - it just won't be set. @attributes should
1032 * be a comma-separated list of attributes or attribute wildcards.
1033 * The wildcard "*" means all attributes, and a wildcard like
1034 * "standard::*" means all attributes in the standard namespace.
1035 * An example attribute query be "standard::*,owner::user".
1036 * The standard attributes are available as defines, like
1037 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1039 * If @cancellable is not %NULL, then the operation can be cancelled
1040 * by triggering the cancellable object from another thread. If the
1041 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1044 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1045 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
1046 * error will be returned. Other errors are possible too.
1048 * Returns: (transfer full): A #GFileEnumerator if successful,
1049 * %NULL on error. Free the returned object with g_object_unref().
1052 g_file_enumerate_children (GFile *file,
1053 const char *attributes,
1054 GFileQueryInfoFlags flags,
1055 GCancellable *cancellable,
1060 g_return_val_if_fail (G_IS_FILE (file), NULL);
1062 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1065 iface = G_FILE_GET_IFACE (file);
1067 if (iface->enumerate_children == NULL)
1069 g_set_error_literal (error, G_IO_ERROR,
1070 G_IO_ERROR_NOT_SUPPORTED,
1071 _("Operation not supported"));
1075 return (* iface->enumerate_children) (file, attributes, flags,
1076 cancellable, error);
1080 * g_file_enumerate_children_async:
1081 * @file: input #GFile
1082 * @attributes: an attribute query string
1083 * @flags: a set of #GFileQueryInfoFlags
1084 * @io_priority: the [I/O priority][io-priority] of the request
1085 * @cancellable: (nullable): optional #GCancellable object,
1087 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1088 * request is satisfied
1089 * @user_data: (closure): the data to pass to callback function
1091 * Asynchronously gets the requested information about the files
1092 * in a directory. The result is a #GFileEnumerator object that will
1093 * give out #GFileInfo objects for all the files in the directory.
1095 * For more details, see g_file_enumerate_children() which is
1096 * the synchronous version of this call.
1098 * When the operation is finished, @callback will be called. You can
1099 * then call g_file_enumerate_children_finish() to get the result of
1103 g_file_enumerate_children_async (GFile *file,
1104 const char *attributes,
1105 GFileQueryInfoFlags flags,
1107 GCancellable *cancellable,
1108 GAsyncReadyCallback callback,
1113 g_return_if_fail (G_IS_FILE (file));
1115 iface = G_FILE_GET_IFACE (file);
1116 (* iface->enumerate_children_async) (file,
1126 * g_file_enumerate_children_finish:
1127 * @file: input #GFile
1128 * @res: a #GAsyncResult
1131 * Finishes an async enumerate children operation.
1132 * See g_file_enumerate_children_async().
1134 * Returns: (transfer full): a #GFileEnumerator or %NULL
1135 * if an error occurred.
1136 * Free the returned object with g_object_unref().
1139 g_file_enumerate_children_finish (GFile *file,
1145 g_return_val_if_fail (G_IS_FILE (file), NULL);
1146 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1148 if (g_async_result_legacy_propagate_error (res, error))
1151 iface = G_FILE_GET_IFACE (file);
1152 return (* iface->enumerate_children_finish) (file, res, error);
1156 * g_file_query_exists:
1157 * @file: input #GFile
1158 * @cancellable: (nullable): optional #GCancellable object,
1161 * Utility function to check if a particular file exists. This is
1162 * implemented using g_file_query_info() and as such does blocking I/O.
1164 * Note that in many cases it is [racy to first check for file existence](https://en.wikipedia.org/wiki/Time_of_check_to_time_of_use)
1165 * and then execute something based on the outcome of that, because the
1166 * file might have been created or removed in between the operations. The
1167 * general approach to handling that is to not check, but just do the
1168 * operation and handle the errors as they come.
1170 * As an example of race-free checking, take the case of reading a file,
1171 * and if it doesn't exist, creating it. There are two racy versions: read
1172 * it, and on error create it; and: check if it exists, if not create it.
1173 * These can both result in two processes creating the file (with perhaps
1174 * a partially written file as the result). The correct approach is to
1175 * always try to create the file with g_file_create() which will either
1176 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1178 * However, in many cases an existence check is useful in a user interface,
1179 * for instance to make a menu item sensitive/insensitive, so that you don't
1180 * have to fool users that something is possible and then just show an error
1181 * dialog. If you do this, you should make sure to also handle the errors
1182 * that can happen due to races when you execute the operation.
1184 * Returns: %TRUE if the file exists (and can be detected without error),
1185 * %FALSE otherwise (or if cancelled).
1188 g_file_query_exists (GFile *file,
1189 GCancellable *cancellable)
1193 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1195 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1196 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1199 g_object_unref (info);
1207 * g_file_query_file_type:
1208 * @file: input #GFile
1209 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1210 * @cancellable: (nullable): optional #GCancellable object,
1213 * Utility function to inspect the #GFileType of a file. This is
1214 * implemented using g_file_query_info() and as such does blocking I/O.
1216 * The primary use case of this method is to check if a file is
1217 * a regular file, directory, or symlink.
1219 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1220 * if the file does not exist
1225 g_file_query_file_type (GFile *file,
1226 GFileQueryInfoFlags flags,
1227 GCancellable *cancellable)
1230 GFileType file_type;
1232 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1233 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1237 file_type = g_file_info_get_file_type (info);
1238 g_object_unref (info);
1241 file_type = G_FILE_TYPE_UNKNOWN;
1247 * g_file_query_info:
1248 * @file: input #GFile
1249 * @attributes: an attribute query string
1250 * @flags: a set of #GFileQueryInfoFlags
1251 * @cancellable: (nullable): optional #GCancellable object,
1255 * Gets the requested information about specified @file.
1256 * The result is a #GFileInfo object that contains key-value
1257 * attributes (such as the type or size of the file).
1259 * The @attributes value is a string that specifies the file
1260 * attributes that should be gathered. It is not an error if
1261 * it's not possible to read a particular requested attribute
1262 * from a file - it just won't be set. @attributes should be a
1263 * comma-separated list of attributes or attribute wildcards.
1264 * The wildcard "*" means all attributes, and a wildcard like
1265 * "standard::*" means all attributes in the standard namespace.
1266 * An example attribute query be "standard::*,owner::user".
1267 * The standard attributes are available as defines, like
1268 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1270 * If @cancellable is not %NULL, then the operation can be cancelled
1271 * by triggering the cancellable object from another thread. If the
1272 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1275 * For symlinks, normally the information about the target of the
1276 * symlink is returned, rather than information about the symlink
1277 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1278 * in @flags the information about the symlink itself will be returned.
1279 * Also, for symlinks that point to non-existing files the information
1280 * about the symlink itself will be returned.
1282 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1283 * returned. Other errors are possible too, and depend on what kind of
1284 * filesystem the file is on.
1286 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1287 * on error. Free the returned object with g_object_unref().
1290 g_file_query_info (GFile *file,
1291 const char *attributes,
1292 GFileQueryInfoFlags flags,
1293 GCancellable *cancellable,
1298 g_return_val_if_fail (G_IS_FILE (file), NULL);
1300 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1303 iface = G_FILE_GET_IFACE (file);
1305 if (iface->query_info == NULL)
1307 g_set_error_literal (error, G_IO_ERROR,
1308 G_IO_ERROR_NOT_SUPPORTED,
1309 _("Operation not supported"));
1313 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1317 * g_file_query_info_async:
1318 * @file: input #GFile
1319 * @attributes: an attribute query string
1320 * @flags: a set of #GFileQueryInfoFlags
1321 * @io_priority: the [I/O priority][io-priority] of the request
1322 * @cancellable: (nullable): optional #GCancellable object,
1324 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1325 * request is satisfied
1326 * @user_data: (closure): the data to pass to callback function
1328 * Asynchronously gets the requested information about specified @file.
1329 * The result is a #GFileInfo object that contains key-value attributes
1330 * (such as type or size for the file).
1332 * For more details, see g_file_query_info() which is the synchronous
1333 * version of this call.
1335 * When the operation is finished, @callback will be called. You can
1336 * then call g_file_query_info_finish() to get the result of the operation.
1339 g_file_query_info_async (GFile *file,
1340 const char *attributes,
1341 GFileQueryInfoFlags flags,
1343 GCancellable *cancellable,
1344 GAsyncReadyCallback callback,
1349 g_return_if_fail (G_IS_FILE (file));
1351 iface = G_FILE_GET_IFACE (file);
1352 (* iface->query_info_async) (file,
1362 * g_file_query_info_finish:
1363 * @file: input #GFile
1364 * @res: a #GAsyncResult
1367 * Finishes an asynchronous file info query.
1368 * See g_file_query_info_async().
1370 * Returns: (transfer full): #GFileInfo for given @file
1371 * or %NULL on error. Free the returned object with
1375 g_file_query_info_finish (GFile *file,
1381 g_return_val_if_fail (G_IS_FILE (file), NULL);
1382 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1384 if (g_async_result_legacy_propagate_error (res, error))
1387 iface = G_FILE_GET_IFACE (file);
1388 return (* iface->query_info_finish) (file, res, error);
1392 * g_file_query_filesystem_info:
1393 * @file: input #GFile
1394 * @attributes: an attribute query string
1395 * @cancellable: (nullable): optional #GCancellable object,
1399 * Similar to g_file_query_info(), but obtains information
1400 * about the filesystem the @file is on, rather than the file itself.
1401 * For instance the amount of space available and the type of
1404 * The @attributes value is a string that specifies the attributes
1405 * that should be gathered. It is not an error if it's not possible
1406 * to read a particular requested attribute from a file - it just
1407 * won't be set. @attributes should be a comma-separated list of
1408 * attributes or attribute wildcards. The wildcard "*" means all
1409 * attributes, and a wildcard like "filesystem::*" means all attributes
1410 * in the filesystem namespace. The standard namespace for filesystem
1411 * attributes is "filesystem". Common attributes of interest are
1412 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1413 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1414 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1416 * If @cancellable is not %NULL, then the operation can be cancelled
1417 * by triggering the cancellable object from another thread. If the
1418 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1421 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1422 * be returned. Other errors are possible too, and depend on what
1423 * kind of filesystem the file is on.
1425 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1426 * Free the returned object with g_object_unref().
1429 g_file_query_filesystem_info (GFile *file,
1430 const char *attributes,
1431 GCancellable *cancellable,
1436 g_return_val_if_fail (G_IS_FILE (file), NULL);
1438 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1441 iface = G_FILE_GET_IFACE (file);
1443 if (iface->query_filesystem_info == NULL)
1445 g_set_error_literal (error, G_IO_ERROR,
1446 G_IO_ERROR_NOT_SUPPORTED,
1447 _("Operation not supported"));
1451 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1455 * g_file_query_filesystem_info_async:
1456 * @file: input #GFile
1457 * @attributes: an attribute query string
1458 * @io_priority: the [I/O priority][io-priority] of the request
1459 * @cancellable: (nullable): optional #GCancellable object,
1461 * @callback: (scope async): a #GAsyncReadyCallback to call
1462 * when the request is satisfied
1463 * @user_data: (closure): the data to pass to callback function
1465 * Asynchronously gets the requested information about the filesystem
1466 * that the specified @file is on. The result is a #GFileInfo object
1467 * that contains key-value attributes (such as type or size for the
1470 * For more details, see g_file_query_filesystem_info() which is the
1471 * synchronous version of this call.
1473 * When the operation is finished, @callback will be called. You can
1474 * then call g_file_query_info_finish() to get the result of the
1478 g_file_query_filesystem_info_async (GFile *file,
1479 const char *attributes,
1481 GCancellable *cancellable,
1482 GAsyncReadyCallback callback,
1487 g_return_if_fail (G_IS_FILE (file));
1489 iface = G_FILE_GET_IFACE (file);
1490 (* iface->query_filesystem_info_async) (file,
1499 * g_file_query_filesystem_info_finish:
1500 * @file: input #GFile
1501 * @res: a #GAsyncResult
1504 * Finishes an asynchronous filesystem info query.
1505 * See g_file_query_filesystem_info_async().
1507 * Returns: (transfer full): #GFileInfo for given @file
1508 * or %NULL on error.
1509 * Free the returned object with g_object_unref().
1512 g_file_query_filesystem_info_finish (GFile *file,
1518 g_return_val_if_fail (G_IS_FILE (file), NULL);
1519 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1521 if (g_async_result_legacy_propagate_error (res, error))
1524 iface = G_FILE_GET_IFACE (file);
1525 return (* iface->query_filesystem_info_finish) (file, res, error);
1529 * g_file_find_enclosing_mount:
1530 * @file: input #GFile
1531 * @cancellable: (nullable): optional #GCancellable object,
1535 * Gets a #GMount for the #GFile.
1537 * If the #GFileIface for @file does not have a mount (e.g.
1538 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1539 * and %NULL will be returned.
1541 * If @cancellable is not %NULL, then the operation can be cancelled by
1542 * triggering the cancellable object from another thread. If the operation
1543 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1545 * Returns: (transfer full): a #GMount where the @file is located
1546 * or %NULL on error.
1547 * Free the returned object with g_object_unref().
1550 g_file_find_enclosing_mount (GFile *file,
1551 GCancellable *cancellable,
1556 g_return_val_if_fail (G_IS_FILE (file), NULL);
1558 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1561 iface = G_FILE_GET_IFACE (file);
1562 if (iface->find_enclosing_mount == NULL)
1565 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1566 /* Translators: This is an error message when
1567 * trying to find the enclosing (user visible)
1568 * mount of a file, but none exists.
1570 _("Containing mount does not exist"));
1574 return (* iface->find_enclosing_mount) (file, cancellable, error);
1578 * g_file_find_enclosing_mount_async:
1580 * @io_priority: the [I/O priority][io-priority] of the request
1581 * @cancellable: (nullable): optional #GCancellable object,
1583 * @callback: (scope async): a #GAsyncReadyCallback to call
1584 * when the request is satisfied
1585 * @user_data: (closure): the data to pass to callback function
1587 * Asynchronously gets the mount for the file.
1589 * For more details, see g_file_find_enclosing_mount() which is
1590 * the synchronous version of this call.
1592 * When the operation is finished, @callback will be called.
1593 * You can then call g_file_find_enclosing_mount_finish() to
1594 * get the result of the operation.
1597 g_file_find_enclosing_mount_async (GFile *file,
1599 GCancellable *cancellable,
1600 GAsyncReadyCallback callback,
1605 g_return_if_fail (G_IS_FILE (file));
1607 iface = G_FILE_GET_IFACE (file);
1608 (* iface->find_enclosing_mount_async) (file,
1616 * g_file_find_enclosing_mount_finish:
1618 * @res: a #GAsyncResult
1621 * Finishes an asynchronous find mount request.
1622 * See g_file_find_enclosing_mount_async().
1624 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1625 * Free the returned object with g_object_unref().
1628 g_file_find_enclosing_mount_finish (GFile *file,
1634 g_return_val_if_fail (G_IS_FILE (file), NULL);
1635 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1637 if (g_async_result_legacy_propagate_error (res, error))
1640 iface = G_FILE_GET_IFACE (file);
1641 return (* iface->find_enclosing_mount_finish) (file, res, error);
1647 * @file: #GFile to read
1648 * @cancellable: (nullable): a #GCancellable
1649 * @error: a #GError, or %NULL
1651 * Opens a file for reading. The result is a #GFileInputStream that
1652 * can be used to read the contents of the file.
1654 * If @cancellable is not %NULL, then the operation can be cancelled by
1655 * triggering the cancellable object from another thread. If the operation
1656 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1658 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1659 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1660 * error will be returned. Other errors are possible too, and depend
1661 * on what kind of filesystem the file is on.
1664 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1665 * Free the returned object with g_object_unref().
1668 g_file_read (GFile *file,
1669 GCancellable *cancellable,
1674 g_return_val_if_fail (G_IS_FILE (file), NULL);
1676 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1679 iface = G_FILE_GET_IFACE (file);
1681 if (iface->read_fn == NULL)
1683 g_set_error_literal (error, G_IO_ERROR,
1684 G_IO_ERROR_NOT_SUPPORTED,
1685 _("Operation not supported"));
1689 return (* iface->read_fn) (file, cancellable, error);
1694 * @file: input #GFile
1695 * @flags: a set of #GFileCreateFlags
1696 * @cancellable: (nullable): optional #GCancellable object,
1698 * @error: a #GError, or %NULL
1700 * Gets an output stream for appending data to the file.
1701 * If the file doesn't already exist it is created.
1703 * By default files created are generally readable by everyone,
1704 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1705 * will be made readable only to the current user, to the level that
1706 * is supported on the target filesystem.
1708 * If @cancellable is not %NULL, then the operation can be cancelled
1709 * by triggering the cancellable object from another thread. If the
1710 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1713 * Some file systems don't allow all file names, and may return an
1714 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1715 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1716 * possible too, and depend on what kind of filesystem the file is on.
1718 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1719 * Free the returned object with g_object_unref().
1722 g_file_append_to (GFile *file,
1723 GFileCreateFlags flags,
1724 GCancellable *cancellable,
1729 g_return_val_if_fail (G_IS_FILE (file), NULL);
1731 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1734 iface = G_FILE_GET_IFACE (file);
1736 if (iface->append_to == NULL)
1738 g_set_error_literal (error, G_IO_ERROR,
1739 G_IO_ERROR_NOT_SUPPORTED,
1740 _("Operation not supported"));
1744 return (* iface->append_to) (file, flags, cancellable, error);
1749 * @file: input #GFile
1750 * @flags: a set of #GFileCreateFlags
1751 * @cancellable: (nullable): optional #GCancellable object,
1753 * @error: a #GError, or %NULL
1755 * Creates a new file and returns an output stream for writing to it.
1756 * The file must not already exist.
1758 * By default files created are generally readable by everyone,
1759 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1760 * will be made readable only to the current user, to the level
1761 * that is supported on the target filesystem.
1763 * If @cancellable is not %NULL, then the operation can be cancelled
1764 * by triggering the cancellable object from another thread. If the
1765 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1768 * If a file or directory with this name already exists the
1769 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1770 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1771 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1772 * be returned. Other errors are possible too, and depend on what kind
1773 * of filesystem the file is on.
1775 * Returns: (transfer full): a #GFileOutputStream for the newly created
1776 * file, or %NULL on error.
1777 * Free the returned object with g_object_unref().
1780 g_file_create (GFile *file,
1781 GFileCreateFlags flags,
1782 GCancellable *cancellable,
1787 g_return_val_if_fail (G_IS_FILE (file), NULL);
1789 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1792 iface = G_FILE_GET_IFACE (file);
1794 if (iface->create == NULL)
1796 g_set_error_literal (error, G_IO_ERROR,
1797 G_IO_ERROR_NOT_SUPPORTED,
1798 _("Operation not supported"));
1802 return (* iface->create) (file, flags, cancellable, error);
1807 * @file: input #GFile
1808 * @etag: (nullable): an optional [entity tag][gfile-etag]
1809 * for the current #GFile, or #NULL to ignore
1810 * @make_backup: %TRUE if a backup should be created
1811 * @flags: a set of #GFileCreateFlags
1812 * @cancellable: (nullable): optional #GCancellable object,
1814 * @error: a #GError, or %NULL
1816 * Returns an output stream for overwriting the file, possibly
1817 * creating a backup copy of the file first. If the file doesn't exist,
1818 * it will be created.
1820 * This will try to replace the file in the safest way possible so
1821 * that any errors during the writing will not affect an already
1822 * existing copy of the file. For instance, for local files it
1823 * may write to a temporary file and then atomically rename over
1824 * the destination when the stream is closed.
1826 * By default files created are generally readable by everyone,
1827 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1828 * will be made readable only to the current user, to the level that
1829 * is supported on the target filesystem.
1831 * If @cancellable is not %NULL, then the operation can be cancelled
1832 * by triggering the cancellable object from another thread. If the
1833 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1836 * If you pass in a non-%NULL @etag value and @file already exists, then
1837 * this value is compared to the current entity tag of the file, and if
1838 * they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
1839 * generally means that the file has been changed since you last read
1840 * it. You can get the new etag from g_file_output_stream_get_etag()
1841 * after you've finished writing and closed the #GFileOutputStream. When
1842 * you load a new file you can use g_file_input_stream_query_info() to
1843 * get the etag of the file.
1845 * If @make_backup is %TRUE, this function will attempt to make a
1846 * backup of the current file before overwriting it. If this fails
1847 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1848 * want to replace anyway, try again with @make_backup set to %FALSE.
1850 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1851 * be returned, and if the file is some other form of non-regular file
1852 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1853 * file systems don't allow all file names, and may return an
1854 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1855 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1856 * possible too, and depend on what kind of filesystem the file is on.
1858 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1859 * Free the returned object with g_object_unref().
1862 g_file_replace (GFile *file,
1864 gboolean make_backup,
1865 GFileCreateFlags flags,
1866 GCancellable *cancellable,
1871 g_return_val_if_fail (G_IS_FILE (file), NULL);
1873 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1876 iface = G_FILE_GET_IFACE (file);
1878 if (iface->replace == NULL)
1880 g_set_error_literal (error, G_IO_ERROR,
1881 G_IO_ERROR_NOT_SUPPORTED,
1882 _("Operation not supported"));
1886 /* Handle empty tag string as NULL in consistent way. */
1887 if (etag && *etag == 0)
1890 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1894 * g_file_open_readwrite:
1895 * @file: #GFile to open
1896 * @cancellable: (nullable): a #GCancellable
1897 * @error: a #GError, or %NULL
1899 * Opens an existing file for reading and writing. The result is
1900 * a #GFileIOStream that can be used to read and write the contents
1903 * If @cancellable is not %NULL, then the operation can be cancelled
1904 * by triggering the cancellable object from another thread. If the
1905 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1908 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1909 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1910 * error will be returned. Other errors are possible too, and depend on
1911 * what kind of filesystem the file is on. Note that in many non-local
1912 * file cases read and write streams are not supported, so make sure you
1913 * really need to do read and write streaming, rather than just opening
1914 * for reading or writing.
1916 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1917 * Free the returned object with g_object_unref().
1922 g_file_open_readwrite (GFile *file,
1923 GCancellable *cancellable,
1928 g_return_val_if_fail (G_IS_FILE (file), NULL);
1930 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1933 iface = G_FILE_GET_IFACE (file);
1935 if (iface->open_readwrite == NULL)
1937 g_set_error_literal (error, G_IO_ERROR,
1938 G_IO_ERROR_NOT_SUPPORTED,
1939 _("Operation not supported"));
1943 return (* iface->open_readwrite) (file, cancellable, error);
1947 * g_file_create_readwrite:
1949 * @flags: a set of #GFileCreateFlags
1950 * @cancellable: (nullable): optional #GCancellable object,
1952 * @error: return location for a #GError, or %NULL
1954 * Creates a new file and returns a stream for reading and
1955 * writing to it. The file must not already exist.
1957 * By default files created are generally readable by everyone,
1958 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1959 * will be made readable only to the current user, to the level
1960 * that is supported on the target filesystem.
1962 * If @cancellable is not %NULL, then the operation can be cancelled
1963 * by triggering the cancellable object from another thread. If the
1964 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1967 * If a file or directory with this name already exists, the
1968 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1969 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1970 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1971 * will be returned. Other errors are possible too, and depend on what
1972 * kind of filesystem the file is on.
1974 * Note that in many non-local file cases read and write streams are
1975 * not supported, so make sure you really need to do read and write
1976 * streaming, rather than just opening for reading or writing.
1978 * Returns: (transfer full): a #GFileIOStream for the newly created
1979 * file, or %NULL on error.
1980 * Free the returned object with g_object_unref().
1985 g_file_create_readwrite (GFile *file,
1986 GFileCreateFlags flags,
1987 GCancellable *cancellable,
1992 g_return_val_if_fail (G_IS_FILE (file), NULL);
1994 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1997 iface = G_FILE_GET_IFACE (file);
1999 if (iface->create_readwrite == NULL)
2001 g_set_error_literal (error, G_IO_ERROR,
2002 G_IO_ERROR_NOT_SUPPORTED,
2003 _("Operation not supported"));
2007 return (* iface->create_readwrite) (file, flags, cancellable, error);
2011 * g_file_replace_readwrite:
2013 * @etag: (nullable): an optional [entity tag][gfile-etag]
2014 * for the current #GFile, or #NULL to ignore
2015 * @make_backup: %TRUE if a backup should be created
2016 * @flags: a set of #GFileCreateFlags
2017 * @cancellable: (nullable): optional #GCancellable object,
2019 * @error: return location for a #GError, or %NULL
2021 * Returns an output stream for overwriting the file in readwrite mode,
2022 * possibly creating a backup copy of the file first. If the file doesn't
2023 * exist, it will be created.
2025 * For details about the behaviour, see g_file_replace() which does the
2026 * same thing but returns an output stream only.
2028 * Note that in many non-local file cases read and write streams are not
2029 * supported, so make sure you really need to do read and write streaming,
2030 * rather than just opening for reading or writing.
2032 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2033 * Free the returned object with g_object_unref().
2038 g_file_replace_readwrite (GFile *file,
2040 gboolean make_backup,
2041 GFileCreateFlags flags,
2042 GCancellable *cancellable,
2047 g_return_val_if_fail (G_IS_FILE (file), NULL);
2049 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2052 iface = G_FILE_GET_IFACE (file);
2054 if (iface->replace_readwrite == NULL)
2056 g_set_error_literal (error, G_IO_ERROR,
2057 G_IO_ERROR_NOT_SUPPORTED,
2058 _("Operation not supported"));
2062 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
2066 * g_file_read_async:
2067 * @file: input #GFile
2068 * @io_priority: the [I/O priority][io-priority] of the request
2069 * @cancellable: (nullable): optional #GCancellable object,
2071 * @callback: (scope async): a #GAsyncReadyCallback to call
2072 * when the request is satisfied
2073 * @user_data: (closure): the data to pass to callback function
2075 * Asynchronously opens @file for reading.
2077 * For more details, see g_file_read() which is
2078 * the synchronous version of this call.
2080 * When the operation is finished, @callback will be called.
2081 * You can then call g_file_read_finish() to get the result
2085 g_file_read_async (GFile *file,
2087 GCancellable *cancellable,
2088 GAsyncReadyCallback callback,
2093 g_return_if_fail (G_IS_FILE (file));
2095 iface = G_FILE_GET_IFACE (file);
2096 (* iface->read_async) (file,
2104 * g_file_read_finish:
2105 * @file: input #GFile
2106 * @res: a #GAsyncResult
2107 * @error: a #GError, or %NULL
2109 * Finishes an asynchronous file read operation started with
2110 * g_file_read_async().
2112 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2113 * Free the returned object with g_object_unref().
2116 g_file_read_finish (GFile *file,
2122 g_return_val_if_fail (G_IS_FILE (file), NULL);
2123 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2125 if (g_async_result_legacy_propagate_error (res, error))
2128 iface = G_FILE_GET_IFACE (file);
2129 return (* iface->read_finish) (file, res, error);
2133 * g_file_append_to_async:
2134 * @file: input #GFile
2135 * @flags: a set of #GFileCreateFlags
2136 * @io_priority: the [I/O priority][io-priority] of the request
2137 * @cancellable: (nullable): optional #GCancellable object,
2139 * @callback: (scope async): a #GAsyncReadyCallback to call
2140 * when the request is satisfied
2141 * @user_data: (closure): the data to pass to callback function
2143 * Asynchronously opens @file for appending.
2145 * For more details, see g_file_append_to() which is
2146 * the synchronous version of this call.
2148 * When the operation is finished, @callback will be called.
2149 * You can then call g_file_append_to_finish() to get the result
2153 g_file_append_to_async (GFile *file,
2154 GFileCreateFlags flags,
2156 GCancellable *cancellable,
2157 GAsyncReadyCallback callback,
2162 g_return_if_fail (G_IS_FILE (file));
2164 iface = G_FILE_GET_IFACE (file);
2165 (* iface->append_to_async) (file,
2174 * g_file_append_to_finish:
2175 * @file: input #GFile
2176 * @res: #GAsyncResult
2177 * @error: a #GError, or %NULL
2179 * Finishes an asynchronous file append operation started with
2180 * g_file_append_to_async().
2182 * Returns: (transfer full): a valid #GFileOutputStream
2183 * or %NULL on error.
2184 * Free the returned object with g_object_unref().
2187 g_file_append_to_finish (GFile *file,
2193 g_return_val_if_fail (G_IS_FILE (file), NULL);
2194 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2196 if (g_async_result_legacy_propagate_error (res, error))
2199 iface = G_FILE_GET_IFACE (file);
2200 return (* iface->append_to_finish) (file, res, error);
2204 * g_file_create_async:
2205 * @file: input #GFile
2206 * @flags: a set of #GFileCreateFlags
2207 * @io_priority: the [I/O priority][io-priority] of the request
2208 * @cancellable: (nullable): optional #GCancellable object,
2210 * @callback: (scope async): a #GAsyncReadyCallback to call
2211 * when the request is satisfied
2212 * @user_data: (closure): the data to pass to callback function
2214 * Asynchronously creates a new file and returns an output stream
2215 * for writing to it. The file must not already exist.
2217 * For more details, see g_file_create() which is
2218 * the synchronous version of this call.
2220 * When the operation is finished, @callback will be called.
2221 * You can then call g_file_create_finish() to get the result
2225 g_file_create_async (GFile *file,
2226 GFileCreateFlags flags,
2228 GCancellable *cancellable,
2229 GAsyncReadyCallback callback,
2234 g_return_if_fail (G_IS_FILE (file));
2236 iface = G_FILE_GET_IFACE (file);
2237 (* iface->create_async) (file,
2246 * g_file_create_finish:
2247 * @file: input #GFile
2248 * @res: a #GAsyncResult
2249 * @error: a #GError, or %NULL
2251 * Finishes an asynchronous file create operation started with
2252 * g_file_create_async().
2254 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2255 * Free the returned object with g_object_unref().
2258 g_file_create_finish (GFile *file,
2264 g_return_val_if_fail (G_IS_FILE (file), NULL);
2265 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2267 if (g_async_result_legacy_propagate_error (res, error))
2270 iface = G_FILE_GET_IFACE (file);
2271 return (* iface->create_finish) (file, res, error);
2275 * g_file_replace_async:
2276 * @file: input #GFile
2277 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2278 * or %NULL to ignore
2279 * @make_backup: %TRUE if a backup should be created
2280 * @flags: a set of #GFileCreateFlags
2281 * @io_priority: the [I/O priority][io-priority] of the request
2282 * @cancellable: (nullable): optional #GCancellable object,
2284 * @callback: (scope async): a #GAsyncReadyCallback to call
2285 * when the request is satisfied
2286 * @user_data: (closure): the data to pass to callback function
2288 * Asynchronously overwrites the file, replacing the contents,
2289 * possibly creating a backup copy of the file first.
2291 * For more details, see g_file_replace() which is
2292 * the synchronous version of this call.
2294 * When the operation is finished, @callback will be called.
2295 * You can then call g_file_replace_finish() to get the result
2299 g_file_replace_async (GFile *file,
2301 gboolean make_backup,
2302 GFileCreateFlags flags,
2304 GCancellable *cancellable,
2305 GAsyncReadyCallback callback,
2310 g_return_if_fail (G_IS_FILE (file));
2312 iface = G_FILE_GET_IFACE (file);
2313 (* iface->replace_async) (file,
2324 * g_file_replace_finish:
2325 * @file: input #GFile
2326 * @res: a #GAsyncResult
2327 * @error: a #GError, or %NULL
2329 * Finishes an asynchronous file replace operation started with
2330 * g_file_replace_async().
2332 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2333 * Free the returned object with g_object_unref().
2336 g_file_replace_finish (GFile *file,
2342 g_return_val_if_fail (G_IS_FILE (file), NULL);
2343 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2345 if (g_async_result_legacy_propagate_error (res, error))
2348 iface = G_FILE_GET_IFACE (file);
2349 return (* iface->replace_finish) (file, res, error);
2353 * g_file_open_readwrite_async
2354 * @file: input #GFile
2355 * @io_priority: the [I/O priority][io-priority] of the request
2356 * @cancellable: (nullable): optional #GCancellable object,
2358 * @callback: (scope async): a #GAsyncReadyCallback to call
2359 * when the request is satisfied
2360 * @user_data: (closure): the data to pass to callback function
2362 * Asynchronously opens @file for reading and writing.
2364 * For more details, see g_file_open_readwrite() which is
2365 * the synchronous version of this call.
2367 * When the operation is finished, @callback will be called.
2368 * You can then call g_file_open_readwrite_finish() to get
2369 * the result of the operation.
2374 g_file_open_readwrite_async (GFile *file,
2376 GCancellable *cancellable,
2377 GAsyncReadyCallback callback,
2382 g_return_if_fail (G_IS_FILE (file));
2384 iface = G_FILE_GET_IFACE (file);
2385 (* iface->open_readwrite_async) (file,
2393 * g_file_open_readwrite_finish:
2394 * @file: input #GFile
2395 * @res: a #GAsyncResult
2396 * @error: a #GError, or %NULL
2398 * Finishes an asynchronous file read operation started with
2399 * g_file_open_readwrite_async().
2401 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2402 * Free the returned object with g_object_unref().
2407 g_file_open_readwrite_finish (GFile *file,
2413 g_return_val_if_fail (G_IS_FILE (file), NULL);
2414 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2416 if (g_async_result_legacy_propagate_error (res, error))
2419 iface = G_FILE_GET_IFACE (file);
2420 return (* iface->open_readwrite_finish) (file, res, error);
2424 * g_file_create_readwrite_async:
2425 * @file: input #GFile
2426 * @flags: a set of #GFileCreateFlags
2427 * @io_priority: the [I/O priority][io-priority] of the request
2428 * @cancellable: (nullable): optional #GCancellable object,
2430 * @callback: (scope async): a #GAsyncReadyCallback to call
2431 * when the request is satisfied
2432 * @user_data: (closure): the data to pass to callback function
2434 * Asynchronously creates a new file and returns a stream
2435 * for reading and writing to it. The file must not already exist.
2437 * For more details, see g_file_create_readwrite() which is
2438 * the synchronous version of this call.
2440 * When the operation is finished, @callback will be called.
2441 * You can then call g_file_create_readwrite_finish() to get
2442 * the result of the operation.
2447 g_file_create_readwrite_async (GFile *file,
2448 GFileCreateFlags flags,
2450 GCancellable *cancellable,
2451 GAsyncReadyCallback callback,
2456 g_return_if_fail (G_IS_FILE (file));
2458 iface = G_FILE_GET_IFACE (file);
2459 (* iface->create_readwrite_async) (file,
2468 * g_file_create_readwrite_finish:
2469 * @file: input #GFile
2470 * @res: a #GAsyncResult
2471 * @error: a #GError, or %NULL
2473 * Finishes an asynchronous file create operation started with
2474 * g_file_create_readwrite_async().
2476 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2477 * Free the returned object with g_object_unref().
2482 g_file_create_readwrite_finish (GFile *file,
2488 g_return_val_if_fail (G_IS_FILE (file), NULL);
2489 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2491 if (g_async_result_legacy_propagate_error (res, error))
2494 iface = G_FILE_GET_IFACE (file);
2495 return (* iface->create_readwrite_finish) (file, res, error);
2499 * g_file_replace_readwrite_async:
2500 * @file: input #GFile
2501 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2502 * or %NULL to ignore
2503 * @make_backup: %TRUE if a backup should be created
2504 * @flags: a set of #GFileCreateFlags
2505 * @io_priority: the [I/O priority][io-priority] of the request
2506 * @cancellable: (nullable): optional #GCancellable object,
2508 * @callback: (scope async): a #GAsyncReadyCallback to call
2509 * when the request is satisfied
2510 * @user_data: (closure): the data to pass to callback function
2512 * Asynchronously overwrites the file in read-write mode,
2513 * replacing the contents, possibly creating a backup copy
2514 * of the file first.
2516 * For more details, see g_file_replace_readwrite() which is
2517 * the synchronous version of this call.
2519 * When the operation is finished, @callback will be called.
2520 * You can then call g_file_replace_readwrite_finish() to get
2521 * the result of the operation.
2526 g_file_replace_readwrite_async (GFile *file,
2528 gboolean make_backup,
2529 GFileCreateFlags flags,
2531 GCancellable *cancellable,
2532 GAsyncReadyCallback callback,
2537 g_return_if_fail (G_IS_FILE (file));
2539 iface = G_FILE_GET_IFACE (file);
2540 (* iface->replace_readwrite_async) (file,
2551 * g_file_replace_readwrite_finish:
2552 * @file: input #GFile
2553 * @res: a #GAsyncResult
2554 * @error: a #GError, or %NULL
2556 * Finishes an asynchronous file replace operation started with
2557 * g_file_replace_readwrite_async().
2559 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2560 * Free the returned object with g_object_unref().
2565 g_file_replace_readwrite_finish (GFile *file,
2571 g_return_val_if_fail (G_IS_FILE (file), NULL);
2572 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2574 if (g_async_result_legacy_propagate_error (res, error))
2577 iface = G_FILE_GET_IFACE (file);
2578 return (* iface->replace_readwrite_finish) (file, res, error);
2582 copy_symlink (GFile *destination,
2583 GFileCopyFlags flags,
2584 GCancellable *cancellable,
2589 gboolean tried_delete;
2591 GFileType file_type;
2593 tried_delete = FALSE;
2597 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2599 /* Maybe it already existed, and we want to overwrite? */
2600 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2601 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2603 g_clear_error (&my_error);
2605 /* Don't overwrite if the destination is a directory */
2606 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2607 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2608 cancellable, &my_error);
2611 file_type = g_file_info_get_file_type (info);
2612 g_object_unref (info);
2614 if (file_type == G_FILE_TYPE_DIRECTORY)
2616 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2617 _("Can’t copy over directory"));
2622 if (!g_file_delete (destination, cancellable, error))
2625 tried_delete = TRUE;
2629 g_propagate_error (error, my_error);
2636 static GFileInputStream *
2637 open_source_for_copy (GFile *source,
2639 GFileCopyFlags flags,
2640 GCancellable *cancellable,
2644 GFileInputStream *ret;
2646 GFileType file_type;
2649 ret = g_file_read (source, cancellable, &my_error);
2653 /* There was an error opening the source, try to set a good error for it: */
2654 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2656 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2657 * as that is less useful to the app. Better check for errors on the
2660 g_error_free (my_error);
2663 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2664 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2665 cancellable, &my_error);
2667 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2669 file_type = g_file_info_get_file_type (info);
2670 g_object_unref (info);
2672 if (flags & G_FILE_COPY_OVERWRITE)
2674 if (file_type == G_FILE_TYPE_DIRECTORY)
2676 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2677 _("Can’t copy directory over directory"));
2680 /* continue to would_recurse error */
2684 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2685 _("Target file exists"));
2691 /* Error getting info from target, return that error
2692 * (except for NOT_FOUND, which is no error here)
2694 g_clear_object (&info);
2695 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2697 g_propagate_error (error, my_error);
2700 g_clear_error (&my_error);
2703 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2704 _("Can’t recursively copy directory"));
2708 g_propagate_error (error, my_error);
2713 should_copy (GFileAttributeInfo *info,
2714 gboolean copy_all_attributes,
2715 gboolean skip_perms)
2717 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2720 if (copy_all_attributes)
2721 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2722 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2726 build_attribute_list_for_copy (GFile *file,
2727 GFileCopyFlags flags,
2728 char **out_attributes,
2729 GCancellable *cancellable,
2732 gboolean ret = FALSE;
2733 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2737 gboolean copy_all_attributes;
2738 gboolean skip_perms;
2740 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2741 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2743 /* Ignore errors here, if the target supports no attributes there is
2744 * nothing to copy. We still honor the cancellable though.
2746 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2747 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2750 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2751 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2754 if (attributes == NULL && namespaces == NULL)
2758 s = g_string_new ("");
2762 for (i = 0; i < attributes->n_infos; i++)
2764 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2769 g_string_append_c (s, ',');
2771 g_string_append (s, attributes->infos[i].name);
2778 for (i = 0; i < namespaces->n_infos; i++)
2780 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2785 g_string_append_c (s, ',');
2787 g_string_append (s, namespaces->infos[i].name);
2788 g_string_append (s, "::*");
2794 *out_attributes = g_string_free (s, FALSE);
2798 g_string_free (s, TRUE);
2800 g_file_attribute_info_list_unref (attributes);
2802 g_file_attribute_info_list_unref (namespaces);
2808 * g_file_copy_attributes:
2809 * @source: a #GFile with attributes
2810 * @destination: a #GFile to copy attributes to
2811 * @flags: a set of #GFileCopyFlags
2812 * @cancellable: (nullable): optional #GCancellable object,
2814 * @error: a #GError, %NULL to ignore
2816 * Copies the file attributes from @source to @destination.
2818 * Normally only a subset of the file attributes are copied,
2819 * those that are copies in a normal file copy operation
2820 * (which for instance does not include e.g. owner). However
2821 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2822 * all the metadata that is possible to copy is copied. This
2823 * is useful when implementing move by copy + delete source.
2825 * Returns: %TRUE if the attributes were copied successfully,
2829 g_file_copy_attributes (GFile *source,
2831 GFileCopyFlags flags,
2832 GCancellable *cancellable,
2835 char *attrs_to_read;
2838 gboolean source_nofollow_symlinks;
2840 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2841 cancellable, error))
2844 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2846 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2847 * we just don't copy it.
2849 info = g_file_query_info (source, attrs_to_read,
2850 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2854 g_free (attrs_to_read);
2859 res = g_file_set_attributes_from_info (destination,
2861 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2864 g_object_unref (info);
2870 /* 256k minus malloc overhead */
2871 #define STREAM_BUFFER_SIZE (1024*256 - 2 *sizeof(gpointer))
2874 copy_stream_with_progress (GInputStream *in,
2877 GCancellable *cancellable,
2878 GFileProgressCallback progress_callback,
2879 gpointer progress_callback_data,
2884 goffset current_size;
2891 /* avoid performance impact of querying total size when it's not needed */
2892 if (progress_callback)
2894 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2895 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2899 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2900 total_size = g_file_info_get_size (info);
2901 g_object_unref (info);
2904 if (total_size == -1)
2906 info = g_file_query_info (source,
2907 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2908 G_FILE_QUERY_INFO_NONE,
2912 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2913 total_size = g_file_info_get_size (info);
2914 g_object_unref (info);
2919 if (total_size == -1)
2922 buffer = g_malloc0 (STREAM_BUFFER_SIZE);
2927 n_read = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, cancellable, error);
2937 current_size += n_read;
2939 res = g_output_stream_write_all (out, buffer, n_read, &n_written, cancellable, error);
2943 if (progress_callback)
2944 progress_callback (current_size, total_size, progress_callback_data);
2948 /* Make sure we send full copied size */
2949 if (progress_callback)
2950 progress_callback (current_size, total_size, progress_callback_data);
2958 do_splice (int fd_in,
2963 long *bytes_transferd,
2969 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2977 else if (errsv == ENOSYS || errsv == EINVAL)
2978 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2979 _("Splice not supported"));
2981 g_set_error (error, G_IO_ERROR,
2982 g_io_error_from_errno (errsv),
2983 _("Error splicing file: %s"),
2984 g_strerror (errsv));
2989 *bytes_transferd = result;
2994 splice_stream_with_progress (GInputStream *in,
2996 GCancellable *cancellable,
2997 GFileProgressCallback progress_callback,
2998 gpointer progress_callback_data,
3001 int buffer[2] = { -1, -1 };
3008 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3009 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3011 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
3015 /* avoid performance impact of querying total size when it's not needed */
3016 if (progress_callback)
3020 if (fstat (fd_in, &sbuf) == 0)
3021 total_size = sbuf.st_size;
3024 if (total_size == -1)
3027 offset_in = offset_out = 0;
3034 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3037 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
3048 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3051 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
3054 n_read -= n_written;
3057 if (progress_callback)
3058 progress_callback (offset_in, total_size, progress_callback_data);
3061 /* Make sure we send full copied size */
3062 if (progress_callback)
3063 progress_callback (offset_in, total_size, progress_callback_data);
3065 if (!g_close (buffer[0], error))
3068 if (!g_close (buffer[1], error))
3072 if (buffer[0] != -1)
3073 (void) g_close (buffer[0], NULL);
3074 if (buffer[1] != -1)
3075 (void) g_close (buffer[1], NULL);
3083 btrfs_reflink_with_progress (GInputStream *in,
3086 GCancellable *cancellable,
3087 GFileProgressCallback progress_callback,
3088 gpointer progress_callback_data,
3091 goffset source_size;
3095 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3096 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3098 if (progress_callback)
3099 source_size = g_file_info_get_size (info);
3101 /* Btrfs clone ioctl properties:
3102 * - Works at the inode level
3103 * - Doesn't work with directories
3104 * - Always follows symlinks (source and destination)
3106 * By the time we get here, *in and *out are both regular files */
3107 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3113 g_set_error_literal (error, G_IO_ERROR,
3114 G_IO_ERROR_NOT_SUPPORTED,
3115 _("Copy (reflink/clone) between mounts is not supported"));
3116 else if (errsv == EINVAL)
3117 g_set_error_literal (error, G_IO_ERROR,
3118 G_IO_ERROR_NOT_SUPPORTED,
3119 _("Copy (reflink/clone) is not supported or invalid"));
3121 /* Most probably something odd happened; retry with fallback */
3122 g_set_error_literal (error, G_IO_ERROR,
3123 G_IO_ERROR_NOT_SUPPORTED,
3124 _("Copy (reflink/clone) is not supported or didn’t work"));
3125 /* We retry with fallback for all error cases because Btrfs is currently
3126 * unstable, and so we can't trust it to do clone properly.
3127 * In addition, any hard errors here would cause the same failure in the
3128 * fallback manual copy as well. */
3132 /* Make sure we send full copied size */
3133 if (progress_callback)
3134 progress_callback (source_size, source_size, progress_callback_data);
3141 file_copy_fallback (GFile *source,
3143 GFileCopyFlags flags,
3144 GCancellable *cancellable,
3145 GFileProgressCallback progress_callback,
3146 gpointer progress_callback_data,
3149 gboolean ret = FALSE;
3150 GFileInputStream *file_in = NULL;
3151 GInputStream *in = NULL;
3152 GOutputStream *out = NULL;
3153 GFileInfo *info = NULL;
3155 char *attrs_to_read;
3156 gboolean do_set_attributes = FALSE;
3158 /* need to know the file type */
3159 info = g_file_query_info (source,
3160 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3161 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3167 /* Maybe copy the symlink? */
3168 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3169 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3171 target = g_file_info_get_symlink_target (info);
3174 if (!copy_symlink (destination, flags, cancellable, target, error))
3180 /* ... else fall back on a regular file copy */
3182 /* Handle "special" files (pipes, device nodes, ...)? */
3183 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3185 /* FIXME: could try to recreate device nodes and others? */
3186 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3187 _("Can’t copy special file"));
3191 /* Everything else should just fall back on a regular copy. */
3193 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3196 in = G_INPUT_STREAM (file_in);
3198 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3199 cancellable, error))
3202 if (attrs_to_read != NULL)
3204 GError *tmp_error = NULL;
3206 /* Ok, ditch the previous lightweight info (on Unix we just
3207 * called lstat()); at this point we gather all the information
3208 * we need about the source from the opened file descriptor.
3210 g_object_unref (info);
3212 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3213 cancellable, &tmp_error);
3216 /* Not all gvfs backends implement query_info_on_read(), we
3217 * can just fall back to the pathname again.
3218 * https://bugzilla.gnome.org/706254
3220 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3222 g_clear_error (&tmp_error);
3223 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3224 cancellable, error);
3228 g_free (attrs_to_read);
3229 g_propagate_error (error, tmp_error);
3233 g_free (attrs_to_read);
3237 do_set_attributes = TRUE;
3240 /* In the local file path, we pass down the source info which
3241 * includes things like unix::mode, to ensure that the target file
3242 * is not created with different permissions from the source file.
3244 * If a future API like g_file_replace_with_info() is added, switch
3245 * this code to use that.
3247 if (G_IS_LOCAL_FILE (destination))
3249 if (flags & G_FILE_COPY_OVERWRITE)
3250 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3252 flags & G_FILE_COPY_BACKUP,
3253 G_FILE_CREATE_REPLACE_DESTINATION,
3255 cancellable, error);
3257 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3259 cancellable, error);
3261 else if (flags & G_FILE_COPY_OVERWRITE)
3263 out = (GOutputStream *)g_file_replace (destination,
3265 flags & G_FILE_COPY_BACKUP,
3266 G_FILE_CREATE_REPLACE_DESTINATION,
3267 cancellable, error);
3271 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3278 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3280 GError *reflink_err = NULL;
3282 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3283 progress_callback, progress_callback_data,
3286 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3288 g_clear_error (&reflink_err);
3292 g_propagate_error (error, reflink_err);
3305 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3307 GError *splice_err = NULL;
3309 if (!splice_stream_with_progress (in, out, cancellable,
3310 progress_callback, progress_callback_data,
3313 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3315 g_clear_error (&splice_err);
3319 g_propagate_error (error, splice_err);
3332 /* A plain read/write loop */
3333 if (!copy_stream_with_progress (in, out, source, cancellable,
3334 progress_callback, progress_callback_data,
3342 /* Don't care about errors in source here */
3343 (void) g_input_stream_close (in, cancellable, NULL);
3344 g_object_unref (in);
3349 /* But write errors on close are bad! */
3350 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3352 g_object_unref (out);
3355 /* Ignore errors here. Failure to copy metadata is not a hard error */
3356 /* TODO: set these attributes /before/ we do the rename() on Unix */
3357 if (ret && do_set_attributes)
3359 g_file_set_attributes_from_info (destination,
3361 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3366 g_clear_object (&info);
3373 * @source: input #GFile
3374 * @destination: destination #GFile
3375 * @flags: set of #GFileCopyFlags
3376 * @cancellable: (nullable): optional #GCancellable object,
3378 * @progress_callback: (nullable) (scope call): function to callback with
3379 * progress information, or %NULL if progress information is not needed
3380 * @progress_callback_data: (closure): user data to pass to @progress_callback
3381 * @error: #GError to set on error, or %NULL
3383 * Copies the file @source to the location specified by @destination.
3384 * Can not handle recursive copies of directories.
3386 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3387 * existing @destination file is overwritten.
3389 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3390 * will be copied as symlinks, otherwise the target of the
3391 * @source symlink will be copied.
3393 * If the flag #G_FILE_COPY_ALL_METADATA is specified then all the metadata
3394 * that is possible to copy is copied, not just the default subset (which,
3395 * for instance, does not include the owner, see #GFileInfo).
3397 * If @cancellable is not %NULL, then the operation can be cancelled by
3398 * triggering the cancellable object from another thread. If the operation
3399 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3401 * If @progress_callback is not %NULL, then the operation can be monitored
3402 * by setting this to a #GFileProgressCallback function.
3403 * @progress_callback_data will be passed to this function. It is guaranteed
3404 * that this callback will be called after all data has been transferred with
3405 * the total number of bytes copied during the operation.
3407 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3408 * is returned, independent on the status of the @destination.
3410 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3411 * the error %G_IO_ERROR_EXISTS is returned.
3413 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3414 * error is returned. If trying to overwrite a directory with a directory the
3415 * %G_IO_ERROR_WOULD_MERGE error is returned.
3417 * If the source is a directory and the target does not exist, or
3418 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3419 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3421 * If you are interested in copying the #GFile object itself (not the on-disk
3422 * file), see g_file_dup().
3424 * Returns: %TRUE on success, %FALSE otherwise.
3427 g_file_copy (GFile *source,
3429 GFileCopyFlags flags,
3430 GCancellable *cancellable,
3431 GFileProgressCallback progress_callback,
3432 gpointer progress_callback_data,
3439 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3440 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3442 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3445 iface = G_FILE_GET_IFACE (destination);
3449 res = (* iface->copy) (source, destination,
3451 progress_callback, progress_callback_data,
3457 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3459 g_propagate_error (error, my_error);
3463 g_clear_error (&my_error);
3466 /* If the types are different, and the destination method failed
3467 * also try the source method
3469 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3471 iface = G_FILE_GET_IFACE (source);
3476 res = (* iface->copy) (source, destination,
3478 progress_callback, progress_callback_data,
3484 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3486 g_propagate_error (error, my_error);
3490 g_clear_error (&my_error);
3494 return file_copy_fallback (source, destination, flags, cancellable,
3495 progress_callback, progress_callback_data,
3500 * g_file_copy_async:
3501 * @source: input #GFile
3502 * @destination: destination #GFile
3503 * @flags: set of #GFileCopyFlags
3504 * @io_priority: the [I/O priority][io-priority] of the request
3505 * @cancellable: (nullable): optional #GCancellable object,
3507 * @progress_callback: (nullable) (scope notified): function to callback with progress
3508 * information, or %NULL if progress information is not needed
3509 * @progress_callback_data: (closure progress_callback) (nullable): user data to pass to @progress_callback
3510 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
3511 * @user_data: (closure callback): the data to pass to callback function
3513 * Copies the file @source to the location specified by @destination
3514 * asynchronously. For details of the behaviour, see g_file_copy().
3516 * If @progress_callback is not %NULL, then that function that will be called
3517 * just like in g_file_copy(). The callback will run in the default main context
3518 * of the thread calling g_file_copy_async() — the same context as @callback is
3521 * When the operation is finished, @callback will be called. You can then call
3522 * g_file_copy_finish() to get the result of the operation.
3525 g_file_copy_async (GFile *source,
3527 GFileCopyFlags flags,
3529 GCancellable *cancellable,
3530 GFileProgressCallback progress_callback,
3531 gpointer progress_callback_data,
3532 GAsyncReadyCallback callback,
3537 g_return_if_fail (G_IS_FILE (source));
3538 g_return_if_fail (G_IS_FILE (destination));
3540 iface = G_FILE_GET_IFACE (source);
3541 (* iface->copy_async) (source,
3547 progress_callback_data,
3553 * g_file_copy_finish:
3554 * @file: input #GFile
3555 * @res: a #GAsyncResult
3556 * @error: a #GError, or %NULL
3558 * Finishes copying the file started with g_file_copy_async().
3560 * Returns: a %TRUE on success, %FALSE on error.
3563 g_file_copy_finish (GFile *file,
3569 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3570 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3572 if (g_async_result_legacy_propagate_error (res, error))
3575 iface = G_FILE_GET_IFACE (file);
3576 return (* iface->copy_finish) (file, res, error);
3581 * @source: #GFile pointing to the source location
3582 * @destination: #GFile pointing to the destination location
3583 * @flags: set of #GFileCopyFlags
3584 * @cancellable: (nullable): optional #GCancellable object,
3586 * @progress_callback: (nullable) (scope call): #GFileProgressCallback
3587 * function for updates
3588 * @progress_callback_data: (closure): gpointer to user data for
3589 * the callback function
3590 * @error: #GError for returning error conditions, or %NULL
3592 * Tries to move the file or directory @source to the location specified
3593 * by @destination. If native move operations are supported then this is
3594 * used, otherwise a copy + delete fallback is used. The native
3595 * implementation may support moving directories (for instance on moves
3596 * inside the same filesystem), but the fallback code does not.
3598 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3599 * existing @destination file is overwritten.
3601 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3602 * will be copied as symlinks, otherwise the target of the
3603 * @source symlink will be copied.
3605 * If @cancellable is not %NULL, then the operation can be cancelled by
3606 * triggering the cancellable object from another thread. If the operation
3607 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3609 * If @progress_callback is not %NULL, then the operation can be monitored
3610 * by setting this to a #GFileProgressCallback function.
3611 * @progress_callback_data will be passed to this function. It is
3612 * guaranteed that this callback will be called after all data has been
3613 * transferred with the total number of bytes copied during the operation.
3615 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3616 * error is returned, independent on the status of the @destination.
3618 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3619 * then the error %G_IO_ERROR_EXISTS is returned.
3621 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3622 * error is returned. If trying to overwrite a directory with a directory the
3623 * %G_IO_ERROR_WOULD_MERGE error is returned.
3625 * If the source is a directory and the target does not exist, or
3626 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3627 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3628 * move operation isn't available).
3630 * Returns: %TRUE on successful move, %FALSE otherwise.
3633 g_file_move (GFile *source,
3635 GFileCopyFlags flags,
3636 GCancellable *cancellable,
3637 GFileProgressCallback progress_callback,
3638 gpointer progress_callback_data,
3645 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3646 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3648 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3651 iface = G_FILE_GET_IFACE (destination);
3655 res = (* iface->move) (source, destination,
3657 progress_callback, progress_callback_data,
3663 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3665 g_propagate_error (error, my_error);
3669 g_clear_error (&my_error);
3672 /* If the types are different, and the destination method failed
3673 * also try the source method
3675 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3677 iface = G_FILE_GET_IFACE (source);
3682 res = (* iface->move) (source, destination,
3684 progress_callback, progress_callback_data,
3690 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3692 g_propagate_error (error, my_error);
3696 g_clear_error (&my_error);
3700 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3702 g_set_error_literal (error, G_IO_ERROR,
3703 G_IO_ERROR_NOT_SUPPORTED,
3704 _("Operation not supported"));
3708 flags |= G_FILE_COPY_ALL_METADATA;
3709 if (!g_file_copy (source, destination, flags, cancellable,
3710 progress_callback, progress_callback_data,
3714 return g_file_delete (source, cancellable, error);
3718 * g_file_make_directory:
3719 * @file: input #GFile
3720 * @cancellable: (nullable): optional #GCancellable object,
3722 * @error: a #GError, or %NULL
3724 * Creates a directory. Note that this will only create a child directory
3725 * of the immediate parent directory of the path or URI given by the #GFile.
3726 * To recursively create directories, see g_file_make_directory_with_parents().
3727 * This function will fail if the parent directory does not exist, setting
3728 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3729 * creating directories, this function will fail, setting @error to
3730 * %G_IO_ERROR_NOT_SUPPORTED.
3732 * For a local #GFile the newly created directory will have the default
3733 * (current) ownership and permissions of the current process.
3735 * If @cancellable is not %NULL, then the operation can be cancelled by
3736 * triggering the cancellable object from another thread. If the operation
3737 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3739 * Returns: %TRUE on successful creation, %FALSE otherwise.
3742 g_file_make_directory (GFile *file,
3743 GCancellable *cancellable,
3748 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3750 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3753 iface = G_FILE_GET_IFACE (file);
3755 if (iface->make_directory == NULL)
3757 g_set_error_literal (error, G_IO_ERROR,
3758 G_IO_ERROR_NOT_SUPPORTED,
3759 _("Operation not supported"));
3763 return (* iface->make_directory) (file, cancellable, error);
3767 * g_file_make_directory_async:
3768 * @file: input #GFile
3769 * @io_priority: the [I/O priority][io-priority] of the request
3770 * @cancellable: (nullable): optional #GCancellable object,
3772 * @callback: a #GAsyncReadyCallback to call
3773 * when the request is satisfied
3774 * @user_data: the data to pass to callback function
3776 * Asynchronously creates a directory.
3778 * Virtual: make_directory_async
3782 g_file_make_directory_async (GFile *file,
3784 GCancellable *cancellable,
3785 GAsyncReadyCallback callback,
3790 g_return_if_fail (G_IS_FILE (file));
3792 iface = G_FILE_GET_IFACE (file);
3793 (* iface->make_directory_async) (file,
3801 * g_file_make_directory_finish:
3802 * @file: input #GFile
3803 * @result: a #GAsyncResult
3804 * @error: a #GError, or %NULL
3806 * Finishes an asynchronous directory creation, started with
3807 * g_file_make_directory_async().
3809 * Virtual: make_directory_finish
3810 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3814 g_file_make_directory_finish (GFile *file,
3815 GAsyncResult *result,
3820 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3821 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3823 iface = G_FILE_GET_IFACE (file);
3824 return (* iface->make_directory_finish) (file, result, error);
3828 * g_file_make_directory_with_parents:
3829 * @file: input #GFile
3830 * @cancellable: (nullable): optional #GCancellable object,
3832 * @error: a #GError, or %NULL
3834 * Creates a directory and any parent directories that may not
3835 * exist similar to 'mkdir -p'. If the file system does not support
3836 * creating directories, this function will fail, setting @error to
3837 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3838 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3839 * the similar g_mkdir_with_parents().
3841 * For a local #GFile the newly created directories will have the default
3842 * (current) ownership and permissions of the current process.
3844 * If @cancellable is not %NULL, then the operation can be cancelled by
3845 * triggering the cancellable object from another thread. If the operation
3846 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3848 * Returns: %TRUE if all directories have been successfully created, %FALSE
3854 g_file_make_directory_with_parents (GFile *file,
3855 GCancellable *cancellable,
3858 GFile *work_file = NULL;
3859 GList *list = NULL, *l;
3860 GError *my_error = NULL;
3862 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3864 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3867 /* Try for the simple case of not having to create any parent
3868 * directories. If any parent directory needs to be created, this
3869 * call will fail with NOT_FOUND. If that happens, then that value of
3870 * my_error persists into the while loop below.
3872 g_file_make_directory (file, cancellable, &my_error);
3873 if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3876 g_propagate_error (error, my_error);
3877 return my_error == NULL;
3880 work_file = g_object_ref (file);
3882 /* Creates the parent directories as needed. In case any particular
3883 * creation operation fails for lack of other parent directories
3884 * (NOT_FOUND), the directory is added to a list of directories to
3885 * create later, and the value of my_error is retained until the next
3886 * iteration of the loop. After the loop my_error should either be
3887 * empty or contain a real failure condition.
3889 while (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3893 parent_file = g_file_get_parent (work_file);
3894 if (parent_file == NULL)
3897 g_clear_error (&my_error);
3898 g_file_make_directory (parent_file, cancellable, &my_error);
3899 /* Another process may have created the directory in between the
3900 * G_IO_ERROR_NOT_FOUND and now
3902 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3903 g_clear_error (&my_error);
3905 g_object_unref (work_file);
3906 work_file = g_object_ref (parent_file);
3908 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3909 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3911 g_object_unref (parent_file);
3914 /* All directories should be able to be created now, so an error at
3915 * this point means the whole operation must fail -- except an EXISTS
3916 * error, which means that another process already created the
3917 * directory in between the previous failure and now.
3919 for (l = list; my_error == NULL && l; l = l->next)
3921 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3922 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3923 g_clear_error (&my_error);
3927 g_object_unref (work_file);
3930 while (list != NULL)
3932 g_object_unref ((GFile *) list->data);
3933 list = g_list_remove (list, list->data);
3936 /* At this point an error in my_error means a that something
3937 * unexpected failed in either of the loops above, so the whole
3938 * operation must fail.
3940 if (my_error != NULL)
3942 g_propagate_error (error, my_error);
3946 return g_file_make_directory (file, cancellable, error);
3950 * g_file_make_symbolic_link:
3951 * @file: a #GFile with the name of the symlink to create
3952 * @symlink_value: (type filename): a string with the path for the target
3953 * of the new symlink
3954 * @cancellable: (nullable): optional #GCancellable object,
3958 * Creates a symbolic link named @file which contains the string
3961 * If @cancellable is not %NULL, then the operation can be cancelled by
3962 * triggering the cancellable object from another thread. If the operation
3963 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3965 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3968 g_file_make_symbolic_link (GFile *file,
3969 const char *symlink_value,
3970 GCancellable *cancellable,
3975 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3976 g_return_val_if_fail (symlink_value != NULL, FALSE);
3978 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3981 if (*symlink_value == '\0')
3983 g_set_error_literal (error, G_IO_ERROR,
3984 G_IO_ERROR_INVALID_ARGUMENT,
3985 _("Invalid symlink value given"));
3989 iface = G_FILE_GET_IFACE (file);
3991 if (iface->make_symbolic_link == NULL)
3993 g_set_error_literal (error, G_IO_ERROR,
3994 G_IO_ERROR_NOT_SUPPORTED,
3995 _("Operation not supported"));
3999 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
4004 * @file: input #GFile
4005 * @cancellable: (nullable): optional #GCancellable object,
4007 * @error: a #GError, or %NULL
4009 * Deletes a file. If the @file is a directory, it will only be
4010 * deleted if it is empty. This has the same semantics as g_unlink().
4012 * If @cancellable is not %NULL, then the operation can be cancelled by
4013 * triggering the cancellable object from another thread. If the operation
4014 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4016 * Virtual: delete_file
4017 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4020 g_file_delete (GFile *file,
4021 GCancellable *cancellable,
4026 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4028 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4031 iface = G_FILE_GET_IFACE (file);
4033 if (iface->delete_file == NULL)
4035 g_set_error_literal (error, G_IO_ERROR,
4036 G_IO_ERROR_NOT_SUPPORTED,
4037 _("Operation not supported"));
4041 return (* iface->delete_file) (file, cancellable, error);
4045 * g_file_delete_async:
4046 * @file: input #GFile
4047 * @io_priority: the [I/O priority][io-priority] of the request
4048 * @cancellable: (nullable): optional #GCancellable object,
4050 * @callback: a #GAsyncReadyCallback to call
4051 * when the request is satisfied
4052 * @user_data: the data to pass to callback function
4054 * Asynchronously delete a file. If the @file is a directory, it will
4055 * only be deleted if it is empty. This has the same semantics as
4058 * Virtual: delete_file_async
4062 g_file_delete_async (GFile *file,
4064 GCancellable *cancellable,
4065 GAsyncReadyCallback callback,
4070 g_return_if_fail (G_IS_FILE (file));
4072 iface = G_FILE_GET_IFACE (file);
4073 (* iface->delete_file_async) (file,
4081 * g_file_delete_finish:
4082 * @file: input #GFile
4083 * @result: a #GAsyncResult
4084 * @error: a #GError, or %NULL
4086 * Finishes deleting a file started with g_file_delete_async().
4088 * Virtual: delete_file_finish
4089 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4093 g_file_delete_finish (GFile *file,
4094 GAsyncResult *result,
4099 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4100 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4102 if (g_async_result_legacy_propagate_error (result, error))
4105 iface = G_FILE_GET_IFACE (file);
4106 return (* iface->delete_file_finish) (file, result, error);
4111 * @file: #GFile to send to trash
4112 * @cancellable: (nullable): optional #GCancellable object,
4114 * @error: a #GError, or %NULL
4116 * Sends @file to the "Trashcan", if possible. This is similar to
4117 * deleting it, but the user can recover it before emptying the trashcan.
4118 * Not all file systems support trashing, so this call can return the
4119 * %G_IO_ERROR_NOT_SUPPORTED error.
4121 * If @cancellable is not %NULL, then the operation can be cancelled by
4122 * triggering the cancellable object from another thread. If the operation
4123 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4126 * Returns: %TRUE on successful trash, %FALSE otherwise.
4129 g_file_trash (GFile *file,
4130 GCancellable *cancellable,
4135 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4137 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4140 iface = G_FILE_GET_IFACE (file);
4142 if (iface->trash == NULL)
4144 g_set_error_literal (error,
4145 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4146 _("Trash not supported"));
4150 return (* iface->trash) (file, cancellable, error);
4154 * g_file_trash_async:
4155 * @file: input #GFile
4156 * @io_priority: the [I/O priority][io-priority] of the request
4157 * @cancellable: (nullable): optional #GCancellable object,
4159 * @callback: a #GAsyncReadyCallback to call
4160 * when the request is satisfied
4161 * @user_data: the data to pass to callback function
4163 * Asynchronously sends @file to the Trash location, if possible.
4165 * Virtual: trash_async
4169 g_file_trash_async (GFile *file,
4171 GCancellable *cancellable,
4172 GAsyncReadyCallback callback,
4177 g_return_if_fail (G_IS_FILE (file));
4179 iface = G_FILE_GET_IFACE (file);
4180 (* iface->trash_async) (file,
4188 * g_file_trash_finish:
4189 * @file: input #GFile
4190 * @result: a #GAsyncResult
4191 * @error: a #GError, or %NULL
4193 * Finishes an asynchronous file trashing operation, started with
4194 * g_file_trash_async().
4196 * Virtual: trash_finish
4197 * Returns: %TRUE on successful trash, %FALSE otherwise.
4201 g_file_trash_finish (GFile *file,
4202 GAsyncResult *result,
4207 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4208 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4210 iface = G_FILE_GET_IFACE (file);
4211 return (* iface->trash_finish) (file, result, error);
4215 * g_file_set_display_name:
4216 * @file: input #GFile
4217 * @display_name: a string
4218 * @cancellable: (nullable): optional #GCancellable object,
4220 * @error: a #GError, or %NULL
4222 * Renames @file to the specified display name.
4224 * The display name is converted from UTF-8 to the correct encoding
4225 * for the target filesystem if possible and the @file is renamed to this.
4227 * If you want to implement a rename operation in the user interface the
4228 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4229 * initial value in the rename widget, and then the result after editing
4230 * should be passed to g_file_set_display_name().
4232 * On success the resulting converted filename is returned.
4234 * If @cancellable is not %NULL, then the operation can be cancelled by
4235 * triggering the cancellable object from another thread. If the operation
4236 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4238 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4239 * or %NULL if there was an error.
4240 * Free the returned object with g_object_unref().
4243 g_file_set_display_name (GFile *file,
4244 const gchar *display_name,
4245 GCancellable *cancellable,
4250 g_return_val_if_fail (G_IS_FILE (file), NULL);
4251 g_return_val_if_fail (display_name != NULL, NULL);
4253 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4257 G_IO_ERROR_INVALID_ARGUMENT,
4258 _("File names cannot contain “%c”"), G_DIR_SEPARATOR);
4262 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4265 iface = G_FILE_GET_IFACE (file);
4267 return (* iface->set_display_name) (file, display_name, cancellable, error);
4271 * g_file_set_display_name_async:
4272 * @file: input #GFile
4273 * @display_name: a string
4274 * @io_priority: the [I/O priority][io-priority] of the request
4275 * @cancellable: (nullable): optional #GCancellable object,
4277 * @callback: (scope async): a #GAsyncReadyCallback to call
4278 * when the request is satisfied
4279 * @user_data: (closure): the data to pass to callback function
4281 * Asynchronously sets the display name for a given #GFile.
4283 * For more details, see g_file_set_display_name() which is
4284 * the synchronous version of this call.
4286 * When the operation is finished, @callback will be called.
4287 * You can then call g_file_set_display_name_finish() to get
4288 * the result of the operation.
4291 g_file_set_display_name_async (GFile *file,
4292 const gchar *display_name,
4294 GCancellable *cancellable,
4295 GAsyncReadyCallback callback,
4300 g_return_if_fail (G_IS_FILE (file));
4301 g_return_if_fail (display_name != NULL);
4303 iface = G_FILE_GET_IFACE (file);
4304 (* iface->set_display_name_async) (file,
4313 * g_file_set_display_name_finish:
4314 * @file: input #GFile
4315 * @res: a #GAsyncResult
4316 * @error: a #GError, or %NULL
4318 * Finishes setting a display name started with
4319 * g_file_set_display_name_async().
4321 * Returns: (transfer full): a #GFile or %NULL on error.
4322 * Free the returned object with g_object_unref().
4325 g_file_set_display_name_finish (GFile *file,
4331 g_return_val_if_fail (G_IS_FILE (file), NULL);
4332 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4334 if (g_async_result_legacy_propagate_error (res, error))
4337 iface = G_FILE_GET_IFACE (file);
4338 return (* iface->set_display_name_finish) (file, res, error);
4342 * g_file_query_settable_attributes:
4343 * @file: input #GFile
4344 * @cancellable: (nullable): optional #GCancellable object,
4346 * @error: a #GError, or %NULL
4348 * Obtain the list of settable attributes for the file.
4350 * Returns the type and full attribute name of all the attributes
4351 * that can be set on this file. This doesn't mean setting it will
4352 * always succeed though, you might get an access failure, or some
4353 * specific file may not support a specific attribute.
4355 * If @cancellable is not %NULL, then the operation can be cancelled by
4356 * triggering the cancellable object from another thread. If the operation
4357 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4359 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4360 * When you are done with it, release it with
4361 * g_file_attribute_info_list_unref()
4363 GFileAttributeInfoList *
4364 g_file_query_settable_attributes (GFile *file,
4365 GCancellable *cancellable,
4370 GFileAttributeInfoList *list;
4372 g_return_val_if_fail (G_IS_FILE (file), NULL);
4374 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4377 iface = G_FILE_GET_IFACE (file);
4379 if (iface->query_settable_attributes == NULL)
4380 return g_file_attribute_info_list_new ();
4383 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4387 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4389 list = g_file_attribute_info_list_new ();
4390 g_error_free (my_error);
4393 g_propagate_error (error, my_error);
4400 * g_file_query_writable_namespaces:
4401 * @file: input #GFile
4402 * @cancellable: (nullable): optional #GCancellable object,
4404 * @error: a #GError, or %NULL
4406 * Obtain the list of attribute namespaces where new attributes
4407 * can be created by a user. An example of this is extended
4408 * attributes (in the "xattr" namespace).
4410 * If @cancellable is not %NULL, then the operation can be cancelled by
4411 * triggering the cancellable object from another thread. If the operation
4412 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4414 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4415 * When you are done with it, release it with
4416 * g_file_attribute_info_list_unref()
4418 GFileAttributeInfoList *
4419 g_file_query_writable_namespaces (GFile *file,
4420 GCancellable *cancellable,
4425 GFileAttributeInfoList *list;
4427 g_return_val_if_fail (G_IS_FILE (file), NULL);
4429 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4432 iface = G_FILE_GET_IFACE (file);
4434 if (iface->query_writable_namespaces == NULL)
4435 return g_file_attribute_info_list_new ();
4438 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4442 g_warn_if_reached();
4443 list = g_file_attribute_info_list_new ();
4446 if (my_error != NULL)
4448 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4450 g_error_free (my_error);
4453 g_propagate_error (error, my_error);
4460 * g_file_set_attribute:
4461 * @file: input #GFile
4462 * @attribute: a string containing the attribute's name
4463 * @type: The type of the attribute
4464 * @value_p: (nullable): a pointer to the value (or the pointer
4465 * itself if the type is a pointer type)
4466 * @flags: a set of #GFileQueryInfoFlags
4467 * @cancellable: (nullable): optional #GCancellable object,
4469 * @error: a #GError, or %NULL
4471 * Sets an attribute in the file with attribute name @attribute to @value.
4473 * Some attributes can be unset by setting @type to
4474 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4476 * If @cancellable is not %NULL, then the operation can be cancelled by
4477 * triggering the cancellable object from another thread. If the operation
4478 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4480 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4483 g_file_set_attribute (GFile *file,
4484 const gchar *attribute,
4485 GFileAttributeType type,
4487 GFileQueryInfoFlags flags,
4488 GCancellable *cancellable,
4493 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4494 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4496 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4499 iface = G_FILE_GET_IFACE (file);
4501 if (iface->set_attribute == NULL)
4503 g_set_error_literal (error, G_IO_ERROR,
4504 G_IO_ERROR_NOT_SUPPORTED,
4505 _("Operation not supported"));
4509 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4513 * g_file_set_attributes_from_info:
4514 * @file: input #GFile
4515 * @info: a #GFileInfo
4516 * @flags: #GFileQueryInfoFlags
4517 * @cancellable: (nullable): optional #GCancellable object,
4519 * @error: a #GError, or %NULL
4521 * Tries to set all attributes in the #GFileInfo on the target
4522 * values, not stopping on the first error.
4524 * If there is any error during this operation then @error will
4525 * be set to the first error. Error on particular fields are flagged
4526 * by setting the "status" field in the attribute value to
4527 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4528 * also detect further errors.
4530 * If @cancellable is not %NULL, then the operation can be cancelled by
4531 * triggering the cancellable object from another thread. If the operation
4532 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4534 * Returns: %FALSE if there was any error, %TRUE otherwise.
4537 g_file_set_attributes_from_info (GFile *file,
4539 GFileQueryInfoFlags flags,
4540 GCancellable *cancellable,
4545 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4546 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4548 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4551 g_file_info_clear_status (info);
4553 iface = G_FILE_GET_IFACE (file);
4555 return (* iface->set_attributes_from_info) (file,
4563 g_file_real_set_attributes_from_info (GFile *file,
4565 GFileQueryInfoFlags flags,
4566 GCancellable *cancellable,
4572 GFileAttributeValue *value;
4576 attributes = g_file_info_list_attributes (info, NULL);
4578 for (i = 0; attributes[i] != NULL; i++)
4580 value = _g_file_info_get_attribute_value (info, attributes[i]);
4582 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4585 if (!g_file_set_attribute (file, attributes[i],
4586 value->type, _g_file_attribute_value_peek_as_pointer (value),
4587 flags, cancellable, error))
4589 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4591 /* Don't set error multiple times */
4595 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4598 g_strfreev (attributes);
4604 * g_file_set_attributes_async:
4605 * @file: input #GFile
4606 * @info: a #GFileInfo
4607 * @flags: a #GFileQueryInfoFlags
4608 * @io_priority: the [I/O priority][io-priority] of the request
4609 * @cancellable: (nullable): optional #GCancellable object,
4611 * @callback: (scope async): a #GAsyncReadyCallback
4612 * @user_data: (closure): a #gpointer
4614 * Asynchronously sets the attributes of @file with @info.
4616 * For more details, see g_file_set_attributes_from_info(),
4617 * which is the synchronous version of this call.
4619 * When the operation is finished, @callback will be called.
4620 * You can then call g_file_set_attributes_finish() to get
4621 * the result of the operation.
4624 g_file_set_attributes_async (GFile *file,
4626 GFileQueryInfoFlags flags,
4628 GCancellable *cancellable,
4629 GAsyncReadyCallback callback,
4634 g_return_if_fail (G_IS_FILE (file));
4635 g_return_if_fail (G_IS_FILE_INFO (info));
4637 iface = G_FILE_GET_IFACE (file);
4638 (* iface->set_attributes_async) (file,
4648 * g_file_set_attributes_finish:
4649 * @file: input #GFile
4650 * @result: a #GAsyncResult
4651 * @info: (out) (transfer full): a #GFileInfo
4652 * @error: a #GError, or %NULL
4654 * Finishes setting an attribute started in g_file_set_attributes_async().
4656 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4659 g_file_set_attributes_finish (GFile *file,
4660 GAsyncResult *result,
4666 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4667 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4669 /* No standard handling of errors here, as we must set info even
4672 iface = G_FILE_GET_IFACE (file);
4673 return (* iface->set_attributes_finish) (file, result, info, error);
4677 * g_file_set_attribute_string:
4678 * @file: input #GFile
4679 * @attribute: a string containing the attribute's name
4680 * @value: a string containing the attribute's value
4681 * @flags: #GFileQueryInfoFlags
4682 * @cancellable: (nullable): optional #GCancellable object,
4684 * @error: a #GError, or %NULL
4686 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4687 * If @attribute is of a different type, this operation will fail.
4689 * If @cancellable is not %NULL, then the operation can be cancelled by
4690 * triggering the cancellable object from another thread. If the operation
4691 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4693 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4696 g_file_set_attribute_string (GFile *file,
4697 const char *attribute,
4699 GFileQueryInfoFlags flags,
4700 GCancellable *cancellable,
4703 return g_file_set_attribute (file, attribute,
4704 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4705 flags, cancellable, error);
4709 * g_file_set_attribute_byte_string:
4710 * @file: input #GFile
4711 * @attribute: a string containing the attribute's name
4712 * @value: a string containing the attribute's new value
4713 * @flags: a #GFileQueryInfoFlags
4714 * @cancellable: (nullable): optional #GCancellable object,
4716 * @error: a #GError, or %NULL
4718 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4719 * If @attribute is of a different type, this operation will fail,
4722 * If @cancellable is not %NULL, then the operation can be cancelled by
4723 * triggering the cancellable object from another thread. If the operation
4724 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4726 * Returns: %TRUE if the @attribute was successfully set to @value
4727 * in the @file, %FALSE otherwise.
4730 g_file_set_attribute_byte_string (GFile *file,
4731 const gchar *attribute,
4733 GFileQueryInfoFlags flags,
4734 GCancellable *cancellable,
4737 return g_file_set_attribute (file, attribute,
4738 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4739 flags, cancellable, error);
4743 * g_file_set_attribute_uint32:
4744 * @file: input #GFile
4745 * @attribute: a string containing the attribute's name
4746 * @value: a #guint32 containing the attribute's new value
4747 * @flags: a #GFileQueryInfoFlags
4748 * @cancellable: (nullable): optional #GCancellable object,
4750 * @error: a #GError, or %NULL
4752 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4753 * If @attribute is of a different type, this operation will fail.
4755 * If @cancellable is not %NULL, then the operation can be cancelled by
4756 * triggering the cancellable object from another thread. If the operation
4757 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4759 * Returns: %TRUE if the @attribute was successfully set to @value
4760 * in the @file, %FALSE otherwise.
4763 g_file_set_attribute_uint32 (GFile *file,
4764 const gchar *attribute,
4766 GFileQueryInfoFlags flags,
4767 GCancellable *cancellable,
4770 return g_file_set_attribute (file, attribute,
4771 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4772 flags, cancellable, error);
4776 * g_file_set_attribute_int32:
4777 * @file: input #GFile
4778 * @attribute: a string containing the attribute's name
4779 * @value: a #gint32 containing the attribute's new value
4780 * @flags: a #GFileQueryInfoFlags
4781 * @cancellable: (nullable): optional #GCancellable object,
4783 * @error: a #GError, or %NULL
4785 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4786 * If @attribute is of a different type, this operation will fail.
4788 * If @cancellable is not %NULL, then the operation can be cancelled by
4789 * triggering the cancellable object from another thread. If the operation
4790 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4792 * Returns: %TRUE if the @attribute was successfully set to @value
4793 * in the @file, %FALSE otherwise.
4796 g_file_set_attribute_int32 (GFile *file,
4797 const gchar *attribute,
4799 GFileQueryInfoFlags flags,
4800 GCancellable *cancellable,
4803 return g_file_set_attribute (file, attribute,
4804 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4805 flags, cancellable, error);
4809 * g_file_set_attribute_uint64:
4810 * @file: input #GFile
4811 * @attribute: a string containing the attribute's name
4812 * @value: a #guint64 containing the attribute's new value
4813 * @flags: a #GFileQueryInfoFlags
4814 * @cancellable: (nullable): optional #GCancellable object,
4816 * @error: a #GError, or %NULL
4818 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4819 * If @attribute is of a different type, this operation will fail.
4821 * If @cancellable is not %NULL, then the operation can be cancelled by
4822 * triggering the cancellable object from another thread. If the operation
4823 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4825 * Returns: %TRUE if the @attribute was successfully set to @value
4826 * in the @file, %FALSE otherwise.
4829 g_file_set_attribute_uint64 (GFile *file,
4830 const gchar *attribute,
4832 GFileQueryInfoFlags flags,
4833 GCancellable *cancellable,
4836 return g_file_set_attribute (file, attribute,
4837 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4838 flags, cancellable, error);
4842 * g_file_set_attribute_int64:
4843 * @file: input #GFile
4844 * @attribute: a string containing the attribute's name
4845 * @value: a #guint64 containing the attribute's new value
4846 * @flags: a #GFileQueryInfoFlags
4847 * @cancellable: (nullable): optional #GCancellable object,
4849 * @error: a #GError, or %NULL
4851 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4852 * If @attribute is of a different type, this operation will fail.
4854 * If @cancellable is not %NULL, then the operation can be cancelled by
4855 * triggering the cancellable object from another thread. If the operation
4856 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4858 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4861 g_file_set_attribute_int64 (GFile *file,
4862 const gchar *attribute,
4864 GFileQueryInfoFlags flags,
4865 GCancellable *cancellable,
4868 return g_file_set_attribute (file, attribute,
4869 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4870 flags, cancellable, error);
4874 * g_file_mount_mountable:
4875 * @file: input #GFile
4876 * @flags: flags affecting the operation
4877 * @mount_operation: (nullable): a #GMountOperation,
4878 * or %NULL to avoid user interaction
4879 * @cancellable: (nullable): optional #GCancellable object,
4881 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4882 * when the request is satisfied, or %NULL
4883 * @user_data: (closure): the data to pass to callback function
4885 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4886 * Using @mount_operation, you can request callbacks when, for instance,
4887 * passwords are needed during authentication.
4889 * If @cancellable is not %NULL, then the operation can be cancelled by
4890 * triggering the cancellable object from another thread. If the operation
4891 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4893 * When the operation is finished, @callback will be called.
4894 * You can then call g_file_mount_mountable_finish() to get
4895 * the result of the operation.
4898 g_file_mount_mountable (GFile *file,
4899 GMountMountFlags flags,
4900 GMountOperation *mount_operation,
4901 GCancellable *cancellable,
4902 GAsyncReadyCallback callback,
4907 g_return_if_fail (G_IS_FILE (file));
4909 iface = G_FILE_GET_IFACE (file);
4911 if (iface->mount_mountable == NULL)
4913 g_task_report_new_error (file, callback, user_data,
4914 g_file_mount_mountable,
4915 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4916 _("Operation not supported"));
4920 (* iface->mount_mountable) (file,
4929 * g_file_mount_mountable_finish:
4930 * @file: input #GFile
4931 * @result: a #GAsyncResult
4932 * @error: a #GError, or %NULL
4934 * Finishes a mount operation. See g_file_mount_mountable() for details.
4936 * Finish an asynchronous mount operation that was started
4937 * with g_file_mount_mountable().
4939 * Returns: (transfer full): a #GFile or %NULL on error.
4940 * Free the returned object with g_object_unref().
4943 g_file_mount_mountable_finish (GFile *file,
4944 GAsyncResult *result,
4949 g_return_val_if_fail (G_IS_FILE (file), NULL);
4950 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4952 if (g_async_result_legacy_propagate_error (result, error))
4954 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4955 return g_task_propagate_pointer (G_TASK (result), error);
4957 iface = G_FILE_GET_IFACE (file);
4958 return (* iface->mount_mountable_finish) (file, result, error);
4962 * g_file_unmount_mountable:
4963 * @file: input #GFile
4964 * @flags: flags affecting the operation
4965 * @cancellable: (nullable): optional #GCancellable object,
4967 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4968 * when the request is satisfied, or %NULL
4969 * @user_data: (closure): the data to pass to callback function
4971 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4973 * If @cancellable is not %NULL, then the operation can be cancelled by
4974 * triggering the cancellable object from another thread. If the operation
4975 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4977 * When the operation is finished, @callback will be called.
4978 * You can then call g_file_unmount_mountable_finish() to get
4979 * the result of the operation.
4981 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4984 g_file_unmount_mountable (GFile *file,
4985 GMountUnmountFlags flags,
4986 GCancellable *cancellable,
4987 GAsyncReadyCallback callback,
4992 g_return_if_fail (G_IS_FILE (file));
4994 iface = G_FILE_GET_IFACE (file);
4996 if (iface->unmount_mountable == NULL)
4998 g_task_report_new_error (file, callback, user_data,
4999 g_file_unmount_mountable_with_operation,
5000 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5001 _("Operation not supported"));
5005 (* iface->unmount_mountable) (file,
5013 * g_file_unmount_mountable_finish:
5014 * @file: input #GFile
5015 * @result: a #GAsyncResult
5016 * @error: a #GError, or %NULL
5018 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
5020 * Finish an asynchronous unmount operation that was started
5021 * with g_file_unmount_mountable().
5023 * Returns: %TRUE if the operation finished successfully.
5026 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
5030 g_file_unmount_mountable_finish (GFile *file,
5031 GAsyncResult *result,
5036 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5037 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5039 if (g_async_result_legacy_propagate_error (result, error))
5041 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5042 return g_task_propagate_boolean (G_TASK (result), error);
5044 iface = G_FILE_GET_IFACE (file);
5045 return (* iface->unmount_mountable_finish) (file, result, error);
5049 * g_file_unmount_mountable_with_operation:
5050 * @file: input #GFile
5051 * @flags: flags affecting the operation
5052 * @mount_operation: (nullable): a #GMountOperation,
5053 * or %NULL to avoid user interaction
5054 * @cancellable: (nullable): optional #GCancellable object,
5056 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5057 * when the request is satisfied, or %NULL
5058 * @user_data: (closure): the data to pass to callback function
5060 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
5062 * If @cancellable is not %NULL, then the operation can be cancelled by
5063 * triggering the cancellable object from another thread. If the operation
5064 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5066 * When the operation is finished, @callback will be called.
5067 * You can then call g_file_unmount_mountable_finish() to get
5068 * the result of the operation.
5073 g_file_unmount_mountable_with_operation (GFile *file,
5074 GMountUnmountFlags flags,
5075 GMountOperation *mount_operation,
5076 GCancellable *cancellable,
5077 GAsyncReadyCallback callback,
5082 g_return_if_fail (G_IS_FILE (file));
5084 iface = G_FILE_GET_IFACE (file);
5086 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
5088 g_task_report_new_error (file, callback, user_data,
5089 g_file_unmount_mountable_with_operation,
5090 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5091 _("Operation not supported"));
5095 if (iface->unmount_mountable_with_operation != NULL)
5096 (* iface->unmount_mountable_with_operation) (file,
5103 (* iface->unmount_mountable) (file,
5111 * g_file_unmount_mountable_with_operation_finish:
5112 * @file: input #GFile
5113 * @result: a #GAsyncResult
5114 * @error: a #GError, or %NULL
5116 * Finishes an unmount operation,
5117 * see g_file_unmount_mountable_with_operation() for details.
5119 * Finish an asynchronous unmount operation that was started
5120 * with g_file_unmount_mountable_with_operation().
5122 * Returns: %TRUE if the operation finished successfully.
5128 g_file_unmount_mountable_with_operation_finish (GFile *file,
5129 GAsyncResult *result,
5134 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5135 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5137 if (g_async_result_legacy_propagate_error (result, error))
5139 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5140 return g_task_propagate_boolean (G_TASK (result), error);
5142 iface = G_FILE_GET_IFACE (file);
5143 if (iface->unmount_mountable_with_operation_finish != NULL)
5144 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5146 return (* iface->unmount_mountable_finish) (file, result, error);
5150 * g_file_eject_mountable:
5151 * @file: input #GFile
5152 * @flags: flags affecting the operation
5153 * @cancellable: (nullable): optional #GCancellable object,
5155 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5156 * when the request is satisfied, or %NULL
5157 * @user_data: (closure): the data to pass to callback function
5159 * Starts an asynchronous eject on a mountable.
5160 * When this operation has completed, @callback will be called with
5161 * @user_user data, and the operation can be finalized with
5162 * g_file_eject_mountable_finish().
5164 * If @cancellable is not %NULL, then the operation can be cancelled by
5165 * triggering the cancellable object from another thread. If the operation
5166 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5168 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5171 g_file_eject_mountable (GFile *file,
5172 GMountUnmountFlags flags,
5173 GCancellable *cancellable,
5174 GAsyncReadyCallback callback,
5179 g_return_if_fail (G_IS_FILE (file));
5181 iface = G_FILE_GET_IFACE (file);
5183 if (iface->eject_mountable == NULL)
5185 g_task_report_new_error (file, callback, user_data,
5186 g_file_eject_mountable_with_operation,
5187 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5188 _("Operation not supported"));
5192 (* iface->eject_mountable) (file,
5200 * g_file_eject_mountable_finish:
5201 * @file: input #GFile
5202 * @result: a #GAsyncResult
5203 * @error: a #GError, or %NULL
5205 * Finishes an asynchronous eject operation started by
5206 * g_file_eject_mountable().
5208 * Returns: %TRUE if the @file was ejected successfully.
5211 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5215 g_file_eject_mountable_finish (GFile *file,
5216 GAsyncResult *result,
5221 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5222 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5224 if (g_async_result_legacy_propagate_error (result, error))
5226 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5227 return g_task_propagate_boolean (G_TASK (result), error);
5229 iface = G_FILE_GET_IFACE (file);
5230 return (* iface->eject_mountable_finish) (file, result, error);
5234 * g_file_eject_mountable_with_operation:
5235 * @file: input #GFile
5236 * @flags: flags affecting the operation
5237 * @mount_operation: (nullable): a #GMountOperation,
5238 * or %NULL to avoid user interaction
5239 * @cancellable: (nullable): optional #GCancellable object,
5241 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5242 * when the request is satisfied, or %NULL
5243 * @user_data: (closure): the data to pass to callback function
5245 * Starts an asynchronous eject on a mountable.
5246 * When this operation has completed, @callback will be called with
5247 * @user_user data, and the operation can be finalized with
5248 * g_file_eject_mountable_with_operation_finish().
5250 * If @cancellable is not %NULL, then the operation can be cancelled by
5251 * triggering the cancellable object from another thread. If the operation
5252 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5257 g_file_eject_mountable_with_operation (GFile *file,
5258 GMountUnmountFlags flags,
5259 GMountOperation *mount_operation,
5260 GCancellable *cancellable,
5261 GAsyncReadyCallback callback,
5266 g_return_if_fail (G_IS_FILE (file));
5268 iface = G_FILE_GET_IFACE (file);
5270 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5272 g_task_report_new_error (file, callback, user_data,
5273 g_file_eject_mountable_with_operation,
5274 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5275 _("Operation not supported"));
5279 if (iface->eject_mountable_with_operation != NULL)
5280 (* iface->eject_mountable_with_operation) (file,
5287 (* iface->eject_mountable) (file,
5295 * g_file_eject_mountable_with_operation_finish:
5296 * @file: input #GFile
5297 * @result: a #GAsyncResult
5298 * @error: a #GError, or %NULL
5300 * Finishes an asynchronous eject operation started by
5301 * g_file_eject_mountable_with_operation().
5303 * Returns: %TRUE if the @file was ejected successfully.
5309 g_file_eject_mountable_with_operation_finish (GFile *file,
5310 GAsyncResult *result,
5315 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5316 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5318 if (g_async_result_legacy_propagate_error (result, error))
5320 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5321 return g_task_propagate_boolean (G_TASK (result), error);
5323 iface = G_FILE_GET_IFACE (file);
5324 if (iface->eject_mountable_with_operation_finish != NULL)
5325 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5327 return (* iface->eject_mountable_finish) (file, result, error);
5331 * g_file_monitor_directory:
5332 * @file: input #GFile
5333 * @flags: a set of #GFileMonitorFlags
5334 * @cancellable: (nullable): optional #GCancellable object,
5336 * @error: a #GError, or %NULL
5338 * Obtains a directory monitor for the given file.
5339 * This may fail if directory monitoring is not supported.
5341 * If @cancellable is not %NULL, then the operation can be cancelled by
5342 * triggering the cancellable object from another thread. If the operation
5343 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5345 * It does not make sense for @flags to contain
5346 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5347 * directories. It is not possible to monitor all the files in a
5348 * directory for changes made via hard links; if you want to do this then
5349 * you must register individual watches with g_file_monitor().
5351 * Virtual: monitor_dir
5352 * Returns: (transfer full): a #GFileMonitor for the given @file,
5353 * or %NULL on error.
5354 * Free the returned object with g_object_unref().
5357 g_file_monitor_directory (GFile *file,
5358 GFileMonitorFlags flags,
5359 GCancellable *cancellable,
5364 g_return_val_if_fail (G_IS_FILE (file), NULL);
5365 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5367 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5370 iface = G_FILE_GET_IFACE (file);
5372 if (iface->monitor_dir == NULL)
5374 g_set_error_literal (error, G_IO_ERROR,
5375 G_IO_ERROR_NOT_SUPPORTED,
5376 _("Operation not supported"));
5380 return (* iface->monitor_dir) (file, flags, cancellable, error);
5384 * g_file_monitor_file:
5385 * @file: input #GFile
5386 * @flags: a set of #GFileMonitorFlags
5387 * @cancellable: (nullable): optional #GCancellable object,
5389 * @error: a #GError, or %NULL
5391 * Obtains a file monitor for the given file. If no file notification
5392 * mechanism exists, then regular polling of the file is used.
5394 * If @cancellable is not %NULL, then the operation can be cancelled by
5395 * triggering the cancellable object from another thread. If the operation
5396 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5398 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5399 * will also attempt to report changes made to the file via another
5400 * filename (ie, a hard link). Without this flag, you can only rely on
5401 * changes made through the filename contained in @file to be
5402 * reported. Using this flag may result in an increase in resource
5403 * usage, and may not have any effect depending on the #GFileMonitor
5404 * backend and/or filesystem type.
5406 * Returns: (transfer full): a #GFileMonitor for the given @file,
5407 * or %NULL on error.
5408 * Free the returned object with g_object_unref().
5411 g_file_monitor_file (GFile *file,
5412 GFileMonitorFlags flags,
5413 GCancellable *cancellable,
5417 GFileMonitor *monitor;
5419 g_return_val_if_fail (G_IS_FILE (file), NULL);
5421 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5424 iface = G_FILE_GET_IFACE (file);
5428 if (iface->monitor_file)
5429 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5431 /* Fallback to polling */
5432 if (monitor == NULL)
5433 monitor = _g_poll_file_monitor_new (file);
5440 * @file: input #GFile
5441 * @flags: a set of #GFileMonitorFlags
5442 * @cancellable: (nullable): optional #GCancellable object,
5444 * @error: a #GError, or %NULL
5446 * Obtains a file or directory monitor for the given file,
5447 * depending on the type of the file.
5449 * If @cancellable is not %NULL, then the operation can be cancelled by
5450 * triggering the cancellable object from another thread. If the operation
5451 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5453 * Returns: (transfer full): a #GFileMonitor for the given @file,
5454 * or %NULL on error.
5455 * Free the returned object with g_object_unref().
5460 g_file_monitor (GFile *file,
5461 GFileMonitorFlags flags,
5462 GCancellable *cancellable,
5465 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5466 return g_file_monitor_directory (file,
5467 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5468 cancellable, error);
5470 return g_file_monitor_file (file, flags, cancellable, error);
5473 /********************************************
5474 * Default implementation of async ops *
5475 ********************************************/
5479 GFileQueryInfoFlags flags;
5480 } QueryInfoAsyncData;
5483 query_info_data_free (QueryInfoAsyncData *data)
5485 g_free (data->attributes);
5490 query_info_async_thread (GTask *task,
5493 GCancellable *cancellable)
5495 QueryInfoAsyncData *data = task_data;
5497 GError *error = NULL;
5499 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5501 g_task_return_pointer (task, info, g_object_unref);
5503 g_task_return_error (task, error);
5507 g_file_real_query_info_async (GFile *file,
5508 const char *attributes,
5509 GFileQueryInfoFlags flags,
5511 GCancellable *cancellable,
5512 GAsyncReadyCallback callback,
5516 QueryInfoAsyncData *data;
5518 data = g_new0 (QueryInfoAsyncData, 1);
5519 data->attributes = g_strdup (attributes);
5520 data->flags = flags;
5522 task = g_task_new (file, cancellable, callback, user_data);
5523 g_task_set_source_tag (task, g_file_real_query_info_async);
5524 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5525 g_task_set_priority (task, io_priority);
5526 g_task_run_in_thread (task, query_info_async_thread);
5527 g_object_unref (task);
5531 g_file_real_query_info_finish (GFile *file,
5535 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5537 return g_task_propagate_pointer (G_TASK (res), error);
5541 query_filesystem_info_async_thread (GTask *task,
5544 GCancellable *cancellable)
5546 const char *attributes = task_data;
5548 GError *error = NULL;
5550 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5552 g_task_return_pointer (task, info, g_object_unref);
5554 g_task_return_error (task, error);
5558 g_file_real_query_filesystem_info_async (GFile *file,
5559 const char *attributes,
5561 GCancellable *cancellable,
5562 GAsyncReadyCallback callback,
5567 task = g_task_new (file, cancellable, callback, user_data);
5568 g_task_set_source_tag (task, g_file_real_query_filesystem_info_async);
5569 g_task_set_task_data (task, g_strdup (attributes), g_free);
5570 g_task_set_priority (task, io_priority);
5571 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5572 g_object_unref (task);
5576 g_file_real_query_filesystem_info_finish (GFile *file,
5580 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5582 return g_task_propagate_pointer (G_TASK (res), error);
5586 enumerate_children_async_thread (GTask *task,
5589 GCancellable *cancellable)
5591 QueryInfoAsyncData *data = task_data;
5592 GFileEnumerator *enumerator;
5593 GError *error = NULL;
5595 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5597 g_task_return_error (task, error);
5599 g_task_return_pointer (task, enumerator, g_object_unref);
5603 g_file_real_enumerate_children_async (GFile *file,
5604 const char *attributes,
5605 GFileQueryInfoFlags flags,
5607 GCancellable *cancellable,
5608 GAsyncReadyCallback callback,
5612 QueryInfoAsyncData *data;
5614 data = g_new0 (QueryInfoAsyncData, 1);
5615 data->attributes = g_strdup (attributes);
5616 data->flags = flags;
5618 task = g_task_new (file, cancellable, callback, user_data);
5619 g_task_set_source_tag (task, g_file_real_enumerate_children_async);
5620 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5621 g_task_set_priority (task, io_priority);
5622 g_task_run_in_thread (task, enumerate_children_async_thread);
5623 g_object_unref (task);
5626 static GFileEnumerator *
5627 g_file_real_enumerate_children_finish (GFile *file,
5631 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5633 return g_task_propagate_pointer (G_TASK (res), error);
5637 open_read_async_thread (GTask *task,
5640 GCancellable *cancellable)
5642 GFileInputStream *stream;
5643 GError *error = NULL;
5645 stream = g_file_read (G_FILE (object), cancellable, &error);
5647 g_task_return_pointer (task, stream, g_object_unref);
5649 g_task_return_error (task, error);
5653 g_file_real_read_async (GFile *file,
5655 GCancellable *cancellable,
5656 GAsyncReadyCallback callback,
5661 task = g_task_new (file, cancellable, callback, user_data);
5662 g_task_set_source_tag (task, g_file_real_read_async);
5663 g_task_set_priority (task, io_priority);
5664 g_task_run_in_thread (task, open_read_async_thread);
5665 g_object_unref (task);
5668 static GFileInputStream *
5669 g_file_real_read_finish (GFile *file,
5673 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5675 return g_task_propagate_pointer (G_TASK (res), error);
5679 append_to_async_thread (GTask *task,
5680 gpointer source_object,
5682 GCancellable *cancellable)
5684 GFileCreateFlags *data = task_data;
5685 GFileOutputStream *stream;
5686 GError *error = NULL;
5688 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5690 g_task_return_pointer (task, stream, g_object_unref);
5692 g_task_return_error (task, error);
5696 g_file_real_append_to_async (GFile *file,
5697 GFileCreateFlags flags,
5699 GCancellable *cancellable,
5700 GAsyncReadyCallback callback,
5703 GFileCreateFlags *data;
5706 data = g_new0 (GFileCreateFlags, 1);
5709 task = g_task_new (file, cancellable, callback, user_data);
5710 g_task_set_source_tag (task, g_file_real_append_to_async);
5711 g_task_set_task_data (task, data, g_free);
5712 g_task_set_priority (task, io_priority);
5714 g_task_run_in_thread (task, append_to_async_thread);
5715 g_object_unref (task);
5718 static GFileOutputStream *
5719 g_file_real_append_to_finish (GFile *file,
5723 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5725 return g_task_propagate_pointer (G_TASK (res), error);
5729 create_async_thread (GTask *task,
5730 gpointer source_object,
5732 GCancellable *cancellable)
5734 GFileCreateFlags *data = task_data;
5735 GFileOutputStream *stream;
5736 GError *error = NULL;
5738 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5740 g_task_return_pointer (task, stream, g_object_unref);
5742 g_task_return_error (task, error);
5746 g_file_real_create_async (GFile *file,
5747 GFileCreateFlags flags,
5749 GCancellable *cancellable,
5750 GAsyncReadyCallback callback,
5753 GFileCreateFlags *data;
5756 data = g_new0 (GFileCreateFlags, 1);
5759 task = g_task_new (file, cancellable, callback, user_data);
5760 g_task_set_source_tag (task, g_file_real_create_async);
5761 g_task_set_task_data (task, data, g_free);
5762 g_task_set_priority (task, io_priority);
5764 g_task_run_in_thread (task, create_async_thread);
5765 g_object_unref (task);
5768 static GFileOutputStream *
5769 g_file_real_create_finish (GFile *file,
5773 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5775 return g_task_propagate_pointer (G_TASK (res), error);
5779 GFileOutputStream *stream;
5781 gboolean make_backup;
5782 GFileCreateFlags flags;
5786 replace_async_data_free (ReplaceAsyncData *data)
5789 g_object_unref (data->stream);
5790 g_free (data->etag);
5795 replace_async_thread (GTask *task,
5796 gpointer source_object,
5798 GCancellable *cancellable)
5800 GFileOutputStream *stream;
5801 ReplaceAsyncData *data = task_data;
5802 GError *error = NULL;
5804 stream = g_file_replace (G_FILE (source_object),
5812 g_task_return_pointer (task, stream, g_object_unref);
5814 g_task_return_error (task, error);
5818 g_file_real_replace_async (GFile *file,
5820 gboolean make_backup,
5821 GFileCreateFlags flags,
5823 GCancellable *cancellable,
5824 GAsyncReadyCallback callback,
5828 ReplaceAsyncData *data;
5830 data = g_new0 (ReplaceAsyncData, 1);
5831 data->etag = g_strdup (etag);
5832 data->make_backup = make_backup;
5833 data->flags = flags;
5835 task = g_task_new (file, cancellable, callback, user_data);
5836 g_task_set_source_tag (task, g_file_real_replace_async);
5837 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5838 g_task_set_priority (task, io_priority);
5840 g_task_run_in_thread (task, replace_async_thread);
5841 g_object_unref (task);
5844 static GFileOutputStream *
5845 g_file_real_replace_finish (GFile *file,
5849 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5851 return g_task_propagate_pointer (G_TASK (res), error);
5855 delete_async_thread (GTask *task,
5858 GCancellable *cancellable)
5860 GError *error = NULL;
5862 if (g_file_delete (G_FILE (object), cancellable, &error))
5863 g_task_return_boolean (task, TRUE);
5865 g_task_return_error (task, error);
5869 g_file_real_delete_async (GFile *file,
5871 GCancellable *cancellable,
5872 GAsyncReadyCallback callback,
5877 task = g_task_new (file, cancellable, callback, user_data);
5878 g_task_set_source_tag (task, g_file_real_delete_async);
5879 g_task_set_priority (task, io_priority);
5880 g_task_run_in_thread (task, delete_async_thread);
5881 g_object_unref (task);
5885 g_file_real_delete_finish (GFile *file,
5889 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5891 return g_task_propagate_boolean (G_TASK (res), error);
5895 trash_async_thread (GTask *task,
5898 GCancellable *cancellable)
5900 GError *error = NULL;
5902 if (g_file_trash (G_FILE (object), cancellable, &error))
5903 g_task_return_boolean (task, TRUE);
5905 g_task_return_error (task, error);
5909 g_file_real_trash_async (GFile *file,
5911 GCancellable *cancellable,
5912 GAsyncReadyCallback callback,
5917 task = g_task_new (file, cancellable, callback, user_data);
5918 g_task_set_source_tag (task, g_file_real_trash_async);
5919 g_task_set_priority (task, io_priority);
5920 g_task_run_in_thread (task, trash_async_thread);
5921 g_object_unref (task);
5925 g_file_real_trash_finish (GFile *file,
5929 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5931 return g_task_propagate_boolean (G_TASK (res), error);
5935 make_directory_async_thread (GTask *task,
5938 GCancellable *cancellable)
5940 GError *error = NULL;
5942 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5943 g_task_return_boolean (task, TRUE);
5945 g_task_return_error (task, error);
5949 g_file_real_make_directory_async (GFile *file,
5951 GCancellable *cancellable,
5952 GAsyncReadyCallback callback,
5957 task = g_task_new (file, cancellable, callback, user_data);
5958 g_task_set_source_tag (task, g_file_real_make_directory_async);
5959 g_task_set_priority (task, io_priority);
5960 g_task_run_in_thread (task, make_directory_async_thread);
5961 g_object_unref (task);
5965 g_file_real_make_directory_finish (GFile *file,
5969 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5971 return g_task_propagate_boolean (G_TASK (res), error);
5975 open_readwrite_async_thread (GTask *task,
5978 GCancellable *cancellable)
5980 GFileIOStream *stream;
5981 GError *error = NULL;
5983 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5986 g_task_return_error (task, error);
5988 g_task_return_pointer (task, stream, g_object_unref);
5992 g_file_real_open_readwrite_async (GFile *file,
5994 GCancellable *cancellable,
5995 GAsyncReadyCallback callback,
6000 task = g_task_new (file, cancellable, callback, user_data);
6001 g_task_set_source_tag (task, g_file_real_open_readwrite_async);
6002 g_task_set_priority (task, io_priority);
6004 g_task_run_in_thread (task, open_readwrite_async_thread);
6005 g_object_unref (task);
6008 static GFileIOStream *
6009 g_file_real_open_readwrite_finish (GFile *file,
6013 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6015 return g_task_propagate_pointer (G_TASK (res), error);
6019 create_readwrite_async_thread (GTask *task,
6022 GCancellable *cancellable)
6024 GFileCreateFlags *data = task_data;
6025 GFileIOStream *stream;
6026 GError *error = NULL;
6028 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
6031 g_task_return_error (task, error);
6033 g_task_return_pointer (task, stream, g_object_unref);
6037 g_file_real_create_readwrite_async (GFile *file,
6038 GFileCreateFlags flags,
6040 GCancellable *cancellable,
6041 GAsyncReadyCallback callback,
6044 GFileCreateFlags *data;
6047 data = g_new0 (GFileCreateFlags, 1);
6050 task = g_task_new (file, cancellable, callback, user_data);
6051 g_task_set_source_tag (task, g_file_real_create_readwrite_async);
6052 g_task_set_task_data (task, data, g_free);
6053 g_task_set_priority (task, io_priority);
6055 g_task_run_in_thread (task, create_readwrite_async_thread);
6056 g_object_unref (task);
6059 static GFileIOStream *
6060 g_file_real_create_readwrite_finish (GFile *file,
6064 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6066 return g_task_propagate_pointer (G_TASK (res), error);
6071 gboolean make_backup;
6072 GFileCreateFlags flags;
6073 } ReplaceRWAsyncData;
6076 replace_rw_async_data_free (ReplaceRWAsyncData *data)
6078 g_free (data->etag);
6083 replace_readwrite_async_thread (GTask *task,
6086 GCancellable *cancellable)
6088 GFileIOStream *stream;
6089 GError *error = NULL;
6090 ReplaceRWAsyncData *data = task_data;
6092 stream = g_file_replace_readwrite (G_FILE (object),
6100 g_task_return_error (task, error);
6102 g_task_return_pointer (task, stream, g_object_unref);
6106 g_file_real_replace_readwrite_async (GFile *file,
6108 gboolean make_backup,
6109 GFileCreateFlags flags,
6111 GCancellable *cancellable,
6112 GAsyncReadyCallback callback,
6116 ReplaceRWAsyncData *data;
6118 data = g_new0 (ReplaceRWAsyncData, 1);
6119 data->etag = g_strdup (etag);
6120 data->make_backup = make_backup;
6121 data->flags = flags;
6123 task = g_task_new (file, cancellable, callback, user_data);
6124 g_task_set_source_tag (task, g_file_real_replace_readwrite_async);
6125 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
6126 g_task_set_priority (task, io_priority);
6128 g_task_run_in_thread (task, replace_readwrite_async_thread);
6129 g_object_unref (task);
6132 static GFileIOStream *
6133 g_file_real_replace_readwrite_finish (GFile *file,
6137 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6139 return g_task_propagate_pointer (G_TASK (res), error);
6143 set_display_name_async_thread (GTask *task,
6146 GCancellable *cancellable)
6148 GError *error = NULL;
6149 char *name = task_data;
6152 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6155 g_task_return_error (task, error);
6157 g_task_return_pointer (task, file, g_object_unref);
6161 g_file_real_set_display_name_async (GFile *file,
6162 const char *display_name,
6164 GCancellable *cancellable,
6165 GAsyncReadyCallback callback,
6170 task = g_task_new (file, cancellable, callback, user_data);
6171 g_task_set_source_tag (task, g_file_real_set_display_name_async);
6172 g_task_set_task_data (task, g_strdup (display_name), g_free);
6173 g_task_set_priority (task, io_priority);
6175 g_task_run_in_thread (task, set_display_name_async_thread);
6176 g_object_unref (task);
6180 g_file_real_set_display_name_finish (GFile *file,
6184 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6186 return g_task_propagate_pointer (G_TASK (res), error);
6190 GFileQueryInfoFlags flags;
6197 set_info_data_free (SetInfoAsyncData *data)
6200 g_object_unref (data->info);
6202 g_error_free (data->error);
6207 set_info_async_thread (GTask *task,
6210 GCancellable *cancellable)
6212 SetInfoAsyncData *data = task_data;
6215 data->res = g_file_set_attributes_from_info (G_FILE (object),
6223 g_file_real_set_attributes_async (GFile *file,
6225 GFileQueryInfoFlags flags,
6227 GCancellable *cancellable,
6228 GAsyncReadyCallback callback,
6232 SetInfoAsyncData *data;
6234 data = g_new0 (SetInfoAsyncData, 1);
6235 data->info = g_file_info_dup (info);
6236 data->flags = flags;
6238 task = g_task_new (file, cancellable, callback, user_data);
6239 g_task_set_source_tag (task, g_file_real_set_attributes_async);
6240 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6241 g_task_set_priority (task, io_priority);
6243 g_task_run_in_thread (task, set_info_async_thread);
6244 g_object_unref (task);
6248 g_file_real_set_attributes_finish (GFile *file,
6253 SetInfoAsyncData *data;
6255 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6257 data = g_task_get_task_data (G_TASK (res));
6260 *info = g_object_ref (data->info);
6262 if (error != NULL && data->error)
6263 *error = g_error_copy (data->error);
6269 find_enclosing_mount_async_thread (GTask *task,
6272 GCancellable *cancellable)
6274 GError *error = NULL;
6277 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6280 g_task_return_error (task, error);
6282 g_task_return_pointer (task, mount, g_object_unref);
6286 g_file_real_find_enclosing_mount_async (GFile *file,
6288 GCancellable *cancellable,
6289 GAsyncReadyCallback callback,
6294 task = g_task_new (file, cancellable, callback, user_data);
6295 g_task_set_source_tag (task, g_file_real_find_enclosing_mount_async);
6296 g_task_set_priority (task, io_priority);
6298 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6299 g_object_unref (task);
6303 g_file_real_find_enclosing_mount_finish (GFile *file,
6307 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6309 return g_task_propagate_pointer (G_TASK (res), error);
6316 GFileCopyFlags flags;
6317 GFileProgressCallback progress_cb;
6318 gpointer progress_cb_data;
6322 copy_async_data_free (CopyAsyncData *data)
6324 g_object_unref (data->source);
6325 g_object_unref (data->destination);
6326 g_slice_free (CopyAsyncData, data);
6330 CopyAsyncData *data;
6331 goffset current_num_bytes;
6332 goffset total_num_bytes;
6336 copy_async_progress_in_main (gpointer user_data)
6338 ProgressData *progress = user_data;
6339 CopyAsyncData *data = progress->data;
6341 data->progress_cb (progress->current_num_bytes,
6342 progress->total_num_bytes,
6343 data->progress_cb_data);
6349 copy_async_progress_callback (goffset current_num_bytes,
6350 goffset total_num_bytes,
6353 GTask *task = user_data;
6354 CopyAsyncData *data = g_task_get_task_data (task);
6355 ProgressData *progress;
6357 progress = g_new (ProgressData, 1);
6358 progress->data = data;
6359 progress->current_num_bytes = current_num_bytes;
6360 progress->total_num_bytes = total_num_bytes;
6362 g_main_context_invoke_full (g_task_get_context (task),
6363 g_task_get_priority (task),
6364 copy_async_progress_in_main,
6370 copy_async_thread (GTask *task,
6373 GCancellable *cancellable)
6375 CopyAsyncData *data = task_data;
6377 GError *error = NULL;
6379 result = g_file_copy (data->source,
6383 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6387 g_task_return_boolean (task, TRUE);
6389 g_task_return_error (task, error);
6393 g_file_real_copy_async (GFile *source,
6395 GFileCopyFlags flags,
6397 GCancellable *cancellable,
6398 GFileProgressCallback progress_callback,
6399 gpointer progress_callback_data,
6400 GAsyncReadyCallback callback,
6404 CopyAsyncData *data;
6406 data = g_slice_new (CopyAsyncData);
6407 data->source = g_object_ref (source);
6408 data->destination = g_object_ref (destination);
6409 data->flags = flags;
6410 data->progress_cb = progress_callback;
6411 data->progress_cb_data = progress_callback_data;
6413 task = g_task_new (source, cancellable, callback, user_data);
6414 g_task_set_source_tag (task, g_file_real_copy_async);
6415 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6416 g_task_set_priority (task, io_priority);
6417 g_task_run_in_thread (task, copy_async_thread);
6418 g_object_unref (task);
6422 g_file_real_copy_finish (GFile *file,
6426 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6428 return g_task_propagate_boolean (G_TASK (res), error);
6432 /********************************************
6433 * Default VFS operations *
6434 ********************************************/
6437 * g_file_new_for_path:
6438 * @path: (type filename): a string containing a relative or absolute path.
6439 * The string must be encoded in the glib filename encoding.
6441 * Constructs a #GFile for a given path. This operation never
6442 * fails, but the returned object might not support any I/O
6443 * operation if @path is malformed.
6445 * Returns: (transfer full): a new #GFile for the given @path.
6446 * Free the returned object with g_object_unref().
6449 g_file_new_for_path (const char *path)
6451 g_return_val_if_fail (path != NULL, NULL);
6453 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6457 * g_file_new_for_uri:
6458 * @uri: a UTF-8 string containing a URI
6460 * Constructs a #GFile for a given URI. This operation never
6461 * fails, but the returned object might not support any I/O
6462 * operation if @uri is malformed or if the uri type is
6465 * Returns: (transfer full): a new #GFile for the given @uri.
6466 * Free the returned object with g_object_unref().
6469 g_file_new_for_uri (const char *uri)
6471 g_return_val_if_fail (uri != NULL, NULL);
6473 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6478 * @tmpl: (type filename) (nullable): Template for the file
6479 * name, as in g_file_open_tmp(), or %NULL for a default template
6480 * @iostream: (out): on return, a #GFileIOStream for the created file
6481 * @error: a #GError, or %NULL
6483 * Opens a file in the preferred directory for temporary files (as
6484 * returned by g_get_tmp_dir()) and returns a #GFile and
6485 * #GFileIOStream pointing to it.
6487 * @tmpl should be a string in the GLib file name encoding
6488 * containing a sequence of six 'X' characters, and containing no
6489 * directory components. If it is %NULL, a default template is used.
6491 * Unlike the other #GFile constructors, this will return %NULL if
6492 * a temporary file could not be created.
6494 * Returns: (transfer full): a new #GFile.
6495 * Free the returned object with g_object_unref().
6500 g_file_new_tmp (const char *tmpl,
6501 GFileIOStream **iostream,
6507 GFileOutputStream *output;
6509 g_return_val_if_fail (iostream != NULL, NULL);
6511 fd = g_file_open_tmp (tmpl, &path, error);
6515 file = g_file_new_for_path (path);
6517 output = _g_local_file_output_stream_new (fd);
6518 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6520 g_object_unref (output);
6527 * g_file_parse_name:
6528 * @parse_name: a file name or path to be parsed
6530 * Constructs a #GFile with the given @parse_name (i.e. something
6531 * given by g_file_get_parse_name()). This operation never fails,
6532 * but the returned object might not support any I/O operation if
6533 * the @parse_name cannot be parsed.
6535 * Returns: (transfer full): a new #GFile.
6538 g_file_parse_name (const char *parse_name)
6540 g_return_val_if_fail (parse_name != NULL, NULL);
6542 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6546 * g_file_new_build_filename:
6547 * @first_element: (type filename): the first element in the path
6548 * @...: remaining elements in path, terminated by %NULL
6550 * Constructs a #GFile from a series of elements using the correct
6551 * separator for filenames.
6553 * Using this function is equivalent to calling g_build_filename(),
6554 * followed by g_file_new_for_path() on the result.
6556 * Returns: (transfer full): a new #GFile
6561 g_file_new_build_filename (const gchar *first_element,
6568 g_return_val_if_fail (first_element != NULL, NULL);
6570 va_start (args, first_element);
6571 str = g_build_filename_valist (first_element, &args);
6574 file = g_file_new_for_path (str);
6581 is_valid_scheme_character (char c)
6583 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6586 /* Following RFC 2396, valid schemes are built like:
6587 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6590 has_valid_scheme (const char *uri)
6596 if (!g_ascii_isalpha (*p))
6601 } while (is_valid_scheme_character (*p));
6607 new_for_cmdline_arg (const gchar *arg,
6613 if (g_path_is_absolute (arg))
6614 return g_file_new_for_path (arg);
6616 if (has_valid_scheme (arg))
6617 return g_file_new_for_uri (arg);
6623 current_dir = g_get_current_dir ();
6624 filename = g_build_filename (current_dir, arg, NULL);
6625 g_free (current_dir);
6628 filename = g_build_filename (cwd, arg, NULL);
6630 file = g_file_new_for_path (filename);
6637 * g_file_new_for_commandline_arg:
6638 * @arg: (type filename): a command line string
6640 * Creates a #GFile with the given argument from the command line.
6641 * The value of @arg can be either a URI, an absolute path or a
6642 * relative path resolved relative to the current working directory.
6643 * This operation never fails, but the returned object might not
6644 * support any I/O operation if @arg points to a malformed path.
6646 * Note that on Windows, this function expects its argument to be in
6647 * UTF-8 -- not the system code page. This means that you
6648 * should not use this function with string from argv as it is passed
6649 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6650 * the commandline. #GApplication also uses UTF-8 but
6651 * g_application_command_line_create_file_for_arg() may be more useful
6652 * for you there. It is also always possible to use this function with
6653 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6655 * Returns: (transfer full): a new #GFile.
6656 * Free the returned object with g_object_unref().
6659 g_file_new_for_commandline_arg (const char *arg)
6661 g_return_val_if_fail (arg != NULL, NULL);
6663 return new_for_cmdline_arg (arg, NULL);
6667 * g_file_new_for_commandline_arg_and_cwd:
6668 * @arg: (type filename): a command line string
6669 * @cwd: (type filename): the current working directory of the commandline
6671 * Creates a #GFile with the given argument from the command line.
6673 * This function is similar to g_file_new_for_commandline_arg() except
6674 * that it allows for passing the current working directory as an
6675 * argument instead of using the current working directory of the
6678 * This is useful if the commandline argument was given in a context
6679 * other than the invocation of the current process.
6681 * See also g_application_command_line_create_file_for_arg().
6683 * Returns: (transfer full): a new #GFile
6688 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6691 g_return_val_if_fail (arg != NULL, NULL);
6692 g_return_val_if_fail (cwd != NULL, NULL);
6694 return new_for_cmdline_arg (arg, cwd);
6698 * g_file_mount_enclosing_volume:
6699 * @location: input #GFile
6700 * @flags: flags affecting the operation
6701 * @mount_operation: (nullable): a #GMountOperation
6702 * or %NULL to avoid user interaction
6703 * @cancellable: (nullable): optional #GCancellable object,
6705 * @callback: (nullable): a #GAsyncReadyCallback to call
6706 * when the request is satisfied, or %NULL
6707 * @user_data: the data to pass to callback function
6709 * Starts a @mount_operation, mounting the volume that contains
6710 * the file @location.
6712 * When this operation has completed, @callback will be called with
6713 * @user_user data, and the operation can be finalized with
6714 * g_file_mount_enclosing_volume_finish().
6716 * If @cancellable is not %NULL, then the operation can be cancelled by
6717 * triggering the cancellable object from another thread. If the operation
6718 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6721 g_file_mount_enclosing_volume (GFile *location,
6722 GMountMountFlags flags,
6723 GMountOperation *mount_operation,
6724 GCancellable *cancellable,
6725 GAsyncReadyCallback callback,
6730 g_return_if_fail (G_IS_FILE (location));
6732 iface = G_FILE_GET_IFACE (location);
6734 if (iface->mount_enclosing_volume == NULL)
6736 g_task_report_new_error (location, callback, user_data,
6737 g_file_mount_enclosing_volume,
6738 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6739 _("volume doesn’t implement mount"));
6743 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6748 * g_file_mount_enclosing_volume_finish:
6749 * @location: input #GFile
6750 * @result: a #GAsyncResult
6751 * @error: a #GError, or %NULL
6753 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6755 * Returns: %TRUE if successful. If an error has occurred,
6756 * this function will return %FALSE and set @error
6757 * appropriately if present.
6760 g_file_mount_enclosing_volume_finish (GFile *location,
6761 GAsyncResult *result,
6766 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6767 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6769 if (g_async_result_legacy_propagate_error (result, error))
6771 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6772 return g_task_propagate_boolean (G_TASK (result), error);
6774 iface = G_FILE_GET_IFACE (location);
6776 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6779 /********************************************
6780 * Utility functions *
6781 ********************************************/
6784 * g_file_query_default_handler:
6785 * @file: a #GFile to open
6786 * @cancellable: optional #GCancellable object, %NULL to ignore
6787 * @error: a #GError, or %NULL
6789 * Returns the #GAppInfo that is registered as the default
6790 * application to handle the file specified by @file.
6792 * If @cancellable is not %NULL, then the operation can be cancelled by
6793 * triggering the cancellable object from another thread. If the operation
6794 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6796 * Returns: (transfer full): a #GAppInfo if the handle was found,
6797 * %NULL if there were errors.
6798 * When you are done with it, release it with g_object_unref()
6801 g_file_query_default_handler (GFile *file,
6802 GCancellable *cancellable,
6806 const char *content_type;
6811 uri_scheme = g_file_get_uri_scheme (file);
6812 if (uri_scheme && uri_scheme[0] != '\0')
6814 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6815 g_free (uri_scheme);
6817 if (appinfo != NULL)
6821 info = g_file_query_info (file,
6822 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6831 content_type = g_file_info_get_content_type (info);
6834 /* Don't use is_native(), as we want to support fuse paths if available */
6835 path = g_file_get_path (file);
6836 appinfo = g_app_info_get_default_for_type (content_type,
6841 g_object_unref (info);
6843 if (appinfo != NULL)
6846 g_set_error_literal (error, G_IO_ERROR,
6847 G_IO_ERROR_NOT_SUPPORTED,
6848 _("No application is registered as handling this file"));
6852 #define GET_CONTENT_BLOCK_SIZE 8192
6855 * g_file_load_contents:
6856 * @file: input #GFile
6857 * @cancellable: optional #GCancellable object, %NULL to ignore
6858 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6859 * @length: (out) (optional): a location to place the length of the contents of the file,
6860 * or %NULL if the length is not needed
6861 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
6862 * or %NULL if the entity tag is not needed
6863 * @error: a #GError, or %NULL
6865 * Loads the content of the file into memory. The data is always
6866 * zero-terminated, but this is not included in the resultant @length.
6867 * The returned @content should be freed with g_free() when no longer
6870 * If @cancellable is not %NULL, then the operation can be cancelled by
6871 * triggering the cancellable object from another thread. If the operation
6872 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6874 * Returns: %TRUE if the @file's contents were successfully loaded.
6875 * %FALSE if there were errors.
6878 g_file_load_contents (GFile *file,
6879 GCancellable *cancellable,
6885 GFileInputStream *in;
6886 GByteArray *content;
6891 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6892 g_return_val_if_fail (contents != NULL, FALSE);
6894 in = g_file_read (file, cancellable, error);
6898 content = g_byte_array_new ();
6901 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6902 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6903 content->data + pos,
6904 GET_CONTENT_BLOCK_SIZE,
6905 cancellable, error)) > 0)
6908 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6915 info = g_file_input_stream_query_info (in,
6916 G_FILE_ATTRIBUTE_ETAG_VALUE,
6921 *etag_out = g_strdup (g_file_info_get_etag (info));
6922 g_object_unref (info);
6926 /* Ignore errors on close */
6927 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6928 g_object_unref (in);
6932 /* error is set already */
6933 g_byte_array_free (content, TRUE);
6940 /* Zero terminate (we got an extra byte allocated for this */
6941 content->data[pos] = 0;
6943 *contents = (char *)g_byte_array_free (content, FALSE);
6950 GFileReadMoreCallback read_more_callback;
6951 GByteArray *content;
6958 load_contents_data_free (LoadContentsData *data)
6961 g_byte_array_free (data->content, TRUE);
6962 g_free (data->etag);
6967 load_contents_close_callback (GObject *obj,
6968 GAsyncResult *close_res,
6971 GInputStream *stream = G_INPUT_STREAM (obj);
6972 LoadContentsData *data = user_data;
6974 /* Ignore errors here, we're only reading anyway */
6975 g_input_stream_close_finish (stream, close_res, NULL);
6976 g_object_unref (stream);
6978 g_task_return_boolean (data->task, TRUE);
6979 g_object_unref (data->task);
6983 load_contents_fstat_callback (GObject *obj,
6984 GAsyncResult *stat_res,
6987 GInputStream *stream = G_INPUT_STREAM (obj);
6988 LoadContentsData *data = user_data;
6991 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6995 data->etag = g_strdup (g_file_info_get_etag (info));
6996 g_object_unref (info);
6999 g_input_stream_close_async (stream, 0,
7000 g_task_get_cancellable (data->task),
7001 load_contents_close_callback, data);
7005 load_contents_read_callback (GObject *obj,
7006 GAsyncResult *read_res,
7009 GInputStream *stream = G_INPUT_STREAM (obj);
7010 LoadContentsData *data = user_data;
7011 GError *error = NULL;
7014 read_size = g_input_stream_read_finish (stream, read_res, &error);
7018 g_task_return_error (data->task, error);
7019 g_object_unref (data->task);
7021 /* Close the file ignoring any error */
7022 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
7023 g_object_unref (stream);
7025 else if (read_size == 0)
7027 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
7028 G_FILE_ATTRIBUTE_ETAG_VALUE,
7030 g_task_get_cancellable (data->task),
7031 load_contents_fstat_callback,
7034 else if (read_size > 0)
7036 data->pos += read_size;
7038 g_byte_array_set_size (data->content,
7039 data->pos + GET_CONTENT_BLOCK_SIZE);
7042 if (data->read_more_callback &&
7043 !data->read_more_callback ((char *)data->content->data, data->pos,
7044 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
7045 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
7046 G_FILE_ATTRIBUTE_ETAG_VALUE,
7048 g_task_get_cancellable (data->task),
7049 load_contents_fstat_callback,
7052 g_input_stream_read_async (stream,
7053 data->content->data + data->pos,
7054 GET_CONTENT_BLOCK_SIZE,
7056 g_task_get_cancellable (data->task),
7057 load_contents_read_callback,
7063 load_contents_open_callback (GObject *obj,
7064 GAsyncResult *open_res,
7067 GFile *file = G_FILE (obj);
7068 GFileInputStream *stream;
7069 LoadContentsData *data = user_data;
7070 GError *error = NULL;
7072 stream = g_file_read_finish (file, open_res, &error);
7076 g_byte_array_set_size (data->content,
7077 data->pos + GET_CONTENT_BLOCK_SIZE);
7078 g_input_stream_read_async (G_INPUT_STREAM (stream),
7079 data->content->data + data->pos,
7080 GET_CONTENT_BLOCK_SIZE,
7082 g_task_get_cancellable (data->task),
7083 load_contents_read_callback,
7088 g_task_return_error (data->task, error);
7089 g_object_unref (data->task);
7094 * g_file_load_partial_contents_async: (skip)
7095 * @file: input #GFile
7096 * @cancellable: optional #GCancellable object, %NULL to ignore
7097 * @read_more_callback: (scope call) (closure user_data): a
7098 * #GFileReadMoreCallback to receive partial data
7099 * and to specify whether further data should be read
7100 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback to call
7101 * when the request is satisfied
7102 * @user_data: the data to pass to the callback functions
7104 * Reads the partial contents of a file. A #GFileReadMoreCallback should
7105 * be used to stop reading from the file when appropriate, else this
7106 * function will behave exactly as g_file_load_contents_async(). This
7107 * operation can be finished by g_file_load_partial_contents_finish().
7109 * Users of this function should be aware that @user_data is passed to
7110 * both the @read_more_callback and the @callback.
7112 * If @cancellable is not %NULL, then the operation can be cancelled by
7113 * triggering the cancellable object from another thread. If the operation
7114 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7117 g_file_load_partial_contents_async (GFile *file,
7118 GCancellable *cancellable,
7119 GFileReadMoreCallback read_more_callback,
7120 GAsyncReadyCallback callback,
7123 LoadContentsData *data;
7125 g_return_if_fail (G_IS_FILE (file));
7127 data = g_new0 (LoadContentsData, 1);
7128 data->read_more_callback = read_more_callback;
7129 data->content = g_byte_array_new ();
7131 data->task = g_task_new (file, cancellable, callback, user_data);
7132 g_task_set_source_tag (data->task, g_file_load_partial_contents_async);
7133 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
7135 g_file_read_async (file,
7137 g_task_get_cancellable (data->task),
7138 load_contents_open_callback,
7143 * g_file_load_partial_contents_finish:
7144 * @file: input #GFile
7145 * @res: a #GAsyncResult
7146 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7147 * @length: (out) (optional): a location to place the length of the contents of the file,
7148 * or %NULL if the length is not needed
7149 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7150 * or %NULL if the entity tag is not needed
7151 * @error: a #GError, or %NULL
7153 * Finishes an asynchronous partial load operation that was started
7154 * with g_file_load_partial_contents_async(). The data is always
7155 * zero-terminated, but this is not included in the resultant @length.
7156 * The returned @content should be freed with g_free() when no longer
7159 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7160 * present, it will be set appropriately.
7163 g_file_load_partial_contents_finish (GFile *file,
7171 LoadContentsData *data;
7173 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7174 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7175 g_return_val_if_fail (contents != NULL, FALSE);
7177 task = G_TASK (res);
7179 if (!g_task_propagate_boolean (task, error))
7186 data = g_task_get_task_data (task);
7189 *length = data->pos;
7193 *etag_out = data->etag;
7197 /* Zero terminate */
7198 g_byte_array_set_size (data->content, data->pos + 1);
7199 data->content->data[data->pos] = 0;
7201 *contents = (char *)g_byte_array_free (data->content, FALSE);
7202 data->content = NULL;
7208 * g_file_load_contents_async:
7209 * @file: input #GFile
7210 * @cancellable: optional #GCancellable object, %NULL to ignore
7211 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7212 * @user_data: the data to pass to callback function
7214 * Starts an asynchronous load of the @file's contents.
7216 * For more details, see g_file_load_contents() which is
7217 * the synchronous version of this call.
7219 * When the load operation has completed, @callback will be called
7220 * with @user data. To finish the operation, call
7221 * g_file_load_contents_finish() with the #GAsyncResult returned by
7224 * If @cancellable is not %NULL, then the operation can be cancelled by
7225 * triggering the cancellable object from another thread. If the operation
7226 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7229 g_file_load_contents_async (GFile *file,
7230 GCancellable *cancellable,
7231 GAsyncReadyCallback callback,
7234 g_file_load_partial_contents_async (file,
7237 callback, user_data);
7241 * g_file_load_contents_finish:
7242 * @file: input #GFile
7243 * @res: a #GAsyncResult
7244 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7245 * @length: (out) (optional): a location to place the length of the contents of the file,
7246 * or %NULL if the length is not needed
7247 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7248 * or %NULL if the entity tag is not needed
7249 * @error: a #GError, or %NULL
7251 * Finishes an asynchronous load of the @file's contents.
7252 * The contents are placed in @contents, and @length is set to the
7253 * size of the @contents string. The @content should be freed with
7254 * g_free() when no longer needed. If @etag_out is present, it will be
7255 * set to the new entity tag for the @file.
7257 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7258 * present, it will be set appropriately.
7261 g_file_load_contents_finish (GFile *file,
7268 return g_file_load_partial_contents_finish (file,
7277 * g_file_replace_contents:
7278 * @file: input #GFile
7279 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7280 * @length: the length of @contents in bytes
7281 * @etag: (nullable): the old [entity-tag][gfile-etag] for the document,
7283 * @make_backup: %TRUE if a backup should be created
7284 * @flags: a set of #GFileCreateFlags
7285 * @new_etag: (out) (optional): a location to a new [entity tag][gfile-etag]
7286 * for the document. This should be freed with g_free() when no longer
7288 * @cancellable: optional #GCancellable object, %NULL to ignore
7289 * @error: a #GError, or %NULL
7291 * Replaces the contents of @file with @contents of @length bytes.
7293 * If @etag is specified (not %NULL), any existing file must have that etag,
7294 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7296 * If @make_backup is %TRUE, this function will attempt to make a backup
7297 * of @file. Internally, it uses g_file_replace(), so will try to replace the
7298 * file contents in the safest way possible. For example, atomic renames are
7299 * used when replacing local files’ contents.
7301 * If @cancellable is not %NULL, then the operation can be cancelled by
7302 * triggering the cancellable object from another thread. If the operation
7303 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7305 * The returned @new_etag can be used to verify that the file hasn't
7306 * changed the next time it is saved over.
7308 * Returns: %TRUE if successful. If an error has occurred, this function
7309 * will return %FALSE and set @error appropriately if present.
7312 g_file_replace_contents (GFile *file,
7313 const char *contents,
7316 gboolean make_backup,
7317 GFileCreateFlags flags,
7319 GCancellable *cancellable,
7322 GFileOutputStream *out;
7323 gsize pos, remainder;
7327 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7328 g_return_val_if_fail (contents != NULL, FALSE);
7330 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7336 while (remainder > 0 &&
7337 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7339 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7347 if (remainder > 0 && res < 0)
7349 /* Ignore errors on close */
7350 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7351 g_object_unref (out);
7353 /* error is set already */
7357 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7360 *new_etag = g_file_output_stream_get_etag (out);
7362 g_object_unref (out);
7373 } ReplaceContentsData;
7376 replace_contents_data_free (ReplaceContentsData *data)
7378 g_bytes_unref (data->content);
7379 g_free (data->etag);
7384 replace_contents_close_callback (GObject *obj,
7385 GAsyncResult *close_res,
7388 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7389 ReplaceContentsData *data = user_data;
7391 /* Ignore errors here, we're only reading anyway */
7392 g_output_stream_close_finish (stream, close_res, NULL);
7393 g_object_unref (stream);
7397 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7398 g_task_return_boolean (data->task, TRUE);
7400 g_object_unref (data->task);
7404 replace_contents_write_callback (GObject *obj,
7405 GAsyncResult *read_res,
7408 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7409 ReplaceContentsData *data = user_data;
7410 GError *error = NULL;
7413 write_size = g_output_stream_write_finish (stream, read_res, &error);
7415 if (write_size <= 0)
7417 /* Error or EOF, close the file */
7420 data->failed = TRUE;
7421 g_task_return_error (data->task, error);
7423 g_output_stream_close_async (stream, 0,
7424 g_task_get_cancellable (data->task),
7425 replace_contents_close_callback, data);
7427 else if (write_size > 0)
7429 const gchar *content;
7432 content = g_bytes_get_data (data->content, &length);
7433 data->pos += write_size;
7435 if (data->pos >= length)
7436 g_output_stream_close_async (stream, 0,
7437 g_task_get_cancellable (data->task),
7438 replace_contents_close_callback, data);
7440 g_output_stream_write_async (stream,
7441 content + data->pos,
7444 g_task_get_cancellable (data->task),
7445 replace_contents_write_callback,
7451 replace_contents_open_callback (GObject *obj,
7452 GAsyncResult *open_res,
7455 GFile *file = G_FILE (obj);
7456 GFileOutputStream *stream;
7457 ReplaceContentsData *data = user_data;
7458 GError *error = NULL;
7460 stream = g_file_replace_finish (file, open_res, &error);
7464 const gchar *content;
7467 content = g_bytes_get_data (data->content, &length);
7468 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7469 content + data->pos,
7472 g_task_get_cancellable (data->task),
7473 replace_contents_write_callback,
7478 g_task_return_error (data->task, error);
7479 g_object_unref (data->task);
7484 * g_file_replace_contents_async:
7485 * @file: input #GFile
7486 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7487 * @length: the length of @contents in bytes
7488 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7489 * @make_backup: %TRUE if a backup should be created
7490 * @flags: a set of #GFileCreateFlags
7491 * @cancellable: optional #GCancellable object, %NULL to ignore
7492 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7493 * @user_data: the data to pass to callback function
7495 * Starts an asynchronous replacement of @file with the given
7496 * @contents of @length bytes. @etag will replace the document's
7497 * current entity tag.
7499 * When this operation has completed, @callback will be called with
7500 * @user_user data, and the operation can be finalized with
7501 * g_file_replace_contents_finish().
7503 * If @cancellable is not %NULL, then the operation can be cancelled by
7504 * triggering the cancellable object from another thread. If the operation
7505 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7507 * If @make_backup is %TRUE, this function will attempt to
7508 * make a backup of @file.
7510 * Note that no copy of @content will be made, so it must stay valid
7511 * until @callback is called. See g_file_replace_contents_bytes_async()
7512 * for a #GBytes version that will automatically hold a reference to the
7513 * contents (without copying) for the duration of the call.
7516 g_file_replace_contents_async (GFile *file,
7517 const char *contents,
7520 gboolean make_backup,
7521 GFileCreateFlags flags,
7522 GCancellable *cancellable,
7523 GAsyncReadyCallback callback,
7528 bytes = g_bytes_new_static (contents, length);
7529 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7530 cancellable, callback, user_data);
7531 g_bytes_unref (bytes);
7535 * g_file_replace_contents_bytes_async:
7536 * @file: input #GFile
7537 * @contents: a #GBytes
7538 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7539 * @make_backup: %TRUE if a backup should be created
7540 * @flags: a set of #GFileCreateFlags
7541 * @cancellable: optional #GCancellable object, %NULL to ignore
7542 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7543 * @user_data: the data to pass to callback function
7545 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7546 * This function will keep a ref on @contents until the operation is done.
7547 * Unlike g_file_replace_contents_async() this allows forgetting about the
7548 * content without waiting for the callback.
7550 * When this operation has completed, @callback will be called with
7551 * @user_user data, and the operation can be finalized with
7552 * g_file_replace_contents_finish().
7557 g_file_replace_contents_bytes_async (GFile *file,
7560 gboolean make_backup,
7561 GFileCreateFlags flags,
7562 GCancellable *cancellable,
7563 GAsyncReadyCallback callback,
7566 ReplaceContentsData *data;
7568 g_return_if_fail (G_IS_FILE (file));
7569 g_return_if_fail (contents != NULL);
7571 data = g_new0 (ReplaceContentsData, 1);
7573 data->content = g_bytes_ref (contents);
7575 data->task = g_task_new (file, cancellable, callback, user_data);
7576 g_task_set_source_tag (data->task, g_file_replace_contents_bytes_async);
7577 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7579 g_file_replace_async (file,
7584 g_task_get_cancellable (data->task),
7585 replace_contents_open_callback,
7590 * g_file_replace_contents_finish:
7591 * @file: input #GFile
7592 * @res: a #GAsyncResult
7593 * @new_etag: (out) (optional): a location of a new [entity tag][gfile-etag]
7594 * for the document. This should be freed with g_free() when it is no
7595 * longer needed, or %NULL
7596 * @error: a #GError, or %NULL
7598 * Finishes an asynchronous replace of the given @file. See
7599 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7600 * tag for the document, if present.
7602 * Returns: %TRUE on success, %FALSE on failure.
7605 g_file_replace_contents_finish (GFile *file,
7611 ReplaceContentsData *data;
7613 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7614 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7616 task = G_TASK (res);
7618 if (!g_task_propagate_boolean (task, error))
7621 data = g_task_get_task_data (task);
7625 *new_etag = data->etag;
7626 data->etag = NULL; /* Take ownership */
7633 g_file_real_measure_disk_usage (GFile *file,
7634 GFileMeasureFlags flags,
7635 GCancellable *cancellable,
7636 GFileMeasureProgressCallback progress_callback,
7637 gpointer progress_data,
7638 guint64 *disk_usage,
7643 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7644 "Operation not supported for the current backend.");
7650 GFileMeasureFlags flags;
7651 GFileMeasureProgressCallback progress_callback;
7652 gpointer progress_data;
7664 GFileMeasureProgressCallback callback;
7667 guint64 current_size;
7673 measure_disk_usage_invoke_progress (gpointer user_data)
7675 MeasureProgress *progress = user_data;
7677 (* progress->callback) (progress->reporting,
7678 progress->current_size, progress->num_dirs, progress->num_files,
7679 progress->user_data);
7685 measure_disk_usage_progress (gboolean reporting,
7686 guint64 current_size,
7691 MeasureProgress progress;
7692 GTask *task = user_data;
7693 MeasureTaskData *data;
7695 data = g_task_get_task_data (task);
7697 progress.callback = data->progress_callback;
7698 progress.user_data = data->progress_data;
7699 progress.reporting = reporting;
7700 progress.current_size = current_size;
7701 progress.num_dirs = num_dirs;
7702 progress.num_files = num_files;
7704 g_main_context_invoke_full (g_task_get_context (task),
7705 g_task_get_priority (task),
7706 measure_disk_usage_invoke_progress,
7707 g_memdup (&progress, sizeof progress),
7712 measure_disk_usage_thread (GTask *task,
7713 gpointer source_object,
7715 GCancellable *cancellable)
7717 MeasureTaskData *data = task_data;
7718 GError *error = NULL;
7719 MeasureResult result = { 0, };
7721 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7722 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7723 &result.disk_usage, &result.num_dirs, &result.num_files,
7725 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7727 g_task_return_error (task, error);
7731 g_file_real_measure_disk_usage_async (GFile *file,
7732 GFileMeasureFlags flags,
7734 GCancellable *cancellable,
7735 GFileMeasureProgressCallback progress_callback,
7736 gpointer progress_data,
7737 GAsyncReadyCallback callback,
7740 MeasureTaskData data;
7744 data.progress_callback = progress_callback;
7745 data.progress_data = progress_data;
7747 task = g_task_new (file, cancellable, callback, user_data);
7748 g_task_set_source_tag (task, g_file_real_measure_disk_usage_async);
7749 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7750 g_task_set_priority (task, io_priority);
7752 g_task_run_in_thread (task, measure_disk_usage_thread);
7753 g_object_unref (task);
7757 g_file_real_measure_disk_usage_finish (GFile *file,
7758 GAsyncResult *result,
7759 guint64 *disk_usage,
7764 MeasureResult *measure_result;
7766 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7768 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7770 if (measure_result == NULL)
7774 *disk_usage = measure_result->disk_usage;
7777 *num_dirs = measure_result->num_dirs;
7780 *num_files = measure_result->num_files;
7782 g_free (measure_result);
7788 * g_file_measure_disk_usage:
7790 * @flags: #GFileMeasureFlags
7791 * @cancellable: (nullable): optional #GCancellable
7792 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7793 * @progress_data: user_data for @progress_callback
7794 * @disk_usage: (out) (optional): the number of bytes of disk space used
7795 * @num_dirs: (out) (optional): the number of directories encountered
7796 * @num_files: (out) (optional): the number of non-directories encountered
7797 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7799 * Recursively measures the disk usage of @file.
7801 * This is essentially an analog of the 'du' command, but it also
7802 * reports the number of directories and non-directory files encountered
7803 * (including things like symbolic links).
7805 * By default, errors are only reported against the toplevel file
7806 * itself. Errors found while recursing are silently ignored, unless
7807 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7809 * The returned size, @disk_usage, is in bytes and should be formatted
7810 * with g_format_size() in order to get something reasonable for showing
7811 * in a user interface.
7813 * @progress_callback and @progress_data can be given to request
7814 * periodic progress updates while scanning. See the documentation for
7815 * #GFileMeasureProgressCallback for information about when and how the
7816 * callback will be invoked.
7818 * Returns: %TRUE if successful, with the out parameters set.
7819 * %FALSE otherwise, with @error set.
7824 g_file_measure_disk_usage (GFile *file,
7825 GFileMeasureFlags flags,
7826 GCancellable *cancellable,
7827 GFileMeasureProgressCallback progress_callback,
7828 gpointer progress_data,
7829 guint64 *disk_usage,
7834 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7835 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7836 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7838 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7839 progress_callback, progress_data,
7840 disk_usage, num_dirs, num_files,
7845 * g_file_measure_disk_usage_async:
7847 * @flags: #GFileMeasureFlags
7848 * @io_priority: the [I/O priority][io-priority] of the request
7849 * @cancellable: (nullable): optional #GCancellable
7850 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7851 * @progress_data: user_data for @progress_callback
7852 * @callback: (nullable): a #GAsyncReadyCallback to call when complete
7853 * @user_data: the data to pass to callback function
7855 * Recursively measures the disk usage of @file.
7857 * This is the asynchronous version of g_file_measure_disk_usage(). See
7858 * there for more information.
7863 g_file_measure_disk_usage_async (GFile *file,
7864 GFileMeasureFlags flags,
7866 GCancellable *cancellable,
7867 GFileMeasureProgressCallback progress_callback,
7868 gpointer progress_data,
7869 GAsyncReadyCallback callback,
7872 g_return_if_fail (G_IS_FILE (file));
7873 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7875 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7876 progress_callback, progress_data,
7877 callback, user_data);
7881 * g_file_measure_disk_usage_finish:
7883 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7884 * @disk_usage: (out) (optional): the number of bytes of disk space used
7885 * @num_dirs: (out) (optional): the number of directories encountered
7886 * @num_files: (out) (optional): the number of non-directories encountered
7887 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7889 * Collects the results from an earlier call to
7890 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7893 * Returns: %TRUE if successful, with the out parameters set.
7894 * %FALSE otherwise, with @error set.
7899 g_file_measure_disk_usage_finish (GFile *file,
7900 GAsyncResult *result,
7901 guint64 *disk_usage,
7906 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7907 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7909 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7913 * g_file_start_mountable:
7914 * @file: input #GFile
7915 * @flags: flags affecting the operation
7916 * @start_operation: (nullable): a #GMountOperation, or %NULL to avoid user interaction
7917 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
7918 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7919 * @user_data: the data to pass to callback function
7921 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7922 * Using @start_operation, you can request callbacks when, for instance,
7923 * passwords are needed during authentication.
7925 * If @cancellable is not %NULL, then the operation can be cancelled by
7926 * triggering the cancellable object from another thread. If the operation
7927 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7929 * When the operation is finished, @callback will be called.
7930 * You can then call g_file_mount_mountable_finish() to get
7931 * the result of the operation.
7936 g_file_start_mountable (GFile *file,
7937 GDriveStartFlags flags,
7938 GMountOperation *start_operation,
7939 GCancellable *cancellable,
7940 GAsyncReadyCallback callback,
7945 g_return_if_fail (G_IS_FILE (file));
7947 iface = G_FILE_GET_IFACE (file);
7949 if (iface->start_mountable == NULL)
7951 g_task_report_new_error (file, callback, user_data,
7952 g_file_start_mountable,
7953 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7954 _("Operation not supported"));
7958 (* iface->start_mountable) (file,
7967 * g_file_start_mountable_finish:
7968 * @file: input #GFile
7969 * @result: a #GAsyncResult
7970 * @error: a #GError, or %NULL
7972 * Finishes a start operation. See g_file_start_mountable() for details.
7974 * Finish an asynchronous start operation that was started
7975 * with g_file_start_mountable().
7977 * Returns: %TRUE if the operation finished successfully. %FALSE
7983 g_file_start_mountable_finish (GFile *file,
7984 GAsyncResult *result,
7989 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7990 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7992 if (g_async_result_legacy_propagate_error (result, error))
7994 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7995 return g_task_propagate_boolean (G_TASK (result), error);
7997 iface = G_FILE_GET_IFACE (file);
7998 return (* iface->start_mountable_finish) (file, result, error);
8002 * g_file_stop_mountable:
8003 * @file: input #GFile
8004 * @flags: flags affecting the operation
8005 * @mount_operation: (nullable): a #GMountOperation,
8006 * or %NULL to avoid user interaction.
8007 * @cancellable: (nullable): optional #GCancellable object,
8009 * @callback: (nullable): a #GAsyncReadyCallback to call
8010 * when the request is satisfied, or %NULL
8011 * @user_data: the data to pass to callback function
8013 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
8015 * If @cancellable is not %NULL, then the operation can be cancelled by
8016 * triggering the cancellable object from another thread. If the operation
8017 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8019 * When the operation is finished, @callback will be called.
8020 * You can then call g_file_stop_mountable_finish() to get
8021 * the result of the operation.
8026 g_file_stop_mountable (GFile *file,
8027 GMountUnmountFlags flags,
8028 GMountOperation *mount_operation,
8029 GCancellable *cancellable,
8030 GAsyncReadyCallback callback,
8035 g_return_if_fail (G_IS_FILE (file));
8037 iface = G_FILE_GET_IFACE (file);
8039 if (iface->stop_mountable == NULL)
8041 g_task_report_new_error (file, callback, user_data,
8042 g_file_stop_mountable,
8043 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8044 _("Operation not supported"));
8048 (* iface->stop_mountable) (file,
8057 * g_file_stop_mountable_finish:
8058 * @file: input #GFile
8059 * @result: a #GAsyncResult
8060 * @error: a #GError, or %NULL
8062 * Finishes an stop operation, see g_file_stop_mountable() for details.
8064 * Finish an asynchronous stop operation that was started
8065 * with g_file_stop_mountable().
8067 * Returns: %TRUE if the operation finished successfully.
8073 g_file_stop_mountable_finish (GFile *file,
8074 GAsyncResult *result,
8079 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8080 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8082 if (g_async_result_legacy_propagate_error (result, error))
8084 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
8085 return g_task_propagate_boolean (G_TASK (result), error);
8087 iface = G_FILE_GET_IFACE (file);
8088 return (* iface->stop_mountable_finish) (file, result, error);
8092 * g_file_poll_mountable:
8093 * @file: input #GFile
8094 * @cancellable: optional #GCancellable object, %NULL to ignore
8095 * @callback: (nullable): a #GAsyncReadyCallback to call
8096 * when the request is satisfied, or %NULL
8097 * @user_data: the data to pass to callback function
8099 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
8101 * If @cancellable is not %NULL, then the operation can be cancelled by
8102 * triggering the cancellable object from another thread. If the operation
8103 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8105 * When the operation is finished, @callback will be called.
8106 * You can then call g_file_mount_mountable_finish() to get
8107 * the result of the operation.
8112 g_file_poll_mountable (GFile *file,
8113 GCancellable *cancellable,
8114 GAsyncReadyCallback callback,
8119 g_return_if_fail (G_IS_FILE (file));
8121 iface = G_FILE_GET_IFACE (file);
8123 if (iface->poll_mountable == NULL)
8125 g_task_report_new_error (file, callback, user_data,
8126 g_file_poll_mountable,
8127 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8128 _("Operation not supported"));
8132 (* iface->poll_mountable) (file,
8139 * g_file_poll_mountable_finish:
8140 * @file: input #GFile
8141 * @result: a #GAsyncResult
8142 * @error: a #GError, or %NULL
8144 * Finishes a poll operation. See g_file_poll_mountable() for details.
8146 * Finish an asynchronous poll operation that was polled
8147 * with g_file_poll_mountable().
8149 * Returns: %TRUE if the operation finished successfully. %FALSE
8155 g_file_poll_mountable_finish (GFile *file,
8156 GAsyncResult *result,
8161 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8162 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8164 if (g_async_result_legacy_propagate_error (result, error))
8166 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
8167 return g_task_propagate_boolean (G_TASK (result), error);
8169 iface = G_FILE_GET_IFACE (file);
8170 return (* iface->poll_mountable_finish) (file, result, error);
8174 * g_file_supports_thread_contexts:
8177 * Checks if @file supports
8178 * [thread-default contexts][g-main-context-push-thread-default-context].
8179 * If this returns %FALSE, you cannot perform asynchronous operations on
8180 * @file in a thread that has a thread-default context.
8182 * Returns: Whether or not @file supports thread-default contexts.
8187 g_file_supports_thread_contexts (GFile *file)
8191 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8193 iface = G_FILE_GET_IFACE (file);
8194 return iface->supports_thread_contexts;
8198 * g_file_load_bytes:
8200 * @cancellable: (nullable): a #GCancellable or %NULL
8201 * @etag_out: (out) (nullable) (optional): a location to place the current
8202 * entity tag for the file, or %NULL if the entity tag is not needed
8203 * @error: a location for a #GError or %NULL
8205 * Loads the contents of @file and returns it as #GBytes.
8207 * If @file is a resource:// based URI, the resulting bytes will reference the
8208 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8209 * g_file_load_contents() and g_bytes_new_take().
8211 * For resources, @etag_out will be set to %NULL.
8213 * The data contained in the resulting #GBytes is always zero-terminated, but
8214 * this is not included in the #GBytes length. The resulting #GBytes should be
8215 * freed with g_bytes_unref() when no longer in use.
8217 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8222 g_file_load_bytes (GFile *file,
8223 GCancellable *cancellable,
8230 g_return_val_if_fail (G_IS_FILE (file), NULL);
8231 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
8232 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8234 if (etag_out != NULL)
8237 if (g_file_has_uri_scheme (file, "resource"))
8240 gchar *uri, *unescaped;
8242 uri = g_file_get_uri (file);
8243 unescaped = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
8246 bytes = g_resources_lookup_data (unescaped, G_RESOURCE_LOOKUP_FLAGS_NONE, error);
8252 /* contents is guaranteed to be \0 terminated */
8253 if (g_file_load_contents (file, cancellable, &contents, &len, etag_out, error))
8254 return g_bytes_new_take (g_steal_pointer (&contents), len);
8260 g_file_load_bytes_cb (GObject *object,
8261 GAsyncResult *result,
8264 GFile *file = G_FILE (object);
8265 GTask *task = user_data;
8266 GError *error = NULL;
8268 gchar *contents = NULL;
8271 g_file_load_contents_finish (file, result, &contents, &len, &etag, &error);
8272 g_task_set_task_data (task, g_steal_pointer (&etag), g_free);
8275 g_task_return_error (task, g_steal_pointer (&error));
8277 g_task_return_pointer (task,
8278 g_bytes_new_take (g_steal_pointer (&contents), len),
8279 (GDestroyNotify)g_bytes_unref);
8281 g_object_unref (task);
8285 * g_file_load_bytes_async:
8287 * @cancellable: (nullable): a #GCancellable or %NULL
8288 * @callback: (scope async): a #GAsyncReadyCallback to call when the
8289 * request is satisfied
8290 * @user_data: (closure): the data to pass to callback function
8292 * Asynchronously loads the contents of @file as #GBytes.
8294 * If @file is a resource:// based URI, the resulting bytes will reference the
8295 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8296 * g_file_load_contents_async() and g_bytes_new_take().
8298 * @callback should call g_file_load_bytes_finish() to get the result of this
8299 * asynchronous operation.
8301 * See g_file_load_bytes() for more information.
8306 g_file_load_bytes_async (GFile *file,
8307 GCancellable *cancellable,
8308 GAsyncReadyCallback callback,
8311 GError *error = NULL;
8315 g_return_if_fail (G_IS_FILE (file));
8316 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
8318 task = g_task_new (file, cancellable, callback, user_data);
8319 g_task_set_source_tag (task, g_file_load_bytes_async);
8321 if (!g_file_has_uri_scheme (file, "resource"))
8323 g_file_load_contents_async (file,
8325 g_file_load_bytes_cb,
8326 g_steal_pointer (&task));
8330 bytes = g_file_load_bytes (file, cancellable, NULL, &error);
8333 g_task_return_error (task, g_steal_pointer (&error));
8335 g_task_return_pointer (task,
8336 g_steal_pointer (&bytes),
8337 (GDestroyNotify)g_bytes_unref);
8339 g_object_unref (task);
8343 * g_file_load_bytes_finish:
8345 * @result: a #GAsyncResult provided to the callback
8346 * @etag_out: (out) (nullable) (optional): a location to place the current
8347 * entity tag for the file, or %NULL if the entity tag is not needed
8348 * @error: a location for a #GError, or %NULL
8350 * Completes an asynchronous request to g_file_load_bytes_async().
8352 * For resources, @etag_out will be set to %NULL.
8354 * The data contained in the resulting #GBytes is always zero-terminated, but
8355 * this is not included in the #GBytes length. The resulting #GBytes should be
8356 * freed with g_bytes_unref() when no longer in use.
8358 * See g_file_load_bytes() for more information.
8360 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8365 g_file_load_bytes_finish (GFile *file,
8366 GAsyncResult *result,
8372 g_return_val_if_fail (G_IS_FILE (file), NULL);
8373 g_return_val_if_fail (G_IS_TASK (result), NULL);
8374 g_return_val_if_fail (g_task_is_valid (G_TASK (result), file), NULL);
8375 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8377 bytes = g_task_propagate_pointer (G_TASK (result), error);
8379 if (etag_out != NULL)
8380 *etag_out = g_strdup (g_task_get_task_data (G_TASK (result)));